46#define MAXABITS cast_int(sizeof(int) * CHAR_BIT - 1)
54#define MAXASIZE luaM_limitN(1u << MAXABITS, TValue)
60#define MAXHBITS (MAXABITS - 1)
68#define MAXHSIZE luaM_limitN(1u << MAXHBITS, Node)
75#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
81#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
84#define hashstr(t,str) hashpow2(t, (str)->hash)
85#define hashboolean(t,p) hashpow2(t, p)
88#define hashpointer(t,p) hashmod(t, point2uint(p))
91#define dummynode (&dummynode_)
93static const Node dummynode_ = {
99static const TValue absentkey = {ABSTKEYCONSTANT};
108static Node *hashint (
const Table *t, lua_Integer i) {
109 lua_Unsigned ui = l_castS2U(i);
110 if (ui <= cast_uint(INT_MAX))
111 return hashmod(t, cast_int(ui));
113 return hashmod(t, ui);
130#if !defined(l_hashfloat)
131static int l_hashfloat (lua_Number n) {
134 n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN);
135 if (!lua_numbertointeger(n, &ni)) {
136 lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL));
140 unsigned int u = cast_uint(i) + cast_uint(ni);
141 return cast_int(u <= cast_uint(INT_MAX) ? u : ~u);
152 switch (ttypetag(key)) {
154 lua_Integer i = ivalue(key);
155 return hashint(t, i);
158 lua_Number n = fltvalue(key);
159 return hashmod(t, l_hashfloat(n));
163 return hashstr(t, ts);
167 return hashpow2(t, luaS_hashlongstr(ts));
170 return hashboolean(t, 0);
172 return hashboolean(t, 1);
173 case LUA_VLIGHTUSERDATA: {
174 void *p = pvalue(key);
175 return hashpointer(t, p);
178 lua_CFunction f = fvalue(key);
179 return hashpointer(t, f);
183 return hashpointer(t, o);
189l_sinline
Node *mainpositionfromnode (
const Table *t,
Node *nd) {
191 getnodekey(cast(
lua_State *, NULL), &key, nd);
192 return mainpositionTV(t, &key);
216static int equalkey (
const TValue *k1,
const Node *n2,
int deadok) {
217 if ((rawtt(k1) != keytt(n2)) &&
218 !(deadok && keyisdead(n2) && iscollectable(k1)))
221 case LUA_VNIL:
case LUA_VFALSE:
case LUA_VTRUE:
224 return (ivalue(k1) == keyival(n2));
226 return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2)));
227 case LUA_VLIGHTUSERDATA:
228 return pvalue(k1) == pvalueraw(keyval(n2));
230 return fvalue(k1) == fvalueraw(keyval(n2));
231 case ctb(LUA_VLNGSTR):
232 return luaS_eqlngstr(tsvalue(k1), keystrval(n2));
234 return gcvalue(k1) == gcvalueraw(keyval(n2));
244#define limitequalsasize(t) (isrealasize(t) || ispow2((t)->alimit))
250LUAI_FUNC
unsigned int luaH_realasize (
const Table *t) {
251 if (limitequalsasize(t))
254 unsigned int size = t->alimit;
260#if (UINT_MAX >> 14) > 3
261 size |= (size >> 16);
262#if (UINT_MAX >> 30) > 3
263 size |= (size >> 32);
267 lua_assert(ispow2(size) && size/2 < t->alimit && t->alimit < size);
278static int ispow2realasize (
const Table *t) {
279 return (!isrealasize(t) || ispow2(t->alimit));
283static unsigned int setlimittosize (
Table *t) {
284 t->alimit = luaH_realasize(t);
290#define limitasasize(t) check_exp(isrealasize(t), t->alimit)
300 Node *n = mainpositionTV(t, key);
302 if (equalkey(key, n, deadok))
318static unsigned int arrayindex (lua_Integer k) {
319 if (l_castS2U(k) - 1u < MAXASIZE)
332 unsigned int asize) {
334 if (ttisnil(key))
return 0;
335 i = ttisinteger(key) ? arrayindex(ivalue(key)) : 0;
339 const TValue *n = getgeneric(t, key, 1);
340 if (l_unlikely(isabstkey(n)))
341 luaG_runerror(L,
"invalid key to 'next'");
342 i = cast_int(nodefromval(n) - gnode(t, 0));
344 return (i + 1) + asize;
350 unsigned int asize = luaH_realasize(t);
351 unsigned int i = findindex(L, t, s2v(key), asize);
352 for (; i < asize; i++) {
353 if (!isempty(&t->array[i])) {
354 setivalue(s2v(key), i + 1);
355 setobj2s(L, key + 1, &t->array[i]);
359 for (i -= asize; cast_int(i) < sizenode(t); i++) {
360 if (!isempty(gval(gnode(t, i)))) {
361 Node *n = gnode(t, i);
362 getnodekey(L, s2v(key), n);
363 setobj2s(L, key + 1, gval(n));
373 luaM_freearray(L, t->node, cast_sizet(sizenode(t)));
391static unsigned int computesizes (
unsigned int nums[],
unsigned int *pna) {
396 unsigned int optimal = 0;
398 for (i = 0, twotoi = 1;
399 twotoi > 0 && *pna > twotoi / 2;
407 lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal);
413static int countint (lua_Integer key,
unsigned int *nums) {
414 unsigned int k = arrayindex(key);
416 nums[luaO_ceillog2(k)]++;
429static unsigned int numusearray (
const Table *t,
unsigned int *nums) {
432 unsigned int ause = 0;
434 unsigned int asize = limitasasize(t);
436 for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) {
438 unsigned int lim = ttlg;
445 for (; i <= lim; i++) {
446 if (!isempty(&t->array[i-1]))
456static int numusehash (
const Table *t,
unsigned int *nums,
unsigned int *pna) {
461 Node *n = &t->node[i];
462 if (!isempty(gval(n))) {
464 ause += countint(keyival(n), nums);
480static void setnodevector (
lua_State *L,
Table *t,
unsigned int size) {
482 t->node = cast(
Node *, dummynode);
488 int lsize = luaO_ceillog2(size);
489 if (lsize > MAXHBITS || (1u << lsize) > MAXHSIZE)
490 luaG_runerror(L,
"table overflow");
492 t->node = luaM_newvector(L, size,
Node);
493 for (i = 0; i < cast_int(size); i++) {
494 Node *n = gnode(t, i);
499 t->lsizenode = cast_byte(lsize);
500 t->lastfree = gnode(t, size);
510 int size = sizenode(ot);
511 for (j = 0; j < size; j++) {
512 Node *old = gnode(ot, j);
513 if (!isempty(gval(old))) {
517 getnodekey(L, &k, old);
518 luaH_set(L, t, &k, gval(old));
527static void exchangehashpart (
Table *t1,
Table *t2) {
528 lu_byte lsizenode = t1->lsizenode;
529 Node *node = t1->node;
530 Node *lastfree = t1->lastfree;
531 t1->lsizenode = t2->lsizenode;
533 t1->lastfree = t2->lastfree;
534 t2->lsizenode = lsizenode;
536 t2->lastfree = lastfree;
554 unsigned int nhsize) {
557 unsigned int oldasize = setlimittosize(t);
560 setnodevector(L, &newt, nhsize);
561 if (newasize < oldasize) {
562 t->alimit = newasize;
563 exchangehashpart(t, &newt);
565 for (i = newasize; i < oldasize; i++) {
566 if (!isempty(&t->array[i]))
567 luaH_setint(L, t, i + 1, &t->array[i]);
569 t->alimit = oldasize;
570 exchangehashpart(t, &newt);
573 newarray = luaM_reallocvector(L, t->array, oldasize, newasize,
TValue);
574 if (l_unlikely(newarray == NULL && newasize > 0)) {
579 exchangehashpart(t, &newt);
581 t->alimit = newasize;
582 for (i = oldasize; i < newasize; i++)
583 setempty(&t->array[i]);
585 reinsert(L, &newt, t);
590void luaH_resizearray (
lua_State *L,
Table *t,
unsigned int nasize) {
591 int nsize = allocsizenode(t);
592 luaH_resize(L, t, nasize, nsize);
601 unsigned int nums[MAXABITS + 1];
604 for (i = 0; i <= MAXABITS; i++) nums[i] = 0;
606 na = numusearray(t, nums);
608 totaluse += numusehash(t, nums, &na);
611 na += countint(ivalue(ek), nums);
614 asize = computesizes(nums, &na);
616 luaH_resize(L, t, asize, totaluse - na);
630 t->flags = cast_byte(maskflags);
633 setnodevector(L, t, 0);
640 luaM_freearray(L, t->array, luaH_realasize(t));
647 while (t->lastfree > t->node) {
649 if (keyisnil(t->lastfree))
668 if (l_unlikely(ttisnil(key)))
669 luaG_runerror(L,
"table index is nil");
670 else if (ttisfloat(key)) {
671 lua_Number f = fltvalue(key);
673 if (luaV_flttointeger(f, &k, F2Ieq)) {
677 else if (l_unlikely(luai_numisnan(f)))
678 luaG_runerror(L,
"table index is NaN");
682 mp = mainpositionTV(t, key);
683 if (!isempty(gval(mp)) || isdummy(t)) {
685 Node *f = getfreepos(t);
689 luaH_set(L, t, key, value);
692 lua_assert(!isdummy(t));
693 othern = mainpositionfromnode(t, mp);
696 while (othern + gnext(othern) != mp)
697 othern += gnext(othern);
698 gnext(othern) = cast_int(f - othern);
700 if (gnext(mp) != 0) {
701 gnext(f) += cast_int(mp - f);
709 gnext(f) = cast_int((mp + gnext(mp)) - f);
710 else lua_assert(gnext(f) == 0);
711 gnext(mp) = cast_int(f - mp);
715 setnodekey(L, mp, key);
716 luaC_barrierback(L, obj2gco(t), key);
717 lua_assert(isempty(gval(mp)));
718 setobj2t(L, gval(mp), value);
730const TValue *luaH_getint (
Table *t, lua_Integer key) {
731 if (l_castS2U(key) - 1u < t->alimit)
732 return &t->array[key - 1];
733 else if (!limitequalsasize(t) &&
734 (l_castS2U(key) == t->alimit + 1 ||
735 l_castS2U(key) - 1u < luaH_realasize(t))) {
736 t->alimit = cast_uint(key);
737 return &t->array[key - 1];
740 Node *n = hashint(t, key);
742 if (keyisinteger(n) && keyival(n) == key)
759 Node *n = hashstr(t, key);
760 lua_assert(key->tt == LUA_VSHRSTR);
762 if (keyisshrstr(n) && eqshrstr(keystrval(n), key))
775 if (key->tt == LUA_VSHRSTR)
776 return luaH_getshortstr(t, key);
779 setsvalue(cast(
lua_State *, NULL), &ko, key);
780 return getgeneric(t, &ko, 0);
789 switch (ttypetag(key)) {
790 case LUA_VSHRSTR:
return luaH_getshortstr(t, tsvalue(key));
791 case LUA_VNUMINT:
return luaH_getint(t, ivalue(key));
792 case LUA_VNIL:
return &absentkey;
795 if (luaV_flttointeger(fltvalue(key), &k, F2Ieq))
796 return luaH_getint(t, k);
800 return getgeneric(t, key, 0);
814 luaH_newkey(L, t, key, value);
816 setobj2t(L, cast(
TValue *, slot), value);
825 const TValue *slot = luaH_get(t, key);
826 luaH_finishset(L, t, key, slot, value);
831 const TValue *p = luaH_getint(t, key);
835 luaH_newkey(L, t, &k, value);
838 setobj2t(L, cast(
TValue *, p), value);
855static lua_Unsigned hash_search (
Table *t, lua_Unsigned j) {
860 if (j <= l_castS2U(LUA_MAXINTEGER) / 2)
864 if (isempty(luaH_getint(t, j)))
869 }
while (!isempty(luaH_getint(t, j)));
872 lua_Unsigned m = (i + j) / 2;
873 if (isempty(luaH_getint(t, m))) j = m;
880static unsigned int binsearch (
const TValue *array,
unsigned int i,
883 unsigned int m = (i + j) / 2;
884 if (isempty(&array[m - 1])) j = m;
923lua_Unsigned luaH_getn (
Table *t) {
924 unsigned int limit = t->alimit;
925 if (limit > 0 && isempty(&t->array[limit - 1])) {
927 if (limit >= 2 && !isempty(&t->array[limit - 2])) {
929 if (ispow2realasize(t) && !ispow2(limit - 1)) {
930 t->alimit = limit - 1;
936 unsigned int boundary = binsearch(t->array, 0, limit);
938 if (ispow2realasize(t) && boundary > luaH_realasize(t) / 2) {
939 t->alimit = boundary;
946 if (!limitequalsasize(t)) {
948 if (isempty(&t->array[limit]))
951 limit = luaH_realasize(t);
952 if (isempty(&t->array[limit - 1])) {
955 unsigned int boundary = binsearch(t->array, t->alimit, limit);
956 t->alimit = boundary;
962 lua_assert(limit == luaH_realasize(t) &&
963 (limit == 0 || !isempty(&t->array[limit - 1])));
964 if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, limit + 1))))
967 return hash_search(t, limit);
972#if defined(LUA_DEBUG)
977 return mainpositionTV(t, key);