2013-11-30 18:22:57 +00:00
|
|
|
/*
|
|
|
|
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) {
|
2014-04-22 18:09:13 +00:00
|
|
|
if (!s.contains(n))
|
2013-11-30 18:22:57 +00:00
|
|
|
return n;
|
|
|
|
n = name(suggestion, i);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 01:32:53 +00:00
|
|
|
|
|
|
|
DECL_UDATA(name_set)
|
2014-05-09 00:17:00 +00:00
|
|
|
static int mk_name_set(lua_State * L) {
|
|
|
|
name_set r;
|
|
|
|
int nargs = lua_gettop(L);
|
|
|
|
for (int i = 1; i <= nargs; i++)
|
|
|
|
r.insert(to_name_ext(L, i));
|
|
|
|
return push_name_set(L, r);
|
|
|
|
}
|
2014-05-08 01:32:53 +00:00
|
|
|
static int name_set_insert(lua_State * L) { return push_name_set(L, insert(to_name_set(L, 1), to_name_ext(L, 2))); }
|
|
|
|
static int name_set_contains(lua_State * L) { return push_boolean(L, to_name_set(L, 1).contains(to_name_ext(L, 2))); }
|
|
|
|
static int name_set_erase(lua_State * L) { return push_name_set(L, erase(to_name_set(L, 1), to_name_ext(L, 2))); }
|
|
|
|
|
|
|
|
static const struct luaL_Reg name_set_m[] = {
|
|
|
|
{"__gc", name_set_gc}, // never throws
|
|
|
|
{"insert", safe_function<name_set_insert>},
|
|
|
|
{"contains", safe_function<name_set_contains>},
|
|
|
|
{"erase", safe_function<name_set_erase>},
|
|
|
|
{0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
void open_name_set(lua_State * L) {
|
|
|
|
luaL_newmetatable(L, name_set_mt);
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_setfield(L, -2, "__index");
|
|
|
|
setfuncs(L, name_set_m, 0);
|
|
|
|
|
|
|
|
SET_GLOBAL_FUN(mk_name_set, "name_set");
|
|
|
|
SET_GLOBAL_FUN(name_set_pred, "is_name_set");
|
|
|
|
}
|
2013-11-30 18:22:57 +00:00
|
|
|
}
|