libhmap-c

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

commit b48c5c8366eda4013eae00186578b0c77b46d378
parent a104a2e0a0287e7d75fa7408634485b8d0c24f1b
Author: Louis Burda <quent.burda@gmail.com>
Date:   Mon, 20 Mar 2023 02:28:43 +0100

Add _link_pos for allocating new links efficiently

Diffstat:
Minclude/hashmap.h | 2++
Mlibhashmap.api | 1+
Mlibhashmap.lds | 1+
Msrc/hashmap.c | 33+++++++++++++++++++++------------
4 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/include/hashmap.h b/include/hashmap.h @@ -47,6 +47,8 @@ void hashmap_clear(struct hashmap *map); struct hashmap_link **hashmap_link_get(struct hashmap *map, const void *key, size_t size); +struct hashmap_link **hashmap_link_pos(struct hashmap *map, + const void *key, size_t size); struct hashmap_link *hashmap_link_pop(struct hashmap *map, const void *key, size_t size); void hashmap_link_set(struct hashmap *map, struct hashmap_link *link, diff --git a/libhashmap.api b/libhashmap.api @@ -8,6 +8,7 @@ hashmap_swap hashmap_clear hashmap_link_get +hashmap_link_pos hashmap_link_pop hashmap_link_set hashmap_link_alloc diff --git a/libhashmap.lds b/libhashmap.lds @@ -9,6 +9,7 @@ LIBHASHMAP_1.3 { hashmap_clear; hashmap_link_get; + hashmap_link_pos; hashmap_link_pop; hashmap_link_set; hashmap_link_alloc; diff --git a/src/hashmap.c b/src/hashmap.c @@ -121,12 +121,26 @@ hashmap_link_get(struct hashmap *map, const void *key, size_t size) return NULL; } +struct hashmap_link ** +hashmap_link_pos(struct hashmap *map, const void *key, size_t keysize) +{ + struct hashmap_link **iter; + + iter = hashmap_link_get(map, key, keysize); + if (iter == NULL) { + iter = &map->buckets[hashmap_key_bucket(map, key, keysize)]; + while (*iter) iter = &((*iter)->next); + } + + return iter; +} + struct hashmap_link * -hashmap_link_pop(struct hashmap *map, const void *key, size_t size) +hashmap_link_pop(struct hashmap *map, const void *key, size_t keysize) { struct hashmap_link **iter; - iter = hashmap_link_get(map, key, size); + iter = hashmap_link_get(map, key, keysize); if (iter) { *iter = (*iter)->next; return *iter; @@ -163,21 +177,21 @@ hashmap_link_alloc(struct hashmap *map, struct hashmap_link **out, } struct hashmap_link * -hashmap_get(struct hashmap *map, const void *key, size_t size) +hashmap_get(struct hashmap *map, const void *key, size_t keysize) { struct hashmap_link **iter; - iter = hashmap_link_get(map, key, size); + iter = hashmap_link_get(map, key, keysize); return iter ? *iter : NULL; } void -hashmap_rm(struct hashmap *map, const void *key, size_t size) +hashmap_rm(struct hashmap *map, const void *key, size_t keysize) { struct hashmap_link *link; - link = hashmap_link_pop(map, key, size); + link = hashmap_link_pop(map, key, keysize); if (!link) return; map->allocator->free(link); } @@ -188,12 +202,7 @@ hashmap_set(struct hashmap *map, void *key, size_t keysize, void *value) struct hashmap_link **iter; int rc; - iter = hashmap_link_get(map, key, keysize); - if (iter == NULL) { - iter = &map->buckets[hashmap_key_bucket(map, key, keysize)]; - while (*iter) iter = &((*iter)->next); - } - + iter = hashmap_link_pos(map, key, keysize); if (*iter) { hashmap_link_set(map, *iter, key, keysize, value); } else {