ltable.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. ** $Id: ltable.c,v 1.82 2001/06/26 13:20:45 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((lu_hash)(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 = (int)(((const lu_byte *)v -
  51. (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, Hash);
  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. TObject *v = luaH_set(L, t, key(old));
  137. setobj(v, val(old));
  138. }
  139. }
  140. luaM_freearray(L, nold, oldsize, Node); /* free old array */
  141. }
  142. /*
  143. ** inserts a new key into a hash table; first, check whether key's main
  144. ** position is free. If not, check whether colliding node is in its main
  145. ** position or not: if it is not, move colliding node to an empty place and
  146. ** put new key in its main position; otherwise (colliding node is in its main
  147. ** position), new key goes to an empty position.
  148. */
  149. static TObject *newkey (lua_State *L, Hash *t, const TObject *key) {
  150. Node *mp = luaH_mainposition(t, key);
  151. if (ttype(val(mp)) != LUA_TNIL) { /* main position is not free? */
  152. Node *othern = luaH_mainposition(t, key(mp)); /* `mp' of colliding node */
  153. Node *n = t->firstfree; /* get a free place */
  154. if (othern != mp) { /* is colliding node out of its main position? */
  155. /* yes; move colliding node into free position */
  156. while (othern->next != mp) othern = othern->next; /* find previous */
  157. othern->next = n; /* redo the chain with `n' in place of `mp' */
  158. *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
  159. mp->next = NULL; /* now `mp' is free */
  160. setnilvalue(val(mp));
  161. }
  162. else { /* colliding node is in its own main position */
  163. /* new node will go into free position */
  164. n->next = mp->next; /* chain new position */
  165. mp->next = n;
  166. mp = n;
  167. }
  168. }
  169. setobj(key(mp), key);
  170. lua_assert(ttype(val(mp)) == LUA_TNIL);
  171. for (;;) { /* correct `firstfree' */
  172. if (ttype(key(t->firstfree)) == LUA_TNIL)
  173. return val(mp); /* OK; table still has a free place */
  174. else if (t->firstfree == t->node) break; /* cannot decrement from here */
  175. else (t->firstfree)--;
  176. }
  177. rehash(L, t); /* no more free places */
  178. return luaH_set(L, t, key); /* `rehash' invalidates this insertion */
  179. }
  180. /*
  181. ** generic search function
  182. */
  183. static const TObject *luaH_getany (Hash *t, const TObject *key) {
  184. if (ttype(key) == LUA_TNIL) return &luaO_nilobject;
  185. else {
  186. Node *n = luaH_mainposition(t, key);
  187. do { /* check whether `key' is somewhere in the chain */
  188. if (luaO_equalObj(key(n), key)) return val(n); /* that's it */
  189. else n = n->next;
  190. } while (n);
  191. return &luaO_nilobject;
  192. }
  193. }
  194. /*
  195. ** search function for integers
  196. */
  197. const TObject *luaH_getnum (Hash *t, int key) {
  198. Node *n = hashnum(t, key);
  199. do { /* check whether `key' is somewhere in the chain */
  200. if (ttype(key(n)) == LUA_TNUMBER && nvalue(key(n)) == (lua_Number)key)
  201. return val(n); /* that's it */
  202. else n = n->next;
  203. } while (n);
  204. return &luaO_nilobject;
  205. }
  206. /*
  207. ** search function for strings
  208. */
  209. const TObject *luaH_getstr (Hash *t, TString *key) {
  210. Node *n = hashstr(t, key);
  211. do { /* check whether `key' is somewhere in the chain */
  212. if (ttype(key(n)) == LUA_TSTRING && tsvalue(key(n)) == key)
  213. return val(n); /* that's it */
  214. else n = n->next;
  215. } while (n);
  216. return &luaO_nilobject;
  217. }
  218. /*
  219. ** main search function
  220. */
  221. const TObject *luaH_get (Hash *t, const TObject *key) {
  222. switch (ttype(key)) {
  223. case LUA_TSTRING: return luaH_getstr(t, tsvalue(key));
  224. case LUA_TNUMBER: {
  225. int k = (int)nvalue(key);
  226. if ((lua_Number)k == nvalue(key)) /* is an integer index? */
  227. return luaH_getnum(t, k); /* use specialized version */
  228. /* else go through */
  229. }
  230. default: return luaH_getany(t, key);
  231. }
  232. }
  233. TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
  234. const TObject *p = luaH_get(t, key);
  235. if (p != &luaO_nilobject) return (TObject *)p;
  236. else if (ttype(key) == LUA_TNIL) luaD_error(L, l_s("table index is nil"));
  237. return newkey(L, t, key);
  238. }
  239. TObject *luaH_setstr (lua_State *L, Hash *t, TString *key) {
  240. const TObject *p = luaH_getstr(t, key);
  241. if (p != &luaO_nilobject) return (TObject *)p;
  242. else {
  243. TObject k;
  244. setsvalue(&k, key);
  245. return newkey(L, t, &k);
  246. }
  247. }
  248. TObject *luaH_setnum (lua_State *L, Hash *t, int key) {
  249. const TObject *p = luaH_getnum(t, key);
  250. if (p != &luaO_nilobject) return (TObject *)p;
  251. else {
  252. TObject k;
  253. setnvalue(&k, key);
  254. return newkey(L, t, &k);
  255. }
  256. }