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.

Macros

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

Functions

static int l_hashfloat (lua_Number n)
 
static Nodemainposition (const Table *t, int ktt, const Value *kvl)
 
static NodemainpositionTV (const Table *t, const TValue *key)
 
static int equalkey (const TValue *k1, const Node *n2, int deadok)
 
LUAI_FUNC unsigned int luaH_realasize (const Table *t)
 
static int ispow2realasize (const Table *t)
 
static unsigned int setlimittosize (Table *t)
 
static const TValuegetgeneric (Table *t, const TValue *key, int deadok)
 
static unsigned int arrayindex (lua_Integer k)
 
static unsigned int findindex (lua_State *L, Table *t, TValue *key, unsigned int asize)
 
int luaH_next (lua_State *L, Table *t, StkId key)
 
static void freehash (lua_State *L, Table *t)
 
static unsigned int computesizes (unsigned int nums[], unsigned int *pna)
 
static int countint (lua_Integer 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 setnodevector (lua_State *L, Table *t, unsigned int size)
 
static void reinsert (lua_State *L, Table *ot, Table *t)
 
static void exchangehashpart (Table *t1, Table *t2)
 
void luaH_resize (lua_State *L, Table *t, unsigned int newasize, 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)
 
void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value)
 
const TValueluaH_getint (Table *t, lua_Integer key)
 
const TValueluaH_getshortstr (Table *t, TString *key)
 
const TValueluaH_getstr (Table *t, TString *key)
 
const TValueluaH_get (Table *t, const TValue *key)
 
void luaH_finishset (lua_State *L, Table *t, const TValue *key, const TValue *slot, TValue *value)
 
void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value)
 
void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value)
 
static lua_Unsigned hash_search (Table *t, lua_Unsigned j)
 
static unsigned int binsearch (const TValue *array, unsigned int i, unsigned int j)
 
lua_Unsigned luaH_getn (Table *t)
 

Variables

static const Node dummynode_
 
static const TValue absentkey = {ABSTKEYCONSTANT}
 

Macro Definition Documentation

◆ dummynode

#define dummynode   (&dummynode_)

Definition at line 93 of file lua-5.4.3/src/ltable.c.

Referenced by setnodevector().

◆ hashboolean

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

Definition at line 85 of file lua-5.4.3/src/ltable.c.

Referenced by mainposition().

◆ hashint

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

Definition at line 87 of file lua-5.4.3/src/ltable.c.

Referenced by luaH_getint(), and mainposition().

◆ hashmod

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

Definition at line 81 of file lua-5.4.3/src/ltable.c.

Referenced by mainposition().

◆ hashpointer

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

Definition at line 90 of file lua-5.4.3/src/ltable.c.

Referenced by mainposition().

◆ hashpow2

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

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

Referenced by mainposition().

◆ hashstr

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

Definition at line 84 of file lua-5.4.3/src/ltable.c.

Referenced by luaH_getshortstr(), and mainposition().

◆ limitasasize

#define limitasasize ( t)    check_exp(isrealasize(t), t->alimit)

Definition at line 279 of file lua-5.4.3/src/ltable.c.

Referenced by numusearray().

◆ limitequalsasize

#define limitequalsasize ( t)    (isrealasize(t) || ispow2((t)->alimit))

Definition at line 235 of file lua-5.4.3/src/ltable.c.

Referenced by luaH_getint(), luaH_getn(), and luaH_realasize().

◆ ltable_c

#define ltable_c

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

◆ LUA_CORE

#define LUA_CORE

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

◆ MAXABITS

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

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

Referenced by numusearray(), and rehash().

◆ MAXASIZE

#define MAXASIZE   luaM_limitN(1u << MAXABITS, TValue)

Definition at line 54 of file lua-5.4.3/src/ltable.c.

Referenced by arrayindex().

◆ MAXHBITS

#define MAXHBITS   (MAXABITS - 1)

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

Referenced by setnodevector().

◆ MAXHSIZE

#define MAXHSIZE   luaM_limitN(1u << MAXHBITS, Node)

Definition at line 68 of file lua-5.4.3/src/ltable.c.

Referenced by setnodevector().

Function Documentation

◆ arrayindex()

static unsigned int arrayindex ( lua_Integer k)
static

Definition at line 307 of file lua-5.4.3/src/ltable.c.

307 {
308 if (l_castS2U(k) - 1u < MAXASIZE) /* 'k' in [1, MAXASIZE]? */
309 return cast_uint(k); /* 'key' is an appropriate array index */
310 else
311 return 0;
312}
#define l_castS2U(i)
#define cast_uint(i)
#define MAXASIZE

References cast_uint, l_castS2U, and MAXASIZE.

Referenced by countint(), and findindex().

◆ binsearch()

static unsigned int binsearch ( const TValue * array,
unsigned int i,
unsigned int j )
static

Definition at line 869 of file lua-5.4.3/src/ltable.c.

870 {
871 while (j - i > 1u) { /* binary search */
872 unsigned int m = (i + j) / 2;
873 if (isempty(&array[m - 1])) j = m;
874 else i = m;
875 }
876 return i;
877}
#define isempty(v)

References isempty.

Referenced by luaH_getn().

◆ computesizes()

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

