ltable.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. ** $Id: ltable.c,v 1.67 2001/01/25 16:45:36 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. static void setnodevector (lua_State *L, Hash *t, luint32 size) {
  106. int i;
  107. if (size > MAX_INT)
  108. luaD_error(L, "table overflow");
  109. t->node = luaM_newvector(L, size, Node);
  110. for (i=0; i<(int)size; i++) {
  111. setnilvalue(&t->node[i].key);
  112. setnilvalue(&t->node[i].val);
  113. t->node[i].next = NULL;
  114. }
  115. t->size = size;
  116. t->firstfree = &t->node[size-1]; /* first free position to be used */
  117. }
  118. Hash *luaH_new (lua_State *L, int size) {
  119. Hash *t = luaM_new(L, Hash);
  120. t->htag = TagDefault;
  121. t->next = G(L)->roottable;
  122. G(L)->roottable = t;
  123. t->mark = t;
  124. t->size = 0;
  125. t->node = NULL;
  126. setnodevector(L, t, luaO_power2(size));
  127. return t;
  128. }
  129. void luaH_free (lua_State *L, Hash *t) {
  130. luaM_freearray(L, t->node, t->size, Node);
  131. luaM_freelem(L, t, Hash);
  132. }
  133. static int numuse (const Hash *t) {
  134. Node *v = t->node;
  135. int size = t->size;
  136. int realuse = 0;
  137. int i;
  138. for (i=0; i<size; i++) {
  139. if (ttype(&v[i].val) != LUA_TNIL)
  140. realuse++;
  141. }
  142. return realuse;
  143. }
  144. static void rehash (lua_State *L, Hash *t) {
  145. int oldsize = t->size;
  146. Node *nold = t->node;
  147. int nelems = numuse(t);
  148. int i;
  149. lua_assert(nelems<=oldsize);
  150. if (nelems >= oldsize-oldsize/4) /* using more than 3/4? */
  151. setnodevector(L, t, (luint32)oldsize*2);
  152. else if (nelems <= oldsize/4 && /* less than 1/4? */
  153. oldsize > MINPOWER2)
  154. setnodevector(L, t, oldsize/2);
  155. else
  156. setnodevector(L, t, oldsize);
  157. for (i=0; i<oldsize; i++) {
  158. Node *old = nold+i;
  159. if (ttype(&old->val) != LUA_TNIL)
  160. setobj(luaH_set(L, t, &old->key), &old->val);
  161. }
  162. luaM_freearray(L, nold, oldsize, Node); /* free old array */
  163. }
  164. /*
  165. ** inserts a new key into a hash table; first, check whether key's main
  166. ** position is free; if not, check whether colliding node is in its main
  167. ** position or not; if it is not, move colliding node to an empty place and
  168. ** put new key in its main position; otherwise (colliding node is in its main
  169. ** position), new key goes to an empty position.
  170. */
  171. static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) {
  172. if (ttype(&mp->val) != LUA_TNIL) { /* main position is not free? */
  173. Node *othern = luaH_mainposition(t, &mp->key); /* `mp' of colliding node */
  174. Node *n = t->firstfree; /* get a free place */
  175. if (othern != mp) { /* is colliding node out of its main position? */
  176. /* yes; move colliding node into free position */
  177. while (othern->next != mp) othern = othern->next; /* find previous */
  178. othern->next = n; /* redo the chain with `n' in place of `mp' */
  179. *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
  180. mp->next = NULL; /* now `mp' is free */
  181. setnilvalue(&mp->val);
  182. }
  183. else { /* colliding node is in its own main position */
  184. /* new node will go into free position */
  185. n->next = mp->next; /* chain new position */
  186. mp->next = n;
  187. mp = n;
  188. }
  189. }
  190. setobj(&mp->key, key);
  191. lua_assert(ttype(&mp->val) == LUA_TNIL);
  192. for (;;) { /* correct `firstfree' */
  193. if (ttype(&t->firstfree->key) == LUA_TNIL)
  194. return &mp->val; /* OK; table still has a free place */
  195. else if (t->firstfree == t->node) break; /* cannot decrement from here */
  196. else (t->firstfree)--;
  197. }
  198. rehash(L, t); /* no more free places */
  199. return luaH_set(L, t, key); /* `rehash' invalidates this insertion */
  200. }
  201. static TObject *luaH_setany (lua_State *L, Hash *t, const TObject *key) {
  202. Node *mp = luaH_mainposition(t, key);
  203. Node *n = mp;
  204. if (!mp)
  205. luaD_error(L, "table index is nil");
  206. do { /* check whether `key' is somewhere in the chain */
  207. if (luaO_equalObj(key, &n->key))
  208. return &n->val; /* that's all */
  209. else n = n->next;
  210. } while (n);
  211. return newkey(L, t, mp, key); /* `key' not found; must insert it */
  212. }
  213. TObject *luaH_setnum (lua_State *L, Hash *t, lua_Number key) {
  214. TObject kobj;
  215. Node *mp = hashnum(t, key);
  216. Node *n = mp;
  217. do { /* check whether `key' is somewhere in the chain */
  218. if (nvalue(&n->key) == key && ttype(&n->key) == LUA_TNUMBER)
  219. return &n->val; /* that's all */
  220. else n = n->next;
  221. } while (n);
  222. /* `key' not found; must insert it */
  223. setnvalue(&kobj, key);
  224. return newkey(L, t, mp, &kobj);
  225. }
  226. TObject *luaH_setstr (lua_State *L, Hash *t, TString *key) {
  227. TObject kobj;
  228. Node *mp = hashstr(t, key);
  229. Node *n = mp;
  230. do { /* check whether `key' is somewhere in the chain */
  231. if (tsvalue(&n->key) == key && ttype(&n->key) == LUA_TSTRING)
  232. return &n->val; /* that's all */
  233. else n = n->next;
  234. } while (n);
  235. /* `key' not found; must insert it */
  236. setsvalue(&kobj, key);
  237. return newkey(L, t, mp, &kobj);
  238. }
  239. TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
  240. switch (ttype(key)) {
  241. case LUA_TNUMBER: return luaH_setnum(L, t, nvalue(key));
  242. case LUA_TSTRING: return luaH_setstr(L, t, tsvalue(key));
  243. default: return luaH_setany(L, t, key);
  244. }
  245. }