ltable.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. /*
  2. ** $Id: ltable.c,v 2.32.1.2 2007/12/28 15:32:23 roberto Exp $
  3. ** Lua tables (hash)
  4. ** See Copyright Notice in lua.h
  5. */
  6. /*
  7. ** Implementation of tables (aka arrays, objects, or hash tables).
  8. ** Tables keep its elements in two parts: an array part and a hash part.
  9. ** Non-negative integer keys are all candidates to be kept in the array
  10. ** part. The actual size of the array is the largest `n' such that at
  11. ** least half the slots between 0 and n are in use.
  12. ** Hash uses a mix of chained scatter table with Brent's variation.
  13. ** A main invariant of these tables is that, if an element is not
  14. ** in its main position (i.e. the `original' position that its hash gives
  15. ** to it), then the colliding element is in its own main position.
  16. ** Hence even when the load factor reaches 100%, performance remains good.
  17. */
  18. #define ltable_c
  19. #define LUA_CORE
  20. #define LUAC_CROSS_FILE
  21. #include "lua.h"
  22. #include <math.h>
  23. #include <string.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 "ltable.h"
  31. #include "lrotable.h"
  32. /*
  33. ** max size of array part is 2^MAXBITS
  34. */
  35. #if LUAI_BITSINT > 26
  36. #define MAXBITS 26
  37. #else
  38. #define MAXBITS (LUAI_BITSINT-2)
  39. #endif
  40. #define MAXASIZE (1 << MAXBITS)
  41. #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
  42. #define hashstr(t,str) hashpow2(t, (str)->tsv.hash)
  43. #define hashboolean(t,p) hashpow2(t, p)
  44. /*
  45. ** for some types, it is better to avoid modulus by power of 2, as
  46. ** they tend to have many 2 factors.
  47. */
  48. #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
  49. #define hashpointer(t,p) hashmod(t, IntPoint(p))
  50. /*
  51. ** number of ints inside a lua_Number
  52. */
  53. #define numints cast_int(sizeof(lua_Number)/sizeof(int))
  54. #define dummynode (&dummynode_)
  55. static const Node dummynode_ = {
  56. {LUA_TVALUE_NIL}, /* value */
  57. {LUA_TKEY_NIL} /* key */
  58. };
  59. /*
  60. ** hash for lua_Numbers
  61. */
  62. static Node *hashnum (const Table *t, lua_Number n) {
  63. unsigned int a[numints];
  64. int i;
  65. if (luai_numeq(n, 0)) /* avoid problems with -0 */
  66. return gnode(t, 0);
  67. memcpy(a, &n, sizeof(a));
  68. for (i = 1; i < numints; i++) a[0] += a[i];
  69. return hashmod(t, a[0]);
  70. }
  71. /*
  72. ** returns the `main' position of an element in a table (that is, the index
  73. ** of its hash value)
  74. */
  75. static Node *mainposition (const Table *t, const TValue *key) {
  76. switch (ttype(key)) {
  77. case LUA_TNUMBER:
  78. return hashnum(t, nvalue(key));
  79. case LUA_TSTRING:
  80. return hashstr(t, rawtsvalue(key));
  81. case LUA_TBOOLEAN:
  82. return hashboolean(t, bvalue(key));
  83. case LUA_TROTABLE:
  84. return hashpointer(t, rvalue(key));
  85. case LUA_TLIGHTUSERDATA:
  86. case LUA_TLIGHTFUNCTION:
  87. return hashpointer(t, pvalue(key));
  88. default:
  89. return hashpointer(t, gcvalue(key));
  90. }
  91. }
  92. /*
  93. ** returns the index for `key' if `key' is an appropriate key to live in
  94. ** the array part of the table, -1 otherwise.
  95. */
  96. static int arrayindex (const TValue *key) {
  97. if (ttisnumber(key)) {
  98. lua_Number n = nvalue(key);
  99. int k;
  100. lua_number2int(k, n);
  101. if (luai_numeq(cast_num(k), n))
  102. return k;
  103. }
  104. return -1; /* `key' did not match some condition */
  105. }
  106. /*
  107. ** returns the index of a `key' for table traversals. First goes all
  108. ** elements in the array part, then elements in the hash part. The
  109. ** beginning of a traversal is signalled by -1.
  110. */
  111. static int findindex (lua_State *L, Table *t, StkId key) {
  112. int i;
  113. if (ttisnil(key)) return -1; /* first iteration */
  114. i = arrayindex(key);
  115. if (0 < i && i <= t->sizearray) /* is `key' inside array part? */
  116. return i-1; /* yes; that's the index (corrected to C) */
  117. else {
  118. Node *n = mainposition(t, key);
  119. do { /* check whether `key' is somewhere in the chain */
  120. /* key may be dead already, but it is ok to use it in `next' */
  121. if (luaO_rawequalObj(key2tval(n), key) ||
  122. (ttype(gkey(n)) == LUA_TDEADKEY && iscollectable(key) &&
  123. gcvalue(gkey(n)) == gcvalue(key))) {
  124. i = cast_int(n - gnode(t, 0)); /* key index in hash table */
  125. /* hash elements are numbered after array ones */
  126. return i + t->sizearray;
  127. }
  128. else n = gnext(n);
  129. } while (n);
  130. luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */
  131. return 0; /* to avoid warnings */
  132. }
  133. }
  134. int luaH_next (lua_State *L, Table *t, StkId key) {
  135. int i = findindex(L, t, key); /* find original element */
  136. for (i++; i < t->sizearray; i++) { /* try first array part */
  137. if (!ttisnil(&t->array[i])) { /* a non-nil value? */
  138. setnvalue(key, cast_num(i+1));
  139. setobj2s(L, key+1, &t->array[i]);
  140. return 1;
  141. }
  142. }
  143. for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */
  144. if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */
  145. setobj2s(L, key, key2tval(gnode(t, i)));
  146. setobj2s(L, key+1, gval(gnode(t, i)));
  147. return 1;
  148. }
  149. }
  150. return 0; /* no more elements */
  151. }
  152. int luaH_next_ro (lua_State *L, void *t, StkId key) {
  153. luaR_next(L, t, key, key+1);
  154. return ttisnil(key) ? 0 : 1;
  155. }
  156. /*
  157. ** {=============================================================
  158. ** Rehash
  159. ** ==============================================================
  160. */
  161. static int computesizes (int nums[], int *narray) {
  162. int i;
  163. int twotoi; /* 2^i */
  164. int a = 0; /* number of elements smaller than 2^i */
  165. int na = 0; /* number of elements to go to array part */
  166. int n = 0; /* optimal size for array part */
  167. for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) {
  168. if (nums[i] > 0) {
  169. a += nums[i];
  170. if (a > twotoi/2) { /* more than half elements present? */
  171. n = twotoi; /* optimal size (till now) */
  172. na = a; /* all elements smaller than n will go to array part */
  173. }
  174. }
  175. if (a == *narray) break; /* all elements already counted */
  176. }
  177. *narray = n;
  178. lua_assert(*narray/2 <= na && na <= *narray);
  179. return na;
  180. }
  181. static int countint (const TValue *key, int *nums) {
  182. int k = arrayindex(key);
  183. if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */
  184. nums[ceillog2(k)]++; /* count as such */
  185. return 1;
  186. }
  187. else
  188. return 0;
  189. }
  190. static int numusearray (const Table *t, int *nums) {
  191. int lg;
  192. int ttlg; /* 2^lg */
  193. int ause = 0; /* summation of `nums' */
  194. int i = 1; /* count to traverse all array keys */
  195. for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */
  196. int lc = 0; /* counter */
  197. int lim = ttlg;
  198. if (lim > t->sizearray) {
  199. lim = t->sizearray; /* adjust upper limit */
  200. if (i > lim)
  201. break; /* no more elements to count */
  202. }
  203. /* count elements in range (2^(lg-1), 2^lg] */
  204. for (; i <= lim; i++) {
  205. if (!ttisnil(&t->array[i-1]))
  206. lc++;
  207. }
  208. nums[lg] += lc;
  209. ause += lc;
  210. }
  211. return ause;
  212. }
  213. static int numusehash (const Table *t, int *nums, int *pnasize) {
  214. int totaluse = 0; /* total number of elements */
  215. int ause = 0; /* summation of `nums' */
  216. int i = sizenode(t);
  217. while (i--) {
  218. Node *n = &t->node[i];
  219. if (!ttisnil(gval(n))) {
  220. ause += countint(key2tval(n), nums);
  221. totaluse++;
  222. }
  223. }
  224. *pnasize += ause;
  225. return totaluse;
  226. }
  227. static void setarrayvector (lua_State *L, Table *t, int size) {
  228. int i;
  229. luaM_reallocvector(L, t->array, t->sizearray, size, TValue);
  230. for (i=t->sizearray; i<size; i++)
  231. setnilvalue(&t->array[i]);
  232. t->sizearray = size;
  233. }
  234. static Node *getfreepos (Table *t) {
  235. while (t->lastfree-- > t->node) {
  236. if (ttisnil(gkey(t->lastfree)))
  237. return t->lastfree;
  238. }
  239. return NULL; /* could not find a free place */
  240. }
  241. static void resizenodevector (lua_State *L, Table *t, int oldsize, int newsize) {
  242. int lsize;
  243. if (newsize == 0) { /* no elements to hash part? */
  244. t->node = cast(Node *, dummynode); /* use common `dummynode' */
  245. lsize = 0;
  246. }
  247. else {
  248. Node *node = t->node;
  249. int i;
  250. lsize = ceillog2(newsize);
  251. if (lsize > MAXBITS)
  252. luaG_runerror(L, "table overflow");
  253. newsize = twoto(lsize);
  254. if (node == dummynode) {
  255. oldsize = 0;
  256. node = NULL; /* don't try to realloc `dummynode' pointer. */
  257. }
  258. luaM_reallocvector(L, node, oldsize, newsize, Node);
  259. t->node = node;
  260. for (i=oldsize; i<newsize; i++) {
  261. Node *n = gnode(t, i);
  262. gnext(n) = NULL;
  263. setnilvalue(gkey(n));
  264. setnilvalue(gval(n));
  265. }
  266. }
  267. t->lsizenode = cast_byte(lsize);
  268. t->lastfree = gnode(t, newsize); /* reset lastfree to end of table. */
  269. }
  270. static Node *find_prev_node(Node *mp, Node *next) {
  271. Node *prev = mp;
  272. while (prev != NULL && gnext(prev) != next) prev = gnext(prev);
  273. return prev;
  274. }
  275. /*
  276. ** move a node from it's old position to it's new position during a rehash;
  277. ** first, check whether the moving node's main position is free. If not, check whether
  278. ** colliding node is in its main position or not: if it is not, move colliding
  279. ** node to an empty place and put moving node in its main position; otherwise
  280. ** (colliding node is in its main position), moving node goes to an empty position.
  281. */
  282. static int move_node (lua_State *L, Table *t, Node *node) {
  283. Node *mp = mainposition(t, key2tval(node));
  284. /* if node is in it's main position, don't need to move node. */
  285. if (mp == node) return 1;
  286. /* if node is in it's main position's chain, don't need to move node. */
  287. if (find_prev_node(mp, node) != NULL) return 1;
  288. /* is main position is free? */
  289. if (!ttisnil(gval(mp)) || mp == dummynode) {
  290. /* no; move main position node if it is out of its main position */
  291. Node *othermp;
  292. othermp = mainposition(t, key2tval(mp));
  293. if (othermp != mp) { /* is colliding node out of its main position? */
  294. /* yes; swap colliding node with the node that is being moved. */
  295. Node *prev;
  296. Node tmp;
  297. tmp = *node;
  298. prev = find_prev_node(othermp, mp); /* find previous */
  299. if (prev != NULL) gnext(prev) = node; /* redo the chain with `n' in place of `mp' */
  300. *node = *mp; /* copy colliding node into free pos. (mp->next also goes) */
  301. *mp = tmp;
  302. return (prev != NULL) ? 1 : 0; /* is colliding node part of its main position chain? */
  303. }
  304. else { /* colliding node is in its own main position */
  305. /* add node to main position's chain. */
  306. gnext(node) = gnext(mp); /* chain new position */
  307. gnext(mp) = node;
  308. }
  309. }
  310. else { /* main position is free, move node */
  311. *mp = *node;
  312. gnext(node) = NULL;
  313. setnilvalue(gkey(node));
  314. setnilvalue(gval(node));
  315. }
  316. return 1;
  317. }
  318. static int move_number (lua_State *L, Table *t, Node *node) {
  319. int key;
  320. lua_Number n = nvalue(key2tval(node));
  321. lua_number2int(key, n);
  322. if (luai_numeq(cast_num(key), nvalue(key2tval(node)))) {/* index is int? */
  323. /* (1 <= key && key <= t->sizearray) */
  324. if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray)) {
  325. setobjt2t(L, &t->array[key-1], gval(node));
  326. setnilvalue(gkey(node));
  327. setnilvalue(gval(node));
  328. return 1;
  329. }
  330. }
  331. return 0;
  332. }
  333. static void resize_hashpart (lua_State *L, Table *t, int nhsize) {
  334. int i;
  335. int lsize=0;
  336. int oldhsize = (t->node != dummynode) ? twoto(t->lsizenode) : 0;
  337. if (nhsize > 0) { /* round new hashpart size up to next power of two. */
  338. lsize=ceillog2(nhsize);
  339. if (lsize > MAXBITS)
  340. luaG_runerror(L, "table overflow");
  341. }
  342. nhsize = twoto(lsize);
  343. /* grow hash part to new size. */
  344. if (oldhsize < nhsize)
  345. resizenodevector(L, t, oldhsize, nhsize);
  346. else { /* hash part might be shrinking */
  347. if (nhsize > 0) {
  348. t->lsizenode = cast_byte(lsize);
  349. t->lastfree = gnode(t, nhsize); /* reset lastfree back to end of table. */
  350. }
  351. else { /* new hashpart size is zero. */
  352. resizenodevector(L, t, oldhsize, nhsize);
  353. return;
  354. }
  355. }
  356. /* break old chains, try moving int keys to array part and compact keys into new hashpart */
  357. for (i = 0; i < oldhsize; i++) {
  358. Node *old = gnode(t, i);
  359. gnext(old) = NULL;
  360. if (ttisnil(gval(old))) { /* clear nodes with nil values. */
  361. setnilvalue(gkey(old));
  362. continue;
  363. }
  364. if (ttisnumber(key2tval(old))) { /* try moving the int keys into array part. */
  365. if(move_number(L, t, old))
  366. continue;
  367. }
  368. if (i >= nhsize) { /* move all valid keys to indices < nhsize. */
  369. Node *n = getfreepos(t); /* get a free place */
  370. lua_assert(n != dummynode && n != NULL);
  371. *n = *old;
  372. }
  373. }
  374. /* shrink hash part */
  375. if (oldhsize > nhsize)
  376. resizenodevector(L, t, oldhsize, nhsize);
  377. /* move nodes to their new mainposition and re-create node chains */
  378. for (i = 0; i < nhsize; i++) {
  379. Node *curr = gnode(t, i);
  380. if (!ttisnil(gval(curr)))
  381. while (move_node(L, t, curr) == 0);
  382. }
  383. }
  384. static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
  385. int i;
  386. int oldasize = t->sizearray;
  387. if (nasize > oldasize) /* array part must grow? */
  388. setarrayvector(L, t, nasize);
  389. if (t->node != dummynode || nhsize>0)
  390. resize_hashpart(L, t, nhsize);
  391. if (nasize < oldasize) { /* array part must shrink? */
  392. t->sizearray = nasize;
  393. /* re-insert elements from vanishing slice */
  394. for (i=nasize; i<oldasize; i++) {
  395. if (!ttisnil(&t->array[i]))
  396. setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]);
  397. }
  398. /* shrink array */
  399. luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
  400. }
  401. }
  402. void luaH_resizearray (lua_State *L, Table *t, int nasize) {
  403. int nsize = (t->node == dummynode) ? 0 : sizenode(t);
  404. resize(L, t, nasize, nsize);
  405. }
  406. static void rehash (lua_State *L, Table *t, const TValue *ek) {
  407. int nasize, na;
  408. int nums[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */
  409. int i;
  410. int totaluse;
  411. for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */
  412. nasize = numusearray(t, nums); /* count keys in array part */
  413. totaluse = nasize; /* all those keys are integer keys */
  414. totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */
  415. /* count extra key */
  416. nasize += countint(ek, nums);
  417. totaluse++;
  418. /* compute new size for array part */
  419. na = computesizes(nums, &nasize);
  420. /* resize the table to new computed sizes */
  421. resize(L, t, nasize, totaluse - na);
  422. }
  423. /*
  424. ** }=============================================================
  425. */
  426. Table *luaH_new (lua_State *L, int narray, int nhash) {
  427. Table *t = luaM_new(L, Table);
  428. luaC_link(L, obj2gco(t), LUA_TTABLE);
  429. sethvalue2s(L, L->top, t); /* put table on stack */
  430. incr_top(L);
  431. t->metatable = NULL;
  432. t->flags = cast_byte(~0);
  433. /* temporary values (kept only if some malloc fails) */
  434. t->array = NULL;
  435. t->sizearray = 0;
  436. t->lsizenode = 0;
  437. t->node = cast(Node *, dummynode);
  438. setarrayvector(L, t, narray);
  439. resizenodevector(L, t, 0, nhash);
  440. L->top--; /* remove table from stack */
  441. return t;
  442. }
  443. void luaH_free (lua_State *L, Table *t) {
  444. if (t->node != dummynode)
  445. luaM_freearray(L, t->node, sizenode(t), Node);
  446. luaM_freearray(L, t->array, t->sizearray, TValue);
  447. luaM_free(L, t);
  448. }
  449. /*
  450. ** inserts a new key into a hash table; first, check whether key's main
  451. ** position is free. If not, check whether colliding node is in its main
  452. ** position or not: if it is not, move colliding node to an empty place and
  453. ** put new key in its main position; otherwise (colliding node is in its main
  454. ** position), new key goes to an empty position.
  455. */
  456. static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
  457. Node *mp = mainposition(t, key);
  458. if (!ttisnil(gval(mp)) || mp == dummynode) {
  459. Node *othern;
  460. Node *n = getfreepos(t); /* get a free place */
  461. if (n == NULL) { /* cannot find a free place? */
  462. rehash(L, t, key); /* grow table */
  463. return luaH_set(L, t, key); /* re-insert key into grown table */
  464. }
  465. lua_assert(n != dummynode);
  466. othern = mainposition(t, key2tval(mp));
  467. if (othern != mp) { /* is colliding node out of its main position? */
  468. /* yes; move colliding node into free position */
  469. while (gnext(othern) != mp) othern = gnext(othern); /* find previous */
  470. gnext(othern) = n; /* redo the chain with `n' in place of `mp' */
  471. *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
  472. gnext(mp) = NULL; /* now `mp' is free */
  473. setnilvalue(gval(mp));
  474. }
  475. else { /* colliding node is in its own main position */
  476. /* new node will go into free position */
  477. gnext(n) = gnext(mp); /* chain new position */
  478. gnext(mp) = n;
  479. mp = n;
  480. }
  481. }
  482. setobj2t(L, gkey(mp), key);
  483. luaC_barriert(L, t, key);
  484. lua_assert(ttisnil(gval(mp)));
  485. return gval(mp);
  486. }
  487. /*
  488. ** search function for integers
  489. */
  490. const TValue *luaH_getnum (Table *t, int key) {
  491. /* (1 <= key && key <= t->sizearray) */
  492. if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray))
  493. return &t->array[key-1];
  494. else {
  495. lua_Number nk = cast_num(key);
  496. Node *n = hashnum(t, nk);
  497. do { /* check whether `key' is somewhere in the chain */
  498. if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk))
  499. return gval(n); /* that's it */
  500. else n = gnext(n);
  501. } while (n);
  502. return luaO_nilobject;
  503. }
  504. }
  505. /* same thing for rotables */
  506. const TValue *luaH_getnum_ro (void *t, int key) {
  507. const TValue *res = NULL; // integer values not supported: luaR_findentryN(t, key, NULL);
  508. return res ? res : luaO_nilobject;
  509. }
  510. /*
  511. ** search function for strings
  512. */
  513. const TValue *luaH_getstr (Table *t, TString *key) {
  514. Node *n = hashstr(t, key);
  515. do { /* check whether `key' is somewhere in the chain */
  516. if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key)
  517. return gval(n); /* that's it */
  518. else n = gnext(n);
  519. } while (n);
  520. return luaO_nilobject;
  521. }
  522. /* same thing for rotables */
  523. const TValue *luaH_getstr_ro (void *t, TString *key) {
  524. if (!t || key->tsv.len>LUA_MAX_ROTABLE_NAME)
  525. return luaO_nilobject;
  526. return luaR_findentry(t, key, NULL);
  527. }
  528. /*
  529. ** main search function
  530. */
  531. const TValue *luaH_get (Table *t, const TValue *key) {
  532. switch (ttype(key)) {
  533. case LUA_TNIL: return luaO_nilobject;
  534. case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
  535. case LUA_TNUMBER: {
  536. int k;
  537. lua_Number n = nvalue(key);
  538. lua_number2int(k, n);
  539. if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */
  540. return luaH_getnum(t, k); /* use specialized version */
  541. /* else go through */
  542. }
  543. default: {
  544. Node *n = mainposition(t, key);
  545. do { /* check whether `key' is somewhere in the chain */
  546. if (luaO_rawequalObj(key2tval(n), key))
  547. return gval(n); /* that's it */
  548. else n = gnext(n);
  549. } while (n);
  550. return luaO_nilobject;
  551. }
  552. }
  553. }
  554. /* same thing for rotables */
  555. const TValue *luaH_get_ro (void *t, const TValue *key) {
  556. switch (ttype(key)) {
  557. case LUA_TNIL: return luaO_nilobject;
  558. case LUA_TSTRING: return luaH_getstr_ro(t, rawtsvalue(key));
  559. case LUA_TNUMBER: {
  560. int k;
  561. lua_Number n = nvalue(key);
  562. lua_number2int(k, n);
  563. if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */
  564. return luaH_getnum_ro(t, k); /* use specialized version */
  565. /* else go through */
  566. }
  567. default: {
  568. return luaO_nilobject;
  569. }
  570. }
  571. }
  572. TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
  573. const TValue *p = luaH_get(t, key);
  574. t->flags = 0;
  575. if (p != luaO_nilobject)
  576. return cast(TValue *, p);
  577. else {
  578. if (ttisnil(key)) luaG_runerror(L, "table index is nil");
  579. else if (ttisnumber(key) && luai_numisnan(nvalue(key)))
  580. luaG_runerror(L, "table index is NaN");
  581. return newkey(L, t, key);
  582. }
  583. }
  584. TValue *luaH_setnum (lua_State *L, Table *t, int key) {
  585. const TValue *p = luaH_getnum(t, key);
  586. if (p != luaO_nilobject)
  587. return cast(TValue *, p);
  588. else {
  589. TValue k;
  590. setnvalue(&k, cast_num(key));
  591. return newkey(L, t, &k);
  592. }
  593. }
  594. TValue *luaH_setstr (lua_State *L, Table *t, TString *key) {
  595. const TValue *p = luaH_getstr(t, key);
  596. if (p != luaO_nilobject)
  597. return cast(TValue *, p);
  598. else {
  599. TValue k;
  600. setsvalue(L, &k, key);
  601. return newkey(L, t, &k);
  602. }
  603. }
  604. static int unbound_search (Table *t, unsigned int j) {
  605. unsigned int i = j; /* i is zero or a present index */
  606. j++;
  607. /* find `i' and `j' such that i is present and j is not */
  608. while (!ttisnil(luaH_getnum(t, j))) {
  609. i = j;
  610. j *= 2;
  611. if (j > cast(unsigned int, MAX_INT)) { /* overflow? */
  612. /* table was built with bad purposes: resort to linear search */
  613. i = 1;
  614. while (!ttisnil(luaH_getnum(t, i))) i++;
  615. return i - 1;
  616. }
  617. }
  618. /* now do a binary search between them */
  619. while (j - i > 1) {
  620. unsigned int m = (i+j)/2;
  621. if (ttisnil(luaH_getnum(t, m))) j = m;
  622. else i = m;
  623. }
  624. return i;
  625. }
  626. /*
  627. ** Try to find a boundary in table `t'. A `boundary' is an integer index
  628. ** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
  629. */
  630. int luaH_getn (Table *t) {
  631. unsigned int j = t->sizearray;
  632. if (j > 0 && ttisnil(&t->array[j - 1])) {
  633. /* there is a boundary in the array part: (binary) search for it */
  634. unsigned int i = 0;
  635. while (j - i > 1) {
  636. unsigned int m = (i+j)/2;
  637. if (ttisnil(&t->array[m - 1])) j = m;
  638. else i = m;
  639. }
  640. return i;
  641. }
  642. /* else must find a boundary in hash part */
  643. else if (t->node == dummynode) /* hash part is empty? */
  644. return j; /* that is easy... */
  645. else return unbound_search(t, j);
  646. }
  647. /* same thing for rotables */
  648. int luaH_getn_ro (void *t) {
  649. return 0; // Integer Keys are not currently supported for ROTables
  650. }
  651. int luaH_isdummy (Node *n) { return n == dummynode; }
  652. #if defined(LUA_DEBUG)
  653. Node *luaH_mainposition (const Table *t, const TValue *key) {
  654. return mainposition(t, key);
  655. }
  656. #endif