Code cleanup, implement LL_ITER macro

This commit is contained in:
2022-03-28 10:52:16 -07:00
parent 91e2ac1994
commit 8b2a459252
14 changed files with 136 additions and 94 deletions

View File

@@ -140,9 +140,9 @@ int bintree_merge(BinTree *merge, BinTree *left, BinTree *right, void *data) {
return 0;
}
void print_node(char* prefix, BinTreeNode *node, int is_left, void (*pfunc)(void* data)) {
void print_node(char *prefix, BinTreeNode *node, int is_left, void (*pfunc)(void *data)) {
if (node != NULL) {
printf("%s%s", prefix, (is_left ? "├──" : "└──" ));
printf("%s%s", prefix, (is_left ? "├──" : "└──"));
pfunc(node->data);
char new_prefix[64];
memset(new_prefix, 0, 64);
@@ -153,8 +153,8 @@ void print_node(char* prefix, BinTreeNode *node, int is_left, void (*pfunc)(void
}
}
void bintree_debug_pfunc_int(void* data) {
int i = *((int*)data);
void bintree_debug_pfunc_int(void *data) {
int i = *((int *) data);
printf("%d\n", i);
}

View File

@@ -4,7 +4,9 @@
#include <limits.h>
#ifdef __linux__
#include <bsd/stdlib.h>
#endif
#include "lfinput.h"
@@ -21,7 +23,7 @@ char *get_input(const char *path) {
size_t fsz = ftell(fp);
rewind(fp);
char* buf = NULL;
char *buf = NULL;
buf = malloc(fsz + 1);
if (buf == NULL) {
fprintf(stderr, "Failed to malloc buf. Returning NULL\n");
@@ -67,7 +69,7 @@ int *get_ints(const char *path, size_t *sz) {
for (size_t idx = 0; idx < *sz; idx++) {
int n;
const char *errstr;
n = (int)strtonum(lines[idx], INT_MIN, INT_MAX, &errstr);
n = (int) strtonum(lines[idx], INT_MIN, INT_MAX, &errstr);
if (errstr) {
printf("Failed to convert %s to int. Returning NULL\n", lines[idx]);
exit(1);

View File

@@ -3,25 +3,25 @@
#include "lflinkedlist.h"
void ll_init(List* list, void (*destroy)(void *data)) {
void ll_init(List *list, void (*destroy)(void *data)) {
list->size = 0;
list->destroy = destroy;
list->head = NULL;
list->tail = NULL;
}
void ll_destroy(List* list) {
void* data;
void ll_destroy(List *list) {
void *data;
while (list->size > 0) {
if (ll_remove(list, list->tail, (void**)&data) == 0 && list->destroy != NULL) {
if (ll_remove(list, list->tail, (void **) &data) == 0 && list->destroy != NULL) {
list->destroy(data);
}
}
memset(list, 0, sizeof(List));
}
int ll_ins_next(List* list, ListNode* node, const void* data) {
ListNode* new_node;
int ll_ins_next(List *list, ListNode *node, const void *data) {
ListNode *new_node;
if (node == NULL && list->size != 0) {
return -1;
}
@@ -29,7 +29,7 @@ int ll_ins_next(List* list, ListNode* node, const void* data) {
return -1;
}
new_node->data = (void*)data;
new_node->data = (void *) data;
if (list->size == 0) {
list->head = new_node;
list->head->prev = NULL;
@@ -50,8 +50,8 @@ int ll_ins_next(List* list, ListNode* node, const void* data) {
return 0;
}
int ll_ins_prev(List* list, ListNode* node, const void* data) {
ListNode* new_node;
int ll_ins_prev(List *list, ListNode *node, const void *data) {
ListNode *new_node;
if (node == NULL && list->size != 0) {
return -1;
}
@@ -59,7 +59,7 @@ int ll_ins_prev(List* list, ListNode* node, const void* data) {
return -1;
}
new_node->data = (void*)data;
new_node->data = (void *) data;
if (list->size == 0) {
list->head = new_node;
list->head->prev = NULL;
@@ -80,7 +80,7 @@ int ll_ins_prev(List* list, ListNode* node, const void* data) {
return 0;
}
int ll_remove(List* list, ListNode* node, void** data) {
int ll_remove(List *list, ListNode *node, void **data) {
if (node == NULL || list->size == 0) {
return -1;
}
@@ -106,14 +106,14 @@ int ll_remove(List* list, ListNode* node, void** data) {
return 0;
}
int ll_remove_next(List* list, ListNode* node, void** data) {
int ll_remove_next(List *list, ListNode *node, void **data) {
if (node->next == NULL) {
return -1;
}
return ll_remove(list, node->next, data);
}
int ll_remove_prev(List* list, ListNode* node, void** data) {
int ll_remove_prev(List *list, ListNode *node, void **data) {
if (node->prev == NULL) {
return -1;
}

View File

@@ -27,7 +27,7 @@ int clamp_int(int i, int low, int high) {
int binstr_to_int(const char *s) {
int n = 0, m = 1;
for (int i = (int)strlen(s) - 1; i >= 0; --i) {
for (int i = (int) strlen(s) - 1; i >= 0; --i) {
if (s[i] == '1') {
n += m;
}

View File

@@ -1,24 +1,24 @@
#include "lfset.h"
void set_init(Set* set, int (*match)(const void* a, const void* b),
void (*destroy)(void* data)) {
void set_init(Set *set, int (*match)(const void *a, const void *b),
void (*destroy)(void *data)) {
ll_init(set, destroy);
set->match = match;
}
void set_destroy(Set* set) {
void set_destroy(Set *set) {
ll_destroy(set);
}
int set_insert(Set* set, const void* data) {
int set_insert(Set *set, const void *data) {
if (set_is_member(set, data)) {
return 1;
}
return ll_ins_next(set, set->tail, data);
}
int set_remove(Set* set, void** data) {
ListNode* node = NULL;
int set_remove(Set *set, void **data) {
ListNode *node = NULL;
for (node = set->head; node != NULL; node = node->next) {
if (set->match(*data, node->data)) {
@@ -32,9 +32,9 @@ int set_remove(Set* set, void** data) {
return ll_remove_next(set, node, data);
}
int set_union(Set* setu, const Set* a, const Set* b) {
ListNode* node;
void* data;
int set_union(Set *setu, const Set *a, const Set *b) {
ListNode *node;
void *data;
set_init(setu, a->match, NULL);
for (node = a->head; node != NULL; node = node->next) {
@@ -59,9 +59,9 @@ int set_union(Set* setu, const Set* a, const Set* b) {
return 0;
}
int set_intersection(Set* seti, const Set* a, const Set* b) {
ListNode* node;
void* data;
int set_intersection(Set *seti, const Set *a, const Set *b) {
ListNode *node;
void *data;
set_init(seti, a->match, NULL);
for (node = a->head; node != NULL; node = node->next) {
@@ -77,9 +77,9 @@ int set_intersection(Set* seti, const Set* a, const Set* b) {
return 0;
}
int set_difference(Set* setd, const Set* a, const Set* b) {
ListNode* node;
void* data;
int set_difference(Set *setd, const Set *a, const Set *b) {
ListNode *node;
void *data;
set_init(setd, a->match, NULL);
for (node = a->head; node != NULL; node = node->next) {
@@ -95,8 +95,8 @@ int set_difference(Set* setd, const Set* a, const Set* b) {
return 0;
}
int set_is_member(const Set* set, const void* data) {
for (ListNode* node = set->head; node != NULL; node = node->next) {
int set_is_member(const Set *set, const void *data) {
for (ListNode *node = set->head; node != NULL; node = node->next) {
if (set->match(data, node->data)) {
return 1;
}
@@ -104,11 +104,11 @@ int set_is_member(const Set* set, const void* data) {
return 0;
}
int set_is_subset(const Set* a, const Set* b) {
int set_is_subset(const Set *a, const Set *b) {
if (a->size > b->size) {
return 0;
}
for (ListNode* node = a->head; node != NULL; node = node->next) {
for (ListNode *node = a->head; node != NULL; node = node->next) {
if (!set_is_member(b, node->data)) {
return 0;
}
@@ -116,7 +116,7 @@ int set_is_subset(const Set* a, const Set* b) {
return 1;
}
int set_is_equal(const Set* a, const Set* b) {
int set_is_equal(const Set *a, const Set *b) {
if (a->size == b->size) {
return 0;
}

View File

@@ -1,14 +1,14 @@
#include "lfstack.h"
void stack_init(Stack* stack, void (*destroy)(void* data)) {
void stack_init(Stack *stack, void (*destroy)(void *data)) {
ll_init(stack, destroy);
}
void stack_destroy(Stack* stack) {
void stack_destroy(Stack *stack) {
ll_destroy(stack);
}
int stack_push(Stack* stack, void *data) {
int stack_push(Stack *stack, void *data) {
if (stack->size == 0) {
return ll_ins_next(stack, NULL, data);
} else {