Allow for arbitrary value types

This commit is contained in:
Nicholas Chambers 2018-05-19 21:36:01 -05:00
parent c9bf7c7a52
commit d1ef28b489
7 changed files with 17 additions and 28 deletions

View File

@ -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})

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -1,5 +1,4 @@
#include <hashmap.h>
#include <stdio.h>
#include <string.h>
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);
}

View File

@ -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;

View File

@ -1,7 +1,7 @@
#include <node.h>
#include <stdlib.h>
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;