Definition at line 380 of file lua-5.4.3/src/ltable.c.

380 {
381 int i;
382 unsigned int twotoi; /* 2^i (candidate for optimal size) */
383 unsigned int a = 0; /* number of elements smaller than 2^i */
384 unsigned int na = 0; /* number of elements to go to array part */
385 unsigned int optimal = 0; /* optimal size for array part */
386 /* loop while keys can fill more than half of total size */
387 for (i = 0, twotoi = 1;
388 twotoi > 0 && *pna > twotoi / 2;
389 i++, twotoi *= 2) {
390 a += nums[i];
391 if (a > twotoi/2) { /* more than half elements present? */
392 optimal = twotoi; /* optimal size (till now) */
393 na = a; /* all elements up to 'optimal' will go to array part */
394 }
395 }
396 lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal);
397 *pna = na;
398 return optimal;
399}
#define lua_assert(c)

References lua_assert.

Referenced by rehash().

◆ countint()

static int countint ( lua_Integer key,
unsigned int * nums )
static

Definition at line 402 of file lua-5.4.3/src/ltable.c.

402 {
403 unsigned int k = arrayindex(key);
404 if (k != 0) { /* is 'key' an appropriate array index? */
405 nums[luaO_ceillog2(k)]++; /* count as such */
406 return 1;
407 }
408 else
409 return 0;
410}
int luaO_ceillog2(unsigned int x)
static unsigned int arrayindex(lua_Integer k)

References arrayindex(), and luaO_ceillog2().

Referenced by numusehash(), and rehash().

◆ equalkey()

static int equalkey ( const TValue * k1,
const Node * n2,
int deadok )
static

Definition at line 207 of file lua-5.4.3/src/ltable.c.

207 {
208 if ((rawtt(k1) != keytt(n2)) && /* not the same variants? */
209 !(deadok && keyisdead(n2) && iscollectable(k1)))
210 return 0; /* cannot be same key */
211 switch (keytt(n2)) {
212 case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE:
213 return 1;
214 case LUA_VNUMINT:
215 return (ivalue(k1) == keyival(n2));
216 case LUA_VNUMFLT:
217 return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2)));
219 return pvalue(k1) == pvalueraw(keyval(n2));
220 case LUA_VLCF:
221 return fvalue(k1) == fvalueraw(keyval(n2));
222 case ctb(LUA_VLNGSTR):
223 return luaS_eqlngstr(tsvalue(k1), keystrval(n2));
224 default:
225 return gcvalue(k1) == gcvalueraw(keyval(n2));
226 }
227}
#define gcvalue(o)
#define pvalue(o)
#define tsvalue(o)
#define iscollectable(o)
#define fvalue(o)
#define ctb(t)
int luaS_eqlngstr(TString *a, TString *b)
#define luai_numeq(a, b)
#define fltvalue(o)
#define ivalue(o)
#define keyival(node)
#define LUA_VLIGHTUSERDATA
#define LUA_VNUMFLT
#define rawtt(o)
#define fvalueraw(v)
#define LUA_VLNGSTR
#define LUA_VNUMINT
#define gcvalueraw(v)
#define fltvalueraw(v)
#define LUA_VFALSE
#define LUA_VLCF
#define LUA_VNIL
#define keyval(node)
#define pvalueraw(v)
#define keyisdead(node)
#define LUA_VTRUE
#define keystrval(node)
#define keytt(node)

References ctb, fltvalue, fltvalueraw, fvalue, fvalueraw, gcvalue, gcvalueraw, iscollectable, ivalue, keyisdead, keyival, keystrval, keytt, keyval, LUA_VFALSE, LUA_VLCF, LUA_VLIGHTUSERDATA, LUA_VLNGSTR, LUA_VNIL, LUA_VNUMFLT, LUA_VNUMINT, LUA_VTRUE, luai_numeq, luaS_eqlngstr(), pvalue, pvalueraw, rawtt, and tsvalue.

Referenced by getgeneric().

◆ exchangehashpart()

static void exchangehashpart ( Table * t1,
Table * t2 )
static

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

516 {
517 lu_byte lsizenode = t1->lsizenode;
518 Node *node = t1->node;
519 Node *lastfree = t1->lastfree;
520 t1->lsizenode = t2->lsizenode;
521 t1->node = t2->node;
522 t1->lastfree = t2->lastfree;
523 t2->lsizenode = lsizenode;
524 t2->node = node;
525 t2->lastfree = lastfree;
526}
unsigned char lu_byte

References Table::lastfree, Table::lsizenode, and Table::node.

Referenced by luaH_resize().

◆ findindex()

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

Definition at line 320 of file lua-5.4.3/src/ltable.c.

321 {
322 unsigned int i;
323 if (ttisnil(key)) return 0; /* first iteration */
324 i = ttisinteger(key) ? arrayindex(ivalue(key)) : 0;
325 if (i - 1u < asize) /* is 'key' inside array part? */
326 return i; /* yes; that's the index */
327 else {
328 const TValue *n = getgeneric(t, key, 1);
329 if (l_unlikely(isabstkey(n)))
330 luaG_runerror(L, "invalid key to 'next'"); /* key not found */
331 i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */
332 /* hash elements are numbered after array ones */
333 return (i + 1) + asize;
334 }
335}
void luaG_runerror(lua_State *L, const char *fmt,...)
#define cast_int(i)
#define ttisnil(o)
#define gnode(t, i)
#define ttisinteger(o)
#define isabstkey(v)
static const TValue * getgeneric(Table *t, const TValue *key, int deadok)
#define nodefromval(v)

References arrayindex(), cast_int, getgeneric(), gnode, isabstkey, ivalue, luaG_runerror(), nodefromval, ttisinteger, and ttisnil.

Referenced by luaH_next().

◆ freehash()

static void freehash ( lua_State * L,
Table * t )
static

Definition at line 360 of file lua-5.4.3/src/ltable.c.

360 {
361 if (!isdummy(t))
363}
#define luaM_freearray(L, b, n, t)
#define sizenode(t)
#define isdummy(n)
#define cast_sizet(i)

References cast_sizet, isdummy, luaM_freearray, Table::node, and sizenode.

Referenced by luaH_free(), and luaH_resize().

◆ getfreepos()

static Node * getfreepos ( Table * t)
static

Definition at line 634 of file lua-5.4.3/src/ltable.c.

634 {
635 if (!isdummy(t)) {
636 while (t->lastfree > t->node) {
637 t->lastfree--;
638 if (keyisnil(t->lastfree))
639 return t->lastfree;
640 }
641 }
642 return NULL; /* could not find a free place */
643}
#define NULL
Definition gmacros.h:924
#define keyisnil(node)

References isdummy, keyisnil, Table::lastfree, Table::node, and NULL.

Referenced by luaH_newkey().

◆ getgeneric()

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

Definition at line 288 of file lua-5.4.3/src/ltable.c.

288 {
289 Node *n = mainpositionTV(t, key);
290 for (;;) { /* check whether 'key' is somewhere in the chain */
291 if (equalkey(key, n, deadok))
292 return gval(n); /* that's it */
293 else {
294 int nx = gnext(n);
295 if (nx == 0)
296 return &absentkey; /* not found */
297 n += nx;
298 }
299 }
300}
#define gnext(n)
#define gval(n)
static Node * mainpositionTV(const Table *t, const TValue *key)
static const TValue absentkey
static int equalkey(const TValue *k1, const Node *n2, int deadok)

References absentkey, equalkey(), gnext, gval, and mainpositionTV().

Referenced by findindex(), luaH_get(), and luaH_getstr().

◆ hash_search()

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

Definition at line 844 of file lua-5.4.3/src/ltable.c.

844 {
845 lua_Unsigned i;
846 if (j == 0) j++; /* the caller ensures 'j + 1' is present */
847 do {
848 i = j; /* 'i' is a present index */
849 if (j <= l_castS2U(LUA_MAXINTEGER) / 2)
850 j *= 2;
851 else {
852 j = LUA_MAXINTEGER;
853 if (isempty(luaH_getint(t, j))) /* t[j] not present? */
854 break; /* 'j' now is an absent index */
855 else /* weird case */
856 return j; /* well, max integer is a boundary... */
857 }
858 } while (!isempty(luaH_getint(t, j))); /* repeat until an absent t[j] */
859 /* i < j && t[i] present && t[j] absent */
860 while (j - i > 1u) { /* do a binary search between them */
861 lua_Unsigned m = (i + j) / 2;
862 if (isempty(luaH_getint(t, m))) j = m;
863 else i = m;
864 }
865 return i;
866}
LUA_UNSIGNED lua_Unsigned
const TValue * luaH_getint(Table *t, lua_Integer key)

References isempty, l_castS2U, and luaH_getint().

Referenced by luaH_getn().

◆ ispow2realasize()

static int ispow2realasize ( const Table * t)
static

Definition at line 267 of file lua-5.4.3/src/ltable.c.

267 {
268 return (!isrealasize(t) || ispow2(t->alimit));
269}
#define ispow2(x)
#define isrealasize(t)
unsigned int alimit

References Table::alimit, ispow2, and isrealasize.

Referenced by luaH_getn().

◆ l_hashfloat()

static int l_hashfloat ( lua_Number n)
static

Definition at line 119 of file lua-5.4.3/src/ltable.c.

119 {
120 int i;
121 lua_Integer ni;
122 n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN);
123 if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */
124 lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL));
125 return 0;
126 }
127 else { /* normal case */
128 unsigned int u = cast_uint(i) + cast_uint(ni);
129 return cast_int(u <= cast_uint(INT_MAX) ? u : ~u);
130 }
131}
#define cast_num(i)
LUA_INTEGER lua_Integer
#define l_mathop(x)
#define luai_numisnan(a)
#define lua_numbertointeger(n, p)

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

Referenced by mainposition().

◆ luaH_finishset()

void luaH_finishset ( lua_State * L,
Table * t,
const TValue * key,
const TValue * slot,
TValue * value )

Definition at line 800 of file lua-5.4.3/src/ltable.c.

801 {
802 if (isabstkey(slot))
803 luaH_newkey(L, t, key, value);
804 else
805 setobj2t(L, cast(TValue *, slot), value);
806}
int value
Definition lsqlite3.c:2155
#define cast(t, exp)
#define setobj2t
void luaH_newkey(lua_State *L, Table *t, const TValue *key, TValue *value)

