xref: /netbsd-src/external/mit/lua/dist/src/ltable.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: ltable.c,v 1.10 2018/07/01 10:08:38 mbalmer Exp $	*/
2 
3 /*
4 ** Id: ltable.c,v 2.118 2016/11/07 12:38:35 roberto Exp
5 ** Lua tables (hash)
6 ** See Copyright Notice in lua.h
7 */
8 
9 #define ltable_c
10 #define LUA_CORE
11 
12 #include "lprefix.h"
13 
14 
15 /*
16 ** Implementation of tables (aka arrays, objects, or hash tables).
17 ** Tables keep its elements in two parts: an array part and a hash part.
18 ** Non-negative integer keys are all candidates to be kept in the array
19 ** part. The actual size of the array is the largest 'n' such that
20 ** more than half the slots between 1 and n are in use.
21 ** Hash uses a mix of chained scatter table with Brent's variation.
22 ** A main invariant of these tables is that, if an element is not
23 ** in its main position (i.e. the 'original' position that its hash gives
24 ** to it), then the colliding element is in its own main position.
25 ** Hence even when the load factor reaches 100%, performance remains good.
26 */
27 
28 #ifndef _KERNEL
29 #include <math.h>
30 #include <limits.h>
31 #endif /* _KERNEL */
32 
33 #include "lua.h"
34 
35 #include "ldebug.h"
36 #include "ldo.h"
37 #include "lgc.h"
38 #include "lmem.h"
39 #include "lobject.h"
40 #include "lstate.h"
41 #include "lstring.h"
42 #include "ltable.h"
43 #include "lvm.h"
44 
45 
46 /*
47 ** Maximum size of array part (MAXASIZE) is 2^MAXABITS. MAXABITS is
48 ** the largest integer such that MAXASIZE fits in an unsigned int.
49 */
50 #define MAXABITS	cast_int(sizeof(int) * CHAR_BIT - 1)
51 #define MAXASIZE	(1u << MAXABITS)
52 
53 /*
54 ** Maximum size of hash part is 2^MAXHBITS. MAXHBITS is the largest
55 ** integer such that 2^MAXHBITS fits in a signed int. (Note that the
56 ** maximum number of elements in a table, 2^MAXABITS + 2^MAXHBITS, still
57 ** fits comfortably in an unsigned int.)
58 */
59 #define MAXHBITS	(MAXABITS - 1)
60 
61 
62 #define hashpow2(t,n)		(gnode(t, lmod((n), sizenode(t))))
63 
64 #define hashstr(t,str)		hashpow2(t, (str)->hash)
65 #define hashboolean(t,p)	hashpow2(t, p)
66 #define hashint(t,i)		hashpow2(t, i)
67 
68 
69 /*
70 ** for some types, it is better to avoid modulus by power of 2, as
71 ** they tend to have many 2 factors.
72 */
73 #define hashmod(t,n)	(gnode(t, ((n) % ((sizenode(t)-1)|1))))
74 
75 
76 #define hashpointer(t,p)	hashmod(t, point2uint(p))
77 
78 
79 #define dummynode		(&dummynode_)
80 
81 static const Node dummynode_ = {
82   {NILCONSTANT},  /* value */
83   {{NILCONSTANT, 0}}  /* key */
84 };
85 
86 
87 #ifndef _KERNEL
88 /*
89 ** Hash for floating-point numbers.
90 ** The main computation should be just
91 **     n = frexp(n, &i); return (n * INT_MAX) + i
92 ** but there are some numerical subtleties.
93 ** In a two-complement representation, INT_MAX does not has an exact
94 ** representation as a float, but INT_MIN does; because the absolute
95 ** value of 'frexp' is smaller than 1 (unless 'n' is inf/NaN), the
96 ** absolute value of the product 'frexp * -INT_MIN' is smaller or equal
97 ** to INT_MAX. Next, the use of 'unsigned int' avoids overflows when
98 ** adding 'i'; the use of '~u' (instead of '-u') avoids problems with
99 ** INT_MIN.
100 */
101 #if !defined(l_hashfloat)
102 static int l_hashfloat (lua_Number n) {
103   int i;
104   lua_Integer ni;
105   n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN);
106   if (!lua_numbertointeger(n, &ni)) {  /* is 'n' inf/-inf/NaN? */
107     lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL));
108     return 0;
109   }
110   else {  /* normal case */
111     unsigned int u = cast(unsigned int, i) + cast(unsigned int, ni);
112     return cast_int(u <= cast(unsigned int, INT_MAX) ? u : ~u);
113   }
114 }
115 #endif
116 #endif /* _KERNEL */
117 
118 
119 /*
120 ** returns the 'main' position of an element in a table (that is, the index
121 ** of its hash value)
122 */
123 static Node *mainposition (const Table *t, const TValue *key) {
124   switch (ttype(key)) {
125     case LUA_TNUMINT:
126       return hashint(t, ivalue(key));
127 #ifndef _KERNEL
128     case LUA_TNUMFLT:
129       return hashmod(t, l_hashfloat(fltvalue(key)));
130 #endif /* _KERNEL */
131     case LUA_TSHRSTR:
132       return hashstr(t, tsvalue(key));
133     case LUA_TLNGSTR:
134       return hashpow2(t, luaS_hashlongstr(tsvalue(key)));
135     case LUA_TBOOLEAN:
136       return hashboolean(t, bvalue(key));
137     case LUA_TLIGHTUSERDATA:
138       return hashpointer(t, pvalue(key));
139     case LUA_TLCF:
140       return hashpointer(t, fvalue(key));
141     default:
142       lua_assert(!ttisdeadkey(key));
143       return hashpointer(t, gcvalue(key));
144   }
145 }
146 
147 
148 /*
149 ** returns the index for 'key' if 'key' is an appropriate key to live in
150 ** the array part of the table, 0 otherwise.
151 */
152 static unsigned int arrayindex (const TValue *key) {
153   if (ttisinteger(key)) {
154     lua_Integer k = ivalue(key);
155     if (0 < k && (lua_Unsigned)k <= MAXASIZE)
156       return cast(unsigned int, k);  /* 'key' is an appropriate array index */
157   }
158   return 0;  /* 'key' did not match some condition */
159 }
160 
161 
162 /*
163 ** returns the index of a 'key' for table traversals. First goes all
164 ** elements in the array part, then elements in the hash part. The
165 ** beginning of a traversal is signaled by 0.
166 */
167 static unsigned int findindex (lua_State *L, Table *t, StkId key) {
168   unsigned int i;
169   if (ttisnil(key)) return 0;  /* first iteration */
170   i = arrayindex(key);
171   if (i != 0 && i <= t->sizearray)  /* is 'key' inside array part? */
172     return i;  /* yes; that's the index */
173   else {
174     int nx;
175     Node *n = mainposition(t, key);
176     for (;;) {  /* check whether 'key' is somewhere in the chain */
177       /* key may be dead already, but it is ok to use it in 'next' */
178       if (luaV_rawequalobj(gkey(n), key) ||
179             (ttisdeadkey(gkey(n)) && iscollectable(key) &&
180              deadvalue(gkey(n)) == gcvalue(key))) {
181         i = cast_int(n - gnode(t, 0));  /* key index in hash table */
182         /* hash elements are numbered after array ones */
183         return (i + 1) + t->sizearray;
184       }
185       nx = gnext(n);
186       if (nx == 0)
187         luaG_runerror(L, "invalid key to 'next'");  /* key not found */
188       else n += nx;
189     }
190   }
191 }
192 
193 
194 int luaH_next (lua_State *L, Table *t, StkId key) {
195   unsigned int i = findindex(L, t, key);  /* find original element */
196   for (; i < t->sizearray; i++) {  /* try first array part */
197     if (!ttisnil(&t->array[i])) {  /* a non-nil value? */
198       setivalue(key, i + 1);
199       setobj2s(L, key+1, &t->array[i]);
200       return 1;
201     }
202   }
203   for (i -= t->sizearray; cast_int(i) < sizenode(t); i++) {  /* hash part */
204     if (!ttisnil(gval(gnode(t, i)))) {  /* a non-nil value? */
205       setobj2s(L, key, gkey(gnode(t, i)));
206       setobj2s(L, key+1, gval(gnode(t, i)));
207       return 1;
208     }
209   }
210   return 0;  /* no more elements */
211 }
212 
213 
214 /*
215 ** {=============================================================
216 ** Rehash
217 ** ==============================================================
218 */
219 
220 /*
221 ** Compute the optimal size for the array part of table 't'. 'nums' is a
222 ** "count array" where 'nums[i]' is the number of integers in the table
223 ** between 2^(i - 1) + 1 and 2^i. 'pna' enters with the total number of
224 ** integer keys in the table and leaves with the number of keys that
225 ** will go to the array part; return the optimal size.
226 */
227 static unsigned int computesizes (unsigned int nums[], unsigned int *pna) {
228   int i;
229   unsigned int twotoi;  /* 2^i (candidate for optimal size) */
230   unsigned int a = 0;  /* number of elements smaller than 2^i */
231   unsigned int na = 0;  /* number of elements to go to array part */
232   unsigned int optimal = 0;  /* optimal size for array part */
233   /* loop while keys can fill more than half of total size */
234   for (i = 0, twotoi = 1; *pna > twotoi / 2; i++, twotoi *= 2) {
235     if (nums[i] > 0) {
236       a += nums[i];
237       if (a > twotoi/2) {  /* more than half elements present? */
238         optimal = twotoi;  /* optimal size (till now) */
239         na = a;  /* all elements up to 'optimal' will go to array part */
240       }
241     }
242   }
243   lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal);
244   *pna = na;
245   return optimal;
246 }
247 
248 
249 static int countint (const TValue *key, unsigned int *nums) {
250   unsigned int k = arrayindex(key);
251   if (k != 0) {  /* is 'key' an appropriate array index? */
252     nums[luaO_ceillog2(k)]++;  /* count as such */
253     return 1;
254   }
255   else
256     return 0;
257 }
258 
259 
260 /*
261 ** Count keys in array part of table 't': Fill 'nums[i]' with
262 ** number of keys that will go into corresponding slice and return
263 ** total number of non-nil keys.
264 */
265 static unsigned int numusearray (const Table *t, unsigned int *nums) {
266   int lg;
267   unsigned int ttlg;  /* 2^lg */
268   unsigned int ause = 0;  /* summation of 'nums' */
269   unsigned int i = 1;  /* count to traverse all array keys */
270   /* traverse each slice */
271   for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) {
272     unsigned int lc = 0;  /* counter */
273     unsigned int lim = ttlg;
274     if (lim > t->sizearray) {
275       lim = t->sizearray;  /* adjust upper limit */
276       if (i > lim)
277         break;  /* no more elements to count */
278     }
279     /* count elements in range (2^(lg - 1), 2^lg] */
280     for (; i <= lim; i++) {
281       if (!ttisnil(&t->array[i-1]))
282         lc++;
283     }
284     nums[lg] += lc;
285     ause += lc;
286   }
287   return ause;
288 }
289 
290 
291 static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) {
292   int totaluse = 0;  /* total number of elements */
293   int ause = 0;  /* elements added to 'nums' (can go to array part) */
294   int i = sizenode(t);
295   while (i--) {
296     Node *n = &t->node[i];
297     if (!ttisnil(gval(n))) {
298       ause += countint(gkey(n), nums);
299       totaluse++;
300     }
301   }
302   *pna += ause;
303   return totaluse;
304 }
305 
306 
307 static void setarrayvector (lua_State *L, Table *t, unsigned int size) {
308   unsigned int i;
309   luaM_reallocvector(L, t->array, t->sizearray, size, TValue);
310   for (i=t->sizearray; i<size; i++)
311      setnilvalue(&t->array[i]);
312   t->sizearray = size;
313 }
314 
315 
316 static void setnodevector (lua_State *L, Table *t, unsigned int size) {
317   if (size == 0) {  /* no elements to hash part? */
318     t->node = cast(Node *, dummynode);  /* use common 'dummynode' */
319     t->lsizenode = 0;
320     t->lastfree = NULL;  /* signal that it is using dummy node */
321   }
322   else {
323     int i;
324     int lsize = luaO_ceillog2(size);
325     if (lsize > MAXHBITS)
326       luaG_runerror(L, "table overflow");
327     size = twoto(lsize);
328     t->node = luaM_newvector(L, size, Node);
329     for (i = 0; i < (int)size; i++) {
330       Node *n = gnode(t, i);
331       gnext(n) = 0;
332       setnilvalue(wgkey(n));
333       setnilvalue(gval(n));
334     }
335     t->lsizenode = cast_byte(lsize);
336     t->lastfree = gnode(t, size);  /* all positions are free */
337   }
338 }
339 
340 
341 typedef struct {
342   Table *t;
343   unsigned int nhsize;
344 } AuxsetnodeT;
345 
346 
347 static void auxsetnode (lua_State *L, void *ud) {
348   AuxsetnodeT *asn = cast(AuxsetnodeT *, ud);
349   setnodevector(L, asn->t, asn->nhsize);
350 }
351 
352 
353 void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
354                                           unsigned int nhsize) {
355   unsigned int i;
356   int j;
357   AuxsetnodeT asn;
358   unsigned int oldasize = t->sizearray;
359   int oldhsize = allocsizenode(t);
360   Node *nold = t->node;  /* save old hash ... */
361   if (nasize > oldasize)  /* array part must grow? */
362     setarrayvector(L, t, nasize);
363   /* create new hash part with appropriate size */
364   asn.t = t; asn.nhsize = nhsize;
365   if (luaD_rawrunprotected(L, auxsetnode, &asn) != LUA_OK) {  /* mem. error? */
366     setarrayvector(L, t, oldasize);  /* array back to its original size */
367     luaD_throw(L, LUA_ERRMEM);  /* rethrow memory error */
368   }
369   if (nasize < oldasize) {  /* array part must shrink? */
370     t->sizearray = nasize;
371     /* re-insert elements from vanishing slice */
372     for (i=nasize; i<oldasize; i++) {
373       if (!ttisnil(&t->array[i]))
374         luaH_setint(L, t, i + 1, &t->array[i]);
375     }
376     /* shrink array */
377     luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
378   }
379   /* re-insert elements from hash part */
380   for (j = oldhsize - 1; j >= 0; j--) {
381     Node *old = nold + j;
382     if (!ttisnil(gval(old))) {
383       /* doesn't need barrier/invalidate cache, as entry was
384          already present in the table */
385       setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old));
386     }
387   }
388   if (oldhsize > 0)  /* not the dummy node? */
389     luaM_freearray(L, nold, cast(size_t, oldhsize)); /* free old hash */
390 }
391 
392 
393 void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) {
394   int nsize = allocsizenode(t);
395   luaH_resize(L, t, nasize, nsize);
396 }
397 
398 /*
399 ** nums[i] = number of keys 'k' where 2^(i - 1) < k <= 2^i
400 */
401 static void rehash (lua_State *L, Table *t, const TValue *ek) {
402   unsigned int asize;  /* optimal size for array part */
403   unsigned int na;  /* number of keys in the array part */
404   unsigned int nums[MAXABITS + 1];
405   int i;
406   int totaluse;
407   for (i = 0; i <= MAXABITS; i++) nums[i] = 0;  /* reset counts */
408   na = numusearray(t, nums);  /* count keys in array part */
409   totaluse = na;  /* all those keys are integer keys */
410   totaluse += numusehash(t, nums, &na);  /* count keys in hash part */
411   /* count extra key */
412   na += countint(ek, nums);
413   totaluse++;
414   /* compute new size for array part */
415   asize = computesizes(nums, &na);
416   /* resize the table to new computed sizes */
417   luaH_resize(L, t, asize, totaluse - na);
418 }
419 
420 
421 
422 /*
423 ** }=============================================================
424 */
425 
426 
427 Table *luaH_new (lua_State *L) {
428   GCObject *o = luaC_newobj(L, LUA_TTABLE, sizeof(Table));
429   Table *t = gco2t(o);
430   t->metatable = NULL;
431   t->flags = cast_byte(~0);
432   t->array = NULL;
433   t->sizearray = 0;
434   setnodevector(L, t, 0);
435   return t;
436 }
437 
438 
439 void luaH_free (lua_State *L, Table *t) {
440   if (!isdummy(t))
441     luaM_freearray(L, t->node, cast(size_t, sizenode(t)));
442   luaM_freearray(L, t->array, t->sizearray);
443   luaM_free(L, t);
444 }
445 
446 
447 static Node *getfreepos (Table *t) {
448   if (!isdummy(t)) {
449     while (t->lastfree > t->node) {
450       t->lastfree--;
451       if (ttisnil(gkey(t->lastfree)))
452         return t->lastfree;
453     }
454   }
455   return NULL;  /* could not find a free place */
456 }
457 
458 
459 
460 /*
461 ** inserts a new key into a hash table; first, check whether key's main
462 ** position is free. If not, check whether colliding node is in its main
463 ** position or not: if it is not, move colliding node to an empty place and
464 ** put new key in its main position; otherwise (colliding node is in its main
465 ** position), new key goes to an empty position.
466 */
467 TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
468   Node *mp;
469 #ifndef _KERNEL
470   TValue aux;
471 #endif /* _KERNEL */
472   if (ttisnil(key)) luaG_runerror(L, "table index is nil");
473 #ifndef _KERNEL
474   else if (ttisfloat(key)) {
475     lua_Integer k;
476     if (luaV_tointeger(key, &k, 0)) {  /* does index fit in an integer? */
477       setivalue(&aux, k);
478       key = &aux;  /* insert it as an integer */
479     }
480     else if (luai_numisnan(fltvalue(key)))
481       luaG_runerror(L, "table index is NaN");
482   }
483 #endif /* _KERNEL */
484   mp = mainposition(t, key);
485   if (!ttisnil(gval(mp)) || isdummy(t)) {  /* main position is taken? */
486     Node *othern;
487     Node *f = getfreepos(t);  /* get a free place */
488     if (f == NULL) {  /* cannot find a free place? */
489       rehash(L, t, key);  /* grow table */
490       /* whatever called 'newkey' takes care of TM cache */
491       return luaH_set(L, t, key);  /* insert key into grown table */
492     }
493     lua_assert(!isdummy(t));
494     othern = mainposition(t, gkey(mp));
495     if (othern != mp) {  /* is colliding node out of its main position? */
496       /* yes; move colliding node into free position */
497       while (othern + gnext(othern) != mp)  /* find previous */
498         othern += gnext(othern);
499       gnext(othern) = cast_int(f - othern);  /* rechain to point to 'f' */
500       *f = *mp;  /* copy colliding node into free pos. (mp->next also goes) */
501       if (gnext(mp) != 0) {
502         gnext(f) += cast_int(mp - f);  /* correct 'next' */
503         gnext(mp) = 0;  /* now 'mp' is free */
504       }
505       setnilvalue(gval(mp));
506     }
507     else {  /* colliding node is in its own main position */
508       /* new node will go into free position */
509       if (gnext(mp) != 0)
510         gnext(f) = cast_int((mp + gnext(mp)) - f);  /* chain new position */
511       else lua_assert(gnext(f) == 0);
512       gnext(mp) = cast_int(f - mp);
513       mp = f;
514     }
515   }
516   setnodekey(L, &mp->i_key, key);
517   luaC_barrierback(L, t, key);
518   lua_assert(ttisnil(gval(mp)));
519   return gval(mp);
520 }
521 
522 
523 /*
524 ** search function for integers
525 */
526 const TValue *luaH_getint (Table *t, lua_Integer key) {
527   /* (1 <= key && key <= t->sizearray) */
528   if (l_castS2U(key) - 1 < t->sizearray)
529     return &t->array[key - 1];
530   else {
531     Node *n = hashint(t, key);
532     for (;;) {  /* check whether 'key' is somewhere in the chain */
533       if (ttisinteger(gkey(n)) && ivalue(gkey(n)) == key)
534         return gval(n);  /* that's it */
535       else {
536         int nx = gnext(n);
537         if (nx == 0) break;
538         n += nx;
539       }
540     }
541     return luaO_nilobject;
542   }
543 }
544 
545 
546 /*
547 ** search function for short strings
548 */
549 const TValue *luaH_getshortstr (Table *t, TString *key) {
550   Node *n = hashstr(t, key);
551   lua_assert(key->tt == LUA_TSHRSTR);
552   for (;;) {  /* check whether 'key' is somewhere in the chain */
553     const TValue *k = gkey(n);
554     if (ttisshrstring(k) && eqshrstr(tsvalue(k), key))
555       return gval(n);  /* that's it */
556     else {
557       int nx = gnext(n);
558       if (nx == 0)
559         return luaO_nilobject;  /* not found */
560       n += nx;
561     }
562   }
563 }
564 
565 
566 /*
567 ** "Generic" get version. (Not that generic: not valid for integers,
568 ** which may be in array part, nor for floats with integral values.)
569 */
570 static const TValue *getgeneric (Table *t, const TValue *key) {
571   Node *n = mainposition(t, key);
572   for (;;) {  /* check whether 'key' is somewhere in the chain */
573     if (luaV_rawequalobj(gkey(n), key))
574       return gval(n);  /* that's it */
575     else {
576       int nx = gnext(n);
577       if (nx == 0)
578         return luaO_nilobject;  /* not found */
579       n += nx;
580     }
581   }
582 }
583 
584 
585 const TValue *luaH_getstr (Table *t, TString *key) {
586   if (key->tt == LUA_TSHRSTR)
587     return luaH_getshortstr(t, key);
588   else {  /* for long strings, use generic case */
589     TValue ko;
590     setsvalue(cast(lua_State *, NULL), &ko, key);
591     return getgeneric(t, &ko);
592   }
593 }
594 
595 
596 /*
597 ** main search function
598 */
599 const TValue *luaH_get (Table *t, const TValue *key) {
600   switch (ttype(key)) {
601     case LUA_TSHRSTR: return luaH_getshortstr(t, tsvalue(key));
602     case LUA_TNUMINT: return luaH_getint(t, ivalue(key));
603     case LUA_TNIL: return luaO_nilobject;
604 #ifndef _KERNEL
605     case LUA_TNUMFLT: {
606       lua_Integer k;
607       if (luaV_tointeger(key, &k, 0)) /* index is int? */
608         return luaH_getint(t, k);  /* use specialized version */
609       /* else... */
610     }  /* FALLTHROUGH */
611 #endif /* _KERNEL */
612     default:
613       return getgeneric(t, key);
614   }
615 }
616 
617 
618 /*
619 ** beware: when using this function you probably need to check a GC
620 ** barrier and invalidate the TM cache.
621 */
622 TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
623   const TValue *p = luaH_get(t, key);
624   if (p != luaO_nilobject)
625     return cast(TValue *, p);
626   else return luaH_newkey(L, t, key);
627 }
628 
629 
630 void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) {
631   const TValue *p = luaH_getint(t, key);
632   TValue *cell;
633   if (p != luaO_nilobject)
634     cell = cast(TValue *, p);
635   else {
636     TValue k;
637     setivalue(&k, key);
638     cell = luaH_newkey(L, t, &k);
639   }
640   setobj2t(L, cell, value);
641 }
642 
643 
644 static int unbound_search (Table *t, unsigned int j) {
645   unsigned int i = j;  /* i is zero or a present index */
646   j++;
647   /* find 'i' and 'j' such that i is present and j is not */
648   while (!ttisnil(luaH_getint(t, j))) {
649     i = j;
650     if (j > cast(unsigned int, MAX_INT)/2) {  /* overflow? */
651       /* table was built with bad purposes: resort to linear search */
652       i = 1;
653       while (!ttisnil(luaH_getint(t, i))) i++;
654       return i - 1;
655     }
656     j *= 2;
657   }
658   /* now do a binary search between them */
659   while (j - i > 1) {
660     unsigned int m = (i+j)/2;
661     if (ttisnil(luaH_getint(t, m))) j = m;
662     else i = m;
663   }
664   return i;
665 }
666 
667 
668 /*
669 ** Try to find a boundary in table 't'. A 'boundary' is an integer index
670 ** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
671 */
672 int luaH_getn (Table *t) {
673   unsigned int j = t->sizearray;
674   if (j > 0 && ttisnil(&t->array[j - 1])) {
675     /* there is a boundary in the array part: (binary) search for it */
676     unsigned int i = 0;
677     while (j - i > 1) {
678       unsigned int m = (i+j)/2;
679       if (ttisnil(&t->array[m - 1])) j = m;
680       else i = m;
681     }
682     return i;
683   }
684   /* else must find a boundary in hash part */
685   else if (isdummy(t))  /* hash part is empty? */
686     return j;  /* that is easy... */
687   else return unbound_search(t, j);
688 }
689 
690 
691 
692 #if defined(LUA_DEBUG)
693 
694 Node *luaH_mainposition (const Table *t, const TValue *key) {
695   return mainposition(t, key);
696 }
697 
698 int luaH_isdummy (const Table *t) { return isdummy(t); }
699 
700 #endif
701