diff src/lua/ltable.c @ 11:27763b933818

raise lua sources up one level
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 11:07:39 -0600
parents src/lua/src/ltable.c@f9f4f1b99eed
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/lua/ltable.c	Sat Mar 03 11:07:39 2012 -0600
     1.3 @@ -0,0 +1,588 @@
     1.4 +/*
     1.5 +** $Id: ltable.c,v 2.32.1.2 2007/12/28 15:32:23 roberto Exp $
     1.6 +** Lua tables (hash)
     1.7 +** See Copyright Notice in lua.h
     1.8 +*/
     1.9 +
    1.10 +
    1.11 +/*
    1.12 +** Implementation of tables (aka arrays, objects, or hash tables).
    1.13 +** Tables keep its elements in two parts: an array part and a hash part.
    1.14 +** Non-negative integer keys are all candidates to be kept in the array
    1.15 +** part. The actual size of the array is the largest `n' such that at
    1.16 +** least half the slots between 0 and n are in use.
    1.17 +** Hash uses a mix of chained scatter table with Brent's variation.
    1.18 +** A main invariant of these tables is that, if an element is not
    1.19 +** in its main position (i.e. the `original' position that its hash gives
    1.20 +** to it), then the colliding element is in its own main position.
    1.21 +** Hence even when the load factor reaches 100%, performance remains good.
    1.22 +*/
    1.23 +
    1.24 +#include <math.h>
    1.25 +#include <string.h>
    1.26 +
    1.27 +#define ltable_c
    1.28 +#define LUA_CORE
    1.29 +
    1.30 +#include "lua.h"
    1.31 +
    1.32 +#include "ldebug.h"
    1.33 +#include "ldo.h"
    1.34 +#include "lgc.h"
    1.35 +#include "lmem.h"
    1.36 +#include "lobject.h"
    1.37 +#include "lstate.h"
    1.38 +#include "ltable.h"
    1.39 +
    1.40 +
    1.41 +/*
    1.42 +** max size of array part is 2^MAXBITS
    1.43 +*/
    1.44 +#if LUAI_BITSINT > 26
    1.45 +#define MAXBITS		26
    1.46 +#else
    1.47 +#define MAXBITS		(LUAI_BITSINT-2)
    1.48 +#endif
    1.49 +
    1.50 +#define MAXASIZE	(1 << MAXBITS)
    1.51 +
    1.52 +
    1.53 +#define hashpow2(t,n)      (gnode(t, lmod((n), sizenode(t))))
    1.54 +  
    1.55 +#define hashstr(t,str)  hashpow2(t, (str)->tsv.hash)
    1.56 +#define hashboolean(t,p)        hashpow2(t, p)
    1.57 +
    1.58 +
    1.59 +/*
    1.60 +** for some types, it is better to avoid modulus by power of 2, as
    1.61 +** they tend to have many 2 factors.
    1.62 +*/
    1.63 +#define hashmod(t,n)	(gnode(t, ((n) % ((sizenode(t)-1)|1))))
    1.64 +
    1.65 +
    1.66 +#define hashpointer(t,p)	hashmod(t, IntPoint(p))
    1.67 +
    1.68 +
    1.69 +/*
    1.70 +** number of ints inside a lua_Number
    1.71 +*/
    1.72 +#define numints		cast_int(sizeof(lua_Number)/sizeof(int))
    1.73 +
    1.74 +
    1.75 +
    1.76 +#define dummynode		(&dummynode_)
    1.77 +
    1.78 +static const Node dummynode_ = {
    1.79 +  {{NULL}, LUA_TNIL},  /* value */
    1.80 +  {{{NULL}, LUA_TNIL, NULL}}  /* key */
    1.81 +};
    1.82 +
    1.83 +
    1.84 +/*
    1.85 +** hash for lua_Numbers
    1.86 +*/
    1.87 +static Node *hashnum (const Table *t, lua_Number n) {
    1.88 +  unsigned int a[numints];
    1.89 +  int i;
    1.90 +  if (luai_numeq(n, 0))  /* avoid problems with -0 */
    1.91 +    return gnode(t, 0);
    1.92 +  memcpy(a, &n, sizeof(a));
    1.93 +  for (i = 1; i < numints; i++) a[0] += a[i];
    1.94 +  return hashmod(t, a[0]);
    1.95 +}
    1.96 +
    1.97 +
    1.98 +
    1.99 +/*
   1.100 +** returns the `main' position of an element in a table (that is, the index
   1.101 +** of its hash value)
   1.102 +*/
   1.103 +static Node *mainposition (const Table *t, const TValue *key) {
   1.104 +  switch (ttype(key)) {
   1.105 +    case LUA_TNUMBER:
   1.106 +      return hashnum(t, nvalue(key));
   1.107 +    case LUA_TSTRING:
   1.108 +      return hashstr(t, rawtsvalue(key));
   1.109 +    case LUA_TBOOLEAN:
   1.110 +      return hashboolean(t, bvalue(key));
   1.111 +    case LUA_TLIGHTUSERDATA:
   1.112 +      return hashpointer(t, pvalue(key));
   1.113 +    default:
   1.114 +      return hashpointer(t, gcvalue(key));
   1.115 +  }
   1.116 +}
   1.117 +
   1.118 +
   1.119 +/*
   1.120 +** returns the index for `key' if `key' is an appropriate key to live in
   1.121 +** the array part of the table, -1 otherwise.
   1.122 +*/
   1.123 +static int arrayindex (const TValue *key) {
   1.124 +  if (ttisnumber(key)) {
   1.125 +    lua_Number n = nvalue(key);
   1.126 +    int k;
   1.127 +    lua_number2int(k, n);
   1.128 +    if (luai_numeq(cast_num(k), n))
   1.129 +      return k;
   1.130 +  }
   1.131 +  return -1;  /* `key' did not match some condition */
   1.132 +}
   1.133 +
   1.134 +
   1.135 +/*
   1.136 +** returns the index of a `key' for table traversals. First goes all
   1.137 +** elements in the array part, then elements in the hash part. The
   1.138 +** beginning of a traversal is signalled by -1.
   1.139 +*/
   1.140 +static int findindex (lua_State *L, Table *t, StkId key) {
   1.141 +  int i;
   1.142 +  if (ttisnil(key)) return -1;  /* first iteration */
   1.143 +  i = arrayindex(key);
   1.144 +  if (0 < i && i <= t->sizearray)  /* is `key' inside array part? */
   1.145 +    return i-1;  /* yes; that's the index (corrected to C) */
   1.146 +  else {
   1.147 +    Node *n = mainposition(t, key);
   1.148 +    do {  /* check whether `key' is somewhere in the chain */
   1.149 +      /* key may be dead already, but it is ok to use it in `next' */
   1.150 +      if (luaO_rawequalObj(key2tval(n), key) ||
   1.151 +            (ttype(gkey(n)) == LUA_TDEADKEY && iscollectable(key) &&
   1.152 +             gcvalue(gkey(n)) == gcvalue(key))) {
   1.153 +        i = cast_int(n - gnode(t, 0));  /* key index in hash table */
   1.154 +        /* hash elements are numbered after array ones */
   1.155 +        return i + t->sizearray;
   1.156 +      }
   1.157 +      else n = gnext(n);
   1.158 +    } while (n);
   1.159 +    luaG_runerror(L, "invalid key to " LUA_QL("next"));  /* key not found */
   1.160 +    return 0;  /* to avoid warnings */
   1.161 +  }
   1.162 +}
   1.163 +
   1.164 +
   1.165 +int luaH_next (lua_State *L, Table *t, StkId key) {
   1.166 +  int i = findindex(L, t, key);  /* find original element */
   1.167 +  for (i++; i < t->sizearray; i++) {  /* try first array part */
   1.168 +    if (!ttisnil(&t->array[i])) {  /* a non-nil value? */
   1.169 +      setnvalue(key, cast_num(i+1));
   1.170 +      setobj2s(L, key+1, &t->array[i]);
   1.171 +      return 1;
   1.172 +    }
   1.173 +  }
   1.174 +  for (i -= t->sizearray; i < sizenode(t); i++) {  /* then hash part */
   1.175 +    if (!ttisnil(gval(gnode(t, i)))) {  /* a non-nil value? */
   1.176 +      setobj2s(L, key, key2tval(gnode(t, i)));
   1.177 +      setobj2s(L, key+1, gval(gnode(t, i)));
   1.178 +      return 1;
   1.179 +    }
   1.180 +  }
   1.181 +  return 0;  /* no more elements */
   1.182 +}
   1.183 +
   1.184 +
   1.185 +/*
   1.186 +** {=============================================================
   1.187 +** Rehash
   1.188 +** ==============================================================
   1.189 +*/
   1.190 +
   1.191 +
   1.192 +static int computesizes (int nums[], int *narray) {
   1.193 +  int i;
   1.194 +  int twotoi;  /* 2^i */
   1.195 +  int a = 0;  /* number of elements smaller than 2^i */
   1.196 +  int na = 0;  /* number of elements to go to array part */
   1.197 +  int n = 0;  /* optimal size for array part */
   1.198 +  for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) {
   1.199 +    if (nums[i] > 0) {
   1.200 +      a += nums[i];
   1.201 +      if (a > twotoi/2) {  /* more than half elements present? */
   1.202 +        n = twotoi;  /* optimal size (till now) */
   1.203 +        na = a;  /* all elements smaller than n will go to array part */
   1.204 +      }
   1.205 +    }
   1.206 +    if (a == *narray) break;  /* all elements already counted */
   1.207 +  }
   1.208 +  *narray = n;
   1.209 +  lua_assert(*narray/2 <= na && na <= *narray);
   1.210 +  return na;
   1.211 +}
   1.212 +
   1.213 +
   1.214 +static int countint (const TValue *key, int *nums) {
   1.215 +  int k = arrayindex(key);
   1.216 +  if (0 < k && k <= MAXASIZE) {  /* is `key' an appropriate array index? */
   1.217 +    nums[ceillog2(k)]++;  /* count as such */
   1.218 +    return 1;
   1.219 +  }
   1.220 +  else
   1.221 +    return 0;
   1.222 +}
   1.223 +
   1.224 +
   1.225 +static int numusearray (const Table *t, int *nums) {
   1.226 +  int lg;
   1.227 +  int ttlg;  /* 2^lg */
   1.228 +  int ause = 0;  /* summation of `nums' */
   1.229 +  int i = 1;  /* count to traverse all array keys */
   1.230 +  for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) {  /* for each slice */
   1.231 +    int lc = 0;  /* counter */
   1.232 +    int lim = ttlg;
   1.233 +    if (lim > t->sizearray) {
   1.234 +      lim = t->sizearray;  /* adjust upper limit */
   1.235 +      if (i > lim)
   1.236 +        break;  /* no more elements to count */
   1.237 +    }
   1.238 +    /* count elements in range (2^(lg-1), 2^lg] */
   1.239 +    for (; i <= lim; i++) {
   1.240 +      if (!ttisnil(&t->array[i-1]))
   1.241 +        lc++;
   1.242 +    }
   1.243 +    nums[lg] += lc;
   1.244 +    ause += lc;
   1.245 +  }
   1.246 +  return ause;
   1.247 +}
   1.248 +
   1.249 +
   1.250 +static int numusehash (const Table *t, int *nums, int *pnasize) {
   1.251 +  int totaluse = 0;  /* total number of elements */
   1.252 +  int ause = 0;  /* summation of `nums' */
   1.253 +  int i = sizenode(t);
   1.254 +  while (i--) {
   1.255 +    Node *n = &t->node[i];
   1.256 +    if (!ttisnil(gval(n))) {
   1.257 +      ause += countint(key2tval(n), nums);
   1.258 +      totaluse++;
   1.259 +    }
   1.260 +  }
   1.261 +  *pnasize += ause;
   1.262 +  return totaluse;
   1.263 +}
   1.264 +
   1.265 +
   1.266 +static void setarrayvector (lua_State *L, Table *t, int size) {
   1.267 +  int i;
   1.268 +  luaM_reallocvector(L, t->array, t->sizearray, size, TValue);
   1.269 +  for (i=t->sizearray; i<size; i++)
   1.270 +     setnilvalue(&t->array[i]);
   1.271 +  t->sizearray = size;
   1.272 +}
   1.273 +
   1.274 +
   1.275 +static void setnodevector (lua_State *L, Table *t, int size) {
   1.276 +  int lsize;
   1.277 +  if (size == 0) {  /* no elements to hash part? */
   1.278 +    t->node = cast(Node *, dummynode);  /* use common `dummynode' */
   1.279 +    lsize = 0;
   1.280 +  }
   1.281 +  else {
   1.282 +    int i;
   1.283 +    lsize = ceillog2(size);
   1.284 +    if (lsize > MAXBITS)
   1.285 +      luaG_runerror(L, "table overflow");
   1.286 +    size = twoto(lsize);
   1.287 +    t->node = luaM_newvector(L, size, Node);
   1.288 +    for (i=0; i<size; i++) {
   1.289 +      Node *n = gnode(t, i);
   1.290 +      gnext(n) = NULL;
   1.291 +      setnilvalue(gkey(n));
   1.292 +      setnilvalue(gval(n));
   1.293 +    }
   1.294 +  }
   1.295 +  t->lsizenode = cast_byte(lsize);
   1.296 +  t->lastfree = gnode(t, size);  /* all positions are free */
   1.297 +}
   1.298 +
   1.299 +
   1.300 +static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
   1.301 +  int i;
   1.302 +  int oldasize = t->sizearray;
   1.303 +  int oldhsize = t->lsizenode;
   1.304 +  Node *nold = t->node;  /* save old hash ... */
   1.305 +  if (nasize > oldasize)  /* array part must grow? */
   1.306 +    setarrayvector(L, t, nasize);
   1.307 +  /* create new hash part with appropriate size */
   1.308 +  setnodevector(L, t, nhsize);  
   1.309 +  if (nasize < oldasize) {  /* array part must shrink? */
   1.310 +    t->sizearray = nasize;
   1.311 +    /* re-insert elements from vanishing slice */
   1.312 +    for (i=nasize; i<oldasize; i++) {
   1.313 +      if (!ttisnil(&t->array[i]))
   1.314 +        setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]);
   1.315 +    }
   1.316 +    /* shrink array */
   1.317 +    luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
   1.318 +  }
   1.319 +  /* re-insert elements from hash part */
   1.320 +  for (i = twoto(oldhsize) - 1; i >= 0; i--) {
   1.321 +    Node *old = nold+i;
   1.322 +    if (!ttisnil(gval(old)))
   1.323 +      setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old));
   1.324 +  }
   1.325 +  if (nold != dummynode)
   1.326 +    luaM_freearray(L, nold, twoto(oldhsize), Node);  /* free old array */
   1.327 +}
   1.328 +
   1.329 +
   1.330 +void luaH_resizearray (lua_State *L, Table *t, int nasize) {
   1.331 +  int nsize = (t->node == dummynode) ? 0 : sizenode(t);
   1.332 +  resize(L, t, nasize, nsize);
   1.333 +}
   1.334 +
   1.335 +
   1.336 +static void rehash (lua_State *L, Table *t, const TValue *ek) {
   1.337 +  int nasize, na;
   1.338 +  int nums[MAXBITS+1];  /* nums[i] = number of keys between 2^(i-1) and 2^i */
   1.339 +  int i;
   1.340 +  int totaluse;
   1.341 +  for (i=0; i<=MAXBITS; i++) nums[i] = 0;  /* reset counts */
   1.342 +  nasize = numusearray(t, nums);  /* count keys in array part */
   1.343 +  totaluse = nasize;  /* all those keys are integer keys */
   1.344 +  totaluse += numusehash(t, nums, &nasize);  /* count keys in hash part */
   1.345 +  /* count extra key */
   1.346 +  nasize += countint(ek, nums);
   1.347 +  totaluse++;
   1.348 +  /* compute new size for array part */
   1.349 +  na = computesizes(nums, &nasize);
   1.350 +  /* resize the table to new computed sizes */
   1.351 +  resize(L, t, nasize, totaluse - na);
   1.352 +}
   1.353 +
   1.354 +
   1.355 +
   1.356 +/*
   1.357 +** }=============================================================
   1.358 +*/
   1.359 +
   1.360 +
   1.361 +Table *luaH_new (lua_State *L, int narray, int nhash) {
   1.362 +  Table *t = luaM_new(L, Table);
   1.363 +  luaC_link(L, obj2gco(t), LUA_TTABLE);
   1.364 +  t->metatable = NULL;
   1.365 +  t->flags = cast_byte(~0);
   1.366 +  /* temporary values (kept only if some malloc fails) */
   1.367 +  t->array = NULL;
   1.368 +  t->sizearray = 0;
   1.369 +  t->lsizenode = 0;
   1.370 +  t->node = cast(Node *, dummynode);
   1.371 +  setarrayvector(L, t, narray);
   1.372 +  setnodevector(L, t, nhash);
   1.373 +  return t;
   1.374 +}
   1.375 +
   1.376 +
   1.377 +void luaH_free (lua_State *L, Table *t) {
   1.378 +  if (t->node != dummynode)
   1.379 +    luaM_freearray(L, t->node, sizenode(t), Node);
   1.380 +  luaM_freearray(L, t->array, t->sizearray, TValue);
   1.381 +  luaM_free(L, t);
   1.382 +}
   1.383 +
   1.384 +
   1.385 +static Node *getfreepos (Table *t) {
   1.386 +  while (t->lastfree-- > t->node) {
   1.387 +    if (ttisnil(gkey(t->lastfree)))
   1.388 +      return t->lastfree;
   1.389 +  }
   1.390 +  return NULL;  /* could not find a free place */
   1.391 +}
   1.392 +
   1.393 +
   1.394 +
   1.395 +/*
   1.396 +** inserts a new key into a hash table; first, check whether key's main 
   1.397 +** position is free. If not, check whether colliding node is in its main 
   1.398 +** position or not: if it is not, move colliding node to an empty place and 
   1.399 +** put new key in its main position; otherwise (colliding node is in its main 
   1.400 +** position), new key goes to an empty position. 
   1.401 +*/
   1.402 +static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
   1.403 +  Node *mp = mainposition(t, key);
   1.404 +  if (!ttisnil(gval(mp)) || mp == dummynode) {
   1.405 +    Node *othern;
   1.406 +    Node *n = getfreepos(t);  /* get a free place */
   1.407 +    if (n == NULL) {  /* cannot find a free place? */
   1.408 +      rehash(L, t, key);  /* grow table */
   1.409 +      return luaH_set(L, t, key);  /* re-insert key into grown table */
   1.410 +    }
   1.411 +    lua_assert(n != dummynode);
   1.412 +    othern = mainposition(t, key2tval(mp));
   1.413 +    if (othern != mp) {  /* is colliding node out of its main position? */
   1.414 +      /* yes; move colliding node into free position */
   1.415 +      while (gnext(othern) != mp) othern = gnext(othern);  /* find previous */
   1.416 +      gnext(othern) = n;  /* redo the chain with `n' in place of `mp' */
   1.417 +      *n = *mp;  /* copy colliding node into free pos. (mp->next also goes) */
   1.418 +      gnext(mp) = NULL;  /* now `mp' is free */
   1.419 +      setnilvalue(gval(mp));
   1.420 +    }
   1.421 +    else {  /* colliding node is in its own main position */
   1.422 +      /* new node will go into free position */
   1.423 +      gnext(n) = gnext(mp);  /* chain new position */
   1.424 +      gnext(mp) = n;
   1.425 +      mp = n;
   1.426 +    }
   1.427 +  }
   1.428 +  gkey(mp)->value = key->value; gkey(mp)->tt = key->tt;
   1.429 +  luaC_barriert(L, t, key);
   1.430 +  lua_assert(ttisnil(gval(mp)));
   1.431 +  return gval(mp);
   1.432 +}
   1.433 +
   1.434 +
   1.435 +/*
   1.436 +** search function for integers
   1.437 +*/
   1.438 +const TValue *luaH_getnum (Table *t, int key) {
   1.439 +  /* (1 <= key && key <= t->sizearray) */
   1.440 +  if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray))
   1.441 +    return &t->array[key-1];
   1.442 +  else {
   1.443 +    lua_Number nk = cast_num(key);
   1.444 +    Node *n = hashnum(t, nk);
   1.445 +    do {  /* check whether `key' is somewhere in the chain */
   1.446 +      if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk))
   1.447 +        return gval(n);  /* that's it */
   1.448 +      else n = gnext(n);
   1.449 +    } while (n);
   1.450 +    return luaO_nilobject;
   1.451 +  }
   1.452 +}
   1.453 +
   1.454 +
   1.455 +/*
   1.456 +** search function for strings
   1.457 +*/
   1.458 +const TValue *luaH_getstr (Table *t, TString *key) {
   1.459 +  Node *n = hashstr(t, key);
   1.460 +  do {  /* check whether `key' is somewhere in the chain */
   1.461 +    if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key)
   1.462 +      return gval(n);  /* that's it */
   1.463 +    else n = gnext(n);
   1.464 +  } while (n);
   1.465 +  return luaO_nilobject;
   1.466 +}
   1.467 +
   1.468 +
   1.469 +/*
   1.470 +** main search function
   1.471 +*/
   1.472 +const TValue *luaH_get (Table *t, const TValue *key) {
   1.473 +  switch (ttype(key)) {
   1.474 +    case LUA_TNIL: return luaO_nilobject;
   1.475 +    case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
   1.476 +    case LUA_TNUMBER: {
   1.477 +      int k;
   1.478 +      lua_Number n = nvalue(key);
   1.479 +      lua_number2int(k, n);
   1.480 +      if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */
   1.481 +        return luaH_getnum(t, k);  /* use specialized version */
   1.482 +      /* else go through */
   1.483 +    }
   1.484 +    default: {
   1.485 +      Node *n = mainposition(t, key);
   1.486 +      do {  /* check whether `key' is somewhere in the chain */
   1.487 +        if (luaO_rawequalObj(key2tval(n), key))
   1.488 +          return gval(n);  /* that's it */
   1.489 +        else n = gnext(n);
   1.490 +      } while (n);
   1.491 +      return luaO_nilobject;
   1.492 +    }
   1.493 +  }
   1.494 +}
   1.495 +
   1.496 +
   1.497 +TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
   1.498 +  const TValue *p = luaH_get(t, key);
   1.499 +  t->flags = 0;
   1.500 +  if (p != luaO_nilobject)
   1.501 +    return cast(TValue *, p);
   1.502 +  else {
   1.503 +    if (ttisnil(key)) luaG_runerror(L, "table index is nil");
   1.504 +    else if (ttisnumber(key) && luai_numisnan(nvalue(key)))
   1.505 +      luaG_runerror(L, "table index is NaN");
   1.506 +    return newkey(L, t, key);
   1.507 +  }
   1.508 +}
   1.509 +
   1.510 +
   1.511 +TValue *luaH_setnum (lua_State *L, Table *t, int key) {
   1.512 +  const TValue *p = luaH_getnum(t, key);
   1.513 +  if (p != luaO_nilobject)
   1.514 +    return cast(TValue *, p);
   1.515 +  else {
   1.516 +    TValue k;
   1.517 +    setnvalue(&k, cast_num(key));
   1.518 +    return newkey(L, t, &k);
   1.519 +  }
   1.520 +}
   1.521 +
   1.522 +
   1.523 +TValue *luaH_setstr (lua_State *L, Table *t, TString *key) {
   1.524 +  const TValue *p = luaH_getstr(t, key);
   1.525 +  if (p != luaO_nilobject)
   1.526 +    return cast(TValue *, p);
   1.527 +  else {
   1.528 +    TValue k;
   1.529 +    setsvalue(L, &k, key);
   1.530 +    return newkey(L, t, &k);
   1.531 +  }
   1.532 +}
   1.533 +
   1.534 +
   1.535 +static int unbound_search (Table *t, unsigned int j) {
   1.536 +  unsigned int i = j;  /* i is zero or a present index */
   1.537 +  j++;
   1.538 +  /* find `i' and `j' such that i is present and j is not */
   1.539 +  while (!ttisnil(luaH_getnum(t, j))) {
   1.540 +    i = j;
   1.541 +    j *= 2;
   1.542 +    if (j > cast(unsigned int, MAX_INT)) {  /* overflow? */
   1.543 +      /* table was built with bad purposes: resort to linear search */
   1.544 +      i = 1;
   1.545 +      while (!ttisnil(luaH_getnum(t, i))) i++;
   1.546 +      return i - 1;
   1.547 +    }
   1.548 +  }
   1.549 +  /* now do a binary search between them */
   1.550 +  while (j - i > 1) {
   1.551 +    unsigned int m = (i+j)/2;
   1.552 +    if (ttisnil(luaH_getnum(t, m))) j = m;
   1.553 +    else i = m;
   1.554 +  }
   1.555 +  return i;
   1.556 +}
   1.557 +
   1.558 +
   1.559 +/*
   1.560 +** Try to find a boundary in table `t'. A `boundary' is an integer index
   1.561 +** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
   1.562 +*/
   1.563 +int luaH_getn (Table *t) {
   1.564 +  unsigned int j = t->sizearray;
   1.565 +  if (j > 0 && ttisnil(&t->array[j - 1])) {
   1.566 +    /* there is a boundary in the array part: (binary) search for it */
   1.567 +    unsigned int i = 0;
   1.568 +    while (j - i > 1) {
   1.569 +      unsigned int m = (i+j)/2;
   1.570 +      if (ttisnil(&t->array[m - 1])) j = m;
   1.571 +      else i = m;
   1.572 +    }
   1.573 +    return i;
   1.574 +  }
   1.575 +  /* else must find a boundary in hash part */
   1.576 +  else if (t->node == dummynode)  /* hash part is empty? */
   1.577 +    return j;  /* that is easy... */
   1.578 +  else return unbound_search(t, j);
   1.579 +}
   1.580 +
   1.581 +
   1.582 +
   1.583 +#if defined(LUA_DEBUG)
   1.584 +
   1.585 +Node *luaH_mainposition (const Table *t, const TValue *key) {
   1.586 +  return mainposition(t, key);
   1.587 +}
   1.588 +
   1.589 +int luaH_isdummy (Node *n) { return n == dummynode; }
   1.590 +
   1.591 +#endif