ltable.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. ** $Id: ltable.c,v 1.62 2000/12/28 12:55:41 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 "lua.h"
  18. #include "lmem.h"
  19. #include "lobject.h"
  20. #include "lstate.h"
  21. #include "ltable.h"
  22. #define TagDefault LUA_TTABLE
  23. #define hashnum(t,n) (&t->node[(luint32)(lint32)(n)&(t->size-1)])
  24. #define hashstr(t,str) (&t->node[(str)->u.s.hash&(t->size-1)])
  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. luint32 h;
  31. switch (ttype(key)) {
  32. case LUA_TNUMBER:
  33. return hashnum(t, nvalue(key));
  34. case LUA_TSTRING:
  35. return hashstr(t, tsvalue(key));
  36. case LUA_TUSERDATA:
  37. h = IntPoint(tsvalue(key));
  38. break;
  39. case LUA_TTABLE:
  40. h = IntPoint(hvalue(key));
  41. break;
  42. case LUA_TFUNCTION:
  43. h = IntPoint(clvalue(key));
  44. break;
  45. default:
  46. return NULL; /* invalid key */
  47. }
  48. return &t->node[h&(t->size-1)];
  49. }
  50. static const TObject *luaH_getany (const Hash *t, const TObject *key) {
  51. Node *n = luaH_mainposition(t, key);
  52. while (n) {
  53. if (luaO_equalObj(key, &n->key))
  54. return &n->val;
  55. n = n->next;
  56. }
  57. return &luaO_nilobject; /* key not found */
  58. }
  59. /* specialized version for numbers */
  60. const TObject *luaH_getnum (const Hash *t, lua_Number key) {
  61. Node *n = hashnum(t, key);
  62. do {
  63. if (nvalue(&n->key) == key && ttype(&n->key) == LUA_TNUMBER)
  64. return &n->val;
  65. n = n->next;
  66. } while (n);
  67. return &luaO_nilobject; /* key not found */
  68. }
  69. /* specialized version for strings */
  70. const TObject *luaH_getstr (const Hash *t, TString *key) {
  71. Node *n = hashstr(t, key);
  72. do {
  73. if (tsvalue(&n->key) == key && ttype(&n->key) == LUA_TSTRING)
  74. return &n->val;
  75. n = n->next;
  76. } while (n);
  77. return &luaO_nilobject; /* key not found */
  78. }
  79. const TObject *luaH_get (const Hash *t, const TObject *key) {
  80. switch (ttype(key)) {
  81. case LUA_TNUMBER: return luaH_getnum(t, nvalue(key));
  82. case LUA_TSTRING: return luaH_getstr(t, tsvalue(key));
  83. default: return luaH_getany(t, key);
  84. }
  85. }
  86. Node *luaH_next (lua_State *L, const Hash *t, const TObject *key) {
  87. int i;
  88. if (ttype(key) == LUA_TNIL)
  89. i = 0; /* first iteration */
  90. else {
  91. const TObject *v = luaH_get(t, key);
  92. if (v == &luaO_nilobject)
  93. lua_error(L, "invalid key for `next'");
  94. i = (int)(((const char *)v -
  95. (const char *)(&t->node[0].val)) / sizeof(Node)) + 1;
  96. }
  97. for (; i<t->size; i++) {
  98. Node *n = node(t, i);
  99. if (ttype(val(n)) != LUA_TNIL)
  100. return n;
  101. }
  102. return NULL; /* no more elements */
  103. }
  104. /*
  105. ** try to remove a key without value from a table. To avoid problems with
  106. ** hash, change `key' for a number with the same hash.
  107. */
  108. void luaH_remove (Hash *t, TObject *key) {
  109. if (ttype(key) == LUA_TNUMBER ||
  110. (ttype(key) == LUA_TSTRING && tsvalue(key)->len <= 30))
  111. return; /* do not remove numbers nor small strings */
  112. else {
  113. /* try to find a number `n' with the same hash as `key' */
  114. Node *mp = luaH_mainposition(t, key);
  115. int n = mp - &t->node[0];
  116. /* make sure `n' is not in `t' */
  117. while (luaH_getnum(t, n) != &luaO_nilobject) {
  118. if (n >= MAX_INT - t->size)
  119. return; /* give up; (to avoid overflow) */
  120. n += t->size;
  121. }
  122. ttype(key) = LUA_TNUMBER;
  123. nvalue(key) = n;
  124. LUA_ASSERT(luaH_mainposition(t, key) == mp, "cannot change hash");
  125. }
  126. }
  127. static void setnodevector (lua_State *L, Hash *t, luint32 size) {
  128. int i;
  129. if (size > MAX_INT)
  130. lua_error(L, "table overflow");
  131. t->node = luaM_newvector(L, size, Node);
  132. for (i=0; i<(int)size; i++) {
  133. ttype(&t->node[i].key) = ttype(&t->node[i].val) = LUA_TNIL;
  134. t->node[i].next = NULL;
  135. }
  136. t->size = size;
  137. t->firstfree = &t->node[size-1]; /* first free position to be used */
  138. }
  139. Hash *luaH_new (lua_State *L, int size) {
  140. Hash *t = luaM_new(L, Hash);
  141. t->htag = TagDefault;
  142. t->next = L->roottable;
  143. L->roottable = t;
  144. t->mark = t;
  145. t->size = 0;
  146. t->node = NULL;
  147. setnodevector(L, t, luaO_power2(size));
  148. return t;
  149. }
  150. void luaH_free (lua_State *L, Hash *t) {
  151. luaM_freearray(L, t->node, t->size, Node);
  152. luaM_freelem(L, t, Hash);
  153. }
  154. static int numuse (const Hash *t) {
  155. Node *v = t->node;
  156. int size = t->size;
  157. int realuse = 0;
  158. int i;
  159. for (i=0; i<size; i++) {
  160. if (ttype(&v[i].val) != LUA_TNIL)
  161. realuse++;
  162. }
  163. return realuse;
  164. }
  165. static void rehash (lua_State *L, Hash *t) {
  166. int oldsize = t->size;
  167. Node *nold = t->node;
  168. int nelems = numuse(t);
  169. int i;
  170. LUA_ASSERT(nelems<=oldsize, "wrong count");
  171. if (nelems >= oldsize-oldsize/4) /* using more than 3/4? */
  172. setnodevector(L, t, (luint32)oldsize*2);
  173. else if (nelems <= oldsize/4 && /* less than 1/4? */
  174. oldsize > MINPOWER2)
  175. setnodevector(L, t, oldsize/2);
  176. else
  177. setnodevector(L, t, oldsize);
  178. for (i=0; i<oldsize; i++) {
  179. Node *old = nold+i;
  180. if (ttype(&old->val) != LUA_TNIL)
  181. *luaH_set(L, t, &old->key) = old->val;
  182. }
  183. luaM_freearray(L, nold, oldsize, Node); /* free old array */
  184. }
  185. /*
  186. ** inserts a new key into a hash table; first, check whether key's main
  187. ** position is free; if not, check whether colliding node is in its main
  188. ** position or not; if it is not, move colliding node to an empty place and
  189. ** put new key in its main position; otherwise (colliding node is in its main
  190. ** position), new key goes to an empty position.
  191. */
  192. static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) {
  193. if (ttype(&mp->key) != LUA_TNIL) { /* main position is not free? */
  194. Node *othern = luaH_mainposition(t, &mp->key); /* `mp' of colliding node */
  195. Node *n = t->firstfree; /* get a free place */
  196. if (othern != mp) { /* is colliding node out of its main position? */
  197. /* yes; move colliding node into free position */
  198. while (othern->next != mp) othern = othern->next; /* find previous */
  199. othern->next = n; /* redo the chain with `n' in place of `mp' */
  200. *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
  201. mp->next = NULL; /* now `mp' is free */
  202. }
  203. else { /* colliding node is in its own main position */
  204. /* new node will go into free position */
  205. n->next = mp->next; /* chain new position */
  206. mp->next = n;
  207. mp = n;
  208. }
  209. }
  210. mp->key = *key;
  211. for (;;) { /* correct `firstfree' */
  212. if (ttype(&t->firstfree->key) == LUA_TNIL)
  213. return &mp->val; /* OK; table still has a free place */
  214. else if (t->firstfree == t->node) break; /* cannot decrement from here */
  215. else (t->firstfree)--;
  216. }
  217. rehash(L, t); /* no more free places */
  218. return luaH_set(L, t, key); /* `rehash' invalidates this insertion */
  219. }
  220. static TObject *luaH_setany (lua_State *L, Hash *t, const TObject *key) {
  221. Node *mp = luaH_mainposition(t, key);
  222. Node *n = mp;
  223. if (!mp)
  224. lua_error(L, "table index is nil");
  225. do { /* check whether `key' is somewhere in the chain */
  226. if (luaO_equalObj(key, &n->key))
  227. return &n->val; /* that's all */
  228. else n = n->next;
  229. } while (n);
  230. return newkey(L, t, mp, key); /* `key' not found; must insert it */
  231. }
  232. TObject *luaH_setnum (lua_State *L, Hash *t, lua_Number key) {
  233. TObject kobj;
  234. Node *mp = hashnum(t, key);
  235. Node *n = mp;
  236. do { /* check whether `key' is somewhere in the chain */
  237. if (nvalue(&n->key) == key && ttype(&n->key) == LUA_TNUMBER)
  238. return &n->val; /* that's all */
  239. else n = n->next;
  240. } while (n);
  241. /* `key' not found; must insert it */
  242. ttype(&kobj) = LUA_TNUMBER;
  243. nvalue(&kobj) = key;
  244. return newkey(L, t, mp, &kobj);
  245. }
  246. TObject *luaH_setstr (lua_State *L, Hash *t, TString *key) {
  247. TObject kobj;
  248. Node *mp = hashstr(t, key);
  249. Node *n = mp;
  250. do { /* check whether `key' is somewhere in the chain */
  251. if (tsvalue(&n->key) == key && ttype(&n->key) == LUA_TSTRING)
  252. return &n->val; /* that's all */
  253. else n = n->next;
  254. } while (n);
  255. /* `key' not found; must insert it */
  256. ttype(&kobj) = LUA_TSTRING;
  257. tsvalue(&kobj) = key;
  258. return newkey(L, t, mp, &kobj);
  259. }
  260. TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
  261. switch (ttype(key)) {
  262. case LUA_TNUMBER: return luaH_setnum(L, t, nvalue(key));
  263. case LUA_TSTRING: return luaH_setstr(L, t, tsvalue(key));
  264. default: return luaH_setany(L, t, key);
  265. }
  266. }