hash.c 6.5 KB

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