|
| 1 | +#ifndef BINARY_SEARCH_HPP |
| 2 | +#define BINARY_SEARCH_HPP |
| 3 | + |
| 4 | +#include <concepts> |
| 5 | +#include <functional> |
| 6 | +#include <vector> |
| 7 | + |
| 8 | +namespace clavis { |
| 9 | +namespace search { |
| 10 | + |
| 11 | +/** |
| 12 | + * @brief Binary search algorithm for finding an element in a sorted array |
| 13 | + * |
| 14 | + * @tparam T Type of elements in the array (must be comparable) |
| 15 | + * @param arr Sorted array to search in |
| 16 | + * @param target Element to search for |
| 17 | + * @return Index of the target element if found, -1 otherwise |
| 18 | + */ |
| 19 | +template <typename T> |
| 20 | +requires std::totally_ordered<T> |
| 21 | +int binary_search(const std::vector<T>& arr, const T& target) { |
| 22 | + int left = 0; |
| 23 | + int right = arr.size() - 1; |
| 24 | + |
| 25 | + while (left <= right) { |
| 26 | + int mid = left + (right - left) / 2; // Avoid potential overflow |
| 27 | + |
| 28 | + if (arr[mid] == target) { |
| 29 | + return mid; // Found the target |
| 30 | + } else if (arr[mid] < target) { |
| 31 | + left = mid + 1; // Search in the right half |
| 32 | + } else { |
| 33 | + right = mid - 1; // Search in the left half |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return -1; // Target not found |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * @brief Lower bound binary search - finds the first element not less than the target |
| 42 | + * |
| 43 | + * @tparam T Type of elements in the array (must be comparable) |
| 44 | + * @param arr Sorted array to search in |
| 45 | + * @param target Element to search for |
| 46 | + * @return Index of the first element not less than target, or arr.size() if all elements are less than target |
| 47 | + */ |
| 48 | +template <typename T> |
| 49 | +requires std::totally_ordered<T> |
| 50 | +int lower_bound(const std::vector<T>& arr, const T& target) { |
| 51 | + int left = 0; |
| 52 | + int right = arr.size(); // Note: right is arr.size(), not arr.size() - 1 |
| 53 | + |
| 54 | + while (left < right) { |
| 55 | + int mid = left + (right - left) / 2; |
| 56 | + |
| 57 | + if (arr[mid] < target) { |
| 58 | + left = mid + 1; |
| 59 | + } else { |
| 60 | + right = mid; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return left; // Returns arr.size() if all elements are less than target |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * @brief Upper bound binary search - finds the first element greater than the target |
| 69 | + * |
| 70 | + * @tparam T Type of elements in the array (must be comparable) |
| 71 | + * @param arr Sorted array to search in |
| 72 | + * @param target Element to search for |
| 73 | + * @return Index of the first element greater than target, or arr.size() if no such element exists |
| 74 | + */ |
| 75 | +template <typename T> |
| 76 | +requires std::totally_ordered<T> |
| 77 | +int upper_bound(const std::vector<T>& arr, const T& target) { |
| 78 | + int left = 0; |
| 79 | + int right = arr.size(); // Note: right is arr.size(), not arr.size() - 1 |
| 80 | + |
| 81 | + while (left < right) { |
| 82 | + int mid = left + (right - left) / 2; |
| 83 | + |
| 84 | + if (arr[mid] <= target) { |
| 85 | + left = mid + 1; |
| 86 | + } else { |
| 87 | + right = mid; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return left; // Returns arr.size() if all elements are less than or equal to target |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * @brief Binary search on a predicate function |
| 96 | + * |
| 97 | + * @tparam T Type of elements in the search space |
| 98 | + * @param left Left boundary of the search space (inclusive) |
| 99 | + * @param right Right boundary of the search space (exclusive) |
| 100 | + * @param predicate Function that returns true for elements that satisfy the condition |
| 101 | + * @return The first element in the range [left, right) for which predicate returns true, or right if none exists |
| 102 | + */ |
| 103 | +template <typename T> |
| 104 | +requires std::integral<T> |
| 105 | +T binary_search_predicate(T left, T right, const std::function<bool(T)>& predicate) { |
| 106 | + while (left < right) { |
| 107 | + T mid = left + (right - left) / 2; |
| 108 | + |
| 109 | + if (predicate(mid)) { |
| 110 | + right = mid; |
| 111 | + } else { |
| 112 | + left = mid + 1; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return left; // Returns right if no element satisfies the predicate |
| 117 | +} |
| 118 | + |
| 119 | +} // namespace search |
| 120 | +} // namespace clavis |
| 121 | + |
| 122 | +#endif // BINARY_SEARCH_HPP |
0 commit comments