References cast, isabstkey, luaH_newkey(), setobj2t, and value.

Referenced by addk(), luaH_set(), luaV_finishset(), and luaX_newstring().

◆ luaH_free()

void luaH_free ( lua_State * L,
Table * t )

Definition at line 627 of file lua-5.4.3/src/ltable.c.

627 {
628 freehash(L, t);
630 luaM_free(L, t);
631}
#define luaM_free(L, b)
LUAI_FUNC unsigned int luaH_realasize(const Table *t)
static void freehash(lua_State *L, Table *t)

References Table::array, freehash(), luaH_realasize(), luaM_free, and luaM_freearray.

◆ luaH_get()

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

Definition at line 777 of file lua-5.4.3/src/ltable.c.

777 {
778 switch (ttypetag(key)) {
779 case LUA_VSHRSTR: return luaH_getshortstr(t, tsvalue(key));
780 case LUA_VNUMINT: return luaH_getint(t, ivalue(key));
781 case LUA_VNIL: return &absentkey;
782 case LUA_VNUMFLT: {
783 lua_Integer k;
784 if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */
785 return luaH_getint(t, k); /* use specialized version */
786 /* else... */
787 } /* FALLTHROUGH */
788 default:
789 return getgeneric(t, key, 0);
790 }
791}
#define ttypetag(o)
#define LUA_VSHRSTR
const TValue * luaH_getshortstr(Table *t, TString *key)
int luaV_flttointeger(lua_Number n, lua_Integer *p, F2Imod mode)
@ F2Ieq

References absentkey, F2Ieq, fltvalue, getgeneric(), ivalue, LUA_VNIL, LUA_VNUMFLT, LUA_VNUMINT, LUA_VSHRSTR, luaH_getint(), luaH_getshortstr(), luaV_flttointeger(), tsvalue, and ttypetag.

Referenced by luaH_set().

◆ luaH_getint()

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

Definition at line 719 of file lua-5.4.3/src/ltable.c.

719 {
720 if (l_castS2U(key) - 1u < t->alimit) /* 'key' in [1, t->alimit]? */
721 return &t->array[key - 1];
722 else if (!limitequalsasize(t) && /* key still may be in the array part? */
723 (l_castS2U(key) == t->alimit + 1 ||
724 l_castS2U(key) - 1u < luaH_realasize(t))) {
725 t->alimit = cast_uint(key); /* probably '#t' is here now */
726 return &t->array[key - 1];
727 }
728 else {
729 Node *n = hashint(t, key);
730 for (;;) { /* check whether 'key' is somewhere in the chain */
731 if (keyisinteger(n) && keyival(n) == key)
732 return gval(n); /* that's it */
733 else {
734 int nx = gnext(n);
735 if (nx == 0) break;
736 n += nx;
737 }
738 }
739 return &absentkey;
740 }
741}
#define keyisinteger(node)
#define hashint(t, i)
#define limitequalsasize(t)

References absentkey, Table::alimit, Table::array, cast_uint, gnext, gval, hashint, keyisinteger, keyival, l_castS2U, limitequalsasize, and luaH_realasize().

Referenced by hash_search(), luaH_get(), luaH_getn(), and luaH_setint().

◆ luaH_getn()

lua_Unsigned luaH_getn ( Table * t)

Definition at line 912 of file lua-5.4.3/src/ltable.c.

912 {
913 unsigned int limit = t->alimit;
914 if (limit > 0 && isempty(&t->array[limit - 1])) { /* (1)? */
915 /* there must be a boundary before 'limit' */
916 if (limit >= 2 && !isempty(&t->array[limit - 2])) {
917 /* 'limit - 1' is a boundary; can it be a new limit? */
918 if (ispow2realasize(t) && !ispow2(limit - 1)) {
919 t->alimit = limit - 1;
920 setnorealasize(t); /* now 'alimit' is not the real size */
921 }
922 return limit - 1;
923 }
924 else { /* must search for a boundary in [0, limit] */
925 unsigned int boundary = binsearch(t->array, 0, limit);
926 /* can this boundary represent the real size of the array? */
927 if (ispow2realasize(t) && boundary > luaH_realasize(t) / 2) {
928 t->alimit = boundary; /* use it as the new limit */
930 }
931 return boundary;
932 }
933 }
934 /* 'limit' is zero or present in table */
935 if (!limitequalsasize(t)) { /* (2)? */
936 /* 'limit' > 0 and array has more elements after 'limit' */
937 if (isempty(&t->array[limit])) /* 'limit + 1' is empty? */
938 return limit; /* this is the boundary */
939 /* else, try last element in the array */
940 limit = luaH_realasize(t);
941 if (isempty(&t->array[limit - 1])) { /* empty? */
942 /* there must be a boundary in the array after old limit,
943 and it must be a valid new limit */
944 unsigned int boundary = binsearch(t->array, t->alimit, limit);
945 t->alimit = boundary;
946 return boundary;
947 }
948 /* else, new limit is present in the table; check the hash part */
949 }
950 /* (3) 'limit' is the last element and either is zero or present in table */
951 lua_assert(limit == luaH_realasize(t) &&
952 (limit == 0 || !isempty(&t->array[limit - 1])));
953 if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, limit + 1))))
954 return limit; /* 'limit + 1' is absent */
955 else /* 'limit + 1' is also present */
956 return hash_search(t, limit);
957}
#define setnorealasize(t)
static int ispow2realasize(const Table *t)
static lua_Unsigned hash_search(Table *t, lua_Unsigned j)
static unsigned int binsearch(const TValue *array, unsigned int i, unsigned int j)

