ltable.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. ** $Id: ltable.c,v 1.38 2000/03/29 20:19:20 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 "ltable.h"
  23. #include "lua.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: case TAG_USERDATA:
  37. h = tsvalue(key)->hash;
  38. break;
  39. case TAG_TABLE:
  40. h = IntPoint(L, avalue(key));
  41. break;
  42. case TAG_LCLOSURE: case TAG_CCLOSURE:
  43. h = IntPoint(L, clvalue(key));
  44. break;
  45. default:
  46. return NULL; /* invalid key */
  47. }
  48. LUA_ASSERT(L, h%(unsigned int)t->size == (h&((unsigned int)t->size-1)),
  49. "a&(x-1) == a%x, for x power of 2");
  50. return &t->node[h&((unsigned int)t->size-1)];
  51. }
  52. const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) {
  53. Node *n = luaH_mainposition(t, key);
  54. if (!n)
  55. lua_error(L, "unexpected type to index table");
  56. else do {
  57. if (luaO_equalObj(key, &n->key))
  58. return &n->val;
  59. n = n->next;
  60. } while (n);
  61. return &luaO_nilobject; /* key not found */
  62. }
  63. int luaH_pos (lua_State *L, const Hash *t, const TObject *key) {
  64. const TObject *v = luaH_get(L, t, key);
  65. return (v == &luaO_nilobject) ? -1 : /* key not found */
  66. (int)(((const char *)v - (const char *)(&t->node[0].val))/sizeof(Node));
  67. }
  68. static Node *hashnodecreate (lua_State *L, int nhash) {
  69. Node *v = luaM_newvector(L, nhash, Node);
  70. int i;
  71. for (i=0; i<nhash; i++) {
  72. ttype(&v[i].key) = ttype(&v[i].val) = TAG_NIL;
  73. v[i].next = NULL;
  74. }
  75. return v;
  76. }
  77. static void setnodevector (lua_State *L, Hash *t, int size) {
  78. t->node = hashnodecreate(L, size);
  79. t->size = size;
  80. t->firstfree = &t->node[size-1]; /* first free position to be used */
  81. L->nblocks += gcsize(L, size);
  82. }
  83. Hash *luaH_new (lua_State *L, int size) {
  84. Hash *t = luaM_new(L, Hash);
  85. setnodevector(L, t, luaO_power2(size));
  86. t->htag = TagDefault;
  87. t->next = L->roottable;
  88. L->roottable = t;
  89. t->marked = 0;
  90. return t;
  91. }
  92. void luaH_free (lua_State *L, Hash *t) {
  93. L->nblocks -= gcsize(L, t->size);
  94. luaM_free(L, t->node);
  95. luaM_free(L, t);
  96. }
  97. static int newsize (const Hash *t) {
  98. Node *v = t->node;
  99. int size = t->size;
  100. int realuse = 0;
  101. int i;
  102. for (i=0; i<size; i++) {
  103. if (ttype(&v[i].val) != TAG_NIL)
  104. realuse++;
  105. }
  106. return luaO_power2(realuse+realuse/4+1);
  107. }
  108. static void rehash (lua_State *L, Hash *t) {
  109. int oldsize = t->size;
  110. Node *nold = t->node;
  111. int i;
  112. L->nblocks -= gcsize(L, oldsize);
  113. setnodevector(L, t, newsize(t)); /* create new array of nodes */
  114. for (i=0; i<oldsize; i++) {
  115. Node *old = nold+i;
  116. if (ttype(&old->val) != TAG_NIL)
  117. luaH_set(L, t, &old->key, &old->val);
  118. }
  119. luaM_free(L, nold); /* free old array */
  120. }
  121. /*
  122. ** sets a pair key-value in a hash table; first, check whether key is
  123. ** already present; if not, check whether key's main position is free;
  124. ** if not, check whether colliding node is in its main position or not;
  125. ** if it is not, move colliding node to an empty place and put new pair
  126. ** in its main position; otherwise (colliding node is in its main position),
  127. ** new pair goes to an empty position.
  128. ** Tricky point: the only place where an old element is moved is when
  129. ** we move the colliding node to an empty place; nevertheless, its old
  130. ** value is still in that position until we set the value for the new
  131. ** pair; therefore, even when `val' points to an element of this table
  132. ** (this happens when we use `luaH_move'), there is no problem.
  133. */
  134. void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val) {
  135. Node *mp = luaH_mainposition(t, key);
  136. Node *n = mp;
  137. if (!mp)
  138. lua_error(L, "unexpected type to index table");
  139. do { /* check whether `key' is somewhere in the chain */
  140. if (luaO_equalObj(key, &n->key)) {
  141. n->val = *val; /* update value */
  142. return; /* that's all */
  143. }
  144. else n = n->next;
  145. } while (n);
  146. /* `key' not found; must insert it */
  147. if (ttype(&mp->key) != TAG_NIL) { /* main position is not free? */
  148. Node *othern; /* main position of colliding node */
  149. n = t->firstfree; /* get a free place */
  150. /* is colliding node out of its main position? (can only happens if
  151. its position if after "firstfree") */
  152. if (mp > n && (othern=luaH_mainposition(t, &mp->key)) != mp) {
  153. /* yes; move colliding node into free position */
  154. while (othern->next != mp) othern = othern->next; /* find previous */
  155. othern->next = n; /* redo the chain with `n' in place of `mp' */
  156. *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
  157. mp->next = NULL; /* now `mp' is free */
  158. }
  159. else { /* colliding node is in its own main position */
  160. /* new node will go into free position */
  161. n->next = mp->next; /* chain new position */
  162. mp->next = n;
  163. mp = n;
  164. }
  165. }
  166. mp->key = *key;
  167. mp->val = *val;
  168. for (;;) { /* check free places */
  169. if (ttype(&t->firstfree->key) == TAG_NIL)
  170. return; /* OK; table still has a free place */
  171. else if (t->firstfree == t->node) break; /* cannot decrement from here */
  172. else (t->firstfree)--;
  173. }
  174. rehash(L, t); /* no more free places */
  175. }
  176. void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val) {
  177. TObject index;
  178. ttype(&index) = TAG_NUMBER;
  179. nvalue(&index) = key;
  180. luaH_set(L, t, &index, val);
  181. }
  182. const TObject *luaH_getint (lua_State *L, const Hash *t, int key) {
  183. TObject index;
  184. ttype(&index) = TAG_NUMBER;
  185. nvalue(&index) = key;
  186. return luaH_get(L, t, &index);
  187. }