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

Function algorithm_lower_bound

algorithm/algorithm.c:891–911  ·  view source on GitHub ↗

* @brief Finds the first position in a sorted range where a value can be inserted without violating the order. * * This function performs a binary search on a sorted range `[base, base + num * size)` to find the first * position where `val` can be inserted while maintaining the sorted order. The range must be sorted * according to the same comparison function `comp`. * * @param base Pointer

Source from the content-addressed store, hash-verified

889 * @return Pointer to the first position where `val` can be inserted without violating the order.
890 */
891void *algorithm_lower_bound(const void *base, size_t num, size_t size, const void *val, CompareFunc comp) {
892 ALGORITHM_LOG("[algorithm_lower_bound] Info: Performing lower bound search in array of %zu elements.", num);
893
894 size_t low = 0;
895 size_t high = num;
896
897 while (low < high) {
898 size_t mid = low + (high - low) / 2;
899 const void *mid_elem = (const char *)base + mid * size;
900
901 if (comp(mid_elem, val) < 0) {
902 low = mid + 1;
903 }
904 else {
905 high = mid;
906 }
907 }
908
909 ALGORITHM_LOG("[algorithm_lower_bound] Success: Found lower bound at index %zu.", low);
910 return (void *)((const char *)base + low * size);
911}
912
913
914/**

Callers 1

algorithm_equal_rangeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected