diff options
author | EuAndreh <eu@euandre.org> | 2024-06-06 08:46:26 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-06-06 08:46:26 -0300 |
commit | 7d144d9053e54c5c1df04d383e96aac4ee9064bb (patch) | |
tree | d17d2c5c10758144670eb966d3dee9a8f88b1088 /src | |
parent | src/{tree,vector}.h: Consistent spacing between sections (diff) | |
download | pindaiba-7d144d9053e54c5c1df04d383e96aac4ee9064bb.tar.gz pindaiba-7d144d9053e54c5c1df04d383e96aac4ee9064bb.tar.xz |
src/set.c: v0 for set_add()
Diffstat (limited to 'src')
-rw-r--r-- | src/set.c | 24 |
1 files changed, 24 insertions, 0 deletions
@@ -8,6 +8,7 @@ #include <stdint.h> #include <string.h> +#include <endiannessbs.h> #include <siphashbs.h> #include "hash.h" @@ -85,3 +86,26 @@ set_free(const struct Set **const s) { vector_free(&table); freeit((void *)s); } + +int +set_add(const struct Set *const s, const void *const value) { + int rc = -1; + + uint8_t hash_bytes[HASH_OUTPUT_LENGTH]; + hash(s->value_size, value, hash_bytes); + + const uint64_t hash_value = endiannessbs_from_le64(hash_bytes); + const size_t idx = hash_value % vector_capacity(s->table); + + const struct Tree *slot; + assert(vector_nth(s->table, idx, (void *)&slot) == 0); + + if (tree_add(slot, value)) { + logerr("tree_add()"); + goto out; + } + + rc = 0; +out: + return rc; +} |