ltable.c 7.3 KB

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