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 @@ -62,7 +62,7 @@ Write,partial_sort_copy,Not assigned,n/a,Not started Merge,merge,Hui Xie,`D128611 `_,✅ Merge,set_difference,Hui Xie,`D128983 `,✅ -Merge,set_intersection,Hui Xie,n/a,Not started +Merge,set_intersection,Hui Xie,`D129233 `,✅ Merge,set_symmetric_difference,Hui Xie,n/a,Not started Merge,set_union,Hui Xie,n/a,Not started Permutation,remove,Nikolas Klauser,`D128618 `_,✅ diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -109,6 +109,7 @@ __algorithm/ranges_replace_if.h __algorithm/ranges_reverse.h __algorithm/ranges_set_difference.h + __algorithm/ranges_set_intersection.h __algorithm/ranges_sort.h __algorithm/ranges_stable_sort.h __algorithm/ranges_swap_ranges.h diff --git a/libcxx/include/__algorithm/iterator_operations.h b/libcxx/include/__algorithm/iterator_operations.h --- a/libcxx/include/__algorithm/iterator_operations.h +++ b/libcxx/include/__algorithm/iterator_operations.h @@ -13,6 +13,7 @@ #include <__iterator/advance.h> #include <__iterator/distance.h> #include <__iterator/iterator_traits.h> +#include <__iterator/next.h> #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) # pragma GCC system_header @@ -24,6 +25,7 @@ struct _RangesIterOps { static constexpr auto advance = ranges::advance; static constexpr auto distance = ranges::distance; + static constexpr auto next = ranges::next; }; #endif @@ -40,6 +42,12 @@ return std::distance(__first, __last); } + template + _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_AFTER_CXX11 + _Iterator next(_Iterator, _Iterator __last) { + return __last; + } + }; _LIBCPP_END_NAMESPACE_STD diff --git a/libcxx/include/__algorithm/ranges_set_intersection.h b/libcxx/include/__algorithm/ranges_set_intersection.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/ranges_set_intersection.h @@ -0,0 +1,117 @@ +//===----------------------------------------------------------------------===// +// +// 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_SET_INTERSECTION_H +#define _LIBCPP___ALGORITHM_RANGES_SET_INTERSECTION_H + +#include <__algorithm/in_in_out_result.h> +#include <__algorithm/iterator_operations.h> +#include <__algorithm/make_projected.h> +#include <__algorithm/set_intersection.h> +#include <__config> +#include <__functional/identity.h> +#include <__functional/invoke.h> +#include <__functional/ranges_operations.h> +#include <__iterator/concepts.h> +#include <__iterator/mergeable.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 + +#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace ranges { + +template +using set_intersection_result = in_in_out_result<_InIter1, _InIter2, _OutIter>; + +namespace __set_intersection { + +struct __fn { + template < + input_iterator _InIter1, + sentinel_for<_InIter1> _Sent1, + input_iterator _InIter2, + sentinel_for<_InIter2> _Sent2, + weakly_incrementable _OutIter, + class _Comp = less, + class _Proj1 = identity, + class _Proj2 = identity> + requires mergeable<_InIter1, _InIter2, _OutIter, _Comp, _Proj1, _Proj2> + _LIBCPP_HIDE_FROM_ABI constexpr set_intersection_result<_InIter1, _InIter2, _OutIter> operator()( + _InIter1 __first1, + _Sent1 __last1, + _InIter2 __first2, + _Sent2 __last2, + _OutIter __result, + _Comp __comp = {}, + _Proj1 __proj1 = {}, + _Proj2 __proj2 = {}) const { + auto __ret = std::__set_intersection<_RangesIterOps>( + std::move(__first1), + std::move(__last1), + std::move(__first2), + std::move(__last2), + std::move(__result), + ranges::__make_projected_comp(__comp, __proj1, __proj2)); + return {std::move(__ret.in1), std::move(__ret.in2), std::move(__ret.out)}; + } + + template < + input_range _Range1, + input_range _Range2, + weakly_incrementable _OutIter, + class _Comp = less, + class _Proj1 = identity, + class _Proj2 = identity> + requires mergeable< + iterator_t<_Range1>, + iterator_t<_Range2>, + _OutIter, + _Comp, + _Proj1, + _Proj2> + _LIBCPP_HIDE_FROM_ABI constexpr set_intersection_result, + borrowed_iterator_t<_Range2>, + _OutIter> + operator()( + _Range1&& __range1, + _Range2&& __range2, + _OutIter __result, + _Comp __comp = {}, + _Proj1 __proj1 = {}, + _Proj2 __proj2 = {}) const { + auto __ret = std::__set_intersection<_RangesIterOps>( + ranges::begin(__range1), + ranges::end(__range1), + ranges::begin(__range2), + ranges::end(__range2), + std::move(__result), + ranges::__make_projected_comp(__comp, __proj1, __proj2)); + return {std::move(__ret.in1), std::move(__ret.in2), std::move(__ret.out)}; + } +}; + +} // namespace __set_intersection + +inline namespace __cpo { + inline constexpr auto set_intersection = __set_intersection::__fn{}; +} // namespace __cpo +} // namespace ranges + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) +#endif // _LIBCPP___ALGORITHM_RANGES_SET_INTERSECTION_H diff --git a/libcxx/include/__algorithm/set_intersection.h b/libcxx/include/__algorithm/set_intersection.h --- a/libcxx/include/__algorithm/set_intersection.h +++ b/libcxx/include/__algorithm/set_intersection.h @@ -11,8 +11,10 @@ #include <__algorithm/comp.h> #include <__algorithm/comp_ref_type.h> +#include <__algorithm/iterator_operations.h> #include <__config> #include <__iterator/iterator_traits.h> +#include <__utility/move.h> #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) # pragma GCC system_header @@ -20,48 +22,69 @@ _LIBCPP_BEGIN_NAMESPACE_STD -template -_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator -__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, - _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) -{ - while (__first1 != __last1 && __first2 != __last2) - { - if (__comp(*__first1, *__first2)) - ++__first1; - else - { - if (!__comp(*__first2, *__first1)) - { - *__result = *__first1; - ++__result; - ++__first1; - } - ++__first2; - } +template +struct __set_intersection_result { + _InIter1 in1; + _InIter2 in2; + _OutIter out; + + // need a constructor as C++03 aggregate init is hard + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 + __set_intersection_result(_InIter1&& __in1, _InIter2&& __in2, _OutIter&& __out) + : in1(std::move(__in1)), in2(std::move(__in2)), out(std::move(__out)) {} +}; + +template < class _IterOper, class _Compare, class _InIter1, class _Sent1, class _InIter2, class _Sent2, class _OutIter> +_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 __set_intersection_result<_InIter1, _InIter2, _OutIter> +__set_intersection( + _InIter1 __first1, _Sent1 __last1, _InIter2 __first2, _Sent2 __last2, _OutIter __result, _Compare&& __comp) { + while (__first1 != __last1 && __first2 != __last2) { + if (__comp(*__first1, *__first2)) + ++__first1; + else { + if (!__comp(*__first2, *__first1)) { + *__result = *__first1; + ++__result; + ++__first1; + } + ++__first2; } - return __result; + } + + return __set_intersection_result<_InIter1, _InIter2, _OutIter>( + _IterOper::next(std::move(__first1), std::move(__last1)), + _IterOper::next(std::move(__first2), std::move(__last2)), + std::move(__result)); } template -inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 -_OutputIterator -set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, - _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) -{ - typedef typename __comp_ref_type<_Compare>::type _Comp_ref; - return _VSTD::__set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp); +inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator set_intersection( + _InputIterator1 __first1, + _InputIterator1 __last1, + _InputIterator2 __first2, + _InputIterator2 __last2, + _OutputIterator __result, + _Compare __comp) { + typedef typename __comp_ref_type<_Compare>::type _Comp_ref; + return std::__set_intersection<_StdIterOps, _Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp).out; } template -inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 -_OutputIterator -set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, - _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result) -{ - return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result, - __less::value_type, - typename iterator_traits<_InputIterator2>::value_type>()); +inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator set_intersection( + _InputIterator1 __first1, + _InputIterator1 __last1, + _InputIterator2 __first2, + _InputIterator2 __last2, + _OutputIterator __result) { + return std::__set_intersection<_StdIterOps>( + __first1, + __last1, + __first2, + __last2, + __result, + __less::value_type, + typename iterator_traits<_InputIterator2>::value_type>()) + .out; } _LIBCPP_END_NAMESPACE_STD diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -549,6 +549,25 @@ set_difference(R1&& r1, R2&& r2, O result, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20 + template + using set_intersection_result = in_in_out_result; // since C++20 + + template S1, input_iterator I2, sentinel_for S2, + weakly_incrementable O, class Comp = ranges::less, + class Proj1 = identity, class Proj2 = identity> + requires mergeable + constexpr set_intersection_result + set_intersection(I1 first1, S1 last1, I2 first2, S2 last2, O result, + Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20 + + template S1, input_iterator I2, sentinel_for S2, + weakly_incrementable O, class Comp = ranges::less, + class Proj1 = identity, class Proj2 = identity> + requires mergeable + constexpr set_intersection_result, borrowed_iterator_t, O> + set_intersection(R1&& r1, R2&& r2, O result, + Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20 + } constexpr bool // constexpr in C++20 @@ -1319,6 +1338,7 @@ #include <__algorithm/ranges_replace_if.h> #include <__algorithm/ranges_reverse.h> #include <__algorithm/ranges_set_difference.h> +#include <__algorithm/ranges_set_intersection.h> #include <__algorithm/ranges_sort.h> #include <__algorithm/ranges_stable_sort.h> #include <__algorithm/ranges_swap_ranges.h> diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in --- a/libcxx/include/module.modulemap.in +++ b/libcxx/include/module.modulemap.in @@ -348,6 +348,7 @@ module ranges_replace_if { private header "__algorithm/ranges_replace_if.h" } module ranges_reverse { private header "__algorithm/ranges_reverse.h" } module ranges_set_difference { private header "__algorithm/ranges_set_difference.h" } + module ranges_set_intersection { private header "__algorithm/ranges_set_intersection.h" } module ranges_sort { private header "__algorithm/ranges_sort.h" } module ranges_stable_sort { private header "__algorithm/ranges_stable_sort.h" } module ranges_swap_ranges { private header "__algorithm/ranges_swap_ranges.h" } diff --git a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp --- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp +++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp @@ -201,8 +201,8 @@ //(void)std::ranges::search_n(a, count, value, Equal(&copies)); assert(copies == 0); (void)std::ranges::set_difference(first, mid, mid, last, first2, Less(&copies)); assert(copies == 0); (void)std::ranges::set_difference(a, b, first2, Less(&copies)); assert(copies == 0); - //(void)std::ranges::set_intersection(first, mid, mid, last, first2, Less(&copies)); assert(copies == 0); - //(void)std::ranges::set_intersection(a, b, first2, Less(&copies)); assert(copies == 0); + (void)std::ranges::set_intersection(first, mid, mid, last, first2, Less(&copies)); assert(copies == 0); + (void)std::ranges::set_intersection(a, b, first2, Less(&copies)); assert(copies == 0); //(void)std::ranges::set_symmetric_difference(first, mid, mid, last, first2, Less(&copies)); assert(copies == 0); //(void)std::ranges::set_symmetric_difference(a, b, first2, Less(&copies)); assert(copies == 0); //(void)std::ranges::set_union(first, mid, mid, last, first2, Less(&copies)); assert(copies == 0); diff --git a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp --- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp +++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp @@ -192,8 +192,8 @@ //(void)std::ranges::search_n(a, count, value, Equal(), Proj(&copies)); assert(copies == 0); (void)std::ranges::set_difference(first, mid, mid, last, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0); (void)std::ranges::set_difference(a, b, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0); - //(void)std::ranges::set_intersection(first, mid, mid, last, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0); - //(void)std::ranges::set_intersection(a, b, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0); + (void)std::ranges::set_intersection(first, mid, mid, last, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0); + (void)std::ranges::set_intersection(a, b, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0); //(void)std::ranges::set_symmetric_difference(first, mid, mid, last, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0); //(void)std::ranges::set_symmetric_difference(a, b, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0); //(void)std::ranges::set_union(first, mid, mid, last, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0); diff --git a/libcxx/test/libcxx/private_headers.verify.cpp b/libcxx/test/libcxx/private_headers.verify.cpp --- a/libcxx/test/libcxx/private_headers.verify.cpp +++ b/libcxx/test/libcxx/private_headers.verify.cpp @@ -146,6 +146,7 @@ #include <__algorithm/ranges_replace_if.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_replace_if.h'}} #include <__algorithm/ranges_reverse.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_reverse.h'}} #include <__algorithm/ranges_set_difference.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_set_difference.h'}} +#include <__algorithm/ranges_set_intersection.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_set_intersection.h'}} #include <__algorithm/ranges_sort.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_sort.h'}} #include <__algorithm/ranges_stable_sort.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_stable_sort.h'}} #include <__algorithm/ranges_swap_ranges.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_swap_ranges.h'}} diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.merge/ranges_merge.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.merge/ranges_merge.pass.cpp --- a/libcxx/test/std/algorithms/alg.sorting/alg.merge/ranges_merge.pass.cpp +++ b/libcxx/test/std/algorithms/alg.sorting/alg.merge/ranges_merge.pass.cpp @@ -205,27 +205,11 @@ // check that ranges::dangling is returned for non-borrowed_range and iterator_t is returned for borrowed_range { - struct NonBorrowedRange { - int* data_; - size_t size_; - - // TODO: std::ranges::merge calls std::ranges::copy - // std::ranges::copy(contiguous_iterator, sentinel_wrapper>, contiguous_iterator) doesn't seem to work. - // It seems that std::ranges::copy calls std::copy, which unwraps contiguous_iterator into int*, - // and then it failed because there is no == between int* and sentinel_wrapper> - using Sent = std::conditional_t, In2, sentinel_wrapper>; - - constexpr NonBorrowedRange(int* d, size_t s) : data_{d}, size_{s} {} - - constexpr In2 begin() const { return In2{data_}; }; - constexpr Sent end() const { return Sent{In2{data_ + size_}}; }; - }; - std::array r1{3, 6, 7, 9}; std::array r2{2, 3, 4}; std::array out; std::same_as::iterator, std::ranges::dangling, int*>> decltype(auto) result = - std::ranges::merge(r1, NonBorrowedRange{r2.data(), r2.size()}, out.data()); + std::ranges::merge(r1, NonBorrowedRange{r2.data(), r2.size()}, out.data()); assert(base(result.in1) == r1.end()); assert(base(result.out) == out.data() + out.size()); assert(std::ranges::equal(out, std::array{2, 3, 3, 4, 6, 7, 9})); diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.set.operations/set.difference/ranges_set_difference.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.set.operations/set.difference/ranges_set_difference.pass.cpp --- a/libcxx/test/std/algorithms/alg.sorting/alg.set.operations/set.difference/ranges_set_difference.pass.cpp +++ b/libcxx/test/std/algorithms/alg.sorting/alg.set.operations/set.difference/ranges_set_difference.pass.cpp @@ -214,27 +214,11 @@ // check that ranges::dangling is returned for non-borrowed_range { - struct NonBorrowedRange { - int* data_; - size_t size_; - - // TODO: std::ranges::set_difference calls std::__copy - // std::ranges::copy(contiguous_iterator, sentinel_wrapper>, contiguous_iterator) doesn't seem to work. - // It seems that std::ranges::copy calls std::copy, which unwraps contiguous_iterator into int*, - // and then it failed because there is no == between int* and sentinel_wrapper> - using Sent = std::conditional_t, In2, sentinel_wrapper>; - - constexpr NonBorrowedRange(int* d, size_t s) : data_{d}, size_{s} {} - - constexpr In2 begin() const { return In2{data_}; }; - constexpr Sent end() const { return Sent{In2{data_ + size_}}; }; - }; - std::array r1{3, 6, 7, 9}; std::array r2{2, 3, 4, 5, 6}; std::array out; std::same_as> decltype(auto) result = - std::ranges::set_difference(NonBorrowedRange{r1.data(), r1.size()}, r2, out.data()); + std::ranges::set_difference(NonBorrowedRange{r1.data(), r1.size()}, r2, out.data()); assert(base(result.out) == out.data() + out.size()); assert(std::ranges::equal(out, std::array{7, 9})); } diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.set.operations/set.difference/ranges_set_difference.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.set.operations/set.intersection/ranges_set_intersection.pass.cpp copy from libcxx/test/std/algorithms/alg.sorting/alg.set.operations/set.difference/ranges_set_difference.pass.cpp copy to libcxx/test/std/algorithms/alg.sorting/alg.set.operations/set.intersection/ranges_set_intersection.pass.cpp --- a/libcxx/test/std/algorithms/alg.sorting/alg.set.operations/set.difference/ranges_set_difference.pass.cpp +++ b/libcxx/test/std/algorithms/alg.sorting/alg.set.operations/set.intersection/ranges_set_intersection.pass.cpp @@ -1,4 +1,3 @@ - //===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. @@ -16,16 +15,16 @@ // weakly_incrementable O, class Comp = ranges::less, // class Proj1 = identity, class Proj2 = identity> // requires mergeable -// constexpr set_difference_result -// set_difference(I1 first1, S1 last1, I2 first2, S2 last2, O result, -// Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20 - +// constexpr set_intersection_result +// set_intersection(I1 first1, S1 last1, I2 first2, S2 last2, O result, +// Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20 +// // template // requires mergeable, iterator_t, O, Comp, Proj1, Proj2> -// constexpr set_difference_result, O> -// set_difference(R1&& r1, R2&& r2, O result, -// Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20 +// constexpr set_intersection_result, borrowed_iterator_t, O> +// set_intersection(R1&& r1, R2&& r2, O result, +// Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20 #include #include @@ -38,15 +37,10 @@ // Test iterator overload's constraints: // ===================================== -template < - class InIter1, - class InIter2, - class OutIter, - class Sent1 = sentinel_wrapper, - class Sent2 = sentinel_wrapper> -concept HasSetDifferenceIter = +template +concept HasSetIntersectionIter = requires(InIter1&& inIter1, InIter2&& inIter2, OutIter&& outIter, Sent1&& sent1, Sent2&& sent2) { - std::ranges::set_difference( + std::ranges::set_intersection( std::forward(inIter1), std::forward(sent1), std::forward(inIter2), @@ -54,55 +48,55 @@ std::forward(outIter)); }; -static_assert(HasSetDifferenceIter); +static_assert(HasSetIntersectionIter); // !std::input_iterator -static_assert(!HasSetDifferenceIter); +static_assert(!HasSetIntersectionIter); // !std::sentinel_for -static_assert(!HasSetDifferenceIter); +static_assert(!HasSetIntersectionIter); // !std::input_iterator -static_assert(!HasSetDifferenceIter); +static_assert(!HasSetIntersectionIter); // !std::sentinel_for -static_assert(!HasSetDifferenceIter); +static_assert(!HasSetIntersectionIter); // !std::weakly_incrementable -static_assert(!HasSetDifferenceIter); +static_assert(!HasSetIntersectionIter); // !std::mergeable -static_assert(!HasSetDifferenceIter); +static_assert(!HasSetIntersectionIter); // Test range overload's constraints: // ===================================== template -concept HasSetDifferenceRange = +concept HasSetIntersectionRange = requires(Range1&& range1, Range2&& range2, OutIter&& outIter) { - std::ranges::set_difference( + std::ranges::set_intersection( std::forward(range1), std::forward(range2), std::forward(outIter)); }; -static_assert(HasSetDifferenceRange, UncheckedRange, int*>); +static_assert(HasSetIntersectionRange, UncheckedRange, int*>); // !std::input_range -static_assert(!HasSetDifferenceRange, UncheckedRange, int*>); +static_assert(!HasSetIntersectionRange, UncheckedRange, int*>); // !std::input_range -static_assert(!HasSetDifferenceRange, UncheckedRange, int*>); +static_assert(!HasSetIntersectionRange, UncheckedRange, int*>); // !std::weakly_incrementable -static_assert(!HasSetDifferenceRange, UncheckedRange, WeaklyIncrementableNotMovable >); +static_assert(!HasSetIntersectionRange, UncheckedRange, WeaklyIncrementableNotMovable >); // !std::mergeable, iterator_t, O, Comp, Proj1, Proj2> -static_assert(!HasSetDifferenceRange< UncheckedRange, UncheckedRange, MoveOnly*>); +static_assert(!HasSetIntersectionRange, UncheckedRange, MoveOnly*>); -using std::ranges::set_difference_result; +using std::ranges::set_intersection_result; template -constexpr void testSetDifferenceImpl(std::array in1, std::array in2, std::array expected) { - // TODO: std::ranges::set_difference calls std::ranges::copy +constexpr void testSetIntersectionImpl(std::array in1, std::array in2, std::array expected) { + // TODO: std::ranges::set_intersection calls std::ranges::copy // std::ranges::copy(contiguous_iterator, sentinel_wrapper>, contiguous_iterator) doesn't seem to work. // It seems that std::ranges::copy calls std::copy, which unwraps contiguous_iterator into int*, // and then it failed because there is no == between int* and sentinel_wrapper> @@ -112,7 +106,7 @@ // iterator overload { std::array out; - std::same_as> decltype(auto) result = std::ranges::set_difference( + std::same_as> decltype(auto) result = std::ranges::set_intersection( In1{in1.data()}, Sent1{In1{in1.data() + in1.size()}}, In2{in2.data()}, @@ -120,7 +114,8 @@ Out{out.data()}); assert(std::ranges::equal(out, expected)); - assert(base(result.in) == in1.data() + in1.size()); + assert(base(result.in1) == in1.data() + in1.size()); + assert(base(result.in2) == in2.data() + in2.size()); assert(base(result.out) == out.data() + out.size()); } @@ -129,11 +124,12 @@ std::array out; std::ranges::subrange r1{In1{in1.data()}, Sent1{In1{in1.data() + in1.size()}}}; std::ranges::subrange r2{In2{in2.data()}, Sent2{In2{in2.data() + in2.size()}}}; - std::same_as> decltype(auto) result = - std::ranges::set_difference(r1, r2, Out{out.data()}); + std::same_as> decltype(auto) result = + std::ranges::set_intersection(r1, r2, Out{out.data()}); assert(std::ranges::equal(out, expected)); - assert(base(result.in) == in1.data() + in1.size()); + assert(base(result.in1) == in1.data() + in1.size()); + assert(base(result.in2) == in2.data() + in2.size()); assert(base(result.out) == out.data() + out.size()); } } @@ -144,48 +140,64 @@ { std::array in1{0, 1, 5, 6, 9, 10}; std::array in2{3, 6, 7, 9, 13, 15, 100}; - std::array expected{0, 1, 5, 10}; - testSetDifferenceImpl(in1, in2, expected); + std::array expected{6, 9}; + testSetIntersectionImpl(in1, in2, expected); } // range 2 shorter than range 1 { std::array in1{2, 6, 8, 12, 15, 16}; std::array in2{0, 2, 8}; - std::array expected{6, 12, 15, 16}; - testSetDifferenceImpl(in1, in2, expected); + std::array expected{2, 8}; + testSetIntersectionImpl(in1, in2, expected); } // range 1 and range 2 has the same length but different elements { std::array in1{2, 6, 8, 12, 15, 16}; std::array in2{0, 2, 8, 15, 17, 19}; - std::array expected{6, 12, 16}; - testSetDifferenceImpl(in1, in2, expected); + std::array expected{2, 8, 15}; + testSetIntersectionImpl(in1, in2, expected); } // range 1 == range 2 { std::array in1{0, 1, 2}; std::array in2{0, 1, 2}; - std::array expected{}; - testSetDifferenceImpl(in1, in2, expected); + std::array expected{0, 1, 2}; + testSetIntersectionImpl(in1, in2, expected); } // range 1 is super set of range 2 { std::array in1{8, 8, 10, 12, 13}; std::array in2{8, 10}; - std::array expected{8, 12, 13}; - testSetDifferenceImpl(in1, in2, expected); + std::array expected{8, 10}; + testSetIntersectionImpl(in1, in2, expected); } // range 2 is super set of range 1 { std::array in1{0, 1, 1}; std::array in2{0, 1, 1, 2, 5}; + std::array expected{0, 1, 1}; + testSetIntersectionImpl(in1, in2, expected); + } + + // range 1 and range 2 have no elements in common + { + std::array in1{7, 7, 9, 12}; + std::array in2{1, 5, 5, 8, 10}; std::array expected{}; - testSetDifferenceImpl(in1, in2, expected); + testSetIntersectionImpl(in1, in2, expected); + } + + // range 1 and range 2 have duplicated equal elements + { + std::array in1{7, 7, 9, 12}; + std::array in2{7, 7, 7, 13}; + std::array expected{7, 7}; + testSetIntersectionImpl(in1, in2, expected); } // range 1 is empty @@ -193,15 +205,15 @@ std::array in1{}; std::array in2{3, 4, 5}; std::array expected{}; - testSetDifferenceImpl(in1, in2, expected); + testSetIntersectionImpl(in1, in2, expected); } // range 2 is empty { std::array in1{3, 4, 5}; std::array in2{}; - std::array expected{3, 4, 5}; - testSetDifferenceImpl(in1, in2, expected); + std::array expected{}; + testSetIntersectionImpl(in1, in2, expected); } // both ranges are empty @@ -209,34 +221,19 @@ std::array in1{}; std::array in2{}; std::array expected{}; - testSetDifferenceImpl(in1, in2, expected); + testSetIntersectionImpl(in1, in2, expected); } // check that ranges::dangling is returned for non-borrowed_range { - struct NonBorrowedRange { - int* data_; - size_t size_; - - // TODO: std::ranges::set_difference calls std::__copy - // std::ranges::copy(contiguous_iterator, sentinel_wrapper>, contiguous_iterator) doesn't seem to work. - // It seems that std::ranges::copy calls std::copy, which unwraps contiguous_iterator into int*, - // and then it failed because there is no == between int* and sentinel_wrapper> - using Sent = std::conditional_t, In2, sentinel_wrapper>; - - constexpr NonBorrowedRange(int* d, size_t s) : data_{d}, size_{s} {} - - constexpr In2 begin() const { return In2{data_}; }; - constexpr Sent end() const { return Sent{In2{data_ + size_}}; }; - }; - std::array r1{3, 6, 7, 9}; - std::array r2{2, 3, 4, 5, 6}; + int r2[] = {2, 3, 4, 5, 6}; std::array out; - std::same_as> decltype(auto) result = - std::ranges::set_difference(NonBorrowedRange{r1.data(), r1.size()}, r2, out.data()); + std::same_as> decltype(auto) result = + std::ranges::set_intersection(NonBorrowedRange{r1.data(), r1.size()}, r2, out.data()); + assert(base(result.in2) == r2 + 5); assert(base(result.out) == out.data() + out.size()); - assert(std::ranges::equal(out, std::array{7, 9})); + assert(std::ranges::equal(out, std::array{3, 6})); } } @@ -284,24 +281,26 @@ // iterator overload { - std::array out; - auto result = std::ranges::set_difference(r1.begin(), r1.end(), r2.begin(), r2.end(), out.data()); + std::array out; + auto result = std::ranges::set_intersection(r1.begin(), r1.end(), r2.begin(), r2.end(), out.data()); - assert(result.in == r1.end()); + assert(result.in1 == r1.end()); + assert(result.in2 == r2.end()); assert(result.out == out.end()); - assert(std::ranges::equal(out, std::array{5, 15, 16})); + assert(std::ranges::equal(out, std::array{3, 8})); assert(std::ranges::all_of(out, &TracedCopy::copiedOnce)); } // range overload { - std::array out; - auto result = std::ranges::set_difference(r1, r2, out.data()); + std::array out; + auto result = std::ranges::set_intersection(r1, r2, out.data()); - assert(result.in == r1.end()); + assert(result.in1 == r1.end()); + assert(result.in2 == r2.end()); assert(result.out == out.end()); - assert(std::ranges::equal(out, std::array{5, 15, 16})); + assert(std::ranges::equal(out, std::array{3, 8})); assert(std::ranges::all_of(out, &TracedCopy::copiedOnce)); } @@ -315,29 +314,29 @@ constexpr auto operator<=>(const IntAndOrder& o) const { return data <=> o.data; } }; - // equal elements should be copied in the original order and only m-n elements are copied + // Stable. If [first1, last1) contains m elements that are equivalent to each other and [first2, last2) + // contains n elements that are equivalent to them, the first min(m, n) elements are copied from the first + // range to the output range, in order. { std::array r1{{{0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4}}}; - std::array r2{{{0, 1}, {0, 2}, {0, 3}}}; + std::array r2{{{0, 5}, {0, 6}, {0, 7}}}; // iterator overload { - std::array out; - std::ranges::set_difference(r1.begin(), r1.end(), r2.begin(), r2.end(), out.data()); + std::array out; + std::ranges::set_intersection(r1.begin(), r1.end(), r2.begin(), r2.end(), out.data()); - assert(std::ranges::equal(out, std::array{0, 0}, {}, &IntAndOrder::data)); - // m-n elements are copied in order - assert(std::ranges::equal(out, std::array{3, 4}, {}, &IntAndOrder::order)); + assert(std::ranges::equal(out, std::array{0, 0, 0}, {}, &IntAndOrder::data)); + assert(std::ranges::equal(out, std::array{0, 1, 2}, {}, &IntAndOrder::order)); } // range overload { - std::array out; - std::ranges::set_difference(r1, r2, out.data()); + std::array out; + std::ranges::set_intersection(r1, r2, out.data()); - assert(std::ranges::equal(out, std::array{0, 0}, {}, &IntAndOrder::data)); - // ID should be in their original order - assert(std::ranges::equal(out, std::array{3, 4}, {}, &IntAndOrder::order)); + assert(std::ranges::equal(out, std::array{0, 0, 0}, {}, &IntAndOrder::data)); + assert(std::ranges::equal(out, std::array{0, 1, 2}, {}, &IntAndOrder::order)); } } @@ -351,55 +350,58 @@ { std::array r1{Data{4}, Data{8}, Data{12}}; std::array r2{Data{8}, Data{9}}; - using Iter1 = std::array::iterator; // iterator overload { - std::array out; - std::same_as> decltype(auto) result = std::ranges::set_difference( + std::array out; + auto result = std::ranges::set_intersection( r1.begin(), r1.end(), r2.begin(), r2.end(), out.data(), [](const Data& x, const Data& y) { return x.data < y.data; }); - assert(std::ranges::equal(out, std::array{4, 12}, {}, &Data::data)); + assert(std::ranges::equal(out, std::array{8}, {}, &Data::data)); - assert(result.in == r1.end()); + assert(result.in1 == r1.end()); + assert(result.in2 == r2.end()); assert(result.out == out.end()); } // range overload { - std::array out; - std::same_as> decltype(auto) result = - std::ranges::set_difference(r1, r2, out.data(), [](const Data& x, const Data& y) { return x.data < y.data; }); + std::array out; + auto result = std::ranges::set_intersection(r1, r2, out.data(), [](const Data& x, const Data& y) { + return x.data < y.data; + }); - assert(std::ranges::equal(out, std::array{4, 12}, {}, &Data::data)); + assert(std::ranges::equal(out, std::array{8}, {}, &Data::data)); - assert(result.in == r1.end()); + assert(result.in1 == r1.end()); + assert(result.in2 == r2.end()); assert(result.out == out.end()); } // member pointer Comparator iterator overload { - std::array out; - std::same_as> decltype(auto) result = - std::ranges::set_difference(r1.begin(), r1.end(), r2.begin(), r2.end(), out.data(), &Data::smallerThan); + std::array out; + auto result = + std::ranges::set_intersection(r1.begin(), r1.end(), r2.begin(), r2.end(), out.data(), &Data::smallerThan); - assert(std::ranges::equal(out, std::array{4, 12}, {}, &Data::data)); + assert(std::ranges::equal(out, std::array{8}, {}, &Data::data)); - assert(result.in == r1.end()); + assert(result.in1 == r1.end()); + assert(result.in2 == r2.end()); assert(result.out == out.end()); } // member pointer Comparator range overload { - std::array out; - std::same_as> decltype(auto) result = - std::ranges::set_difference(r1, r2, out.data(), &Data::smallerThan); + std::array out; + auto result = std::ranges::set_intersection(r1, r2, out.data(), &Data::smallerThan); - assert(std::ranges::equal(out, std::array{4, 12}, {}, &Data::data)); + assert(std::ranges::equal(out, std::array{8}, {}, &Data::data)); - assert(result.in == r1.end()); + assert(result.in1 == r1.end()); + assert(result.in2 == r2.end()); assert(result.out == out.end()); } } @@ -407,71 +409,72 @@ // Test Projection { std::array r1{Data{1}, Data{3}, Data{5}}; - std::array r2{Data{2}, Data{3}, Data{4}}; - using Iter1 = std::array::iterator; + std::array r2{Data{2}, Data{3}, Data{5}}; const auto proj = [](const Data& d) { return d.data; }; // iterator overload { std::array out; - std::same_as> decltype(auto) result = std::ranges::set_difference( + auto result = std::ranges::set_intersection( r1.begin(), r1.end(), r2.begin(), r2.end(), out.data(), std::ranges::less{}, proj, proj); - assert(std::ranges::equal(out, std::array{1, 5}, {}, &Data::data)); + assert(std::ranges::equal(out, std::array{3, 5}, {}, &Data::data)); - assert(result.in == r1.end()); + assert(result.in1 == r1.end()); + assert(result.in2 == r2.end()); assert(result.out == out.end()); } // range overload { std::array out; - std::same_as> decltype(auto) result = - std::ranges::set_difference(r1, r2, out.data(), std::ranges::less{}, proj, proj); + auto result = std::ranges::set_intersection(r1, r2, out.data(), std::ranges::less{}, proj, proj); - assert(std::ranges::equal(out, std::array{1, 5}, {}, &Data::data)); + assert(std::ranges::equal(out, std::array{3, 5}, {}, &Data::data)); - assert(result.in == r1.end()); + assert(result.in1 == r1.end()); + assert(result.in2 == r2.end()); assert(result.out == out.end()); } // member pointer Projection iterator overload { std::array out; - std::same_as> decltype(auto) result = std::ranges::set_difference( + auto result = std::ranges::set_intersection( r1.begin(), r1.end(), r2.begin(), r2.end(), out.data(), {}, &Data::data, &Data::data); - assert(std::ranges::equal(out, std::array{1, 5}, {}, &Data::data)); + assert(std::ranges::equal(out, std::array{3, 5}, {}, &Data::data)); - assert(result.in == r1.end()); + assert(result.in1 == r1.end()); + assert(result.in2 == r2.end()); assert(result.out == out.end()); } // member pointer Projection range overload { std::array out; - std::same_as> decltype(auto) result = - std::ranges::set_difference(r1, r2, out.data(), std::ranges::less{}, &Data::data, &Data::data); + auto result = std::ranges::set_intersection(r1, r2, out.data(), std::ranges::less{}, &Data::data, &Data::data); - assert(std::ranges::equal(out, std::array{1, 5}, {}, &Data::data)); + assert(std::ranges::equal(out, std::array{3, 5}, {}, &Data::data)); - assert(result.in == r1.end()); + assert(result.in1 == r1.end()); + assert(result.in2 == r2.end()); assert(result.out == out.end()); } } // Complexity: At most 2 * ((last1 - first1) + (last2 - first2)) - 1 comparisons and applications of each projection. { - std::array r1{{{4}, {8}, {12}}}; - std::array r2{{{1}, {2}, {3}, {5}, {6}, {7}, {9}, {10}, {11}}}; - std::array expected{4, 8, 12}; + std::array r1{{{1}, {3}, {5}, {7}, {9}}}; + std::array r2{{{2}, {4}, {6}, {8}, {10}}}; + std::array expected{}; const std::size_t maxOperation = 2 * (r1.size() + r2.size()) - 1; // iterator overload { - std::array out; + std::array out{}; std::size_t numberOfComp = 0; std::size_t numberOfProj1 = 0; std::size_t numberOfProj2 = 0; @@ -491,7 +494,7 @@ return d.data; }; - std::ranges::set_difference(r1.begin(), r1.end(), r2.begin(), r2.end(), out.data(), comp, proj1, proj2); + std::ranges::set_intersection(r1.begin(), r1.end(), r2.begin(), r2.end(), out.data(), comp, proj1, proj2); assert(std::ranges::equal(out, expected, {}, &Data::data)); assert(numberOfComp < maxOperation); @@ -501,7 +504,7 @@ // range overload { - std::array out; + std::array out{}; std::size_t numberOfComp = 0; std::size_t numberOfProj1 = 0; std::size_t numberOfProj2 = 0; @@ -521,7 +524,7 @@ return d.data; }; - std::ranges::set_difference(r1, r2, out.data(), comp, proj1, proj2); + std::ranges::set_intersection(r1, r2, out.data(), comp, proj1, proj2); assert(std::ranges::equal(out, expected, {}, &Data::data)); assert(numberOfComp < maxOperation); @@ -536,23 +539,23 @@ bool b; constexpr operator bool() const { return b; } }; - Data r1[] = {{2}, {4}}; + Data r1[] = {{3}, {4}}; Data r2[] = {{3}, {4}, {5}}; const auto comp = [](const Data& x, const Data& y) { return ConvertibleToBool{x.data < y.data}; }; // iterator overload { - std::array out; - std::ranges::set_difference(r1, r1 + 2, r2, r2 + 3, out.data(), comp); - assert(std::ranges::equal(out, std::array{2}, {}, &Data::data)); + std::array out; + std::ranges::set_intersection(r1, r1 + 2, r2, r2 + 3, out.data(), comp); + assert(std::ranges::equal(out, std::array{3, 4}, {}, &Data::data)); } // range overload { - std::array out; - std::ranges::set_difference(r1, r2, out.data(), comp); - assert(std::ranges::equal(out, std::array{2}, {}, &Data::data)); + std::array out; + std::ranges::set_intersection(r1, r2, out.data(), comp); + assert(std::ranges::equal(out, std::array{3, 4}, {}, &Data::data)); } } @@ -563,6 +566,11 @@ test(); static_assert(test()); + // Cannot static_assert on the entire permutation test because it exceeds the constexpr execution step limit + // due to the large number of combination of types of iterators (it is a 3-dimensional cartesian product) + // Instead of having one single static_assert that tests all the combinations, in the runAllIteratorPermutationsTests + // function, it has lots of smaller static_assert and each of them test 2-dimensional cartesian product which is less + // than the step limit. runAllIteratorPermutationsTests(); return 0; diff --git a/libcxx/test/std/algorithms/alg.sorting/sortable_helpers.h b/libcxx/test/std/algorithms/alg.sorting/sortable_helpers.h --- a/libcxx/test/std/algorithms/alg.sorting/sortable_helpers.h +++ b/libcxx/test/std/algorithms/alg.sorting/sortable_helpers.h @@ -15,6 +15,8 @@ #if TEST_STD_VER > 17 #include +#include +#include "test_iterators.h" #endif struct TrivialSortable { @@ -102,6 +104,23 @@ constexpr bool operator==(const TracedCopy& o) const { return data == o.data; } constexpr auto operator<=>(const TracedCopy& o) const { return data <=> o.data; } }; + +template +struct NonBorrowedRange { + int* data_; + size_t size_; + + // TODO: some algorithms calls std::__copy + // std::__copy(contiguous_iterator, sentinel_wrapper>, contiguous_iterator) doesn't seem to work. + // It seems that it unwraps contiguous_iterator into int*, and then it failed because there is no == between int* and + // sentinel_wrapper> + using Sent = std::conditional_t, Iter, sentinel_wrapper>; + + constexpr NonBorrowedRange(int* d, size_t s) : data_{d}, size_{s} {} + + constexpr Iter begin() const { return Iter{data_}; }; + constexpr Sent end() const { return Sent{Iter{data_ + size_}}; }; +}; #endif // TEST_STD_VER > 17 #endif // SORTABLE_HELPERS_H 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 @@ -133,7 +133,7 @@ //static_assert(test(std::ranges::search, a, a)); //static_assert(test(std::ranges::search_n, a, 10, 42)); static_assert(test(std::ranges::set_difference, a, a, a)); -//static_assert(test(std::ranges::set_intersection, a, a, a)); +static_assert(test(std::ranges::set_intersection, a, a, a)); //static_assert(test(std::ranges::set_symmetric_difference, a, a, a)); //static_assert(test(std::ranges::set_union, a, a, a)); //static_assert(test(std::ranges::shuffle, a, g));