Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
ltable.c File Reference
#include "lprefix.h"
#include <math.h>
#include <limits.h>
#include "lua.h"
#include "ldebug.h"
#include "ldo.h"
#include "lgc.h"
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "lvm.h"

Go to the source code of this file.

Data Structures

struct  AuxsetnodeT
 

Macros

#define ltable_c
 
#define LUA_CORE
 
#define MAXABITS   cast_int(sizeof(int) * CHAR_BIT - 1)
 
#define MAXASIZE   (1u << MAXABITS)
 
#define MAXHBITS   (MAXABITS - 1)
 
#define hashpow2(t, n)   (gnode(t, lmod((n), sizenode(t))))
 
#define hashstr(t, str)   hashpow2(t, (str)->hash)
 
#define hashboolean(t, p)   hashpow2(t, p)
 
#define hashint(t, i)   hashpow2(t, i)
 
#define hashmod(t, n)   (gnode(t, ((n) % ((sizenode(t)-1)|1))))
 
#define hashpointer(t, p)   hashmod(t, point2uint(p))
 
#define dummynode   (&dummynode_)
 

Functions

static int l_hashfloat (lua_Number n)
 
static Nodemainposition (const Table *t, const TValue *key)
 
static unsigned int arrayindex (const TValue *key)
 
static unsigned int findindex (lua_State *L, Table *t, StkId key)
 
int luaH_next (lua_State *L, Table *t, StkId key)
 
static unsigned int computesizes (unsigned int nums[], unsigned int *pna)
 
static int countint (const TValue *key, unsigned int *nums)
 
static unsigned int numusearray (const Table *t, unsigned int *nums)
 
static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna)
 
static void setarrayvector (lua_State *L, Table *t, unsigned int size)
 
static void setnodevector (lua_State *L, Table *t, unsigned int size)
 
static void auxsetnode (lua_State *L, void *ud)
 
void luaH_resize (lua_State *L, Table *t, unsigned int nasize, unsigned int nhsize)
 
void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize)
 
static void rehash (lua_State *L, Table *t, const TValue *ek)
 
TableluaH_new (lua_State *L)
 
void luaH_free (lua_State *L, Table *t)
 
static Nodegetfreepos (Table *t)
 
TValueluaH_newkey (lua_State *L, Table *t, const TValue *key)
 
const TValueluaH_getint (Table *t, lua_Integer key)
 
const TValueluaH_getshortstr (Table *t, TString *key)
 
static const TValuegetgeneric (Table *t, const TValue *key)
 
const TValueluaH_getstr (Table *t, TString *key)
 
const TValueluaH_get (Table *t, const TValue *key)
 
TValueluaH_set (lua_State *L, Table *t, const TValue *key)
 
void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value)
 
static lua_Unsigned unbound_search (Table *t, lua_Unsigned j)
 
lua_Unsigned luaH_getn (Table *t)
 

Variables

static const Node dummynode_
 

Macro Definition Documentation

◆ dummynode

#define dummynode   (&dummynode_)

Definition at line 75 of file lua-5.3.6/src/ltable.c.

Referenced by setnodevector().

◆ hashboolean

#define hashboolean ( t,
p )   hashpow2(t, p)

Definition at line 61 of file lua-5.3.6/src/ltable.c.

Referenced by mainposition().

◆ hashint

#define hashint ( t,
i )   hashpow2(t, i)

Definition at line 62 of file lua-5.3.6/src/ltable.c.

Referenced by luaH_getint(), and mainposition().

◆ hashmod

#define hashmod ( t,
n )   (gnode(t, ((n) % ((sizenode(t)-1)|1))))

Definition at line 69 of file lua-5.3.6/src/ltable.c.

Referenced by mainposition().

◆ hashpointer

#define hashpointer ( t,
p )   hashmod(t, point2uint(p))

Definition at line 72 of file lua-5.3.6/src/ltable.c.

Referenced by mainposition().

◆ hashpow2

#define hashpow2 ( t,
n )   (gnode(t, lmod((n), sizenode(t))))

Definition at line 58 of file lua-5.3.6/src/ltable.c.

Referenced by mainposition().

◆ hashstr

#define hashstr ( t,
str )   hashpow2(t, (str)->hash)

Definition at line 60 of file lua-5.3.6/src/ltable.c.

Referenced by luaH_getshortstr(), and mainposition().

◆ ltable_c

#define ltable_c

Definition at line 7 of file lua-5.3.6/src/ltable.c.

◆ LUA_CORE

#define LUA_CORE

