Format code.

This commit is contained in:
Danila Fedorin 2016-12-21 14:50:32 -08:00
parent d41a085bcd
commit 52640ed8f9
7 changed files with 305 additions and 337 deletions

3
.clang-format Normal file
View File

@ -0,0 +1,3 @@
DerivePointerAlignment: false
PointerAlignment: Left
BasedOnStyle: google

View File

@ -3,10 +3,7 @@
#include <stdarg.h> #include <stdarg.h>
enum libds_result_e { enum libds_result_e { SUCCESS, MALLOC };
SUCCESS,
MALLOC
};
typedef enum libds_result_e libds_result; typedef enum libds_result_e libds_result;
typedef int (*compare_func)(void*, void*); typedef int (*compare_func)(void*, void*);

View File

@ -1,8 +1,8 @@
#include "ht.h" #include "ht.h"
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
unsigned int ht_default_hash_func(void* data){ unsigned int ht_default_hash_func(void* data) {
const unsigned int fnv_prime = 16777619; const unsigned int fnv_prime = 16777619;
const unsigned int fnv_offset_basis = 2166136261; const unsigned int fnv_offset_basis = 2166136261;
unsigned int hash; unsigned int hash;
@ -15,32 +15,28 @@ unsigned int ht_default_hash_func(void* data){
} }
return hash; return hash;
} }
int ht_default_cmp_func(void* a, void* b){ int ht_default_cmp_func(void* a, void* b) { return strcmp(a, b) == 0; }
return strcmp(a, b) == 0;
}
void* ht_default_copy_func(void* from) { void* ht_default_copy_func(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);
} }
return copy; return copy;
} }
void ht_default_free_func(void* data){ void ht_default_free_func(void* data) { free(data); }
free(data);
}
void ht_init(ht* ht){ void ht_init(ht* ht) {
ht->cmp_func = ht_default_cmp_func; ht->cmp_func = ht_default_cmp_func;
ht->copy_func = ht_default_copy_func; ht->copy_func = ht_default_copy_func;
ht->free_func = ht_default_free_func; ht->free_func = ht_default_free_func;
ht->hash_func = ht_default_hash_func; ht->hash_func = ht_default_hash_func;
memset(ht->data, 0, sizeof(ht->data)); memset(ht->data, 0, sizeof(ht->data));
} }
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]; ht_node* head = ht->data[index];
while(head){ while (head) {
ht_node* to_delete = head; ht_node* to_delete = head;
head = head->next; head = head->next;
ht->free_func(to_delete->key); ht->free_func(to_delete->key);
@ -49,25 +45,24 @@ void ht_free(ht* ht){
} }
} }
libds_result ht_put(ht* ht, void* key, void* data){ libds_result ht_put(ht* ht, void* key, void* data) {
libds_result result = SUCCESS; libds_result result = SUCCESS;
ht_node* new_node = NULL; ht_node* new_node = NULL;
unsigned int key_int = ht->hash_func(key) % LIBDS_HT_SIZE; unsigned int key_int = ht->hash_func(key) % LIBDS_HT_SIZE;
new_node = malloc(sizeof(*new_node)); new_node = malloc(sizeof(*new_node));
if(new_node) { if (new_node) {
new_node->data = data; new_node->data = data;
new_node->key = ht->copy_func(key); new_node->key = ht->copy_func(key);
if(new_node->key == NULL){ if (new_node->key == NULL) {
result = MALLOC; result = MALLOC;
} }
} else { } else {
result = MALLOC; result = MALLOC;
} }
if(result != SUCCESS) { if (result != SUCCESS) {
if(new_node) { if (new_node) {
free(new_node); free(new_node);
new_node = NULL; new_node = NULL;
} }
@ -84,8 +79,8 @@ void* ht_get(ht* ht, void* key) {
unsigned int key_int = ht->hash_func(key) % LIBDS_HT_SIZE; unsigned int key_int = ht->hash_func(key) % LIBDS_HT_SIZE;
search_node = ht->data[key_int]; search_node = ht->data[key_int];
while(search_node && data == NULL){ while (search_node && data == NULL) {
if(ht->cmp_func(search_node->key, key)) { if (ht->cmp_func(search_node->key, key)) {
data = search_node->data; data = search_node->data;
} }
search_node = search_node->next; search_node = search_node->next;
@ -93,13 +88,13 @@ void* ht_get(ht* ht, void* key) {
return data; return data;
} }
void ht_remove(ht* ht, void* key){ void ht_remove(ht* ht, void* key) {
ht_node** search_ptr; ht_node** search_ptr;
unsigned int key_int = ht->hash_func(key) % LIBDS_HT_SIZE; unsigned int key_int = ht->hash_func(key) % LIBDS_HT_SIZE;
search_ptr = &ht->data[key_int]; search_ptr = &ht->data[key_int];
while(*search_ptr){ while (*search_ptr) {
if(ht->cmp_func((*search_ptr)->key, key)){ if (ht->cmp_func((*search_ptr)->key, key)) {
ht_node* to_delete = *search_ptr; ht_node* to_delete = *search_ptr;
*search_ptr = (*search_ptr)->next; *search_ptr = (*search_ptr)->next;
ht->free_func(to_delete->key); ht->free_func(to_delete->key);
@ -110,16 +105,17 @@ void ht_remove(ht* ht, void* key){
} }
} }
int ht_foreach(ht* ht, void* data, compare_func compare, foreach_func foreach, ...){ int ht_foreach(ht* ht, 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;
for(; index < LIBDS_HT_SIZE && return_code == 0; index++){ for (; index < LIBDS_HT_SIZE && return_code == 0; index++) {
ht_node* search_node = ht->data[index]; ht_node* search_node = ht->data[index];
while(search_node && return_code == 0){ while (search_node && return_code == 0) {
if(compare(data, search_node->data)) { if (compare(data, search_node->data)) {
va_start(args, foreach); va_start(args, foreach);
return_code = foreach(search_node->data, args); return_code = foreach (search_node->data, args);
va_end(args); va_end(args);
} }
search_node = search_node->next; search_node = search_node->next;

View File

@ -1,5 +1,3 @@
#include "libds.h" #include "libds.h"
int compare_always(void* a, void* b){ int compare_always(void* a, void* b) { return 1; }
return 1;
}

View File

@ -1,41 +1,39 @@
#include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "vec.h"
#include "ht.h" #include "ht.h"
#include "vec.h"
int _test_vec_foreach_func(void* data, va_list args){ int _test_vec_foreach_func(void* data, va_list args) {
char* real_data = data; char* real_data = data;
int* sum = va_arg(args, int*); int* sum = va_arg(args, int*);
*sum += *real_data; *sum += *real_data;
return 0; return 0;
} }
int _test_vec_find_string(void* a, void* b){ int _test_vec_find_string(void* a, void* b) { return strcmp(a, b) == 0; }
return strcmp(a, b) == 0;
}
int test_vec_basic(){ int test_vec_basic() {
vec test_vec; vec test_vec;
libds_result result = vec_init(&test_vec); libds_result result = vec_init(&test_vec);
vec_free(&test_vec); vec_free(&test_vec);
return result == SUCCESS; return result == SUCCESS;
} }
int test_vec_add(){ int test_vec_add() {
vec test_vec; vec test_vec;
char* test_string = "test"; char* test_string = "test";
int return_code; int return_code;
vec_init(&test_vec); vec_init(&test_vec);
return_code = vec_add(&test_vec, test_string) == SUCCESS; return_code = vec_add(&test_vec, test_string) == SUCCESS;
if(return_code) return_code = *((void**) test_vec.data) == test_string; if (return_code) return_code = *((void**)test_vec.data) == test_string;
vec_free(&test_vec); vec_free(&test_vec);
return return_code; return return_code;
} }
int test_vec_remove(){ int test_vec_remove() {
vec test_vec; vec test_vec;
char* test_string = "test"; char* test_string = "test";
int return_code; int return_code;
@ -43,49 +41,43 @@ int test_vec_remove(){
vec_init(&test_vec); vec_init(&test_vec);
vec_add(&test_vec, test_string); vec_add(&test_vec, 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) == NULL;
vec_free(&test_vec); vec_free(&test_vec);
return return_code; return return_code;
} }
int test_vec_find(){ int test_vec_find() {
vec test_vec; vec test_vec;
char* test_string = "test"; char* test_string = "test";
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);
return_code = vec_find(&test_vec, test_string, _test_vec_find_string) == test_string; return_code =
vec_find(&test_vec, test_string, _test_vec_find_string) == test_string;
vec_free(&test_vec); vec_free(&test_vec);
return return_code; return return_code;
} }
int test_vec_foreach(){ int test_vec_foreach() {
vec test_vec; vec test_vec;
int return_code; int return_code;
int sum = 0; int sum = 0;
char* test_strings[3] = { char* test_strings[3] = {"1", "2", "3"};
"1",
"2",
"3"
};
vec_init(&test_vec); vec_init(&test_vec);
vec_add(&test_vec, test_strings[0]); vec_add(&test_vec, test_strings[0]);
vec_add(&test_vec, test_strings[1]); vec_add(&test_vec, test_strings[1]);
vec_add(&test_vec, test_strings[2]); vec_add(&test_vec, test_strings[2]);
vec_foreach(&test_vec, NULL, compare_always, _test_vec_foreach_func, &sum); 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; return_code =
test_strings[0][0] + test_strings[1][0] + test_strings[2][0] == sum;
vec_free(&test_vec); vec_free(&test_vec);
return return_code; return return_code;
} }
int test_vec_index(){ int test_vec_index() {
vec test_vec; vec test_vec;
int return_code; int return_code;
char* test_strings[3] = { char* test_strings[3] = {"1", "2", "3"};
"1",
"2",
"3"
};
vec_init(&test_vec); vec_init(&test_vec);
vec_add(&test_vec, test_strings[0]); vec_add(&test_vec, test_strings[0]);
@ -96,7 +88,7 @@ int test_vec_index(){
return return_code; 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;
int* sum = va_arg(args, int*); int* sum = va_arg(args, int*);
@ -130,11 +122,12 @@ int test_ht_get() {
ht_init(&test_ht); ht_init(&test_ht);
ht_put(&test_ht, test_one, test_two); ht_put(&test_ht, test_one, test_two);
ht_put(&test_ht, test_two, test_one); 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; return_code = ht_get(&test_ht, test_one) == test_two &&
ht_get(&test_ht, test_two) == test_one;
ht_free(&test_ht); ht_free(&test_ht);
return return_code; return return_code;
} }
int test_ht_remove(){ int test_ht_remove() {
int return_code; int return_code;
ht test_ht; ht test_ht;
char* test_one = "creamwove"; char* test_one = "creamwove";
@ -148,7 +141,7 @@ int test_ht_remove(){
ht_free(&test_ht); ht_free(&test_ht);
return return_code; return return_code;
} }
int test_ht_foreach(){ int test_ht_foreach() {
int return_code; int return_code;
int sum = 0; int sum = 0;
ht test_ht; ht test_ht;
@ -170,39 +163,19 @@ int run_test(char* test_name, int (*test_func)()) {
return success; 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",
char* test_names[11] = { "vec_foreach", "vec_index", "ht_basic", "ht_put",
"vec_basic", "ht_get", "ht_remove", "ht_foreach"};
"vec_add",
"vec_remove",
"vec_find",
"vec_foreach",
"vec_index",
"ht_basic",
"ht_put",
"ht_get",
"ht_remove",
"ht_foreach"
};
int (*test_functions[11])() = { int (*test_functions[11])() = {
test_vec_basic, test_vec_basic, test_vec_add, test_vec_remove, test_vec_find,
test_vec_add, test_vec_foreach, test_vec_index, test_ht_basic, test_ht_put,
test_vec_remove, test_ht_get, test_ht_remove, test_ht_foreach};
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 test_index = 0;
int result = 1; int result = 1;
for(; test_index < 11 && result; test_index++){ for (; test_index < 11 && 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;

View File

@ -1,34 +1,34 @@
#include "vec.h" #include "vec.h"
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
libds_result vec_init(vec* v){ libds_result vec_init(vec* v) {
libds_result result = SUCCESS; libds_result result = SUCCESS;
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 = calloc(LIBDS_VEC_CAPACITY, sizeof(void*));
if(v->data == NULL){ if (v->data == NULL) {
result = MALLOC; result = MALLOC;
} }
return result; return result;
} }
void vec_free(vec* v){ void vec_free(vec* v) {
v->size = 0; v->size = 0;
v->capacity = 0; v->capacity = 0;
if(v->data) { if (v->data) {
free(v->data); free(v->data);
} }
v->data = NULL; v->data = NULL;
} }
libds_result vec_add(vec* v, void* data){ libds_result vec_add(vec* v, void* data) {
libds_result result = SUCCESS; libds_result result = SUCCESS;
if(v->size >= v->capacity) { if (v->size >= v->capacity) {
void* new_mem = calloc(v->capacity *= 2, sizeof(void*)); 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); memcpy(new_mem, v->data, sizeof(void*) * v->capacity / 2);
free(v->data); free(v->data);
v->data = new_mem; v->data = new_mem;
@ -37,11 +37,11 @@ libds_result vec_add(vec* v, void* data){
} }
} }
if(result == SUCCESS) { if (result == SUCCESS) {
int index = 0; int index = 0;
while(index < v->capacity) { while (index < v->capacity) {
void** data_array = v->data; void** data_array = v->data;
if(data_array[index] == NULL) { if (data_array[index] == NULL) {
data_array[index] = data; data_array[index] = data;
v->size++; v->size++;
break; break;
@ -52,14 +52,14 @@ libds_result vec_add(vec* v, void* data){
return result; return result;
} }
void vec_remove(vec* v, void* data){ void vec_remove(vec* v, void* data) {
int elements = v->size; int elements = v->size;
int index = 0; int index = 0;
void** data_array = v->data; void** data_array = v->data;
while(elements > 0) { while (elements > 0) {
if(data_array[index]) { if (data_array[index]) {
elements--; elements--;
if(data_array[index] == data) { if (data_array[index] == data) {
data_array[index] = NULL; data_array[index] = NULL;
v->size--; v->size--;
} }
@ -68,14 +68,14 @@ void vec_remove(vec* v, void* data){
} }
} }
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 elements = v->size;
int 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 (elements > 0 && found == NULL) {
if(data_array[index]){ if (data_array[index]) {
if(compare(data, data_array[index])){ if (compare(data, data_array[index])) {
found = data_array[index]; found = data_array[index];
} }
elements--; elements--;
@ -84,17 +84,18 @@ 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(vec* v, void* data, compare_func compare, foreach_func foreach,
...) {
int elements = v->size; int elements = v->size;
int 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 (elements > 0 && return_code == 0) {
if(data_array[index]){ if (data_array[index]) {
if(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--; elements--;
@ -104,9 +105,9 @@ int vec_foreach(vec* v, void* data, compare_func compare, foreach_func foreach,
return return_code; return return_code;
} }
void* vec_index(vec* v, int index){ void* vec_index(vec* v, int index) {
void* to_return = NULL; void* to_return = NULL;
if(index < v->capacity && index >= 0) { if (index < v->capacity && index >= 0) {
void** data_array = v->data; void** data_array = v->data;
to_return = data_array[index]; to_return = data_array[index];
} }