References Table::alimit, Table::array, binsearch(), cast, hash_search(), isdummy, isempty, ispow2, ispow2realasize(), limitequalsasize, lua_assert, luaH_getint(), luaH_realasize(), and setnorealasize.

◆ luaH_getshortstr()

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

Definition at line 747 of file lua-5.4.3/src/ltable.c.

747 {
748 Node *n = hashstr(t, key);
749 lua_assert(key->tt == LUA_VSHRSTR);
750 for (;;) { /* check whether 'key' is somewhere in the chain */
751 if (keyisshrstr(n) && eqshrstr(keystrval(n), key))
752 return gval(n); /* that's it */
753 else {
754 int nx = gnext(n);
755 if (nx == 0)
756 return &absentkey; /* not found */
757 n += nx;
758 }
759 }
760}
#define eqshrstr(a, b)
#define keyisshrstr(node)
#define hashstr(t, str)

References absentkey, eqshrstr, gnext, gval, hashstr, keyisshrstr, keystrval, lua_assert, and LUA_VSHRSTR.

Referenced by luaH_get(), and luaH_getstr().

◆ luaH_getstr()

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

Definition at line 763 of file lua-5.4.3/src/ltable.c.

763 {
764 if (key->tt == LUA_VSHRSTR)
765 return luaH_getshortstr(t, key);
766 else { /* for long strings, use generic case */
767 TValue ko;
768 setsvalue(cast(lua_State *, NULL), &ko, key);
769 return getgeneric(t, &ko, 0);
770 }
771}
#define setsvalue(L, obj, x)

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

◆ luaH_new()

Table * luaH_new ( lua_State * L)

Definition at line 615 of file lua-5.4.3/src/ltable.c.

615 {
616 GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table));
617 Table *t = gco2t(o);
618 t->metatable = NULL;
619 t->flags = cast_byte(maskflags); /* table has no metamethod fields */
620 t->array = NULL;
621 t->alimit = 0;
622 setnodevector(L, t, 0);
623 return t;
624}
#define cast_byte(i)
GCObject * luaC_newobj(lua_State *L, int tt, size_t sz, GCObject **list, int offset)
#define gco2t(o)
#define LUA_VTABLE
static void setnodevector(lua_State *L, Table *t, unsigned int size)
#define maskflags
struct Table * metatable

References Table::alimit, Table::array, cast_byte, Table::flags, gco2t, LUA_VTABLE, luaC_newobj(), maskflags, Table::metatable, NULL, and setnodevector().

◆ luaH_newkey()

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

Definition at line 654 of file lua-5.4.3/src/ltable.c.

654 {
655 Node *mp;
656 TValue aux;
657 if (l_unlikely(ttisnil(key)))
658 luaG_runerror(L, "table index is nil");
659 else if (ttisfloat(key)) {
660 lua_Number f = fltvalue(key);
661 lua_Integer k;
662 if (luaV_flttointeger(f, &k, F2Ieq)) { /* does key fit in an integer? */
663 setivalue(&aux, k);
664 key = &aux; /* insert it as an integer */
665 }
666 else if (l_unlikely(luai_numisnan(f)))
667 luaG_runerror(L, "table index is NaN");
668 }
669 if (ttisnil(value))
670 return; /* do not insert nil values */
671 mp = mainpositionTV(t, key);
672 if (!isempty(gval(mp)) || isdummy(t)) { /* main position is taken? */
673 Node *othern;
674 Node *f = getfreepos(t); /* get a free place */
675 if (f == NULL) { /* cannot find a free place? */
676 rehash(L, t, key); /* grow table */
677 /* whatever called 'newkey' takes care of TM cache */
678 luaH_set(L, t, key, value); /* insert key into grown table */
679 return;
680 }
681 lua_assert(!isdummy(t));
682 othern = mainposition(t, keytt(mp), &keyval(mp));
683 if (othern != mp) { /* is colliding node out of its main position? */
684 /* yes; move colliding node into free position */
685 while (othern + gnext(othern) != mp) /* find previous */
686 othern += gnext(othern);
687 gnext(othern) = cast_int(f - othern); /* rechain to point to 'f' */
688 *f = *mp; /* copy colliding node into free pos. (mp->next also goes) */
689 if (gnext(mp) != 0) {
690 gnext(f) += cast_int(mp - f); /* correct 'next' */
691 gnext(mp) = 0; /* now 'mp' is free */
692 }
693 setempty(gval(mp));
694 }
695 else { /* colliding node is in its own main position */
696 /* new node will go into free position */
697 if (gnext(mp) != 0)
698 gnext(f) = cast_int((mp + gnext(mp)) - f); /* chain new position */
699 else lua_assert(gnext(f) == 0);
700 gnext(mp) = cast_int(f - mp);
701 mp = f;
702 }
703 }
704 setnodekey(L, mp, key);
705 luaC_barrierback(L, obj2gco(t), key);
706 lua_assert(isempty(gval(mp)));
707 setobj2t(L, gval(mp), value);
708}
#define obj2gco(v)
LUA_NUMBER lua_Number
#define luaC_barrierback(L, p, v)
#define setnodekey(L, key, obj)
#define ttisfloat(o)
#define setivalue(obj, x)
#define setempty(v)
void luaH_set(lua_State *L, Table *t, const TValue *key, TValue *value)
static Node * getfreepos(Table *t)
static Node * mainposition(const Table *t, int ktt, const Value *kvl)
static void rehash(lua_State *L, Table *t, const TValue *ek)

