2
0

hash.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. ** hash.c
  3. ** hash manager for lua
  4. */
  5. char *rcs_hash="$Id: hash.c,v 2.8 1994/10/11 12:59:49 celes Exp $";
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include "mm.h"
  9. #include "opcode.h"
  10. #include "hash.h"
  11. #include "inout.h"
  12. #include "table.h"
  13. #include "lua.h"
  14. #define streq(s1,s2) (s1 == s2 || (*(s1) == *(s2) && strcmp(s1,s2)==0))
  15. #define new(s) ((s *)malloc(sizeof(s)))
  16. #define newvector(n,s) ((s *)calloc(n,sizeof(s)))
  17. #define nhash(t) ((t)->nhash)
  18. #define nuse(t) ((t)->nuse)
  19. #define markarray(t) ((t)->mark)
  20. #define nodevector(t) ((t)->node)
  21. #define node(t,i) (&(t)->node[i])
  22. #define ref(n) (&(n)->ref)
  23. #define val(n) (&(n)->val)
  24. #define REHASH_LIMIT 0.70 /* avoid more than this % full */
  25. static Hash *listhead = NULL;
  26. /* hash dimensions values */
  27. static int dimensions[] =
  28. {3, 5, 7, 11, 23, 47, 97, 197, 397, 797, 1597, 3203, 6421,
  29. 12853, 25717, 51437, 0};
  30. static int redimension (int nhash)
  31. {
  32. int i;
  33. for (i=0; dimensions[i]!=0; i++)
  34. {
  35. if (dimensions[i] > nhash)
  36. return dimensions[i];
  37. }
  38. return nhash*2+1;
  39. }
  40. static int hashindex (Hash *t, Object *ref) /* hash function */
  41. {
  42. switch (tag(ref))
  43. {
  44. case T_NUMBER:
  45. return (((int)nvalue(ref))%nhash(t));
  46. case T_STRING:
  47. {
  48. int h;
  49. char *name = svalue(ref);
  50. for (h=0; *name!=0; name++) /* interpret name as binary number */
  51. {
  52. h <<= 8;
  53. h += (unsigned char) *name; /* avoid sign extension */
  54. h %= nhash(t); /* make it a valid index */
  55. }
  56. return h;
  57. }
  58. case T_FUNCTION:
  59. return (((int)bvalue(ref))%nhash(t));
  60. case T_CFUNCTION:
  61. return (((int)fvalue(ref))%nhash(t));
  62. case T_ARRAY:
  63. return (((int)avalue(ref))%nhash(t));
  64. case T_USERDATA:
  65. return (((int)uvalue(ref))%nhash(t));
  66. default:
  67. lua_reportbug ("unexpected type to index table");
  68. return -1;
  69. }
  70. }
  71. static int equalObj (Object *t1, Object *t2)
  72. {
  73. if (tag(t1) != tag(t2)) return 0;
  74. switch (tag(t1))
  75. {
  76. case T_NUMBER: return nvalue(t1) == nvalue(t2);
  77. case T_STRING: return streq(svalue(t1), svalue(t2));
  78. default: return uvalue(t1) == uvalue(t2);
  79. }
  80. }
  81. static int present (Hash *t, Object *ref)
  82. {
  83. int h = hashindex(t, ref);
  84. if (h < 0) return h;
  85. while (tag(ref(node(t, h))) != T_NIL)
  86. {
  87. if (equalObj(ref, ref(node(t, h))))
  88. return h;
  89. h = (h+1) % nhash(t);
  90. }
  91. return h;
  92. }
  93. /*
  94. ** Alloc a vector node
  95. */
  96. static Node *hashnodecreate (int nhash)
  97. {
  98. int i;
  99. Node *v = newvector (nhash, Node);
  100. if (v == NULL)
  101. {
  102. lua_error ("not enough memory");
  103. return NULL;
  104. }
  105. for (i=0; i<nhash; i++)
  106. tag(ref(&v[i])) = T_NIL;
  107. return v;
  108. }
  109. /*
  110. ** Create a new hash. Return the hash pointer or NULL on error.
  111. */
  112. static Hash *hashcreate (int nhash)
  113. {
  114. Hash *t = new (Hash);
  115. if (t == NULL)
  116. {
  117. lua_error ("not enough memory");
  118. return NULL;
  119. }
  120. nhash = redimension((int)((float)nhash/REHASH_LIMIT));
  121. nodevector(t) = hashnodecreate(nhash);
  122. if (nodevector(t) == NULL)
  123. return NULL;
  124. nhash(t) = nhash;
  125. nuse(t) = 0;
  126. markarray(t) = 0;
  127. return t;
  128. }
  129. /*
  130. ** Delete a hash
  131. */
  132. static void hashdelete (Hash *t)
  133. {
  134. free (nodevector(t));
  135. free(t);
  136. }
  137. /*
  138. ** Mark a hash and check its elements
  139. */
  140. void lua_hashmark (Hash *h)
  141. {
  142. if (markarray(h) == 0)
  143. {
  144. int i;
  145. markarray(h) = 1;
  146. for (i=0; i<nhash(h); i++)
  147. {
  148. Node *n = node(h,i);
  149. if (tag(ref(n)) != T_NIL)
  150. {
  151. lua_markobject(&n->ref);
  152. lua_markobject(&n->val);
  153. }
  154. }
  155. }
  156. }
  157. /*
  158. ** Garbage collection to arrays
  159. ** Delete all unmarked arrays.
  160. */
  161. void lua_hashcollector (void)
  162. {
  163. Hash *curr_array = listhead, *prev = NULL;
  164. while (curr_array != NULL)
  165. {
  166. Hash *next = curr_array->next;
  167. if (markarray(curr_array) != 1)
  168. {
  169. if (prev == NULL) listhead = next;
  170. else prev->next = next;
  171. hashdelete(curr_array);
  172. ++lua_recovered;
  173. }
  174. else
  175. {
  176. markarray(curr_array) = 0;
  177. prev = curr_array;
  178. }
  179. curr_array = next;
  180. }
  181. }
  182. /*
  183. ** Create a new array
  184. ** This function inserts the new array in the array list. It also
  185. ** executes garbage collection if the number of arrays created
  186. ** exceed a pre-defined range.
  187. */
  188. Hash *lua_createarray (int nhash)
  189. {
  190. Hash *array = hashcreate(nhash);
  191. if (array == NULL)
  192. {
  193. lua_error ("not enough memory");
  194. return NULL;
  195. }
  196. if (lua_nentity == lua_block)
  197. lua_pack();
  198. lua_nentity++;
  199. array->next = listhead;
  200. listhead = array;
  201. return array;
  202. }
  203. /*
  204. ** Re-hash
  205. */
  206. static void rehash (Hash *t)
  207. {
  208. int i;
  209. int nold = nhash(t);
  210. Node *vold = nodevector(t);
  211. nhash(t) = redimension(nhash(t));
  212. nodevector(t) = hashnodecreate(nhash(t));
  213. for (i=0; i<nold; i++)
  214. {
  215. Node *n = vold+i;
  216. if (tag(ref(n)) != T_NIL && tag(val(n)) != T_NIL)
  217. *node(t, present(t, ref(n))) = *n; /* copy old node to new hahs */
  218. }
  219. free(vold);
  220. }
  221. /*
  222. ** If the hash node is present, return its pointer, otherwise search
  223. ** the node at parent table, recursively, if there is parent.
  224. ** If no parent and the node is not present, return a static nil object.
  225. */
  226. Object *lua_hashget (Hash *t, Object *ref)
  227. {
  228. static int count = 1000;
  229. static Object nil_obj = {T_NIL, {NULL}};
  230. int h = present(t, ref);
  231. if (h < 0) return &nil_obj;
  232. if (tag(ref(node(t, h))) != T_NIL) return val(node(t, h));
  233. if (--count == 0)
  234. {
  235. lua_reportbug ("hierarchy too deep (maybe there is an inheritance loop)");
  236. return &nil_obj;
  237. }
  238. { /* check "parent" or "godparent" field */
  239. Hash *p;
  240. Object parent;
  241. Object godparent;
  242. tag(&parent) = T_STRING; svalue(&parent) = "parent";
  243. tag(&godparent) = T_STRING; svalue(&godparent) = "godparent";
  244. h = present(t, &parent); /* assert(h >= 0); */
  245. p = tag(ref(node(t, h))) != T_NIL && tag(val(node(t, h))) == T_ARRAY ?
  246. avalue(val(node(t, h))) : NULL;
  247. if (p != NULL)
  248. {
  249. Object *r = lua_hashget(p, ref);
  250. if (tag(r) != T_NIL) { count++; return r; }
  251. }
  252. h = present(t, &godparent); /* assert(h >= 0); */
  253. p = tag(ref(node(t, h))) != T_NIL && tag(val(node(t, h))) == T_ARRAY ?
  254. avalue(val(node(t, h))) : NULL;
  255. if (p != NULL)
  256. {
  257. Object *r = lua_hashget(p, ref);
  258. if (tag(r) != T_NIL) { count++; return r; }
  259. }
  260. }
  261. count++;
  262. return &nil_obj;
  263. }
  264. /*
  265. ** If the hash node is present, return its pointer, otherwise create a new
  266. ** node for the given reference and also return its pointer.
  267. ** On error, return NULL.
  268. */
  269. Object *lua_hashdefine (Hash *t, Object *ref)
  270. {
  271. int h;
  272. Node *n;
  273. h = present(t, ref);
  274. if (h < 0) return NULL;
  275. n = node(t, h);
  276. if (tag(ref(n)) == T_NIL)
  277. {
  278. nuse(t)++;
  279. if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT)
  280. {
  281. rehash(t);
  282. h = present(t, ref);
  283. n = node(t, h);
  284. }
  285. *ref(n) = *ref;
  286. tag(val(n)) = T_NIL;
  287. }
  288. return (val(n));
  289. }
  290. /*
  291. ** Internal function to manipulate arrays.
  292. ** Given an array object and a reference value, return the next element
  293. ** in the hash.
  294. ** This function pushs the element value and its reference to the stack.
  295. */
  296. static void hashnext (Hash *t, int i)
  297. {
  298. if (i >= nhash(t))
  299. {
  300. lua_pushnil(); lua_pushnil();
  301. return;
  302. }
  303. while (tag(ref(node(t,i))) == T_NIL || tag(val(node(t,i))) == T_NIL)
  304. {
  305. if (++i >= nhash(t))
  306. {
  307. lua_pushnil(); lua_pushnil();
  308. return;
  309. }
  310. }
  311. lua_pushobject(ref(node(t,i)));
  312. lua_pushobject(val(node(t,i)));
  313. }
  314. void lua_next (void)
  315. {
  316. Hash *t;
  317. Object *o = lua_getparam (1);
  318. Object *r = lua_getparam (2);
  319. if (o == NULL || r == NULL)
  320. { lua_error ("too few arguments to function `next'"); return; }
  321. if (lua_getparam (3) != NULL)
  322. { lua_error ("too many arguments to function `next'"); return; }
  323. if (tag(o) != T_ARRAY)
  324. { lua_error ("first argument of function `next' is not a table"); return; }
  325. t = avalue(o);
  326. if (tag(r) == T_NIL)
  327. {
  328. hashnext(t, 0);
  329. }
  330. else
  331. {
  332. int h = present (t, r);
  333. if (h >= 0)
  334. hashnext(t, h+1);
  335. else
  336. lua_error ("error in function 'next': reference not found");
  337. }
  338. }