feat(util/name_set): add mk_unique (with respect to a name_set)
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
parent
6da13cc245
commit
7ff791eb9f
4 changed files with 40 additions and 3 deletions
|
@ -9,6 +9,7 @@ Author: Leonardo de Moura
|
||||||
#include "util/test.h"
|
#include "util/test.h"
|
||||||
#include "util/name.h"
|
#include "util/name.h"
|
||||||
#include "util/name_generator.h"
|
#include "util/name_generator.h"
|
||||||
|
#include "util/name_set.h"
|
||||||
using namespace lean;
|
using namespace lean;
|
||||||
|
|
||||||
static void tst1() {
|
static void tst1() {
|
||||||
|
@ -146,6 +147,16 @@ static void tst11() {
|
||||||
lean_assert(n2 == name("a"));
|
lean_assert(n2 == name("a"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void tst12() {
|
||||||
|
name_set s;
|
||||||
|
s.insert(name("foo"));
|
||||||
|
s.insert(name(name("foo"), 1));
|
||||||
|
s.insert(name("bla"));
|
||||||
|
lean_assert(mk_unique(s, name("boo")) == name("boo"));
|
||||||
|
lean_assert(mk_unique(s, name("bla")) == name(name("bla"), 1));
|
||||||
|
lean_assert(mk_unique(s, name("foo")) == name(name("foo"), 2));
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
tst1();
|
tst1();
|
||||||
tst2();
|
tst2();
|
||||||
|
@ -158,5 +169,6 @@ int main() {
|
||||||
tst9();
|
tst9();
|
||||||
tst10();
|
tst10();
|
||||||
tst11();
|
tst11();
|
||||||
|
tst12();
|
||||||
return has_violations() ? 1 : 0;
|
return has_violations() ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
add_library(util trace.cpp debug.cpp name.cpp exception.cpp
|
add_library(util trace.cpp debug.cpp name.cpp name_set.cpp
|
||||||
interrupt.cpp hash.cpp escaped.cpp bit_tricks.cpp safe_arith.cpp
|
exception.cpp interrupt.cpp hash.cpp escaped.cpp bit_tricks.cpp
|
||||||
ascii.cpp memory.cpp shared_mutex.cpp realpath.cpp
|
safe_arith.cpp ascii.cpp memory.cpp shared_mutex.cpp realpath.cpp
|
||||||
script_state.cpp script_exception.cpp splay_map.cpp lua.cpp
|
script_state.cpp script_exception.cpp splay_map.cpp lua.cpp
|
||||||
luaref.cpp)
|
luaref.cpp)
|
||||||
|
|
||||||
|
|
20
src/util/name_set.cpp
Normal file
20
src/util/name_set.cpp
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
/*
|
||||||
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
||||||
|
Released under Apache 2.0 license as described in the file LICENSE.
|
||||||
|
|
||||||
|
Author: Leonardo de Moura
|
||||||
|
*/
|
||||||
|
#include "util/name_set.h"
|
||||||
|
|
||||||
|
namespace lean {
|
||||||
|
name mk_unique(name_set const & s, name const & suggestion) {
|
||||||
|
name n = suggestion;
|
||||||
|
int i = 1;
|
||||||
|
while (true) {
|
||||||
|
if (s.find(n) == s.end())
|
||||||
|
return n;
|
||||||
|
n = name(suggestion, i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,4 +9,9 @@ Author: Leonardo de Moura
|
||||||
#include "util/name.h"
|
#include "util/name.h"
|
||||||
namespace lean {
|
namespace lean {
|
||||||
typedef std::unordered_set<name, name_hash, name_eq> name_set;
|
typedef std::unordered_set<name, name_hash, name_eq> name_set;
|
||||||
|
/**
|
||||||
|
\brief Make a name that does not occur in \c s, based on
|
||||||
|
the given suggestion.
|
||||||
|
*/
|
||||||
|
name mk_unique(name_set const & s, name const & suggestion);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue