Implement a growing insertion.
This commit is contained in:
parent
316d2daf89
commit
c318aae32c
|
@ -95,6 +95,16 @@ void sprs_free(sprs* sprs);
|
|||
* @param data the value to store.
|
||||
*/
|
||||
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.
|
||||
* @param sprs the sparse set to check.
|
||||
|
|
28
src/main.c
28
src/main.c
|
@ -297,6 +297,26 @@ int test_sprs_put(){
|
|||
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 return_code;
|
||||
sprs sprs;
|
||||
|
@ -357,14 +377,14 @@ int run_test(char* test_name, int (*test_func)()) {
|
|||
}
|
||||
|
||||
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",
|
||||
"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_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 result = 1;
|
||||
|
|
29
src/sprs.c
29
src/sprs.c
|
@ -41,6 +41,35 @@ void sprs_put(sprs* sprs, size_t index, void* data){
|
|||
}
|
||||
}
|
||||
}
|
||||
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(sprs* sprs, size_t index){
|
||||
return sprs->sparse[index].index < sprs->size && sprs->dense[sprs->sparse[index].index] == index;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user