Code cleanup, implement LL_ITER macro

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

1
.idea/.name Normal file
View File

@ -0,0 +1 @@
flint

View File

@ -11,18 +11,26 @@ typedef struct {
int size; int size;
int (*compare)(const void *a, const void *b); int (*compare)(const void *a, const void *b);
void (*destroy)(void *data); void (*destroy)(void *data);
struct BinTreeNode *root; struct BinTreeNode *root;
} BinTree; } BinTree;
void bintree_init(BinTree *tree, void (*destroy)(void *data)); void bintree_init(BinTree *tree, void (*destroy)(void *data));
void bintree_destroy(BinTree *tree); void bintree_destroy(BinTree *tree);
int bintree_ins_left(BinTree *tree, BinTreeNode *node, void *data); int bintree_ins_left(BinTree *tree, BinTreeNode *node, void *data);
int bintree_ins_right(BinTree *tree, BinTreeNode *node, void *data); int bintree_ins_right(BinTree *tree, BinTreeNode *node, void *data);
void bintree_rem_left(BinTree *tree, BinTreeNode *node); void bintree_rem_left(BinTree *tree, BinTreeNode *node);
void bintree_rem_right(BinTree *tree, BinTreeNode *node); void bintree_rem_right(BinTree *tree, BinTreeNode *node);
int bintree_merge(BinTree *merge, BinTree *left, BinTree *right, void *data); int bintree_merge(BinTree *merge, BinTree *left, BinTree *right, void *data);
void bintree_debug_print(BinTree *tree); void bintree_debug_print(BinTree *tree);
#define bintree_is_eob(node) ((node) == NULL) #define bintree_is_eob(node) ((node) == NULL)

View File

@ -4,10 +4,15 @@
#include <stdlib.h> #include <stdlib.h>
char *get_input(const char *); char *get_input(const char *);
char **split(char *, size_t *, const char *); char **split(char *, size_t *, const char *);
char **get_lines(const char *, size_t *); char **get_lines(const char *, size_t *);
int *get_ints(const char *, size_t *); int *get_ints(const char *, size_t *);
void del_split(char **); void del_split(char **);
void del_lines(char **); void del_lines(char **);
#endif // LIBFLINT_INPUT_H #endif // LIBFLINT_INPUT_H

View File

@ -4,27 +4,37 @@
#include <stddef.h> #include <stddef.h>
typedef struct ListNode { typedef struct ListNode {
void* data; void *data;
struct ListNode* next; struct ListNode *next;
struct ListNode* prev; struct ListNode *prev;
} ListNode; } ListNode;
typedef struct { typedef struct {
size_t size; size_t size;
void (*destroy)(void* data); void (*destroy)(void *data);
int (*match)(const void* a, const void* b);
struct ListNode* head; int (*match)(const void *a, const void *b);
struct ListNode* tail;
struct ListNode *head;
struct ListNode *tail;
} List; } List;
void ll_init(List* list, void (*destroy)(void *data)); void ll_init(List *list, void (*destroy)(void *data));
void ll_destroy(List* list);
int ll_ins_next(List* list, ListNode* node, const void* data); void ll_destroy(List *list);
int ll_ins_prev(List* list, ListNode* node, const void* data);
int ll_remove(List* list, ListNode* node, void** data); int ll_ins_next(List *list, ListNode *node, const void *data);
int ll_remove_next(List* list, ListNode* node, void** data);
int ll_remove_prev(List* list, ListNode* node, void** data); int ll_ins_prev(List *list, ListNode *node, const void *data);
int ll_remove(List *list, ListNode *node, void **data);
int ll_remove_next(List *list, ListNode *node, void **data);
int ll_remove_prev(List *list, ListNode *node, void **data);
/* Provides ListNode *node for the iteration loop */
#define LL_ITER(list) for(ListNode *node = (list)->head; node != NULL; node = node->next)
#endif #endif

View File

@ -2,8 +2,11 @@
#define LIBFLINT_H_MATH #define LIBFLINT_H_MATH
int max_int(int a, int b); int max_int(int a, int b);
int min_int(int a, int b); int min_int(int a, int b);
int clamp_int(int i, int low, int high); int clamp_int(int i, int low, int high);
int binstr_to_int(const char *s); int binstr_to_int(const char *s);
#endif // LIBFLINT_H_MATH #endif // LIBFLINT_H_MATH

