hash.c 6.7 KB

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