ltable.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. ** $Id: ltable.c,v 1.61 2000/12/22 16:57:46 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. #include "lua.h"
  18. #include "lmem.h"
  19. #include "lobject.h"
  20. #include "lstate.h"
  21. #include "lstring.h"
  22. #include "ltable.h"
  23. #define TagDefault LUA_TTABLE
  24. /*
  25. ** returns the `main' position of an element in a table (that is, the index
  26. ** of its hash value)
  27. */
  28. Node *luaH_mainposition (const Hash *t, const TObject *key) {
  29. luint32 h;
  30. switch (ttype(key)) {
  31. case LUA_TNUMBER:
  32. h = (luint32)(lint32)nvalue(key);
  33. break;
  34. case LUA_TSTRING:
  35. h = tsvalue(key)->u.s.hash;
  36. break;
  37. case LUA_TUSERDATA:
  38. h = IntPoint(tsvalue(key));
  39. break;
  40. case LUA_TTABLE:
  41. h = IntPoint(hvalue(key));
  42. break;
  43. case LUA_TFUNCTION:
  44. h = IntPoint(clvalue(key));
  45. break;
  46. default:
  47. return NULL; /* invalid key */
  48. }
  49. LUA_ASSERT(h%(unsigned int)t->size == (h&((unsigned int)t->size-1)),
  50. "a&(x-1) == a%x, for x power of 2");
  51. return &t->node[h&(t->size-1)];
  52. }
  53. static const TObject *luaH_getany (lua_State *L, const Hash *t,
  54. const TObject *key) {
  55. Node *n = luaH_mainposition(t, key);
  56. if (!n)
  57. lua_error(L, "table index is nil");
  58. else do {
  59. if (luaO_equalObj(key, &n->key))
  60. return &n->val;
  61. n = n->next;
  62. } while (n);
  63. return &luaO_nilobject; /* key not found */
  64. }
  65. /* specialized version for numbers */
  66. const TObject *luaH_getnum (const Hash *t, lua_Number key) {
  67. Node *n = &t->node[(luint32)(lint32)key&(t->size-1)];
  68. do {
  69. if (ttype(&n->key) == LUA_TNUMBER && nvalue(&n->key) == key)
  70. return &n->val;
  71. n = n->next;
  72. } while (n);
  73. return &luaO_nilobject; /* key not found */
  74. }
  75. /* specialized version for strings */
  76. const TObject *luaH_getstr (const Hash *t, TString *key) {
  77. Node *n = &t->node[key->u.s.hash&(t->size-1)];
  78. do {
  79. if (ttype(&n->key) == LUA_TSTRING && tsvalue(&n->key) == key)
  80. return &n->val;
  81. n = n->next;
  82. } while (n);
  83. return &luaO_nilobject; /* key not found */
  84. }
  85. const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) {
  86. switch (ttype(key)) {
  87. case LUA_TNUMBER: return luaH_getnum(t, nvalue(key));
  88. case LUA_TSTRING: return luaH_getstr(t, tsvalue(key));
  89. default: return luaH_getany(L, t, key);
  90. }
  91. }
  92. Node *luaH_next (lua_State *L, const Hash *t, const TObject *key) {
  93. int i;
  94. if (ttype(key) == LUA_TNIL)
  95. i = 0; /* first iteration */
  96. else {
  97. const TObject *v = luaH_get(L, t, key);
  98. if (v == &luaO_nilobject)
  99. lua_error(L, "invalid key for `next'");
  100. i = (int)(((const char *)v -
  101. (const char *)(&t->node[0].val)) / sizeof(Node)) + 1;
  102. }
  103. for (; i<t->size; i++) {
  104. Node *n = node(t, i);
  105. if (ttype(val(n)) != LUA_TNIL)
  106. return n;
  107. }
  108. return NULL; /* no more elements */
  109. }
  110. /*
  111. ** try to remove a key without value from a table. To avoid problems with
  112. ** hash, change `key' for a number with the same hash.
  113. */
  114. void luaH_remove (Hash *t, TObject *key) {
  115. if (ttype(key) == LUA_TNUMBER ||
  116. (ttype(key) == LUA_TSTRING && tsvalue(key)->len <= 30))
  117. return; /* do not remove numbers nor small strings */
  118. else {
  119. /* try to find a number `n' with the same hash as `key' */
  120. Node *mp = luaH_mainposition(t, key);
  121. int n = mp - &t->node[0];
  122. /* make sure `n' is not in `t' */
  123. while (luaH_getnum(t, n) != &luaO_nilobject) {
  124. if (n >= MAX_INT - t->size)
  125. return; /* give up; (to avoid overflow) */
  126. n += t->size;
  127. }
  128. ttype(key) = LUA_TNUMBER;
  129. nvalue(key) = n;
  130. LUA_ASSERT(luaH_mainposition(t, key) == mp, "cannot change hash");
  131. }
  132. }
  133. static void setnodevector (lua_State *L, Hash *t, luint32 size) {
  134. int i;
  135. if (size > MAX_INT)
  136. lua_error(L, "table overflow");
  137. t->node = luaM_newvector(L, size, Node);
  138. for (i=0; i<(int)size; i++) {
  139. ttype(&t->node[i].key) = ttype(&t->node[i].val) = LUA_TNIL;
  140. t->node[i].next = NULL;
  141. }
  142. t->size = size;
  143. t->firstfree = &t->node[size-1]; /* first free position to be used */
  144. }
  145. Hash *luaH_new (lua_State *L, int size) {
  146. Hash *t = luaM_new(L, Hash);
  147. t->htag = TagDefault;
  148. t->next = L->roottable;
  149. L->roottable = t;
  150. t->mark = t;
  151. t->size = 0;
  152. t->node = NULL;
  153. setnodevector(L, t, luaO_power2(size));
  154. return t;
  155. }
  156. void luaH_free (lua_State *L, Hash *t) {
  157. luaM_freearray(L, t->node, t->size, Node);
  158. luaM_freelem(L, t, Hash);
  159. }
  160. static int numuse (const Hash *t) {
  161. Node *v = t->node;
  162. int size = t->size;
  163. int realuse = 0;
  164. int i;
  165. for (i=0; i<size; i++) {
  166. if (ttype(&v[i].val) != LUA_TNIL)
  167. realuse++;
  168. }
  169. return realuse;
  170. }
  171. static void rehash (lua_State *L, Hash *t) {
  172. int oldsize = t->size;
  173. Node *nold = t->node;
  174. int nelems = numuse(t);
  175. int i;
  176. LUA_ASSERT(nelems<=oldsize, "wrong count");
  177. if (nelems >= oldsize-oldsize/4) /* using more than 3/4? */
  178. setnodevector(L, t, (luint32)oldsize*2);
  179. else if (nelems <= oldsize/4 && /* less than 1/4? */
  180. oldsize > MINPOWER2)
  181. setnodevector(L, t, oldsize/2);
  182. else
  183. setnodevector(L, t, oldsize);
  184. for (i=0; i<oldsize; i++) {
  185. Node *old = nold+i;
  186. if (ttype(&old->val) != LUA_TNIL)
  187. *luaH_set(L, t, &old->key) = old->val;
  188. }
  189. luaM_freearray(L, nold, oldsize, Node); /* free old array */
  190. }
  191. /*
  192. ** inserts a key into a hash table; first, check whether key is
  193. ** already present; if not, check whether key's main position is free;
  194. ** if not, check whether colliding node is in its main position or not;
  195. ** if it is not, move colliding node to an empty place and put new key
  196. ** in its main position; otherwise (colliding node is in its main position),
  197. ** new key goes to an empty position.
  198. */
  199. TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
  200. Node *mp = luaH_mainposition(t, key);
  201. Node *n = mp;
  202. if (!mp)
  203. lua_error(L, "table index is nil");
  204. do { /* check whether `key' is somewhere in the chain */
  205. if (luaO_equalObj(key, &n->key))
  206. return &n->val; /* that's all */
  207. else n = n->next;
  208. } while (n);
  209. /* `key' not found; must insert it */
  210. if (ttype(&mp->key) != LUA_TNIL) { /* main position is not free? */
  211. Node *othern; /* main position of colliding node */
  212. n = t->firstfree; /* get a free place */
  213. /* is colliding node out of its main position? (can only happens if
  214. its position is after "firstfree") */
  215. if (mp > n && (othern=luaH_mainposition(t, &mp->key)) != mp) {
  216. /* yes; move colliding node into free position */
  217. while (othern->next != mp) othern = othern->next; /* find previous */
  218. othern->next = n; /* redo the chain with `n' in place of `mp' */
  219. *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
  220. mp->next = NULL; /* now `mp' is free */
  221. }
  222. else { /* colliding node is in its own main position */
  223. /* new node will go into free position */
  224. n->next = mp->next; /* chain new position */
  225. mp->next = n;
  226. mp = n;
  227. }
  228. }
  229. mp->key = *key;
  230. for (;;) { /* correct `firstfree' */
  231. if (ttype(&t->firstfree->key) == LUA_TNIL)
  232. return &mp->val; /* OK; table still has a free place */
  233. else if (t->firstfree == t->node) break; /* cannot decrement from here */
  234. else (t->firstfree)--;
  235. }
  236. rehash(L, t); /* no more free places */
  237. return luaH_set(L, t, key); /* `rehash' invalidates this insertion */
  238. }
  239. TObject *luaH_setint (lua_State *L, Hash *t, int key) {
  240. TObject index;
  241. ttype(&index) = LUA_TNUMBER;
  242. nvalue(&index) = key;
  243. return luaH_set(L, t, &index);
  244. }
  245. void luaH_setstrnum (lua_State *L, Hash *t, TString *key, lua_Number val) {
  246. TObject *value, index;
  247. ttype(&index) = LUA_TSTRING;
  248. tsvalue(&index) = key;
  249. value = luaH_set(L, t, &index);
  250. ttype(value) = LUA_TNUMBER;
  251. nvalue(value) = val;
  252. }
  253. const TObject *luaH_getglobal (lua_State *L, const char *name) {
  254. return luaH_getstr(L->gt, luaS_new(L, name));
  255. }