Definition at line 8 of file lua-5.3.6/src/ltable.c.

◆ MAXABITS

#define MAXABITS   cast_int(sizeof(int) * CHAR_BIT - 1)

Definition at line 46 of file lua-5.3.6/src/ltable.c.

Referenced by numusearray(), and rehash().

◆ MAXASIZE

#define MAXASIZE   (1u << MAXABITS)

Definition at line 47 of file lua-5.3.6/src/ltable.c.

Referenced by arrayindex().

◆ MAXHBITS

#define MAXHBITS   (MAXABITS - 1)

Definition at line 55 of file lua-5.3.6/src/ltable.c.

Referenced by setnodevector().

Function Documentation

◆ arrayindex()

static unsigned int arrayindex ( const TValue * key)
static

Definition at line 144 of file lua-5.3.6/src/ltable.c.

144 {
145 if (ttisinteger(key)) {
146 lua_Integer k = ivalue(key);
147 if (0 < k && (lua_Unsigned)k <= MAXASIZE)
148 return cast(unsigned int, k); /* 'key' is an appropriate array index */
149 }
150 return 0; /* 'key' did not match some condition */
151}
#define cast(t, exp)
LUA_INTEGER lua_Integer
LUA_UNSIGNED lua_Unsigned
#define ttisinteger(o)
#define ivalue(o)
#define MAXASIZE

References cast, ivalue, MAXASIZE, and ttisinteger.

Referenced by countint(), and findindex().

◆ auxsetnode()

static void auxsetnode ( lua_State * L,
void * ud )
static

Definition at line 341 of file lua-5.3.6/src/ltable.c.

341 {
342 AuxsetnodeT *asn = cast(AuxsetnodeT *, ud);
343 setnodevector(L, asn->t, asn->nhsize);
344}
static void setnodevector(lua_State *L, Table *t, unsigned int size)
unsigned int nhsize

References cast, AuxsetnodeT::nhsize, setnodevector(), and AuxsetnodeT::t.

Referenced by luaH_resize().

◆ computesizes()

static unsigned int computesizes ( unsigned int nums[],
unsigned int * pna )
static

Definition at line 219 of file lua-5.3.6/src/ltable.c.

219 {
220 int i;
221 unsigned int twotoi; /* 2^i (candidate for optimal size) */
222 unsigned int a = 0; /* number of elements smaller than 2^i */
223 unsigned int na = 0; /* number of elements to go to array part */
224 unsigned int optimal = 0; /* optimal size for array part */
225 /* loop while keys can fill more than half of total size */
226 for (i = 0, twotoi = 1;
227 twotoi > 0 && *pna > twotoi / 2;
228 i++, twotoi *= 2) {
229 if (nums[i] > 0) {
230 a += nums[i];
231 if (a > twotoi/2) { /* more than half elements present? */
232 optimal = twotoi; /* optimal size (till now) */
233 na = a; /* all elements up to 'optimal' will go to array part */
234 }
235 }
236 }
237 lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal);
238 *pna = na;
239 return optimal;
240}
#define lua_assert(c)

References lua_assert.

Referenced by rehash().

◆ countint()

static int countint ( const TValue * key,
unsigned int * nums )
static

Definition at line 243 of file lua-5.3.6/src/ltable.c.

243 {
244 unsigned int k = arrayindex(key);
245 if (k != 0) { /* is 'key' an appropriate array index? */
246 nums[luaO_ceillog2(k)]++; /* count as such */
247 return 1;
248 }
249 else
250 return 0;
251}
int luaO_ceillog2(unsigned int x)
static unsigned int arrayindex(const TValue *key)

References arrayindex(), and luaO_ceillog2().

Referenced by numusehash(), and rehash().

◆ findindex()

static unsigned int findindex ( lua_State * L,
Table * t,
StkId key )
static

Definition at line 159 of file lua-5.3.6/src/ltable.c.

