| 9427 | |
| 9428 | |
| 9429 | static int computesizes (int nums[], int *narray) { |
| 9430 | int i; |
| 9431 | int twotoi; /* 2^i */ |
| 9432 | int a = 0; /* number of elements smaller than 2^i */ |
| 9433 | int na = 0; /* number of elements to go to array part */ |
| 9434 | int n = 0; /* optimal size for array part */ |
| 9435 | for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) { |
| 9436 | if (nums[i] > 0) { |
| 9437 | a += nums[i]; |
| 9438 | if (a > twotoi/2) { /* more than half elements present? */ |
| 9439 | n = twotoi; /* optimal size (till now) */ |
| 9440 | na = a; /* all elements smaller than n will go to array part */ |
| 9441 | } |
| 9442 | } |
| 9443 | if (a == *narray) break; /* all elements already counted */ |
| 9444 | } |
| 9445 | *narray = n; |
| 9446 | lua_assert(*narray/2 <= na && na <= *narray); |
| 9447 | return na; |
| 9448 | } |
| 9449 | |
| 9450 | |
| 9451 | static int countint (const TValue *key, int *nums) { |