aboutsummaryrefslogtreecommitdiff
path: root/ltable.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-05-08 14:34:00 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-05-08 14:34:00 -0300
commita4d06736d4a66e1653599fad3df39099bf5b1276 (patch)
tree8ec8b7ee1ff1c3af6c441c08a8cdf05c197b127e /ltable.c
parentdea6b6da9422f34ad91c8f6ad9ad3ed650e95713 (diff)
downloadlua-a4d06736d4a66e1653599fad3df39099bf5b1276.tar.gz
lua-a4d06736d4a66e1653599fad3df39099bf5b1276.tar.bz2
lua-a4d06736d4a66e1653599fad3df39099bf5b1276.zip
correct implementation for arrays of size 1
Diffstat (limited to 'ltable.c')
-rw-r--r--ltable.c19
1 files 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 @@
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
149static void computesizes (int nums[], int ntotal, int *narray, int *nhash) { 149static 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