159 {
160 unsigned int i;
161 if (ttisnil(key)) return 0; /* first iteration */
162 i = arrayindex(key);
163 if (i != 0 && i <= t->sizearray) /* is 'key' inside array part? */
164 return i; /* yes; that's the index */
165 else {
166 int nx;
167 Node *n = mainposition(t, key);
168 for (;;) { /* check whether 'key' is somewhere in the chain */
169 /* key may be dead already, but it is ok to use it in 'next' */
170 if (luaV_rawequalobj(gkey(n), key) ||
171 (ttisdeadkey(gkey(n)) && iscollectable(key) &&
172 deadvalue(gkey(n)) == gcvalue(key))) {
173 i = cast_int(n - gnode(t, 0)); /* key index in hash table */
174 /* hash elements are numbered after array ones */
175 return (i + 1) + t->sizearray;
176 }
177 nx = gnext(n);
178 if (nx == 0)
179 luaG_runerror(L, "invalid key to 'next'"); /* key not found */
180 else n += nx;
181 }
182 }
183}
void luaG_runerror(lua_State *L, const char *fmt,...)
#define cast_int(i)
#define gcvalue(o)
#define ttisnil(o)
#define iscollectable(o)
#define gnode(t, i)
#define gnext(n)
#define gkey(n)
#define ttisdeadkey(o)
#define deadvalue(o)
#define luaV_rawequalobj(o1, o2)
static Node * mainposition(const Table *t, const TValue *key)

References arrayindex(), cast_int, deadvalue, gcvalue, gkey, gnext, gnode, iscollectable, luaG_runerror(), luaV_rawequalobj, mainposition(), Table::sizearray, ttisdeadkey, and ttisnil.

Referenced by luaH_next().

◆ getfreepos()

static Node * getfreepos ( Table * t)
static

Definition at line 441 of file lua-5.3.6/src/ltable.c.

441 {
442 if (!isdummy(t)) {
443 while (t->lastfree > t->node) {
444 t->lastfree--;
445 if (ttisnil(gkey(t->lastfree)))
446 return t->lastfree;
447 }
448 }
449 return NULL; /* could not find a free place */
450}
#define NULL
Definition gmacros.h:924
#define isdummy(n)

References gkey, isdummy, Table::lastfree, Table::node, NULL, and ttisnil.

Referenced by luaH_newkey().

◆ getgeneric()

static const TValue * getgeneric ( Table * t,
const TValue * key )
static

Definition at line 560 of file lua-5.3.6/src/ltable.c.

560 {
561 Node *n = mainposition(t, key);
562 for (;;) { /* check whether 'key' is somewhere in the chain */
563 if (luaV_rawequalobj(gkey(n), key))
564 return gval(n); /* that's it */
565 else {
566 int nx = gnext(n);
567 if (nx == 0)
568 return luaO_nilobject; /* not found */
569 n += nx;
570 }
571 }
572}
#define luaO_nilobject
#define gval(n)

References gkey, gnext, gval, luaO_nilobject, luaV_rawequalobj, and mainposition().

Referenced by luaH_get(), and luaH_getstr().

◆ l_hashfloat()

static int l_hashfloat ( lua_Number n)
static

Definition at line 97 of file lua-5.3.6/src/ltable.c.

97 {
98 int i;
99 lua_Integer ni;
100 n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN);
101 if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */
102 lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL));
103 return 0;
104 }
105 else { /* normal case */
106 unsigned int u = cast(unsigned int, i) + cast(unsigned int, ni);
107 return cast_int(u <= cast(unsigned int, INT_MAX) ? u : ~u);
108 }
109}
#define cast_num(i)
#define l_mathop(x)
#define luai_numisnan(a)
#define lua_numbertointeger(n, p)

References cast, cast_int, cast_num, l_mathop, lua_assert, lua_numbertointeger, and luai_numisnan.

Referenced by mainposition().

◆ luaH_free()

void luaH_free ( lua_State * L,
Table * t )

Definition at line 433 of file lua-5.3.6/src/ltable.c.

433 {
434 if (!isdummy(t))
435 luaM_freearray(L, t->node, cast(size_t, sizenode(t)));
436 luaM_freearray(L, t->array, t->sizearray);
437 luaM_free(L, t);
438}
#define luaM_freearray(L, b, n, t)
#define luaM_free(L, b)
#define sizenode(t)

References Table::array, cast, isdummy, luaM_free, luaM_freearray, Table::node, Table::sizearray, and sizenode.

◆ luaH_get()

const TValue * luaH_get ( Table * t,
const TValue * key )

Definition at line 589 of file lua-5.3.6/src/ltable.c.

589 {
590 switch (ttype(key)) {
591 case LUA_TSHRSTR: return luaH_getshortstr(t, tsvalue(key));
592 case LUA_TNUMINT: return luaH_getint(t, ivalue(key));
593 case LUA_TNIL: return luaO_nilobject;
594 case LUA_TNUMFLT: {
595 lua_Integer k;
596 if (luaV_tointeger(key, &k, 0)) /* index is int? */
597 return luaH_getint(t, k); /* use specialized version */
598 /* else... */
599 } /* FALLTHROUGH */
600 default:
601 return getgeneric(t, key);
602 }
603}
#define tsvalue(o)
#define ttype(o)
#define LUA_TNIL
#define LUA_TSHRSTR
#define LUA_TNUMINT
#define LUA_TNUMFLT
static const TValue * getgeneric(Table *t, const TValue *key)
const TValue * luaH_getint(Table *t, lua_Integer key)
const TValue * luaH_getshortstr(Table *t, TString *key)
int luaV_tointeger(const TValue *obj, lua_Integer *p, int mode)

