ltable.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. ** $Id: ltable.c,v 1.51 2000/08/04 19:38:35 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. if (ttype(key) == TAG_NUMBER ||
  106. (ttype(key) == TAG_STRING && tsvalue(key)->u.s.len <= 30))
  107. return; /* do not remove numbers nor small strings */
  108. else {
  109. /* try to find a number `n' with the same hash as `key' */
  110. Node *mp = luaH_mainposition(t, key);
  111. int n = mp - &t->node[0];
  112. /* make sure `n' is not in `t' */
  113. while (luaH_getnum(t, n) != &luaO_nilobject) {
  114. if (n >= MAX_INT - t->size)
  115. return; /* give up; (to avoid overflow) */
  116. n += t->size;
  117. }
  118. ttype(key) = TAG_NUMBER;
  119. nvalue(key) = n;
  120. LUA_ASSERT(luaH_mainposition(t, key) == mp, "cannot change hash");
  121. }
  122. }
  123. static void setnodevector (lua_State *L, Hash *t, lint32 size) {
  124. int i;
  125. if (size > MAX_INT)
  126. lua_error(L, "table overflow");
  127. t->node = luaM_newvector(L, size, Node);
  128. for (i=0; i<(int)size; i++) {
  129. ttype(&t->node[i].key) = ttype(&t->node[i].val) = TAG_NIL;
  130. t->node[i].next = NULL;
  131. }
  132. L->nblocks += gcsize(L, size) - gcsize(L, t->size);
  133. t->size = size;
  134. t->firstfree = &t->node[size-1]; /* first free position to be used */
  135. }
  136. Hash *luaH_new (lua_State *L, int size) {
  137. Hash *t = luaM_new(L, Hash);
  138. t->htag = TagDefault;
  139. t->next = L->roottable;
  140. L->roottable = t;
  141. t->mark = t;
  142. t->size = 0;
  143. L->nblocks += gcsize(L, 0);
  144. t->node = NULL;
  145. setnodevector(L, t, luaO_power2(size));
  146. return t;
  147. }
  148. void luaH_free (lua_State *L, Hash *t) {
  149. L->nblocks -= gcsize(L, t->size);
  150. luaM_free(L, t->node);
  151. luaM_free(L, t);
  152. }
  153. static int numuse (const Hash *t) {
  154. Node *v = t->node;
  155. int size = t->size;
  156. int realuse = 0;
  157. int i;
  158. for (i=0; i<size; i++) {
  159. if (ttype(&v[i].val) != TAG_NIL)
  160. realuse++;
  161. }
  162. return realuse;
  163. }
  164. static void rehash (lua_State *L, Hash *t) {
  165. int oldsize = t->size;
  166. Node *nold = t->node;
  167. int nelems = numuse(t);
  168. int i;
  169. LUA_ASSERT(nelems<=oldsize, "wrong count");
  170. if (nelems >= oldsize-oldsize/4) /* using more than 3/4? */
  171. setnodevector(L, t, (lint32)oldsize*2);
  172. else if (nelems <= oldsize/4 && /* less than 1/4? */
  173. oldsize > MINPOWER2)
  174. setnodevector(L, t, oldsize/2);
  175. else
  176. setnodevector(L, t, oldsize);
  177. for (i=0; i<oldsize; i++) {
  178. Node *old = nold+i;
  179. if (ttype(&old->val) != TAG_NIL)
  180. *luaH_set(L, t, &old->key) = old->val;
  181. }
  182. luaM_free(L, nold); /* free old array */
  183. }
  184. /*
  185. ** inserts a key into a hash table; first, check whether key is
  186. ** already present; if not, check whether key's main position is free;
  187. ** if not, check whether colliding node is in its main position or not;
  188. ** if it is not, move colliding node to an empty place and put new key
  189. ** in its main position; otherwise (colliding node is in its main position),
  190. ** new key goes to an empty position.
  191. */
  192. TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
  193. Node *mp = luaH_mainposition(t, key);
  194. Node *n = mp;
  195. if (!mp)
  196. lua_error(L, "table index is nil");
  197. do { /* check whether `key' is somewhere in the chain */
  198. if (luaO_equalObj(key, &n->key))
  199. return &n->val; /* that's all */
  200. else n = n->next;
  201. } while (n);
  202. /* `key' not found; must insert it */
  203. if (ttype(&mp->key) != TAG_NIL) { /* main position is not free? */
  204. Node *othern; /* main position of colliding node */
  205. n = t->firstfree; /* get a free place */
  206. /* is colliding node out of its main position? (can only happens if
  207. its position is after "firstfree") */
  208. if (mp > n && (othern=luaH_mainposition(t, &mp->key)) != mp) {
  209. /* yes; move colliding node into free position */
  210. while (othern->next != mp) othern = othern->next; /* find previous */
  211. othern->next = n; /* redo the chain with `n' in place of `mp' */
  212. *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
  213. mp->next = NULL; /* now `mp' is free */
  214. }
  215. else { /* colliding node is in its own main position */
  216. /* new node will go into free position */
  217. n->next = mp->next; /* chain new position */
  218. mp->next = n;
  219. mp = n;
  220. }
  221. }
  222. mp->key = *key;
  223. for (;;) { /* correct `firstfree' */
  224. if (ttype(&t->firstfree->key) == TAG_NIL)
  225. return &mp->val; /* OK; table still has a free place */
  226. else if (t->firstfree == t->node) break; /* cannot decrement from here */
  227. else (t->firstfree)--;
  228. }
  229. rehash(L, t); /* no more free places */
  230. return luaH_set(L, t, key); /* `rehash' invalidates this insertion */
  231. }
  232. TObject *luaH_setint (lua_State *L, Hash *t, int key) {
  233. TObject index;
  234. ttype(&index) = TAG_NUMBER;
  235. nvalue(&index) = key;
  236. return luaH_set(L, t, &index);
  237. }
  238. void luaH_setstrnum (lua_State *L, Hash *t, TString *key, Number val) {
  239. TObject *value, index;
  240. ttype(&index) = TAG_STRING;
  241. tsvalue(&index) = key;
  242. value = luaH_set(L, t, &index);
  243. ttype(value) = TAG_NUMBER;
  244. nvalue(value) = val;
  245. }
  246. const TObject *luaH_getglobal (lua_State *L, const char *name) {
  247. return luaH_getstr(L->gt, luaS_new(L, name));
  248. }