libhmap-c

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

commit a21f5271c522c5938d96af8218d3f24ff85401d9
parent a7042dfef6c4329776d0523bddb8c8c2288cc92c
Author: Louis Burda <quent.burda@gmail.com>
Date:   Mon, 20 Mar 2023 14:53:06 +0100

Zero-cost immediate storage for types with size < sizeof(void *)

Diffstat:
Minclude/hashmap.h | 22+++++++++++++++++-----
Msrc/hashmap.c | 10++++++----
Msrc/test.c | 10++++------
3 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/include/hashmap.h b/include/hashmap.h @@ -20,10 +20,20 @@ enum { HMAP_KEY_MISSING, }; +struct hashmap_val { + union { + char c; + float f; + int i; + unsigned int u; + void *p; + }; +}; + struct hashmap_link { void *key; size_t keysize; - void *value; + struct hashmap_val value; struct hashmap_link *next; }; @@ -59,15 +69,17 @@ struct hashmap_link **hashmap_link_pos(struct hashmap *map, 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, - void *key, size_t keysize, void *value); + void *key, size_t keysize, struct hashmap_val value); int hashmap_link_alloc(struct hashmap *map, struct hashmap_link **out, - void *key, size_t keysize, void *value); + void *key, size_t keysize, struct hashmap_val value); struct hashmap_link *hashmap_get(struct hashmap *map, const void *key, size_t size); void hashmap_rm(struct hashmap *map, const void *key, size_t size); -int hashmap_set(struct hashmap *map, void *key, size_t keysize, void *value); -int hashmap_add(struct hashmap *map, void *key, size_t keysize, void *value); +int hashmap_set(struct hashmap *map, void *key, size_t keysize, + struct hashmap_val value); +int hashmap_add(struct hashmap *map, void *key, size_t keysize, + struct hashmap_val value); void hashmap_iter_init(struct hashmap_iter *iter); bool hashmap_iter_next(const struct hashmap *map, struct hashmap_iter *iter); diff --git a/src/hashmap.c b/src/hashmap.c @@ -153,7 +153,7 @@ hashmap_link_pop(struct hashmap *map, const void *key, size_t keysize) void hashmap_link_set(struct hashmap *map, struct hashmap_link *link, - void *key, size_t keysize, void *value) + void *key, size_t keysize, struct hashmap_val value) { link->key = key; link->keysize = keysize; @@ -162,7 +162,7 @@ hashmap_link_set(struct hashmap *map, struct hashmap_link *link, int hashmap_link_alloc(struct hashmap *map, struct hashmap_link **out, - void *key, size_t keysize, void *value) + void *key, size_t keysize, struct hashmap_val value) { struct hashmap_link *link; int rc; @@ -199,7 +199,8 @@ hashmap_rm(struct hashmap *map, const void *key, size_t keysize) } int -hashmap_set(struct hashmap *map, void *key, size_t keysize, void *value) +hashmap_set(struct hashmap *map, void *key, size_t keysize, + struct hashmap_val value) { struct hashmap_link **iter; @@ -212,7 +213,8 @@ hashmap_set(struct hashmap *map, void *key, size_t keysize, void *value) } int -hashmap_add(struct hashmap *map, void *key, size_t keysize, void *value) +hashmap_add(struct hashmap *map, void *key, size_t keysize, + struct hashmap_val value) { struct hashmap_link **iter; int rc; diff --git a/src/test.c b/src/test.c @@ -12,7 +12,7 @@ main(int argc, const char **argv) { struct hashmap hashmap; struct hashmap_iter iter; - void *key, *value; + void *key; int i, rc; rc = hashmap_init(&hashmap, 10, hashmap_str_hasher, @@ -21,16 +21,14 @@ main(int argc, const char **argv) for (i = 1; i < argc; i++) { key = strdup(argv[i]); - value = malloc(sizeof(int)); - memcpy(value, &i, sizeof(int)); - rc = hashmap_set(&hashmap, key, strlen(key) + 1, - value, sizeof(int)); + rc = hashmap_add(&hashmap, key, strlen(key) + 1, + (struct hashmap_val) {.i = i}); if (rc) LIBHASHMAP_ERR(rc); } for (HASHMAP_ITER(&hashmap, &iter)) { printf("%s: %i\n", (char *)iter.link->key, - *(int*)iter.link->value); + iter.link->value.i); } hashmap_deinit(&hashmap);