feat(util): name_set Lua API
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
parent
7fe61bc69c
commit
1a8d75c4f0
4 changed files with 45 additions and 4 deletions
|
|
@ -17,4 +17,30 @@ name mk_unique(name_set const & s, name const & suggestion) {
|
|||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
DECL_UDATA(name_set)
|
||||
static int mk_name_set(lua_State * L) { return push_name_set(L, name_set()); }
|
||||
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}
|
||||
};
|
||||
static void name_set_migrate(lua_State * src, int i, lua_State * tgt) { push_name_set(tgt, to_name_set(src, i)); }
|
||||
|
||||
void open_name_set(lua_State * L) {
|
||||
luaL_newmetatable(L, name_set_mt);
|
||||
set_migrate_fn_field(L, -1, name_set_migrate);
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,11 +7,12 @@ Author: Leonardo de Moura
|
|||
#pragma once
|
||||
#include "util/rb_tree.h"
|
||||
#include "util/name.h"
|
||||
#include "util/lua.h"
|
||||
namespace lean {
|
||||
typedef rb_tree<name, name_quick_cmp> name_set;
|
||||
/**
|
||||
\brief Make a name that does not occur in \c s, based on
|
||||
the given suggestion.
|
||||
*/
|
||||
/** \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);
|
||||
|
||||
UDATA_DEFS_CORE(name_set)
|
||||
void open_name_set(lua_State * L);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ Author: Leonardo de Moura
|
|||
#include "util/script_exception.h"
|
||||
#include "util/name.h"
|
||||
#include "util/name_generator.h"
|
||||
#include "util/name_set.h"
|
||||
#include "util/rb_map.h"
|
||||
#include "util/lean_path.h"
|
||||
|
||||
|
|
@ -86,6 +87,7 @@ struct script_state::imp {
|
|||
open_exception(m_state);
|
||||
open_name(m_state);
|
||||
open_name_generator(m_state);
|
||||
open_name_set(m_state);
|
||||
open_rb_map(m_state);
|
||||
open_extra(m_state);
|
||||
|
||||
|
|
|
|||
12
tests/lua/ns1.lua
Normal file
12
tests/lua/ns1.lua
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
local s = name_set()
|
||||
assert(not s:contains("a"))
|
||||
s = s:insert("a")
|
||||
assert(s:contains("a"))
|
||||
s = s:insert("b")
|
||||
assert(s:contains("a"))
|
||||
assert(s:contains("b"))
|
||||
local s2 = s:erase("a")
|
||||
assert(s:contains("a"))
|
||||
assert(s:contains("b"))
|
||||
assert(not s2:contains("a"))
|
||||
assert(s2:contains("b"))
|
||||
Loading…
Add table
Reference in a new issue