tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
upper_bound.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_ALGORITHM_UPPER_BOUND_HPP
4#define TETL_ALGORITHM_UPPER_BOUND_HPP
5
10
11namespace etl {
12
15
23template <typename ForwardIt, typename T, typename Compare>
24[[nodiscard]] constexpr auto upper_bound(ForwardIt first, ForwardIt last, T const& value, Compare comp) -> ForwardIt
25{
26 auto count = etl::distance(first, last);
27 while (count > 0) {
28 auto it = first;
29 auto step = count / 2;
30 etl::advance(it, step);
31 if (not comp(value, *it)) {
32 first = ++it;
33 count -= step + 1;
34 } else {
35 count = step;
36 }
37 }
38
39 return first;
40}
41
42template <typename ForwardIt, typename T>
43[[nodiscard]] constexpr auto upper_bound(ForwardIt first, ForwardIt last, T const& value) -> ForwardIt
44{
45 return etl::upper_bound(first, last, value, etl::less());
46}
47
49
50} // namespace etl
51
52#endif // TETL_ALGORITHM_UPPER_BOUND_HPP
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 upper_bound(ForwardIt first, ForwardIt last, T const &value, Compare comp) -> ForwardIt
Returns an iterator pointing to the first element in the range [first, last) that is greater than val...
Definition upper_bound.hpp:24
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
Function object for performing comparisons. Unless specialised, invokes operator< on type T....
Definition less.hpp:14