diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-04-11 07:21:28 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-04-11 08:37:33 -0600 |
commit | 8db1fb76584a22ec4f93da1b52b3ff63a8e21c96 (patch) | |
tree | 358c65a75ec808c520ae230e29bb4ce60a0552bf /libmisc/include | |
parent | b44c17b2791ffa3f11d96db8fb19fe9e4d45b837 (diff) |
Move the linkedlist to libmisc
Diffstat (limited to 'libmisc/include')
-rw-r--r-- | libmisc/include/libmisc/linkedlist.h | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/libmisc/include/libmisc/linkedlist.h b/libmisc/include/libmisc/linkedlist.h new file mode 100644 index 0000000..045a468 --- /dev/null +++ b/libmisc/include/libmisc/linkedlist.h @@ -0,0 +1,57 @@ +/* libmisc/linkedlist.h - Singly- and doubly- linked lists + * + * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#ifndef _LIBMISC_LINKEDLIST_H_ +#define _LIBMISC_LINKEDLIST_H_ + +#include <libmisc/assert.h> + +/* singly linked list *********************************************************/ + +typedef struct _lm_sll_node { + struct _lm_sll_node *rear; +} lm_sll_node; + +typedef struct { + lm_sll_node *front, *rear; +} lm_sll_root; + +#define lm_sll_node_cast(node_typ, node_ptr) \ + ({ \ + static_assert(_Generic(node_ptr, lm_sll_node *: 1, default: 0), \ + "typeof("#node_ptr") != lm_sll_node *"); \ + assert(node_ptr); \ + static_assert(offsetof(node_typ, lm_sll_node) == 0); \ + ((node_typ*)(node_ptr)); \ + }) + +void lm_sll_push_to_rear(lm_sll_root *root, lm_sll_node *node); +void lm_sll_pop_from_front(lm_sll_root *root); + +/* doubly linked list *********************************************************/ + +typedef struct _lm_dll_node { + struct _lm_dll_node *front, *rear; +} lm_dll_node; + +typedef struct { + lm_dll_node *front, *rear; +} lm_dll_root; + +#define lm_dll_node_cast(node_typ, node_ptr) \ + ({ \ + static_assert(_Generic(node_ptr, lm_dll_node *: 1, default: 0), \ + "typeof("#node_ptr") != lm_dll_node *"); \ + assert(node_ptr); \ + static_assert(offsetof(node_typ, lm_dll_node) == 0); \ + ((node_typ*)(node_ptr)); \ + }) + +void lm_dll_push_to_rear(lm_dll_root *root, lm_dll_node *node); +void lm_dll_remove(lm_dll_root *root, lm_dll_node *node); +void lm_dll_pop_from_front(lm_dll_root *root); + +#endif /* _LIBMISC_LINKEDLIST_H_ */ |