hash.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. ** hash.c
  3. ** hash manager for lua
  4. */
  5. char *rcs_hash="$Id: hash.c,v 2.13 1994/11/07 15:19:51 roberto Exp roberto $";
  6. #include <string.h>
  7. #include <stdlib.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 new(s) ((s *)malloc(sizeof(s)))
  15. #define newvector(n,s) ((s *)calloc(n,sizeof(s)))
  16. #define nhash(t) ((t)->nhash)
  17. #define nuse(t) ((t)->nuse)
  18. #define markarray(t) ((t)->mark)
  19. #define nodevector(t) ((t)->node)
  20. #define node(t,i) (&(t)->node[i])
  21. #define ref(n) (&(n)->ref)
  22. #define val(n) (&(n)->val)
  23. #define REHASH_LIMIT 0.70 /* avoid more than this % full */
  24. static Hash *listhead = NULL;
  25. /* hash dimensions values */
  26. static int dimensions[] =
  27. {3, 5, 7, 11, 23, 47, 97, 197, 397, 797, 1597, 3203, 6421,
  28. 12853, 25717, 51437, 0};
  29. static int redimension (int nhash)
  30. {
  31. int i;
  32. for (i=0; dimensions[i]!=0; i++)
  33. {
  34. if (dimensions[i] > nhash)
  35. return dimensions[i];
  36. }
  37. return nhash*2+1;
  38. }
  39. static int hashindex (Hash *t, Object *ref) /* hash function */
  40. {
  41. switch (tag(ref))
  42. {
  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. case LUA_T_USERDATA:
  64. return (((int)uvalue(ref))%nhash(t));
  65. default:
  66. lua_reportbug ("unexpected type to index table");
  67. return -1; /* UNREACHEABLE */
  68. }
  69. }
  70. static int equalObj (Object *t1, Object *t2)
  71. {
  72. if (tag(t1) != tag(t2)) return 0;
  73. switch (tag(t1))
  74. {
  75. case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
  76. case LUA_T_STRING: return streq(svalue(t1), svalue(t2));
  77. default: return uvalue(t1) == uvalue(t2);
  78. }
  79. }
  80. static int present (Hash *t, Object *ref)
  81. {
  82. int h = hashindex(t, ref);
  83. while (tag(ref(node(t, h))) != LUA_T_NIL)
  84. {
  85. if (equalObj(ref, ref(node(t, h))))
  86. return h;
  87. h = (h+1) % nhash(t);
  88. }
  89. return h;
  90. }
  91. /*
  92. ** Alloc a vector node
  93. */
  94. static Node *hashnodecreate (int nhash)
  95. {
  96. int i;
  97. Node *v = newvector (nhash, Node);
  98. if (v == NULL)
  99. lua_error ("not enough memory");
  100. for (i=0; i<nhash; i++)
  101. tag(ref(&v[i])) = LUA_T_NIL;
  102. return v;
  103. }
  104. /*
  105. ** Create a new hash. Return the hash pointer or NULL on error.
  106. */
  107. static Hash *hashcreate (int nhash)
  108. {
  109. Hash *t = new (Hash);
  110. if (t == NULL)
  111. lua_error ("not enough memory");
  112. nhash = redimension((int)((float)nhash/REHASH_LIMIT));
  113. nodevector(t) = hashnodecreate(nhash);
  114. if (nodevector(t) == NULL)
  115. lua_error ("not enough memory");
  116. nhash(t) = nhash;
  117. nuse(t) = 0;
  118. markarray(t) = 0;
  119. return t;
  120. }
  121. /*
  122. ** Delete a hash
  123. */
  124. static void hashdelete (Hash *t)
  125. {
  126. free (nodevector(t));
  127. free(t);
  128. }
  129. /*
  130. ** Mark a hash and check its elements
  131. */
  132. void lua_hashmark (Hash *h)
  133. {
  134. if (markarray(h) == 0)
  135. {
  136. int i;
  137. markarray(h) = 1;
  138. for (i=0; i<nhash(h); i++)
  139. {
  140. Node *n = node(h,i);
  141. if (tag(ref(n)) != LUA_T_NIL)
  142. {
  143. lua_markobject(&n->ref);
  144. lua_markobject(&n->val);
  145. }
  146. }
  147. }
  148. }
  149. /*
  150. ** Garbage collection to arrays
  151. ** Delete all unmarked arrays.
  152. */
  153. void lua_hashcollector (void)
  154. {
  155. Hash *curr_array = listhead, *prev = NULL;
  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. ++lua_recovered;
  165. }
  166. else
  167. {
  168. markarray(curr_array) = 0;
  169. prev = curr_array;
  170. }
  171. curr_array = next;
  172. }
  173. }
  174. /*
  175. ** Create a new array
  176. ** This function inserts the new array in the array list. It also
  177. ** executes garbage collection if the number of arrays created
  178. ** exceed a pre-defined range.
  179. */
  180. Hash *lua_createarray (int nhash)
  181. {
  182. Hash *array = hashcreate(nhash);
  183. if (lua_nentity == lua_block)
  184. lua_pack();
  185. lua_nentity++;
  186. array->next = listhead;
  187. listhead = array;
  188. return array;
  189. }
  190. /*
  191. ** Re-hash
  192. */
  193. static void rehash (Hash *t)
  194. {
  195. int i;
  196. int nold = nhash(t);
  197. Node *vold = nodevector(t);
  198. nhash(t) = 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. 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. int 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. int 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, int 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 == 0 || r == 0)
  272. lua_error ("too few arguments to function `next'");
  273. if (lua_getparam(3) != 0)
  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. int h = present (t, luaI_Address(r));
  285. hashnext(t, h+1);
  286. }
  287. }