diff options
Diffstat (limited to '')
-rw-r--r-- | ltable.c | 34 |
1 files changed, 17 insertions, 17 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.c,v 1.10 1998/01/09 14:44:55 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 1.11 1998/01/13 18:06:27 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 | */ |
@@ -121,36 +121,36 @@ Hash *luaH_new (int nhash) | |||
121 | } | 121 | } |
122 | 122 | ||
123 | 123 | ||
124 | /* | 124 | static int newsize (Hash *t) |
125 | ** Rehash: | ||
126 | ** Check if table has deleted slots. It it has, it does not need to | ||
127 | ** grow, since rehash will reuse them. | ||
128 | */ | ||
129 | static int emptyslots (Hash *t) | ||
130 | { | 125 | { |
126 | Node *v = t->node; | ||
127 | int size = nhash(t); | ||
128 | int realuse = 0; | ||
131 | int i; | 129 | int i; |
132 | for (i=nhash(t)-1; i>=0; i--) { | 130 | for (i=0; i<size; i++) { |
133 | Node *n = node(t, i); | 131 | if (ttype(ref(v+i)) != LUA_T_NIL && ttype(val(v+i)) != LUA_T_NIL) |
134 | if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) == LUA_T_NIL) | 132 | realuse++; |
135 | return 1; | ||
136 | } | 133 | } |
137 | return 0; | 134 | if (2*(realuse+1) <= size) /* +1 is the new element */ |
135 | return size; /* don't need to grow, just rehash */ | ||
136 | else | ||
137 | return luaO_redimension(size); | ||
138 | } | 138 | } |
139 | 139 | ||
140 | static void rehash (Hash *t) | 140 | static void rehash (Hash *t) |
141 | { | 141 | { |
142 | int nold = nhash(t); | 142 | int nold = nhash(t); |
143 | Node *vold = nodevector(t); | 143 | Node *vold = nodevector(t); |
144 | int nnew = newsize(t); | ||
144 | int i; | 145 | int i; |
145 | if (!emptyslots(t)) | 146 | nodevector(t) = hashnodecreate(nnew); |
146 | nhash(t) = luaO_redimension(nhash(t)); | 147 | nhash(t) = nnew; |
147 | nodevector(t) = hashnodecreate(nhash(t)); | ||
148 | for (i=0; i<nold; i++) { | 148 | for (i=0; i<nold; i++) { |
149 | Node *n = vold+i; | 149 | Node *n = vold+i; |
150 | if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL) | 150 | if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL) |
151 | *node(t, present(t, ref(n))) = *n; /* copy old node to luaM_new hash */ | 151 | *node(t, present(t, ref(n))) = *n; /* copy old node to luaM_new hash */ |
152 | } | 152 | } |
153 | L->nblocks += gcsize(t->nhash)-gcsize(nold); | 153 | L->nblocks += gcsize(nnew)-gcsize(nold); |
154 | luaM_free(vold); | 154 | luaM_free(vold); |
155 | } | 155 | } |
156 | 156 | ||