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,77 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace ranges { + +template +using mismatch_result = in_in_result<_I1, _I2>; + +namespace __mismatch { +struct __fn { + 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 + ranges::mismatch_result<_I1, _I2> operator()(_I1 __first1, + _S1 __last1, + _I2 __first2, + _S2 __last2, + _Pred __pred = {}, + _Proj1 __proj1 = {}, + _Proj2 __proj2 = {}) const { + while (__first1 != __last1 && __first2 != __last2) { + if (!invoke(__pred, invoke(__proj1, *__first1), invoke(__proj2, *__first2))) + break; + ++__first1; + ++__first2; + } + return {__first1, __first2}; + } + + template + requires indirectly_comparable, iterator_t<_R2>, _Pred, _Proj1, _Proj2> + _LIBCPP_HIDE_FROM_ABI constexpr ranges::mismatch_result, borrowed_iterator_t<_R2>> + operator()(_R1&& __r1, _R2&& __r2, _Pred&& __pred = {}, _Proj1&& __proj1 = {}, _Proj2&& __proj2 = {}) const { + return operator()(ranges::begin(__r1), + ranges::end(__r1), + ranges::begin(__r2), + ranges::end(__r2), + std::forward<_Pred>(__pred), + std::forward<_Proj1>(__proj1), + std::forward<_Proj2>(__proj2)); + } +}; +} // namespace __mismatch + +inline namespace __cpo { + constexpr inline auto mismatch = __mismatch::__fn{}; +} // namespace __cpo +} // namespace 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 rnages_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,109 @@ +//===----------------------------------------------------------------------===// +// +// 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 "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(begin1, end1, begin2, end2); + 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_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, forward_iterator>(); + test_iters, bidirectional_iterator>(); + test_iters, random_access_iterator>(); + test_iters, int*>(); + + test_iters, forward_iterator>(); + test_iters, bidirectional_iterator>(); + test_iters, random_access_iterator>(); + test_iters, int*>(); + + test_iters, forward_iterator>(); + test_iters, bidirectional_iterator>(); + test_iters, random_access_iterator>(); + test_iters, int*>(); + + 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(); + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + return 0; +}