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

@ -6,25 +6,25 @@
#include "libds.h" #include "libds.h"
struct ht_node_s { struct ht_node_s {
void* key; void* key;
void* data; void* data;
struct ht_node_s* next; struct ht_node_s* next;
}; };
struct ht_s { struct ht_s {
struct ht_node_s* data[LIBDS_HT_SIZE]; struct ht_node_s* data[LIBDS_HT_SIZE];
unsigned int (*hash_func)(void*); unsigned int (*hash_func)(void*);
int (*cmp_func)(void*, void*); int (*cmp_func)(void*, void*);
void* (*copy_func)(void*); void* (*copy_func)(void*);
void (*free_func)(void*); void (*free_func)(void*);
}; };
typedef struct ht_node_s ht_node; typedef struct ht_node_s ht_node;
typedef struct ht_s ht; typedef struct ht_s ht;
unsigned int ht_default_hash_func(void*); unsigned int ht_default_hash_func(void*);
int ht_default_cmp_func(void*, void*); int ht_default_cmp_func(void*, void*);
void* ht_default_copy_func(void*); void* ht_default_copy_func(void*);
void ht_default_free_func(void*); void ht_default_free_func(void*);
void ht_init(ht*); void ht_init(ht*);

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*);

206
src/ht.c
View File

