src/cx/linked_list.h

changeset 1100
2ca6fa71e55e
parent 993
b642eca4b956
child 1111
78eeeb950883
equal deleted inserted replaced
1099:ce5eb95ffba2 1100:2ca6fa71e55e
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 /** 28 /**
29 * \file linked_list.h 29 * @file linked_list.h
30 * \brief Linked list implementation. 30 * @brief Linked list implementation.
31 * \details Also provides several low-level functions for custom linked list implementations. 31 * @author Mike Becker
32 * \author Mike Becker 32 * @author Olaf Wintermann
33 * \author Olaf Wintermann 33 * @copyright 2-Clause BSD License
34 * \copyright 2-Clause BSD License
35 */ 34 */
36 35
37 #ifndef UCX_LINKED_LIST_H 36 #ifndef UCX_LINKED_LIST_H
38 #define UCX_LINKED_LIST_H 37 #define UCX_LINKED_LIST_H
39 38
44 extern "C" { 43 extern "C" {
45 #endif 44 #endif
46 45
47 /** 46 /**
48 * The maximum item size that uses SBO swap instead of relinking. 47 * The maximum item size that uses SBO swap instead of relinking.
48 *
49 */ 49 */
50 extern const unsigned cx_linked_list_swap_sbo_size; 50 extern const unsigned cx_linked_list_swap_sbo_size;
51 51
52 /** 52 /**
53 * Allocates a linked list for storing elements with \p elem_size bytes each. 53 * Allocates a linked list for storing elements with @p elem_size bytes each.
54 * 54 *
55 * If \p elem_size is CX_STORE_POINTERS, the created list will be created as if 55 * If @p elem_size is CX_STORE_POINTERS, the created list will be created as if
56 * cxListStorePointers() was called immediately after creation and the compare 56 * cxListStorePointers() was called immediately after creation and the compare
57 * function will be automatically set to cx_cmp_ptr(), if none is given. 57 * function will be automatically set to cx_cmp_ptr(), if none is given.
58 * 58 *
59 * @param allocator the allocator for allocating the list nodes 59 * @param allocator the allocator for allocating the list nodes
60 * (if \c NULL, a default stdlib allocator will be used) 60 * (if @c NULL, a default stdlib allocator will be used)
61 * @param comparator the comparator for the elements 61 * @param comparator the comparator for the elements
62 * (if \c NULL, and the list is not storing pointers, sort and find 62 * (if @c NULL, and the list is not storing pointers, sort and find
63 * functions will not work) 63 * functions will not work)
64 * @param elem_size the size of each element in bytes 64 * @param elem_size the size of each element in bytes
65 * @return the created list 65 * @return the created list
66 */ 66 */
67 cx_attr_nodiscard 67 cx_attr_nodiscard
72 cx_compare_func comparator, 72 cx_compare_func comparator,
73 size_t elem_size 73 size_t elem_size
74 ); 74 );
75 75
76 /** 76 /**
77 * Allocates a linked list for storing elements with \p elem_size bytes each. 77 * Allocates a linked list for storing elements with @p elem_size bytes each.
78 * 78 *
79 * The list will use cxDefaultAllocator and no comparator function. If you want 79 * The list will use cxDefaultAllocator and no comparator function. If you want
80 * to call functions that need a comparator, you must either set one immediately 80 * to call functions that need a comparator, you must either set one immediately
81 * after list creation or use cxLinkedListCreate(). 81 * after list creation or use cxLinkedListCreate().
82 * 82 *
83 * If \p elem_size is CX_STORE_POINTERS, the created list will be created as if 83 * If @p elem_size is CX_STORE_POINTERS, the created list will be created as if
84 * cxListStorePointers() was called immediately after creation and the compare 84 * cxListStorePointers() was called immediately after creation and the compare
85 * function will be automatically set to cx_cmp_ptr(). 85 * function will be automatically set to cx_cmp_ptr().
86 * 86 *
87 * @param elem_size the size of each element in bytes 87 * @param elem_size (@c size_t) the size of each element in bytes
88 * @return the created list 88 * @return (@c CxList*) the created list
89 */ 89 */
90 #define cxLinkedListCreateSimple(elem_size) \ 90 #define cxLinkedListCreateSimple(elem_size) \
91 cxLinkedListCreate(NULL, NULL, elem_size) 91 cxLinkedListCreate(NULL, NULL, elem_size)
92 92
93 /** 93 /**
94 * Finds the node at a certain index. 94 * Finds the node at a certain index.
95 * 95 *
96 * This function can be used to start at an arbitrary position within the list. 96 * This function can be used to start at an arbitrary position within the list.
97 * If the search index is large than the start index, \p loc_advance must denote 97 * If the search index is large than the start index, @p loc_advance must denote
98 * the location of some sort of \c next pointer (i.e. a pointer to the next node). 98 * the location of some sort of @c next pointer (i.e. a pointer to the next node).
99 * But it is also possible that the search index is smaller than the start index 99 * But it is also possible that the search index is smaller than the start index
100 * (e.g. in cases where traversing a list backwards is faster) in which case 100 * (e.g. in cases where traversing a list backwards is faster) in which case
101 * \p loc_advance must denote the location of some sort of \c prev pointer 101 * @p loc_advance must denote the location of some sort of @c prev pointer
102 * (i.e. a pointer to the previous node). 102 * (i.e. a pointer to the previous node).
103 * 103 *
104 * @param start a pointer to the start node 104 * @param start a pointer to the start node
105 * @param start_index the start index 105 * @param start_index the start index
106 * @param loc_advance the location of the pointer to advance 106 * @param loc_advance the location of the pointer to advance
119 /** 119 /**
120 * Finds the index of an element within a linked list. 120 * Finds the index of an element within a linked list.
121 * 121 *
122 * @param start a pointer to the start node 122 * @param start a pointer to the start node
123 * @param loc_advance the location of the pointer to advance 123 * @param loc_advance the location of the pointer to advance
124 * @param loc_data the location of the \c data pointer within your node struct 124 * @param loc_data the location of the @c data pointer within your node struct
125 * @param cmp_func a compare function to compare \p elem against the node data 125 * @param cmp_func a compare function to compare @p elem against the node data
126 * @param elem a pointer to the element to find 126 * @param elem a pointer to the element to find
127 * @return the index of the element or a negative value if it could not be found 127 * @return the index of the element or a negative value if it could not be found
128 */ 128 */
129 cx_attr_nonnull 129 cx_attr_nonnull
130 ssize_t cx_linked_list_find( 130 ssize_t cx_linked_list_find(
136 ); 136 );
137 137
138 /** 138 /**
139 * Finds the node containing an element within a linked list. 139 * Finds the node containing an element within a linked list.
140 * 140 *
141 * @param result a pointer to the memory where the node pointer (or \c NULL if the element 141 * @param result a pointer to the memory where the node pointer (or @c NULL if the element
142 * could not be found) shall be stored to 142 * could not be found) shall be stored to
143 * @param start a pointer to the start node 143 * @param start a pointer to the start node
144 * @param loc_advance the location of the pointer to advance 144 * @param loc_advance the location of the pointer to advance
145 * @param loc_data the location of the \c data pointer within your node struct 145 * @param loc_data the location of the @c data pointer within your node struct
146 * @param cmp_func a compare function to compare \p elem against the node data 146 * @param cmp_func a compare function to compare @p elem against the node data
147 * @param elem a pointer to the element to find 147 * @param elem a pointer to the element to find
148 * @return the index of the element or a negative value if it could not be found 148 * @return the index of the element or a negative value if it could not be found
149 */ 149 */
150 cx_attr_nonnull 150 cx_attr_nonnull
151 ssize_t cx_linked_list_find_node( 151 ssize_t cx_linked_list_find_node(
158 ); 158 );
159 159
160 /** 160 /**
161 * Finds the first node in a linked list. 161 * Finds the first node in a linked list.
162 * 162 *
163 * The function starts with the pointer denoted by \p node and traverses the list 163 * The function starts with the pointer denoted by @p node and traverses the list
164 * along a prev pointer whose location within the node struct is 164 * along a prev pointer whose location within the node struct is
165 * denoted by \p loc_prev. 165 * denoted by @p loc_prev.
166 * 166 *
167 * @param node a pointer to a node in the list 167 * @param node a pointer to a node in the list
168 * @param loc_prev the location of the \c prev pointer 168 * @param loc_prev the location of the @c prev pointer
169 * @return a pointer to the first node 169 * @return a pointer to the first node
170 */ 170 */
171 cx_attr_nonnull 171 cx_attr_nonnull
172 cx_attr_returns_nonnull 172 cx_attr_returns_nonnull
173 void *cx_linked_list_first( 173 void *cx_linked_list_first(
176 ); 176 );
177 177
178 /** 178 /**
179 * Finds the last node in a linked list. 179 * Finds the last node in a linked list.
180 * 180 *
181 * The function starts with the pointer denoted by \p node and traverses the list 181 * The function starts with the pointer denoted by @p node and traverses the list
182 * along a next pointer whose location within the node struct is 182 * along a next pointer whose location within the node struct is
183 * denoted by \p loc_next. 183 * denoted by @p loc_next.
184 * 184 *
185 * @param node a pointer to a node in the list 185 * @param node a pointer to a node in the list
186 * @param loc_next the location of the \c next pointer 186 * @param loc_next the location of the @c next pointer
187 * @return a pointer to the last node 187 * @return a pointer to the last node
188 */ 188 */
189 cx_attr_nonnull 189 cx_attr_nonnull
190 cx_attr_returns_nonnull 190 cx_attr_returns_nonnull
191 void *cx_linked_list_last( 191 void *cx_linked_list_last(
194 ); 194 );
195 195
196 /** 196 /**
197 * Finds the predecessor of a node in case it is not linked. 197 * Finds the predecessor of a node in case it is not linked.
198 * 198 *
199 * \remark If \p node is not contained in the list starting with \p begin, the behavior is undefined. 199 * @remark If @p node is not contained in the list starting with @p begin, the behavior is undefined.
200 * 200 *
201 * @param begin the node where to start the search 201 * @param begin the node where to start the search
202 * @param loc_next the location of the \c next pointer 202 * @param loc_next the location of the @c next pointer
203 * @param node the successor of the node to find 203 * @param node the successor of the node to find
204 * @return the node or \c NULL if \p node has no predecessor 204 * @return the node or @c NULL if @p node has no predecessor
205 */ 205 */
206 cx_attr_nonnull 206 cx_attr_nonnull
207 void *cx_linked_list_prev( 207 void *cx_linked_list_prev(
208 const void *begin, 208 const void *begin,
209 ptrdiff_t loc_next, 209 ptrdiff_t loc_next,
212 212
213 /** 213 /**
214 * Adds a new node to a linked list. 214 * Adds a new node to a linked list.
215 * The node must not be part of any list already. 215 * The node must not be part of any list already.
216 * 216 *
217 * \remark One of the pointers \p begin or \p end may be \c NULL, but not both. 217 * @remark One of the pointers @p begin or @p end may be @c NULL, but not both.
218 * 218 *
219 * @param begin a pointer to the begin node pointer (if your list has one) 219 * @param begin a pointer to the beginning node pointer (if your list has one)
220 * @param end a pointer to the end node pointer (if your list has one) 220 * @param end a pointer to the end node pointer (if your list has one)
221 * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one) 221 * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one)
222 * @param loc_next the location of a \c next pointer within your node struct (required) 222 * @param loc_next the location of a @c next pointer within your node struct (required)
223 * @param new_node a pointer to the node that shall be appended 223 * @param new_node a pointer to the node that shall be appended
224 */ 224 */
225 cx_attr_nonnull_arg(5) 225 cx_attr_nonnull_arg(5)
226 void cx_linked_list_add( 226 void cx_linked_list_add(
227 void **begin, 227 void **begin,
233 233
234 /** 234 /**
235 * Prepends a new node to a linked list. 235 * Prepends a new node to a linked list.
236 * The node must not be part of any list already. 236 * The node must not be part of any list already.
237 * 237 *
238 * \remark One of the pointers \p begin or \p end may be \c NULL, but not both. 238 * @remark One of the pointers @p begin or @p end may be @c NULL, but not both.
239 * 239 *
240 * @param begin a pointer to the begin node pointer (if your list has one) 240 * @param begin a pointer to the beginning node pointer (if your list has one)
241 * @param end a pointer to the end node pointer (if your list has one) 241 * @param end a pointer to the end node pointer (if your list has one)
242 * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one) 242 * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one)
243 * @param loc_next the location of a \c next pointer within your node struct (required) 243 * @param loc_next the location of a @c next pointer within your node struct (required)
244 * @param new_node a pointer to the node that shall be prepended 244 * @param new_node a pointer to the node that shall be prepended
245 */ 245 */
246 cx_attr_nonnull_arg(5) 246 cx_attr_nonnull_arg(5)
247 void cx_linked_list_prepend( 247 void cx_linked_list_prepend(
248 void **begin, 248 void **begin,
253 ); 253 );
254 254
255 /** 255 /**
256 * Links two nodes. 256 * Links two nodes.
257 * 257 *
258 * @param left the new predecessor of \p right 258 * @param left the new predecessor of @p right
259 * @param right the new successor of \p left 259 * @param right the new successor of @p left
260 * @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_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one)
261 * @param loc_next the location of a \c next pointer within your node struct (required) 261 * @param loc_next the location of a @c next pointer within your node struct (required)
262 */ 262 */
263 cx_attr_nonnull 263 cx_attr_nonnull
264 void cx_linked_list_link( 264 void cx_linked_list_link(
265 void *left, 265 void *left,
266 void *right, 266 void *right,
271 /** 271 /**
272 * Unlinks two nodes. 272 * Unlinks two nodes.
273 * 273 *
274 * If right is not the successor of left, the behavior is undefined. 274 * If right is not the successor of left, the behavior is undefined.
275 * 275 *
276 * @param left the predecessor of \p right 276 * @param left the predecessor of @p right
277 * @param right the successor of \p left 277 * @param right the successor of @p left
278 * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one) 278 * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one)
279 * @param loc_next the location of a \c next pointer within your node struct (required) 279 * @param loc_next the location of a @c next pointer within your node struct (required)
280 */ 280 */
281 cx_attr_nonnull 281 cx_attr_nonnull
282 void cx_linked_list_unlink( 282 void cx_linked_list_unlink(
283 void *left, 283 void *left,
284 void *right, 284 void *right,
288 288
289 /** 289 /**
290 * Inserts a new node after a given node of a linked list. 290 * Inserts a new node after a given node of a linked list.
291 * The new node must not be part of any list already. 291 * The new node must not be part of any list already.
292 * 292 *
293 * \note If you specify \c NULL as the \p node to insert after, this function needs either the \p begin or 293 * @note If you specify @c NULL as the @p node to insert after, this function needs either the @p begin or
294 * the \p end pointer to determine the start of the list. Then the new node will be prepended to the list. 294 * the @p end pointer to determine the start of the list. Then the new node will be prepended to the list.
295 * 295 *
296 * @param begin a pointer to the begin node pointer (if your list has one) 296 * @param begin a pointer to the beginning node pointer (if your list has one)
297 * @param end a pointer to the end node pointer (if your list has one) 297 * @param end a pointer to the end node pointer (if your list has one)
298 * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one) 298 * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one)
299 * @param loc_next the location of a \c next pointer within your node struct (required) 299 * @param loc_next the location of a @c next pointer within your node struct (required)
300 * @param node the node after which to insert (\c NULL if you want to prepend the node to the list) 300 * @param node the node after which to insert (@c NULL if you want to prepend the node to the list)
301 * @param new_node a pointer to the node that shall be inserted 301 * @param new_node a pointer to the node that shall be inserted
302 */ 302 */
303 cx_attr_nonnull_arg(6) 303 cx_attr_nonnull_arg(6)
304 void cx_linked_list_insert( 304 void cx_linked_list_insert(
305 void **begin, 305 void **begin,
313 /** 313 /**
314 * Inserts a chain of nodes after a given node of a linked list. 314 * Inserts a chain of nodes after a given node of a linked list.
315 * The chain must not be part of any list already. 315 * The chain must not be part of any list already.
316 * 316 *
317 * If you do not explicitly specify the end of the chain, it will be determined by traversing 317 * If you do not explicitly specify the end of the chain, it will be determined by traversing
318 * the \c next pointer. 318 * the @c next pointer.
319 * 319 *
320 * \note If you specify \c NULL as the \p node to insert after, this function needs either the \p begin or 320 * @note If you specify @c NULL as the @p node to insert after, this function needs either the @p begin or
321 * the \p end pointer to determine the start of the list. If only the \p end pointer is specified, you also need 321 * the @p end pointer to determine the start of the list. If only the @p end pointer is specified, you also need
322 * to provide a valid \p loc_prev location. 322 * to provide a valid @p loc_prev location.
323 * Then the chain will be prepended to the list. 323 * Then the chain will be prepended to the list.
324 * 324 *
325 * @param begin a pointer to the begin node pointer (if your list has one) 325 * @param begin a pointer to the beginning node pointer (if your list has one)
326 * @param end a pointer to the end node pointer (if your list has one) 326 * @param end a pointer to the end node pointer (if your list has one)
327 * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one) 327 * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one)
328 * @param loc_next the location of a \c next pointer within your node struct (required) 328 * @param loc_next the location of a @c next pointer within your node struct (required)
329 * @param node the node after which to insert (\c NULL to prepend the chain to the list) 329 * @param node the node after which to insert (@c NULL to prepend the chain to the list)
330 * @param insert_begin a pointer to the first node of the chain that shall be inserted 330 * @param insert_begin a pointer to the first node of the chain that shall be inserted
331 * @param insert_end a pointer to the last node of the chain (or NULL if the last node shall be determined) 331 * @param insert_end a pointer to the last node of the chain (or NULL if the last node shall be determined)
332 */ 332 */
333 cx_attr_nonnull_arg(6) 333 cx_attr_nonnull_arg(6)
334 void cx_linked_list_insert_chain( 334 void cx_linked_list_insert_chain(
343 343
344 /** 344 /**
345 * Inserts a node into a sorted linked list. 345 * Inserts a node into a sorted linked list.
346 * The new node must not be part of any list already. 346 * The new node must not be part of any list already.
347 * 347 *
348 * If the list starting with the node pointed to by \p begin is not sorted 348 * If the list starting with the node pointed to by @p begin is not sorted
349 * already, the behavior is undefined. 349 * already, the behavior is undefined.
350 * 350 *
351 * @param begin a pointer to the begin node pointer (required) 351 * @param begin a pointer to the beginning node pointer (required)
352 * @param end a pointer to the end node pointer (if your list has one) 352 * @param end a pointer to the end node pointer (if your list has one)
353 * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one) 353 * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one)
354 * @param loc_next the location of a \c next pointer within your node struct (required) 354 * @param loc_next the location of a @c next pointer within your node struct (required)
355 * @param new_node a pointer to the node that shall be inserted 355 * @param new_node a pointer to the node that shall be inserted
356 * @param cmp_func a compare function that will receive the node pointers 356 * @param cmp_func a compare function that will receive the node pointers
357 */ 357 */
358 cx_attr_nonnull_arg(1, 5, 6) 358 cx_attr_nonnull_arg(1, 5, 6)
359 void cx_linked_list_insert_sorted( 359 void cx_linked_list_insert_sorted(
367 367
368 /** 368 /**
369 * Inserts a chain of nodes into a sorted linked list. 369 * Inserts a chain of nodes into a sorted linked list.
370 * The chain must not be part of any list already. 370 * The chain must not be part of any list already.
371 * 371 *
372 * If either the list starting with the node pointed to by \p begin or the list 372 * If either the list starting with the node pointed to by @p begin or the list
373 * starting with \p insert_begin is not sorted, the behavior is undefined. 373 * starting with @p insert_begin is not sorted, the behavior is undefined.
374 * 374 *
375 * \attention In contrast to cx_linked_list_insert_chain(), the source chain 375 * @attention In contrast to cx_linked_list_insert_chain(), the source chain
376 * will be broken and inserted into the target list so that the resulting list 376 * will be broken and inserted into the target list so that the resulting list
377 * will be sorted according to \p cmp_func. That means, each node in the source 377 * will be sorted according to @p cmp_func. That means, each node in the source
378 * chain may be re-linked with nodes from the target list. 378 * chain may be re-linked with nodes from the target list.
379 * 379 *
380 * @param begin a pointer to the begin node pointer (required) 380 * @param begin a pointer to the beginning node pointer (required)
381 * @param end a pointer to the end node pointer (if your list has one) 381 * @param end a pointer to the end node pointer (if your list has one)
382 * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one) 382 * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one)
383 * @param loc_next the location of a \c next pointer within your node struct (required) 383 * @param loc_next the location of a @c next pointer within your node struct (required)
384 * @param insert_begin a pointer to the first node of the chain that shall be inserted 384 * @param insert_begin a pointer to the first node of the chain that shall be inserted
385 * @param cmp_func a compare function that will receive the node pointers 385 * @param cmp_func a compare function that will receive the node pointers
386 */ 386 */
387 cx_attr_nonnull_arg(1, 5, 6) 387 cx_attr_nonnull_arg(1, 5, 6)
388 void cx_linked_list_insert_sorted_chain( 388 void cx_linked_list_insert_sorted_chain(
395 ); 395 );
396 396
397 /** 397 /**
398 * Removes a chain of nodes from the linked list. 398 * Removes a chain of nodes from the linked list.
399 * 399 *
400 * If one of the nodes to remove is the begin (resp. end) node of the list and if \p begin (resp. \p end) 400 * If one of the nodes to remove is the beginning (resp. end) node of the list and if @p begin (resp. @p end)
401 * addresses are provided, the pointers are adjusted accordingly. 401 * addresses are provided, the pointers are adjusted accordingly.
402 * 402 *
403 * The following combinations of arguments are valid (more arguments are optional): 403 * The following combinations of arguments are valid (more arguments are optional):
404 * \li \p loc_next and \p loc_prev (ancestor node is determined by using the prev pointer, overall O(1) performance) 404 * @li @p loc_next and @p loc_prev (ancestor node is determined by using the prev pointer, overall O(1) performance)
405 * \li \p loc_next and \p begin (ancestor node is determined by list traversal, overall O(n) performance) 405 * @li @p loc_next and @p begin (ancestor node is determined by list traversal, overall O(n) performance)
406 * 406 *
407 * \remark The \c next and \c prev pointers of the removed node are not cleared by this function and may still be used 407 * @remark The @c next and @c prev pointers of the removed node are not cleared by this function and may still be used
408 * to traverse to a former adjacent node in the list, or within the chain. 408 * to traverse to a former adjacent node in the list, or within the chain.
409 * 409 *
410 * @param begin a pointer to the begin node pointer (optional) 410 * @param begin a pointer to the beginning node pointer (optional)
411 * @param end a pointer to the end node pointer (optional) 411 * @param end a pointer to the end node pointer (optional)
412 * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one) 412 * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one)
413 * @param loc_next the location of a \c next pointer within your node struct (required) 413 * @param loc_next the location of a @c next pointer within your node struct (required)
414 * @param node the start node of the chain 414 * @param node the start node of the chain
415 * @param num the number of nodes to remove 415 * @param num the number of nodes to remove
416 * @return the actual number of nodes that were removed (may be less when the list did not have enough nodes) 416 * @return the actual number of nodes that were removed (can be less when the list did not have enough nodes)
417 */ 417 */
418 cx_attr_nonnull_arg(5) 418 cx_attr_nonnull_arg(5)
419 size_t cx_linked_list_remove_chain( 419 size_t cx_linked_list_remove_chain(
420 void **begin, 420 void **begin,
421 void **end, 421 void **end,
426 ); 426 );
427 427
428 /** 428 /**
429 * Removes a node from the linked list. 429 * Removes a node from the linked list.
430 * 430 *
431 * If the node to remove is the begin (resp. end) node of the list and if \p begin (resp. \p end) 431 * If the node to remove is the beginning (resp. end) node of the list and if @p begin (resp. @p end)
432 * addresses are provided, the pointers are adjusted accordingly. 432 * addresses are provided, the pointers are adjusted accordingly.
433 * 433 *
434 * The following combinations of arguments are valid (more arguments are optional): 434 * The following combinations of arguments are valid (more arguments are optional):
435 * \li \p loc_next and \p loc_prev (ancestor node is determined by using the prev pointer, overall O(1) performance) 435 * @li @p loc_next and @p loc_prev (ancestor node is determined by using the prev pointer, overall O(1) performance)
436 * \li \p loc_next and \p begin (ancestor node is determined by list traversal, overall O(n) performance) 436 * @li @p loc_next and @p begin (ancestor node is determined by list traversal, overall O(n) performance)
437 * 437 *
438 * \remark The \c next and \c prev pointers of the removed node are not cleared by this function and may still be used 438 * @remark The @c next and @c prev pointers of the removed node are not cleared by this function and may still be used
439 * to traverse to a former adjacent node in the list. 439 * to traverse to a former adjacent node in the list.
440 * 440 *
441 * @param begin a pointer to the begin node pointer (optional) 441 * @param begin a pointer to the beginning node pointer (optional)
442 * @param end a pointer to the end node pointer (optional) 442 * @param end a pointer to the end node pointer (optional)
443 * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one) 443 * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one)
444 * @param loc_next the location of a \c next pointer within your node struct (required) 444 * @param loc_next the location of a @c next pointer within your node struct (required)
445 * @param node the node to remove 445 * @param node the node to remove
446 */ 446 */
447 cx_attr_nonnull_arg(5) 447 cx_attr_nonnull_arg(5)
448 static inline void cx_linked_list_remove( 448 static inline void cx_linked_list_remove(
449 void **begin, 449 void **begin,
454 ) { 454 ) {
455 cx_linked_list_remove_chain(begin, end, loc_prev, loc_next, node, 1); 455 cx_linked_list_remove_chain(begin, end, loc_prev, loc_next, node, 1);
456 } 456 }
457 457
458 /** 458 /**
459 * Determines the size of a linked list starting with \p node. 459 * Determines the size of a linked list starting with @p node.
460 *
460 * @param node the first node 461 * @param node the first node
461 * @param loc_next the location of the \c next pointer within the node struct 462 * @param loc_next the location of the @c next pointer within the node struct
462 * @return the size of the list or zero if \p node is \c NULL 463 * @return the size of the list or zero if @p node is @c NULL
463 */ 464 */
464 size_t cx_linked_list_size( 465 size_t cx_linked_list_size(
465 const void *node, 466 const void *node,
466 ptrdiff_t loc_next 467 ptrdiff_t loc_next
467 ); 468 );
468 469
469 /** 470 /**
470 * Sorts a linked list based on a comparison function. 471 * Sorts a linked list based on a comparison function.
471 * 472 *
472 * This function can work with linked lists of the following structure: 473 * This function can work with linked lists of the following structure:
473 * \code 474 * @code
474 * typedef struct node node; 475 * typedef struct node node;
475 * struct node { 476 * struct node {
476 * node* prev; 477 * node* prev;
477 * node* next; 478 * node* next;
478 * my_payload data; 479 * my_payload data;
479 * } 480 * }
480 * \endcode 481 * @endcode
481 * 482 *
482 * @note This is a recursive function with at most logarithmic recursion depth. 483 * @note This is a recursive function with at most logarithmic recursion depth.
483 * 484 *
484 * @param begin a pointer to the begin node pointer (required) 485 * @param begin a pointer to the beginning node pointer (required)
485 * @param end a pointer to the end node pointer (optional) 486 * @param end a pointer to the end node pointer (optional)
486 * @param loc_prev the location of a \c prev pointer within your node struct (negative if not present) 487 * @param loc_prev the location of a @c prev pointer within your node struct (negative if not present)
487 * @param loc_next the location of a \c next pointer within your node struct (required) 488 * @param loc_next the location of a @c next pointer within your node struct (required)
488 * @param loc_data the location of the \c data pointer within your node struct 489 * @param loc_data the location of the @c data pointer within your node struct
489 * @param cmp_func the compare function defining the sort order 490 * @param cmp_func the compare function defining the sort order
490 */ 491 */
491 cx_attr_nonnull_arg(1, 6) 492 cx_attr_nonnull_arg(1, 6)
492 void cx_linked_list_sort( 493 void cx_linked_list_sort(
493 void **begin, 494 void **begin,
500 501
501 502
502 /** 503 /**
503 * Compares two lists element wise. 504 * Compares two lists element wise.
504 * 505 *
505 * \note Both list must have the same structure. 506 * @attention Both list must have the same structure.
506 * 507 *
507 * @param begin_left the begin of the left list (\c NULL denotes an empty list) 508 * @param begin_left the beginning of the left list (@c NULL denotes an empty list)
508 * @param begin_right the begin of the right list (\c NULL denotes an empty list) 509 * @param begin_right the beginning of the right list (@c NULL denotes an empty list)
509 * @param loc_advance the location of the pointer to advance 510 * @param loc_advance the location of the pointer to advance
510 * @param loc_data the location of the \c data pointer within your node struct 511 * @param loc_data the location of the @c data pointer within your node struct
511 * @param cmp_func the function to compare the elements 512 * @param cmp_func the function to compare the elements
512 * @return the first non-zero result of invoking \p cmp_func or: negative if the left list is smaller than the 513 * @return the first non-zero result of invoking @p cmp_func or: negative if the left list is smaller than the
513 * right list, positive if the left list is larger than the right list, zero if both lists are equal. 514 * right list, positive if the left list is larger than the right list, zero if both lists are equal.
514 */ 515 */
515 cx_attr_nonnull_arg(5) 516 cx_attr_nonnull_arg(5)
516 int cx_linked_list_compare( 517 int cx_linked_list_compare(
517 const void *begin_left, 518 const void *begin_left,
522 ); 523 );
523 524
524 /** 525 /**
525 * Reverses the order of the nodes in a linked list. 526 * Reverses the order of the nodes in a linked list.
526 * 527 *
527 * @param begin a pointer to the begin node pointer (required) 528 * @param begin a pointer to the beginning node pointer (required)
528 * @param end a pointer to the end node pointer (optional) 529 * @param end a pointer to the end node pointer (optional)
529 * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one) 530 * @param loc_prev the location of a @c prev pointer within your node struct (negative if your struct does not have one)
530 * @param loc_next the location of a \c next pointer within your node struct (required) 531 * @param loc_next the location of a @c next pointer within your node struct (required)
531 */ 532 */
532 cx_attr_nonnull_arg(1) 533 cx_attr_nonnull_arg(1)
533 void cx_linked_list_reverse( 534 void cx_linked_list_reverse(
534 void **begin, 535 void **begin,
535 void **end, 536 void **end,

mercurial