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