References getgeneric(), ivalue, LUA_TNIL, LUA_TNUMFLT, LUA_TNUMINT, LUA_TSHRSTR, luaH_getint(), luaH_getshortstr(), luaO_nilobject, luaV_tointeger(), tsvalue, and ttype.

Referenced by luaH_set().

◆ luaH_getint()

const TValue * luaH_getint ( Table * t,
lua_Integer key )

Definition at line 516 of file lua-5.3.6/src/ltable.c.

516 {
517 /* (1 <= key && key <= t->sizearray) */
518 if (l_castS2U(key) - 1 < t->sizearray)
519 return &t->array[key - 1];
520 else {
521 Node *n = hashint(t, key);
522 for (;;) { /* check whether 'key' is somewhere in the chain */
523 if (ttisinteger(gkey(n)) && ivalue(gkey(n)) == key)
524 return gval(n); /* that's it */
525 else {
526 int nx = gnext(n);
527 if (nx == 0) break;
528 n += nx;
529 }
530 }
531 return luaO_nilobject;
532 }
533}
#define l_castS2U(i)
#define hashint(t, i)

References Table::array, gkey, gnext, gval, hashint, ivalue, l_castS2U, luaO_nilobject, Table::sizearray, and ttisinteger.

Referenced by luaH_get(), luaH_setint(), and unbound_search().

◆ luaH_getn()

lua_Unsigned luaH_getn ( Table * t)

Definition at line 660 of file lua-5.3.6/src/ltable.c.

660 {
661 unsigned int j = t->sizearray;
662 if (j > 0 && ttisnil(&t->array[j - 1])) {
663 /* there is a boundary in the array part: (binary) search for it */
664 unsigned int i = 0;
665 while (j - i > 1) {
666 unsigned int m = (i+j)/2;
667 if (ttisnil(&t->array[m - 1])) j = m;
668 else i = m;
669 }
670 return i;
671 }
672 /* else must find a boundary in hash part */
673 else if (isdummy(t)) /* hash part is empty? */
674 return j; /* that is easy... */
675 else return unbound_search(t, j);
676}
static lua_Unsigned unbound_search(Table *t, lua_Unsigned j)

References Table::array, isdummy, Table::sizearray, ttisnil, and unbound_search().

◆ luaH_getshortstr()

const TValue * luaH_getshortstr ( Table * t,
TString * key )

Definition at line 539 of file lua-5.3.6/src/ltable.c.

539 {
540 Node *n = hashstr(t, key);
541 lua_assert(key->tt == LUA_TSHRSTR);
542 for (;;) { /* check whether 'key' is somewhere in the chain */
543 const TValue *k = gkey(n);
544 if (ttisshrstring(k) && eqshrstr(tsvalue(k), key))
545 return gval(n); /* that's it */
546 else {
547 int nx = gnext(n);
548 if (nx == 0)
549 return luaO_nilobject; /* not found */
550 n += nx;
551 }
552 }
553}
#define ttisshrstring(o)
#define eqshrstr(a, b)
#define hashstr(t, str)

References eqshrstr, gkey, gnext, gval, hashstr, lua_assert, LUA_TSHRSTR, luaO_nilobject, tsvalue, and ttisshrstring.

Referenced by luaH_get(), luaH_getstr(), luaT_gettm(), luaT_gettmbyobj(), luaT_objtypename(), and luaV_execute().

◆ luaH_getstr()

const TValue * luaH_getstr ( Table * t,
TString * key )

Definition at line 575 of file lua-5.3.6/src/ltable.c.

575 {
576 if (key->tt == LUA_TSHRSTR)
577 return luaH_getshortstr(t, key);
578 else { /* for long strings, use generic case */
579 TValue ko;
580 setsvalue(cast(lua_State *, NULL), &ko, key);
581 return getgeneric(t, &ko);
582 }
583}
#define setsvalue(L, obj, x)

References cast, getgeneric(), LUA_TSHRSTR, luaH_getshortstr(), NULL, and setsvalue.

◆ luaH_new()

Table * luaH_new ( lua_State * L)

