hash.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. ** hash.c
  3. ** hash manager for lua
  4. */
  5. char *rcs_hash="$Id: hash.c,v 2.6 1994/08/17 17:41:23 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 || (*(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. if (tag(ref) == T_NUMBER && tag(ref(node(t, h))) == T_NUMBER &&
  82. nvalue(ref) == nvalue(ref(node(t, h)))
  83. ) return h;
  84. if (tag(ref) == T_STRING && tag(ref(node(t, h))) == T_STRING &&
  85. streq(svalue(ref),svalue(ref(node(t, h))))
  86. ) return h;
  87. if (tag(ref) == tag(ref(node(t, h))) &&
  88. uvalue(ref) == uvalue(ref(node(t, h))) /* all others are pointers */
  89. ) return h;
  90. h = (h+1) % nhash(t);
  91. }
  92. return h;
  93. }
  94. /*
  95. ** Alloc a vector node
  96. */
  97. static Node *hashnodecreate (int nhash)
  98. {
  99. int i;
  100. Node *v = newvector (nhash, Node);
  101. if (v == NULL)
  102. {
  103. lua_error ("not enough memory");
  104. return NULL;
  105. }
  106. for (i=0; i<nhash; i++)
  107. tag(ref(&v[i])) = T_NIL;
  108. return v;
  109. }
  110. /*
  111. ** Create a new hash. Return the hash pointer or NULL on error.
  112. */
  113. static Hash *hashcreate (int nhash)
  114. {
  115. Hash *t = new (Hash);
  116. if (t == NULL)
  117. {
  118. lua_error ("not enough memory");
  119. return NULL;
  120. }
  121. nhash = redimension(nhash);
  122. nodevector(t) = hashnodecreate(nhash);
  123. if (nodevector(t) == NULL)
  124. return NULL;
  125. nhash(t) = nhash;
  126. nuse(t) = 0;
  127. markarray(t) = 0;
  128. return t;
  129. }
  130. /*
  131. ** Delete a hash
  132. */
  133. static void hashdelete (Hash *t)
  134. {
  135. free (nodevector(t));
  136. free(t);
  137. }
  138. /*
  139. ** Mark a hash and check its elements
  140. */
  141. void lua_hashmark (Hash *h)
  142. {
  143. if (markarray(h) == 0)
  144. {
  145. int i;
  146. markarray(h) = 1;
  147. for (i=0; i<nhash(h); i++)
  148. {
  149. Node *n = node(h,i);
  150. if (tag(ref(n)) != T_NIL)
  151. {
  152. lua_markobject(&n->ref);
  153. lua_markobject(&n->val);
  154. }
  155. }
  156. }
  157. }
  158. /*
  159. ** Garbage collection to arrays
  160. ** Delete all unmarked arrays.
  161. */
  162. void lua_hashcollector (void)
  163. {
  164. ArrayList *curr = listhead, *prev = NULL;
  165. while (curr != NULL)
  166. {
  167. ArrayList *next = curr->next;
  168. if (markarray(curr->array) != 1)
  169. {
  170. if (prev == NULL) listhead = next;
  171. else prev->next = next;
  172. hashdelete(curr->array);
  173. free(curr);
  174. }
  175. else
  176. {
  177. markarray(curr->array) = 0;
  178. prev = curr;
  179. }
  180. curr = next;
  181. }
  182. }
  183. /*
  184. ** Create a new array
  185. ** This function insert the new array at array list. It also
  186. ** execute garbage collection if the number of array created
  187. ** exceed a pre-defined range.
  188. */
  189. Hash *lua_createarray (int nhash)
  190. {
  191. ArrayList *new = new(ArrayList);
  192. if (new == NULL)
  193. {
  194. lua_error ("not enough memory");
  195. return NULL;
  196. }
  197. new->array = hashcreate(nhash);
  198. if (new->array == NULL)
  199. {
  200. lua_error ("not enough memory");
  201. return NULL;
  202. }
  203. if (lua_nentity == lua_block)
  204. lua_pack();
  205. lua_nentity++;
  206. new->next = listhead;
  207. listhead = new;
  208. return new->array;
  209. }
  210. /*
  211. ** Re-hash
  212. */
  213. static void rehash (Hash *t)
  214. {
  215. int i;
  216. int nold = nhash(t);
  217. Node *vold = nodevector(t);
  218. nhash(t) = redimension(nhash(t));
  219. nodevector(t) = hashnodecreate(nhash(t));
  220. for (i=0; i<nold; i++)
  221. {
  222. Node *n = vold+i;
  223. if (tag(ref(n)) != T_NIL && tag(val(n)) != T_NIL)
  224. *node(t, present(t, ref(n))) = *n; /* copy old node to new hahs */
  225. }
  226. free(vold);
  227. }
  228. /*
  229. ** If the hash node is present, return its pointer, otherwise search
  230. ** the node at parent table, recursively, if there is parent.
  231. ** If no parent and the node is not present, return a static nil object.
  232. */
  233. Object *lua_hashget (Hash *t, Object *ref)
  234. {
  235. static int count = 1000;
  236. static Object nil_obj = {T_NIL, {NULL}};
  237. int h = present(t, ref);
  238. if (h < 0) return NULL;
  239. if (tag(ref(node(t, h))) != T_NIL) return val(node(t, h));
  240. if (--count == 0)
  241. {
  242. lua_reportbug ("hierarchy too deep (maybe there is an inheritance loop)");
  243. return &nil_obj;
  244. }
  245. { /* check "parent" field */
  246. Hash *p;
  247. Object parent;
  248. tag(&parent) = T_STRING;
  249. svalue(&parent) = "parent";
  250. h = present(t, &parent); /* assert(h >= 0); */
  251. p = tag(ref(node(t, h))) != T_NIL && tag(val(node(t, h))) == T_ARRAY ?
  252. avalue(val(node(t, h))) : NULL;
  253. if (p != NULL)
  254. {
  255. Object *r = lua_hashget(p, ref);
  256. if (tag(r) != T_NIL) { count++; return r; }
  257. }
  258. }
  259. { /* check "parents" field */
  260. Hash *ps;
  261. Object parents;
  262. tag(&parents) = T_STRING;
  263. svalue(&parents) = "parents";
  264. h = present(t, &parents); /* assert(h >= 0); */
  265. ps = tag(ref(node(t, h))) != T_NIL && tag(val(node(t, h))) == T_ARRAY ?
  266. avalue(val(node(t, h))) : NULL;
  267. if (ps != NULL)
  268. {
  269. Hash *p;
  270. Object index;
  271. tag(&index) = T_NUMBER;
  272. nvalue(&index) = 1;
  273. h = present(ps, &index);
  274. p = tag(ref(node(ps, h))) != T_NIL && tag(val(node(ps, h))) == T_ARRAY ?
  275. avalue(val(node(ps, h))) : NULL;
  276. while (p != NULL)
  277. {
  278. Object *r = lua_hashget(p, ref);
  279. if (tag(r) != T_NIL) { count++; return r; }
  280. nvalue(&index)++;
  281. h = present(ps, &index);
  282. p = tag(ref(node(ps, h))) != T_NIL && tag(val(node(ps, h))) == T_ARRAY ?
  283. avalue(val(node(ps, h))) : NULL;
  284. }
  285. }
  286. }
  287. count++;
  288. return &nil_obj;
  289. }
  290. /*
  291. ** If the hash node is present, return its pointer, otherwise create a new
  292. ** node for the given reference and also return its pointer.
  293. ** On error, return NULL.
  294. */
  295. Object *lua_hashdefine (Hash *t, Object *ref)
  296. {
  297. int h;
  298. Node *n;
  299. h = present(t, ref);
  300. if (h < 0) return NULL;
  301. n = node(t, h);
  302. if (tag(ref(n)) == T_NIL)
  303. {
  304. nuse(t)++;
  305. if (nuse(t) > nhash(t)*REHASH_LIMIT)
  306. {
  307. rehash(t);
  308. h = present(t, ref);
  309. n = node(t, h);
  310. }
  311. *ref(n) = *ref;
  312. tag(val(n)) = T_NIL;
  313. }
  314. return (val(n));
  315. }
  316. /*
  317. ** Internal function to manipulate arrays.
  318. ** Given an array object and a reference value, return the next element
  319. ** in the hash.
  320. ** This function pushs the element value and its reference to the stack.
  321. */
  322. static void hashnext (Hash *t, int i)
  323. {
  324. if (i >= nhash(t))
  325. {
  326. lua_pushnil(); lua_pushnil();
  327. return;
  328. }
  329. while (tag(ref(node(t,i))) == T_NIL || tag(val(node(t,i))) == T_NIL)
  330. {
  331. if (++i >= nhash(t))
  332. {
  333. lua_pushnil(); lua_pushnil();
  334. return;
  335. }
  336. }
  337. lua_pushobject(ref(node(t,i)));
  338. lua_pushobject(val(node(t,i)));
  339. }
  340. void lua_next (void)
  341. {
  342. Hash *t;
  343. Object *o = lua_getparam (1);
  344. Object *r = lua_getparam (2);
  345. if (o == NULL || r == NULL)
  346. { lua_error ("too few arguments to function `next'"); return; }
  347. if (lua_getparam (3) != NULL)
  348. { lua_error ("too many arguments to function `next'"); return; }
  349. if (tag(o) != T_ARRAY)
  350. { lua_error ("first argument of function `next' is not a table"); return; }
  351. t = avalue(o);
  352. if (tag(r) == T_NIL)
  353. {
  354. hashnext(t, 0);
  355. }
  356. else
  357. {
  358. int h = present (t, r);
  359. if (h >= 0)
  360. hashnext(t, h+1);
  361. else
  362. lua_error ("error in function 'next': reference not found");
  363. }
  364. }