* @brief Merges two sorted halves of an array in place. * * @param base Pointer to the start of the array. * @param middle Index of the middle element. * @param num Number of elements in the array. * @param size Size of each element in bytes. * @param comp Comparison function used to order the elements. */
| 1747 | void algorithm_shuffle(void *base, size_t num, size_t size, UniformRandomNumberGenerator rng) { |
| 1748 | ALGORITHM_LOG("[algorithm_shuffle] Info: Shuffling array of %zu elements.", num); |
| 1749 | |
| 1750 | if (num < 2 || !base || size == 0 || !rng) { |
| 1751 | return; |
| 1752 | } |
| 1753 | char *arr = (char *)base; |
| 1754 | /* Fisher-Yates, index rng() % (i + 1) as before, minus its modulo bias */ |
| 1755 | for (size_t i = num - 1; i > 0; i--) { |
| 1756 | size_t j = alg_random_index(rng, i + 1); |
| 1757 | if (j != i) { |
| 1758 | alg_swap_bytes(arr + i * size, arr + j * size, size); |
| 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | ALGORITHM_LOG("[algorithm_shuffle] Success: Shuffling completed."); |
| 1763 | } |
| 1764 | |
| 1765 | |
| 1766 | /** |
| 1767 | * @brief Finds the first position in a sorted range where a value can be inserted without violating the order. |
| 1768 | * |
| 1769 | * This function performs a binary search on a sorted range `[base, base + num * size)` to find the first |
| 1770 | * position where `val` can be inserted while maintaining the sorted order. The range must be sorted |
| 1771 | * according to the same comparison function `comp`. |
| 1772 | * |
| 1773 | * @param base Pointer to the start of the sorted array. |
| 1774 | * @param num Number of elements in the array. |
| 1775 | * @param size Size of each element in bytes. |
| 1776 | * @param val Pointer to the value to be inserted. |
| 1777 | * @param comp Comparison function used to compare elements. |
| 1778 | * It should return a negative value if the first element is less than the second, |
| 1779 | * zero if they are equal, and a positive value if the first element is greater than the second. |
| 1780 | * |
| 1781 | * @return Pointer to the first position where `val` can be inserted without violating the order. |
| 1782 | */ |
| 1783 | void *algorithm_lower_bound(const void *base, size_t num, size_t size, const void *val, CompareFunc comp) { |
| 1784 | ALGORITHM_LOG("[algorithm_lower_bound] Info: Performing lower bound search in array of %zu elements.", num); |
| 1785 | |
| 1786 | const char *b = (const char *)base; |
nothing calls this directly
no outgoing calls
no test coverage detected