References cast_int, F2Ieq, fltvalue, getfreepos(), gnext, gval, isdummy, isempty, keytt, keyval, lua_assert, luaC_barrierback, luaG_runerror(), luaH_set(), luai_numisnan, luaV_flttointeger(), mainposition(), mainpositionTV(), NULL, obj2gco, rehash(), setempty, setivalue, setnodekey, setobj2t, ttisfloat, ttisnil, and value.

Referenced by luaH_finishset(), and luaH_setint().

◆ luaH_next()

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

Definition at line 338 of file lua-5.4.3/src/ltable.c.

338 {
339 unsigned int asize = luaH_realasize(t);
340 unsigned int i = findindex(L, t, s2v(key), asize); /* find original key */
341 for (; i < asize; i++) { /* try first array part */
342 if (!isempty(&t->array[i])) { /* a non-empty entry? */
343 setivalue(s2v(key), i + 1);
344 setobj2s(L, key + 1, &t->array[i]);
345 return 1;
346 }
347 }
348 for (i -= asize; cast_int(i) < sizenode(t); i++) { /* hash part */
349 if (!isempty(gval(gnode(t, i)))) { /* a non-empty entry? */
350 Node *n = gnode(t, i);
351 getnodekey(L, s2v(key), n);
352 setobj2s(L, key + 1, gval(n));
353 return 1;
354 }
355 }
356 return 0; /* no more elements */
357}
#define setobj2s
#define s2v(o)
#define getnodekey(L, obj, node)
static unsigned int findindex(lua_State *L, Table *t, TValue *key, unsigned int asize)

References Table::array, cast_int, findindex(), getnodekey, gnode, gval, isempty, luaH_realasize(), s2v, setivalue, setobj2s, and sizenode.

◆ luaH_realasize()

LUAI_FUNC unsigned int luaH_realasize ( const Table * t)

Definition at line 241 of file lua-5.4.3/src/ltable.c.

241 {
242 if (limitequalsasize(t))
243 return t->alimit; /* this is the size */
244 else {
245 unsigned int size = t->alimit;
246 /* compute the smallest power of 2 not smaller than 'n' */
247 size |= (size >> 1);
248 size |= (size >> 2);
249 size |= (size >> 4);
250 size |= (size >> 8);
251 size |= (size >> 16);
252#if (UINT_MAX >> 30) > 3
253 size |= (size >> 32); /* unsigned int has more than 32 bits */
254#endif
255 size++;
256 lua_assert(ispow2(size) && size/2 < t->alimit && t->alimit < size);
257 return size;
258 }
259}

References Table::alimit, ispow2, limitequalsasize, and lua_assert.

Referenced by clearbyvalues(), luaH_free(), luaH_getint(), luaH_getn(), luaH_next(), luaV_execute(), setlimittosize(), traverseephemeron(), and traversestrongtable().

◆ luaH_resize()

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

Definition at line 542 of file lua-5.4.3/src/ltable.c.

543 {
544 unsigned int i;
545 Table newt; /* to keep the new hash part */
546 unsigned int oldasize = setlimittosize(t);
547 TValue *newarray;
548 /* create new hash part with appropriate size into 'newt' */
549 setnodevector(L, &newt, nhsize);
550 if (newasize < oldasize) { /* will array shrink? */
551 t->alimit = newasize; /* pretend array has new size... */
552 exchangehashpart(t, &newt); /* and new hash */
553 /* re-insert into the new hash the elements from vanishing slice */
554 for (i = newasize; i < oldasize; i++) {
555 if (!isempty(&t->array[i]))
556 luaH_setint(L, t, i + 1, &t->array[i]);
557 }
558 t->alimit = oldasize; /* restore current size... */
559 exchangehashpart(t, &newt); /* and hash (in case of errors) */
560 }
561 /* allocate new array */
562 newarray = luaM_reallocvector(L, t->array, oldasize, newasize, TValue);
563 if (l_unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */
564 freehash(L, &newt); /* release new hash part */
565 luaM_error(L); /* raise error (with array unchanged) */
566 }
567 /* allocation ok; initialize new part of the array */
568 exchangehashpart(t, &newt); /* 't' has the new hash ('newt' has the old) */
569 t->array = newarray; /* set new array part */
570 t->alimit = newasize;
571 for (i = oldasize; i < newasize; i++) /* clear new slice of the array */
572 setempty(&t->array[i]);
573 /* re-insert elements from old hash part into new parts */
574 reinsert(L, &newt, t); /* 'newt' now has the old hash */
575 freehash(L, &newt); /* free old hash part */
576}
#define luaM_reallocvector(L, v, oldn, n, t)
#define luaM_error(L)
static unsigned int setlimittosize(Table *t)
void luaH_setint(lua_State *L, Table *t, lua_Integer key, TValue *value)
static void reinsert(lua_State *L, Table *ot, Table *t)
static void exchangehashpart(Table *t1, Table *t2)

