ltable.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. ** $Id: ltable.c,v 1.1 1997/08/14 20:19:10 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. Hash *luaH_root = NULL;
  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. static void inserttable (Hash *table)
  86. {
  87. ++luaO_nentities;
  88. table->head.next = (GCnode *)luaH_root;
  89. luaH_root = table;
  90. table->head.marked = 0;
  91. }
  92. Hash *luaH_new (int nhash)
  93. {
  94. Hash *t = luaM_new(Hash);
  95. nhash = luaO_redimension((int)((float)nhash/REHASH_LIMIT));
  96. nodevector(t) = hashnodecreate(nhash);
  97. nhash(t) = nhash;
  98. nuse(t) = 0;
  99. t->htag = TagDefault;
  100. inserttable(t);
  101. return t;
  102. }
  103. /*
  104. ** Rehash:
  105. ** Check if table has deleted slots. It it has, it does not need to
  106. ** grow, since rehash will reuse them.
  107. */
  108. static int emptyslots (Hash *t)
  109. {
  110. int i;
  111. for (i=nhash(t)-1; i>=0; i--) {
  112. Node *n = node(t, i);
  113. if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) == LUA_T_NIL)
  114. return 1;
  115. }
  116. return 0;
  117. }
  118. static void rehash (Hash *t)
  119. {
  120. int nold = nhash(t);
  121. Node *vold = nodevector(t);
  122. int i;
  123. if (!emptyslots(t))
  124. nhash(t) = luaO_redimension(nhash(t));
  125. nodevector(t) = hashnodecreate(nhash(t));
  126. for (i=0; i<nold; i++) {
  127. Node *n = vold+i;
  128. if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL)
  129. *node(t, present(t, ref(n))) = *n; /* copy old node to luaM_new hash */
  130. }
  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. }