Some minor improvements to table code
This commit is contained in:
@@ -7,26 +7,27 @@
|
||||
|
||||
bool table_equal(LuaStack &LS, LuaSlot t1, LuaSlot t2) {
|
||||
lua_State *L = LS.state();
|
||||
int top = lua_gettop(L);
|
||||
LS.checktable(t1);
|
||||
LS.checktable(t2);
|
||||
int nkeys1 = lua_nkeys(L, t1.index());
|
||||
int nkeys2 = lua_nkeys(L, t2.index());
|
||||
if (nkeys1 != nkeys2) return false;
|
||||
int total = 0;
|
||||
lua_pushnil(L);
|
||||
int total1 = 0;
|
||||
while (lua_next(L, t1.index()) != 0) {
|
||||
lua_pushvalue(L, -2); // k v1 k
|
||||
lua_rawget(L, t2.index()); // k v1 v2
|
||||
if (!lua_rawequal(L, -1, -2)) {
|
||||
lua_settop(L, top);
|
||||
return false;
|
||||
}
|
||||
lua_pop(L, 2);
|
||||
total1 += 1;
|
||||
total += 1;
|
||||
}
|
||||
int total2 = 0;
|
||||
lua_pushnil(L);
|
||||
while (lua_next(L, t2.index()) != 0) {
|
||||
lua_pop(L, 1);
|
||||
total2 += 1;
|
||||
}
|
||||
return total1 == total2;
|
||||
assert(total == nkeys1);
|
||||
lua_settop(L, top);
|
||||
return true;
|
||||
}
|
||||
|
||||
LuaDefine(table_equal, "table1,table2", "return true if two tables contain the same keys and values") {
|
||||
@@ -103,7 +104,7 @@ LuaDefine(table_find, "vector,value", "find the first occurrence of value in vec
|
||||
}
|
||||
}
|
||||
|
||||
LuaDefine(table_empty, "table", "return true if the table is empty") {
|
||||
LuaDefine(table_empty, "table", "return true if the table has zero keys") {
|
||||
luaL_checktype(L, -1, LUA_TTABLE);
|
||||
int total = lua_nkeys(L, -1);
|
||||
lua_pushboolean(L, (total == 0)?1:0);
|
||||
|
||||
Reference in New Issue
Block a user