hash.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. ** hash.c
  3. ** hash manager for lua
  4. */
  5. char *rcs_hash="$Id: hash.c,v 2.40 1997/04/04 15:35:37 roberto Exp roberto $";
  6. #include "luamem.h"
  7. #include "opcode.h"
  8. #include "hash.h"
  9. #include "table.h"
  10. #include "lua.h"
  11. #include "auxlib.h"
  12. #define nhash(t) ((t)->nhash)
  13. #define nuse(t) ((t)->nuse)
  14. #define markarray(t) ((t)->mark)
  15. #define nodevector(t) ((t)->node)
  16. #define node(t,i) (&(t)->node[i])
  17. #define ref(n) (&(n)->ref)
  18. #define val(n) (&(n)->val)
  19. #define REHASH_LIMIT 0.70 /* avoid more than this % full */
  20. #define TagDefault LUA_T_ARRAY;
  21. static Hash *listhead = NULL;
  22. /* hash dimensions values */
  23. static Long dimensions[] =
  24. {3L, 5L, 7L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L,
  25. 12853L, 25717L, 51437L, 102811L, 205619L, 411233L, 822433L,
  26. 1644817L, 3289613L, 6579211L, 13158023L, MAX_INT};
  27. int luaI_redimension (int nhash)
  28. {
  29. int i;
  30. for (i=0; dimensions[i]<MAX_INT; 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 int hashindex (Hash *t, TObject *ref) /* hash function */
  39. {
  40. long int h;
  41. switch (ttype(ref)) {
  42. case LUA_T_NUMBER:
  43. h = (long int)nvalue(ref); break;
  44. case LUA_T_STRING: case LUA_T_USERDATA:
  45. h = tsvalue(ref)->hash; break;
  46. case LUA_T_FUNCTION:
  47. h = (IntPoint)ref->value.tf; break;
  48. case LUA_T_CFUNCTION:
  49. h = (IntPoint)fvalue(ref); break;
  50. case LUA_T_ARRAY:
  51. h = (IntPoint)avalue(ref); break;
  52. default:
  53. lua_error ("unexpected type to index table");
  54. h = 0; /* UNREACHEABLE */
  55. }
  56. if (h < 0) h = -h;
  57. return h%nhash(t); /* make it a valid index */
  58. }
  59. int lua_equalObj (TObject *t1, TObject *t2)
  60. {
  61. if (ttype(t1) != ttype(t2)) return 0;
  62. switch (ttype(t1))
  63. {
  64. case LUA_T_NIL: return 1;
  65. case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
  66. case LUA_T_STRING: case LUA_T_USERDATA: 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:
  71. lua_error("internal error in `lua_equalObj'");
  72. return 0; /* UNREACHEABLE */
  73. }
  74. }
  75. static int present (Hash *t, TObject *ref)
  76. {
  77. int h = hashindex(t, ref);
  78. while (ttype(ref(node(t, h))) != LUA_T_NIL)
  79. {
  80. if (lua_equalObj(ref, ref(node(t, h))))
  81. return h;
  82. h = (h+1) % nhash(t);
  83. }
  84. return h;
  85. }
  86. /*
  87. ** Alloc a vector node
  88. */
  89. static Node *hashnodecreate (int nhash)
  90. {
  91. int i;
  92. Node *v = newvector (nhash, Node);
  93. for (i=0; i<nhash; i++)
  94. ttype(ref(&v[i])) = LUA_T_NIL;
  95. return v;
  96. }
  97. /*
  98. ** Create a new hash. Return the hash pointer or NULL on error.
  99. */
  100. static Hash *hashcreate (int nhash)
  101. {
  102. Hash *t = new(Hash);
  103. nhash = luaI_redimension((int)((float)nhash/REHASH_LIMIT));
  104. nodevector(t) = hashnodecreate(nhash);
  105. nhash(t) = nhash;
  106. nuse(t) = 0;
  107. markarray(t) = 0;
  108. t->htag = TagDefault;
  109. return t;
  110. }
  111. /*
  112. ** Delete a hash
  113. */
  114. static void hashdelete (Hash *t)
  115. {
  116. luaI_free (nodevector(t));
  117. luaI_free(t);
  118. }
  119. /*
  120. ** Mark a hash and check its elements
  121. */
  122. void lua_hashmark (Hash *h)
  123. {
  124. if (markarray(h) == 0)
  125. {
  126. int i;
  127. markarray(h) = 1;
  128. for (i=0; i<nhash(h); i++)
  129. {
  130. Node *n = node(h,i);
  131. if (ttype(ref(n)) != LUA_T_NIL)
  132. {
  133. lua_markobject(&n->ref);
  134. lua_markobject(&n->val);
  135. }
  136. }
  137. }
  138. }
  139. void luaI_hashcallIM (void)
  140. {
  141. Hash *curr_array;
  142. TObject t;
  143. ttype(&t) = LUA_T_ARRAY;
  144. for (curr_array = listhead; curr_array; curr_array = curr_array->next)
  145. if (markarray(curr_array) != 1)
  146. {
  147. avalue(&t) = curr_array;
  148. luaI_gcIM(&t);
  149. }
  150. }
  151. /*
  152. ** Garbage collection to arrays
  153. ** Delete all unmarked arrays.
  154. */
  155. Long lua_hashcollector (void)
  156. {
  157. Hash *curr_array = listhead, *prev = NULL;
  158. Long counter = 0;
  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 (ttype(ref(n)) != LUA_T_NIL && ttype(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. TObject *lua_hashget (Hash *t, TObject *ref)
  216. {
  217. int h = present(t, ref);
  218. if (ttype(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. TObject *lua_hashdefine (Hash *t, TObject *ref)
  226. {
  227. int h;
  228. Node *n;
  229. h = present(t, ref);
  230. n = node(t, h);
  231. if (ttype(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. ttype(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 (ttype(ref(node(t,i))) == LUA_T_NIL ||
  256. ttype(val(node(t,i))) == LUA_T_NIL)
  257. {
  258. if (++i >= nhash(t))
  259. return;
  260. }
  261. luaI_pushobject(ref(node(t,i)));
  262. luaI_pushobject(val(node(t,i)));
  263. }
  264. void lua_next (void)
  265. {
  266. Hash *t;
  267. lua_Object o = lua_getparam(1);
  268. lua_Object r = lua_getparam(2);
  269. luaL_arg_check(lua_istable(o), 1, "table expected");
  270. luaL_arg_check(r != LUA_NOOBJECT, 2, "value expected");
  271. t = avalue(luaI_Address(o));
  272. if (lua_isnil(r))
  273. {
  274. hashnext(t, 0);
  275. }
  276. else
  277. {
  278. int h = present (t, luaI_Address(r));
  279. hashnext(t, h+1);
  280. }
  281. }