switch to grub_snprint
[grub-extras.git] / ltable.c
blobf6501f8d50e81e7f7b0cec90d3bb6effb0fc97a4
1 /*
2 ** $Id: ltable.c,v 2.32.1.2 2007/12/28 15:32:23 roberto Exp $
3 ** Lua tables (hash)
4 ** See Copyright Notice in lua.h
5 */
8 /*
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.
21 #if 0
22 #include <math.h>
23 #include <string.h>
24 #endif
26 #define ltable_c
27 #define LUA_CORE
29 #include "lua.h"
31 #include "ldebug.h"
32 #include "ldo.h"
33 #include "lgc.h"
34 #include "lmem.h"
35 #include "lobject.h"
36 #include "lstate.h"
37 #include "ltable.h"
41 ** max size of array part is 2^MAXBITS
43 #if LUAI_BITSINT > 26
44 #define MAXBITS 26
45 #else
46 #define MAXBITS (LUAI_BITSINT-2)
47 #endif
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];
88 int i;
89 if (luai_numeq(n, 0)) /* avoid problems with -0 */
90 return gnode(t, 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)) {
104 case LUA_TNUMBER:
105 return hashnum(t, nvalue(key));
106 case LUA_TSTRING:
107 return hashstr(t, rawtsvalue(key));
108 case LUA_TBOOLEAN:
109 return hashboolean(t, bvalue(key));
110 case LUA_TLIGHTUSERDATA:
111 return hashpointer(t, pvalue(key));
112 default:
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);
125 int k;
126 lua_number2int(k, n);
127 if (luai_numeq(cast_num(k), n))
128 return k;
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) {
140 int i;
141 if (ttisnil(key)) return -1; /* first iteration */
142 i = arrayindex(key);
143 if (0 < i && i <= t->sizearray) /* is `key' inside array part? */
144 return i-1; /* yes; that's the index (corrected to C) */
145 else {
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;
156 else n = gnext(n);
157 } while (n);
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]);
170 return 1;
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)));
177 return 1;
180 return 0; /* no more elements */
185 ** {=============================================================
186 ** Rehash
187 ** ==============================================================
191 static int computesizes (int nums[], int *narray) {
192 int i;
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) {
198 if (nums[i] > 0) {
199 a += nums[i];
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 */
207 *narray = n;
208 lua_assert(*narray/2 <= na && na <= *narray);
209 return na;
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 */
217 return 1;
219 else
220 return 0;
224 static int numusearray (const Table *t, int *nums) {
225 int lg;
226 int ttlg; /* 2^lg */
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 */
231 int lim = ttlg;
232 if (lim > t->sizearray) {
233 lim = t->sizearray; /* adjust upper limit */
234 if (i > lim)
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]))
240 lc++;
242 nums[lg] += lc;
243 ause += lc;
245 return ause;
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' */
252 int i = sizenode(t);
253 while (i--) {
254 Node *n = &t->node[i];
255 if (!ttisnil(gval(n))) {
256 ause += countint(key2tval(n), nums);
257 totaluse++;
260 *pnasize += ause;
261 return totaluse;
265 static void setarrayvector (lua_State *L, Table *t, int size) {
266 int i;
267 luaM_reallocvector(L, t->array, t->sizearray, size, TValue);
268 for (i=t->sizearray; i<size; i++)
269 setnilvalue(&t->array[i]);
270 t->sizearray = size;
274 static void setnodevector (lua_State *L, Table *t, int size) {
275 int lsize;
276 if (size == 0) { /* no elements to hash part? */
277 t->node = cast(Node *, dummynode); /* use common `dummynode' */
278 lsize = 0;
280 else {
281 int i;
282 lsize = ceillog2(size);
283 if (lsize > MAXBITS)
284 luaG_runerror(L, "table overflow");
285 size = twoto(lsize);
286 t->node = luaM_newvector(L, size, Node);
287 for (i=0; i<size; i++) {
288 Node *n = gnode(t, i);
289 gnext(n) = NULL;
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) {
300 int i;
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]);
315 /* shrink array */
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--) {
320 Node *old = nold+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) {
336 int nasize, na;
337 int nums[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */
338 int i;
339 int totaluse;
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);
346 totaluse++;
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);
363 t->metatable = NULL;
364 t->flags = cast_byte(~0);
365 /* temporary values (kept only if some malloc fails) */
366 t->array = NULL;
367 t->sizearray = 0;
368 t->lsizenode = 0;
369 t->node = cast(Node *, dummynode);
370 setarrayvector(L, t, narray);
371 setnodevector(L, t, nhash);
372 return t;
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);
380 luaM_free(L, t);
384 static Node *getfreepos (Table *t) {
385 while (t->lastfree-- > t->node) {
386 if (ttisnil(gkey(t->lastfree)))
387 return 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) {
404 Node *othern;
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' */
416 #if 0
417 *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
418 #else
419 memcpy (n, mp, sizeof (*n));
420 #endif
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 */
427 gnext(mp) = n;
428 mp = n;
431 gkey(mp)->value = key->value; gkey(mp)->tt = key->tt;
432 luaC_barriert(L, t, key);
433 lua_assert(ttisnil(gval(mp)));
434 return 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];
445 else {
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 */
451 else n = gnext(n);
452 } while (n);
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 */
466 else n = gnext(n);
467 } while (n);
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));
479 case LUA_TNUMBER: {
480 int k;
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 */
487 default: {
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 */
492 else n = gnext(n);
493 } while (n);
494 return luaO_nilobject;
500 TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
501 const TValue *p = luaH_get(t, key);
502 t->flags = 0;
503 if (p != luaO_nilobject)
504 return cast(TValue *, p);
505 else {
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);
518 else {
519 TValue k;
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);
530 else {
531 TValue k;
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 */
540 j++;
541 /* find `i' and `j' such that i is present and j is not */
542 while (!ttisnil(luaH_getnum(t, j))) {
543 i = j;
544 j *= 2;
545 if (j > cast(unsigned int, MAX_INT)) { /* overflow? */
546 /* table was built with bad purposes: resort to linear search */
547 i = 1;
548 while (!ttisnil(luaH_getnum(t, i))) i++;
549 return i - 1;
552 /* now do a binary search between them */
553 while (j - i > 1) {
554 unsigned int m = (i+j)/2;
555 if (ttisnil(luaH_getnum(t, m))) j = m;
556 else i = m;
558 return i;
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 */
570 unsigned int i = 0;
571 while (j - i > 1) {
572 unsigned int m = (i+j)/2;
573 if (ttisnil(&t->array[m - 1])) j = m;
574 else i = m;
576 return i;
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; }
594 #endif