ltable.c 19 KB

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