ltable.c 8.7 KB

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