ltable.c 19 KB

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