From 671b003a9c75ccaa8954a1830591bd30d3e8b2da Mon Sep 17 00:00:00 2001 From: Nick Chambers Date: Fri, 25 Nov 2022 01:01:04 -0600 Subject: [PATCH] Break functions into modules --- src/examine.c | 9 +++ src/iter.c | 47 +++++++++++++++ src/maint.c | 103 ++++++++++++++++++++++++++++++++ src/ordinary.c | 156 ------------------------------------------------- 4 files changed, 159 insertions(+), 156 deletions(-) create mode 100644 src/examine.c create mode 100644 src/iter.c create mode 100644 src/maint.c diff --git a/src/examine.c b/src/examine.c new file mode 100644 index 0000000..2449369 --- /dev/null +++ b/src/examine.c @@ -0,0 +1,9 @@ +#include + +uint8_t ordinary_list_empty(struct ordinary_list *list) { + return !list->head; +} + +uint8_t ordinary_list_full(struct ordinary_list *list) { + return !list->limit || list->count == list->limit; +} diff --git a/src/iter.c b/src/iter.c new file mode 100644 index 0000000..94d3c45 --- /dev/null +++ b/src/iter.c @@ -0,0 +1,47 @@ +#include + +struct ordinary_node *ordinary_list_at(struct ordinary_list *list, uint32_t idx) { + struct ordinary_node *node = list->head; + + for(; node; node = node->next) { + idx -= 1; + + if(!idx) { + return node; + } + } + + return NULL; +} + +struct ordinary_node *ordinary_list_find(struct ordinary_list *list, callback cb) { + struct ordinary_node *node = list->head; + uint32_t idx = 0; + + for(; node; node = node->next) { + if(cb(node, idx)) { + return node; + } + + if(list->limit) { + idx += 1; + } + } + + return NULL; +} + +uint8_t ordinary_list_for(struct ordinary_list *list, callback cb) { + struct ordinary_node *node = list->head; + uint32_t idx = 0, res = 0; + + for(; node; node = node->next) { + res |= cb(node, idx); + + if(list->limit) { + idx += 1; + } + } + + return res; +} diff --git a/src/maint.c b/src/maint.c new file mode 100644 index 0000000..a109eca --- /dev/null +++ b/src/maint.c @@ -0,0 +1,103 @@ +#include + +static void ordinary_list_insert(struct ordinary_list *list, struct ordinary_node *node) { + if(!list->head) { + list->head = node; + list->tail = node; + } else if(list->head == list->tail) { + list->tail = node; + list->head->next = list->tail; + list->tail->prev = list->head; + } else { + list->tail->next = node; + node->prev = list->tail; + list->tail = node; + } + + if(list->limit) { + list->count += 1; + } +} + +struct ordinary_node *ordinary_list_add(struct ordinary_list *list, void *val) { + if(ordinary_list_full(list)) { + return NULL; + } + + struct ordinary_node *node = malloc(sizeof(struct ordinary_node)); + + if(!node) { + return NULL; + } + + node->prev = NULL; + node->next = NULL; + node->val = val; + ordinary_list_insert(list, node); + return node; +} + +uint8_t ordinary_list_mov(struct ordinary_list *dst, struct ordinary_list *src, struct ordinary_node *node) { + if(ordinary_list_full(dst) || ordinary_list_empty(src)) { + return 0; + } + + struct ordinary_node *prev = node->prev; + struct ordinary_node *next = node->next; + + if(prev) { + prev->next = next; + node->prev = NULL; + } + + if(next) { + next->prev = prev; + node->next = NULL; + } + + ordinary_list_insert(dst, node); + return 1; +} + +struct ordinary_node *ordinary_list_pop(struct ordinary_list *list) { + if(ordinary_list_empty(list)) { + return NULL; + } + + struct ordinary_node *node = list->tail; + + if(list->head == list->tail) { + list->head = NULL; + list->tail = NULL; + } else { + list->tail = list->tail->prev; + list->tail->next = NULL; + } + + if(list->limit) { + list->count -= 1; + } + + return node; +} + +void ordinary_list_rem(struct ordinary_list *list, struct ordinary_node *node) { + struct ordinary_node *prev = node->prev; + struct ordinary_node *next = node->next; + + if(prev) { + prev->next = next; + node->prev = NULL; + } + + if(next) { + next->prev = prev; + node->next = NULL; + } + + free(node); + + if(list->limit) { + list->count -= 1; + } +} diff --git a/src/ordinary.c b/src/ordinary.c index ec5d082..f44777b 100644 --- a/src/ordinary.c +++ b/src/ordinary.c @@ -22,159 +22,3 @@ void ordinary_list_delete(struct ordinary_list *list) { list->count = 0; list->limit = 0; } - -uint8_t ordinary_list_empty(struct ordinary_list *list) { - return !list->head; -} - -uint8_t ordinary_list_full(struct ordinary_list *list) { - return !list->limit || list->count == list->limit; -} - -struct ordinary_node *ordinary_list_at(struct ordinary_list *list, uint32_t idx) { - struct ordinary_node *node = list->head; - - for(; node; node = node->next) { - idx -= 1; - - if(!idx) { - return node; - } - } - - return NULL; -} - -struct ordinary_node *ordinary_list_find(struct ordinary_list *list, callback cb) { - struct ordinary_node *node = list->head; - uint32_t idx = 0; - - for(; node; node = node->next) { - if(cb(node, idx)) { - return node; - } - - if(list->limit) { - idx += 1; - } - } - - return NULL; -} - -uint8_t ordinary_list_for(struct ordinary_list *list, callback cb) { - struct ordinary_node *node = list->head; - uint32_t idx = 0, res = 0; - - for(; node; node = node->next) { - res |= cb(node, idx); - - if(list->limit) { - idx += 1; - } - } - - return res; -} - -static void ordinary_list_insert(struct ordinary_list *list, struct ordinary_node *node) { - if(!list->head) { - list->head = node; - list->tail = node; - } else if(list->head == list->tail) { - list->tail = node; - list->head->next = list->tail; - list->tail->prev = list->head; - } else { - list->tail->next = node; - node->prev = list->tail; - list->tail = node; - } - - if(list->limit) { - list->count += 1; - } -} - -struct ordinary_node *ordinary_list_add(struct ordinary_list *list, void *val) { - if(ordinary_list_full(list)) { - return NULL; - } - - struct ordinary_node *node = malloc(sizeof(struct ordinary_node)); - - if(!node) { - return NULL; - } - - node->prev = NULL; - node->next = NULL; - node->val = val; - ordinary_list_insert(list, node); - return node; -} - -uint8_t ordinary_list_mov(struct ordinary_list *dst, struct ordinary_list *src, struct ordinary_node *node) { - if(ordinary_list_full(dst) || ordinary_list_empty(src)) { - return 0; - } - - struct ordinary_node *prev = node->prev; - struct ordinary_node *next = node->next; - - if(prev) { - prev->next = next; - node->prev = NULL; - } - - if(next) { - next->prev = prev; - node->next = NULL; - } - - ordinary_list_insert(dst, node); - return 1; -} - -struct ordinary_node *ordinary_list_pop(struct ordinary_list *list) { - if(ordinary_list_empty(list)) { - return NULL; - } - - struct ordinary_node *node = list->tail; - - if(list->head == list->tail) { - list->head = NULL; - list->tail = NULL; - } else { - list->tail = list->tail->prev; - list->tail->next = NULL; - } - - if(list->limit) { - list->count -= 1; - } - - return node; -} - -void ordinary_list_rem(struct ordinary_list *list, struct ordinary_node *node) { - struct ordinary_node *prev = node->prev; - struct ordinary_node *next = node->next; - - if(prev) { - prev->next = next; - node->prev = NULL; - } - - if(next) { - next->prev = prev; - node->next = NULL; - } - - free(node); - - if(list->limit) { - list->count -= 1; - } -}