lua/ltable.c

263 lines
7.5 KiB
C
Raw Normal View History

1997-09-16 23:25:59 +04:00
/*
2001-02-01 20:40:48 +03:00
** $Id: ltable.c,v 1.74 2001/01/30 19:48:37 roberto Exp roberto $
1997-09-16 23:25:59 +04:00
** Lua tables (hash)
** See Copyright Notice in lua.h
*/
1999-10-14 23:13:31 +04: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.
*/
#include "lua.h"
#include "ldo.h"
1997-09-16 23:25:59 +04:00
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"
1997-09-16 23:25:59 +04:00
#include "ltable.h"
2000-10-05 16:14:08 +04:00
#define TagDefault LUA_TTABLE
1997-09-16 23:25:59 +04:00
2001-01-26 18:58:50 +03:00
#define hashnum(t,n) (&t->node[lmod((luint32)(lint32)(n), t->size)])
#define hashstr(t,str) (&t->node[lmod((str)->u.s.hash, t->size)])
#define hashpointer(t,p) (&t->node[lmod(IntPoint(p), t->size)])
1997-09-16 23:25:59 +04:00
1999-10-14 23:13:31 +04:00
/*
** returns the `main' position of an element in a table (that is, the index
** of its hash value)
*/
Node *luaH_mainposition (const Hash *t, const Node *n) {
switch (ttype_key(n)) {
2000-10-05 16:14:08 +04:00
case LUA_TNUMBER:
return hashnum(t, nvalue_key(n));
2000-10-05 16:14:08 +04:00
case LUA_TSTRING:
return hashstr(t, tsvalue_key(n));
default: /* all other types are hashed as (void *) */
return hashpointer(t, tsvalue_key(n));
1997-09-16 23:25:59 +04:00
}
}
Node *luaH_next (lua_State *L, Hash *t, const TObject *key) {
2000-08-31 18:08:27 +04:00
int i;
2000-10-05 16:14:08 +04:00
if (ttype(key) == LUA_TNIL)
2000-08-31 18:08:27 +04:00
i = 0; /* first iteration */
else {
const TObject *v = luaH_get(t, key);
2000-08-31 18:08:27 +04:00
if (v == &luaO_nilobject)
luaD_error(L, "invalid key for `next'");
2000-08-31 18:08:27 +04: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 16:14:08 +04:00
if (ttype(val(n)) != LUA_TNIL)
2000-08-31 18:08:27 +04:00
return n;
}
return NULL; /* no more elements */
1997-09-16 23:25:59 +04:00
}
2000-08-31 18:08:27 +04:00
2001-01-29 16:14:49 +03:00
int luaH_nexti (Hash *t, int i) {
for (i++; i<t->size; i++) {
if (ttype(val(node(t, i))) != LUA_TNIL) /* a non-nil value? */
return i;
}
return -1; /* no more elements */
}
static void setnodevector (lua_State *L, Hash *t, luint32 size) {
1999-01-22 21:47:23 +03:00
int i;
2000-05-24 17:54:49 +04:00
if (size > MAX_INT)
luaD_error(L, "table overflow");
2000-05-11 22:57:19 +04:00
t->node = luaM_newvector(L, size, Node);
2000-05-24 17:54:49 +04:00
for (i=0; i<(int)size; i++) {
2000-05-11 22:57:19 +04:00
t->node[i].next = NULL;
t->node[i].key_tt = LUA_TNIL;
setnilvalue(&t->node[i].val);
1999-10-14 23:13:31 +04:00
}
t->size = size;
t->firstfree = &t->node[size-1]; /* first free position to be used */
}
Hash *luaH_new (lua_State *L, int size) {
Hash *t = luaM_new(L, Hash);
1997-09-16 23:25:59 +04:00
t->htag = TagDefault;
t->next = G(L)->roottable;
G(L)->roottable = t;
t->mark = t;
t->size = 0;
t->node = NULL;
setnodevector(L, t, luaO_power2(size));
1997-09-16 23:25:59 +04:00
return t;
}
void luaH_free (lua_State *L, Hash *t) {
luaM_freearray(L, t->node, t->size, Node);
luaM_freelem(L, t, Hash);
}
2000-05-24 17:54:49 +04:00
static int numuse (const Hash *t) {
1999-10-14 23:13:31 +04:00
Node *v = t->node;
int size = t->size;
int realuse = 0;
1997-09-16 23:25:59 +04:00
int i;
for (i=0; i<size; i++) {
2000-10-05 16:14:08 +04:00
if (ttype(&v[i].val) != LUA_TNIL)
realuse++;
1997-09-16 23:25:59 +04:00
}
2000-05-24 17:54:49 +04:00
return realuse;
1997-09-16 23:25:59 +04:00
}
1999-01-22 21:47:23 +03:00
static void rehash (lua_State *L, Hash *t) {
1999-10-14 23:13:31 +04:00
int oldsize = t->size;
Node *nold = t->node;
2000-06-06 20:31:41 +04:00
int nelems = numuse(t);
1999-10-14 23:13:31 +04:00
int i;
lua_assert(nelems<=oldsize);
2000-06-06 20:31:41 +04:00
if (nelems >= oldsize-oldsize/4) /* using more than 3/4? */
setnodevector(L, t, (luint32)oldsize*2);
2000-06-06 20:31:41 +04:00
else if (nelems <= oldsize/4 && /* less than 1/4? */
2000-05-24 17:54:49 +04:00
oldsize > MINPOWER2)
setnodevector(L, t, oldsize/2);
else
setnodevector(L, t, oldsize);
1999-10-14 23:13:31 +04:00
for (i=0; i<oldsize; i++) {
Node *old = nold+i;
if (ttype(&old->val) != LUA_TNIL) {
TObject o;
TObject *v;
setkey2obj(&o, old);
v = luaH_set(L, t, &o);
setobj(v, &old->val);
}
1999-10-14 23:13:31 +04:00
}
luaM_freearray(L, nold, oldsize, Node); /* free old array */
1997-09-16 23:25:59 +04:00
}
1999-10-14 23:13:31 +04: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-14 23:13:31 +04:00
*/
static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) {
2001-01-26 16:18:00 +03:00
if (ttype(&mp->val) != LUA_TNIL) { /* main position is not free? */
Node *othern = luaH_mainposition(t, mp); /* `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-14 23:13:31 +04: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-25 19:45:36 +03:00
setnilvalue(&mp->val);
1999-10-14 23:13:31 +04: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;
}
}
setobj2key(mp, key);
2001-01-25 19:45:36 +03:00
lua_assert(ttype(&mp->val) == LUA_TNIL);
2000-06-06 20:31:41 +04:00
for (;;) { /* correct `firstfree' */
if (ttype_key(t->firstfree) == LUA_TNIL)
2000-06-06 20:31:41 +04:00
return &mp->val; /* OK; table still has a free place */
1999-10-14 23:13:31 +04:00
else if (t->firstfree == t->node) break; /* cannot decrement from here */
else (t->firstfree)--;
}
rehash(L, t); /* no more free places */
2000-06-06 20:31:41 +04:00
return luaH_set(L, t, key); /* `rehash' invalidates this insertion */
1997-09-16 23:25:59 +04:00
}
/*
** search function for numbers
*/
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 */
2001-01-30 22:48:37 +03:00
if (ttype_key(n) == LUA_TNUMBER && nvalue_key(n) == key)
return &n->val; /* that's all */
else n = n->next;
} while (n);
if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */
/* `key' not found; must insert it */
setnvalue(&kobj, key);
return newkey(L, t, mp, &kobj);
}
/*
** search function for strings
*/
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 */
2001-01-30 22:48:37 +03:00
if (ttype_key(n) == LUA_TSTRING && tsvalue_key(n) == key)
return &n->val; /* that's all */
else n = n->next;
} while (n);
if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */
/* `key' not found; must insert it */
setsvalue(&kobj, key);
return newkey(L, t, mp, &kobj);
2000-06-06 00:15:33 +04:00
}
/*
** search function for 'pointer' types
*/
static TObject *luaH_setany (lua_State *L, Hash *t, const TObject *key) {
Node *mp = hashpointer(t, hvalue(key));
Node *n = mp;
do { /* check whether `key' is somewhere in the chain */
/* compare as `tsvalue', but may be other pointers (it is the same) */
2001-01-30 22:48:37 +03:00
if (ttype_key(n) == ttype(key) && tsvalue_key(n) == tsvalue(key))
return &n->val; /* that's all */
else n = n->next;
} while (n);
if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */
return newkey(L, t, mp, key); /* `key' not found; must insert it */
}
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));
case LUA_TNIL:
if (L) luaD_error(L, "table index is nil");
return (TObject *)&luaO_nilobject; /* get option */
default: return luaH_setany(L, t, key);
}
}
2000-05-24 17:54:49 +04:00