[libc++][ranges] Implement ranges::mismatch

Implement `ranges::mismatch`

Reviewed By: Quuxplusone, ldionne, #libc

Spies: libcxx-commits, mgorny

Differential Revision: https://reviews.llvm.org/D117817
This commit is contained in:
Nikolas Klauser 2022-03-08 23:12:35 +01:00
parent b81d5baa0f
commit c2cd15a665
8 changed files with 411 additions and 2 deletions

View File

@ -7,7 +7,7 @@ Search,find_if,Christopher Di Bella,`D105792 <https://llvm.org/D105792>`_
Search,find_if_not,Christopher Di Bella,`D105792 <https://llvm.org/D105792>`_
Search,find_first_of,Not assigned,n/a,Not started
Search,adjacent_find,Not assigned,n/a,Not started
Search,mismatch,Not assigned,n/a,Not started
Search,mismatch,Nikolas Klauser,`D117817 <https://llvm.org/D117817>`,Complete
Search,equal,Not assigned,n/a,Not started
Search,lexicographical_compare,Not assigned,n/a,Not started
Search,partition_point,Christopher Di Bella,`D105794 <https://llvm.org/D105794>`_,Under review

1 Category,Algorithm,Assignee,CL,Complete
7 Search,find_if_not,Christopher Di Bella,`D105792 <https://llvm.org/D105792>`_
8 Search,find_first_of,Not assigned,n/a,Not started
9 Search,adjacent_find,Not assigned,n/a,Not started
10 Search,mismatch,Not assigned,n/a,Not started Search,mismatch,Nikolas Klauser,`D117817 <https://llvm.org/D117817>`,Complete
11 Search,equal,Not assigned,n/a,Not started
12 Search,lexicographical_compare,Not assigned,n/a,Not started
13 Search,partition_point,Christopher Di Bella,`D105794 <https://llvm.org/D105794>`_,Under review

View File

