commit a4d06736d4a66e1653599fad3df39099bf5b1276
parent dea6b6da9422f34ad91c8f6ad9ad3ed650e95713
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Wed, 8 May 2002 14:33:38 -0300
correct implementation for arrays of size 1
Diffstat:
1 file changed, 10 insertions(+), 9 deletions(-)
diff --git a/ltable.c b/ltable.c
@@ -1,5 +1,5 @@
/*
-** $Id: ltable.c,v 1.104 2002/04/22 14:40:23 roberto Exp roberto $
+** $Id: ltable.c,v 1.105 2002/04/23 15:04:39 roberto Exp roberto $
** Lua tables (hash)
** See Copyright Notice in lua.h
*/
@@ -147,21 +147,22 @@ int luaH_next (lua_State *L, Table *t, TObject *key) {
static void computesizes (int nums[], int ntotal, int *narray, int *nhash) {
- int n = 0; /* (log of) optimal size for array part */
- int na = 0; /* number of elements to go to array part */
int i;
int a = nums[0]; /* number of elements smaller than 2^i */
+ int na = a; /* number of elements to go to array part */
+ int n = (na == 0) ? -1 : 0; /* (log of) optimal size for array part */
for (i = 1; i <= MAXBITS && *narray >= twoto(i-1); i++) {
- if (nums[i] == 0) continue;
- a += nums[i];
- if (a >= twoto(i-1)) { /* more than half elements in use? */
- n = i;
- na = a;
+ if (nums[i] > 0) {
+ a += nums[i];
+ if (a >= twoto(i-1)) { /* more than half elements in use? */
+ n = i;
+ na = a;
+ }
}
}
lua_assert(na <= *narray && *narray <= ntotal);
*nhash = ntotal - na;
- *narray = (n == 0) ? 0 : twoto(n);
+ *narray = (n == -1) ? 0 : twoto(n);
lua_assert(na <= *narray && na >= *narray/2);
}