1997-09-17 03:25:59 +08:00
|
|
|
/*
|
1999-10-05 01:51:04 +08:00
|
|
|
** $Id: ltable.c,v 1.24 1999/09/22 14:38:45 roberto Exp roberto $
|
1997-09-17 03:25:59 +08:00
|
|
|
** Lua tables (hash)
|
|
|
|
** See Copyright Notice in lua.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;
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-08-17 04:52:00 +08:00
|
|
|
static long int hashindex (const TObject *ref) {
|
1997-09-17 03:25:59 +08:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-17 04:52:00 +08:00
|
|
|
Node *luaH_present (const Hash *t, const TObject *key) {
|
1999-09-22 22:38:45 +08:00
|
|
|
const int tsize = nhash(t);
|
|
|
|
const long int h = hashindex(key);
|
1997-09-17 03:25:59 +08:00
|
|
|
int h1 = h%tsize;
|
1999-01-23 02:47:23 +08:00
|
|
|
Node *n = node(t, h1);
|
1999-01-26 01:41:19 +08:00
|
|
|
/* keep looking until an entry with "ref" equal to key or nil */
|
|
|
|
while ((ttype(ref(n)) == ttype(key)) ? !luaO_equalval(key, ref(n))
|
|
|
|
: ttype(ref(n)) != LUA_T_NIL) {
|
|
|
|
h1 += (h&(tsize-2)) + 1; /* double hashing */
|
|
|
|
if (h1 >= tsize) h1 -= tsize;
|
|
|
|
n = node(t, h1);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
1999-01-23 02:47:23 +08:00
|
|
|
return n;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-01-23 02:47:23 +08:00
|
|
|
static Node *hashnodecreate (int nhash) {
|
1999-09-22 22:38:45 +08:00
|
|
|
Node *const v = luaM_newvector(nhash, Node);
|
1999-01-23 02:47:23 +08:00
|
|
|
int i;
|
|
|
|
for (i=0; i<nhash; i++)
|
1999-01-25 20:30:11 +08:00
|
|
|
ttype(ref(&v[i])) = ttype(val(&v[i])) = LUA_T_NIL;
|
1999-01-23 02:47:23 +08:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-08-12 00:38:34 +08:00
|
|
|
Hash *luaH_new (int nhash) {
|
1999-09-22 22:38:45 +08:00
|
|
|
Hash *const 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;
|
1999-10-05 01:51:04 +08:00
|
|
|
t->next = L->roottable;
|
|
|
|
L->roottable = t;
|
|
|
|
t->marked = 0;
|
1997-11-20 01:29:23 +08:00
|
|
|
L->nblocks += gcsize(nhash);
|
1997-09-17 03:25:59 +08:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-05 01:51:04 +08:00
|
|
|
void luaH_free (Hash *t) {
|
|
|
|
L->nblocks -= gcsize(t->nhash);
|
|
|
|
luaM_free(nodevector(t));
|
|
|
|
luaM_free(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-08-11 05:36:32 +08:00
|
|
|
static int newsize (Hash *t) {
|
1999-09-22 22:38:45 +08:00
|
|
|
Node *const v = t->node;
|
|
|
|
const int size = nhash(t);
|
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++) {
|
1999-01-25 20:30:11 +08:00
|
|
|
if (ttype(val(v+i)) != LUA_T_NIL)
|
1998-01-29 00:50:33 +08:00
|
|
|
realuse++;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
1999-09-22 22:38:45 +08:00
|
|
|
return luaO_redimension(realuse*2);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
1999-01-23 02:47:23 +08:00
|
|
|
|
1998-08-12 00:38:34 +08:00
|
|
|
static void rehash (Hash *t) {
|
1999-09-22 22:38:45 +08:00
|
|
|
const int nold = nhash(t);
|
|
|
|
Node *const vold = nodevector(t);
|
|
|
|
const 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;
|
1999-01-25 20:30:11 +08:00
|
|
|
if (ttype(val(n)) != LUA_T_NIL) {
|
|
|
|
*luaH_present(t, ref(n)) = *n; /* copy old node to 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);
|
|
|
|
}
|
|
|
|
|
1999-01-23 02:47:23 +08:00
|
|
|
|
1999-08-17 04:52:00 +08:00
|
|
|
void luaH_set (Hash *t, const TObject *ref, const TObject *val) {
|
1999-09-22 22:38:45 +08:00
|
|
|
Node *const n = luaH_present(t, ref);
|
|
|
|
*val(n) = *val;
|
|
|
|
if (ttype(ref(n)) == LUA_T_NIL) { /* new node? */
|
|
|
|
*ref(n) = *ref; /* set key */
|
|
|
|
nuse(t)++; /* count it */
|
|
|
|
if ((long)nuse(t)*3L > (long)nhash(t)*2L) /* check size */
|
1997-09-17 03:25:59 +08:00
|
|
|
rehash(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-17 04:52:00 +08:00
|
|
|
int luaH_pos (const Hash *t, const TObject *r) {
|
1999-09-22 22:38:45 +08:00
|
|
|
Node *const n = luaH_present(t, r);
|
1999-02-23 22:57:28 +08:00
|
|
|
luaL_arg_check(ttype(val(n)) != LUA_T_NIL, 2, "key not found");
|
|
|
|
return n-(t->node);
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
1998-07-13 00:15:19 +08:00
|
|
|
|
|
|
|
|
1999-08-17 04:52:00 +08:00
|
|
|
void luaH_setint (Hash *t, int ref, const TObject *val) {
|
1998-07-13 00:15:19 +08:00
|
|
|
TObject index;
|
|
|
|
ttype(&index) = LUA_T_NUMBER;
|
|
|
|
nvalue(&index) = ref;
|
1999-01-26 01:41:19 +08:00
|
|
|
luaH_set(t, &index, val);
|
1998-07-13 00:15:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-17 04:52:00 +08:00
|
|
|
TObject *luaH_getint (const Hash *t, int ref) {
|
1998-07-13 00:15:19 +08:00
|
|
|
TObject index;
|
|
|
|
ttype(&index) = LUA_T_NUMBER;
|
|
|
|
nvalue(&index) = ref;
|
|
|
|
return luaH_get(t, &index);
|
|
|
|
}
|
|
|
|
|