/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 | | |
29 | | #include "lua.h" |
30 | | |
31 | | #include "ldebug.h" |
32 | | #include "ldo.h" |
33 | | #include "lgc.h" |
34 | | #include "lmem.h" |
35 | | #include "lobject.h" |
36 | | #include "lstate.h" |
37 | | #include "lstring.h" |
38 | | #include "ltable.h" |
39 | | #include "lvm.h" |
40 | | |
41 | | |
42 | | /* |
43 | | ** MAXABITS is the largest integer such that MAXASIZE fits in an |
44 | | ** unsigned int. |
45 | | */ |
46 | 120M | #define MAXABITS cast_int(sizeof(int) * CHAR_BIT - 1) |
47 | | |
48 | | |
49 | | /* |
50 | | ** MAXASIZE is the maximum size of the array part. It is the minimum |
51 | | ** between 2^MAXABITS and the maximum size that, measured in bytes, |
52 | | ** fits in a 'size_t'. |
53 | | */ |
54 | 876k | #define MAXASIZE luaM_limitN(1u << MAXABITS, TValue) |
55 | | |
56 | | /* |
57 | | ** MAXHBITS is the largest integer such that 2^MAXHBITS fits in a |
58 | | ** signed int. |
59 | | */ |
60 | 7.54M | #define MAXHBITS (MAXABITS - 1) |
61 | | |
62 | | |
63 | | /* |
64 | | ** MAXHSIZE is the maximum size of the hash part. It is the minimum |
65 | | ** between 2^MAXHBITS and the maximum size such that, measured in bytes, |
66 | | ** it fits in a 'size_t'. |
67 | | */ |
68 | 3.77M | #define MAXHSIZE luaM_limitN(1u << MAXHBITS, Node) |
69 | | |
70 | | |
71 | | /* |
72 | | ** When the original hash value is good, hashing by a power of 2 |
73 | | ** avoids the cost of '%'. |
74 | | */ |
75 | 454M | #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) |
76 | | |
77 | | /* |
78 | | ** for other types, it is better to avoid modulo by power of 2, as |
79 | | ** they can have many 2 factors. |
80 | | */ |
81 | 8.94M | #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) |
82 | | |
83 | | |
84 | 449M | #define hashstr(t,str) hashpow2(t, (str)->hash) |
85 | 32.7k | #define hashboolean(t,p) hashpow2(t, p) |
86 | | |
87 | | |
88 | 27.2k | #define hashpointer(t,p) hashmod(t, point2uint(p)) |
89 | | |
90 | | |
91 | | #define dummynode (&dummynode_) |
92 | | |
93 | | static const Node dummynode_ = { |
94 | | {{NULL}, LUA_VEMPTY, /* value's value and type */ |
95 | | LUA_VNIL, 0, {NULL}} /* key type, next, and key value */ |
96 | | }; |
97 | | |
98 | | |
99 | | static const TValue absentkey = {ABSTKEYCONSTANT}; |
100 | | |
101 | | |
102 | | /* |
103 | | ** Hash for integers. To allow a good hash, use the remainder operator |
104 | | ** ('%'). If integer fits as a non-negative int, compute an int |
105 | | ** remainder, which is faster. Otherwise, use an unsigned-integer |
106 | | ** remainder, which uses all bits and ensures a non-negative result. |
107 | | */ |
108 | 7.20M | static Node *hashint (const Table *t, lua_Integer i) { |
109 | 7.20M | lua_Unsigned ui = l_castS2U(i); |
110 | 7.20M | if (ui <= cast_uint(INT_MAX)) |
111 | 5.62M | return hashmod(t, cast_int(ui)); |
112 | 1.57M | else |
113 | 1.57M | return hashmod(t, ui); |
114 | 7.20M | } |
115 | | |
116 | | |
117 | | /* |
118 | | ** Hash for floating-point numbers. |
119 | | ** The main computation should be just |
120 | | ** n = frexp(n, &i); return (n * INT_MAX) + i |
121 | | ** but there are some numerical subtleties. |
122 | | ** In a two-complement representation, INT_MAX does not has an exact |
123 | | ** representation as a float, but INT_MIN does; because the absolute |
124 | | ** value of 'frexp' is smaller than 1 (unless 'n' is inf/NaN), the |
125 | | ** absolute value of the product 'frexp * -INT_MIN' is smaller or equal |
126 | | ** to INT_MAX. Next, the use of 'unsigned int' avoids overflows when |
127 | | ** adding 'i'; the use of '~u' (instead of '-u') avoids problems with |
128 | | ** INT_MIN. |
129 | | */ |
130 | | #if !defined(l_hashfloat) |
131 | 1.71M | static int l_hashfloat (lua_Number n) { |
132 | 1.71M | int i; |
133 | 1.71M | lua_Integer ni; |
134 | 1.71M | n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN); |
135 | 1.71M | if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */ |
136 | 280k | lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL)); |
137 | 280k | return 0; |
138 | 280k | } |
139 | 1.43M | else { /* normal case */ |
140 | 1.43M | unsigned int u = cast_uint(i) + cast_uint(ni); |
141 | 1.43M | return cast_int(u <= cast_uint(INT_MAX) ? u : ~u); |
142 | 1.43M | } |
143 | 1.71M | } |
144 | | #endif |
145 | | |
146 | | |
147 | | /* |
148 | | ** returns the 'main' position of an element in a table (that is, |
149 | | ** the index of its hash value). |
150 | | */ |
151 | 31.1M | static Node *mainpositionTV (const Table *t, const TValue *key) { |
152 | 31.1M | switch (ttypetag(key)) { |
153 | 1.82M | case LUA_VNUMINT: { |
154 | 1.82M | lua_Integer i = ivalue(key); |
155 | 1.82M | return hashint(t, i); |
156 | 0 | } |
157 | 1.71M | case LUA_VNUMFLT: { |
158 | 1.71M | lua_Number n = fltvalue(key); |
159 | 1.71M | return hashmod(t, l_hashfloat(n)); |
160 | 0 | } |
161 | 21.8M | case LUA_VSHRSTR: { |
162 | 21.8M | TString *ts = tsvalue(key); |
163 | 21.8M | return hashstr(t, ts); |
164 | 0 | } |
165 | 5.63M | case LUA_VLNGSTR: { |
166 | 5.63M | TString *ts = tsvalue(key); |
167 | 5.63M | return hashpow2(t, luaS_hashlongstr(ts)); |
168 | 0 | } |
169 | 24.5k | case LUA_VFALSE: |
170 | 24.5k | return hashboolean(t, 0); |
171 | 8.22k | case LUA_VTRUE: |
172 | 8.22k | return hashboolean(t, 1); |
173 | 0 | case LUA_VLIGHTUSERDATA: { |
174 | 0 | void *p = pvalue(key); |
175 | 0 | return hashpointer(t, p); |
176 | 0 | } |
177 | 167 | case LUA_VLCF: { |
178 | 167 | lua_CFunction f = fvalue(key); |
179 | 167 | return hashpointer(t, f); |
180 | 0 | } |
181 | 27.0k | default: { |
182 | 27.0k | GCObject *o = gcvalue(key); |
183 | 27.0k | return hashpointer(t, o); |
184 | 0 | } |
185 | 31.1M | } |
186 | 31.1M | } |
187 | | |
188 | | |
189 | 4.05M | l_sinline Node *mainpositionfromnode (const Table *t, Node *nd) { |
190 | 4.05M | TValue key; |
191 | 4.05M | getnodekey(cast(lua_State *, NULL), &key, nd); |
192 | 4.05M | return mainpositionTV(t, &key); |
193 | 4.05M | } |
194 | | |
195 | | |
196 | | /* |
197 | | ** Check whether key 'k1' is equal to the key in node 'n2'. This |
198 | | ** equality is raw, so there are no metamethods. Floats with integer |
199 | | ** values have been normalized, so integers cannot be equal to |
200 | | ** floats. It is assumed that 'eqshrstr' is simply pointer equality, so |
201 | | ** that short strings are handled in the default case. |
202 | | ** A true 'deadok' means to accept dead keys as equal to their original |
203 | | ** values. All dead keys are compared in the default case, by pointer |
204 | | ** identity. (Only collectable objects can produce dead keys.) Note that |
205 | | ** dead long strings are also compared by identity. |
206 | | ** Once a key is dead, its corresponding value may be collected, and |
207 | | ** then another value can be created with the same address. If this |
208 | | ** other value is given to 'next', 'equalkey' will signal a false |
209 | | ** positive. In a regular traversal, this situation should never happen, |
210 | | ** as all keys given to 'next' came from the table itself, and therefore |
211 | | ** could not have been collected. Outside a regular traversal, we |
212 | | ** have garbage in, garbage out. What is relevant is that this false |
213 | | ** positive does not break anything. (In particular, 'next' will return |
214 | | ** some other valid item on the table or nil.) |
215 | | */ |
216 | 15.0M | static int equalkey (const TValue *k1, const Node *n2, int deadok) { |
217 | 15.0M | if ((rawtt(k1) != keytt(n2)) && /* not the same variants? */ |
218 | 15.0M | !(deadok && keyisdead(n2) && iscollectable(k1))) |
219 | 5.49M | return 0; /* cannot be same key */ |
220 | 9.51M | switch (keytt(n2)) { |
221 | 19.1k | case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: |
222 | 19.1k | return 1; |
223 | 98.8k | case LUA_VNUMINT: |
224 | 98.8k | return (ivalue(k1) == keyival(n2)); |
225 | 796k | case LUA_VNUMFLT: |
226 | 796k | return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2))); |
227 | 0 | case LUA_VLIGHTUSERDATA: |
228 | 0 | return pvalue(k1) == pvalueraw(keyval(n2)); |
229 | 0 | case LUA_VLCF: |
230 | 0 | return fvalue(k1) == fvalueraw(keyval(n2)); |
231 | 1.95M | case ctb(LUA_VLNGSTR): |
232 | 1.95M | return luaS_eqlngstr(tsvalue(k1), keystrval(n2)); |
233 | 6.63M | default: |
234 | 6.63M | return gcvalue(k1) == gcvalueraw(keyval(n2)); |
235 | 9.51M | } |
236 | 9.51M | } |
237 | | |
238 | | |
239 | | /* |
240 | | ** True if value of 'alimit' is equal to the real size of the array |
241 | | ** part of table 't'. (Otherwise, the array part must be larger than |
242 | | ** 'alimit'.) |
243 | | */ |
244 | 57.4M | #define limitequalsasize(t) (isrealasize(t) || ispow2((t)->alimit)) |
245 | | |
246 | | |
247 | | /* |
248 | | ** Returns the real size of the 'array' array |
249 | | */ |
250 | 44.4M | LUAI_FUNC unsigned int luaH_realasize (const Table *t) { |
251 | 44.4M | if (limitequalsasize(t)) |
252 | 44.3M | return t->alimit; /* this is the size */ |
253 | 96.1k | else { |
254 | 96.1k | unsigned int size = t->alimit; |
255 | | /* compute the smallest power of 2 not smaller than 'n' */ |
256 | 96.1k | size |= (size >> 1); |
257 | 96.1k | size |= (size >> 2); |
258 | 96.1k | size |= (size >> 4); |
259 | 96.1k | size |= (size >> 8); |
260 | 96.1k | #if (UINT_MAX >> 14) > 3 /* unsigned int has more than 16 bits */ |
261 | 96.1k | size |= (size >> 16); |
262 | | #if (UINT_MAX >> 30) > 3 |
263 | | size |= (size >> 32); /* unsigned int has more than 32 bits */ |
264 | | #endif |
265 | 96.1k | #endif |
266 | 96.1k | size++; |
267 | 96.1k | lua_assert(ispow2(size) && size/2 < t->alimit && t->alimit < size); |
268 | 96.1k | return size; |
269 | 96.1k | } |
270 | 44.4M | } |
271 | | |
272 | | |
273 | | /* |
274 | | ** Check whether real size of the array is a power of 2. |
275 | | ** (If it is not, 'alimit' cannot be changed to any other value |
276 | | ** without changing the real size.) |
277 | | */ |
278 | 2.43M | static int ispow2realasize (const Table *t) { |
279 | 2.43M | return (!isrealasize(t) || ispow2(t->alimit)); |
280 | 2.43M | } |
281 | | |
282 | | |
283 | 10.9M | static unsigned int setlimittosize (Table *t) { |
284 | 10.9M | t->alimit = luaH_realasize(t); |
285 | 10.9M | setrealasize(t); |
286 | 10.9M | return t->alimit; |
287 | 10.9M | } |
288 | | |
289 | | |
290 | 3.38M | #define limitasasize(t) check_exp(isrealasize(t), t->alimit) |
291 | | |
292 | | |
293 | | |
294 | | /* |
295 | | ** "Generic" get version. (Not that generic: not valid for integers, |
296 | | ** which may be in array part, nor for floats with integral values.) |
297 | | ** See explanation about 'deadok' in function 'equalkey'. |
298 | | */ |
299 | 11.1M | static const TValue *getgeneric (Table *t, const TValue *key, int deadok) { |
300 | 11.1M | Node *n = mainpositionTV(t, key); |
301 | 15.0M | for (;;) { /* check whether 'key' is somewhere in the chain */ |
302 | 15.0M | if (equalkey(key, n, deadok)) |
303 | 7.65M | return gval(n); /* that's it */ |
304 | 7.35M | else { |
305 | 7.35M | int nx = gnext(n); |
306 | 7.35M | if (nx == 0) |
307 | 3.50M | return &absentkey; /* not found */ |
308 | 3.84M | n += nx; |
309 | 3.84M | } |
310 | 15.0M | } |
311 | 11.1M | } |
312 | | |
313 | | |
314 | | /* |
315 | | ** returns the index for 'k' if 'k' is an appropriate key to live in |
316 | | ** the array part of a table, 0 otherwise. |
317 | | */ |
318 | 876k | static unsigned int arrayindex (lua_Integer k) { |
319 | 876k | if (l_castS2U(k) - 1u < MAXASIZE) /* 'k' in [1, MAXASIZE]? */ |
320 | 707k | return cast_uint(k); /* 'key' is an appropriate array index */ |
321 | 169k | else |
322 | 169k | return 0; |
323 | 876k | } |
324 | | |
325 | | |
326 | | /* |
327 | | ** returns the index of a 'key' for table traversals. First goes all |
328 | | ** elements in the array part, then elements in the hash part. The |
329 | | ** beginning of a traversal is signaled by 0. |
330 | | */ |
331 | | static unsigned int findindex (lua_State *L, Table *t, TValue *key, |
332 | 5.60M | unsigned int asize) { |
333 | 5.60M | unsigned int i; |
334 | 5.60M | if (ttisnil(key)) return 0; /* first iteration */ |
335 | 5.27M | i = ttisinteger(key) ? arrayindex(ivalue(key)) : 0; |
336 | 5.27M | if (i - 1u < asize) /* is 'key' inside array part? */ |
337 | 109k | return i; /* yes; that's the index */ |
338 | 5.16M | else { |
339 | 5.16M | const TValue *n = getgeneric(t, key, 1); |
340 | 5.16M | if (l_unlikely(isabstkey(n))) |
341 | 0 | luaG_runerror(L, "invalid key to 'next'"); /* key not found */ |
342 | 5.16M | i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ |
343 | | /* hash elements are numbered after array ones */ |
344 | 5.16M | return (i + 1) + asize; |
345 | 5.16M | } |
346 | 5.27M | } |
347 | | |
348 | | |
349 | 5.60M | int luaH_next (lua_State *L, Table *t, StkId key) { |
350 | 5.60M | unsigned int asize = luaH_realasize(t); |
351 | 5.60M | unsigned int i = findindex(L, t, s2v(key), asize); /* find original key */ |
352 | 5.60M | for (; i < asize; i++) { /* try first array part */ |
353 | 109k | if (!isempty(&t->array[i])) { /* a non-empty entry? */ |
354 | 109k | setivalue(s2v(key), i + 1); |
355 | 109k | setobj2s(L, key + 1, &t->array[i]); |
356 | 109k | return 1; |
357 | 109k | } |
358 | 109k | } |
359 | 9.21M | for (i -= asize; cast_int(i) < sizenode(t); i++) { /* hash part */ |
360 | 9.03M | if (!isempty(gval(gnode(t, i)))) { /* a non-empty entry? */ |
361 | 5.31M | Node *n = gnode(t, i); |
362 | 5.31M | getnodekey(L, s2v(key), n); |
363 | 5.31M | setobj2s(L, key + 1, gval(n)); |
364 | 5.31M | return 1; |
365 | 5.31M | } |
366 | 9.03M | } |
367 | 181k | return 0; /* no more elements */ |
368 | 5.49M | } |
369 | | |
370 | | |
371 | 20.5M | static void freehash (lua_State *L, Table *t) { |
372 | 20.5M | if (!isdummy(t)) |
373 | 3.77M | luaM_freearray(L, t->node, cast_sizet(sizenode(t))); |
374 | 20.5M | } |
375 | | |
376 | | |
377 | | /* |
378 | | ** {============================================================= |
379 | | ** Rehash |
380 | | ** ============================================================== |
381 | | */ |
382 | | |
383 | | /* |
384 | | ** Compute the optimal size for the array part of table 't'. 'nums' is a |
385 | | ** "count array" where 'nums[i]' is the number of integers in the table |
386 | | ** between 2^(i - 1) + 1 and 2^i. 'pna' enters with the total number of |
387 | | ** integer keys in the table and leaves with the number of keys that |
388 | | ** will go to the array part; return the optimal size. (The condition |
389 | | ** 'twotoi > 0' in the for loop stops the loop if 'twotoi' overflows.) |
390 | | */ |
391 | 3.38M | static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { |
392 | 3.38M | int i; |
393 | 3.38M | unsigned int twotoi; /* 2^i (candidate for optimal size) */ |
394 | 3.38M | unsigned int a = 0; /* number of elements smaller than 2^i */ |
395 | 3.38M | unsigned int na = 0; /* number of elements to go to array part */ |
396 | 3.38M | unsigned int optimal = 0; /* optimal size for array part */ |
397 | | /* loop while keys can fill more than half of total size */ |
398 | 3.38M | for (i = 0, twotoi = 1; |
399 | 4.87M | twotoi > 0 && *pna > twotoi / 2; |
400 | 3.38M | i++, twotoi *= 2) { |
401 | 1.48M | a += nums[i]; |
402 | 1.48M | if (a > twotoi/2) { /* more than half elements present? */ |
403 | 1.09M | optimal = twotoi; /* optimal size (till now) */ |
404 | 1.09M | na = a; /* all elements up to 'optimal' will go to array part */ |
405 | 1.09M | } |
406 | 1.48M | } |
407 | 3.38M | lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal); |
408 | 3.38M | *pna = na; |
409 | 3.38M | return optimal; |
410 | 3.38M | } |
411 | | |
412 | | |
413 | 668k | static int countint (lua_Integer key, unsigned int *nums) { |
414 | 668k | unsigned int k = arrayindex(key); |
415 | 668k | if (k != 0) { /* is 'key' an appropriate array index? */ |
416 | 525k | nums[luaO_ceillog2(k)]++; /* count as such */ |
417 | 525k | return 1; |
418 | 525k | } |
419 | 143k | else |
420 | 143k | return 0; |
421 | 668k | } |
422 | | |
423 | | |
424 | | /* |
425 | | ** Count keys in array part of table 't': Fill 'nums[i]' with |
426 | | ** number of keys that will go into corresponding slice and return |
427 | | ** total number of non-nil keys. |
428 | | */ |
429 | 3.38M | static unsigned int numusearray (const Table *t, unsigned int *nums) { |
430 | 3.38M | int lg; |
431 | 3.38M | unsigned int ttlg; /* 2^lg */ |
432 | 3.38M | unsigned int ause = 0; /* summation of 'nums' */ |
433 | 3.38M | unsigned int i = 1; /* count to traverse all array keys */ |
434 | 3.38M | unsigned int asize = limitasasize(t); /* real array size */ |
435 | | /* traverse each slice */ |
436 | 4.47M | for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) { |
437 | 4.47M | unsigned int lc = 0; /* counter */ |
438 | 4.47M | unsigned int lim = ttlg; |
439 | 4.47M | if (lim > asize) { |
440 | 3.42M | lim = asize; /* adjust upper limit */ |
441 | 3.42M | if (i > lim) |
442 | 3.38M | break; /* no more elements to count */ |
443 | 3.42M | } |
444 | | /* count elements in range (2^(lg - 1), 2^lg] */ |
445 | 4.02M | for (; i <= lim; i++) { |
446 | 2.93M | if (!isempty(&t->array[i-1])) |
447 | 1.31M | lc++; |
448 | 2.93M | } |
449 | 1.09M | nums[lg] += lc; |
450 | 1.09M | ause += lc; |
451 | 1.09M | } |
452 | 3.38M | return ause; |
453 | 3.38M | } |
454 | | |
455 | | |
456 | 3.38M | static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) { |
457 | 3.38M | int totaluse = 0; /* total number of elements */ |
458 | 3.38M | int ause = 0; /* elements added to 'nums' (can go to array part) */ |
459 | 3.38M | int i = sizenode(t); |
460 | 11.0M | while (i--) { |
461 | 7.62M | Node *n = &t->node[i]; |
462 | 7.62M | if (!isempty(gval(n))) { |
463 | 5.78M | if (keyisinteger(n)) |
464 | 479k | ause += countint(keyival(n), nums); |
465 | 5.78M | totaluse++; |
466 | 5.78M | } |
467 | 7.62M | } |
468 | 3.38M | *pna += ause; |
469 | 3.38M | return totaluse; |
470 | 3.38M | } |
471 | | |
472 | | |
473 | | /* |
474 | | ** Creates an array for the hash part of a table with the given |
475 | | ** size, or reuses the dummy node if size is zero. |
476 | | ** The computation for size overflow is in two steps: the first |
477 | | ** comparison ensures that the shift in the second one does not |
478 | | ** overflow. |
479 | | */ |
480 | 20.5M | static void setnodevector (lua_State *L, Table *t, unsigned int size) { |
481 | 20.5M | if (size == 0) { /* no elements to hash part? */ |
482 | 16.7M | t->node = cast(Node *, dummynode); /* use common 'dummynode' */ |
483 | 16.7M | t->lsizenode = 0; |
484 | 16.7M | t->lastfree = NULL; /* signal that it is using dummy node */ |
485 | 16.7M | } |
486 | 3.77M | else { |
487 | 3.77M | int i; |
488 | 3.77M | int lsize = luaO_ceillog2(size); |
489 | 3.77M | if (lsize > MAXHBITS || (1u << lsize) > MAXHSIZE) |
490 | 0 | luaG_runerror(L, "table overflow"); |
491 | 3.77M | size = twoto(lsize); |
492 | 3.77M | t->node = luaM_newvector(L, size, Node); |
493 | 18.2M | for (i = 0; i < cast_int(size); i++) { |
494 | 14.4M | Node *n = gnode(t, i); |
495 | 14.4M | gnext(n) = 0; |
496 | 14.4M | setnilkey(n); |
497 | 14.4M | setempty(gval(n)); |
498 | 14.4M | } |
499 | 3.77M | t->lsizenode = cast_byte(lsize); |
500 | 3.77M | t->lastfree = gnode(t, size); /* all positions are free */ |
501 | 3.77M | } |
502 | 20.5M | } |
503 | | |
504 | | |
505 | | /* |
506 | | ** (Re)insert all elements from the hash part of 'ot' into table 't'. |
507 | | */ |
508 | 7.61M | static void reinsert (lua_State *L, Table *ot, Table *t) { |
509 | 7.61M | int j; |
510 | 7.61M | int size = sizenode(ot); |
511 | 19.4M | for (j = 0; j < size; j++) { |
512 | 11.8M | Node *old = gnode(ot, j); |
513 | 11.8M | if (!isempty(gval(old))) { |
514 | | /* doesn't need barrier/invalidate cache, as entry was |
515 | | already present in the table */ |
516 | 5.78M | TValue k; |
517 | 5.78M | getnodekey(L, &k, old); |
518 | 5.78M | luaH_set(L, t, &k, gval(old)); |
519 | 5.78M | } |
520 | 11.8M | } |
521 | 7.61M | } |
522 | | |
523 | | |
524 | | /* |
525 | | ** Exchange the hash part of 't1' and 't2'. |
526 | | */ |
527 | 7.71M | static void exchangehashpart (Table *t1, Table *t2) { |
528 | 7.71M | lu_byte lsizenode = t1->lsizenode; |
529 | 7.71M | Node *node = t1->node; |
530 | 7.71M | Node *lastfree = t1->lastfree; |
531 | 7.71M | t1->lsizenode = t2->lsizenode; |
532 | 7.71M | t1->node = t2->node; |
533 | 7.71M | t1->lastfree = t2->lastfree; |
534 | 7.71M | t2->lsizenode = lsizenode; |
535 | 7.71M | t2->node = node; |
536 | 7.71M | t2->lastfree = lastfree; |
537 | 7.71M | } |
538 | | |
539 | | |
540 | | /* |
541 | | ** Resize table 't' for the new given sizes. Both allocations (for |
542 | | ** the hash part and for the array part) can fail, which creates some |
543 | | ** subtleties. If the first allocation, for the hash part, fails, an |
544 | | ** error is raised and that is it. Otherwise, it copies the elements from |
545 | | ** the shrinking part of the array (if it is shrinking) into the new |
546 | | ** hash. Then it reallocates the array part. If that fails, the table |
547 | | ** is in its original state; the function frees the new hash part and then |
548 | | ** raises the allocation error. Otherwise, it sets the new hash part |
549 | | ** into the table, initializes the new part of the array (if any) with |
550 | | ** nils and reinserts the elements of the old hash back into the new |
551 | | ** parts of the table. |
552 | | */ |
553 | | void luaH_resize (lua_State *L, Table *t, unsigned int newasize, |
554 | 7.61M | unsigned int nhsize) { |
555 | 7.61M | unsigned int i; |
556 | 7.61M | Table newt; /* to keep the new hash part */ |
557 | 7.61M | unsigned int oldasize = setlimittosize(t); |
558 | 7.61M | TValue *newarray; |
559 | | /* create new hash part with appropriate size into 'newt' */ |
560 | 7.61M | setnodevector(L, &newt, nhsize); |
561 | 7.61M | if (newasize < oldasize) { /* will array shrink? */ |
562 | 49.8k | t->alimit = newasize; /* pretend array has new size... */ |
563 | 49.8k | exchangehashpart(t, &newt); /* and new hash */ |
564 | | /* re-insert into the new hash the elements from vanishing slice */ |
565 | 1.75M | for (i = newasize; i < oldasize; i++) { |
566 | 1.70M | if (!isempty(&t->array[i])) |
567 | 242k | luaH_setint(L, t, i + 1, &t->array[i]); |
568 | 1.70M | } |
569 | 49.8k | t->alimit = oldasize; /* restore current size... */ |
570 | 49.8k | exchangehashpart(t, &newt); /* and hash (in case of errors) */ |
571 | 49.8k | } |
572 | | /* allocate new array */ |
573 | 7.61M | newarray = luaM_reallocvector(L, t->array, oldasize, newasize, TValue); |
574 | 7.61M | if (l_unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */ |
575 | 0 | freehash(L, &newt); /* release new hash part */ |
576 | 0 | luaM_error(L); /* raise error (with array unchanged) */ |
577 | 0 | } |
578 | | /* allocation ok; initialize new part of the array */ |
579 | 7.61M | exchangehashpart(t, &newt); /* 't' has the new hash ('newt' has the old) */ |
580 | 7.61M | t->array = newarray; /* set new array part */ |
581 | 7.61M | t->alimit = newasize; |
582 | 22.4M | for (i = oldasize; i < newasize; i++) /* clear new slice of the array */ |
583 | 14.8M | setempty(&t->array[i]); |
584 | | /* re-insert elements from old hash part into new parts */ |
585 | 7.61M | reinsert(L, &newt, t); /* 'newt' now has the old hash */ |
586 | 7.61M | freehash(L, &newt); /* free old hash part */ |
587 | 7.61M | } |
588 | | |
589 | | |
590 | 13.1k | void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) { |
591 | 13.1k | int nsize = allocsizenode(t); |
592 | 13.1k | luaH_resize(L, t, nasize, nsize); |
593 | 13.1k | } |
594 | | |
595 | | /* |
596 | | ** nums[i] = number of keys 'k' where 2^(i - 1) < k <= 2^i |
597 | | */ |
598 | 3.38M | static void rehash (lua_State *L, Table *t, const TValue *ek) { |
599 | 3.38M | unsigned int asize; /* optimal size for array part */ |
600 | 3.38M | unsigned int na; /* number of keys in the array part */ |
601 | 3.38M | unsigned int nums[MAXABITS + 1]; |
602 | 3.38M | int i; |
603 | 3.38M | int totaluse; |
604 | 111M | for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */ |
605 | 3.38M | setlimittosize(t); |
606 | 3.38M | na = numusearray(t, nums); /* count keys in array part */ |
607 | 3.38M | totaluse = na; /* all those keys are integer keys */ |
608 | 3.38M | totaluse += numusehash(t, nums, &na); /* count keys in hash part */ |
609 | | /* count extra key */ |
610 | 3.38M | if (ttisinteger(ek)) |
611 | 189k | na += countint(ivalue(ek), nums); |
612 | 3.38M | totaluse++; |
613 | | /* compute new size for array part */ |
614 | 3.38M | asize = computesizes(nums, &na); |
615 | | /* resize the table to new computed sizes */ |
616 | 3.38M | luaH_resize(L, t, asize, totaluse - na); |
617 | 3.38M | } |
618 | | |
619 | | |
620 | | |
621 | | /* |
622 | | ** }============================================================= |
623 | | */ |
624 | | |
625 | | |
626 | 12.9M | Table *luaH_new (lua_State *L) { |
627 | 12.9M | GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table)); |
628 | 12.9M | Table *t = gco2t(o); |
629 | 12.9M | t->metatable = NULL; |
630 | 12.9M | t->flags = cast_byte(maskflags); /* table has no metamethod fields */ |
631 | 12.9M | t->array = NULL; |
632 | 12.9M | t->alimit = 0; |
633 | 12.9M | setnodevector(L, t, 0); |
634 | 12.9M | return t; |
635 | 12.9M | } |
636 | | |
637 | | |
638 | 12.9M | void luaH_free (lua_State *L, Table *t) { |
639 | 12.9M | freehash(L, t); |
640 | 12.9M | luaM_freearray(L, t->array, luaH_realasize(t)); |
641 | 12.9M | luaM_free(L, t); |
642 | 12.9M | } |
643 | | |
644 | | |
645 | 7.44M | static Node *getfreepos (Table *t) { |
646 | 7.44M | if (!isdummy(t)) { |
647 | 10.3M | while (t->lastfree > t->node) { |
648 | 8.78M | t->lastfree--; |
649 | 8.78M | if (keyisnil(t->lastfree)) |
650 | 4.05M | return t->lastfree; |
651 | 8.78M | } |
652 | 5.63M | } |
653 | 3.38M | return NULL; /* could not find a free place */ |
654 | 7.44M | } |
655 | | |
656 | | |
657 | | |
658 | | /* |
659 | | ** inserts a new key into a hash table; first, check whether key's main |
660 | | ** position is free. If not, check whether colliding node is in its main |
661 | | ** position or not: if it is not, move colliding node to an empty place and |
662 | | ** put new key in its main position; otherwise (colliding node is in its main |
663 | | ** position), new key goes to an empty position. |
664 | | */ |
665 | | static void luaH_newkey (lua_State *L, Table *t, const TValue *key, |
666 | 21.5M | TValue *value) { |
667 | 21.5M | Node *mp; |
668 | 21.5M | TValue aux; |
669 | 21.5M | if (l_unlikely(ttisnil(key))) |
670 | 4 | luaG_runerror(L, "table index is nil"); |
671 | 21.5M | else if (ttisfloat(key)) { |
672 | 515k | lua_Number f = fltvalue(key); |
673 | 515k | lua_Integer k; |
674 | 515k | if (luaV_flttointeger(f, &k, F2Ieq)) { /* does key fit in an integer? */ |
675 | 3.40k | setivalue(&aux, k); |
676 | 3.40k | key = &aux; /* insert it as an integer */ |
677 | 3.40k | } |
678 | 512k | else if (l_unlikely(luai_numisnan(f))) |
679 | 0 | luaG_runerror(L, "table index is NaN"); |
680 | 515k | } |
681 | 21.5M | if (ttisnil(value)) |
682 | 5.59M | return; /* do not insert nil values */ |
683 | 15.9M | mp = mainpositionTV(t, key); |
684 | 15.9M | if (!isempty(gval(mp)) || isdummy(t)) { /* main position is taken? */ |
685 | 7.44M | Node *othern; |
686 | 7.44M | Node *f = getfreepos(t); /* get a free place */ |
687 | 7.44M | if (f == NULL) { /* cannot find a free place? */ |
688 | 3.38M | rehash(L, t, key); /* grow table */ |
689 | | /* whatever called 'newkey' takes care of TM cache */ |
690 | 3.38M | luaH_set(L, t, key, value); /* insert key into grown table */ |
691 | 3.38M | return; |
692 | 3.38M | } |
693 | 4.05M | lua_assert(!isdummy(t)); |
694 | 4.05M | othern = mainpositionfromnode(t, mp); |
695 | 4.05M | if (othern != mp) { /* is colliding node out of its main position? */ |
696 | | /* yes; move colliding node into free position */ |
697 | 1.03M | while (othern + gnext(othern) != mp) /* find previous */ |
698 | 224k | othern += gnext(othern); |
699 | 806k | gnext(othern) = cast_int(f - othern); /* rechain to point to 'f' */ |
700 | 806k | *f = *mp; /* copy colliding node into free pos. (mp->next also goes) */ |
701 | 806k | if (gnext(mp) != 0) { |
702 | 144k | gnext(f) += cast_int(mp - f); /* correct 'next' */ |
703 | 144k | gnext(mp) = 0; /* now 'mp' is free */ |
704 | 144k | } |
705 | 806k | setempty(gval(mp)); |
706 | 806k | } |
707 | 3.24M | else { /* colliding node is in its own main position */ |
708 | | /* new node will go into free position */ |
709 | 3.24M | if (gnext(mp) != 0) |
710 | 712k | gnext(f) = cast_int((mp + gnext(mp)) - f); /* chain new position */ |
711 | 2.53M | else lua_assert(gnext(f) == 0); |
712 | 3.24M | gnext(mp) = cast_int(f - mp); |
713 | 3.24M | mp = f; |
714 | 3.24M | } |
715 | 4.05M | } |
716 | 12.5M | setnodekey(L, mp, key); |
717 | 12.5M | luaC_barrierback(L, obj2gco(t), key); |
718 | 12.5M | lua_assert(isempty(gval(mp))); |
719 | 12.5M | setobj2t(L, gval(mp), value); |
720 | 12.5M | } |
721 | | |
722 | | |
723 | | /* |
724 | | ** Search function for integers. If integer is inside 'alimit', get it |
725 | | ** directly from the array part. Otherwise, if 'alimit' is not equal to |
726 | | ** the real size of the array, key still can be in the array part. In |
727 | | ** this case, try to avoid a call to 'luaH_realasize' when key is just |
728 | | ** one more than the limit (so that it can be incremented without |
729 | | ** changing the real size of the array). |
730 | | */ |
731 | 5.59M | const TValue *luaH_getint (Table *t, lua_Integer key) { |
732 | 5.59M | if (l_castS2U(key) - 1u < t->alimit) /* 'key' in [1, t->alimit]? */ |
733 | 196k | return &t->array[key - 1]; |
734 | 5.39M | else if (!limitequalsasize(t) && /* key still may be in the array part? */ |
735 | 5.39M | (l_castS2U(key) == t->alimit + 1 || |
736 | 54.7k | l_castS2U(key) - 1u < luaH_realasize(t))) { |
737 | 20.5k | t->alimit = cast_uint(key); /* probably '#t' is here now */ |
738 | 20.5k | return &t->array[key - 1]; |
739 | 20.5k | } |
740 | 5.37M | else { |
741 | 5.37M | Node *n = hashint(t, key); |
742 | 7.70M | for (;;) { /* check whether 'key' is somewhere in the chain */ |
743 | 7.70M | if (keyisinteger(n) && keyival(n) == key) |
744 | 2.35M | return gval(n); /* that's it */ |
745 | 5.34M | else { |
746 | 5.34M | int nx = gnext(n); |
747 | 5.34M | if (nx == 0) break; |
748 | 2.32M | n += nx; |
749 | 2.32M | } |
750 | 7.70M | } |
751 | 3.01M | return &absentkey; |
752 | 5.37M | } |
753 | 5.59M | } |
754 | | |
755 | | |
756 | | /* |
757 | | ** search function for short strings |
758 | | */ |
759 | 427M | const TValue *luaH_getshortstr (Table *t, TString *key) { |
760 | 427M | Node *n = hashstr(t, key); |
761 | 427M | lua_assert(key->tt == LUA_VSHRSTR); |
762 | 566M | for (;;) { /* check whether 'key' is somewhere in the chain */ |
763 | 566M | if (keyisshrstr(n) && eqshrstr(keystrval(n), key)) |
764 | 215M | return gval(n); /* that's it */ |
765 | 351M | else { |
766 | 351M | int nx = gnext(n); |
767 | 351M | if (nx == 0) |
768 | 211M | return &absentkey; /* not found */ |
769 | 139M | n += nx; |
770 | 139M | } |
771 | 566M | } |
772 | 427M | } |
773 | | |
774 | | |
775 | 28.6M | const TValue *luaH_getstr (Table *t, TString *key) { |
776 | 28.6M | if (key->tt == LUA_VSHRSTR) |
777 | 28.2M | return luaH_getshortstr(t, key); |
778 | 415k | else { /* for long strings, use generic case */ |
779 | 415k | TValue ko; |
780 | 415k | setsvalue(cast(lua_State *, NULL), &ko, key); |
781 | 415k | return getgeneric(t, &ko, 0); |
782 | 415k | } |
783 | 28.6M | } |
784 | | |
785 | | |
786 | | /* |
787 | | ** main search function |
788 | | */ |
789 | 37.6M | const TValue *luaH_get (Table *t, const TValue *key) { |
790 | 37.6M | switch (ttypetag(key)) { |
791 | 29.4M | case LUA_VSHRSTR: return luaH_getshortstr(t, tsvalue(key)); |
792 | 2.68M | case LUA_VNUMINT: return luaH_getint(t, ivalue(key)); |
793 | 1.26k | case LUA_VNIL: return &absentkey; |
794 | 1.17M | case LUA_VNUMFLT: { |
795 | 1.17M | lua_Integer k; |
796 | 1.17M | if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */ |
797 | 11.1k | return luaH_getint(t, k); /* use specialized version */ |
798 | | /* else... */ |
799 | 1.17M | } /* FALLTHROUGH */ |
800 | 5.58M | default: |
801 | 5.58M | return getgeneric(t, key, 0); |
802 | 37.6M | } |
803 | 37.6M | } |
804 | | |
805 | | |
806 | | /* |
807 | | ** Finish a raw "set table" operation, where 'slot' is where the value |
808 | | ** should have been (the result of a previous "get table"). |
809 | | ** Beware: when using this function you probably need to check a GC |
810 | | ** barrier and invalidate the TM cache. |
811 | | */ |
812 | | void luaH_finishset (lua_State *L, Table *t, const TValue *key, |
813 | 26.9M | const TValue *slot, TValue *value) { |
814 | 26.9M | if (isabstkey(slot)) |
815 | 21.2M | luaH_newkey(L, t, key, value); |
816 | 5.63M | else |
817 | 5.63M | setobj2t(L, cast(TValue *, slot), value); |
818 | 26.9M | } |
819 | | |
820 | | |
821 | | /* |
822 | | ** beware: when using this function you probably need to check a GC |
823 | | ** barrier and invalidate the TM cache. |
824 | | */ |
825 | 9.17M | void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value) { |
826 | 9.17M | const TValue *slot = luaH_get(t, key); |
827 | 9.17M | luaH_finishset(L, t, key, slot, value); |
828 | 9.17M | } |
829 | | |
830 | | |
831 | 264k | void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { |
832 | 264k | const TValue *p = luaH_getint(t, key); |
833 | 264k | if (isabstkey(p)) { |
834 | 242k | TValue k; |
835 | 242k | setivalue(&k, key); |
836 | 242k | luaH_newkey(L, t, &k, value); |
837 | 242k | } |
838 | 22.2k | else |
839 | 22.2k | setobj2t(L, cast(TValue *, p), value); |
840 | 264k | } |
841 | | |
842 | | |
843 | | /* |
844 | | ** Try to find a boundary in the hash part of table 't'. From the |
845 | | ** caller, we know that 'j' is zero or present and that 'j + 1' is |
846 | | ** present. We want to find a larger key that is absent from the |
847 | | ** table, so that we can do a binary search between the two keys to |
848 | | ** find a boundary. We keep doubling 'j' until we get an absent index. |
849 | | ** If the doubling would overflow, we try LUA_MAXINTEGER. If it is |
850 | | ** absent, we are ready for the binary search. ('j', being max integer, |
851 | | ** is larger or equal to 'i', but it cannot be equal because it is |
852 | | ** absent while 'i' is present; so 'j > i'.) Otherwise, 'j' is a |
853 | | ** boundary. ('j + 1' cannot be a present integer key because it is |
854 | | ** not a valid integer in Lua.) |
855 | | */ |
856 | 73.2k | static lua_Unsigned hash_search (Table *t, lua_Unsigned j) { |
857 | 73.2k | lua_Unsigned i; |
858 | 73.2k | if (j == 0) j++; /* the caller ensures 'j + 1' is present */ |
859 | 456k | do { |
860 | 456k | i = j; /* 'i' is a present index */ |
861 | 456k | if (j <= l_castS2U(LUA_MAXINTEGER) / 2) |
862 | 449k | j *= 2; |
863 | 6.97k | else { |
864 | 6.97k | j = LUA_MAXINTEGER; |
865 | 6.97k | if (isempty(luaH_getint(t, j))) /* t[j] not present? */ |
866 | 6.97k | break; /* 'j' now is an absent index */ |
867 | 0 | else /* weird case */ |
868 | 0 | return j; /* well, max integer is a boundary... */ |
869 | 6.97k | } |
870 | 456k | } while (!isempty(luaH_getint(t, j))); /* repeat until an absent t[j] */ |
871 | | /* i < j && t[i] present && t[j] absent */ |
872 | 624k | while (j - i > 1u) { /* do a binary search between them */ |
873 | 551k | lua_Unsigned m = (i + j) / 2; |
874 | 551k | if (isempty(luaH_getint(t, m))) j = m; |
875 | 59.4k | else i = m; |
876 | 551k | } |
877 | 73.2k | return i; |
878 | 73.2k | } |
879 | | |
880 | | |
881 | | static unsigned int binsearch (const TValue *array, unsigned int i, |
882 | 2.35M | unsigned int j) { |
883 | 2.42M | while (j - i > 1u) { /* binary search */ |
884 | 69.5k | unsigned int m = (i + j) / 2; |
885 | 69.5k | if (isempty(&array[m - 1])) j = m; |
886 | 36.8k | else i = m; |
887 | 69.5k | } |
888 | 2.35M | return i; |
889 | 2.35M | } |
890 | | |
891 | | |
892 | | /* |
893 | | ** Try to find a boundary in table 't'. (A 'boundary' is an integer index |
894 | | ** such that t[i] is present and t[i+1] is absent, or 0 if t[1] is absent |
895 | | ** and 'maxinteger' if t[maxinteger] is present.) |
896 | | ** (In the next explanation, we use Lua indices, that is, with base 1. |
897 | | ** The code itself uses base 0 when indexing the array part of the table.) |
898 | | ** The code starts with 'limit = t->alimit', a position in the array |
899 | | ** part that may be a boundary. |
900 | | ** |
901 | | ** (1) If 't[limit]' is empty, there must be a boundary before it. |
902 | | ** As a common case (e.g., after 't[#t]=nil'), check whether 'limit-1' |
903 | | ** is present. If so, it is a boundary. Otherwise, do a binary search |
904 | | ** between 0 and limit to find a boundary. In both cases, try to |
905 | | ** use this boundary as the new 'alimit', as a hint for the next call. |
906 | | ** |
907 | | ** (2) If 't[limit]' is not empty and the array has more elements |
908 | | ** after 'limit', try to find a boundary there. Again, try first |
909 | | ** the special case (which should be quite frequent) where 'limit+1' |
910 | | ** is empty, so that 'limit' is a boundary. Otherwise, check the |
911 | | ** last element of the array part. If it is empty, there must be a |
912 | | ** boundary between the old limit (present) and the last element |
913 | | ** (absent), which is found with a binary search. (This boundary always |
914 | | ** can be a new limit.) |
915 | | ** |
916 | | ** (3) The last case is when there are no elements in the array part |
917 | | ** (limit == 0) or its last element (the new limit) is present. |
918 | | ** In this case, must check the hash part. If there is no hash part |
919 | | ** or 'limit+1' is absent, 'limit' is a boundary. Otherwise, call |
920 | | ** 'hash_search' to find a boundary in the hash part of the table. |
921 | | ** (In those cases, the boundary is not inside the array part, and |
922 | | ** therefore cannot be used as a new limit.) |
923 | | */ |
924 | 4.62M | lua_Unsigned luaH_getn (Table *t) { |
925 | 4.62M | unsigned int limit = t->alimit; |
926 | 4.62M | if (limit > 0 && isempty(&t->array[limit - 1])) { /* (1)? */ |
927 | | /* there must be a boundary before 'limit' */ |
928 | 2.43M | if (limit >= 2 && !isempty(&t->array[limit - 2])) { |
929 | | /* 'limit - 1' is a boundary; can it be a new limit? */ |
930 | 78.7k | if (ispow2realasize(t) && !ispow2(limit - 1)) { |
931 | 69.9k | t->alimit = limit - 1; |
932 | 69.9k | setnorealasize(t); /* now 'alimit' is not the real size */ |
933 | 69.9k | } |
934 | 78.7k | return limit - 1; |
935 | 78.7k | } |
936 | 2.35M | else { /* must search for a boundary in [0, limit] */ |
937 | 2.35M | unsigned int boundary = binsearch(t->array, 0, limit); |
938 | | /* can this boundary represent the real size of the array? */ |
939 | 2.35M | if (ispow2realasize(t) && boundary > luaH_realasize(t) / 2) { |
940 | 11.3k | t->alimit = boundary; /* use it as the new limit */ |
941 | 11.3k | setnorealasize(t); |
942 | 11.3k | } |
943 | 2.35M | return boundary; |
944 | 2.35M | } |
945 | 2.43M | } |
946 | | /* 'limit' is zero or present in table */ |
947 | 2.18M | if (!limitequalsasize(t)) { /* (2)? */ |
948 | | /* 'limit' > 0 and array has more elements after 'limit' */ |
949 | 15.5k | if (isempty(&t->array[limit])) /* 'limit + 1' is empty? */ |
950 | 15.5k | return limit; /* this is the boundary */ |
951 | | /* else, try last element in the array */ |
952 | 0 | limit = luaH_realasize(t); |
953 | 0 | if (isempty(&t->array[limit - 1])) { /* empty? */ |
954 | | /* there must be a boundary in the array after old limit, |
955 | | and it must be a valid new limit */ |
956 | 0 | unsigned int boundary = binsearch(t->array, t->alimit, limit); |
957 | 0 | t->alimit = boundary; |
958 | 0 | return boundary; |
959 | 0 | } |
960 | | /* else, new limit is present in the table; check the hash part */ |
961 | 0 | } |
962 | | /* (3) 'limit' is the last element and either is zero or present in table */ |
963 | 2.17M | lua_assert(limit == luaH_realasize(t) && |
964 | 2.17M | (limit == 0 || !isempty(&t->array[limit - 1]))); |
965 | 2.17M | if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, limit + 1)))) |
966 | 2.09M | return limit; /* 'limit + 1' is absent */ |
967 | 73.2k | else /* 'limit + 1' is also present */ |
968 | 73.2k | return hash_search(t, limit); |
969 | 2.17M | } |
970 | | |
971 | | |
972 | | |
973 | | #if defined(LUA_DEBUG) |
974 | | |
975 | | /* export these functions for the test library */ |
976 | | |
977 | | Node *luaH_mainposition (const Table *t, const TValue *key) { |
978 | | return mainpositionTV(t, key); |
979 | | } |
980 | | |
981 | | #endif |