Lazuli
list.h
Go to the documentation of this file.
1 /*
2  * SPDX-License-Identifier: GPL-3.0-only
3  * This file is part of Lazuli.
4  */
5 
14 #ifndef LAZULI_LIST_H
15 #define LAZULI_LIST_H
16 
17 #include <Lazuli/common.h>
18 #include <Lazuli/config.h>
19 
21 
25 typedef struct _Lz_LinkedListElement {
28 
32 
36 typedef struct {
39 
43 
47 #define LINKED_LIST_INIT { NULL, NULL }
48 
52 #define LINKED_LIST_ELEMENT_INIT { NULL, NULL }
53 
61 void
62 List_Append(Lz_LinkedList * const linkedList,
63  Lz_LinkedListElement * const item);
64 
72 void
73 List_Prepend(Lz_LinkedList * const linkedList,
74  Lz_LinkedListElement * const item);
75 
84 void
85 List_AppendList(Lz_LinkedList * const linkedListDestination,
86  Lz_LinkedList * const linkedListToMove);
87 
99 List_PickFirst(Lz_LinkedList * const linkedList);
100 
112 List_PointFirst(const Lz_LinkedList * const linkedList);
113 
127 bool
128 List_IsEmpty(const Lz_LinkedList * const linkedList);
129 
146 #define List_UntypedForEach(LINKEDLIST, ITEM) \
147  if ((LZ_CONFIG_CHECK_NULL_PARAMETERS_IN_LISTS && (NULL == (LINKEDLIST))) || \
148  (NULL == (LINKEDLIST)->first)) \
149  {} \
150  else \
151  for ((ITEM) = (LINKEDLIST)->first; \
152  NULL != (ITEM); \
153  (ITEM) = (ITEM)->next)
154 
178 #define List_ForEach(LINKEDLIST, TYPE, ITEM, MEMBER) \
179  if ((LZ_CONFIG_CHECK_NULL_PARAMETERS_IN_LISTS && (NULL == (LINKEDLIST))) || \
180  (NULL == (LINKEDLIST)->first)) \
181  {} \
182  else \
183  for ((ITEM) = CONTAINER_OF((LINKEDLIST)->first, MEMBER, TYPE); \
184  NULL != (ITEM); \
185  (ITEM) = \
186  (NULL == ((ITEM)->MEMBER).next) \
187  ? NULL \
188  : CONTAINER_OF(((ITEM)->MEMBER).next, MEMBER, TYPE))
189 
222 #define List_RemovableForEach(LINKEDLIST, TYPE, ITEM, MEMBER, ITERATOR) \
223  if ((LZ_CONFIG_CHECK_NULL_PARAMETERS_IN_LISTS && (NULL == (LINKEDLIST))) || \
224  (NULL == (LINKEDLIST)->first)) \
225  {} \
226  else \
227  for (STATIC_CHECK_TYPE((*(ITERATOR)), Lz_LinkedListElement), \
228  (ITERATOR) = (LINKEDLIST)->first, \
229  (ITEM) = CONTAINER_OF((LINKEDLIST)->first, MEMBER, TYPE); \
230  \
231  NULL != (ITEM); \
232  \
233  ((ITERATOR) = (NULL == (ITERATOR)) ? \
234  (LINKEDLIST)->first : (ITERATOR)->next), \
235  ((ITEM) = (NULL == (ITERATOR)) ? \
236  NULL : CONTAINER_OF((ITERATOR), MEMBER, TYPE)))
237 
250 void
251 List_InsertAfter(Lz_LinkedList * const linkedList,
252  Lz_LinkedListElement * const listItem,
253  Lz_LinkedListElement * const itemToInsert);
254 
267 void
268 List_InsertBefore(Lz_LinkedList * const linkedList,
269  Lz_LinkedListElement * const listItem,
270  Lz_LinkedListElement * const itemToInsert);
271 
289 List_Remove(Lz_LinkedList * const linkedList,
290  Lz_LinkedListElement * const itemToRemove);
291 
306 List_PointElementAt(const Lz_LinkedList * const linkedList, const size_t index);
307 
313 void
314 List_InitLinkedList(Lz_LinkedList * const linkedList);
315 
321 void
323 
325 
326 #endif /* LAZULI_LIST_H */
struct _Lz_LinkedListElement Lz_LinkedListElement
Represents an element of a doubly linked list.
void List_InsertAfter(Lz_LinkedList *const linkedList, Lz_LinkedListElement *const listItem, Lz_LinkedListElement *const itemToInsert)
Insert an element after another in an Lz_LinkedList.
Definition: list.c:151
#define _EXTERN_C_DECL_BEGIN
Open C++ header file declarations.
Definition: common.h:37
bool List_IsEmpty(const Lz_LinkedList *const linkedList)
Test if an Lz_LinkedList is empty.
Definition: list.c:139
void List_InsertBefore(Lz_LinkedList *const linkedList, Lz_LinkedListElement *const listItem, Lz_LinkedListElement *const itemToInsert)
Insert an element before another in a Lz_LinkedList.
Definition: list.c:171
Include appropriate config file.
Lz_LinkedListElement * List_PointFirst(const Lz_LinkedList *const linkedList)
Return a pointer to the first element of an existing linked list.
Definition: list.c:127
void List_Append(Lz_LinkedList *const linkedList, Lz_LinkedListElement *const item)
Insert an Lz_LinkedListElement as the last element of an existing Lz_LinkedList.
Definition: list.c:20
Lz_LinkedListElement * List_PickFirst(Lz_LinkedList *const linkedList)
Return the first element of an existing linked list.
Definition: list.c:96
void List_InitLinkedList(Lz_LinkedList *const linkedList)
Initialize an Lz_LinkedList.
Definition: list.c:250
struct _Lz_LinkedListElement * next
A pointer to the next element in the list.
Definition: list.h:27
void List_InitLinkedListElement(Lz_LinkedListElement *const item)
Initialize an Lz_LinkedListElement.
Definition: list.c:264
Represents the main container for doubly linked elements.
Definition: list.h:36
void List_Prepend(Lz_LinkedList *const linkedList, Lz_LinkedListElement *const item)
Insert an Lz_LinkedListElement as the first element of an existing Lz_LinkedList. ...
Definition: list.c:44
Lz_LinkedListElement * List_PointElementAt(const Lz_LinkedList *const linkedList, const size_t index)
Point to an indexed element in an Lz_LinkedList, starting at index 0.
Definition: list.c:227
void List_AppendList(Lz_LinkedList *const linkedListDestination, Lz_LinkedList *const linkedListToMove)
Move the content of an Lz_LinkedList to the end of an existing Lz_LinkedList.
Definition: list.c:69
Lz_LinkedListElement * last
A pointer to the last element of the linked list.
Definition: list.h:41
Lz_LinkedListElement * first
A pointer to the first element of the linked list.
Definition: list.h:38
Basic type definitions and useful macros.
struct _Lz_LinkedListElement * prev
A pointer to the previous element in the list.
Definition: list.h:30
Represents an element of a doubly linked list.
Definition: list.h:25
#define _EXTERN_C_DECL_END
Close C++ header file declarations.
Definition: common.h:42
Lz_LinkedListElement * List_Remove(Lz_LinkedList *const linkedList, Lz_LinkedListElement *const itemToRemove)
Remove an element from an Lz_LinkedList.
Definition: list.c:195