diff options
Diffstat (limited to 'src/ltable.c')
-rw-r--r-- | src/ltable.c | 92 |
1 files changed, 53 insertions, 39 deletions
diff --git a/src/ltable.c b/src/ltable.c index 5d4a828a..029cd506 100644 --- a/src/ltable.c +++ b/src/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.27 2005/10/24 17:37:52 roberto Exp $ +** $Id: ltable.c,v 2.31 2006/01/10 13:13:06 roberto Exp $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -66,13 +66,15 @@ /* ** number of ints inside a lua_Number */ -#define numints cast(int, sizeof(lua_Number)/sizeof(int)) +#define numints cast_int(sizeof(lua_Number)/sizeof(int)) -const Node luaH_dummynode = { +#define dummynode (&dummynode_) + +static const Node dummynode_ = { {{NULL}, LUA_TNIL}, /* value */ - {{NULL}, LUA_TNIL, NULL} /* key */ + {{{NULL}, LUA_TNIL, NULL}} /* key */ }; @@ -95,7 +97,7 @@ static Node *hashnum (const Table *t, lua_Number n) { ** returns the `main' position of an element in a table (that is, the index ** of its hash value) */ -Node *luaH_mainposition (const Table *t, const TValue *key) { +static Node *mainposition (const Table *t, const TValue *key) { switch (ttype(key)) { case LUA_TNUMBER: return hashnum(t, nvalue(key)); @@ -120,7 +122,7 @@ static int arrayindex (const TValue *key) { lua_Number n = nvalue(key); int k; lua_number2int(k, n); - if (luai_numeq(cast(lua_Number, k), nvalue(key))) + if (luai_numeq(cast_num(k), nvalue(key))) return k; } return -1; /* `key' did not match some condition */ @@ -139,13 +141,13 @@ static int findindex (lua_State *L, Table *t, StkId key) { if (0 < i && i <= t->sizearray) /* is `key' inside array part? */ return i-1; /* yes; that's the index (corrected to C) */ else { - Node *n = luaH_mainposition(t, key); + Node *n = mainposition(t, key); do { /* check whether `key' is somewhere in the chain */ /* key may be dead already, but it is ok to use it in `next' */ if (luaO_rawequalObj(key2tval(n), key) || (ttype(gkey(n)) == LUA_TDEADKEY && iscollectable(key) && gcvalue(gkey(n)) == gcvalue(key))) { - i = cast(int, n - gnode(t, 0)); /* key index in hash table */ + i = cast_int(n - gnode(t, 0)); /* key index in hash table */ /* hash elements are numbered after array ones */ return i + t->sizearray; } @@ -161,7 +163,7 @@ int luaH_next (lua_State *L, Table *t, StkId key) { int i = findindex(L, t, key); /* find original element */ for (i++; i < t->sizearray; i++) { /* try first array part */ if (!ttisnil(&t->array[i])) { /* a non-nil value? */ - setnvalue(key, cast(lua_Number, i+1)); + setnvalue(key, cast_num(i+1)); setobj2s(L, key+1, &t->array[i]); return 1; } @@ -270,7 +272,7 @@ static void setarrayvector (lua_State *L, Table *t, int size) { static void setnodevector (lua_State *L, Table *t, int size) { int lsize; if (size == 0) { /* no elements to hash part? */ - t->node = cast(Node *, &luaH_dummynode); /* use common `dummynode' */ + t->node = cast(Node *, dummynode); /* use common `dummynode' */ lsize = 0; } else { @@ -281,12 +283,13 @@ static void setnodevector (lua_State *L, Table *t, int size) { size = twoto(lsize); t->node = luaM_newvector(L, size, Node); for (i=0; i<size; i++) { - gnext(&t->node[i]) = NULL; - setnilvalue(gkey(gnode(t, i))); - setnilvalue(gval(gnode(t, i))); + Node *n = gnode(t, i); + gnext(n) = NULL; + setnilvalue(gkey(n)); + setnilvalue(gval(n)); } } - t->lsizenode = cast(lu_byte, lsize); + t->lsizenode = cast_byte(lsize); t->lastfree = gnode(t, size); /* all positions are free */ } @@ -316,13 +319,13 @@ static void resize (lua_State *L, Table *t, int nasize, int nhsize) { if (!ttisnil(gval(old))) setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old)); } - if (nold != &luaH_dummynode) + if (nold != dummynode) luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */ } void luaH_resizearray (lua_State *L, Table *t, int nasize) { - int nsize = (t->node == &luaH_dummynode) ? 0 : sizenode(t); + int nsize = (t->node == dummynode) ? 0 : sizenode(t); resize(L, t, nasize, nsize); } @@ -356,12 +359,12 @@ Table *luaH_new (lua_State *L, int narray, int nhash) { Table *t = luaM_new(L, Table); luaC_link(L, obj2gco(t), LUA_TTABLE); t->metatable = NULL; - t->flags = cast(lu_byte, ~0); + t->flags = cast_byte(~0); /* temporary values (kept only if some malloc fails) */ t->array = NULL; t->sizearray = 0; t->lsizenode = 0; - t->node = cast(Node *, &luaH_dummynode); + t->node = cast(Node *, dummynode); setarrayvector(L, t, narray); setnodevector(L, t, nhash); return t; @@ -369,7 +372,7 @@ Table *luaH_new (lua_State *L, int narray, int nhash) { void luaH_free (lua_State *L, Table *t) { - if (t->node != &luaH_dummynode) + if (t->node != dummynode) luaM_freearray(L, t->node, sizenode(t), Node); luaM_freearray(L, t->array, t->sizearray, TValue); luaM_free(L, t); @@ -394,16 +397,16 @@ static Node *getfreepos (Table *t) { ** position), new key goes to an empty position. */ static TValue *newkey (lua_State *L, Table *t, const TValue *key) { - Node *mp = luaH_mainposition(t, key); - if (!ttisnil(gval(mp)) || mp == &luaH_dummynode) { + Node *mp = mainposition(t, key); + if (!ttisnil(gval(mp)) || mp == dummynode) { Node *othern; Node *n = getfreepos(t); /* get a free place */ if (n == NULL) { /* cannot find a free place? */ rehash(L, t, key); /* grow table */ return luaH_set(L, t, key); /* re-insert key into grown table */ } - lua_assert(n != &luaH_dummynode); - othern = luaH_mainposition(t, key2tval(mp)); + lua_assert(n != dummynode); + othern = mainposition(t, key2tval(mp)); if (othern != mp) { /* is colliding node out of its main position? */ /* yes; move colliding node into free position */ while (gnext(othern) != mp) othern = gnext(othern); /* find previous */ @@ -434,14 +437,14 @@ const TValue *luaH_getnum (Table *t, int key) { if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray)) return &t->array[key-1]; else { - lua_Number nk = cast(lua_Number, key); + lua_Number nk = cast_num(key); Node *n = hashnum(t, nk); do { /* check whether `key' is somewhere in the chain */ if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk)) return gval(n); /* that's it */ else n = gnext(n); } while (n); - return &luaO_nilobject; + return luaO_nilobject; } } @@ -456,7 +459,7 @@ const TValue *luaH_getstr (Table *t, TString *key) { return gval(n); /* that's it */ else n = gnext(n); } while (n); - return &luaO_nilobject; + return luaO_nilobject; } @@ -465,24 +468,24 @@ const TValue *luaH_getstr (Table *t, TString *key) { */ const TValue *luaH_get (Table *t, const TValue *key) { switch (ttype(key)) { - case LUA_TNIL: return &luaO_nilobject; + case LUA_TNIL: return luaO_nilobject; case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key)); case LUA_TNUMBER: { int k; lua_Number n = nvalue(key); lua_number2int(k, n); - if (luai_numeq(cast(lua_Number, k), nvalue(key))) /* index is int? */ + if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */ return luaH_getnum(t, k); /* use specialized version */ /* else go through */ } default: { - Node *n = luaH_mainposition(t, key); + Node *n = mainposition(t, key); do { /* check whether `key' is somewhere in the chain */ if (luaO_rawequalObj(key2tval(n), key)) return gval(n); /* that's it */ else n = gnext(n); } while (n); - return &luaO_nilobject; + return luaO_nilobject; } } } @@ -491,11 +494,11 @@ const TValue *luaH_get (Table *t, const TValue *key) { TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { const TValue *p = luaH_get(t, key); t->flags = 0; - if (p != &luaO_nilobject) + if (p != luaO_nilobject) return cast(TValue *, p); else { if (ttisnil(key)) luaG_runerror(L, "table index is nil"); - else if (ttisnumber(key) && !luai_numeq(nvalue(key), nvalue(key))) + else if (ttisnumber(key) && luai_numisnan(nvalue(key))) luaG_runerror(L, "table index is NaN"); return newkey(L, t, key); } @@ -504,11 +507,11 @@ TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { TValue *luaH_setnum (lua_State *L, Table *t, int key) { const TValue *p = luaH_getnum(t, key); - if (p != &luaO_nilobject) + if (p != luaO_nilobject) return cast(TValue *, p); else { TValue k; - setnvalue(&k, cast(lua_Number, key)); + setnvalue(&k, cast_num(key)); return newkey(L, t, &k); } } @@ -516,7 +519,7 @@ TValue *luaH_setnum (lua_State *L, Table *t, int key) { TValue *luaH_setstr (lua_State *L, Table *t, TString *key) { const TValue *p = luaH_getstr(t, key); - if (p != &luaO_nilobject) + if (p != luaO_nilobject) return cast(TValue *, p); else { TValue k; @@ -528,11 +531,11 @@ TValue *luaH_setstr (lua_State *L, Table *t, TString *key) { static int unbound_search (Table *t, unsigned int j) { unsigned int i = j; /* i is zero or a present index */ - j = j+1; + j++; /* find `i' and `j' such that i is present and j is not */ while (!ttisnil(luaH_getnum(t, j))) { i = j; - j = i*2; + j *= 2; if (j > cast(unsigned int, MAX_INT)) { /* overflow? */ /* table was built with bad purposes: resort to linear search */ i = 1; @@ -541,7 +544,7 @@ static int unbound_search (Table *t, unsigned int j) { } } /* now do a binary search between them */ - while (i < j-1) { + while (j - i > 1) { unsigned int m = (i+j)/2; if (ttisnil(luaH_getnum(t, m))) j = m; else i = m; @@ -567,8 +570,19 @@ int luaH_getn (Table *t) { return i; } /* else must find a boundary in hash part */ - else if (t->node == &luaH_dummynode) /* hash part is empty? */ + else if (t->node == dummynode) /* hash part is empty? */ return j; /* that is easy... */ else return unbound_search(t, j); } + + +#if defined(LUA_DEBUG) + +Node *luaH_mainposition (const Table *t, const TValue *key) { + return mainposition(t, key); +} + +int luaH_isdummy (Node *n) { return n == dummynode; } + +#endif |