Definition at line 421 of file lua-5.3.6/src/ltable.c.

421 {
422 GCObject *o = luaC_newobj(L, LUA_TTABLE, sizeof(Table));
423 Table *t = gco2t(o);
424 t->metatable = NULL;
425 t->flags = cast_byte(~0);
426 t->array = NULL;
427 t->sizearray = 0;
428 setnodevector(L, t, 0);
429 return t;
430}
#define cast_byte(i)
#define LUA_TTABLE
GCObject * luaC_newobj(lua_State *L, int tt, size_t sz, GCObject **list, int offset)
#define gco2t(o)
struct Table * metatable

References Table::array, cast_byte, Table::flags, gco2t, LUA_TTABLE, luaC_newobj(), Table::metatable, NULL, setnodevector(), and Table::sizearray.

◆ luaH_newkey()

TValue * luaH_newkey ( lua_State * L,
Table * t,
const TValue * key )

Definition at line 461 of file lua-5.3.6/src/ltable.c.

461 {
462 Node *mp;
463 TValue aux;
464 if (ttisnil(key)) luaG_runerror(L, "table index is nil");
465 else if (ttisfloat(key)) {
466 lua_Integer k;
467 if (luaV_tointeger(key, &k, 0)) { /* does index fit in an integer? */
468 setivalue(&aux, k);
469 key = &aux; /* insert it as an integer */
470 }
471 else if (luai_numisnan(fltvalue(key)))
472 luaG_runerror(L, "table index is NaN");
473 }
474 mp = mainposition(t, key);
475 if (!ttisnil(gval(mp)) || isdummy(t)) { /* main position is taken? */
476 Node *othern;
477 Node *f = getfreepos(t); /* get a free place */
478 if (f == NULL) { /* cannot find a free place? */
479 rehash(L, t, key); /* grow table */
480 /* whatever called 'newkey' takes care of TM cache */
481 return luaH_set(L, t, key); /* insert key into grown table */
482 }
483 lua_assert(!isdummy(t));
484 othern = mainposition(t, gkey(mp));
485 if (othern != mp) { /* is colliding node out of its main position? */
486 /* yes; move colliding node into free position */
487 while (othern + gnext(othern) != mp) /* find previous */
488 othern += gnext(othern);
489 gnext(othern) = cast_int(f - othern); /* rechain to point to 'f' */
490 *f = *mp; /* copy colliding node into free pos. (mp->next also goes) */
491 if (gnext(mp) != 0) {
492 gnext(f) += cast_int(mp - f); /* correct 'next' */
493 gnext(mp) = 0; /* now 'mp' is free */
494 }
495 setnilvalue(gval(mp));
496 }
497 else { /* colliding node is in its own main position */
498 /* new node will go into free position */
499 if (gnext(mp) != 0)
500 gnext(f) = cast_int((mp + gnext(mp)) - f); /* chain new position */
501 else lua_assert(gnext(f) == 0);
502 gnext(mp) = cast_int(f - mp);
503 mp = f;
504 }
505 }
506 setnodekey(L, &mp->i_key, key);
507 luaC_barrierback(L, t, key);
508 lua_assert(ttisnil(gval(mp)));
509 return gval(mp);
510}
#define setnilvalue(obj)
#define luaC_barrierback(L, p, v)
#define setnodekey(L, key, obj)
#define fltvalue(o)
#define ttisfloat(o)
#define setivalue(obj, x)
static Node * getfreepos(Table *t)
TValue * luaH_set(lua_State *L, Table *t, const TValue *key)
static void rehash(lua_State *L, Table *t, const TValue *ek)

References cast_int, fltvalue, getfreepos(), gkey, gnext, gval, Node::i_key, isdummy, lua_assert, luaC_barrierback, luaG_runerror(), luaH_set(), luai_numisnan, luaV_tointeger(), mainposition(), NULL, rehash(), setivalue, setnilvalue, setnodekey, ttisfloat, and ttisnil.

Referenced by luaH_set(), and luaH_setint().

◆ luaH_next()

int luaH_next ( lua_State * L,
Table * t,
StkId key )

Definition at line 186 of file lua-5.3.6/src/ltable.c.

186 {
187 unsigned int i = findindex(L, t, key); /* find original element */
188 for (; i < t->sizearray; i++) { /* try first array part */
189 if (!ttisnil(&t->array[i])) { /* a non-nil value? */
190 setivalue(key, i + 1);
191 setobj2s(L, key+1, &t->array[i]);
192 return 1;
193 }
194 }
195 for (i -= t->sizearray; cast_int(i) < sizenode(t); i++) { /* hash part */
196 if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */
197 setobj2s(L, key, gkey(gnode(t, i)));
198 setobj2s(L, key+1, gval(gnode(t, i)));
199 return 1;
200 }
201 }
202 return 0; /* no more elements */
203}
#define setobj2s
static unsigned int findindex(lua_State *L, Table *t, StkId key)

