| 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_MISMATCH_HPP |
| 14 |
#define RANGES_V3_ALGORITHM_MISMATCH_HPP |
| 15 |
|
| 16 |
#include <utility> |
| 17 |
|
| 18 |
#include <meta/meta.hpp> |
| 19 |
|
| 20 |
#include <range/v3/range_fwd.hpp> |
| 21 |
|
| 22 |
#include <range/v3/algorithm/result_types.hpp> |
| 23 |
#include <range/v3/functional/comparisons.hpp> |
| 24 |
#include <range/v3/functional/identity.hpp> |
| 25 |
#include <range/v3/functional/invoke.hpp> |
| 26 |
#include <range/v3/iterator/concepts.hpp> |
| 27 |
#include <range/v3/iterator/traits.hpp> |
| 28 |
#include <range/v3/range/access.hpp> |
| 29 |
#include <range/v3/range/concepts.hpp> |
| 30 |
#include <range/v3/range/dangling.hpp> |
| 31 |
#include <range/v3/range/traits.hpp> |
| 32 |
#include <range/v3/utility/static_const.hpp> |
| 33 |
|
| 34 |
#include <range/v3/detail/prologue.hpp> |
| 35 |
|
| 36 |
namespace ranges |
| 37 |
{ |
| 38 |
/// \addtogroup group-algorithms |
| 39 |
/// @{ |
| 40 |
template<typename I1, typename I2> |
| 41 |
using mismatch_result = detail::in1_in2_result<I1, I2>; |
| 42 |
|
| 43 |
RANGES_FUNC_BEGIN(mismatch) |
| 44 |
|
| 45 |
/// \brief function template \c mismatch |
| 46 |
template(typename I1, |
| 47 |
typename S1, |
| 48 |
typename I2, |
| 49 |
typename C = equal_to, |
| 50 |
typename P1 = identity, |
| 51 |
typename P2 = identity)( |
| 52 |
requires input_iterator<I1> AND sentinel_for<S1, I1> AND |
| 53 |
input_iterator<I2> AND |
| 54 |
indirect_relation<C, projected<I1, P1>, projected<I2, P2>>) |
| 55 |
RANGES_DEPRECATED( |
| 56 |
"Use the variant of ranges::mismatch that takes an upper bound for " |
| 57 |
"both sequences") |
| 58 |
mismatch_result<I1, I2> RANGES_FUNC(mismatch)(I1 begin1, |
| 59 |
S1 end1, |
| 60 |
I2 begin2, |
| 61 |
C pred = C{}, |
| 62 |
P1 proj1 = P1{}, |
| 63 |
P2 proj2 = P2{}) // |
| 64 |
{ |
| 65 |
for(; begin1 != end1; ++begin1, ++begin2) |
| 66 |
if(!invoke(pred, invoke(proj1, *begin1), invoke(proj2, *begin2))) |
| 67 |
break; |
| 68 |
return {begin1, begin2}; |
| 69 |
} |
| 70 |
|
| 71 |
/// \overload |
| 72 |
template(typename I1, |
| 73 |
typename S1, |
| 74 |
typename I2, |
| 75 |
typename S2, |
| 76 |
typename C = equal_to, |
| 77 |
typename P1 = identity, |
| 78 |
typename P2 = identity)( |
| 79 |
requires input_iterator<I1> AND sentinel_for<S1, I1> AND |
| 80 |
input_iterator<I2> AND sentinel_for<S2, I2> AND |
| 81 |
indirect_relation<C, projected<I1, P1>, projected<I2, P2>>) |
| 82 |
constexpr mismatch_result<I1, I2> RANGES_FUNC(mismatch)(I1 begin1, |
| 83 |
S1 end1, |
| 84 |
I2 begin2, |
| 85 |
S2 end2, |
| 86 |
C pred = C{}, |
| 87 |
P1 proj1 = P1{}, |
| 88 |
P2 proj2 = P2{}) // |
| 89 |
{ |
| 90 |
for(; begin1 != end1 && begin2 != end2; ++begin1, ++begin2) |
| 91 |
if(!invoke(pred, invoke(proj1, *begin1), invoke(proj2, *begin2))) |
| 92 |
break; |
| 93 |
return {begin1, begin2}; |
| 94 |
} |
| 95 |
|
| 96 |
/// \overload |
| 97 |
template(typename Rng1, |
| 98 |
typename I2Ref, |
| 99 |
typename C = equal_to, |
| 100 |
typename P1 = identity, |
| 101 |
typename P2 = identity)( //s |
| 102 |
requires input_range<Rng1> AND input_iterator<uncvref_t<I2Ref>> AND |
| 103 |
indirect_relation<C, |
| 104 |
projected<iterator_t<Rng1>, P1>, |
| 105 |
projected<uncvref_t<I2Ref>, P2>>) |
| 106 |
RANGES_DEPRECATED( |
| 107 |
"Use the variant of ranges::mismatch that takes an upper bound for " |
| 108 |
"both sequences") |
| 109 |
mismatch_result<borrowed_iterator_t<Rng1>, uncvref_t<I2Ref>> |
| 110 |
RANGES_FUNC(mismatch)(Rng1 && rng1, |
| 111 |
I2Ref && begin2, |
| 112 |
C pred = C{}, // see below [*] |
| 113 |
P1 proj1 = P1{}, |
| 114 |
P2 proj2 = P2{}) // |
| 115 |
{ |
| 116 |
RANGES_DIAGNOSTIC_PUSH |
| 117 |
RANGES_DIAGNOSTIC_IGNORE_DEPRECATED_DECLARATIONS |
| 118 |
return (*this)(begin(rng1), |
| 119 |
end(rng1), |
| 120 |
static_cast<uncvref_t<I2Ref> &&>(begin2), |
| 121 |
std::move(pred), |
| 122 |
std::move(proj1), |
| 123 |
std::move(proj2)); |
| 124 |
RANGES_DIAGNOSTIC_POP |
| 125 |
} |
| 126 |
|
| 127 |
/// \overload |
| 128 |
template(typename Rng1, |
| 129 |
typename Rng2, |
| 130 |
typename C = equal_to, |
| 131 |
typename P1 = identity, |
| 132 |
typename P2 = identity)( |
| 133 |
requires input_range<Rng1> AND input_range<Rng2> AND |
| 134 |
indirect_relation<C, |
| 135 |
projected<iterator_t<Rng1>, P1>, |
| 136 |
projected<iterator_t<Rng2>, P2>>) |
| 137 |
constexpr mismatch_result<borrowed_iterator_t<Rng1>, borrowed_iterator_t<Rng2>> // |
| 138 |
RANGES_FUNC(mismatch)(Rng1 && rng1, |
| 139 |
Rng2 && rng2, |
| 140 |
C pred = C{}, |
| 141 |
P1 proj1 = P1{}, |
| 142 |
P2 proj2 = P2{}) // |
| 143 |
{ |
| 144 |
return (*this)(begin(rng1), |
| 145 |
end(rng1), |
| 146 |
begin(rng2), |
| 147 |
end(rng2), |
| 148 |
std::move(pred), |
| 149 |
std::move(proj1), |
| 150 |
std::move(proj2)); |
| 151 |
} |
| 152 |
|
| 153 |
RANGES_FUNC_END(mismatch) |
| 154 |
|
| 155 |
namespace cpp20 |
| 156 |
{ |
| 157 |
using ranges::mismatch; |
| 158 |
using ranges::mismatch_result; |
| 159 |
} // namespace cpp20 |
| 160 |
|
| 161 |
// [*] In this case, the 'begin2' iterator is taken by universal reference. Why? So |
| 162 |
// that we can properly distinguish this case: |
| 163 |
// int x[] = {1,2,3,4}; |
| 164 |
// int y[] = {1,2,3,4}; |
| 165 |
// mismatch(x, y); |
| 166 |
// Had 'begin2' been taken by value as is customary, this call could match as either |
| 167 |
// two ranges, or a range and an iterator, where the iterator is the array, decayed |
| 168 |
// to a pointer. Yuk! |
| 169 |
|
| 170 |
/// @} |
| 171 |
} // namespace ranges |
| 172 |
|
| 173 |
#include <range/v3/detail/epilogue.hpp> |
| 174 |
|
| 175 |
#endif |