From 9d8f79521da20fe77d8b7ac50fc450f363bb738e Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sat, 24 Mar 2018 17:14:09 -0700 Subject: [PATCH] Use const where applicable to allow for const data structures. --- include/ht.h | 6 +++--- include/ll.h | 8 ++++---- include/sprs.h | 8 ++++---- include/vec.h | 6 +++--- src/ht.c | 6 +++--- src/ll.c | 8 ++++---- src/sprs.c | 8 ++++---- src/vec.c | 6 +++--- 8 files changed, 28 insertions(+), 28 deletions(-) diff --git a/include/ht.h b/include/ht.h index 444ac3a..d5b50fe 100644 --- a/include/ht.h +++ b/include/ht.h @@ -124,7 +124,7 @@ libds_result ht_put(ht* ht, const void* key, void* value); * @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, const void* key); +void* ht_get(const ht* ht, const void* key); /** * Retreives the first value from the hash table * that's stored with the given key and passes the compare function. @@ -134,7 +134,7 @@ void* ht_get(ht* ht, const void* key); * @param compare the comparison function used to compare adta. * @return the data, or NULL if it is not found. */ -void* ht_find(ht* ht, const void* key, void* data, compare_func compare); +void* ht_find(const ht* ht, const void* key, void* data, compare_func compare); /** * Removes a value from the hash table. * @param ht the hash table to remove a value from. @@ -154,6 +154,6 @@ void ht_remove(ht* ht, const void* key); * @param ... variable arguments to be passed to the foreach function. * @return the code returned by the foreach functions. */ -int ht_foreach(ht* ht, void* data, compare_func compare, foreach_func foreach, ...); +int ht_foreach(const ht* ht, void* data, compare_func compare, foreach_func foreach, ...); #endif diff --git a/include/ll.h b/include/ll.h index 0bcfee6..9165ef2 100644 --- a/include/ll.h +++ b/include/ll.h @@ -82,7 +82,7 @@ void ll_remove(ll* ll, void* data); * @param compare the comparison function * @return the first element that is matched by the comparison function, or NULL if none are matched. */ -void* ll_find(ll* ll, void* data, compare_func compare); +void* ll_find(const ll* ll, void* data, compare_func compare); /** * Runs through every element in the linked list, and compares it against the * given data using the given comparison function. If the comparison function returns @@ -95,20 +95,20 @@ void* ll_find(ll* ll, void* data, compare_func compare); * @param ... variable arguments to be passed on to the foreach function * @return 0 if all goes well, or the first nonzero code returned by foreach. */ -int ll_foreach(ll* ll, void* data, compare_func compare, foreach_func foreach, ...); +int ll_foreach(const ll* ll, void* data, compare_func compare, foreach_func foreach, ...); /** * Gets the element at the beginning of the linked list. * @param ll the linked list to get the data from. * @return the element at the beginning of the element, or NULL if there is none. */ -void* ll_head(ll* ll); +void* ll_head(const ll* ll); /** * Gets the element at the end of the linked list. * @param ll the linked list to get the data from. * @return the element at the end of the element, or NULL if there is none. */ -void* ll_tail(ll* ll); +void* ll_tail(const ll* ll); /** * Removes the element at the beginning of the linked list, * and returns it. diff --git a/include/sprs.h b/include/sprs.h index 3435251..5e28abc 100644 --- a/include/sprs.h +++ b/include/sprs.h @@ -111,7 +111,7 @@ libds_result sprs_put_grow(sprs* sprs, size_t index, void* data); * @param index the index for which to check. * @return 1 if the index exists, 0 if not. */ -int sprs_contains(sprs* sprs, size_t index); +int sprs_contains(const sprs* sprs, size_t index); /** * Gets the value stored under the given sparse set index. * This will check for whether the index is in the sparse set first, @@ -123,7 +123,7 @@ int sprs_contains(sprs* sprs, size_t index); * @param index the index from under which to retrieve the value. * @return the value stored under the index, or NULL if there is nothing there. */ -void* sprs_get(sprs* sprs, size_t index); +void* sprs_get(const sprs* sprs, size_t index); /** * Runs through every element in the sparse set, and compares it against the @@ -135,7 +135,7 @@ void* sprs_get(sprs* sprs, size_t index); * @param compare the comparison function * @return the first element that is matched by the comparison function, or NULL if none are matched. */ -void* sprs_find(sprs* sprs, void* data, compare_func compare); +void* sprs_find(const sprs* sprs, void* data, compare_func compare); /** * Runs through every element in the sparse set, and compares it against the * given data using the given comparison function. If the comparison function returns @@ -148,6 +148,6 @@ void* sprs_find(sprs* sprs, void* data, compare_func compare); * @param ... variable arguments to be passed on to the foreach function * @return 0 if all goes well, or the first nonzero code returned by foreach. */ -int sprs_foreach(sprs* sprs, void* data, compare_func compare, foreach_func foreach, ...); +int sprs_foreach(const sprs* sprs, void* data, compare_func compare, foreach_func foreach, ...); #endif diff --git a/include/vec.h b/include/vec.h index 152210e..42c10a0 100644 --- a/include/vec.h +++ b/include/vec.h @@ -67,7 +67,7 @@ void vec_remove(vec* vec, void* val); * @param compare the comparison function * @return the first element that is matched by the comparison function, or NULL if none are matched. */ -void* vec_find(vec* vec, void* data, compare_func compare); +void* vec_find(const vec* vec, void* data, compare_func compare); /** * Runs through every element in the vector, and compares it against the * given data using the given comparison function. If the comparison function returns @@ -80,7 +80,7 @@ void* vec_find(vec* vec, void* data, compare_func compare); * @param ... variable arguments to be passed on to the foreach function * @return 0 if all goes well, or the first nonzero code returned by foreach. */ -int vec_foreach(vec* vec, void* data, compare_func compare, foreach_func foreach, ...); +int vec_foreach(const vec* vec, void* data, compare_func compare, foreach_func foreach, ...); /** * Gets the value at the given index of the vector. @@ -88,7 +88,7 @@ int vec_foreach(vec* vec, void* data, compare_func compare, foreach_func foreach * @param index the index to retreive a value from * @return pointer to the value, or, if the index is out of bounds (or there is nothing there) NULL. */ -void* vec_index(vec* vec, size_t index); +void* vec_index(const vec* vec, size_t index); /** * Clears the vector, removing all elements from it * but not resizing it down. diff --git a/src/ht.c b/src/ht.c index 350dbe1..10cb70c 100644 --- a/src/ht.c +++ b/src/ht.c @@ -69,7 +69,7 @@ libds_result ht_put(ht* ht, const void* key, void* data) { return result; } -void* ht_get(ht* ht, const void* key) { +void* ht_get(const 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, const void* key) { return data; } -void* ht_find(ht* ht, const void* key, void* data, compare_func compare) { +void* ht_find(const ht* ht, const void* key, void* data, compare_func compare) { void* found = NULL; ht_node* search_node; unsigned long key_int = ht->hash_func(key) % LIBDS_HT_SIZE; @@ -116,7 +116,7 @@ void ht_remove(ht* ht, const void* key) { } } -int ht_foreach(ht* ht, void* data, compare_func compare, foreach_func foreach, +int ht_foreach(const ht* ht, void* data, compare_func compare, foreach_func foreach, ...) { int index = 0; int return_code = 0; diff --git a/src/ll.c b/src/ll.c index 08a4b3c..5f94d52 100644 --- a/src/ll.c +++ b/src/ll.c @@ -62,7 +62,7 @@ void ll_remove(ll* ll, void* data) { } } -void* ll_find(ll* ll, void* data, compare_func compare) { +void* ll_find(const ll* ll, void* data, compare_func compare) { void* to_return = NULL; ll_node* head = ll->head; while (head && to_return == NULL) { @@ -73,7 +73,7 @@ void* ll_find(ll* ll, void* data, compare_func compare) { } return to_return; } -int ll_foreach(ll* ll, void* data, compare_func compare, foreach_func foreach, +int ll_foreach(const ll* ll, void* data, compare_func compare, foreach_func foreach, ...) { int return_code = 0; ll_node* head = ll->head; @@ -89,8 +89,8 @@ int ll_foreach(ll* ll, void* data, compare_func compare, foreach_func foreach, return return_code; } -void* ll_head(ll* ll) { return ll->head ? ll->head->data : NULL; } -void* ll_tail(ll* ll) { return ll->tail ? ll->tail->data : NULL; } +void* ll_head(const ll* ll) { return ll->head ? ll->head->data : NULL; } +void* ll_tail(const ll* ll) { return ll->tail ? ll->tail->data : NULL; } void* ll_pophead(ll* ll) { void* to_return = NULL; if (ll->head) { diff --git a/src/sprs.c b/src/sprs.c index fc7c62b..4f8f50c 100644 --- a/src/sprs.c +++ b/src/sprs.c @@ -70,17 +70,17 @@ libds_result sprs_put_grow(sprs* sprs, size_t index, void* data) { } return result; } -int sprs_contains(sprs* sprs, size_t index){ +int sprs_contains(const sprs* sprs, size_t index){ return sprs->sparse[index].index < sprs->size && sprs->dense[sprs->sparse[index].index] == index; } -void* sprs_get(sprs* sprs, size_t index){ +void* sprs_get(const sprs* sprs, size_t index){ void* data = NULL; if(sprs_contains(sprs, index)){ data = sprs->sparse[index].data; } return data; } -void* sprs_find(sprs* sprs, void* data, compare_func compare){ +void* sprs_find(const sprs* sprs, void* data, compare_func compare){ int index = 0; void* to_return = NULL; for(; index < sprs->size && to_return == NULL; index++){ @@ -90,7 +90,7 @@ void* sprs_find(sprs* sprs, void* data, compare_func compare){ } return to_return; } -int sprs_foreach(sprs* sprs, void* data, compare_func compare, foreach_func foreach, ...){ +int sprs_foreach(const sprs* sprs, void* data, compare_func compare, foreach_func foreach, ...){ int index = 0; int return_code = 0; va_list args; diff --git a/src/vec.c b/src/vec.c index 82f86fe..9a609e8 100644 --- a/src/vec.c +++ b/src/vec.c @@ -61,7 +61,7 @@ void vec_remove(vec* v, void* data) { } } -void* vec_find(vec* v, void* data, compare_func compare) { +void* vec_find(const vec* v, void* data, compare_func compare) { size_t index = 0; void* found = NULL; void** data_array = v->data; @@ -73,7 +73,7 @@ void* vec_find(vec* v, void* data, compare_func compare) { } return found; } -int vec_foreach(vec* v, void* data, compare_func compare, foreach_func foreach, +int vec_foreach(const vec* v, void* data, compare_func compare, foreach_func foreach, ...) { size_t index = 0; int return_code = 0; @@ -90,7 +90,7 @@ int vec_foreach(vec* v, void* data, compare_func compare, foreach_func foreach, return return_code; } -void* vec_index(vec* v, size_t index) { +void* vec_index(const vec* v, size_t index) { void* to_return = NULL; if (index < v->capacity) { to_return = ((void**) v->data)[index];