ltable.c 8.1 KB

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