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