Lely core libraries 1.9.2
sllist.h
Go to the documentation of this file.
1
23#ifndef LELY_UTIL_SLLIST_H_
24#define LELY_UTIL_SLLIST_H_
25
26#include <lely/features.h>
27
28#include <stddef.h>
29
30#ifndef LELY_UTIL_SLLIST_INLINE
31#define LELY_UTIL_SLLIST_INLINE static inline
32#endif
33
39struct slnode {
41 struct slnode *next;
42};
43
45#define SLNODE_INIT \
46 { \
47 NULL \
48 }
49
51struct sllist {
53 struct slnode *first;
55 struct slnode **plast;
56};
57
58#ifdef __cplusplus
59extern "C" {
60#endif
61
63LELY_UTIL_SLLIST_INLINE void slnode_init(struct slnode *node);
64
73#ifdef __COUNTER__
74#define slnode_foreach(first, node) slnode_foreach_(__COUNTER__, first, node)
75#else
76#define slnode_foreach(first, node) slnode_foreach_(__LINE__, first, node)
77#endif
78#define slnode_foreach_(n, first, node) slnode_foreach__(n, first, node)
79// clang-format off
80#define slnode_foreach__(n, first, node) \
81 for (struct slnode *node = (first), \
82 *_slnode_next_##n = (node) ? (node)->next : NULL; \
83 (node); (node) = _slnode_next_##n, \
84 _slnode_next_##n = (node) ? (node)->next : NULL)
85// clang-format on
86
88LELY_UTIL_SLLIST_INLINE void sllist_init(struct sllist *list);
89
94LELY_UTIL_SLLIST_INLINE int sllist_empty(const struct sllist *list);
95
100LELY_UTIL_SLLIST_INLINE size_t sllist_size(const struct sllist *list);
101
108LELY_UTIL_SLLIST_INLINE void sllist_push_front(
109 struct sllist *list, struct slnode *node);
110
116LELY_UTIL_SLLIST_INLINE void sllist_push_back(
117 struct sllist *list, struct slnode *node);
118
125LELY_UTIL_SLLIST_INLINE struct slnode *sllist_pop_front(struct sllist *list);
126
132struct slnode *sllist_pop_back(struct sllist *list);
133
140struct slnode *sllist_remove(struct sllist *list, struct slnode *node);
141
148LELY_UTIL_SLLIST_INLINE struct sllist *sllist_append(
149 struct sllist *dst, struct sllist *src);
150
157LELY_UTIL_SLLIST_INLINE struct slnode *sllist_first(const struct sllist *list);
158
165struct slnode *sllist_last(const struct sllist *list);
166
175#define sllist_foreach(list, node) slnode_foreach (sllist_first(list), node)
176
177inline void
178slnode_init(struct slnode *node)
179{
180 node->next = NULL;
181}
182
183inline void
184sllist_init(struct sllist *list)
185{
186 *(list->plast = &list->first) = NULL;
187}
188
189inline int
190sllist_empty(const struct sllist *list)
191{
192 return !list->first;
193}
194
195inline size_t
196sllist_size(const struct sllist *list)
197{
198 size_t size = 0;
199 sllist_foreach (list, node)
200 size++;
201 return size;
202}
203
204inline void
205sllist_push_front(struct sllist *list, struct slnode *node)
206{
207 if (!(node->next = list->first))
208 list->plast = &node->next;
209 list->first = node;
210}
211
212inline void
213sllist_push_back(struct sllist *list, struct slnode *node)
214{
215 *list->plast = node;
216 list->plast = &(*list->plast)->next;
217 *list->plast = NULL;
218}
219
220inline struct slnode *
222{
223 struct slnode *node = list->first;
224 if (node) {
225 if (!(list->first = node->next))
226 list->plast = &list->first;
227 node->next = NULL;
228 }
229 return node;
230}
231
232inline struct sllist *
233sllist_append(struct sllist *dst, struct sllist *src)
234{
235 if (src->first) {
236 *dst->plast = src->first;
237 dst->plast = src->plast;
238 sllist_init(src);
239 }
240 return dst;
241}
242
243inline struct slnode *
244sllist_first(const struct sllist *list)
245{
246 return list->first;
247}
248
249#ifdef __cplusplus
250}
251#endif
252
253#endif // !LELY_UTIL_SLLIST_H_
This header file is part of the Lely libraries; it contains the compiler feature definitions.
void sllist_init(struct sllist *list)
Initializes a singly-linked list.
Definition: sllist.h:184
struct sllist * sllist_append(struct sllist *dst, struct sllist *src)
Appends the singly-linked list at src to the one at dst.
Definition: sllist.h:233
size_t sllist_size(const struct sllist *list)
Returns the size (in number of nodes) of a singly-linked list.
Definition: sllist.h:196
void sllist_push_front(struct sllist *list, struct slnode *node)
Pushes a node to the front of a singly-linked list.
Definition: sllist.h:205
#define sllist_foreach(list, node)
Iterates in order over each node in a singly-linked list.
Definition: sllist.h:175
struct slnode * sllist_remove(struct sllist *list, struct slnode *node)
Removes a node from a singly-linked list.
Definition: sllist.c:46
void sllist_push_back(struct sllist *list, struct slnode *node)
Pushes a node to the back of a singly-linked list.
Definition: sllist.h:213
void slnode_init(struct slnode *node)
Initializes a node in a singly-linked list.
Definition: sllist.h:178
struct slnode * sllist_last(const struct sllist *list)
Returns a pointer to the last node in a singly-linked list.
Definition: sllist.c:64
struct slnode * sllist_pop_back(struct sllist *list)
Pops a node from the back of a singly-linked list.
Definition: sllist.c:32
int sllist_empty(const struct sllist *list)
Returns 1 if the singly-linked list is empty, and 0 if not.
Definition: sllist.h:190
struct slnode * sllist_pop_front(struct sllist *list)
Pops a node from the front of a singly-linked list.
Definition: sllist.h:221
struct slnode * sllist_first(const struct sllist *list)
Returns a pointer to the first node in a singly-linked list.
Definition: sllist.h:244
This header file is part of the C11 and POSIX compatibility library; it includes <stddef....
A singly-linked list.
Definition: sllist.h:51
struct slnode * first
A pointer to the first node in the list.
Definition: sllist.h:53
struct slnode ** plast
A pointer to the next field of the last node in the list.
Definition: sllist.h:55
A node in a singly-linked list.
Definition: sllist.h:39
struct slnode * next
A pointer to the next node in the list.
Definition: sllist.h:41