ltable.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. ** $Id: ltable.c,v 1.74 2001/01/30 19:48:37 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((luint32)(lint32)(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, "invalid key for `next'");
  49. i = (int)(((const char *)v -
  50. (const 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. static void setnodevector (lua_State *L, Hash *t, luint32 size) {
  67. int i;
  68. if (size > MAX_INT)
  69. luaD_error(L, "table overflow");
  70. t->node = luaM_newvector(L, size, Node);
  71. for (i=0; i<(int)size; i++) {
  72. t->node[i].next = NULL;
  73. t->node[i].key_tt = LUA_TNIL;
  74. setnilvalue(&t->node[i].val);
  75. }
  76. t->size = size;
  77. t->firstfree = &t->node[size-1]; /* first free position to be used */
  78. }
  79. Hash *luaH_new (lua_State *L, int size) {
  80. Hash *t = luaM_new(L, Hash);
  81. t->htag = TagDefault;
  82. t->next = G(L)->roottable;
  83. G(L)->roottable = t;
  84. t->mark = t;
  85. t->size = 0;
  86. t->node = NULL;
  87. setnodevector(L, t, luaO_power2(size));
  88. return t;
  89. }
  90. void luaH_free (lua_State *L, Hash *t) {
  91. luaM_freearray(L, t->node, t->size, Node);
  92. luaM_freelem(L, t, Hash);
  93. }
  94. static int numuse (const Hash *t) {
  95. Node *v = t->node;
  96. int size = t->size;
  97. int realuse = 0;
  98. int i;
  99. for (i=0; i<size; i++) {
  100. if (ttype(&v[i].val) != LUA_TNIL)
  101. realuse++;
  102. }
  103. return realuse;
  104. }
  105. static void rehash (lua_State *L, Hash *t) {
  106. int oldsize = t->size;
  107. Node *nold = t->node;
  108. int nelems = numuse(t);
  109. int i;
  110. lua_assert(nelems<=oldsize);
  111. if (nelems >= oldsize-oldsize/4) /* using more than 3/4? */
  112. setnodevector(L, t, (luint32)oldsize*2);
  113. else if (nelems <= oldsize/4 && /* less than 1/4? */
  114. oldsize > MINPOWER2)
  115. setnodevector(L, t, oldsize/2);
  116. else
  117. setnodevector(L, t, oldsize);
  118. for (i=0; i<oldsize; i++) {
  119. Node *old = nold+i;
  120. if (ttype(&old->val) != LUA_TNIL) {
  121. TObject o;
  122. TObject *v;
  123. setkey2obj(&o, old);
  124. v = luaH_set(L, t, &o);
  125. setobj(v, &old->val);
  126. }
  127. }
  128. luaM_freearray(L, nold, oldsize, Node); /* free old array */
  129. }
  130. /*
  131. ** inserts a new key into a hash table; first, check whether key's main
  132. ** position is free. If not, check whether colliding node is in its main
  133. ** position or not: if it is not, move colliding node to an empty place and
  134. ** put new key in its main position; otherwise (colliding node is in its main
  135. ** position), new key goes to an empty position.
  136. */
  137. static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) {
  138. if (ttype(&mp->val) != LUA_TNIL) { /* main position is not free? */
  139. Node *othern = luaH_mainposition(t, mp); /* `mp' of colliding node */
  140. Node *n = t->firstfree; /* get a free place */
  141. if (othern != mp) { /* is colliding node out of its main position? */
  142. /* yes; move colliding node into free position */
  143. while (othern->next != mp) othern = othern->next; /* find previous */
  144. othern->next = n; /* redo the chain with `n' in place of `mp' */
  145. *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
  146. mp->next = NULL; /* now `mp' is free */
  147. setnilvalue(&mp->val);
  148. }
  149. else { /* colliding node is in its own main position */
  150. /* new node will go into free position */
  151. n->next = mp->next; /* chain new position */
  152. mp->next = n;
  153. mp = n;
  154. }
  155. }
  156. setobj2key(mp, key);
  157. lua_assert(ttype(&mp->val) == LUA_TNIL);
  158. for (;;) { /* correct `firstfree' */
  159. if (ttype_key(t->firstfree) == LUA_TNIL)
  160. return &mp->val; /* OK; table still has a free place */
  161. else if (t->firstfree == t->node) break; /* cannot decrement from here */
  162. else (t->firstfree)--;
  163. }
  164. rehash(L, t); /* no more free places */
  165. return luaH_set(L, t, key); /* `rehash' invalidates this insertion */
  166. }
  167. /*
  168. ** search function for numbers
  169. */
  170. TObject *luaH_setnum (lua_State *L, Hash *t, lua_Number key) {
  171. TObject kobj;
  172. Node *mp = hashnum(t, key);
  173. Node *n = mp;
  174. do { /* check whether `key' is somewhere in the chain */
  175. if (ttype_key(n) == LUA_TNUMBER && nvalue_key(n) == key)
  176. return &n->val; /* that's all */
  177. else n = n->next;
  178. } while (n);
  179. if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */
  180. /* `key' not found; must insert it */
  181. setnvalue(&kobj, key);
  182. return newkey(L, t, mp, &kobj);
  183. }
  184. /*
  185. ** search function for strings
  186. */
  187. TObject *luaH_setstr (lua_State *L, Hash *t, TString *key) {
  188. TObject kobj;
  189. Node *mp = hashstr(t, key);
  190. Node *n = mp;
  191. do { /* check whether `key' is somewhere in the chain */
  192. if (ttype_key(n) == LUA_TSTRING && tsvalue_key(n) == key)
  193. return &n->val; /* that's all */
  194. else n = n->next;
  195. } while (n);
  196. if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */
  197. /* `key' not found; must insert it */
  198. setsvalue(&kobj, key);
  199. return newkey(L, t, mp, &kobj);
  200. }
  201. /*
  202. ** search function for 'pointer' types
  203. */
  204. static TObject *luaH_setany (lua_State *L, Hash *t, const TObject *key) {
  205. Node *mp = hashpointer(t, hvalue(key));
  206. Node *n = mp;
  207. do { /* check whether `key' is somewhere in the chain */
  208. /* compare as `tsvalue', but may be other pointers (it is the same) */
  209. if (ttype_key(n) == ttype(key) && tsvalue_key(n) == tsvalue(key))
  210. return &n->val; /* that's all */
  211. else n = n->next;
  212. } while (n);
  213. if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */
  214. return newkey(L, t, mp, key); /* `key' not found; must insert it */
  215. }
  216. TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
  217. switch (ttype(key)) {
  218. case LUA_TNUMBER: return luaH_setnum(L, t, nvalue(key));
  219. case LUA_TSTRING: return luaH_setstr(L, t, tsvalue(key));
  220. case LUA_TNIL:
  221. if (L) luaD_error(L, "table index is nil");
  222. return (TObject *)&luaO_nilobject; /* get option */
  223. default: return luaH_setany(L, t, key);
  224. }
  225. }