** 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. */
| 397 | ** position), new key goes to an empty position. |
| 398 | */ |
| 399 | static TValue *newkey (lua_State *L, Table *t, const TValue *key) { |
| 400 | Node *mp = mainposition(t, key); |
| 401 | if (!ttisnil(gval(mp)) || mp == dummynode) { |
| 402 | Node *othern; |
| 403 | Node *n = getfreepos(t); /* get a free place */ |
| 404 | if (n == NULL) { /* cannot find a free place? */ |
| 405 | rehash(L, t, key); /* grow table */ |
| 406 | return luaH_set(L, t, key); /* re-insert key into grown table */ |
| 407 | } |
| 408 | lua_assert(n != dummynode); |
| 409 | othern = mainposition(t, key2tval(mp)); |
| 410 | if (othern != mp) { /* is colliding node out of its main position? */ |
| 411 | /* yes; move colliding node into free position */ |
| 412 | while (gnext(othern) != mp) othern = gnext(othern); /* find previous */ |
| 413 | gnext(othern) = n; /* redo the chain with `n' in place of `mp' */ |
| 414 | *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */ |
| 415 | gnext(mp) = NULL; /* now `mp' is free */ |
| 416 | setnilvalue(gval(mp)); |
| 417 | } |
| 418 | else { /* colliding node is in its own main position */ |
| 419 | /* new node will go into free position */ |
| 420 | gnext(n) = gnext(mp); /* chain new position */ |
| 421 | gnext(mp) = n; |
| 422 | mp = n; |
| 423 | } |
| 424 | } |
| 425 | gkey(mp)->value = key->value; gkey(mp)->tt = key->tt; |
| 426 | luaC_barriert(L, t, key); |
| 427 | lua_assert(ttisnil(gval(mp))); |
| 428 | return gval(mp); |
| 429 | } |
| 430 | |
| 431 | |
| 432 | /* |
no test coverage detected