/src/testdir/build/lua-master/source/ltable.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ** $Id: ltable.c $ |
3 | | ** Lua tables (hash) |
4 | | ** See Copyright Notice in lua.h |
5 | | */ |
6 | | |
7 | | #define ltable_c |
8 | | #define LUA_CORE |
9 | | |
10 | | #include "lprefix.h" |
11 | | |
12 | | |
13 | | /* |
14 | | ** Implementation of tables (aka arrays, objects, or hash tables). |
15 | | ** Tables keep its elements in two parts: an array part and a hash part. |
16 | | ** Non-negative integer keys are all candidates to be kept in the array |
17 | | ** part. The actual size of the array is the largest 'n' such that |
18 | | ** more than half the slots between 1 and n are in use. |
19 | | ** Hash uses a mix of chained scatter table with Brent's variation. |
20 | | ** A main invariant of these tables is that, if an element is not |
21 | | ** in its main position (i.e. the 'original' position that its hash gives |
22 | | ** to it), then the colliding element is in its own main position. |
23 | | ** Hence even when the load factor reaches 100%, performance remains good. |
24 | | */ |
25 | | |
26 | | #include <math.h> |
27 | | #include <limits.h> |
28 | | #include <string.h> |
29 | | |
30 | | #include "lua.h" |
31 | | |
32 | | #include "ldebug.h" |
33 | | #include "ldo.h" |
34 | | #include "lgc.h" |
35 | | #include "lmem.h" |
36 | | #include "lobject.h" |
37 | | #include "lstate.h" |
38 | | #include "lstring.h" |
39 | | #include "ltable.h" |
40 | | #include "lvm.h" |
41 | | |
42 | | |
43 | | /* |
44 | | ** Only tables with hash parts larger than 2^LIMFORLAST has a 'lastfree' |
45 | | ** field that optimizes finding a free slot. That field is stored just |
46 | | ** before the array of nodes, in the same block. Smaller tables do a |
47 | | ** complete search when looking for a free slot. |
48 | | */ |
49 | 678k | #define LIMFORLAST 2 /* log2 of real limit */ |
50 | | |
51 | | /* |
52 | | ** The union 'Limbox' stores 'lastfree' and ensures that what follows it |
53 | | ** is properly aligned to store a Node. |
54 | | */ |
55 | | typedef struct { Node *dummy; Node follows_pNode; } Limbox_aux; |
56 | | |
57 | | typedef union { |
58 | | Node *lastfree; |
59 | | char padding[offsetof(Limbox_aux, follows_pNode)]; |
60 | | } Limbox; |
61 | | |
62 | 529k | #define haslastfree(t) ((t)->lsizenode > LIMFORLAST) |
63 | 518k | #define getlastfree(t) ((cast(Limbox *, (t)->node) - 1)->lastfree) |
64 | | |
65 | | |
66 | | /* |
67 | | ** MAXABITS is the largest integer such that 2^MAXABITS fits in an |
68 | | ** unsigned int. |
69 | | */ |
70 | 6.52M | #define MAXABITS cast_int(sizeof(int) * CHAR_BIT - 1) |
71 | | |
72 | | |
73 | | /* |
74 | | ** MAXASIZEB is the maximum number of elements in the array part such |
75 | | ** that the size of the array fits in 'size_t'. |
76 | | */ |
77 | 404k | #define MAXASIZEB (MAX_SIZET/(sizeof(Value) + 1)) |
78 | | |
79 | | |
80 | | /* |
81 | | ** MAXASIZE is the maximum size of the array part. It is the minimum |
82 | | ** between 2^MAXABITS and MAXASIZEB. |
83 | | */ |
84 | | #define MAXASIZE \ |
85 | 404k | (((1u << MAXABITS) < MAXASIZEB) ? (1u << MAXABITS) : cast_uint(MAXASIZEB)) |
86 | | |
87 | | /* |
88 | | ** MAXHBITS is the largest integer such that 2^MAXHBITS fits in a |
89 | | ** signed int. |
90 | | */ |
91 | 298k | #define MAXHBITS (MAXABITS - 1) |
92 | | |
93 | | |
94 | | /* |
95 | | ** MAXHSIZE is the maximum size of the hash part. It is the minimum |
96 | | ** between 2^MAXHBITS and the maximum size such that, measured in bytes, |
97 | | ** it fits in a 'size_t'. |
98 | | */ |
99 | 149k | #define MAXHSIZE luaM_limitN(1u << MAXHBITS, Node) |
100 | | |
101 | | |
102 | | /* |
103 | | ** When the original hash value is good, hashing by a power of 2 |
104 | | ** avoids the cost of '%'. |
105 | | */ |
106 | 58.8M | #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) |
107 | | |
108 | | /* |
109 | | ** for other types, it is better to avoid modulo by power of 2, as |
110 | | ** they can have many 2 factors. |
111 | | */ |
112 | 1.33M | #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) |
113 | | |
114 | | |
115 | 58.6M | #define hashstr(t,str) hashpow2(t, (str)->hash) |
116 | 1.89k | #define hashboolean(t,p) hashpow2(t, p) |
117 | | |
118 | | |
119 | 517 | #define hashpointer(t,p) hashmod(t, point2uint(p)) |
120 | | |
121 | | |
122 | | #define dummynode (&dummynode_) |
123 | | |
124 | | static const Node dummynode_ = { |
125 | | {{NULL}, LUA_VEMPTY, /* value's value and type */ |
126 | | LUA_VNIL, 0, {NULL}} /* key type, next, and key value */ |
127 | | }; |
128 | | |
129 | | |
130 | | static const TValue absentkey = {ABSTKEYCONSTANT}; |
131 | | |
132 | | |
133 | | /* |
134 | | ** Hash for integers. To allow a good hash, use the remainder operator |
135 | | ** ('%'). If integer fits as a non-negative int, compute an int |
136 | | ** remainder, which is faster. Otherwise, use an unsigned-integer |
137 | | ** remainder, which uses all bits and ensures a non-negative result. |
138 | | */ |
139 | 1.14M | static Node *hashint (const Table *t, lua_Integer i) { |
140 | 1.14M | lua_Unsigned ui = l_castS2U(i); |
141 | 1.14M | if (ui <= cast_uint(INT_MAX)) |
142 | 751k | return hashmod(t, cast_int(ui)); |
143 | 394k | else |
144 | 394k | return hashmod(t, ui); |
145 | 1.14M | } |
146 | | |
147 | | |
148 | | /* |
149 | | ** Hash for floating-point numbers. |
150 | | ** The main computation should be just |
151 | | ** n = frexp(n, &i); return (n * INT_MAX) + i |
152 | | ** but there are some numerical subtleties. |
153 | | ** In a two-complement representation, INT_MAX does not has an exact |
154 | | ** representation as a float, but INT_MIN does; because the absolute |
155 | | ** value of 'frexp' is smaller than 1 (unless 'n' is inf/NaN), the |
156 | | ** absolute value of the product 'frexp * -INT_MIN' is smaller or equal |
157 | | ** to INT_MAX. Next, the use of 'unsigned int' avoids overflows when |
158 | | ** adding 'i'; the use of '~u' (instead of '-u') avoids problems with |
159 | | ** INT_MIN. |
160 | | */ |
161 | | #if !defined(l_hashfloat) |
162 | 192k | static int l_hashfloat (lua_Number n) { |
163 | 192k | int i; |
164 | 192k | lua_Integer ni; |
165 | 192k | n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN); |
166 | 192k | if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */ |
167 | 11.2k | lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL)); |
168 | 11.2k | return 0; |
169 | 11.2k | } |
170 | 181k | else { /* normal case */ |
171 | 181k | unsigned int u = cast_uint(i) + cast_uint(ni); |
172 | 181k | return cast_int(u <= cast_uint(INT_MAX) ? u : ~u); |
173 | 181k | } |
174 | 192k | } |
175 | | #endif |
176 | | |
177 | | |
178 | | /* |
179 | | ** returns the 'main' position of an element in a table (that is, |
180 | | ** the index of its hash value). |
181 | | */ |
182 | 1.31M | static Node *mainpositionTV (const Table *t, const TValue *key) { |
183 | 1.31M | switch (ttypetag(key)) { |
184 | 370k | case LUA_VNUMINT: { |
185 | 370k | lua_Integer i = ivalue(key); |
186 | 0 | return hashint(t, i); |
187 | 370k | } |
188 | 192k | case LUA_VNUMFLT: { |
189 | 192k | lua_Number n = fltvalue(key); |
190 | 192k | return hashmod(t, l_hashfloat(n)); |
191 | 192k | } |
192 | 549k | case LUA_VSHRSTR: { |
193 | 1.09M | TString *ts = tsvalue(key); |
194 | 1.09M | return hashstr(t, ts); |
195 | 1.09M | } |
196 | 203k | case LUA_VLNGSTR: { |
197 | 407k | TString *ts = tsvalue(key); |
198 | 407k | return hashpow2(t, luaS_hashlongstr(ts)); |
199 | 407k | } |
200 | 21 | case LUA_VFALSE: |
201 | 21 | return hashboolean(t, 0); |
202 | 1.87k | case LUA_VTRUE: |
203 | 1.87k | return hashboolean(t, 1); |
204 | 0 | case LUA_VLIGHTUSERDATA: { |
205 | 0 | void *p = pvalue(key); |
206 | 0 | return hashpointer(t, p); |
207 | 0 | } |
208 | 0 | case LUA_VLCF: { |
209 | 0 | lua_CFunction f = fvalue(key); |
210 | 0 | return hashpointer(t, f); |
211 | 0 | } |
212 | 517 | default: { |
213 | 517 | GCObject *o = gcvalue(key); |
214 | 517 | return hashpointer(t, o); |
215 | 517 | } |
216 | 1.31M | } |
217 | 1.31M | } |
218 | | |
219 | | |
220 | 230k | l_sinline Node *mainpositionfromnode (const Table *t, Node *nd) { |
221 | 230k | TValue key; |
222 | 230k | getnodekey(cast(lua_State *, NULL), &key, nd); |
223 | 230k | return mainpositionTV(t, &key); |
224 | 230k | } |
225 | | |
226 | | |
227 | | /* |
228 | | ** Check whether key 'k1' is equal to the key in node 'n2'. This |
229 | | ** equality is raw, so there are no metamethods. Floats with integer |
230 | | ** values have been normalized, so integers cannot be equal to |
231 | | ** floats. It is assumed that 'eqshrstr' is simply pointer equality, so |
232 | | ** that short strings are handled in the default case. |
233 | | ** A true 'deadok' means to accept dead keys as equal to their original |
234 | | ** values. All dead keys are compared in the default case, by pointer |
235 | | ** identity. (Only collectable objects can produce dead keys.) Note that |
236 | | ** dead long strings are also compared by identity. |
237 | | ** Once a key is dead, its corresponding value may be collected, and |
238 | | ** then another value can be created with the same address. If this |
239 | | ** other value is given to 'next', 'equalkey' will signal a false |
240 | | ** positive. In a regular traversal, this situation should never happen, |
241 | | ** as all keys given to 'next' came from the table itself, and therefore |
242 | | ** could not have been collected. Outside a regular traversal, we |
243 | | ** have garbage in, garbage out. What is relevant is that this false |
244 | | ** positive does not break anything. (In particular, 'next' will return |
245 | | ** some other valid item on the table or nil.) |
246 | | */ |
247 | 512k | static int equalkey (const TValue *k1, const Node *n2, int deadok) { |
248 | 512k | if ((rawtt(k1) != keytt(n2)) && /* not the same variants? */ |
249 | 512k | !(deadok && keyisdead(n2) && iscollectable(k1))) |
250 | 128k | return 0; /* cannot be same key */ |
251 | 384k | switch (keytt(n2)) { |
252 | 980 | case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: |
253 | 980 | return 1; |
254 | 0 | case LUA_VNUMINT: |
255 | 0 | return (ivalue(k1) == keyival(n2)); |
256 | 181k | case LUA_VNUMFLT: |
257 | 181k | return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2))); |
258 | 0 | case LUA_VLIGHTUSERDATA: |
259 | 0 | return pvalue(k1) == pvalueraw(keyval(n2)); |
260 | 0 | case LUA_VLCF: |
261 | 0 | return fvalue(k1) == fvalueraw(keyval(n2)); |
262 | 201k | case ctb(LUA_VLNGSTR): |
263 | 403k | return luaS_eqlngstr(tsvalue(k1), keystrval(n2)); |
264 | 463 | default: |
265 | 463 | return gcvalue(k1) == gcvalueraw(keyval(n2)); |
266 | 384k | } |
267 | 384k | } |
268 | | |
269 | | |
270 | | /* |
271 | | ** True if value of 'alimit' is equal to the real size of the array |
272 | | ** part of table 't'. (Otherwise, the array part must be larger than |
273 | | ** 'alimit'.) |
274 | | */ |
275 | 1.64M | #define limitequalsasize(t) (isrealasize(t) || ispow2((t)->alimit)) |
276 | | |
277 | | |
278 | | /* |
279 | | ** Returns the real size of the 'array' array |
280 | | */ |
281 | 1.55M | LUAI_FUNC unsigned int luaH_realasize (const Table *t) { |
282 | 1.55M | if (limitequalsasize(t)) |
283 | 1.20M | return t->alimit; /* this is the size */ |
284 | 347k | else { |
285 | 347k | unsigned int size = t->alimit; |
286 | | /* compute the smallest power of 2 not smaller than 'size' */ |
287 | 347k | size |= (size >> 1); |
288 | 347k | size |= (size >> 2); |
289 | 347k | size |= (size >> 4); |
290 | 347k | size |= (size >> 8); |
291 | 347k | #if (UINT_MAX >> 14) > 3 /* unsigned int has more than 16 bits */ |
292 | 347k | size |= (size >> 16); |
293 | | #if (UINT_MAX >> 30) > 3 |
294 | | size |= (size >> 32); /* unsigned int has more than 32 bits */ |
295 | | #endif |
296 | 347k | #endif |
297 | 347k | size++; |
298 | 347k | lua_assert(ispow2(size) && size/2 < t->alimit && t->alimit < size); |
299 | 347k | return size; |
300 | 347k | } |
301 | 1.55M | } |
302 | | |
303 | | |
304 | | /* |
305 | | ** Check whether real size of the array is a power of 2. |
306 | | ** (If it is not, 'alimit' cannot be changed to any other value |
307 | | ** without changing the real size.) |
308 | | */ |
309 | 100k | static int ispow2realasize (const Table *t) { |
310 | 100k | return (!isrealasize(t) || ispow2(t->alimit)); |
311 | 100k | } |
312 | | |
313 | | |
314 | 353k | static unsigned int setlimittosize (Table *t) { |
315 | 353k | t->alimit = luaH_realasize(t); |
316 | 353k | setrealasize(t); |
317 | 353k | return t->alimit; |
318 | 353k | } |
319 | | |
320 | | |
321 | 149k | #define limitasasize(t) check_exp(isrealasize(t), t->alimit) |
322 | | |
323 | | |
324 | | |
325 | | /* |
326 | | ** "Generic" get version. (Not that generic: not valid for integers, |
327 | | ** which may be in array part, nor for floats with integral values.) |
328 | | ** See explanation about 'deadok' in function 'equalkey'. |
329 | | */ |
330 | 358k | static const TValue *getgeneric (Table *t, const TValue *key, int deadok) { |
331 | 358k | Node *n = mainpositionTV(t, key); |
332 | 512k | for (;;) { /* check whether 'key' is somewhere in the chain */ |
333 | 512k | if (equalkey(key, n, deadok)) |
334 | 320k | return gval(n); /* that's it */ |
335 | 191k | else { |
336 | 191k | int nx = gnext(n); |
337 | 191k | if (nx == 0) |
338 | 38.1k | return &absentkey; /* not found */ |
339 | 153k | n += nx; |
340 | 153k | } |
341 | 512k | } |
342 | 358k | } |
343 | | |
344 | | |
345 | | /* |
346 | | ** returns the index for 'k' if 'k' is an appropriate key to live in |
347 | | ** the array part of a table, 0 otherwise. |
348 | | */ |
349 | 200k | static unsigned int arrayindex (lua_Integer k) { |
350 | 200k | if (l_castS2U(k) - 1u < MAXASIZE) /* 'k' in [1, MAXASIZE]? */ |
351 | 154k | return cast_uint(k); /* 'key' is an appropriate array index */ |
352 | 46.1k | else |
353 | 46.1k | return 0; |
354 | 200k | } |
355 | | |
356 | | |
357 | | /* |
358 | | ** returns the index of a 'key' for table traversals. First goes all |
359 | | ** elements in the array part, then elements in the hash part. The |
360 | | ** beginning of a traversal is signaled by 0. |
361 | | */ |
362 | | static unsigned findindex (lua_State *L, Table *t, TValue *key, |
363 | 0 | unsigned asize) { |
364 | 0 | unsigned int i; |
365 | 0 | if (ttisnil(key)) return 0; /* first iteration */ |
366 | 0 | i = ttisinteger(key) ? arrayindex(ivalue(key)) : 0; |
367 | 0 | if (i - 1u < asize) /* is 'key' inside array part? */ |
368 | 0 | return i; /* yes; that's the index */ |
369 | 0 | else { |
370 | 0 | const TValue *n = getgeneric(t, key, 1); |
371 | 0 | if (l_unlikely(isabstkey(n))) |
372 | 0 | luaG_runerror(L, "invalid key to 'next'"); /* key not found */ |
373 | 0 | i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ |
374 | | /* hash elements are numbered after array ones */ |
375 | 0 | return (i + 1) + asize; |
376 | 0 | } |
377 | 0 | } |
378 | | |
379 | | |
380 | 0 | int luaH_next (lua_State *L, Table *t, StkId key) { |
381 | 0 | unsigned int asize = luaH_realasize(t); |
382 | 0 | unsigned int i = findindex(L, t, s2v(key), asize); /* find original key */ |
383 | 0 | for (; i < asize; i++) { /* try first array part */ |
384 | 0 | int tag = *getArrTag(t, i); |
385 | 0 | if (!tagisempty(tag)) { /* a non-empty entry? */ |
386 | 0 | setivalue(s2v(key), i + 1); |
387 | 0 | farr2val(t, i + 1, tag, s2v(key + 1)); |
388 | 0 | return 1; |
389 | 0 | } |
390 | 0 | } |
391 | 0 | for (i -= asize; cast_int(i) < sizenode(t); i++) { /* hash part */ |
392 | 0 | if (!isempty(gval(gnode(t, i)))) { /* a non-empty entry? */ |
393 | 0 | Node *n = gnode(t, i); |
394 | 0 | getnodekey(L, s2v(key), n); |
395 | 0 | setobj2s(L, key + 1, gval(n)); |
396 | 0 | return 1; |
397 | 0 | } |
398 | 0 | } |
399 | 0 | return 0; /* no more elements */ |
400 | 0 | } |
401 | | |
402 | | |
403 | 330k | static void freehash (lua_State *L, Table *t) { |
404 | 330k | if (!isdummy(t)) { |
405 | 149k | size_t bsize = sizenode(t) * sizeof(Node); /* 'node' size in bytes */ |
406 | 149k | char *arr = cast_charp(t->node); |
407 | 149k | if (haslastfree(t)) { |
408 | 30.3k | bsize += sizeof(Limbox); |
409 | 30.3k | arr -= sizeof(Limbox); |
410 | 30.3k | } |
411 | 149k | luaM_freearray(L, arr, bsize); |
412 | 149k | } |
413 | 330k | } |
414 | | |
415 | | |
416 | | /* |
417 | | ** Check whether an integer key is in the array part. If 'alimit' is |
418 | | ** not the real size of the array, the key still can be in the array |
419 | | ** part. In this case, do the "Xmilia trick" to check whether 'key-1' |
420 | | ** is smaller than the real size. |
421 | | ** The trick works as follow: let 'p' be the integer such that |
422 | | ** '2^(p+1) >= alimit > 2^p', or '2^(p+1) > alimit-1 >= 2^p'. That is, |
423 | | ** 'p' is the highest 1-bit in 'alimit-1', and 2^(p+1) is the real size |
424 | | ** of the array. What we have to check becomes 'key-1 < 2^(p+1)'. We |
425 | | ** compute '(key-1) & ~(alimit-1)', which we call 'res'; it will have |
426 | | ** the 'p' bit cleared. (It may also clear other bits smaller than 'p', |
427 | | ** but no bit higher than 'p'.) If the key is outside the array, that |
428 | | ** is, 'key-1 >= 2^(p+1)', then 'res' will have some 1-bit higher than |
429 | | ** 'p', therefore it will be larger or equal to 'alimit', and the check |
430 | | ** will fail. If 'key-1 < 2^(p+1)', then 'res' has no 1-bit higher than |
431 | | ** 'p', and as the bit 'p' itself was cleared, 'res' will be smaller |
432 | | ** than 2^p, therefore smaller than 'alimit', and the check succeeds. |
433 | | ** As special cases, when 'alimit' is 0 the condition is trivially false, |
434 | | ** and when 'alimit' is 1 the condition simplifies to 'key-1 < alimit'. |
435 | | ** If key is 0 or negative, 'res' will have its higher bit on, so that |
436 | | ** it cannot be smaller than 'alimit'. |
437 | | */ |
438 | 889k | static int keyinarray (Table *t, lua_Integer key) { |
439 | 889k | lua_Unsigned alimit = t->alimit; |
440 | 889k | if (l_castS2U(key) - 1u < alimit) /* 'key' in [1, t->alimit]? */ |
441 | 127k | return 1; |
442 | 761k | else if (!isrealasize(t) && /* key still may be in the array part? */ |
443 | 761k | (((l_castS2U(key) - 1u) & ~(alimit - 1u)) < alimit)) { |
444 | 547 | t->alimit = cast_uint(key); /* probably '#t' is here now */ |
445 | 547 | return 1; |
446 | 547 | } |
447 | 761k | else |
448 | 761k | return 0; |
449 | 889k | } |
450 | | |
451 | | |
452 | | /* |
453 | | ** {============================================================= |
454 | | ** Rehash |
455 | | ** ============================================================== |
456 | | */ |
457 | | |
458 | | /* |
459 | | ** Compute the optimal size for the array part of table 't'. 'nums' is a |
460 | | ** "count array" where 'nums[i]' is the number of integers in the table |
461 | | ** between 2^(i - 1) + 1 and 2^i. 'pna' enters with the total number of |
462 | | ** integer keys in the table and leaves with the number of keys that |
463 | | ** will go to the array part; return the optimal size. (The condition |
464 | | ** 'twotoi > 0' in the for loop stops the loop if 'twotoi' overflows.) |
465 | | */ |
466 | 149k | static unsigned computesizes (unsigned nums[], unsigned *pna) { |
467 | 149k | int i; |
468 | 149k | unsigned int twotoi; /* 2^i (candidate for optimal size) */ |
469 | 149k | unsigned int a = 0; /* number of elements smaller than 2^i */ |
470 | 149k | unsigned int na = 0; /* number of elements to go to array part */ |
471 | 149k | unsigned int optimal = 0; /* optimal size for array part */ |
472 | | /* loop while keys can fill more than half of total size */ |
473 | 149k | for (i = 0, twotoi = 1; |
474 | 592k | twotoi > 0 && *pna > twotoi / 2; |
475 | 443k | i++, twotoi *= 2) { |
476 | 443k | a += nums[i]; |
477 | 443k | if (a > twotoi/2) { /* more than half elements present? */ |
478 | 439k | optimal = twotoi; /* optimal size (till now) */ |
479 | 439k | na = a; /* all elements up to 'optimal' will go to array part */ |
480 | 439k | } |
481 | 443k | } |
482 | 149k | lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal); |
483 | 149k | *pna = na; |
484 | 149k | return optimal; |
485 | 149k | } |
486 | | |
487 | | |
488 | 200k | static int countint (lua_Integer key, unsigned int *nums) { |
489 | 200k | unsigned int k = arrayindex(key); |
490 | 200k | if (k != 0) { /* is 'key' an appropriate array index? */ |
491 | 154k | nums[luaO_ceillog2(k)]++; /* count as such */ |
492 | 154k | return 1; |
493 | 154k | } |
494 | 46.1k | else |
495 | 46.1k | return 0; |
496 | 200k | } |
497 | | |
498 | | |
499 | 1.00M | l_sinline int arraykeyisempty (const Table *t, lua_Integer key) { |
500 | 1.00M | int tag = *getArrTag(t, key - 1); |
501 | 1.00M | return tagisempty(tag); |
502 | 1.00M | } |
503 | | |
504 | | |
505 | | /* |
506 | | ** Count keys in array part of table 't': Fill 'nums[i]' with |
507 | | ** number of keys that will go into corresponding slice and return |
508 | | ** total number of non-nil keys. |
509 | | */ |
510 | 149k | static unsigned numusearray (const Table *t, unsigned *nums) { |
511 | 149k | int lg; |
512 | 149k | unsigned int ttlg; /* 2^lg */ |
513 | 149k | unsigned int ause = 0; /* summation of 'nums' */ |
514 | 149k | unsigned int i = 1; /* count to traverse all array keys */ |
515 | 149k | unsigned int asize = limitasasize(t); /* real array size */ |
516 | | /* traverse each slice */ |
517 | 631k | for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) { |
518 | 631k | unsigned int lc = 0; /* counter */ |
519 | 631k | unsigned int lim = ttlg; |
520 | 631k | if (lim > asize) { |
521 | 193k | lim = asize; /* adjust upper limit */ |
522 | 193k | if (i > lim) |
523 | 149k | break; /* no more elements to count */ |
524 | 193k | } |
525 | | /* count elements in range (2^(lg - 1), 2^lg] */ |
526 | 1.12M | for (; i <= lim; i++) { |
527 | 641k | if (!arraykeyisempty(t, i)) |
528 | 451k | lc++; |
529 | 641k | } |
530 | 482k | nums[lg] += lc; |
531 | 482k | ause += lc; |
532 | 482k | } |
533 | 149k | return ause; |
534 | 149k | } |
535 | | |
536 | | |
537 | 149k | static int numusehash (const Table *t, unsigned *nums, unsigned *pna) { |
538 | 149k | int totaluse = 0; /* total number of elements */ |
539 | 149k | int ause = 0; /* elements added to 'nums' (can go to array part) */ |
540 | 149k | int i = sizenode(t); |
541 | 512k | while (i--) { |
542 | 363k | Node *n = &t->node[i]; |
543 | 363k | if (!isempty(gval(n))) { |
544 | 318k | if (keyisinteger(n)) |
545 | 135k | ause += countint(keyival(n), nums); |
546 | 318k | totaluse++; |
547 | 318k | } |
548 | 363k | } |
549 | 149k | *pna += ause; |
550 | 149k | return totaluse; |
551 | 149k | } |
552 | | |
553 | | |
554 | | /* |
555 | | ** Convert an "abstract size" (number of slots in an array) to |
556 | | ** "concrete size" (number of bytes in the array). |
557 | | */ |
558 | 243k | static size_t concretesize (unsigned int size) { |
559 | 243k | return size * sizeof(Value) + size; /* space for the two arrays */ |
560 | 243k | } |
561 | | |
562 | | |
563 | | /* |
564 | | ** Resize the array part of a table. If new size is equal to the old, |
565 | | ** do nothing. Else, if new size is zero, free the old array. (It must |
566 | | ** be present, as the sizes are different.) Otherwise, allocate a new |
567 | | ** array, move the common elements to new proper position, and then |
568 | | ** frees old array. |
569 | | ** When array grows, we could reallocate it, but we still would need |
570 | | ** to move the elements to their new position, so the copy implicit |
571 | | ** in realloc is a waste. When array shrinks, it always erases some |
572 | | ** elements that should still be in the array, so we must reallocate in |
573 | | ** two steps anyway. It is simpler to always reallocate in two steps. |
574 | | */ |
575 | | static Value *resizearray (lua_State *L , Table *t, |
576 | | unsigned oldasize, |
577 | 330k | unsigned newasize) { |
578 | 330k | if (oldasize == newasize) |
579 | 175k | return t->array; /* nothing to be done */ |
580 | 154k | else if (newasize == 0) { /* erasing array? */ |
581 | 55.1k | Value *op = t->array - oldasize; /* original array's real address */ |
582 | 55.1k | luaM_freemem(L, op, concretesize(oldasize)); /* free it */ |
583 | 55.1k | return NULL; |
584 | 55.1k | } |
585 | 99.5k | else { |
586 | 99.5k | size_t newasizeb = concretesize(newasize); |
587 | 99.5k | Value *np = cast(Value *, |
588 | 99.5k | luaM_reallocvector(L, NULL, 0, newasizeb, lu_byte)); |
589 | 99.5k | if (np == NULL) /* allocation error? */ |
590 | 0 | return NULL; |
591 | 99.5k | if (oldasize > 0) { |
592 | 44.3k | Value *op = t->array - oldasize; /* real original array */ |
593 | 44.3k | unsigned tomove = (oldasize < newasize) ? oldasize : newasize; |
594 | 44.3k | lua_assert(tomove > 0); |
595 | | /* move common elements to new position */ |
596 | 44.3k | memcpy(np + newasize - tomove, |
597 | 44.3k | op + oldasize - tomove, |
598 | 44.3k | concretesize(tomove)); |
599 | 44.3k | luaM_freemem(L, op, concretesize(oldasize)); |
600 | 44.3k | } |
601 | 99.5k | return np + newasize; /* shift pointer to the end of value segment */ |
602 | 99.5k | } |
603 | 330k | } |
604 | | |
605 | | |
606 | | /* |
607 | | ** Creates an array for the hash part of a table with the given |
608 | | ** size, or reuses the dummy node if size is zero. |
609 | | ** The computation for size overflow is in two steps: the first |
610 | | ** comparison ensures that the shift in the second one does not |
611 | | ** overflow. |
612 | | */ |
613 | 330k | static void setnodevector (lua_State *L, Table *t, unsigned size) { |
614 | 330k | if (size == 0) { /* no elements to hash part? */ |
615 | 180k | t->node = cast(Node *, dummynode); /* use common 'dummynode' */ |
616 | 180k | t->lsizenode = 0; |
617 | 180k | setdummy(t); /* signal that it is using dummy node */ |
618 | 180k | } |
619 | 149k | else { |
620 | 149k | int i; |
621 | 149k | int lsize = luaO_ceillog2(size); |
622 | 149k | if (lsize > MAXHBITS || (1u << lsize) > MAXHSIZE) |
623 | 0 | luaG_runerror(L, "table overflow"); |
624 | 149k | size = twoto(lsize); |
625 | 149k | if (lsize <= LIMFORLAST) /* no 'lastfree' field? */ |
626 | 119k | t->node = luaM_newvector(L, size, Node); |
627 | 30.3k | else { |
628 | 30.3k | size_t bsize = size * sizeof(Node) + sizeof(Limbox); |
629 | 30.3k | char *node = luaM_newblock(L, bsize); |
630 | 30.3k | t->node = cast(Node *, node + sizeof(Limbox)); |
631 | 30.3k | getlastfree(t) = gnode(t, size); /* all positions are free */ |
632 | 30.3k | } |
633 | 149k | t->lsizenode = cast_byte(lsize); |
634 | 149k | setnodummy(t); |
635 | 807k | for (i = 0; i < cast_int(size); i++) { |
636 | 658k | Node *n = gnode(t, i); |
637 | 658k | gnext(n) = 0; |
638 | 658k | setnilkey(n); |
639 | 658k | setempty(gval(n)); |
640 | 658k | } |
641 | 149k | } |
642 | 330k | } |
643 | | |
644 | | |
645 | | /* |
646 | | ** (Re)insert all elements from the hash part of 'ot' into table 't'. |
647 | | */ |
648 | 204k | static void reinsert (lua_State *L, Table *ot, Table *t) { |
649 | 204k | int j; |
650 | 204k | int size = sizenode(ot); |
651 | 622k | for (j = 0; j < size; j++) { |
652 | 418k | Node *old = gnode(ot, j); |
653 | 418k | if (!isempty(gval(old))) { |
654 | | /* doesn't need barrier/invalidate cache, as entry was |
655 | | already present in the table */ |
656 | 318k | TValue k; |
657 | 318k | getnodekey(L, &k, old); |
658 | 318k | luaH_set(L, t, &k, gval(old)); |
659 | 318k | } |
660 | 418k | } |
661 | 204k | } |
662 | | |
663 | | |
664 | | /* |
665 | | ** Exchange the hash part of 't1' and 't2'. (In 'flags', only the |
666 | | ** dummy bit must be exchanged: The 'isrealasize' is not related |
667 | | ** to the hash part, and the metamethod bits do not change during |
668 | | ** a resize, so the "real" table can keep their values.) |
669 | | */ |
670 | 292k | static void exchangehashpart (Table *t1, Table *t2) { |
671 | 292k | lu_byte lsizenode = t1->lsizenode; |
672 | 292k | Node *node = t1->node; |
673 | 292k | int bitdummy1 = t1->flags & BITDUMMY; |
674 | 292k | t1->lsizenode = t2->lsizenode; |
675 | 292k | t1->node = t2->node; |
676 | 292k | t1->flags = (t1->flags & NOTBITDUMMY) | (t2->flags & BITDUMMY); |
677 | 292k | t2->lsizenode = lsizenode; |
678 | 292k | t2->node = node; |
679 | 292k | t2->flags = (t2->flags & NOTBITDUMMY) | bitdummy1; |
680 | 292k | } |
681 | | |
682 | | |
683 | | /* |
684 | | ** Re-insert into the new hash part of a table the elements from the |
685 | | ** vanishing slice of the array part. |
686 | | */ |
687 | | static void reinsertOldSlice (lua_State *L, Table *t, unsigned oldasize, |
688 | 44.1k | unsigned newasize) { |
689 | 44.1k | unsigned i; |
690 | 44.1k | t->alimit = newasize; /* pretend array has new size... */ |
691 | 88.3k | for (i = newasize; i < oldasize; i++) { /* traverse vanishing slice */ |
692 | 44.1k | int tag = *getArrTag(t, i); |
693 | 44.1k | if (!tagisempty(tag)) { /* a non-empty entry? */ |
694 | 42 | TValue aux; |
695 | 42 | farr2val(t, i + 1, tag, &aux); /* copy entry into 'aux' */ |
696 | 42 | luaH_setint(L, t, i + 1, &aux); /* re-insert it into the table */ |
697 | 42 | } |
698 | 44.1k | } |
699 | 44.1k | t->alimit = oldasize; /* restore current size... */ |
700 | 44.1k | } |
701 | | |
702 | | |
703 | | /* |
704 | | ** Clear new slice of the array. |
705 | | */ |
706 | 204k | static void clearNewSlice (Table *t, unsigned oldasize, unsigned newasize) { |
707 | 2.11M | for (; oldasize < newasize; oldasize++) |
708 | 1.90M | *getArrTag(t, oldasize) = LUA_VEMPTY; |
709 | 204k | } |
710 | | |
711 | | |
712 | | /* |
713 | | ** Resize table 't' for the new given sizes. Both allocations (for |
714 | | ** the hash part and for the array part) can fail, which creates some |
715 | | ** subtleties. If the first allocation, for the hash part, fails, an |
716 | | ** error is raised and that is it. Otherwise, it copies the elements from |
717 | | ** the shrinking part of the array (if it is shrinking) into the new |
718 | | ** hash. Then it reallocates the array part. If that fails, the table |
719 | | ** is in its original state; the function frees the new hash part and then |
720 | | ** raises the allocation error. Otherwise, it sets the new hash part |
721 | | ** into the table, initializes the new part of the array (if any) with |
722 | | ** nils and reinserts the elements of the old hash back into the new |
723 | | ** parts of the table. |
724 | | */ |
725 | | void luaH_resize (lua_State *L, Table *t, unsigned newasize, |
726 | 204k | unsigned nhsize) { |
727 | 204k | Table newt; /* to keep the new hash part */ |
728 | 204k | unsigned int oldasize = setlimittosize(t); |
729 | 204k | Value *newarray; |
730 | 204k | if (newasize > MAXASIZE) |
731 | 0 | luaG_runerror(L, "table overflow"); |
732 | | /* create new hash part with appropriate size into 'newt' */ |
733 | 204k | newt.flags = 0; |
734 | 204k | setnodevector(L, &newt, nhsize); |
735 | 204k | if (newasize < oldasize) { /* will array shrink? */ |
736 | | /* re-insert into the new hash the elements from vanishing slice */ |
737 | 44.1k | exchangehashpart(t, &newt); /* pretend table has new hash */ |
738 | 44.1k | reinsertOldSlice(L, t, oldasize, newasize); |
739 | 44.1k | exchangehashpart(t, &newt); /* restore old hash (in case of errors) */ |
740 | 44.1k | } |
741 | | /* allocate new array */ |
742 | 204k | newarray = resizearray(L, t, oldasize, newasize); |
743 | 204k | if (l_unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */ |
744 | 0 | freehash(L, &newt); /* release new hash part */ |
745 | 0 | luaM_error(L); /* raise error (with array unchanged) */ |
746 | 0 | } |
747 | | /* allocation ok; initialize new part of the array */ |
748 | 204k | exchangehashpart(t, &newt); /* 't' has the new hash ('newt' has the old) */ |
749 | 204k | t->array = newarray; /* set new array part */ |
750 | 204k | t->alimit = newasize; |
751 | 204k | clearNewSlice(t, oldasize, newasize); |
752 | | /* re-insert elements from old hash part into new parts */ |
753 | 204k | reinsert(L, &newt, t); /* 'newt' now has the old hash */ |
754 | 204k | freehash(L, &newt); /* free old hash part */ |
755 | 204k | } |
756 | | |
757 | | |
758 | 10.1k | void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) { |
759 | 10.1k | int nsize = allocsizenode(t); |
760 | 10.1k | luaH_resize(L, t, nasize, nsize); |
761 | 10.1k | } |
762 | | |
763 | | /* |
764 | | ** nums[i] = number of keys 'k' where 2^(i - 1) < k <= 2^i |
765 | | */ |
766 | 149k | static void rehash (lua_State *L, Table *t, const TValue *ek) { |
767 | 149k | unsigned int asize; /* optimal size for array part */ |
768 | 149k | unsigned int na; /* number of keys in the array part */ |
769 | 149k | unsigned int nums[MAXABITS + 1]; |
770 | 149k | int i; |
771 | 149k | int totaluse; |
772 | 4.92M | for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */ |
773 | 149k | setlimittosize(t); |
774 | 149k | na = numusearray(t, nums); /* count keys in array part */ |
775 | 149k | totaluse = na; /* all those keys are integer keys */ |
776 | 149k | totaluse += numusehash(t, nums, &na); /* count keys in hash part */ |
777 | | /* count extra key */ |
778 | 149k | if (ttisinteger(ek)) |
779 | 64.6k | na += countint(ivalue(ek), nums); |
780 | 149k | totaluse++; |
781 | | /* compute new size for array part */ |
782 | 149k | asize = computesizes(nums, &na); |
783 | | /* resize the table to new computed sizes */ |
784 | 149k | luaH_resize(L, t, asize, totaluse - na); |
785 | 149k | } |
786 | | |
787 | | |
788 | | |
789 | | /* |
790 | | ** }============================================================= |
791 | | */ |
792 | | |
793 | | |
794 | 125k | Table *luaH_new (lua_State *L) { |
795 | 125k | GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table)); |
796 | 125k | Table *t = gco2t(o); |
797 | 0 | t->metatable = NULL; |
798 | 125k | t->flags = cast_byte(maskflags); /* table has no metamethod fields */ |
799 | 125k | t->array = NULL; |
800 | 125k | t->alimit = 0; |
801 | 125k | setnodevector(L, t, 0); |
802 | 125k | return t; |
803 | 125k | } |
804 | | |
805 | | |
806 | | /* |
807 | | ** Frees a table. |
808 | | */ |
809 | 125k | void luaH_free (lua_State *L, Table *t) { |
810 | 125k | unsigned int realsize = luaH_realasize(t); |
811 | 125k | freehash(L, t); |
812 | 125k | resizearray(L, t, realsize, 0); |
813 | 125k | luaM_free(L, t); |
814 | 125k | } |
815 | | |
816 | | |
817 | 380k | static Node *getfreepos (Table *t) { |
818 | 380k | if (haslastfree(t)) { /* does it have 'lastfree' information? */ |
819 | | /* look for a spot before 'lastfree', updating 'lastfree' */ |
820 | 246k | while (getlastfree(t) > t->node) { |
821 | 241k | Node *free = --getlastfree(t); |
822 | 241k | if (keyisnil(free)) |
823 | 138k | return free; |
824 | 241k | } |
825 | 143k | } |
826 | 236k | else { /* no 'lastfree' information */ |
827 | 236k | if (!isdummy(t)) { |
828 | 191k | int i = sizenode(t); |
829 | 448k | while (i--) { /* do a linear search */ |
830 | 348k | Node *free = gnode(t, i); |
831 | 348k | if (keyisnil(free)) |
832 | 92.2k | return free; |
833 | 348k | } |
834 | 191k | } |
835 | 236k | } |
836 | 149k | return NULL; /* could not find a free place */ |
837 | 380k | } |
838 | | |
839 | | |
840 | | |
841 | | /* |
842 | | ** Inserts a new key into a hash table; first, check whether key's main |
843 | | ** position is free. If not, check whether colliding node is in its main |
844 | | ** position or not: if it is not, move colliding node to an empty place |
845 | | ** and put new key in its main position; otherwise (colliding node is in |
846 | | ** its main position), new key goes to an empty position. |
847 | | */ |
848 | | static void luaH_newkey (lua_State *L, Table *t, const TValue *key, |
849 | 1.10M | TValue *value) { |
850 | 1.10M | Node *mp; |
851 | 1.10M | TValue aux; |
852 | 1.10M | if (l_unlikely(ttisnil(key))) |
853 | 0 | luaG_runerror(L, "table index is nil"); |
854 | 1.10M | else if (ttisfloat(key)) { |
855 | 37.4k | lua_Number f = fltvalue(key); |
856 | 0 | lua_Integer k; |
857 | 37.4k | if (luaV_flttointeger(f, &k, F2Ieq)) { /* does key fit in an integer? */ |
858 | 24.1k | setivalue(&aux, k); |
859 | 24.1k | key = &aux; /* insert it as an integer */ |
860 | 24.1k | } |
861 | 13.2k | else if (l_unlikely(luai_numisnan(f))) |
862 | 0 | luaG_runerror(L, "table index is NaN"); |
863 | 37.4k | } |
864 | 1.10M | if (ttisnil(value)) |
865 | 375k | return; /* do not insert nil values */ |
866 | 729k | mp = mainpositionTV(t, key); |
867 | 729k | if (!isempty(gval(mp)) || isdummy(t)) { /* main position is taken? */ |
868 | 380k | Node *othern; |
869 | 380k | Node *f = getfreepos(t); /* get a free place */ |
870 | 380k | if (f == NULL) { /* cannot find a free place? */ |
871 | 149k | rehash(L, t, key); /* grow table */ |
872 | | /* whatever called 'newkey' takes care of TM cache */ |
873 | 149k | luaH_set(L, t, key, value); /* insert key into grown table */ |
874 | 149k | return; |
875 | 149k | } |
876 | 230k | lua_assert(!isdummy(t)); |
877 | 230k | othern = mainpositionfromnode(t, mp); |
878 | 230k | if (othern != mp) { /* is colliding node out of its main position? */ |
879 | | /* yes; move colliding node into free position */ |
880 | 55.2k | while (othern + gnext(othern) != mp) /* find previous */ |
881 | 12.1k | othern += gnext(othern); |
882 | 43.0k | gnext(othern) = cast_int(f - othern); /* rechain to point to 'f' */ |
883 | 43.0k | *f = *mp; /* copy colliding node into free pos. (mp->next also goes) */ |
884 | 43.0k | if (gnext(mp) != 0) { |
885 | 5.77k | gnext(f) += cast_int(mp - f); /* correct 'next' */ |
886 | 5.77k | gnext(mp) = 0; /* now 'mp' is free */ |
887 | 5.77k | } |
888 | 43.0k | setempty(gval(mp)); |
889 | 43.0k | } |
890 | 187k | else { /* colliding node is in its own main position */ |
891 | | /* new node will go into free position */ |
892 | 187k | if (gnext(mp) != 0) |
893 | 37.4k | gnext(f) = cast_int((mp + gnext(mp)) - f); /* chain new position */ |
894 | 187k | else lua_assert(gnext(f) == 0); |
895 | 187k | gnext(mp) = cast_int(f - mp); |
896 | 187k | mp = f; |
897 | 187k | } |
898 | 230k | } |
899 | 580k | setnodekey(L, mp, key); |
900 | 580k | luaC_barrierback(L, obj2gco(t), key); |
901 | 580k | lua_assert(isempty(gval(mp))); |
902 | 580k | setobj2t(L, gval(mp), value); |
903 | 580k | } |
904 | | |
905 | | |
906 | 776k | static const TValue *getintfromhash (Table *t, lua_Integer key) { |
907 | 776k | Node *n = hashint(t, key); |
908 | 776k | lua_assert(l_castS2U(key) - 1u >= luaH_realasize(t)); |
909 | 961k | for (;;) { /* check whether 'key' is somewhere in the chain */ |
910 | 961k | if (keyisinteger(n) && keyival(n) == key) |
911 | 475k | return gval(n); /* that's it */ |
912 | 485k | else { |
913 | 485k | int nx = gnext(n); |
914 | 485k | if (nx == 0) break; |
915 | 184k | n += nx; |
916 | 184k | } |
917 | 961k | } |
918 | 300k | return &absentkey; |
919 | 776k | } |
920 | | |
921 | | |
922 | 14.7k | static int hashkeyisempty (Table *t, lua_Integer key) { |
923 | 14.7k | const TValue *val = getintfromhash(t, key); |
924 | 14.7k | return isempty(val); |
925 | 14.7k | } |
926 | | |
927 | | |
928 | 29.0M | static int finishnodeget (const TValue *val, TValue *res) { |
929 | 29.0M | if (!ttisnil(val)) { |
930 | 27.7M | setobj(((lua_State*)NULL), res, val); |
931 | 27.7M | } |
932 | 29.0M | return ttypetag(val); |
933 | 29.0M | } |
934 | | |
935 | | |
936 | 314k | int luaH_getint (Table *t, lua_Integer key, TValue *res) { |
937 | 314k | if (keyinarray(t, key)) { |
938 | 116k | int tag = *getArrTag(t, key - 1); |
939 | 116k | if (!tagisempty(tag)) |
940 | 116k | farr2val(t, key, tag, res); |
941 | 116k | return tag; |
942 | 116k | } |
943 | 197k | else |
944 | 197k | return finishnodeget(getintfromhash(t, key), res); |
945 | 314k | } |
946 | | |
947 | | |
948 | | /* |
949 | | ** search function for short strings |
950 | | */ |
951 | 58.1M | const TValue *luaH_Hgetshortstr (Table *t, TString *key) { |
952 | 58.1M | Node *n = hashstr(t, key); |
953 | 58.1M | lua_assert(key->tt == LUA_VSHRSTR); |
954 | 70.2M | for (;;) { /* check whether 'key' is somewhere in the chain */ |
955 | 70.2M | if (keyisshrstr(n) && eqshrstr(keystrval(n), key)) |
956 | 56.1M | return gval(n); /* that's it */ |
957 | 14.0M | else { |
958 | 14.0M | int nx = gnext(n); |
959 | 14.0M | if (nx == 0) |
960 | 2.01M | return &absentkey; /* not found */ |
961 | 12.0M | n += nx; |
962 | 12.0M | } |
963 | 70.2M | } |
964 | 58.1M | } |
965 | | |
966 | | |
967 | 2.23M | int luaH_getshortstr (Table *t, TString *key, TValue *res) { |
968 | 2.23M | return finishnodeget(luaH_Hgetshortstr(t, key), res); |
969 | 2.23M | } |
970 | | |
971 | | |
972 | 26.8M | static const TValue *Hgetstr (Table *t, TString *key) { |
973 | 26.8M | if (key->tt == LUA_VSHRSTR) |
974 | 26.8M | return luaH_Hgetshortstr(t, key); |
975 | 80.3k | else { /* for long strings, use generic case */ |
976 | 80.3k | TValue ko; |
977 | 80.3k | setsvalue(cast(lua_State *, NULL), &ko, key); |
978 | 80.3k | return getgeneric(t, &ko, 0); |
979 | 80.3k | } |
980 | 26.8M | } |
981 | | |
982 | | |
983 | 246 | int luaH_getstr (Table *t, TString *key, TValue *res) { |
984 | 246 | return finishnodeget(Hgetstr(t, key), res); |
985 | 246 | } |
986 | | |
987 | | |
988 | 26.8M | TString *luaH_getstrkey (Table *t, TString *key) { |
989 | 26.8M | const TValue *o = Hgetstr(t, key); |
990 | 26.8M | if (!isabstkey(o)) /* string already present? */ |
991 | 26.8M | return keystrval(nodefromval(o)); /* get saved copy */ |
992 | 32.1k | else |
993 | 32.1k | return NULL; |
994 | 26.8M | } |
995 | | |
996 | | |
997 | | /* |
998 | | ** main search function |
999 | | */ |
1000 | 26.8M | int luaH_get (Table *t, const TValue *key, TValue *res) { |
1001 | 26.8M | const TValue *slot; |
1002 | 26.8M | switch (ttypetag(key)) { |
1003 | 26.3M | case LUA_VSHRSTR: |
1004 | 26.3M | slot = luaH_Hgetshortstr(t, tsvalue(key)); |
1005 | 0 | break; |
1006 | 229k | case LUA_VNUMINT: |
1007 | 229k | return luaH_getint(t, ivalue(key), res); |
1008 | 30.3k | case LUA_VNIL: |
1009 | 30.3k | slot = &absentkey; |
1010 | 30.3k | break; |
1011 | 127k | case LUA_VNUMFLT: { |
1012 | 127k | lua_Integer k; |
1013 | 127k | if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */ |
1014 | 8.53k | return luaH_getint(t, k, res); /* use specialized version */ |
1015 | | /* else... */ |
1016 | 127k | } /* FALLTHROUGH */ |
1017 | 201k | default: |
1018 | 201k | slot = getgeneric(t, key, 0); |
1019 | 201k | break; |
1020 | 26.8M | } |
1021 | 26.5M | return finishnodeget(slot, res); |
1022 | 26.8M | } |
1023 | | |
1024 | | |
1025 | 3.39M | static int finishnodeset (Table *t, const TValue *slot, TValue *val) { |
1026 | 3.39M | if (!ttisnil(slot)) { |
1027 | 2.18M | setobj(((lua_State*)NULL), cast(TValue*, slot), val); |
1028 | 2.18M | return HOK; /* success */ |
1029 | 2.18M | } |
1030 | 1.20M | else if (isabstkey(slot)) |
1031 | 1.10M | return HNOTFOUND; /* no slot with that key */ |
1032 | 104k | else /* return node encoded */ |
1033 | 104k | return cast_int((cast(Node*, slot) - t->node)) + HFIRSTNODE; |
1034 | 3.39M | } |
1035 | | |
1036 | | |
1037 | 42 | static int rawfinishnodeset (const TValue *slot, TValue *val) { |
1038 | 42 | if (isabstkey(slot)) |
1039 | 42 | return 0; /* no slot with that key */ |
1040 | 0 | else { |
1041 | 0 | setobj(((lua_State*)NULL), cast(TValue*, slot), val); |
1042 | 0 | return 1; /* success */ |
1043 | 0 | } |
1044 | 42 | } |
1045 | | |
1046 | | |
1047 | 573k | int luaH_psetint (Table *t, lua_Integer key, TValue *val) { |
1048 | 573k | if (keyinarray(t, key)) { |
1049 | 10.3k | lu_byte *tag = getArrTag(t, key - 1); |
1050 | 10.3k | if (!tagisempty(*tag) || checknoTM(t->metatable, TM_NEWINDEX)) { |
1051 | 10.3k | fval2arr(t, key, tag, val); |
1052 | 10.3k | return HOK; /* success */ |
1053 | 10.3k | } |
1054 | 0 | else |
1055 | 0 | return ~cast_int(key); /* empty slot in the array part */ |
1056 | 10.3k | } |
1057 | 563k | else |
1058 | 563k | return finishnodeset(t, getintfromhash(t, key), val); |
1059 | 573k | } |
1060 | | |
1061 | | |
1062 | 2.75M | int luaH_psetshortstr (Table *t, TString *key, TValue *val) { |
1063 | 2.75M | return finishnodeset(t, luaH_Hgetshortstr(t, key), val); |
1064 | 2.75M | } |
1065 | | |
1066 | | |
1067 | 180 | int luaH_psetstr (Table *t, TString *key, TValue *val) { |
1068 | 180 | return finishnodeset(t, Hgetstr(t, key), val); |
1069 | 180 | } |
1070 | | |
1071 | | |
1072 | 715k | int luaH_pset (Table *t, const TValue *key, TValue *val) { |
1073 | 715k | switch (ttypetag(key)) { |
1074 | 378k | case LUA_VSHRSTR: return luaH_psetshortstr(t, tsvalue(key), val); |
1075 | 209k | case LUA_VNUMINT: return luaH_psetint(t, ivalue(key), val); |
1076 | 0 | case LUA_VNIL: return HNOTFOUND; |
1077 | 103k | case LUA_VNUMFLT: { |
1078 | 103k | lua_Integer k; |
1079 | 103k | if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */ |
1080 | 49.4k | return luaH_psetint(t, k, val); /* use specialized version */ |
1081 | | /* else... */ |
1082 | 103k | } /* FALLTHROUGH */ |
1083 | 77.3k | default: |
1084 | 77.3k | return finishnodeset(t, getgeneric(t, key, 0), val); |
1085 | 715k | } |
1086 | 715k | } |
1087 | | |
1088 | | /* |
1089 | | ** Finish a raw "set table" operation, where 'slot' is where the value |
1090 | | ** should have been (the result of a previous "get table"). |
1091 | | ** Beware: when using this function you probably need to check a GC |
1092 | | ** barrier and invalidate the TM cache. |
1093 | | */ |
1094 | | |
1095 | | |
1096 | | void luaH_finishset (lua_State *L, Table *t, const TValue *key, |
1097 | 1.23M | TValue *value, int hres) { |
1098 | 1.23M | lua_assert(hres != HOK); |
1099 | 1.23M | if (hres == HNOTFOUND) { |
1100 | 1.10M | luaH_newkey(L, t, key, value); |
1101 | 1.10M | } |
1102 | 133k | else if (hres > 0) { /* regular Node? */ |
1103 | 104k | setobj2t(L, gval(gnode(t, hres - HFIRSTNODE)), value); |
1104 | 104k | } |
1105 | 28.8k | else { /* array entry */ |
1106 | 28.8k | hres = ~hres; /* real index */ |
1107 | 28.8k | obj2arr(t, hres, value); |
1108 | 28.8k | } |
1109 | 1.23M | } |
1110 | | |
1111 | | |
1112 | | /* |
1113 | | ** beware: when using this function you probably need to check a GC |
1114 | | ** barrier and invalidate the TM cache. |
1115 | | */ |
1116 | 662k | void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value) { |
1117 | 662k | int hres = luaH_pset(t, key, value); |
1118 | 662k | if (hres != HOK) |
1119 | 508k | luaH_finishset(L, t, key, value, hres); |
1120 | 662k | } |
1121 | | |
1122 | | |
1123 | | /* |
1124 | | ** Ditto for a GC barrier. (No need to invalidate the TM cache, as |
1125 | | ** integers cannot be keys to metamethods.) |
1126 | | */ |
1127 | 1.72k | void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { |
1128 | 1.72k | if (keyinarray(t, key)) |
1129 | 1.68k | obj2arr(t, key, value); |
1130 | 42 | else { |
1131 | 42 | int ok = rawfinishnodeset(getintfromhash(t, key), value); |
1132 | 42 | if (!ok) { |
1133 | 42 | TValue k; |
1134 | 42 | setivalue(&k, key); |
1135 | 42 | luaH_newkey(L, t, &k, value); |
1136 | 42 | } |
1137 | 42 | } |
1138 | 1.72k | } |
1139 | | |
1140 | | |
1141 | | /* |
1142 | | ** Try to find a boundary in the hash part of table 't'. From the |
1143 | | ** caller, we know that 'j' is zero or present and that 'j + 1' is |
1144 | | ** present. We want to find a larger key that is absent from the |
1145 | | ** table, so that we can do a binary search between the two keys to |
1146 | | ** find a boundary. We keep doubling 'j' until we get an absent index. |
1147 | | ** If the doubling would overflow, we try LUA_MAXINTEGER. If it is |
1148 | | ** absent, we are ready for the binary search. ('j', being max integer, |
1149 | | ** is larger or equal to 'i', but it cannot be equal because it is |
1150 | | ** absent while 'i' is present; so 'j > i'.) Otherwise, 'j' is a |
1151 | | ** boundary. ('j + 1' cannot be a present integer key because it is |
1152 | | ** not a valid integer in Lua.) |
1153 | | */ |
1154 | 1.99k | static lua_Unsigned hash_search (Table *t, lua_Unsigned j) { |
1155 | 1.99k | lua_Unsigned i; |
1156 | 1.99k | if (j == 0) j++; /* the caller ensures 'j + 1' is present */ |
1157 | 2.26k | do { |
1158 | 2.26k | i = j; /* 'i' is a present index */ |
1159 | 2.26k | if (j <= l_castS2U(LUA_MAXINTEGER) / 2) |
1160 | 2.26k | j *= 2; |
1161 | 0 | else { |
1162 | 0 | j = LUA_MAXINTEGER; |
1163 | 0 | if (hashkeyisempty(t, j)) /* t[j] not present? */ |
1164 | 0 | break; /* 'j' now is an absent index */ |
1165 | 0 | else /* weird case */ |
1166 | 0 | return j; /* well, max integer is a boundary... */ |
1167 | 0 | } |
1168 | 2.26k | } while (!hashkeyisempty(t, j)); /* repeat until an absent t[j] */ |
1169 | | /* i < j && t[i] present && t[j] absent */ |
1170 | 11.9k | while (j - i > 1u) { /* do a binary search between them */ |
1171 | 9.90k | lua_Unsigned m = (i + j) / 2; |
1172 | 9.90k | if (hashkeyisempty(t, m)) j = m; |
1173 | 3.29k | else i = m; |
1174 | 9.90k | } |
1175 | 1.99k | return i; |
1176 | 1.99k | } |
1177 | | |
1178 | | |
1179 | 15.2k | static unsigned int binsearch (Table *array, unsigned int i, unsigned int j) { |
1180 | 74.0k | while (j - i > 1u) { /* binary search */ |
1181 | 58.8k | unsigned int m = (i + j) / 2; |
1182 | 58.8k | if (arraykeyisempty(array, m)) j = m; |
1183 | 10.3k | else i = m; |
1184 | 58.8k | } |
1185 | 15.2k | return i; |
1186 | 15.2k | } |
1187 | | |
1188 | | |
1189 | | /* |
1190 | | ** Try to find a boundary in table 't'. (A 'boundary' is an integer index |
1191 | | ** such that t[i] is present and t[i+1] is absent, or 0 if t[1] is absent |
1192 | | ** and 'maxinteger' if t[maxinteger] is present.) |
1193 | | ** (In the next explanation, we use Lua indices, that is, with base 1. |
1194 | | ** The code itself uses base 0 when indexing the array part of the table.) |
1195 | | ** The code starts with 'limit = t->alimit', a position in the array |
1196 | | ** part that may be a boundary. |
1197 | | ** |
1198 | | ** (1) If 't[limit]' is empty, there must be a boundary before it. |
1199 | | ** As a common case (e.g., after 't[#t]=nil'), check whether 'limit-1' |
1200 | | ** is present. If so, it is a boundary. Otherwise, do a binary search |
1201 | | ** between 0 and limit to find a boundary. In both cases, try to |
1202 | | ** use this boundary as the new 'alimit', as a hint for the next call. |
1203 | | ** |
1204 | | ** (2) If 't[limit]' is not empty and the array has more elements |
1205 | | ** after 'limit', try to find a boundary there. Again, try first |
1206 | | ** the special case (which should be quite frequent) where 'limit+1' |
1207 | | ** is empty, so that 'limit' is a boundary. Otherwise, check the |
1208 | | ** last element of the array part. If it is empty, there must be a |
1209 | | ** boundary between the old limit (present) and the last element |
1210 | | ** (absent), which is found with a binary search. (This boundary always |
1211 | | ** can be a new limit.) |
1212 | | ** |
1213 | | ** (3) The last case is when there are no elements in the array part |
1214 | | ** (limit == 0) or its last element (the new limit) is present. |
1215 | | ** In this case, must check the hash part. If there is no hash part |
1216 | | ** or 'limit+1' is absent, 'limit' is a boundary. Otherwise, call |
1217 | | ** 'hash_search' to find a boundary in the hash part of the table. |
1218 | | ** (In those cases, the boundary is not inside the array part, and |
1219 | | ** therefore cannot be used as a new limit.) |
1220 | | */ |
1221 | 187k | lua_Unsigned luaH_getn (Table *t) { |
1222 | 187k | unsigned int limit = t->alimit; |
1223 | 187k | if (limit > 0 && arraykeyisempty(t, limit)) { /* (1)? */ |
1224 | | /* there must be a boundary before 'limit' */ |
1225 | 100k | if (limit >= 2 && !arraykeyisempty(t, limit - 1)) { |
1226 | | /* 'limit - 1' is a boundary; can it be a new limit? */ |
1227 | 85.2k | if (ispow2realasize(t) && !ispow2(limit - 1)) { |
1228 | 85.1k | t->alimit = limit - 1; |
1229 | 85.1k | setnorealasize(t); /* now 'alimit' is not the real size */ |
1230 | 85.1k | } |
1231 | 85.2k | return limit - 1; |
1232 | 85.2k | } |
1233 | 15.2k | else { /* must search for a boundary in [0, limit] */ |
1234 | 15.2k | unsigned int boundary = binsearch(t, 0, limit); |
1235 | | /* can this boundary represent the real size of the array? */ |
1236 | 15.2k | if (ispow2realasize(t) && boundary > luaH_realasize(t) / 2) { |
1237 | 19 | t->alimit = boundary; /* use it as the new limit */ |
1238 | 19 | setnorealasize(t); |
1239 | 19 | } |
1240 | 15.2k | return boundary; |
1241 | 15.2k | } |
1242 | 100k | } |
1243 | | /* 'limit' is zero or present in table */ |
1244 | 87.4k | if (!limitequalsasize(t)) { /* (2)? */ |
1245 | | /* 'limit' > 0 and array has more elements after 'limit' */ |
1246 | 48.2k | if (arraykeyisempty(t, limit + 1)) /* 'limit + 1' is empty? */ |
1247 | 48.2k | return limit; /* this is the boundary */ |
1248 | | /* else, try last element in the array */ |
1249 | 0 | limit = luaH_realasize(t); |
1250 | 0 | if (arraykeyisempty(t, limit)) { /* empty? */ |
1251 | | /* there must be a boundary in the array after old limit, |
1252 | | and it must be a valid new limit */ |
1253 | 0 | unsigned int boundary = binsearch(t, t->alimit, limit); |
1254 | 0 | t->alimit = boundary; |
1255 | 0 | return boundary; |
1256 | 0 | } |
1257 | | /* else, new limit is present in the table; check the hash part */ |
1258 | 0 | } |
1259 | | /* (3) 'limit' is the last element and either is zero or present in table */ |
1260 | 39.1k | lua_assert(limit == luaH_realasize(t) && |
1261 | 39.1k | (limit == 0 || !arraykeyisempty(t, limit))); |
1262 | 39.1k | if (isdummy(t) || hashkeyisempty(t, cast(lua_Integer, limit + 1))) |
1263 | 37.1k | return limit; /* 'limit + 1' is absent */ |
1264 | 1.99k | else /* 'limit + 1' is also present */ |
1265 | 1.99k | return hash_search(t, limit); |
1266 | 39.1k | } |
1267 | | |
1268 | | |
1269 | | |
1270 | | #if defined(LUA_DEBUG) |
1271 | | |
1272 | | /* export these functions for the test library */ |
1273 | | |
1274 | | Node *luaH_mainposition (const Table *t, const TValue *key) { |
1275 | | return mainpositionTV(t, key); |
1276 | | } |
1277 | | |
1278 | | #endif |