lua/ltable.c

291 lines
8.2 KiB
C
Raw Normal View History

1997-09-16 21:25:59 +02:00
/*
2000-08-09 21:16:57 +02:00
** $Id: ltable.c,v 1.52 2000/08/07 20:21:34 roberto Exp roberto $
1997-09-16 21:25:59 +02:00
** Lua tables (hash)
** See Copyright Notice in lua.h
*/
1999-10-14 21:13:31 +02: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"
1997-09-16 21:25:59 +02:00
#include "lauxlib.h"
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"
#include "lstring.h"
1997-09-16 21:25:59 +02:00
#include "ltable.h"
#define gcsize(L, n) numblocks(L, n*2, sizeof(Hash))
1997-09-16 21:25:59 +02:00
2000-03-27 22:10:21 +02:00
#define TagDefault TAG_TABLE
1997-09-16 21:25:59 +02:00
1999-10-14 21:13:31 +02:00
/*
** returns the `main' position of an element in a table (that is, the index
** of its hash value)
*/
1999-12-07 13:05:34 +01:00
Node *luaH_mainposition (const Hash *t, const TObject *key) {
1999-10-14 21:13:31 +02:00
unsigned long h;
switch (ttype(key)) {
2000-03-10 19:37:44 +01:00
case TAG_NUMBER:
1999-10-14 21:13:31 +02:00
h = (unsigned long)(long)nvalue(key);
1997-09-16 21:25:59 +02:00
break;
case TAG_STRING:
h = tsvalue(key)->u.s.hash;
break;
case TAG_USERDATA:
h = IntPoint(tsvalue(key));
1997-09-16 21:25:59 +02:00
break;
2000-03-27 22:10:21 +02:00
case TAG_TABLE:
h = IntPoint(hvalue(key));
1997-09-16 21:25:59 +02:00
break;
2000-03-10 19:37:44 +01:00
case TAG_LCLOSURE: case TAG_CCLOSURE:
h = IntPoint(clvalue(key));
1997-09-16 21:25:59 +02:00
break;
default:
1999-12-07 13:05:34 +01:00
return NULL; /* invalid key */
1997-09-16 21:25:59 +02:00
}
2000-06-30 16:35:17 +02:00
LUA_ASSERT(h%(unsigned int)t->size == (h&((unsigned int)t->size-1)),
"a&(x-1) == a%x, for x power of 2");
2000-04-25 18:55:09 +02:00
return &t->node[h&(t->size-1)];
1997-09-16 21:25:59 +02:00
}
2000-04-25 18:55:09 +02:00
static const TObject *luaH_getany (lua_State *L, const Hash *t,
const TObject *key) {
1999-12-07 13:05:34 +01:00
Node *n = luaH_mainposition(t, key);
if (!n)
2000-06-28 19:03:56 +02:00
lua_error(L, "table index is nil");
1999-12-07 13:05:34 +01:00
else do {
1999-10-14 21:13:31 +02:00
if (luaO_equalObj(key, &n->key))
return &n->val;
n = n->next;
} while (n);
1999-12-07 13:05:34 +01:00
return &luaO_nilobject; /* key not found */
1999-10-14 21:13:31 +02:00
}
2000-04-25 18:55:09 +02:00
/* specialized version for numbers */
const TObject *luaH_getnum (const Hash *t, Number key) {
Node *n = &t->node[(unsigned long)(long)key&(t->size-1)];
do {
if (ttype(&n->key) == TAG_NUMBER && nvalue(&n->key) == key)
return &n->val;
n = n->next;
} while (n);
return &luaO_nilobject; /* key not found */
}
/* specialized version for strings */
const TObject *luaH_getstr (const Hash *t, TString *key) {
Node *n = &t->node[key->u.s.hash&(t->size-1)];
2000-04-25 18:55:09 +02:00
do {
if (ttype(&n->key) == TAG_STRING && tsvalue(&n->key) == key)
return &n->val;
n = n->next;
} while (n);
return &luaO_nilobject; /* key not found */
}
const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) {
switch (ttype(key)) {
case TAG_NUMBER: return luaH_getnum(t, nvalue(key));
case TAG_STRING: return luaH_getstr(t, tsvalue(key));
default: return luaH_getany(L, t, key);
}
}
int luaH_pos (lua_State *L, const Hash *t, const TObject *key) {
const TObject *v = luaH_get(L, t, key);
1999-10-14 21:13:31 +02:00
return (v == &luaO_nilobject) ? -1 : /* key not found */
2000-02-08 17:39:42 +01:00
(int)(((const char *)v - (const char *)(&t->node[0].val))/sizeof(Node));
1997-09-16 21:25:59 +02:00
}
2000-06-05 22:07:53 +02:00
/*
** try to remove a key without value from a table. To avoid problems with
** hash, change `key' for a number with the same hash.
*/
void luaH_remove (Hash *t, TObject *key) {
if (ttype(key) == TAG_NUMBER ||
(ttype(key) == TAG_STRING && tsvalue(key)->u.s.len <= 30))
return; /* do not remove numbers nor small strings */
else {
2000-06-05 22:07:53 +02:00
/* try to find a number `n' with the same hash as `key' */
Node *mp = luaH_mainposition(t, key);
int n = mp - &t->node[0];
/* make sure `n' is not in `t' */
while (luaH_getnum(t, n) != &luaO_nilobject) {
if (n >= MAX_INT - t->size)
2000-06-05 22:07:53 +02:00
return; /* give up; (to avoid overflow) */
n += t->size;
}
ttype(key) = TAG_NUMBER;
nvalue(key) = n;
2000-06-30 16:35:17 +02:00
LUA_ASSERT(luaH_mainposition(t, key) == mp, "cannot change hash");
2000-06-05 22:07:53 +02:00
}
}
1997-09-16 21:25:59 +02:00
2000-05-24 15:54:49 +02:00
static void setnodevector (lua_State *L, Hash *t, lint32 size) {
1999-01-22 19:47:23 +01:00
int i;
2000-05-24 15:54:49 +02:00
if (size > MAX_INT)
lua_error(L, "table overflow");
2000-05-11 20:57:19 +02:00
t->node = luaM_newvector(L, size, Node);
2000-05-24 15:54:49 +02:00
for (i=0; i<(int)size; i++) {
2000-05-11 20:57:19 +02:00
ttype(&t->node[i].key) = ttype(&t->node[i].val) = TAG_NIL;
t->node[i].next = NULL;
1999-10-14 21:13:31 +02:00
}
L->nblocks += gcsize(L, size) - gcsize(L, t->size);
1999-10-14 21:13:31 +02: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 21:25:59 +02:00
t->htag = TagDefault;
t->next = L->roottable;
L->roottable = t;
t->mark = t;
t->size = 0;
L->nblocks += gcsize(L, 0);
t->node = NULL;
setnodevector(L, t, luaO_power2(size));
1997-09-16 21:25:59 +02:00
return t;
}
void luaH_free (lua_State *L, Hash *t) {
L->nblocks -= gcsize(L, t->size);
luaM_free(L, t->node);
luaM_free(L, t);
}
2000-05-24 15:54:49 +02:00
static int numuse (const Hash *t) {
1999-10-14 21:13:31 +02:00
Node *v = t->node;
int size = t->size;
int realuse = 0;
1997-09-16 21:25:59 +02:00
int i;
for (i=0; i<size; i++) {
2000-03-10 19:37:44 +01:00
if (ttype(&v[i].val) != TAG_NIL)
realuse++;
1997-09-16 21:25:59 +02:00
}
2000-05-24 15:54:49 +02:00
return realuse;
1997-09-16 21:25:59 +02:00
}
1999-01-22 19:47:23 +01:00
static void rehash (lua_State *L, Hash *t) {
1999-10-14 21:13:31 +02:00
int oldsize = t->size;
Node *nold = t->node;
2000-06-06 18:31:41 +02:00
int nelems = numuse(t);
1999-10-14 21:13:31 +02:00
int i;
2000-06-30 16:35:17 +02:00
LUA_ASSERT(nelems<=oldsize, "wrong count");
2000-06-06 18:31:41 +02:00
if (nelems >= oldsize-oldsize/4) /* using more than 3/4? */
2000-05-24 15:54:49 +02:00
setnodevector(L, t, (lint32)oldsize*2);
2000-06-06 18:31:41 +02:00
else if (nelems <= oldsize/4 && /* less than 1/4? */
2000-05-24 15:54:49 +02:00
oldsize > MINPOWER2)
setnodevector(L, t, oldsize/2);
else
setnodevector(L, t, oldsize);
1999-10-14 21:13:31 +02:00
for (i=0; i<oldsize; i++) {
Node *old = nold+i;
2000-03-10 19:37:44 +01:00
if (ttype(&old->val) != TAG_NIL)
2000-06-06 18:31:41 +02:00
*luaH_set(L, t, &old->key) = old->val;
1999-10-14 21:13:31 +02:00
}
luaM_free(L, nold); /* free old array */
1997-09-16 21:25:59 +02:00
}
1999-10-14 21:13:31 +02:00
/*
2000-06-06 18:31:41 +02:00
** inserts a key into a hash table; first, check whether key is
1999-10-14 21:13:31 +02:00
** already present; if not, check whether key's main position is free;
** if not, check whether colliding node is in its main position or not;
2000-06-06 18:31:41 +02:00
** if it is not, move colliding node to an empty place and put new key
1999-10-14 21:13:31 +02:00
** in its main position; otherwise (colliding node is in its main position),
2000-06-06 18:31:41 +02:00
** new key goes to an empty position.
1999-10-14 21:13:31 +02:00
*/
2000-06-06 18:31:41 +02:00
TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
1999-12-07 13:05:34 +01:00
Node *mp = luaH_mainposition(t, key);
1999-10-14 21:13:31 +02:00
Node *n = mp;
1999-12-07 13:05:34 +01:00
if (!mp)
2000-06-28 19:03:56 +02:00
lua_error(L, "table index is nil");
1999-10-14 21:13:31 +02:00
do { /* check whether `key' is somewhere in the chain */
2000-06-06 18:31:41 +02:00
if (luaO_equalObj(key, &n->key))
return &n->val; /* that's all */
1999-10-14 21:13:31 +02:00
else n = n->next;
} while (n);
/* `key' not found; must insert it */
2000-03-10 19:37:44 +01:00
if (ttype(&mp->key) != TAG_NIL) { /* main position is not free? */
1999-10-14 21:13:31 +02:00
Node *othern; /* main position of colliding node */
n = t->firstfree; /* get a free place */
/* is colliding node out of its main position? (can only happens if
2000-06-06 18:31:41 +02:00
its position is after "firstfree") */
1999-12-07 13:05:34 +01:00
if (mp > n && (othern=luaH_mainposition(t, &mp->key)) != mp) {
1999-10-14 21:13:31 +02: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 */
}
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;
}
}
mp->key = *key;
2000-06-06 18:31:41 +02:00
for (;;) { /* correct `firstfree' */
2000-03-31 18:28:45 +02:00
if (ttype(&t->firstfree->key) == TAG_NIL)
2000-06-06 18:31:41 +02:00
return &mp->val; /* OK; table still has a free place */
1999-10-14 21:13:31 +02: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 18:31:41 +02:00
return luaH_set(L, t, key); /* `rehash' invalidates this insertion */
1997-09-16 21:25:59 +02:00
}
2000-06-06 18:31:41 +02:00
TObject *luaH_setint (lua_State *L, Hash *t, int key) {
TObject index;
2000-03-10 19:37:44 +01:00
ttype(&index) = TAG_NUMBER;
1999-10-14 21:13:31 +02:00
nvalue(&index) = key;
2000-06-06 18:31:41 +02:00
return luaH_set(L, t, &index);
}
2000-06-06 18:31:41 +02:00
void luaH_setstrnum (lua_State *L, Hash *t, TString *key, Number val) {
TObject *value, index;
2000-06-05 22:15:33 +02:00
ttype(&index) = TAG_STRING;
tsvalue(&index) = key;
2000-06-06 18:31:41 +02:00
value = luaH_set(L, t, &index);
ttype(value) = TAG_NUMBER;
nvalue(value) = val;
2000-06-05 22:15:33 +02:00
}
const TObject *luaH_getglobal (lua_State *L, const char *name) {
return luaH_getstr(L->gt, luaS_new(L, name));
}
2000-05-24 15:54:49 +02:00