aboutsummaryrefslogtreecommitdiff
path: root/ltable.c
diff options
context:
space:
mode:
Diffstat (limited to 'ltable.c')
-rw-r--r--ltable.c67
1 files changed, 34 insertions, 33 deletions
diff --git a/ltable.c b/ltable.c
index 1be291c7..c2d957e9 100644
--- a/ltable.c
+++ b/ltable.c
@@ -109,7 +109,7 @@ typedef union {
109** for other types, it is better to avoid modulo by power of 2, as 109** for other types, it is better to avoid modulo by power of 2, as
110** they can have many 2 factors. 110** they can have many 2 factors.
111*/ 111*/
112#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) 112#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1u)|1u))))
113 113
114 114
115#define hashstr(t,str) hashpow2(t, (str)->hash) 115#define hashstr(t,str) hashpow2(t, (str)->hash)
@@ -139,7 +139,7 @@ static const TValue absentkey = {ABSTKEYCONSTANT};
139static Node *hashint (const Table *t, lua_Integer i) { 139static Node *hashint (const Table *t, lua_Integer i) {
140 lua_Unsigned ui = l_castS2U(i); 140 lua_Unsigned ui = l_castS2U(i);
141 if (ui <= cast_uint(INT_MAX)) 141 if (ui <= cast_uint(INT_MAX))
142 return hashmod(t, cast_int(ui)); 142 return gnode(t, cast_int(ui) % cast_int((sizenode(t)-1) | 1));
143 else 143 else
144 return hashmod(t, ui); 144 return hashmod(t, ui);
145} 145}
@@ -159,7 +159,7 @@ static Node *hashint (const Table *t, lua_Integer i) {
159** INT_MIN. 159** INT_MIN.
160*/ 160*/
161#if !defined(l_hashfloat) 161#if !defined(l_hashfloat)
162static int l_hashfloat (lua_Number n) { 162static unsigned l_hashfloat (lua_Number n) {
163 int i; 163 int i;
164 lua_Integer ni; 164 lua_Integer ni;
165 n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN); 165 n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN);
@@ -169,7 +169,7 @@ static int l_hashfloat (lua_Number n) {
169 } 169 }
170 else { /* normal case */ 170 else { /* normal case */
171 unsigned int u = cast_uint(i) + cast_uint(ni); 171 unsigned int u = cast_uint(i) + cast_uint(ni);
172 return cast_int(u <= cast_uint(INT_MAX) ? u : ~u); 172 return (u <= cast_uint(INT_MAX) ? u : ~u);
173 } 173 }
174} 174}
175#endif 175#endif
@@ -370,7 +370,7 @@ static unsigned findindex (lua_State *L, Table *t, TValue *key,
370 const TValue *n = getgeneric(t, key, 1); 370 const TValue *n = getgeneric(t, key, 1);
371 if (l_unlikely(isabstkey(n))) 371 if (l_unlikely(isabstkey(n)))
372 luaG_runerror(L, "invalid key to 'next'"); /* key not found */ 372 luaG_runerror(L, "invalid key to 'next'"); /* key not found */
373 i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ 373 i = cast_uint(nodefromval(n) - gnode(t, 0)); /* key index in hash table */
374 /* hash elements are numbered after array ones */ 374 /* hash elements are numbered after array ones */
375 return (i + 1) + asize; 375 return (i + 1) + asize;
376 } 376 }
@@ -381,14 +381,14 @@ int luaH_next (lua_State *L, Table *t, StkId key) {
381 unsigned int asize = luaH_realasize(t); 381 unsigned int asize = luaH_realasize(t);
382 unsigned int i = findindex(L, t, s2v(key), asize); /* find original key */ 382 unsigned int i = findindex(L, t, s2v(key), asize); /* find original key */
383 for (; i < asize; i++) { /* try first array part */ 383 for (; i < asize; i++) { /* try first array part */
384 int tag = *getArrTag(t, i); 384 lu_byte tag = *getArrTag(t, i);
385 if (!tagisempty(tag)) { /* a non-empty entry? */ 385 if (!tagisempty(tag)) { /* a non-empty entry? */
386 setivalue(s2v(key), i + 1); 386 setivalue(s2v(key), cast_int(i) + 1);
387 farr2val(t, i, tag, s2v(key + 1)); 387 farr2val(t, i, tag, s2v(key + 1));
388 return 1; 388 return 1;
389 } 389 }
390 } 390 }
391 for (i -= asize; cast_int(i) < sizenode(t); i++) { /* hash part */ 391 for (i -= asize; i < sizenode(t); i++) { /* hash part */
392 if (!isempty(gval(gnode(t, i)))) { /* a non-empty entry? */ 392 if (!isempty(gval(gnode(t, i)))) { /* a non-empty entry? */
393 Node *n = gnode(t, i); 393 Node *n = gnode(t, i);
394 getnodekey(L, s2v(key), n); 394 getnodekey(L, s2v(key), n);
@@ -485,7 +485,7 @@ static unsigned computesizes (unsigned nums[], unsigned *pna) {
485} 485}
486 486
487 487
488static int countint (lua_Integer key, unsigned int *nums) { 488static unsigned countint (lua_Integer key, unsigned int *nums) {
489 unsigned int k = arrayindex(key); 489 unsigned int k = arrayindex(key);
490 if (k != 0) { /* is 'key' an appropriate array index? */ 490 if (k != 0) { /* is 'key' an appropriate array index? */
491 nums[luaO_ceillog2(k)]++; /* count as such */ 491 nums[luaO_ceillog2(k)]++; /* count as such */
@@ -496,7 +496,7 @@ static int countint (lua_Integer key, unsigned int *nums) {
496} 496}
497 497
498 498
499l_sinline int arraykeyisempty (const Table *t, lua_Integer key) { 499l_sinline int arraykeyisempty (const Table *t, lua_Unsigned key) {
500 int tag = *getArrTag(t, key - 1); 500 int tag = *getArrTag(t, key - 1);
501 return tagisempty(tag); 501 return tagisempty(tag);
502} 502}
@@ -534,10 +534,10 @@ static unsigned numusearray (const Table *t, unsigned *nums) {
534} 534}
535 535
536 536
537static int numusehash (const Table *t, unsigned *nums, unsigned *pna) { 537static unsigned numusehash (const Table *t, unsigned *nums, unsigned *pna) {
538 int totaluse = 0; /* total number of elements */ 538 unsigned totaluse = 0; /* total number of elements */
539 int ause = 0; /* elements added to 'nums' (can go to array part) */ 539 unsigned ause = 0; /* elements added to 'nums' (can go to array part) */
540 int i = sizenode(t); 540 unsigned i = sizenode(t);
541 while (i--) { 541 while (i--) {
542 Node *n = &t->node[i]; 542 Node *n = &t->node[i];
543 if (!isempty(gval(n))) { 543 if (!isempty(gval(n))) {
@@ -646,8 +646,8 @@ static void setnodevector (lua_State *L, Table *t, unsigned size) {
646** (Re)insert all elements from the hash part of 'ot' into table 't'. 646** (Re)insert all elements from the hash part of 'ot' into table 't'.
647*/ 647*/
648static void reinsert (lua_State *L, Table *ot, Table *t) { 648static void reinsert (lua_State *L, Table *ot, Table *t) {
649 int j; 649 unsigned j;
650 int size = sizenode(ot); 650 unsigned size = sizenode(ot);
651 for (j = 0; j < size; j++) { 651 for (j = 0; j < size; j++) {
652 Node *old = gnode(ot, j); 652 Node *old = gnode(ot, j);
653 if (!isempty(gval(old))) { 653 if (!isempty(gval(old))) {
@@ -673,10 +673,10 @@ static void exchangehashpart (Table *t1, Table *t2) {
673 int bitdummy1 = t1->flags & BITDUMMY; 673 int bitdummy1 = t1->flags & BITDUMMY;
674 t1->lsizenode = t2->lsizenode; 674 t1->lsizenode = t2->lsizenode;
675 t1->node = t2->node; 675 t1->node = t2->node;
676 t1->flags = (t1->flags & NOTBITDUMMY) | (t2->flags & BITDUMMY); 676 t1->flags = cast_byte((t1->flags & NOTBITDUMMY) | (t2->flags & BITDUMMY));
677 t2->lsizenode = lsizenode; 677 t2->lsizenode = lsizenode;
678 t2->node = node; 678 t2->node = node;
679 t2->flags = (t2->flags & NOTBITDUMMY) | bitdummy1; 679 t2->flags = cast_byte((t2->flags & NOTBITDUMMY) | bitdummy1);
680} 680}
681 681
682 682
@@ -689,11 +689,12 @@ static void reinsertOldSlice (lua_State *L, Table *t, unsigned oldasize,
689 unsigned i; 689 unsigned i;
690 t->alimit = newasize; /* pretend array has new size... */ 690 t->alimit = newasize; /* pretend array has new size... */
691 for (i = newasize; i < oldasize; i++) { /* traverse vanishing slice */ 691 for (i = newasize; i < oldasize; i++) { /* traverse vanishing slice */
692 int tag = *getArrTag(t, i); 692 lu_byte tag = *getArrTag(t, i);
693 if (!tagisempty(tag)) { /* a non-empty entry? */ 693 if (!tagisempty(tag)) { /* a non-empty entry? */
694 TValue aux; 694 TValue aux;
695 farr2val(t, i, tag, &aux); /* copy entry into 'aux' */ 695 farr2val(t, i, tag, &aux); /* copy entry into 'aux' */
696 luaH_setint(L, t, i + 1, &aux); /* re-insert it into the table */ 696 /* re-insert it into the table */
697 luaH_setint(L, t, cast_int(i) + 1, &aux);
697 } 698 }
698 } 699 }
699 t->alimit = oldasize; /* restore current size... */ 700 t->alimit = oldasize; /* restore current size... */
@@ -756,7 +757,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned newasize,
756 757
757 758
758void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) { 759void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) {
759 int nsize = allocsizenode(t); 760 unsigned nsize = allocsizenode(t);
760 luaH_resize(L, t, nasize, nsize); 761 luaH_resize(L, t, nasize, nsize);
761} 762}
762 763
@@ -768,7 +769,7 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) {
768 unsigned int na; /* number of keys in the array part */ 769 unsigned int na; /* number of keys in the array part */
769 unsigned int nums[MAXABITS + 1]; 770 unsigned int nums[MAXABITS + 1];
770 int i; 771 int i;
771 int totaluse; 772 unsigned totaluse;
772 for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */ 773 for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */
773 setlimittosize(t); 774 setlimittosize(t);
774 na = numusearray(t, nums); /* count keys in array part */ 775 na = numusearray(t, nums); /* count keys in array part */
@@ -795,7 +796,7 @@ Table *luaH_new (lua_State *L) {
795 GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table)); 796 GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table));
796 Table *t = gco2t(o); 797 Table *t = gco2t(o);
797 t->metatable = NULL; 798 t->metatable = NULL;
798 t->flags = cast_byte(maskflags); /* table has no metamethod fields */ 799 t->flags = maskflags; /* table has no metamethod fields */
799 t->array = NULL; 800 t->array = NULL;
800 t->alimit = 0; 801 t->alimit = 0;
801 setnodevector(L, t, 0); 802 setnodevector(L, t, 0);
@@ -825,7 +826,7 @@ static Node *getfreepos (Table *t) {
825 } 826 }
826 else { /* no 'lastfree' information */ 827 else { /* no 'lastfree' information */
827 if (!isdummy(t)) { 828 if (!isdummy(t)) {
828 int i = sizenode(t); 829 unsigned i = sizenode(t);
829 while (i--) { /* do a linear search */ 830 while (i--) { /* do a linear search */
830 Node *free = gnode(t, i); 831 Node *free = gnode(t, i);
831 if (keyisnil(free)) 832 if (keyisnil(free))
@@ -919,13 +920,13 @@ static const TValue *getintfromhash (Table *t, lua_Integer key) {
919} 920}
920 921
921 922
922static int hashkeyisempty (Table *t, lua_Integer key) { 923static int hashkeyisempty (Table *t, lua_Unsigned key) {
923 const TValue *val = getintfromhash(t, key); 924 const TValue *val = getintfromhash(t, l_castU2S(key));
924 return isempty(val); 925 return isempty(val);
925} 926}
926 927
927 928
928static int finishnodeget (const TValue *val, TValue *res) { 929static lu_byte finishnodeget (const TValue *val, TValue *res) {
929 if (!ttisnil(val)) { 930 if (!ttisnil(val)) {
930 setobj(((lua_State*)NULL), res, val); 931 setobj(((lua_State*)NULL), res, val);
931 } 932 }
@@ -933,9 +934,9 @@ static int finishnodeget (const TValue *val, TValue *res) {
933} 934}
934 935
935 936
936int luaH_getint (Table *t, lua_Integer key, TValue *res) { 937lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res) {
937 if (keyinarray(t, key)) { 938 if (keyinarray(t, key)) {
938 int tag = *getArrTag(t, key - 1); 939 lu_byte tag = *getArrTag(t, key - 1);
939 if (!tagisempty(tag)) 940 if (!tagisempty(tag))
940 farr2val(t, key - 1, tag, res); 941 farr2val(t, key - 1, tag, res);
941 return tag; 942 return tag;
@@ -964,7 +965,7 @@ const TValue *luaH_Hgetshortstr (Table *t, TString *key) {
964} 965}
965 966
966 967
967int luaH_getshortstr (Table *t, TString *key, TValue *res) { 968lu_byte luaH_getshortstr (Table *t, TString *key, TValue *res) {
968 return finishnodeget(luaH_Hgetshortstr(t, key), res); 969 return finishnodeget(luaH_Hgetshortstr(t, key), res);
969} 970}
970 971
@@ -980,7 +981,7 @@ static const TValue *Hgetstr (Table *t, TString *key) {
980} 981}
981 982
982 983
983int luaH_getstr (Table *t, TString *key, TValue *res) { 984lu_byte luaH_getstr (Table *t, TString *key, TValue *res) {
984 return finishnodeget(Hgetstr(t, key), res); 985 return finishnodeget(Hgetstr(t, key), res);
985} 986}
986 987
@@ -997,7 +998,7 @@ TString *luaH_getstrkey (Table *t, TString *key) {
997/* 998/*
998** main search function 999** main search function
999*/ 1000*/
1000int luaH_get (Table *t, const TValue *key, TValue *res) { 1001lu_byte luaH_get (Table *t, const TValue *key, TValue *res) {
1001 const TValue *slot; 1002 const TValue *slot;
1002 switch (ttypetag(key)) { 1003 switch (ttypetag(key)) {
1003 case LUA_VSHRSTR: 1004 case LUA_VSHRSTR:
@@ -1259,7 +1260,7 @@ lua_Unsigned luaH_getn (Table *t) {
1259 /* (3) 'limit' is the last element and either is zero or present in table */ 1260 /* (3) 'limit' is the last element and either is zero or present in table */
1260 lua_assert(limit == luaH_realasize(t) && 1261 lua_assert(limit == luaH_realasize(t) &&
1261 (limit == 0 || !arraykeyisempty(t, limit))); 1262 (limit == 0 || !arraykeyisempty(t, limit)));
1262 if (isdummy(t) || hashkeyisempty(t, cast(lua_Integer, limit + 1))) 1263 if (isdummy(t) || hashkeyisempty(t, limit + 1))
1263 return limit; /* 'limit + 1' is absent */ 1264 return limit; /* 'limit + 1' is absent */
1264 else /* 'limit + 1' is also present */ 1265 else /* 'limit + 1' is also present */
1265 return hash_search(t, limit); 1266 return hash_search(t, limit);