MCPcopy Create free account
hub / github.com/RomanKubiak/ctrlr / newkey

Function newkey

Source/Misc/lua/src/lua.c:9639–9669  ·  view source on GitHub ↗

** inserts a new key into a hash table; first, check whether key's main ** position is free. If not, check whether colliding node is in its main ** position or not: if it is not, move colliding node to an empty place and ** put new key in its main position; otherwise (colliding node is in its main ** position), new key goes to an empty position. */

Source from the content-addressed store, hash-verified

9637** position), new key goes to an empty position.
9638*/
9639static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
9640Node *mp = mainposition(t, key);
9641if (!ttisnil(gval(mp)) || mp == dummynode) {
9642Node *othern;
9643Node *n = getfreepos(t); /* get a free place */
9644if (n == NULL) { /* cannot find a free place? */
9645rehash(L, t, key); /* grow table */
9646return luaH_set(L, t, key); /* re-insert key into grown table */
9647}
9648lua_assert(n != dummynode);
9649othern = mainposition(t, key2tval(mp));
9650if (othern != mp) { /* is colliding node out of its main position? */
9651/* yes; move colliding node into free position */
9652while (gnext(othern) != mp) othern = gnext(othern); /* find previous */
9653gnext(othern) = n; /* redo the chain with `n' in place of `mp' */
9654*n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
9655gnext(mp) = NULL; /* now `mp' is free */
9656setnilvalue(gval(mp));
9657}
9658else { /* colliding node is in its own main position */
9659/* new node will go into free position */
9660gnext(n) = gnext(mp); /* chain new position */
9661gnext(mp) = n;
9662mp = n;
9663}
9664}
9665gkey(mp)->value = key->value; gkey(mp)->tt = key->tt;
9666luaC_barriert(L, t, key);
9667lua_assert(ttisnil(gval(mp)));
9668return gval(mp);
9669}
9670
9671
9672/*

Callers 3

luaH_setFunction · 0.85
luaH_setnumFunction · 0.85
luaH_setstrFunction · 0.85

Calls 4

mainpositionFunction · 0.85
getfreeposFunction · 0.85
rehashFunction · 0.85
luaH_setFunction · 0.85

Tested by

no test coverage detected