aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-01-25 15:41:19 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-01-25 15:41:19 -0200
commitfd7d0774e549130e59f716f2397d195cedc1a412 (patch)
tree6de4ad002b79592e3156d7ef577acfc4f1424b97
parent57ffc3f0094978fd2eb7ec2883cf6fca28145e90 (diff)
downloadlua-fd7d0774e549130e59f716f2397d195cedc1a412.tar.gz
lua-fd7d0774e549130e59f716f2397d195cedc1a412.tar.bz2
lua-fd7d0774e549130e59f716f2397d195cedc1a412.zip
luaH_set does the set and protect its value; luaH_move can then be a
macro. New algorithm for double hashing (does not use "%").
-rw-r--r--ltable.c47
1 files changed, 16 insertions, 31 deletions
diff --git a/ltable.c b/ltable.c
index 3363be88..7b355519 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.c,v 1.18 1999/01/22 18:47:23 roberto Exp roberto $ 2** $Id: ltable.c,v 1.19 1999/01/25 12:30:11 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,21 +53,17 @@ static long int hashindex (TObject *ref) {
53} 53}
54 54
55 55
56Node *luaH_present (Hash *t, TObject *ref) { 56Node *luaH_present (Hash *t, TObject *key) {
57 int tsize = nhash(t); 57 int tsize = nhash(t);
58 long int h = hashindex(ref); 58 long int h = hashindex(key);
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 ref or nil */ 61 /* keep looking until an entry with "ref" equal to key or nil */
62 if ((ttype(ref(n)) == ttype(ref) ? !luaO_equalval(ref, ref(n)) 62 while ((ttype(ref(n)) == ttype(key)) ? !luaO_equalval(key, ref(n))
63 : ttype(ref(n)) != LUA_T_NIL)) { 63 : ttype(ref(n)) != LUA_T_NIL) {
64 int h2 = h%(tsize-2) + 1; 64 h1 += (h&(tsize-2)) + 1; /* double hashing */
65 do { 65 if (h1 >= tsize) h1 -= tsize;
66 h1 += h2; 66 n = node(t, h1);
67 if (h1 >= tsize) h1 -= tsize;
68 n = node(t, h1);
69 } while ((ttype(ref(n)) == ttype(ref) ? !luaO_equalval(ref, ref(n))
70 : ttype(ref(n)) != LUA_T_NIL));
71 } 67 }
72 return n; 68 return n;
73} 69}
@@ -139,21 +135,20 @@ static void rehash (Hash *t) {
139} 135}
140 136
141 137
142/* 138void luaH_set (Hash *t, TObject *ref, TObject *val) {
143** If the hash node is present, return its pointer, otherwise create a new
144** node for the given reference and also return its pointer.
145*/
146TObject *luaH_set (Hash *t, TObject *ref) {
147 Node *n = luaH_present(t, ref); 139 Node *n = luaH_present(t, ref);
148 if (ttype(ref(n)) == LUA_T_NIL) { 140 if (ttype(ref(n)) != LUA_T_NIL)
141 *val(n) = *val;
142 else {
143 TObject buff = *val; /* rehash may invalidate this address */
149 if ((long)nuse(t)*3L > (long)nhash(t)*2L) { 144 if ((long)nuse(t)*3L > (long)nhash(t)*2L) {
150 rehash(t); 145 rehash(t);
151 n = luaH_present(t, ref); 146 n = luaH_present(t, ref);
152 } 147 }
153 nuse(t)++; 148 nuse(t)++;
154 *ref(n) = *ref; 149 *ref(n) = *ref;
150 *val(n) = buff;
155 } 151 }
156 return (val(n));
157} 152}
158 153
159 154
@@ -186,7 +181,7 @@ void luaH_setint (Hash *t, int ref, TObject *val) {
186 TObject index; 181 TObject index;
187 ttype(&index) = LUA_T_NUMBER; 182 ttype(&index) = LUA_T_NUMBER;
188 nvalue(&index) = ref; 183 nvalue(&index) = ref;
189 *(luaH_set(t, &index)) = *val; 184 luaH_set(t, &index, val);
190} 185}
191 186
192 187
@@ -197,13 +192,3 @@ TObject *luaH_getint (Hash *t, int ref) {
197 return luaH_get(t, &index); 192 return luaH_get(t, &index);
198} 193}
199 194
200
201void luaH_move (Hash *t, int from, int to) {
202 TObject index;
203 TObject *toadd;
204 ttype(&index) = LUA_T_NUMBER;
205 nvalue(&index) = to;
206 toadd = luaH_set(t, &index);
207 nvalue(&index) = from;
208 *toadd = *luaH_get(t, &index);
209}