tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
binary_search.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_ALGORITHM_BINARY_SEARCH_HPP
5#define TETL_ALGORITHM_BINARY_SEARCH_HPP
6
7#include <etl/_algorithm/lower_bound.hpp>
8#include <etl/_functional/less.hpp>
9
10namespace etl {
11
12/// \ingroup algorithm
13///@{
14
15/// Checks if an element equivalent to value appears within the range
16/// `[first, last)`. For binary_search to succeed, the range `[first, last)`
17/// must be at least partially ordered with respect to `value`.
18///
19/// https://en.cppreference.com/w/cpp/algorithm/binary_search
20template <typename ForwardIt, typename T, typename Compare>
21[[nodiscard]] constexpr auto binary_search(ForwardIt first, ForwardIt last, T const& value, Compare comp) -> bool
22{
23 first = etl::lower_bound(first, last, value, comp);
24 return first != last and not comp(value, *first);
25}
26
27template <typename ForwardIt, typename T>
28[[nodiscard]] constexpr auto binary_search(ForwardIt first, ForwardIt last, T const& value) -> bool
29{
30 return etl::binary_search(first, last, value, etl::less());
31}
32
33///@}
34
35} // namespace etl
36
37#endif // TETL_ALGORITHM_BINARY_SEARCH_HPP
constexpr auto binary_search(ForwardIt first, ForwardIt last, T const &value, Compare comp) -> bool
Definition binary_search.hpp:21
constexpr auto binary_search(ForwardIt first, ForwardIt last, T const &value) -> bool
Definition binary_search.hpp:28
Definition adjacent_find.hpp:9
Function object for performing comparisons. Unless specialised, invokes operator< on type T....
Definition less.hpp:15