113 const void *sorted_data, |
113 const void *sorted_data, |
114 size_t n |
114 size_t n |
115 ); |
115 ); |
116 |
116 |
117 /** |
117 /** |
|
118 * Member function for inserting multiple elements if they do not exist. |
|
119 * |
|
120 * @see cx_list_default_insert_unique() |
|
121 */ |
|
122 size_t (*insert_unique)( |
|
123 struct cx_list_s *list, |
|
124 const void *sorted_data, |
|
125 size_t n |
|
126 ); |
|
127 |
|
128 /** |
118 * Member function for inserting an element relative to an iterator position. |
129 * Member function for inserting an element relative to an iterator position. |
119 */ |
130 */ |
120 int (*insert_iter)( |
131 int (*insert_iter)( |
121 struct cx_iterator_s *iter, |
132 struct cx_iterator_s *iter, |
122 const void *elem, |
133 const void *elem, |
261 * @return the number of elements actually inserted |
272 * @return the number of elements actually inserted |
262 */ |
273 */ |
263 cx_attr_nonnull |
274 cx_attr_nonnull |
264 cx_attr_export |
275 cx_attr_export |
265 size_t cx_list_default_insert_sorted( |
276 size_t cx_list_default_insert_sorted( |
|
277 struct cx_list_s *list, |
|
278 const void *sorted_data, |
|
279 size_t n |
|
280 ); |
|
281 |
|
282 /** |
|
283 * Default implementation of an array insert where only elements are inserted when they don't exist in the list. |
|
284 * |
|
285 * This function is similar to cx_list_default_insert_sorted(), except it skips elements that are already in the list. |
|
286 * The @p sorted_data itself must not contain duplicates. |
|
287 * |
|
288 * @note The return value of this function denotes the number of elements from the @p sorted_data that are definitely |
|
289 * contained in the list after completing the call. It is @em not the number of elements that were newly inserted. |
|
290 * That means, when no error occurred, the return value should be @p n. |
|
291 * |
|
292 * Use this in your own list class if you do not want to implement an optimized version for your list. |
|
293 * |
|
294 * @param list the list |
|
295 * @param sorted_data a pointer to the array of pre-sorted data to insert |
|
296 * @param n the number of elements to insert |
|
297 * @return the number of elements from the @p sorted_data that are definitely present in the list after this call |
|
298 */ |
|
299 cx_attr_nonnull |
|
300 cx_attr_export |
|
301 size_t cx_list_default_insert_unique( |
266 struct cx_list_s *list, |
302 struct cx_list_s *list, |
267 const void *sorted_data, |
303 const void *sorted_data, |
268 size_t n |
304 size_t n |
269 ); |
305 ); |
270 |
306 |
488 const void *data = list->collection.store_pointer ? &elem : elem; |
524 const void *data = list->collection.store_pointer ? &elem : elem; |
489 return list->cl->insert_sorted(list, data, 1) == 0; |
525 return list->cl->insert_sorted(list, data, 1) == 0; |
490 } |
526 } |
491 |
527 |
492 /** |
528 /** |
|
529 * Inserts an item into a sorted list if it does not exist. |
|
530 * |
|
531 * If the list is not sorted already, the behavior is undefined. |
|
532 * |
|
533 * @param list the list |
|
534 * @param elem a pointer to the element to add |
|
535 * @retval zero success (also when the element was already in the list) |
|
536 * @retval non-zero memory allocation failure |
|
537 */ |
|
538 cx_attr_nonnull |
|
539 static inline int cxListInsertUnique( |
|
540 CxList *list, |
|
541 const void *elem |
|
542 ) { |
|
543 assert(list->collection.sorted || list->collection.size == 0); |
|
544 list->collection.sorted = true; |
|
545 const void *data = list->collection.store_pointer ? &elem : elem; |
|
546 return list->cl->insert_unique(list, data, 1) == 0; |
|
547 } |
|
548 |
|
549 /** |
493 * Inserts multiple items to the list at the specified index. |
550 * Inserts multiple items to the list at the specified index. |
494 * If @p index equals the list size, this is effectively cxListAddArray(). |
551 * If @p index equals the list size, this is effectively cxListAddArray(). |
495 * |
552 * |
496 * This method is usually more efficient than invoking cxListInsert() |
553 * This method is usually more efficient than invoking cxListInsert() |
497 * multiple times. |
554 * multiple times. |
520 } |
577 } |
521 |
578 |
522 /** |
579 /** |
523 * Inserts a sorted array into a sorted list. |
580 * Inserts a sorted array into a sorted list. |
524 * |
581 * |
525 * This method is usually more efficient than inserting each element separately, |
582 * This method is usually more efficient than inserting each element separately |
526 * because consecutive chunks of sorted data are inserted in one pass. |
583 * because consecutive chunks of sorted data are inserted in one pass. |
527 * |
584 * |
528 * If there is not enough memory to add all elements, the returned value is |
585 * If there is not enough memory to add all elements, the returned value is |
529 * less than @p n. |
586 * less than @p n. |
530 * |
587 * |
545 size_t n |
602 size_t n |
546 ) { |
603 ) { |
547 assert(list->collection.sorted || list->collection.size == 0); |
604 assert(list->collection.sorted || list->collection.size == 0); |
548 list->collection.sorted = true; |
605 list->collection.sorted = true; |
549 return list->cl->insert_sorted(list, array, n); |
606 return list->cl->insert_sorted(list, array, n); |
|
607 } |
|
608 |
|
609 /** |
|
610 * Inserts a sorted array into a sorted list, skipping duplicates. |
|
611 * |
|
612 * This method is usually more efficient than inserting each element separately |
|
613 * because consecutive chunks of sorted data are inserted in one pass. |
|
614 * |
|
615 * If there is not enough memory to add all elements, the returned value is |
|
616 * less than @p n. |
|
617 * |
|
618 * @note The return value of this function denotes the number of elements |
|
619 * from the @p sorted_data that are definitely contained in the list after |
|
620 * completing the call. It is @em not the number of elements that were newly |
|
621 * inserted. That means, when no error occurred, the return value should |
|
622 * be @p n. |
|
623 * |
|
624 * If this list is storing pointers instead of objects @p array is expected to |
|
625 * be an array of pointers. |
|
626 * |
|
627 * If the list is not sorted already, the behavior is undefined. |
|
628 * This is also the case when the @p array is not sorted or already contains duplicates. |
|
629 * |
|
630 * @param list the list |
|
631 * @param array a pointer to the elements to add |
|
632 * @param n the number of elements to add |
|
633 * @return the number of added elements |
|
634 * |
|
635 * @return the number of elements from the @p sorted_data that are definitely present in the list after this call |
|
636 */ |
|
637 cx_attr_nonnull |
|
638 static inline size_t cxListInsertUniqueArray( |
|
639 CxList *list, |
|
640 const void *array, |
|
641 size_t n |
|
642 ) { |
|
643 assert(list->collection.sorted || list->collection.size == 0); |
|
644 list->collection.sorted = true; |
|
645 return list->cl->insert_unique(list, array, n); |
550 } |
646 } |
551 |
647 |
552 /** |
648 /** |
553 * Inserts an element after the current location of the specified iterator. |
649 * Inserts an element after the current location of the specified iterator. |
554 * |
650 * |