reimplementation of 'luaH_getn', trying to handle numeric limits

properly.
This commit is contained in:
Roberto Ierusalimschy
2017-05-16 16:07:08 -03:00
parent 6d95de83c6
commit 3d879fbc5d

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: ltable.c,v 2.118 2016/11/07 12:38:35 roberto Exp roberto $ ** $Id: ltable.c,v 2.119 2017/05/09 14:39:46 roberto Exp roberto $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -224,12 +224,10 @@ static unsigned int computesizes (unsigned int nums[], unsigned int *pna) {
unsigned int optimal = 0; /* optimal size for array part */ unsigned int optimal = 0; /* optimal size for array part */
/* loop while keys can fill more than half of total size */ /* loop while keys can fill more than half of total size */
for (i = 0, twotoi = 1; *pna > twotoi / 2; i++, twotoi *= 2) { for (i = 0, twotoi = 1; *pna > twotoi / 2; i++, twotoi *= 2) {
if (nums[i] > 0) { a += nums[i];
a += nums[i]; if (a > twotoi/2) { /* more than half elements present? */
if (a > twotoi/2) { /* more than half elements present? */ optimal = twotoi; /* optimal size (till now) */
optimal = twotoi; /* optimal size (till now) */ na = a; /* all elements up to 'optimal' will go to array part */
na = a; /* all elements up to 'optimal' will go to array part */
}
} }
} }
lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal); lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal);
@@ -610,23 +608,31 @@ void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) {
} }
static int unbound_search (Table *t, unsigned int j) { /*
unsigned int i = j; /* i is zero or a present index */ ** Try to find a boundary in the hash part of table 't'. From the
j++; ** caller, we know that 'i' is zero or present. We need to find an
/* find 'i' and 'j' such that i is present and j is not */ ** upper bound (an absent index larger than 'i') to do a binary search
while (!ttisnil(luaH_getint(t, j))) { ** for a boundary. We try 'max', a number larger than the total number
i = j; ** of keys in the table. (Given the size of the array elements, 'max'
if (j > cast(unsigned int, MAX_INT)/2) { /* overflow? */ ** computation cannot overflow a 'size_t'.) If 'max' does not fit in a
/* table was built with bad purposes: resort to linear search */ ** lua_Integer or it is present in the table, we try LUA_MAXINTEGER. If
i = 1; ** LUA_MAXINTEGER is present, it is a boundary, so we are done. Otherwise,
while (!ttisnil(luaH_getint(t, i))) i++; ** we are left with a 'j' that is within the size of lua_Integers and
return i - 1; ** absent, so can do the binary search.
} */
j *= 2; static lua_Unsigned hash_search (Table *t, lua_Unsigned i) {
lua_Unsigned j;
size_t max = (cast(size_t, i) + sizenode(t) + 10) * 2;
if (max <= l_castS2U(LUA_MAXINTEGER) && ttisnil(luaH_getint(t, max)))
j = max;
else {
j = LUA_MAXINTEGER;
if (!ttisnil(luaH_getint(t, j))) /* weird case? */
return j; /* well, that is a boundary... */
} }
/* now do a binary search between them */ /* now, 'i' is zero or present and 'j' is absent */
while (j - i > 1) { while (j - i > 1u) { /* do a binary search between them */
unsigned int m = (i+j)/2; size_t m = (i + j) / 2;
if (ttisnil(luaH_getint(t, m))) j = m; if (ttisnil(luaH_getint(t, m))) j = m;
else i = m; else i = m;
} }
@@ -635,25 +641,30 @@ static int unbound_search (Table *t, unsigned int j) {
/* /*
** Try to find a boundary in table 't'. A 'boundary' is an integer index ** Try to find a boundary in table 't'. (A 'boundary' is an integer index
** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil). ** such that t[i] is non-nil and t[i+1] is nil (or 0 if t[1] is nil).)
** First, try the array part: if there is an array part and its last
** element is nil, there must be a boundary there; a binary search
** finds that boundary. Otherwise, if the hash part is empty or does not
** contain 'j + 1', 'j' is a boundary. Othersize, call 'hash_search'
** to find a boundary in the hash part.
*/ */
int luaH_getn (Table *t) { lua_Unsigned luaH_getn (Table *t) {
unsigned int j = t->sizearray; unsigned int j = t->sizearray;
if (j > 0 && ttisnil(&t->array[j - 1])) { if (j > 0 && ttisnil(&t->array[j - 1])) {
/* there is a boundary in the array part: (binary) search for it */
unsigned int i = 0; unsigned int i = 0;
while (j - i > 1) { while (j - i > 1u) { /* binary search */
unsigned int m = (i+j)/2; unsigned int m = (i + j) / 2;
if (ttisnil(&t->array[m - 1])) j = m; if (ttisnil(&t->array[m - 1])) j = m;
else i = m; else i = m;
} }
return i; return i;
} }
/* else must find a boundary in hash part */ /* 'j' is zero or present in table */
else if (isdummy(t)) /* hash part is empty? */ else if (isdummy(t) || ttisnil(luaH_getint(t, l_castU2S(j + 1))))
return j; /* that is easy... */ return j; /* 'j + 1' is absent... */
else return unbound_search(t, j); else
return hash_search(t, j);
} }