summaryrefslogtreecommitdiff
path: root/ltable.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-06-26 10:20:45 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-06-26 10:20:45 -0300
commit37f3a1c0452439bce1f5c2069ca015af148bf62f (patch)
tree0ad887fb08400103904dab1954695d28aece0500 /ltable.c
parent9559c111a32479794acc59fba2cbeab365a567f3 (diff)
downloadlua-37f3a1c0452439bce1f5c2069ca015af148bf62f.tar.gz
lua-37f3a1c0452439bce1f5c2069ca015af148bf62f.tar.bz2
lua-37f3a1c0452439bce1f5c2069ca015af148bf62f.zip
too much optimization to "break" keys in tables; keep them as TObjects...
Diffstat (limited to 'ltable.c')
-rw-r--r--ltable.c57
1 files changed, 27 insertions, 30 deletions
diff --git a/ltable.c b/ltable.c
index 4a20bb23..c51b0a81 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.c,v 1.80 2001/06/06 18:00:19 roberto Exp roberto $ 2** $Id: ltable.c,v 1.81 2001/06/15 20:36:57 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*/
@@ -32,9 +32,9 @@
32#define TagDefault LUA_TTABLE 32#define TagDefault LUA_TTABLE
33 33
34 34
35#define hashnum(t,n) (&t->node[lmod((lu_hash)(ls_hash)(n), t->size)]) 35#define hashnum(t,n) (node(t, lmod((lu_hash)(ls_hash)(n), t->size)))
36#define hashstr(t,str) (&t->node[lmod((str)->tsv.hash, t->size)]) 36#define hashstr(t,str) (node(t, lmod((str)->tsv.hash, t->size)))
37#define hashpointer(t,p) (&t->node[lmod(IntPoint(p), t->size)]) 37#define hashpointer(t,p) (node(t, lmod(IntPoint(p), t->size)))
38 38
39 39
40/* 40/*
@@ -42,13 +42,13 @@
42** of its hash value) 42** of its hash value)
43*/ 43*/
44Node *luaH_mainposition (const Hash *t, const Node *n) { 44Node *luaH_mainposition (const Hash *t, const Node *n) {
45 switch (ttype_key(n)) { 45 switch (ttype(key(n))) {
46 case LUA_TNUMBER: 46 case LUA_TNUMBER:
47 return hashnum(t, nvalue_key(n)); 47 return hashnum(t, nvalue(key(n)));
48 case LUA_TSTRING: 48 case LUA_TSTRING:
49 return hashstr(t, tsvalue_key(n)); 49 return hashstr(t, tsvalue(key(n)));
50 default: /* all other types are hashed as (void *) */ 50 default: /* all other types are hashed as (void *) */
51 return hashpointer(t, tsvalue_key(n)); 51 return hashpointer(t, tsvalue(key(n)));
52 } 52 }
53} 53}
54 54
@@ -62,7 +62,7 @@ Node *luaH_next (lua_State *L, Hash *t, const TObject *key) {
62 if (v == &luaO_nilobject) 62 if (v == &luaO_nilobject)
63 luaD_error(L, l_s("invalid key for `next'")); 63 luaD_error(L, l_s("invalid key for `next'"));
64 i = (int)(((const l_char *)v - 64 i = (int)(((const l_char *)v -
65 (const l_char *)(&t->node[0].val)) / sizeof(Node)) + 1; 65 (const l_char *)(val(node(t, 0)))) / sizeof(Node)) + 1;
66 } 66 }
67 for (; i<t->size; i++) { 67 for (; i<t->size; i++) {
68 Node *n = node(t, i); 68 Node *n = node(t, i);
@@ -103,11 +103,11 @@ static void setnodevector (lua_State *L, Hash *t, int size) {
103 t->node = luaM_newvector(L, size, Node); 103 t->node = luaM_newvector(L, size, Node);
104 for (i=0; i<size; i++) { 104 for (i=0; i<size; i++) {
105 t->node[i].next = NULL; 105 t->node[i].next = NULL;
106 t->node[i].key_tt = LUA_TNIL; 106 setnilvalue(key(node(t, i)));
107 setnilvalue(&t->node[i].val); 107 setnilvalue(val(node(t, i)));
108 } 108 }
109 t->size = size; 109 t->size = size;
110 t->firstfree = &t->node[size-1]; /* first free position to be used */ 110 t->firstfree = node(t, size-1); /* first free position to be used */
111} 111}
112 112
113 113
@@ -161,12 +161,9 @@ static void rehash (lua_State *L, Hash *t) {
161 setnodevector(L, t, oldsize); /* just rehash; keep the same size */ 161 setnodevector(L, t, oldsize); /* just rehash; keep the same size */
162 for (i=0; i<oldsize; i++) { 162 for (i=0; i<oldsize; i++) {
163 Node *old = nold+i; 163 Node *old = nold+i;
164 if (ttype(&old->val) != LUA_TNIL) { 164 if (ttype(val(old)) != LUA_TNIL) {
165 TObject o; 165 TObject *v = luaH_set(L, t, key(old));
166 TObject *v; 166 setobj(v, val(old));
167 setkey2obj(&o, old);
168 v = luaH_set(L, t, &o);
169 setobj(v, &old->val);
170 } 167 }
171 } 168 }
172 luaM_freearray(L, nold, oldsize, Node); /* free old array */ 169 luaM_freearray(L, nold, oldsize, Node); /* free old array */
@@ -181,7 +178,7 @@ static void rehash (lua_State *L, Hash *t) {
181** position), new key goes to an empty position. 178** position), new key goes to an empty position.
182*/ 179*/
183static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) { 180static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) {
184 if (ttype(&mp->val) != LUA_TNIL) { /* main position is not free? */ 181 if (ttype(val(mp)) != LUA_TNIL) { /* main position is not free? */
185 Node *othern = luaH_mainposition(t, mp); /* `mp' of colliding node */ 182 Node *othern = luaH_mainposition(t, mp); /* `mp' of colliding node */
186 Node *n = t->firstfree; /* get a free place */ 183 Node *n = t->firstfree; /* get a free place */
187 if (othern != mp) { /* is colliding node out of its main position? */ 184 if (othern != mp) { /* is colliding node out of its main position? */
@@ -190,7 +187,7 @@ static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) {
190 othern->next = n; /* redo the chain with `n' in place of `mp' */ 187 othern->next = n; /* redo the chain with `n' in place of `mp' */
191 *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */ 188 *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
192 mp->next = NULL; /* now `mp' is free */ 189 mp->next = NULL; /* now `mp' is free */
193 setnilvalue(&mp->val); 190 setnilvalue(val(mp));
194 } 191 }
195 else { /* colliding node is in its own main position */ 192 else { /* colliding node is in its own main position */
196 /* new node will go into free position */ 193 /* new node will go into free position */
@@ -199,11 +196,11 @@ static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) {
199 mp = n; 196 mp = n;
200 } 197 }
201 } 198 }
202 setobj2key(mp, key); 199 setobj(key(mp), key);
203 lua_assert(ttype(&mp->val) == LUA_TNIL); 200 lua_assert(ttype(val(mp)) == LUA_TNIL);
204 for (;;) { /* correct `firstfree' */ 201 for (;;) { /* correct `firstfree' */
205 if (ttype_key(t->firstfree) == LUA_TNIL) 202 if (ttype(key(t->firstfree)) == LUA_TNIL)
206 return &mp->val; /* OK; table still has a free place */ 203 return val(mp); /* OK; table still has a free place */
207 else if (t->firstfree == t->node) break; /* cannot decrement from here */ 204 else if (t->firstfree == t->node) break; /* cannot decrement from here */
208 else (t->firstfree)--; 205 else (t->firstfree)--;
209 } 206 }
@@ -220,8 +217,8 @@ TObject *luaH_setnum (lua_State *L, Hash *t, lua_Number key) {
220 Node *mp = hashnum(t, key); 217 Node *mp = hashnum(t, key);
221 Node *n = mp; 218 Node *n = mp;
222 do { /* check whether `key' is somewhere in the chain */ 219 do { /* check whether `key' is somewhere in the chain */
223 if (ttype_key(n) == LUA_TNUMBER && nvalue_key(n) == key) 220 if (ttype(key(n)) == LUA_TNUMBER && nvalue(key(n)) == key)
224 return &n->val; /* that's all */ 221 return val(n); /* that's all */
225 else n = n->next; 222 else n = n->next;
226 } while (n); 223 } while (n);
227 if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */ 224 if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */
@@ -239,8 +236,8 @@ TObject *luaH_setstr (lua_State *L, Hash *t, TString *key) {
239 Node *mp = hashstr(t, key); 236 Node *mp = hashstr(t, key);
240 Node *n = mp; 237 Node *n = mp;
241 do { /* check whether `key' is somewhere in the chain */ 238 do { /* check whether `key' is somewhere in the chain */
242 if (ttype_key(n) == LUA_TSTRING && tsvalue_key(n) == key) 239 if (ttype(key(n)) == LUA_TSTRING && tsvalue(key(n)) == key)
243 return &n->val; /* that's all */ 240 return val(n); /* that's all */
244 else n = n->next; 241 else n = n->next;
245 } while (n); 242 } while (n);
246 if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */ 243 if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */
@@ -258,8 +255,8 @@ static TObject *luaH_setany (lua_State *L, Hash *t, const TObject *key) {
258 Node *n = mp; 255 Node *n = mp;
259 do { /* check whether `key' is somewhere in the chain */ 256 do { /* check whether `key' is somewhere in the chain */
260 /* compare as `tsvalue', but may be other pointers (it is the same) */ 257 /* compare as `tsvalue', but may be other pointers (it is the same) */
261 if (ttype_key(n) == ttype(key) && tsvalue_key(n) == tsvalue(key)) 258 if (ttype(key(n)) == ttype(key) && tsvalue(key(n)) == tsvalue(key))
262 return &n->val; /* that's all */ 259 return val(n); /* that's all */
263 else n = n->next; 260 else n = n->next;
264 } while (n); 261 } while (n);
265 if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */ 262 if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */