mirror of
https://github.com/lua/lua.git
synced 2025-02-04 06:13:04 +08:00
table entries with ref=null always have val=null too.
This commit is contained in:
parent
933bead92e
commit
1b45e967b4
@ -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
|
** Built-in functions
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@ -348,7 +348,7 @@ static void luaB_foreach (void) {
|
|||||||
luaD_checkstack(3); /* for f, ref, and val */
|
luaD_checkstack(3); /* for f, ref, and val */
|
||||||
for (i=0; i<a->nhash; i++) {
|
for (i=0; i<a->nhash; i++) {
|
||||||
Node *nd = &(a->node[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++) = *f;
|
||||||
*(L->stack.top++) = *ref(nd);
|
*(L->stack.top++) = *ref(nd);
|
||||||
*(L->stack.top++) = *val(nd);
|
*(L->stack.top++) = *val(nd);
|
||||||
|
43
ltable.c
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)
|
** Lua tables (hash)
|
||||||
** See Copyright Notice in lua.h
|
** 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);
|
int tsize = nhash(t);
|
||||||
long int h = hashindex(key);
|
long int h = hashindex(ref);
|
||||||
int h1 = h%tsize;
|
int h1 = h%tsize;
|
||||||
Node *n = node(t, h1);
|
Node *n = node(t, h1);
|
||||||
/* keep looking until an entry with "ref" equal to key or nil */
|
/* keep looking until an entry with "ref" equal to ref or nil */
|
||||||
if ((ttype(ref(n)) == ttype(key) ? !luaO_equalval(key, ref(n))
|
if ((ttype(ref(n)) == ttype(ref) ? !luaO_equalval(ref, ref(n))
|
||||||
: ttype(ref(n)) != LUA_T_NIL)) {
|
: ttype(ref(n)) != LUA_T_NIL)) {
|
||||||
int h2 = h%(tsize-2) + 1;
|
int h2 = h%(tsize-2) + 1;
|
||||||
do {
|
do {
|
||||||
h1 += h2;
|
h1 += h2;
|
||||||
if (h1 >= tsize) h1 -= tsize;
|
if (h1 >= tsize) h1 -= tsize;
|
||||||
n = node(t, h1);
|
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));
|
: ttype(ref(n)) != LUA_T_NIL));
|
||||||
}
|
}
|
||||||
return n;
|
return n;
|
||||||
@ -88,7 +88,7 @@ static Node *hashnodecreate (int nhash) {
|
|||||||
Node *v = luaM_newvector(nhash, Node);
|
Node *v = luaM_newvector(nhash, Node);
|
||||||
int i;
|
int i;
|
||||||
for (i=0; i<nhash; 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;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,7 +112,7 @@ static int newsize (Hash *t) {
|
|||||||
int realuse = 0;
|
int realuse = 0;
|
||||||
int i;
|
int i;
|
||||||
for (i=0; i<size; 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++;
|
realuse++;
|
||||||
}
|
}
|
||||||
return luaO_redimension((realuse+1)*2); /* +1 is the new element */
|
return luaO_redimension((realuse+1)*2); /* +1 is the new element */
|
||||||
@ -129,8 +129,8 @@ static void rehash (Hash *t) {
|
|||||||
nuse(t) = 0;
|
nuse(t) = 0;
|
||||||
for (i=0; i<nold; i++) {
|
for (i=0; i<nold; i++) {
|
||||||
Node *n = vold+i;
|
Node *n = vold+i;
|
||||||
if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL) {
|
if (ttype(val(n)) != LUA_T_NIL) {
|
||||||
*present(t, ref(n)) = *n; /* copy old node to new hash */
|
*luaH_present(t, ref(n)) = *n; /* copy old node to new hash */
|
||||||
nuse(t)++;
|
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
|
** If the hash node is present, return its pointer, otherwise create a new
|
||||||
** node for the given reference and also return its pointer.
|
** node for the given reference and also return its pointer.
|
||||||
*/
|
*/
|
||||||
TObject *luaH_set (Hash *t, TObject *ref) {
|
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 (ttype(ref(n)) == LUA_T_NIL) {
|
||||||
if ((long)nuse(t)*3L > (long)nhash(t)*2L) {
|
if ((long)nuse(t)*3L > (long)nhash(t)*2L) {
|
||||||
rehash(t);
|
rehash(t);
|
||||||
n = present(t, ref);
|
n = luaH_present(t, ref);
|
||||||
}
|
}
|
||||||
nuse(t)++;
|
nuse(t)++;
|
||||||
*ref(n) = *ref;
|
*ref(n) = *ref;
|
||||||
ttype(val(n)) = LUA_T_NIL;
|
|
||||||
}
|
}
|
||||||
return (val(n));
|
return (val(n));
|
||||||
}
|
}
|
||||||
@ -175,7 +163,7 @@ static Node *hashnext (Hash *t, int i) {
|
|||||||
if (i >= tsize)
|
if (i >= tsize)
|
||||||
return NULL;
|
return NULL;
|
||||||
n = node(t, i);
|
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)
|
if (++i >= tsize)
|
||||||
return NULL;
|
return NULL;
|
||||||
n = node(t, i);
|
n = node(t, i);
|
||||||
@ -187,9 +175,8 @@ Node *luaH_next (Hash *t, TObject *r) {
|
|||||||
if (ttype(r) == LUA_T_NIL)
|
if (ttype(r) == LUA_T_NIL)
|
||||||
return hashnext(t, 0);
|
return hashnext(t, 0);
|
||||||
else {
|
else {
|
||||||
Node *n = present(t, r);
|
Node *n = luaH_present(t, r);
|
||||||
luaL_arg_check(ttype(ref(n))!=LUA_T_NIL && ttype(val(n))!=LUA_T_NIL,
|
luaL_arg_check(ttype(val(n)) != LUA_T_NIL, 2, "key not found");
|
||||||
2, "key not found");
|
|
||||||
return hashnext(t, (n-(t->node))+1);
|
return hashnext(t, (n-(t->node))+1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
6
ltable.h
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)
|
** Lua tables (hash)
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@ -15,9 +15,11 @@
|
|||||||
#define val(n) (&(n)->val)
|
#define val(n) (&(n)->val)
|
||||||
#define nhash(t) ((t)->nhash)
|
#define nhash(t) ((t)->nhash)
|
||||||
|
|
||||||
|
#define luaH_get(t,ref) (val(luaH_present((t), (ref))))
|
||||||
|
|
||||||
Hash *luaH_new (int nhash);
|
Hash *luaH_new (int nhash);
|
||||||
void luaH_free (Hash *frees);
|
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);
|
TObject *luaH_set (Hash *t, TObject *ref);
|
||||||
Node *luaH_next (Hash *t, TObject *r);
|
Node *luaH_next (Hash *t, TObject *r);
|
||||||
void luaH_setint (Hash *t, int ref, TObject *val);
|
void luaH_setint (Hash *t, int ref, TObject *val);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user