View File

@ -5,16 +5,25 @@
#define Set List #define Set List
void set_init(Set* set, int (*match)(const void* a, const void* b), void set_init(Set *set, int (*match)(const void *a, const void *b),
void (*destroy)(void* data)); void (*destroy)(void *data));
void set_destroy(Set* set);
int set_insert(Set* set, const void* data); void set_destroy(Set *set);
int set_remove(Set* set, void** data);
int set_union(Set* setu, const Set* a, const Set* b); int set_insert(Set *set, const void *data);
int set_intersection(Set* seti, const Set* a, const Set* b);
int set_difference(Set* setd, const Set* a, const Set* b); int set_remove(Set *set, void **data);
int set_is_member(const Set* set, const void* data);
int set_is_subset(const Set* a, const Set* b); int set_union(Set *setu, const Set *a, const Set *b);
int set_is_equal(const Set* a, const Set* b);
int set_intersection(Set *seti, const Set *a, const Set *b);
int set_difference(Set *setd, const Set *a, const Set *b);
int set_is_member(const Set *set, const void *data);
int set_is_subset(const Set *a, const Set *b);
int set_is_equal(const Set *a, const Set *b);
#endif #endif

View File

@ -5,10 +5,14 @@
#define Stack List #define Stack List
void stack_init(Stack* stack, void (*destroy)(void* data)); void stack_init(Stack *stack, void (*destroy)(void *data));
void stack_destroy(Stack* stack);
int stack_push(Stack* stack, void *data); void stack_destroy(Stack *stack);
int stack_push(Stack *stack, void *data);
void *stack_peek(Stack *stack); void *stack_peek(Stack *stack);
int stack_pop(Stack *stack, void **data); int stack_pop(Stack *stack, void **data);
#endif #endif

View File

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

View File

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

View File

