ltable.c 7.9 KB

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