annotate src/lua/ltable.c @ 589:d9d86f6018e8

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