| 74 * Destructor function. |
74 * Destructor function. |
| 75 * |
75 * |
| 76 * Implementations SHALL invoke the content destructor functions if provided |
76 * Implementations SHALL invoke the content destructor functions if provided |
| 77 * and SHALL deallocate the entire list memory. |
77 * and SHALL deallocate the entire list memory. |
| 78 */ |
78 */ |
| 79 cx_attr_nonnull |
|
| 80 void (*deallocate)(struct cx_list_s *list); |
79 void (*deallocate)(struct cx_list_s *list); |
| 81 |
80 |
| 82 /** |
81 /** |
| 83 * Member function for inserting a single element. |
82 * Member function for inserting a single element. |
| 84 * Implementors SHOULD see to performant implementations for corner cases. |
83 * Implementors SHOULD see to performant implementations for corner cases. |
| 85 */ |
84 */ |
| 86 cx_attr_nonnull |
|
| 87 int (*insert_element)( |
85 int (*insert_element)( |
| 88 struct cx_list_s *list, |
86 struct cx_list_s *list, |
| 89 size_t index, |
87 size_t index, |
| 90 const void *data |
88 const void *data |
| 91 ); |
89 ); |
| 92 |
90 |
| 93 /** |
91 /** |
| 94 * Member function for inserting multiple elements. |
92 * Member function for inserting multiple elements. |
| 95 * Implementors SHOULD see to performant implementations for corner cases. |
93 * Implementors SHOULD see to performant implementations for corner cases. |
| |
94 * |
| 96 * @see cx_list_default_insert_array() |
95 * @see cx_list_default_insert_array() |
| 97 */ |
96 */ |
| 98 cx_attr_nonnull |
|
| 99 size_t (*insert_array)( |
97 size_t (*insert_array)( |
| 100 struct cx_list_s *list, |
98 struct cx_list_s *list, |
| 101 size_t index, |
99 size_t index, |
| 102 const void *data, |
100 const void *data, |
| 103 size_t n |
101 size_t n |
| 106 /** |
104 /** |
| 107 * Member function for inserting sorted elements into a sorted list. |
105 * Member function for inserting sorted elements into a sorted list. |
| 108 * |
106 * |
| 109 * @see cx_list_default_insert_sorted() |
107 * @see cx_list_default_insert_sorted() |
| 110 */ |
108 */ |
| 111 cx_attr_nonnull |
|
| 112 size_t (*insert_sorted)( |
109 size_t (*insert_sorted)( |
| 113 struct cx_list_s *list, |
110 struct cx_list_s *list, |
| 114 const void *sorted_data, |
111 const void *sorted_data, |
| 115 size_t n |
112 size_t n |
| 116 ); |
113 ); |
| 117 |
114 |
| 118 /** |
115 /** |
| 119 * Member function for inserting an element relative to an iterator position. |
116 * Member function for inserting an element relative to an iterator position. |
| 120 */ |
117 */ |
| 121 cx_attr_nonnull |
|
| 122 int (*insert_iter)( |
118 int (*insert_iter)( |
| 123 struct cx_iterator_s *iter, |
119 struct cx_iterator_s *iter, |
| 124 const void *elem, |
120 const void *elem, |
| 125 int prepend |
121 int prepend |
| 126 ); |
122 ); |
| 127 |
123 |
| 128 /** |
124 /** |
| 129 * Member function for removing elements. |
125 * Member function for removing elements. |
| 130 * |
126 * |
| 131 * Implementations SHALL check if \p targetbuf is set and copy the elements |
127 * Implementations SHALL check if @p targetbuf is set and copy the elements |
| 132 * to the buffer without invoking any destructor. |
128 * to the buffer without invoking any destructor. |
| 133 * When \p targetbuf is not set, the destructors SHALL be invoked. |
129 * When @p targetbuf is not set, the destructors SHALL be invoked. |
| 134 * |
130 * |
| 135 * The function SHALL return the actual number of elements removed, which |
131 * The function SHALL return the actual number of elements removed, which |
| 136 * might be lower than \p num when going out of bounds. |
132 * might be lower than @p num when going out of bounds. |
| 137 */ |
133 */ |
| 138 cx_attr_nonnull_arg(1) |
|
| 139 cx_attr_access_w(4) |
|
| 140 size_t (*remove)( |
134 size_t (*remove)( |
| 141 struct cx_list_s *list, |
135 struct cx_list_s *list, |
| 142 size_t index, |
136 size_t index, |
| 143 size_t num, |
137 size_t num, |
| 144 void *targetbuf |
138 void *targetbuf |
| 145 ); |
139 ); |
| 146 |
140 |
| 147 /** |
141 /** |
| 148 * Member function for removing all elements. |
142 * Member function for removing all elements. |
| 149 */ |
143 */ |
| 150 cx_attr_nonnull |
|
| 151 void (*clear)(struct cx_list_s *list); |
144 void (*clear)(struct cx_list_s *list); |
| 152 |
145 |
| 153 /** |
146 /** |
| 154 * Member function for swapping two elements. |
147 * Member function for swapping two elements. |
| |
148 * |
| 155 * @see cx_list_default_swap() |
149 * @see cx_list_default_swap() |
| 156 */ |
150 */ |
| 157 cx_attr_nonnull |
|
| 158 int (*swap)( |
151 int (*swap)( |
| 159 struct cx_list_s *list, |
152 struct cx_list_s *list, |
| 160 size_t i, |
153 size_t i, |
| 161 size_t j |
154 size_t j |
| 162 ); |
155 ); |
| 163 |
156 |
| 164 /** |
157 /** |
| 165 * Member function for element lookup. |
158 * Member function for element lookup. |
| 166 */ |
159 */ |
| 167 cx_attr_nonnull |
|
| 168 cx_attr_nodiscard |
|
| 169 void *(*at)( |
160 void *(*at)( |
| 170 const struct cx_list_s *list, |
161 const struct cx_list_s *list, |
| 171 size_t index |
162 size_t index |
| 172 ); |
163 ); |
| 173 |
164 |
| 174 /** |
165 /** |
| 175 * Member function for finding and optionally removing an element. |
166 * Member function for finding and optionally removing an element. |
| 176 */ |
167 */ |
| 177 cx_attr_nonnull |
|
| 178 cx_attr_nodiscard |
|
| 179 ssize_t (*find_remove)( |
168 ssize_t (*find_remove)( |
| 180 struct cx_list_s *list, |
169 struct cx_list_s *list, |
| 181 const void *elem, |
170 const void *elem, |
| 182 bool remove |
171 bool remove |
| 183 ); |
172 ); |
| 184 |
173 |
| 185 /** |
174 /** |
| 186 * Member function for sorting the list in-place. |
175 * Member function for sorting the list. |
| |
176 * |
| 187 * @see cx_list_default_sort() |
177 * @see cx_list_default_sort() |
| 188 */ |
178 */ |
| 189 cx_attr_nonnull |
|
| 190 void (*sort)(struct cx_list_s *list); |
179 void (*sort)(struct cx_list_s *list); |
| 191 |
180 |
| 192 /** |
181 /** |
| 193 * Optional member function for comparing this list |
182 * Optional member function for comparing this list |
| 194 * to another list of the same type. |
183 * to another list of the same type. |
| 195 * If set to \c NULL, comparison won't be optimized. |
184 * If set to @c NULL, comparison won't be optimized. |
| 196 */ |
185 */ |
| 197 cx_attr_nonnull |
186 cx_attr_nonnull |
| 198 int (*compare)( |
187 int (*compare)( |
| 199 const struct cx_list_s *list, |
188 const struct cx_list_s *list, |
| 200 const struct cx_list_s *other |
189 const struct cx_list_s *other |
| 201 ); |
190 ); |
| 202 |
191 |
| 203 /** |
192 /** |
| 204 * Member function for reversing the order of the items. |
193 * Member function for reversing the order of the items. |
| 205 */ |
194 */ |
| 206 cx_attr_nonnull |
|
| 207 void (*reverse)(struct cx_list_s *list); |
195 void (*reverse)(struct cx_list_s *list); |
| 208 |
196 |
| 209 /** |
197 /** |
| 210 * Member function for returning an iterator pointing to the specified index. |
198 * Member function for returning an iterator pointing to the specified index. |
| 211 */ |
199 */ |
| 212 cx_attr_nonnull |
|
| 213 struct cx_iterator_s (*iterator)( |
200 struct cx_iterator_s (*iterator)( |
| 214 const struct cx_list_s *list, |
201 const struct cx_list_s *list, |
| 215 size_t index, |
202 size_t index, |
| 216 bool backward |
203 bool backward |
| 217 ); |
204 ); |
| 283 * version for your list. |
270 * version for your list. |
| 284 * |
271 * |
| 285 * @param list the list in which to swap |
272 * @param list the list in which to swap |
| 286 * @param i index of one element |
273 * @param i index of one element |
| 287 * @param j index of the other element |
274 * @param j index of the other element |
| 288 * @return zero on success, non-zero when indices are out of bounds or memory |
275 * @retval zero success |
| |
276 * @retval non-zero when indices are out of bounds or memory |
| 289 * allocation for the temporary buffer fails |
277 * allocation for the temporary buffer fails |
| 290 */ |
278 */ |
| 291 cx_attr_nonnull |
279 cx_attr_nonnull |
| 292 int cx_list_default_swap(struct cx_list_s *list, size_t i, size_t j); |
280 int cx_list_default_swap(struct cx_list_s *list, size_t i, size_t j); |
| 293 |
281 |
| 366 * Adds multiple items to the end of the list. |
355 * Adds multiple items to the end of the list. |
| 367 * |
356 * |
| 368 * This method is more efficient than invoking cxListAdd() multiple times. |
357 * This method is more efficient than invoking cxListAdd() multiple times. |
| 369 * |
358 * |
| 370 * If there is not enough memory to add all elements, the returned value is |
359 * If there is not enough memory to add all elements, the returned value is |
| 371 * less than \p n. |
360 * less than @p n. |
| 372 * |
361 * |
| 373 * If this list is storing pointers instead of objects \p array is expected to |
362 * If this list is storing pointers instead of objects @p array is expected to |
| 374 * be an array of pointers. |
363 * be an array of pointers. |
| 375 * |
364 * |
| 376 * @param list the list |
365 * @param list the list |
| 377 * @param array a pointer to the elements to add |
366 * @param array a pointer to the elements to add |
| 378 * @param n the number of elements to add |
367 * @param n the number of elements to add |
| 388 } |
377 } |
| 389 |
378 |
| 390 /** |
379 /** |
| 391 * Inserts an item at the specified index. |
380 * Inserts an item at the specified index. |
| 392 * |
381 * |
| 393 * If \p index equals the list \c size, this is effectively cxListAdd(). |
382 * If @p index equals the list @c size, this is effectively cxListAdd(). |
| 394 * |
383 * |
| 395 * @param list the list |
384 * @param list the list |
| 396 * @param index the index the element shall have |
385 * @param index the index the element shall have |
| 397 * @param elem a pointer to the element to add |
386 * @param elem a pointer to the element to add |
| 398 * @return zero on success, non-zero on memory allocation failure |
387 * @retval zero success |
| 399 * or when the index is out of bounds |
388 * @retval non-zero memory allocation failure or the index is out of bounds |
| 400 * @see cxListInsertAfter() |
389 * @see cxListInsertAfter() |
| 401 * @see cxListInsertBefore() |
390 * @see cxListInsertBefore() |
| 402 */ |
391 */ |
| 403 cx_attr_nonnull |
392 cx_attr_nonnull |
| 404 static inline int cxListInsert( |
393 static inline int cxListInsert( |
| 425 return list->cl->insert_sorted(list, data, 1) == 0; |
415 return list->cl->insert_sorted(list, data, 1) == 0; |
| 426 } |
416 } |
| 427 |
417 |
| 428 /** |
418 /** |
| 429 * Inserts multiple items to the list at the specified index. |
419 * Inserts multiple items to the list at the specified index. |
| 430 * If \p index equals the list size, this is effectively cxListAddArray(). |
420 * If @p index equals the list size, this is effectively cxListAddArray(). |
| 431 * |
421 * |
| 432 * This method is usually more efficient than invoking cxListInsert() |
422 * This method is usually more efficient than invoking cxListInsert() |
| 433 * multiple times. |
423 * multiple times. |
| 434 * |
424 * |
| 435 * If there is not enough memory to add all elements, the returned value is |
425 * If there is not enough memory to add all elements, the returned value is |
| 436 * less than \p n. |
426 * less than @p n. |
| 437 * |
427 * |
| 438 * If this list is storing pointers instead of objects \p array is expected to |
428 * If this list is storing pointers instead of objects @p array is expected to |
| 439 * be an array of pointers. |
429 * be an array of pointers. |
| 440 * |
430 * |
| 441 * @param list the list |
431 * @param list the list |
| 442 * @param index the index where to add the elements |
432 * @param index the index where to add the elements |
| 443 * @param array a pointer to the elements to add |
433 * @param array a pointer to the elements to add |
| 459 * |
449 * |
| 460 * This method is usually more efficient than inserting each element separately, |
450 * This method is usually more efficient than inserting each element separately, |
| 461 * because consecutive chunks of sorted data are inserted in one pass. |
451 * because consecutive chunks of sorted data are inserted in one pass. |
| 462 * |
452 * |
| 463 * If there is not enough memory to add all elements, the returned value is |
453 * If there is not enough memory to add all elements, the returned value is |
| 464 * less than \p n. |
454 * less than @p n. |
| 465 * |
455 * |
| 466 * If this list is storing pointers instead of objects \p array is expected to |
456 * If this list is storing pointers instead of objects @p array is expected to |
| 467 * be an array of pointers. |
457 * be an array of pointers. |
| 468 * |
458 * |
| 469 * @param list the list |
459 * @param list the list |
| 470 * @param array a pointer to the elements to add |
460 * @param array a pointer to the elements to add |
| 471 * @param n the number of elements to add |
461 * @param n the number of elements to add |
| 484 * Inserts an element after the current location of the specified iterator. |
474 * Inserts an element after the current location of the specified iterator. |
| 485 * |
475 * |
| 486 * The used iterator remains operational, but all other active iterators should |
476 * The used iterator remains operational, but all other active iterators should |
| 487 * be considered invalidated. |
477 * be considered invalidated. |
| 488 * |
478 * |
| 489 * If \p iter is not a list iterator, the behavior is undefined. |
479 * If @p iter is not a list iterator, the behavior is undefined. |
| 490 * If \p iter is a past-the-end iterator, the new element gets appended to the list. |
480 * If @p iter is a past-the-end iterator, the new element gets appended to the list. |
| 491 * |
481 * |
| 492 * @param iter an iterator |
482 * @param iter an iterator |
| 493 * @param elem the element to insert |
483 * @param elem the element to insert |
| 494 * @return zero on success, non-zero on memory allocation failure |
484 * @retval zero success |
| |
485 * @retval non-zero memory allocation failure |
| 495 * @see cxListInsert() |
486 * @see cxListInsert() |
| 496 * @see cxListInsertBefore() |
487 * @see cxListInsertBefore() |
| 497 */ |
488 */ |
| 498 cx_attr_nonnull |
489 cx_attr_nonnull |
| 499 static inline int cxListInsertAfter( |
490 static inline int cxListInsertAfter( |
| 507 * Inserts an element before the current location of the specified iterator. |
498 * Inserts an element before the current location of the specified iterator. |
| 508 * |
499 * |
| 509 * The used iterator remains operational, but all other active iterators should |
500 * The used iterator remains operational, but all other active iterators should |
| 510 * be considered invalidated. |
501 * be considered invalidated. |
| 511 * |
502 * |
| 512 * If \p iter is not a list iterator, the behavior is undefined. |
503 * If @p iter is not a list iterator, the behavior is undefined. |
| 513 * If \p iter is a past-the-end iterator, the new element gets appended to the list. |
504 * If @p iter is a past-the-end iterator, the new element gets appended to the list. |
| 514 * |
505 * |
| 515 * @param iter an iterator |
506 * @param iter an iterator |
| 516 * @param elem the element to insert |
507 * @param elem the element to insert |
| 517 * @return zero on success, non-zero on memory allocation failure |
508 * @retval zero success |
| |
509 * @retval non-zero memory allocation failure |
| 518 * @see cxListInsert() |
510 * @see cxListInsert() |
| 519 * @see cxListInsertAfter() |
511 * @see cxListInsertAfter() |
| 520 */ |
512 */ |
| 521 cx_attr_nonnull |
513 cx_attr_nonnull |
| 522 static inline int cxListInsertBefore( |
514 static inline int cxListInsertBefore( |
| 546 |
539 |
| 547 /** |
540 /** |
| 548 * Removes and returns the element at the specified index. |
541 * Removes and returns the element at the specified index. |
| 549 * |
542 * |
| 550 * No destructor is called and instead the element is copied to the |
543 * No destructor is called and instead the element is copied to the |
| 551 * \p targetbuf which MUST be large enough to hold the removed element. |
544 * @p targetbuf which MUST be large enough to hold the removed element. |
| 552 * |
545 * |
| 553 * @param list the list |
546 * @param list the list |
| 554 * @param index the index of the element |
547 * @param index the index of the element |
| 555 * @param targetbuf a buffer where to copy the element |
548 * @param targetbuf a buffer where to copy the element |
| 556 * @return zero on success, non-zero if the index is out of bounds |
549 * @retval zero success |
| |
550 * @retval non-zero index out of bounds |
| 557 */ |
551 */ |
| 558 cx_attr_nonnull |
552 cx_attr_nonnull |
| 559 cx_attr_access_w(3) |
553 cx_attr_access_w(3) |
| 560 static inline int cxListRemoveAndGet( |
554 static inline int cxListRemoveAndGet( |
| 561 CxList *list, |
555 CxList *list, |
| 867 * First, the list sizes are compared. |
863 * First, the list sizes are compared. |
| 868 * If they match, the lists are compared element-wise. |
864 * If they match, the lists are compared element-wise. |
| 869 * |
865 * |
| 870 * @param list the list |
866 * @param list the list |
| 871 * @param other the list to compare to |
867 * @param other the list to compare to |
| 872 * @return zero, if both lists are equal element wise, |
868 * @retval zero both lists are equal element wise |
| 873 * negative if the first list is smaller, positive if the first list is larger |
869 * @retval negative the first list is smaller |
| |
870 * or the first non-equal element in the first list is smaller |
| |
871 * @retval positive the first list is larger |
| |
872 * or the first non-equal element in the first list is larger |
| 874 */ |
873 */ |
| 875 cx_attr_nonnull |
874 cx_attr_nonnull |
| 876 cx_attr_nodiscard |
875 cx_attr_nodiscard |
| 877 int cxListCompare( |
876 int cxListCompare( |
| 878 const CxList *list, |
877 const CxList *list, |