** Compute the optimal size for the array part of table 't'. 'nums' is a ** "count array" where 'nums[i]' is the number of integers in the table ** between 2^(i - 1) + 1 and 2^i. 'pna' enters with the total number of ** integer keys in the table and leaves with the number of keys that ** will go to the array part; return the optimal size. (The condition ** 'twotoi > 0' in the for loop stops the l
| 389 | ** 'twotoi > 0' in the for loop stops the loop if 'twotoi' overflows.) |
| 390 | */ |
| 391 | static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { |
| 392 | int i; |
| 393 | unsigned int twotoi; /* 2^i (candidate for optimal size) */ |
| 394 | unsigned int a = 0; /* number of elements smaller than 2^i */ |
| 395 | unsigned int na = 0; /* number of elements to go to array part */ |
| 396 | unsigned int optimal = 0; /* optimal size for array part */ |
| 397 | /* loop while keys can fill more than half of total size */ |
| 398 | for (i = 0, twotoi = 1; |
| 399 | twotoi > 0 && *pna > twotoi / 2; |
| 400 | i++, twotoi *= 2) { |
| 401 | a += nums[i]; |
| 402 | if (a > twotoi/2) { /* more than half elements present? */ |
| 403 | optimal = twotoi; /* optimal size (till now) */ |
| 404 | na = a; /* all elements up to 'optimal' will go to array part */ |
| 405 | } |
| 406 | } |
| 407 | lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal); |
| 408 | *pna = na; |
| 409 | return optimal; |
| 410 | } |
| 411 | |
| 412 | |
| 413 | static int countint (lua_Integer key, unsigned int *nums) { |