Make as many variables const as possible.
This commit is contained in:
parent
cd822fedfc
commit
1d037dd31e
18
include/ht.h
18
include/ht.h
|
@ -45,21 +45,21 @@ struct ht_s {
|
||||||
* @param input the data being hashed.
|
* @param input the data being hashed.
|
||||||
* @return the data's hash.
|
* @return the data's hash.
|
||||||
*/
|
*/
|
||||||
unsigned long (*hash_func)(void* input);
|
unsigned long (*hash_func)(const void* input);
|
||||||
/**
|
/**
|
||||||
* Function to compare two keys.
|
* Function to compare two keys.
|
||||||
* @param given_key the key being used to retrieve data
|
* @param given_key the key being used to retrieve data
|
||||||
* @param current_key the key of a hash table node
|
* @param current_key the key of a hash table node
|
||||||
* @return nonzero value if the keys match.
|
* @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
|
* Function used to duplicate the given key and
|
||||||
* store it in a node.
|
* store it in a node.
|
||||||
* @param key the key to copy
|
* @param key the key to copy
|
||||||
* @return pointer to a new key.
|
* @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.
|
* Function used to free a previously copied key.
|
||||||
* @param key the key to free.
|
* @param key the key to free.
|
||||||
|
@ -76,7 +76,7 @@ typedef struct ht_s ht;
|
||||||
* @param key the data to hash.
|
* @param key the data to hash.
|
||||||
* @return the produced hashed integer.
|
* @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.
|
* The default key comparison function.
|
||||||
* Assumes both keys are strings and uses strcmp.
|
* 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
|
* @param b the key compared against
|
||||||
* @return true if the keys represent the same string.
|
* @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.
|
* The default key copy function.
|
||||||
* @param key the key being copied
|
* @param key the key being copied
|
||||||
* @return a pointer to the copy of the key.
|
* @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()
|
* The default key free function. Simply calls free()
|
||||||
* @param key the key to free.
|
* @param key the key to free.
|
||||||
|
@ -117,20 +117,20 @@ void ht_free(ht* ht);
|
||||||
* @param value the value to store.
|
* @param value the value to store.
|
||||||
* @return LIBDS_SUCCESS if all goes well, LIBDS_MALLOC if an allocation fails.
|
* @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.
|
* Retrieves a value from the hash table.
|
||||||
* @param ht the hash table to retrieve a value from.
|
* @param ht the hash table to retrieve a value from.
|
||||||
* @param key the key to use to find the data.
|
* @param key the key to use to find the data.
|
||||||
* @return the data, or NULL if it is not found.
|
* @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.
|
* Removes a value from the hash table.
|
||||||
* @param ht the hash table to remove a value from.
|
* @param ht the hash table to remove a value from.
|
||||||
* @param key the key to use to find the data.
|
* @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
|
* Runs through every element in the hash table, and compares it against the
|
||||||
|
|
|
@ -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
|
* The function should take two elements, and, if they match, return 1. Otherwise, it should
|
||||||
* return 0.
|
* return 0.
|
||||||
*/
|
*/
|
||||||
typedef int (*compare_func)(void*, void*);
|
typedef int (*compare_func)(const void*, const void*);
|
||||||
/**
|
/**
|
||||||
* Foreach function type.
|
* Foreach function type.
|
||||||
* Foreach functions are passed to _foreach calls.
|
* 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
|
* @param b The second piece of data being compared
|
||||||
* @return always true, i.e 1
|
* @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 ==.
|
* A compare_func implementation that compares two pointers using ==.
|
||||||
* @param a the first piece of data being compared
|
* @param a the first piece of data being compared
|
||||||
* @param b the second piece of data being compared
|
* @param b the second piece of data being compared
|
||||||
* @return true if a and b are the same value.
|
* @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
|
#endif
|
||||||
|
|
14
src/ht.c
14
src/ht.c
|
@ -2,11 +2,11 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
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_prime = 16777619;
|
||||||
const unsigned long fnv_offset_basis = 2166136261;
|
const unsigned long fnv_offset_basis = 2166136261;
|
||||||
unsigned long hash;
|
unsigned long hash;
|
||||||
char* string;
|
const char* string;
|
||||||
hash = fnv_offset_basis;
|
hash = fnv_offset_basis;
|
||||||
string = data;
|
string = data;
|
||||||
while (*string) {
|
while (*string) {
|
||||||
|
@ -15,8 +15,8 @@ unsigned long ht_default_hash_func(void* data) {
|
||||||
}
|
}
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
int ht_default_cmp_func(void* a, void* b) { return strcmp(a, b) == 0; }
|
int ht_default_cmp_func(const void* a, const void* b) { return strcmp(a, b) == 0; }
|
||||||
void* ht_default_copy_func(void* from) {
|
void* ht_default_copy_func(const void* from) {
|
||||||
void* copy = malloc(sizeof(char) * (strlen(from) + 1));
|
void* copy = malloc(sizeof(char) * (strlen(from) + 1));
|
||||||
if (copy) {
|
if (copy) {
|
||||||
strcpy(copy, from);
|
strcpy(copy, from);
|
||||||
|
@ -48,7 +48,7 @@ void ht_free(ht* ht) {
|
||||||
ht->hash_func = NULL;
|
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;
|
libds_result result = LIBDS_SUCCESS;
|
||||||
ht_node* new_node = NULL;
|
ht_node* new_node = NULL;
|
||||||
void* new_key = NULL;
|
void* new_key = NULL;
|
||||||
|
@ -69,7 +69,7 @@ libds_result ht_put(ht* ht, void* key, void* data) {
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
void* ht_get(ht* ht, void* key) {
|
void* ht_get(ht* ht, const void* key) {
|
||||||
void* data = NULL;
|
void* data = NULL;
|
||||||
ht_node* search_node;
|
ht_node* search_node;
|
||||||
unsigned long key_int = ht->hash_func(key) % LIBDS_HT_SIZE;
|
unsigned long key_int = ht->hash_func(key) % LIBDS_HT_SIZE;
|
||||||
|
@ -84,7 +84,7 @@ void* ht_get(ht* ht, void* key) {
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
void ht_remove(ht* ht, void* key) {
|
void ht_remove(ht* ht, const void* key) {
|
||||||
ht_node** search_ptr;
|
ht_node** search_ptr;
|
||||||
unsigned long key_int = ht->hash_func(key) % LIBDS_HT_SIZE;
|
unsigned long key_int = ht->hash_func(key) % LIBDS_HT_SIZE;
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#include "libds.h"
|
#include "libds.h"
|
||||||
|
|
||||||
int compare_always(void* a, void* b) { return 1; }
|
int compare_always(const void* a, const void* b) { return 1; }
|
||||||
int compare_pointer(void* a, void* b) { return a == b; }
|
int compare_pointer(const void* a, const void* b) { return a == b; }
|
||||||
|
|
|
@ -14,7 +14,7 @@ int _test_vec_foreach_func(void* data, va_list args) {
|
||||||
*sum += *real_data;
|
*sum += *real_data;
|
||||||
return 0;
|
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() {
|
int test_vec_basic() {
|
||||||
vec test_vec;
|
vec test_vec;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user