1997-09-17 03:25:59 +08:00
|
|
|
/*
|
2001-01-26 21:18:00 +08:00
|
|
|
** $Id: ltable.c,v 1.67 2001/01/25 16:45:36 roberto Exp roberto $
|
1997-09-17 03:25:59 +08:00
|
|
|
** Lua tables (hash)
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
1999-10-15 03:13:31 +08:00
|
|
|
/*
|
|
|
|
** Implementation of tables (aka arrays, objects, or hash tables);
|
|
|
|
** uses a mix of chained scatter table with Brent's variation.
|
|
|
|
** 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2000-06-12 21:52:05 +08:00
|
|
|
#include "lua.h"
|
|
|
|
|
2001-01-24 23:45:33 +08:00
|
|
|
#include "ldo.h"
|
1997-09-17 03:25:59 +08:00
|
|
|
#include "lmem.h"
|
|
|
|
#include "lobject.h"
|
1997-11-20 01:29:23 +08:00
|
|
|
#include "lstate.h"
|
1997-09-17 03:25:59 +08:00
|
|
|
#include "ltable.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-10-05 20:14:08 +08:00
|
|
|
#define TagDefault LUA_TTABLE
|
1997-09-17 03:25:59 +08:00
|
|
|
|
|
|
|
|
2001-01-11 02:56:11 +08:00
|
|
|
#define hashnum(t,n) (&t->node[(luint32)(lint32)(n)&(t->size-1)])
|
|
|
|
#define hashstr(t,str) (&t->node[(str)->u.s.hash&(t->size-1)])
|
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
|
1999-10-15 03:13:31 +08:00
|
|
|
/*
|
|
|
|
** returns the `main' position of an element in a table (that is, the index
|
|
|
|
** of its hash value)
|
|
|
|
*/
|
1999-12-07 20:05:34 +08:00
|
|
|
Node *luaH_mainposition (const Hash *t, const TObject *key) {
|
2000-11-25 01:39:56 +08:00
|
|
|
luint32 h;
|
1999-10-15 03:13:31 +08:00
|
|
|
switch (ttype(key)) {
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TNUMBER:
|
2001-01-11 02:56:11 +08:00
|
|
|
return hashnum(t, nvalue(key));
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TSTRING:
|
2001-01-11 02:56:11 +08:00
|
|
|
return hashstr(t, tsvalue(key));
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TUSERDATA:
|
2000-05-09 03:32:53 +08:00
|
|
|
h = IntPoint(tsvalue(key));
|
1997-09-17 03:25:59 +08:00
|
|
|
break;
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TTABLE:
|
2000-06-09 02:27:13 +08:00
|
|
|
h = IntPoint(hvalue(key));
|
1997-09-17 03:25:59 +08:00
|
|
|
break;
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TFUNCTION:
|
2000-05-09 03:32:53 +08:00
|
|
|
h = IntPoint(clvalue(key));
|
1997-09-17 03:25:59 +08:00
|
|
|
break;
|
|
|
|
default:
|
1999-12-07 20:05:34 +08:00
|
|
|
return NULL; /* invalid key */
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
2000-04-26 00:55:09 +08:00
|
|
|
return &t->node[h&(t->size-1)];
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-11 02:56:11 +08:00
|
|
|
static const TObject *luaH_getany (const Hash *t, const TObject *key) {
|
1999-12-07 20:05:34 +08:00
|
|
|
Node *n = luaH_mainposition(t, key);
|
2001-01-11 02:56:11 +08:00
|
|
|
while (n) {
|
1999-10-15 03:13:31 +08:00
|
|
|
if (luaO_equalObj(key, &n->key))
|
|
|
|
return &n->val;
|
|
|
|
n = n->next;
|
2001-01-11 02:56:11 +08:00
|
|
|
}
|
1999-12-07 20:05:34 +08:00
|
|
|
return &luaO_nilobject; /* key not found */
|
1999-10-15 03:13:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-04-26 00:55:09 +08:00
|
|
|
/* specialized version for numbers */
|
2000-12-05 02:33:40 +08:00
|
|
|
const TObject *luaH_getnum (const Hash *t, lua_Number key) {
|
2001-01-11 02:56:11 +08:00
|
|
|
Node *n = hashnum(t, key);
|
2000-04-26 00:55:09 +08:00
|
|
|
do {
|
2001-01-11 02:56:11 +08:00
|
|
|
if (nvalue(&n->key) == key && ttype(&n->key) == LUA_TNUMBER)
|
2000-04-26 00:55:09 +08:00
|
|
|
return &n->val;
|
|
|
|
n = n->next;
|
|
|
|
} while (n);
|
|
|
|
return &luaO_nilobject; /* key not found */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* specialized version for strings */
|
2000-05-09 03:32:53 +08:00
|
|
|
const TObject *luaH_getstr (const Hash *t, TString *key) {
|
2001-01-11 02:56:11 +08:00
|
|
|
Node *n = hashstr(t, key);
|
2000-04-26 00:55:09 +08:00
|
|
|
do {
|
2001-01-11 02:56:11 +08:00
|
|
|
if (tsvalue(&n->key) == key && ttype(&n->key) == LUA_TSTRING)
|
2000-04-26 00:55:09 +08:00
|
|
|
return &n->val;
|
|
|
|
n = n->next;
|
|
|
|
} while (n);
|
|
|
|
return &luaO_nilobject; /* key not found */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-11 02:56:11 +08:00
|
|
|
const TObject *luaH_get (const Hash *t, const TObject *key) {
|
2000-04-26 00:55:09 +08:00
|
|
|
switch (ttype(key)) {
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TNUMBER: return luaH_getnum(t, nvalue(key));
|
|
|
|
case LUA_TSTRING: return luaH_getstr(t, tsvalue(key));
|
2001-01-11 02:56:11 +08:00
|
|
|
default: return luaH_getany(t, key);
|
2000-04-26 00:55:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-31 22:08:27 +08:00
|
|
|
Node *luaH_next (lua_State *L, const Hash *t, const TObject *key) {
|
|
|
|
int i;
|
2000-10-05 20:14:08 +08:00
|
|
|
if (ttype(key) == LUA_TNIL)
|
2000-08-31 22:08:27 +08:00
|
|
|
i = 0; /* first iteration */
|
|
|
|
else {
|
2001-01-11 02:56:11 +08:00
|
|
|
const TObject *v = luaH_get(t, key);
|
2000-08-31 22:08:27 +08:00
|
|
|
if (v == &luaO_nilobject)
|
2001-01-24 23:45:33 +08:00
|
|
|
luaD_error(L, "invalid key for `next'");
|
2000-08-31 22:08:27 +08:00
|
|
|
i = (int)(((const char *)v -
|
|
|
|
(const char *)(&t->node[0].val)) / sizeof(Node)) + 1;
|
|
|
|
}
|
|
|
|
for (; i<t->size; i++) {
|
|
|
|
Node *n = node(t, i);
|
2000-10-05 20:14:08 +08:00
|
|
|
if (ttype(val(n)) != LUA_TNIL)
|
2000-08-31 22:08:27 +08:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
return NULL; /* no more elements */
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
2000-08-31 22:08:27 +08:00
|
|
|
|
2000-11-25 01:39:56 +08:00
|
|
|
static void setnodevector (lua_State *L, Hash *t, luint32 size) {
|
1999-01-23 02:47:23 +08:00
|
|
|
int i;
|
2000-05-24 21:54:49 +08:00
|
|
|
if (size > MAX_INT)
|
2001-01-24 23:45:33 +08:00
|
|
|
luaD_error(L, "table overflow");
|
2000-05-12 02:57:19 +08:00
|
|
|
t->node = luaM_newvector(L, size, Node);
|
2000-05-24 21:54:49 +08:00
|
|
|
for (i=0; i<(int)size; i++) {
|
2001-01-18 23:59:09 +08:00
|
|
|
setnilvalue(&t->node[i].key);
|
|
|
|
setnilvalue(&t->node[i].val);
|
2000-05-12 02:57:19 +08:00
|
|
|
t->node[i].next = NULL;
|
1999-10-15 03:13:31 +08:00
|
|
|
}
|
|
|
|
t->size = size;
|
|
|
|
t->firstfree = &t->node[size-1]; /* first free position to be used */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 21:12:07 +08:00
|
|
|
Hash *luaH_new (lua_State *L, int size) {
|
|
|
|
Hash *t = luaM_new(L, Hash);
|
1997-09-17 03:25:59 +08:00
|
|
|
t->htag = TagDefault;
|
2001-01-19 21:20:30 +08:00
|
|
|
t->next = G(L)->roottable;
|
|
|
|
G(L)->roottable = t;
|
2000-08-08 04:21:34 +08:00
|
|
|
t->mark = t;
|
2000-08-05 03:38:35 +08:00
|
|
|
t->size = 0;
|
|
|
|
t->node = NULL;
|
|
|
|
setnodevector(L, t, luaO_power2(size));
|
1997-09-17 03:25:59 +08:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 21:12:07 +08:00
|
|
|
void luaH_free (lua_State *L, Hash *t) {
|
2000-12-28 20:55:41 +08:00
|
|
|
luaM_freearray(L, t->node, t->size, Node);
|
|
|
|
luaM_freelem(L, t, Hash);
|
1999-10-05 01:51:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-05-24 21:54:49 +08:00
|
|
|
static int numuse (const Hash *t) {
|
1999-10-15 03:13:31 +08:00
|
|
|
Node *v = t->node;
|
|
|
|
int size = t->size;
|
1998-01-29 00:50:33 +08:00
|
|
|
int realuse = 0;
|
1997-09-17 03:25:59 +08:00
|
|
|
int i;
|
1998-01-29 00:50:33 +08:00
|
|
|
for (i=0; i<size; i++) {
|
2000-10-05 20:14:08 +08:00
|
|
|
if (ttype(&v[i].val) != LUA_TNIL)
|
1998-01-29 00:50:33 +08:00
|
|
|
realuse++;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
2000-05-24 21:54:49 +08:00
|
|
|
return realuse;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
1999-01-23 02:47:23 +08:00
|
|
|
|
1999-11-22 21:12:07 +08:00
|
|
|
static void rehash (lua_State *L, Hash *t) {
|
1999-10-15 03:13:31 +08:00
|
|
|
int oldsize = t->size;
|
|
|
|
Node *nold = t->node;
|
2000-06-07 00:31:41 +08:00
|
|
|
int nelems = numuse(t);
|
1999-10-15 03:13:31 +08:00
|
|
|
int i;
|
2001-01-19 21:20:30 +08:00
|
|
|
lua_assert(nelems<=oldsize);
|
2000-06-07 00:31:41 +08:00
|
|
|
if (nelems >= oldsize-oldsize/4) /* using more than 3/4? */
|
2000-11-25 01:39:56 +08:00
|
|
|
setnodevector(L, t, (luint32)oldsize*2);
|
2000-06-07 00:31:41 +08:00
|
|
|
else if (nelems <= oldsize/4 && /* less than 1/4? */
|
2000-05-24 21:54:49 +08:00
|
|
|
oldsize > MINPOWER2)
|
|
|
|
setnodevector(L, t, oldsize/2);
|
|
|
|
else
|
|
|
|
setnodevector(L, t, oldsize);
|
1999-10-15 03:13:31 +08:00
|
|
|
for (i=0; i<oldsize; i++) {
|
|
|
|
Node *old = nold+i;
|
2000-10-05 20:14:08 +08:00
|
|
|
if (ttype(&old->val) != LUA_TNIL)
|
2001-01-18 23:59:09 +08:00
|
|
|
setobj(luaH_set(L, t, &old->key), &old->val);
|
1999-10-15 03:13:31 +08:00
|
|
|
}
|
2000-12-28 20:55:41 +08:00
|
|
|
luaM_freearray(L, nold, oldsize, Node); /* free old array */
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-15 03:13:31 +08:00
|
|
|
/*
|
2001-01-11 02:56:11 +08:00
|
|
|
** 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.
|
1999-10-15 03:13:31 +08:00
|
|
|
*/
|
2001-01-11 02:56:11 +08:00
|
|
|
static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) {
|
2001-01-26 21:18:00 +08:00
|
|
|
if (ttype(&mp->val) != LUA_TNIL) { /* main position is not free? */
|
2001-01-11 02:56:11 +08:00
|
|
|
Node *othern = luaH_mainposition(t, &mp->key); /* `mp' of colliding node */
|
|
|
|
Node *n = t->firstfree; /* get a free place */
|
|
|
|
if (othern != mp) { /* is colliding node out of its main position? */
|
1999-10-15 03:13:31 +08:00
|
|
|
/* yes; move colliding node into free position */
|
|
|
|
while (othern->next != mp) othern = othern->next; /* find previous */
|
|
|
|
othern->next = n; /* redo the chain with `n' in place of `mp' */
|
|
|
|
*n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
|
|
|
|
mp->next = NULL; /* now `mp' is free */
|
2001-01-26 00:45:36 +08:00
|
|
|
setnilvalue(&mp->val);
|
1999-10-15 03:13:31 +08:00
|
|
|
}
|
|
|
|
else { /* colliding node is in its own main position */
|
|
|
|
/* new node will go into free position */
|
|
|
|
n->next = mp->next; /* chain new position */
|
|
|
|
mp->next = n;
|
|
|
|
mp = n;
|
|
|
|
}
|
|
|
|
}
|
2001-01-18 23:59:09 +08:00
|
|
|
setobj(&mp->key, key);
|
2001-01-26 00:45:36 +08:00
|
|
|
lua_assert(ttype(&mp->val) == LUA_TNIL);
|
2000-06-07 00:31:41 +08:00
|
|
|
for (;;) { /* correct `firstfree' */
|
2000-10-05 20:14:08 +08:00
|
|
|
if (ttype(&t->firstfree->key) == LUA_TNIL)
|
2000-06-07 00:31:41 +08:00
|
|
|
return &mp->val; /* OK; table still has a free place */
|
1999-10-15 03:13:31 +08:00
|
|
|
else if (t->firstfree == t->node) break; /* cannot decrement from here */
|
|
|
|
else (t->firstfree)--;
|
|
|
|
}
|
1999-11-22 21:12:07 +08:00
|
|
|
rehash(L, t); /* no more free places */
|
2000-06-07 00:31:41 +08:00
|
|
|
return luaH_set(L, t, key); /* `rehash' invalidates this insertion */
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
1998-07-13 00:15:19 +08:00
|
|
|
|
|
|
|
|
2001-01-11 02:56:11 +08:00
|
|
|
static TObject *luaH_setany (lua_State *L, Hash *t, const TObject *key) {
|
|
|
|
Node *mp = luaH_mainposition(t, key);
|
|
|
|
Node *n = mp;
|
|
|
|
if (!mp)
|
2001-01-24 23:45:33 +08:00
|
|
|
luaD_error(L, "table index is nil");
|
2001-01-11 02:56:11 +08:00
|
|
|
do { /* check whether `key' is somewhere in the chain */
|
|
|
|
if (luaO_equalObj(key, &n->key))
|
|
|
|
return &n->val; /* that's all */
|
|
|
|
else n = n->next;
|
|
|
|
} while (n);
|
|
|
|
return newkey(L, t, mp, key); /* `key' not found; must insert it */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TObject *luaH_setnum (lua_State *L, Hash *t, lua_Number key) {
|
|
|
|
TObject kobj;
|
|
|
|
Node *mp = hashnum(t, key);
|
|
|
|
Node *n = mp;
|
|
|
|
do { /* check whether `key' is somewhere in the chain */
|
|
|
|
if (nvalue(&n->key) == key && ttype(&n->key) == LUA_TNUMBER)
|
|
|
|
return &n->val; /* that's all */
|
|
|
|
else n = n->next;
|
|
|
|
} while (n);
|
|
|
|
/* `key' not found; must insert it */
|
2001-01-18 23:59:09 +08:00
|
|
|
setnvalue(&kobj, key);
|
2001-01-11 02:56:11 +08:00
|
|
|
return newkey(L, t, mp, &kobj);
|
1998-07-13 00:15:19 +08:00
|
|
|
}
|
|
|
|
|
2000-05-09 03:32:53 +08:00
|
|
|
|
2001-01-11 02:56:11 +08:00
|
|
|
TObject *luaH_setstr (lua_State *L, Hash *t, TString *key) {
|
|
|
|
TObject kobj;
|
|
|
|
Node *mp = hashstr(t, key);
|
|
|
|
Node *n = mp;
|
|
|
|
do { /* check whether `key' is somewhere in the chain */
|
|
|
|
if (tsvalue(&n->key) == key && ttype(&n->key) == LUA_TSTRING)
|
|
|
|
return &n->val; /* that's all */
|
|
|
|
else n = n->next;
|
|
|
|
} while (n);
|
|
|
|
/* `key' not found; must insert it */
|
2001-01-18 23:59:09 +08:00
|
|
|
setsvalue(&kobj, key);
|
2001-01-11 02:56:11 +08:00
|
|
|
return newkey(L, t, mp, &kobj);
|
2000-06-06 04:15:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-11 02:56:11 +08:00
|
|
|
TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
|
|
|
|
switch (ttype(key)) {
|
|
|
|
case LUA_TNUMBER: return luaH_setnum(L, t, nvalue(key));
|
|
|
|
case LUA_TSTRING: return luaH_setstr(L, t, tsvalue(key));
|
|
|
|
default: return luaH_setany(L, t, key);
|
|
|
|
}
|
2000-05-09 03:32:53 +08:00
|
|
|
}
|
2000-05-24 21:54:49 +08:00
|
|
|
|