2
** $Id: ltable.c,v 2.32.1.2 2007/12/28 15:32:23 roberto Exp $
4
** See Copyright Notice in lua.h
9
** Implementation of tables (aka arrays, objects, or hash tables).
10
** Tables keep its elements in two parts: an array part and a hash part.
11
** Non-negative integer keys are all candidates to be kept in the array
12
** part. The actual size of the array is the largest `n' such that at
13
** least half the slots between 0 and n are in use.
14
** Hash uses a mix of chained scatter table with Brent's variation.
15
** A main invariant of these tables is that, if an element is not
16
** in its main position (i.e. the `original' position that its hash gives
17
** to it), then the colliding element is in its own main position.
18
** Hence even when the load factor reaches 100%, performance remains good.
41
** max size of array part is 2^MAXBITS
46
#define MAXBITS (LUAI_BITSINT-2)
49
#define MAXASIZE (1 << MAXBITS)
52
#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
54
#define hashstr(t,str) hashpow2(t, (str)->tsv.hash)
55
#define hashboolean(t,p) hashpow2(t, p)
59
** for some types, it is better to avoid modulus by power of 2, as
60
** they tend to have many 2 factors.
62
#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
65
#define hashpointer(t,p) hashmod(t, IntPoint(p))
69
** number of ints inside a lua_Number
71
#define numints cast_int(sizeof(lua_Number)/sizeof(int))
75
#define dummynode (&dummynode_)
77
static const Node dummynode_ = {
78
{{NULL}, LUA_TNIL}, /* value */
79
{{{NULL}, LUA_TNIL, NULL}} /* key */
84
** hash for lua_Numbers
86
static Node *hashnum (const Table *t, lua_Number n) {
87
unsigned int a[numints];
89
if (luai_numeq(n, 0)) /* avoid problems with -0 */
91
memcpy(a, &n, sizeof(a));
92
for (i = 1; i < numints; i++) a[0] += a[i];
93
return hashmod(t, a[0]);
99
** returns the `main' position of an element in a table (that is, the index
100
** of its hash value)
102
static Node *mainposition (const Table *t, const TValue *key) {
103
switch (ttype(key)) {
105
return hashnum(t, nvalue(key));
107
return hashstr(t, rawtsvalue(key));
109
return hashboolean(t, bvalue(key));
110
case LUA_TLIGHTUSERDATA:
111
return hashpointer(t, pvalue(key));
113
return hashpointer(t, gcvalue(key));
119
** returns the index for `key' if `key' is an appropriate key to live in
120
** the array part of the table, -1 otherwise.
122
static int arrayindex (const TValue *key) {
123
if (ttisnumber(key)) {
124
lua_Number n = nvalue(key);
126
lua_number2int(k, n);
127
if (luai_numeq(cast_num(k), n))
130
return -1; /* `key' did not match some condition */
135
** returns the index of a `key' for table traversals. First goes all
136
** elements in the array part, then elements in the hash part. The
137
** beginning of a traversal is signalled by -1.
139
static int findindex (lua_State *L, Table *t, StkId key) {
141
if (ttisnil(key)) return -1; /* first iteration */
143
if (0 < i && i <= t->sizearray) /* is `key' inside array part? */
144
return i-1; /* yes; that's the index (corrected to C) */
146
Node *n = mainposition(t, key);
147
do { /* check whether `key' is somewhere in the chain */
148
/* key may be dead already, but it is ok to use it in `next' */
149
if (luaO_rawequalObj(key2tval(n), key) ||
150
(ttype(gkey(n)) == LUA_TDEADKEY && iscollectable(key) &&
151
gcvalue(gkey(n)) == gcvalue(key))) {
152
i = cast_int(n - gnode(t, 0)); /* key index in hash table */
153
/* hash elements are numbered after array ones */
154
return i + t->sizearray;
158
luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */
159
return 0; /* to avoid warnings */
164
int luaH_next (lua_State *L, Table *t, StkId key) {
165
int i = findindex(L, t, key); /* find original element */
166
for (i++; i < t->sizearray; i++) { /* try first array part */
167
if (!ttisnil(&t->array[i])) { /* a non-nil value? */
168
setnvalue(key, cast_num(i+1));
169
setobj2s(L, key+1, &t->array[i]);
173
for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */
174
if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */
175
setobj2s(L, key, key2tval(gnode(t, i)));
176
setobj2s(L, key+1, gval(gnode(t, i)));
180
return 0; /* no more elements */
185
** {=============================================================
187
** ==============================================================
191
static int computesizes (int nums[], int *narray) {
193
int twotoi; /* 2^i */
194
int a = 0; /* number of elements smaller than 2^i */
195
int na = 0; /* number of elements to go to array part */
196
int n = 0; /* optimal size for array part */
197
for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) {
200
if (a > twotoi/2) { /* more than half elements present? */
201
n = twotoi; /* optimal size (till now) */
202
na = a; /* all elements smaller than n will go to array part */
205
if (a == *narray) break; /* all elements already counted */
208
lua_assert(*narray/2 <= na && na <= *narray);
213
static int countint (const TValue *key, int *nums) {
214
int k = arrayindex(key);
215
if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */
216
nums[ceillog2(k)]++; /* count as such */
224
static int numusearray (const Table *t, int *nums) {
227
int ause = 0; /* summation of `nums' */
228
int i = 1; /* count to traverse all array keys */
229
for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */
230
int lc = 0; /* counter */
232
if (lim > t->sizearray) {
233
lim = t->sizearray; /* adjust upper limit */
235
break; /* no more elements to count */
237
/* count elements in range (2^(lg-1), 2^lg] */
238
for (; i <= lim; i++) {
239
if (!ttisnil(&t->array[i-1]))
249
static int numusehash (const Table *t, int *nums, int *pnasize) {
250
int totaluse = 0; /* total number of elements */
251
int ause = 0; /* summation of `nums' */
254
Node *n = &t->node[i];
255
if (!ttisnil(gval(n))) {
256
ause += countint(key2tval(n), nums);
265
static void setarrayvector (lua_State *L, Table *t, int size) {
267
luaM_reallocvector(L, t->array, t->sizearray, size, TValue);
268
for (i=t->sizearray; i<size; i++)
269
setnilvalue(&t->array[i]);
274
static void setnodevector (lua_State *L, Table *t, int size) {
276
if (size == 0) { /* no elements to hash part? */
277
t->node = cast(Node *, dummynode); /* use common `dummynode' */
282
lsize = ceillog2(size);
284
luaG_runerror(L, "table overflow");
286
t->node = luaM_newvector(L, size, Node);
287
for (i=0; i<size; i++) {
288
Node *n = gnode(t, i);
290
setnilvalue(gkey(n));
291
setnilvalue(gval(n));
294
t->lsizenode = cast_byte(lsize);
295
t->lastfree = gnode(t, size); /* all positions are free */
299
static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
301
int oldasize = t->sizearray;
302
int oldhsize = t->lsizenode;
303
Node *nold = t->node; /* save old hash ... */
304
if (nasize > oldasize) /* array part must grow? */
305
setarrayvector(L, t, nasize);
306
/* create new hash part with appropriate size */
307
setnodevector(L, t, nhsize);
308
if (nasize < oldasize) { /* array part must shrink? */
309
t->sizearray = nasize;
310
/* re-insert elements from vanishing slice */
311
for (i=nasize; i<oldasize; i++) {
312
if (!ttisnil(&t->array[i]))
313
setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]);
316
luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
318
/* re-insert elements from hash part */
319
for (i = twoto(oldhsize) - 1; i >= 0; i--) {
321
if (!ttisnil(gval(old)))
322
setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old));
324
if (nold != dummynode)
325
luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */
329
void luaH_resizearray (lua_State *L, Table *t, int nasize) {
330
int nsize = (t->node == dummynode) ? 0 : sizenode(t);
331
resize(L, t, nasize, nsize);
335
static void rehash (lua_State *L, Table *t, const TValue *ek) {
337
int nums[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */
340
for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */
341
nasize = numusearray(t, nums); /* count keys in array part */
342
totaluse = nasize; /* all those keys are integer keys */
343
totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */
344
/* count extra key */
345
nasize += countint(ek, nums);
347
/* compute new size for array part */
348
na = computesizes(nums, &nasize);
349
/* resize the table to new computed sizes */
350
resize(L, t, nasize, totaluse - na);
356
** }=============================================================
360
Table *luaH_new (lua_State *L, int narray, int nhash) {
361
Table *t = luaM_new(L, Table);
362
luaC_link(L, obj2gco(t), LUA_TTABLE);
364
t->flags = cast_byte(~0);
365
/* temporary values (kept only if some malloc fails) */
369
t->node = cast(Node *, dummynode);
370
setarrayvector(L, t, narray);
371
setnodevector(L, t, nhash);
376
void luaH_free (lua_State *L, Table *t) {
377
if (t->node != dummynode)
378
luaM_freearray(L, t->node, sizenode(t), Node);
379
luaM_freearray(L, t->array, t->sizearray, TValue);
384
static Node *getfreepos (Table *t) {
385
while (t->lastfree-- > t->node) {
386
if (ttisnil(gkey(t->lastfree)))
389
return NULL; /* could not find a free place */
395
** inserts a new key into a hash table; first, check whether key's main
396
** position is free. If not, check whether colliding node is in its main
397
** position or not: if it is not, move colliding node to an empty place and
398
** put new key in its main position; otherwise (colliding node is in its main
399
** position), new key goes to an empty position.
401
static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
402
Node *mp = mainposition(t, key);
403
if (!ttisnil(gval(mp)) || mp == dummynode) {
405
Node *n = getfreepos(t); /* get a free place */
406
if (n == NULL) { /* cannot find a free place? */
407
rehash(L, t, key); /* grow table */
408
return luaH_set(L, t, key); /* re-insert key into grown table */
410
lua_assert(n != dummynode);
411
othern = mainposition(t, key2tval(mp));
412
if (othern != mp) { /* is colliding node out of its main position? */
413
/* yes; move colliding node into free position */
414
while (gnext(othern) != mp) othern = gnext(othern); /* find previous */
415
gnext(othern) = n; /* redo the chain with `n' in place of `mp' */
417
*n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
419
memcpy (n, mp, sizeof (*n));
421
gnext(mp) = NULL; /* now `mp' is free */
422
setnilvalue(gval(mp));
424
else { /* colliding node is in its own main position */
425
/* new node will go into free position */
426
gnext(n) = gnext(mp); /* chain new position */
431
gkey(mp)->value = key->value; gkey(mp)->tt = key->tt;
432
luaC_barriert(L, t, key);
433
lua_assert(ttisnil(gval(mp)));
439
** search function for integers
441
const TValue *luaH_getnum (Table *t, int key) {
442
/* (1 <= key && key <= t->sizearray) */
443
if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray))
444
return &t->array[key-1];
446
lua_Number nk = cast_num(key);
447
Node *n = hashnum(t, nk);
448
do { /* check whether `key' is somewhere in the chain */
449
if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk))
450
return gval(n); /* that's it */
453
return luaO_nilobject;
459
** search function for strings
461
const TValue *luaH_getstr (Table *t, TString *key) {
462
Node *n = hashstr(t, key);
463
do { /* check whether `key' is somewhere in the chain */
464
if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key)
465
return gval(n); /* that's it */
468
return luaO_nilobject;
473
** main search function
475
const TValue *luaH_get (Table *t, const TValue *key) {
476
switch (ttype(key)) {
477
case LUA_TNIL: return luaO_nilobject;
478
case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
481
lua_Number n = nvalue(key);
482
lua_number2int(k, n);
483
if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */
484
return luaH_getnum(t, k); /* use specialized version */
485
/* else go through */
488
Node *n = mainposition(t, key);
489
do { /* check whether `key' is somewhere in the chain */
490
if (luaO_rawequalObj(key2tval(n), key))
491
return gval(n); /* that's it */
494
return luaO_nilobject;
500
TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
501
const TValue *p = luaH_get(t, key);
503
if (p != luaO_nilobject)
504
return cast(TValue *, p);
506
if (ttisnil(key)) luaG_runerror(L, "table index is nil");
507
else if (ttisnumber(key) && luai_numisnan(nvalue(key)))
508
luaG_runerror(L, "table index is NaN");
509
return newkey(L, t, key);
514
TValue *luaH_setnum (lua_State *L, Table *t, int key) {
515
const TValue *p = luaH_getnum(t, key);
516
if (p != luaO_nilobject)
517
return cast(TValue *, p);
520
setnvalue(&k, cast_num(key));
521
return newkey(L, t, &k);
526
TValue *luaH_setstr (lua_State *L, Table *t, TString *key) {
527
const TValue *p = luaH_getstr(t, key);
528
if (p != luaO_nilobject)
529
return cast(TValue *, p);
532
setsvalue(L, &k, key);
533
return newkey(L, t, &k);
538
static int unbound_search (Table *t, unsigned int j) {
539
unsigned int i = j; /* i is zero or a present index */
541
/* find `i' and `j' such that i is present and j is not */
542
while (!ttisnil(luaH_getnum(t, j))) {
545
if (j > cast(unsigned int, MAX_INT)) { /* overflow? */
546
/* table was built with bad purposes: resort to linear search */
548
while (!ttisnil(luaH_getnum(t, i))) i++;
552
/* now do a binary search between them */
554
unsigned int m = (i+j)/2;
555
if (ttisnil(luaH_getnum(t, m))) j = m;
563
** Try to find a boundary in table `t'. A `boundary' is an integer index
564
** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
566
int luaH_getn (Table *t) {
567
unsigned int j = t->sizearray;
568
if (j > 0 && ttisnil(&t->array[j - 1])) {
569
/* there is a boundary in the array part: (binary) search for it */
572
unsigned int m = (i+j)/2;
573
if (ttisnil(&t->array[m - 1])) j = m;
578
/* else must find a boundary in hash part */
579
else if (t->node == dummynode) /* hash part is empty? */
580
return j; /* that is easy... */
581
else return unbound_search(t, j);
586
#if defined(LUA_DEBUG)
588
Node *luaH_mainposition (const Table *t, const TValue *key) {
589
return mainposition(t, key);
592
int luaH_isdummy (Node *n) { return n == dummynode; }