Code cleanup, implement LL_ITER macro
This commit is contained in:
		
							
								
								
									
										1
									
								
								.idea/.name
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								.idea/.name
									
									
									
										generated
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| flint | ||||
| @@ -11,18 +11,26 @@ typedef struct { | ||||
|     int size; | ||||
|  | ||||
|     int (*compare)(const void *a, const void *b); | ||||
|  | ||||
|     void (*destroy)(void *data); | ||||
|  | ||||
|     struct BinTreeNode *root; | ||||
| } BinTree; | ||||
|  | ||||
| void bintree_init(BinTree *tree, void (*destroy)(void *data)); | ||||
|  | ||||
| void bintree_destroy(BinTree *tree); | ||||
|  | ||||
| int bintree_ins_left(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_right(BinTree *tree, BinTreeNode *node); | ||||
|  | ||||
| int bintree_merge(BinTree *merge, BinTree *left, BinTree *right, void *data); | ||||
|  | ||||
| void bintree_debug_print(BinTree *tree); | ||||
|  | ||||
| #define bintree_is_eob(node) ((node) == NULL) | ||||
|   | ||||
| @@ -4,10 +4,15 @@ | ||||
| #include <stdlib.h> | ||||
|  | ||||
| char *get_input(const char *); | ||||
|  | ||||
| char **split(char *, size_t *, const char *); | ||||
|  | ||||
| char **get_lines(const char *, size_t *); | ||||
|  | ||||
| int *get_ints(const char *, size_t *); | ||||
|  | ||||
| void del_split(char **); | ||||
|  | ||||
| void del_lines(char **); | ||||
|  | ||||
| #endif // LIBFLINT_INPUT_H | ||||
|   | ||||
| @@ -4,27 +4,37 @@ | ||||
| #include <stddef.h> | ||||
|  | ||||
| typedef struct ListNode { | ||||
|     void* data; | ||||
|     struct ListNode* next; | ||||
|     struct ListNode* prev; | ||||
|     void *data; | ||||
|     struct ListNode *next; | ||||
|     struct ListNode *prev; | ||||
| } ListNode; | ||||
|  | ||||
| typedef struct { | ||||
|     size_t size; | ||||
|  | ||||
|     void (*destroy)(void* data); | ||||
|     int (*match)(const void* a, const void* b); | ||||
|     void (*destroy)(void *data); | ||||
|  | ||||
|     struct ListNode* head; | ||||
|     struct ListNode* tail; | ||||
|     int (*match)(const void *a, const void *b); | ||||
|  | ||||
|     struct ListNode *head; | ||||
|     struct ListNode *tail; | ||||
| } List; | ||||
|  | ||||
| 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); | ||||
| 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); | ||||
| 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); | ||||
|  | ||||
| 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 | ||||
|   | ||||
| @@ -2,8 +2,11 @@ | ||||
| #define LIBFLINT_H_MATH | ||||
|  | ||||
| int max_int(int a, int b); | ||||
|  | ||||
| int min_int(int a, int b); | ||||
|  | ||||
| int clamp_int(int i, int low, int high); | ||||
|  | ||||
| int binstr_to_int(const char *s); | ||||
|  | ||||
| #endif // LIBFLINT_H_MATH | ||||
|   | ||||
| @@ -5,16 +5,25 @@ | ||||
|  | ||||
| #define Set List | ||||
|  | ||||
| void set_init(Set* set, int (*match)(const void* a, const void* b), | ||||
|         void (*destroy)(void* data)); | ||||
| void set_destroy(Set* set); | ||||
| int set_insert(Set* set, const void* data); | ||||
| int set_remove(Set* set, void** data); | ||||
| int set_union(Set* setu, 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); | ||||
| void set_init(Set *set, int (*match)(const void *a, const void *b), | ||||
|               void (*destroy)(void *data)); | ||||
|  | ||||
| void set_destroy(Set *set); | ||||
|  | ||||
| int set_insert(Set *set, const void *data); | ||||
|  | ||||
| int set_remove(Set *set, void **data); | ||||
|  | ||||
| int set_union(Set *setu, 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 | ||||
|   | ||||
| @@ -5,10 +5,14 @@ | ||||
|  | ||||
| #define Stack List | ||||
|  | ||||
| void stack_init(Stack* stack, void (*destroy)(void* data)); | ||||
| void stack_destroy(Stack* stack); | ||||
| int stack_push(Stack* stack, 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_peek(Stack *stack); | ||||
|  | ||||
| int stack_pop(Stack *stack, void **data); | ||||
|  | ||||
| #endif | ||||
|   | ||||
| @@ -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); | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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); | ||||
|   | ||||
| @@ -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; | ||||
|     } | ||||
|   | ||||
| @@ -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; | ||||
|         } | ||||
|   | ||||
							
								
								
									
										40
									
								
								src/lfset.c
									
									
									
									
									
								
							
							
						
						
									
										40
									
								
								src/lfset.c
									
									
									
									
									
								
							| @@ -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; | ||||
