* @brief Removes consecutive duplicate elements from a sorted array. * * This function removes consecutive duplicate elements from a sorted array `base` as determined * by the comparison function `comp`. The array must be sorted before calling this function. * * @param base Pointer to the start of the array. * @param num Number of elements in the array. * @param size Size of each element in
| 1020 | |
| 1021 | /** |
| 1022 | * @brief Sorts an array with a stable merge sort: equal elements keep their |
| 1023 | * relative order (std::stable_sort). |
| 1024 | * |
| 1025 | * Bottom-up merge sort over insertion-sorted runs, merging between the array |
| 1026 | * and a buffer of the same size: cache-sized blocks first, equal-length runs |
| 1027 | * with parity merges (both ends at once, two run pairs in lockstep). Runs |
| 1028 | * already in order are concatenated, reversed ones swapped as blocks, and |
| 1029 | * sorted or strictly descending input is recognised in one pass (O(n)). |
| 1030 | * Element sizes 4, 8 and 16 use specialised copies. If the buffer cannot be |
| 1031 | * allocated the runs are merged in place with rotations (O(n log^2 n)) - the |
| 1032 | * array is always sorted. |
| 1033 | * |
| 1034 | * @param base Pointer to the start of the array. |
| 1035 | * @param num Number of elements in the array. |
| 1036 | * @param size Size of each element in bytes. |
| 1037 | * @param comp Comparison function (negative / zero / positive, like qsort). |
| 1038 | */ |
| 1039 | void algorithm_stable_sort(void *base, size_t num, size_t size, CompareFunc comp) { |
| 1040 | if (num < 2 || base == NULL || size == 0 || comp == NULL) { |
| 1041 | ALGORITHM_LOG("[algorithm_stable_sort] No sorting needed / invalid arguments."); |
| 1042 | return; |
| 1043 | } |
| 1044 | ALGORITHM_LOG("[algorithm_stable_sort] Starting stable sort for %zu elements.", num); |
| 1045 | |
| 1046 | char *a = (char *)base; |
| 1047 | AlgScratch stk; |
| 1048 | char *tmp = (size <= sizeof stk.bytes) ? (char *)stk.bytes : (char *)malloc(size); |
nothing calls this directly
no outgoing calls
no test coverage detected