ltable.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 &= ~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. /* results from get/pset */
  32. #define HOK 0
  33. #define HNOTFOUND 1
  34. #define HNOTATABLE 2
  35. #define HFIRSTNODE 3
  36. /*
  37. ** Besides these values, pset (pre-set) operations may also return an
  38. ** encoding of where the value should go (usually called 'hres'). That
  39. ** means that there is a slot with that key but with no value. (pset
  40. ** cannot set that value because there might be a metamethod.) If the
  41. ** slot is in the hash part, the encoding is (HFIRSTNODE + hash index);
  42. ** if the slot is in the array part, the encoding is (~array index).
  43. */
  44. /*
  45. ** The array part of a table is represented by an array of cells.
  46. ** Each cell is composed of (NM + 1) elements, and each element has the
  47. ** type 'ArrayCell'. In each cell, only one element has the variant
  48. ** 'tag', while the other NM elements have the variant 'value'. The
  49. ** array in the 'tag' element holds the tags of the other elements in
  50. ** that cell.
  51. */
  52. #define NM ((unsigned int)sizeof(Value))
  53. union ArrayCell {
  54. unsigned char tag[NM];
  55. Value value;
  56. };
  57. /*
  58. ** 'NMTag' defines which cell element has the tags; that could be any
  59. ** value between 0 (tags come before all values) and NM (tags come after
  60. ** all values).
  61. */
  62. #define NMTag 0
  63. /*
  64. ** Computes the concrete index that holds the tag of abstract index 'i'
  65. */
  66. #define TagIndex(i) (((i)/NM * (NM + 1u)) + NMTag)
  67. /*
  68. ** Computes the concrete index that holds the value of abstract index 'i'
  69. */
  70. #define ValueIndex(i) ((i) + (((i) + (NM - NMTag))/NM))
  71. /* Computes the address of the tag for the abstract index 'k' */
  72. #define getArrTag(t,k) (&(t)->array[TagIndex(k)].tag[(k)%NM])
  73. /* Computes the address of the value for the abstract index 'k' */
  74. #define getArrVal(t,k) (&(t)->array[ValueIndex(k)].value)
  75. /*
  76. ** Move TValues to/from arrays, using Lua indices
  77. */
  78. #define arr2obj(h,k,val) \
  79. ((val)->tt_ = *getArrTag(h,(k)-1u), (val)->value_ = *getArrVal(h,(k)-1u))
  80. #define obj2arr(h,k,val) \
  81. (*getArrTag(h,(k)-1u) = (val)->tt_, *getArrVal(h,(k)-1u) = (val)->value_)
  82. /*
  83. ** Often, we need to check the tag of a value before moving it. These
  84. ** macros also move TValues to/from arrays, but receive the precomputed
  85. ** tag value or address as an extra argument.
  86. */
  87. #define farr2val(h,k,tag,res) \
  88. ((res)->tt_ = tag, (res)->value_ = *getArrVal(h,(k)-1u))
  89. #define fval2arr(h,k,tag,val) \
  90. (*tag = (val)->tt_, *getArrVal(h,(k)-1u) = (val)->value_)
  91. LUAI_FUNC int luaH_getshortstr (Table *t, TString *key, TValue *res);
  92. LUAI_FUNC int luaH_getstr (Table *t, TString *key, TValue *res);
  93. LUAI_FUNC int luaH_get (Table *t, const TValue *key, TValue *res);
  94. LUAI_FUNC int luaH_getint (Table *t, lua_Integer key, TValue *res);
  95. LUAI_FUNC TString *luaH_getstrkey (Table *t, TString *key);
  96. LUAI_FUNC int luaH_psetint (Table *t, lua_Integer key, TValue *val);
  97. LUAI_FUNC int luaH_psetshortstr (Table *t, TString *key, TValue *val);
  98. LUAI_FUNC int luaH_psetstr (Table *t, TString *key, TValue *val);
  99. LUAI_FUNC int luaH_pset (Table *t, const TValue *key, TValue *val);
  100. LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key,
  101. TValue *value);
  102. LUAI_FUNC const TValue *luaH_Hgetshortstr (Table *t, TString *key);
  103. LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key,
  104. TValue *value);
  105. LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key,
  106. TValue *value, int aux);
  107. LUAI_FUNC Table *luaH_new (lua_State *L);
  108. LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
  109. unsigned int nhsize);
  110. LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize);
  111. LUAI_FUNC void luaH_free (lua_State *L, Table *t);
  112. LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key);
  113. LUAI_FUNC lua_Unsigned luaH_getn (Table *t);
  114. LUAI_FUNC unsigned int luaH_realasize (const Table *t);
  115. #if defined(LUA_DEBUG)
  116. LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key);
  117. #endif
  118. #endif