diff --git a/include/ht.h b/include/ht.h index 4f3586a..73b669b 100644 --- a/include/ht.h +++ b/include/ht.h @@ -45,21 +45,21 @@ struct ht_s { * @param input the data being hashed. * @return the data's hash. */ - unsigned long (*hash_func)(void* input); + unsigned long (*hash_func)(const void* input); /** * Function to compare two keys. * @param given_key the key being used to retrieve data * @param current_key the key of a hash table node * @return nonzero value if the keys match. */ - int (*cmp_func)(void* given_key, void* current_key); + int (*cmp_func)(const void* given_key, const void* current_key); /** * Function used to duplicate the given key and * store it in a node. * @param key the key to copy * @return pointer to a new key. */ - void* (*copy_func)(void* key); + void* (*copy_func)(const void* key); /** * Function used to free a previously copied key. * @param key the key to free. @@ -76,7 +76,7 @@ typedef struct ht_s ht; * @param key the data to hash. * @return the produced hashed integer. */ -unsigned long ht_default_hash_func(void* key); +unsigned long ht_default_hash_func(const void* key); /** * The default key comparison function. * Assumes both keys are strings and uses strcmp. @@ -84,13 +84,13 @@ unsigned long ht_default_hash_func(void* key); * @param b the key compared against * @return true if the keys represent the same string. */ -int ht_default_cmp_func(void* a, void* b); +int ht_default_cmp_func(const void* a, const void* b); /** * The default key copy function. * @param key the key being copied * @return a pointer to the copy of the key. */ -void* ht_default_copy_func(void* key); +void* ht_default_copy_func(const void* key); /** * The default key free function. Simply calls free() * @param key the key to free. @@ -117,20 +117,20 @@ void ht_free(ht* ht); * @param value the value to store. * @return LIBDS_SUCCESS if all goes well, LIBDS_MALLOC if an allocation fails. */ -libds_result ht_put(ht* ht, void* key, void* value); +libds_result ht_put(ht* ht, const void* key, void* value); /** * Retrieves a value from the hash table. * @param ht the hash table to retrieve a value from. * @param key the key to use to find the data. * @return the data, or NULL if it is not found. */ -void* ht_get(ht* ht, void* key); +void* ht_get(ht* ht, const void* key); /** * Removes a value from the hash table. * @param ht the hash table to remove a value from. * @param key the key to use to find the data. */ -void ht_remove(ht* ht, void* key); +void ht_remove(ht* ht, const void* key); /** * Runs through every element in the hash table, and compares it against the diff --git a/include/libds.h b/include/libds.h index 0c9b566..d951a7b 100644 --- a/include/libds.h +++ b/include/libds.h @@ -15,7 +15,7 @@ typedef enum libds_result_e libds_result; * The function should take two elements, and, if they match, return 1. Otherwise, it should * return 0. */ -typedef int (*compare_func)(void*, void*); +typedef int (*compare_func)(const void*, const void*); /** * Foreach function type. * Foreach functions are passed to _foreach calls. @@ -29,13 +29,13 @@ typedef int (*foreach_func)(void*, va_list); * @param b The second piece of data being compared * @return always true, i.e 1 */ -int compare_always(void* a, void* b); +int compare_always(const void* a, const void* b); /** * A compare_func implementation that compares two pointers using ==. * @param a the first piece of data being compared * @param b the second piece of data being compared * @return true if a and b are the same value. */ -int compare_pointer(void* a, void* b); +int compare_pointer(const void* a, const void* b); #endif diff --git a/src/ht.c b/src/ht.c index 7a3c48e..ba3d6e9 100644 --- a/src/ht.c +++ b/src/ht.c @@ -2,11 +2,11 @@ #include #include -unsigned long ht_default_hash_func(void* data) { +unsigned long ht_default_hash_func(const void* data) { const unsigned long fnv_prime = 16777619; const unsigned long fnv_offset_basis = 2166136261; unsigned long hash; - char* string; + const char* string; hash = fnv_offset_basis; string = data; while (*string) { @@ -15,8 +15,8 @@ unsigned long ht_default_hash_func(void* data) { } return hash; } -int ht_default_cmp_func(void* a, void* b) { return strcmp(a, b) == 0; } -void* ht_default_copy_func(void* from) { +int ht_default_cmp_func(const void* a, const void* b) { return strcmp(a, b) == 0; } +void* ht_default_copy_func(const void* from) { void* copy = malloc(sizeof(char) * (strlen(from) + 1)); if (copy) { strcpy(copy, from); @@ -48,7 +48,7 @@ void ht_free(ht* ht) { ht->hash_func = NULL; } -libds_result ht_put(ht* ht, void* key, void* data) { +libds_result ht_put(ht* ht, const void* key, void* data) { libds_result result = LIBDS_SUCCESS; ht_node* new_node = NULL; void* new_key = NULL; @@ -69,7 +69,7 @@ libds_result ht_put(ht* ht, void* key, void* data) { return result; } -void* ht_get(ht* ht, void* key) { +void* ht_get(ht* ht, const void* key) { void* data = NULL; ht_node* search_node; unsigned long key_int = ht->hash_func(key) % LIBDS_HT_SIZE; @@ -84,7 +84,7 @@ void* ht_get(ht* ht, void* key) { return data; } -void ht_remove(ht* ht, void* key) { +void ht_remove(ht* ht, const void* key) { ht_node** search_ptr; unsigned long key_int = ht->hash_func(key) % LIBDS_HT_SIZE; diff --git a/src/libds.c b/src/libds.c index 6b07d43..24843ca 100644 --- a/src/libds.c +++ b/src/libds.c @@ -1,4 +1,4 @@ #include "libds.h" -int compare_always(void* a, void* b) { return 1; } -int compare_pointer(void* a, void* b) { return a == b; } +int compare_always(const void* a, const void* b) { return 1; } +int compare_pointer(const void* a, const void* b) { return a == b; } diff --git a/src/main.c b/src/main.c index 2caf821..9b4da95 100644 --- a/src/main.c +++ b/src/main.c @@ -14,7 +14,7 @@ int _test_vec_foreach_func(void* data, va_list args) { *sum += *real_data; return 0; } -int _test_vec_find_string(void* a, void* b) { return strcmp(a, b) == 0; } +int _test_vec_find_string(const void* a, const void* b) { return strcmp(a, b) == 0; } int test_vec_basic() { vec test_vec;