hash.c 6.4 KB

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