From 52640ed8f9be0e0d9b2b92e4f58cc5edf469af65 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 21 Dec 2016 14:50:32 -0800 Subject: [PATCH] Format code. --- .clang-format | 3 + include/ht.h | 20 +-- include/libds.h | 5 +- src/ht.c | 206 +++++++++++++++-------------- src/libds.c | 4 +- src/main.c | 335 ++++++++++++++++++++++-------------------------- src/vec.c | 69 +++++----- 7 files changed, 305 insertions(+), 337 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..d4ccc0b --- /dev/null +++ b/.clang-format @@ -0,0 +1,3 @@ +DerivePointerAlignment: false +PointerAlignment: Left +BasedOnStyle: google diff --git a/include/ht.h b/include/ht.h index bbab4f7..2212f07 100644 --- a/include/ht.h +++ b/include/ht.h @@ -6,25 +6,25 @@ #include "libds.h" struct ht_node_s { - void* key; - void* data; - struct ht_node_s* next; + void* key; + void* data; + struct ht_node_s* next; }; struct ht_s { - struct ht_node_s* data[LIBDS_HT_SIZE]; - unsigned int (*hash_func)(void*); - int (*cmp_func)(void*, void*); - void* (*copy_func)(void*); - void (*free_func)(void*); + struct ht_node_s* data[LIBDS_HT_SIZE]; + unsigned int (*hash_func)(void*); + int (*cmp_func)(void*, void*); + void* (*copy_func)(void*); + void (*free_func)(void*); }; typedef struct ht_node_s ht_node; typedef struct ht_s ht; unsigned int ht_default_hash_func(void*); -int ht_default_cmp_func(void*, void*); -void* ht_default_copy_func(void*); +int ht_default_cmp_func(void*, void*); +void* ht_default_copy_func(void*); void ht_default_free_func(void*); void ht_init(ht*); diff --git a/include/libds.h b/include/libds.h index f8c9203..2ba6506 100644 --- a/include/libds.h +++ b/include/libds.h @@ -3,10 +3,7 @@ #include -enum libds_result_e { - SUCCESS, - MALLOC -}; +enum libds_result_e { SUCCESS, MALLOC }; typedef enum libds_result_e libds_result; typedef int (*compare_func)(void*, void*); diff --git a/src/ht.c b/src/ht.c index 6ef1127..6106f85 100644 --- a/src/ht.c +++ b/src/ht.c @@ -1,129 +1,125 @@ #include "ht.h" -#include #include +#include -unsigned int ht_default_hash_func(void* data){ - const unsigned int fnv_prime = 16777619; - const unsigned int fnv_offset_basis = 2166136261; - unsigned int hash; - char* string; - hash = fnv_offset_basis; - string = data; - while (*string) { - hash = hash * fnv_prime; - hash = hash ^ (*(string++)); - } - return hash; -} -int ht_default_cmp_func(void* a, void* b){ - return strcmp(a, b) == 0; +unsigned int ht_default_hash_func(void* data) { + const unsigned int fnv_prime = 16777619; + const unsigned int fnv_offset_basis = 2166136261; + unsigned int hash; + char* string; + hash = fnv_offset_basis; + string = data; + while (*string) { + hash = hash * fnv_prime; + hash = hash ^ (*(string++)); + } + return hash; } +int ht_default_cmp_func(void* a, void* b) { return strcmp(a, b) == 0; } void* ht_default_copy_func(void* from) { - void* copy = malloc(sizeof(char) * (strlen(from) + 1)); - if(copy) { - strcpy(copy, from); - } - return copy; + void* copy = malloc(sizeof(char) * (strlen(from) + 1)); + if (copy) { + strcpy(copy, from); + } + return copy; } -void ht_default_free_func(void* data){ - free(data); +void ht_default_free_func(void* data) { free(data); } + +void ht_init(ht* ht) { + ht->cmp_func = ht_default_cmp_func; + ht->copy_func = ht_default_copy_func; + ht->free_func = ht_default_free_func; + ht->hash_func = ht_default_hash_func; + memset(ht->data, 0, sizeof(ht->data)); +} +void ht_free(ht* ht) { + int index = 0; + for (; index < LIBDS_HT_SIZE; index++) { + ht_node* head = ht->data[index]; + while (head) { + ht_node* to_delete = head; + head = head->next; + ht->free_func(to_delete->key); + free(to_delete); + } + } } -void ht_init(ht* ht){ - ht->cmp_func = ht_default_cmp_func; - ht->copy_func = ht_default_copy_func; - ht->free_func = ht_default_free_func; - ht->hash_func = ht_default_hash_func; - memset(ht->data, 0, sizeof(ht->data)); -} -void ht_free(ht* ht){ - int index = 0; - for(; index < LIBDS_HT_SIZE; index++){ - ht_node* head = ht->data[index]; - while(head){ - ht_node* to_delete = head; - head = head->next; - ht->free_func(to_delete->key); - free(to_delete); - } +libds_result ht_put(ht* ht, void* key, void* data) { + libds_result result = SUCCESS; + ht_node* new_node = NULL; + unsigned int key_int = ht->hash_func(key) % LIBDS_HT_SIZE; + + new_node = malloc(sizeof(*new_node)); + if (new_node) { + new_node->data = data; + new_node->key = ht->copy_func(key); + if (new_node->key == NULL) { + result = MALLOC; } -} + } else { + result = MALLOC; + } -libds_result ht_put(ht* ht, void* key, void* data){ - libds_result result = SUCCESS; - ht_node* new_node = NULL; - unsigned int key_int = ht->hash_func(key) % LIBDS_HT_SIZE; - - - new_node = malloc(sizeof(*new_node)); - if(new_node) { - new_node->data = data; - new_node->key = ht->copy_func(key); - if(new_node->key == NULL){ - result = MALLOC; - } - } else { - result = MALLOC; + if (result != SUCCESS) { + if (new_node) { + free(new_node); + new_node = NULL; } + } else { + new_node->next = ht->data[key_int]; + ht->data[key_int] = new_node; + } - if(result != SUCCESS) { - if(new_node) { - free(new_node); - new_node = NULL; - } - } else { - new_node->next = ht->data[key_int]; - ht->data[key_int] = new_node; - } - - return result; + return result; } void* ht_get(ht* ht, void* key) { - void* data = NULL; - ht_node* search_node; - unsigned int key_int = ht->hash_func(key) % LIBDS_HT_SIZE; + void* data = NULL; + ht_node* search_node; + unsigned int key_int = ht->hash_func(key) % LIBDS_HT_SIZE; - search_node = ht->data[key_int]; - while(search_node && data == NULL){ - if(ht->cmp_func(search_node->key, key)) { - data = search_node->data; - } - search_node = search_node->next; + search_node = ht->data[key_int]; + while (search_node && data == NULL) { + if (ht->cmp_func(search_node->key, key)) { + data = search_node->data; } + search_node = search_node->next; + } - return data; + return data; } -void ht_remove(ht* ht, void* key){ - ht_node** search_ptr; - unsigned int key_int = ht->hash_func(key) % LIBDS_HT_SIZE; +void ht_remove(ht* ht, void* key) { + ht_node** search_ptr; + unsigned int key_int = ht->hash_func(key) % LIBDS_HT_SIZE; - search_ptr = &ht->data[key_int]; - while(*search_ptr){ - if(ht->cmp_func((*search_ptr)->key, key)){ - ht_node* to_delete = *search_ptr; - *search_ptr = (*search_ptr)->next; - ht->free_func(to_delete->key); - free(to_delete); - } else { - search_ptr = &(*search_ptr)->next; - } + search_ptr = &ht->data[key_int]; + while (*search_ptr) { + if (ht->cmp_func((*search_ptr)->key, key)) { + ht_node* to_delete = *search_ptr; + *search_ptr = (*search_ptr)->next; + ht->free_func(to_delete->key); + free(to_delete); + } else { + search_ptr = &(*search_ptr)->next; } + } } -int ht_foreach(ht* ht, void* data, compare_func compare, foreach_func foreach, ...){ - int index = 0; - int return_code = 0; - va_list args; - for(; index < LIBDS_HT_SIZE && return_code == 0; index++){ - ht_node* search_node = ht->data[index]; - while(search_node && return_code == 0){ - if(compare(data, search_node->data)) { - va_start(args, foreach); - return_code = foreach(search_node->data, args); - va_end(args); - } - search_node = search_node->next; - } +int ht_foreach(ht* ht, void* data, compare_func compare, foreach_func foreach, + ...) { + int index = 0; + int return_code = 0; + va_list args; + for (; index < LIBDS_HT_SIZE && return_code == 0; index++) { + ht_node* search_node = ht->data[index]; + while (search_node && return_code == 0) { + if (compare(data, search_node->data)) { + va_start(args, foreach); + return_code = foreach (search_node->data, args); + va_end(args); + } + search_node = search_node->next; } - return return_code; + } + return return_code; } diff --git a/src/libds.c b/src/libds.c index e6ea5c1..e2bc17b 100644 --- a/src/libds.c +++ b/src/libds.c @@ -1,5 +1,3 @@ #include "libds.h" -int compare_always(void* a, void* b){ - return 1; -} +int compare_always(void* a, void* b) { return 1; } diff --git a/src/main.c b/src/main.c index 21a1ee5..c15b873 100644 --- a/src/main.c +++ b/src/main.c @@ -1,209 +1,182 @@ -#include #include #include +#include #include -#include "vec.h" #include "ht.h" +#include "vec.h" -int _test_vec_foreach_func(void* data, va_list args){ - char* real_data = data; - int* sum = va_arg(args, int*); +int _test_vec_foreach_func(void* data, va_list args) { + char* real_data = data; + int* sum = va_arg(args, int*); - *sum += *real_data; - return 0; + *sum += *real_data; + return 0; } -int _test_vec_find_string(void* a, void* b){ - return strcmp(a, b) == 0; +int _test_vec_find_string(void* a, void* b) { return strcmp(a, b) == 0; } + +int test_vec_basic() { + vec test_vec; + libds_result result = vec_init(&test_vec); + vec_free(&test_vec); + + return result == SUCCESS; +} +int test_vec_add() { + vec test_vec; + char* test_string = "test"; + int return_code; + + vec_init(&test_vec); + return_code = vec_add(&test_vec, test_string) == SUCCESS; + if (return_code) return_code = *((void**)test_vec.data) == test_string; + vec_free(&test_vec); + + return return_code; +} +int test_vec_remove() { + vec test_vec; + char* test_string = "test"; + int return_code; + + vec_init(&test_vec); + vec_add(&test_vec, test_string); + vec_remove(&test_vec, test_string); + return_code = *((void**)test_vec.data) == NULL; + vec_free(&test_vec); + + return return_code; +} +int test_vec_find() { + vec test_vec; + char* test_string = "test"; + int return_code; + + vec_init(&test_vec); + vec_add(&test_vec, test_string); + return_code = + vec_find(&test_vec, test_string, _test_vec_find_string) == test_string; + vec_free(&test_vec); + return return_code; +} +int test_vec_foreach() { + vec test_vec; + int return_code; + int sum = 0; + char* test_strings[3] = {"1", "2", "3"}; + + vec_init(&test_vec); + vec_add(&test_vec, test_strings[0]); + vec_add(&test_vec, test_strings[1]); + vec_add(&test_vec, test_strings[2]); + vec_foreach(&test_vec, NULL, compare_always, _test_vec_foreach_func, &sum); + return_code = + test_strings[0][0] + test_strings[1][0] + test_strings[2][0] == sum; + vec_free(&test_vec); + return return_code; +} +int test_vec_index() { + vec test_vec; + int return_code; + char* test_strings[3] = {"1", "2", "3"}; + + vec_init(&test_vec); + vec_add(&test_vec, test_strings[0]); + vec_add(&test_vec, test_strings[1]); + vec_add(&test_vec, test_strings[2]); + return_code = vec_index(&test_vec, 1) == test_strings[1]; + vec_free(&test_vec); + return return_code; } -int test_vec_basic(){ - vec test_vec; - libds_result result = vec_init(&test_vec); - vec_free(&test_vec); +int _test_ht_foreach_func(void* data, va_list args) { + char* real_data = data; + int* sum = va_arg(args, int*); - return result == SUCCESS; -} -int test_vec_add(){ - vec test_vec; - char* test_string = "test"; - int return_code; - - vec_init(&test_vec); - return_code = vec_add(&test_vec, test_string) == SUCCESS; - if(return_code) return_code = *((void**) test_vec.data) == test_string; - vec_free(&test_vec); - - return return_code; -} -int test_vec_remove(){ - vec test_vec; - char* test_string = "test"; - int return_code; - - vec_init(&test_vec); - vec_add(&test_vec, test_string); - vec_remove(&test_vec, test_string); - return_code = *((void**) test_vec.data) == NULL; - vec_free(&test_vec); - - return return_code; -} -int test_vec_find(){ - vec test_vec; - char* test_string = "test"; - int return_code; - - vec_init(&test_vec); - vec_add(&test_vec, test_string); - return_code = vec_find(&test_vec, test_string, _test_vec_find_string) == test_string; - vec_free(&test_vec); - return return_code; -} -int test_vec_foreach(){ - vec test_vec; - int return_code; - int sum = 0; - char* test_strings[3] = { - "1", - "2", - "3" - }; - - vec_init(&test_vec); - vec_add(&test_vec, test_strings[0]); - vec_add(&test_vec, test_strings[1]); - vec_add(&test_vec, test_strings[2]); - vec_foreach(&test_vec, NULL, compare_always, _test_vec_foreach_func, &sum); - return_code = test_strings[0][0] + test_strings[1][0] + test_strings[2][0] == sum; - vec_free(&test_vec); - return return_code; -} -int test_vec_index(){ - vec test_vec; - int return_code; - char* test_strings[3] = { - "1", - "2", - "3" - }; - - vec_init(&test_vec); - vec_add(&test_vec, test_strings[0]); - vec_add(&test_vec, test_strings[1]); - vec_add(&test_vec, test_strings[2]); - return_code = vec_index(&test_vec, 1) == test_strings[1]; - vec_free(&test_vec); - return return_code; -} - -int _test_ht_foreach_func(void* data, va_list args){ - char* real_data = data; - int* sum = va_arg(args, int*); - - *sum += *real_data; - return 0; + *sum += *real_data; + return 0; } int test_ht_basic() { - ht test_ht; - ht_init(&test_ht); - ht_free(&test_ht); - return 1; + ht test_ht; + ht_init(&test_ht); + ht_free(&test_ht); + return 1; } int test_ht_put() { - int return_code; - ht test_ht; - char* test_key = "creamwove"; - char* test_value = "quists"; - unsigned int index = ht_default_hash_func(test_key) % LIBDS_HT_SIZE; - ht_init(&test_ht); - ht_put(&test_ht, test_key, test_value); - return_code = test_ht.data[index] && test_ht.data[index]->data == test_value; - ht_free(&test_ht); - return return_code; + int return_code; + ht test_ht; + char* test_key = "creamwove"; + char* test_value = "quists"; + unsigned int index = ht_default_hash_func(test_key) % LIBDS_HT_SIZE; + ht_init(&test_ht); + ht_put(&test_ht, test_key, test_value); + return_code = test_ht.data[index] && test_ht.data[index]->data == test_value; + ht_free(&test_ht); + return return_code; } int test_ht_get() { - int return_code; - ht test_ht; - char* test_one = "creamwove"; - char* test_two = "quists"; - ht_init(&test_ht); - ht_put(&test_ht, test_one, test_two); - ht_put(&test_ht, test_two, test_one); - return_code = ht_get(&test_ht, test_one) == test_two && ht_get(&test_ht, test_two) == test_one; - ht_free(&test_ht); - return return_code; + int return_code; + ht test_ht; + char* test_one = "creamwove"; + char* test_two = "quists"; + ht_init(&test_ht); + ht_put(&test_ht, test_one, test_two); + ht_put(&test_ht, test_two, test_one); + return_code = ht_get(&test_ht, test_one) == test_two && + ht_get(&test_ht, test_two) == test_one; + ht_free(&test_ht); + return return_code; } -int test_ht_remove(){ - int return_code; - ht test_ht; - char* test_one = "creamwove"; - char* test_two = "quists"; - unsigned int index = ht_default_hash_func(test_one) % LIBDS_HT_SIZE; - ht_init(&test_ht); - ht_put(&test_ht, test_one, test_two); - ht_put(&test_ht, test_two, test_one); - ht_remove(&test_ht, test_two); - return_code = test_ht.data[index] && test_ht.data[index]->data == test_two; - ht_free(&test_ht); - return return_code; +int test_ht_remove() { + int return_code; + ht test_ht; + char* test_one = "creamwove"; + char* test_two = "quists"; + unsigned int index = ht_default_hash_func(test_one) % LIBDS_HT_SIZE; + ht_init(&test_ht); + ht_put(&test_ht, test_one, test_two); + ht_put(&test_ht, test_two, test_one); + ht_remove(&test_ht, test_two); + return_code = test_ht.data[index] && test_ht.data[index]->data == test_two; + ht_free(&test_ht); + return return_code; } -int test_ht_foreach(){ - int return_code; - int sum = 0; - ht test_ht; - ht_init(&test_ht); - char* test_one = "one"; - char* test_two = "two"; - ht_put(&test_ht, test_one, test_two); - ht_put(&test_ht, test_two, test_one); - ht_foreach(&test_ht, NULL, compare_always, _test_ht_foreach_func, &sum); - return_code = *test_one + *test_two == sum; - ht_free(&test_ht); - return return_code; +int test_ht_foreach() { + int return_code; + int sum = 0; + ht test_ht; + ht_init(&test_ht); + char* test_one = "one"; + char* test_two = "two"; + ht_put(&test_ht, test_one, test_two); + ht_put(&test_ht, test_two, test_one); + ht_foreach(&test_ht, NULL, compare_always, _test_ht_foreach_func, &sum); + return_code = *test_one + *test_two == sum; + ht_free(&test_ht); + return return_code; } int run_test(char* test_name, int (*test_func)()) { - int success = test_func(); - printf("Running test %-15s . . . ", test_name); - printf("%s\n", success ? "Passed" : "Failed"); - return success; + int success = test_func(); + printf("Running test %-15s . . . ", test_name); + printf("%s\n", success ? "Passed" : "Failed"); + return success; } -int main(int argc, char** argv){ +int main(int argc, char** argv) { + char* test_names[11] = {"vec_basic", "vec_add", "vec_remove", "vec_find", + "vec_foreach", "vec_index", "ht_basic", "ht_put", + "ht_get", "ht_remove", "ht_foreach"}; - char* test_names[11] = { - "vec_basic", - "vec_add", - "vec_remove", - "vec_find", - "vec_foreach", - "vec_index", - "ht_basic", - "ht_put", - "ht_get", - "ht_remove", - "ht_foreach" - }; + int (*test_functions[11])() = { + 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_ht_get, test_ht_remove, test_ht_foreach}; - int (*test_functions[11])() = { - 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_ht_get, - test_ht_remove, - test_ht_foreach - }; - - int test_index = 0; - int result = 1; - for(; test_index < 11 && result; test_index++){ - result = run_test(test_names[test_index], test_functions[test_index]); - } + int test_index = 0; + int result = 1; + for (; test_index < 11 && result; test_index++) { + result = run_test(test_names[test_index], test_functions[test_index]); + } return result ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/src/vec.c b/src/vec.c index 707a394..6be340a 100644 --- a/src/vec.c +++ b/src/vec.c @@ -1,34 +1,34 @@ #include "vec.h" -#include #include +#include #include -libds_result vec_init(vec* v){ +libds_result vec_init(vec* v) { libds_result result = SUCCESS; - + v->size = 0; v->capacity = LIBDS_VEC_CAPACITY; v->data = calloc(LIBDS_VEC_CAPACITY, sizeof(void*)); - if(v->data == NULL){ + if (v->data == NULL) { result = MALLOC; } return result; } -void vec_free(vec* v){ +void vec_free(vec* v) { v->size = 0; v->capacity = 0; - if(v->data) { + if (v->data) { free(v->data); } v->data = NULL; } -libds_result vec_add(vec* v, void* data){ +libds_result vec_add(vec* v, void* data) { libds_result result = SUCCESS; - if(v->size >= v->capacity) { + if (v->size >= v->capacity) { void* new_mem = calloc(v->capacity *= 2, sizeof(void*)); - if(new_mem) { + if (new_mem) { memcpy(new_mem, v->data, sizeof(void*) * v->capacity / 2); free(v->data); v->data = new_mem; @@ -37,14 +37,14 @@ libds_result vec_add(vec* v, void* data){ } } - if(result == SUCCESS) { + if (result == SUCCESS) { int index = 0; - while(index < v->capacity) { + while (index < v->capacity) { void** data_array = v->data; - if(data_array[index] == NULL) { + if (data_array[index] == NULL) { data_array[index] = data; - v->size++; - break; + v->size++; + break; } index++; } @@ -52,49 +52,50 @@ libds_result vec_add(vec* v, void* data){ return result; } -void vec_remove(vec* v, void* data){ +void vec_remove(vec* v, void* data) { int elements = v->size; int index = 0; void** data_array = v->data; - while(elements > 0) { - if(data_array[index]) { + while (elements > 0) { + if (data_array[index]) { elements--; - if(data_array[index] == data) { + if (data_array[index] == data) { data_array[index] = NULL; - v->size--; + v->size--; } } 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; int index = 0; void* found = NULL; void** data_array = v->data; - while(elements > 0 && found == NULL){ - if(data_array[index]){ - if(compare(data, data_array[index])){ - found = data_array[index]; - } - elements--; + while (elements > 0 && found == NULL) { + if (data_array[index]) { + if (compare(data, data_array[index])) { + found = data_array[index]; } - index++; + elements--; + } + index++; } return found; } -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; int index = 0; int return_code = 0; void** data_array = v->data; va_list args; - while(elements > 0 && return_code == 0) { - if(data_array[index]){ - if(compare(data, data_array[index])){ + while (elements > 0 && return_code == 0) { + if (data_array[index]) { + if (compare(data, data_array[index])) { va_start(args, foreach); - return_code = foreach(data_array[index], args); + return_code = foreach (data_array[index], args); va_end(args); } elements--; @@ -104,9 +105,9 @@ int vec_foreach(vec* v, void* data, compare_func compare, foreach_func foreach, return return_code; } -void* vec_index(vec* v, int index){ +void* vec_index(vec* v, int index) { void* to_return = NULL; - if(index < v->capacity && index >= 0) { + if (index < v->capacity && index >= 0) { void** data_array = v->data; to_return = data_array[index]; }