aboutsummaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1994-11-03 20:20:15 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1994-11-03 20:20:15 -0200
commit5cb6856ebc8c95d8d6bd38b3e4b366dc46c6a903 (patch)
tree9deade25c755459aacafc52bc813d89f1a756f60 /hash.c
parent852d9a859733ebf7322474c05244105e4f2748a2 (diff)
downloadlua-5cb6856ebc8c95d8d6bd38b3e4b366dc46c6a903.tar.gz
lua-5cb6856ebc8c95d8d6bd38b3e4b366dc46c6a903.tar.bz2
lua-5cb6856ebc8c95d8d6bd38b3e4b366dc46c6a903.zip
because lua_error now does a longjmp, many functions do not need
to check conditions.
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c54
1 files changed, 14 insertions, 40 deletions
diff --git a/hash.c b/hash.c
index 16c0b461..bed99340 100644
--- a/hash.c
+++ b/hash.c
@@ -3,7 +3,7 @@
3** hash manager for lua 3** hash manager for lua
4*/ 4*/
5 5
6char *rcs_hash="$Id: hash.c,v 2.10 1994/11/01 17:54:31 roberto Exp roberto $"; 6char *rcs_hash="$Id: hash.c,v 2.11 1994/11/02 20:29:09 roberto Exp roberto $";
7 7
8#include <string.h> 8#include <string.h>
9#include <stdlib.h> 9#include <stdlib.h>
@@ -78,7 +78,7 @@ static int hashindex (Hash *t, Object *ref) /* hash function */
78 return (((int)uvalue(ref))%nhash(t)); 78 return (((int)uvalue(ref))%nhash(t));
79 default: 79 default:
80 lua_reportbug ("unexpected type to index table"); 80 lua_reportbug ("unexpected type to index table");
81 return -1; 81 return -1; /* UNREACHEABLE */
82 } 82 }
83} 83}
84 84
@@ -96,7 +96,6 @@ static int equalObj (Object *t1, Object *t2)
96static int present (Hash *t, Object *ref) 96static int present (Hash *t, Object *ref)
97{ 97{
98 int h = hashindex(t, ref); 98 int h = hashindex(t, ref);
99 if (h < 0) return h;
100 while (tag(ref(node(t, h))) != LUA_T_NIL) 99 while (tag(ref(node(t, h))) != LUA_T_NIL)
101 { 100 {
102 if (equalObj(ref, ref(node(t, h)))) 101 if (equalObj(ref, ref(node(t, h))))
@@ -115,12 +114,9 @@ static Node *hashnodecreate (int nhash)
115 int i; 114 int i;
116 Node *v = newvector (nhash, Node); 115 Node *v = newvector (nhash, Node);
117 if (v == NULL) 116 if (v == NULL)
118 { 117 lua_error ("not enough memory");
119 lua_error ("not enough memory");
120 return NULL;
121 }
122 for (i=0; i<nhash; i++) 118 for (i=0; i<nhash; i++)
123 tag(ref(&v[i])) = LUA_T_NIL; 119 tag(ref(&v[i])) = LUA_T_NIL;
124 return v; 120 return v;
125} 121}
126 122
@@ -131,16 +127,12 @@ static Hash *hashcreate (int nhash)
131{ 127{
132 Hash *t = new (Hash); 128 Hash *t = new (Hash);
133 if (t == NULL) 129 if (t == NULL)
134 { 130 lua_error ("not enough memory");
135 lua_error ("not enough memory");
136 return NULL;
137 }
138 nhash = redimension((int)((float)nhash/REHASH_LIMIT)); 131 nhash = redimension((int)((float)nhash/REHASH_LIMIT));
139 132
140 nodevector(t) = hashnodecreate(nhash); 133 nodevector(t) = hashnodecreate(nhash);
141 if (nodevector(t) == NULL) 134 if (nodevector(t) == NULL)
142 return NULL; 135 lua_error ("not enough memory");
143
144 nhash(t) = nhash; 136 nhash(t) = nhash;
145 nuse(t) = 0; 137 nuse(t) = 0;
146 markarray(t) = 0; 138 markarray(t) = 0;
@@ -214,15 +206,8 @@ void lua_hashcollector (void)
214Hash *lua_createarray (int nhash) 206Hash *lua_createarray (int nhash)
215{ 207{
216 Hash *array = hashcreate(nhash); 208 Hash *array = hashcreate(nhash);
217 if (array == NULL)
218 {
219 lua_error ("not enough memory");
220 return NULL;
221 }
222
223 if (lua_nentity == lua_block) 209 if (lua_nentity == lua_block)
224 lua_pack(); 210 lua_pack();
225
226 lua_nentity++; 211 lua_nentity++;
227 array->next = listhead; 212 array->next = listhead;
228 listhead = array; 213 listhead = array;
@@ -259,14 +244,9 @@ Object *lua_hashget (Hash *t, Object *ref)
259 static int count = 1000; 244 static int count = 1000;
260 static Object nil_obj = {LUA_T_NIL, {NULL}}; 245 static Object nil_obj = {LUA_T_NIL, {NULL}};
261 int h = present(t, ref); 246 int h = present(t, ref);
262 if (h < 0) return &nil_obj;
263 if (tag(ref(node(t, h))) != LUA_T_NIL) return val(node(t, h)); 247 if (tag(ref(node(t, h))) != LUA_T_NIL) return val(node(t, h));
264 if (--count == 0) 248 if (--count == 0)
265 { 249 lua_reportbug ("hierarchy too deep (maybe there is an inheritance loop)");
266 lua_reportbug ("hierarchy too deep (maybe there is an inheritance loop)");
267 return &nil_obj;
268 }
269
270 { /* check "parent" or "godparent" field */ 250 { /* check "parent" or "godparent" field */
271 Hash *p; 251 Hash *p;
272 Object parent; 252 Object parent;
@@ -274,7 +254,7 @@ Object *lua_hashget (Hash *t, Object *ref)
274 tag(&parent) = LUA_T_STRING; svalue(&parent) = "parent"; 254 tag(&parent) = LUA_T_STRING; svalue(&parent) = "parent";
275 tag(&godparent) = LUA_T_STRING; svalue(&godparent) = "godparent"; 255 tag(&godparent) = LUA_T_STRING; svalue(&godparent) = "godparent";
276 256
277 h = present(t, &parent); /* assert(h >= 0); */ 257 h = present(t, &parent);
278 p = tag(ref(node(t, h))) != LUA_T_NIL && tag(val(node(t, h))) == LUA_T_ARRAY ? 258 p = tag(ref(node(t, h))) != LUA_T_NIL && tag(val(node(t, h))) == LUA_T_ARRAY ?
279 avalue(val(node(t, h))) : NULL; 259 avalue(val(node(t, h))) : NULL;
280 if (p != NULL) 260 if (p != NULL)
@@ -283,7 +263,7 @@ Object *lua_hashget (Hash *t, Object *ref)
283 if (tag(r) != LUA_T_NIL) { count++; return r; } 263 if (tag(r) != LUA_T_NIL) { count++; return r; }
284 } 264 }
285 265
286 h = present(t, &godparent); /* assert(h >= 0); */ 266 h = present(t, &godparent);
287 p = tag(ref(node(t, h))) != LUA_T_NIL && tag(val(node(t, h))) == LUA_T_ARRAY ? 267 p = tag(ref(node(t, h))) != LUA_T_NIL && tag(val(node(t, h))) == LUA_T_ARRAY ?
288 avalue(val(node(t, h))) : NULL; 268 avalue(val(node(t, h))) : NULL;
289 if (p != NULL) 269 if (p != NULL)
@@ -299,14 +279,12 @@ Object *lua_hashget (Hash *t, Object *ref)
299/* 279/*
300** If the hash node is present, return its pointer, otherwise create a new 280** If the hash node is present, return its pointer, otherwise create a new
301** node for the given reference and also return its pointer. 281** node for the given reference and also return its pointer.
302** On error, return NULL.
303*/ 282*/
304Object *lua_hashdefine (Hash *t, Object *ref) 283Object *lua_hashdefine (Hash *t, Object *ref)
305{ 284{
306 int h; 285 int h;
307 Node *n; 286 Node *n;
308 h = present(t, ref); 287 h = present(t, ref);
309 if (h < 0) return NULL;
310 n = node(t, h); 288 n = node(t, h);
311 if (tag(ref(n)) == LUA_T_NIL) 289 if (tag(ref(n)) == LUA_T_NIL)
312 { 290 {
@@ -355,12 +333,11 @@ void lua_next (void)
355 Object *o = lua_getparam (1); 333 Object *o = lua_getparam (1);
356 Object *r = lua_getparam (2); 334 Object *r = lua_getparam (2);
357 if (o == NULL || r == NULL) 335 if (o == NULL || r == NULL)
358 { lua_error ("too few arguments to function `next'"); return; } 336 lua_error ("too few arguments to function `next'");
359 if (lua_getparam (3) != NULL) 337 if (lua_getparam (3) != NULL)
360 { lua_error ("too many arguments to function `next'"); return; } 338 lua_error ("too many arguments to function `next'");
361 if (tag(o) != LUA_T_ARRAY) 339 if (tag(o) != LUA_T_ARRAY)
362 { lua_error ("first argument of function `next' is not a table"); return; } 340 lua_error ("first argument of function `next' is not a table");
363
364 t = avalue(o); 341 t = avalue(o);
365 if (tag(r) == LUA_T_NIL) 342 if (tag(r) == LUA_T_NIL)
366 { 343 {
@@ -369,9 +346,6 @@ void lua_next (void)
369 else 346 else
370 { 347 {
371 int h = present (t, r); 348 int h = present (t, r);
372 if (h >= 0) 349 hashnext(t, h+1);
373 hashnext(t, h+1);
374 else
375 lua_error ("error in function 'next': reference not found");
376 } 350 }
377} 351}