@ -68,6 +68,7 @@ set(files
__algorithm/push_heap.h
__algorithm/ranges_max_element.h
__algorithm/ranges_min_element.h
__algorithm/ranges_mismatch.h
__algorithm/ranges_swap_ranges.h
__algorithm/remove.h
__algorithm/remove_copy.h

View File

@ -0,0 +1,85 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCPP___ALGORITHM_RANGES_MISMATCH_H
#define _LIBCPP___ALGORITHM_RANGES_MISMATCH_H
#include <__algorithm/in_in_result.h>
#include <__config>
#include <__functional/identity.h>
#include <__functional/invoke.h>
#include <__functional/ranges_operations.h>
#include <__iterator/concepts.h>
#include <__iterator/indirectly_comparable.h>
#include <__ranges/access.h>
#include <__ranges/concepts.h>
#include <__ranges/dangling.h>
#include <__utility/move.h>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif
_LIBCPP_BEGIN_NAMESPACE_STD
#if !defined(_LIBCPP_HAS_NO_CONCEPTS)
namespace ranges {
template <class _I1, class _I2>
using mismatch_result = in_in_result<_I1, _I2>;
namespace __mismatch {
struct __fn {
template <class _I1, class _S1, class _I2, class _S2,
class _Pred, class _Proj1, class _Proj2>
static _LIBCPP_HIDE_FROM_ABI constexpr
mismatch_result<_I1, _I2>
__go(_I1 __first1, _S1 __last1, _I2 __first2, _S2 __last2,
_Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
while (__first1 != __last1 && __first2 != __last2) {
if (!std::invoke(__pred, std::invoke(__proj1, *__first1), std::invoke(__proj2, *__first2)))
break;
++__first1;
++__first2;
}
return {std::move(__first1), std::move(__first2)};
}
template <input_iterator _I1, sentinel_for<_I1> _S1,
input_iterator _I2, sentinel_for<_I2> _S2,
class _Pred = ranges::equal_to, class _Proj1 = identity, class _Proj2 = identity>
requires indirectly_comparable<_I1, _I2, _Pred, _Proj1, _Proj2>
_LIBCPP_HIDE_FROM_ABI constexpr
mismatch_result<_I1, _I2> operator()(_I1 __first1, _S1 __last1, _I2 __first2, _S2 __last2,
_Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
return __go(std::move(__first1), __last1, std::move(__first2), __last2, __pred, __proj1, __proj2);
}
template <input_range _R1, input_range _R2,
class _Pred = ranges::equal_to, class _Proj1 = identity, class _Proj2 = identity>
requires indirectly_comparable<iterator_t<_R1>, iterator_t<_R2>, _Pred, _Proj1, _Proj2>
_LIBCPP_HIDE_FROM_ABI constexpr
mismatch_result<borrowed_iterator_t<_R1>, borrowed_iterator_t<_R2>>
operator()(_R1&& __r1, _R2&& __r2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
return __go(ranges::begin(__r1), ranges::end(__r1), ranges::begin(__r2), ranges::end(__r2),
__pred, __proj1, __proj2);
}
};
} // namespace __mismatch
inline namespace __cpo {
constexpr inline auto mismatch = __mismatch::__fn{};
} // namespace __cpo
} // namespace ranges
#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_RANGES)
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP___ALGORITHM_RANGES_MISMATCH_H

View File

@ -44,6 +44,18 @@ namespace ranges {
template<forward_range R, class Proj = identity,
indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less> // since C++20
constexpr borrowed_iterator_t<R> min_element(R&& r, Comp comp = {}, Proj proj = {});
template <input_iterator I1, sentinel_for<_I1> S1, input_iterator I2, sentinel_for<_I2> S2,
class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
requires indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
constexpr mismatch_result<_I1, _I2>
mismatch()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) // since C++20
template <input_range R1, input_range R2,
class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
requires indirectly_comparable<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
constexpr mismatch_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>>
mismatch(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) // since C++20
}
template <class InputIterator, class Predicate>
@ -765,6 +777,7 @@ template <class BidirectionalIterator, class Compare>
#include <__algorithm/push_heap.h>
#include <__algorithm/ranges_max_element.h>
#include <__algorithm/ranges_min_element.h>
#include <__algorithm/ranges_mismatch.h>
#include <__algorithm/ranges_swap_ranges.h>
#include <__algorithm/remove.h>
#include <__algorithm/remove_copy.h>

View File

