ucx/cx/linked_list.h

changeset 440
7c4b9cba09ca
parent 324
ce13a778654a
equal deleted inserted replaced
439:bf7084544cb1 440:7c4b9cba09ca
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.
49 */ 48 *
50 extern unsigned cx_linked_list_swap_sbo_size; 49 */
51 50 extern const unsigned cx_linked_list_swap_sbo_size;
52 /** 51
53 * Allocates a linked list for storing elements with \p elem_size bytes each. 52 /**
54 * 53 * Allocates a linked list for storing elements with @p elem_size bytes each.
55 * If \p elem_size is CX_STORE_POINTERS, the created list will be created as if 54 *
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 the cxDefaultAllocator 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
68 cx_attr_malloc
69 cx_attr_dealloc(cxListFree, 1)
67 CxList *cxLinkedListCreate( 70 CxList *cxLinkedListCreate(
68 const CxAllocator *allocator, 71 const CxAllocator *allocator,
69 cx_compare_func comparator, 72 cx_compare_func comparator,
70 size_t elem_size 73 size_t elem_size
71 ); 74 );
72 75
73 /** 76 /**
74 * 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.
75 * 78 *
76 * 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
77 * 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
78 * after list creation or use cxLinkedListCreate(). 81 * after list creation or use cxLinkedListCreate().
79 * 82 *
80 * 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
81 * cxListStorePointers() was called immediately after creation and the compare 84 * cxListStorePointers() was called immediately after creation and the compare
82 * function will be automatically set to cx_cmp_ptr(). 85 * function will be automatically set to cx_cmp_ptr().
83 * 86 *
84 * @param elem_size the size of each element in bytes 87 * @param elem_size (@c size_t) the size of each element in bytes
85 * @return the created list 88 * @return (@c CxList*) the created list
86 */ 89 */
87 #define cxLinkedListCreateSimple(elem_size) \ 90 #define cxLinkedListCreateSimple(elem_size) \
88 cxLinkedListCreate(NULL, NULL, elem_size) 91 cxLinkedListCreate(NULL, NULL, elem_size)
89 92
90 /** 93 /**
91 * Finds the node at a certain index. 94 * Finds the node at a certain index.
92 * 95 *
93 * 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.
94 * 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
95 * 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).
96 * 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
97 * (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
98 * \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
99 * (i.e. a pointer to the previous node). 102 * (i.e. a pointer to the previous node).
100 * 103 *
101 * @param start a pointer to the start node 104 * @param start a pointer to the start node
102 * @param start_index the start index 105 * @param start_index the start index
103 * @param loc_advance the location of the pointer to advance 106 * @param loc_advance the location of the pointer to advance
104 * @param index the search index 107 * @param index the search index
105 * @return the node found at the specified index 108 * @return the node found at the specified index
106 */ 109 */
107 __attribute__((__nonnull__)) 110 cx_attr_nonnull
111 cx_attr_nodiscard
108 void *cx_linked_list_at( 112 void *cx_linked_list_at(
109 const void *start, 113 const void *start,
110 size_t start_index, 114 size_t start_index,
111 ptrdiff_t loc_advance, 115 ptrdiff_t loc_advance,
112 size_t index 116 size_t index
115 /** 119 /**
116 * Finds the index of an element within a linked list. 120 * Finds the index of an element within a linked list.
117 * 121 *
118 * @param start a pointer to the start node 122 * @param start a pointer to the start node
119 * @param loc_advance the location of the pointer to advance 123 * @param loc_advance the location of the pointer to advance
120 * @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
121 * @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
122 * @param elem a pointer to the element to find 126 * @param elem a pointer to the element to find
123 * @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
124 */ 128 */
125 __attribute__((__nonnull__)) 129 cx_attr_nonnull
126 ssize_t cx_linked_list_find( 130 ssize_t cx_linked_list_find(
127 const void *start, 131 const void *start,
128 ptrdiff_t loc_advance, 132 ptrdiff_t loc_advance,
129 ptrdiff_t loc_data, 133 ptrdiff_t loc_data,
130 cx_compare_func cmp_func, 134 cx_compare_func cmp_func,
132 ); 136 );
133 137
134 /** 138 /**
135 * Finds the node containing an element within a linked list. 139 * Finds the node containing an element within a linked list.
136 * 140 *
137 * @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
138 * could not be found) shall be stored to 142 * could not be found) shall be stored to
139 * @param start a pointer to the start node 143 * @param start a pointer to the start node
140 * @param loc_advance the location of the pointer to advance 144 * @param loc_advance the location of the pointer to advance
141 * @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
142 * @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
143 * @param elem a pointer to the element to find 147 * @param elem a pointer to the element to find
144 * @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
145 */ 149 */
146 __attribute__((__nonnull__)) 150 cx_attr_nonnull
147 ssize_t cx_linked_list_find_node( 151 ssize_t cx_linked_list_find_node(
148 void **result, 152 void **result,
149 const void *start, 153 const void *start,
150 ptrdiff_t loc_advance, 154 ptrdiff_t loc_advance,
151 ptrdiff_t loc_data, 155 ptrdiff_t loc_data,
154 ); 158 );
155 159
156 /** 160 /**
157 * Finds the first node in a linked list. 161 * Finds the first node in a linked list.
158 * 162 *
159 * 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
160 * along a prev pointer whose location within the node struct is 164 * along a prev pointer whose location within the node struct is
161 * denoted by \p loc_prev. 165 * denoted by @p loc_prev.
162 * 166 *
163 * @param node a pointer to a node in the list 167 * @param node a pointer to a node in the list
164 * @param loc_prev the location of the \c prev pointer 168 * @param loc_prev the location of the @c prev pointer
165 * @return a pointer to the first node 169 * @return a pointer to the first node
166 */ 170 */
167 __attribute__((__nonnull__)) 171 cx_attr_nonnull
172 cx_attr_returns_nonnull
168 void *cx_linked_list_first( 173 void *cx_linked_list_first(
169 const void *node, 174 const void *node,
170 ptrdiff_t loc_prev 175 ptrdiff_t loc_prev
171 ); 176 );
172 177
173 /** 178 /**
174 * Finds the last node in a linked list. 179 * Finds the last node in a linked list.
175 * 180 *
176 * 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
177 * along a next pointer whose location within the node struct is 182 * along a next pointer whose location within the node struct is
178 * denoted by \p loc_next. 183 * denoted by @p loc_next.
179 * 184 *
180 * @param node a pointer to a node in the list 185 * @param node a pointer to a node in the list
181 * @param loc_next the location of the \c next pointer 186 * @param loc_next the location of the @c next pointer
182 * @return a pointer to the last node 187 * @return a pointer to the last node
183 */ 188 */
184 __attribute__((__nonnull__)) 189 cx_attr_nonnull
190 cx_attr_returns_nonnull
185 void *cx_linked_list_last( 191 void *cx_linked_list_last(
186 const void *node, 192 const void *node,
187 ptrdiff_t loc_next 193 ptrdiff_t loc_next
188 ); 194 );
189 195
190 /** 196 /**
191 * 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.
192 * 198 *
193 * \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.
194 * 200 *
195 * @param begin the node where to start the search 201 * @param begin the node where to start the search
196 * @param loc_next the location of the \c next pointer 202 * @param loc_next the location of the @c next pointer
197 * @param node the successor of the node to find 203 * @param node the successor of the node to find
198 * @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
199 */ 205 */
200 __attribute__((__nonnull__)) 206 cx_attr_nonnull
201 void *cx_linked_list_prev( 207 void *cx_linked_list_prev(
202 const void *begin, 208 const void *begin,
203 ptrdiff_t loc_next, 209 ptrdiff_t loc_next,
204 const void *node 210 const void *node
205 ); 211 );
206 212
207 /** 213 /**
208 * Adds a new node to a linked list. 214 * Adds a new node to a linked list.
209 * The node must not be part of any list already. 215 * The node must not be part of any list already.
210 * 216 *
211 * \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.
212 * 218 *
213 * @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)
214 * @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)
215 * @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)
216 * @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)
217 * @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
218 */ 224 */
219 __attribute__((__nonnull__(5))) 225 cx_attr_nonnull_arg(5)
220 void cx_linked_list_add( 226 void cx_linked_list_add(
221 void **begin, 227 void **begin,
222 void **end, 228 void **end,
223 ptrdiff_t loc_prev, 229 ptrdiff_t loc_prev,
224 ptrdiff_t loc_next, 230 ptrdiff_t loc_next,
227 233
228 /** 234 /**
229 * Prepends a new node to a linked list. 235 * Prepends a new node to a linked list.
230 * The node must not be part of any list already. 236 * The node must not be part of any list already.
231 * 237 *
232 * \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.
233 * 239 *
234 * @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)
235 * @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)
236 * @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)
237 * @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)
238 * @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
239 */ 245 */
240 __attribute__((__nonnull__(5))) 246 cx_attr_nonnull_arg(5)
241 void cx_linked_list_prepend( 247 void cx_linked_list_prepend(
242 void **begin, 248 void **begin,
243 void **end, 249 void **end,
244 ptrdiff_t loc_prev, 250 ptrdiff_t loc_prev,
245 ptrdiff_t loc_next, 251 ptrdiff_t loc_next,
247 ); 253 );
248 254
249 /** 255 /**
250 * Links two nodes. 256 * Links two nodes.
251 * 257 *
252 * @param left the new predecessor of \p right 258 * @param left the new predecessor of @p right
253 * @param right the new successor of \p left 259 * @param right the new successor of @p left
254 * @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)
255 * @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)
256 */ 262 */
257 __attribute__((__nonnull__)) 263 cx_attr_nonnull
258 void cx_linked_list_link( 264 void cx_linked_list_link(
259 void *left, 265 void *left,
260 void *right, 266 void *right,
261 ptrdiff_t loc_prev, 267 ptrdiff_t loc_prev,
262 ptrdiff_t loc_next 268 ptrdiff_t loc_next
265 /** 271 /**
266 * Unlinks two nodes. 272 * Unlinks two nodes.
267 * 273 *
268 * 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.
269 * 275 *
270 * @param left the predecessor of \p right 276 * @param left the predecessor of @p right
271 * @param right the successor of \p left 277 * @param right the successor of @p left
272 * @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)
273 * @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)
274 */ 280 */
275 __attribute__((__nonnull__)) 281 cx_attr_nonnull
276 void cx_linked_list_unlink( 282 void cx_linked_list_unlink(
277 void *left, 283 void *left,
278 void *right, 284 void *right,
279 ptrdiff_t loc_prev, 285 ptrdiff_t loc_prev,
280 ptrdiff_t loc_next 286 ptrdiff_t loc_next
282 288
283 /** 289 /**
284 * 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.
285 * The new node must not be part of any list already. 291 * The new node must not be part of any list already.
286 * 292 *
287 * \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
288 * 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.
289 * 295 *
290 * @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)
291 * @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)
292 * @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)
293 * @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)
294 * @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)
295 * @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
296 */ 302 */
297 __attribute__((__nonnull__(6))) 303 cx_attr_nonnull_arg(6)
298 void cx_linked_list_insert( 304 void cx_linked_list_insert(
299 void **begin, 305 void **begin,
300 void **end, 306 void **end,
301 ptrdiff_t loc_prev, 307 ptrdiff_t loc_prev,
302 ptrdiff_t loc_next, 308 ptrdiff_t loc_next,
307 /** 313 /**
308 * 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.
309 * The chain must not be part of any list already. 315 * The chain must not be part of any list already.
310 * 316 *
311 * 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
312 * the \c next pointer. 318 * the @c next pointer.
313 * 319 *
314 * \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
315 * 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
316 * to provide a valid \p loc_prev location. 322 * to provide a valid @p loc_prev location.
317 * Then the chain will be prepended to the list. 323 * Then the chain will be prepended to the list.
318 * 324 *
319 * @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)
320 * @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)
321 * @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)
322 * @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)
323 * @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)
324 * @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
325 * @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)
326 */ 332 */
327 __attribute__((__nonnull__(6))) 333 cx_attr_nonnull_arg(6)
328 void cx_linked_list_insert_chain( 334 void cx_linked_list_insert_chain(
329 void **begin, 335 void **begin,
330 void **end, 336 void **end,
331 ptrdiff_t loc_prev, 337 ptrdiff_t loc_prev,
332 ptrdiff_t loc_next, 338 ptrdiff_t loc_next,
337 343
338 /** 344 /**
339 * Inserts a node into a sorted linked list. 345 * Inserts a node into a sorted linked list.
340 * The new node must not be part of any list already. 346 * The new node must not be part of any list already.
341 * 347 *
342 * 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
343 * already, the behavior is undefined. 349 * already, the behavior is undefined.
344 * 350 *
345 * @param begin a pointer to the begin node pointer (required) 351 * @param begin a pointer to the beginning node pointer (required)
346 * @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)
347 * @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)
348 * @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)
349 * @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
350 * @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
351 */ 357 */
352 __attribute__((__nonnull__(1, 5, 6))) 358 cx_attr_nonnull_arg(1, 5, 6)
353 void cx_linked_list_insert_sorted( 359 void cx_linked_list_insert_sorted(
354 void **begin, 360 void **begin,
355 void **end, 361 void **end,
356 ptrdiff_t loc_prev, 362 ptrdiff_t loc_prev,
357 ptrdiff_t loc_next, 363 ptrdiff_t loc_next,
361 367
362 /** 368 /**
363 * Inserts a chain of nodes into a sorted linked list. 369 * Inserts a chain of nodes into a sorted linked list.
364 * The chain must not be part of any list already. 370 * The chain must not be part of any list already.
365 * 371 *
366 * 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
367 * 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.
368 * 374 *
369 * \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
370 * 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
371 * 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
372 * chain may be re-linked with nodes from the target list. 378 * chain may be re-linked with nodes from the target list.
373 * 379 *
374 * @param begin a pointer to the begin node pointer (required) 380 * @param begin a pointer to the beginning node pointer (required)
375 * @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)
376 * @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)
377 * @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)
378 * @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
379 * @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
380 */ 386 */
381 __attribute__((__nonnull__(1, 5, 6))) 387 cx_attr_nonnull_arg(1, 5, 6)
382 void cx_linked_list_insert_sorted_chain( 388 void cx_linked_list_insert_sorted_chain(
383 void **begin, 389 void **begin,
384 void **end, 390 void **end,
385 ptrdiff_t loc_prev, 391 ptrdiff_t loc_prev,
386 ptrdiff_t loc_next, 392 ptrdiff_t loc_next,
387 void *insert_begin, 393 void *insert_begin,
388 cx_compare_func cmp_func 394 cx_compare_func cmp_func
389 ); 395 );
390 396
391 /** 397 /**
398 * Removes a chain of nodes from the linked list.
399 *
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.
402 *
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)
405 * @li @p loc_next and @p begin (ancestor node is determined by list traversal, overall O(n) performance)
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
408 * to traverse to a former adjacent node in the list, or within the chain.
409 *
410 * @param begin a pointer to the beginning 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)
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
415 * @param num the number of nodes to remove
416 * @return the actual number of nodes that were removed (can be less when the list did not have enough nodes)
417 */
418 cx_attr_nonnull_arg(5)
419 size_t cx_linked_list_remove_chain(
420 void **begin,
421 void **end,
422 ptrdiff_t loc_prev,
423 ptrdiff_t loc_next,
424 void *node,
425 size_t num
426 );
427
428 /**
392 * Removes a node from the linked list. 429 * Removes a node from the linked list.
393 * 430 *
394 * 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)
395 * addresses are provided, the pointers are adjusted accordingly. 432 * addresses are provided, the pointers are adjusted accordingly.
396 * 433 *
397 * The following combinations of arguments are valid (more arguments are optional): 434 * The following combinations of arguments are valid (more arguments are optional):
398 * \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)
399 * \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)
400 * 437 *
401 * \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
402 * to traverse to a former adjacent node in the list. 439 * to traverse to a former adjacent node in the list.
403 * 440 *
404 * @param begin a pointer to the begin node pointer (optional) 441 * @param begin a pointer to the beginning node pointer (optional)
405 * @param end a pointer to the end node pointer (optional) 442 * @param end a pointer to the end node pointer (optional)
406 * @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)
407 * @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)
408 * @param node the node to remove 445 * @param node the node to remove
409 */ 446 */
410 __attribute__((__nonnull__(5))) 447 cx_attr_nonnull_arg(5)
411 void cx_linked_list_remove( 448 static inline void cx_linked_list_remove(
412 void **begin, 449 void **begin,
413 void **end, 450 void **end,
414 ptrdiff_t loc_prev, 451 ptrdiff_t loc_prev,
415 ptrdiff_t loc_next, 452 ptrdiff_t loc_next,
416 void *node 453 void *node
417 ); 454 ) {
418 455 cx_linked_list_remove_chain(begin, end, loc_prev, loc_next, node, 1);
419 456 }
420 /** 457
421 * Determines the size of a linked list starting with \p node. 458 /**
459 * Determines the size of a linked list starting with @p node.
460 *
422 * @param node the first node 461 * @param node the first node
423 * @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
424 * @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
425 */ 464 */
426 size_t cx_linked_list_size( 465 size_t cx_linked_list_size(
427 const void *node, 466 const void *node,
428 ptrdiff_t loc_next 467 ptrdiff_t loc_next
429 ); 468 );
430 469
431 /** 470 /**
432 * Sorts a linked list based on a comparison function. 471 * Sorts a linked list based on a comparison function.
433 * 472 *
434 * This function can work with linked lists of the following structure: 473 * This function can work with linked lists of the following structure:
435 * \code 474 * @code
436 * typedef struct node node; 475 * typedef struct node node;
437 * struct node { 476 * struct node {
438 * node* prev; 477 * node* prev;
439 * node* next; 478 * node* next;
440 * my_payload data; 479 * my_payload data;
441 * } 480 * }
442 * \endcode 481 * @endcode
443 * 482 *
444 * @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.
445 * 484 *
446 * @param begin a pointer to the begin node pointer (required) 485 * @param begin a pointer to the beginning node pointer (required)
447 * @param end a pointer to the end node pointer (optional) 486 * @param end a pointer to the end node pointer (optional)
448 * @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)
449 * @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)
450 * @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
451 * @param cmp_func the compare function defining the sort order 490 * @param cmp_func the compare function defining the sort order
452 */ 491 */
453 __attribute__((__nonnull__(1, 6))) 492 cx_attr_nonnull_arg(1, 6)
454 void cx_linked_list_sort( 493 void cx_linked_list_sort(
455 void **begin, 494 void **begin,
456 void **end, 495 void **end,
457 ptrdiff_t loc_prev, 496 ptrdiff_t loc_prev,
458 ptrdiff_t loc_next, 497 ptrdiff_t loc_next,
462 501
463 502
464 /** 503 /**
465 * Compares two lists element wise. 504 * Compares two lists element wise.
466 * 505 *
467 * \note Both list must have the same structure. 506 * @attention Both list must have the same structure.
468 * 507 *
469 * @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)
470 * @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)
471 * @param loc_advance the location of the pointer to advance 510 * @param loc_advance the location of the pointer to advance
472 * @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
473 * @param cmp_func the function to compare the elements 512 * @param cmp_func the function to compare the elements
474 * @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
475 * 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.
476 */ 515 */
477 __attribute__((__nonnull__(5))) 516 cx_attr_nonnull_arg(5)
478 int cx_linked_list_compare( 517 int cx_linked_list_compare(
479 const void *begin_left, 518 const void *begin_left,
480 const void *begin_right, 519 const void *begin_right,
481 ptrdiff_t loc_advance, 520 ptrdiff_t loc_advance,
482 ptrdiff_t loc_data, 521 ptrdiff_t loc_data,
484 ); 523 );
485 524
486 /** 525 /**
487 * Reverses the order of the nodes in a linked list. 526 * Reverses the order of the nodes in a linked list.
488 * 527 *
489 * @param begin a pointer to the begin node pointer (required) 528 * @param begin a pointer to the beginning node pointer (required)
490 * @param end a pointer to the end node pointer (optional) 529 * @param end a pointer to the end node pointer (optional)
491 * @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)
492 * @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)
493 */ 532 */
494 __attribute__((__nonnull__(1))) 533 cx_attr_nonnull_arg(1)
495 void cx_linked_list_reverse( 534 void cx_linked_list_reverse(
496 void **begin, 535 void **begin,
497 void **end, 536 void **end,
498 ptrdiff_t loc_prev, 537 ptrdiff_t loc_prev,
499 ptrdiff_t loc_next 538 ptrdiff_t loc_next

mercurial