References Table::alimit, Table::array, exchangehashpart(), freehash(), isempty, luaH_setint(), luaM_error, luaM_reallocvector, NULL, reinsert(), setempty, setlimittosize(), and setnodevector().

Referenced by luaH_resizearray(), and rehash().

◆ luaH_resizearray()

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

Definition at line 579 of file lua-5.4.3/src/ltable.c.

579 {
580 int nsize = allocsizenode(t);
581 luaH_resize(L, t, nasize, nsize);
582}
#define allocsizenode(t)
void luaH_resize(lua_State *L, Table *t, unsigned int newasize, unsigned int nhsize)

References allocsizenode, and luaH_resize().

◆ luaH_set()

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

Definition at line 813 of file lua-5.4.3/src/ltable.c.

813 {
814 const TValue *slot = luaH_get(t, key);
815 luaH_finishset(L, t, key, slot, value);
816}
const TValue * luaH_get(Table *t, const TValue *key)
void luaH_finishset(lua_State *L, Table *t, const TValue *key, const TValue *slot, TValue *value)

References luaH_finishset(), luaH_get(), and value.

Referenced by luaH_newkey(), and reinsert().

◆ luaH_setint()

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

Definition at line 819 of file lua-5.4.3/src/ltable.c.

819 {
820 const TValue *p = luaH_getint(t, key);
821 if (isabstkey(p)) {
822 TValue k;
823 setivalue(&k, key);
824 luaH_newkey(L, t, &k, value);
825 }
826 else
827 setobj2t(L, cast(TValue *, p), value);
828}

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

Referenced by luaH_resize().

◆ mainposition()

static Node * mainposition ( const Table * t,
int ktt,
const Value * kvl )
static

Definition at line 141 of file lua-5.4.3/src/ltable.c.

141 {
142 switch (withvariant(ktt)) {
143 case LUA_VNUMINT: {
144 lua_Integer key = ivalueraw(*kvl);
145 return hashint(t, key);
146 }
147 case LUA_VNUMFLT: {
148 lua_Number n = fltvalueraw(*kvl);
149 return hashmod(t, l_hashfloat(n));
150 }
151 case LUA_VSHRSTR: {
152 TString *ts = tsvalueraw(*kvl);
153 return hashstr(t, ts);
154 }
155 case LUA_VLNGSTR: {
156 TString *ts = tsvalueraw(*kvl);
157 return hashpow2(t, luaS_hashlongstr(ts));
158 }
159 case LUA_VFALSE:
160 return hashboolean(t, 0);
161 case LUA_VTRUE:
162 return hashboolean(t, 1);
163 case LUA_VLIGHTUSERDATA: {
164 void *p = pvalueraw(*kvl);
165 return hashpointer(t, p);
166 }
167 case LUA_VLCF: {
168 lua_CFunction f = fvalueraw(*kvl);
169 return hashpointer(t, f);
170 }
171 default: {
172 GCObject *o = gcvalueraw(*kvl);
173 return hashpointer(t, o);
174 }
175 }
176}
int(* lua_CFunction)(lua_State *L)
unsigned int luaS_hashlongstr(TString *ts)
#define tsvalueraw(v)
#define ivalueraw(v)
#define withvariant(t)
#define hashmod(t, n)
#define hashpow2(t, n)
#define hashboolean(t, p)
#define hashpointer(t, p)
static int l_hashfloat(lua_Number n)

References fltvalueraw, fvalueraw, gcvalueraw, hashboolean, hashint, hashmod, hashpointer, hashpow2, hashstr, ivalueraw, l_hashfloat(), LUA_VFALSE, LUA_VLCF, LUA_VLIGHTUSERDATA, LUA_VLNGSTR, LUA_VNUMFLT, LUA_VNUMINT, LUA_VSHRSTR, LUA_VTRUE, luaS_hashlongstr(), pvalueraw, tsvalueraw, and withvariant.

Referenced by luaH_newkey(), and mainpositionTV().

◆ mainpositionTV()

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

Definition at line 182 of file lua-5.4.3/src/ltable.c.

182 {
183 return mainposition(t, rawtt(key), valraw(key));
184}
#define valraw(o)

References mainposition(), rawtt, and valraw.

Referenced by getgeneric(), and luaH_newkey().

◆ numusearray()

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

Definition at line 418 of file lua-5.4.3/src/ltable.c.

418 {
419 int lg;
420 unsigned int ttlg; /* 2^lg */
421 unsigned int ause = 0; /* summation of 'nums' */
422 unsigned int i = 1; /* count to traverse all array keys */
423 unsigned int asize = limitasasize(t); /* real array size */
424 /* traverse each slice */
425 for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) {
426 unsigned int lc = 0; /* counter */
427 unsigned int lim = ttlg;
428 if (lim > asize) {
429 lim = asize; /* adjust upper limit */
430 if (i > lim)
431 break; /* no more elements to count */
432 }
433 /* count elements in range (2^(lg - 1), 2^lg] */
434 for (; i <= lim; i++) {
435 if (!isempty(&t->array[i-1]))
436 lc++;
437 }
438 nums[lg] += lc;
439 ause += lc;
440 }
441 return ause;
442}
#define MAXABITS
#define limitasasize(t)

