diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ea6eff..39aaab0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,9 +3,8 @@ project(hashmap VERSION 1.0.1 DESCRIPTION "A small hashmap written in C to use w add_library(hashmap SHARED src src/hashmap.c src/linkedlist.c src/node.c) set_target_properties(hashmap PROPERTIES VERSION ${PROJECT_VERSION}) set_target_properties(hashmap PROPERTIES SOVERSION 1) -set_target_properties(hashmap PROPERTIES PUBLIC_HEADER include/hashmap.c) +set_target_properties(hashmap PROPERTIES PUBLIC_HEADER "include/hashmap.h;include/linkedlist.h;include/node.h") target_include_directories(hashmap PRIVATE include) -# target_include_directories(hashmap PRIVATE src) include(GNUInstallDirs) configure_file(hashmap.pc.in hashmap.pc @ONLY) install(TARGETS hashmap LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) diff --git a/include/hashmap.h b/include/hashmap.h index dc2b118..b8b0a20 100644 --- a/include/hashmap.h +++ b/include/hashmap.h @@ -10,11 +10,10 @@ struct hashmap { }; struct hashmap new_map(); -void insert_map(struct hashmap *map, const char *key, const char *value); -const char *lookup_map(struct hashmap *map, const char *key); +void insert_map(struct hashmap *map, const char *key, const void *value); +const void *lookup_map(struct hashmap *map, const char *key); int exists_map(struct hashmap *map, const char *key); -const char *remove_map(struct hashmap *map, const char *key); -void foreach_map(struct hashmap *map, void (*cb)(const char *key, const char *value)); -void print_map(struct hashmap *map); +const void *remove_map(struct hashmap *map, const char *key); +void foreach_map(struct hashmap *map, void (*cb)(const char *key, const void *value)); #endif diff --git a/include/linkedlist.h b/include/linkedlist.h index 68fed91..73a2941 100644 --- a/include/linkedlist.h +++ b/include/linkedlist.h @@ -10,7 +10,7 @@ struct ll { }; struct ll new_ll(); -void insert_ll(struct ll *list, const char *key, const char *value); +void insert_ll(struct ll *list, const char *key, const void *value); void remove_ll(struct ll *list, struct node *link); #endif diff --git a/include/node.h b/include/node.h index c3456f5..b2ca773 100644 --- a/include/node.h +++ b/include/node.h @@ -5,10 +5,10 @@ struct node { struct node *next; struct node *prev; const char *key; - const char *value; + const void *value; }; -struct node *new_node(const char *key, const char *value); -struct node *insert_node(struct node *entry, const char *key, const char *value); +struct node *new_node(const char *key, const void *value); +struct node *insert_node(struct node *entry, const char *key, const void *value); #endif diff --git a/src/hashmap.c b/src/hashmap.c index c54fb2d..f7ce349 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -1,5 +1,4 @@ #include -#include #include struct hashmap new_map() { @@ -28,7 +27,7 @@ static unsigned int hash_map(const char *str) { return val % BUCKET_SIZE; } -void insert_map(struct hashmap *map, const char *key, const char *value) { +void insert_map(struct hashmap *map, const char *key, const void *value) { if(key == NULL) { return; } @@ -45,7 +44,7 @@ void insert_map(struct hashmap *map, const char *key, const char *value) { insert_ll(&map->buckets[index], key, value); } -const char *lookup_map(struct hashmap *map, const char *key) { +const void *lookup_map(struct hashmap *map, const char *key) { if(key == NULL) { return NULL; } @@ -65,7 +64,7 @@ int exists_map(struct hashmap *map, const char *key) { return lookup_map(map, key) != NULL; } -const char *remove_map(struct hashmap *map, const char *key) { +const void *remove_map(struct hashmap *map, const char *key) { if(key == NULL) { return NULL; } @@ -74,7 +73,7 @@ const char *remove_map(struct hashmap *map, const char *key) { for(struct node *iter = map->buckets[index].head; iter != NULL; iter = iter->next) { if(strcmp(iter->key, key) == 0) { - const char *value = iter->value; + const void *value = iter->value; remove_ll(&map->buckets[index], iter); return value; } @@ -83,18 +82,10 @@ const char *remove_map(struct hashmap *map, const char *key) { return NULL; } -void foreach_map(struct hashmap *map, void (*cb)(const char *key, const char *value)) { +void foreach_map(struct hashmap *map, void (*cb)(const char *key, const void *value)) { for(int count = 0; count < BUCKET_SIZE; ++count) { for(struct node *iter = map->buckets[count].head; iter != NULL; iter = iter->next) { cb(iter->key, iter->value); } } } - -static void print_value_map(const char *key, const char *value) { - printf("%s: %s\n", key, value); -} - -void print_map(struct hashmap *map) { - foreach_map(map, &print_value_map); -} diff --git a/src/linkedlist.c b/src/linkedlist.c index e1ab46b..e3abc6d 100644 --- a/src/linkedlist.c +++ b/src/linkedlist.c @@ -9,7 +9,7 @@ struct ll new_ll() { return new; } -void insert_ll(struct ll *list, const char *key, const char *value) { +void insert_ll(struct ll *list, const char *key, const void *value) { list->list = insert_node(list->tail, key, value); list->tail = list->list; diff --git a/src/node.c b/src/node.c index 14ce831..02d8c32 100644 --- a/src/node.c +++ b/src/node.c @@ -1,7 +1,7 @@ #include #include -struct node *new_node(const char *key, const char *value) { +struct node *new_node(const char *key, const void *value) { struct node *new = calloc(1, sizeof(struct node)); new->next = NULL; new->prev = NULL; @@ -10,7 +10,7 @@ struct node *new_node(const char *key, const char *value) { return new; } -struct node *insert_node(struct node *entry, const char *key, const char *value) { +struct node *insert_node(struct node *entry, const char *key, const void *value) { struct node *insert = new_node(key, value); insert->prev = entry;