diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-08-05 11:50:39 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-08-05 11:50:39 -0300 |
| commit | 5037196f6fddb828056578b1c0d352cdef672d6a (patch) | |
| tree | d1be52a5ad8fd5cebf30f7023a9c66b7bdc9563f /ltable.c | |
| parent | 9fb80bde3c80557f8ad2d5642bcbeac343999994 (diff) | |
| download | lua-5037196f6fddb828056578b1c0d352cdef672d6a.tar.gz lua-5037196f6fddb828056578b1c0d352cdef672d6a.tar.bz2 lua-5037196f6fddb828056578b1c0d352cdef672d6a.zip | |
new macros `ttis*'
Diffstat (limited to 'ltable.c')
| -rw-r--r-- | ltable.c | 42 |
1 files changed, 21 insertions, 21 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltable.c,v 1.113 2002/07/02 17:54:23 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 1.114 2002/07/17 16:25:13 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 | */ |
| @@ -99,7 +99,7 @@ Node *luaH_mainposition (const Table *t, const TObject *key) { | |||
| 99 | ** the array part of the table, -1 otherwise. | 99 | ** the array part of the table, -1 otherwise. |
| 100 | */ | 100 | */ |
| 101 | static int arrayindex (const TObject *key) { | 101 | static int arrayindex (const TObject *key) { |
| 102 | if (ttype(key) == LUA_TNUMBER) { | 102 | if (ttisnumber(key)) { |
| 103 | int k; | 103 | int k; |
| 104 | lua_number2int(k, (nvalue(key))); | 104 | lua_number2int(k, (nvalue(key))); |
| 105 | if (cast(lua_Number, k) == nvalue(key) && k >= 1 && !toobig(k)) | 105 | if (cast(lua_Number, k) == nvalue(key) && k >= 1 && !toobig(k)) |
| @@ -116,7 +116,7 @@ static int arrayindex (const TObject *key) { | |||
| 116 | */ | 116 | */ |
| 117 | static int luaH_index (lua_State *L, Table *t, const TObject *key) { | 117 | static int luaH_index (lua_State *L, Table *t, const TObject *key) { |
| 118 | int i; | 118 | int i; |
| 119 | if (ttype(key) == LUA_TNIL) return -1; /* first iteration */ | 119 | if (ttisnil(key)) return -1; /* first iteration */ |
| 120 | i = arrayindex(key); | 120 | i = arrayindex(key); |
| 121 | if (0 <= i && i <= t->sizearray) { /* is `key' inside array part? */ | 121 | if (0 <= i && i <= t->sizearray) { /* is `key' inside array part? */ |
| 122 | return i-1; /* yes; that's the index (corrected to C) */ | 122 | return i-1; /* yes; that's the index (corrected to C) */ |
| @@ -135,14 +135,14 @@ static int luaH_index (lua_State *L, Table *t, const TObject *key) { | |||
| 135 | int luaH_next (lua_State *L, Table *t, TObject *key) { | 135 | int luaH_next (lua_State *L, Table *t, TObject *key) { |
| 136 | int i = luaH_index(L, t, key); /* find original element */ | 136 | int i = luaH_index(L, t, key); /* find original element */ |
| 137 | for (i++; i < t->sizearray; i++) { /* try first array part */ | 137 | for (i++; i < t->sizearray; i++) { /* try first array part */ |
| 138 | if (ttype(&t->array[i]) != LUA_TNIL) { /* a non-nil value? */ | 138 | if (!ttisnil(&t->array[i])) { /* a non-nil value? */ |
| 139 | setnvalue(key, i+1); | 139 | setnvalue(key, i+1); |
| 140 | setobj(key+1, &t->array[i]); | 140 | setobj(key+1, &t->array[i]); |
| 141 | return 1; | 141 | return 1; |
| 142 | } | 142 | } |
| 143 | } | 143 | } |
| 144 | for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */ | 144 | for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */ |
| 145 | if (ttype(val(node(t, i))) != LUA_TNIL) { /* a non-nil value? */ | 145 | if (!ttisnil(val(node(t, i)))) { /* a non-nil value? */ |
| 146 | setobj(key, key(node(t, i))); | 146 | setobj(key, key(node(t, i))); |
| 147 | setobj(key+1, val(node(t, i))); | 147 | setobj(key+1, val(node(t, i))); |
| 148 | return 1; | 148 | return 1; |
| @@ -192,7 +192,7 @@ static void numuse (const Table *t, int *narray, int *nhash) { | |||
| 192 | int from = to/2; | 192 | int from = to/2; |
| 193 | if (to > t->sizearray) to = t->sizearray; | 193 | if (to > t->sizearray) to = t->sizearray; |
| 194 | for (; from < to; from++) | 194 | for (; from < to; from++) |
| 195 | if (ttype(&t->array[from]) != LUA_TNIL) { | 195 | if (!ttisnil(&t->array[from])) { |
| 196 | nums[i]++; | 196 | nums[i]++; |
| 197 | totaluse++; | 197 | totaluse++; |
| 198 | } | 198 | } |
| @@ -201,7 +201,7 @@ static void numuse (const Table *t, int *narray, int *nhash) { | |||
| 201 | /* count elements in hash part */ | 201 | /* count elements in hash part */ |
| 202 | i = sizenode(t); | 202 | i = sizenode(t); |
| 203 | while (i--) { | 203 | while (i--) { |
| 204 | if (ttype(val(&t->node[i])) != LUA_TNIL) { | 204 | if (!ttisnil(val(&t->node[i]))) { |
| 205 | int k = arrayindex(key(&t->node[i])); | 205 | int k = arrayindex(key(&t->node[i])); |
| 206 | if (k >= 0) { /* is `key' an appropriate array index? */ | 206 | if (k >= 0) { /* is `key' an appropriate array index? */ |
| 207 | nums[luaO_log2(k-1)+1]++; /* count as such */ | 207 | nums[luaO_log2(k-1)+1]++; /* count as such */ |
| @@ -230,8 +230,8 @@ static void setnodevector (lua_State *L, Table *t, int lsize) { | |||
| 230 | luaG_runerror(L, "table overflow"); | 230 | luaG_runerror(L, "table overflow"); |
| 231 | if (lsize == 0) { /* no elements to hash part? */ | 231 | if (lsize == 0) { /* no elements to hash part? */ |
| 232 | t->node = G(L)->dummynode; /* use common `dummynode' */ | 232 | t->node = G(L)->dummynode; /* use common `dummynode' */ |
| 233 | lua_assert(ttype(key(t->node)) == LUA_TNIL); /* assert invariants: */ | 233 | lua_assert(ttisnil(key(t->node))); /* assert invariants: */ |
| 234 | lua_assert(ttype(val(t->node)) == LUA_TNIL); | 234 | lua_assert(ttisnil(val(t->node))); |
| 235 | lua_assert(t->node->next == NULL); /* (`dummynode' must be empty) */ | 235 | lua_assert(t->node->next == NULL); /* (`dummynode' must be empty) */ |
| 236 | } | 236 | } |
| 237 | else { | 237 | else { |
| @@ -272,7 +272,7 @@ static void resize (lua_State *L, Table *t, int nasize, int nhsize) { | |||
| 272 | t->sizearray = nasize; | 272 | t->sizearray = nasize; |
| 273 | /* re-insert elements from vanishing slice */ | 273 | /* re-insert elements from vanishing slice */ |
| 274 | for (i=nasize; i<oldasize; i++) { | 274 | for (i=nasize; i<oldasize; i++) { |
| 275 | if (ttype(&t->array[i]) != LUA_TNIL) | 275 | if (!ttisnil(&t->array[i])) |
| 276 | setobj(luaH_setnum(L, t, i+1), &t->array[i]); | 276 | setobj(luaH_setnum(L, t, i+1), &t->array[i]); |
| 277 | } | 277 | } |
| 278 | /* shrink array */ | 278 | /* shrink array */ |
| @@ -281,7 +281,7 @@ static void resize (lua_State *L, Table *t, int nasize, int nhsize) { | |||
| 281 | /* re-insert elements in hash part */ | 281 | /* re-insert elements in hash part */ |
| 282 | for (i = twoto(oldhsize) - 1; i >= 0; i--) { | 282 | for (i = twoto(oldhsize) - 1; i >= 0; i--) { |
| 283 | Node *old = nold+i; | 283 | Node *old = nold+i; |
| 284 | if (ttype(val(old)) != LUA_TNIL) | 284 | if (!ttisnil(val(old))) |
| 285 | setobj(luaH_set(L, t, key(old)), val(old)); | 285 | setobj(luaH_set(L, t, key(old)), val(old)); |
| 286 | } | 286 | } |
| 287 | if (oldhsize) | 287 | if (oldhsize) |
| @@ -342,7 +342,7 @@ void luaH_remove (Table *t, Node *e) { | |||
| 342 | else { | 342 | else { |
| 343 | if (e->next != NULL) ?? | 343 | if (e->next != NULL) ?? |
| 344 | } | 344 | } |
| 345 | lua_assert(ttype(val(node)) == LUA_TNIL); | 345 | lua_assert(ttisnil(val(node))); |
| 346 | setnilvalue(key(e)); /* clear node `e' */ | 346 | setnilvalue(key(e)); /* clear node `e' */ |
| 347 | e->next = NULL; | 347 | e->next = NULL; |
| 348 | } | 348 | } |
| @@ -359,7 +359,7 @@ void luaH_remove (Table *t, Node *e) { | |||
| 359 | static TObject *newkey (lua_State *L, Table *t, const TObject *key) { | 359 | static TObject *newkey (lua_State *L, Table *t, const TObject *key) { |
| 360 | TObject *val; | 360 | TObject *val; |
| 361 | Node *mp = luaH_mainposition(t, key); | 361 | Node *mp = luaH_mainposition(t, key); |
| 362 | if (ttype(val(mp)) != LUA_TNIL) { /* main position is not free? */ | 362 | if (!ttisnil(val(mp))) { /* main position is not free? */ |
| 363 | Node *othern = luaH_mainposition(t, key(mp)); /* `mp' of colliding node */ | 363 | Node *othern = luaH_mainposition(t, key(mp)); /* `mp' of colliding node */ |
| 364 | Node *n = t->firstfree; /* get a free place */ | 364 | Node *n = t->firstfree; /* get a free place */ |
| 365 | if (othern != mp) { /* is colliding node out of its main position? */ | 365 | if (othern != mp) { /* is colliding node out of its main position? */ |
| @@ -378,9 +378,9 @@ static TObject *newkey (lua_State *L, Table *t, const TObject *key) { | |||
| 378 | } | 378 | } |
| 379 | } | 379 | } |
| 380 | setobj(key(mp), key); | 380 | setobj(key(mp), key); |
| 381 | lua_assert(ttype(val(mp)) == LUA_TNIL); | 381 | lua_assert(ttisnil(val(mp))); |
| 382 | for (;;) { /* correct `firstfree' */ | 382 | for (;;) { /* correct `firstfree' */ |
| 383 | if (ttype(key(t->firstfree)) == LUA_TNIL) | 383 | if (ttisnil(key(t->firstfree))) |
| 384 | return val(mp); /* OK; table still has a free place */ | 384 | return val(mp); /* OK; table still has a free place */ |
| 385 | else if (t->firstfree == t->node) break; /* cannot decrement from here */ | 385 | else if (t->firstfree == t->node) break; /* cannot decrement from here */ |
| 386 | else (t->firstfree)--; | 386 | else (t->firstfree)--; |
| @@ -389,7 +389,7 @@ static TObject *newkey (lua_State *L, Table *t, const TObject *key) { | |||
| 389 | setbvalue(val(mp), 0); /* avoid new key being removed */ | 389 | setbvalue(val(mp), 0); /* avoid new key being removed */ |
| 390 | rehash(L, t); /* grow table */ | 390 | rehash(L, t); /* grow table */ |
| 391 | val = cast(TObject *, luaH_get(t, key)); /* get new position */ | 391 | val = cast(TObject *, luaH_get(t, key)); /* get new position */ |
| 392 | lua_assert(ttype(val) == LUA_TBOOLEAN); | 392 | lua_assert(ttisboolean(val)); |
| 393 | setnilvalue(val); | 393 | setnilvalue(val); |
| 394 | return val; | 394 | return val; |
| 395 | } | 395 | } |
| @@ -399,7 +399,7 @@ static TObject *newkey (lua_State *L, Table *t, const TObject *key) { | |||
| 399 | ** generic search function | 399 | ** generic search function |
| 400 | */ | 400 | */ |
| 401 | static const TObject *luaH_getany (Table *t, const TObject *key) { | 401 | static const TObject *luaH_getany (Table *t, const TObject *key) { |
| 402 | if (ttype(key) == LUA_TNIL) return &luaO_nilobject; | 402 | if (ttisnil(key)) return &luaO_nilobject; |
| 403 | else { | 403 | else { |
| 404 | Node *n = luaH_mainposition(t, key); | 404 | Node *n = luaH_mainposition(t, key); |
| 405 | do { /* check whether `key' is somewhere in the chain */ | 405 | do { /* check whether `key' is somewhere in the chain */ |
| @@ -420,7 +420,7 @@ const TObject *luaH_getnum (Table *t, int key) { | |||
| 420 | else { | 420 | else { |
| 421 | Node *n = hashnum(t, key); | 421 | Node *n = hashnum(t, key); |
| 422 | do { /* check whether `key' is somewhere in the chain */ | 422 | do { /* check whether `key' is somewhere in the chain */ |
| 423 | if (ttype(key(n)) == LUA_TNUMBER && nvalue(key(n)) == (lua_Number)key) | 423 | if (ttisnumber(key(n)) && nvalue(key(n)) == (lua_Number)key) |
| 424 | return val(n); /* that's it */ | 424 | return val(n); /* that's it */ |
| 425 | else n = n->next; | 425 | else n = n->next; |
| 426 | } while (n); | 426 | } while (n); |
| @@ -435,7 +435,7 @@ const TObject *luaH_getnum (Table *t, int key) { | |||
| 435 | const TObject *luaH_getstr (Table *t, TString *key) { | 435 | const TObject *luaH_getstr (Table *t, TString *key) { |
| 436 | Node *n = hashstr(t, key); | 436 | Node *n = hashstr(t, key); |
| 437 | do { /* check whether `key' is somewhere in the chain */ | 437 | do { /* check whether `key' is somewhere in the chain */ |
| 438 | if (ttype(key(n)) == LUA_TSTRING && tsvalue(key(n)) == key) | 438 | if (ttisstring(key(n)) && tsvalue(key(n)) == key) |
| 439 | return val(n); /* that's it */ | 439 | return val(n); /* that's it */ |
| 440 | else n = n->next; | 440 | else n = n->next; |
| 441 | } while (n); | 441 | } while (n); |
| @@ -467,8 +467,8 @@ TObject *luaH_set (lua_State *L, Table *t, const TObject *key) { | |||
| 467 | if (p != &luaO_nilobject) | 467 | if (p != &luaO_nilobject) |
| 468 | return cast(TObject *, p); | 468 | return cast(TObject *, p); |
| 469 | else { | 469 | else { |
| 470 | if (ttype(key) == LUA_TNIL) luaG_runerror(L, "table index is nil"); | 470 | if (ttisnil(key)) luaG_runerror(L, "table index is nil"); |
| 471 | else if (ttype(key) == LUA_TNUMBER && nvalue(key) != nvalue(key)) | 471 | else if (ttisnumber(key) && nvalue(key) != nvalue(key)) |
| 472 | luaG_runerror(L, "table index is NaN"); | 472 | luaG_runerror(L, "table index is NaN"); |
| 473 | return newkey(L, t, key); | 473 | return newkey(L, t, key); |
| 474 | } | 474 | } |
