diff options
Diffstat (limited to 'ltable.c')
-rw-r--r-- | ltable.c | 39 |
1 files changed, 22 insertions, 17 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.c,v 1.70 2001/01/26 15:58:50 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 1.71 2001/01/29 13:02:20 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 | */ |
@@ -40,14 +40,14 @@ | |||
40 | ** returns the `main' position of an element in a table (that is, the index | 40 | ** returns the `main' position of an element in a table (that is, the index |
41 | ** of its hash value) | 41 | ** of its hash value) |
42 | */ | 42 | */ |
43 | Node *luaH_mainposition (const Hash *t, const TObject *key) { | 43 | Node *luaH_mainposition (const Hash *t, const Node *n) { |
44 | switch (ttype(key)) { | 44 | switch (ttype_key(n)) { |
45 | case LUA_TNUMBER: | 45 | case LUA_TNUMBER: |
46 | return hashnum(t, nvalue(key)); | 46 | return hashnum(t, nvalue_key(n)); |
47 | case LUA_TSTRING: | 47 | case LUA_TSTRING: |
48 | return hashstr(t, tsvalue(key)); | 48 | return hashstr(t, tsvalue_key(n)); |
49 | default: /* all other types are hashed as (void *) */ | 49 | default: /* all other types are hashed as (void *) */ |
50 | return hashpointer(t, hvalue(key)); | 50 | return hashpointer(t, tsvalue_key(n)); |
51 | } | 51 | } |
52 | } | 52 | } |
53 | 53 | ||
@@ -87,9 +87,9 @@ static void setnodevector (lua_State *L, Hash *t, luint32 size) { | |||
87 | luaD_error(L, "table overflow"); | 87 | luaD_error(L, "table overflow"); |
88 | t->node = luaM_newvector(L, size, Node); | 88 | t->node = luaM_newvector(L, size, Node); |
89 | for (i=0; i<(int)size; i++) { | 89 | for (i=0; i<(int)size; i++) { |
90 | setnilvalue(&t->node[i].key); | ||
91 | setnilvalue(&t->node[i].val); | ||
92 | t->node[i].next = NULL; | 90 | t->node[i].next = NULL; |
91 | t->node[i].key_tt = LUA_TNIL; | ||
92 | setnilvalue(&t->node[i].val); | ||
93 | } | 93 | } |
94 | t->size = size; | 94 | t->size = size; |
95 | t->firstfree = &t->node[size-1]; /* first free position to be used */ | 95 | t->firstfree = &t->node[size-1]; /* first free position to be used */ |
@@ -143,8 +143,13 @@ static void rehash (lua_State *L, Hash *t) { | |||
143 | setnodevector(L, t, oldsize); | 143 | setnodevector(L, t, oldsize); |
144 | for (i=0; i<oldsize; i++) { | 144 | for (i=0; i<oldsize; i++) { |
145 | Node *old = nold+i; | 145 | Node *old = nold+i; |
146 | if (ttype(&old->val) != LUA_TNIL) | 146 | if (ttype(&old->val) != LUA_TNIL) { |
147 | setobj(luaH_set(L, t, &old->key), &old->val); | 147 | TObject o; |
148 | TObject *v; | ||
149 | setkey2obj(&o, old); | ||
150 | v = luaH_set(L, t, &o); | ||
151 | setobj(v, &old->val); | ||
152 | } | ||
148 | } | 153 | } |
149 | luaM_freearray(L, nold, oldsize, Node); /* free old array */ | 154 | luaM_freearray(L, nold, oldsize, Node); /* free old array */ |
150 | } | 155 | } |
@@ -159,7 +164,7 @@ static void rehash (lua_State *L, Hash *t) { | |||
159 | */ | 164 | */ |
160 | static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) { | 165 | static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) { |
161 | if (ttype(&mp->val) != LUA_TNIL) { /* main position is not free? */ | 166 | if (ttype(&mp->val) != LUA_TNIL) { /* main position is not free? */ |
162 | Node *othern = luaH_mainposition(t, &mp->key); /* `mp' of colliding node */ | 167 | Node *othern = luaH_mainposition(t, mp); /* `mp' of colliding node */ |
163 | Node *n = t->firstfree; /* get a free place */ | 168 | Node *n = t->firstfree; /* get a free place */ |
164 | if (othern != mp) { /* is colliding node out of its main position? */ | 169 | if (othern != mp) { /* is colliding node out of its main position? */ |
165 | /* yes; move colliding node into free position */ | 170 | /* yes; move colliding node into free position */ |
@@ -176,10 +181,10 @@ static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) { | |||
176 | mp = n; | 181 | mp = n; |
177 | } | 182 | } |
178 | } | 183 | } |
179 | setobj(&mp->key, key); | 184 | setobj2key(mp, key); |
180 | lua_assert(ttype(&mp->val) == LUA_TNIL); | 185 | lua_assert(ttype(&mp->val) == LUA_TNIL); |
181 | for (;;) { /* correct `firstfree' */ | 186 | for (;;) { /* correct `firstfree' */ |
182 | if (ttype(&t->firstfree->key) == LUA_TNIL) | 187 | if (ttype_key(t->firstfree) == LUA_TNIL) |
183 | return &mp->val; /* OK; table still has a free place */ | 188 | return &mp->val; /* OK; table still has a free place */ |
184 | else if (t->firstfree == t->node) break; /* cannot decrement from here */ | 189 | else if (t->firstfree == t->node) break; /* cannot decrement from here */ |
185 | else (t->firstfree)--; | 190 | else (t->firstfree)--; |
@@ -197,7 +202,7 @@ TObject *luaH_setnum (lua_State *L, Hash *t, lua_Number key) { | |||
197 | Node *mp = hashnum(t, key); | 202 | Node *mp = hashnum(t, key); |
198 | Node *n = mp; | 203 | Node *n = mp; |
199 | do { /* check whether `key' is somewhere in the chain */ | 204 | do { /* check whether `key' is somewhere in the chain */ |
200 | if (nvalue(&n->key) == key && ttype(&n->key) == LUA_TNUMBER) | 205 | if (nvalue_key(n) == key && ttype_key(n) == LUA_TNUMBER) |
201 | return &n->val; /* that's all */ | 206 | return &n->val; /* that's all */ |
202 | else n = n->next; | 207 | else n = n->next; |
203 | } while (n); | 208 | } while (n); |
@@ -216,7 +221,7 @@ TObject *luaH_setstr (lua_State *L, Hash *t, TString *key) { | |||
216 | Node *mp = hashstr(t, key); | 221 | Node *mp = hashstr(t, key); |
217 | Node *n = mp; | 222 | Node *n = mp; |
218 | do { /* check whether `key' is somewhere in the chain */ | 223 | do { /* check whether `key' is somewhere in the chain */ |
219 | if (tsvalue(&n->key) == key && ttype(&n->key) == LUA_TSTRING) | 224 | if (tsvalue_key(n) == key && ttype_key(n) == LUA_TSTRING) |
220 | return &n->val; /* that's all */ | 225 | return &n->val; /* that's all */ |
221 | else n = n->next; | 226 | else n = n->next; |
222 | } while (n); | 227 | } while (n); |
@@ -234,8 +239,8 @@ static TObject *luaH_setany (lua_State *L, Hash *t, const TObject *key) { | |||
234 | Node *mp = hashpointer(t, hvalue(key)); | 239 | Node *mp = hashpointer(t, hvalue(key)); |
235 | Node *n = mp; | 240 | Node *n = mp; |
236 | do { /* check whether `key' is somewhere in the chain */ | 241 | do { /* check whether `key' is somewhere in the chain */ |
237 | /* compare as `hvalue', but may be other pointers (it is the same) */ | 242 | /* compare as `tsvalue', but may be other pointers (it is the same) */ |
238 | if (hvalue(&n->key) == hvalue(key) && ttype(&n->key) == ttype(key)) | 243 | if (tsvalue_key(n) == tsvalue(key) && ttype_key(n) == ttype(key)) |
239 | return &n->val; /* that's all */ | 244 | return &n->val; /* that's all */ |
240 | else n = n->next; | 245 | else n = n->next; |
241 | } while (n); | 246 | } while (n); |