584 * @see cx_array_simple_insert_sorted_a() |
584 * @see cx_array_simple_insert_sorted_a() |
585 */ |
585 */ |
586 #define cx_array_simple_insert_sorted(array, src, n, cmp_func) \ |
586 #define cx_array_simple_insert_sorted(array, src, n, cmp_func) \ |
587 cx_array_simple_insert_sorted_a(NULL, array, src, n, cmp_func) |
587 cx_array_simple_insert_sorted_a(NULL, array, src, n, cmp_func) |
588 |
588 |
|
589 |
|
590 /** |
|
591 * Inserts a sorted array into another sorted array, avoiding duplicates. |
|
592 * |
|
593 * If either the target or the source array is not already sorted with respect |
|
594 * to the specified @p cmp_func, the behavior is undefined. |
|
595 * Also, the @p src array must not contain duplicates within itself. |
|
596 * |
|
597 * If the capacity is insufficient to hold the new data, a reallocation |
|
598 * attempt is made. |
|
599 * You can create your own reallocator by hand, use #cx_array_default_reallocator, |
|
600 * or use the convenience function cx_array_reallocator() to create a custom reallocator. |
|
601 * |
|
602 * @param target a pointer to the target array |
|
603 * @param size a pointer to the size of the target array |
|
604 * @param capacity a pointer to the capacity of the target array |
|
605 * @param cmp_func the compare function for the elements |
|
606 * @param src the source array |
|
607 * @param elem_size the size of one element |
|
608 * @param elem_count the number of elements to insert |
|
609 * @param reallocator the array reallocator to use |
|
610 * (@c NULL defaults to #cx_array_default_reallocator) |
|
611 * @retval zero success |
|
612 * @retval non-zero failure |
|
613 */ |
|
614 cx_attr_nonnull_arg(1, 2, 3, 5) |
|
615 cx_attr_export |
|
616 int cx_array_insert_unique( |
|
617 void **target, |
|
618 size_t *size, |
|
619 size_t *capacity, |
|
620 cx_compare_func cmp_func, |
|
621 const void *src, |
|
622 size_t elem_size, |
|
623 size_t elem_count, |
|
624 CxArrayReallocator *reallocator |
|
625 ); |
|
626 |
|
627 /** |
|
628 * Inserts an element into a sorted array if it does not exist. |
|
629 * |
|
630 * If the target array is not already sorted with respect |
|
631 * to the specified @p cmp_func, the behavior is undefined. |
|
632 * |
|
633 * If the capacity is insufficient to hold the new data, a reallocation |
|
634 * attempt is made. |
|
635 * |
|
636 * The \@ SIZE_TYPE is flexible and can be any unsigned integer type. |
|
637 * It is important, however, that @p size and @p capacity are pointers to |
|
638 * variables of the same type. |
|
639 * |
|
640 * @param target (@c void**) a pointer to the target array |
|
641 * @param size (@c SIZE_TYPE*) a pointer to the size of the target array |
|
642 * @param capacity (@c SIZE_TYPE*) a pointer to the capacity of the target array |
|
643 * @param elem_size (@c size_t) the size of one element |
|
644 * @param elem (@c void*) a pointer to the element to add |
|
645 * @param cmp_func (@c cx_cmp_func) the compare function for the elements |
|
646 * @param reallocator (@c CxArrayReallocator*) the array reallocator to use |
|
647 * @retval zero success (also when the element was already present) |
|
648 * @retval non-zero failure |
|
649 */ |
|
650 #define cx_array_add_unique(target, size, capacity, elem_size, elem, cmp_func, reallocator) \ |
|
651 cx_array_insert_unique((void**)(target), size, capacity, cmp_func, elem, elem_size, 1, reallocator) |
|
652 |
|
653 /** |
|
654 * Convenience macro for cx_array_add_unique() with a default |
|
655 * layout and the specified reallocator. |
|
656 * |
|
657 * @param reallocator (@c CxArrayReallocator*) the array reallocator to use |
|
658 * @param array the name of the array (NOT a pointer or alias to the array) |
|
659 * @param elem the element to add (NOT a pointer, address is automatically taken) |
|
660 * @param cmp_func (@c cx_cmp_func) the compare function for the elements |
|
661 * @retval zero success |
|
662 * @retval non-zero failure |
|
663 * @see CX_ARRAY_DECLARE() |
|
664 * @see cx_array_simple_add_unique() |
|
665 */ |
|
666 #define cx_array_simple_add_unique_a(reallocator, array, elem, cmp_func) \ |
|
667 cx_array_add_unique(&array, &(array##_size), &(array##_capacity), \ |
|
668 sizeof((array)[0]), &(elem), cmp_func, reallocator) |
|
669 |
|
670 /** |
|
671 * Convenience macro for cx_array_add_unique() with a default |
|
672 * layout and the default reallocator. |
|
673 * |
|
674 * @param array the name of the array (NOT a pointer or alias to the array) |
|
675 * @param elem the element to add (NOT a pointer, address is automatically taken) |
|
676 * @param cmp_func (@c cx_cmp_func) the compare function for the elements |
|
677 * @retval zero success |
|
678 * @retval non-zero failure |
|
679 * @see CX_ARRAY_DECLARE() |
|
680 * @see cx_array_simple_add_unique_a() |
|
681 */ |
|
682 #define cx_array_simple_add_unique(array, elem, cmp_func) \ |
|
683 cx_array_simple_add_unique_a(NULL, array, elem, cmp_func) |
|
684 |
|
685 /** |
|
686 * Convenience macro for cx_array_insert_unique() with a default |
|
687 * layout and the specified reallocator. |
|
688 * |
|
689 * @param reallocator (@c CxArrayReallocator*) the array reallocator to use |
|
690 * @param array the name of the array (NOT a pointer or alias to the array) |
|
691 * @param src (@c void*) pointer to the source array |
|
692 * @param n (@c size_t) number of elements in the source array |
|
693 * @param cmp_func (@c cx_cmp_func) the compare function for the elements |
|
694 * @retval zero success |
|
695 * @retval non-zero failure |
|
696 * @see CX_ARRAY_DECLARE() |
|
697 * @see cx_array_simple_insert_unique() |
|
698 */ |
|
699 #define cx_array_simple_insert_unique_a(reallocator, array, src, n, cmp_func) \ |
|
700 cx_array_insert_unique((void**)(&array), &(array##_size), &(array##_capacity), \ |
|
701 cmp_func, src, sizeof((array)[0]), n, reallocator) |
|
702 |
|
703 /** |
|
704 * Convenience macro for cx_array_insert_unique() with a default |
|
705 * layout and the default reallocator. |
|
706 * |
|
707 * @param array the name of the array (NOT a pointer or alias to the array) |
|
708 * @param src (@c void*) pointer to the source array |
|
709 * @param n (@c size_t) number of elements in the source array |
|
710 * @param cmp_func (@c cx_cmp_func) the compare function for the elements |
|
711 * @retval zero success |
|
712 * @retval non-zero failure |
|
713 * @see CX_ARRAY_DECLARE() |
|
714 * @see cx_array_simple_insert_unique_a() |
|
715 */ |
|
716 #define cx_array_simple_insert_unique(array, src, n, cmp_func) \ |
|
717 cx_array_simple_insert_unique_a(NULL, array, src, n, cmp_func) |
|
718 |
589 /** |
719 /** |
590 * Searches the largest lower bound in a sorted array. |
720 * Searches the largest lower bound in a sorted array. |
591 * |
721 * |
592 * In other words, this function returns the index of the largest element |
722 * In other words, this function returns the index of the largest element |
593 * in @p arr that is less or equal to @p elem with respect to @p cmp_func. |
723 * in @p arr that is less or equal to @p elem with respect to @p cmp_func. |