diff options
-rw-r--r-- | ltable.c | 9 |
1 files changed, 6 insertions, 3 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.c,v 2.120 2017/05/16 19:07:08 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 2.121 2017/05/19 12:47:00 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 | */ |
@@ -214,7 +214,8 @@ int luaH_next (lua_State *L, Table *t, StkId key) { | |||
214 | ** "count array" where 'nums[i]' is the number of integers in the table | 214 | ** "count array" where 'nums[i]' is the number of integers in the table |
215 | ** between 2^(i - 1) + 1 and 2^i. 'pna' enters with the total number of | 215 | ** between 2^(i - 1) + 1 and 2^i. 'pna' enters with the total number of |
216 | ** integer keys in the table and leaves with the number of keys that | 216 | ** integer keys in the table and leaves with the number of keys that |
217 | ** will go to the array part; return the optimal size. | 217 | ** will go to the array part; return the optimal size. (The condition |
218 | ** 'twotoi > 0' in the for loop stops the loop if 'twotoi' overflows.) | ||
218 | */ | 219 | */ |
219 | static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { | 220 | static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { |
220 | int i; | 221 | int i; |
@@ -223,7 +224,9 @@ static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { | |||
223 | unsigned int na = 0; /* number of elements to go to array part */ | 224 | unsigned int na = 0; /* number of elements to go to array part */ |
224 | unsigned int optimal = 0; /* optimal size for array part */ | 225 | unsigned int optimal = 0; /* optimal size for array part */ |
225 | /* loop while keys can fill more than half of total size */ | 226 | /* loop while keys can fill more than half of total size */ |
226 | for (i = 0, twotoi = 1; *pna > twotoi / 2; i++, twotoi *= 2) { | 227 | for (i = 0, twotoi = 1; |
228 | twotoi > 0 && *pna > twotoi / 2; | ||
229 | i++, twotoi *= 2) { | ||
227 | a += nums[i]; | 230 | a += nums[i]; |
228 | if (a > twotoi/2) { /* more than half elements present? */ | 231 | if (a > twotoi/2) { /* more than half elements present? */ |
229 | optimal = twotoi; /* optimal size (till now) */ | 232 | optimal = twotoi; /* optimal size (till now) */ |