Browse Source

table entries with ref=null always have val=null too.

v5-2
Roberto Ierusalimschy 26 years ago
parent
commit
1b45e967b4
  1. 4
      lbuiltin.c
  2. 43
      ltable.c
  3. 6
      ltable.h

4
lbuiltin.c

@ -1,5 +1,5 @@
/*
** $Id: lbuiltin.c,v 1.45 1999/01/04 17:34:49 roberto Exp roberto $
** $Id: lbuiltin.c,v 1.46 1999/01/22 18:46:11 roberto Exp roberto $
** Built-in functions
** See Copyright Notice in lua.h
*/
@ -348,7 +348,7 @@ static void luaB_foreach (void) {
luaD_checkstack(3); /* for f, ref, and val */
for (i=0; i<a->nhash; i++) {
Node *nd = &(a->node[i]);
if (ttype(ref(nd)) != LUA_T_NIL && ttype(val(nd)) != LUA_T_NIL) {
if (ttype(val(nd)) != LUA_T_NIL) {
*(L->stack.top++) = *f;
*(L->stack.top++) = *ref(nd);
*(L->stack.top++) = *val(nd);

43
ltable.c

@ -1,5 +1,5 @@
/*
** $Id: ltable.c,v 1.17 1999/01/04 12:54:33 roberto Exp $
** $Id: ltable.c,v 1.18 1999/01/22 18:47:23 roberto Exp roberto $
** Lua tables (hash)
** See Copyright Notice in lua.h
*/
@ -53,20 +53,20 @@ static long int hashindex (TObject *ref) {
}
static Node *present (Hash *t, TObject *key) {
Node *luaH_present (Hash *t, TObject *ref) {
int tsize = nhash(t);
long int h = hashindex(key);
long int h = hashindex(ref);
int h1 = h%tsize;
Node *n = node(t, h1);
/* keep looking until an entry with "ref" equal to key or nil */
if ((ttype(ref(n)) == ttype(key) ? !luaO_equalval(key, ref(n))
/* keep looking until an entry with "ref" equal to ref or nil */
if ((ttype(ref(n)) == ttype(ref) ? !luaO_equalval(ref, ref(n))
: ttype(ref(n)) != LUA_T_NIL)) {
int h2 = h%(tsize-2) + 1;
do {
h1 += h2;
if (h1 >= tsize) h1 -= tsize;
n = node(t, h1);
} while ((ttype(ref(n)) == ttype(key) ? !luaO_equalval(key, ref(n))
} while ((ttype(ref(n)) == ttype(ref) ? !luaO_equalval(ref, ref(n))
: ttype(ref(n)) != LUA_T_NIL));
}
return n;
@ -88,7 +88,7 @@ static Node *hashnodecreate (int nhash) {
Node *v = luaM_newvector(nhash, Node);
int i;
for (i=0; i<nhash; i++)
ttype(ref(&v[i])) = LUA_T_NIL;
ttype(ref(&v[i])) = ttype(val(&v[i])) = LUA_T_NIL;
return v;
}
@ -112,7 +112,7 @@ static int newsize (Hash *t) {
int realuse = 0;
int i;
for (i=0; i<size; i++) {
if (ttype(ref(v+i)) != LUA_T_NIL && ttype(val(v+i)) != LUA_T_NIL)
if (ttype(val(v+i)) != LUA_T_NIL)
realuse++;
}
return luaO_redimension((realuse+1)*2); /* +1 is the new element */
@ -129,8 +129,8 @@ static void rehash (Hash *t) {
nuse(t) = 0;
for (i=0; i<nold; i++) {
Node *n = vold+i;
if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL) {
*present(t, ref(n)) = *n; /* copy old node to new hash */
if (ttype(val(n)) != LUA_T_NIL) {
*luaH_present(t, ref(n)) = *n; /* copy old node to new hash */
nuse(t)++;
}
}
@ -139,31 +139,19 @@ static void rehash (Hash *t) {
}
/*
** If the hash node is present, return its pointer, otherwise return
** null.
*/
TObject *luaH_get (Hash *t, TObject *ref) {
Node *n = present(t, ref);
if (ttype(ref(n)) != LUA_T_NIL) return val(n);
else return &luaO_nilobject;
}
/*
** If the hash node is present, return its pointer, otherwise create a new
** node for the given reference and also return its pointer.
*/
TObject *luaH_set (Hash *t, TObject *ref) {
Node *n = present(t, ref);
Node *n = luaH_present(t, ref);
if (ttype(ref(n)) == LUA_T_NIL) {
if ((long)nuse(t)*3L > (long)nhash(t)*2L) {
rehash(t);
n = present(t, ref);
n = luaH_present(t, ref);
}
nuse(t)++;
*ref(n) = *ref;
ttype(val(n)) = LUA_T_NIL;
}
return (val(n));
}
@ -175,7 +163,7 @@ static Node *hashnext (Hash *t, int i) {
if (i >= tsize)
return NULL;
n = node(t, i);
while (ttype(ref(n)) == LUA_T_NIL || ttype(val(n)) == LUA_T_NIL) {
while (ttype(val(n)) == LUA_T_NIL) {
if (++i >= tsize)
return NULL;
n = node(t, i);
@ -187,9 +175,8 @@ Node *luaH_next (Hash *t, TObject *r) {
if (ttype(r) == LUA_T_NIL)
return hashnext(t, 0);
else {
Node *n = present(t, r);
luaL_arg_check(ttype(ref(n))!=LUA_T_NIL && ttype(val(n))!=LUA_T_NIL,
2, "key not found");
Node *n = luaH_present(t, r);
luaL_arg_check(ttype(val(n)) != LUA_T_NIL, 2, "key not found");
return hashnext(t, (n-(t->node))+1);
}
}

6
ltable.h

@ -1,5 +1,5 @@
/*
** $Id: ltable.h,v 1.7 1998/12/30 13:14:46 roberto Exp $
** $Id: ltable.h,v 1.8 1999/01/04 12:54:33 roberto Exp roberto $
** Lua tables (hash)
** See Copyright Notice in lua.h
*/
@ -15,9 +15,11 @@
#define val(n) (&(n)->val)
#define nhash(t) ((t)->nhash)
#define luaH_get(t,ref) (val(luaH_present((t), (ref))))
Hash *luaH_new (int nhash);
void luaH_free (Hash *frees);
TObject *luaH_get (Hash *t, TObject *ref);
Node *luaH_present (Hash *t, TObject *ref);
TObject *luaH_set (Hash *t, TObject *ref);
Node *luaH_next (Hash *t, TObject *r);
void luaH_setint (Hash *t, int ref, TObject *val);

Loading…
Cancel
Save