mirror of
https://github.com/lua/lua.git
synced 2025-01-28 06:03:00 +08:00
correct implementation for arrays of size 1
This commit is contained in:
parent
dea6b6da94
commit
a4d06736d4
19
ltable.c
19
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)
|
** Lua tables (hash)
|
||||||
** See Copyright Notice in lua.h
|
** 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) {
|
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 i;
|
||||||
int a = nums[0]; /* number of elements smaller than 2^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++) {
|
for (i = 1; i <= MAXBITS && *narray >= twoto(i-1); i++) {
|
||||||
if (nums[i] == 0) continue;
|
if (nums[i] > 0) {
|
||||||
a += nums[i];
|
a += nums[i];
|
||||||
if (a >= twoto(i-1)) { /* more than half elements in use? */
|
if (a >= twoto(i-1)) { /* more than half elements in use? */
|
||||||
n = i;
|
n = i;
|
||||||
na = a;
|
na = a;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lua_assert(na <= *narray && *narray <= ntotal);
|
lua_assert(na <= *narray && *narray <= ntotal);
|
||||||
*nhash = ntotal - na;
|
*nhash = ntotal - na;
|
||||||
*narray = (n == 0) ? 0 : twoto(n);
|
*narray = (n == -1) ? 0 : twoto(n);
|
||||||
lua_assert(na <= *narray && na >= *narray/2);
|
lua_assert(na <= *narray && na >= *narray/2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user