hash.c 6.7 KB

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