Add or Overwrite: * Add an element, discarding the old value if the key already exists. * Return 1 if the key was added from scratch, 0 if there was already an * element with such key and HashTableReplace() just performed a value update * operation. */
| 434 | * element with such key and HashTableReplace() just performed a value update |
| 435 | * operation. */ |
| 436 | int HashTableReplace(dict *d, void *key, void *val) |
| 437 | { |
| 438 | dictEntry *entry, *existing, auxentry; |
| 439 | |
| 440 | /* Try to add the element. If the key |
| 441 | * does not exists HashTableAdd will succeed. */ |
| 442 | entry = HashTableAddRaw(d,key,&existing); |
| 443 | if (entry) { |
| 444 | HashTableSetVal(d, entry, val); |
| 445 | return 1; |
| 446 | } |
| 447 | |
| 448 | /* Set the new value and free the old one. Note that it is important |
| 449 | * to do that in this order, as the value may just be exactly the same |
| 450 | * as the previous one. In this context, think to reference counting, |
| 451 | * you want to increment (set), and then decrement (free), and not the |
| 452 | * reverse. */ |
| 453 | auxentry = *existing; |
| 454 | HashTableSetVal(d, existing, val); |
| 455 | dictFreeVal(d, &auxentry); |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | /* Add or Find: |
| 460 | * HashTableAddOrFind() is simply a version of HashTableAddRaw() that always |
nothing calls this directly
no test coverage detected