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)
l_hashfloat(lua_Number n)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 */
mainposition(const Table * t,int ktt,const Value * kvl)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 */
mainpositionTV(const Table * t,const TValue * key)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 */
equalkey(const TValue * k1,const Node * n2)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 */
luaH_realasize(const Table * t)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 */
ispow2realasize(const Table * t)235 static int ispow2realasize (const Table *t) {
236 return (!isrealasize(t) || ispow2(t->alimit));
237 }
238
239
setlimittosize(Table * t)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 */
getgeneric(Table * t,const TValue * key)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 */
arrayindex(lua_Integer k)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 */
findindex(lua_State * L,Table * t,TValue * key,unsigned int asize)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
luaH_next(lua_State * L,Table * t,StkId key)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
freehash(lua_State * L,Table * t)327 static void freehash (lua_State *L, Table *t) {
328 if (!isdummy(t))
329 luaM_freearray(L, t->node, cast_sizet(sizenode(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 */
computesizes(unsigned int nums[],unsigned int * pna)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
countint(lua_Integer key,unsigned int * nums)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 */
numusearray(const Table * t,unsigned int * nums)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
numusehash(const Table * t,unsigned int * nums,unsigned int * pna)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 */
setnodevector(lua_State * L,Table * t,unsigned int size)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 */
reinsert(lua_State * L,Table * ot,Table * t)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 */
exchangehashpart(Table * t1,Table * t2)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 */
luaH_resize(lua_State * L,Table * t,unsigned int newasize,unsigned int nhsize)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
luaH_resizearray(lua_State * L,Table * t,unsigned int nasize)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 */
rehash(lua_State * L,Table * t,const TValue * ek)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
luaH_new(lua_State * L)582 Table *luaH_new (lua_State *L) {
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
luaH_free(lua_State * L,Table * t)594 void luaH_free (lua_State *L, Table *t) {
595 freehash(L, t);
596 luaM_freearray(L, t->array, luaH_realasize(t));
597 luaM_free(L, t);
598 }
599
600
getfreepos(Table * t)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 */
luaH_newkey(lua_State * L,Table * t,const TValue * key)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 */
luaH_getint(Table * t,lua_Integer key)683 const TValue *luaH_getint (Table *t, lua_Integer key) {
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 */
luaH_getshortstr(Table * t,TString * key)711 const TValue *luaH_getshortstr (Table *t, TString *key) {
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
luaH_getstr(Table * t,TString * key)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 */
luaH_get(Table * t,const TValue * key)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 */
luaH_set(lua_State * L,Table * t,const TValue * key)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
luaH_setint(lua_State * L,Table * t,lua_Integer key,TValue * value)770 void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) {
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 */
hash_search(Table * t,lua_Unsigned j)797 static lua_Unsigned hash_search (Table *t, lua_Unsigned j) {
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
binsearch(const TValue * array,unsigned int i,unsigned int j)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 */
luaH_getn(Table * t)865 lua_Unsigned luaH_getn (Table *t) {
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
luaH_mainposition(const Table * t,const TValue * key)918 Node *luaH_mainposition (const Table *t, const TValue *key) {
919 return mainpositionTV(t, key);
920 }
921
luaH_isdummy(const Table * t)922 int luaH_isdummy (const Table *t) { return isdummy(t); }
923
924 #endif
925