hash.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. ** hash.c
  3. ** hash manager for lua
  4. */
  5. char *rcs_hash="$Id: hash.c,v 2.7 1994/09/08 15:27:10 celes Exp celes $";
  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 index (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 = index(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. }
  173. else
  174. {
  175. markarray(curr_array) = 0;
  176. prev = curr_array;
  177. }
  178. curr_array = next;
  179. }
  180. }
  181. /*
  182. ** Create a new array
  183. ** This function inserts the new array in the array list. It also
  184. ** executes garbage collection if the number of arrays created
  185. ** exceed a pre-defined range.
  186. */
  187. Hash *lua_createarray (int nhash)
  188. {
  189. Hash *array = hashcreate(nhash);
  190. if (array == NULL)
  191. {
  192. lua_error ("not enough memory");
  193. return NULL;
  194. }
  195. if (lua_nentity == lua_block)
  196. lua_pack();
  197. lua_nentity++;
  198. array->next = listhead;
  199. listhead = array;
  200. return array;
  201. }
  202. /*
  203. ** Re-hash
  204. */
  205. static void rehash (Hash *t)
  206. {
  207. int i;
  208. int nold = nhash(t);
  209. Node *vold = nodevector(t);
  210. nhash(t) = redimension(nhash(t));
  211. nodevector(t) = hashnodecreate(nhash(t));
  212. for (i=0; i<nold; i++)
  213. {
  214. Node *n = vold+i;
  215. if (tag(ref(n)) != T_NIL && tag(val(n)) != T_NIL)
  216. *node(t, present(t, ref(n))) = *n; /* copy old node to new hahs */
  217. }
  218. free(vold);
  219. }
  220. /*
  221. ** If the hash node is present, return its pointer, otherwise search
  222. ** the node at parent table, recursively, if there is parent.
  223. ** If no parent and the node is not present, return a static nil object.
  224. */
  225. Object *lua_hashget (Hash *t, Object *ref)
  226. {
  227. static int count = 1000;
  228. static Object nil_obj = {T_NIL, {NULL}};
  229. int h = present(t, ref);
  230. if (h < 0) return &nil_obj;
  231. if (tag(ref(node(t, h))) != T_NIL) return val(node(t, h));
  232. if (--count == 0)
  233. {
  234. lua_reportbug ("hierarchy too deep (maybe there is an inheritance loop)");
  235. return &nil_obj;
  236. }
  237. { /* check "parent" or "godparent" field */
  238. Hash *p;
  239. Object parent;
  240. Object godparent;
  241. tag(&parent) = T_STRING; svalue(&parent) = "parent";
  242. tag(&godparent) = T_STRING; svalue(&godparent) = "godparent";
  243. h = present(t, &parent); /* assert(h >= 0); */
  244. p = tag(ref(node(t, h))) != T_NIL && tag(val(node(t, h))) == T_ARRAY ?
  245. avalue(val(node(t, h))) : NULL;
  246. if (p != NULL)
  247. {
  248. Object *r = lua_hashget(p, ref);
  249. if (tag(r) != T_NIL) { count++; return r; }
  250. }
  251. h = present(t, &godparent); /* assert(h >= 0); */
  252. p = tag(ref(node(t, h))) != T_NIL && tag(val(node(t, h))) == T_ARRAY ?
  253. avalue(val(node(t, h))) : NULL;
  254. if (p != NULL)
  255. {
  256. Object *r = lua_hashget(p, ref);
  257. if (tag(r) != T_NIL) { count++; return r; }
  258. }
  259. }
  260. count++;
  261. return &nil_obj;
  262. }
  263. /*
  264. ** If the hash node is present, return its pointer, otherwise create a new
  265. ** node for the given reference and also return its pointer.
  266. ** On error, return NULL.
  267. */
  268. Object *lua_hashdefine (Hash *t, Object *ref)
  269. {
  270. int h;
  271. Node *n;
  272. h = present(t, ref);
  273. if (h < 0) return NULL;
  274. n = node(t, h);
  275. if (tag(ref(n)) == T_NIL)
  276. {
  277. nuse(t)++;
  278. if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT)
  279. {
  280. rehash(t);
  281. h = present(t, ref);
  282. n = node(t, h);
  283. }
  284. *ref(n) = *ref;
  285. tag(val(n)) = T_NIL;
  286. }
  287. return (val(n));
  288. }
  289. /*
  290. ** Internal function to manipulate arrays.
  291. ** Given an array object and a reference value, return the next element
  292. ** in the hash.
  293. ** This function pushs the element value and its reference to the stack.
  294. */
  295. static void hashnext (Hash *t, int i)
  296. {
  297. if (i >= nhash(t))
  298. {
  299. lua_pushnil(); lua_pushnil();
  300. return;
  301. }
  302. while (tag(ref(node(t,i))) == T_NIL || tag(val(node(t,i))) == T_NIL)
  303. {
  304. if (++i >= nhash(t))
  305. {
  306. lua_pushnil(); lua_pushnil();
  307. return;
  308. }
  309. }
  310. lua_pushobject(ref(node(t,i)));
  311. lua_pushobject(val(node(t,i)));
  312. }
  313. void lua_next (void)
  314. {
  315. Hash *t;
  316. Object *o = lua_getparam (1);
  317. Object *r = lua_getparam (2);
  318. if (o == NULL || r == NULL)
  319. { lua_error ("too few arguments to function `next'"); return; }
  320. if (lua_getparam (3) != NULL)
  321. { lua_error ("too many arguments to function `next'"); return; }
  322. if (tag(o) != T_ARRAY)
  323. { lua_error ("first argument of function `next' is not a table"); return; }
  324. t = avalue(o);
  325. if (tag(r) == T_NIL)
  326. {
  327. hashnext(t, 0);
  328. }
  329. else
  330. {
  331. int h = present (t, r);
  332. if (h >= 0)
  333. hashnext(t, h+1);
  334. else
  335. lua_error ("error in function 'next': reference not found");
  336. }
  337. }