Compare commits

...

8 Commits

12 changed files with 149 additions and 60 deletions

View File

@@ -6,12 +6,13 @@ endif(TARGET ds)
project(libds) project(libds)
set_property(GLOBAL PROPERTY C_STANDARD 90) add_compile_options(-pedantic -Wall)
ADD_COMPILE_OPTIONS(-pedantic -Wall)
add_library(ds STATIC src/libds.c src/vec.c src/ht.c src/ll.c src/sprs.c) add_library(ds STATIC src/libds.c src/vec.c src/ht.c src/ll.c src/sprs.c)
add_executable(libds src/main.c) add_executable(libds src/main.c)
set_property(TARGET ds PROPERTY C_STANDARD 90)
set_property(TARGET libds PROPERTY C_STANDARD 90)
target_include_directories(ds PUBLIC include) target_include_directories(ds PUBLIC include)
target_include_directories(libds PUBLIC include) target_include_directories(libds PUBLIC include)

View File

@@ -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,30 @@ 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(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.
* @param ht the hash table to retreive the value from.
* @param key the key to use to find the data.
* @param data the data to compare the candidate value to.
* @param compare the comparison function used to compare adta.
* @return the data, or NULL if it is not found.
*/
void* ht_find(const ht* ht, const void* key, void* data, compare_func compare);
/** /**
* 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
@@ -144,6 +154,6 @@ void ht_remove(ht* ht, void* key);
* @param ... variable arguments to be passed to the foreach function. * @param ... variable arguments to be passed to the foreach function.
* @return the code returned by the foreach functions. * @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 #endif

View File

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

View File

@@ -82,7 +82,7 @@ void ll_remove(ll* ll, void* data);
* @param compare the comparison function * @param compare the comparison function
* @return the first element that is matched by the comparison function, or NULL if none are matched. * @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 * 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 * 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 * @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. * @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. * Gets the element at the beginning of the linked list.
* @param ll the linked list to get the data from. * @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. * @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. * Gets the element at the end of the linked list.
* @param ll the linked list to get the data from. * @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. * @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, * Removes the element at the beginning of the linked list,
* and returns it. * and returns it.

View File

@@ -58,7 +58,7 @@ struct sprs_s {
* The dense array. * The dense array.
* This is a pointer so that an array of a certain size can be allocated as needed. * This is a pointer so that an array of a certain size can be allocated as needed.
*/ */
int* dense; size_t* dense;
/** /**
* The sparse array. * The sparse array.
* This is a pointer so that an array of a certain size can be allocated as needed. * This is a pointer so that an array of a certain size can be allocated as needed.
@@ -94,14 +94,24 @@ void sprs_free(sprs* sprs);
* @param index the index at which to store the value * @param index the index at which to store the value
* @param data the value to store. * @param data the value to store.
*/ */
void sprs_put(sprs* sprs, int index, void* data); void sprs_put(sprs* sprs, size_t index, void* data);
/**
* Stores a value in the sparse set under a given index.
* If the capacity of the sparse set is smaller than the given index,
* this grows the sparse set by doubling its size.
* @param sprs the sparse set into which to store the value.
* @param index the index at which to store the value.
* @param data the value to store.
* @return LIBDS_SUCCESS if all goes well, or LIBDS_MALLOC if the growing failed.
*/
libds_result sprs_put_grow(sprs* sprs, size_t index, void* data);
/** /**
* Checks if the sparse set contains data under a given index. * Checks if the sparse set contains data under a given index.
* @param sprs the sparse set to check. * @param sprs the sparse set to check.
* @param index the index for which to check. * @param index the index for which to check.
* @return 1 if the index exists, 0 if not. * @return 1 if the index exists, 0 if not.
*/ */
int sprs_contains(sprs* sprs, int index); int sprs_contains(const sprs* sprs, size_t index);
/** /**
* Gets the value stored under the given sparse set index. * Gets the value stored under the given sparse set index.
* This will check for whether the index is in the sparse set first, * This will check for whether the index is in the sparse set first,
@@ -113,7 +123,7 @@ int sprs_contains(sprs* sprs, int index);
* @param index the index from under which to retrieve the value. * @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. * @return the value stored under the index, or NULL if there is nothing there.
*/ */
void* sprs_get(sprs* sprs, int index); void* sprs_get(const sprs* sprs, size_t index);
/** /**
* Runs through every element in the sparse set, and compares it against the * Runs through every element in the sparse set, and compares it against the
@@ -125,7 +135,7 @@ void* sprs_get(sprs* sprs, int index);
* @param compare the comparison function * @param compare the comparison function
* @return the first element that is matched by the comparison function, or NULL if none are matched. * @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 * 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 * given data using the given comparison function. If the comparison function returns
@@ -138,6 +148,6 @@ void* sprs_find(sprs* sprs, void* data, compare_func compare);
* @param ... variable arguments to be passed on to the foreach function * @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. * @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 #endif

View File

@@ -67,7 +67,7 @@ void vec_remove(vec* vec, void* val);
* @param compare the comparison function * @param compare the comparison function
* @return the first element that is matched by the comparison function, or NULL if none are matched. * @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 * Runs through every element in the vector, and compares it against the
* given data using the given comparison function. If the comparison function returns * 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 * @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. * @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. * 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 * @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. * @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 * Clears the vector, removing all elements from it
* but not resizing it down. * but not resizing it down.

View File

@@ -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(const 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,22 @@ void* ht_get(ht* ht, void* key) {
return data; return data;
} }
void ht_remove(ht* ht, void* key) { 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;
search_node = ht->data[key_int];
while (search_node && found == NULL) {
if (ht->cmp_func(search_node->key, key) &&
compare(data, search_node->data)) {
found = search_node->data;
}
search_node = search_node->next;
}
return found;
}
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;
@@ -101,7 +116,7 @@ void ht_remove(ht* ht, 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 index = 0;
int return_code = 0; int return_code = 0;

View File

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

View File

@@ -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; void* to_return = NULL;
ll_node* head = ll->head; ll_node* head = ll->head;
while (head && to_return == NULL) { while (head && to_return == NULL) {
@@ -73,7 +73,7 @@ void* ll_find(ll* ll, void* data, compare_func compare) {
} }
return to_return; 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; int return_code = 0;
ll_node* head = ll->head; 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; return return_code;
} }
void* ll_head(ll* ll) { return ll->head ? ll->head->data : NULL; } void* ll_head(const ll* ll) { return ll->head ? ll->head->data : NULL; }
void* ll_tail(ll* ll) { return ll->tail ? ll->tail->data : NULL; } void* ll_tail(const ll* ll) { return ll->tail ? ll->tail->data : NULL; }
void* ll_pophead(ll* ll) { void* ll_pophead(ll* ll) {
void* to_return = NULL; void* to_return = NULL;
if (ll->head) { if (ll->head) {

View File

@@ -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;
@@ -95,11 +95,11 @@ int test_vec_index() {
} }
int test_vec_clear(){ int test_vec_clear(){
vec test_vec; vec test_vec;
char* test_string = "test";
void** vec_data = NULL; void** vec_data = NULL;
int return_code; int return_code;
vec_init(&test_vec); vec_init(&test_vec);
vec_data = test_vec.data; vec_data = test_vec.data;
char* test_string = "test";
vec_add(&test_vec, test_string); vec_add(&test_vec, test_string);
vec_add(&test_vec, test_string); vec_add(&test_vec, test_string);
vec_add(&test_vec, test_string); vec_add(&test_vec, test_string);
@@ -297,6 +297,26 @@ int test_sprs_put(){
return return_code; return return_code;
} }
int test_sprs_put_grow(){
char* test_string_one = "test 1";
char* test_string_two = "test 2";
int return_code = 1;
sprs sprs;
sprs_init(&sprs);
sprs_setup(&sprs, 2);
sprs_put_grow(&sprs, 1, test_string_one);
return_code &= sprs.dense[0] == 1 && sprs.sparse[1].index == 0 && sprs.sparse[1].data == test_string_one;
return_code &= sprs.capacity == 2;
sprs_put_grow(&sprs, 3, test_string_two);
return_code &= sprs.dense[1] == 3 && sprs.sparse[3].index == 1 && sprs.sparse[3].data == test_string_two;
return_code &= sprs.capacity == 4;
sprs_put_grow(&sprs, 12, test_string_one);
return_code &= sprs.dense[2] == 12 && sprs.sparse[12].index == 2 && sprs.sparse[12].data == test_string_one;
return_code &= sprs.capacity == 16;
sprs_free(&sprs);
return return_code;
}
int test_sprs_contains(){ int test_sprs_contains(){
int return_code; int return_code;
sprs sprs; sprs sprs;
@@ -357,14 +377,14 @@ int run_test(char* test_name, int (*test_func)()) {
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
char* test_names[26] = {"vec_basic", "vec_add", "vec_remove", "vec_find", char* test_names[27] = {"vec_basic", "vec_add", "vec_remove", "vec_find",
"vec_foreach", "vec_index", "vec_clear", "ht_basic", "ht_put", "vec_foreach", "vec_index", "vec_clear", "ht_basic", "ht_put",
"ht_get", "ht_remove", "ht_foreach", "ll_basic", "ll_append", "ll_prepend", "ll_remove", "ll_find" , "ll_foreach", "ll_pophead", "ll_poptail", "sprs_basic", "sprs_put", "sprs_contains", "sprs_get", "sprs_find", "sprs_foreach"}; "ht_get", "ht_remove", "ht_foreach", "ll_basic", "ll_append", "ll_prepend", "ll_remove", "ll_find" , "ll_foreach", "ll_pophead", "ll_poptail", "sprs_basic", "sprs_put", "sprs_put_grow", "sprs_contains", "sprs_get", "sprs_find", "sprs_foreach"};
int (*test_functions[26])() = { int (*test_functions[27])() = {
test_vec_basic, test_vec_add, test_vec_remove, test_vec_find, test_vec_basic, test_vec_add, test_vec_remove, test_vec_find,
test_vec_foreach, test_vec_index, test_vec_clear, test_ht_basic, test_ht_put, test_vec_foreach, test_vec_index, test_vec_clear, test_ht_basic, test_ht_put,
test_ht_get, test_ht_remove, test_ht_foreach, test_ll_basic, test_ll_append, test_ll_prepend, test_ll_remove, test_ll_find, test_ll_foreach, test_ll_pophead, test_ll_poptail, test_sprs_basic, test_sprs_put, test_sprs_contains, test_sprs_get, test_sprs_find, test_sprs_foreach}; test_ht_get, test_ht_remove, test_ht_foreach, test_ll_basic, test_ll_append, test_ll_prepend, test_ll_remove, test_ll_find, test_ll_foreach, test_ll_pophead, test_ll_poptail, test_sprs_basic, test_sprs_put, test_sprs_put_grow, test_sprs_contains, test_sprs_get, test_sprs_find, test_sprs_foreach};
int test_index = 0; int test_index = 0;
int result = 1; int result = 1;

View File

@@ -30,24 +30,57 @@ void sprs_free(sprs* sprs){
sprs->dense = NULL; sprs->dense = NULL;
sprs->sparse = NULL; sprs->sparse = NULL;
} }
void sprs_put(sprs* sprs, int index, void* data){ void sprs_put(sprs* sprs, size_t index, void* data){
if(index < sprs->capacity && sprs->size < sprs->capacity){ if(index < sprs->capacity && sprs->size < sprs->capacity){
if(!sprs_contains(sprs, index)){
sprs->dense[sprs->size++] = index; sprs->dense[sprs->size++] = index;
sprs->sparse[index].index = sprs->size - 1; sprs->sparse[index].index = sprs->size - 1;
sprs->sparse[index].data = data; sprs->sparse[index].data = data;
} else {
sprs->sparse[index].data = data;
} }
} }
int sprs_contains(sprs* sprs, int index){
return index >= 0 && sprs->sparse[index].index < sprs->size && sprs->dense[sprs->sparse[index].index] == index;
} }
void* sprs_get(sprs* sprs, int index){ libds_result sprs_put_grow(sprs* sprs, size_t index, void* data) {
libds_result result = LIBDS_SUCCESS;
if(!sprs_contains(sprs, index) && (index >= sprs->capacity || sprs->size >= sprs->capacity)) {
int increase_required = 0;
size_t ratio = index / sprs->capacity;
size_t new_size;
void* new_sparse;
void* new_dense;
while(ratio >>= 1) {
increase_required++;
}
new_size = sprs->capacity * (1 << (increase_required + 1));
new_sparse = realloc(sprs->sparse, sizeof(*(sprs->sparse)) * new_size);
new_dense = realloc(sprs->dense, sizeof(*(sprs->dense)) * new_size);
if(new_sparse == NULL || new_dense == NULL){
free(new_sparse);
free(new_dense);
result = LIBDS_MALLOC;
} else {
sprs->sparse = new_sparse;
sprs->dense = new_dense;
sprs->capacity = new_size;
}
}
if(result == LIBDS_SUCCESS) {
sprs_put(sprs, index, data);
}
return result;
}
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(const sprs* sprs, size_t index){
void* data = NULL; void* data = NULL;
if(sprs_contains(sprs, index)){ if(sprs_contains(sprs, index)){
data = sprs->sparse[index].data; data = sprs->sparse[index].data;
} }
return 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; int index = 0;
void* to_return = NULL; void* to_return = NULL;
for(; index < sprs->size && to_return == NULL; index++){ for(; index < sprs->size && to_return == NULL; index++){
@@ -57,7 +90,7 @@ void* sprs_find(sprs* sprs, void* data, compare_func compare){
} }
return to_return; 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 index = 0;
int return_code = 0; int return_code = 0;
va_list args; va_list args;

View File

@@ -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; size_t index = 0;
void* found = NULL; void* found = NULL;
void** data_array = v->data; void** data_array = v->data;
@@ -73,7 +73,7 @@ void* vec_find(vec* v, void* data, compare_func compare) {
} }
return found; 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; size_t index = 0;
int return_code = 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; return return_code;
} }
void* vec_index(vec* v, size_t index) { void* vec_index(const vec* v, size_t index) {
void* to_return = NULL; void* to_return = NULL;
if (index < v->capacity) { if (index < v->capacity) {
to_return = ((void**) v->data)[index]; to_return = ((void**) v->data)[index];