Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
ltable.h File Reference
#include "lobject.h"

Go to the source code of this file.

Macros

#define gnode(t, i)   (&(t)->node[i])
 
#define gkey(n)   (&(n)->i_key.tvk)
 
#define gval(n)   (&(n)->i_val)
 
#define gnext(n)   ((n)->i_key.nk.next)
 
#define invalidateTMcache(t)   ((t)->flags = 0)
 
#define keyfromval(v)    (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val))))
 

Functions

LUAI_FUNC const TValueluaH_getint (Table *t, int key)
 
LUAI_FUNC void luaH_setint (lua_State *L, Table *t, int key, TValue *value)
 
LUAI_FUNC const TValueluaH_getstr (Table *t, TString *key)
 
LUAI_FUNC const TValueluaH_get (Table *t, const TValue *key)
 
LUAI_FUNC TValueluaH_newkey (lua_State *L, Table *t, const TValue *key)
 
LUAI_FUNC TValueluaH_set (lua_State *L, Table *t, const TValue *key)
 
LUAI_FUNC TableluaH_new (lua_State *L)
 
LUAI_FUNC void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize)
 
LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize)
 
LUAI_FUNC void luaH_free (lua_State *L, Table *t)
 
LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key)
 
LUAI_FUNC int luaH_getn (Table *t)
 

Macro Definition Documentation

◆ gkey

#define gkey ( n)    (&(n)->i_key.tvk)

Definition at line 14 of file lua-5.2.4/src/ltable.h.

◆ gnext

#define gnext ( n)    ((n)->i_key.nk.next)

Definition at line 16 of file lua-5.2.4/src/ltable.h.

◆ gnode

#define gnode ( t,
i )   (&(t)->node[i])

Definition at line 13 of file lua-5.2.4/src/ltable.h.

◆ gval

#define gval ( n)    (&(n)->i_val)

Definition at line 15 of file lua-5.2.4/src/ltable.h.

◆ invalidateTMcache

#define invalidateTMcache ( t)    ((t)->flags = 0)

◆ keyfromval

#define keyfromval ( v)     (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val))))

Definition at line 21 of file lua-5.2.4/src/ltable.h.

21#define keyfromval(v) \
22 (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val))))

Referenced by luaX_newstring().

Function Documentation

◆ luaH_free()

LUAI_FUNC void luaH_free ( lua_State * L,
Table * t )

Definition at line 374 of file lua-5.1.5/src/ltable.c.

374 {
375 if (t->node != dummynode)
376 luaM_freearray(L, t->node, sizenode(t), Node);
378 luaM_free(L, t);
379}
#define luaM_freearray(L, b, n, t)
#define luaM_free(L, b)
#define sizenode(t)
#define dummynode

◆ luaH_get()

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

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

469 {
470 switch (ttype(key)) {
471 case LUA_TNIL: return luaO_nilobject;
472 case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
473 case LUA_TNUMBER: {
474 int k;
475 lua_Number n = nvalue(key);
476 lua_number2int(k, n);
477 if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */
478 return luaH_getnum(t, k); /* use specialized version */
479 /* else go through */
480 }
481 default: {
482 Node *n = mainposition(t, key);
483 do { /* check whether `key' is somewhere in the chain */
484 if (luaO_rawequalObj(key2tval(n), key))
485 return gval(n); /* that's it */
486 else n = gnext(n);
487 } while (n);
488 return luaO_nilobject;
489 }
490 }
491}
#define cast_num(i)
int luaO_rawequalObj(const TValue *t1, const TValue *t2)
#define nvalue(o)
#define luaO_nilobject
#define rawtsvalue(o)
#define ttype(o)
const TValue * luaH_getnum(Table *t, int key)
const TValue * luaH_getstr(Table *t, TString *key)
static Node * mainposition(const Table *t, const TValue *key)
#define gnext(n)
#define gval(n)
#define key2tval(n)
#define LUA_TSTRING
#define LUA_TNUMBER
#define LUA_TNIL
LUA_NUMBER lua_Number
#define lua_number2int(i, d)
#define luai_numeq(a, b)

◆ luaH_getint()

LUAI_FUNC const TValue * luaH_getint ( Table * t,
int key )

Definition at line 446 of file lua-5.2.4/src/ltable.c.

446 {
447 /* (1 <= key && key <= t->sizearray) */
448 if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray))
449 return &t->array[key-1];
450 else {
451 lua_Number nk = cast_num(key);
452 Node *n = hashnum(t, nk);
453 do { /* check whether `key' is somewhere in the chain */
454 if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk))
455 return gval(n); /* that's it */
456 else n = gnext(n);
457 } while (n);
458 return luaO_nilobject;
459 }
460}
#define cast(t, exp)
#define ttisnumber(o)
#define gkey(n)
static Node * hashnum(const Table *t, lua_Number n)

