diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-01-04 13:55:12 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-01-04 13:55:12 -0200 |
commit | 65726f3e2e226f6a350a5dba643c13c8edd34965 (patch) | |
tree | a65e8ac39cb13cbafdfc6870ffaea248e96b25ba | |
parent | 9fe2d0266ac3ba30d432ca8edf6116beb94fe2af (diff) | |
download | lua-65726f3e2e226f6a350a5dba643c13c8edd34965.tar.gz lua-65726f3e2e226f6a350a5dba643c13c8edd34965.tar.bz2 lua-65726f3e2e226f6a350a5dba643c13c8edd34965.zip |
cleaner interface to `ltable'
-rw-r--r-- | lapi.c | 4 | ||||
-rw-r--r-- | lparser.c | 4 | ||||
-rw-r--r-- | lstate.c | 6 | ||||
-rw-r--r-- | ltable.c | 50 | ||||
-rw-r--r-- | ltable.h | 4 | ||||
-rw-r--r-- | lvm.c | 8 |
6 files changed, 44 insertions, 32 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 2.22 2004/12/06 17:53:42 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.23 2004/12/13 12:15:11 roberto Exp $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -542,7 +542,7 @@ LUA_API void lua_rawgeti (lua_State *L, int idx, int n) { | |||
542 | LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { | 542 | LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { |
543 | lua_lock(L); | 543 | lua_lock(L); |
544 | luaC_checkGC(L); | 544 | luaC_checkGC(L); |
545 | sethvalue(L, L->top, luaH_new(L, narray, luaO_log2(nrec) + 1)); | 545 | sethvalue(L, L->top, luaH_new(L, narray, nrec)); |
546 | api_incr_top(L); | 546 | api_incr_top(L); |
547 | lua_unlock(L); | 547 | lua_unlock(L); |
548 | } | 548 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lparser.c,v 2.10 2004/12/03 20:50:25 roberto Exp roberto $ | 2 | ** $Id: lparser.c,v 2.11 2004/12/07 18:31:16 roberto Exp $ |
3 | ** Lua Parser | 3 | ** Lua Parser |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -547,7 +547,7 @@ static void constructor (LexState *ls, expdesc *t) { | |||
547 | check_match(ls, '}', '{', line); | 547 | check_match(ls, '}', '{', line); |
548 | lastlistfield(fs, &cc); | 548 | lastlistfield(fs, &cc); |
549 | SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ | 549 | SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ |
550 | SETARG_C(fs->f->code[pc], luaO_log2(cc.nh)+1); /* set initial table size */ | 550 | SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh+1)); /* set initial table size */ |
551 | } | 551 | } |
552 | 552 | ||
553 | /* }====================================================================== */ | 553 | /* }====================================================================== */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstate.c,v 2.18 2004/12/06 17:53:42 roberto Exp roberto $ | 2 | ** $Id: lstate.c,v 2.19 2004/12/13 12:15:11 roberto Exp $ |
3 | ** Global State | 3 | ** Global State |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -87,9 +87,9 @@ static void f_luaopen (lua_State *L, void *ud) { | |||
87 | setbit(u->uv.marked, FIXEDBIT); | 87 | setbit(u->uv.marked, FIXEDBIT); |
88 | setbit(L->marked, FIXEDBIT); | 88 | setbit(L->marked, FIXEDBIT); |
89 | stack_init(L, L); /* init stack */ | 89 | stack_init(L, L); /* init stack */ |
90 | sethvalue(L, gt(L), luaH_new(L, 0, 4)); /* table of globals */ | 90 | sethvalue(L, gt(L), luaH_new(L, 0, 20)); /* table of globals */ |
91 | hvalue(gt(L))->metatable = luaH_new(L, 0, 0); /* globals metatable */ | 91 | hvalue(gt(L))->metatable = luaH_new(L, 0, 0); /* globals metatable */ |
92 | sethvalue(L, registry(L), luaH_new(L, 4, 4)); /* registry */ | 92 | sethvalue(L, registry(L), luaH_new(L, 6, 20)); /* registry */ |
93 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ | 93 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ |
94 | luaT_init(L); | 94 | luaT_init(L); |
95 | luaX_init(L); | 95 | luaX_init(L); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.c,v 2.11 2004/12/03 20:50:25 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 2.12 2004/12/04 18:10:22 roberto Exp $ |
3 | ** Lua tables (hash) | 3 | ** Lua tables (hash) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -197,7 +197,18 @@ static void computesizes (int nums[], int ntotal, int *narray, int *nhash) { | |||
197 | } | 197 | } |
198 | 198 | ||
199 | 199 | ||
200 | static void numuse (const Table *t, int *narray, int *nhash) { | 200 | static int countint (const TValue *key, int *nums) { |
201 | int k = arrayindex(key); | ||
202 | if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ | ||
203 | nums[luaO_log2(k-1)+1]++; /* count as such */ | ||
204 | return 1; | ||
205 | } | ||
206 | else | ||
207 | return 0; | ||
208 | } | ||
209 | |||
210 | |||
211 | static void numuse (const Table *t, int *narray, int *nhash, const TValue *ek) { | ||
201 | int nums[MAXBITS+1]; | 212 | int nums[MAXBITS+1]; |
202 | int i, lg; | 213 | int i, lg; |
203 | int totaluse = 0; | 214 | int totaluse = 0; |
@@ -223,14 +234,13 @@ static void numuse (const Table *t, int *narray, int *nhash) { | |||
223 | while (i--) { | 234 | while (i--) { |
224 | Node *n = &t->node[i]; | 235 | Node *n = &t->node[i]; |
225 | if (!ttisnil(gval(n))) { | 236 | if (!ttisnil(gval(n))) { |
226 | int k = arrayindex(key2tval(n)); | 237 | *narray += countint(key2tval(n), nums); |
227 | if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ | ||
228 | nums[luaO_log2(k-1)+1]++; /* count as such */ | ||
229 | (*narray)++; | ||
230 | } | ||
231 | totaluse++; | 238 | totaluse++; |
232 | } | 239 | } |
233 | } | 240 | } |
241 | /* count extra key */ | ||
242 | *narray += countint(ek, nums); | ||
243 | totaluse++; | ||
234 | computesizes(nums, totaluse, narray, nhash); | 244 | computesizes(nums, totaluse, narray, nhash); |
235 | } | 245 | } |
236 | 246 | ||
@@ -268,7 +278,7 @@ static void setnodevector (lua_State *L, Table *t, int lsize) { | |||
268 | } | 278 | } |
269 | 279 | ||
270 | 280 | ||
271 | void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) { | 281 | static void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) { |
272 | int i; | 282 | int i; |
273 | int oldasize = t->sizearray; | 283 | int oldasize = t->sizearray; |
274 | int oldhsize = t->lsizenode; | 284 | int oldhsize = t->lsizenode; |
@@ -310,9 +320,16 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) { | |||
310 | } | 320 | } |
311 | 321 | ||
312 | 322 | ||
313 | static void rehash (lua_State *L, Table *t) { | 323 | void luaH_resizearray (lua_State *L, Table *t, int nasize) { |
324 | luaH_resize(L, t, nasize, t->lsizenode); | ||
325 | } | ||
326 | |||
327 | |||
328 | static void rehash (lua_State *L, Table *t, const TValue *ek) { | ||
314 | int nasize, nhsize; | 329 | int nasize, nhsize; |
315 | numuse(t, &nasize, &nhsize); /* compute new sizes for array and hash parts */ | 330 | /* compute new sizes for array and hash parts */ |
331 | numuse(t, &nasize, &nhsize, ek); | ||
332 | /* resize the table to new computed sizes */ | ||
316 | luaH_resize(L, t, nasize, luaO_log2(nhsize)+1); | 333 | luaH_resize(L, t, nasize, luaO_log2(nhsize)+1); |
317 | } | 334 | } |
318 | 335 | ||
@@ -323,7 +340,7 @@ static void rehash (lua_State *L, Table *t) { | |||
323 | */ | 340 | */ |
324 | 341 | ||
325 | 342 | ||
326 | Table *luaH_new (lua_State *L, int narray, int lnhash) { | 343 | Table *luaH_new (lua_State *L, int narray, int nhash) { |
327 | Table *t = luaM_new(L, Table); | 344 | Table *t = luaM_new(L, Table); |
328 | luaC_link(L, obj2gco(t), LUA_TTABLE); | 345 | luaC_link(L, obj2gco(t), LUA_TTABLE); |
329 | t->metatable = NULL; | 346 | t->metatable = NULL; |
@@ -334,7 +351,7 @@ Table *luaH_new (lua_State *L, int narray, int lnhash) { | |||
334 | t->lsizenode = 0; | 351 | t->lsizenode = 0; |
335 | t->node = NULL; | 352 | t->node = NULL; |
336 | setarrayvector(L, t, narray); | 353 | setarrayvector(L, t, narray); |
337 | setnodevector(L, t, lnhash); | 354 | setnodevector(L, t, luaO_log2(nhash)+1); |
338 | return t; | 355 | return t; |
339 | } | 356 | } |
340 | 357 | ||
@@ -356,7 +373,6 @@ void luaH_free (lua_State *L, Table *t) { | |||
356 | ** position), new key goes to an empty position. | 373 | ** position), new key goes to an empty position. |
357 | */ | 374 | */ |
358 | static TValue *newkey (lua_State *L, Table *t, const TValue *key) { | 375 | static TValue *newkey (lua_State *L, Table *t, const TValue *key) { |
359 | TValue *val; | ||
360 | Node *mp = luaH_mainposition(t, key); | 376 | Node *mp = luaH_mainposition(t, key); |
361 | if (!ttisnil(gval(mp))) { /* main position is not free? */ | 377 | if (!ttisnil(gval(mp))) { /* main position is not free? */ |
362 | /* `mp' of colliding node */ | 378 | /* `mp' of colliding node */ |
@@ -387,12 +403,8 @@ static TValue *newkey (lua_State *L, Table *t, const TValue *key) { | |||
387 | else (t->firstfree)--; | 403 | else (t->firstfree)--; |
388 | } | 404 | } |
389 | /* no more free places; must create one */ | 405 | /* no more free places; must create one */ |
390 | setbvalue(gval(mp), 0); /* avoid new key being removed */ | 406 | rehash(L, t, key); /* grow table */ |
391 | rehash(L, t); /* grow table */ | 407 | return luaH_set(L, t, key); /* re-insert in new table */ |
392 | val = cast(TValue *, luaH_get(t, key)); /* get new position */ | ||
393 | lua_assert(ttisboolean(val)); | ||
394 | setnilvalue(val); | ||
395 | return val; | ||
396 | } | 408 | } |
397 | 409 | ||
398 | 410 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.h,v 2.2 2004/03/26 14:02:41 roberto Exp roberto $ | 2 | ** $Id: ltable.h,v 2.3 2004/10/06 18:34:16 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 | */ |
@@ -25,7 +25,7 @@ TValue *luaH_setstr (lua_State *L, Table *t, TString *key); | |||
25 | const TValue *luaH_get (Table *t, const TValue *key); | 25 | const TValue *luaH_get (Table *t, const TValue *key); |
26 | TValue *luaH_set (lua_State *L, Table *t, const TValue *key); | 26 | TValue *luaH_set (lua_State *L, Table *t, const TValue *key); |
27 | Table *luaH_new (lua_State *L, int narray, int lnhash); | 27 | Table *luaH_new (lua_State *L, int narray, int lnhash); |
28 | void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize); | 28 | void luaH_resizearray (lua_State *L, Table *t, int nasize); |
29 | void luaH_free (lua_State *L, Table *t); | 29 | void luaH_free (lua_State *L, Table *t); |
30 | int luaH_next (lua_State *L, Table *t, StkId key); | 30 | int luaH_next (lua_State *L, Table *t, StkId key); |
31 | 31 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 2.17 2004/11/01 15:06:50 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.18 2004/12/03 20:35:33 roberto Exp $ |
3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -461,8 +461,8 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { | |||
461 | } | 461 | } |
462 | case OP_NEWTABLE: { | 462 | case OP_NEWTABLE: { |
463 | int b = GETARG_B(i); | 463 | int b = GETARG_B(i); |
464 | b = luaO_fb2int(b); | 464 | int c = GETARG_C(i); |
465 | sethvalue(L, ra, luaH_new(L, b, GETARG_C(i))); | 465 | sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c) - 1)); |
466 | L->ci->savedpc = pc; | 466 | L->ci->savedpc = pc; |
467 | luaC_checkGC(L); /***/ | 467 | luaC_checkGC(L); /***/ |
468 | base = L->base; | 468 | base = L->base; |
@@ -723,7 +723,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { | |||
723 | if (c == 0) c = cast(int, *pc++); | 723 | if (c == 0) c = cast(int, *pc++); |
724 | last = ((c-1)*LFIELDS_PER_FLUSH) + n + LUA_FIRSTINDEX - 1; | 724 | last = ((c-1)*LFIELDS_PER_FLUSH) + n + LUA_FIRSTINDEX - 1; |
725 | if (last > h->sizearray) /* needs more space? */ | 725 | if (last > h->sizearray) /* needs more space? */ |
726 | luaH_resize(L, h, last, h->lsizenode); /* pre-alloc it at once */ | 726 | luaH_resizearray(L, h, last); /* pre-alloc it at once */ |
727 | for (; n > 0; n--) { | 727 | for (; n > 0; n--) { |
728 | TValue *val = ra+n; | 728 | TValue *val = ra+n; |
729 | setobj2t(L, luaH_setnum(L, h, last--), val); | 729 | setobj2t(L, luaH_setnum(L, h, last--), val); |