| 187 | |
| 188 | |
| 189 | static int computesizes (int nums[], int *narray) { |
| 190 | int i; |
| 191 | int twotoi; /* 2^i */ |
| 192 | int a = 0; /* number of elements smaller than 2^i */ |
| 193 | int na = 0; /* number of elements to go to array part */ |
| 194 | int n = 0; /* optimal size for array part */ |
| 195 | for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) { |
| 196 | if (nums[i] > 0) { |
| 197 | a += nums[i]; |
| 198 | if (a > twotoi/2) { /* more than half elements present? */ |
| 199 | n = twotoi; /* optimal size (till now) */ |
| 200 | na = a; /* all elements smaller than n will go to array part */ |
| 201 | } |
| 202 | } |
| 203 | if (a == *narray) break; /* all elements already counted */ |
| 204 | } |
| 205 | *narray = n; |
| 206 | lua_assert(*narray/2 <= na && na <= *narray); |
| 207 | return na; |
| 208 | } |
| 209 | |
| 210 | |
| 211 | static int countint (const TValue *key, int *nums) { |