diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-04-25 13:55:09 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-04-25 13:55:09 -0300 |
| commit | 534c3a64d3b47585b415f229aa03af35f9a4796e (patch) | |
| tree | bda9de835dbeff8b73ed5f0977e81dc9573ad046 /ltable.c | |
| parent | b9c98cd4d97774d703a48fefd56c66f552e0cd83 (diff) | |
| download | lua-534c3a64d3b47585b415f229aa03af35f9a4796e.tar.gz lua-534c3a64d3b47585b415f229aa03af35f9a4796e.tar.bz2 lua-534c3a64d3b47585b415f229aa03af35f9a4796e.zip | |
small optimizations for table access
Diffstat (limited to 'ltable.c')
| -rw-r--r-- | ltable.c | 48 |
1 files changed, 37 insertions, 11 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltable.c,v 1.38 2000/03/29 20:19:20 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 1.39 2000/03/31 16:28:45 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 | */ |
| @@ -60,11 +60,12 @@ Node *luaH_mainposition (const Hash *t, const TObject *key) { | |||
| 60 | } | 60 | } |
| 61 | LUA_ASSERT(L, h%(unsigned int)t->size == (h&((unsigned int)t->size-1)), | 61 | LUA_ASSERT(L, h%(unsigned int)t->size == (h&((unsigned int)t->size-1)), |
| 62 | "a&(x-1) == a%x, for x power of 2"); | 62 | "a&(x-1) == a%x, for x power of 2"); |
| 63 | return &t->node[h&((unsigned int)t->size-1)]; | 63 | return &t->node[h&(t->size-1)]; |
| 64 | } | 64 | } |
| 65 | 65 | ||
| 66 | 66 | ||
| 67 | const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) { | 67 | static const TObject *luaH_getany (lua_State *L, const Hash *t, |
| 68 | const TObject *key) { | ||
| 68 | Node *n = luaH_mainposition(t, key); | 69 | Node *n = luaH_mainposition(t, key); |
| 69 | if (!n) | 70 | if (!n) |
| 70 | lua_error(L, "unexpected type to index table"); | 71 | lua_error(L, "unexpected type to index table"); |
| @@ -77,6 +78,39 @@ const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) { | |||
| 77 | } | 78 | } |
| 78 | 79 | ||
| 79 | 80 | ||
| 81 | /* specialized version for numbers */ | ||
| 82 | const TObject *luaH_getnum (const Hash *t, Number key) { | ||
| 83 | Node *n = &t->node[(unsigned long)(long)key&(t->size-1)]; | ||
| 84 | do { | ||
| 85 | if (ttype(&n->key) == TAG_NUMBER && nvalue(&n->key) == key) | ||
| 86 | return &n->val; | ||
| 87 | n = n->next; | ||
| 88 | } while (n); | ||
| 89 | return &luaO_nilobject; /* key not found */ | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | /* specialized version for strings */ | ||
| 94 | static const TObject *luaH_getstr (const Hash *t, TString *key) { | ||
| 95 | Node *n = &t->node[key->hash&(t->size-1)]; | ||
| 96 | do { | ||
| 97 | if (ttype(&n->key) == TAG_STRING && tsvalue(&n->key) == key) | ||
| 98 | return &n->val; | ||
| 99 | n = n->next; | ||
| 100 | } while (n); | ||
| 101 | return &luaO_nilobject; /* key not found */ | ||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) { | ||
| 106 | switch (ttype(key)) { | ||
| 107 | case TAG_NUMBER: return luaH_getnum(t, nvalue(key)); | ||
| 108 | case TAG_STRING: return luaH_getstr(t, tsvalue(key)); | ||
| 109 | default: return luaH_getany(L, t, key); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 80 | int luaH_pos (lua_State *L, const Hash *t, const TObject *key) { | 114 | int luaH_pos (lua_State *L, const Hash *t, const TObject *key) { |
| 81 | const TObject *v = luaH_get(L, t, key); | 115 | const TObject *v = luaH_get(L, t, key); |
| 82 | return (v == &luaO_nilobject) ? -1 : /* key not found */ | 116 | return (v == &luaO_nilobject) ? -1 : /* key not found */ |
| @@ -214,11 +248,3 @@ void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val) { | |||
| 214 | luaH_set(L, t, &index, val); | 248 | luaH_set(L, t, &index, val); |
| 215 | } | 249 | } |
| 216 | 250 | ||
| 217 | |||
| 218 | const TObject *luaH_getint (lua_State *L, const Hash *t, int key) { | ||
| 219 | TObject index; | ||
| 220 | ttype(&index) = TAG_NUMBER; | ||
| 221 | nvalue(&index) = key; | ||
| 222 | return luaH_get(L, t, &index); | ||
| 223 | } | ||
| 224 | |||
