ltable.c 8.5 KB

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