1
0
mirror of https://github.com/lua/lua.git synced 2025-02-04 06:13:04 +08:00

new macro 'isdummy'

This commit is contained in:
Roberto Ierusalimschy 2009-11-19 15:54:07 -02:00
parent 1dea393058
commit 2e5ef6a9fb

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.c,v 2.43 2009/11/05 17:43:54 roberto Exp roberto $ ** $Id: ltable.c,v 2.44 2009/11/06 17:07:12 roberto Exp roberto $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -71,6 +71,8 @@
#define dummynode (&dummynode_) #define dummynode (&dummynode_)
#define isdummy(n) ((n) == dummynode)
static const Node dummynode_ = { static const Node dummynode_ = {
{NILCONSTANT}, /* value */ {NILCONSTANT}, /* value */
{{NILCONSTANT, NULL}} /* key */ {{NILCONSTANT, NULL}} /* key */
@ -318,13 +320,13 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) {
if (!ttisnil(gval(old))) if (!ttisnil(gval(old)))
setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old)); setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old));
} }
if (nold != dummynode) if (!isdummy(nold))
luaM_freearray(L, nold, twoto(oldhsize)); /* free old array */ luaM_freearray(L, nold, twoto(oldhsize)); /* free old array */
} }
void luaH_resizearray (lua_State *L, Table *t, int nasize) { void luaH_resizearray (lua_State *L, Table *t, int nasize) {
int nsize = (t->node == dummynode) ? 0 : sizenode(t); int nsize = isdummy(t->node) ? 0 : sizenode(t);
luaH_resize(L, t, nasize, nsize); luaH_resize(L, t, nasize, nsize);
} }
@ -367,7 +369,7 @@ Table *luaH_new (lua_State *L) {
void luaH_free (lua_State *L, Table *t) { void luaH_free (lua_State *L, Table *t) {
if (t->node != dummynode) if (!isdummy(t->node))
luaM_freearray(L, t->node, sizenode(t)); luaM_freearray(L, t->node, sizenode(t));
luaM_freearray(L, t->array, t->sizearray); luaM_freearray(L, t->array, t->sizearray);
luaM_free(L, t); luaM_free(L, t);
@ -394,14 +396,14 @@ static Node *getfreepos (Table *t) {
*/ */
static TValue *newkey (lua_State *L, Table *t, const TValue *key) { static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
Node *mp = mainposition(t, key); Node *mp = mainposition(t, key);
if (!ttisnil(gval(mp)) || mp == dummynode) { if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */
Node *othern; Node *othern;
Node *n = getfreepos(t); /* get a free place */ Node *n = getfreepos(t); /* get a free place */
if (n == NULL) { /* cannot find a free place? */ if (n == NULL) { /* cannot find a free place? */
rehash(L, t, key); /* grow table */ rehash(L, t, key); /* grow table */
return luaH_set(L, t, key); /* re-insert key into grown table */ return luaH_set(L, t, key); /* re-insert key into grown table */
} }
lua_assert(n != dummynode); lua_assert(!isdummy(n));
othern = mainposition(t, key2tval(mp)); othern = mainposition(t, key2tval(mp));
if (othern != mp) { /* is colliding node out of its main position? */ if (othern != mp) { /* is colliding node out of its main position? */
/* yes; move colliding node into free position */ /* yes; move colliding node into free position */
@ -566,7 +568,7 @@ int luaH_getn (Table *t) {
return i; return i;
} }
/* else must find a boundary in hash part */ /* else must find a boundary in hash part */
else if (t->node == dummynode) /* hash part is empty? */ else if (isdummy(t->node)) /* hash part is empty? */
return j; /* that is easy... */ return j; /* that is easy... */
else return unbound_search(t, j); else return unbound_search(t, j);
} }
@ -579,6 +581,6 @@ Node *luaH_mainposition (const Table *t, const TValue *key) {
return mainposition(t, key); return mainposition(t, key);
} }
int luaH_isdummy (Node *n) { return n == dummynode; } int luaH_isdummy (Node *n) { return isdummy(n); }
#endif #endif