diff --git a/libcxx/docs/Status/RangesAlgorithms.csv b/libcxx/docs/Status/RangesAlgorithms.csv --- a/libcxx/docs/Status/RangesAlgorithms.csv +++ b/libcxx/docs/Status/RangesAlgorithms.csv @@ -7,7 +7,7 @@ Search,find_if_not,Christopher Di Bella,`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 `,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 `_,Under review diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -63,6 +63,7 @@ __algorithm/pop_heap.h __algorithm/prev_permutation.h __algorithm/push_heap.h + __algorithm/ranges_mismatch.h __algorithm/remove.h __algorithm/remove_copy.h __algorithm/remove_copy_if.h diff --git a/libcxx/include/__algorithm/ranges_mismatch.h b/libcxx/include/__algorithm/ranges_mismatch.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/ranges_mismatch.h @@ -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 +using mismatch_result = in_in_result<_I1, _I2>; + +namespace __mismatch { +struct __fn { + template + 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 _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 + requires indirectly_comparable, iterator_t<_R2>, _Pred, _Proj1, _Proj2> + _LIBCPP_HIDE_FROM_ABI constexpr + mismatch_result, 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 diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -27,6 +27,18 @@ template struct in_out_out_result; // since C++20 + + template S1, input_iterator I2, sentinel_for<_I2> S2, + class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity> + requires indirectly_comparable + constexpr mismatch_result<_I1, _I2> + mismatch()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) + + template + requires indirectly_comparable, iterator_t, Pred, Proj1, Proj2> + constexpr mismatch_result, borrowed_iterator_t> + mismatch(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) } template @@ -739,6 +751,7 @@ #include <__algorithm/pop_heap.h> #include <__algorithm/prev_permutation.h> #include <__algorithm/push_heap.h> +#include <__algorithm/ranges_mismatch.h> #include <__algorithm/remove.h> #include <__algorithm/remove_copy.h> #include <__algorithm/remove_copy_if.h> diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -284,6 +284,7 @@ module pop_heap { private header "__algorithm/pop_heap.h" } module prev_permutation { private header "__algorithm/prev_permutation.h" } module push_heap { private header "__algorithm/push_heap.h" } + module ranges_mismatch { private header "__algorithm/ranges_mismatch.h" } module remove { private header "__algorithm/remove.h" } module remove_copy { private header "__algorithm/remove_copy.h" } module remove_copy_if { private header "__algorithm/remove_copy_if.h" } diff --git a/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_mismatch.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_mismatch.module.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_mismatch.module.verify.cpp @@ -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> diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/mismatch/ranges_mismatch.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/mismatch/ranges_mismatch.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.nonmodifying/mismatch/ranges_mismatch.pass.cpp @@ -0,0 +1,190 @@ +//===----------------------------------------------------------------------===// +// +// 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 S1, input_iterator I2, sentinel_for<_I2> S2, +// class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity> +// requires indirectly_comparable +// constexpr mismatch_result<_I1, _I2> +// ranges::mismatch()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) + +// template +// requires indirectly_comparable, iterator_t, Pred, Proj1, Proj2> +// constexpr mismatch_result, borrowed_iterator_t> +// ranges::mismatch(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) + +#include +#include +#include +#include + +#include "test_iterators.h" + +template +constexpr void test_iterators(Iter1 begin1, Iter1 end1, Iter2 begin2, Iter2 end2, int* expected1, int* expected2) { + using Expected = std::ranges::mismatch_result; + std::same_as auto ret = std::ranges::mismatch(std::move(begin1), sentinel_wrapper(std::move(end1)), + std::move(begin2), sentinel_wrapper(std::move(end2))); + assert(base(ret.in1) == expected1); + assert(base(ret.in2) == expected2); +} + +template +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>(); + test_iters, cpp20_input_iterator>(); + test_iters, forward_iterator>(); + test_iters, bidirectional_iterator>(); + test_iters, random_access_iterator>(); + test_iters, contiguous_iterator>(); + test_iters, int*>(); + + test_iters, cpp17_input_iterator>(); + test_iters, cpp20_input_iterator>(); + test_iters, forward_iterator>(); + test_iters, bidirectional_iterator>(); + test_iters, random_access_iterator>(); + test_iters, contiguous_iterator>(); + test_iters, int*>(); + + test_iters, cpp17_input_iterator>(); + test_iters, cpp20_input_iterator>(); + test_iters, forward_iterator>(); + test_iters, bidirectional_iterator>(); + test_iters, random_access_iterator>(); + test_iters, contiguous_iterator>(); + test_iters, int*>(); + + test_iters, cpp17_input_iterator>(); + test_iters, cpp20_input_iterator>(); + test_iters, forward_iterator>(); + test_iters, bidirectional_iterator>(); + test_iters, random_access_iterator>(); + test_iters, contiguous_iterator>(); + test_iters, int*>(); + + test_iters, cpp17_input_iterator>(); + test_iters, cpp20_input_iterator>(); + test_iters, forward_iterator>(); + test_iters, bidirectional_iterator>(); + test_iters, random_access_iterator>(); + test_iters, contiguous_iterator>(); + test_iters, int*>(); + + test_iters, cpp17_input_iterator>(); + test_iters, cpp20_input_iterator>(); + test_iters, forward_iterator>(); + test_iters, bidirectional_iterator>(); + test_iters, random_access_iterator>(); + test_iters, contiguous_iterator>(); + test_iters, int*>(); + + test_iters>(); + test_iters>(); + test_iters>(); + test_iters>(); + test_iters>(); + test_iters>(); + test_iters(); + + { // test with a range + std::array a = {1, 2, 3, 4, 5}; + std::array b = {1, 2, 3, 5, 4}; + using Expected = std::ranges::mismatch_result; + std::same_as 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; + using Expected = std::ranges::mismatch_result; + + std::same_as 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 sized + { + 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; + std::same_as 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; + std::same_as 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; + { + std::same_as auto ret = std::ranges::mismatch(r1, std::views::all(r2)); + assert(ret.in1 == r1 + 3); + assert(ret.in2 == r2 + 3); + } + { + std::same_as auto ret = std::ranges::mismatch(std::views::all(r1), r2); + assert(ret.in1 == r1 + 3); + assert(ret.in2 == r2 + 3); + } + { + std::same_as 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); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp --- a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp +++ b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp @@ -103,7 +103,7 @@ //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));