Allow for arbitrary value types
This commit is contained in:
parent
c9bf7c7a52
commit
d1ef28b489
|
@ -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)
|
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 VERSION ${PROJECT_VERSION})
|
||||||
set_target_properties(hashmap PROPERTIES SOVERSION 1)
|
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 include)
|
||||||
# target_include_directories(hashmap PRIVATE src)
|
|
||||||
include(GNUInstallDirs)
|
include(GNUInstallDirs)
|
||||||
configure_file(hashmap.pc.in hashmap.pc @ONLY)
|
configure_file(hashmap.pc.in hashmap.pc @ONLY)
|
||||||
install(TARGETS hashmap LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
|
install(TARGETS hashmap LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
|
||||||
|
|
|
@ -10,11 +10,10 @@ struct hashmap {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct hashmap new_map();
|
struct hashmap new_map();
|
||||||
void insert_map(struct hashmap *map, const char *key, const char *value);
|
void insert_map(struct hashmap *map, const char *key, const void *value);
|
||||||
const char *lookup_map(struct hashmap *map, const char *key);
|
const void *lookup_map(struct hashmap *map, const char *key);
|
||||||
int exists_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);
|
const void *remove_map(struct hashmap *map, const char *key);
|
||||||
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));
|
||||||
void print_map(struct hashmap *map);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -10,7 +10,7 @@ struct ll {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ll new_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);
|
void remove_ll(struct ll *list, struct node *link);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -5,10 +5,10 @@ struct node {
|
||||||
struct node *next;
|
struct node *next;
|
||||||
struct node *prev;
|
struct node *prev;
|
||||||
const char *key;
|
const char *key;
|
||||||
const char *value;
|
const void *value;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct node *new_node(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 char *value);
|
struct node *insert_node(struct node *entry, const char *key, const void *value);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
#include <hashmap.h>
|
#include <hashmap.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
struct hashmap new_map() {
|
struct hashmap new_map() {
|
||||||
|
@ -28,7 +27,7 @@ static unsigned int hash_map(const char *str) {
|
||||||
return val % BUCKET_SIZE;
|
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) {
|
if(key == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -45,7 +44,7 @@ void insert_map(struct hashmap *map, const char *key, const char *value) {
|
||||||
insert_ll(&map->buckets[index], key, 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) {
|
if(key == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -65,7 +64,7 @@ int exists_map(struct hashmap *map, const char *key) {
|
||||||
return lookup_map(map, key) != NULL;
|
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) {
|
if(key == NULL) {
|
||||||
return 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) {
|
for(struct node *iter = map->buckets[index].head; iter != NULL; iter = iter->next) {
|
||||||
if(strcmp(iter->key, key) == 0) {
|
if(strcmp(iter->key, key) == 0) {
|
||||||
const char *value = iter->value;
|
const void *value = iter->value;
|
||||||
remove_ll(&map->buckets[index], iter);
|
remove_ll(&map->buckets[index], iter);
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
@ -83,18 +82,10 @@ const char *remove_map(struct hashmap *map, const char *key) {
|
||||||
return NULL;
|
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(int count = 0; count < BUCKET_SIZE; ++count) {
|
||||||
for(struct node *iter = map->buckets[count].head; iter != NULL; iter = iter->next) {
|
for(struct node *iter = map->buckets[count].head; iter != NULL; iter = iter->next) {
|
||||||
cb(iter->key, iter->value);
|
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);
|
|
||||||
}
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ struct ll new_ll() {
|
||||||
return new;
|
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->list = insert_node(list->tail, key, value);
|
||||||
list->tail = list->list;
|
list->tail = list->list;
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#include <node.h>
|
#include <node.h>
|
||||||
#include <stdlib.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));
|
struct node *new = calloc(1, sizeof(struct node));
|
||||||
new->next = NULL;
|
new->next = NULL;
|
||||||
new->prev = NULL;
|
new->prev = NULL;
|
||||||
|
@ -10,7 +10,7 @@ struct node *new_node(const char *key, const char *value) {
|
||||||
return new;
|
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);
|
struct node *insert = new_node(key, value);
|
||||||
insert->prev = entry;
|
insert->prev = entry;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue