hash.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. ** hash.c
  3. ** hash manager for lua
  4. */
  5. char *rcs_hash="$Id: hash.c,v 2.25 1995/05/02 18:43:03 roberto Exp $";
  6. #include <string.h>
  7. #include "mem.h"
  8. #include "opcode.h"
  9. #include "hash.h"
  10. #include "table.h"
  11. #include "lua.h"
  12. #define streq(s1,s2) (s1 == s2 || (*(s1) == *(s2) && strcmp(s1,s2)==0))
  13. #define nhash(t) ((t)->nhash)
  14. #define nuse(t) ((t)->nuse)
  15. #define markarray(t) ((t)->mark)
  16. #define nodevector(t) ((t)->node)
  17. #define node(t,i) (&(t)->node[i])
  18. #define ref(n) (&(n)->ref)
  19. #define val(n) (&(n)->val)
  20. #define REHASH_LIMIT 0.70 /* avoid more than this % full */
  21. static Hash *listhead = NULL;
  22. /* hash dimensions values */
  23. static Word dimensions[] =
  24. {3, 5, 7, 11, 23, 47, 97, 197, 397, 797, 1597, 3203, 6421,
  25. 12853, 25717, 51437, 65521, 0}; /* 65521 == last prime < MAX_WORD */
  26. static Word redimension (Word nhash)
  27. {
  28. Word i;
  29. for (i=0; dimensions[i]!=0; i++)
  30. {
  31. if (dimensions[i] > nhash)
  32. return dimensions[i];
  33. }
  34. lua_error("table overflow");
  35. return 0; /* to avoid warnings */
  36. }
  37. static Word hashindex (Hash *t, Object *ref) /* hash function */
  38. {
  39. switch (tag(ref))
  40. {
  41. case LUA_T_NIL:
  42. lua_error ("unexpected type to index table");
  43. return -1; /* UNREACHEABLE */
  44. case LUA_T_NUMBER:
  45. return (((Word)nvalue(ref))%nhash(t));
  46. case LUA_T_STRING:
  47. {
  48. unsigned long h = tsvalue(ref)->hash;
  49. if (h == 0)
  50. {
  51. char *name = svalue(ref);
  52. while (*name)
  53. h = ((h<<5)-h)^(unsigned char)*(name++);
  54. tsvalue(ref)->hash = h;
  55. }
  56. return (Word)h%nhash(t); /* make it a valid index */
  57. }
  58. case LUA_T_FUNCTION:
  59. return (((IntPoint)ref->value.tf)%nhash(t));
  60. case LUA_T_CFUNCTION:
  61. return (((IntPoint)fvalue(ref))%nhash(t));
  62. case LUA_T_ARRAY:
  63. return (((IntPoint)avalue(ref))%nhash(t));
  64. default: /* user data */
  65. return (((IntPoint)uvalue(ref))%nhash(t));
  66. }
  67. }
  68. Bool lua_equalObj (Object *t1, Object *t2)
  69. {
  70. if (tag(t1) != tag(t2)) return 0;
  71. switch (tag(t1))
  72. {
  73. case LUA_T_NIL: return 1;
  74. case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
  75. case LUA_T_STRING: return streq(svalue(t1), svalue(t2));
  76. case LUA_T_ARRAY: return avalue(t1) == avalue(t2);
  77. case LUA_T_FUNCTION: return t1->value.tf == t2->value.tf;
  78. case LUA_T_CFUNCTION: return fvalue(t1) == fvalue(t2);
  79. default: return uvalue(t1) == uvalue(t2);
  80. }
  81. }
  82. static Word present (Hash *t, Object *ref)
  83. {
  84. Word h = hashindex(t, ref);
  85. while (tag(ref(node(t, h))) != LUA_T_NIL)
  86. {
  87. if (lua_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 (Word nhash)
  97. {
  98. Word i;
  99. Node *v = newvector (nhash, Node);
  100. for (i=0; i<nhash; i++)
  101. tag(ref(&v[i])) = LUA_T_NIL;
  102. return v;
  103. }
  104. /*
  105. ** Create a new hash. Return the hash pointer or NULL on error.
  106. */
  107. static Hash *hashcreate (Word nhash)
  108. {
  109. Hash *t = new(Hash);
  110. nhash = redimension((Word)((float)nhash/REHASH_LIMIT));
  111. nodevector(t) = hashnodecreate(nhash);
  112. nhash(t) = nhash;
  113. nuse(t) = 0;
  114. markarray(t) = 0;
  115. return t;
  116. }
  117. /*
  118. ** Delete a hash
  119. */
  120. static void hashdelete (Hash *t)
  121. {
  122. luaI_free (nodevector(t));
  123. luaI_free(t);
  124. }
  125. /*
  126. ** Mark a hash and check its elements
  127. */
  128. void lua_hashmark (Hash *h)
  129. {
  130. if (markarray(h) == 0)
  131. {
  132. Word i;
  133. markarray(h) = 1;
  134. for (i=0; i<nhash(h); i++)
  135. {
  136. Node *n = node(h,i);
  137. if (tag(ref(n)) != LUA_T_NIL)
  138. {
  139. lua_markobject(&n->ref);
  140. lua_markobject(&n->val);
  141. }
  142. }
  143. }
  144. }
  145. static void call_fallbacks (void)
  146. {
  147. Hash *curr_array;
  148. Object t;
  149. tag(&t) = LUA_T_ARRAY;
  150. for (curr_array = listhead; curr_array; curr_array = curr_array->next)
  151. if (markarray(curr_array) != 1)
  152. {
  153. avalue(&t) = curr_array;
  154. luaI_gcFB(&t);
  155. }
  156. tag(&t) = LUA_T_NIL;
  157. luaI_gcFB(&t); /* end of list */
  158. }
  159. /*
  160. ** Garbage collection to arrays
  161. ** Delete all unmarked arrays.
  162. */
  163. Long lua_hashcollector (void)
  164. {
  165. Hash *curr_array = listhead, *prev = NULL;
  166. Long counter = 0;
  167. call_fallbacks();
  168. while (curr_array != NULL)
  169. {
  170. Hash *next = curr_array->next;
  171. if (markarray(curr_array) != 1)
  172. {
  173. if (prev == NULL) listhead = next;
  174. else prev->next = next;
  175. hashdelete(curr_array);
  176. ++counter;
  177. }
  178. else
  179. {
  180. markarray(curr_array) = 0;
  181. prev = curr_array;
  182. }
  183. curr_array = next;
  184. }
  185. return counter;
  186. }
  187. /*
  188. ** Create a new array
  189. ** This function inserts the new array in the array list. It also
  190. ** executes garbage collection if the number of arrays created
  191. ** exceed a pre-defined range.
  192. */
  193. Hash *lua_createarray (Word nhash)
  194. {
  195. Hash *array;
  196. lua_pack();
  197. array = hashcreate(nhash);
  198. array->next = listhead;
  199. listhead = array;
  200. return array;
  201. }
  202. /*
  203. ** Re-hash
  204. */
  205. static void rehash (Hash *t)
  206. {
  207. Word i;
  208. Word 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)) != LUA_T_NIL && tag(val(n)) != LUA_T_NIL)
  216. *node(t, present(t, ref(n))) = *n; /* copy old node to new hahs */
  217. }
  218. luaI_free(vold);
  219. }
  220. /*
  221. ** If the hash node is present, return its pointer, otherwise return
  222. ** null.
  223. */
  224. Object *lua_hashget (Hash *t, Object *ref)
  225. {
  226. Word h = present(t, ref);
  227. if (tag(ref(node(t, h))) != LUA_T_NIL) return val(node(t, h));
  228. else return NULL;
  229. }
  230. /*
  231. ** If the hash node is present, return its pointer, otherwise create a new
  232. ** node for the given reference and also return its pointer.
  233. */
  234. Object *lua_hashdefine (Hash *t, Object *ref)
  235. {
  236. Word h;
  237. Node *n;
  238. h = present(t, ref);
  239. n = node(t, h);
  240. if (tag(ref(n)) == LUA_T_NIL)
  241. {
  242. nuse(t)++;
  243. if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT)
  244. {
  245. rehash(t);
  246. h = present(t, ref);
  247. n = node(t, h);
  248. }
  249. *ref(n) = *ref;
  250. tag(val(n)) = LUA_T_NIL;
  251. }
  252. return (val(n));
  253. }
  254. /*
  255. ** Internal function to manipulate arrays.
  256. ** Given an array object and a reference value, return the next element
  257. ** in the hash.
  258. ** This function pushs the element value and its reference to the stack.
  259. */
  260. static void hashnext (Hash *t, Word i)
  261. {
  262. if (i >= nhash(t))
  263. {
  264. lua_pushnil(); lua_pushnil();
  265. return;
  266. }
  267. while (tag(ref(node(t,i))) == LUA_T_NIL || tag(val(node(t,i))) == LUA_T_NIL)
  268. {
  269. if (++i >= nhash(t))
  270. {
  271. lua_pushnil(); lua_pushnil();
  272. return;
  273. }
  274. }
  275. luaI_pushobject(ref(node(t,i)));
  276. luaI_pushobject(val(node(t,i)));
  277. }
  278. void lua_next (void)
  279. {
  280. Hash *t;
  281. lua_Object o = lua_getparam(1);
  282. lua_Object r = lua_getparam(2);
  283. if (o == LUA_NOOBJECT || r == LUA_NOOBJECT)
  284. lua_error ("too few arguments to function `next'");
  285. if (lua_getparam(3) != LUA_NOOBJECT)
  286. lua_error ("too many arguments to function `next'");
  287. if (!lua_istable(o))
  288. lua_error ("first argument of function `next' is not a table");
  289. t = avalue(luaI_Address(o));
  290. if (lua_isnil(r))
  291. {
  292. hashnext(t, 0);
  293. }
  294. else
  295. {
  296. Word h = present (t, luaI_Address(r));
  297. hashnext(t, h+1);
  298. }
  299. }