basic/set: add set_ensure_put()

It's such a common operation to allocate the set and put an item in it,
that it deserves a helper. set_ensure_put() has the same return values
as set_put().

Comes with tests!
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2020-06-22 16:32:37 +02:00
parent aaffd34933
commit 0f9ccd9552
3 changed files with 36 additions and 0 deletions
+10
View File
@@ -1247,6 +1247,16 @@ int set_put(Set *s, const void *key) {
return hashmap_put_boldly(s, hash, &swap, true);
}
int _set_ensure_put(Set **s, const struct hash_ops *hash_ops, const void *key HASHMAP_DEBUG_PARAMS) {
int r;
r = _set_ensure_allocated(s, hash_ops HASHMAP_DEBUG_PASS_ARGS);
if (r < 0)
return r;
return set_put(*s, key);
}
int hashmap_replace(Hashmap *h, const void *key, void *value) {
struct swap_entries swap;
struct plain_hashmap_entry *e;