ltable.c 6.5 KB

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