References Table::array, isempty, limitasasize, and MAXABITS.

Referenced by rehash().

◆ numusehash()

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

Definition at line 445 of file lua-5.4.3/src/ltable.c.

445 {
446 int totaluse = 0; /* total number of elements */
447 int ause = 0; /* elements added to 'nums' (can go to array part) */
448 int i = sizenode(t);
449 while (i--) {
450 Node *n = &t->node[i];
451 if (!isempty(gval(n))) {
452 if (keyisinteger(n))
453 ause += countint(keyival(n), nums);
454 totaluse++;
455 }
456 }
457 *pna += ause;
458 return totaluse;
459}
static int countint(lua_Integer key, unsigned int *nums)

References countint(), gval, isempty, keyisinteger, keyival, Table::node, and sizenode.

Referenced by rehash().

◆ rehash()

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

Definition at line 587 of file lua-5.4.3/src/ltable.c.

587 {
588 unsigned int asize; /* optimal size for array part */
589 unsigned int na; /* number of keys in the array part */
590 unsigned int nums[MAXABITS + 1];
591 int i;
592 int totaluse;
593 for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */
595 na = numusearray(t, nums); /* count keys in array part */
596 totaluse = na; /* all those keys are integer keys */
597 totaluse += numusehash(t, nums, &na); /* count keys in hash part */
598 /* count extra key */
599 if (ttisinteger(ek))
600 na += countint(ivalue(ek), nums);
601 totaluse++;
602 /* compute new size for array part */
603 asize = computesizes(nums, &na);
604 /* resize the table to new computed sizes */
605 luaH_resize(L, t, asize, totaluse - na);
606}
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(), ivalue, luaH_resize(), MAXABITS, numusearray(), numusehash(), setlimittosize(), and ttisinteger.

Referenced by luaH_newkey().

◆ reinsert()

static void reinsert ( lua_State * L,
Table * ot,
Table * t )
static

Definition at line 497 of file lua-5.4.3/src/ltable.c.

497 {
498 int j;
499 int size = sizenode(ot);
500 for (j = 0; j < size; j++) {
501 Node *old = gnode(ot, j);
502 if (!isempty(gval(old))) {
503 /* doesn't need barrier/invalidate cache, as entry was
504 already present in the table */
505 TValue k;
506 getnodekey(L, &k, old);
507 luaH_set(L, t, &k, gval(old));
508 }
509 }
510}

References getnodekey, gnode, gval, isempty, luaH_set(), and sizenode.

Referenced by luaH_resize().

◆ setlimittosize()

static unsigned int setlimittosize ( Table * t)
static

Definition at line 272 of file lua-5.4.3/src/ltable.c.

272 {
273 t->alimit = luaH_realasize(t);
274 setrealasize(t);
275 return t->alimit;
276}
#define setrealasize(t)

References Table::alimit, luaH_realasize(), and setrealasize.

Referenced by luaH_resize(), and rehash().

◆ setnodevector()

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

Definition at line 469 of file lua-5.4.3/src/ltable.c.

469 {
470 if (size == 0) { /* no elements to hash part? */
471 t->node = cast(Node *, dummynode); /* use common 'dummynode' */
472 t->lsizenode = 0;
473 t->lastfree = NULL; /* signal that it is using dummy node */
474 }
475 else {
476 int i;
477 int lsize = luaO_ceillog2(size);
478 if (lsize > MAXHBITS || (1u << lsize) > MAXHSIZE)
479 luaG_runerror(L, "table overflow");
480 size = twoto(lsize);
481 t->node = luaM_newvector(L, size, Node);
482 for (i = 0; i < (int)size; i++) {
483 Node *n = gnode(t, i);
484 gnext(n) = 0;
485 setnilkey(n);
486 setempty(gval(n));
487 }
488 t->lsizenode = cast_byte(lsize);
489 t->lastfree = gnode(t, size); /* all positions are free */
490 }
491}
#define luaM_newvector(L, n, t)
#define twoto(x)
#define setnilkey(node)
#define dummynode
#define MAXHSIZE
#define MAXHBITS

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

Referenced by luaH_new(), and luaH_resize().

Variable Documentation

◆ absentkey

const TValue absentkey = {ABSTKEYCONSTANT}
static

Definition at line 101 of file lua-5.4.3/src/ltable.c.

#define ABSTKEYCONSTANT

Referenced by getgeneric(), luaH_get(), luaH_getint(), and luaH_getshortstr().

◆ dummynode_

const Node dummynode_
static
Initial value:
= {
LUA_VNIL, 0, {NULL}}
}
#define LUA_VEMPTY

Definition at line 95 of file lua-5.4.3/src/ltable.c.

95 {
96 {{NULL}, LUA_VEMPTY, /* value's value and type */
97 LUA_VNIL, 0, {NULL}} /* key type, next, and key value */
98};