ltable.c 7.9 KB

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