hash.c 6.4 KB

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