MCPcopy Create free account
hub / github.com/KaisenAmin/c_std / algorithm_upper_bound

Function algorithm_upper_bound

algorithm/algorithm.c:930–950  ·  view source on GitHub ↗

* @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.

Source from the content-addressed store, hash-verified

928 * @return Pointer to the first position where elements are greater than `val`.
929 */
930void *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/**

Callers 1

algorithm_equal_rangeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected