libcstl
Loading...
Searching...
No Matches
slist.h
Go to the documentation of this file.
1/*!
2 * @file
3 */
4
5#ifndef CSTL_SLIST_H
6#define CSTL_SLIST_H
7
8/*!
9 * @defgroup slist Singly-linked list
10 * @ingroup lists
11 * @brief A linked list allowing traversal in the forward direction
12 */
13/*!
14 * @addtogroup slist
15 * @{
16 */
17
18#include "cstl/common.h"
19
20/*!
21 * @brief Node to anchor an element within a list
22 *
23 * Users of the slist object declare this object within another
24 * object as follows:
25 * @code{.c}
26 * struct object {
27 * ...
28 * struct cstl_slist_node node;
29 * ...
30 * };
31 * @endcode
32 *
33 * When calling cstl_slist_init(), the caller passes the offset of @p node
34 * within their object as the @p off parameter of that function, e.g.
35 * @code{.c}
36 * offsetof(struct object, node)
37 * @endcode
38 */
40{
41 /*! @privatesection */
42 struct cstl_slist_node * n;
43};
44
45/*!
46 * @brief List object
47 *
48 * Callers declare or allocate an object of this type to instantiate
49 * a list. Users are encouraged to declare (and initialize) this
50 * object with the DECLARE_CSTL_SLIST() macro. Any other declaration or
51 * allocation must be initialized via cstl_slist_init().
52 */
54{
55 /*! @privatesection */
56 struct cstl_slist_node h;
57 struct cstl_slist_node * t;
58
59 size_t count;
60 size_t off;
61};
62
63/*!
64 * @brief Constant initialization of a slist object
65 *
66 * @param NAME The name of the variable being initialized
67 * @param TYPE The type of object that the list will hold
68 * @param MEMB The name of the @p cstl_slist_node member within @p TYPE.
69 *
70 * @see cstl_slist_node for a description of the relationship between
71 * @p TYPE and @p MEMB
72 */
73#define CSTL_SLIST_INITIALIZER(NAME, TYPE, MEMB) \
74 { \
75 .h = { \
76 .n = NULL, \
77 }, \
78 .t = &NAME.h, \
79 .count = 0, \
80 .off = offsetof(TYPE, MEMB), \
81 }
82/*!
83 * @brief (Statically) declare and initialize a list
84 *
85 * @param NAME The name of the variable being declared
86 * @param TYPE The type of object that the list will hold
87 * @param MEMB The name of the @p cstl_slist_node member within @p TYPE.
88 *
89 * @see cstl_slist_node for a description of the relationship between
90 * @p TYPE and @p MEMB
91 */
92#define DECLARE_CSTL_SLIST(NAME, TYPE, MEMB) \
93 struct cstl_slist NAME = CSTL_SLIST_INITIALIZER(NAME, TYPE, MEMB)
94
95/*!
96 * @brief Initialize a slist object
97 *
98 * @param[in,out] sl A pointer to the object to be initialized
99 * @param[in] off The offset of the @p cstl_slist_node object within the
100 * object(s) that will be stored in the list
101 */
102static inline void cstl_slist_init(
103 struct cstl_slist * const sl, const size_t off)
104{
105 sl->h.n = NULL;
106 sl->t = &sl->h;
107 sl->count = 0;
108 sl->off = off;
109}
110
111/*!
112 * @brief Get the number of objects in the list
113 *
114 * @param[in] sl A pointer to the list
115 *
116 * @return The number of objects in the list
117 */
118static inline size_t cstl_slist_size(const struct cstl_slist * sl)
119{
120 return sl->count;
121}
122
123/*!
124 * @brief Insert a new object into the list
125 *
126 * @param[in] sl A pointer to the list into which to insert the object
127 * @param[in] before A pointer to an object already in the list
128 * @param[in] obj A pointer to an object to insert into the list
129 *
130 * The new object will be inserted after the object pointed to by @p before
131 */
133 struct cstl_slist * sl, void * before, void * obj);
134/*!
135 * @brief Remove an object from the list
136 *
137 * @param[in] sl The list in which the object currently resides
138 * @param[in] bef A pointer to the object in front of the object to be removed
139 */
140void * cstl_slist_erase_after(struct cstl_slist * sl, void * bef);
141
142/*!
143 * @brief Insert a new object at the front of the list
144 *
145 * @param[in] sl A pointer to a list
146 * @param[in] obj A pointer to the object to be inserted
147 */
148void cstl_slist_push_front(struct cstl_slist * sl, void * obj);
149/*!
150 * @brief Insert a new object at the back of the list
151 *
152 * @param[in] sl A pointer to a list
153 * @param[in] obj A pointer to the object to be inserted
154 */
155void cstl_slist_push_back(struct cstl_slist * sl, void * obj);
156
157/*!
158 * @brief Remove the first item in the list and return it
159 *
160 * @param[in] sl A pointer to a list
161 *
162 * @return A pointer to the removed object
163 * @retval NULL The list was empty and no object was removed
164 */
165void * cstl_slist_pop_front(struct cstl_slist * sl);
166
167/*!
168 * @brief Get a pointer to the first object in the list
169 *
170 * @param[in] sl A pointer to a list
171 *
172 * @return A pointer to the first object in the list
173 * @retval NULL The list is empty
174 */
175void * cstl_slist_front(const struct cstl_slist * sl);
176/*!
177 * @brief Get a pointer to the last object in the list
178 *
179 * @param[in] sl A pointer to a list
180 *
181 * @return A pointer to the last object in the list
182 * @retval NULL The list is empty
183 */
184void * cstl_slist_back(const struct cstl_slist * sl);
185
186/*!
187 * @brief Reverse the order of items in the list
188 *
189 * @param[in] sl A pointer to the list
190 */
191void cstl_slist_reverse(struct cstl_slist * sl);
192/*!
193 * @brief Sort the items in a list
194 *
195 * @param[in] sl A pointer to the list
196 * @param[in] cmp A pointer to a function to compare objects
197 * @param[in] priv A pointer to be passed to each invocation
198 * of the comparison function
199 *
200 * The items are sorted from least to greatest, according to the provided
201 * comparison function.
202 */
203void cstl_slist_sort(struct cstl_slist * sl,
204 cstl_compare_func_t * cmp, void * priv);
205/*!
206 * @brief Append one list to the end of another
207 *
208 * @param[in] list The list to which to append more objects
209 * @param[in] more The list of objects to append
210 *
211 * Upon return @p list will have all of the objects from @p more
212 * appended to its end.
213 */
214void cstl_slist_concat(struct cstl_slist * list, struct cstl_slist * more);
215
216/*!
217 * @brief Call a user-supplied function for each object in a list
218 *
219 * @param[in] sl The list containing the objects to visit
220 * @param[in] visit A pointer to the function to call for each object
221 * @param[in] priv A pointer to be passed to each invocation of the function
222 *
223 * The user-supplied function must return 0 in order to continue visiting
224 * objects in the list. The visiting of objects will stop at the first
225 * occurrence of a non-zero return from the visit function.
226 *
227 * @return The value returned from the user-supplied function from the
228 * last object visited or 0
229 */
230int cstl_slist_foreach(struct cstl_slist * sl,
231 cstl_visit_func_t * visit, void * priv);
232/*!
233 * @brief Remove objects from and reinitialize a list
234 *
235 * @param[in] sl The list to be cleared
236 * @param[in] clr The function to be called for each object in the list
237 *
238 * All objects are removed from the list and the @p clr function is
239 * called for each object that was in the list. The order in which
240 * the objects are removed and @p clr is called is not specified, but
241 * the callee may take ownership of an object at the time that @p clr
242 * is called for that object and not before.
243 *
244 * Upon return from this function, the list contains no objects, and it
245 * is as it was immediately after being initialized. No further operations
246 * on the list are necessary to make it ready to go out of scope or be
247 * destroyed.
248 */
249void cstl_slist_clear(struct cstl_slist * sl, cstl_xtor_func_t * clr);
250
251/*!
252 * @brief Swap the list objects at the two given locations
253 *
254 * @param[in,out] a A pointer to a list
255 * @param[in,out] b A pointer to a(nother) list
256 *
257 * The lists at the given locations will be swapped such that upon return,
258 * @p a will contain the list previously pointed to by @p b and vice versa.
259 */
260void cstl_slist_swap(struct cstl_slist * const a, struct cstl_slist * const b);
261
262/*!
263 * @}
264 */
265
266#endif
void cstl_xtor_func_t(void *obj, void *priv)
Type for functions called to construct, clear, or destroy an object.
Definition common.h:97
int cstl_visit_func_t(void *obj, void *priv)
Type for visit callbacks from objects supporting foreach.
Definition common.h:65
int cstl_compare_func_t(const void *obj1, const void *obj2, void *priv)
Function type for comparing (in)equality of two objects.
Definition common.h:49
void * cstl_slist_back(const struct cstl_slist *sl)
Get a pointer to the last object in the list.
Definition slist.c:94
void cstl_slist_insert_after(struct cstl_slist *sl, void *before, void *obj)
Insert a new object into the list.
Definition slist.c:58
void * cstl_slist_pop_front(struct cstl_slist *sl)
Remove the first item in the list and return it.
Definition slist.c:81
void cstl_slist_clear(struct cstl_slist *sl, cstl_xtor_func_t *clr)
Remove objects from and reinitialize a list.
Definition slist.c:216
void cstl_slist_reverse(struct cstl_slist *sl)
Reverse the order of items in the list.
Definition slist.c:102
static size_t cstl_slist_size(const struct cstl_slist *sl)
Get the number of objects in the list.
Definition slist.h:118
void cstl_slist_sort(struct cstl_slist *sl, cstl_compare_func_t *cmp, void *priv)
Sort the items in a list.
Definition slist.c:140
void cstl_slist_push_front(struct cstl_slist *sl, void *obj)
Insert a new object at the front of the list.
Definition slist.c:71
void cstl_slist_swap(struct cstl_slist *const a, struct cstl_slist *const b)
Swap the list objects at the two given locations.
Definition slist.c:231
int cstl_slist_foreach(struct cstl_slist *sl, cstl_visit_func_t *visit, void *priv)
Call a user-supplied function for each object in a list.
Definition slist.c:201
void * cstl_slist_front(const struct cstl_slist *sl)
Get a pointer to the first object in the list.
Definition slist.c:86
static void cstl_slist_init(struct cstl_slist *const sl, const size_t off)
Initialize a slist object.
Definition slist.h:102
void * cstl_slist_erase_after(struct cstl_slist *sl, void *bef)
Remove an object from the list.
Definition slist.c:65
void cstl_slist_concat(struct cstl_slist *list, struct cstl_slist *more)
Append one list to the end of another.
Definition slist.c:125
void cstl_slist_push_back(struct cstl_slist *sl, void *obj)
Insert a new object at the back of the list.
Definition slist.c:76
Node to anchor an element within a list.
Definition slist.h:40
List object.
Definition slist.h:54