References Table::array, cast_int, findindex(), gkey, gnode, gval, setivalue, setobj2s, Table::sizearray, sizenode, and ttisnil.

◆ luaH_resize()

void luaH_resize ( lua_State * L,
Table * t,
unsigned int nasize,
unsigned int nhsize )

Definition at line 347 of file lua-5.3.6/src/ltable.c.

348 {
349 unsigned int i;
350 int j;
351 AuxsetnodeT asn;
352 unsigned int oldasize = t->sizearray;
353 int oldhsize = allocsizenode(t);
354 Node *nold = t->node; /* save old hash ... */
355 if (nasize > oldasize) /* array part must grow? */
356 setarrayvector(L, t, nasize);
357 /* create new hash part with appropriate size */
358 asn.t = t; asn.nhsize = nhsize;
359 if (luaD_rawrunprotected(L, auxsetnode, &asn) != LUA_OK) { /* mem. error? */
360 setarrayvector(L, t, oldasize); /* array back to its original size */
361 luaD_throw(L, LUA_ERRMEM); /* rethrow memory error */
362 }
363 if (nasize < oldasize) { /* array part must shrink? */
364 t->sizearray = nasize;
365 /* re-insert elements from vanishing slice */
366 for (i=nasize; i<oldasize; i++) {
367 if (!ttisnil(&t->array[i]))
368 luaH_setint(L, t, i + 1, &t->array[i]);
369 }
370 /* shrink array */
371 luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
372 }
373 /* re-insert elements from hash part */
374 for (j = oldhsize - 1; j >= 0; j--) {
375 Node *old = nold + j;
376 if (!ttisnil(gval(old))) {
377 /* doesn't need barrier/invalidate cache, as entry was
378 already present in the table */
379 setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old));
380 }
381 }
382 if (oldhsize > 0) /* not the dummy node? */
383 luaM_freearray(L, nold, cast(size_t, oldhsize)); /* free old hash */
384}
void luaD_throw(lua_State *L, int errcode)
int luaD_rawrunprotected(lua_State *L, Pfunc f, void *ud)
#define luaM_reallocvector(L, v, oldn, n, t)
#define setobjt2t
#define LUA_ERRMEM
#define LUA_OK
static void setarrayvector(lua_State *L, Table *t, unsigned int size)
void luaH_setint(lua_State *L, Table *t, lua_Integer key, TValue *value)
static void auxsetnode(lua_State *L, void *ud)
#define allocsizenode(t)

References allocsizenode, Table::array, auxsetnode(), cast, gkey, gval, LUA_ERRMEM, LUA_OK, luaD_rawrunprotected(), luaD_throw(), luaH_set(), luaH_setint(), luaM_freearray, luaM_reallocvector, AuxsetnodeT::nhsize, Table::node, setarrayvector(), setobjt2t, Table::sizearray, AuxsetnodeT::t, and ttisnil.

Referenced by luaH_resizearray(), and rehash().

◆ luaH_resizearray()

void luaH_resizearray ( lua_State * L,
Table * t,
unsigned int nasize )

Definition at line 387 of file lua-5.3.6/src/ltable.c.

387 {
388 int nsize = allocsizenode(t);
389 luaH_resize(L, t, nasize, nsize);
390}
void luaH_resize(lua_State *L, Table *t, unsigned int nasize, unsigned int nhsize)

References allocsizenode, and luaH_resize().

◆ luaH_set()

TValue * luaH_set ( lua_State * L,
Table * t,
const TValue * key )

Definition at line 610 of file lua-5.3.6/src/ltable.c.

610 {
611 const TValue *p = luaH_get(t, key);
612 if (p != luaO_nilobject)
613 return cast(TValue *, p);
614 else return luaH_newkey(L, t, key);
615}
const TValue * luaH_get(Table *t, const TValue *key)
TValue * luaH_newkey(lua_State *L, Table *t, const TValue *key)

References cast, luaH_get(), luaH_newkey(), and luaO_nilobject.

Referenced by luaH_newkey(), and luaH_resize().

◆ luaH_setint()

void luaH_setint ( lua_State * L,
Table * t,
lua_Integer key,
TValue * value )

Definition at line 618 of file lua-5.3.6/src/ltable.c.

