Mon, 18 Dec 2023 14:14:47 +0100
increase version number to 3.1
remove per-file version information
from Doxygen output
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28 /**
29 * \file linked_list.h
30 * \brief Linked list implementation.
31 * \details Also provides several low-level functions for custom linked list implementations.
32 * \author Mike Becker
33 * \author Olaf Wintermann
34 * \copyright 2-Clause BSD License
35 */
37 #ifndef UCX_LINKED_LIST_H
38 #define UCX_LINKED_LIST_H
40 #include "common.h"
41 #include "list.h"
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
47 /**
48 * Set this flag to true, if you want to disable the use of SBO for
49 * linked list swap operations.
50 */
51 extern bool CX_DISABLE_LINKED_LIST_SWAP_SBO;
53 /**
54 * Allocates a linked list for storing elements with \p item_size bytes each.
55 *
56 * If \p item_size is CX_STORE_POINTERS, the created list will be created as if
57 * cxListStorePointers() was called immediately after creation.
58 *
59 * @param allocator the allocator for allocating the list nodes
60 * (if \c NULL the cxDefaultAllocator will be used)
61 * @param comparator the comparator for the elements
62 * (if \c NULL sort and find functions will not work)
63 * @param item_size the size of each element in bytes
64 * @return the created list
65 */
66 CxList *cxLinkedListCreate(
67 CxAllocator const *allocator,
68 cx_compare_func comparator,
69 size_t item_size
70 );
72 /**
73 * Allocates a linked list for storing elements with \p item_size bytes each.
74 *
75 * The list will use cxDefaultAllocator and no comparator function. If you want
76 * to call functions that need a comparator, you must either set one immediately
77 * after list creation or use cxLinkedListCreate().
78 *
79 * If \p item_size is CX_STORE_POINTERS, the created list will be created as if
80 * cxListStorePointers() was called immediately after creation.
81 *
82 * @param item_size the size of each element in bytes
83 * @return the created list
84 */
85 #define cxLinkedListCreateSimple(item_size) \
86 cxLinkedListCreate(NULL, NULL, item_size)
88 /**
89 * Finds the node at a certain index.
90 *
91 * This function can be used to start at an arbitrary position within the list.
92 * If the search index is large than the start index, \p loc_advance must denote
93 * the location of some sort of \c next pointer (i.e. a pointer to the next node).
94 * But it is also possible that the search index is smaller than the start index
95 * (e.g. in cases where traversing a list backwards is faster) in which case
96 * \p loc_advance must denote the location of some sort of \c prev pointer
97 * (i.e. a pointer to the previous node).
98 *
99 * @param start a pointer to the start node
100 * @param start_index the start index
101 * @param loc_advance the location of the pointer to advance
102 * @param index the search index
103 * @return the node found at the specified index
104 */
105 void *cx_linked_list_at(
106 void const *start,
107 size_t start_index,
108 ptrdiff_t loc_advance,
109 size_t index
110 ) __attribute__((__nonnull__));
112 /**
113 * Finds the index of an element within a linked list.
114 *
115 * @param start a pointer to the start node
116 * @param loc_advance the location of the pointer to advance
117 * @param loc_data the location of the \c data pointer within your node struct
118 * @param cmp_func a compare function to compare \p elem against the node data
119 * @param elem a pointer to the element to find
120 * @return the index of the element or a negative value if it could not be found
121 */
122 ssize_t cx_linked_list_find(
123 void const *start,
124 ptrdiff_t loc_advance,
125 ptrdiff_t loc_data,
126 cx_compare_func cmp_func,
127 void const *elem
128 ) __attribute__((__nonnull__));
130 /**
131 * Finds the first node in a linked list.
132 *
133 * The function starts with the pointer denoted by \p node and traverses the list
134 * along a prev pointer whose location within the node struct is
135 * denoted by \p loc_prev.
136 *
137 * @param node a pointer to a node in the list
138 * @param loc_prev the location of the \c prev pointer
139 * @return a pointer to the first node
140 */
141 void *cx_linked_list_first(
142 void const *node,
143 ptrdiff_t loc_prev
144 ) __attribute__((__nonnull__));
146 /**
147 * Finds the last node in a linked list.
148 *
149 * The function starts with the pointer denoted by \p node and traverses the list
150 * along a next pointer whose location within the node struct is
151 * denoted by \p loc_next.
152 *
153 * @param node a pointer to a node in the list
154 * @param loc_next the location of the \c next pointer
155 * @return a pointer to the last node
156 */
157 void *cx_linked_list_last(
158 void const *node,
159 ptrdiff_t loc_next
160 ) __attribute__((__nonnull__));
162 /**
163 * Finds the predecessor of a node in case it is not linked.
164 *
165 * \remark If \p node is not contained in the list starting with \p begin, the behavior is undefined.
166 *
167 * @param begin the node where to start the search
168 * @param loc_next the location of the \c next pointer
169 * @param node the successor of the node to find
170 * @return the node or \c NULL if \p node has no predecessor
171 */
172 void *cx_linked_list_prev(
173 void const *begin,
174 ptrdiff_t loc_next,
175 void const *node
176 ) __attribute__((__nonnull__));
178 /**
179 * Adds a new node to a linked list.
180 * The node must not be part of any list already.
181 *
182 * \remark One of the pointers \p begin or \p end may be \c NULL, but not both.
183 *
184 * @param begin a pointer to the begin node pointer (if your list has one)
185 * @param end a pointer to the end node pointer (if your list has one)
186 * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one)
187 * @param loc_next the location of a \c next pointer within your node struct (required)
188 * @param new_node a pointer to the node that shall be appended
189 */
190 void cx_linked_list_add(
191 void **begin,
192 void **end,
193 ptrdiff_t loc_prev,
194 ptrdiff_t loc_next,
195 void *new_node
196 ) __attribute__((__nonnull__(5)));
198 /**
199 * Prepends a new node to a linked list.
200 * The node must not be part of any list already.
201 *
202 * \remark One of the pointers \p begin or \p end may be \c NULL, but not both.
203 *
204 * @param begin a pointer to the begin node pointer (if your list has one)
205 * @param end a pointer to the end node pointer (if your list has one)
206 * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one)
207 * @param loc_next the location of a \c next pointer within your node struct (required)
208 * @param new_node a pointer to the node that shall be prepended
209 */
210 void cx_linked_list_prepend(
211 void **begin,
212 void **end,
213 ptrdiff_t loc_prev,
214 ptrdiff_t loc_next,
215 void *new_node
216 ) __attribute__((__nonnull__(5)));
218 /**
219 * Links two nodes.
220 *
221 * @param left the new predecessor of \p right
222 * @param right the new successor of \p left
223 * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one)
224 * @param loc_next the location of a \c next pointer within your node struct (required)
225 */
226 void cx_linked_list_link(
227 void *left,
228 void *right,
229 ptrdiff_t loc_prev,
230 ptrdiff_t loc_next
231 ) __attribute__((__nonnull__));
233 /**
234 * Unlinks two nodes.
235 *
236 * If right is not the successor of left, the behavior is undefined.
237 *
238 * @param left the predecessor of \p right
239 * @param right the successor of \p left
240 * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one)
241 * @param loc_next the location of a \c next pointer within your node struct (required)
242 */
243 void cx_linked_list_unlink(
244 void *left,
245 void *right,
246 ptrdiff_t loc_prev,
247 ptrdiff_t loc_next
248 ) __attribute__((__nonnull__));
250 /**
251 * Inserts a new node after a given node of a linked list.
252 * The new node must not be part of any list already.
253 *
254 * \note If you specify \c NULL as the \p node to insert after, this function needs either the \p begin or
255 * the \p end pointer to determine the start of the list. Then the new node will be prepended to the list.
256 *
257 * @param begin a pointer to the begin node pointer (if your list has one)
258 * @param end a pointer to the end node pointer (if your list has one)
259 * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one)
260 * @param loc_next the location of a \c next pointer within your node struct (required)
261 * @param node the node after which to insert (\c NULL if you want to prepend the node to the list)
262 * @param new_node a pointer to the node that shall be prepended
263 */
264 void cx_linked_list_insert(
265 void **begin,
266 void **end,
267 ptrdiff_t loc_prev,
268 ptrdiff_t loc_next,
269 void *node,
270 void *new_node
271 ) __attribute__((__nonnull__(6)));
273 /**
274 * Inserts a chain of nodes after a given node of a linked list.
275 * The chain must not be part of any list already.
276 *
277 * If you do not explicitly specify the end of the chain, it will be determined by traversing
278 * the \c next pointer.
279 *
280 * \note If you specify \c NULL as the \p node to insert after, this function needs either the \p begin or
281 * the \p end pointer to determine the start of the list. If only the \p end pointer is specified, you also need
282 * to provide a valid \p loc_prev location.
283 * Then the chain will be prepended to the list.
284 *
285 * @param begin a pointer to the begin node pointer (if your list has one)
286 * @param end a pointer to the end node pointer (if your list has one)
287 * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one)
288 * @param loc_next the location of a \c next pointer within your node struct (required)
289 * @param node the node after which to insert (\c NULL to prepend the chain to the list)
290 * @param insert_begin a pointer to the first node of the chain that shall be inserted
291 * @param insert_end a pointer to the last node of the chain (or NULL if the last node shall be determined)
292 */
293 void cx_linked_list_insert_chain(
294 void **begin,
295 void **end,
296 ptrdiff_t loc_prev,
297 ptrdiff_t loc_next,
298 void *node,
299 void *insert_begin,
300 void *insert_end
301 ) __attribute__((__nonnull__(6)));
303 /**
304 * Removes a node from the linked list.
305 *
306 * If the node to remove is the begin (resp. end) node of the list and if \p begin (resp. \p end)
307 * addresses are provided, the pointers are adjusted accordingly.
308 *
309 * The following combinations of arguments are valid (more arguments are optional):
310 * \li \p loc_next and \p loc_prev (ancestor node is determined by using the prev pointer, overall O(1) performance)
311 * \li \p loc_next and \p begin (ancestor node is determined by list traversal, overall O(n) performance)
312 *
313 * \remark The \c next and \c prev pointers of the removed node are not cleared by this function and may still be used
314 * to traverse to a former adjacent node in the list.
315 *
316 * @param begin a pointer to the begin node pointer (optional)
317 * @param end a pointer to the end node pointer (optional)
318 * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one)
319 * @param loc_next the location of a \c next pointer within your node struct (required)
320 * @param node the node to remove
321 */
322 void cx_linked_list_remove(
323 void **begin,
324 void **end,
325 ptrdiff_t loc_prev,
326 ptrdiff_t loc_next,
327 void *node
328 ) __attribute__((__nonnull__(5)));
331 /**
332 * Determines the size of a linked list starting with \p node.
333 * @param node the first node
334 * @param loc_next the location of the \c next pointer within the node struct
335 * @return the size of the list or zero if \p node is \c NULL
336 */
337 size_t cx_linked_list_size(
338 void const *node,
339 ptrdiff_t loc_next
340 );
342 /**
343 * Sorts a linked list based on a comparison function.
344 *
345 * This function can work with linked lists of the following structure:
346 * \code
347 * typedef struct node node;
348 * struct node {
349 * node* prev;
350 * node* next;
351 * my_payload data;
352 * }
353 * \endcode
354 *
355 * @note This is a recursive function with at most logarithmic recursion depth.
356 *
357 * @param begin a pointer to the begin node pointer (required)
358 * @param end a pointer to the end node pointer (optional)
359 * @param loc_prev the location of a \c prev pointer within your node struct (negative if not present)
360 * @param loc_next the location of a \c next pointer within your node struct (required)
361 * @param loc_data the location of the \c data pointer within your node struct
362 * @param cmp_func the compare function defining the sort order
363 */
364 void cx_linked_list_sort(
365 void **begin,
366 void **end,
367 ptrdiff_t loc_prev,
368 ptrdiff_t loc_next,
369 ptrdiff_t loc_data,
370 cx_compare_func cmp_func
371 ) __attribute__((__nonnull__(1, 6)));
374 /**
375 * Compares two lists element wise.
376 *
377 * \note Both list must have the same structure.
378 *
379 * @param begin_left the begin of the left list (\c NULL denotes an empty list)
380 * @param begin_right the begin of the right list (\c NULL denotes an empty list)
381 * @param loc_advance the location of the pointer to advance
382 * @param loc_data the location of the \c data pointer within your node struct
383 * @param cmp_func the function to compare the elements
384 * @return the first non-zero result of invoking \p cmp_func or: negative if the left list is smaller than the
385 * right list, positive if the left list is larger than the right list, zero if both lists are equal.
386 */
387 int cx_linked_list_compare(
388 void const *begin_left,
389 void const *begin_right,
390 ptrdiff_t loc_advance,
391 ptrdiff_t loc_data,
392 cx_compare_func cmp_func
393 ) __attribute__((__nonnull__(5)));
395 /**
396 * Reverses the order of the nodes in a linked list.
397 *
398 * @param begin a pointer to the begin node pointer (required)
399 * @param end a pointer to the end node pointer (optional)
400 * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one)
401 * @param loc_next the location of a \c next pointer within your node struct (required)
402 */
403 void cx_linked_list_reverse(
404 void **begin,
405 void **end,
406 ptrdiff_t loc_prev,
407 ptrdiff_t loc_next
408 ) __attribute__((__nonnull__(1)));
410 #ifdef __cplusplus
411 } // extern "C"
412 #endif
414 #endif // UCX_LINKED_LIST_H