diff --git a/hmap.c b/hmap.c index a0bf538..ed843ef 100644 --- a/hmap.c +++ b/hmap.c @@ -66,24 +66,6 @@ uint64_t hash_str_fnv1a_64(char * s) { return hash; } -struct _map_node { - struct _map_node *next; - void * key; - uint64_t hash; - void *data; -}; - -typedef struct _map_node hmap_node; - -typedef struct { - size_t capacity; - size_t len; - size_t keysize; - hmap_node *buckets; -} hmap; - -void* hmap_insert(hmap * map, void *key, void *data); - hmap_node* hmap_node_init(void *key, size_t keysize, void *data) { hmap_node *node; uint64_t hash; @@ -242,13 +224,15 @@ void* hmap_remove(hmap *map, void *key) { ret = current->data; map->len--; if (previous != NULL) { - previous->next = current; + previous->next = current->next; } else { map->buckets[idx] = *current->next; } + free(current); break; } else { current = current->next; + previous = current; } } diff --git a/include/haggis_private.h b/include/haggis_private.h index 4ea6b9e..3539e06 100644 --- a/include/haggis_private.h +++ b/include/haggis_private.h @@ -64,4 +64,31 @@ int haggis_store_filetype(FILE *stream, haggis_filetype *filetype); uint64_t hash_fnv1a_64(uint8_t *key, size_t len); uint64_t hash_str_fnv1a_64(char *s); +struct _map_node { + struct _map_node *next; + void * key; + uint64_t hash; + void *data; +}; + +typedef struct _map_node hmap_node; + +typedef struct { + size_t capacity; + size_t len; + size_t keysize; + hmap_node *buckets; +} hmap; + +hmap_node* hmap_node_init(void *key, size_t keysize, void *data); +void hmap_node_deinit(hmap_node *node); +void* hmap_node_attach(hmap_node *root, hmap_node *leaf, size_t keysize); +void* hmap_node_search(hmap_node *root, void *key, size_t keysize); +hmap* hmap_init(size_t keysize); +void hmap_deinit(hmap *map); +int hmap_expand(hmap *map); +void* hmap_insert(hmap *map, void *key, void *data); +void* hmap_get(hmap *map, void *key); +void* hmap_remove(hmap *map, void *key); + #endif // !HAGGIS_PRIVATE_H diff --git a/test/Makefile b/test/Makefile index 5885f3d..4b3bbf9 100644 --- a/test/Makefile +++ b/test/Makefile @@ -61,6 +61,9 @@ tests += store_file_sha256 tests += load_file_sha256 tests += fnv1a_hash_inode tests += fnv1a_hash_str +tests += init_hmap +tests += hmap_insert +tests += hmap_get total != echo $(tests) | wc -w | awk '{ print $$1 }' diff --git a/test/hmap_get.c b/test/hmap_get.c new file mode 100644 index 0000000..ce8f8ea --- /dev/null +++ b/test/hmap_get.c @@ -0,0 +1,29 @@ +#include "haggis.h" +#include "haggis_private.h" +#include +#include +#include +#include + +int main() { + hmap *map; + struct stat st; + char *data; + union { + ino_t val; + u8 bytes[sizeof(ino_t)]; + } nod; + char *ret; + + map = hmap_init(sizeof(ino_t)); + assert(map != NULL); + data = "Makefile"; + stat(data, &st); + nod.val = st.st_ino; + hmap_insert(map, &nod.bytes[0], data); + assert(map->len == 1); + ret = hmap_get(map, &nod.bytes[0]); + assert(memcmp(ret, data, 9) == 0); + hmap_deinit(map); + return 0; +} diff --git a/test/hmap_insert.c b/test/hmap_insert.c new file mode 100644 index 0000000..8a4bc2d --- /dev/null +++ b/test/hmap_insert.c @@ -0,0 +1,26 @@ +#include "haggis.h" +#include "haggis_private.h" +#include +#include +#include +#include + +int main() { + hmap *map; + struct stat st; + char *data; + union { + ino_t val; + u8 bytes[sizeof(ino_t)]; + } nod; + + map = hmap_init(sizeof(ino_t)); + assert(map != NULL); + data = "Makefile"; + stat(data, &st); + nod.val = st.st_ino; + hmap_insert(map, &nod.bytes[0], data); + assert(map->len == 1); + hmap_deinit(map); + return 0; +} diff --git a/test/init_hmap.c b/test/init_hmap.c new file mode 100644 index 0000000..cbd0219 --- /dev/null +++ b/test/init_hmap.c @@ -0,0 +1,12 @@ +#include "haggis_private.h" +#include +#include + +int main() { + hmap *map; + + map = hmap_init(sizeof(ino_t)); + assert(map != NULL); + hmap_deinit(map); + return 0; +}