@ -3,25 +3,25 @@
#include "lflinkedlist.h" #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->size = 0;
list->destroy = destroy; list->destroy = destroy;
list->head = NULL; list->head = NULL;
list->tail = NULL; list->tail = NULL;
} }
void ll_destroy(List* list) { void ll_destroy(List *list) {
void* data; void *data;
while (list->size > 0) { 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); list->destroy(data);
} }
} }
memset(list, 0, sizeof(List)); memset(list, 0, sizeof(List));
} }
int ll_ins_next(List* list, ListNode* node, const void* data) { int ll_ins_next(List *list, ListNode *node, const void *data) {
ListNode* new_node; ListNode *new_node;
if (node == NULL && list->size != 0) { if (node == NULL && list->size != 0) {
return -1; return -1;
} }
@ -29,7 +29,7 @@ int ll_ins_next(List* list, ListNode* node, const void* data) {
return -1; return -1;
} }
new_node->data = (void*)data; new_node->data = (void *) data;
if (list->size == 0) { if (list->size == 0) {
list->head = new_node; list->head = new_node;
list->head->prev = NULL; list->head->prev = NULL;
@ -50,8 +50,8 @@ int ll_ins_next(List* list, ListNode* node, const void* data) {
return 0; return 0;
} }
int ll_ins_prev(List* list, ListNode* node, const void* data) { int ll_ins_prev(List *list, ListNode *node, const void *data) {
ListNode* new_node; ListNode *new_node;
if (node == NULL && list->size != 0) { if (node == NULL && list->size != 0) {
return -1; return -1;
} }
@ -59,7 +59,7 @@ int ll_ins_prev(List* list, ListNode* node, const void* data) {
return -1; return -1;
} }
new_node->data = (void*)data; new_node->data = (void *) data;
if (list->size == 0) { if (list->size == 0) {
list->head = new_node; list->head = new_node;
list->head->prev = NULL; list->head->prev = NULL;
@ -80,7 +80,7 @@ int ll_ins_prev(List* list, ListNode* node, const void* data) {
return 0; 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) { if (node == NULL || list->size == 0) {
return -1; return -1;
} }
@ -106,14 +106,14 @@ int ll_remove(List* list, ListNode* node, void** data) {
return 0; 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) { if (node->next == NULL) {
return -1; return -1;
} }
return ll_remove(list, node->next, data); 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) { if (node->prev == NULL) {
return -1; 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 binstr_to_int(const char *s) {
int n = 0, m = 1; 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') { if (s[i] == '1') {
n += m; n += m;
} }

View File

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

View File

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

View File

@ -7,65 +7,65 @@
#include "lfbinarytree.h" #include "lfbinarytree.h"
#include "lfmath.h" #include "lfmath.h"
void print_ll(List* list) { void print_ll(List *list) {
for (ListNode* node = list->head; node != NULL; node = node->next) { LL_ITER(list) {
printf(" %d", *((int*)node->data)); printf(" %d", *((int *) node->data));
} }
printf("\n"); printf("\n");
} }
void test_ll() { void test_ll() {
printf("\n--- LIST TEST ---\n"); printf("\n--- LIST TEST ---\n");
List* list = malloc(sizeof(List)); List *list = malloc(sizeof(List));
ll_init(list, NULL); ll_init(list, NULL);
int i = 1; int i = 1;
int j = 2; int j = 2;
int k = 4; int k = 4;
ll_ins_next(list, list->head, (void*)&i); ll_ins_next(list, list->head, (void *) &i);
ll_ins_next(list, list->tail, (void*)&j); ll_ins_next(list, list->tail, (void *) &j);
ll_ins_next(list, list->tail, (void*)&k); ll_ins_next(list, list->tail, (void *) &k);
printf("List: "); printf("List: ");
print_ll(list); print_ll(list);
void* data; void *data;
ll_remove_next(list, list->head, &data); ll_remove_next(list, list->head, &data);
printf("List: "); printf("List: ");
print_ll(list); print_ll(list);
printf("Removed: %d\n", *((int*)data)); printf("Removed: %d\n", *((int *) data));
ll_destroy(list); ll_destroy(list);
free(list); free(list);
} }
int int_match(const void* a, const void* b) { int int_match(const void *a, const void *b) {
return *((int*)a) == *((int*)b); return *((int *) a) == *((int *) b);
} }
void test_set() { void test_set() {
printf("\n--- SET TEST ---\n"); printf("\n--- SET TEST ---\n");
Set* set = malloc(sizeof(Set)); Set *set = malloc(sizeof(Set));
set_init(set, int_match, NULL); set_init(set, int_match, NULL);
int i = 1; int i = 1;
int j = 2; int j = 2;
int k = 2; int k = 2;
set_insert(set, (void*)&i); set_insert(set, (void *) &i);
set_insert(set, (void*)&j); set_insert(set, (void *) &j);
set_insert(set, (void*)&k); set_insert(set, (void *) &k);
int i2 = 1; int i2 = 1;
int j2 = 4; int j2 = 4;
Set* set2 = malloc(sizeof(Set)); Set *set2 = malloc(sizeof(Set));
set_init(set2, int_match, NULL); set_init(set2, int_match, NULL);
set_insert(set2, (void*)&i2); set_insert(set2, (void *) &i2);
set_insert(set2, (void*)&j2); set_insert(set2, (void *) &j2);
printf("Set 1:"); printf("Set 1:");
print_ll(set); print_ll(set);
@ -74,9 +74,9 @@ void test_set() {
print_ll(set2); print_ll(set2);
printf("\n"); printf("\n");
Set* set_u = malloc(sizeof(Set)); Set *set_u = malloc(sizeof(Set));
Set* set_i = malloc(sizeof(Set)); Set *set_i = malloc(sizeof(Set));
Set* set_d = malloc(sizeof(Set)); Set *set_d = malloc(sizeof(Set));
set_union(set_u, set, set2); set_union(set_u, set, set2);
printf("Union:"); printf("Union:");
@ -113,10 +113,10 @@ void test_stack() {
printf("Stack size: %lu\n", stack->size); printf("Stack size: %lu\n", stack->size);
int *p = NULL; int *p = NULL;
stack_pop(stack, (void **)&p); stack_pop(stack, (void **) &p);
printf("b = %d\n", *p); printf("b = %d\n", *p);
stack_pop(stack, (void **)&p); stack_pop(stack, (void **) &p);
printf("a = %d\n", *p); printf("a = %d\n", *p);
printf("Stack size: %lu\n", stack->size); printf("Stack size: %lu\n", stack->size);