ltable.c 19 KB

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