618 {
619 const TValue *p = luaH_getint(t, key);
620 TValue *cell;
621 if (p != luaO_nilobject)
622 cell = cast(TValue *, p);
623 else {
624 TValue k;
625 setivalue(&k, key);
626 cell = luaH_newkey(L, t, &k);
627 }
628 setobj2t(L, cell, value);
629}
int value
Definition lsqlite3.c:2155
#define setobj2t

References cast, luaH_getint(), luaH_newkey(), luaO_nilobject, setivalue, setobj2t, and value.

Referenced by luaH_resize().

◆ mainposition()

static Node * mainposition ( const Table * t,
const TValue * key )
static

Definition at line 117 of file lua-5.3.6/src/ltable.c.

117 {
118 switch (ttype(key)) {
119 case LUA_TNUMINT:
120 return hashint(t, ivalue(key));
121 case LUA_TNUMFLT:
122 return hashmod(t, l_hashfloat(fltvalue(key)));
123 case LUA_TSHRSTR:
124 return hashstr(t, tsvalue(key));
125 case LUA_TLNGSTR:
126 return hashpow2(t, luaS_hashlongstr(tsvalue(key)));
127 case LUA_TBOOLEAN:
128 return hashboolean(t, bvalue(key));
130 return hashpointer(t, pvalue(key));
131 case LUA_TLCF:
132 return hashpointer(t, fvalue(key));
133 default:
134 lua_assert(!ttisdeadkey(key));
135 return hashpointer(t, gcvalue(key));
136 }
137}
#define pvalue(o)
#define bvalue(o)
#define LUA_TBOOLEAN
#define LUA_TLIGHTUSERDATA
#define fvalue(o)
#define LUA_TLNGSTR
#define LUA_TLCF
unsigned int luaS_hashlongstr(TString *ts)
#define hashmod(t, n)
#define hashpow2(t, n)
#define hashboolean(t, p)
#define hashpointer(t, p)
static int l_hashfloat(lua_Number n)

References bvalue, fltvalue, fvalue, gcvalue, hashboolean, hashint, hashmod, hashpointer, hashpow2, hashstr, ivalue, l_hashfloat(), lua_assert, LUA_TBOOLEAN, LUA_TLCF, LUA_TLIGHTUSERDATA, LUA_TLNGSTR, LUA_TNUMFLT, LUA_TNUMINT, LUA_TSHRSTR, luaS_hashlongstr(), pvalue, tsvalue, ttisdeadkey, and ttype.

Referenced by findindex(), getgeneric(), and luaH_newkey().

◆ numusearray()

static unsigned int numusearray ( const Table * t,
unsigned int * nums )
static

Definition at line 259 of file lua-5.3.6/src/ltable.c.

259 {
260 int lg;
261 unsigned int ttlg; /* 2^lg */
262 unsigned int ause = 0; /* summation of 'nums' */
263 unsigned int i = 1; /* count to traverse all array keys */
264 /* traverse each slice */
265 for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) {
266 unsigned int lc = 0; /* counter */
267 unsigned int lim = ttlg;
268 if (lim > t->sizearray) {
269 lim = t->sizearray; /* adjust upper limit */
270 if (i > lim)
271 break; /* no more elements to count */
272 }
273 /* count elements in range (2^(lg - 1), 2^lg] */
274 for (; i <= lim; i++) {
275 if (!ttisnil(&t->array[i-1]))
276 lc++;
277 }
278 nums[lg] += lc;
279 ause += lc;
280 }
281 return ause;
282}
#define MAXABITS

References Table::array, MAXABITS, Table::sizearray, and ttisnil.

Referenced by rehash().

◆ numusehash()

static int numusehash ( const Table * t,
unsigned int * nums,
unsigned int * pna )
static

Definition at line 285 of file lua-5.3.6/src/ltable.c.

285 {
286 int totaluse = 0; /* total number of elements */
287 int ause = 0; /* elements added to 'nums' (can go to array part) */
288 int i = sizenode(t);
289 while (i--) {
290 Node *n = &t->node[i];
291 if (!ttisnil(gval(n))) {
292 ause += countint(gkey(n), nums);
293 totaluse++;
294 }
295 }
296 *pna += ause;
297 return totaluse;
298}
static int countint(const TValue *key, unsigned int *nums)

References countint(), gkey, gval, Table::node, sizenode, and ttisnil.

Referenced by rehash().

◆ rehash()

static void rehash ( lua_State * L,
Table * t,
const TValue * ek )
static

