summaryrefslogtreecommitdiff
path: root/src/ltable.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ltable.c')
-rw-r--r--src/ltable.c101
1 files changed, 49 insertions, 52 deletions
diff --git a/src/ltable.c b/src/ltable.c
index ec84f4fa..78ff1d74 100644
--- a/src/ltable.c
+++ b/src/ltable.c
@@ -1,5 +1,5 @@
/*
-** $Id: ltable.c,v 2.32.1.2 2007/12/28 15:32:23 roberto Exp $
+** $Id: ltable.c,v 2.47 2009/12/17 15:46:44 roberto Exp $
** Lua tables (hash)
** See Copyright Notice in lua.h
*/
@@ -18,7 +18,6 @@
** Hence even when the load factor reaches 100%, performance remains good.
*/
-#include <math.h>
#include <string.h>
#define ltable_c
@@ -48,7 +47,7 @@
#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
-
+
#define hashstr(t,str) hashpow2(t, (str)->tsv.hash)
#define hashboolean(t,p) hashpow2(t, p)
@@ -72,9 +71,11 @@
#define dummynode (&dummynode_)
+#define isdummy(n) ((n) == dummynode)
+
static const Node dummynode_ = {
- {{NULL}, LUA_TNIL}, /* value */
- {{{NULL}, LUA_TNIL, NULL}} /* key */
+ {NILCONSTANT}, /* value */
+ {{NILCONSTANT, NULL}} /* key */
};
@@ -82,13 +83,13 @@ static const Node dummynode_ = {
** hash for lua_Numbers
*/
static Node *hashnum (const Table *t, lua_Number n) {
- unsigned int a[numints];
int i;
- if (luai_numeq(n, 0)) /* avoid problems with -0 */
- return gnode(t, 0);
- memcpy(a, &n, sizeof(a));
- for (i = 1; i < numints; i++) a[0] += a[i];
- return hashmod(t, a[0]);
+ luai_hashnum(i, n);
+ if (i < 0) {
+ i = -i; /* must be a positive value */
+ if (i < 0) i = 0; /* handle INT_MIN */
+ }
+ return hashmod(t, i);
}
@@ -132,7 +133,7 @@ static int arrayindex (const TValue *key) {
/*
** returns the index of a `key' for table traversals. First goes all
** elements in the array part, then elements in the hash part. The
-** beginning of a traversal is signalled by -1.
+** beginning of a traversal is signaled by -1.
*/
static int findindex (lua_State *L, Table *t, StkId key) {
int i;
@@ -211,7 +212,7 @@ static int computesizes (int nums[], int *narray) {
static int countint (const TValue *key, int *nums) {
int k = arrayindex(key);
if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */
- nums[ceillog2(k)]++; /* count as such */
+ nums[luaO_ceillog2(k)]++; /* count as such */
return 1;
}
else
@@ -277,7 +278,7 @@ static void setnodevector (lua_State *L, Table *t, int size) {
}
else {
int i;
- lsize = ceillog2(size);
+ lsize = luaO_ceillog2(size);
if (lsize > MAXBITS)
luaG_runerror(L, "table overflow");
size = twoto(lsize);
@@ -294,7 +295,7 @@ static void setnodevector (lua_State *L, Table *t, int size) {
}
-static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
+void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) {
int i;
int oldasize = t->sizearray;
int oldhsize = t->lsizenode;
@@ -302,13 +303,13 @@ static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
if (nasize > oldasize) /* array part must grow? */
setarrayvector(L, t, nasize);
/* create new hash part with appropriate size */
- setnodevector(L, t, nhsize);
+ setnodevector(L, t, nhsize);
if (nasize < oldasize) { /* array part must shrink? */
t->sizearray = nasize;
/* re-insert elements from vanishing slice */
for (i=nasize; i<oldasize; i++) {
if (!ttisnil(&t->array[i]))
- setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]);
+ setobjt2t(L, luaH_setint(L, t, i+1), &t->array[i]);
}
/* shrink array */
luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
@@ -319,14 +320,14 @@ 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 != dummynode)
- luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */
+ if (!isdummy(nold))
+ luaM_freearray(L, nold, twoto(oldhsize)); /* free old array */
}
void luaH_resizearray (lua_State *L, Table *t, int nasize) {
- int nsize = (t->node == dummynode) ? 0 : sizenode(t);
- resize(L, t, nasize, nsize);
+ int nsize = isdummy(t->node) ? 0 : sizenode(t);
+ luaH_resize(L, t, nasize, nsize);
}
@@ -345,7 +346,7 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) {
/* compute new size for array part */
na = computesizes(nums, &nasize);
/* resize the table to new computed sizes */
- resize(L, t, nasize, totaluse - na);
+ luaH_resize(L, t, nasize, totaluse - na);
}
@@ -355,32 +356,28 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) {
*/
-Table *luaH_new (lua_State *L, int narray, int nhash) {
- Table *t = luaM_new(L, Table);
- luaC_link(L, obj2gco(t), LUA_TTABLE);
+Table *luaH_new (lua_State *L) {
+ Table *t = &luaC_newobj(L, LUA_TTABLE, sizeof(Table), NULL, 0)->h;
t->metatable = NULL;
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 *, dummynode);
- setarrayvector(L, t, narray);
- setnodevector(L, t, nhash);
+ setnodevector(L, t, 0);
return t;
}
void luaH_free (lua_State *L, Table *t) {
- if (t->node != dummynode)
- luaM_freearray(L, t->node, sizenode(t), Node);
- luaM_freearray(L, t->array, t->sizearray, TValue);
+ if (!isdummy(t->node))
+ luaM_freearray(L, t->node, sizenode(t));
+ luaM_freearray(L, t->array, t->sizearray);
luaM_free(L, t);
}
static Node *getfreepos (Table *t) {
- while (t->lastfree-- > t->node) {
+ while (t->lastfree > t->node) {
+ t->lastfree--;
if (ttisnil(gkey(t->lastfree)))
return t->lastfree;
}
@@ -390,22 +387,22 @@ static Node *getfreepos (Table *t) {
/*
-** inserts a new key into a hash table; first, check whether key's main
-** position is free. If not, check whether colliding node is in its main
-** position or not: if it is not, move colliding node to an empty place and
-** put new key in its main position; otherwise (colliding node is in its main
-** position), new key goes to an empty position.
+** inserts a new key into a hash table; first, check whether key's main
+** position is free. If not, check whether colliding node is in its main
+** position or not: if it is not, move colliding node to an empty place and
+** put new key in its main position; otherwise (colliding node is in its main
+** position), new key goes to an empty position.
*/
static TValue *newkey (lua_State *L, Table *t, const TValue *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 *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 != dummynode);
+ lua_assert(!isdummy(n));
othern = mainposition(t, key2tval(mp));
if (othern != mp) { /* is colliding node out of its main position? */
/* yes; move colliding node into free position */
@@ -422,7 +419,7 @@ static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
mp = n;
}
}
- gkey(mp)->value = key->value; gkey(mp)->tt = key->tt;
+ setobj2t(L, gkey(mp), key);
luaC_barriert(L, t, key);
lua_assert(ttisnil(gval(mp)));
return gval(mp);
@@ -432,7 +429,7 @@ static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
/*
** search function for integers
*/
-const TValue *luaH_getnum (Table *t, int key) {
+const TValue *luaH_getint (Table *t, int key) {
/* (1 <= key && key <= t->sizearray) */
if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray))
return &t->array[key-1];
@@ -475,7 +472,7 @@ const TValue *luaH_get (Table *t, const TValue *key) {
lua_Number n = nvalue(key);
lua_number2int(k, n);
if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */
- return luaH_getnum(t, k); /* use specialized version */
+ return luaH_getint(t, k); /* use specialized version */
/* else go through */
}
default: {
@@ -498,15 +495,15 @@ TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
return cast(TValue *, p);
else {
if (ttisnil(key)) luaG_runerror(L, "table index is nil");
- else if (ttisnumber(key) && luai_numisnan(nvalue(key)))
+ else if (ttisnumber(key) && luai_numisnan(L, nvalue(key)))
luaG_runerror(L, "table index is NaN");
return newkey(L, t, key);
}
}
-TValue *luaH_setnum (lua_State *L, Table *t, int key) {
- const TValue *p = luaH_getnum(t, key);
+TValue *luaH_setint (lua_State *L, Table *t, int key) {
+ const TValue *p = luaH_getint(t, key);
if (p != luaO_nilobject)
return cast(TValue *, p);
else {
@@ -533,20 +530,20 @@ static int unbound_search (Table *t, unsigned int j) {
unsigned int i = j; /* i is zero or a present index */
j++;
/* find `i' and `j' such that i is present and j is not */
- while (!ttisnil(luaH_getnum(t, j))) {
+ while (!ttisnil(luaH_getint(t, j))) {
i = j;
j *= 2;
if (j > cast(unsigned int, MAX_INT)) { /* overflow? */
/* table was built with bad purposes: resort to linear search */
i = 1;
- while (!ttisnil(luaH_getnum(t, i))) i++;
+ while (!ttisnil(luaH_getint(t, i))) i++;
return i - 1;
}
}
/* now do a binary search between them */
while (j - i > 1) {
unsigned int m = (i+j)/2;
- if (ttisnil(luaH_getnum(t, m))) j = m;
+ if (ttisnil(luaH_getint(t, m))) j = m;
else i = m;
}
return i;
@@ -570,7 +567,7 @@ int luaH_getn (Table *t) {
return i;
}
/* 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... */
else return unbound_search(t, j);
}
@@ -583,6 +580,6 @@ Node *luaH_mainposition (const Table *t, const TValue *key) {
return mainposition(t, key);
}
-int luaH_isdummy (Node *n) { return n == dummynode; }
+int luaH_isdummy (Node *n) { return isdummy(n); }
#endif