tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
lower_bound.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_ALGORITHM_LOWER_BOUND_HPP
4#define TETL_ALGORITHM_LOWER_BOUND_HPP
5
10
11namespace etl {
12
20template <typename ForwardIt, typename T, typename Compare>
21[[nodiscard]] constexpr auto lower_bound(ForwardIt first, ForwardIt last, T const& value, Compare comp) noexcept
22 -> ForwardIt
23{
25 ForwardIt it{};
26 diff_t count{};
27 diff_t step{};
28 count = etl::distance(first, last);
29
30 while (count > 0) {
31 it = first;
32 step = count / 2;
33 etl::advance(it, step);
34 if (comp(*it, value)) {
35 first = ++it;
36 count -= step + 1;
37 } else {
38 count = step;
39 }
40 }
41
42 return first;
43}
44
46template <typename ForwardIt, typename T>
47[[nodiscard]] constexpr auto lower_bound(ForwardIt first, ForwardIt last, T const& value) noexcept -> ForwardIt
48{
49 return etl::lower_bound(first, last, value, less());
50}
51
52} // namespace etl
53
54#endif // TETL_ALGORITHM_LOWER_BOUND_HPP
constexpr auto lower_bound(ForwardIt first, ForwardIt last, T const &value, Compare comp) noexcept -> ForwardIt
Returns an iterator pointing to the first element in the range [first, last) that is not less than (i...
Definition lower_bound.hpp:21
constexpr auto count(InputIt first, InputIt last, T const &value) -> typename iterator_traits< InputIt >::difference_type
Returns the number of elements in the range [first, last) satisfying specific criteria....
Definition count.hpp:21
constexpr auto advance(It &it, Distance n) -> void
Increments given iterator it by n elements.
Definition advance.hpp:22
constexpr auto distance(It first, It last) -> typename iterator_traits< It >::difference_type
Returns the number of hops from first to last.
Definition distance.hpp:16
Definition adjacent_find.hpp:8
typename etl::iterator_traits< etl::remove_cvref_t< Iter > >::difference_type diff_t
Definition format_to.hpp:13
iterator_traits is the type trait class that provides uniform interface to the properties of LegacyIt...
Definition iterator_traits.hpp:47
Function object for performing comparisons. Unless specialised, invokes operator< on type T....
Definition less.hpp:14