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 @@ -61,6 +61,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,80 @@ +//===----------------------------------------------------------------------===// +// +// 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/ranges_operations.h> +#include <__iterator/concepts.h> +#include <__iterator/indirectly_comparable.h> +#include <__ranges/concepts.h> +#include <__ranges/dangling.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_RANGES) + +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 (!_VSTD::invoke(__pred, _VSTD::invoke(__proj1, *__first1), _VSTD::invoke(__proj2, *__first2))) + break; + ++__first1; + ++__first2; + } + return {_VSTD::move(__first1), _VSTD::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(_VSTD::move(__first1), __last1, _VSTD::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 @@ -731,6 +731,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 @@ -281,6 +281,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,163 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +#include +#include +#include +#include + +#include "test_iterators.h" + +template +constexpr void test_iterators(Iter1 begin1, Iter1 end1, Iter2 begin2, Iter2 end2, Iter1 expected1, Iter2 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))); + if constexpr (!std::is_same_v> && + !std::is_same_v>) { + assert(ret.in1 == expected1); + assert(ret.in2 == expected2); + } +} + +template +constexpr void test_ranges(Range1& r1, Range2& r2, Iter1 expected1, Iter2 expected2) { + using Expected = std::ranges::mismatch_result; + std::same_as auto ret = std::ranges::mismatch(r1, r2); + assert(ret.in1 == expected1); + assert(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), Iter1(a + 3), Iter2(b + 3)); +} + +constexpr void test_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); +} + +constexpr void test_c_arrays() { + 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); +} + +constexpr void test_different_lengths() { + std::array a = {1, 2, 3}; + std::array b = {1, 2}; + + test_iterators(a.begin(), a.begin() + 3, b.begin(), b.begin() + 2, a.begin() + 2, b.begin() + 2); + test_ranges(a, b, a.begin() + 2, b.begin() + 2); +} + +constexpr void test_borrowed_range() { + int r1[] = {1, 2, 3, 4, 5}; + int r2[] = {1, 2, 3, 5, 4}; + + using Iter = int*; + using Sentinel = sentinel_wrapper; + + std::ranges::subrange b1{Iter(r1), Sentinel(Iter(r1 + 5))}; + std::ranges::subrange b2{Iter(r2), Sentinel(Iter(r2 + 5))}; + + test_ranges(b1, b2, Iter(r1 + 3), Iter(r2 + 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(); + + std::array a = {1, 2, 3, 4, 5}; + std::array b = {1, 2, 3, 5, 4}; + test_ranges(a, b, a.begin() + 3, b.begin() + 3); + test_sentinel(); + test_different_lengths(); + test_borrowed_range(); + test_c_arrays(); + + 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));