References Table::array, cast, cast_num, gkey, gnext, gval, hashnum(), luai_numeq, luaO_nilobject, nvalue, Table::sizearray, and ttisnumber.

Referenced by lua_getglobal(), lua_getglobal(), lua_geti(), lua_load(), lua_rawgeti(), lua_rawgeti(), lua_setglobal(), lua_setglobal(), lua_seti(), luaH_get(), luaH_setint(), and unbound_search().

◆ luaH_getn()

LUAI_FUNC int luaH_getn ( Table * t)

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

560 {
561 unsigned int j = t->sizearray;
562 if (j > 0 && ttisnil(&t->array[j - 1])) {
563 /* there is a boundary in the array part: (binary) search for it */
564 unsigned int i = 0;
565 while (j - i > 1) {
566 unsigned int m = (i+j)/2;
567 if (ttisnil(&t->array[m - 1])) j = m;
568 else i = m;
569 }
570 return i;
571 }
572 /* else must find a boundary in hash part */
573 else if (t->node == dummynode) /* hash part is empty? */
574 return j; /* that is easy... */
575 else return unbound_search(t, j);
576}
#define ttisnil(o)
static int unbound_search(Table *t, unsigned int j)

◆ luaH_getstr()

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

Definition at line 455 of file lua-5.1.5/src/ltable.c.

455 {
456 Node *n = hashstr(t, key);
457 do { /* check whether `key' is somewhere in the chain */
458 if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key)
459 return gval(n); /* that's it */
460 else n = gnext(n);
461 } while (n);
462 return luaO_nilobject;
463}
#define ttisstring(o)
#define hashstr(t, str)

◆ luaH_new()

LUAI_FUNC Table * luaH_new ( lua_State * L)

Definition at line 368 of file lua-5.2.4/src/ltable.c.

368 {
369 Table *t = &luaC_newobj(L, LUA_TTABLE, sizeof(Table), NULL, 0)->h;
370 t->metatable = NULL;
371 t->flags = cast_byte(~0);
372 t->array = NULL;
373 t->sizearray = 0;
374 setnodevector(L, t, 0);
375 return t;
376}
#define NULL
Definition gmacros.h:924
#define cast_byte(i)
#define LUA_TTABLE
GCObject * luaC_newobj(lua_State *L, int tt, size_t sz, GCObject **list, int offset)
static void setnodevector(lua_State *L, Table *t, int size)
struct Table * metatable
struct Table h

◆ luaH_newkey()

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

Definition at line 405 of file lua-5.2.4/src/ltable.c.

405 {
406 Node *mp;
407 if (ttisnil(key)) luaG_runerror(L, "table index is nil");
408 else if (ttisnumber(key) && luai_numisnan(L, nvalue(key)))
409 luaG_runerror(L, "table index is NaN");
410 mp = mainposition(t, key);
411 if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */
412 Node *othern;
413 Node *n = getfreepos(t); /* get a free place */
414 if (n == NULL) { /* cannot find a free place? */
415 rehash(L, t, key); /* grow table */
416 /* whatever called 'newkey' take care of TM cache and GC barrier */
417 return luaH_set(L, t, key); /* insert key into grown table */
418 }
419 lua_assert(!isdummy(n));
420 othern = mainposition(t, gkey(mp));
421 if (othern != mp) { /* is colliding node out of its main position? */
422 /* yes; move colliding node into free position */
423 while (gnext(othern) != mp) othern = gnext(othern); /* find previous */
424 gnext(othern) = n; /* redo the chain with `n' in place of `mp' */
425 *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
426 gnext(mp) = NULL; /* now `mp' is free */
427 setnilvalue(gval(mp));
428 }
429 else { /* colliding node is in its own main position */
430 /* new node will go into free position */
431 gnext(n) = gnext(mp); /* chain new position */
432 gnext(mp) = n;
433 mp = n;
434 }
435 }
436 setobj2t(L, gkey(mp), key);
437 luaC_barrierback(L, obj2gco(t), key);
438 lua_assert(ttisnil(gval(mp)));
439 return gval(mp);
440}
void luaG_runerror(lua_State *L, const char *fmt,...)
#define lua_assert(c)
#define setnilvalue(obj)
#define setobj2t
#define obj2gco(v)
#define luaC_barrierback(L, p, v)
static Node * getfreepos(Table *t)
#define isdummy(n)
TValue * luaH_set(lua_State *L, Table *t, const TValue *key)
static Node * mainposition(const Table *t, const TValue *key)
static void rehash(lua_State *L, Table *t, const TValue *ek)
#define luai_numisnan(a)

◆ luaH_next()

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

Definition at line 162 of file lua-5.1.5/src/ltable.c.

