* @brief Finds the first position in a sorted range where a value is greater than a specified value. * * This function performs a binary search on a sorted range `[base, base + num * size)` to find the first * position where the elements are greater than `val`. The range must be sorted according to the same comparison function `comp`. * * @param base Pointer to the start of the sorted array.
| 928 | * @return Pointer to the first position where elements are greater than `val`. |
| 929 | */ |
| 930 | void *algorithm_upper_bound(const void *base, size_t num, size_t size, const void *val, CompareFunc comp) { |
| 931 | ALGORITHM_LOG("[algorithm_upper_bound] Info: Performing upper bound search in array of %zu elements.", num); |
| 932 | |
| 933 | size_t low = 0; |
| 934 | size_t high = num; |
| 935 | |
| 936 | while (low < high) { |
| 937 | size_t mid = low + (high - low) / 2; |
| 938 | const void *mid_elem = (const char *)base + mid * size; |
| 939 | |
| 940 | if (comp(mid_elem, val) <= 0) { |
| 941 | low = mid + 1; |
| 942 | } |
| 943 | else { |
| 944 | high = mid; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | ALGORITHM_LOG("[algorithm_upper_bound] Success: Found upper bound at index %zu.", low); |
| 949 | return (void *)((const char *)base + low * size); |
| 950 | } |
| 951 | |
| 952 | |
| 953 | /** |
no outgoing calls
no test coverage detected