ltable.c 8.1 KB

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