** 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. */
| 217 | ** will go to the array part; return the optimal size. |
| 218 | */ |
| 219 | static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { |
| 220 | int i; |
| 221 | unsigned int twotoi; /* 2^i (candidate for optimal size) */ |
| 222 | unsigned int a = 0; /* number of elements smaller than 2^i */ |
| 223 | unsigned int na = 0; /* number of elements to go to array part */ |
| 224 | unsigned int optimal = 0; /* optimal size for array part */ |
| 225 | /* loop while keys can fill more than half of total size */ |
| 226 | for (i = 0, twotoi = 1; |
| 227 | twotoi > 0 && *pna > twotoi / 2; |
| 228 | i++, twotoi *= 2) { |
| 229 | if (nums[i] > 0) { |
| 230 | a += nums[i]; |
| 231 | if (a > twotoi/2) { /* more than half elements present? */ |
| 232 | optimal = twotoi; /* optimal size (till now) */ |
| 233 | na = a; /* all elements up to 'optimal' will go to array part */ |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal); |
| 238 | *pna = na; |
| 239 | return optimal; |
| 240 | } |
| 241 | |
| 242 | |
| 243 | static int countint (const TValue *key, unsigned int *nums) { |