ltable.c 7.9 KB

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