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 @@ -64,7 +64,7 @@ Merge,set_difference,Hui Xie,`D128983 `,✅ Merge,set_intersection,Hui Xie,`D129233 `,✅ Merge,set_symmetric_difference,Hui Xie,`D129520 `,✅ -Merge,set_union,Hui Xie,n/a,Not started +Merge,set_union,Hui Xie,`D129657 `,✅ Permutation,remove,Nikolas Klauser,`D128618 `_,✅ Permutation,remove_if,Nikolas Klauser,`D128618 `_,✅ Permutation,reverse,Nikolas Klauser,`D125752 `_,✅ diff --git a/libcxx/include/__algorithm/ranges_set_union.h b/libcxx/include/__algorithm/ranges_set_union.h --- a/libcxx/include/__algorithm/ranges_set_union.h +++ b/libcxx/include/__algorithm/ranges_set_union.h @@ -42,34 +42,68 @@ namespace __set_union { struct __fn { - - template _Sent1, - input_iterator _InIter2, sentinel_for<_InIter2> _Sent2, - weakly_incrementable _OutIter, class _Comp = ranges::less, - class _Proj1 = identity, class _Proj2 = identity> - requires mergeable<_InIter1, _InIter2, _OutIter, _Comp, _Proj1, _Proj2> - _LIBCPP_HIDE_FROM_ABI constexpr - set_union_result<_InIter1, _InIter2, _OutIter> - operator()(_InIter1 __first1, _Sent1 __last1, _InIter2 __first2, _Sent2 __last2, _OutIter __result, _Comp __comp = {}, - _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const { - // TODO: implement - (void)__first1; (void)__last1; (void)__first2; (void)__last2; (void)__result; (void)__comp; (void)__proj1; - (void)__proj2; - return {}; + template < + input_iterator _InIter1, + sentinel_for<_InIter1> _Sent1, + input_iterator _InIter2, + sentinel_for<_InIter2> _Sent2, + weakly_incrementable _OutIter, + class _Comp = ranges::less, + class _Proj1 = identity, + class _Proj2 = identity> + requires mergeable<_InIter1, _InIter2, _OutIter, _Comp, _Proj1, _Proj2> + _LIBCPP_HIDE_FROM_ABI constexpr set_union_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_union( + 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 - requires mergeable, iterator_t<_Range2>, _OutIter, _Comp, _Proj1, _Proj2> - _LIBCPP_HIDE_FROM_ABI constexpr - set_union_result, borrowed_iterator_t<_Range2>, _OutIter> - operator()(_Range1&& __range1, _Range2&& __range2, _OutIter __result, _Comp __comp = {}, - _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const { - // TODO: implement - (void)__range1; (void)__range2; (void)__result; (void)__comp; (void)__proj1; (void)__proj2; - return {}; + template < + input_range _Range1, + input_range _Range2, + weakly_incrementable _OutIter, + class _Comp = ranges::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_union_result, + borrowed_iterator_t<_Range2>, + _OutIter> + operator()( + _Range1&& __range1, + _Range2&& __range2, + _OutIter __result, + _Comp __comp = {}, + _Proj1 __proj1 = {}, + _Proj2 __proj2 = {}) const { + auto __ret = std::__set_union( + 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_union diff --git a/libcxx/include/__algorithm/set_union.h b/libcxx/include/__algorithm/set_union.h --- a/libcxx/include/__algorithm/set_union.h +++ b/libcxx/include/__algorithm/set_union.h @@ -14,6 +14,7 @@ #include <__algorithm/copy.h> #include <__config> #include <__iterator/iterator_traits.h> +#include <__utility/move.h> #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) # pragma GCC system_header @@ -21,50 +22,77 @@ _LIBCPP_BEGIN_NAMESPACE_STD -template -_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator -__set_union(_InputIterator1 __first1, _InputIterator1 __last1, - _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) -{ - for (; __first1 != __last1; ++__result) - { - if (__first2 == __last2) - return _VSTD::copy(__first1, __last1, __result); - if (__comp(*__first2, *__first1)) - { - *__result = *__first2; - ++__first2; - } - else - { - if (!__comp(*__first1, *__first2)) - ++__first2; - *__result = *__first1; - ++__first1; - } +template +struct __set_union_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_union_result(_InIter1&& __in_iter1, _InIter2&& __in_iter2, _OutIter&& __out_iter) + : __in1_(std::move(__in_iter1)), __in2_(std::move(__in_iter2)), __out_(std::move(__out_iter)) {} +}; + +template +_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 __set_union_result<_InIter1, _InIter2, _OutIter> __set_union( + _InIter1 __first1, _Sent1 __last1, _InIter2 __first2, _Sent2 __last2, _OutIter __result, _Compare&& __comp) { + for (; __first1 != __last1; ++__result) { + if (__first2 == __last2) { + auto __ret1 = std::__copy_impl(std::move(__first1), std::move(__last1), std::move(__result)); + return __set_union_result<_InIter1, _InIter2, _OutIter>( + std::move(__ret1.first), std::move(__first2), std::move((__ret1.second))); + } + if (__comp(*__first2, *__first1)) { + *__result = *__first2; + ++__first2; + } else { + if (!__comp(*__first1, *__first2)) { + ++__first2; + } + *__result = *__first1; + ++__first1; } - return _VSTD::copy(__first2, __last2, __result); + } + auto __ret2 = std::__copy_impl(std::move(__first2), std::move(__last2), std::move(__result)); + return __set_union_result<_InIter1, _InIter2, _OutIter>( + std::move(__first1), std::move(__ret2.first), std::move((__ret2.second))); } template -inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 -_OutputIterator -set_union(_InputIterator1 __first1, _InputIterator1 __last1, - _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) -{ - typedef typename __comp_ref_type<_Compare>::type _Comp_ref; - return _VSTD::__set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp); +_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator set_union( + _InputIterator1 __first1, + _InputIterator1 __last1, + _InputIterator2 __first2, + _InputIterator2 __last2, + _OutputIterator __result, + _Compare __comp) { + typedef typename __comp_ref_type<_Compare>::type _Comp_ref; + return std::__set_union<_Comp_ref>( + std::move(__first1), + std::move(__last1), + std::move(__first2), + std::move(__last2), + std::move(__result), + __comp) + .__out_; } template -inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 -_OutputIterator -set_union(_InputIterator1 __first1, _InputIterator1 __last1, - _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result) -{ - return _VSTD::set_union(__first1, __last1, __first2, __last2, __result, - __less::value_type, - typename iterator_traits<_InputIterator2>::value_type>()); +_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator set_union( + _InputIterator1 __first1, + _InputIterator1 __last1, + _InputIterator2 __first2, + _InputIterator2 __last2, + _OutputIterator __result) { + return std::set_union( + std::move(__first1), + std::move(__last1), + std::move(__first2), + std::move(__last2), + std::move(__result), + __less::value_type, + typename iterator_traits<_InputIterator2>::value_type>()); } _LIBCPP_END_NAMESPACE_STD diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -712,6 +712,23 @@ set_symmetric_difference(R1&& r1, R2&& r2, O result, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20 + template + using set_union_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_union_result + set_union(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_union_result, borrowed_iterator_t, O> + set_union(R1&& r1, R2&& r2, O result, Comp comp = {}, + Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20 } constexpr bool // constexpr in C++20 @@ -1493,6 +1510,7 @@ #include <__algorithm/ranges_set_difference.h> #include <__algorithm/ranges_set_intersection.h> #include <__algorithm/ranges_set_symmetric_difference.h> +#include <__algorithm/ranges_set_union.h> #include <__algorithm/ranges_sort.h> #include <__algorithm/ranges_sort_heap.h> #include <__algorithm/ranges_stable_sort.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 @@ -205,8 +205,8 @@ (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); - //(void)std::ranges::set_union(a, b, first2, Less(&copies)); assert(copies == 0); + (void)std::ranges::set_union(first, mid, mid, last, first2, Less(&copies)); assert(copies == 0); + (void)std::ranges::set_union(a, b, first2, Less(&copies)); assert(copies == 0); (void)std::ranges::sort(first, last, Less(&copies)); assert(copies == 0); (void)std::ranges::sort(a, Less(&copies)); assert(copies == 0); (void)std::ranges::sort_heap(first, last, 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 @@ -196,8 +196,8 @@ (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); - //(void)std::ranges::set_union(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); + (void)std::ranges::set_union(a, b, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0); (void)std::ranges::sort(first, last, Less(), Proj(&copies)); assert(copies == 0); (void)std::ranges::sort(a, Less(), Proj(&copies)); assert(copies == 0); (void)std::ranges::sort_heap(first, last, Less(), Proj(&copies)); assert(copies == 0); diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.set.operations/set.union/ranges_set_union.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.set.operations/set.union/ranges_set_union.pass.cpp --- a/libcxx/test/std/algorithms/alg.sorting/alg.set.operations/set.union/ranges_set_union.pass.cpp +++ b/libcxx/test/std/algorithms/alg.sorting/alg.set.operations/set.union/ranges_set_union.pass.cpp @@ -31,17 +31,464 @@ #include #include #include +#include #include "almost_satisfies_types.h" +#include "MoveOnly.h" #include "test_iterators.h" +#include "../../sortable_helpers.h" -// TODO: SFINAE tests. +// Test iterator overload's constraints: +// ===================================== +template +concept HasSetUnionIter = + requires(InIter1&& inIter1, InIter2&& inIter2, OutIter&& outIter, Sent1&& sent1, Sent2&& sent2) { + std::ranges::set_union( + std::forward(inIter1), + std::forward(sent1), + std::forward(inIter2), + std::forward(sent2), + std::forward(outIter)); + }; + +static_assert(HasSetUnionIter); + +// !std::input_iterator +static_assert(!HasSetUnionIter); + +// !std::sentinel_for +static_assert(!HasSetUnionIter); + +// !std::input_iterator +static_assert(!HasSetUnionIter); + +// !std::sentinel_for +static_assert(!HasSetUnionIter); + +// !std::weakly_incrementable +static_assert(!HasSetUnionIter); + +// !std::mergeable +static_assert(!HasSetUnionIter); + +// Test range overload's constraints: +// ===================================== + +template +concept HasSetUnionRange = + requires(Range1&& range1, Range2&& range2, OutIter&& outIter) { + std::ranges::set_union( + std::forward(range1), std::forward(range2), std::forward(outIter)); + }; + +template +using R = UncheckedRange; + +static_assert(HasSetUnionRange, R, int*>); + +// !std::input_range +static_assert(!HasSetUnionRange, R, int*>); + +// !std::input_range +static_assert(!HasSetUnionRange, R, int*>); + +// !std::weakly_incrementable +static_assert(!HasSetUnionRange, R, WeaklyIncrementableNotMovable >); + +// !std::mergeable, iterator_t, O, Comp, Proj1, Proj2> +static_assert(!HasSetUnionRange, R, MoveOnly*>); + +using std::ranges::set_union_result; + +template +constexpr void testSetUnionImpl(std::array in1, std::array in2, std::array expected) { + // TODO: std::ranges::set_union 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 Sent1 = std::conditional_t, In1, sentinel_wrapper>; + using Sent2 = std::conditional_t, In2, sentinel_wrapper>; + + // iterator overload + { + std::array out; + std::same_as> decltype(auto) result = std::ranges::set_union( + In1{in1.data()}, + Sent1{In1{in1.data() + in1.size()}}, + In2{in2.data()}, + Sent2{In2{in2.data() + in2.size()}}, + Out{out.data()}); + assert(std::ranges::equal(out, expected)); + + assert(base(result.in1) == in1.data() + in1.size()); + assert(base(result.in2) == in2.data() + in2.size()); + assert(base(result.out) == out.data() + out.size()); + } + + // range overload + { + 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_union(r1, r2, Out{out.data()}); + assert(std::ranges::equal(out, expected)); + + assert(base(result.in1) == in1.data() + in1.size()); + assert(base(result.in2) == in2.data() + in2.size()); + assert(base(result.out) == out.data() + out.size()); + } +} + +template +constexpr void testImpl() { + // range 1 shorter than range2 + { + std::array in1{0, 1, 5, 6, 9, 10}; + std::array in2{3, 6, 7, 9, 13, 15, 100}; + std::array expected{0, 1, 3, 5, 6, 7, 9, 10, 13, 15, 100}; + testSetUnionImpl(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{0, 2, 6, 8, 12, 15, 16}; + testSetUnionImpl(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{0, 2, 6, 8, 12, 15, 16, 17, 19}; + testSetUnionImpl(in1, in2, expected); + } + + // range 1 == range 2 + { + std::array in1{0, 1, 2}; + std::array in2{0, 1, 2}; + std::array expected{0, 1, 2}; + testSetUnionImpl(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, 8, 10, 12, 13}; + testSetUnionImpl(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, 2, 5}; + testSetUnionImpl(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{1, 5, 5, 7, 7, 8, 9, 10, 12}; + testSetUnionImpl(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, 7, 9, 12, 13}; + testSetUnionImpl(in1, in2, expected); + } + + // range 1 is empty + { + std::array in1{}; + std::array in2{3, 4, 5}; + std::array expected{3, 4, 5}; + testSetUnionImpl(in1, in2, expected); + } + + // range 2 is empty + { + std::array in1{3, 4, 5}; + std::array in2{}; + std::array expected{3, 4, 5}; + testSetUnionImpl(in1, in2, expected); + } + + // both ranges are empty + { + std::array in1{}; + std::array in2{}; + std::array expected{}; + testSetUnionImpl(in1, in2, expected); + } + + // check that ranges::dangling is returned for non-borrowed_range + { + std::array r1{3, 6, 7, 9}; + int r2[] = {2, 3, 4, 5, 6}; + std::array out; + std::same_as> decltype(auto) result = + std::ranges::set_union(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{2, 3, 4, 5, 6, 7, 9})); + } +} + +template +constexpr void withAllPermutationsOfInIter1() { + // C++17 InputIterator may or may not satisfy std::input_iterator + testImpl, InIter2, OutIter>(); + testImpl, InIter2, OutIter>(); + testImpl, InIter2, OutIter>(); + testImpl, InIter2, OutIter>(); + testImpl, InIter2, OutIter>(); +} + +template +constexpr bool withAllPermutationsOfInIter1AndInIter2() { + withAllPermutationsOfInIter1, OutIter>(); + withAllPermutationsOfInIter1, OutIter>(); + withAllPermutationsOfInIter1, OutIter>(); + withAllPermutationsOfInIter1, OutIter>(); + withAllPermutationsOfInIter1, OutIter>(); + return true; +} + +constexpr void runAllIteratorPermutationsTests() { + withAllPermutationsOfInIter1AndInIter2>(); + withAllPermutationsOfInIter1AndInIter2>(); + withAllPermutationsOfInIter1AndInIter2>(); + withAllPermutationsOfInIter1AndInIter2>(); + withAllPermutationsOfInIter1AndInIter2>(); + withAllPermutationsOfInIter1AndInIter2>(); + + static_assert(withAllPermutationsOfInIter1AndInIter2>()); + static_assert(withAllPermutationsOfInIter1AndInIter2>()); + static_assert(withAllPermutationsOfInIter1AndInIter2>()); + static_assert(withAllPermutationsOfInIter1AndInIter2>()); + static_assert(withAllPermutationsOfInIter1AndInIter2>()); + static_assert(withAllPermutationsOfInIter1AndInIter2>()); +} constexpr bool test() { - // TODO: main tests. - // TODO: A custom comparator works. - // TODO: A custom projection works. + // check that every element is copied exactly once + { + std::array r1{3, 5, 8, 15, 16}; + std::array r2{1, 3, 8}; + // iterator overload + { + std::array out; + auto result = std::ranges::set_union(r1.begin(), r1.end(), r2.begin(), r2.end(), out.data()); + + assert(result.in1 == r1.end()); + assert(result.in2 == r2.end()); + assert(result.out == out.end()); + assert(std::ranges::equal(out, std::array{1, 3, 5, 8, 15, 16})); + + assert(std::ranges::all_of(out, &TracedCopy::copiedOnce)); + } + + // range overload + { + std::array out; + auto result = std::ranges::set_union(r1, r2, out.data()); + + assert(result.in1 == r1.end()); + assert(result.in2 == r2.end()); + assert(result.out == out.end()); + assert(std::ranges::equal(out, std::array{1, 3, 5, 8, 15, 16})); + + assert(std::ranges::all_of(out, &TracedCopy::copiedOnce)); + } + } + + struct IntAndOrder { + int data; + int order; + + constexpr auto operator==(const IntAndOrder& o) const { return data == o.data; } + constexpr auto operator<=>(const IntAndOrder& o) const { return data <=> o.data; } + }; + + // Stable ([algorithm.stable]). If [first1, last1) contains m elements that are + // equivalent to each other and [first2, last2) contains n elements that are + // equivalent to them, then all m elements from the first range are copied to the + // output range, in order, and then the final max(n−m,0) elements from the second + // range are copied to the output range, in order. + { + std::array r1{{{0, 0}, {0, 1}, {0, 2}}}; + std::array r2{{{0, 3}, {0, 4}, {0, 5}, {0, 6}, {0, 7}}}; + + // iterator overload + { + std::array out; + std::ranges::set_union(r1.begin(), r1.end(), r2.begin(), r2.end(), out.data()); + + assert(std::ranges::equal(out, std::array{0, 0, 0, 0, 0}, {}, &IntAndOrder::data)); + assert(std::ranges::equal(out, std::array{0, 1, 2, 6, 7}, {}, &IntAndOrder::order)); + } + + // range overload + { + std::array out; + std::ranges::set_union(r1, r2, out.data()); + + assert(std::ranges::equal(out, std::array{0, 0, 0, 0, 0}, {}, &IntAndOrder::data)); + assert(std::ranges::equal(out, std::array{0, 1, 2, 6, 7}, {}, &IntAndOrder::order)); + } + } + + struct Data { + int data; + }; + + // Test custom comparator + { + std::array r1{Data{4}, Data{8}, Data{12}}; + std::array r2{Data{8}, Data{9}}; + + // iterator overload + { + std::array out; + auto result = std::ranges::set_union( + 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, 8, 9, 12}, {}, &Data::data)); + + assert(result.in1 == r1.end()); + assert(result.in2 == r2.end()); + assert(result.out == out.end()); + } + + // range overload + { + std::array out; + auto result = std::ranges::set_union(r1, r2, out.data(), [](const Data& x, const Data& y) { + return x.data < y.data; + }); + + assert(std::ranges::equal(out, std::array{4, 8, 9, 12}, {}, &Data::data)); + + assert(result.in1 == r1.end()); + assert(result.in2 == r2.end()); + assert(result.out == out.end()); + } + } + + // Test Projection + { + std::array r1{Data{1}, Data{3}, Data{5}}; + std::array r2{Data{2}, Data{3}, Data{5}}; + + const auto proj = [](const Data& d) { return d.data; }; + + // iterator overload + { + std::array out; + auto result = std::ranges::set_union( + r1.begin(), r1.end(), r2.begin(), r2.end(), out.data(), std::ranges::less{}, proj, proj); + + assert(std::ranges::equal(out, std::array{1, 2, 3, 5}, {}, &Data::data)); + + assert(result.in1 == r1.end()); + assert(result.in2 == r2.end()); + assert(result.out == out.end()); + } + + // range overload + { + std::array out; + auto result = std::ranges::set_union(r1, r2, out.data(), std::ranges::less{}, proj, proj); + + assert(std::ranges::equal(out, std::array{1, 2, 3, 5}, {}, &Data::data)); + + 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. + { + struct CompProjs { + std::size_t numberOfComp = 0; + std::size_t numberOfProj1 = 0; + std::size_t numberOfProj2 = 0; + + constexpr auto comp() { + return [this](int x, int y) { + ++numberOfComp; + return x < y; + }; + } + + constexpr auto proj1() { + return [this](const Data& d) { + ++numberOfProj1; + return d.data; + }; + } + + constexpr auto proj2() { + return [this](const Data& d) { + ++numberOfProj2; + return d.data; + }; + } + }; + + std::array r1{{{0}, {1}, {2}}}; + std::array r2{{{0}, {2}, {2}, {5}}}; + std::array expected{0, 1, 2, 2, 5}; + + const std::size_t maxOperation = 2 * (r1.size() + r2.size()) - 1; + + // iterator overload + { + std::array out; + CompProjs compProjs{}; + + std::ranges::set_union( + r1.begin(), + r1.end(), + r2.begin(), + r2.end(), + out.data(), + compProjs.comp(), + compProjs.proj1(), + compProjs.proj2()); + + assert(std::ranges::equal(out, expected, {}, &Data::data)); + assert(compProjs.numberOfComp < maxOperation); + assert(compProjs.numberOfProj1 < maxOperation); + assert(compProjs.numberOfProj2 < maxOperation); + } + + // range overload + { + std::array out; + CompProjs compProjs{}; + + std::ranges::set_union(r1, r2, out.data(), compProjs.comp(), compProjs.proj1(), compProjs.proj2()); + + assert(std::ranges::equal(out, expected, {}, &Data::data)); + assert(compProjs.numberOfComp < maxOperation); + assert(compProjs.numberOfProj1 < maxOperation); + assert(compProjs.numberOfProj2 < maxOperation); + } + } + return true; } @@ -49,5 +496,12 @@ 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/ranges_robust_against_nonbool_predicates.compile.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_nonbool_predicates.compile.pass.cpp --- a/libcxx/test/std/algorithms/ranges_robust_against_nonbool_predicates.compile.pass.cpp +++ b/libcxx/test/std/algorithms/ranges_robust_against_nonbool_predicates.compile.pass.cpp @@ -153,7 +153,7 @@ in2_out_pred(std::ranges::set_difference, in, in2, out, binary_pred); in2_out_pred(std::ranges::set_intersection, in, in2, out, binary_pred); in2_out_pred(std::ranges::set_symmetric_difference, in, in2, out, binary_pred); - //in2_out_pred(std::ranges::set_union, in, in2, out, binary_pred); + in2_out_pred(std::ranges::set_union, in, in2, out, binary_pred); in_pred(std::ranges::remove_if, in, unary_pred); //in_pred(std::ranges::unique, in, binary_pred); //in_pred(std::ranges::partition, in, binary_pred); diff --git a/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.compile.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.compile.pass.cpp --- a/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.compile.pass.cpp +++ b/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.compile.pass.cpp @@ -194,7 +194,7 @@ in2_out_pred(std::ranges::set_difference, in, in2, out, &Foo::binary_pred, &Bar::val, &Bar::val); in2_out_pred(std::ranges::set_intersection, in, in2, out, &Foo::binary_pred, &Bar::val, &Bar::val); in2_out_pred(std::ranges::set_symmetric_difference, in, in2, out, &Foo::binary_pred, &Bar::val, &Bar::val); - //in2_out_pred(std::ranges::set_union, in, in2, out, &Foo::binary_pred, &Bar::val, &Bar::val); + in2_out_pred(std::ranges::set_union, in, in2, out, &Foo::binary_pred, &Bar::val, &Bar::val); in_val(std::ranges::remove, in, x, &Bar::val); in_pred(std::ranges::remove_if, in, &Foo::unary_pred, &Bar::val); // `reverse` has neither a projection nor a predicate. 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 @@ -136,7 +136,7 @@ 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_symmetric_difference, a, a, a)); -//static_assert(test(std::ranges::set_union, a, a, a)); +static_assert(test(std::ranges::set_union, a, a, a)); //static_assert(test(std::ranges::shuffle, a, g)); static_assert(test(std::ranges::sort, a)); static_assert(test(std::ranges::sort_heap, a));