1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/*
** $Id: ltable.c,v 1.82 2001/06/26 13:20:45 roberto Exp roberto $
** Lua tables (hash)
** See Copyright Notice in lua.h
*/
/*
** Implementation of tables (aka arrays, objects, or hash tables);
** uses a mix of chained scatter table with Brent's variation.
** A main invariant of these tables is that, if an element is not
** in its main position (i.e. the `original' position that its hash gives
** to it), then the colliding element is in its own main position.
** In other words, there are collisions only when two elements have the
** same main position (i.e. the same hash values for that table size).
** Because of that, the load factor of these tables can be 100% without
** performance penalties.
*/
#define LUA_PRIVATE
#include "lua.h"
#include "ldo.h"
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"
#include "ltable.h"
#define TagDefault LUA_TTABLE
#define hashnum(t,n) (node(t, lmod((lu_hash)(ls_hash)(n), t->size)))
#define hashstr(t,str) (node(t, lmod((str)->tsv.hash, t->size)))
#define hashpointer(t,p) (node(t, lmod(IntPoint(p), t->size)))
/*
** returns the `main' position of an element in a table (that is, the index
** of its hash value)
*/
Node *luaH_mainposition (const Hash *t, const TObject *key) {
switch (ttype(key)) {
case LUA_TNUMBER:
return hashnum(t, nvalue(key));
case LUA_TSTRING:
return hashstr(t, tsvalue(key));
default: /* all other types are hashed as (void *) */
return hashpointer(t, tsvalue(key));
}
}
Node *luaH_next (lua_State *L, Hash *t, const TObject *key) {
int i;
if (ttype(key) == LUA_TNIL)
i = 0; /* first iteration */
else {
const TObject *v = luaH_get(t, key);
if (v == &luaO_nilobject)
luaD_error(L, l_s("invalid key for `next'"));
i = (int)(((const lu_byte *)v -
(const lu_byte *)(val(node(t, 0)))) / sizeof(Node)) + 1;
}
for (; i<t->size; i++) {
Node *n = node(t, i);
if (ttype(val(n)) != LUA_TNIL)
return n;
}
return NULL; /* no more elements */
}
int luaH_nexti (Hash *t, int i) {
while ((++i)<t->size) {
if (ttype(val(node(t, i))) != LUA_TNIL) /* a non-nil value? */
return i;
}
return -1; /* no more elements */
}
#define check_grow(L, p, n) \
if ((p) >= MAX_INT/(n)) luaD_error(L, l_s("table overflow"));
/*
** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
*/
static int power2 (lua_State *L, int n) {
int p = MINPOWER2;
while (p <= n) {
check_grow(L, p, 2);
p *= 2;
}
return p;
}
static void setnodevector (lua_State *L, Hash *t, int size) {
int i;
t->node = luaM_newvector(L, size, Node);
for (i=0; i<size; i++) {
t->node[i].next = NULL;
setnilvalue(key(node(t, i)));
setnilvalue(val(node(t, i)));
}
t->size = size;
t->firstfree = node(t, size-1); /* first free position to be used */
}
Hash *luaH_new (lua_State *L, int size) {
Hash *t = luaM_new(L, Hash);
t->htag = TagDefault;
t->next = G(L)->roottable;
G(L)->roottable = t;
t->mark = t;
t->size = 0;
t->weakmode = 0;
t->node = NULL;
setnodevector(L, t, power2(L, size));
return t;
}
void luaH_free (lua_State *L, Hash *t) {
luaM_freearray(L, t->node, t->size, Node);
luaM_freelem(L, t, Hash);
}
static int numuse (const Hash *t) {
Node *v = t->node;
int size = t->size;
int realuse = 0;
int i;
for (i=0; i<size; i++) {
if (ttype(&v[i].val) != LUA_TNIL)
realuse++;
}
return realuse;
}
static void rehash (lua_State *L, Hash *t) {
int oldsize = t->size;
Node *nold = t->node;
int nelems = numuse(t);
int i;
lua_assert(nelems<=oldsize);
if (nelems >= oldsize-oldsize/4) { /* using more than 3/4? */
check_grow(L, oldsize, 2);
setnodevector(L, t, oldsize*2); /* grow array */
}
else if (nelems <= oldsize/4 && /* less than 1/4? */
oldsize > MINPOWER2)
setnodevector(L, t, oldsize/2); /* shrink array */
else
setnodevector(L, t, oldsize); /* just rehash; keep the same size */
for (i=0; i<oldsize; i++) {
Node *old = nold+i;
if (ttype(val(old)) != LUA_TNIL) {
TObject *v = luaH_set(L, t, key(old));
setobj(v, val(old));
}
}
luaM_freearray(L, nold, oldsize, Node); /* free old array */
}
/*
** inserts a new key into a hash table; first, check whether key's main
** position is free. If not, check whether colliding node is in its main
** position or not: if it is not, move colliding node to an empty place and
** put new key in its main position; otherwise (colliding node is in its main
** position), new key goes to an empty position.
*/
static TObject *newkey (lua_State *L, Hash *t, const TObject *key) {
Node *mp = luaH_mainposition(t, key);
if (ttype(val(mp)) != LUA_TNIL) { /* main position is not free? */
Node *othern = luaH_mainposition(t, key(mp)); /* `mp' of colliding node */
Node *n = t->firstfree; /* get a free place */
if (othern != mp) { /* is colliding node out of its main position? */
/* yes; move colliding node into free position */
while (othern->next != mp) othern = othern->next; /* find previous */
othern->next = n; /* redo the chain with `n' in place of `mp' */
*n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
mp->next = NULL; /* now `mp' is free */
setnilvalue(val(mp));
}
else { /* colliding node is in its own main position */
/* new node will go into free position */
n->next = mp->next; /* chain new position */
mp->next = n;
mp = n;
}
}
setobj(key(mp), key);
lua_assert(ttype(val(mp)) == LUA_TNIL);
for (;;) { /* correct `firstfree' */
if (ttype(key(t->firstfree)) == LUA_TNIL)
return val(mp); /* OK; table still has a free place */
else if (t->firstfree == t->node) break; /* cannot decrement from here */
else (t->firstfree)--;
}
rehash(L, t); /* no more free places */
return luaH_set(L, t, key); /* `rehash' invalidates this insertion */
}
/*
** generic search function
*/
static const TObject *luaH_getany (Hash *t, const TObject *key) {
if (ttype(key) == LUA_TNIL) return &luaO_nilobject;
else {
Node *n = luaH_mainposition(t, key);
do { /* check whether `key' is somewhere in the chain */
if (luaO_equalObj(key(n), key)) return val(n); /* that's it */
else n = n->next;
} while (n);
return &luaO_nilobject;
}
}
/*
** search function for integers
*/
const TObject *luaH_getnum (Hash *t, int key) {
Node *n = hashnum(t, key);
do { /* check whether `key' is somewhere in the chain */
if (ttype(key(n)) == LUA_TNUMBER && nvalue(key(n)) == (lua_Number)key)
return val(n); /* that's it */
else n = n->next;
} while (n);
return &luaO_nilobject;
}
/*
** search function for strings
*/
const TObject *luaH_getstr (Hash *t, TString *key) {
Node *n = hashstr(t, key);
do { /* check whether `key' is somewhere in the chain */
if (ttype(key(n)) == LUA_TSTRING && tsvalue(key(n)) == key)
return val(n); /* that's it */
else n = n->next;
} while (n);
return &luaO_nilobject;
}
/*
** main search function
*/
const TObject *luaH_get (Hash *t, const TObject *key) {
switch (ttype(key)) {
case LUA_TSTRING: return luaH_getstr(t, tsvalue(key));
case LUA_TNUMBER: {
int k = (int)nvalue(key);
if ((lua_Number)k == nvalue(key)) /* is an integer index? */
return luaH_getnum(t, k); /* use specialized version */
/* else go through */
}
default: return luaH_getany(t, key);
}
}
TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
const TObject *p = luaH_get(t, key);
if (p != &luaO_nilobject) return (TObject *)p;
else if (ttype(key) == LUA_TNIL) luaD_error(L, l_s("table index is nil"));
return newkey(L, t, key);
}
TObject *luaH_setstr (lua_State *L, Hash *t, TString *key) {
const TObject *p = luaH_getstr(t, key);
if (p != &luaO_nilobject) return (TObject *)p;
else {
TObject k;
setsvalue(&k, key);
return newkey(L, t, &k);
}
}
TObject *luaH_setnum (lua_State *L, Hash *t, int key) {
const TObject *p = luaH_getnum(t, key);
if (p != &luaO_nilobject) return (TObject *)p;
else {
TObject k;
setnvalue(&k, key);
return newkey(L, t, &k);
}
}
|