summaryrefslogtreecommitdiff
path: root/src/ltable.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ltable.c')
-rw-r--r--src/ltable.c109
1 files changed, 61 insertions, 48 deletions
diff --git a/src/ltable.c b/src/ltable.c
index 0c64adb1..9fc508ba 100644
--- a/src/ltable.c
+++ b/src/ltable.c
@@ -1,5 +1,5 @@
/*
-** $Id: ltable.c,v 1.132 2003/04/03 13:35:34 roberto Exp $
+** $Id: ltable.c,v 2.1 2003/12/10 12:13:36 roberto Exp $
** Lua tables (hash)
** See Copyright Notice in lua.h
*/
@@ -15,10 +15,7 @@
** A main invariant of these tables is that, if an element is not
** in its main position (i.e. the `original' position that its hash gives
** to it), then the colliding element is in its own main position.
-** In other words, there are collisions only when two elements have the
-** same main position (i.e. the same hash values for that table size).
-** Because of that, the load factor of these tables can be 100% without
-** performance penalties.
+** Hence even when the load factor reaches 100%, performance remains good.
*/
#include <string.h>
@@ -45,9 +42,7 @@
#define MAXBITS (BITS_INT-2)
#endif
-/* check whether `x' < 2^MAXBITS */
-#define toobig(x) ((((x)-1) >> MAXBITS) != 0)
-
+#define MAXASIZE (1 << MAXBITS)
/* function to convert a lua_Number to int (with any rounding method) */
#ifndef lua_number2int
@@ -87,7 +82,7 @@ static Node *hashnum (const Table *t, lua_Number n) {
lua_assert(sizeof(a) <= sizeof(n));
memcpy(a, &n, sizeof(a));
for (i = 1; i < numints; i++) a[0] += a[i];
- return hashmod(t, cast(lu_hash, a[0]));
+ return hashmod(t, a[0]);
}
@@ -96,12 +91,12 @@ 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 TObject *key) {
+Node *luaH_mainposition (const Table *t, const TValue *key) {
switch (ttype(key)) {
case LUA_TNUMBER:
return hashnum(t, nvalue(key));
case LUA_TSTRING:
- return hashstr(t, tsvalue(key));
+ return hashstr(t, rawtsvalue(key));
case LUA_TBOOLEAN:
return hashboolean(t, bvalue(key));
case LUA_TLIGHTUSERDATA:
@@ -116,11 +111,13 @@ Node *luaH_mainposition (const Table *t, const TObject *key) {
** returns the index for `key' if `key' is an appropriate key to live in
** the array part of the table, -1 otherwise.
*/
-static int arrayindex (const TObject *key) {
+static int arrayindex (const TValue *key, lua_Number lim) {
if (ttisnumber(key)) {
+ lua_Number n = nvalue(key);
int k;
- lua_number2int(k, (nvalue(key)));
- if (cast(lua_Number, k) == nvalue(key) && k >= 1 && !toobig(k))
+ if (n <= 0 || n > lim) return -1; /* out of range? */
+ lua_number2int(k, n);
+ if (cast(lua_Number, k) == nvalue(key))
return k;
}
return -1; /* `key' did not match some condition */
@@ -135,12 +132,12 @@ static int arrayindex (const TObject *key) {
static int luaH_index (lua_State *L, Table *t, StkId key) {
int i;
if (ttisnil(key)) return -1; /* first iteration */
- i = arrayindex(key);
- if (0 <= i && i <= t->sizearray) { /* is `key' inside array part? */
+ i = arrayindex(key, t->sizearray);
+ if (0 <= i) { /* is `key' inside array part? */
return i-1; /* yes; that's the index (corrected to C) */
}
else {
- const TObject *v = luaH_get(t, key);
+ const TValue *v = luaH_get(t, key);
if (v == &luaO_nilobject)
luaG_runerror(L, "invalid key for `next'");
i = cast(int, (cast(const lu_byte *, v) -
@@ -155,14 +152,14 @@ int luaH_next (lua_State *L, Table *t, StkId key) {
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));
- setobj2s(key+1, &t->array[i]);
+ setobj2s(L, key+1, &t->array[i]);
return 1;
}
}
for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */
if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */
- setobj2s(key, gkey(gnode(t, i)));
- setobj2s(key+1, gval(gnode(t, i)));
+ setobj2s(L, key, gkey(gnode(t, i)));
+ setobj2s(L, key+1, gval(gnode(t, i)));
return 1;
}
}
@@ -202,6 +199,7 @@ static void numuse (const Table *t, int *narray, int *nhash) {
int nums[MAXBITS+1];
int i, lg;
int totaluse = 0;
+ lua_Number sizelimit; /* an upper bound for the array size */
/* count elements in array part */
for (i=0, lg=0; lg<=MAXBITS; lg++) { /* for each slice [2^(lg-1) to 2^lg) */
int ttlg = twoto(lg); /* 2^lg */
@@ -221,10 +219,13 @@ static void numuse (const Table *t, int *narray, int *nhash) {
*narray = totaluse; /* all previous uses were in array part */
/* count elements in hash part */
i = sizenode(t);
+ /* array part cannot be larger than twice the maximum number of elements */
+ sizelimit = cast(lua_Number, totaluse + i) * 2;
+ if (sizelimit >= MAXASIZE) sizelimit = MAXASIZE;
while (i--) {
Node *n = &t->node[i];
if (!ttisnil(gval(n))) {
- int k = arrayindex(gkey(n));
+ int k = arrayindex(gkey(n), sizelimit);
if (k >= 0) { /* is `key' an appropriate array index? */
nums[luaO_log2(k-1)+1]++; /* count as such */
(*narray)++;
@@ -238,7 +239,7 @@ static void numuse (const Table *t, int *narray, int *nhash) {
static void setarrayvector (lua_State *L, Table *t, int size) {
int i;
- luaM_reallocvector(L, t->array, t->sizearray, size, TObject);
+ luaM_reallocvector(L, t->array, t->sizearray, size, TValue);
for (i=t->sizearray; i<size; i++)
setnilvalue(&t->array[i]);
t->sizearray = size;
@@ -295,16 +296,16 @@ static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
/* re-insert elements from vanishing slice */
for (i=nasize; i<oldasize; i++) {
if (!ttisnil(&t->array[i]))
- setobjt2t(luaH_setnum(L, t, i+1), &t->array[i]);
+ setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]);
}
/* shrink array */
- luaM_reallocvector(L, t->array, oldasize, nasize, TObject);
+ luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
}
/* re-insert elements in hash part */
for (i = twoto(oldhsize) - 1; i >= 0; i--) {
Node *old = nold+i;
if (!ttisnil(gval(old)))
- setobjt2t(luaH_set(L, t, gkey(old)), gval(old));
+ setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old));
}
if (oldhsize)
luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */
@@ -326,8 +327,8 @@ static void rehash (lua_State *L, Table *t) {
Table *luaH_new (lua_State *L, int narray, int lnhash) {
Table *t = luaM_new(L, Table);
- luaC_link(L, valtogco(t), LUA_TTABLE);
- t->metatable = hvalue(defaultmeta(L));
+ luaC_link(L, obj2gco(t), LUA_TTABLE);
+ t->metatable = NULL;
t->flags = cast(lu_byte, ~0);
/* temporary values (kept only if some malloc fails) */
t->array = NULL;
@@ -343,7 +344,7 @@ Table *luaH_new (lua_State *L, int narray, int lnhash) {
void luaH_free (lua_State *L, Table *t) {
if (t->lsizenode)
luaM_freearray(L, t->node, sizenode(t), Node);
- luaM_freearray(L, t->array, t->sizearray, TObject);
+ luaM_freearray(L, t->array, t->sizearray, TValue);
luaM_freelem(L, t);
}
@@ -376,8 +377,8 @@ void luaH_remove (Table *t, Node *e) {
** put new key in its main position; otherwise (colliding node is in its main
** position), new key goes to an empty position.
*/
-static TObject *newkey (lua_State *L, Table *t, const TObject *key) {
- TObject *val;
+static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
+ TValue *val;
Node *mp = luaH_mainposition(t, key);
if (!ttisnil(gval(mp))) { /* main position is not free? */
Node *othern = luaH_mainposition(t, gkey(mp)); /* `mp' of colliding node */
@@ -397,7 +398,8 @@ static TObject *newkey (lua_State *L, Table *t, const TObject *key) {
mp = n;
}
}
- setobj2t(gkey(mp), key); /* write barrier */
+ setobj2t(L, gkey(mp), key);
+ luaC_barrier(L, t, key);
lua_assert(ttisnil(gval(mp)));
for (;;) { /* correct `firstfree' */
if (ttisnil(gkey(t->firstfree)))
@@ -408,7 +410,7 @@ static TObject *newkey (lua_State *L, Table *t, const TObject *key) {
/* no more free places; must create one */
setbvalue(gval(mp), 0); /* avoid new key being removed */
rehash(L, t); /* grow table */
- val = cast(TObject *, luaH_get(t, key)); /* get new position */
+ val = cast(TValue *, luaH_get(t, key)); /* get new position */
lua_assert(ttisboolean(val));
setnilvalue(val);
return val;
@@ -418,23 +420,22 @@ static TObject *newkey (lua_State *L, Table *t, const TObject *key) {
/*
** generic search function
*/
-static const TObject *luaH_getany (Table *t, const TObject *key) {
- if (ttisnil(key)) return &luaO_nilobject;
- else {
+static const TValue *luaH_getany (Table *t, const TValue *key) {
+ if (!ttisnil(key)) {
Node *n = luaH_mainposition(t, key);
do { /* check whether `key' is somewhere in the chain */
if (luaO_rawequalObj(gkey(n), key)) return gval(n); /* that's it */
else n = n->next;
} while (n);
- return &luaO_nilobject;
}
+ return &luaO_nilobject;
}
/*
** search function for integers
*/
-const TObject *luaH_getnum (Table *t, int key) {
+const TValue *luaH_getnum (Table *t, int key) {
if (1 <= key && key <= t->sizearray)
return &t->array[key-1];
else {
@@ -453,10 +454,10 @@ const TObject *luaH_getnum (Table *t, int key) {
/*
** search function for strings
*/
-const TObject *luaH_getstr (Table *t, TString *key) {
+const TValue *luaH_getstr (Table *t, TString *key) {
Node *n = hashstr(t, key);
do { /* check whether `key' is somewhere in the chain */
- if (ttisstring(gkey(n)) && tsvalue(gkey(n)) == key)
+ if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key)
return gval(n); /* that's it */
else n = n->next;
} while (n);
@@ -467,9 +468,9 @@ const TObject *luaH_getstr (Table *t, TString *key) {
/*
** main search function
*/
-const TObject *luaH_get (Table *t, const TObject *key) {
+const TValue *luaH_get (Table *t, const TValue *key) {
switch (ttype(key)) {
- case LUA_TSTRING: return luaH_getstr(t, tsvalue(key));
+ case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
case LUA_TNUMBER: {
int k;
lua_number2int(k, (nvalue(key)));
@@ -482,11 +483,11 @@ const TObject *luaH_get (Table *t, const TObject *key) {
}
-TObject *luaH_set (lua_State *L, Table *t, const TObject *key) {
- const TObject *p = luaH_get(t, 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)
- return cast(TObject *, p);
+ return cast(TValue *, p);
else {
if (ttisnil(key)) luaG_runerror(L, "table index is nil");
else if (ttisnumber(key) && nvalue(key) != nvalue(key))
@@ -496,14 +497,26 @@ TObject *luaH_set (lua_State *L, Table *t, const TObject *key) {
}
-TObject *luaH_setnum (lua_State *L, Table *t, int key) {
- const TObject *p = luaH_getnum(t, key);
+TValue *luaH_setnum (lua_State *L, Table *t, int key) {
+ const TValue *p = luaH_getnum(t, key);
if (p != &luaO_nilobject)
- return cast(TObject *, p);
+ return cast(TValue *, p);
else {
- TObject k;
+ TValue k;
setnvalue(&k, cast(lua_Number, key));
return newkey(L, t, &k);
}
}
+
+TValue *luaH_setstr (lua_State *L, Table *t, TString *key) {
+ const TValue *p = luaH_getstr(t, key);
+ if (p != &luaO_nilobject)
+ return cast(TValue *, p);
+ else {
+ TValue k;
+ setsvalue(L, &k, key);
+ return newkey(L, t, &k);
+ }
+}
+