1*61145dc2SMartin Matuska // SPDX-License-Identifier: MIT
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy ** $Id: ltable.c,v 2.72.1.1 2013/04/12 18:48:47 roberto Exp $
4eda14cbcSMatt Macy ** Lua tables (hash)
5eda14cbcSMatt Macy ** See Copyright Notice in lua.h
6eda14cbcSMatt Macy */
7eda14cbcSMatt Macy
8eda14cbcSMatt Macy
9eda14cbcSMatt Macy /*
10eda14cbcSMatt Macy ** Implementation of tables (aka arrays, objects, or hash tables).
11eda14cbcSMatt Macy ** Tables keep its elements in two parts: an array part and a hash part.
12eda14cbcSMatt Macy ** Non-negative integer keys are all candidates to be kept in the array
13eda14cbcSMatt Macy ** part. The actual size of the array is the largest `n' such that at
14eda14cbcSMatt Macy ** least half the slots between 0 and n are in use.
15eda14cbcSMatt Macy ** Hash uses a mix of chained scatter table with Brent's variation.
16eda14cbcSMatt Macy ** A main invariant of these tables is that, if an element is not
17eda14cbcSMatt Macy ** in its main position (i.e. the `original' position that its hash gives
18eda14cbcSMatt Macy ** to it), then the colliding element is in its own main position.
19eda14cbcSMatt Macy ** Hence even when the load factor reaches 100%, performance remains good.
20eda14cbcSMatt Macy */
21eda14cbcSMatt Macy
22eda14cbcSMatt Macy
23eda14cbcSMatt Macy #define ltable_c
24eda14cbcSMatt Macy #define LUA_CORE
25eda14cbcSMatt Macy
26eda14cbcSMatt Macy #include <sys/lua/lua.h>
27eda14cbcSMatt Macy
28eda14cbcSMatt Macy #include "ldebug.h"
29eda14cbcSMatt Macy #include "ldo.h"
30eda14cbcSMatt Macy #include "lgc.h"
31eda14cbcSMatt Macy #include "lmem.h"
32eda14cbcSMatt Macy #include "lobject.h"
33eda14cbcSMatt Macy #include "lstate.h"
34eda14cbcSMatt Macy #include "lstring.h"
35eda14cbcSMatt Macy #include "ltable.h"
36eda14cbcSMatt Macy #include "lvm.h"
37eda14cbcSMatt Macy
38eda14cbcSMatt Macy
39eda14cbcSMatt Macy /*
40eda14cbcSMatt Macy ** max size of array part is 2^MAXBITS
41eda14cbcSMatt Macy */
42eda14cbcSMatt Macy #if LUAI_BITSINT >= 32
43eda14cbcSMatt Macy #define MAXBITS 30
44eda14cbcSMatt Macy #else
45eda14cbcSMatt Macy #define MAXBITS (LUAI_BITSINT-2)
46eda14cbcSMatt Macy #endif
47eda14cbcSMatt Macy
48eda14cbcSMatt Macy #define MAXASIZE (1 << MAXBITS)
49eda14cbcSMatt Macy
50eda14cbcSMatt Macy
51eda14cbcSMatt Macy #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
52eda14cbcSMatt Macy
53eda14cbcSMatt Macy #define hashstr(t,str) hashpow2(t, (str)->tsv.hash)
54eda14cbcSMatt Macy #define hashboolean(t,p) hashpow2(t, p)
55eda14cbcSMatt Macy
56eda14cbcSMatt Macy
57eda14cbcSMatt Macy /*
58eda14cbcSMatt Macy ** for some types, it is better to avoid modulus by power of 2, as
59eda14cbcSMatt Macy ** they tend to have many 2 factors.
60eda14cbcSMatt Macy */
61eda14cbcSMatt Macy #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
62eda14cbcSMatt Macy
63eda14cbcSMatt Macy
64eda14cbcSMatt Macy #define hashpointer(t,p) hashmod(t, IntPoint(p))
65eda14cbcSMatt Macy
66eda14cbcSMatt Macy
67eda14cbcSMatt Macy #define dummynode (&dummynode_)
68eda14cbcSMatt Macy
69eda14cbcSMatt Macy #define isdummy(n) ((n) == dummynode)
70eda14cbcSMatt Macy
71eda14cbcSMatt Macy static const Node dummynode_ = {
72eda14cbcSMatt Macy {NILCONSTANT}, /* value */
73eda14cbcSMatt Macy {{NILCONSTANT, NULL}} /* key */
74eda14cbcSMatt Macy };
75eda14cbcSMatt Macy
76eda14cbcSMatt Macy
77eda14cbcSMatt Macy /*
78eda14cbcSMatt Macy ** hash for lua_Numbers
79eda14cbcSMatt Macy */
hashnum(const Table * t,lua_Number n)80eda14cbcSMatt Macy static Node *hashnum (const Table *t, lua_Number n) {
81eda14cbcSMatt Macy int i;
82eda14cbcSMatt Macy luai_hashnum(i, n);
83eda14cbcSMatt Macy if (i < 0) {
84eda14cbcSMatt Macy if (cast(unsigned int, i) == 0u - i) /* use unsigned to avoid overflows */
85eda14cbcSMatt Macy i = 0; /* handle INT_MIN */
86eda14cbcSMatt Macy i = -i; /* must be a positive value */
87eda14cbcSMatt Macy }
88eda14cbcSMatt Macy return hashmod(t, i);
89eda14cbcSMatt Macy }
90eda14cbcSMatt Macy
91eda14cbcSMatt Macy
92eda14cbcSMatt Macy
93eda14cbcSMatt Macy /*
94eda14cbcSMatt Macy ** returns the `main' position of an element in a table (that is, the index
95eda14cbcSMatt Macy ** of its hash value)
96eda14cbcSMatt Macy */
mainposition(const Table * t,const TValue * key)97eda14cbcSMatt Macy static Node *mainposition (const Table *t, const TValue *key) {
98eda14cbcSMatt Macy switch (ttype(key)) {
99eda14cbcSMatt Macy case LUA_TNUMBER:
100eda14cbcSMatt Macy return hashnum(t, nvalue(key));
101eda14cbcSMatt Macy case LUA_TLNGSTR: {
102eda14cbcSMatt Macy TString *s = rawtsvalue(key);
103eda14cbcSMatt Macy if (s->tsv.extra == 0) { /* no hash? */
104eda14cbcSMatt Macy s->tsv.hash = luaS_hash(getstr(s), s->tsv.len, s->tsv.hash);
105eda14cbcSMatt Macy s->tsv.extra = 1; /* now it has its hash */
106eda14cbcSMatt Macy }
107eda14cbcSMatt Macy return hashstr(t, rawtsvalue(key));
108eda14cbcSMatt Macy }
109eda14cbcSMatt Macy case LUA_TSHRSTR:
110eda14cbcSMatt Macy return hashstr(t, rawtsvalue(key));
111eda14cbcSMatt Macy case LUA_TBOOLEAN:
112eda14cbcSMatt Macy return hashboolean(t, bvalue(key));
113eda14cbcSMatt Macy case LUA_TLIGHTUSERDATA:
114eda14cbcSMatt Macy return hashpointer(t, pvalue(key));
115eda14cbcSMatt Macy case LUA_TLCF:
116eda14cbcSMatt Macy return hashpointer(t, fvalue(key));
117eda14cbcSMatt Macy default:
118eda14cbcSMatt Macy return hashpointer(t, gcvalue(key));
119eda14cbcSMatt Macy }
120eda14cbcSMatt Macy }
121eda14cbcSMatt Macy
122eda14cbcSMatt Macy
123eda14cbcSMatt Macy /*
124eda14cbcSMatt Macy ** returns the index for `key' if `key' is an appropriate key to live in
125eda14cbcSMatt Macy ** the array part of the table, -1 otherwise.
126eda14cbcSMatt Macy */
arrayindex(const TValue * key)127eda14cbcSMatt Macy static int arrayindex (const TValue *key) {
128eda14cbcSMatt Macy if (ttisnumber(key)) {
129eda14cbcSMatt Macy lua_Number n = nvalue(key);
130eda14cbcSMatt Macy int k;
131eda14cbcSMatt Macy lua_number2int(k, n);
132eda14cbcSMatt Macy if (luai_numeq(cast_num(k), n))
133eda14cbcSMatt Macy return k;
134eda14cbcSMatt Macy }
135eda14cbcSMatt Macy return -1; /* `key' did not match some condition */
136eda14cbcSMatt Macy }
137eda14cbcSMatt Macy
138eda14cbcSMatt Macy
139eda14cbcSMatt Macy /*
140eda14cbcSMatt Macy ** returns the index of a `key' for table traversals. First goes all
141eda14cbcSMatt Macy ** elements in the array part, then elements in the hash part. The
142eda14cbcSMatt Macy ** beginning of a traversal is signaled by -1.
143eda14cbcSMatt Macy */
findindex(lua_State * L,Table * t,StkId key)144eda14cbcSMatt Macy static int findindex (lua_State *L, Table *t, StkId key) {
145eda14cbcSMatt Macy int i;
146eda14cbcSMatt Macy if (ttisnil(key)) return -1; /* first iteration */
147eda14cbcSMatt Macy i = arrayindex(key);
148eda14cbcSMatt Macy if (0 < i && i <= t->sizearray) /* is `key' inside array part? */
149eda14cbcSMatt Macy return i-1; /* yes; that's the index (corrected to C) */
150eda14cbcSMatt Macy else {
151eda14cbcSMatt Macy Node *n = mainposition(t, key);
152eda14cbcSMatt Macy for (;;) { /* check whether `key' is somewhere in the chain */
153eda14cbcSMatt Macy /* key may be dead already, but it is ok to use it in `next' */
154eda14cbcSMatt Macy if (luaV_rawequalobj(gkey(n), key) ||
155eda14cbcSMatt Macy (ttisdeadkey(gkey(n)) && iscollectable(key) &&
156eda14cbcSMatt Macy deadvalue(gkey(n)) == gcvalue(key))) {
157eda14cbcSMatt Macy i = cast_int(n - gnode(t, 0)); /* key index in hash table */
158eda14cbcSMatt Macy /* hash elements are numbered after array ones */
159eda14cbcSMatt Macy return i + t->sizearray;
160eda14cbcSMatt Macy }
161eda14cbcSMatt Macy else n = gnext(n);
162eda14cbcSMatt Macy if (n == NULL)
163eda14cbcSMatt Macy luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */
164eda14cbcSMatt Macy }
165eda14cbcSMatt Macy }
166eda14cbcSMatt Macy }
167eda14cbcSMatt Macy
168eda14cbcSMatt Macy
luaH_next(lua_State * L,Table * t,StkId key)169eda14cbcSMatt Macy int luaH_next (lua_State *L, Table *t, StkId key) {
170eda14cbcSMatt Macy int i = findindex(L, t, key); /* find original element */
171eda14cbcSMatt Macy for (i++; i < t->sizearray; i++) { /* try first array part */
172eda14cbcSMatt Macy if (!ttisnil(&t->array[i])) { /* a non-nil value? */
173eda14cbcSMatt Macy setnvalue(key, cast_num(i+1));
174eda14cbcSMatt Macy setobj2s(L, key+1, &t->array[i]);
175eda14cbcSMatt Macy return 1;
176eda14cbcSMatt Macy }
177eda14cbcSMatt Macy }
178eda14cbcSMatt Macy for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */
179eda14cbcSMatt Macy if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */
180eda14cbcSMatt Macy setobj2s(L, key, gkey(gnode(t, i)));
181eda14cbcSMatt Macy setobj2s(L, key+1, gval(gnode(t, i)));
182eda14cbcSMatt Macy return 1;
183eda14cbcSMatt Macy }
184eda14cbcSMatt Macy }
185eda14cbcSMatt Macy return 0; /* no more elements */
186eda14cbcSMatt Macy }
187eda14cbcSMatt Macy
188eda14cbcSMatt Macy
189eda14cbcSMatt Macy /*
190eda14cbcSMatt Macy ** {=============================================================
191eda14cbcSMatt Macy ** Rehash
192eda14cbcSMatt Macy ** ==============================================================
193eda14cbcSMatt Macy */
194eda14cbcSMatt Macy
195eda14cbcSMatt Macy
computesizes(int nums[],int * narray)196eda14cbcSMatt Macy static int computesizes (int nums[], int *narray) {
197eda14cbcSMatt Macy int i;
198eda14cbcSMatt Macy int twotoi; /* 2^i */
199eda14cbcSMatt Macy int a = 0; /* number of elements smaller than 2^i */
200eda14cbcSMatt Macy int na = 0; /* number of elements to go to array part */
201eda14cbcSMatt Macy int n = 0; /* optimal size for array part */
202eda14cbcSMatt Macy for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) {
203eda14cbcSMatt Macy if (nums[i] > 0) {
204eda14cbcSMatt Macy a += nums[i];
205eda14cbcSMatt Macy if (a > twotoi/2) { /* more than half elements present? */
206eda14cbcSMatt Macy n = twotoi; /* optimal size (till now) */
207eda14cbcSMatt Macy na = a; /* all elements smaller than n will go to array part */
208eda14cbcSMatt Macy }
209eda14cbcSMatt Macy }
210eda14cbcSMatt Macy if (a == *narray) break; /* all elements already counted */
211eda14cbcSMatt Macy }
212eda14cbcSMatt Macy *narray = n;
213eda14cbcSMatt Macy lua_assert(*narray/2 <= na && na <= *narray);
214eda14cbcSMatt Macy return na;
215eda14cbcSMatt Macy }
216eda14cbcSMatt Macy
217eda14cbcSMatt Macy
countint(const TValue * key,int * nums)218eda14cbcSMatt Macy static int countint (const TValue *key, int *nums) {
219eda14cbcSMatt Macy int k = arrayindex(key);
220eda14cbcSMatt Macy if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */
221eda14cbcSMatt Macy nums[luaO_ceillog2(k)]++; /* count as such */
222eda14cbcSMatt Macy return 1;
223eda14cbcSMatt Macy }
224eda14cbcSMatt Macy else
225eda14cbcSMatt Macy return 0;
226eda14cbcSMatt Macy }
227eda14cbcSMatt Macy
228eda14cbcSMatt Macy
numusearray(const Table * t,int * nums)229eda14cbcSMatt Macy static int numusearray (const Table *t, int *nums) {
230eda14cbcSMatt Macy int lg;
231eda14cbcSMatt Macy int ttlg; /* 2^lg */
232eda14cbcSMatt Macy int ause = 0; /* summation of `nums' */
233eda14cbcSMatt Macy int i = 1; /* count to traverse all array keys */
234eda14cbcSMatt Macy for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */
235eda14cbcSMatt Macy int lc = 0; /* counter */
236eda14cbcSMatt Macy int lim = ttlg;
237eda14cbcSMatt Macy if (lim > t->sizearray) {
238eda14cbcSMatt Macy lim = t->sizearray; /* adjust upper limit */
239eda14cbcSMatt Macy if (i > lim)
240eda14cbcSMatt Macy break; /* no more elements to count */
241eda14cbcSMatt Macy }
242eda14cbcSMatt Macy /* count elements in range (2^(lg-1), 2^lg] */
243eda14cbcSMatt Macy for (; i <= lim; i++) {
244eda14cbcSMatt Macy if (!ttisnil(&t->array[i-1]))
245eda14cbcSMatt Macy lc++;
246eda14cbcSMatt Macy }
247eda14cbcSMatt Macy nums[lg] += lc;
248eda14cbcSMatt Macy ause += lc;
249eda14cbcSMatt Macy }
250eda14cbcSMatt Macy return ause;
251eda14cbcSMatt Macy }
252eda14cbcSMatt Macy
253eda14cbcSMatt Macy
numusehash(const Table * t,int * nums,int * pnasize)254eda14cbcSMatt Macy static int numusehash (const Table *t, int *nums, int *pnasize) {
255eda14cbcSMatt Macy int totaluse = 0; /* total number of elements */
256eda14cbcSMatt Macy int ause = 0; /* summation of `nums' */
257eda14cbcSMatt Macy int i = sizenode(t);
258eda14cbcSMatt Macy while (i--) {
259eda14cbcSMatt Macy Node *n = &t->node[i];
260eda14cbcSMatt Macy if (!ttisnil(gval(n))) {
261eda14cbcSMatt Macy ause += countint(gkey(n), nums);
262eda14cbcSMatt Macy totaluse++;
263eda14cbcSMatt Macy }
264eda14cbcSMatt Macy }
265eda14cbcSMatt Macy *pnasize += ause;
266eda14cbcSMatt Macy return totaluse;
267eda14cbcSMatt Macy }
268eda14cbcSMatt Macy
269eda14cbcSMatt Macy
setarrayvector(lua_State * L,Table * t,int size)270eda14cbcSMatt Macy static void setarrayvector (lua_State *L, Table *t, int size) {
271eda14cbcSMatt Macy int i;
272eda14cbcSMatt Macy luaM_reallocvector(L, t->array, t->sizearray, size, TValue);
273eda14cbcSMatt Macy for (i=t->sizearray; i<size; i++)
274eda14cbcSMatt Macy setnilvalue(&t->array[i]);
275eda14cbcSMatt Macy t->sizearray = size;
276eda14cbcSMatt Macy }
277eda14cbcSMatt Macy
278eda14cbcSMatt Macy
setnodevector(lua_State * L,Table * t,int size)279eda14cbcSMatt Macy static void setnodevector (lua_State *L, Table *t, int size) {
280eda14cbcSMatt Macy int lsize;
281eda14cbcSMatt Macy if (size == 0) { /* no elements to hash part? */
282eda14cbcSMatt Macy t->node = cast(Node *, dummynode); /* use common `dummynode' */
283eda14cbcSMatt Macy lsize = 0;
284eda14cbcSMatt Macy }
285eda14cbcSMatt Macy else {
286eda14cbcSMatt Macy int i;
287eda14cbcSMatt Macy lsize = luaO_ceillog2(size);
288eda14cbcSMatt Macy if (lsize > MAXBITS)
289eda14cbcSMatt Macy luaG_runerror(L, "table overflow");
290eda14cbcSMatt Macy size = twoto(lsize);
291eda14cbcSMatt Macy t->node = luaM_newvector(L, size, Node);
292eda14cbcSMatt Macy for (i=0; i<size; i++) {
293eda14cbcSMatt Macy Node *n = gnode(t, i);
294eda14cbcSMatt Macy gnext(n) = NULL;
295eda14cbcSMatt Macy setnilvalue(gkey(n));
296eda14cbcSMatt Macy setnilvalue(gval(n));
297eda14cbcSMatt Macy }
298eda14cbcSMatt Macy }
299eda14cbcSMatt Macy t->lsizenode = cast_byte(lsize);
300eda14cbcSMatt Macy t->lastfree = gnode(t, size); /* all positions are free */
301eda14cbcSMatt Macy }
302eda14cbcSMatt Macy
303eda14cbcSMatt Macy
luaH_resize(lua_State * L,Table * t,int nasize,int nhsize)304eda14cbcSMatt Macy void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) {
305eda14cbcSMatt Macy int i;
306eda14cbcSMatt Macy int oldasize = t->sizearray;
307eda14cbcSMatt Macy int oldhsize = t->lsizenode;
308eda14cbcSMatt Macy Node *nold = t->node; /* save old hash ... */
309eda14cbcSMatt Macy if (nasize > oldasize) /* array part must grow? */
310eda14cbcSMatt Macy setarrayvector(L, t, nasize);
311eda14cbcSMatt Macy /* create new hash part with appropriate size */
312eda14cbcSMatt Macy setnodevector(L, t, nhsize);
313eda14cbcSMatt Macy if (nasize < oldasize) { /* array part must shrink? */
314eda14cbcSMatt Macy t->sizearray = nasize;
315eda14cbcSMatt Macy /* re-insert elements from vanishing slice */
316eda14cbcSMatt Macy for (i=nasize; i<oldasize; i++) {
317eda14cbcSMatt Macy if (!ttisnil(&t->array[i]))
318eda14cbcSMatt Macy luaH_setint(L, t, i + 1, &t->array[i]);
319eda14cbcSMatt Macy }
320eda14cbcSMatt Macy /* shrink array */
321eda14cbcSMatt Macy luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
322eda14cbcSMatt Macy }
323eda14cbcSMatt Macy /* re-insert elements from hash part */
324eda14cbcSMatt Macy for (i = twoto(oldhsize) - 1; i >= 0; i--) {
325eda14cbcSMatt Macy Node *old = nold+i;
326eda14cbcSMatt Macy if (!ttisnil(gval(old))) {
327eda14cbcSMatt Macy /* doesn't need barrier/invalidate cache, as entry was
328eda14cbcSMatt Macy already present in the table */
329eda14cbcSMatt Macy setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old));
330eda14cbcSMatt Macy }
331eda14cbcSMatt Macy }
332eda14cbcSMatt Macy if (!isdummy(nold))
333eda14cbcSMatt Macy luaM_freearray(L, nold, cast(size_t, twoto(oldhsize))); /* free old array */
334eda14cbcSMatt Macy }
335eda14cbcSMatt Macy
336eda14cbcSMatt Macy
luaH_resizearray(lua_State * L,Table * t,int nasize)337eda14cbcSMatt Macy void luaH_resizearray (lua_State *L, Table *t, int nasize) {
338eda14cbcSMatt Macy int nsize = isdummy(t->node) ? 0 : sizenode(t);
339eda14cbcSMatt Macy luaH_resize(L, t, nasize, nsize);
340eda14cbcSMatt Macy }
341eda14cbcSMatt Macy
342eda14cbcSMatt Macy
rehash(lua_State * L,Table * t,const TValue * ek)343eda14cbcSMatt Macy static void rehash (lua_State *L, Table *t, const TValue *ek) {
344eda14cbcSMatt Macy int nasize, na;
345eda14cbcSMatt Macy int nums[MAXBITS+1]; /* nums[i] = number of keys with 2^(i-1) < k <= 2^i */
346eda14cbcSMatt Macy int i;
347eda14cbcSMatt Macy int totaluse;
348eda14cbcSMatt Macy for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */
349eda14cbcSMatt Macy nasize = numusearray(t, nums); /* count keys in array part */
350eda14cbcSMatt Macy totaluse = nasize; /* all those keys are integer keys */
351eda14cbcSMatt Macy totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */
352eda14cbcSMatt Macy /* count extra key */
353eda14cbcSMatt Macy nasize += countint(ek, nums);
354eda14cbcSMatt Macy totaluse++;
355eda14cbcSMatt Macy /* compute new size for array part */
356eda14cbcSMatt Macy na = computesizes(nums, &nasize);
357eda14cbcSMatt Macy /* resize the table to new computed sizes */
358eda14cbcSMatt Macy luaH_resize(L, t, nasize, totaluse - na);
359eda14cbcSMatt Macy }
360eda14cbcSMatt Macy
361eda14cbcSMatt Macy
362eda14cbcSMatt Macy
363eda14cbcSMatt Macy /*
364eda14cbcSMatt Macy ** }=============================================================
365eda14cbcSMatt Macy */
366eda14cbcSMatt Macy
367eda14cbcSMatt Macy
luaH_new(lua_State * L)368eda14cbcSMatt Macy Table *luaH_new (lua_State *L) {
369eda14cbcSMatt Macy Table *t = &luaC_newobj(L, LUA_TTABLE, sizeof(Table), NULL, 0)->h;
370eda14cbcSMatt Macy t->metatable = NULL;
371eda14cbcSMatt Macy t->flags = cast_byte(~0);
372eda14cbcSMatt Macy t->array = NULL;
373eda14cbcSMatt Macy t->sizearray = 0;
374eda14cbcSMatt Macy setnodevector(L, t, 0);
375eda14cbcSMatt Macy return t;
376eda14cbcSMatt Macy }
377eda14cbcSMatt Macy
378eda14cbcSMatt Macy
luaH_free(lua_State * L,Table * t)379eda14cbcSMatt Macy void luaH_free (lua_State *L, Table *t) {
380eda14cbcSMatt Macy if (!isdummy(t->node))
381eda14cbcSMatt Macy luaM_freearray(L, t->node, cast(size_t, sizenode(t)));
382eda14cbcSMatt Macy luaM_freearray(L, t->array, t->sizearray);
383eda14cbcSMatt Macy luaM_free(L, t);
384eda14cbcSMatt Macy }
385eda14cbcSMatt Macy
386eda14cbcSMatt Macy
getfreepos(Table * t)387eda14cbcSMatt Macy static Node *getfreepos (Table *t) {
388eda14cbcSMatt Macy while (t->lastfree > t->node) {
389eda14cbcSMatt Macy t->lastfree--;
390eda14cbcSMatt Macy if (ttisnil(gkey(t->lastfree)))
391eda14cbcSMatt Macy return t->lastfree;
392eda14cbcSMatt Macy }
393eda14cbcSMatt Macy return NULL; /* could not find a free place */
394eda14cbcSMatt Macy }
395eda14cbcSMatt Macy
396eda14cbcSMatt Macy
397eda14cbcSMatt Macy
398eda14cbcSMatt Macy /*
399eda14cbcSMatt Macy ** inserts a new key into a hash table; first, check whether key's main
400eda14cbcSMatt Macy ** position is free. If not, check whether colliding node is in its main
401eda14cbcSMatt Macy ** position or not: if it is not, move colliding node to an empty place and
402eda14cbcSMatt Macy ** put new key in its main position; otherwise (colliding node is in its main
403eda14cbcSMatt Macy ** position), new key goes to an empty position.
404eda14cbcSMatt Macy */
luaH_newkey(lua_State * L,Table * t,const TValue * key)405eda14cbcSMatt Macy TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
406eda14cbcSMatt Macy Node *mp;
407eda14cbcSMatt Macy if (ttisnil(key)) luaG_runerror(L, "table index is nil");
408eda14cbcSMatt Macy #if defined LUA_HAS_FLOAT_NUMBERS
409eda14cbcSMatt Macy else if (ttisnumber(key) && luai_numisnan(L, nvalue(key)))
410eda14cbcSMatt Macy luaG_runerror(L, "table index is NaN");
411eda14cbcSMatt Macy #endif
412eda14cbcSMatt Macy mp = mainposition(t, key);
413eda14cbcSMatt Macy if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */
414eda14cbcSMatt Macy Node *othern;
415eda14cbcSMatt Macy Node *n = getfreepos(t); /* get a free place */
416eda14cbcSMatt Macy if (n == NULL) { /* cannot find a free place? */
417eda14cbcSMatt Macy rehash(L, t, key); /* grow table */
418eda14cbcSMatt Macy /* whatever called 'newkey' take care of TM cache and GC barrier */
419eda14cbcSMatt Macy return luaH_set(L, t, key); /* insert key into grown table */
420eda14cbcSMatt Macy }
421eda14cbcSMatt Macy lua_assert(!isdummy(n));
422eda14cbcSMatt Macy othern = mainposition(t, gkey(mp));
423eda14cbcSMatt Macy if (othern != mp) { /* is colliding node out of its main position? */
424eda14cbcSMatt Macy /* yes; move colliding node into free position */
425eda14cbcSMatt Macy while (gnext(othern) != mp) othern = gnext(othern); /* find previous */
426eda14cbcSMatt Macy gnext(othern) = n; /* redo the chain with `n' in place of `mp' */
427eda14cbcSMatt Macy *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
428eda14cbcSMatt Macy gnext(mp) = NULL; /* now `mp' is free */
429eda14cbcSMatt Macy setnilvalue(gval(mp));
430eda14cbcSMatt Macy }
431eda14cbcSMatt Macy else { /* colliding node is in its own main position */
432eda14cbcSMatt Macy /* new node will go into free position */
433eda14cbcSMatt Macy gnext(n) = gnext(mp); /* chain new position */
434eda14cbcSMatt Macy gnext(mp) = n;
435eda14cbcSMatt Macy mp = n;
436eda14cbcSMatt Macy }
437eda14cbcSMatt Macy }
438eda14cbcSMatt Macy setobj2t(L, gkey(mp), key);
439eda14cbcSMatt Macy luaC_barrierback(L, obj2gco(t), key);
440eda14cbcSMatt Macy lua_assert(ttisnil(gval(mp)));
441eda14cbcSMatt Macy return gval(mp);
442eda14cbcSMatt Macy }
443eda14cbcSMatt Macy
444eda14cbcSMatt Macy
445eda14cbcSMatt Macy /*
446eda14cbcSMatt Macy ** search function for integers
447eda14cbcSMatt Macy */
luaH_getint(Table * t,int key)448eda14cbcSMatt Macy const TValue *luaH_getint (Table *t, int key) {
449eda14cbcSMatt Macy /* (1 <= key && key <= t->sizearray) */
450eda14cbcSMatt Macy if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray))
451eda14cbcSMatt Macy return &t->array[key-1];
452eda14cbcSMatt Macy else {
453eda14cbcSMatt Macy lua_Number nk = cast_num(key);
454eda14cbcSMatt Macy Node *n = hashnum(t, nk);
455eda14cbcSMatt Macy do { /* check whether `key' is somewhere in the chain */
456eda14cbcSMatt Macy if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk))
457eda14cbcSMatt Macy return gval(n); /* that's it */
458eda14cbcSMatt Macy else n = gnext(n);
459eda14cbcSMatt Macy } while (n);
460eda14cbcSMatt Macy return luaO_nilobject;
461eda14cbcSMatt Macy }
462eda14cbcSMatt Macy }
463eda14cbcSMatt Macy
464eda14cbcSMatt Macy
465eda14cbcSMatt Macy /*
466eda14cbcSMatt Macy ** search function for short strings
467eda14cbcSMatt Macy */
luaH_getstr(Table * t,TString * key)468eda14cbcSMatt Macy const TValue *luaH_getstr (Table *t, TString *key) {
469eda14cbcSMatt Macy Node *n = hashstr(t, key);
470eda14cbcSMatt Macy lua_assert(key->tsv.tt == LUA_TSHRSTR);
471eda14cbcSMatt Macy do { /* check whether `key' is somewhere in the chain */
472eda14cbcSMatt Macy if (ttisshrstring(gkey(n)) && eqshrstr(rawtsvalue(gkey(n)), key))
473eda14cbcSMatt Macy return gval(n); /* that's it */
474eda14cbcSMatt Macy else n = gnext(n);
475eda14cbcSMatt Macy } while (n);
476eda14cbcSMatt Macy return luaO_nilobject;
477eda14cbcSMatt Macy }
478eda14cbcSMatt Macy
479eda14cbcSMatt Macy
480eda14cbcSMatt Macy /*
481eda14cbcSMatt Macy ** main search function
482eda14cbcSMatt Macy */
luaH_get(Table * t,const TValue * key)483eda14cbcSMatt Macy const TValue *luaH_get (Table *t, const TValue *key) {
484eda14cbcSMatt Macy switch (ttype(key)) {
485eda14cbcSMatt Macy case LUA_TSHRSTR: return luaH_getstr(t, rawtsvalue(key));
486eda14cbcSMatt Macy case LUA_TNIL: return luaO_nilobject;
487eda14cbcSMatt Macy case LUA_TNUMBER: {
488eda14cbcSMatt Macy int k;
489eda14cbcSMatt Macy lua_Number n = nvalue(key);
490eda14cbcSMatt Macy lua_number2int(k, n);
491eda14cbcSMatt Macy if (luai_numeq(cast_num(k), n)) /* index is int? */
492eda14cbcSMatt Macy return luaH_getint(t, k); /* use specialized version */
493eda14cbcSMatt Macy /* else go through */
494eda14cbcSMatt Macy }
495c03c5b1cSMartin Matuska zfs_fallthrough;
496eda14cbcSMatt Macy default: {
497eda14cbcSMatt Macy Node *n = mainposition(t, key);
498eda14cbcSMatt Macy do { /* check whether `key' is somewhere in the chain */
499eda14cbcSMatt Macy if (luaV_rawequalobj(gkey(n), key))
500eda14cbcSMatt Macy return gval(n); /* that's it */
501eda14cbcSMatt Macy else n = gnext(n);
502eda14cbcSMatt Macy } while (n);
503eda14cbcSMatt Macy return luaO_nilobject;
504eda14cbcSMatt Macy }
505eda14cbcSMatt Macy }
506eda14cbcSMatt Macy }
507eda14cbcSMatt Macy
508eda14cbcSMatt Macy
509eda14cbcSMatt Macy /*
510eda14cbcSMatt Macy ** beware: when using this function you probably need to check a GC
511eda14cbcSMatt Macy ** barrier and invalidate the TM cache.
512eda14cbcSMatt Macy */
luaH_set(lua_State * L,Table * t,const TValue * key)513eda14cbcSMatt Macy TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
514eda14cbcSMatt Macy const TValue *p = luaH_get(t, key);
515eda14cbcSMatt Macy if (p != luaO_nilobject)
516eda14cbcSMatt Macy return cast(TValue *, p);
517eda14cbcSMatt Macy else return luaH_newkey(L, t, key);
518eda14cbcSMatt Macy }
519eda14cbcSMatt Macy
520eda14cbcSMatt Macy
luaH_setint(lua_State * L,Table * t,int key,TValue * value)521eda14cbcSMatt Macy void luaH_setint (lua_State *L, Table *t, int key, TValue *value) {
522eda14cbcSMatt Macy const TValue *p = luaH_getint(t, key);
523eda14cbcSMatt Macy TValue *cell;
524eda14cbcSMatt Macy if (p != luaO_nilobject)
525eda14cbcSMatt Macy cell = cast(TValue *, p);
526eda14cbcSMatt Macy else {
527eda14cbcSMatt Macy TValue k;
528eda14cbcSMatt Macy setnvalue(&k, cast_num(key));
529eda14cbcSMatt Macy cell = luaH_newkey(L, t, &k);
530eda14cbcSMatt Macy }
531eda14cbcSMatt Macy setobj2t(L, cell, value);
532eda14cbcSMatt Macy }
533eda14cbcSMatt Macy
534eda14cbcSMatt Macy
unbound_search(Table * t,unsigned int j)535eda14cbcSMatt Macy static int unbound_search (Table *t, unsigned int j) {
536eda14cbcSMatt Macy unsigned int i = j; /* i is zero or a present index */
537eda14cbcSMatt Macy j++;
538eda14cbcSMatt Macy /* find `i' and `j' such that i is present and j is not */
539eda14cbcSMatt Macy while (!ttisnil(luaH_getint(t, j))) {
540eda14cbcSMatt Macy i = j;
541eda14cbcSMatt Macy j *= 2;
542eda14cbcSMatt Macy if (j > cast(unsigned int, MAX_INT)) { /* overflow? */
543eda14cbcSMatt Macy /* table was built with bad purposes: resort to linear search */
544eda14cbcSMatt Macy i = 1;
545eda14cbcSMatt Macy while (!ttisnil(luaH_getint(t, i))) i++;
546eda14cbcSMatt Macy return i - 1;
547eda14cbcSMatt Macy }
548eda14cbcSMatt Macy }
549eda14cbcSMatt Macy /* now do a binary search between them */
550eda14cbcSMatt Macy while (j - i > 1) {
551eda14cbcSMatt Macy unsigned int m = (i+j)/2;
552eda14cbcSMatt Macy if (ttisnil(luaH_getint(t, m))) j = m;
553eda14cbcSMatt Macy else i = m;
554eda14cbcSMatt Macy }
555eda14cbcSMatt Macy return i;
556eda14cbcSMatt Macy }
557eda14cbcSMatt Macy
558eda14cbcSMatt Macy
559eda14cbcSMatt Macy /*
560eda14cbcSMatt Macy ** Try to find a boundary in table `t'. A `boundary' is an integer index
561eda14cbcSMatt Macy ** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
562eda14cbcSMatt Macy */
luaH_getn(Table * t)563eda14cbcSMatt Macy int luaH_getn (Table *t) {
564eda14cbcSMatt Macy unsigned int j = t->sizearray;
565eda14cbcSMatt Macy if (j > 0 && ttisnil(&t->array[j - 1])) {
566eda14cbcSMatt Macy /* there is a boundary in the array part: (binary) search for it */
567eda14cbcSMatt Macy unsigned int i = 0;
568eda14cbcSMatt Macy while (j - i > 1) {
569eda14cbcSMatt Macy unsigned int m = (i+j)/2;
570eda14cbcSMatt Macy if (ttisnil(&t->array[m - 1])) j = m;
571eda14cbcSMatt Macy else i = m;
572eda14cbcSMatt Macy }
573eda14cbcSMatt Macy return i;
574eda14cbcSMatt Macy }
575eda14cbcSMatt Macy /* else must find a boundary in hash part */
576eda14cbcSMatt Macy else if (isdummy(t->node)) /* hash part is empty? */
577eda14cbcSMatt Macy return j; /* that is easy... */
578eda14cbcSMatt Macy else return unbound_search(t, j);
579eda14cbcSMatt Macy }
580eda14cbcSMatt Macy
581eda14cbcSMatt Macy
582eda14cbcSMatt Macy
583eda14cbcSMatt Macy #if defined(LUA_DEBUG)
584eda14cbcSMatt Macy
luaH_mainposition(const Table * t,const TValue * key)585eda14cbcSMatt Macy Node *luaH_mainposition (const Table *t, const TValue *key) {
586eda14cbcSMatt Macy return mainposition(t, key);
587eda14cbcSMatt Macy }
588eda14cbcSMatt Macy
luaH_isdummy(Node * n)589eda14cbcSMatt Macy int luaH_isdummy (Node *n) { return isdummy(n); }
590eda14cbcSMatt Macy
591eda14cbcSMatt Macy #endif
592