1997-09-17 03:25:59 +08:00
|
|
|
/*
|
1998-12-30 21:14:46 +08:00
|
|
|
** $Id: ltable.c,v 1.15 1998/08/11 16:38:34 roberto Exp roberto $
|
1997-09-17 03:25:59 +08:00
|
|
|
** Lua tables (hash)
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#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"
|
|
|
|
#include "lua.h"
|
|
|
|
|
|
|
|
|
1997-10-24 00:26:37 +08:00
|
|
|
#define gcsize(n) (1+(n/16))
|
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
#define nuse(t) ((t)->nuse)
|
|
|
|
#define nodevector(t) ((t)->node)
|
|
|
|
|
|
|
|
|
|
|
|
#define TagDefault LUA_T_ARRAY;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static long int hashindex (TObject *ref)
|
|
|
|
{
|
|
|
|
long int h;
|
|
|
|
switch (ttype(ref)) {
|
|
|
|
case LUA_T_NUMBER:
|
|
|
|
h = (long int)nvalue(ref);
|
|
|
|
break;
|
|
|
|
case LUA_T_STRING: case LUA_T_USERDATA:
|
|
|
|
h = (IntPoint)tsvalue(ref);
|
|
|
|
break;
|
1998-01-09 22:44:55 +08:00
|
|
|
case LUA_T_ARRAY:
|
|
|
|
h = (IntPoint)avalue(ref);
|
1997-09-17 03:25:59 +08:00
|
|
|
break;
|
1997-12-16 00:17:20 +08:00
|
|
|
case LUA_T_PROTO:
|
|
|
|
h = (IntPoint)tfvalue(ref);
|
|
|
|
break;
|
|
|
|
case LUA_T_CPROTO:
|
|
|
|
h = (IntPoint)fvalue(ref);
|
|
|
|
break;
|
1998-01-09 22:44:55 +08:00
|
|
|
case LUA_T_CLOSURE:
|
|
|
|
h = (IntPoint)clvalue(ref);
|
1997-09-17 03:25:59 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
lua_error("unexpected type to index table");
|
1997-12-09 21:50:08 +08:00
|
|
|
h = 0; /* to avoid warnings */
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
return (h >= 0 ? h : -(h+1));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int present (Hash *t, TObject *key)
|
|
|
|
{
|
|
|
|
int tsize = nhash(t);
|
|
|
|
long int h = hashindex(key);
|
|
|
|
int h1 = h%tsize;
|
|
|
|
TObject *rf = ref(node(t, h1));
|
|
|
|
if (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf)) {
|
|
|
|
int h2 = h%(tsize-2) + 1;
|
|
|
|
do {
|
1998-01-14 02:06:27 +08:00
|
|
|
h1 += h2;
|
|
|
|
if (h1 >= tsize) h1 -= tsize;
|
1997-09-17 03:25:59 +08:00
|
|
|
rf = ref(node(t, h1));
|
|
|
|
} while (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf));
|
|
|
|
}
|
|
|
|
return h1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Alloc a vector node
|
|
|
|
*/
|
|
|
|
static Node *hashnodecreate (int nhash)
|
|
|
|
{
|
1997-10-24 00:26:37 +08:00
|
|
|
Node *v = luaM_newvector(nhash, Node);
|
|
|
|
int i;
|
|
|
|
for (i=0; i<nhash; i++)
|
|
|
|
ttype(ref(&v[i])) = LUA_T_NIL;
|
|
|
|
return v;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Delete a hash
|
|
|
|
*/
|
|
|
|
static void hashdelete (Hash *t)
|
|
|
|
{
|
1997-11-22 03:00:46 +08:00
|
|
|
luaM_free(nodevector(t));
|
1997-09-17 03:25:59 +08:00
|
|
|
luaM_free(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaH_free (Hash *frees)
|
|
|
|
{
|
|
|
|
while (frees) {
|
|
|
|
Hash *next = (Hash *)frees->head.next;
|
1997-11-20 01:29:23 +08:00
|
|
|
L->nblocks -= gcsize(frees->nhash);
|
1997-09-17 03:25:59 +08:00
|
|
|
hashdelete(frees);
|
|
|
|
frees = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-08-12 00:38:34 +08:00
|
|
|
Hash *luaH_new (int nhash) {
|
1997-09-17 03:25:59 +08:00
|
|
|
Hash *t = luaM_new(Hash);
|
1998-08-12 00:38:34 +08:00
|
|
|
nhash = luaO_redimension(nhash*3/2);
|
1997-09-17 03:25:59 +08:00
|
|
|
nodevector(t) = hashnodecreate(nhash);
|
|
|
|
nhash(t) = nhash;
|
|
|
|
nuse(t) = 0;
|
|
|
|
t->htag = TagDefault;
|
1997-11-20 01:29:23 +08:00
|
|
|
luaO_insertlist(&(L->roottable), (GCnode *)t);
|
|
|
|
L->nblocks += gcsize(nhash);
|
1997-09-17 03:25:59 +08:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-08-11 05:36:32 +08:00
|
|
|
static int newsize (Hash *t) {
|
1998-01-29 00:50:33 +08:00
|
|
|
Node *v = t->node;
|
|
|
|
int size = nhash(t);
|
|
|
|
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++) {
|
|
|
|
if (ttype(ref(v+i)) != LUA_T_NIL && ttype(val(v+i)) != LUA_T_NIL)
|
|
|
|
realuse++;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
1998-08-11 05:36:32 +08:00
|
|
|
return luaO_redimension((realuse+1)*2); /* +1 is the new element */
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
1998-08-12 00:38:34 +08:00
|
|
|
static void rehash (Hash *t) {
|
1998-01-29 00:50:33 +08:00
|
|
|
int nold = nhash(t);
|
1997-09-17 03:25:59 +08:00
|
|
|
Node *vold = nodevector(t);
|
1998-01-29 00:50:33 +08:00
|
|
|
int nnew = newsize(t);
|
1997-09-17 03:25:59 +08:00
|
|
|
int i;
|
1998-01-29 00:50:33 +08:00
|
|
|
nodevector(t) = hashnodecreate(nnew);
|
|
|
|
nhash(t) = nnew;
|
1998-08-12 00:38:34 +08:00
|
|
|
nuse(t) = 0;
|
1997-09-17 03:25:59 +08:00
|
|
|
for (i=0; i<nold; i++) {
|
|
|
|
Node *n = vold+i;
|
1998-08-12 00:38:34 +08:00
|
|
|
if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL) {
|
1997-09-17 03:25:59 +08:00
|
|
|
*node(t, present(t, ref(n))) = *n; /* copy old node to luaM_new hash */
|
1998-08-12 00:38:34 +08:00
|
|
|
nuse(t)++;
|
|
|
|
}
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
1998-01-29 00:50:33 +08:00
|
|
|
L->nblocks += gcsize(nnew)-gcsize(nold);
|
1997-09-17 03:25:59 +08:00
|
|
|
luaM_free(vold);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** If the hash node is present, return its pointer, otherwise return
|
|
|
|
** null.
|
|
|
|
*/
|
|
|
|
TObject *luaH_get (Hash *t, TObject *ref)
|
|
|
|
{
|
|
|
|
int h = present(t, ref);
|
|
|
|
if (ttype(ref(node(t, h))) != LUA_T_NIL) return val(node(t, h));
|
1998-07-13 00:15:19 +08:00
|
|
|
else return &luaO_nilobject;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** If the hash node is present, return its pointer, otherwise create a luaM_new
|
|
|
|
** node for the given reference and also return its pointer.
|
|
|
|
*/
|
|
|
|
TObject *luaH_set (Hash *t, TObject *ref)
|
|
|
|
{
|
|
|
|
Node *n = node(t, present(t, ref));
|
|
|
|
if (ttype(ref(n)) == LUA_T_NIL) {
|
1998-08-12 00:38:34 +08:00
|
|
|
if ((long)nuse(t)*3L > (long)nhash(t)*2L) {
|
1997-09-17 03:25:59 +08:00
|
|
|
rehash(t);
|
|
|
|
n = node(t, present(t, ref));
|
|
|
|
}
|
1998-08-12 00:38:34 +08:00
|
|
|
nuse(t)++;
|
1997-09-17 03:25:59 +08:00
|
|
|
*ref(n) = *ref;
|
|
|
|
ttype(val(n)) = LUA_T_NIL;
|
|
|
|
}
|
|
|
|
return (val(n));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-12-30 21:14:46 +08:00
|
|
|
static Node *hashnext (Hash *t, int i) {
|
1997-09-17 03:25:59 +08:00
|
|
|
Node *n;
|
|
|
|
int tsize = nhash(t);
|
|
|
|
if (i >= tsize)
|
|
|
|
return NULL;
|
|
|
|
n = node(t, i);
|
|
|
|
while (ttype(ref(n)) == LUA_T_NIL || ttype(val(n)) == LUA_T_NIL) {
|
|
|
|
if (++i >= tsize)
|
|
|
|
return NULL;
|
|
|
|
n = node(t, i);
|
|
|
|
}
|
|
|
|
return node(t, i);
|
|
|
|
}
|
|
|
|
|
1998-12-30 21:14:46 +08:00
|
|
|
Node *luaH_next (Hash *t, TObject *r) {
|
1997-09-17 03:25:59 +08:00
|
|
|
if (ttype(r) == LUA_T_NIL)
|
|
|
|
return hashnext(t, 0);
|
|
|
|
else {
|
|
|
|
int i = present(t, r);
|
|
|
|
Node *n = node(t, i);
|
|
|
|
luaL_arg_check(ttype(ref(n))!=LUA_T_NIL && ttype(val(n))!=LUA_T_NIL,
|
|
|
|
2, "key not found");
|
|
|
|
return hashnext(t, i+1);
|
|
|
|
}
|
|
|
|
}
|
1998-07-13 00:15:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
void luaH_setint (Hash *t, int ref, TObject *val) {
|
|
|
|
TObject index;
|
|
|
|
ttype(&index) = LUA_T_NUMBER;
|
|
|
|
nvalue(&index) = ref;
|
|
|
|
*(luaH_set(t, &index)) = *val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TObject *luaH_getint (Hash *t, int ref) {
|
|
|
|
TObject index;
|
|
|
|
ttype(&index) = LUA_T_NUMBER;
|
|
|
|
nvalue(&index) = ref;
|
|
|
|
return luaH_get(t, &index);
|
|
|
|
}
|
|
|
|
|