From 10ed0d417296dbd3aba961e51251c26e67732fce Mon Sep 17 00:00:00 2001 From: Nick Chambers Date: Thu, 17 Nov 2022 10:05:44 -0600 Subject: [PATCH] Actually add some code --- include/ordinary.h | 13 ++++---- src/ordinary.c | 80 +++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 79 insertions(+), 14 deletions(-) diff --git a/include/ordinary.h b/include/ordinary.h index b8a39b9..771d94e 100644 --- a/include/ordinary.h +++ b/include/ordinary.h @@ -19,15 +19,16 @@ struct ordinary_list { typedef uint8_t (*callback)(struct ordinary_node *node, uint32_t idx); void ordinary_list_new(struct ordinary_list *list, uint32_t limit); -void ordinary_list_del(struct ordinary_list *list); +void ordinary_list_delete(struct ordinary_list *list); -struct ordinary_node *ordinary_list_add(struct ordinary_list *list, void *val); -void standard_list_mov(struct standard_list *list, struct standard_node *node); -struct ordinary_node *ordinary_list_pop(struct ordinary_list *list); - -struct ordinary_node *ordinary_list_at(struct ordinary_list *list, uint32_t idx); +uint8_t ordinary_list_empty(struct ordinary_list *list); uint8_t ordinary_list_full(struct ordinary_list *list); +struct ordinary_node *ordinary_list_at(struct ordinary_list *list, uint32_t idx); struct ordinary_node *ordinary_list_find(struct ordinary_list *list, callback cb); uint8_t ordinary_list_for(struct ordinary_list *list, callback cb); +struct ordinary_node *ordinary_list_add(struct ordinary_list *list, void *val); +void ordinary_list_move(struct standard_list *dst, struct standard_list *src, struct standard_node *node); +struct ordinary_node *ordinary_list_pop(struct ordinary_list *list); + #endif diff --git a/src/ordinary.c b/src/ordinary.c index 9027c7c..1bb15c7 100644 --- a/src/ordinary.c +++ b/src/ordinary.c @@ -8,16 +8,78 @@ void ordinary_list_new(struct ordinary_list *list, uint32_t limit) { list->limit = limit; } -void ordinary_list_del(struct ordinary_list *list) { - struct ordinary_node *at = list->head; +void ordinary_list_delete(struct ordinary_list *list) { + struct ordinary_node *node = list->head; - while(at) { - struct ordinary_node *next = at->next; - free(at); - at = next; + while(node) { + struct ordinary_node *next = node->next; + free(node); // R.I.P. + node = next; } } +uint8_t ordinary_list_empty(struct ordinary_list *list) { + if(!list->limit) { + return 1; + } + + return list->count == 0; +} + +uint8_t ordinary_list_full(struct ordinary_list *list) { + if(!list->limit) { + return 1; + } + + return 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(list, 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; @@ -57,7 +119,9 @@ struct ordinary_node *ordinary_list_add(struct ordinary_list *list, void *val) { return node; } -void ordinary_list_mov(struct standard_list *list, struct standard_node *node) { +void ordinary_list_mov(struct standard_list *dst, struct standard_list *src, struct standard_node *node) { + if(dst->limit &&) + struct ordinary_node *prev = node->prev; struct ordinary_node *next = node->next; @@ -71,5 +135,5 @@ void ordinary_list_mov(struct standard_list *list, struct standard_node *node) { node->next = NULL; } - ordinary_list_insert(list, node); + ordinary_list_insert(dst, node); }