diff options
author | Malfurious <m@lfurio.us> | 2024-05-03 22:30:34 -0400 |
---|---|---|
committer | Malfurious <m@lfurio.us> | 2024-05-08 05:57:59 -0400 |
commit | b67c753ac8bed628b5596ff71909708500d432a2 (patch) | |
tree | 232b181a6d404023da5083cfaab4d63e03336217 | |
parent | ac95555111931d77b46ca6ad9fff51ae7db61be9 (diff) | |
download | misplays-b67c753ac8bed628b5596ff71909708500d432a2.tar.gz misplays-b67c753ac8bed628b5596ff71909708500d432a2.zip |
Add additional list operations
Add list functions which can efficiently cut down on some bolierplate.
The rest of the code will eventually be cleaned to make use of them.
Signed-off-by: Malfurious <m@lfurio.us>
-rw-r--r-- | list.c | 24 | ||||
-rw-r--r-- | list.h | 8 |
2 files changed, 28 insertions, 4 deletions
@@ -1,4 +1,3 @@ -#include <stddef.h> #include "list.h" struct _list { @@ -11,6 +10,26 @@ void list_init(struct list *list) { list->end = list; } +int list_empty(struct list *list) { + return list->head == list->end; +} + +int list_singleton(struct list *list) { + return list->head == list->tail && list->head != list->end; +} + +void *list_circular_prev(void *_node) { + struct _list *node = _node; + do { node = node->prev; } while (node == node->listhead); + return node; +} + +void *list_circular_next(void *_node) { + struct _list *node = _node; + do { node = node->next; } while (node == node->listhead); + return node; +} + void list_insert(void *_next, void *_node) { struct _list *node = _node; struct _list *next = _next; @@ -19,6 +38,7 @@ void list_insert(void *_next, void *_node) { prev->next = node; node->next = next; node->prev = prev; + node->listhead = next->listhead; } void list_remove(void *_node) { @@ -27,6 +47,4 @@ void list_remove(void *_node) { struct _list *prev = node->prev; next->prev = prev; prev->next = next; - node->next = NULL; - node->prev = NULL; } @@ -1,10 +1,16 @@ #pragma once -#define LINKEDLIST void *prev, *next +#define LINKEDLIST void *prev, *next, *listhead struct list { void *tail, *head, *end; }; extern void list_init(struct list *list); +extern int list_empty(struct list *list); +extern int list_singleton(struct list *list); + +extern void *list_circular_prev(void *_node); +extern void *list_circular_next(void *_node); + extern void list_insert(void *_next, void *_node); extern void list_remove(void *_node); |