ltable.c 8.2 KB

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