MCPcopy Create free account
hub / github.com/ArduPilot/ardupilot / luaH_newkey

Function luaH_newkey

libraries/AP_Scripting/lua/src/ltable.c:468–517  ·  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

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

Callers 3

luaV_finishsetFunction · 0.85
luaH_setFunction · 0.85
luaH_setintFunction · 0.85

Calls 6

luaG_runerrorFunction · 0.85
luaV_tointegerFunction · 0.85
mainpositionFunction · 0.85
getfreeposFunction · 0.85
rehashFunction · 0.85
luaH_setFunction · 0.85

Tested by

no test coverage detected