ltable.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. /*
  2. ** $Id: ltable.c $
  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. ** Only tables with hash parts larget than LIMFORLAST has a 'lastfree'
  35. ** field that optimizes finding a free slot. Smaller tables do a
  36. ** complete search when looking for a free slot.
  37. */
  38. #define LLIMFORLAST 2 /* log2 of LIMTFORLAST */
  39. #define LIMFORLAST twoto(LLIMFORLAST)
  40. /*
  41. ** Union to store an int field ensuring that what follows it in
  42. ** memory is properly aligned to store a TValue.
  43. */
  44. typedef union {
  45. int lastfree;
  46. char padding[offsetof(struct { int i; TValue v; }, v)];
  47. } Limbox;
  48. #define haslastfree(t) ((t)->lsizenode > LLIMFORLAST)
  49. #define getlastfree(t) (&((cast(Limbox *, (t)->node) - 1)->lastfree))
  50. /*
  51. ** MAXABITS is the largest integer such that MAXASIZE fits in an
  52. ** unsigned int.
  53. */
  54. #define MAXABITS cast_int(sizeof(int) * CHAR_BIT - 1)
  55. /*
  56. ** MAXASIZE is the maximum size of the array part. It is the minimum
  57. ** between 2^MAXABITS and the maximum size that, measured in bytes,
  58. ** fits in a 'size_t'.
  59. */
  60. #define MAXASIZE luaM_limitN(1u << MAXABITS, TValue)
  61. /*
  62. ** MAXHBITS is the largest integer such that 2^MAXHBITS fits in a
  63. ** signed int.
  64. */
  65. #define MAXHBITS (MAXABITS - 1)
  66. /*
  67. ** MAXHSIZE is the maximum size of the hash part. It is the minimum
  68. ** between 2^MAXHBITS and the maximum size such that, measured in bytes,
  69. ** it fits in a 'size_t'.
  70. */
  71. #define MAXHSIZE luaM_limitN(1u << MAXHBITS, Node)
  72. /*
  73. ** When the original hash value is good, hashing by a power of 2
  74. ** avoids the cost of '%'.
  75. */
  76. #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
  77. /*
  78. ** for other types, it is better to avoid modulo by power of 2, as
  79. ** they can have many 2 factors.
  80. */
  81. #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
  82. #define hashstr(t,str) hashpow2(t, (str)->hash)
  83. #define hashboolean(t,p) hashpow2(t, p)
  84. #define hashpointer(t,p) hashmod(t, point2uint(p))
  85. #define dummynode (&dummynode_)
  86. static const Node dummynode_ = {
  87. {{NULL}, LUA_VEMPTY, /* value's value and type */
  88. LUA_VNIL, 0, {NULL}} /* key type, next, and key value */
  89. };
  90. static const TValue absentkey = {ABSTKEYCONSTANT};
  91. /*
  92. ** Hash for integers. To allow a good hash, use the remainder operator
  93. ** ('%'). If integer fits as a non-negative int, compute an int
  94. ** remainder, which is faster. Otherwise, use an unsigned-integer
  95. ** remainder, which uses all bits and ensures a non-negative result.
  96. */
  97. static Node *hashint (const Table *t, lua_Integer i) {
  98. lua_Unsigned ui = l_castS2U(i);
  99. if (ui <= cast_uint(INT_MAX))
  100. return hashmod(t, cast_int(ui));
  101. else
  102. return hashmod(t, ui);
  103. }
  104. /*
  105. ** Hash for floating-point numbers.
  106. ** The main computation should be just
  107. ** n = frexp(n, &i); return (n * INT_MAX) + i
  108. ** but there are some numerical subtleties.
  109. ** In a two-complement representation, INT_MAX does not has an exact
  110. ** representation as a float, but INT_MIN does; because the absolute
  111. ** value of 'frexp' is smaller than 1 (unless 'n' is inf/NaN), the
  112. ** absolute value of the product 'frexp * -INT_MIN' is smaller or equal
  113. ** to INT_MAX. Next, the use of 'unsigned int' avoids overflows when
  114. ** adding 'i'; the use of '~u' (instead of '-u') avoids problems with
  115. ** INT_MIN.
  116. */
  117. #if !defined(l_hashfloat)
  118. static int l_hashfloat (lua_Number n) {
  119. int i;
  120. lua_Integer ni;
  121. n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN);
  122. if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */
  123. lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL));
  124. return 0;
  125. }
  126. else { /* normal case */
  127. unsigned int u = cast_uint(i) + cast_uint(ni);
  128. return cast_int(u <= cast_uint(INT_MAX) ? u : ~u);
  129. }
  130. }
  131. #endif
  132. /*
  133. ** returns the 'main' position of an element in a table (that is,
  134. ** the index of its hash value).
  135. */
  136. static Node *mainpositionTV (const Table *t, const TValue *key) {
  137. switch (ttypetag(key)) {
  138. case LUA_VNUMINT: {
  139. lua_Integer i = ivalue(key);
  140. return hashint(t, i);
  141. }
  142. case LUA_VNUMFLT: {
  143. lua_Number n = fltvalue(key);
  144. return hashmod(t, l_hashfloat(n));
  145. }
  146. case LUA_VSHRSTR: {
  147. TString *ts = tsvalue(key);
  148. return hashstr(t, ts);
  149. }
  150. case LUA_VLNGSTR: {
  151. TString *ts = tsvalue(key);
  152. return hashpow2(t, luaS_hashlongstr(ts));
  153. }
  154. case LUA_VFALSE:
  155. return hashboolean(t, 0);
  156. case LUA_VTRUE:
  157. return hashboolean(t, 1);
  158. case LUA_VLIGHTUSERDATA: {
  159. void *p = pvalue(key);
  160. return hashpointer(t, p);
  161. }
  162. case LUA_VLCF: {
  163. lua_CFunction f = fvalue(key);
  164. return hashpointer(t, f);
  165. }
  166. default: {
  167. GCObject *o = gcvalue(key);
  168. return hashpointer(t, o);
  169. }
  170. }
  171. }
  172. l_sinline Node *mainpositionfromnode (const Table *t, Node *nd) {
  173. TValue key;
  174. getnodekey(cast(lua_State *, NULL), &key, nd);
  175. return mainpositionTV(t, &key);
  176. }
  177. /*
  178. ** Check whether key 'k1' is equal to the key in node 'n2'. This
  179. ** equality is raw, so there are no metamethods. Floats with integer
  180. ** values have been normalized, so integers cannot be equal to
  181. ** floats. It is assumed that 'eqshrstr' is simply pointer equality, so
  182. ** that short strings are handled in the default case.
  183. ** A true 'deadok' means to accept dead keys as equal to their original
  184. ** values. All dead keys are compared in the default case, by pointer
  185. ** identity. (Only collectable objects can produce dead keys.) Note that
  186. ** dead long strings are also compared by identity.
  187. ** Once a key is dead, its corresponding value may be collected, and
  188. ** then another value can be created with the same address. If this
  189. ** other value is given to 'next', 'equalkey' will signal a false
  190. ** positive. In a regular traversal, this situation should never happen,
  191. ** as all keys given to 'next' came from the table itself, and therefore
  192. ** could not have been collected. Outside a regular traversal, we
  193. ** have garbage in, garbage out. What is relevant is that this false
  194. ** positive does not break anything. (In particular, 'next' will return
  195. ** some other valid item on the table or nil.)
  196. */
  197. static int equalkey (const TValue *k1, const Node *n2, int deadok) {
  198. if ((rawtt(k1) != keytt(n2)) && /* not the same variants? */
  199. !(deadok && keyisdead(n2) && iscollectable(k1)))
  200. return 0; /* cannot be same key */
  201. switch (keytt(n2)) {
  202. case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE:
  203. return 1;
  204. case LUA_VNUMINT:
  205. return (ivalue(k1) == keyival(n2));
  206. case LUA_VNUMFLT:
  207. return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2)));
  208. case LUA_VLIGHTUSERDATA:
  209. return pvalue(k1) == pvalueraw(keyval(n2));
  210. case LUA_VLCF:
  211. return fvalue(k1) == fvalueraw(keyval(n2));
  212. case ctb(LUA_VLNGSTR):
  213. return luaS_eqlngstr(tsvalue(k1), keystrval(n2));
  214. default:
  215. return gcvalue(k1) == gcvalueraw(keyval(n2));
  216. }
  217. }
  218. /*
  219. ** True if value of 'alimit' is equal to the real size of the array
  220. ** part of table 't'. (Otherwise, the array part must be larger than
  221. ** 'alimit'.)
  222. */
  223. #define limitequalsasize(t) (isrealasize(t) || ispow2((t)->alimit))
  224. /*
  225. ** Returns the real size of the 'array' array
  226. */
  227. LUAI_FUNC unsigned int luaH_realasize (const Table *t) {
  228. if (limitequalsasize(t))
  229. return t->alimit; /* this is the size */
  230. else {
  231. unsigned int size = t->alimit;
  232. /* compute the smallest power of 2 not smaller than 'size' */
  233. size |= (size >> 1);
  234. size |= (size >> 2);
  235. size |= (size >> 4);
  236. size |= (size >> 8);
  237. #if (UINT_MAX >> 14) > 3 /* unsigned int has more than 16 bits */
  238. size |= (size >> 16);
  239. #if (UINT_MAX >> 30) > 3
  240. size |= (size >> 32); /* unsigned int has more than 32 bits */
  241. #endif
  242. #endif
  243. size++;
  244. lua_assert(ispow2(size) && size/2 < t->alimit && t->alimit < size);
  245. return size;
  246. }
  247. }
  248. /*
  249. ** Check whether real size of the array is a power of 2.
  250. ** (If it is not, 'alimit' cannot be changed to any other value
  251. ** without changing the real size.)
  252. */
  253. static int ispow2realasize (const Table *t) {
  254. return (!isrealasize(t) || ispow2(t->alimit));
  255. }
  256. static unsigned int setlimittosize (Table *t) {
  257. t->alimit = luaH_realasize(t);
  258. setrealasize(t);
  259. return t->alimit;
  260. }
  261. #define limitasasize(t) check_exp(isrealasize(t), t->alimit)
  262. /*
  263. ** "Generic" get version. (Not that generic: not valid for integers,
  264. ** which may be in array part, nor for floats with integral values.)
  265. ** See explanation about 'deadok' in function 'equalkey'.
  266. */
  267. static const TValue *getgeneric (Table *t, const TValue *key, int deadok) {
  268. Node *n = mainpositionTV(t, key);
  269. for (;;) { /* check whether 'key' is somewhere in the chain */
  270. if (equalkey(key, n, deadok))
  271. return gval(n); /* that's it */
  272. else {
  273. int nx = gnext(n);
  274. if (nx == 0)
  275. return &absentkey; /* not found */
  276. n += nx;
  277. }
  278. }
  279. }
  280. /*
  281. ** returns the index for 'k' if 'k' is an appropriate key to live in
  282. ** the array part of a table, 0 otherwise.
  283. */
  284. static unsigned int arrayindex (lua_Integer k) {
  285. if (l_castS2U(k) - 1u < MAXASIZE) /* 'k' in [1, MAXASIZE]? */
  286. return cast_uint(k); /* 'key' is an appropriate array index */
  287. else
  288. return 0;
  289. }
  290. /*
  291. ** returns the index of a 'key' for table traversals. First goes all
  292. ** elements in the array part, then elements in the hash part. The
  293. ** beginning of a traversal is signaled by 0.
  294. */
  295. static unsigned int findindex (lua_State *L, Table *t, TValue *key,
  296. unsigned int asize) {
  297. unsigned int i;
  298. if (ttisnil(key)) return 0; /* first iteration */
  299. i = ttisinteger(key) ? arrayindex(ivalue(key)) : 0;
  300. if (i - 1u < asize) /* is 'key' inside array part? */
  301. return i; /* yes; that's the index */
  302. else {
  303. const TValue *n = getgeneric(t, key, 1);
  304. if (l_unlikely(isabstkey(n)))
  305. luaG_runerror(L, "invalid key to 'next'"); /* key not found */
  306. i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */
  307. /* hash elements are numbered after array ones */
  308. return (i + 1) + asize;
  309. }
  310. }
  311. int luaH_next (lua_State *L, Table *t, StkId key) {
  312. unsigned int asize = luaH_realasize(t);
  313. unsigned int i = findindex(L, t, s2v(key), asize); /* find original key */
  314. for (; i < asize; i++) { /* try first array part */
  315. int tag = *getArrTag(t, i);
  316. if (!tagisempty(tag)) { /* a non-empty entry? */
  317. setivalue(s2v(key), i + 1);
  318. farr2val(t, i + 1, tag, s2v(key + 1));
  319. return 1;
  320. }
  321. }
  322. for (i -= asize; cast_int(i) < sizenode(t); i++) { /* hash part */
  323. if (!isempty(gval(gnode(t, i)))) { /* a non-empty entry? */
  324. Node *n = gnode(t, i);
  325. getnodekey(L, s2v(key), n);
  326. setobj2s(L, key + 1, gval(n));
  327. return 1;
  328. }
  329. }
  330. return 0; /* no more elements */
  331. }
  332. static void freehash (lua_State *L, Table *t) {
  333. if (!isdummy(t)) {
  334. size_t bsize = sizenode(t) * sizeof(Node); /* 'node' size in bytes */
  335. char *arr = cast_charp(t->node);
  336. if (haslastfree(t)) {
  337. bsize += sizeof(Limbox);
  338. arr -= sizeof(Limbox);
  339. }
  340. luaM_freearray(L, arr, bsize);
  341. }
  342. }
  343. /*
  344. ** Check whether an integer key is in the array part. If 'alimit' is
  345. ** not the real size of the array, the key still can be in the array
  346. ** part. In this case, do the "Xmilia trick" to check whether 'key-1'
  347. ** is smaller than the real size.
  348. ** The trick works as follow: let 'p' be an integer such that
  349. ** '2^(p+1) >= alimit > 2^p', or '2^(p+1) > alimit-1 >= 2^p'.
  350. ** That is, 2^(p+1) is the real size of the array, and 'p' is the highest
  351. ** bit on in 'alimit-1'. What we have to check becomes 'key-1 < 2^(p+1)'.
  352. ** We compute '(key-1) & ~(alimit-1)', which we call 'res'; it will
  353. ** have the 'p' bit cleared. If the key is outside the array, that is,
  354. ** 'key-1 >= 2^(p+1)', then 'res' will have some 1-bit higher than 'p',
  355. ** therefore it will be larger or equal to 'alimit', and the check
  356. ** will fail. If 'key-1 < 2^(p+1)', then 'res' has no 1-bit higher than
  357. ** 'p', and as the bit 'p' itself was cleared, 'res' will be smaller
  358. ** than 2^p, therefore smaller than 'alimit', and the check succeeds.
  359. ** As special cases, when 'alimit' is 0 the condition is trivially false,
  360. ** and when 'alimit' is 1 the condition simplifies to 'key-1 < alimit'.
  361. ** If key is 0 or negative, 'res' will have its higher bit on, so that
  362. ** if cannot be smaller than alimit.
  363. */
  364. static int keyinarray (Table *t, lua_Integer key) {
  365. lua_Unsigned alimit = t->alimit;
  366. if (l_castS2U(key) - 1u < alimit) /* 'key' in [1, t->alimit]? */
  367. return 1;
  368. else if (!isrealasize(t) && /* key still may be in the array part? */
  369. (((l_castS2U(key) - 1u) & ~(alimit - 1u)) < alimit)) {
  370. t->alimit = cast_uint(key); /* probably '#t' is here now */
  371. return 1;
  372. }
  373. else
  374. return 0;
  375. }
  376. /*
  377. ** {=============================================================
  378. ** Rehash
  379. ** ==============================================================
  380. */
  381. /*
  382. ** Compute the optimal size for the array part of table 't'. 'nums' is a
  383. ** "count array" where 'nums[i]' is the number of integers in the table
  384. ** between 2^(i - 1) + 1 and 2^i. 'pna' enters with the total number of
  385. ** integer keys in the table and leaves with the number of keys that
  386. ** will go to the array part; return the optimal size. (The condition
  387. ** 'twotoi > 0' in the for loop stops the loop if 'twotoi' overflows.)
  388. */
  389. static unsigned int computesizes (unsigned int nums[], unsigned int *pna) {
  390. int i;
  391. unsigned int twotoi; /* 2^i (candidate for optimal size) */
  392. unsigned int a = 0; /* number of elements smaller than 2^i */
  393. unsigned int na = 0; /* number of elements to go to array part */
  394. unsigned int optimal = 0; /* optimal size for array part */
  395. /* loop while keys can fill more than half of total size */
  396. for (i = 0, twotoi = 1;
  397. twotoi > 0 && *pna > twotoi / 2;
  398. i++, twotoi *= 2) {
  399. a += nums[i];
  400. if (a > twotoi/2) { /* more than half elements present? */
  401. optimal = twotoi; /* optimal size (till now) */
  402. na = a; /* all elements up to 'optimal' will go to array part */
  403. }
  404. }
  405. lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal);
  406. *pna = na;
  407. return optimal;
  408. }
  409. static int countint (lua_Integer key, unsigned int *nums) {
  410. unsigned int k = arrayindex(key);
  411. if (k != 0) { /* is 'key' an appropriate array index? */
  412. nums[luaO_ceillog2(k)]++; /* count as such */
  413. return 1;
  414. }
  415. else
  416. return 0;
  417. }
  418. l_sinline int arraykeyisempty (const Table *t, lua_Integer key) {
  419. int tag = *getArrTag(t, key - 1);
  420. return tagisempty(tag);
  421. }
  422. /*
  423. ** Count keys in array part of table 't': Fill 'nums[i]' with
  424. ** number of keys that will go into corresponding slice and return
  425. ** total number of non-nil keys.
  426. */
  427. static unsigned int numusearray (const Table *t, unsigned int *nums) {
  428. int lg;
  429. unsigned int ttlg; /* 2^lg */
  430. unsigned int ause = 0; /* summation of 'nums' */
  431. unsigned int i = 1; /* count to traverse all array keys */
  432. unsigned int asize = limitasasize(t); /* real array size */
  433. /* traverse each slice */
  434. for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) {
  435. unsigned int lc = 0; /* counter */
  436. unsigned int lim = ttlg;
  437. if (lim > asize) {
  438. lim = asize; /* adjust upper limit */
  439. if (i > lim)
  440. break; /* no more elements to count */
  441. }
  442. /* count elements in range (2^(lg - 1), 2^lg] */
  443. for (; i <= lim; i++) {
  444. if (!arraykeyisempty(t, i))
  445. lc++;
  446. }
  447. nums[lg] += lc;
  448. ause += lc;
  449. }
  450. return ause;
  451. }
  452. static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) {
  453. int totaluse = 0; /* total number of elements */
  454. int ause = 0; /* elements added to 'nums' (can go to array part) */
  455. int i = sizenode(t);
  456. while (i--) {
  457. Node *n = &t->node[i];
  458. if (!isempty(gval(n))) {
  459. if (keyisinteger(n))
  460. ause += countint(keyival(n), nums);
  461. totaluse++;
  462. }
  463. }
  464. *pna += ause;
  465. return totaluse;
  466. }
  467. /*
  468. ** Convert an "abstract size" (number of values in an array) to
  469. ** "concrete size" (number of cell elements in the array). Cells
  470. ** do not need to be full; we only must make sure it has the values
  471. ** needed and its 'tag' element. So, we compute the concrete tag index
  472. ** and the concrete value index of the last element, get their maximum
  473. ** and adds 1.
  474. */
  475. static unsigned int concretesize (unsigned int size) {
  476. if (size == 0) return 0;
  477. else {
  478. unsigned int ts = TagIndex(size - 1);
  479. unsigned int vs = ValueIndex(size - 1);
  480. return ((ts >= vs) ? ts : vs) + 1;
  481. }
  482. }
  483. static ArrayCell *resizearray (lua_State *L , Table *t,
  484. unsigned int oldasize,
  485. unsigned int newasize) {
  486. oldasize = concretesize(oldasize);
  487. newasize = concretesize(newasize);
  488. return luaM_reallocvector(L, t->array, oldasize, newasize, ArrayCell);
  489. }
  490. /*
  491. ** Creates an array for the hash part of a table with the given
  492. ** size, or reuses the dummy node if size is zero.
  493. ** The computation for size overflow is in two steps: the first
  494. ** comparison ensures that the shift in the second one does not
  495. ** overflow.
  496. */
  497. static void setnodevector (lua_State *L, Table *t, unsigned int size) {
  498. if (size == 0) { /* no elements to hash part? */
  499. t->node = cast(Node *, dummynode); /* use common 'dummynode' */
  500. t->lsizenode = 0;
  501. setdummy(t); /* signal that it is using dummy node */
  502. }
  503. else {
  504. int i;
  505. int lsize = luaO_ceillog2(size);
  506. if (lsize > MAXHBITS || (1u << lsize) > MAXHSIZE)
  507. luaG_runerror(L, "table overflow");
  508. size = twoto(lsize);
  509. if (lsize <= LLIMFORLAST) /* no 'lastfree' field? */
  510. t->node = luaM_newvector(L, size, Node);
  511. else {
  512. size_t bsize = size * sizeof(Node) + sizeof(Limbox);
  513. char *node = luaM_newblock(L, bsize);
  514. t->node = cast(Node *, node + sizeof(Limbox));
  515. *getlastfree(t) = size; /* all positions are free */
  516. }
  517. t->lsizenode = cast_byte(lsize);
  518. setnodummy(t);
  519. for (i = 0; i < cast_int(size); i++) {
  520. Node *n = gnode(t, i);
  521. gnext(n) = 0;
  522. setnilkey(n);
  523. setempty(gval(n));
  524. }
  525. }
  526. }
  527. /*
  528. ** (Re)insert all elements from the hash part of 'ot' into table 't'.
  529. */
  530. static void reinsert (lua_State *L, Table *ot, Table *t) {
  531. int j;
  532. int size = sizenode(ot);
  533. for (j = 0; j < size; j++) {
  534. Node *old = gnode(ot, j);
  535. if (!isempty(gval(old))) {
  536. /* doesn't need barrier/invalidate cache, as entry was
  537. already present in the table */
  538. TValue k;
  539. getnodekey(L, &k, old);
  540. luaH_set(L, t, &k, gval(old));
  541. }
  542. }
  543. }
  544. /*
  545. ** Exchange the hash part of 't1' and 't2'. (In 'flags', only the
  546. ** dummy bit must be exchanged: The 'isrealasize' is not related
  547. ** to the hash part, and the metamethod bits do not change during
  548. ** a resize, so the "real" table can keep their values.)
  549. */
  550. static void exchangehashpart (Table *t1, Table *t2) {
  551. lu_byte lsizenode = t1->lsizenode;
  552. Node *node = t1->node;
  553. int bitdummy1 = t1->flags & BITDUMMY;
  554. t1->lsizenode = t2->lsizenode;
  555. t1->node = t2->node;
  556. t1->flags = (t1->flags & NOTBITDUMMY) | (t2->flags & BITDUMMY);
  557. t2->lsizenode = lsizenode;
  558. t2->node = node;
  559. t2->flags = (t2->flags & NOTBITDUMMY) | bitdummy1;
  560. }
  561. /*
  562. ** Resize table 't' for the new given sizes. Both allocations (for
  563. ** the hash part and for the array part) can fail, which creates some
  564. ** subtleties. If the first allocation, for the hash part, fails, an
  565. ** error is raised and that is it. Otherwise, it copies the elements from
  566. ** the shrinking part of the array (if it is shrinking) into the new
  567. ** hash. Then it reallocates the array part. If that fails, the table
  568. ** is in its original state; the function frees the new hash part and then
  569. ** raises the allocation error. Otherwise, it sets the new hash part
  570. ** into the table, initializes the new part of the array (if any) with
  571. ** nils and reinserts the elements of the old hash back into the new
  572. ** parts of the table.
  573. */
  574. void luaH_resize (lua_State *L, Table *t, unsigned int newasize,
  575. unsigned int nhsize) {
  576. unsigned int i;
  577. Table newt; /* to keep the new hash part */
  578. unsigned int oldasize = setlimittosize(t);
  579. ArrayCell *newarray;
  580. /* create new hash part with appropriate size into 'newt' */
  581. newt.flags = 0;
  582. setnodevector(L, &newt, nhsize);
  583. if (newasize < oldasize) { /* will array shrink? */
  584. t->alimit = newasize; /* pretend array has new size... */
  585. exchangehashpart(t, &newt); /* and new hash */
  586. /* re-insert into the new hash the elements from vanishing slice */
  587. for (i = newasize; i < oldasize; i++) {
  588. int tag = *getArrTag(t, i);
  589. if (!tagisempty(tag)) { /* a non-empty entry? */
  590. TValue aux;
  591. farr2val(t, i + 1, tag, &aux);
  592. luaH_setint(L, t, i + 1, &aux);
  593. }
  594. }
  595. t->alimit = oldasize; /* restore current size... */
  596. exchangehashpart(t, &newt); /* and hash (in case of errors) */
  597. }
  598. /* allocate new array */
  599. newarray = resizearray(L, t, oldasize, newasize);
  600. if (l_unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */
  601. freehash(L, &newt); /* release new hash part */
  602. luaM_error(L); /* raise error (with array unchanged) */
  603. }
  604. /* allocation ok; initialize new part of the array */
  605. exchangehashpart(t, &newt); /* 't' has the new hash ('newt' has the old) */
  606. t->array = newarray; /* set new array part */
  607. t->alimit = newasize;
  608. for (i = oldasize; i < newasize; i++) /* clear new slice of the array */
  609. *getArrTag(t, i) = LUA_VEMPTY;
  610. /* re-insert elements from old hash part into new parts */
  611. reinsert(L, &newt, t); /* 'newt' now has the old hash */
  612. freehash(L, &newt); /* free old hash part */
  613. }
  614. void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) {
  615. int nsize = allocsizenode(t);
  616. luaH_resize(L, t, nasize, nsize);
  617. }
  618. /*
  619. ** nums[i] = number of keys 'k' where 2^(i - 1) < k <= 2^i
  620. */
  621. static void rehash (lua_State *L, Table *t, const TValue *ek) {
  622. unsigned int asize; /* optimal size for array part */
  623. unsigned int na; /* number of keys in the array part */
  624. unsigned int nums[MAXABITS + 1];
  625. int i;
  626. int totaluse;
  627. for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */
  628. setlimittosize(t);
  629. na = numusearray(t, nums); /* count keys in array part */
  630. totaluse = na; /* all those keys are integer keys */
  631. totaluse += numusehash(t, nums, &na); /* count keys in hash part */
  632. /* count extra key */
  633. if (ttisinteger(ek))
  634. na += countint(ivalue(ek), nums);
  635. totaluse++;
  636. /* compute new size for array part */
  637. asize = computesizes(nums, &na);
  638. /* resize the table to new computed sizes */
  639. luaH_resize(L, t, asize, totaluse - na);
  640. }
  641. /*
  642. ** }=============================================================
  643. */
  644. Table *luaH_new (lua_State *L) {
  645. GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table));
  646. Table *t = gco2t(o);
  647. t->metatable = NULL;
  648. t->flags = cast_byte(maskflags); /* table has no metamethod fields */
  649. t->array = NULL;
  650. t->alimit = 0;
  651. setnodevector(L, t, 0);
  652. return t;
  653. }
  654. void luaH_free (lua_State *L, Table *t) {
  655. unsigned ps = concretesize(luaH_realasize(t));
  656. freehash(L, t);
  657. luaM_freearray(L, t->array, ps);
  658. luaM_free(L, t);
  659. }
  660. static Node *getfreepos (Table *t) {
  661. if (haslastfree(t)) { /* does it have 'lastfree' information? */
  662. /* look for a spot before 'lastfree', updating 'lastfree' */
  663. while (*getlastfree(t) > 0) {
  664. Node *free = gnode(t, --(*getlastfree(t)));
  665. if (keyisnil(free))
  666. return free;
  667. }
  668. }
  669. else { /* no 'lastfree' information */
  670. if (!isdummy(t)) {
  671. int i = sizenode(t);
  672. while (i--) { /* do a linear search */
  673. Node *free = gnode(t, i);
  674. if (keyisnil(free))
  675. return free;
  676. }
  677. }
  678. }
  679. return NULL; /* could not find a free place */
  680. }
  681. /*
  682. ** inserts a new key into a hash table; first, check whether key's main
  683. ** position is free. If not, check whether colliding node is in its main
  684. ** position or not: if it is not, move colliding node to an empty place and
  685. ** put new key in its main position; otherwise (colliding node is in its main
  686. ** position), new key goes to an empty position.
  687. */
  688. static void luaH_newkey (lua_State *L, Table *t, const TValue *key,
  689. TValue *value) {
  690. Node *mp;
  691. TValue aux;
  692. if (l_unlikely(ttisnil(key)))
  693. luaG_runerror(L, "table index is nil");
  694. else if (ttisfloat(key)) {
  695. lua_Number f = fltvalue(key);
  696. lua_Integer k;
  697. if (luaV_flttointeger(f, &k, F2Ieq)) { /* does key fit in an integer? */
  698. setivalue(&aux, k);
  699. key = &aux; /* insert it as an integer */
  700. }
  701. else if (l_unlikely(luai_numisnan(f)))
  702. luaG_runerror(L, "table index is NaN");
  703. }
  704. if (ttisnil(value))
  705. return; /* do not insert nil values */
  706. mp = mainpositionTV(t, key);
  707. if (!isempty(gval(mp)) || isdummy(t)) { /* main position is taken? */
  708. Node *othern;
  709. Node *f = getfreepos(t); /* get a free place */
  710. if (f == NULL) { /* cannot find a free place? */
  711. rehash(L, t, key); /* grow table */
  712. /* whatever called 'newkey' takes care of TM cache */
  713. luaH_set(L, t, key, value); /* insert key into grown table */
  714. return;
  715. }
  716. lua_assert(!isdummy(t));
  717. othern = mainpositionfromnode(t, mp);
  718. if (othern != mp) { /* is colliding node out of its main position? */
  719. /* yes; move colliding node into free position */
  720. while (othern + gnext(othern) != mp) /* find previous */
  721. othern += gnext(othern);
  722. gnext(othern) = cast_int(f - othern); /* rechain to point to 'f' */
  723. *f = *mp; /* copy colliding node into free pos. (mp->next also goes) */
  724. if (gnext(mp) != 0) {
  725. gnext(f) += cast_int(mp - f); /* correct 'next' */
  726. gnext(mp) = 0; /* now 'mp' is free */
  727. }
  728. setempty(gval(mp));
  729. }
  730. else { /* colliding node is in its own main position */
  731. /* new node will go into free position */
  732. if (gnext(mp) != 0)
  733. gnext(f) = cast_int((mp + gnext(mp)) - f); /* chain new position */
  734. else lua_assert(gnext(f) == 0);
  735. gnext(mp) = cast_int(f - mp);
  736. mp = f;
  737. }
  738. }
  739. setnodekey(L, mp, key);
  740. luaC_barrierback(L, obj2gco(t), key);
  741. lua_assert(isempty(gval(mp)));
  742. setobj2t(L, gval(mp), value);
  743. }
  744. static const TValue *getintfromhash (Table *t, lua_Integer key) {
  745. Node *n = hashint(t, key);
  746. lua_assert(l_castS2U(key) - 1u >= luaH_realasize(t));
  747. for (;;) { /* check whether 'key' is somewhere in the chain */
  748. if (keyisinteger(n) && keyival(n) == key)
  749. return gval(n); /* that's it */
  750. else {
  751. int nx = gnext(n);
  752. if (nx == 0) break;
  753. n += nx;
  754. }
  755. }
  756. return &absentkey;
  757. }
  758. static int hashkeyisempty (Table *t, lua_Integer key) {
  759. const TValue *val = getintfromhash(t, key);
  760. return isempty(val);
  761. }
  762. static int finishnodeget (const TValue *val, TValue *res) {
  763. if (!ttisnil(val)) {
  764. setobj(((lua_State*)NULL), res, val);
  765. return HOK; /* success */
  766. }
  767. else
  768. return HNOTFOUND; /* could not get value */
  769. }
  770. int luaH_getint (Table *t, lua_Integer key, TValue *res) {
  771. if (keyinarray(t, key)) {
  772. int tag = *getArrTag(t, key - 1);
  773. if (!tagisempty(tag)) {
  774. farr2val(t, key, tag, res);
  775. return HOK; /* success */
  776. }
  777. else
  778. return ~cast_int(key); /* empty slot in the array part */
  779. }
  780. else
  781. return finishnodeget(getintfromhash(t, key), res);
  782. }
  783. /*
  784. ** search function for short strings
  785. */
  786. const TValue *luaH_Hgetshortstr (Table *t, TString *key) {
  787. Node *n = hashstr(t, key);
  788. lua_assert(key->tt == LUA_VSHRSTR);
  789. for (;;) { /* check whether 'key' is somewhere in the chain */
  790. if (keyisshrstr(n) && eqshrstr(keystrval(n), key))
  791. return gval(n); /* that's it */
  792. else {
  793. int nx = gnext(n);
  794. if (nx == 0)
  795. return &absentkey; /* not found */
  796. n += nx;
  797. }
  798. }
  799. }
  800. int luaH_getshortstr (Table *t, TString *key, TValue *res) {
  801. return finishnodeget(luaH_Hgetshortstr(t, key), res);
  802. }
  803. static const TValue *Hgetstr (Table *t, TString *key) {
  804. if (key->tt == LUA_VSHRSTR)
  805. return luaH_Hgetshortstr(t, key);
  806. else { /* for long strings, use generic case */
  807. TValue ko;
  808. setsvalue(cast(lua_State *, NULL), &ko, key);
  809. return getgeneric(t, &ko, 0);
  810. }
  811. }
  812. int luaH_getstr (Table *t, TString *key, TValue *res) {
  813. return finishnodeget(Hgetstr(t, key), res);
  814. }
  815. TString *luaH_getstrkey (Table *t, TString *key) {
  816. const TValue *o = Hgetstr(t, key);
  817. if (!isabstkey(o)) /* string already present? */
  818. return keystrval(nodefromval(o)); /* get saved copy */
  819. else
  820. return NULL;
  821. }
  822. /*
  823. ** main search function
  824. */
  825. int luaH_get (Table *t, const TValue *key, TValue *res) {
  826. const TValue *slot;
  827. switch (ttypetag(key)) {
  828. case LUA_VSHRSTR:
  829. slot = luaH_Hgetshortstr(t, tsvalue(key));
  830. break;
  831. case LUA_VNUMINT:
  832. return luaH_getint(t, ivalue(key), res);
  833. case LUA_VNIL:
  834. slot = &absentkey;
  835. break;
  836. case LUA_VNUMFLT: {
  837. lua_Integer k;
  838. if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */
  839. return luaH_getint(t, k, res); /* use specialized version */
  840. /* else... */
  841. } /* FALLTHROUGH */
  842. default:
  843. slot = getgeneric(t, key, 0);
  844. break;
  845. }
  846. return finishnodeget(slot, res);
  847. }
  848. static int finishnodeset (Table *t, const TValue *slot, TValue *val) {
  849. if (!ttisnil(slot)) {
  850. setobj(((lua_State*)NULL), cast(TValue*, slot), val);
  851. return HOK; /* success */
  852. }
  853. else if (isabstkey(slot))
  854. return HNOTFOUND; /* no slot with that key */
  855. else return (cast(Node*, slot) - t->node) + HFIRSTNODE; /* node encoded */
  856. }
  857. int luaH_psetint (Table *t, lua_Integer key, TValue *val) {
  858. if (keyinarray(t, key)) {
  859. lu_byte *tag = getArrTag(t, key - 1);
  860. if (!tagisempty(*tag)) {
  861. fval2arr(t, key, tag, val);
  862. return HOK; /* success */
  863. }
  864. else
  865. return ~cast_int(key); /* empty slot in the array part */
  866. }
  867. else
  868. return finishnodeset(t, getintfromhash(t, key), val);
  869. }
  870. int luaH_psetshortstr (Table *t, TString *key, TValue *val) {
  871. return finishnodeset(t, luaH_Hgetshortstr(t, key), val);
  872. }
  873. int luaH_psetstr (Table *t, TString *key, TValue *val) {
  874. return finishnodeset(t, Hgetstr(t, key), val);
  875. }
  876. int luaH_pset (Table *t, const TValue *key, TValue *val) {
  877. switch (ttypetag(key)) {
  878. case LUA_VSHRSTR: return luaH_psetshortstr(t, tsvalue(key), val);
  879. case LUA_VNUMINT: return luaH_psetint(t, ivalue(key), val);
  880. case LUA_VNIL: return HNOTFOUND;
  881. case LUA_VNUMFLT: {
  882. lua_Integer k;
  883. if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */
  884. return luaH_psetint(t, k, val); /* use specialized version */
  885. /* else... */
  886. } /* FALLTHROUGH */
  887. default:
  888. return finishnodeset(t, getgeneric(t, key, 0), val);
  889. }
  890. }
  891. /*
  892. ** Finish a raw "set table" operation, where 'slot' is where the value
  893. ** should have been (the result of a previous "get table").
  894. ** Beware: when using this function you probably need to check a GC
  895. ** barrier and invalidate the TM cache.
  896. */
  897. void luaH_finishset (lua_State *L, Table *t, const TValue *key,
  898. TValue *value, int hres) {
  899. lua_assert(hres != HOK);
  900. if (hres == HNOTFOUND) {
  901. luaH_newkey(L, t, key, value);
  902. }
  903. else if (hres > 0) { /* regular Node? */
  904. setobj2t(L, gval(gnode(t, hres - HFIRSTNODE)), value);
  905. }
  906. else { /* array entry */
  907. hres = ~hres; /* real index */
  908. obj2arr(t, hres, value);
  909. }
  910. }
  911. /*
  912. ** beware: when using this function you probably need to check a GC
  913. ** barrier and invalidate the TM cache.
  914. */
  915. void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value) {
  916. int hres = luaH_pset(t, key, value);
  917. if (hres != HOK)
  918. luaH_finishset(L, t, key, value, hres);
  919. }
  920. void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) {
  921. int hres = luaH_psetint(t, key, value);
  922. if (hres != HOK) {
  923. TValue k;
  924. setivalue(&k, key);
  925. luaH_finishset(L, t, &k, value, hres);
  926. }
  927. }
  928. /*
  929. ** Try to find a boundary in the hash part of table 't'. From the
  930. ** caller, we know that 'j' is zero or present and that 'j + 1' is
  931. ** present. We want to find a larger key that is absent from the
  932. ** table, so that we can do a binary search between the two keys to
  933. ** find a boundary. We keep doubling 'j' until we get an absent index.
  934. ** If the doubling would overflow, we try LUA_MAXINTEGER. If it is
  935. ** absent, we are ready for the binary search. ('j', being max integer,
  936. ** is larger or equal to 'i', but it cannot be equal because it is
  937. ** absent while 'i' is present; so 'j > i'.) Otherwise, 'j' is a
  938. ** boundary. ('j + 1' cannot be a present integer key because it is
  939. ** not a valid integer in Lua.)
  940. */
  941. static lua_Unsigned hash_search (Table *t, lua_Unsigned j) {
  942. lua_Unsigned i;
  943. if (j == 0) j++; /* the caller ensures 'j + 1' is present */
  944. do {
  945. i = j; /* 'i' is a present index */
  946. if (j <= l_castS2U(LUA_MAXINTEGER) / 2)
  947. j *= 2;
  948. else {
  949. j = LUA_MAXINTEGER;
  950. if (hashkeyisempty(t, j)) /* t[j] not present? */
  951. break; /* 'j' now is an absent index */
  952. else /* weird case */
  953. return j; /* well, max integer is a boundary... */
  954. }
  955. } while (!hashkeyisempty(t, j)); /* repeat until an absent t[j] */
  956. /* i < j && t[i] present && t[j] absent */
  957. while (j - i > 1u) { /* do a binary search between them */
  958. lua_Unsigned m = (i + j) / 2;
  959. if (hashkeyisempty(t, m)) j = m;
  960. else i = m;
  961. }
  962. return i;
  963. }
  964. static unsigned int binsearch (Table *array, unsigned int i, unsigned int j) {
  965. while (j - i > 1u) { /* binary search */
  966. unsigned int m = (i + j) / 2;
  967. if (arraykeyisempty(array, m)) j = m;
  968. else i = m;
  969. }
  970. return i;
  971. }
  972. /*
  973. ** Try to find a boundary in table 't'. (A 'boundary' is an integer index
  974. ** such that t[i] is present and t[i+1] is absent, or 0 if t[1] is absent
  975. ** and 'maxinteger' if t[maxinteger] is present.)
  976. ** (In the next explanation, we use Lua indices, that is, with base 1.
  977. ** The code itself uses base 0 when indexing the array part of the table.)
  978. ** The code starts with 'limit = t->alimit', a position in the array
  979. ** part that may be a boundary.
  980. **
  981. ** (1) If 't[limit]' is empty, there must be a boundary before it.
  982. ** As a common case (e.g., after 't[#t]=nil'), check whether 'limit-1'
  983. ** is present. If so, it is a boundary. Otherwise, do a binary search
  984. ** between 0 and limit to find a boundary. In both cases, try to
  985. ** use this boundary as the new 'alimit', as a hint for the next call.
  986. **
  987. ** (2) If 't[limit]' is not empty and the array has more elements
  988. ** after 'limit', try to find a boundary there. Again, try first
  989. ** the special case (which should be quite frequent) where 'limit+1'
  990. ** is empty, so that 'limit' is a boundary. Otherwise, check the
  991. ** last element of the array part. If it is empty, there must be a
  992. ** boundary between the old limit (present) and the last element
  993. ** (absent), which is found with a binary search. (This boundary always
  994. ** can be a new limit.)
  995. **
  996. ** (3) The last case is when there are no elements in the array part
  997. ** (limit == 0) or its last element (the new limit) is present.
  998. ** In this case, must check the hash part. If there is no hash part
  999. ** or 'limit+1' is absent, 'limit' is a boundary. Otherwise, call
  1000. ** 'hash_search' to find a boundary in the hash part of the table.
  1001. ** (In those cases, the boundary is not inside the array part, and
  1002. ** therefore cannot be used as a new limit.)
  1003. */
  1004. lua_Unsigned luaH_getn (Table *t) {
  1005. unsigned int limit = t->alimit;
  1006. if (limit > 0 && arraykeyisempty(t, limit)) { /* (1)? */
  1007. /* there must be a boundary before 'limit' */
  1008. if (limit >= 2 && !arraykeyisempty(t, limit - 1)) {
  1009. /* 'limit - 1' is a boundary; can it be a new limit? */
  1010. if (ispow2realasize(t) && !ispow2(limit - 1)) {
  1011. t->alimit = limit - 1;
  1012. setnorealasize(t); /* now 'alimit' is not the real size */
  1013. }
  1014. return limit - 1;
  1015. }
  1016. else { /* must search for a boundary in [0, limit] */
  1017. unsigned int boundary = binsearch(t, 0, limit);
  1018. /* can this boundary represent the real size of the array? */
  1019. if (ispow2realasize(t) && boundary > luaH_realasize(t) / 2) {
  1020. t->alimit = boundary; /* use it as the new limit */
  1021. setnorealasize(t);
  1022. }
  1023. return boundary;
  1024. }
  1025. }
  1026. /* 'limit' is zero or present in table */
  1027. if (!limitequalsasize(t)) { /* (2)? */
  1028. /* 'limit' > 0 and array has more elements after 'limit' */
  1029. if (arraykeyisempty(t, limit + 1)) /* 'limit + 1' is empty? */
  1030. return limit; /* this is the boundary */
  1031. /* else, try last element in the array */
  1032. limit = luaH_realasize(t);
  1033. if (arraykeyisempty(t, limit)) { /* empty? */
  1034. /* there must be a boundary in the array after old limit,
  1035. and it must be a valid new limit */
  1036. unsigned int boundary = binsearch(t, t->alimit, limit);
  1037. t->alimit = boundary;
  1038. return boundary;
  1039. }
  1040. /* else, new limit is present in the table; check the hash part */
  1041. }
  1042. /* (3) 'limit' is the last element and either is zero or present in table */
  1043. lua_assert(limit == luaH_realasize(t) &&
  1044. (limit == 0 || !arraykeyisempty(t, limit)));
  1045. if (isdummy(t) || hashkeyisempty(t, cast(lua_Integer, limit + 1)))
  1046. return limit; /* 'limit + 1' is absent */
  1047. else /* 'limit + 1' is also present */
  1048. return hash_search(t, limit);
  1049. }
  1050. #if defined(LUA_DEBUG)
  1051. /* export these functions for the test library */
  1052. Node *luaH_mainposition (const Table *t, const TValue *key) {
  1053. return mainpositionTV(t, key);
  1054. }
  1055. #endif