libhmap-c

C hashmap library
git clone https://git.sinitax.com/sinitax/libhmap-c
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

commit 21c4993cc3c70a205d960306418a8e68492df21a
parent b980199ca9bcea1f9b4cdae83da0f05b375c2ad5
Author: Louis Burda <quent.burda@gmail.com>
Date:   Mon, 27 Mar 2023 00:25:41 +0200

Switch to const * where possible

Diffstat:
Minclude/hmap.h | 24++++++++++++------------
Msrc/hmap.c | 45+++++++++++++++++++++++++--------------------
2 files changed, 37 insertions(+), 32 deletions(-)

diff --git a/include/hmap.h b/include/hmap.h @@ -86,28 +86,28 @@ struct hmap { const struct allocator *allocator; }; -int hmap_init(struct hmap *map, size_t size, hmap_hash_func hasher, +int hmap_init(struct hmap *map, size_t buckets, hmap_hash_func hasher, hmap_keycmp_func keycmp, const struct allocator *allocator); void hmap_deinit(struct hmap *map); -int hmap_alloc(struct hmap **map, size_t size, hmap_hash_func hasher, +int hmap_alloc(struct hmap **map, size_t buckets, hmap_hash_func hasher, hmap_keycmp_func keycmp, const struct allocator *allocator); void hmap_free(struct hmap *map); -int hmap_copy(struct hmap *dst, const struct hmap *src); +int hmap_copy(const struct hmap *dst, const struct hmap *src); void hmap_swap(struct hmap *m1, struct hmap *m2); -void hmap_clear(struct hmap *map); +void hmap_clear(const struct hmap *map); -struct hmap_link **hmap_link_get(struct hmap *map, struct hmap_key key); -struct hmap_link **hmap_link_pos(struct hmap *map, struct hmap_key key); -struct hmap_link *hmap_link_pop(struct hmap *map, struct hmap_key key); -int hmap_link_alloc(struct hmap *map, struct hmap_link **out, +struct hmap_link **hmap_link_get(const struct hmap *map, struct hmap_key key); +struct hmap_link **hmap_link_pos(const struct hmap *map, struct hmap_key key); +struct hmap_link *hmap_link_pop(const struct hmap *map, struct hmap_key key); +int hmap_link_alloc(const struct hmap *map, struct hmap_link **out, struct hmap_key key, struct hmap_val value); -struct hmap_link *hmap_get(struct hmap *map, struct hmap_key key); -void hmap_rm(struct hmap *map, struct hmap_key key); -int hmap_set(struct hmap *map, struct hmap_key key, struct hmap_val value); -int hmap_add(struct hmap *map, struct hmap_key key, struct hmap_val value); +struct hmap_link *hmap_get(const struct hmap *map, struct hmap_key key); +int hmap_set(const struct hmap *map, struct hmap_key key, struct hmap_val value); +int hmap_rm(const struct hmap *map, struct hmap_key key); +int hmap_add(const struct hmap *map, struct hmap_key key, struct hmap_val value); void hmap_iter_init(struct hmap_iter *iter); bool hmap_iter_next(const struct hmap *map, struct hmap_iter *iter); diff --git a/src/hmap.c b/src/hmap.c @@ -4,7 +4,7 @@ #include <string.h> static inline size_t -hmap_key_bucket(struct hmap *map, struct hmap_key key) +hmap_key_bucket(const struct hmap *map, struct hmap_key key) { return map->hash(key) % map->bucketcnt; } @@ -69,7 +69,7 @@ hmap_free(struct hmap *map) } int -hmap_copy(struct hmap *dst, const struct hmap *src) +hmap_copy(const struct hmap *dst, const struct hmap *src) { struct hmap_iter iter; int rc; @@ -97,7 +97,7 @@ hmap_swap(struct hmap *m1, struct hmap *m2) } void -hmap_clear(struct hmap *map) +hmap_clear(const struct hmap *map) { struct hmap_iter iter; struct hmap_link *prev; @@ -117,7 +117,7 @@ hmap_clear(struct hmap *map) } struct hmap_link ** -hmap_link_get(struct hmap *map, struct hmap_key key) +hmap_link_get(const struct hmap *map, struct hmap_key key) { struct hmap_link **iter, *link; @@ -135,7 +135,7 @@ hmap_link_get(struct hmap *map, struct hmap_key key) } struct hmap_link ** -hmap_link_pos(struct hmap *map, struct hmap_key key) +hmap_link_pos(const struct hmap *map, struct hmap_key key) { struct hmap_link **iter, *link; @@ -153,7 +153,7 @@ hmap_link_pos(struct hmap *map, struct hmap_key key) } struct hmap_link * -hmap_link_pop(struct hmap *map, struct hmap_key key) +hmap_link_pop(const struct hmap *map, struct hmap_key key) { struct hmap_link **iter; @@ -169,7 +169,7 @@ hmap_link_pop(struct hmap *map, struct hmap_key key) } int -hmap_link_alloc(struct hmap *map, struct hmap_link **out, +hmap_link_alloc(const struct hmap *map, struct hmap_link **out, struct hmap_key key, struct hmap_val value) { struct hmap_link *link; @@ -188,7 +188,7 @@ hmap_link_alloc(struct hmap *map, struct hmap_link **out, } struct hmap_link * -hmap_get(struct hmap *map, struct hmap_key key) +hmap_get(const struct hmap *map, struct hmap_key key) { struct hmap_link **iter; @@ -199,35 +199,40 @@ hmap_get(struct hmap *map, struct hmap_key key) return iter ? *iter : NULL; } -void -hmap_rm(struct hmap *map, struct hmap_key key) +int +hmap_set(const struct hmap *map, struct hmap_key key, struct hmap_val value) { - struct hmap_link *link; + struct hmap_link **iter; LIBHMAP_ABORT_ON_ARGS(!map); - link = hmap_link_pop(map, key); - if (!link) return; - map->allocator->free(link); + iter = hmap_link_pos(map, key); + if (!*iter) return HMAP_KEY_MISSING; + + (*iter)->value = value; + + return 0; } int -hmap_set(struct hmap *map, struct hmap_key key, struct hmap_val value) +hmap_rm(const struct hmap *map, struct hmap_key key) { - struct hmap_link **iter; + struct hmap_link *link; + int rc; LIBHMAP_ABORT_ON_ARGS(!map); - iter = hmap_link_pos(map, key); - if (!*iter) return HMAP_KEY_MISSING; + link = hmap_link_pop(map, key); + if (!link) return HMAP_KEY_MISSING; - (*iter)->value = value; + rc = map->allocator->free(link); + if (rc) return -rc; return 0; } int -hmap_add(struct hmap *map, struct hmap_key key, struct hmap_val value) +hmap_add(const struct hmap *map, struct hmap_key key, struct hmap_val value) { struct hmap_link **iter; int rc;