ltable.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. ** $Id: ltable.c,v 1.41 2000/05/08 19:32:53 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 "lauxlib.h"
  19. #include "lmem.h"
  20. #include "lobject.h"
  21. #include "lstate.h"
  22. #include "lstring.h"
  23. #include "ltable.h"
  24. #include "lua.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(avalue(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. static void setnodevector (lua_State *L, Hash *t, int size) {
  101. int i;
  102. t->node = luaM_newvector(L, size, Node);
  103. for (i=0; i<size; i++) {
  104. ttype(&t->node[i].key) = ttype(&t->node[i].val) = TAG_NIL;
  105. t->node[i].next = NULL;
  106. }
  107. t->size = size;
  108. t->firstfree = &t->node[size-1]; /* first free position to be used */
  109. L->nblocks += gcsize(L, size);
  110. }
  111. Hash *luaH_new (lua_State *L, int size) {
  112. Hash *t = luaM_new(L, Hash);
  113. setnodevector(L, t, luaO_power2(size));
  114. t->htag = TagDefault;
  115. t->next = L->roottable;
  116. L->roottable = t;
  117. t->marked = 0;
  118. return t;
  119. }
  120. void luaH_free (lua_State *L, Hash *t) {
  121. L->nblocks -= gcsize(L, t->size);
  122. luaM_free(L, t->node);
  123. luaM_free(L, t);
  124. }
  125. static int newsize (const Hash *t) {
  126. Node *v = t->node;
  127. int size = t->size;
  128. int realuse = 0;
  129. int i;
  130. for (i=0; i<size; i++) {
  131. if (ttype(&v[i].val) != TAG_NIL)
  132. realuse++;
  133. }
  134. return luaO_power2(realuse+realuse/4+1);
  135. }
  136. static void rehash (lua_State *L, Hash *t) {
  137. int oldsize = t->size;
  138. Node *nold = t->node;
  139. int i;
  140. L->nblocks -= gcsize(L, oldsize);
  141. setnodevector(L, t, newsize(t)); /* create new array of nodes */
  142. for (i=0; i<oldsize; i++) {
  143. Node *old = nold+i;
  144. if (ttype(&old->val) != TAG_NIL)
  145. luaH_set(L, t, &old->key, &old->val);
  146. }
  147. luaM_free(L, nold); /* free old array */
  148. }
  149. /*
  150. ** sets a pair key-value in a hash table; first, check whether key is
  151. ** already present; if not, check whether key's main position is free;
  152. ** if not, check whether colliding node is in its main position or not;
  153. ** if it is not, move colliding node to an empty place and put new pair
  154. ** in its main position; otherwise (colliding node is in its main position),
  155. ** new pair goes to an empty position.
  156. ** Tricky point: the only place where an old element is moved is when
  157. ** we move the colliding node to an empty place; nevertheless, its old
  158. ** value is still in that position until we set the value for the new
  159. ** pair; therefore, even when `val' points to an element of this table
  160. ** (this happens when we use `luaH_move'), there is no problem.
  161. */
  162. void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val) {
  163. Node *mp = luaH_mainposition(t, key);
  164. Node *n = mp;
  165. if (!mp)
  166. lua_error(L, "unexpected type to index table");
  167. do { /* check whether `key' is somewhere in the chain */
  168. if (luaO_equalObj(key, &n->key)) {
  169. n->val = *val; /* update value */
  170. return; /* that's all */
  171. }
  172. else n = n->next;
  173. } while (n);
  174. /* `key' not found; must insert it */
  175. if (ttype(&mp->key) != TAG_NIL) { /* main position is not free? */
  176. Node *othern; /* main position of colliding node */
  177. n = t->firstfree; /* get a free place */
  178. /* is colliding node out of its main position? (can only happens if
  179. its position if after "firstfree") */
  180. if (mp > n && (othern=luaH_mainposition(t, &mp->key)) != mp) {
  181. /* yes; move colliding node into free position */
  182. while (othern->next != mp) othern = othern->next; /* find previous */
  183. othern->next = n; /* redo the chain with `n' in place of `mp' */
  184. *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
  185. mp->next = NULL; /* now `mp' is free */
  186. }
  187. else { /* colliding node is in its own main position */
  188. /* new node will go into free position */
  189. n->next = mp->next; /* chain new position */
  190. mp->next = n;
  191. mp = n;
  192. }
  193. }
  194. mp->key = *key;
  195. mp->val = *val;
  196. for (;;) { /* check free places */
  197. if (ttype(&t->firstfree->key) == TAG_NIL)
  198. return; /* OK; table still has a free place */
  199. else if (t->firstfree == t->node) break; /* cannot decrement from here */
  200. else (t->firstfree)--;
  201. }
  202. rehash(L, t); /* no more free places */
  203. }
  204. void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val) {
  205. TObject index;
  206. ttype(&index) = TAG_NUMBER;
  207. nvalue(&index) = key;
  208. luaH_set(L, t, &index, val);
  209. }
  210. const TObject *luaH_getglobal (lua_State *L, const char *name) {
  211. return luaH_getstr(L->gt, luaS_new(L, name));
  212. }