diff options
-rw-r--r-- | ltable.c | 18 |
1 files changed, 10 insertions, 8 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.c,v 2.43 2009/11/05 17:43:54 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 2.44 2009/11/06 17:07:12 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 | */ |
@@ -71,6 +71,8 @@ | |||
71 | 71 | ||
72 | #define dummynode (&dummynode_) | 72 | #define dummynode (&dummynode_) |
73 | 73 | ||
74 | #define isdummy(n) ((n) == dummynode) | ||
75 | |||
74 | static const Node dummynode_ = { | 76 | static const Node dummynode_ = { |
75 | {NILCONSTANT}, /* value */ | 77 | {NILCONSTANT}, /* value */ |
76 | {{NILCONSTANT, NULL}} /* key */ | 78 | {{NILCONSTANT, NULL}} /* key */ |
@@ -318,13 +320,13 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) { | |||
318 | if (!ttisnil(gval(old))) | 320 | if (!ttisnil(gval(old))) |
319 | setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old)); | 321 | setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old)); |
320 | } | 322 | } |
321 | if (nold != dummynode) | 323 | if (!isdummy(nold)) |
322 | luaM_freearray(L, nold, twoto(oldhsize)); /* free old array */ | 324 | luaM_freearray(L, nold, twoto(oldhsize)); /* free old array */ |
323 | } | 325 | } |
324 | 326 | ||
325 | 327 | ||
326 | void luaH_resizearray (lua_State *L, Table *t, int nasize) { | 328 | void luaH_resizearray (lua_State *L, Table *t, int nasize) { |
327 | int nsize = (t->node == dummynode) ? 0 : sizenode(t); | 329 | int nsize = isdummy(t->node) ? 0 : sizenode(t); |
328 | luaH_resize(L, t, nasize, nsize); | 330 | luaH_resize(L, t, nasize, nsize); |
329 | } | 331 | } |
330 | 332 | ||
@@ -367,7 +369,7 @@ Table *luaH_new (lua_State *L) { | |||
367 | 369 | ||
368 | 370 | ||
369 | void luaH_free (lua_State *L, Table *t) { | 371 | void luaH_free (lua_State *L, Table *t) { |
370 | if (t->node != dummynode) | 372 | if (!isdummy(t->node)) |
371 | luaM_freearray(L, t->node, sizenode(t)); | 373 | luaM_freearray(L, t->node, sizenode(t)); |
372 | luaM_freearray(L, t->array, t->sizearray); | 374 | luaM_freearray(L, t->array, t->sizearray); |
373 | luaM_free(L, t); | 375 | luaM_free(L, t); |
@@ -394,14 +396,14 @@ static Node *getfreepos (Table *t) { | |||
394 | */ | 396 | */ |
395 | static TValue *newkey (lua_State *L, Table *t, const TValue *key) { | 397 | static TValue *newkey (lua_State *L, Table *t, const TValue *key) { |
396 | Node *mp = mainposition(t, key); | 398 | Node *mp = mainposition(t, key); |
397 | if (!ttisnil(gval(mp)) || mp == dummynode) { | 399 | if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */ |
398 | Node *othern; | 400 | Node *othern; |
399 | Node *n = getfreepos(t); /* get a free place */ | 401 | Node *n = getfreepos(t); /* get a free place */ |
400 | if (n == NULL) { /* cannot find a free place? */ | 402 | if (n == NULL) { /* cannot find a free place? */ |
401 | rehash(L, t, key); /* grow table */ | 403 | rehash(L, t, key); /* grow table */ |
402 | return luaH_set(L, t, key); /* re-insert key into grown table */ | 404 | return luaH_set(L, t, key); /* re-insert key into grown table */ |
403 | } | 405 | } |
404 | lua_assert(n != dummynode); | 406 | lua_assert(!isdummy(n)); |
405 | othern = mainposition(t, key2tval(mp)); | 407 | othern = mainposition(t, key2tval(mp)); |
406 | if (othern != mp) { /* is colliding node out of its main position? */ | 408 | if (othern != mp) { /* is colliding node out of its main position? */ |
407 | /* yes; move colliding node into free position */ | 409 | /* yes; move colliding node into free position */ |
@@ -566,7 +568,7 @@ int luaH_getn (Table *t) { | |||
566 | return i; | 568 | return i; |
567 | } | 569 | } |
568 | /* else must find a boundary in hash part */ | 570 | /* else must find a boundary in hash part */ |
569 | else if (t->node == dummynode) /* hash part is empty? */ | 571 | else if (isdummy(t->node)) /* hash part is empty? */ |
570 | return j; /* that is easy... */ | 572 | return j; /* that is easy... */ |
571 | else return unbound_search(t, j); | 573 | else return unbound_search(t, j); |
572 | } | 574 | } |
@@ -579,6 +581,6 @@ Node *luaH_mainposition (const Table *t, const TValue *key) { | |||
579 | return mainposition(t, key); | 581 | return mainposition(t, key); |
580 | } | 582 | } |
581 | 583 | ||
582 | int luaH_isdummy (Node *n) { return n == dummynode; } | 584 | int luaH_isdummy (Node *n) { return isdummy(n); } |
583 | 585 | ||
584 | #endif | 586 | #endif |