Compare commits
8 Commits
c8d733e410
...
cd822fedfc
| Author | SHA1 | Date | |
|---|---|---|---|
| cd822fedfc | |||
| 1b57ebb027 | |||
| c97538265d | |||
| 2bf3f32bed | |||
| b1a7c21caa | |||
| 20507f73bb | |||
| a04490cbdd | |||
| 1a7ba8e393 |
@@ -1,4 +1,9 @@
|
|||||||
cmake_minimum_required(VERSION 2.7)
|
cmake_minimum_required(VERSION 2.7)
|
||||||
|
|
||||||
|
if(TARGET ds)
|
||||||
|
return()
|
||||||
|
endif(TARGET ds)
|
||||||
|
|
||||||
project(libds)
|
project(libds)
|
||||||
|
|
||||||
set_property(GLOBAL PROPERTY C_STANDARD 90)
|
set_property(GLOBAL PROPERTY C_STANDARD 90)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef LIBDS_SPRS_HEADER
|
#ifndef LIBDS_SPRS_HEADER
|
||||||
#define LIBDS_SPRS_HEADER
|
#define LIBDS_SPRS_HEADER
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
#include "libds.h"
|
#include "libds.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -33,7 +34,7 @@ struct sprs_element_s {
|
|||||||
/**
|
/**
|
||||||
* The index of this element in the dense array.
|
* The index of this element in the dense array.
|
||||||
*/
|
*/
|
||||||
int index;
|
size_t index;
|
||||||
/**
|
/**
|
||||||
* A piece of data (optional) associated with this index.
|
* A piece of data (optional) associated with this index.
|
||||||
*/
|
*/
|
||||||
@@ -47,11 +48,11 @@ struct sprs_s {
|
|||||||
/**
|
/**
|
||||||
* The maximum size of the set. This is the limit for integer indices.
|
* The maximum size of the set. This is the limit for integer indices.
|
||||||
*/
|
*/
|
||||||
int capacity;
|
size_t capacity;
|
||||||
/**
|
/**
|
||||||
* The current size of the sparse set, and the next available index in the dense array.
|
* The current size of the sparse set, and the next available index in the dense array.
|
||||||
*/
|
*/
|
||||||
int size;
|
size_t size;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The dense array.
|
* The dense array.
|
||||||
@@ -80,7 +81,7 @@ void sprs_init(sprs* sprs);
|
|||||||
* @param size the maximum capacity of the sparse set to use.
|
* @param size the maximum capacity of the sparse set to use.
|
||||||
* @return LIBDS_SUCCESS if all goes well, or LIBDS_MALLOC if an allocation failed.
|
* @return LIBDS_SUCCESS if all goes well, or LIBDS_MALLOC if an allocation failed.
|
||||||
*/
|
*/
|
||||||
libds_result sprs_setup(sprs* sprs, int size);
|
libds_result sprs_setup(sprs* sprs, size_t size);
|
||||||
/**
|
/**
|
||||||
* Frees memory allocated by the sparse set, and resets its capacity and size.
|
* Frees memory allocated by the sparse set, and resets its capacity and size.
|
||||||
* @param sprs the sparse set to free.
|
* @param sprs the sparse set to free.
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#define LIBDS_VEC_CAPACITY 4
|
#define LIBDS_VEC_CAPACITY 4
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
#include "libds.h"
|
#include "libds.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,11 +19,11 @@ struct vec_s {
|
|||||||
/**
|
/**
|
||||||
* The maximum size of the vector.
|
* The maximum size of the vector.
|
||||||
*/
|
*/
|
||||||
int capacity;
|
size_t capacity;
|
||||||
/**
|
/**
|
||||||
* The current number of items in the vector.
|
* The current number of items in the vector.
|
||||||
*/
|
*/
|
||||||
int size;
|
size_t size;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct vec_s vec;
|
typedef struct vec_s vec;
|
||||||
@@ -87,6 +88,12 @@ 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, int index);
|
void* vec_index(vec* vec, size_t index);
|
||||||
|
/**
|
||||||
|
* Clears the vector, removing all elements from it
|
||||||
|
* but not resizing it down.
|
||||||
|
* @param vec the vector to clear.
|
||||||
|
*/
|
||||||
|
void vec_clear(vec* vec);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
34
src/ht.c
34
src/ht.c
@@ -35,40 +35,36 @@ void ht_init(ht* ht) {
|
|||||||
void ht_free(ht* ht) {
|
void ht_free(ht* ht) {
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (; index < LIBDS_HT_SIZE; index++) {
|
for (; index < LIBDS_HT_SIZE; index++) {
|
||||||
ht_node* head = ht->data[index];
|
while (ht->data[index]) {
|
||||||
while (head) {
|
ht_node* to_delete = ht->data[index];
|
||||||
ht_node* to_delete = head;
|
ht->data[index] = to_delete->next;
|
||||||
head = head->next;
|
|
||||||
ht->free_func(to_delete->key);
|
ht->free_func(to_delete->key);
|
||||||
free(to_delete);
|
free(to_delete);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ht->cmp_func = NULL;
|
||||||
|
ht->copy_func = NULL;
|
||||||
|
ht->free_func = NULL;
|
||||||
|
ht->hash_func = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
libds_result ht_put(ht* ht, void* key, void* data) {
|
libds_result ht_put(ht* ht, 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;
|
||||||
unsigned long key_int = ht->hash_func(key) % LIBDS_HT_SIZE;
|
unsigned long key_int = ht->hash_func(key) % LIBDS_HT_SIZE;
|
||||||
|
|
||||||
new_node = malloc(sizeof(*new_node));
|
new_node = malloc(sizeof(*new_node));
|
||||||
if (new_node) {
|
new_key = ht->copy_func(key);
|
||||||
|
if (new_node && new_key) {
|
||||||
new_node->data = data;
|
new_node->data = data;
|
||||||
new_node->key = ht->copy_func(key);
|
new_node->key = new_key;
|
||||||
if (new_node->key == NULL) {
|
|
||||||
result = LIBDS_MALLOC;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
result = LIBDS_MALLOC;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result != LIBDS_SUCCESS) {
|
|
||||||
if (new_node) {
|
|
||||||
free(new_node);
|
|
||||||
new_node = NULL;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
new_node->next = ht->data[key_int];
|
new_node->next = ht->data[key_int];
|
||||||
ht->data[key_int] = new_node;
|
ht->data[key_int] = new_node;
|
||||||
|
} else {
|
||||||
|
free(new_node);
|
||||||
|
free(new_key);
|
||||||
|
result = LIBDS_MALLOC;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
32
src/main.c
32
src/main.c
@@ -38,12 +38,16 @@ int test_vec_add() {
|
|||||||
int test_vec_remove() {
|
int test_vec_remove() {
|
||||||
vec test_vec;
|
vec test_vec;
|
||||||
char* test_string = "test";
|
char* test_string = "test";
|
||||||
|
char* second_test_string = "test2";
|
||||||
int return_code;
|
int return_code;
|
||||||
|
|
||||||
vec_init(&test_vec);
|
vec_init(&test_vec);
|
||||||
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, second_test_string);
|
||||||
vec_remove(&test_vec, test_string);
|
vec_remove(&test_vec, test_string);
|
||||||
return_code = *((void**)test_vec.data) == NULL;
|
return_code = ((void**)test_vec.data)[0] == second_test_string;
|
||||||
vec_free(&test_vec);
|
vec_free(&test_vec);
|
||||||
|
|
||||||
return return_code;
|
return return_code;
|
||||||
@@ -89,6 +93,22 @@ int test_vec_index() {
|
|||||||
vec_free(&test_vec);
|
vec_free(&test_vec);
|
||||||
return return_code;
|
return return_code;
|
||||||
}
|
}
|
||||||
|
int test_vec_clear(){
|
||||||
|
vec test_vec;
|
||||||
|
void** vec_data = NULL;
|
||||||
|
int return_code;
|
||||||
|
vec_init(&test_vec);
|
||||||
|
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_clear(&test_vec);
|
||||||
|
return_code = (vec_data[0] == vec_data[1]) && (vec_data[1] == vec_data[2]) && (vec_data[2] == vec_data[3]) && (test_vec.size == 0);
|
||||||
|
vec_free(&test_vec);
|
||||||
|
return return_code;
|
||||||
|
}
|
||||||
|
|
||||||
int _test_ht_foreach_func(void* data, va_list args) {
|
int _test_ht_foreach_func(void* data, va_list args) {
|
||||||
char* real_data = data;
|
char* real_data = data;
|
||||||
@@ -337,18 +357,18 @@ int run_test(char* test_name, int (*test_func)()) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
char* test_names[25] = {"vec_basic", "vec_add", "vec_remove", "vec_find",
|
char* test_names[26] = {"vec_basic", "vec_add", "vec_remove", "vec_find",
|
||||||
"vec_foreach", "vec_index", "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_contains", "sprs_get", "sprs_find", "sprs_foreach"};
|
||||||
|
|
||||||
int (*test_functions[25])() = {
|
int (*test_functions[26])() = {
|
||||||
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_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_contains, test_sprs_get, test_sprs_find, test_sprs_foreach};
|
||||||
|
|
||||||
int test_index = 0;
|
int test_index = 0;
|
||||||
int result = 1;
|
int result = 1;
|
||||||
for (; test_index < 25 && result; test_index++) {
|
for (; test_index < 26 && result; test_index++) {
|
||||||
result = run_test(test_names[test_index], test_functions[test_index]);
|
result = run_test(test_names[test_index], test_functions[test_index]);
|
||||||
}
|
}
|
||||||
return result ? EXIT_SUCCESS : EXIT_FAILURE;
|
return result ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||||
|
|||||||
@@ -7,13 +7,15 @@ void sprs_init(sprs* sprs){
|
|||||||
sprs->dense = NULL;
|
sprs->dense = NULL;
|
||||||
sprs->sparse = NULL;
|
sprs->sparse = NULL;
|
||||||
}
|
}
|
||||||
libds_result sprs_setup(sprs* sprs, int size){
|
libds_result sprs_setup(sprs* sprs, size_t size){
|
||||||
libds_result result = LIBDS_SUCCESS;
|
libds_result result = LIBDS_SUCCESS;
|
||||||
sprs->sparse = malloc(sizeof(*(sprs->sparse)) * size);
|
sprs->sparse = malloc(sizeof(*(sprs->sparse)) * size);
|
||||||
sprs->dense = malloc(sizeof(*(sprs->dense)) * size);
|
sprs->dense = malloc(sizeof(*(sprs->dense)) * size);
|
||||||
if(sprs->sparse == NULL || sprs->dense == NULL){
|
if(sprs->sparse == NULL || sprs->dense == NULL){
|
||||||
free(sprs->sparse);
|
free(sprs->sparse);
|
||||||
free(sprs->dense);
|
free(sprs->dense);
|
||||||
|
sprs->sparse = NULL;
|
||||||
|
sprs->dense = NULL;
|
||||||
result = LIBDS_MALLOC;
|
result = LIBDS_MALLOC;
|
||||||
} else {
|
} else {
|
||||||
sprs->capacity = size;
|
sprs->capacity = size;
|
||||||
@@ -36,7 +38,7 @@ void sprs_put(sprs* sprs, int index, void* data){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
int sprs_contains(sprs* sprs, int index){
|
int sprs_contains(sprs* sprs, int index){
|
||||||
return index >= 0 && sprs->sparse[index].index >= 0 && sprs->sparse[index].index < sprs->size && sprs->dense[sprs->sparse[index].index] == index;
|
return index >= 0 && sprs->sparse[index].index < sprs->size && sprs->dense[sprs->sparse[index].index] == index;
|
||||||
}
|
}
|
||||||
void* sprs_get(sprs* sprs, int index){
|
void* sprs_get(sprs* sprs, int index){
|
||||||
void* data = NULL;
|
void* data = NULL;
|
||||||
|
|||||||
73
src/vec.c
73
src/vec.c
@@ -8,7 +8,7 @@ libds_result vec_init(vec* v) {
|
|||||||
|
|
||||||
v->size = 0;
|
v->size = 0;
|
||||||
v->capacity = LIBDS_VEC_CAPACITY;
|
v->capacity = LIBDS_VEC_CAPACITY;
|
||||||
v->data = calloc(LIBDS_VEC_CAPACITY, sizeof(void*));
|
v->data = malloc(LIBDS_VEC_CAPACITY * sizeof(void*));
|
||||||
if (v->data == NULL) {
|
if (v->data == NULL) {
|
||||||
result = LIBDS_MALLOC;
|
result = LIBDS_MALLOC;
|
||||||
}
|
}
|
||||||
@@ -27,7 +27,7 @@ void vec_free(vec* v) {
|
|||||||
libds_result vec_add(vec* v, void* data) {
|
libds_result vec_add(vec* v, void* data) {
|
||||||
libds_result result = LIBDS_SUCCESS;
|
libds_result result = LIBDS_SUCCESS;
|
||||||
if (v->size >= v->capacity) {
|
if (v->size >= v->capacity) {
|
||||||
void* new_mem = calloc(v->capacity *= 2, sizeof(void*));
|
void* new_mem = malloc((v->capacity *= 2) * sizeof(void*));
|
||||||
if (new_mem) {
|
if (new_mem) {
|
||||||
memcpy(new_mem, v->data, sizeof(void*) * v->capacity / 2);
|
memcpy(new_mem, v->data, sizeof(void*) * v->capacity / 2);
|
||||||
free(v->data);
|
free(v->data);
|
||||||
@@ -38,47 +38,36 @@ libds_result vec_add(vec* v, void* data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (result == LIBDS_SUCCESS) {
|
if (result == LIBDS_SUCCESS) {
|
||||||
int index = 0;
|
((void**) v->data)[v->size++] = data;
|
||||||
while (index < v->capacity) {
|
|
||||||
void** data_array = v->data;
|
|
||||||
if (data_array[index] == NULL) {
|
|
||||||
data_array[index] = data;
|
|
||||||
v->size++;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
index++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
void vec_remove(vec* v, void* data) {
|
void vec_remove(vec* v, void* data) {
|
||||||
int elements = v->size;
|
size_t index = 0;
|
||||||
int index = 0;
|
|
||||||
void** data_array = v->data;
|
void** data_array = v->data;
|
||||||
while (elements > 0) {
|
while (index < v->size && data_array[index]) {
|
||||||
if (data_array[index]) {
|
size_t search_ahead = 0;
|
||||||
elements--;
|
while(data_array[index + search_ahead] == data){
|
||||||
if (data_array[index] == data) {
|
search_ahead++;
|
||||||
data_array[index] = NULL;
|
}
|
||||||
v->size--;
|
|
||||||
}
|
if(search_ahead){
|
||||||
|
size_t remaining_elements = v->size - (index + search_ahead);
|
||||||
|
memmove(data_array + index, data_array + index + search_ahead, remaining_elements);
|
||||||
|
v->size -= search_ahead;
|
||||||
}
|
}
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void* vec_find(vec* v, void* data, compare_func compare) {
|
void* vec_find(vec* v, void* data, compare_func compare) {
|
||||||
int elements = v->size;
|
size_t index = 0;
|
||||||
int index = 0;
|
|
||||||
void* found = NULL;
|
void* found = NULL;
|
||||||
void** data_array = v->data;
|
void** data_array = v->data;
|
||||||
while (elements > 0 && found == NULL) {
|
while (index < v->size && found == NULL) {
|
||||||
if (data_array[index]) {
|
if (data_array[index] && compare(data, data_array[index])) {
|
||||||
if (compare(data, data_array[index])) {
|
found = data_array[index];
|
||||||
found = data_array[index];
|
|
||||||
}
|
|
||||||
elements--;
|
|
||||||
}
|
}
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
@@ -86,30 +75,28 @@ void* vec_find(vec* v, void* data, compare_func compare) {
|
|||||||
}
|
}
|
||||||
int vec_foreach(vec* v, void* data, compare_func compare, foreach_func foreach,
|
int vec_foreach(vec* v, void* data, compare_func compare, foreach_func foreach,
|
||||||
...) {
|
...) {
|
||||||
int elements = v->size;
|
size_t index = 0;
|
||||||
int index = 0;
|
|
||||||
int return_code = 0;
|
int return_code = 0;
|
||||||
void** data_array = v->data;
|
void** data_array = v->data;
|
||||||
va_list args;
|
va_list args;
|
||||||
while (elements > 0 && return_code == 0) {
|
while (index < v->size && return_code == 0) {
|
||||||
if (data_array[index]) {
|
if (data_array[index] && compare(data, data_array[index])) {
|
||||||
if (compare(data, data_array[index])) {
|
va_start(args, foreach);
|
||||||
va_start(args, foreach);
|
return_code = foreach (data_array[index], args);
|
||||||
return_code = foreach (data_array[index], args);
|
va_end(args);
|
||||||
va_end(args);
|
|
||||||
}
|
|
||||||
elements--;
|
|
||||||
}
|
}
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
return return_code;
|
return return_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* vec_index(vec* v, int index) {
|
void* vec_index(vec* v, size_t index) {
|
||||||
void* to_return = NULL;
|
void* to_return = NULL;
|
||||||
if (index < v->capacity && index >= 0) {
|
if (index < v->capacity) {
|
||||||
void** data_array = v->data;
|
to_return = ((void**) v->data)[index];
|
||||||
to_return = data_array[index];
|
|
||||||
}
|
}
|
||||||
return to_return;
|
return to_return;
|
||||||
}
|
}
|
||||||
|
void vec_clear(vec* vec) {
|
||||||
|
vec->size = 0;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user