diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-01-25 10:30:11 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-01-25 10:30:11 -0200 |
| commit | 1b45e967b4dcb234551c2a731147b111584f4145 (patch) | |
| tree | 4a257873ccadd2779df799eb8f41f146407a4081 | |
| parent | 933bead92e48cdd8f2c9b5953854270e98d58c9a (diff) | |
| download | lua-1b45e967b4dcb234551c2a731147b111584f4145.tar.gz lua-1b45e967b4dcb234551c2a731147b111584f4145.tar.bz2 lua-1b45e967b4dcb234551c2a731147b111584f4145.zip | |
table entries with ref=null always have val=null too.
| -rw-r--r-- | lbuiltin.c | 4 | ||||
| -rw-r--r-- | ltable.c | 43 | ||||
| -rw-r--r-- | ltable.h | 6 |
3 files changed, 21 insertions, 32 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbuiltin.c,v 1.45 1999/01/04 17:34:49 roberto Exp roberto $ | 2 | ** $Id: lbuiltin.c,v 1.46 1999/01/22 18:46:11 roberto Exp roberto $ |
| 3 | ** Built-in functions | 3 | ** Built-in functions |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -348,7 +348,7 @@ static void luaB_foreach (void) { | |||
| 348 | luaD_checkstack(3); /* for f, ref, and val */ | 348 | luaD_checkstack(3); /* for f, ref, and val */ |
| 349 | for (i=0; i<a->nhash; i++) { | 349 | for (i=0; i<a->nhash; i++) { |
| 350 | Node *nd = &(a->node[i]); | 350 | Node *nd = &(a->node[i]); |
| 351 | if (ttype(ref(nd)) != LUA_T_NIL && ttype(val(nd)) != LUA_T_NIL) { | 351 | if (ttype(val(nd)) != LUA_T_NIL) { |
| 352 | *(L->stack.top++) = *f; | 352 | *(L->stack.top++) = *f; |
| 353 | *(L->stack.top++) = *ref(nd); | 353 | *(L->stack.top++) = *ref(nd); |
| 354 | *(L->stack.top++) = *val(nd); | 354 | *(L->stack.top++) = *val(nd); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltable.c,v 1.17 1999/01/04 12:54:33 roberto Exp $ | 2 | ** $Id: ltable.c,v 1.18 1999/01/22 18:47:23 roberto Exp roberto $ |
| 3 | ** Lua tables (hash) | 3 | ** Lua tables (hash) |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -53,20 +53,20 @@ static long int hashindex (TObject *ref) { | |||
| 53 | } | 53 | } |
| 54 | 54 | ||
| 55 | 55 | ||
| 56 | static Node *present (Hash *t, TObject *key) { | 56 | Node *luaH_present (Hash *t, TObject *ref) { |
| 57 | int tsize = nhash(t); | 57 | int tsize = nhash(t); |
| 58 | long int h = hashindex(key); | 58 | long int h = hashindex(ref); |
| 59 | int h1 = h%tsize; | 59 | int h1 = h%tsize; |
| 60 | Node *n = node(t, h1); | 60 | Node *n = node(t, h1); |
| 61 | /* keep looking until an entry with "ref" equal to key or nil */ | 61 | /* keep looking until an entry with "ref" equal to ref or nil */ |
| 62 | if ((ttype(ref(n)) == ttype(key) ? !luaO_equalval(key, ref(n)) | 62 | if ((ttype(ref(n)) == ttype(ref) ? !luaO_equalval(ref, ref(n)) |
| 63 | : ttype(ref(n)) != LUA_T_NIL)) { | 63 | : ttype(ref(n)) != LUA_T_NIL)) { |
| 64 | int h2 = h%(tsize-2) + 1; | 64 | int h2 = h%(tsize-2) + 1; |
| 65 | do { | 65 | do { |
| 66 | h1 += h2; | 66 | h1 += h2; |
| 67 | if (h1 >= tsize) h1 -= tsize; | 67 | if (h1 >= tsize) h1 -= tsize; |
| 68 | n = node(t, h1); | 68 | n = node(t, h1); |
| 69 | } while ((ttype(ref(n)) == ttype(key) ? !luaO_equalval(key, ref(n)) | 69 | } while ((ttype(ref(n)) == ttype(ref) ? !luaO_equalval(ref, ref(n)) |
| 70 | : ttype(ref(n)) != LUA_T_NIL)); | 70 | : ttype(ref(n)) != LUA_T_NIL)); |
| 71 | } | 71 | } |
| 72 | return n; | 72 | return n; |
| @@ -88,7 +88,7 @@ static Node *hashnodecreate (int nhash) { | |||
| 88 | Node *v = luaM_newvector(nhash, Node); | 88 | Node *v = luaM_newvector(nhash, Node); |
| 89 | int i; | 89 | int i; |
| 90 | for (i=0; i<nhash; i++) | 90 | for (i=0; i<nhash; i++) |
| 91 | ttype(ref(&v[i])) = LUA_T_NIL; | 91 | ttype(ref(&v[i])) = ttype(val(&v[i])) = LUA_T_NIL; |
| 92 | return v; | 92 | return v; |
| 93 | } | 93 | } |
| 94 | 94 | ||
| @@ -112,7 +112,7 @@ static int newsize (Hash *t) { | |||
| 112 | int realuse = 0; | 112 | int realuse = 0; |
| 113 | int i; | 113 | int i; |
| 114 | for (i=0; i<size; i++) { | 114 | for (i=0; i<size; i++) { |
| 115 | if (ttype(ref(v+i)) != LUA_T_NIL && ttype(val(v+i)) != LUA_T_NIL) | 115 | if (ttype(val(v+i)) != LUA_T_NIL) |
| 116 | realuse++; | 116 | realuse++; |
| 117 | } | 117 | } |
| 118 | return luaO_redimension((realuse+1)*2); /* +1 is the new element */ | 118 | return luaO_redimension((realuse+1)*2); /* +1 is the new element */ |
| @@ -129,8 +129,8 @@ static void rehash (Hash *t) { | |||
| 129 | nuse(t) = 0; | 129 | nuse(t) = 0; |
| 130 | for (i=0; i<nold; i++) { | 130 | for (i=0; i<nold; i++) { |
| 131 | Node *n = vold+i; | 131 | Node *n = vold+i; |
| 132 | if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL) { | 132 | if (ttype(val(n)) != LUA_T_NIL) { |
| 133 | *present(t, ref(n)) = *n; /* copy old node to new hash */ | 133 | *luaH_present(t, ref(n)) = *n; /* copy old node to new hash */ |
| 134 | nuse(t)++; | 134 | nuse(t)++; |
| 135 | } | 135 | } |
| 136 | } | 136 | } |
| @@ -140,30 +140,18 @@ static void rehash (Hash *t) { | |||
| 140 | 140 | ||
| 141 | 141 | ||
| 142 | /* | 142 | /* |
| 143 | ** If the hash node is present, return its pointer, otherwise return | ||
| 144 | ** null. | ||
| 145 | */ | ||
| 146 | TObject *luaH_get (Hash *t, TObject *ref) { | ||
| 147 | Node *n = present(t, ref); | ||
| 148 | if (ttype(ref(n)) != LUA_T_NIL) return val(n); | ||
| 149 | else return &luaO_nilobject; | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | /* | ||
| 154 | ** If the hash node is present, return its pointer, otherwise create a new | 143 | ** If the hash node is present, return its pointer, otherwise create a new |
| 155 | ** node for the given reference and also return its pointer. | 144 | ** node for the given reference and also return its pointer. |
| 156 | */ | 145 | */ |
| 157 | TObject *luaH_set (Hash *t, TObject *ref) { | 146 | TObject *luaH_set (Hash *t, TObject *ref) { |
| 158 | Node *n = present(t, ref); | 147 | Node *n = luaH_present(t, ref); |
| 159 | if (ttype(ref(n)) == LUA_T_NIL) { | 148 | if (ttype(ref(n)) == LUA_T_NIL) { |
| 160 | if ((long)nuse(t)*3L > (long)nhash(t)*2L) { | 149 | if ((long)nuse(t)*3L > (long)nhash(t)*2L) { |
| 161 | rehash(t); | 150 | rehash(t); |
| 162 | n = present(t, ref); | 151 | n = luaH_present(t, ref); |
| 163 | } | 152 | } |
| 164 | nuse(t)++; | 153 | nuse(t)++; |
| 165 | *ref(n) = *ref; | 154 | *ref(n) = *ref; |
| 166 | ttype(val(n)) = LUA_T_NIL; | ||
| 167 | } | 155 | } |
| 168 | return (val(n)); | 156 | return (val(n)); |
| 169 | } | 157 | } |
| @@ -175,7 +163,7 @@ static Node *hashnext (Hash *t, int i) { | |||
| 175 | if (i >= tsize) | 163 | if (i >= tsize) |
| 176 | return NULL; | 164 | return NULL; |
| 177 | n = node(t, i); | 165 | n = node(t, i); |
| 178 | while (ttype(ref(n)) == LUA_T_NIL || ttype(val(n)) == LUA_T_NIL) { | 166 | while (ttype(val(n)) == LUA_T_NIL) { |
| 179 | if (++i >= tsize) | 167 | if (++i >= tsize) |
| 180 | return NULL; | 168 | return NULL; |
| 181 | n = node(t, i); | 169 | n = node(t, i); |
| @@ -187,9 +175,8 @@ Node *luaH_next (Hash *t, TObject *r) { | |||
| 187 | if (ttype(r) == LUA_T_NIL) | 175 | if (ttype(r) == LUA_T_NIL) |
| 188 | return hashnext(t, 0); | 176 | return hashnext(t, 0); |
| 189 | else { | 177 | else { |
| 190 | Node *n = present(t, r); | 178 | Node *n = luaH_present(t, r); |
| 191 | luaL_arg_check(ttype(ref(n))!=LUA_T_NIL && ttype(val(n))!=LUA_T_NIL, | 179 | luaL_arg_check(ttype(val(n)) != LUA_T_NIL, 2, "key not found"); |
| 192 | 2, "key not found"); | ||
| 193 | return hashnext(t, (n-(t->node))+1); | 180 | return hashnext(t, (n-(t->node))+1); |
| 194 | } | 181 | } |
| 195 | } | 182 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltable.h,v 1.7 1998/12/30 13:14:46 roberto Exp $ | 2 | ** $Id: ltable.h,v 1.8 1999/01/04 12:54:33 roberto Exp roberto $ |
| 3 | ** Lua tables (hash) | 3 | ** Lua tables (hash) |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -15,9 +15,11 @@ | |||
| 15 | #define val(n) (&(n)->val) | 15 | #define val(n) (&(n)->val) |
| 16 | #define nhash(t) ((t)->nhash) | 16 | #define nhash(t) ((t)->nhash) |
| 17 | 17 | ||
| 18 | #define luaH_get(t,ref) (val(luaH_present((t), (ref)))) | ||
| 19 | |||
| 18 | Hash *luaH_new (int nhash); | 20 | Hash *luaH_new (int nhash); |
| 19 | void luaH_free (Hash *frees); | 21 | void luaH_free (Hash *frees); |
| 20 | TObject *luaH_get (Hash *t, TObject *ref); | 22 | Node *luaH_present (Hash *t, TObject *ref); |
| 21 | TObject *luaH_set (Hash *t, TObject *ref); | 23 | TObject *luaH_set (Hash *t, TObject *ref); |
| 22 | Node *luaH_next (Hash *t, TObject *r); | 24 | Node *luaH_next (Hash *t, TObject *r); |
| 23 | void luaH_setint (Hash *t, int ref, TObject *val); | 25 | void luaH_setint (Hash *t, int ref, TObject *val); |
