1/*	$NetBSD$	*/
2
3/*
4** Id: ltable.c,v 2.32.1.2 2007/12/28 15:32:23 roberto Exp
5** Lua tables (hash)
6** See Copyright Notice in lua.h
7*/
8
9
10/*
11** Implementation of tables (aka arrays, objects, or hash tables).
12** Tables keep its elements in two parts: an array part and a hash part.
13** Non-negative integer keys are all candidates to be kept in the array
14** part. The actual size of the array is the largest `n' such that at
15** least half the slots between 0 and n are in use.
16** Hash uses a mix of chained scatter table with Brent's variation.
17** A main invariant of these tables is that, if an element is not
18** in its main position (i.e. the `original' position that its hash gives
19** to it), then the colliding element is in its own main position.
20** Hence even when the load factor reaches 100%, performance remains good.
21*/
22
23#include <math.h>
24#include <string.h>
25
26#define ltable_c
27#define LUA_CORE
28
29#include "lua.h"
30
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"
38
39
40/*
41** max size of array part is 2^MAXBITS
42*/
43#if LUAI_BITSINT > 26
44#define MAXBITS		26
45#else
46#define MAXBITS		(LUAI_BITSINT-2)
47#endif
48
49#define MAXASIZE	(1 << MAXBITS)
50
51
52#define hashpow2(t,n)      (gnode(t, lmod((n), sizenode(t))))
53
54#define hashstr(t,str)  hashpow2(t, (str)->tsv.hash)
55#define hashboolean(t,p)        hashpow2(t, p)
56
57
58/*
59** for some types, it is better to avoid modulus by power of 2, as
60** they tend to have many 2 factors.
61*/
62#define hashmod(t,n)	(gnode(t, ((n) % ((sizenode(t)-1)|1))))
63
64
65#define hashpointer(t,p)	hashmod(t, IntPoint(p))
66
67
68/*
69** number of ints inside a lua_Number
70*/
71#define numints		cast_int(sizeof(lua_Number)/sizeof(int))
72
73
74
75#define dummynode		(&dummynode_)
76
77static const Node dummynode_ = {
78  {{NULL}, LUA_TNIL},  /* value */
79  {{{NULL}, LUA_TNIL, NULL}}  /* key */
80};
81
82
83/*
84** hash for lua_Numbers
85*/
86static 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]);
94}
95
96
97
98/*
99** returns the `main' position of an element in a table (that is, the index
100** of its hash value)
101*/
102static 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));
114  }
115}
116
117
118/*
119** returns the index for `key' if `key' is an appropriate key to live in
120** the array part of the table, -1 otherwise.
121*/
122static 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;
129  }
130  return -1;  /* `key' did not match some condition */
131}
132
133
134/*
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.
138*/
139static 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;
155      }
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 */
160  }
161}
162
163
164int 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;
171    }
172  }
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;
178    }
179  }
180  return 0;  /* no more elements */
181}
182
183
184/*
185** {=============================================================
186** Rehash
187** ==============================================================
188*/
189
190
191static 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 */
203      }
204    }
205    if (a == *narray) break;  /* all elements already counted */
206  }
207  *narray = n;
208  lua_assert(*narray/2 <= na && na <= *narray);
209  return na;
210}
211
212
213static 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;
218  }
219  else
220    return 0;
221}
222
223
224static 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 */
236    }
237    /* count elements in range (2^(lg-1), 2^lg] */
238    for (; i <= lim; i++) {
239      if (!ttisnil(&t->array[i-1]))
240        lc++;
241    }
242    nums[lg] += lc;
243    ause += lc;
244  }
245  return ause;
246}
247
248
249static 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++;
258    }
259  }
260  *pnasize += ause;
261  return totaluse;
262}
263
264
265static 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;
271}
272
273
274static 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;
279  }
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));
292    }
293  }
294  t->lsizenode = cast_byte(lsize);
295  t->lastfree = gnode(t, size);  /* all positions are free */
296}
297
298
299static 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]);
314    }
315    /* shrink array */
316    luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
317  }
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));
323  }
324  if (nold != dummynode)
325    luaM_freearray(L, nold, twoto(oldhsize), Node);  /* free old array */
326}
327
328
329void luaH_resizearray (lua_State *L, Table *t, int nasize) {
330  int nsize = (t->node == dummynode) ? 0 : sizenode(t);
331  resize(L, t, nasize, nsize);
332}
333
334
335static 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);
351}
352
353
354
355/*
356** }=============================================================
357*/
358
359
360Table *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;
373}
374
375
376void 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);
381}
382
383
384static Node *getfreepos (Table *t) {
385  while (t->lastfree-- > t->node) {
386    if (ttisnil(gkey(t->lastfree)))
387      return t->lastfree;
388  }
389  return NULL;  /* could not find a free place */
390}
391
392
393
394/*
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.
400*/
401static 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 */
409    }
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      *n = *mp;  /* copy colliding node into free pos. (mp->next also goes) */
417      gnext(mp) = NULL;  /* now `mp' is free */
418      setnilvalue(gval(mp));
419    }
420    else {  /* colliding node is in its own main position */
421      /* new node will go into free position */
422      gnext(n) = gnext(mp);  /* chain new position */
423      gnext(mp) = n;
424      mp = n;
425    }
426  }
427  gkey(mp)->value = key->value; gkey(mp)->tt = key->tt;
428  luaC_barriert(L, t, key);
429  lua_assert(ttisnil(gval(mp)));
430  return gval(mp);
431}
432
433
434/*
435** search function for integers
436*/
437const TValue *luaH_getnum (Table *t, int key) {
438  /* (1 <= key && key <= t->sizearray) */
439  if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray))
440    return &t->array[key-1];
441  else {
442    lua_Number nk = cast_num(key);
443    Node *n = hashnum(t, nk);
444    do {  /* check whether `key' is somewhere in the chain */
445      if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk))
446        return gval(n);  /* that's it */
447      else n = gnext(n);
448    } while (n);
449    return luaO_nilobject;
450  }
451}
452
453
454/*
455** search function for strings
456*/
457const TValue *luaH_getstr (Table *t, TString *key) {
458  Node *n = hashstr(t, key);
459  do {  /* check whether `key' is somewhere in the chain */
460    if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key)
461      return gval(n);  /* that's it */
462    else n = gnext(n);
463  } while (n);
464  return luaO_nilobject;
465}
466
467
468/*
469** main search function
470*/
471const TValue *luaH_get (Table *t, const TValue *key) {
472  switch (ttype(key)) {
473    case LUA_TNIL: return luaO_nilobject;
474    case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
475    case LUA_TNUMBER: {
476      int k;
477      lua_Number n = nvalue(key);
478      lua_number2int(k, n);
479      if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */
480        return luaH_getnum(t, k);  /* use specialized version */
481      /* else go through */
482    }
483    default: {
484      Node *n = mainposition(t, key);
485      do {  /* check whether `key' is somewhere in the chain */
486        if (luaO_rawequalObj(key2tval(n), key))
487          return gval(n);  /* that's it */
488        else n = gnext(n);
489      } while (n);
490      return luaO_nilobject;
491    }
492  }
493}
494
495
496TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
497  const TValue *p = luaH_get(t, key);
498  t->flags = 0;
499  if (p != luaO_nilobject)
500    return cast(TValue *, p);
501  else {
502    if (ttisnil(key)) luaG_runerror(L, "table index is nil");
503    else if (ttisnumber(key) && luai_numisnan(nvalue(key)))
504      luaG_runerror(L, "table index is NaN");
505    return newkey(L, t, key);
506  }
507}
508
509
510TValue *luaH_setnum (lua_State *L, Table *t, int key) {
511  const TValue *p = luaH_getnum(t, key);
512  if (p != luaO_nilobject)
513    return cast(TValue *, p);
514  else {
515    TValue k;
516    setnvalue(&k, cast_num(key));
517    return newkey(L, t, &k);
518  }
519}
520
521
522TValue *luaH_setstr (lua_State *L, Table *t, TString *key) {
523  const TValue *p = luaH_getstr(t, key);
524  if (p != luaO_nilobject)
525    return cast(TValue *, p);
526  else {
527    TValue k;
528    setsvalue(L, &k, key);
529    return newkey(L, t, &k);
530  }
531}
532
533
534static int unbound_search (Table *t, unsigned int j) {
535  unsigned int i = j;  /* i is zero or a present index */
536  j++;
537  /* find `i' and `j' such that i is present and j is not */
538  while (!ttisnil(luaH_getnum(t, j))) {
539    i = j;
540    j *= 2;
541    if (j > cast(unsigned int, MAX_INT)) {  /* overflow? */
542      /* table was built with bad purposes: resort to linear search */
543      i = 1;
544      while (!ttisnil(luaH_getnum(t, i))) i++;
545      return i - 1;
546    }
547  }
548  /* now do a binary search between them */
549  while (j - i > 1) {
550    unsigned int m = (i+j)/2;
551    if (ttisnil(luaH_getnum(t, m))) j = m;
552    else i = m;
553  }
554  return i;
555}
556
557
558/*
559** Try to find a boundary in table `t'. A `boundary' is an integer index
560** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
561*/
562int luaH_getn (Table *t) {
563  unsigned int j = t->sizearray;
564  if (j > 0 && ttisnil(&t->array[j - 1])) {
565    /* there is a boundary in the array part: (binary) search for it */
566    unsigned int i = 0;
567    while (j - i > 1) {
568      unsigned int m = (i+j)/2;
569      if (ttisnil(&t->array[m - 1])) j = m;
570      else i = m;
571    }
572    return i;
573  }
574  /* else must find a boundary in hash part */
575  else if (t->node == dummynode)  /* hash part is empty? */
576    return j;  /* that is easy... */
577  else return unbound_search(t, j);
578}
579
580
581
582#if defined(LUA_DEBUG)
583
584Node *luaH_mainposition (const Table *t, const TValue *key) {
585  return mainposition(t, key);
586}
587
588int luaH_isdummy (Node *n) { return n == dummynode; }
589
590#endif
591