Definition at line 395 of file lua-5.3.6/src/ltable.c.

395 {
396 unsigned int asize; /* optimal size for array part */
397 unsigned int na; /* number of keys in the array part */
398 unsigned int nums[MAXABITS + 1];
399 int i;
400 int totaluse;
401 for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */
402 na = numusearray(t, nums); /* count keys in array part */
403 totaluse = na; /* all those keys are integer keys */
404 totaluse += numusehash(t, nums, &na); /* count keys in hash part */
405 /* count extra key */
406 na += countint(ek, nums);
407 totaluse++;
408 /* compute new size for array part */
409 asize = computesizes(nums, &na);
410 /* resize the table to new computed sizes */
411 luaH_resize(L, t, asize, totaluse - na);
412}
static unsigned int computesizes(unsigned int nums[], unsigned int *pna)
static int numusehash(const Table *t, unsigned int *nums, unsigned int *pna)
static unsigned int numusearray(const Table *t, unsigned int *nums)

References computesizes(), countint(), luaH_resize(), MAXABITS, numusearray(), and numusehash().

Referenced by luaH_newkey().

◆ setarrayvector()

static void setarrayvector ( lua_State * L,
Table * t,
unsigned int size )
static

Definition at line 301 of file lua-5.3.6/src/ltable.c.

301 {
302 unsigned int i;
303 luaM_reallocvector(L, t->array, t->sizearray, size, TValue);
304 for (i=t->sizearray; i<size; i++)
305 setnilvalue(&t->array[i]);
306 t->sizearray = size;
307}

References Table::array, luaM_reallocvector, setnilvalue, and Table::sizearray.

Referenced by luaH_resize().

◆ setnodevector()

static void setnodevector ( lua_State * L,
Table * t,
unsigned int size )
static

Definition at line 310 of file lua-5.3.6/src/ltable.c.

310 {
311 if (size == 0) { /* no elements to hash part? */
312 t->node = cast(Node *, dummynode); /* use common 'dummynode' */
313 t->lsizenode = 0;
314 t->lastfree = NULL; /* signal that it is using dummy node */
315 }
316 else {
317 int i;
318 int lsize = luaO_ceillog2(size);
319 if (lsize > MAXHBITS)
320 luaG_runerror(L, "table overflow");
321 size = twoto(lsize);
322 t->node = luaM_newvector(L, size, Node);
323 for (i = 0; i < (int)size; i++) {
324 Node *n = gnode(t, i);
325 gnext(n) = 0;
326 setnilvalue(wgkey(n));
327 setnilvalue(gval(n));
328 }
329 t->lsizenode = cast_byte(lsize);
330 t->lastfree = gnode(t, size); /* all positions are free */
331 }
332}
#define luaM_newvector(L, n, t)
#define twoto(x)
#define dummynode
#define MAXHBITS
#define wgkey(n)

References cast, cast_byte, dummynode, gnext, gnode, gval, Table::lastfree, Table::lsizenode, luaG_runerror(), luaM_newvector, luaO_ceillog2(), MAXHBITS, Table::node, NULL, setnilvalue, twoto, and wgkey.

Referenced by auxsetnode(), and luaH_new().

◆ unbound_search()

static lua_Unsigned unbound_search ( Table * t,
lua_Unsigned j )
static

Definition at line 632 of file lua-5.3.6/src/ltable.c.

632 {
633 lua_Unsigned i = j; /* i is zero or a present index */
634 j++;
635 /* find 'i' and 'j' such that i is present and j is not */
636 while (!ttisnil(luaH_getint(t, j))) {
637 i = j;
638 if (j > l_castS2U(LUA_MAXINTEGER) / 2) { /* overflow? */
639 /* table was built with bad purposes: resort to linear search */
640 i = 1;
641 while (!ttisnil(luaH_getint(t, i))) i++;
642 return i - 1;
643 }
644 j *= 2;
645 }
646 /* now do a binary search between them */
647 while (j - i > 1) {
648 lua_Unsigned m = (i+j)/2;
649 if (ttisnil(luaH_getint(t, m))) j = m;
650 else i = m;
651 }
652 return i;
653}

References l_castS2U, luaH_getint(), and ttisnil.

Referenced by luaH_getn().

Variable Documentation

◆ dummynode_

const Node dummynode_
static
Initial value:
= {
{{NILCONSTANT, 0}}
}
#define NILCONSTANT

Definition at line 77 of file lua-5.3.6/src/ltable.c.

77 {
78 {NILCONSTANT}, /* value */
79 {{NILCONSTANT, 0}} /* key */
80};