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 /ltable.c | |
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.
Diffstat (limited to 'ltable.c')
-rw-r--r-- | ltable.c | 43 |
1 files changed, 15 insertions, 28 deletions
@@ -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 | } |