Lazuli
Loading...
Searching...
No Matches
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
32
43
47#define LINKED_LIST_INIT { NULL, NULL }
48
52#define LINKED_LIST_ELEMENT_INIT { NULL, NULL }
53
61void
62List_Append(Lz_LinkedList * const linkedList,
63 Lz_LinkedListElement * const item);
64
72void
73List_Prepend(Lz_LinkedList * const linkedList,
74 Lz_LinkedListElement * const item);
75
84void
85List_AppendList(Lz_LinkedList * const linkedListDestination,
86 Lz_LinkedList * const linkedListToMove);
87
99List_PickFirst(Lz_LinkedList * const linkedList);
100
112List_PointFirst(const Lz_LinkedList * const linkedList);
113
127bool
128List_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
250void
251List_InsertAfter(Lz_LinkedList * const linkedList,
252 Lz_LinkedListElement * const listItem,
253 Lz_LinkedListElement * const itemToInsert);
254
267void
268List_InsertBefore(Lz_LinkedList * const linkedList,
269 Lz_LinkedListElement * const listItem,
270 Lz_LinkedListElement * const itemToInsert);
271
289List_Remove(Lz_LinkedList * const linkedList,
290 Lz_LinkedListElement * const itemToRemove);
291
306List_PointElementAt(const Lz_LinkedList * const linkedList, const size_t index);
307
313void
314List_InitLinkedList(Lz_LinkedList * const linkedList);
315
321void
323
325
326#endif /* LAZULI_LIST_H */
Basic type definitions and useful macros.
#define _EXTERN_C_DECL_BEGIN
Open C++ header file declarations.
Definition common.h:39
#define _EXTERN_C_DECL_END
Close C++ header file declarations.
Definition common.h:44
Include appropriate config file.
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:22
struct _Lz_LinkedListElement Lz_LinkedListElement
Represents an element of a doubly linked list.
bool List_IsEmpty(const Lz_LinkedList *const linkedList)
Test if an Lz_LinkedList is empty.
Definition list.c:141
Lz_LinkedListElement * List_PickFirst(Lz_LinkedList *const linkedList)
Return the first element of an existing linked list.
Definition list.c:98
Lz_LinkedListElement * List_Remove(Lz_LinkedList *const linkedList, Lz_LinkedListElement *const itemToRemove)
Remove an element from an Lz_LinkedList.
Definition list.c:197
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:71
Lz_LinkedListElement * List_PointFirst(const Lz_LinkedList *const linkedList)
Return a pointer to the first element of an existing linked list.
Definition list.c:129
void List_InitLinkedList(Lz_LinkedList *const linkedList)
Initialize an Lz_LinkedList.
Definition list.c:252
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:46
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:153
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:229
void List_InitLinkedListElement(Lz_LinkedListElement *const item)
Initialize an Lz_LinkedListElement.
Definition list.c:266
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:173
Represents the main container for doubly linked elements.
Definition list.h:36
Lz_LinkedListElement * first
A pointer to the first element of the linked list.
Definition list.h:38
Lz_LinkedListElement * last
A pointer to the last element of the linked list.
Definition list.h:41
Represents an element of a doubly linked list.
Definition list.h:25
struct _Lz_LinkedListElement * next
A pointer to the next element in the list.
Definition list.h:27
struct _Lz_LinkedListElement * prev
A pointer to the previous element in the list.
Definition list.h:30