Lely core libraries  2.2.5
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 
39 struct slnode {
41  struct slnode *next;
42 };
43 
45 #define SLNODE_INIT \
46  { \
47  NULL \
48  }
49 
51 struct sllist {
53  struct slnode *first;
55  struct slnode **plast;
56 };
57 
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61 
63 LELY_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 
88 LELY_UTIL_SLLIST_INLINE void sllist_init(struct sllist *list);
89 
94 LELY_UTIL_SLLIST_INLINE int sllist_empty(const struct sllist *list);
95 
100 LELY_UTIL_SLLIST_INLINE size_t sllist_size(const struct sllist *list);
101 
108 LELY_UTIL_SLLIST_INLINE void sllist_push_front(
109  struct sllist *list, struct slnode *node);
110 
116 LELY_UTIL_SLLIST_INLINE void sllist_push_back(
117  struct sllist *list, struct slnode *node);
118 
125 LELY_UTIL_SLLIST_INLINE struct slnode *sllist_pop_front(struct sllist *list);
126 
132 struct slnode *sllist_pop_back(struct sllist *list);
133 
140 struct slnode *sllist_remove(struct sllist *list, struct slnode *node);
141 
148 LELY_UTIL_SLLIST_INLINE struct sllist *sllist_append(
149  struct sllist *dst, struct sllist *src);
150 
157 LELY_UTIL_SLLIST_INLINE struct slnode *sllist_first(const struct sllist *list);
158 
165 struct slnode *sllist_last(const struct sllist *list);
166 
175 #define sllist_foreach(list, node) slnode_foreach (sllist_first(list), node)
176 
177 inline void
178 slnode_init(struct slnode *node)
179 {
180  node->next = NULL;
181 }
182 
183 inline void
184 sllist_init(struct sllist *list)
185 {
186  *(list->plast = &list->first) = NULL;
187 }
188 
189 inline int
190 sllist_empty(const struct sllist *list)
191 {
192  return !list->first;
193 }
194 
195 inline size_t
196 sllist_size(const struct sllist *list)
197 {
198  size_t size = 0;
199  sllist_foreach (list, node)
200  size++;
201  return size;
202 }
203 
204 inline void
205 sllist_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 
212 inline void
213 sllist_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 
220 inline 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 
232 inline struct sllist *
233 sllist_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 
243 inline struct slnode *
244 sllist_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_
features.h
sllist_remove
struct slnode * sllist_remove(struct sllist *list, struct slnode *node)
Removes a node from a singly-linked list.
Definition: sllist.c:46
sllist_first
struct slnode * sllist_first(const struct sllist *list)
Returns a pointer to the first node in a singly-linked list.
Definition: sllist.h:244
slnode::next
struct slnode * next
A pointer to the next node in the list.
Definition: sllist.h:41
sllist_push_front
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
sllist_append
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
sllist_pop_back
struct slnode * sllist_pop_back(struct sllist *list)
Pops a node from the back of a singly-linked list.
Definition: sllist.c:32
sllist_empty
int sllist_empty(const struct sllist *list)
Returns 1 if the singly-linked list is empty, and 0 if not.
Definition: sllist.h:190
sllist::plast
struct slnode ** plast
A pointer to the next field of the last node in the list.
Definition: sllist.h:55
sllist::first
struct slnode * first
A pointer to the first node in the list.
Definition: sllist.h:53
sllist_init
void sllist_init(struct sllist *list)
Initializes a singly-linked list.
Definition: sllist.h:184
sllist_pop_front
struct slnode * sllist_pop_front(struct sllist *list)
Pops a node from the front of a singly-linked list.
Definition: sllist.h:221
slnode_init
void slnode_init(struct slnode *node)
Initializes a node in a singly-linked list.
Definition: sllist.h:178
sllist
A singly-linked list.
Definition: sllist.h:51
sllist_last
struct slnode * sllist_last(const struct sllist *list)
Returns a pointer to the last node in a singly-linked list.
Definition: sllist.c:64
sllist_foreach
#define sllist_foreach(list, node)
Iterates in order over each node in a singly-linked list.
Definition: sllist.h:175
slnode
A node in a singly-linked list.
Definition: sllist.h:39
stddef.h
sllist_push_back
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
sllist_size
size_t sllist_size(const struct sllist *list)
Returns the size (in number of nodes) of a singly-linked list.
Definition: sllist.h:196