@ -1,129 +1,125 @@
#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;
char* string; char* string;
hash = fnv_offset_basis; hash = fnv_offset_basis;
string = data; string = data;
while (*string) { while (*string) {
hash = hash * fnv_prime; hash = hash * fnv_prime;
hash = hash ^ (*(string++)); hash = hash ^ (*(string++));
} }
return hash; return hash;
}
int ht_default_cmp_func(void* a, void* b){
return strcmp(a, b) == 0;
} }
int ht_default_cmp_func(void* a, void* b) { 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) {
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){ libds_result ht_put(ht* ht, void* key, void* data) {
ht->cmp_func = ht_default_cmp_func; libds_result result = SUCCESS;
ht->copy_func = ht_default_copy_func; ht_node* new_node = NULL;
ht->free_func = ht_default_free_func; unsigned int key_int = ht->hash_func(key) % LIBDS_HT_SIZE;
ht->hash_func = ht_default_hash_func;
memset(ht->data, 0, sizeof(ht->data)); new_node = malloc(sizeof(*new_node));
} if (new_node) {
void ht_free(ht* ht){ new_node->data = data;
int index = 0; new_node->key = ht->copy_func(key);
for(; index < LIBDS_HT_SIZE; index++){ if (new_node->key == NULL) {
ht_node* head = ht->data[index]; result = MALLOC;
while(head){
ht_node* to_delete = head;
head = head->next;
ht->free_func(to_delete->key);
free(to_delete);
}
} }
} } else {
result = MALLOC;
}
libds_result ht_put(ht* ht, void* key, void* data){ if (result != SUCCESS) {
libds_result result = SUCCESS; if (new_node) {
ht_node* new_node = NULL; free(new_node);
unsigned int key_int = ht->hash_func(key) % LIBDS_HT_SIZE; new_node = NULL;
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;
} }
} else {
new_node->next = ht->data[key_int];
ht->data[key_int] = new_node;
}
if(result != SUCCESS) { return result;
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;
} }
void* ht_get(ht* ht, void* key) { void* ht_get(ht* ht, void* key) {
void* data = NULL; void* data = NULL;
ht_node* search_node; ht_node* search_node;
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;
}
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);
free(to_delete); free(to_delete);
} else { } else {
search_ptr = &(*search_ptr)->next; search_ptr = &(*search_ptr)->next;
}
} }
}
} }
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 return_code = 0; int index = 0;
va_list args; int return_code = 0;
for(; index < LIBDS_HT_SIZE && return_code == 0; index++){ va_list args;
ht_node* search_node = ht->data[index]; for (; index < LIBDS_HT_SIZE && return_code == 0; index++) {
while(search_node && return_code == 0){ ht_node* search_node = ht->data[index];
if(compare(data, search_node->data)) { while (search_node && return_code == 0) {
va_start(args, foreach); if (compare(data, search_node->data)) {
return_code = foreach(search_node->data, args); va_start(args, foreach);
va_end(args); return_code = foreach (search_node->data, args);
} va_end(args);
search_node = search_node->next; }
} search_node = search_node->next;
} }
return return_code; }
return return_code;
} }

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,209 +1,182 @@
#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() {
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(){ int _test_ht_foreach_func(void* data, va_list args) {
vec test_vec; char* real_data = data;
libds_result result = vec_init(&test_vec); int* sum = va_arg(args, int*);
vec_free(&test_vec);
return result == SUCCESS; *sum += *real_data;
} return 0;
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;
} }
int test_ht_basic() { int test_ht_basic() {
ht test_ht; ht test_ht;
ht_init(&test_ht); ht_init(&test_ht);
ht_free(&test_ht); ht_free(&test_ht);
return 1; return 1;
} }
int test_ht_put() { int test_ht_put() {
int return_code; int return_code;
ht test_ht; ht test_ht;
char* test_key = "creamwove"; char* test_key = "creamwove";
char* test_value = "quists"; char* test_value = "quists";
unsigned int index = ht_default_hash_func(test_key) % LIBDS_HT_SIZE; unsigned int index = ht_default_hash_func(test_key) % LIBDS_HT_SIZE;
ht_init(&test_ht); ht_init(&test_ht);
ht_put(&test_ht, test_key, test_value); ht_put(&test_ht, test_key, test_value);
return_code = test_ht.data[index] && test_ht.data[index]->data == test_value; return_code = test_ht.data[index] && test_ht.data[index]->data == test_value;
ht_free(&test_ht); ht_free(&test_ht);
return return_code; return return_code;
} }
int test_ht_get() { int test_ht_get() {
int return_code; int return_code;
ht test_ht; ht test_ht;
char* test_one = "creamwove"; char* test_one = "creamwove";
char* test_two = "quists"; char* test_two = "quists";
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_free(&test_ht); ht_get(&test_ht, test_two) == test_one;
return return_code; ht_free(&test_ht);
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";
char* test_two = "quists"; char* test_two = "quists";
unsigned int index = ht_default_hash_func(test_one) % LIBDS_HT_SIZE; unsigned int index = ht_default_hash_func(test_one) % LIBDS_HT_SIZE;
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);
ht_remove(&test_ht, test_two); ht_remove(&test_ht, test_two);
return_code = test_ht.data[index] && test_ht.data[index]->data == test_two; return_code = test_ht.data[index] && test_ht.data[index]->data == test_two;
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;
ht_init(&test_ht); ht_init(&test_ht);
char* test_one = "one"; char* test_one = "one";
char* test_two = "two"; char* test_two = "two";
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);
ht_foreach(&test_ht, NULL, compare_always, _test_ht_foreach_func, &sum); ht_foreach(&test_ht, NULL, compare_always, _test_ht_foreach_func, &sum);
return_code = *test_one + *test_two == sum; return_code = *test_one + *test_two == sum;
ht_free(&test_ht); ht_free(&test_ht);
return return_code; return return_code;
} }
int run_test(char* test_name, int (*test_func)()) { int run_test(char* test_name, int (*test_func)()) {
int success = test_func(); int success = test_func();
printf("Running test %-15s . . . ", test_name); printf("Running test %-15s . . . ", test_name);
printf("%s\n", success ? "Passed" : "Failed"); printf("%s\n", success ? "Passed" : "Failed");
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",
"vec_foreach", "vec_index", "ht_basic", "ht_put",
"ht_get", "ht_remove", "ht_foreach"};
char* test_names[11] = { int (*test_functions[11])() = {
"vec_basic", test_vec_basic, test_vec_add, test_vec_remove, test_vec_find,
"vec_add", test_vec_foreach, test_vec_index, test_ht_basic, test_ht_put,
"vec_remove", test_ht_get, test_ht_remove, test_ht_foreach};
"vec_find",
"vec_foreach",
"vec_index",
"ht_basic",
"ht_put",
"ht_get",
"ht_remove",
"ht_foreach"
};
int (*test_functions[11])() = { int test_index = 0;
test_vec_basic, int result = 1;
test_vec_add, for (; test_index < 11 && result; test_index++) {
test_vec_remove, result = run_test(test_names[test_index], test_functions[test_index]);
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]);
}
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,14 +37,14 @@ 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;
} }
index++; index++;
} }
@ -52,49 +52,50 @@ 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--;
} }
} }
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; 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--;
} }
index++; elements--;
}
index++;
} }
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];
} }