--- a/src/cx/array_list.h Thu Dec 18 12:26:25 2025 +0100 +++ b/src/cx/array_list.h Thu Dec 18 14:53:16 2025 +0100 @@ -397,17 +397,17 @@ * @param allocator the allocator to use for a possible reallocation * @param array a pointer to the array structure * @param elem_size the size of one element - * @param cmp_func * @param sorted_data a pointer to an array of data that shall be inserted * @param n the number of elements that shall be inserted + * @param cmp_func the compare function * @param allow_duplicates @c false if duplicates shall be skipped during insertion * @retval zero success * @retval non-zero a re-allocation was necessary but failed */ cx_attr_nonnull CX_EXPORT int cx_array_insert_sorted_(const CxAllocator *allocator, CxArray *array, - size_t elem_size, cx_compare_func cmp_func, const void *sorted_data, size_t n, - bool allow_duplicates); + size_t elem_size, const void *sorted_data, size_t n, + cx_compare_func cmp_func, bool allow_duplicates); /** * Inserts an element into a sorted array. @@ -418,13 +418,13 @@ * * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation * @param array the name of the array where the elements shall be inserted + * @param element (@c void*) a pointer to element that shall be inserted * @param cmp_func (@c cx_compare_func) the compare function that establishes the order - * @param element (@c void*) a pointer to element that shall be inserted * @retval zero success * @retval non-zero a re-allocation was necessary but failed */ -#define cx_array_insert_sorted_a(allocator, array, cmp_func, element) \ - cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), cmp_func, element, 1, true) +#define cx_array_insert_sorted_a(allocator, array, element, cmp_func) \ + cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), element, 1, cmp_func, true) /** * Inserts an element into a sorted array. @@ -434,13 +434,13 @@ * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined. * * @param array the name of the array where the elements shall be inserted + * @param element (@c void*) a pointer to element that shall be inserted * @param cmp_func (@c cx_compare_func) the compare function that establishes the order - * @param element (@c void*) a pointer to element that shall be inserted * @retval zero success * @retval non-zero a re-allocation was necessary but failed */ -#define cx_array_insert_sorted(array, cmp_func, element) \ - cx_array_insert_sorted_a(cxDefaultAllocator, array, cmp_func, element) +#define cx_array_insert_sorted(array, element, cmp_func) \ + cx_array_insert_sorted_a(cxDefaultAllocator, array, element, cmp_func) /** * Inserts sorted data into a sorted array. @@ -451,14 +451,14 @@ * * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation * @param array the name of the array where the elements shall be inserted - * @param cmp_func (@c cx_compare_func) the compare function that establishes the order * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted * @param n (@c size_t) the number of elements that shall be inserted + * @param cmp_func (@c cx_compare_func) the compare function that establishes the order * @retval zero success * @retval non-zero a re-allocation was necessary but failed */ -#define cx_array_insert_sorted_array_a(allocator, array, cmp_func, sorted_data, n) \ - cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), cmp_func, sorted_data, n, true) +#define cx_array_insert_sorted_array_a(allocator, array, sorted_data, n, cmp_func) \ + cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), sorted_data, n, cmp_func, true) /** * Inserts sorted data into a sorted array. @@ -468,14 +468,14 @@ * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. * * @param array the name of the array where the elements shall be inserted - * @param cmp_func (@c cx_compare_func) the compare function that establishes the order * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted * @param n (@c size_t) the number of elements that shall be inserted + * @param cmp_func (@c cx_compare_func) the compare function that establishes the order * @retval zero success * @retval non-zero a re-allocation was necessary but failed */ -#define cx_array_insert_sorted_array(array, cmp_func, sorted_data, n) \ - cx_array_insert_sorted_array_a(cxDefaultAllocator, array, cmp_func, sorted_data, n) +#define cx_array_insert_sorted_array(array, sorted_data, n, cmp_func) \ + cx_array_insert_sorted_array_a(cxDefaultAllocator, array, sorted_data, n, cmp_func) /** * Inserts an element into a sorted array if it is not already contained. @@ -486,13 +486,13 @@ * * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation * @param array the name of the array where the elements shall be inserted + * @param element (@c void*) a pointer to element that shall be inserted * @param cmp_func (@c cx_compare_func) the compare function that establishes the order - * @param element (@c void*) a pointer to element that shall be inserted * @retval zero success * @retval non-zero a re-allocation was necessary but failed */ -#define cx_array_insert_unique_a(allocator, array, cmp_func, element) \ - cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), cmp_func, element, 1, false) +#define cx_array_insert_unique_a(allocator, array, element, cmp_func) \ + cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), element, 1, cmp_func, false) /** * Inserts an element into a sorted array if it is not already contained. @@ -502,13 +502,13 @@ * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined. * * @param array the name of the array where the elements shall be inserted + * @param element (@c void*) a pointer to element that shall be inserted * @param cmp_func (@c cx_compare_func) the compare function that establishes the order - * @param element (@c void*) a pointer to element that shall be inserted * @retval zero success * @retval non-zero a re-allocation was necessary but failed */ -#define cx_array_insert_unique(array, cmp_func, element) \ - cx_array_insert_unique_a(cxDefaultAllocator, array, cmp_func, element) +#define cx_array_insert_unique(array, element, cmp_func) \ + cx_array_insert_unique_a(cxDefaultAllocator, array, element, cmp_func) /** * Inserts sorted data into a sorted array, skipping duplicates. @@ -519,14 +519,14 @@ * * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation * @param array the name of the array where the elements shall be inserted - * @param cmp_func (@c cx_compare_func) the compare function that establishes the order * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted * @param n (@c size_t) the number of elements that shall be inserted + * @param cmp_func (@c cx_compare_func) the compare function that establishes the order * @retval zero success * @retval non-zero a re-allocation was necessary but failed */ -#define cx_array_insert_unique_array_a(allocator, array, cmp_func, sorted_data, n) \ - cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), cmp_func, sorted_data, n, false) +#define cx_array_insert_unique_array_a(allocator, array, sorted_data, n, cmp_func) \ + cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), sorted_data, n, cmp_func, false) /** * Inserts sorted data into a sorted array, skipping duplicates. @@ -536,14 +536,179 @@ * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. * * @param array the name of the array where the elements shall be inserted - * @param cmp_func (@c cx_compare_func) the compare function that establishes the order * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted * @param n (@c size_t) the number of elements that shall be inserted + * @param cmp_func (@c cx_compare_func) the compare function that establishes the order + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_insert_unique_array(array, sorted_data, n, cmp_func) \ + cx_array_insert_unique_array_a(cxDefaultAllocator, array, sorted_data, n, cmp_func) + +/** + * Inserts sorted data into a sorted array. + * + * Internal function - do not use. + * + * @param allocator the allocator to use for a possible reallocation + * @param array a pointer to the array structure + * @param elem_size the size of one element + * @param sorted_data a pointer to an array of data that shall be inserted + * @param n the number of elements that shall be inserted + * @param cmp_func the compare function + * @param context additional context for the compare function + * @param allow_duplicates @c false if duplicates shall be skipped during insertion + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +cx_attr_nonnull +CX_EXPORT int cx_array_insert_sorted_c_(const CxAllocator *allocator, CxArray *array, + size_t elem_size, const void *sorted_data, size_t n, + cx_compare_func2 cmp_func, void *context, bool allow_duplicates); + +/** + * Inserts an element into a sorted array. + * + * When the capacity is not enough to hold the new element, a re-allocation is attempted. + * + * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined. + * + * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation + * @param array the name of the array where the elements shall be inserted + * @param element (@c void*) a pointer to element that shall be inserted + * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order + * @param context (@c void*) additional context for the compare function + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_insert_sorted_ca(allocator, array, element, cmp_func) \ + cx_array_insert_sorted_c_(allocator, (CxArray*)&(array), sizeof((array).data[0]), element, 1, cmp_func, context, true) + +/** + * Inserts an element into a sorted array. + * + * When the capacity is not enough to hold the new element, a re-allocation is attempted. + * + * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined. + * + * @param array the name of the array where the elements shall be inserted + * @param element (@c void*) a pointer to element that shall be inserted + * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order + * @param context (@c void*) additional context for the compare function + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_insert_sorted_c(array, element, cmp_func, context) \ + cx_array_insert_sorted_ca(cxDefaultAllocator, array, element, cmp_func, context) + +/** + * Inserts sorted data into a sorted array. + * + * When the capacity is not enough to hold the new elements, a re-allocation is attempted. + * + * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. + * + * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation + * @param array the name of the array where the elements shall be inserted + * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted + * @param n (@c size_t) the number of elements that shall be inserted + * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order + * @param context (@c void*) additional context for the compare function * @retval zero success * @retval non-zero a re-allocation was necessary but failed */ -#define cx_array_insert_unique_array(array, cmp_func, sorted_data, n) \ - cx_array_insert_unique_array_a(cxDefaultAllocator, array, cmp_func, sorted_data, n) +#define cx_array_insert_sorted_array_ca(allocator, array, sorted_data, n, cmp_func, context) \ + cx_array_insert_sorted_c_(allocator, (CxArray*)&(array), sizeof((array).data[0]), sorted_data, n, cmp_func, context, true) + +/** + * Inserts sorted data into a sorted array. + * + * When the capacity is not enough to hold the new elements, a re-allocation is attempted. + * + * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. + * + * @param array the name of the array where the elements shall be inserted + * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted + * @param n (@c size_t) the number of elements that shall be inserted + * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order + * @param context (@c void*) additional context for the compare function + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_insert_sorted_array_c(array, sorted_data, n, cmp_func, context) \ + cx_array_insert_sorted_array_ca(cxDefaultAllocator, array, sorted_data, n, cmp_func, context) + +/** + * Inserts an element into a sorted array if it is not already contained. + * + * When the capacity is not enough to hold the new element, a re-allocation is attempted. + * + * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined. + * + * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation + * @param array the name of the array where the elements shall be inserted + * @param element (@c void*) a pointer to element that shall be inserted + * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order + * @param context (@c void*) additional context for the compare function + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_insert_unique_ca(allocator, array, element, cmp_func, context) \ + cx_array_insert_sorted_c_(allocator, (CxArray*)&(array), sizeof((array).data[0]), element, 1, cmp_func, context, false) + +/** + * Inserts an element into a sorted array if it is not already contained. + * + * When the capacity is not enough to hold the new element, a re-allocation is attempted. + * + * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined. + * + * @param array the name of the array where the elements shall be inserted + * @param element (@c void*) a pointer to element that shall be inserted + * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order + * @param context (@c void*) additional context for the compare function + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_insert_unique_c(array, element, cmp_func, context) \ + cx_array_insert_unique_ca(cxDefaultAllocator, array, element, cmp_func, context) + +/** + * Inserts sorted data into a sorted array, skipping duplicates. + * + * When the capacity is not enough to hold the new elements, a re-allocation is attempted. + * + * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. + * + * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation + * @param array the name of the array where the elements shall be inserted + * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted + * @param n (@c size_t) the number of elements that shall be inserted + * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order + * @param context (@c void*) additional context for the compare function + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_insert_unique_array_ca(allocator, array, sorted_data, n, cmp_func, context) \ + cx_array_insert_sorted_c_(allocator, (CxArray*)&(array), sizeof((array).data[0]), sorted_data, n, cmp_func, context, false) + +/** + * Inserts sorted data into a sorted array, skipping duplicates. + * + * When the capacity is not enough to hold the new elements, a re-allocation is attempted. + * + * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. + * + * @param array the name of the array where the elements shall be inserted + * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted + * @param n (@c size_t) the number of elements that shall be inserted + * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order + * @param context (@c void*) additional context for the compare function + * @retval zero success + * @retval non-zero a re-allocation was necessary but failed + */ +#define cx_array_insert_unique_array_c(array, sorted_data, n, cmp_func, context) \ + cx_array_insert_unique_array_ca(cxDefaultAllocator, array, sorted_data, n, cmp_func, context) /** * An alternative to qsort_r() when that is not available on your platform.