ltable.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. ** $Id: ltable.c,v 1.1 1997/09/16 19:25:59 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 nuse(t) ((t)->nuse)
  13. #define nodevector(t) ((t)->node)
  14. #define val(n) (&(n)->val)
  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_CFUNCTION:
  32. h = (IntPoint)fvalue(ref);
  33. break;
  34. case LUA_T_ARRAY:
  35. h = (IntPoint)avalue(ref);
  36. break;
  37. default:
  38. lua_error("unexpected type to index table");
  39. h = 0; /* UNREACHEABLE */
  40. }
  41. return (h >= 0 ? h : -(h+1));
  42. }
  43. static int present (Hash *t, TObject *key)
  44. {
  45. int tsize = nhash(t);
  46. long int h = hashindex(key);
  47. int h1 = h%tsize;
  48. TObject *rf = ref(node(t, h1));
  49. if (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf)) {
  50. int h2 = h%(tsize-2) + 1;
  51. do {
  52. h1 = (h1+h2)%tsize;
  53. rf = ref(node(t, h1));
  54. } while (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf));
  55. }
  56. return h1;
  57. }
  58. /*
  59. ** Alloc a vector node
  60. */
  61. static Node *hashnodecreate (int nhash)
  62. {
  63. int i;
  64. Node *v = luaM_newvector (nhash, Node);
  65. for (i=0; i<nhash; i++)
  66. ttype(ref(&v[i])) = LUA_T_NIL;
  67. return v;
  68. }
  69. /*
  70. ** Delete a hash
  71. */
  72. static void hashdelete (Hash *t)
  73. {
  74. luaM_free (nodevector(t));
  75. luaM_free(t);
  76. }
  77. void luaH_free (Hash *frees)
  78. {
  79. while (frees) {
  80. Hash *next = (Hash *)frees->head.next;
  81. hashdelete(frees);
  82. frees = next;
  83. }
  84. }
  85. Hash *luaH_new (int nhash)
  86. {
  87. Hash *t = luaM_new(Hash);
  88. nhash = luaO_redimension((int)((float)nhash/REHASH_LIMIT));
  89. nodevector(t) = hashnodecreate(nhash);
  90. nhash(t) = nhash;
  91. nuse(t) = 0;
  92. t->htag = TagDefault;
  93. luaO_insertlist(&luaH_root, (GCnode *)t);
  94. return t;
  95. }
  96. /*
  97. ** Rehash:
  98. ** Check if table has deleted slots. It it has, it does not need to
  99. ** grow, since rehash will reuse them.
  100. */
  101. static int emptyslots (Hash *t)
  102. {
  103. int i;
  104. for (i=nhash(t)-1; i>=0; i--) {
  105. Node *n = node(t, i);
  106. if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) == LUA_T_NIL)
  107. return 1;
  108. }
  109. return 0;
  110. }
  111. static void rehash (Hash *t)
  112. {
  113. int nold = nhash(t);
  114. Node *vold = nodevector(t);
  115. int i;
  116. if (!emptyslots(t))
  117. nhash(t) = luaO_redimension(nhash(t));
  118. nodevector(t) = hashnodecreate(nhash(t));
  119. for (i=0; i<nold; i++) {
  120. Node *n = vold+i;
  121. if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL)
  122. *node(t, present(t, ref(n))) = *n; /* copy old node to luaM_new hash */
  123. }
  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. }