ltable.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. ** $Id: ltable.c,v 1.30 1999/11/22 13:12:07 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 (lua_State *L, 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_PROTO:
  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_CLOSURE:
  49. h = IntPoint(L, clvalue(key));
  50. break;
  51. default:
  52. lua_error(L, "unexpected type to index table");
  53. h = 0; /* to avoid warnings */
  54. }
  55. LUA_ASSERT(L, h%(unsigned int)t->size == (h&((unsigned int)t->size-1)),
  56. "a&(x-1) == a%x, for x power of 2");
  57. return &t->node[h&((unsigned int)t->size-1)];
  58. }
  59. const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) {
  60. Node *n = luaH_mainposition(L, t, key);
  61. do {
  62. if (luaO_equalObj(key, &n->key))
  63. return &n->val;
  64. n = n->next;
  65. } while (n);
  66. return &luaO_nilobject;
  67. }
  68. int luaH_pos (lua_State *L, const Hash *t, const TObject *key) {
  69. const TObject *v = luaH_get(L, t, key);
  70. return (v == &luaO_nilobject) ? -1 : /* key not found */
  71. ((const char *)v - (const char *)(&t->node[0].val))/sizeof(Node);
  72. }
  73. static Node *hashnodecreate (lua_State *L, int nhash) {
  74. Node *v = luaM_newvector(L, nhash, Node);
  75. int i;
  76. for (i=0; i<nhash; i++) {
  77. ttype(&v[i].key) = ttype(&v[i].val) = LUA_T_NIL;
  78. v[i].next = NULL;
  79. }
  80. return v;
  81. }
  82. static void setnodevector (lua_State *L, Hash *t, int size) {
  83. t->node = hashnodecreate(L, size);
  84. t->size = size;
  85. t->firstfree = &t->node[size-1]; /* first free position to be used */
  86. L->nblocks += gcsize(L, size);
  87. }
  88. Hash *luaH_new (lua_State *L, int size) {
  89. Hash *t = luaM_new(L, Hash);
  90. setnodevector(L, t, luaO_power2(size));
  91. t->htag = TagDefault;
  92. t->next = L->roottable;
  93. L->roottable = t;
  94. t->marked = 0;
  95. return t;
  96. }
  97. void luaH_free (lua_State *L, Hash *t) {
  98. L->nblocks -= gcsize(L, t->size);
  99. luaM_free(L, t->node);
  100. luaM_free(L, t);
  101. }
  102. static int newsize (const Hash *t) {
  103. Node *v = t->node;
  104. int size = t->size;
  105. int realuse = 0;
  106. int i;
  107. for (i=0; i<size; i++) {
  108. if (ttype(&v[i].val) != LUA_T_NIL)
  109. realuse++;
  110. }
  111. return luaO_power2(realuse+realuse/4+1);
  112. }
  113. /*
  114. ** the rehash is done in two stages: first, we insert only the elements whose
  115. ** main position is free, to avoid needless collisions. In the second stage,
  116. ** we insert the other elements.
  117. */
  118. static void rehash (lua_State *L, Hash *t) {
  119. int oldsize = t->size;
  120. Node *nold = t->node;
  121. int i;
  122. L->nblocks -= gcsize(L, oldsize);
  123. setnodevector(L, t, newsize(t)); /* create new array of nodes */
  124. /* first loop; set only elements that can go in their main positions */
  125. for (i=0; i<oldsize; i++) {
  126. Node *old = nold+i;
  127. if (ttype(&old->val) == LUA_T_NIL)
  128. old->next = NULL; /* `remove' it for next loop */
  129. else {
  130. Node *mp = luaH_mainposition(L, t, &old->key); /* new main position */
  131. if (ttype(&mp->key) == LUA_T_NIL) { /* is it empty? */
  132. mp->key = old->key; /* put element there */
  133. mp->val = old->val;
  134. old->next = NULL; /* `remove' it for next loop */
  135. }
  136. else /* it will be copied in next loop */
  137. old->next = mp; /* to be used in next loop */
  138. }
  139. }
  140. /* update `firstfree' */
  141. while (ttype(&t->firstfree->key) != LUA_T_NIL) t->firstfree--;
  142. /* second loop; update elements with colision */
  143. for (i=0; i<oldsize; i++) {
  144. Node *old = nold+i;
  145. if (old->next) { /* wasn't already `removed'? */
  146. Node *mp = old->next; /* main position */
  147. Node *e = t->firstfree; /* actual position */
  148. e->key = old->key; /* put element in the free position */
  149. e->val = old->val;
  150. e->next = mp->next; /* chain actual position in main position's list */
  151. mp->next = e;
  152. do { /* update `firstfree' */
  153. t->firstfree--;
  154. } while (ttype(&t->firstfree->key) != LUA_T_NIL);
  155. }
  156. }
  157. luaM_free(L, nold); /* free old array */
  158. }
  159. /*
  160. ** sets a pair key-value in a hash table; first, check whether key is
  161. ** already present; if not, check whether key's main position is free;
  162. ** if not, check whether colliding node is in its main position or not;
  163. ** if it is not, move colliding node to an empty place and put new pair
  164. ** in its main position; otherwise (colliding node is in its main position),
  165. ** new pair goes to an empty position.
  166. ** Tricky point: the only place where an old element is moved is when
  167. ** we move the colliding node to an empty place; nevertheless, its old
  168. ** value is still in that position until we set the value for the new
  169. ** pair; therefore, even when `val' points to an element of this table
  170. ** (this happens when we use `luaH_move'), there is no problem.
  171. */
  172. void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val) {
  173. Node *mp = luaH_mainposition(L, t, key);
  174. Node *n = mp;
  175. do { /* check whether `key' is somewhere in the chain */
  176. if (luaO_equalObj(key, &n->key)) {
  177. n->val = *val; /* update value */
  178. return; /* that's all */
  179. }
  180. else n = n->next;
  181. } while (n);
  182. /* `key' not found; must insert it */
  183. if (ttype(&mp->key) != LUA_T_NIL) { /* main position is not free? */
  184. Node *othern; /* main position of colliding node */
  185. n = t->firstfree; /* get a free place */
  186. /* is colliding node out of its main position? (can only happens if
  187. its position if after "firstfree") */
  188. if (mp > n && (othern=luaH_mainposition(L, t, &mp->key)) != mp) {
  189. /* yes; move colliding node into free position */
  190. while (othern->next != mp) othern = othern->next; /* find previous */
  191. othern->next = n; /* redo the chain with `n' in place of `mp' */
  192. *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
  193. mp->next = NULL; /* now `mp' is free */
  194. }
  195. else { /* colliding node is in its own main position */
  196. /* new node will go into free position */
  197. n->next = mp->next; /* chain new position */
  198. mp->next = n;
  199. mp = n;
  200. }
  201. }
  202. mp->key = *key;
  203. mp->val = *val;
  204. for (;;) { /* check free places */
  205. if (ttype(&(t->firstfree)->key) == LUA_T_NIL)
  206. return; /* OK; table still has a free place */
  207. else if (t->firstfree == t->node) break; /* cannot decrement from here */
  208. else (t->firstfree)--;
  209. }
  210. rehash(L, t); /* no more free places */
  211. }
  212. void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val) {
  213. TObject index;
  214. ttype(&index) = LUA_T_NUMBER;
  215. nvalue(&index) = key;
  216. luaH_set(L, t, &index, val);
  217. }
  218. const TObject *luaH_getint (lua_State *L, const Hash *t, int key) {
  219. TObject index;
  220. ttype(&index) = LUA_T_NUMBER;
  221. nvalue(&index) = key;
  222. return luaH_get(L, t, &index);
  223. }