@ -296,6 +296,7 @@ module std [system] {
module push_heap { private header "__algorithm/push_heap.h" }
module ranges_max_element { private header "__algorithm/ranges_max_element.h" }
module ranges_min_element { private header "__algorithm/ranges_min_element.h" }
module ranges_mismatch { private header "__algorithm/ranges_mismatch.h" }
module ranges_swap_ranges { private header "__algorithm/ranges_swap_ranges.h" }
module remove { private header "__algorithm/remove.h" }
module remove_copy { private header "__algorithm/remove_copy.h" }

View File

@ -0,0 +1,15 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// REQUIRES: modules-build
// WARNING: This test was generated by 'generate_private_header_tests.py'
// and should not be edited manually.
// expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_mismatch.h'}}
#include <__algorithm/ranges_mismatch.h>

View File

@ -0,0 +1,294 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-no-concepts
// UNSUPPORTED: libcpp-has-no-incomplete-ranges
// template <input_iterator I1, sentinel_for<_I1> S1, input_iterator I2, sentinel_for<_I2> S2,
// class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
// requires indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
// constexpr mismatch_result<_I1, _I2>
// ranges::mismatch()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {})
// template <input_range R1, input_range R2,
// class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
// requires indirectly_comparable<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
// constexpr mismatch_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>>
// ranges::mismatch(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {})
#include <algorithm>
#include <array>
#include <cassert>
#include <ranges>
#include "test_iterators.h"
template <class Iter1, class Iter2>
constexpr void test_iterators(Iter1 begin1, Iter1 end1, Iter2 begin2, Iter2 end2, int* expected1, int* expected2) {
using Expected = std::ranges::mismatch_result<Iter1, Iter2>;
std::same_as<Expected> auto ret = std::ranges::mismatch(std::move(begin1), sentinel_wrapper<Iter1>(std::move(end1)),
std::move(begin2), sentinel_wrapper<Iter2>(std::move(end2)));
assert(base(ret.in1) == expected1);
assert(base(ret.in2) == expected2);
}
template <class Iter1, class Iter2>
constexpr void test_iters() {
int a[] = {1, 2, 3, 4, 5};
int b[] = {1, 2, 3, 5, 4};
test_iterators(Iter1(a), Iter1(a + 5), Iter2(b), Iter2(b + 5), a + 3, b + 3);
}
constexpr bool test() {
test_iters<cpp17_input_iterator<int*>, cpp17_input_iterator<int*>>();
test_iters<cpp17_input_iterator<int*>, cpp20_input_iterator<int*>>();
test_iters<cpp17_input_iterator<int*>, forward_iterator<int*>>();
test_iters<cpp17_input_iterator<int*>, bidirectional_iterator<int*>>();
test_iters<cpp17_input_iterator<int*>, random_access_iterator<int*>>();
test_iters<cpp17_input_iterator<int*>, contiguous_iterator<int*>>();
test_iters<cpp17_input_iterator<int*>, int*>();
test_iters<cpp20_input_iterator<int*>, cpp17_input_iterator<int*>>();
test_iters<cpp20_input_iterator<int*>, cpp20_input_iterator<int*>>();
test_iters<cpp20_input_iterator<int*>, forward_iterator<int*>>();
test_iters<cpp20_input_iterator<int*>, bidirectional_iterator<int*>>();
test_iters<cpp20_input_iterator<int*>, random_access_iterator<int*>>();
test_iters<cpp20_input_iterator<int*>, contiguous_iterator<int*>>();
test_iters<cpp20_input_iterator<int*>, int*>();
test_iters<forward_iterator<int*>, cpp17_input_iterator<int*>>();
test_iters<forward_iterator<int*>, cpp20_input_iterator<int*>>();
test_iters<forward_iterator<int*>, forward_iterator<int*>>();
test_iters<forward_iterator<int*>, bidirectional_iterator<int*>>();
test_iters<forward_iterator<int*>, random_access_iterator<int*>>();
test_iters<forward_iterator<int*>, contiguous_iterator<int*>>();
test_iters<forward_iterator<int*>, int*>();
test_iters<bidirectional_iterator<int*>, cpp17_input_iterator<int*>>();
test_iters<bidirectional_iterator<int*>, cpp20_input_iterator<int*>>();
test_iters<bidirectional_iterator<int*>, forward_iterator<int*>>();
test_iters<bidirectional_iterator<int*>, bidirectional_iterator<int*>>();
test_iters<bidirectional_iterator<int*>, random_access_iterator<int*>>();
test_iters<bidirectional_iterator<int*>, contiguous_iterator<int*>>();
test_iters<bidirectional_iterator<int*>, int*>();
test_iters<random_access_iterator<int*>, cpp17_input_iterator<int*>>();
test_iters<random_access_iterator<int*>, cpp20_input_iterator<int*>>();
test_iters<random_access_iterator<int*>, forward_iterator<int*>>();
test_iters<random_access_iterator<int*>, bidirectional_iterator<int*>>();
test_iters<random_access_iterator<int*>, random_access_iterator<int*>>();
test_iters<random_access_iterator<int*>, contiguous_iterator<int*>>();
test_iters<random_access_iterator<int*>, int*>();
test_iters<contiguous_iterator<int*>, cpp17_input_iterator<int*>>();
test_iters<contiguous_iterator<int*>, cpp20_input_iterator<int*>>();
test_iters<contiguous_iterator<int*>, forward_iterator<int*>>();
test_iters<contiguous_iterator<int*>, bidirectional_iterator<int*>>();
test_iters<contiguous_iterator<int*>, random_access_iterator<int*>>();
test_iters<contiguous_iterator<int*>, contiguous_iterator<int*>>();
test_iters<contiguous_iterator<int*>, int*>();
test_iters<int*, cpp17_input_iterator<int*>>();
test_iters<int*, cpp20_input_iterator<int*>>();
test_iters<int*, forward_iterator<int*>>();
test_iters<int*, bidirectional_iterator<int*>>();
test_iters<int*, random_access_iterator<int*>>();
test_iters<int*, contiguous_iterator<int*>>();
test_iters<int*, int*>();
{ // test with a range
std::array<int, 5> a = {1, 2, 3, 4, 5};
std::array<int, 5> b = {1, 2, 3, 5, 4};
using Expected = std::ranges::mismatch_result<int*, int*>;
std::same_as<Expected> auto ret = std::ranges::mismatch(a, b);
assert(ret.in1 == a.begin() + 3);
assert(ret.in2 == b.begin() + 3);
}
{ // test with non-iterator sentinel
int a[] = {1, 2, 3, 4, 5};
int b[] = {1, 2, 3, 5, 4};
using Iter = int*;
using Sentinel = sentinel_wrapper<Iter>;
using Expected = std::ranges::mismatch_result<Iter, Iter>;
std::same_as<Expected> auto r = std::ranges::mismatch(Iter(a), Sentinel(a + 5), Iter(b), Sentinel(b + 5));
assert(r.in1 == a + 3);
assert(r.in2 == b + 3);
}
{ // test with different array sizes
{
int a[] = {1, 2, 3};
int b[] = {1, 2};
test_iterators(a, a + 3, b, b + 2, a + 2, b + 2);
using Expected = std::ranges::mismatch_result<int*, int*>;
std::same_as<Expected> auto ret = std::ranges::mismatch(a, b);
assert(ret.in1 == a + 2);
assert(ret.in2 == b + 2);
}
{
int a[] = {1, 2};
int b[] = {1, 2, 3};
test_iterators(a, a + 2, b, b + 3, a + 2, b + 2);
using Expected = std::ranges::mismatch_result<int*, int*>;
std::same_as<Expected> auto ret = std::ranges::mismatch(a, b);
assert(ret.in1 == a + 2);
assert(ret.in2 == b + 2);
}
}
{ // test with borrowed ranges
int r1[] = {1, 2, 3, 4, 5};
int r2[] = {1, 2, 3, 5, 4};
using Expected = std::ranges::mismatch_result<int*, int*>;
{
std::same_as<Expected> auto ret = std::ranges::mismatch(r1, std::views::all(r2));
assert(ret.in1 == r1 + 3);
assert(ret.in2 == r2 + 3);
}
{
std::same_as<Expected> auto ret = std::ranges::mismatch(std::views::all(r1), r2);
assert(ret.in1 == r1 + 3);
assert(ret.in2 == r2 + 3);
}
{
std::same_as<Expected> auto ret = std::ranges::mismatch(std::views::all(r1), std::views::all(r2));
assert(ret.in1 == r1 + 3);
assert(ret.in2 == r2 + 3);
}
}
{ // test structured bindings
int a[] = {1, 2, 3, 4};
int b[] = {1, 2, 4, 8, 16};
auto [ai, bi] = std::ranges::mismatch(a, b);
assert(ai == a + 2);
assert(bi == b + 2);
auto [aj, bj] = std::ranges::mismatch(a, a+4, b, b+5);
assert(aj == a + 2);
assert(bj == b + 2);
}
{ // test predicate
{
int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
int b[] = {6, 5, 8, 2, 5, 1, 2, 4};
auto ret = std::ranges::mismatch(a, a + 8, b, b + 8, std::ranges::greater{});
assert(ret.in1 == a + 4);
assert(ret.in2 == b + 4);
assert(*ret.in1 == 5);
assert(*ret.in2 == 5);
}
{
int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
int b[] = {6, 5, 8, 2, 5, 1, 2, 4};
auto ret = std::ranges::mismatch(a, b, std::ranges::greater{});
assert(ret.in1 == a + 4);
assert(ret.in2 == b + 4);
assert(*ret.in1 == 5);
assert(*ret.in2 == 5);
}
}
{ // test projection
{
int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
int b[] = {6, 5, 8, 2, 5, 1, 2, 4};
auto ret = std::ranges::mismatch(a, b,
std::ranges::greater{},
[](int i) { return i == 5 ? +100 : i; },
[](int i) { return i == 5 ? -100 : i; });
assert(ret.in1 == a + 5);
assert(ret.in2 == b + 5);
assert(*ret.in1 == 1);
assert(*ret.in2 == 1);
}
{
int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
auto ret = std::ranges::mismatch(a, a,
std::less<double>{},
[](int i) { return i * 1.01; },
[c = 0](int i) mutable { return c++ < 5 ? i * 1.02 : i; });
assert(ret.in1 == a + 5);
assert(ret.in2 == a + 5);
assert(*ret.in1 == 1);
assert(*ret.in2 == 1);
}
{
int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
int b[] = {6, 5, 8, 2, 5, 1, 2, 4};
auto ret = std::ranges::mismatch(a, a + 8, b, b + 8,
std::ranges::greater{},
[](int i) { return i == 5 ? +100 : i; },
[](int i) { return i == 5 ? -100 : i; });
assert(ret.in1 == a + 5);
assert(ret.in2 == b + 5);
assert(*ret.in1 == 1);
assert(*ret.in2 == 1);
}
{
int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
auto ret = std::ranges::mismatch(a, a + 8, a, a + 8,
std::less<double>{},
[](int i) { return i * 1.01; },
[c = 0](int i) mutable { return c++ < 5 ? i * 1.02 : i; });
assert(ret.in1 == a + 5);
assert(ret.in2 == a + 5);
assert(*ret.in1 == 1);
assert(*ret.in2 == 1);
}
}
{ // test predicate and projection call count
{
int pred_count = 0;
int proj1_count = 0;
int proj2_count = 0;
int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
auto ret = std::ranges::mismatch(a, a,
[&](int lhs, int rhs) { ++pred_count; return lhs == rhs; },
[&](int i) { ++proj1_count; return i; },
[&](int i) { ++proj2_count; return i; });
assert(ret.in1 == a + 8);
assert(ret.in2 == a + 8);
assert(pred_count == 8);
assert(proj1_count == 8);
assert(proj2_count == 8);
}
{
int pred_count = 0;
int proj1_count = 0;
int proj2_count = 0;
int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
auto ret = std::ranges::mismatch(a, a + 8, a, a + 8,
[&](int lhs, int rhs) { ++pred_count; return lhs == rhs; },
[&](int i) { ++proj1_count; return i; },
[&](int i) { ++proj2_count; return i; });
assert(ret.in1 == a + 8);
assert(ret.in2 == a + 8);
assert(pred_count == 8);
assert(proj1_count == 8);
assert(proj2_count == 8);
}
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}

View File

@ -104,7 +104,7 @@ static_assert(test(std::ranges::max_element, a));
static_assert(test(std::ranges::min_element, a));
//static_assert(test(std::ranges::minmax, a));
//static_assert(test(std::ranges::minmax_element, a));
//static_assert(test(std::ranges::mismatch, a, a));
static_assert(test(std::ranges::mismatch, a, a));
//static_assert(test(std::ranges::move, a, a));
//static_assert(test(std::ranges::move_backward, a, a));
//static_assert(test(std::ranges::next_permutation, a));