| 1 |
/// \file |
| 2 |
// Range v3 library |
| 3 |
// |
| 4 |
// Copyright Eric Niebler 2014-present |
| 5 |
// |
| 6 |
// Use, modification and distribution is subject to the |
| 7 |
// Boost Software License, Version 1.0. (See accompanying |
| 8 |
// file LICENSE_1_0.txt or copy at |
| 9 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 10 |
// |
| 11 |
// Project home: https://github.com/ericniebler/range-v3 |
| 12 |
// |
| 13 |
#ifndef RANGES_V3_ALGORITHM_EQUAL_HPP |
| 14 |
#define RANGES_V3_ALGORITHM_EQUAL_HPP |
| 15 |
|
| 16 |
#include <utility> |
| 17 |
|
| 18 |
#include <range/v3/range_fwd.hpp> |
| 19 |
|
| 20 |
#include <range/v3/functional/comparisons.hpp> |
| 21 |
#include <range/v3/functional/identity.hpp> |
| 22 |
#include <range/v3/functional/invoke.hpp> |
| 23 |
#include <range/v3/iterator/concepts.hpp> |
| 24 |
#include <range/v3/iterator/operations.hpp> |
| 25 |
#include <range/v3/iterator/traits.hpp> |
| 26 |
#include <range/v3/range/access.hpp> |
| 27 |
#include <range/v3/range/concepts.hpp> |
| 28 |
#include <range/v3/range/traits.hpp> |
| 29 |
#include <range/v3/utility/static_const.hpp> |
| 30 |
|
| 31 |
#include <range/v3/detail/prologue.hpp> |
| 32 |
|
| 33 |
namespace ranges |
| 34 |
{ |
| 35 |
/// \addtogroup group-algorithms |
| 36 |
/// @{ |
| 37 |
|
| 38 |
/// \cond |
| 39 |
namespace detail |
| 40 |
{ |
| 41 |
template<typename I0, typename S0, typename I1, typename S1, typename C, |
| 42 |
typename P0, typename P1> |
| 43 |
constexpr bool equal_nocheck(I0 begin0, S0 end0, I1 begin1, S1 end1, C pred, |
| 44 |
P0 proj0, P1 proj1) |
| 45 |
{ |
| 46 |
for(; begin0 != end0 && begin1 != end1; ++begin0, ++begin1) |
| 47 |
if(!invoke(pred, invoke(proj0, *begin0), invoke(proj1, *begin1))) |
| 48 |
return false; |
| 49 |
return begin0 == end0 && begin1 == end1; |
| 50 |
} |
| 51 |
} // namespace detail |
| 52 |
/// \endcond |
| 53 |
|
| 54 |
RANGES_FUNC_BEGIN(equal) |
| 55 |
|
| 56 |
/// \brief function template \c equal |
| 57 |
template(typename I0, |
| 58 |
typename S0, |
| 59 |
typename I1, |
| 60 |
typename C = equal_to, |
| 61 |
typename P0 = identity, |
| 62 |
typename P1 = identity)( |
| 63 |
requires input_iterator<I0> AND sentinel_for<S0, I0> AND |
| 64 |
input_iterator<I1> AND indirectly_comparable<I0, I1, C, P0, P1>) |
| 65 |
RANGES_DEPRECATED( |
| 66 |
"Use the variant of ranges::equal that takes an upper bound for " |
| 67 |
"both sequences") |
| 68 |
constexpr bool RANGES_FUNC(equal)(I0 begin0, |
| 69 |
S0 end0, |
| 70 |
I1 begin1, |
| 71 |
C pred = C{}, |
| 72 |
P0 proj0 = P0{}, |
| 73 |
P1 proj1 = P1{}) // |
| 74 |
{ |
| 75 |
for(; begin0 != end0; ++begin0, ++begin1) |
| 76 |
if(!invoke(pred, invoke(proj0, *begin0), invoke(proj1, *begin1))) |
| 77 |
return false; |
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
/// \overload |
| 82 |
template(typename I0, |
| 83 |
typename S0, |
| 84 |
typename I1, |
| 85 |
typename S1, |
| 86 |
typename C = equal_to, |
| 87 |
typename P0 = identity, |
| 88 |
typename P1 = identity)( |
| 89 |
requires input_iterator<I0> AND sentinel_for<S0, I0> AND |
| 90 |
input_iterator<I1> AND sentinel_for<S1, I1> AND |
| 91 |
indirectly_comparable<I0, I1, C, P0, P1>) |
| 92 |
constexpr bool RANGES_FUNC(equal)(I0 begin0, |
| 93 |
S0 end0, |
| 94 |
I1 begin1, |
| 95 |
S1 end1, |
| 96 |
C pred = C{}, |
| 97 |
P0 proj0 = P0{}, |
| 98 |
P1 proj1 = P1{}) // |
| 99 |
{ |
| 100 |
if(RANGES_CONSTEXPR_IF(sized_sentinel_for<S0, I0> && |
| 101 |
sized_sentinel_for<S1, I1>)) |
| 102 |
if(distance(begin0, end0) != distance(begin1, end1)) |
| 103 |
return false; |
| 104 |
return detail::equal_nocheck(std::move(begin0), |
| 105 |
std::move(end0), |
| 106 |
std::move(begin1), |
| 107 |
std::move(end1), |
| 108 |
std::move(pred), |
| 109 |
std::move(proj0), |
| 110 |
std::move(proj1)); |
| 111 |
} |
| 112 |
|
| 113 |
/// \overload |
| 114 |
template(typename Rng0, |
| 115 |
typename I1Ref, |
| 116 |
typename C = equal_to, |
| 117 |
typename P0 = identity, |
| 118 |
typename P1 = identity)( |
| 119 |
requires input_range<Rng0> AND input_iterator<uncvref_t<I1Ref>> AND |
| 120 |
indirectly_comparable<iterator_t<Rng0>, uncvref_t<I1Ref>, C, P0, P1>) |
| 121 |
RANGES_DEPRECATED( |
| 122 |
"Use the variant of ranges::equal that takes an upper bound for " |
| 123 |
"both sequences") |
| 124 |
constexpr bool RANGES_FUNC(equal)(Rng0 && rng0, |
| 125 |
I1Ref && begin1, |
| 126 |
C pred = C{}, |
| 127 |
P0 proj0 = P0{}, |
| 128 |
P1 proj1 = P1{}) // |
| 129 |
{ |
| 130 |
RANGES_DIAGNOSTIC_PUSH |
| 131 |
RANGES_DIAGNOSTIC_IGNORE_DEPRECATED_DECLARATIONS |
| 132 |
return (*this)(begin(rng0), |
| 133 |
end(rng0), |
| 134 |
(I1Ref &&) begin1, |
| 135 |
std::move(pred), |
| 136 |
std::move(proj0), |
| 137 |
std::move(proj1)); |
| 138 |
RANGES_DIAGNOSTIC_POP |
| 139 |
} |
| 140 |
|
| 141 |
/// \overload |
| 142 |
template(typename Rng0, |
| 143 |
typename Rng1, |
| 144 |
typename C = equal_to, |
| 145 |
typename P0 = identity, |
| 146 |
typename P1 = identity)( |
| 147 |
requires input_range<Rng0> AND input_range<Rng1> AND |
| 148 |
indirectly_comparable<iterator_t<Rng0>, iterator_t<Rng1>, C, P0, P1>) |
| 149 |
constexpr bool RANGES_FUNC(equal)( |
| 150 |
Rng0 && rng0, Rng1 && rng1, C pred = C{}, P0 proj0 = P0{}, P1 proj1 = P1{}) // |
| 151 |
{ |
| 152 |
if(RANGES_CONSTEXPR_IF(sized_range<Rng0> && sized_range<Rng1>)) |
| 153 |
if(distance(rng0) != distance(rng1)) |
| 154 |
return false; |
| 155 |
return detail::equal_nocheck(begin(rng0), |
| 156 |
end(rng0), |
| 157 |
begin(rng1), |
| 158 |
end(rng1), |
| 159 |
std::move(pred), |
| 160 |
std::move(proj0), |
| 161 |
std::move(proj1)); |
| 162 |
} |
| 163 |
|
| 164 |
RANGES_FUNC_END(equal) |
| 165 |
|
| 166 |
namespace cpp20 |
| 167 |
{ |
| 168 |
using ranges::equal; |
| 169 |
} |
| 170 |
/// @} |
| 171 |
} // namespace ranges |
| 172 |
|
| 173 |
#include <range/v3/detail/epilogue.hpp> |
| 174 |
|
| 175 |
#endif |