ltable.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. ** $Id: ltable.c,v 1.10 1998/01/09 14:44:55 roberto Exp roberto $
  3. ** Lua tables (hash)
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include "lauxlib.h"
  8. #include "lmem.h"
  9. #include "lobject.h"
  10. #include "lstate.h"
  11. #include "ltable.h"
  12. #include "lua.h"
  13. #define gcsize(n) (1+(n/16))
  14. #define nuse(t) ((t)->nuse)
  15. #define nodevector(t) ((t)->node)
  16. #define REHASH_LIMIT 0.70 /* avoid more than this % full */
  17. #define TagDefault LUA_T_ARRAY;
  18. static long int hashindex (TObject *ref)
  19. {
  20. long int h;
  21. switch (ttype(ref)) {
  22. case LUA_T_NUMBER:
  23. h = (long int)nvalue(ref);
  24. break;
  25. case LUA_T_STRING: case LUA_T_USERDATA:
  26. h = (IntPoint)tsvalue(ref);
  27. break;
  28. case LUA_T_ARRAY:
  29. h = (IntPoint)avalue(ref);
  30. break;
  31. case LUA_T_PROTO:
  32. h = (IntPoint)tfvalue(ref);
  33. break;
  34. case LUA_T_CPROTO:
  35. h = (IntPoint)fvalue(ref);
  36. break;
  37. case LUA_T_CLOSURE:
  38. h = (IntPoint)clvalue(ref);
  39. break;
  40. default:
  41. lua_error("unexpected type to index table");
  42. h = 0; /* to avoid warnings */
  43. }
  44. return (h >= 0 ? h : -(h+1));
  45. }
  46. static int present (Hash *t, TObject *key)
  47. {
  48. int tsize = nhash(t);
  49. long int h = hashindex(key);
  50. int h1 = h%tsize;
  51. TObject *rf = ref(node(t, h1));
  52. if (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf)) {
  53. int h2 = h%(tsize-2) + 1;
  54. do {
  55. h1 += h2;
  56. if (h1 >= tsize) h1 -= tsize;
  57. rf = ref(node(t, h1));
  58. } while (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf));
  59. }
  60. return h1;
  61. }
  62. /*
  63. ** Alloc a vector node
  64. */
  65. static Node *hashnodecreate (int nhash)
  66. {
  67. Node *v = luaM_newvector(nhash, Node);
  68. int i;
  69. for (i=0; i<nhash; i++)
  70. ttype(ref(&v[i])) = LUA_T_NIL;
  71. return v;
  72. }
  73. /*
  74. ** Delete a hash
  75. */
  76. static void hashdelete (Hash *t)
  77. {
  78. luaM_free(nodevector(t));
  79. luaM_free(t);
  80. }
  81. void luaH_free (Hash *frees)
  82. {
  83. while (frees) {
  84. Hash *next = (Hash *)frees->head.next;
  85. L->nblocks -= gcsize(frees->nhash);
  86. hashdelete(frees);
  87. frees = next;
  88. }
  89. }
  90. Hash *luaH_new (int nhash)
  91. {
  92. Hash *t = luaM_new(Hash);
  93. nhash = luaO_redimension((int)((float)nhash/REHASH_LIMIT));
  94. nodevector(t) = hashnodecreate(nhash);
  95. nhash(t) = nhash;
  96. nuse(t) = 0;
  97. t->htag = TagDefault;
  98. luaO_insertlist(&(L->roottable), (GCnode *)t);
  99. L->nblocks += gcsize(nhash);
  100. return t;
  101. }
  102. /*
  103. ** Rehash:
  104. ** Check if table has deleted slots. It it has, it does not need to
  105. ** grow, since rehash will reuse them.
  106. */
  107. static int emptyslots (Hash *t)
  108. {
  109. int i;
  110. for (i=nhash(t)-1; i>=0; i--) {
  111. Node *n = node(t, i);
  112. if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) == LUA_T_NIL)
  113. return 1;
  114. }
  115. return 0;
  116. }
  117. static void rehash (Hash *t)
  118. {
  119. int nold = nhash(t);
  120. Node *vold = nodevector(t);
  121. int i;
  122. if (!emptyslots(t))
  123. nhash(t) = luaO_redimension(nhash(t));
  124. nodevector(t) = hashnodecreate(nhash(t));
  125. for (i=0; i<nold; i++) {
  126. Node *n = vold+i;
  127. if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL)
  128. *node(t, present(t, ref(n))) = *n; /* copy old node to luaM_new hash */
  129. }
  130. L->nblocks += gcsize(t->nhash)-gcsize(nold);
  131. luaM_free(vold);
  132. }
  133. /*
  134. ** If the hash node is present, return its pointer, otherwise return
  135. ** null.
  136. */
  137. TObject *luaH_get (Hash *t, TObject *ref)
  138. {
  139. int h = present(t, ref);
  140. if (ttype(ref(node(t, h))) != LUA_T_NIL) return val(node(t, h));
  141. else return NULL;
  142. }
  143. /*
  144. ** If the hash node is present, return its pointer, otherwise create a luaM_new
  145. ** node for the given reference and also return its pointer.
  146. */
  147. TObject *luaH_set (Hash *t, TObject *ref)
  148. {
  149. Node *n = node(t, present(t, ref));
  150. if (ttype(ref(n)) == LUA_T_NIL) {
  151. nuse(t)++;
  152. if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT) {
  153. rehash(t);
  154. n = node(t, present(t, ref));
  155. }
  156. *ref(n) = *ref;
  157. ttype(val(n)) = LUA_T_NIL;
  158. }
  159. return (val(n));
  160. }
  161. static Node *hashnext (Hash *t, int i)
  162. {
  163. Node *n;
  164. int tsize = nhash(t);
  165. if (i >= tsize)
  166. return NULL;
  167. n = node(t, i);
  168. while (ttype(ref(n)) == LUA_T_NIL || ttype(val(n)) == LUA_T_NIL) {
  169. if (++i >= tsize)
  170. return NULL;
  171. n = node(t, i);
  172. }
  173. return node(t, i);
  174. }
  175. Node *luaH_next (TObject *o, TObject *r)
  176. {
  177. Hash *t = avalue(o);
  178. if (ttype(r) == LUA_T_NIL)
  179. return hashnext(t, 0);
  180. else {
  181. int i = present(t, r);
  182. Node *n = node(t, i);
  183. luaL_arg_check(ttype(ref(n))!=LUA_T_NIL && ttype(val(n))!=LUA_T_NIL,
  184. 2, "key not found");
  185. return hashnext(t, i+1);
  186. }
  187. }