ltable.c 4.2 KB

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