tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
equal.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_EQUAL_HPP
5#define TETL_ALGORITHM_EQUAL_HPP
6
7#include <etl/_functional/equal_to.hpp>
8#include <etl/_iterator/distance.hpp>
9#include <etl/_iterator/iterator_traits.hpp>
10#include <etl/_iterator/tags.hpp>
11#include <etl/_type_traits/is_base_of.hpp>
12
13namespace etl {
14
15/// \brief Returns true if the range `[first1, last1)` is equal to the range
16/// `[first2, first2 + (last1 - first1))`, and false otherwise.
17/// \ingroup algorithm
18template <typename InputIt1, typename InputIt2, typename Predicate>
19[[nodiscard]] constexpr auto equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, Predicate p) -> bool
20{
21 for (; first1 != last1; ++first1, (void)++first2) {
22 if (not p(*first1, *first2)) {
23 return false;
24 }
25 }
26 return true;
27}
28
29/// \ingroup algorithm
30template <typename InputIt1, typename InputIt2>
31[[nodiscard]] constexpr auto equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) -> bool
32{
33 return etl::equal(first1, last1, first2, etl::equal_to());
34}
35
36/// \ingroup algorithm
37template <typename InputIt1, typename InputIt2, typename Predicate>
38[[nodiscard]] constexpr auto equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Predicate p)
39 -> bool
40{
42 using Category1 = typename etl::iterator_traits<InputIt1>::iterator_category;
43 using Category2 = typename etl::iterator_traits<InputIt2>::iterator_category;
44
45 if constexpr (etl::is_base_of_v<Tag, Category1> and etl::is_base_of_v<Tag, Category2>) {
46 if (etl::distance(first1, last1) != etl::distance(first2, last2)) {
47 return false;
48 }
49 }
50 return etl::equal(first1, last1, first2, p);
51}
52
53/// \ingroup algorithm
54template <typename InputIt1, typename InputIt2>
55[[nodiscard]] constexpr auto equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) -> bool
56{
57 return etl::equal(first1, last1, first2, last2, etl::equal_to());
58}
59
60} // namespace etl
61
62#endif // TETL_ALGORITHM_EQUAL_HPP
constexpr auto equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) -> bool
Definition equal.hpp:31
constexpr auto equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) -> bool
Definition equal.hpp:55
constexpr auto equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Predicate p) -> bool
Definition equal.hpp:38
constexpr auto equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, Predicate p) -> bool
Returns true if the range [first1, last1) is equal to the range [first2, first2 + (last1 - first1)),...
Definition equal.hpp:19
Definition adjacent_find.hpp:9
Function object for performing comparisons. Unless specialised, invokes operator== on type T....
Definition equal_to.hpp:15
iterator_traits is the type trait class that provides uniform interface to the properties of LegacyIt...
Definition iterator_traits.hpp:48
Defines the category of an iterator. Each tag is an empty type and corresponds to one of the five (un...
Definition tags.hpp:37