diff options
Diffstat (limited to 'ltable.c')
-rw-r--r-- | ltable.c | 34 |
1 files changed, 19 insertions, 15 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.c,v 2.71 2012/05/23 15:37:09 roberto Exp $ | 2 | ** $Id: ltable.c,v 2.73 2013/04/15 15:44:46 roberto Exp roberto $ |
3 | ** Lua tables (hash) | 3 | ** Lua tables (hash) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -123,9 +123,9 @@ static Node *mainposition (const Table *t, const TValue *key) { | |||
123 | } | 123 | } |
124 | 124 | ||
125 | 125 | ||
126 | static int numisint (lua_Number n, int *p) { | 126 | static int numisint (lua_Number n, lua_Integer *p) { |
127 | int k; | 127 | lua_Integer k; |
128 | lua_number2int(k, n); | 128 | lua_number2integer(k, n); |
129 | if (luai_numeq(cast_num(k), n)) { /* 'k' is int? */ | 129 | if (luai_numeq(cast_num(k), n)) { /* 'k' is int? */ |
130 | *p = k; | 130 | *p = k; |
131 | return 1; | 131 | return 1; |
@@ -139,8 +139,12 @@ static int numisint (lua_Number n, int *p) { | |||
139 | ** the array part of the table, -1 otherwise. | 139 | ** the array part of the table, -1 otherwise. |
140 | */ | 140 | */ |
141 | static int arrayindex (const TValue *key) { | 141 | static int arrayindex (const TValue *key) { |
142 | if (ttisinteger(key)) return ivalue(key); | 142 | if (ttisinteger(key)) { |
143 | else return -1; /* `key' did not match some condition */ | 143 | lua_Integer k = ivalue(key); |
144 | if (0 < k && k <= MAXASIZE) /* is `key' an appropriate array index? */ | ||
145 | return cast_int(k); | ||
146 | } | ||
147 | return -1; /* `key' did not match some condition */ | ||
144 | } | 148 | } |
145 | 149 | ||
146 | 150 | ||
@@ -225,7 +229,7 @@ static int computesizes (int nums[], int *narray) { | |||
225 | 229 | ||
226 | static int countint (const TValue *key, int *nums) { | 230 | static int countint (const TValue *key, int *nums) { |
227 | int k = arrayindex(key); | 231 | int k = arrayindex(key); |
228 | if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ | 232 | if (k > 0) { /* is `key' an appropriate array index? */ |
229 | nums[luaO_ceillog2(k)]++; /* count as such */ | 233 | nums[luaO_ceillog2(k)]++; /* count as such */ |
230 | return 1; | 234 | return 1; |
231 | } | 235 | } |
@@ -416,7 +420,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { | |||
416 | if (ttisnil(key)) luaG_runerror(L, "table index is nil"); | 420 | if (ttisnil(key)) luaG_runerror(L, "table index is nil"); |
417 | else if (ttisfloat(key)) { | 421 | else if (ttisfloat(key)) { |
418 | lua_Number n = fltvalue(key); | 422 | lua_Number n = fltvalue(key); |
419 | int k; | 423 | lua_Integer k; |
420 | if (luai_numisnan(L, n)) | 424 | if (luai_numisnan(L, n)) |
421 | luaG_runerror(L, "table index is NaN"); | 425 | luaG_runerror(L, "table index is NaN"); |
422 | if (numisint(n, &k)) { /* index is int? */ | 426 | if (numisint(n, &k)) { /* index is int? */ |
@@ -460,10 +464,10 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { | |||
460 | /* | 464 | /* |
461 | ** search function for integers | 465 | ** search function for integers |
462 | */ | 466 | */ |
463 | const TValue *luaH_getint (Table *t, int key) { | 467 | const TValue *luaH_getint (Table *t, lua_Integer key) { |
464 | /* (1 <= key && key <= t->sizearray) */ | 468 | /* (1 <= key && key <= t->sizearray) */ |
465 | if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray)) | 469 | if (cast_unsigned(key - 1) < cast_unsigned(t->sizearray)) |
466 | return &t->array[key-1]; | 470 | return &t->array[key - 1]; |
467 | else { | 471 | else { |
468 | Node *n = hashint(t, key); | 472 | Node *n = hashint(t, key); |
469 | do { /* check whether `key' is somewhere in the chain */ | 473 | do { /* check whether `key' is somewhere in the chain */ |
@@ -500,7 +504,7 @@ const TValue *luaH_get (Table *t, const TValue *key) { | |||
500 | case LUA_TNUMINT: return luaH_getint(t, ivalue(key)); | 504 | case LUA_TNUMINT: return luaH_getint(t, ivalue(key)); |
501 | case LUA_TNIL: return luaO_nilobject; | 505 | case LUA_TNIL: return luaO_nilobject; |
502 | case LUA_TNUMFLT: { | 506 | case LUA_TNUMFLT: { |
503 | int k; | 507 | lua_Integer k; |
504 | if (numisint(fltvalue(key), &k)) /* index is int? */ | 508 | if (numisint(fltvalue(key), &k)) /* index is int? */ |
505 | return luaH_getint(t, k); /* use specialized version */ | 509 | return luaH_getint(t, k); /* use specialized version */ |
506 | /* else go through */ | 510 | /* else go through */ |
@@ -530,7 +534,7 @@ TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { | |||
530 | } | 534 | } |
531 | 535 | ||
532 | 536 | ||
533 | void luaH_setint (lua_State *L, Table *t, int key, TValue *value) { | 537 | void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { |
534 | const TValue *p = luaH_getint(t, key); | 538 | const TValue *p = luaH_getint(t, key); |
535 | TValue *cell; | 539 | TValue *cell; |
536 | if (p != luaO_nilobject) | 540 | if (p != luaO_nilobject) |
@@ -550,13 +554,13 @@ static int unbound_search (Table *t, unsigned int j) { | |||
550 | /* find `i' and `j' such that i is present and j is not */ | 554 | /* find `i' and `j' such that i is present and j is not */ |
551 | while (!ttisnil(luaH_getint(t, j))) { | 555 | while (!ttisnil(luaH_getint(t, j))) { |
552 | i = j; | 556 | i = j; |
553 | j *= 2; | 557 | if (j > cast(unsigned int, MAX_INT)/2) { /* overflow? */ |
554 | if (j > cast(unsigned int, MAX_INT)) { /* overflow? */ | ||
555 | /* table was built with bad purposes: resort to linear search */ | 558 | /* table was built with bad purposes: resort to linear search */ |
556 | i = 1; | 559 | i = 1; |
557 | while (!ttisnil(luaH_getint(t, i))) i++; | 560 | while (!ttisnil(luaH_getint(t, i))) i++; |
558 | return i - 1; | 561 | return i - 1; |
559 | } | 562 | } |
563 | j *= 2; | ||
560 | } | 564 | } |
561 | /* now do a binary search between them */ | 565 | /* now do a binary search between them */ |
562 | while (j - i > 1) { | 566 | while (j - i > 1) { |