ltable.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. ** $Id: ltable.c,v 1.83 2001/07/05 20:31:14 roberto Exp roberto $
  3. ** Lua tables (hash)
  4. ** See Copyright Notice in lua.h
  5. */
  6. /*
  7. ** Implementation of tables (aka arrays, objects, or hash tables);
  8. ** uses a mix of chained scatter table with Brent's variation.
  9. ** A main invariant of these tables is that, if an element is not
  10. ** in its main position (i.e. the `original' position that its hash gives
  11. ** to it), then the colliding element is in its own main position.
  12. ** In other words, there are collisions only when two elements have the
  13. ** same main position (i.e. the same hash values for that table size).
  14. ** Because of that, the load factor of these tables can be 100% without
  15. ** performance penalties.
  16. */
  17. #define LUA_PRIVATE
  18. #include "lua.h"
  19. #include "ldo.h"
  20. #include "lmem.h"
  21. #include "lobject.h"
  22. #include "lstate.h"
  23. #include "ltable.h"
  24. #define TagDefault LUA_TTABLE
  25. #define hashnum(t,n) (node(t, lmod(cast(lu_hash, cast(ls_hash, n)), t->size)))
  26. #define hashstr(t,str) (node(t, lmod((str)->tsv.hash, t->size)))
  27. #define hashpointer(t,p) (node(t, lmod(IntPoint(p), t->size)))
  28. /*
  29. ** returns the `main' position of an element in a table (that is, the index
  30. ** of its hash value)
  31. */
  32. Node *luaH_mainposition (const Hash *t, const TObject *key) {
  33. switch (ttype(key)) {
  34. case LUA_TNUMBER:
  35. return hashnum(t, nvalue(key));
  36. case LUA_TSTRING:
  37. return hashstr(t, tsvalue(key));
  38. default: /* all other types are hashed as (void *) */
  39. return hashpointer(t, tsvalue(key));
  40. }
  41. }
  42. Node *luaH_next (lua_State *L, Hash *t, const TObject *key) {
  43. int i;
  44. if (ttype(key) == LUA_TNIL)
  45. i = 0; /* first iteration */
  46. else {
  47. const TObject *v = luaH_get(t, key);
  48. if (v == &luaO_nilobject)
  49. luaD_error(L, l_s("invalid key for `next'"));
  50. i = cast(int, (cast(const lu_byte *, v) -
  51. cast(const lu_byte *, val(node(t, 0)))) / sizeof(Node)) + 1;
  52. }
  53. for (; i<t->size; i++) {
  54. Node *n = node(t, i);
  55. if (ttype(val(n)) != LUA_TNIL)
  56. return n;
  57. }
  58. return NULL; /* no more elements */
  59. }
  60. int luaH_nexti (Hash *t, int i) {
  61. while ((++i)<t->size) {
  62. if (ttype(val(node(t, i))) != LUA_TNIL) /* a non-nil value? */
  63. return i;
  64. }
  65. return -1; /* no more elements */
  66. }
  67. #define check_grow(L, p, n) \
  68. if ((p) >= MAX_INT/(n)) luaD_error(L, l_s("table overflow"));
  69. /*
  70. ** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
  71. */
  72. static int power2 (lua_State *L, int n) {
  73. int p = MINPOWER2;
  74. while (p <= n) {
  75. check_grow(L, p, 2);
  76. p *= 2;
  77. }
  78. return p;
  79. }
  80. static void setnodevector (lua_State *L, Hash *t, int size) {
  81. int i;
  82. t->node = luaM_newvector(L, size, Node);
  83. for (i=0; i<size; i++) {
  84. t->node[i].next = NULL;
  85. setnilvalue(key(node(t, i)));
  86. setnilvalue(val(node(t, i)));
  87. }
  88. t->size = size;
  89. t->firstfree = node(t, size-1); /* first free position to be used */
  90. }
  91. Hash *luaH_new (lua_State *L, int size) {
  92. Hash *t = luaM_new(L, Hash);
  93. t->htag = TagDefault;
  94. t->next = G(L)->roottable;
  95. G(L)->roottable = t;
  96. t->mark = t;
  97. t->size = 0;
  98. t->weakmode = 0;
  99. t->node = NULL;
  100. setnodevector(L, t, power2(L, size));
  101. return t;
  102. }
  103. void luaH_free (lua_State *L, Hash *t) {
  104. luaM_freearray(L, t->node, t->size, Node);
  105. luaM_freelem(L, t);
  106. }
  107. static int numuse (const Hash *t) {
  108. Node *v = t->node;
  109. int size = t->size;
  110. int realuse = 0;
  111. int i;
  112. for (i=0; i<size; i++) {
  113. if (ttype(&v[i].val) != LUA_TNIL)
  114. realuse++;
  115. }
  116. return realuse;
  117. }
  118. static void rehash (lua_State *L, Hash *t) {
  119. int oldsize = t->size;
  120. Node *nold = t->node;
  121. int nelems = numuse(t);
  122. int i;
  123. lua_assert(nelems<=oldsize);
  124. if (nelems >= oldsize-oldsize/4) { /* using more than 3/4? */
  125. check_grow(L, oldsize, 2);
  126. setnodevector(L, t, oldsize*2); /* grow array */
  127. }
  128. else if (nelems <= oldsize/4 && /* less than 1/4? */
  129. oldsize > MINPOWER2)
  130. setnodevector(L, t, oldsize/2); /* shrink array */
  131. else
  132. setnodevector(L, t, oldsize); /* just rehash; keep the same size */
  133. for (i=0; i<oldsize; i++) {
  134. Node *old = nold+i;
  135. if (ttype(val(old)) != LUA_TNIL)
  136. luaH_set(L, t, key(old), val(old));
  137. }
  138. luaM_freearray(L, nold, oldsize, Node); /* free old array */
  139. }
  140. /*
  141. ** inserts a new key into a hash table; first, check whether key's main
  142. ** position is free. If not, check whether colliding node is in its main
  143. ** position or not: if it is not, move colliding node to an empty place and
  144. ** put new key in its main position; otherwise (colliding node is in its main
  145. ** position), new key goes to an empty position.
  146. */
  147. static TObject *newkey (lua_State *L, Hash *t, const TObject *key) {
  148. Node *mp = luaH_mainposition(t, key);
  149. if (ttype(val(mp)) != LUA_TNIL) { /* main position is not free? */
  150. Node *othern = luaH_mainposition(t, key(mp)); /* `mp' of colliding node */
  151. Node *n = t->firstfree; /* get a free place */
  152. if (othern != mp) { /* is colliding node out of its main position? */
  153. /* yes; move colliding node into free position */
  154. while (othern->next != mp) othern = othern->next; /* find previous */
  155. othern->next = n; /* redo the chain with `n' in place of `mp' */
  156. *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
  157. mp->next = NULL; /* now `mp' is free */
  158. setnilvalue(val(mp));
  159. }
  160. else { /* colliding node is in its own main position */
  161. /* new node will go into free position */
  162. n->next = mp->next; /* chain new position */
  163. mp->next = n;
  164. mp = n;
  165. }
  166. }
  167. setobj(key(mp), key);
  168. lua_assert(ttype(val(mp)) == LUA_TNIL);
  169. for (;;) { /* correct `firstfree' */
  170. if (ttype(key(t->firstfree)) == LUA_TNIL)
  171. return val(mp); /* OK; table still has a free place */
  172. else if (t->firstfree == t->node) break; /* cannot decrement from here */
  173. else (t->firstfree)--;
  174. }
  175. rehash(L, t); /* no more free places */
  176. return newkey(L, t, key); /* `rehash' invalidates this insertion */
  177. }
  178. /*
  179. ** generic search function
  180. */
  181. static const TObject *luaH_getany (Hash *t, const TObject *key) {
  182. if (ttype(key) == LUA_TNIL) return &luaO_nilobject;
  183. else {
  184. Node *n = luaH_mainposition(t, key);
  185. do { /* check whether `key' is somewhere in the chain */
  186. if (luaO_equalObj(key(n), key)) return val(n); /* that's it */
  187. else n = n->next;
  188. } while (n);
  189. return &luaO_nilobject;
  190. }
  191. }
  192. /*
  193. ** search function for integers
  194. */
  195. const TObject *luaH_getnum (Hash *t, int key) {
  196. Node *n = hashnum(t, key);
  197. do { /* check whether `key' is somewhere in the chain */
  198. if (ttype(key(n)) == LUA_TNUMBER && nvalue(key(n)) == (lua_Number)key)
  199. return val(n); /* that's it */
  200. else n = n->next;
  201. } while (n);
  202. return &luaO_nilobject;
  203. }
  204. /*
  205. ** search function for strings
  206. */
  207. const TObject *luaH_getstr (Hash *t, TString *key) {
  208. Node *n = hashstr(t, key);
  209. do { /* check whether `key' is somewhere in the chain */
  210. if (ttype(key(n)) == LUA_TSTRING && tsvalue(key(n)) == key)
  211. return val(n); /* that's it */
  212. else n = n->next;
  213. } while (n);
  214. return &luaO_nilobject;
  215. }
  216. /*
  217. ** main search function
  218. */
  219. const TObject *luaH_get (Hash *t, const TObject *key) {
  220. switch (ttype(key)) {
  221. case LUA_TSTRING: return luaH_getstr(t, tsvalue(key));
  222. case LUA_TNUMBER: {
  223. int k = cast(int, nvalue(key));
  224. if (cast(lua_Number, k) == nvalue(key)) /* is an integer index? */
  225. return luaH_getnum(t, k); /* use specialized version */
  226. /* else go through */
  227. }
  228. default: return luaH_getany(t, key);
  229. }
  230. }
  231. void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val) {
  232. const TObject *p = luaH_get(t, key);
  233. if (p == &luaO_nilobject) {
  234. if (ttype(key) == LUA_TNIL) luaD_error(L, l_s("table index is nil"));
  235. p = newkey(L, t, key);
  236. }
  237. settableval(p, val);
  238. }
  239. void luaH_setstr (lua_State *L, Hash *t, TString *key, const TObject *val) {
  240. const TObject *p = luaH_getstr(t, key);
  241. if (p == &luaO_nilobject) {
  242. TObject k;
  243. setsvalue(&k, key);
  244. p = newkey(L, t, &k);
  245. }
  246. settableval(p, val);
  247. }
  248. void luaH_setnum (lua_State *L, Hash *t, int key, const TObject *val) {
  249. const TObject *p = luaH_getnum(t, key);
  250. if (p == &luaO_nilobject) {
  251. TObject k;
  252. setnvalue(&k, key);
  253. p = newkey(L, t, &k);
  254. }
  255. settableval(p, val);
  256. }