162 {
163 int i = findindex(L, t, key); /* find original element */
164 for (i++; i < t->sizearray; i++) { /* try first array part */
165 if (!ttisnil(&t->array[i])) { /* a non-nil value? */
166 setnvalue(key, cast_num(i+1));
167 setobj2s(L, key+1, &t->array[i]);
168 return 1;
169 }
170 }
171 for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */
172 if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */
173 setobj2s(L, key, key2tval(gnode(t, i)));
174 setobj2s(L, key+1, gval(gnode(t, i)));
175 return 1;
176 }
177 }
178 return 0; /* no more elements */
179}
#define setobj2s
#define setnvalue(obj, x)
static int findindex(lua_State *L, Table *t, StkId key)
#define gnode(t, i)

◆ luaH_resize()

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

Definition at line 304 of file lua-5.2.4/src/ltable.c.

304 {
305 int i;
306 int oldasize = t->sizearray;
307 int oldhsize = t->lsizenode;
308 Node *nold = t->node; /* save old hash ... */
309 if (nasize > oldasize) /* array part must grow? */
310 setarrayvector(L, t, nasize);
311 /* create new hash part with appropriate size */
312 setnodevector(L, t, nhsize);
313 if (nasize < oldasize) { /* array part must shrink? */
314 t->sizearray = nasize;
315 /* re-insert elements from vanishing slice */
316 for (i=nasize; i<oldasize; i++) {
317 if (!ttisnil(&t->array[i]))
318 luaH_setint(L, t, i + 1, &t->array[i]);
319 }
320 /* shrink array */
321 luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
322 }
323 /* re-insert elements from hash part */
324 for (i = twoto(oldhsize) - 1; i >= 0; i--) {
325 Node *old = nold+i;
326 if (!ttisnil(gval(old))) {
327 /* doesn't need barrier/invalidate cache, as entry was
328 already present in the table */
329 setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old));
330 }
331 }
332 if (!isdummy(nold))
333 luaM_freearray(L, nold, cast(size_t, twoto(oldhsize))); /* free old array */
334}
#define luaM_reallocvector(L, v, oldn, n, t)
#define setobjt2t
#define twoto(x)
static void setarrayvector(lua_State *L, Table *t, int size)
void luaH_setint(lua_State *L, Table *t, int key, TValue *value)

References Table::array, cast, gkey, gval, isdummy, Table::lsizenode, luaH_set(), luaH_setint(), luaM_freearray, luaM_reallocvector, Table::node, setarrayvector(), setnodevector(), setobjt2t, Table::sizearray, ttisnil, and twoto.

Referenced by init_registry(), init_registry(), init_registry(), lua_createtable(), luaH_resizearray(), luaV_execute(), luaV_execute(), and rehash().

◆ luaH_resizearray()

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

Definition at line 327 of file lua-5.1.5/src/ltable.c.

327 {
328 int nsize = (t->node == dummynode) ? 0 : sizenode(t);
329 resize(L, t, nasize, nsize);
330}
static void resize(lua_State *L, Table *t, int nasize, int nhsize)

References dummynode, isdummy, luaH_resize(), Table::node, resize(), and sizenode.

Referenced by luaV_execute(), luaV_execute(), and luaV_execute().

◆ luaH_set()

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

Definition at line 494 of file lua-5.1.5/src/ltable.c.

494 {
495 const TValue *p = luaH_get(t, key);
496 t->flags = 0;
497 if (p != luaO_nilobject)
498 return cast(TValue *, p);
499 else {
500 if (ttisnil(key)) luaG_runerror(L, "table index is nil");
501 else if (ttisnumber(key) && luai_numisnan(nvalue(key)))
502 luaG_runerror(L, "table index is NaN");
503 return newkey(L, t, key);
504 }
505}
const TValue * luaH_get(Table *t, const TValue *key)
static TValue * newkey(lua_State *L, Table *t, const TValue *key)

◆ luaH_setint()

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

Definition at line 518 of file lua-5.2.4/src/ltable.c.

518 {
519 const TValue *p = luaH_getint(t, key);
520 TValue *cell;
521 if (p != luaO_nilobject)
522 cell = cast(TValue *, p);
523 else {
524 TValue k;
525 setnvalue(&k, cast_num(key));
526 cell = luaH_newkey(L, t, &k);
527 }
528 setobj2t(L, cell, value);
529}
int value
Definition lsqlite3.c:2155
TValue * luaH_newkey(lua_State *L, Table *t, const TValue *key)
const TValue * luaH_getint(Table *t, int key)

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

Referenced by collectvalidlines(), collectvalidlines(), collectvalidlines(), init_registry(), init_registry(), lua_rawseti(), lua_rawseti(), luaH_resize(), and luaV_execute().