From a4d06736d4a66e1653599fad3df39099bf5b1276 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 8 May 2002 14:34:00 -0300 Subject: [PATCH] correct implementation for arrays of size 1 --- ltable.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/ltable.c b/ltable.c index 7071ad0e..64a72f53 100644 --- 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); }