summaryrefslogtreecommitdiff
path: root/ltable.c
diff options
context:
space:
mode:
Diffstat (limited to 'ltable.c')
-rw-r--r--ltable.c48
1 files changed, 37 insertions, 11 deletions
diff --git a/ltable.c b/ltable.c
index 299821ca..cc3a64f1 100644
--- a/ltable.c
+++ b/ltable.c
@@ -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
67const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) { 67static 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 */
82const 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 */
94static 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
105const 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
80int luaH_pos (lua_State *L, const Hash *t, const TObject *key) { 114int 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
218const 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