ltable.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. ** $Id: ltable.c,v 1.20 1999/01/25 17:41:19 roberto Exp roberto $
  3. ** Lua tables (hash)
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include "lauxlib.h"
  8. #include "lmem.h"
  9. #include "lobject.h"
  10. #include "lstate.h"
  11. #include "ltable.h"
  12. #include "lua.h"
  13. #define gcsize(n) (1+(n/16))
  14. #define nuse(t) ((t)->nuse)
  15. #define nodevector(t) ((t)->node)
  16. #define TagDefault LUA_T_ARRAY;
  17. static long int hashindex (TObject *ref) {
  18. long int h;
  19. switch (ttype(ref)) {
  20. case LUA_T_NUMBER:
  21. h = (long int)nvalue(ref);
  22. break;
  23. case LUA_T_STRING: case LUA_T_USERDATA:
  24. h = (IntPoint)tsvalue(ref);
  25. break;
  26. case LUA_T_ARRAY:
  27. h = (IntPoint)avalue(ref);
  28. break;
  29. case LUA_T_PROTO:
  30. h = (IntPoint)tfvalue(ref);
  31. break;
  32. case LUA_T_CPROTO:
  33. h = (IntPoint)fvalue(ref);
  34. break;
  35. case LUA_T_CLOSURE:
  36. h = (IntPoint)clvalue(ref);
  37. break;
  38. default:
  39. lua_error("unexpected type to index table");
  40. h = 0; /* to avoid warnings */
  41. }
  42. return (h >= 0 ? h : -(h+1));
  43. }
  44. Node *luaH_present (Hash *t, TObject *key) {
  45. int tsize = nhash(t);
  46. long int h = hashindex(key);
  47. int h1 = h%tsize;
  48. Node *n = node(t, h1);
  49. /* keep looking until an entry with "ref" equal to key or nil */
  50. while ((ttype(ref(n)) == ttype(key)) ? !luaO_equalval(key, ref(n))
  51. : ttype(ref(n)) != LUA_T_NIL) {
  52. h1 += (h&(tsize-2)) + 1; /* double hashing */
  53. if (h1 >= tsize) h1 -= tsize;
  54. n = node(t, h1);
  55. }
  56. return n;
  57. }
  58. void luaH_free (Hash *frees) {
  59. while (frees) {
  60. Hash *next = (Hash *)frees->head.next;
  61. L->nblocks -= gcsize(frees->nhash);
  62. luaM_free(nodevector(frees));
  63. luaM_free(frees);
  64. frees = next;
  65. }
  66. }
  67. static Node *hashnodecreate (int nhash) {
  68. Node *v = luaM_newvector(nhash, Node);
  69. int i;
  70. for (i=0; i<nhash; i++)
  71. ttype(ref(&v[i])) = ttype(val(&v[i])) = LUA_T_NIL;
  72. return v;
  73. }
  74. Hash *luaH_new (int nhash) {
  75. Hash *t = luaM_new(Hash);
  76. nhash = luaO_redimension(nhash*3/2);
  77. nodevector(t) = hashnodecreate(nhash);
  78. nhash(t) = nhash;
  79. nuse(t) = 0;
  80. t->htag = TagDefault;
  81. luaO_insertlist(&(L->roottable), (GCnode *)t);
  82. L->nblocks += gcsize(nhash);
  83. return t;
  84. }
  85. static int newsize (Hash *t) {
  86. Node *v = t->node;
  87. int size = nhash(t);
  88. int realuse = 0;
  89. int i;
  90. for (i=0; i<size; i++) {
  91. if (ttype(val(v+i)) != LUA_T_NIL)
  92. realuse++;
  93. }
  94. return luaO_redimension((realuse+1)*2); /* +1 is the new element */
  95. }
  96. static void rehash (Hash *t) {
  97. int nold = nhash(t);
  98. Node *vold = nodevector(t);
  99. int nnew = newsize(t);
  100. int i;
  101. nodevector(t) = hashnodecreate(nnew);
  102. nhash(t) = nnew;
  103. nuse(t) = 0;
  104. for (i=0; i<nold; i++) {
  105. Node *n = vold+i;
  106. if (ttype(val(n)) != LUA_T_NIL) {
  107. *luaH_present(t, ref(n)) = *n; /* copy old node to new hash */
  108. nuse(t)++;
  109. }
  110. }
  111. L->nblocks += gcsize(nnew)-gcsize(nold);
  112. luaM_free(vold);
  113. }
  114. void luaH_set (Hash *t, TObject *ref, TObject *val) {
  115. Node *n = luaH_present(t, ref);
  116. if (ttype(ref(n)) != LUA_T_NIL)
  117. *val(n) = *val;
  118. else {
  119. TObject buff = *val; /* rehash may invalidate this address */
  120. if ((long)nuse(t)*3L > (long)nhash(t)*2L) {
  121. rehash(t);
  122. n = luaH_present(t, ref);
  123. }
  124. nuse(t)++;
  125. *ref(n) = *ref;
  126. *val(n) = buff;
  127. }
  128. }
  129. int luaH_pos (Hash *t, TObject *r) {
  130. Node *n = luaH_present(t, r);
  131. luaL_arg_check(ttype(val(n)) != LUA_T_NIL, 2, "key not found");
  132. return n-(t->node);
  133. }
  134. void luaH_setint (Hash *t, int ref, TObject *val) {
  135. TObject index;
  136. ttype(&index) = LUA_T_NUMBER;
  137. nvalue(&index) = ref;
  138. luaH_set(t, &index, val);
  139. }
  140. TObject *luaH_getint (Hash *t, int ref) {
  141. TObject index;
  142. ttype(&index) = LUA_T_NUMBER;
  143. nvalue(&index) = ref;
  144. return luaH_get(t, &index);
  145. }