ltable.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. ** $Id: ltable.c,v 1.70 2001/01/26 15:58:50 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 TObject *key) {
  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. default: /* all other types are hashed as (void *) */
  38. return hashpointer(t, hvalue(key));
  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. setnilvalue(&t->node[i].key);
  73. setnilvalue(&t->node[i].val);
  74. t->node[i].next = NULL;
  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. setobj(luaH_set(L, t, &old->key), &old->val);
  122. }
  123. luaM_freearray(L, nold, oldsize, Node); /* free old array */
  124. }
  125. /*
  126. ** inserts a new key into a hash table; first, check whether key's main
  127. ** position is free. If not, check whether colliding node is in its main
  128. ** position or not: if it is not, move colliding node to an empty place and
  129. ** put new key in its main position; otherwise (colliding node is in its main
  130. ** position), new key goes to an empty position.
  131. */
  132. static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) {
  133. if (ttype(&mp->val) != LUA_TNIL) { /* main position is not free? */
  134. Node *othern = luaH_mainposition(t, &mp->key); /* `mp' of colliding node */
  135. Node *n = t->firstfree; /* get a free place */
  136. if (othern != mp) { /* is colliding node out of its main position? */
  137. /* yes; move colliding node into free position */
  138. while (othern->next != mp) othern = othern->next; /* find previous */
  139. othern->next = n; /* redo the chain with `n' in place of `mp' */
  140. *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
  141. mp->next = NULL; /* now `mp' is free */
  142. setnilvalue(&mp->val);
  143. }
  144. else { /* colliding node is in its own main position */
  145. /* new node will go into free position */
  146. n->next = mp->next; /* chain new position */
  147. mp->next = n;
  148. mp = n;
  149. }
  150. }
  151. setobj(&mp->key, key);
  152. lua_assert(ttype(&mp->val) == LUA_TNIL);
  153. for (;;) { /* correct `firstfree' */
  154. if (ttype(&t->firstfree->key) == LUA_TNIL)
  155. return &mp->val; /* OK; table still has a free place */
  156. else if (t->firstfree == t->node) break; /* cannot decrement from here */
  157. else (t->firstfree)--;
  158. }
  159. rehash(L, t); /* no more free places */
  160. return luaH_set(L, t, key); /* `rehash' invalidates this insertion */
  161. }
  162. /*
  163. ** search function for numbers
  164. */
  165. TObject *luaH_setnum (lua_State *L, Hash *t, lua_Number key) {
  166. TObject kobj;
  167. Node *mp = hashnum(t, key);
  168. Node *n = mp;
  169. do { /* check whether `key' is somewhere in the chain */
  170. if (nvalue(&n->key) == key && ttype(&n->key) == LUA_TNUMBER)
  171. return &n->val; /* that's all */
  172. else n = n->next;
  173. } while (n);
  174. if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */
  175. /* `key' not found; must insert it */
  176. setnvalue(&kobj, key);
  177. return newkey(L, t, mp, &kobj);
  178. }
  179. /*
  180. ** search function for strings
  181. */
  182. TObject *luaH_setstr (lua_State *L, Hash *t, TString *key) {
  183. TObject kobj;
  184. Node *mp = hashstr(t, key);
  185. Node *n = mp;
  186. do { /* check whether `key' is somewhere in the chain */
  187. if (tsvalue(&n->key) == key && ttype(&n->key) == LUA_TSTRING)
  188. return &n->val; /* that's all */
  189. else n = n->next;
  190. } while (n);
  191. if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */
  192. /* `key' not found; must insert it */
  193. setsvalue(&kobj, key);
  194. return newkey(L, t, mp, &kobj);
  195. }
  196. /*
  197. ** search function for 'pointer' types
  198. */
  199. static TObject *luaH_setany (lua_State *L, Hash *t, const TObject *key) {
  200. Node *mp = hashpointer(t, hvalue(key));
  201. Node *n = mp;
  202. do { /* check whether `key' is somewhere in the chain */
  203. /* compare as `hvalue', but may be other pointers (it is the same) */
  204. if (hvalue(&n->key) == hvalue(key) && ttype(&n->key) == ttype(key))
  205. return &n->val; /* that's all */
  206. else n = n->next;
  207. } while (n);
  208. if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */
  209. return newkey(L, t, mp, key); /* `key' not found; must insert it */
  210. }
  211. TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
  212. switch (ttype(key)) {
  213. case LUA_TNUMBER: return luaH_setnum(L, t, nvalue(key));
  214. case LUA_TSTRING: return luaH_setstr(L, t, tsvalue(key));
  215. case LUA_TNIL:
  216. if (L) luaD_error(L, "table index is nil");
  217. return (TObject *)&luaO_nilobject; /* get option */
  218. default: return luaH_setany(L, t, key);
  219. }
  220. }