diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-11-03 15:26:13 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-11-03 15:26:13 -0300 |
commit | 08a077d673b25cf1fbfe21794f240f4ff4999667 (patch) | |
tree | 77ae945c9a2680af23d5fa11c156482b35c14e04 /ltable.h | |
parent | 43c8e5bded052801f54a7439d18933b83570eb82 (diff) | |
download | lua-08a077d673b25cf1fbfe21794f240f4ff4999667.tar.gz lua-08a077d673b25cf1fbfe21794f240f4ff4999667.tar.bz2 lua-08a077d673b25cf1fbfe21794f240f4ff4999667.zip |
Full implementation of new representation for arrays
Diffstat (limited to 'ltable.h')
-rw-r--r-- | ltable.h | 63 |
1 files changed, 50 insertions, 13 deletions
@@ -51,31 +51,68 @@ | |||
51 | */ | 51 | */ |
52 | 52 | ||
53 | 53 | ||
54 | struct ArrayCell { | 54 | /* |
55 | lu_byte tt; | 55 | ** The array part of a table is represented by an array of cells. |
56 | ** Each cell is composed of (NM + 1) elements, and each element has the | ||
57 | ** type 'ArrayCell'. In each cell, only one element has the variant | ||
58 | ** 'tag', while the other NM elements have the variant 'value'. The | ||
59 | ** array in the 'tag' element holds the tags of the other elements in | ||
60 | ** that cell. | ||
61 | */ | ||
62 | #define NM ((unsigned int)sizeof(Value)) | ||
63 | |||
64 | union ArrayCell { | ||
65 | unsigned char tag[NM]; | ||
56 | Value value; | 66 | Value value; |
57 | }; | 67 | }; |
58 | 68 | ||
59 | 69 | ||
60 | /* fast access to components of array values */ | 70 | /* |
61 | #define getArrTag(t,k) (&(t)->array[k - 1].tt) | 71 | ** 'NMTag' defines which cell element has the tags; that could be any |
62 | #define getArrVal(t,k) (&(t)->array[k - 1].value) | 72 | ** value between 0 (tags come before all values) and NM (tags come after |
73 | ** all values). | ||
74 | */ | ||
75 | #define NMTag 0 | ||
63 | 76 | ||
64 | #define tagisempty(tag) (novariant(tag) == LUA_TNIL) | ||
65 | 77 | ||
78 | /* | ||
79 | ** Computes the concrete index that holds the tag of abstract index 'i' | ||
80 | */ | ||
81 | #define TagIndex(i) (((i)/NM * (NM + 1u)) + NMTag) | ||
66 | 82 | ||
67 | #define farr2val(h,k,tag,res) \ | 83 | /* |
68 | ((res)->tt_ = tag, (res)->value_ = *getArrVal(h,k)) | 84 | ** Computes the concrete index that holds the value of abstract index 'i' |
85 | */ | ||
86 | #define ValueIndex(i) ((i) + (((i) + (NM - NMTag))/NM)) | ||
69 | 87 | ||
70 | #define fval2arr(h,k,tag,val) \ | ||
71 | (*tag = (val)->tt_, *getArrVal(h,k) = (val)->value_) | ||
72 | 88 | ||
89 | /* Computes the address of the tag for the abstract index 'k' */ | ||
90 | #define getArrTag(t,k) (&(t)->array[TagIndex(k)].tag[(k)%NM]) | ||
73 | 91 | ||
74 | #define obj2arr(h,k,val) \ | 92 | /* Computes the address of the value for the abstract index 'k' */ |
75 | (*getArrTag(h,k) = (val)->tt_, *getArrVal(h,k) = (val)->value_) | 93 | #define getArrVal(t,k) (&(t)->array[ValueIndex(k)].value) |
76 | 94 | ||
95 | |||
96 | /* | ||
97 | ** Move TValues to/from arrays, using Lua indices | ||
98 | */ | ||
77 | #define arr2obj(h,k,val) \ | 99 | #define arr2obj(h,k,val) \ |
78 | ((val)->tt_ = *getArrTag(h,k), (val)->value_ = *getArrVal(h,k)) | 100 | ((val)->tt_ = *getArrTag(h,(k)-1u), (val)->value_ = *getArrVal(h,(k)-1u)) |
101 | |||
102 | #define obj2arr(h,k,val) \ | ||
103 | (*getArrTag(h,(k)-1u) = (val)->tt_, *getArrVal(h,(k)-1u) = (val)->value_) | ||
104 | |||
105 | |||
106 | /* | ||
107 | ** Often, we need to check the tag of a value before moving it. These | ||
108 | ** macros also move TValues to/from arrays, but receive the precomputed | ||
109 | ** tag value or address as an extra argument. | ||
110 | */ | ||
111 | #define farr2val(h,k,tag,res) \ | ||
112 | ((res)->tt_ = tag, (res)->value_ = *getArrVal(h,(k)-1u)) | ||
113 | |||
114 | #define fval2arr(h,k,tag,val) \ | ||
115 | (*tag = (val)->tt_, *getArrVal(h,(k)-1u) = (val)->value_) | ||
79 | 116 | ||
80 | 117 | ||
81 | LUAI_FUNC int luaH_getshortstr (Table *t, TString *key, TValue *res); | 118 | LUAI_FUNC int luaH_getshortstr (Table *t, TString *key, TValue *res); |