diff options
author | Thomas Voss <mail@thomasvoss.com> | 2022-12-24 02:25:49 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2022-12-24 02:25:49 +0100 |
commit | 144bd88ff5146af4df99596606a7bf2a624723fc (patch) | |
tree | a8cadc390525894bb7f63b599505bdc5b9eb8677 | |
parent | 2ba320056c0f08c6171fd61766eb880f88b54b75 (diff) |
Simplify hashmaps by not freeing keys
-rw-r--r-- | examples/gehashmap.c | 6 | ||||
-rw-r--r-- | src/gehashmap.h | 15 |
2 files changed, 8 insertions, 13 deletions
diff --git a/examples/gehashmap.c b/examples/gehashmap.c index b7a86ff..1ddd391 100644 --- a/examples/gehashmap.c +++ b/examples/gehashmap.c @@ -62,9 +62,9 @@ main(void) atoimap_new(map_a = malloc(sizeof(atoimap_t))); itoumap_new(map_b = malloc(sizeof(itoumap_t))); - atoimap_set(map_a, strdup("Thomas Voß"), 5); - atoimap_set(map_a, strdup("THOMAS VOẞ"), 6); - atoimap_set(map_a, strdup("Thomas Voss"), 7); + atoimap_set(map_a, "Thomas Voß", 5); + atoimap_set(map_a, "THOMAS VOẞ", 6); + atoimap_set(map_a, "Thomas Voss", 7); atoimap_get(map_a, "Thomas Voß", &i); printf("Thomas Voß -> %d\n", i); atoimap_get(map_a, "THOMAS VOẞ", &i); printf("THOMAS VOẞ -> %d\n", i); atoimap_get(map_a, "Thomas Voss", &i); printf("Thomas Voss -> %d\n", i); diff --git a/src/gehashmap.h b/src/gehashmap.h index 0a3d7c1..311c2c3 100644 --- a/src/gehashmap.h +++ b/src/gehashmap.h @@ -1,5 +1,5 @@ -#ifndef GEHASHMAP_H -#define GEHASHMAP_H +#ifndef LIBGE_GEHASHMAP_H +#define LIBGE_GEHASHMAP_H #include <stdbool.h> #include <stdlib.h> @@ -32,7 +32,6 @@ int n##_remove(n##_t *, k); \ int n##_resize(n##_t *, size_t); \ bool n##_key_iseq(k, k); \ - void n##_key_free(k); \ size_t n##_key_hash(k); #define GEHASHMAP_IMPL(k, v, n) \ @@ -60,7 +59,6 @@ struct n##_entry *entry = map->entries[i]; \ while (entry != NULL) { \ struct n##_entry *next = entry->next; \ - n##_key_free(entry->key); \ free(entry); \ entry = next; \ } \ @@ -171,9 +169,8 @@ } \ \ /* Function to remove an element with a given key from a hashmap. If - * the value was dynamically allocated it will not be freed. The key - * will be freed however as per n##_key_free(). On error -1 is - * returned, otherwise 0 is returned. + * the key or value were dynamically allocated they will not be freed. + * On error -1 is returned, otherwise 0 is returned. */ \ int \ n##_remove(n##_t *map, k key) \ @@ -186,7 +183,6 @@ \ if (n##_key_iseq(entry->key, key)) { \ map->entries[hash] = entry->next; \ - n##_key_free(entry->key); \ free(entry); \ map->size--; \ return 0; \ @@ -196,7 +192,6 @@ if (n##_key_iseq(entry->next->key, key)) { \ struct n##_entry *next = entry->next; \ entry->next = next->next; \ - n##_key_free(next->key); \ free(next); \ map->size--; \ return 0; \ @@ -207,4 +202,4 @@ return -1; \ } -#endif /* !GEHASHMAP_H */ +#endif /* !LIBGE_GEHASHMAP_H */ |