ltable.c 7.4 KB

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