|     } | ||||
|   | ||||
| @@ -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 { | ||||
|   | ||||
| @@ -7,65 +7,65 @@ | ||||
| #include "lfbinarytree.h" | ||||
| #include "lfmath.h" | ||||
|  | ||||
| void print_ll(List* list) { | ||||
|     for (ListNode* node = list->head; node != NULL; node = node->next) { | ||||
|         printf(" %d", *((int*)node->data)); | ||||
| void print_ll(List *list) { | ||||
|     LL_ITER(list) { | ||||
|         printf(" %d", *((int *) node->data)); | ||||
|     } | ||||
|     printf("\n"); | ||||
| } | ||||
|  | ||||
| void test_ll() { | ||||
|     printf("\n--- LIST TEST ---\n"); | ||||
|     List* list = malloc(sizeof(List)); | ||||
|     List *list = malloc(sizeof(List)); | ||||
|     ll_init(list, NULL); | ||||
|  | ||||
|     int i = 1; | ||||
|     int j = 2; | ||||
|     int k = 4; | ||||
|  | ||||
|     ll_ins_next(list, list->head, (void*)&i); | ||||
|     ll_ins_next(list, list->tail, (void*)&j); | ||||
|     ll_ins_next(list, list->tail, (void*)&k); | ||||
|     ll_ins_next(list, list->head, (void *) &i); | ||||
|     ll_ins_next(list, list->tail, (void *) &j); | ||||
|     ll_ins_next(list, list->tail, (void *) &k); | ||||
|  | ||||
|     printf("List: "); | ||||
|     print_ll(list); | ||||
|  | ||||
|     void* data; | ||||
|     void *data; | ||||
|     ll_remove_next(list, list->head, &data); | ||||
|  | ||||
|     printf("List: "); | ||||
|     print_ll(list); | ||||
|     printf("Removed: %d\n", *((int*)data)); | ||||
|     printf("Removed: %d\n", *((int *) data)); | ||||
|  | ||||
|     ll_destroy(list); | ||||
|     free(list); | ||||
| } | ||||
|  | ||||
| int int_match(const void* a, const void* b) { | ||||
|     return *((int*)a) == *((int*)b); | ||||
| int int_match(const void *a, const void *b) { | ||||
|     return *((int *) a) == *((int *) b); | ||||
| } | ||||
|  | ||||
| void test_set() { | ||||
|     printf("\n--- SET TEST ---\n"); | ||||
|     Set* set = malloc(sizeof(Set)); | ||||
|     Set *set = malloc(sizeof(Set)); | ||||
|     set_init(set, int_match, NULL); | ||||
|  | ||||
|     int i = 1; | ||||
|     int j = 2; | ||||
|     int k = 2; | ||||
|  | ||||
|     set_insert(set, (void*)&i); | ||||
|     set_insert(set, (void*)&j); | ||||
|     set_insert(set, (void*)&k); | ||||
|     set_insert(set, (void *) &i); | ||||
|     set_insert(set, (void *) &j); | ||||
|     set_insert(set, (void *) &k); | ||||
|  | ||||
|     int i2 = 1; | ||||
|     int j2 = 4; | ||||
|  | ||||
|     Set* set2 = malloc(sizeof(Set)); | ||||
|     Set *set2 = malloc(sizeof(Set)); | ||||
|     set_init(set2, int_match, NULL); | ||||
|  | ||||
|     set_insert(set2, (void*)&i2); | ||||
|     set_insert(set2, (void*)&j2); | ||||
|     set_insert(set2, (void *) &i2); | ||||
|     set_insert(set2, (void *) &j2); | ||||
|  | ||||
|     printf("Set 1:"); | ||||
|     print_ll(set); | ||||
| @@ -74,9 +74,9 @@ void test_set() { | ||||
|     print_ll(set2); | ||||
|     printf("\n"); | ||||
|  | ||||
|     Set* set_u = malloc(sizeof(Set)); | ||||
|     Set* set_i = malloc(sizeof(Set)); | ||||
|     Set* set_d = malloc(sizeof(Set)); | ||||
|     Set *set_u = malloc(sizeof(Set)); | ||||
|     Set *set_i = malloc(sizeof(Set)); | ||||
|     Set *set_d = malloc(sizeof(Set)); | ||||
|  | ||||
|     set_union(set_u, set, set2); | ||||
|     printf("Union:"); | ||||
| @@ -113,10 +113,10 @@ void test_stack() { | ||||
|     printf("Stack size: %lu\n", stack->size); | ||||
|  | ||||
|     int *p = NULL; | ||||
|     stack_pop(stack, (void **)&p); | ||||
|     stack_pop(stack, (void **) &p); | ||||
|     printf("b = %d\n", *p); | ||||
|  | ||||
|     stack_pop(stack, (void **)&p); | ||||
|     stack_pop(stack, (void **) &p); | ||||
|     printf("a = %d\n", *p); | ||||
|     printf("Stack size: %lu\n", stack->size); | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user