ltable.c
Go to the documentation of this file.
1 /*
2 ** $Id: ltable.c $
3 ** Lua tables (hash)
4 ** See Copyright Notice in lua.h
5 */
6 
7 #define ltable_c
8 #define LUA_CORE
9 
10 #include "lprefix.h"
11 
12 
13 /*
14 ** Implementation of tables (aka arrays, objects, or hash tables).
15 ** Tables keep its elements in two parts: an array part and a hash part.
16 ** Non-negative integer keys are all candidates to be kept in the array
17 ** part. The actual size of the array is the largest 'n' such that
18 ** more than half the slots between 1 and n are in use.
19 ** Hash uses a mix of chained scatter table with Brent's variation.
20 ** A main invariant of these tables is that, if an element is not
21 ** in its main position (i.e. the 'original' position that its hash gives
22 ** to it), then the colliding element is in its own main position.
23 ** Hence even when the load factor reaches 100%, performance remains good.
24 */
25 
26 #include <math.h>
27 #include <limits.h>
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 "lstring.h"
38 #include "ltable.h"
39 #include "lvm.h"
40 
41 
42 /*
43 ** MAXABITS is the largest integer such that MAXASIZE fits in an
44 ** unsigned int.
45 */
46 #define MAXABITS cast_int(sizeof(int) * CHAR_BIT - 1)
47 
48 
49 /*
50 ** MAXASIZE is the maximum size of the array part. It is the minimum
51 ** between 2^MAXABITS and the maximum size that, measured in bytes,
52 ** fits in a 'size_t'.
53 */
54 #define MAXASIZE luaM_limitN(1u << MAXABITS, TValue)
55 
56 /*
57 ** MAXHBITS is the largest integer such that 2^MAXHBITS fits in a
58 ** signed int.
59 */
60 #define MAXHBITS (MAXABITS - 1)
61 
62 
63 /*
64 ** MAXHSIZE is the maximum size of the hash part. It is the minimum
65 ** between 2^MAXHBITS and the maximum size such that, measured in bytes,
66 ** it fits in a 'size_t'.
67 */
68 #define MAXHSIZE luaM_limitN(1u << MAXHBITS, Node)
69 
70 
71 #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
72 
73 #define hashstr(t,str) hashpow2(t, (str)->hash)
74 #define hashboolean(t,p) hashpow2(t, p)
75 #define hashint(t,i) hashpow2(t, i)
76 
77 
78 /*
79 ** for some types, it is better to avoid modulus by power of 2, as
80 ** they tend to have many 2 factors.
81 */
82 #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
83 
84 
85 #define hashpointer(t,p) hashmod(t, point2uint(p))
86 
87 
88 #define dummynode (&dummynode_)
89 
90 static const Node dummynode_ = {
91  {{NULL}, LUA_VEMPTY, /* value's value and type */
92  LUA_VNIL, 0, {NULL}} /* key type, next, and key value */
93 };
94 
95 
96 static const TValue absentkey = {ABSTKEYCONSTANT};
97 
98 
99 
100 /*
101 ** Hash for floating-point numbers.
102 ** The main computation should be just
103 ** n = frexp(n, &i); return (n * INT_MAX) + i
104 ** but there are some numerical subtleties.
105 ** In a two-complement representation, INT_MAX does not has an exact
106 ** representation as a float, but INT_MIN does; because the absolute
107 ** value of 'frexp' is smaller than 1 (unless 'n' is inf/NaN), the
108 ** absolute value of the product 'frexp * -INT_MIN' is smaller or equal
109 ** to INT_MAX. Next, the use of 'unsigned int' avoids overflows when
110 ** adding 'i'; the use of '~u' (instead of '-u') avoids problems with
111 ** INT_MIN.
112 */
113 #if !defined(l_hashfloat)
114 static int l_hashfloat (lua_Number n) {
115  int i;
116  lua_Integer ni;
117  n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN);
118  if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */
119  lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL));
120  return 0;
121  }
122  else { /* normal case */
123  unsigned int u = cast_uint(i) + cast_uint(ni);
124  return cast_int(u <= cast_uint(INT_MAX) ? u : ~u);
125  }
126 }
127 #endif
128 
129 
130 /*
131 ** returns the 'main' position of an element in a table (that is,
132 ** the index of its hash value). The key comes broken (tag in 'ktt'
133 ** and value in 'vkl') so that we can call it on keys inserted into
134 ** nodes.
135 */
136 static Node *mainposition (const Table *t, int ktt, const Value *kvl) {
137  switch (withvariant(ktt)) {
138  case LUA_VNUMINT:
139  return hashint(t, ivalueraw(*kvl));
140  case LUA_VNUMFLT:
141  return hashmod(t, l_hashfloat(fltvalueraw(*kvl)));
142  case LUA_VSHRSTR:
143  return hashstr(t, tsvalueraw(*kvl));
144  case LUA_VLNGSTR:
145  return hashpow2(t, luaS_hashlongstr(tsvalueraw(*kvl)));
146  case LUA_VFALSE:
147  return hashboolean(t, 0);
148  case LUA_VTRUE:
149  return hashboolean(t, 1);
150  case LUA_VLIGHTUSERDATA:
151  return hashpointer(t, pvalueraw(*kvl));
152  case LUA_VLCF:
153  return hashpointer(t, fvalueraw(*kvl));
154  default:
155  return hashpointer(t, gcvalueraw(*kvl));
156  }
157 }
158 
159 
160 /*
161 ** Returns the main position of an element given as a 'TValue'
162 */
163 static Node *mainpositionTV (const Table *t, const TValue *key) {
164  return mainposition(t, rawtt(key), valraw(key));
165 }
166 
167 
168 /*
169 ** Check whether key 'k1' is equal to the key in node 'n2'.
170 ** This equality is raw, so there are no metamethods. Floats
171 ** with integer values have been normalized, so integers cannot
172 ** be equal to floats. It is assumed that 'eqshrstr' is simply
173 ** pointer equality, so that short strings are handled in the
174 ** default case.
175 */
176 static int equalkey (const TValue *k1, const Node *n2) {
177  if (rawtt(k1) != keytt(n2)) /* not the same variants? */
178  return 0; /* cannot be same key */
179  switch (ttypetag(k1)) {
180  case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE:
181  return 1;
182  case LUA_VNUMINT:
183  return (ivalue(k1) == keyival(n2));
184  case LUA_VNUMFLT:
185  return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2)));
186  case LUA_VLIGHTUSERDATA:
187  return pvalue(k1) == pvalueraw(keyval(n2));
188  case LUA_VLCF:
189  return fvalue(k1) == fvalueraw(keyval(n2));
190  case LUA_VLNGSTR:
191  return luaS_eqlngstr(tsvalue(k1), keystrval(n2));
192  default:
193  return gcvalue(k1) == gcvalueraw(keyval(n2));
194  }
195 }
196 
197 
198 /*
199 ** True if value of 'alimit' is equal to the real size of the array
200 ** part of table 't'. (Otherwise, the array part must be larger than
201 ** 'alimit'.)
202 */
203 #define limitequalsasize(t) (isrealasize(t) || ispow2((t)->alimit))
204 
205 
206 /*
207 ** Returns the real size of the 'array' array
208 */
209 LUAI_FUNC unsigned int luaH_realasize (const Table *t) {
210  if (limitequalsasize(t))
211  return t->alimit; /* this is the size */
212  else {
213  unsigned int size = t->alimit;
214  /* compute the smallest power of 2 not smaller than 'n' */
215  size |= (size >> 1);
216  size |= (size >> 2);
217  size |= (size >> 4);
218  size |= (size >> 8);
219  size |= (size >> 16);
220 #if (UINT_MAX >> 30) > 3
221  size |= (size >> 32); /* unsigned int has more than 32 bits */
222 #endif
223  size++;
224  lua_assert(ispow2(size) && size/2 < t->alimit && t->alimit < size);
225  return size;
226  }
227 }
228 
229 
230 /*
231 ** Check whether real size of the array is a power of 2.
232 ** (If it is not, 'alimit' cannot be changed to any other value
233 ** without changing the real size.)
234 */
235 static int ispow2realasize (const Table *t) {
236  return (!isrealasize(t) || ispow2(t->alimit));
237 }
238 
239 
240 static unsigned int setlimittosize (Table *t) {
241  t->alimit = luaH_realasize(t);
242  setrealasize(t);
243  return t->alimit;
244 }
245 
246 
247 #define limitasasize(t) check_exp(isrealasize(t), t->alimit)
248 
249 
250 
251 /*
252 ** "Generic" get version. (Not that generic: not valid for integers,
253 ** which may be in array part, nor for floats with integral values.)
254 */
255 static const TValue *getgeneric (Table *t, const TValue *key) {
256  Node *n = mainpositionTV(t, key);
257  for (;;) { /* check whether 'key' is somewhere in the chain */
258  if (equalkey(key, n))
259  return gval(n); /* that's it */
260  else {
261  int nx = gnext(n);
262  if (nx == 0)
263  return &absentkey; /* not found */
264  n += nx;
265  }
266  }
267 }
268 
269 
270 /*
271 ** returns the index for 'k' if 'k' is an appropriate key to live in
272 ** the array part of a table, 0 otherwise.
273 */
274 static unsigned int arrayindex (lua_Integer k) {
275  if (l_castS2U(k) - 1u < MAXASIZE) /* 'k' in [1, MAXASIZE]? */
276  return cast_uint(k); /* 'key' is an appropriate array index */
277  else
278  return 0;
279 }
280 
281 
282 /*
283 ** returns the index of a 'key' for table traversals. First goes all
284 ** elements in the array part, then elements in the hash part. The
285 ** beginning of a traversal is signaled by 0.
286 */
287 static unsigned int findindex (lua_State *L, Table *t, TValue *key,
288  unsigned int asize) {
289  unsigned int i;
290  if (ttisnil(key)) return 0; /* first iteration */
291  i = ttisinteger(key) ? arrayindex(ivalue(key)) : 0;
292  if (i - 1u < asize) /* is 'key' inside array part? */
293  return i; /* yes; that's the index */
294  else {
295  const TValue *n = getgeneric(t, key);
296  if (unlikely(isabstkey(n)))
297  luaG_runerror(L, "invalid key to 'next'"); /* key not found */
298  i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */
299  /* hash elements are numbered after array ones */
300  return (i + 1) + asize;
301  }
302 }
303 
304 
305 int luaH_next (lua_State *L, Table *t, StkId key) {
306  unsigned int asize = luaH_realasize(t);
307  unsigned int i = findindex(L, t, s2v(key), asize); /* find original key */
308  for (; i < asize; i++) { /* try first array part */
309  if (!isempty(&t->array[i])) { /* a non-empty entry? */
310  setivalue(s2v(key), i + 1);
311  setobj2s(L, key + 1, &t->array[i]);
312  return 1;
313  }
314  }
315  for (i -= asize; cast_int(i) < sizenode(t); i++) { /* hash part */
316  if (!isempty(gval(gnode(t, i)))) { /* a non-empty entry? */
317  Node *n = gnode(t, i);
318  getnodekey(L, s2v(key), n);
319  setobj2s(L, key + 1, gval(n));
320  return 1;
321  }
322  }
323  return 0; /* no more elements */
324 }
325 
326 
327 static void freehash (lua_State *L, Table *t) {
328  if (!isdummy(t))
330 }
331 
332 
333 /*
334 ** {=============================================================
335 ** Rehash
336 ** ==============================================================
337 */
338 
339 /*
340 ** Compute the optimal size for the array part of table 't'. 'nums' is a
341 ** "count array" where 'nums[i]' is the number of integers in the table
342 ** between 2^(i - 1) + 1 and 2^i. 'pna' enters with the total number of
343 ** integer keys in the table and leaves with the number of keys that
344 ** will go to the array part; return the optimal size. (The condition
345 ** 'twotoi > 0' in the for loop stops the loop if 'twotoi' overflows.)
346 */
347 static unsigned int computesizes (unsigned int nums[], unsigned int *pna) {
348  int i;
349  unsigned int twotoi; /* 2^i (candidate for optimal size) */
350  unsigned int a = 0; /* number of elements smaller than 2^i */
351  unsigned int na = 0; /* number of elements to go to array part */
352  unsigned int optimal = 0; /* optimal size for array part */
353  /* loop while keys can fill more than half of total size */
354  for (i = 0, twotoi = 1;
355  twotoi > 0 && *pna > twotoi / 2;
356  i++, twotoi *= 2) {
357  a += nums[i];
358  if (a > twotoi/2) { /* more than half elements present? */
359  optimal = twotoi; /* optimal size (till now) */
360  na = a; /* all elements up to 'optimal' will go to array part */
361  }
362  }
363  lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal);
364  *pna = na;
365  return optimal;
366 }
367 
368 
369 static int countint (lua_Integer key, unsigned int *nums) {
370  unsigned int k = arrayindex(key);
371  if (k != 0) { /* is 'key' an appropriate array index? */
372  nums[luaO_ceillog2(k)]++; /* count as such */
373  return 1;
374  }
375  else
376  return 0;
377 }
378 
379 
380 /*
381 ** Count keys in array part of table 't': Fill 'nums[i]' with
382 ** number of keys that will go into corresponding slice and return
383 ** total number of non-nil keys.
384 */
385 static unsigned int numusearray (const Table *t, unsigned int *nums) {
386  int lg;
387  unsigned int ttlg; /* 2^lg */
388  unsigned int ause = 0; /* summation of 'nums' */
389  unsigned int i = 1; /* count to traverse all array keys */
390  unsigned int asize = limitasasize(t); /* real array size */
391  /* traverse each slice */
392  for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) {
393  unsigned int lc = 0; /* counter */
394  unsigned int lim = ttlg;
395  if (lim > asize) {
396  lim = asize; /* adjust upper limit */
397  if (i > lim)
398  break; /* no more elements to count */
399  }
400  /* count elements in range (2^(lg - 1), 2^lg] */
401  for (; i <= lim; i++) {
402  if (!isempty(&t->array[i-1]))
403  lc++;
404  }
405  nums[lg] += lc;
406  ause += lc;
407  }
408  return ause;
409 }
410 
411 
412 static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) {
413  int totaluse = 0; /* total number of elements */
414  int ause = 0; /* elements added to 'nums' (can go to array part) */
415  int i = sizenode(t);
416  while (i--) {
417  Node *n = &t->node[i];
418  if (!isempty(gval(n))) {
419  if (keyisinteger(n))
420  ause += countint(keyival(n), nums);
421  totaluse++;
422  }
423  }
424  *pna += ause;
425  return totaluse;
426 }
427 
428 
429 /*
430 ** Creates an array for the hash part of a table with the given
431 ** size, or reuses the dummy node if size is zero.
432 ** The computation for size overflow is in two steps: the first
433 ** comparison ensures that the shift in the second one does not
434 ** overflow.
435 */
436 static void setnodevector (lua_State *L, Table *t, unsigned int size) {
437  if (size == 0) { /* no elements to hash part? */
438  t->node = cast(Node *, dummynode); /* use common 'dummynode' */
439  t->lsizenode = 0;
440  t->lastfree = NULL; /* signal that it is using dummy node */
441  }
442  else {
443  int i;
444  int lsize = luaO_ceillog2(size);
445  if (lsize > MAXHBITS || (1u << lsize) > MAXHSIZE)
446  luaG_runerror(L, "table overflow");
447  size = twoto(lsize);
448  t->node = luaM_newvector(L, size, Node);
449  for (i = 0; i < (int)size; i++) {
450  Node *n = gnode(t, i);
451  gnext(n) = 0;
452  setnilkey(n);
453  setempty(gval(n));
454  }
455  t->lsizenode = cast_byte(lsize);
456  t->lastfree = gnode(t, size); /* all positions are free */
457  }
458 }
459 
460 
461 /*
462 ** (Re)insert all elements from the hash part of 'ot' into table 't'.
463 */
464 static void reinsert (lua_State *L, Table *ot, Table *t) {
465  int j;
466  int size = sizenode(ot);
467  for (j = 0; j < size; j++) {
468  Node *old = gnode(ot, j);
469  if (!isempty(gval(old))) {
470  /* doesn't need barrier/invalidate cache, as entry was
471  already present in the table */
472  TValue k;
473  getnodekey(L, &k, old);
474  setobjt2t(L, luaH_set(L, t, &k), gval(old));
475  }
476  }
477 }
478 
479 
480 /*
481 ** Exchange the hash part of 't1' and 't2'.
482 */
483 static void exchangehashpart (Table *t1, Table *t2) {
484  lu_byte lsizenode = t1->lsizenode;
485  Node *node = t1->node;
486  Node *lastfree = t1->lastfree;
487  t1->lsizenode = t2->lsizenode;
488  t1->node = t2->node;
489  t1->lastfree = t2->lastfree;
490  t2->lsizenode = lsizenode;
491  t2->node = node;
492  t2->lastfree = lastfree;
493 }
494 
495 
496 /*
497 ** Resize table 't' for the new given sizes. Both allocations (for
498 ** the hash part and for the array part) can fail, which creates some
499 ** subtleties. If the first allocation, for the hash part, fails, an
500 ** error is raised and that is it. Otherwise, it copies the elements from
501 ** the shrinking part of the array (if it is shrinking) into the new
502 ** hash. Then it reallocates the array part. If that fails, the table
503 ** is in its original state; the function frees the new hash part and then
504 ** raises the allocation error. Otherwise, it sets the new hash part
505 ** into the table, initializes the new part of the array (if any) with
506 ** nils and reinserts the elements of the old hash back into the new
507 ** parts of the table.
508 */
509 void luaH_resize (lua_State *L, Table *t, unsigned int newasize,
510  unsigned int nhsize) {
511  unsigned int i;
512  Table newt; /* to keep the new hash part */
513  unsigned int oldasize = setlimittosize(t);
514  TValue *newarray;
515  /* create new hash part with appropriate size into 'newt' */
516  setnodevector(L, &newt, nhsize);
517  if (newasize < oldasize) { /* will array shrink? */
518  t->alimit = newasize; /* pretend array has new size... */
519  exchangehashpart(t, &newt); /* and new hash */
520  /* re-insert into the new hash the elements from vanishing slice */
521  for (i = newasize; i < oldasize; i++) {
522  if (!isempty(&t->array[i]))
523  luaH_setint(L, t, i + 1, &t->array[i]);
524  }
525  t->alimit = oldasize; /* restore current size... */
526  exchangehashpart(t, &newt); /* and hash (in case of errors) */
527  }
528  /* allocate new array */
529  newarray = luaM_reallocvector(L, t->array, oldasize, newasize, TValue);
530  if (unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */
531  freehash(L, &newt); /* release new hash part */
532  luaM_error(L); /* raise error (with array unchanged) */
533  }
534  /* allocation ok; initialize new part of the array */
535  exchangehashpart(t, &newt); /* 't' has the new hash ('newt' has the old) */
536  t->array = newarray; /* set new array part */
537  t->alimit = newasize;
538  for (i = oldasize; i < newasize; i++) /* clear new slice of the array */
539  setempty(&t->array[i]);
540  /* re-insert elements from old hash part into new parts */
541  reinsert(L, &newt, t); /* 'newt' now has the old hash */
542  freehash(L, &newt); /* free old hash part */
543 }
544 
545 
546 void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) {
547  int nsize = allocsizenode(t);
548  luaH_resize(L, t, nasize, nsize);
549 }
550 
551 /*
552 ** nums[i] = number of keys 'k' where 2^(i - 1) < k <= 2^i
553 */
554 static void rehash (lua_State *L, Table *t, const TValue *ek) {
555  unsigned int asize; /* optimal size for array part */
556  unsigned int na; /* number of keys in the array part */
557  unsigned int nums[MAXABITS + 1];
558  int i;
559  int totaluse;
560  for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */
561  setlimittosize(t);
562  na = numusearray(t, nums); /* count keys in array part */
563  totaluse = na; /* all those keys are integer keys */
564  totaluse += numusehash(t, nums, &na); /* count keys in hash part */
565  /* count extra key */
566  if (ttisinteger(ek))
567  na += countint(ivalue(ek), nums);
568  totaluse++;
569  /* compute new size for array part */
570  asize = computesizes(nums, &na);
571  /* resize the table to new computed sizes */
572  luaH_resize(L, t, asize, totaluse - na);
573 }
574 
575 
576 
577 /*
578 ** }=============================================================
579 */
580 
581 
583  GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table));
584  Table *t = gco2t(o);
585  t->metatable = NULL;
586  t->flags = cast_byte(maskflags); /* table has no metamethod fields */
587  t->array = NULL;
588  t->alimit = 0;
589  setnodevector(L, t, 0);
590  return t;
591 }
592 
593 
594 void luaH_free (lua_State *L, Table *t) {
595  freehash(L, t);
597  luaM_free(L, t);
598 }
599 
600 
601 static Node *getfreepos (Table *t) {
602  if (!isdummy(t)) {
603  while (t->lastfree > t->node) {
604  t->lastfree--;
605  if (keyisnil(t->lastfree))
606  return t->lastfree;
607  }
608  }
609  return NULL; /* could not find a free place */
610 }
611 
612 
613 
614 /*
615 ** inserts a new key into a hash table; first, check whether key's main
616 ** position is free. If not, check whether colliding node is in its main
617 ** position or not: if it is not, move colliding node to an empty place and
618 ** put new key in its main position; otherwise (colliding node is in its main
619 ** position), new key goes to an empty position.
620 */
621 TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
622  Node *mp;
623  TValue aux;
624  if (unlikely(ttisnil(key)))
625  luaG_runerror(L, "table index is nil");
626  else if (ttisfloat(key)) {
627  lua_Number f = fltvalue(key);
628  lua_Integer k;
629  if (luaV_flttointeger(f, &k, F2Ieq)) { /* does key fit in an integer? */
630  setivalue(&aux, k);
631  key = &aux; /* insert it as an integer */
632  }
633  else if (unlikely(luai_numisnan(f)))
634  luaG_runerror(L, "table index is NaN");
635  }
636  mp = mainpositionTV(t, key);
637  if (!isempty(gval(mp)) || isdummy(t)) { /* main position is taken? */
638  Node *othern;
639  Node *f = getfreepos(t); /* get a free place */
640  if (f == NULL) { /* cannot find a free place? */
641  rehash(L, t, key); /* grow table */
642  /* whatever called 'newkey' takes care of TM cache */
643  return luaH_set(L, t, key); /* insert key into grown table */
644  }
645  lua_assert(!isdummy(t));
646  othern = mainposition(t, keytt(mp), &keyval(mp));
647  if (othern != mp) { /* is colliding node out of its main position? */
648  /* yes; move colliding node into free position */
649  while (othern + gnext(othern) != mp) /* find previous */
650  othern += gnext(othern);
651  gnext(othern) = cast_int(f - othern); /* rechain to point to 'f' */
652  *f = *mp; /* copy colliding node into free pos. (mp->next also goes) */
653  if (gnext(mp) != 0) {
654  gnext(f) += cast_int(mp - f); /* correct 'next' */
655  gnext(mp) = 0; /* now 'mp' is free */
656  }
657  setempty(gval(mp));
658  }
659  else { /* colliding node is in its own main position */
660  /* new node will go into free position */
661  if (gnext(mp) != 0)
662  gnext(f) = cast_int((mp + gnext(mp)) - f); /* chain new position */
663  else lua_assert(gnext(f) == 0);
664  gnext(mp) = cast_int(f - mp);
665  mp = f;
666  }
667  }
668  setnodekey(L, mp, key);
669  luaC_barrierback(L, obj2gco(t), key);
670  lua_assert(isempty(gval(mp)));
671  return gval(mp);
672 }
673 
674 
675 /*
676 ** Search function for integers. If integer is inside 'alimit', get it
677 ** directly from the array part. Otherwise, if 'alimit' is not equal to
678 ** the real size of the array, key still can be in the array part. In
679 ** this case, try to avoid a call to 'luaH_realasize' when key is just
680 ** one more than the limit (so that it can be incremented without
681 ** changing the real size of the array).
682 */
684  if (l_castS2U(key) - 1u < t->alimit) /* 'key' in [1, t->alimit]? */
685  return &t->array[key - 1];
686  else if (!limitequalsasize(t) && /* key still may be in the array part? */
687  (l_castS2U(key) == t->alimit + 1 ||
688  l_castS2U(key) - 1u < luaH_realasize(t))) {
689  t->alimit = cast_uint(key); /* probably '#t' is here now */
690  return &t->array[key - 1];
691  }
692  else {
693  Node *n = hashint(t, key);
694  for (;;) { /* check whether 'key' is somewhere in the chain */
695  if (keyisinteger(n) && keyival(n) == key)
696  return gval(n); /* that's it */
697  else {
698  int nx = gnext(n);
699  if (nx == 0) break;
700  n += nx;
701  }
702  }
703  return &absentkey;
704  }
705 }
706 
707 
708 /*
709 ** search function for short strings
710 */
712  Node *n = hashstr(t, key);
713  lua_assert(key->tt == LUA_VSHRSTR);
714  for (;;) { /* check whether 'key' is somewhere in the chain */
715  if (keyisshrstr(n) && eqshrstr(keystrval(n), key))
716  return gval(n); /* that's it */
717  else {
718  int nx = gnext(n);
719  if (nx == 0)
720  return &absentkey; /* not found */
721  n += nx;
722  }
723  }
724 }
725 
726 
727 const TValue *luaH_getstr (Table *t, TString *key) {
728  if (key->tt == LUA_VSHRSTR)
729  return luaH_getshortstr(t, key);
730  else { /* for long strings, use generic case */
731  TValue ko;
732  setsvalue(cast(lua_State *, NULL), &ko, key);
733  return getgeneric(t, &ko);
734  }
735 }
736 
737 
738 /*
739 ** main search function
740 */
741 const TValue *luaH_get (Table *t, const TValue *key) {
742  switch (ttypetag(key)) {
743  case LUA_VSHRSTR: return luaH_getshortstr(t, tsvalue(key));
744  case LUA_VNUMINT: return luaH_getint(t, ivalue(key));
745  case LUA_VNIL: return &absentkey;
746  case LUA_VNUMFLT: {
747  lua_Integer k;
748  if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */
749  return luaH_getint(t, k); /* use specialized version */
750  /* else... */
751  } /* FALLTHROUGH */
752  default:
753  return getgeneric(t, key);
754  }
755 }
756 
757 
758 /*
759 ** beware: when using this function you probably need to check a GC
760 ** barrier and invalidate the TM cache.
761 */
762 TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
763  const TValue *p = luaH_get(t, key);
764  if (!isabstkey(p))
765  return cast(TValue *, p);
766  else return luaH_newkey(L, t, key);
767 }
768 
769 
771  const TValue *p = luaH_getint(t, key);
772  TValue *cell;
773  if (!isabstkey(p))
774  cell = cast(TValue *, p);
775  else {
776  TValue k;
777  setivalue(&k, key);
778  cell = luaH_newkey(L, t, &k);
779  }
780  setobj2t(L, cell, value);
781 }
782 
783 
784 /*
785 ** Try to find a boundary in the hash part of table 't'. From the
786 ** caller, we know that 'j' is zero or present and that 'j + 1' is
787 ** present. We want to find a larger key that is absent from the
788 ** table, so that we can do a binary search between the two keys to
789 ** find a boundary. We keep doubling 'j' until we get an absent index.
790 ** If the doubling would overflow, we try LUA_MAXINTEGER. If it is
791 ** absent, we are ready for the binary search. ('j', being max integer,
792 ** is larger or equal to 'i', but it cannot be equal because it is
793 ** absent while 'i' is present; so 'j > i'.) Otherwise, 'j' is a
794 ** boundary. ('j + 1' cannot be a present integer key because it is
795 ** not a valid integer in Lua.)
796 */
798  lua_Unsigned i;
799  if (j == 0) j++; /* the caller ensures 'j + 1' is present */
800  do {
801  i = j; /* 'i' is a present index */
802  if (j <= l_castS2U(LUA_MAXINTEGER) / 2)
803  j *= 2;
804  else {
805  j = LUA_MAXINTEGER;
806  if (isempty(luaH_getint(t, j))) /* t[j] not present? */
807  break; /* 'j' now is an absent index */
808  else /* weird case */
809  return j; /* well, max integer is a boundary... */
810  }
811  } while (!isempty(luaH_getint(t, j))); /* repeat until an absent t[j] */
812  /* i < j && t[i] present && t[j] absent */
813  while (j - i > 1u) { /* do a binary search between them */
814  lua_Unsigned m = (i + j) / 2;
815  if (isempty(luaH_getint(t, m))) j = m;
816  else i = m;
817  }
818  return i;
819 }
820 
821 
822 static unsigned int binsearch (const TValue *array, unsigned int i,
823  unsigned int j) {
824  while (j - i > 1u) { /* binary search */
825  unsigned int m = (i + j) / 2;
826  if (isempty(&array[m - 1])) j = m;
827  else i = m;
828  }
829  return i;
830 }
831 
832 
833 /*
834 ** Try to find a boundary in table 't'. (A 'boundary' is an integer index
835 ** such that t[i] is present and t[i+1] is absent, or 0 if t[1] is absent
836 ** and 'maxinteger' if t[maxinteger] is present.)
837 ** (In the next explanation, we use Lua indices, that is, with base 1.
838 ** The code itself uses base 0 when indexing the array part of the table.)
839 ** The code starts with 'limit = t->alimit', a position in the array
840 ** part that may be a boundary.
841 **
842 ** (1) If 't[limit]' is empty, there must be a boundary before it.
843 ** As a common case (e.g., after 't[#t]=nil'), check whether 'limit-1'
844 ** is present. If so, it is a boundary. Otherwise, do a binary search
845 ** between 0 and limit to find a boundary. In both cases, try to
846 ** use this boundary as the new 'alimit', as a hint for the next call.
847 **
848 ** (2) If 't[limit]' is not empty and the array has more elements
849 ** after 'limit', try to find a boundary there. Again, try first
850 ** the special case (which should be quite frequent) where 'limit+1'
851 ** is empty, so that 'limit' is a boundary. Otherwise, check the
852 ** last element of the array part. If it is empty, there must be a
853 ** boundary between the old limit (present) and the last element
854 ** (absent), which is found with a binary search. (This boundary always
855 ** can be a new limit.)
856 **
857 ** (3) The last case is when there are no elements in the array part
858 ** (limit == 0) or its last element (the new limit) is present.
859 ** In this case, must check the hash part. If there is no hash part
860 ** or 'limit+1' is absent, 'limit' is a boundary. Otherwise, call
861 ** 'hash_search' to find a boundary in the hash part of the table.
862 ** (In those cases, the boundary is not inside the array part, and
863 ** therefore cannot be used as a new limit.)
864 */
866  unsigned int limit = t->alimit;
867  if (limit > 0 && isempty(&t->array[limit - 1])) { /* (1)? */
868  /* there must be a boundary before 'limit' */
869  if (limit >= 2 && !isempty(&t->array[limit - 2])) {
870  /* 'limit - 1' is a boundary; can it be a new limit? */
871  if (ispow2realasize(t) && !ispow2(limit - 1)) {
872  t->alimit = limit - 1;
873  setnorealasize(t); /* now 'alimit' is not the real size */
874  }
875  return limit - 1;
876  }
877  else { /* must search for a boundary in [0, limit] */
878  unsigned int boundary = binsearch(t->array, 0, limit);
879  /* can this boundary represent the real size of the array? */
880  if (ispow2realasize(t) && boundary > luaH_realasize(t) / 2) {
881  t->alimit = boundary; /* use it as the new limit */
882  setnorealasize(t);
883  }
884  return boundary;
885  }
886  }
887  /* 'limit' is zero or present in table */
888  if (!limitequalsasize(t)) { /* (2)? */
889  /* 'limit' > 0 and array has more elements after 'limit' */
890  if (isempty(&t->array[limit])) /* 'limit + 1' is empty? */
891  return limit; /* this is the boundary */
892  /* else, try last element in the array */
893  limit = luaH_realasize(t);
894  if (isempty(&t->array[limit - 1])) { /* empty? */
895  /* there must be a boundary in the array after old limit,
896  and it must be a valid new limit */
897  unsigned int boundary = binsearch(t->array, t->alimit, limit);
898  t->alimit = boundary;
899  return boundary;
900  }
901  /* else, new limit is present in the table; check the hash part */
902  }
903  /* (3) 'limit' is the last element and either is zero or present in table */
904  lua_assert(limit == luaH_realasize(t) &&
905  (limit == 0 || !isempty(&t->array[limit - 1])));
906  if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, limit + 1))))
907  return limit; /* 'limit + 1' is absent */
908  else /* 'limit + 1' is also present */
909  return hash_search(t, limit);
910 }
911 
912 
913 
914 #if defined(LUA_DEBUG)
915 
916 /* export these functions for the test library */
917 
918 Node *luaH_mainposition (const Table *t, const TValue *key) {
919  return mainpositionTV(t, key);
920 }
921 
922 int luaH_isdummy (const Table *t) { return isdummy(t); }
923 
924 #endif
#define luaM_error(L)
Definition: lmem.h:17
#define hashstr(t, str)
Definition: ltable.c:73
static lua_Unsigned hash_search(Table *t, lua_Unsigned j)
Definition: ltable.c:797
enum MQTTPropertyCodes value
#define cast_num(i)
Definition: llimits.h:127
#define gcvalue(o)
Definition: lobject.h:281
#define ttisfloat(o)
Definition: lobject.h:303
#define hashboolean(t, p)
Definition: ltable.c:74
#define luaM_newvector(L, n, t)
Definition: lmem.h:60
#define setobjt2t
Definition: lobject.h:129
#define ttisinteger(o)
Definition: lobject.h:304
#define s2v(o)
Definition: lobject.h:148
Definition: lobject.h:712
#define obj2gco(v)
Definition: lstate.h:381
#define LUA_VEMPTY
Definition: lobject.h:162
#define hashmod(t, n)
Definition: ltable.c:82
#define gnode(t, i)
Definition: ltable.h:13
#define luaM_freearray(L, b, n)
Definition: lmem.h:57
static void freehash(lua_State *L, Table *t)
Definition: ltable.c:327
const TValue * luaH_getint(Table *t, lua_Integer key)
Definition: ltable.c:683
#define LUAI_FUNC
Definition: luaconf.h:322
static Node * mainpositionTV(const Table *t, const TValue *key)
Definition: ltable.c:163
#define gco2t(o)
Definition: lstate.h:371
#define gval(n)
Definition: ltable.h:14
#define fvalue(o)
Definition: lobject.h:580
unsigned int luaS_hashlongstr(TString *ts)
Definition: lstring.c:62
static int equalkey(const TValue *k1, const Node *n2)
Definition: ltable.c:176
#define LUA_VNUMINT
Definition: lobject.h:299
#define hashint(t, i)
Definition: ltable.c:75
#define hashpointer(t, p)
Definition: ltable.c:85
#define gcvalueraw(v)
Definition: lobject.h:283
#define cast_uint(i)
Definition: llimits.h:129
#define isrealasize(t)
Definition: lobject.h:707
#define pvalueraw(v)
Definition: lobject.h:415
Definition: lobject.h:63
#define cast(t, exp)
Definition: llimits.h:123
#define fltvalue(o)
Definition: lobject.h:308
#define setnodekey(L, node, obj)
Definition: lobject.h:686
const TValue * luaH_getstr(Table *t, TString *key)
Definition: ltable.c:727
#define keytt(node)
Definition: lobject.h:728
#define cast_byte(i)
Definition: llimits.h:130
#define LUA_VTABLE
Definition: lobject.h:653
void luaH_free(lua_State *L, Table *t)
Definition: ltable.c:594
#define dummynode
Definition: ltable.c:88
#define ABSTKEYCONSTANT
Definition: lobject.h:197
static unsigned int binsearch(const TValue *array, unsigned int i, unsigned int j)
Definition: ltable.c:822
#define LUA_VNUMFLT
Definition: lobject.h:300
#define getnodekey(L, obj, node)
Definition: lobject.h:693
#define tsvalueraw(v)
Definition: lobject.h:343
const TValue * luaH_getshortstr(Table *t, TString *key)
Definition: ltable.c:711
#define pvalue(o)
Definition: lobject.h:412
static int l_hashfloat(lua_Number n)
Definition: ltable.c:114
#define gnext(n)
Definition: ltable.h:15
#define unlikely(x)
Definition: llimits.h:162
#define keyival(node)
Definition: lobject.h:733
#define luai_numisnan(a)
Definition: llimits.h:332
#define nodefromval(v)
Definition: ltable.h:35
static int countint(lua_Integer key, unsigned int *nums)
Definition: ltable.c:369
Definition: lobject.h:47
#define isempty(v)
Definition: lobject.h:193
#define luaM_reallocvector(L, v, oldn, n, t)
Definition: lmem.h:70
static const Node dummynode_
Definition: ltable.c:90
#define fvalueraw(v)
Definition: lobject.h:583
static void reinsert(lua_State *L, Table *ot, Table *t)
Definition: ltable.c:464
LUA_INTEGER lua_Integer
Definition: lua.h:94
TValue * luaH_newkey(lua_State *L, Table *t, const TValue *key)
Definition: ltable.c:621
#define keystrval(node)
Definition: lobject.h:735
unsigned char lu_byte
Definition: llimits.h:36
static int ispow2realasize(const Table *t)
Definition: ltable.c:235
int luaO_ceillog2(unsigned int x)
Definition: lobject.c:35
#define MAXABITS
Definition: ltable.c:46
Definition: lvm.h:44
#define ispow2(x)
Definition: llimits.h:66
#define LUA_VNIL
Definition: lobject.h:159
TValue * luaH_set(lua_State *L, Table *t, const TValue *key)
Definition: ltable.c:762
void luaH_resize(lua_State *L, Table *t, unsigned int newasize, unsigned int nhsize)
Definition: ltable.c:509
#define LUA_VFALSE
Definition: lobject.h:215
#define lua_numbertointeger(n, p)
Definition: luaconf.h:428
#define LUA_VSHRSTR
Definition: lobject.h:336
#define allocsizenode(t)
Definition: ltable.h:31
static void setnodevector(lua_State *L, Table *t, unsigned int size)
Definition: ltable.c:436
#define withvariant(t)
Definition: lobject.h:79
#define ttisnil(v)
Definition: lobject.h:169
#define LUA_VTRUE
Definition: lobject.h:216
#define ivalue(o)
Definition: lobject.h:309
#define l_castS2U(i)
Definition: llimits.h:139
#define MAXASIZE
Definition: ltable.c:54
#define LUA_VLNGSTR
Definition: lobject.h:337
#define keyval(node)
Definition: lobject.h:729
#define fltvalueraw(v)
Definition: lobject.h:311
#define setnilkey(node)
Definition: lobject.h:737
#define luaM_free(L, b)
Definition: lmem.h:56
TValue * array
Definition: lobject.h:717
l_noret luaG_runerror(lua_State *L, const char *fmt,...)
Definition: ldebug.c:767
lu_byte lsizenode
Definition: lobject.h:715
#define twoto(x)
Definition: lobject.h:765
#define LUA_VLIGHTUSERDATA
Definition: lobject.h:405
static unsigned int numusearray(const Table *t, unsigned int *nums)
Definition: ltable.c:385
Table * luaH_new(lua_State *L)
Definition: ltable.c:582
#define keyisinteger(node)
Definition: lobject.h:732
#define MAXHSIZE
Definition: ltable.c:68
Node * node
Definition: lobject.h:718
#define ttypetag(o)
Definition: lobject.h:80
#define tsvalue(o)
Definition: lobject.h:345
#define setobj2t
Definition: lobject.h:133
static Node * getfreepos(Table *t)
Definition: ltable.c:601
#define lua_assert(c)
Definition: llimits.h:101
static int numusehash(const Table *t, unsigned int *nums, unsigned int *pna)
Definition: ltable.c:412
static unsigned int setlimittosize(Table *t)
Definition: ltable.c:240
#define valraw(o)
Definition: lobject.h:69
#define eqshrstr(a, b)
Definition: lstring.h:41
#define maskflags
Definition: ltm.h:54
lua_Unsigned luaH_getn(Table *t)
Definition: ltable.c:865
#define setivalue(obj, x)
Definition: lobject.h:320
#define hashpow2(t, n)
Definition: ltable.c:71
struct Table * metatable
Definition: lobject.h:720
static unsigned int findindex(lua_State *L, Table *t, TValue *key, unsigned int asize)
Definition: ltable.c:287
static unsigned int computesizes(unsigned int nums[], unsigned int *pna)
Definition: ltable.c:347
static void exchangehashpart(Table *t1, Table *t2)
Definition: ltable.c:483
static Node * mainposition(const Table *t, int ktt, const Value *kvl)
Definition: ltable.c:136
lu_byte flags
Definition: lobject.h:714
static const TValue absentkey
Definition: ltable.c:96
void luaH_resizearray(lua_State *L, Table *t, unsigned int nasize)
Definition: ltable.c:546
#define luaC_barrierback(L, p, v)
Definition: lgc.h:169
#define l_mathop(op)
Definition: luaconf.h:478
LUA_UNSIGNED lua_Unsigned
Definition: lua.h:97
int luaV_flttointeger(lua_Number n, lua_Integer *p, F2Imod mode)
Definition: lvm.c:121
#define ivalueraw(v)
Definition: lobject.h:312
void luaH_setint(lua_State *L, Table *t, lua_Integer key, TValue *value)
Definition: ltable.c:770
#define isdummy(t)
Definition: ltable.h:27
GCObject * luaC_newobj(lua_State *L, int tt, size_t sz)
Definition: lgc.c:259
#define rawtt(o)
Definition: lobject.h:73
#define keyisnil(node)
Definition: lobject.h:731
#define sizenode(t)
Definition: lobject.h:766
#define setrealasize(t)
Definition: lobject.h:708
Node * lastfree
Definition: lobject.h:719
#define LUA_VLCF
Definition: lobject.h:567
Definition: lobject.h:674
int luaH_next(lua_State *L, Table *t, StkId key)
Definition: ltable.c:305
#define setempty(v)
Definition: lobject.h:201
#define limitasasize(t)
Definition: ltable.c:247
#define cast_int(i)
Definition: llimits.h:128
#define cast_sizet(i)
Definition: llimits.h:134
#define limitequalsasize(t)
Definition: ltable.c:203
#define luai_numeq(a, b)
Definition: llimits.h:327
LUAI_FUNC unsigned int luaH_realasize(const Table *t)
Definition: ltable.c:209
unsigned int alimit
Definition: lobject.h:716
static unsigned int arrayindex(lua_Integer k)
Definition: ltable.c:274
#define keyisshrstr(node)
Definition: lobject.h:734
const TValue * luaH_get(Table *t, const TValue *key)
Definition: ltable.c:741
int luaS_eqlngstr(TString *a, TString *b)
Definition: lstring.c:44
static void rehash(lua_State *L, Table *t, const TValue *ek)
Definition: ltable.c:554
LUA_NUMBER lua_Number
Definition: lua.h:90
#define setobj2s(L, o1, o2)
Definition: lobject.h:127
#define setnorealasize(t)
Definition: lobject.h:709
#define isabstkey(v)
Definition: lobject.h:179
static const TValue * getgeneric(Table *t, const TValue *key)
Definition: ltable.c:255
#define setsvalue(L, obj, x)
Definition: lobject.h:347
#define MAXHBITS
Definition: ltable.c:60


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:09