ltable.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. ** $Id: ltable.h $
  3. ** Lua tables (hash)
  4. ** See Copyright Notice in lua.h
  5. */
  6. #ifndef ltable_h
  7. #define ltable_h
  8. #include "lobject.h"
  9. #define gnode(t,i) (&(t)->node[i])
  10. #define gval(n) (&(n)->i_val)
  11. #define gnext(n) ((n)->u.next)
  12. /*
  13. ** Clear all bits of fast-access metamethods, which means that the table
  14. ** may have any of these metamethods. (First access that fails after the
  15. ** clearing will set the bit again.)
  16. */
  17. #define invalidateTMcache(t) ((t)->flags &= cast_byte(~maskflags))
  18. /*
  19. ** Bit BITDUMMY set in 'flags' means the table is using the dummy node
  20. ** for its hash part.
  21. */
  22. #define BITDUMMY (1 << 6)
  23. #define NOTBITDUMMY cast_byte(~BITDUMMY)
  24. #define isdummy(t) ((t)->flags & BITDUMMY)
  25. #define setnodummy(t) ((t)->flags &= NOTBITDUMMY)
  26. #define setdummy(t) ((t)->flags |= BITDUMMY)
  27. /* allocated size for hash nodes */
  28. #define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t))
  29. /* returns the Node, given the value of a table entry */
  30. #define nodefromval(v) cast(Node *, (v))
  31. #define luaH_fastgeti(t,k,res,tag) \
  32. { Table *h = t; lua_Unsigned u = l_castS2U(k) - 1u; \
  33. if ((u < h->asize)) { \
  34. tag = *getArrTag(h, u); \
  35. if (!tagisempty(tag)) { farr2val(h, u, tag, res); }} \
  36. else { tag = luaH_getint(h, (k), res); }}
  37. #define luaH_fastseti(t,k,val,hres) \
  38. { Table *h = t; lua_Unsigned u = l_castS2U(k) - 1u; \
  39. if ((u < h->asize)) { \
  40. lu_byte *tag = getArrTag(h, u); \
  41. if (checknoTM(h->metatable, TM_NEWINDEX) || !tagisempty(*tag)) \
  42. { fval2arr(h, u, tag, val); hres = HOK; } \
  43. else hres = ~cast_int(u); } \
  44. else { hres = luaH_psetint(h, k, val); }}
  45. /* results from pset */
  46. #define HOK 0
  47. #define HNOTFOUND 1
  48. #define HNOTATABLE 2
  49. #define HFIRSTNODE 3
  50. /*
  51. ** 'luaH_get*' operations set 'res', unless the value is absent, and
  52. ** return the tag of the result.
  53. ** The 'luaH_pset*' (pre-set) operations set the given value and return
  54. ** HOK, unless the original value is absent. In that case, if the key
  55. ** is really absent, they return HNOTFOUND. Otherwise, if there is a
  56. ** slot with that key but with no value, 'luaH_pset*' return an encoding
  57. ** of where the key is (usually called 'hres'). (pset cannot set that
  58. ** value because there might be a metamethod.) If the slot is in the
  59. ** hash part, the encoding is (HFIRSTNODE + hash index); if the slot is
  60. ** in the array part, the encoding is (~array index), a negative value.
  61. ** The value HNOTATABLE is used by the fast macros to signal that the
  62. ** value being indexed is not a table.
  63. ** (The size for the array part is limited by the maximum power of two
  64. ** that fits in an unsigned integer; that is INT_MAX+1. So, the C-index
  65. ** ranges from 0, which encodes to -1, to INT_MAX, which encodes to
  66. ** INT_MIN. The size of the hash part is limited by the maximum power of
  67. ** two that fits in a signed integer; that is (INT_MAX+1)/2. So, it is
  68. ** safe to add HFIRSTNODE to any index there.)
  69. */
  70. /*
  71. ** The array part of a table is represented by an inverted array of
  72. ** values followed by an array of tags, to avoid wasting space with
  73. ** padding. In between them there is an unsigned int, explained later.
  74. ** The 'array' pointer points between the two arrays, so that values are
  75. ** indexed with negative indices and tags with non-negative indices.
  76. Values Tags
  77. --------------------------------------------------------
  78. ... | Value 1 | Value 0 |unsigned|0|1|...
  79. --------------------------------------------------------
  80. ^ t->array
  81. ** All accesses to 't->array' should be through the macros 'getArrTag'
  82. ** and 'getArrVal'.
  83. */
  84. /* Computes the address of the tag for the abstract C-index 'k' */
  85. #define getArrTag(t,k) (cast(lu_byte*, (t)->array) + sizeof(unsigned) + (k))
  86. /* Computes the address of the value for the abstract C-index 'k' */
  87. #define getArrVal(t,k) ((t)->array - 1 - (k))
  88. /*
  89. ** The unsigned between the two arrays is used as a hint for #t;
  90. ** see luaH_getn. It is stored there to avoid wasting space in
  91. ** the structure Table for tables with no array part.
  92. */
  93. #define lenhint(t) cast(unsigned*, (t)->array)
  94. /*
  95. ** Move TValues to/from arrays, using C indices
  96. */
  97. #define arr2obj(h,k,val) \
  98. ((val)->tt_ = *getArrTag(h,(k)), (val)->value_ = *getArrVal(h,(k)))
  99. #define obj2arr(h,k,val) \
  100. (*getArrTag(h,(k)) = (val)->tt_, *getArrVal(h,(k)) = (val)->value_)
  101. /*
  102. ** Often, we need to check the tag of a value before moving it. The
  103. ** following macros also move TValues to/from arrays, but receive the
  104. ** precomputed tag value or address as an extra argument.
  105. */
  106. #define farr2val(h,k,tag,res) \
  107. ((res)->tt_ = tag, (res)->value_ = *getArrVal(h,(k)))
  108. #define fval2arr(h,k,tag,val) \
  109. (*tag = (val)->tt_, *getArrVal(h,(k)) = (val)->value_)
  110. LUAI_FUNC lu_byte luaH_get (Table *t, const TValue *key, TValue *res);
  111. LUAI_FUNC lu_byte luaH_getshortstr (Table *t, TString *key, TValue *res);
  112. LUAI_FUNC lu_byte luaH_getstr (Table *t, TString *key, TValue *res);
  113. LUAI_FUNC lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res);
  114. /* Special get for metamethods */
  115. LUAI_FUNC const TValue *luaH_Hgetshortstr (Table *t, TString *key);
  116. LUAI_FUNC int luaH_psetint (Table *t, lua_Integer key, TValue *val);
  117. LUAI_FUNC int luaH_psetshortstr (Table *t, TString *key, TValue *val);
  118. LUAI_FUNC int luaH_psetstr (Table *t, TString *key, TValue *val);
  119. LUAI_FUNC int luaH_pset (Table *t, const TValue *key, TValue *val);
  120. LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key,
  121. TValue *value);
  122. LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key,
  123. TValue *value);
  124. LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key,
  125. TValue *value, int hres);
  126. LUAI_FUNC Table *luaH_new (lua_State *L);
  127. LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned nasize,
  128. unsigned nhsize);
  129. LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned nasize);
  130. LUAI_FUNC lu_mem luaH_size (Table *t);
  131. LUAI_FUNC void luaH_free (lua_State *L, Table *t);
  132. LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key);
  133. LUAI_FUNC lua_Unsigned luaH_getn (lua_State *L, Table *t);
  134. #if defined(LUA_DEBUG)
  135. LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key);
  136. #endif
  137. #endif