diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-11-07 17:26:15 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-11-07 17:26:15 -0300 |
| commit | 37c215b43f27a1c41e8a920987e1c3bd7b34330d (patch) | |
| tree | f14f4417384cffb9d2e5ef3c09621555a5d1e9a2 /ltable.h | |
| parent | 9e99f3071d07767f9e882c4abf3537f75ce2d161 (diff) | |
| parent | fa075b79530af1cbc977349f2e467d69ce01202c (diff) | |
| download | lua-37c215b43f27a1c41e8a920987e1c3bd7b34330d.tar.gz lua-37c215b43f27a1c41e8a920987e1c3bd7b34330d.tar.bz2 lua-37c215b43f27a1c41e8a920987e1c3bd7b34330d.zip | |
Merge branch 'newarray' into nextversion
Diffstat (limited to 'ltable.h')
| -rw-r--r-- | ltable.h | 99 |
1 files changed, 94 insertions, 5 deletions
| @@ -45,16 +45,105 @@ | |||
| 45 | #define nodefromval(v) cast(Node *, (v)) | 45 | #define nodefromval(v) cast(Node *, (v)) |
| 46 | 46 | ||
| 47 | 47 | ||
| 48 | LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); | 48 | /* results from get/pset */ |
| 49 | #define HOK 0 | ||
| 50 | #define HNOTFOUND 1 | ||
| 51 | #define HNOTATABLE 2 | ||
| 52 | #define HFIRSTNODE 3 | ||
| 53 | |||
| 54 | /* | ||
| 55 | ** Besides these values, pset (pre-set) operations may also return an | ||
| 56 | ** encoding of where the value should go (usually called 'hres'). That | ||
| 57 | ** means that there is a slot with that key but with no value. (pset | ||
| 58 | ** cannot set that value because there might be a metamethod.) If the | ||
| 59 | ** slot is in the hash part, the encoding is (HFIRSTNODE + hash index); | ||
| 60 | ** if the slot is in the array part, the encoding is (~array index). | ||
| 61 | */ | ||
| 62 | |||
| 63 | |||
| 64 | /* | ||
| 65 | ** The array part of a table is represented by an array of cells. | ||
| 66 | ** Each cell is composed of (NM + 1) elements, and each element has the | ||
| 67 | ** type 'ArrayCell'. In each cell, only one element has the variant | ||
| 68 | ** 'tag', while the other NM elements have the variant 'value'. The | ||
| 69 | ** array in the 'tag' element holds the tags of the other elements in | ||
| 70 | ** that cell. | ||
| 71 | */ | ||
| 72 | #define NM ((unsigned int)sizeof(Value)) | ||
| 73 | |||
| 74 | union ArrayCell { | ||
| 75 | unsigned char tag[NM]; | ||
| 76 | Value value; | ||
| 77 | }; | ||
| 78 | |||
| 79 | |||
| 80 | /* | ||
| 81 | ** 'NMTag' defines which cell element has the tags; that could be any | ||
| 82 | ** value between 0 (tags come before all values) and NM (tags come after | ||
| 83 | ** all values). | ||
| 84 | */ | ||
| 85 | #define NMTag 0 | ||
| 86 | |||
| 87 | |||
| 88 | /* | ||
| 89 | ** Computes the concrete index that holds the tag of abstract index 'i' | ||
| 90 | */ | ||
| 91 | #define TagIndex(i) (((i)/NM * (NM + 1u)) + NMTag) | ||
| 92 | |||
| 93 | /* | ||
| 94 | ** Computes the concrete index that holds the value of abstract index 'i' | ||
| 95 | */ | ||
| 96 | #define ValueIndex(i) ((i) + (((i) + (NM - NMTag))/NM)) | ||
| 97 | |||
| 98 | |||
| 99 | /* Computes the address of the tag for the abstract index 'k' */ | ||
| 100 | #define getArrTag(t,k) (&(t)->array[TagIndex(k)].tag[(k)%NM]) | ||
| 101 | |||
| 102 | /* Computes the address of the value for the abstract index 'k' */ | ||
| 103 | #define getArrVal(t,k) (&(t)->array[ValueIndex(k)].value) | ||
| 104 | |||
| 105 | |||
| 106 | /* | ||
| 107 | ** Move TValues to/from arrays, using Lua indices | ||
| 108 | */ | ||
| 109 | #define arr2obj(h,k,val) \ | ||
| 110 | ((val)->tt_ = *getArrTag(h,(k)-1u), (val)->value_ = *getArrVal(h,(k)-1u)) | ||
| 111 | |||
| 112 | #define obj2arr(h,k,val) \ | ||
| 113 | (*getArrTag(h,(k)-1u) = (val)->tt_, *getArrVal(h,(k)-1u) = (val)->value_) | ||
| 114 | |||
| 115 | |||
| 116 | /* | ||
| 117 | ** Often, we need to check the tag of a value before moving it. These | ||
| 118 | ** macros also move TValues to/from arrays, but receive the precomputed | ||
| 119 | ** tag value or address as an extra argument. | ||
| 120 | */ | ||
| 121 | #define farr2val(h,k,tag,res) \ | ||
| 122 | ((res)->tt_ = tag, (res)->value_ = *getArrVal(h,(k)-1u)) | ||
| 123 | |||
| 124 | #define fval2arr(h,k,tag,val) \ | ||
| 125 | (*tag = (val)->tt_, *getArrVal(h,(k)-1u) = (val)->value_) | ||
| 126 | |||
| 127 | |||
| 128 | LUAI_FUNC int luaH_getshortstr (Table *t, TString *key, TValue *res); | ||
| 129 | LUAI_FUNC int luaH_getstr (Table *t, TString *key, TValue *res); | ||
| 130 | LUAI_FUNC int luaH_get (Table *t, const TValue *key, TValue *res); | ||
| 131 | LUAI_FUNC int luaH_getint (Table *t, lua_Integer key, TValue *res); | ||
| 132 | |||
| 133 | LUAI_FUNC TString *luaH_getstrkey (Table *t, TString *key); | ||
| 134 | |||
| 135 | LUAI_FUNC int luaH_psetint (Table *t, lua_Integer key, TValue *val); | ||
| 136 | LUAI_FUNC int luaH_psetshortstr (Table *t, TString *key, TValue *val); | ||
| 137 | LUAI_FUNC int luaH_psetstr (Table *t, TString *key, TValue *val); | ||
| 138 | LUAI_FUNC int luaH_pset (Table *t, const TValue *key, TValue *val); | ||
| 139 | |||
| 49 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, | 140 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, |
| 50 | TValue *value); | 141 | TValue *value); |
| 51 | LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key); | 142 | LUAI_FUNC const TValue *luaH_Hgetshortstr (Table *t, TString *key); |
| 52 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); | ||
| 53 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); | ||
| 54 | LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key, | 143 | LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key, |
| 55 | TValue *value); | 144 | TValue *value); |
| 56 | LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key, | 145 | LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key, |
| 57 | const TValue *slot, TValue *value); | 146 | TValue *value, int aux); |
| 58 | LUAI_FUNC Table *luaH_new (lua_State *L); | 147 | LUAI_FUNC Table *luaH_new (lua_State *L); |
| 59 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, | 148 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, |
| 60 | unsigned int nhsize); | 149 | unsigned int nhsize); |
