diff options
| -rw-r--r-- | ltable.c | 19 |
1 files changed, 10 insertions, 9 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltable.c,v 1.104 2002/04/22 14:40:23 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 1.105 2002/04/23 15:04:39 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 | */ |
| @@ -147,21 +147,22 @@ int luaH_next (lua_State *L, Table *t, TObject *key) { | |||
| 147 | 147 | ||
| 148 | 148 | ||
| 149 | static void computesizes (int nums[], int ntotal, int *narray, int *nhash) { | 149 | static void computesizes (int nums[], int ntotal, int *narray, int *nhash) { |
| 150 | int n = 0; /* (log of) optimal size for array part */ | ||
| 151 | int na = 0; /* number of elements to go to array part */ | ||
| 152 | int i; | 150 | int i; |
| 153 | int a = nums[0]; /* number of elements smaller than 2^i */ | 151 | int a = nums[0]; /* number of elements smaller than 2^i */ |
| 152 | int na = a; /* number of elements to go to array part */ | ||
| 153 | int n = (na == 0) ? -1 : 0; /* (log of) optimal size for array part */ | ||
| 154 | for (i = 1; i <= MAXBITS && *narray >= twoto(i-1); i++) { | 154 | for (i = 1; i <= MAXBITS && *narray >= twoto(i-1); i++) { |
| 155 | if (nums[i] == 0) continue; | 155 | if (nums[i] > 0) { |
| 156 | a += nums[i]; | 156 | a += nums[i]; |
| 157 | if (a >= twoto(i-1)) { /* more than half elements in use? */ | 157 | if (a >= twoto(i-1)) { /* more than half elements in use? */ |
| 158 | n = i; | 158 | n = i; |
| 159 | na = a; | 159 | na = a; |
| 160 | } | ||
| 160 | } | 161 | } |
| 161 | } | 162 | } |
| 162 | lua_assert(na <= *narray && *narray <= ntotal); | 163 | lua_assert(na <= *narray && *narray <= ntotal); |
| 163 | *nhash = ntotal - na; | 164 | *nhash = ntotal - na; |
| 164 | *narray = (n == 0) ? 0 : twoto(n); | 165 | *narray = (n == -1) ? 0 : twoto(n); |
| 165 | lua_assert(na <= *narray && na >= *narray/2); | 166 | lua_assert(na <= *narray && na >= *narray/2); |
| 166 | } | 167 | } |
| 167 | 168 | ||
