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 @@ -60,11 +60,11 @@ Write,unique_copy,Not assigned,n/a,Not started Write,partition_copy,Not assigned,n/a,Not started Write,partial_sort_copy,Not assigned,n/a,Not started -Merge,merge,Not assigned,n/a,Not started -Merge,set_difference,Not assigned,n/a,Not started -Merge,set_intersection,Not assigned,n/a,Not started -Merge,set_symmetric_difference,Not assigned,n/a,Not started -Merge,set_union,Not assigned,n/a,Not started +Merge,merge,Hui Xie,n/a,✅ +Merge,set_difference,Hui Xie,n/a,Not started +Merge,set_intersection,Hui Xie,n/a,Not started +Merge,set_symmetric_difference,Hui Xie,n/a,Not started +Merge,set_union,Not assigned,Hui Xie,Not started Permutation,remove,Not assigned,n/a,Not started Permutation,remove_if,Not assigned,n/a,Not started Permutation,reverse,Nikolas Klauser,`D125752 `_,✅ diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -94,6 +94,7 @@ __algorithm/ranges_lower_bound.h __algorithm/ranges_max.h __algorithm/ranges_max_element.h + __algorithm/ranges_merge.h __algorithm/ranges_min.h __algorithm/ranges_min_element.h __algorithm/ranges_minmax.h diff --git a/libcxx/include/__algorithm/ranges_merge.h b/libcxx/include/__algorithm/ranges_merge.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/ranges_merge.h @@ -0,0 +1,142 @@ +//===----------------------------------------------------------------------===// +// +// 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_MERGE_H +#define _LIBCPP___ALGORITHM_RANGES_MERGE_H + +#include "__algorithm/in_in_out_result.h" +#include "__algorithm/ranges_copy.h" +#include "__iterator/concepts.h" +#include "__iterator/mergeable.h" +#include "__functional/identity.h" +#include "__functional/invoke.h" +#include "__functional/ranges_operations.h" +#include "__utility/move.h" +#include "__ranges/access.h" +#include "__ranges/concepts.h" +#include "__ranges/dangling.h" +#include "__type_traits/decay.h" +#include <__config> + +#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 merge_result = in_in_out_result<_InIter1, _InIter2, _OutIter>; + +template < + class _InIter1, + class _Sent1, + class _InIter2, + class _Sent2, + class _OutIter, + class _Comp, + class _Proj1, + class _Proj2> +_LIBCPP_HIDE_FROM_ABI constexpr ranges::merge_result, decay_t<_InIter2>, decay_t<_OutIter>> +__merge_impl( + _InIter1&& __first1, + _Sent1&& __last1, + _InIter2&& __first2, + _Sent2&& __last2, + _OutIter&& __result, + _Comp&& __comp, + _Proj1&& __proj1, + _Proj2&& __proj2) { + for (; __first1 != __last1 && __first2 != __last2; ++__result) { + if (std::invoke(__comp, std::invoke(__proj2, *__first2), std::invoke(__proj1, *__first1))) { + *__result = *__first2; + ++__first2; + } else { + *__result = *__first1; + ++__first1; + } + } + auto __ret1 = ranges::copy(std::move(__first1), std::move(__last1), std::move(__result)); + auto __ret2 = ranges::copy(std::move(__first2), std::move(__last2), std::move(__ret1.out)); + return {std::move(__ret1.in), std::move(__ret2.in), std::move(__ret2.out)}; +} + +namespace __merge { + + struct __fn { + 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 std::mergeable<_InIter1, _InIter2, _OutIter, _Comp, _Proj1, _Proj2> + _LIBCPP_HIDE_FROM_ABI constexpr ranges::merge_result<_InIter1, _InIter2, _OutIter> operator()( + _InIter1 __first1, + _Sent1 __last1, + _InIter2 __first2, + _Sent2 __last2, + _OutIter __result, + _Comp __comp = {}, + _Proj1 __proj1 = {}, + _Proj2 __proj2 = {}) const { + return ranges::__merge_impl(__first1, __last1, __first2, __last2, __result, __comp, __proj1, __proj2); + } + + template < + ranges::input_range _Range1, + ranges::input_range _Range2, + std::weakly_incrementable _OutIter, + class _Comp = ranges::less, + class _Proj1 = identity, + class _Proj2 = identity > + requires std::mergeable< + ranges::iterator_t<_Range1>, + ranges::iterator_t<_Range2>, + _OutIter, + _Comp, + _Proj1, + _Proj2> _LIBCPP_HIDE_FROM_ABI constexpr ranges:: + merge_result, ranges::borrowed_iterator_t<_Range2>, _OutIter> + operator()( + _Range1&& __range1, + _Range2&& __range2, + _OutIter __result, + _Comp __comp = {}, + _Proj1 __proj1 = {}, + _Proj2 __proj2 = {}) const { + return ranges::__merge_impl( + ranges::begin(__range1), + ranges::end(__range1), + ranges::begin(__range2), + ranges::end(__range2), + std::move(__result), + std::move(__comp), + std::move(__proj1), + std::move(__proj2)); + } + }; + +} // namespace __merge + +inline namespace __cpo { + inline constexpr auto merge = __merge::__fn{}; +} // namespace __cpo +} // namespace ranges + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +#endif // _LIBCPP___ALGORITHM_RANGES_MERGE_H diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -470,6 +470,23 @@ constexpr ranges::move_result, O> ranges::move(R&& r, O result); // since C++20 + template + using merge_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 merge_result + merge(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 merge_result, borrowed_iterator_t, O> + merge(R1&& r1, R2&& r2, O result, + Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20 } @@ -1216,6 +1233,7 @@ #include <__algorithm/ranges_minmax.h> #include <__algorithm/ranges_minmax_element.h> #include <__algorithm/ranges_mismatch.h> +#include <__algorithm/ranges_merge.h> #include <__algorithm/ranges_move.h> #include <__algorithm/ranges_move_backward.h> #include <__algorithm/ranges_none_of.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 @@ -338,6 +338,7 @@ module ranges_minmax { private header "__algorithm/ranges_minmax.h" } module ranges_minmax_element { private header "__algorithm/ranges_minmax_element.h" } module ranges_mismatch { private header "__algorithm/ranges_mismatch.h" } + module ranges_merge { private header "__algorithm/ranges_merge.h" } module ranges_move { private header "__algorithm/ranges_move.h" } module ranges_move_backward { private header "__algorithm/ranges_move_backward.h" } module ranges_none_of { private header "__algorithm/ranges_none_of.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 @@ -79,7 +79,7 @@ { void *a[10] = {}; void *b[10] = {}; - //void *half[5] = {}; + void *half[5] = {}; void **first = a; void **mid = a+5; void **last = a+10; @@ -151,8 +151,8 @@ (void)std::ranges::max(a, Less(&copies)); assert(copies == 0); (void)std::ranges::max_element(first, last, Less(&copies)); assert(copies == 0); (void)std::ranges::max_element(a, Less(&copies)); assert(copies == 0); - //(void)std::ranges::merge(first, mid, mid, last, first2, Less(&copies)); assert(copies == 0); - //(void)std::ranges::merge(half, half, b, Less(&copies)); assert(copies == 0); + (void)std::ranges::merge(first, mid, mid, last, first2, Less(&copies)); assert(copies == 0); + (void)std::ranges::merge(half, half, b, Less(&copies)); assert(copies == 0); (void)std::ranges::min(value, value, Less(&copies)); assert(copies == 0); (void)std::ranges::min({ value, value }, Less(&copies)); assert(copies == 0); (void)std::ranges::min(a, 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 @@ -61,7 +61,7 @@ { T a[10] = {}; T b[10] = {}; - //T half[5] = {}; + T half[5] = {}; T *first = a; T *mid = a+5; T *last = a+10; @@ -134,8 +134,8 @@ (void)std::ranges::max(a, Less(), Proj(&copies)); assert(copies == 0); (void)std::ranges::max_element(first, last, Less(), Proj(&copies)); assert(copies == 0); (void)std::ranges::max_element(a, Less(), Proj(&copies)); assert(copies == 0); - //(void)std::ranges::merge(first, mid, mid, last, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0); - //(void)std::ranges::merge(half, half, b, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0); + (void)std::ranges::merge(first, mid, mid, last, first2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0); + (void)std::ranges::merge(half, half, b, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0); (void)std::ranges::min(T(), T(), Less(), Proj(&copies)); assert(copies == 0); (void)std::ranges::min({ T(), T() }, Less(), Proj(&copies)); assert(copies == 0); (void)std::ranges::min(a, Less(), 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 @@ -136,6 +136,7 @@ #include <__algorithm/ranges_minmax.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_minmax.h'}} #include <__algorithm/ranges_minmax_element.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_minmax_element.h'}} #include <__algorithm/ranges_mismatch.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_mismatch.h'}} +#include <__algorithm/ranges_merge.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_merge.h'}} #include <__algorithm/ranges_move.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_move.h'}} #include <__algorithm/ranges_move_backward.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_move_backward.h'}} #include <__algorithm/ranges_none_of.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_none_of.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 new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.sorting/alg.merge/ranges_merge.pass.cpp @@ -0,0 +1,468 @@ +//===----------------------------------------------------------------------===// +// +// 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-has-no-incomplete-ranges + +// template S1, input_­iterator I2, sentinel_­for S2, +// weakly_­incrementable O, class Comp = ranges::less, class Proj1 = identity, +// class Proj2 = identity> +// requires mergeable +// constexpr merge_result +// merge(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 merge_result, borrowed_iterator_t, O> +// merge(R1&& r1, R2&& r2, O result, +// Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20 + +#include +#include + +#include "almost_satisfies_types.h" +#include "MoveOnly.h" +#include "test_iterators.h" + +template +concept canMerge = requires(Args&&... args) { std::ranges::merge(std::forward(args)...); }; + +// Test iterator overload's constraints: +// ===================================== + +template +struct Func; + +template +struct Func : Func... { + using Func::operator()...; + R operator()(Args...) const; +}; + +struct Foo {}; +struct Bar {}; + +using CompareOverloadSet = Func< + bool(const Foo&, const Bar&), + bool(const Bar&, const Foo&), + bool(const Foo&, const Foo&), + bool(const Bar&, const Bar&)>; + +// Positive tests +static_assert(canMerge); + +// Negative tests: !std::input_iterator +static_assert(!canMerge, sentinel_wrapper>, int*, int*, int*>); +static_assert(!canMerge, int*, int*, int*>); + +// Negative tests: !std::sentinel_for +static_assert(!canMerge); +static_assert(!canMerge, int*, int*, int*, int*>); +static_assert(!canMerge, int*, int*, int*>); + +// Negative tests: !std::input_iterator +static_assert(!canMerge, sentinel_wrapper>, int*>); +static_assert(!canMerge, int*>); + +// Negative tests: !std::sentinel_for +static_assert(!canMerge); +static_assert(!canMerge, int*, int*>); +static_assert(!canMerge, int*>); + +// Negative tests: !std::weakly_incrementable +static_assert(!canMerge); + +// Negative tests: !std::indirectly_copyable +static_assert(!canMerge); +static_assert(!canMerge); +static_assert(!canMerge); + +// Negative tests: !std::indirectly_copyable +static_assert(!canMerge); + +// Negative tests: !std::indirect_strict_weak_order, std::projected> +static_assert(!canMerge>); +static_assert(!canMerge>); +static_assert(!canMerge>); +static_assert( + !canMerge, Func, Func>, + "std::relation requires the predicate can be applied to all combination of Foo and Bar"); + +// Test range overload's constraints: +// ===================================== + +template > +using Range = UncheckedRange; + +// Positive tests +static_assert(canMerge, Range, int*>); + +// Negative tests: !std::input_range +static_assert(!canMerge>, Range, int*>); +static_assert(!canMerge, Range, int*>); + +// Negative tests: !std::input_range +static_assert(!canMerge, Range>, int*>); +static_assert(!canMerge, Range, int*>); + +// Negative tests: !std::weakly_incrementable +static_assert(!canMerge, Range, WeaklyIncrementableNotMovable>); + +// Negative tests: !std::indirectly_copyable, Out> +static_assert(!canMerge, Range, const int*>); +static_assert(!canMerge, Range, Foo*, CompareOverloadSet>); +static_assert(!canMerge, Range, MoveOnly*>); + +// Negative tests: !std::indirectly_copyable, Out> +static_assert(!canMerge, Range, Foo*, CompareOverloadSet>); + +// Negative tests: !std::indirect_strict_weak_order, Proj1>, std::projected, Proj2>> +static_assert(!canMerge, Range, int*, Func>); +static_assert(!canMerge, Range, int*, std::ranges::less, Func>); +static_assert(!canMerge, Range, int*, std::ranges::less, std::identity, Func>); +static_assert( + !canMerge, Range, int*, Func, Func, Func>, + "std::relation requires the predicate can be applied to all combination of Foo and Bar"); + +// test the range is correctly merged for different kinds of input ranges and output iterator +template +struct type_list {}; + +template +constexpr auto cartesian_product_impl( + std::tuple, std::tuple, std::tuple, std::index_sequence) + -> type_list< type_list< + std::tuple_element_t>, + std::tuple_element_t>, + std::tuple_element_t> >... >; + +template +constexpr auto +cartesian_product(type_list, type_list, type_list) -> decltype(cartesian_product_impl( + std::declval>(), + std::declval>(), + std::declval>(), + std::make_index_sequence())) { + return {}; +} + +template +constexpr void testMergeCorrectImpl(type_list) { + std::array i1{0, 1, 5, 6, 9, 10}; + std::array i2{3, 6, 7, 9, 13, 15, 100}; + std::array expected{0, 1, 3, 5, 6, 6, 7, 9, 9, 10, 13, 15, 100}; + + // TODO: std::ranges::merge calls std::ranges::copy + // std::ranges::copy(contiguous_iterator, sentinel_wrapper>, contiguous_iteartor) 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::merge( + In1{i1.data()}, + Sent1{In1{i1.data() + i1.size()}}, + In2{i2.data()}, + Sent2{In2{i2.data() + i2.size()}}, + Out{out.data()}); + assert(std::ranges::equal(out, expected)); + + assert(base(result.in1) == i1.data() + i1.size()); + assert(base(result.in2) == i2.data() + i2.size()); + assert(base(result.out) == out.data() + out.size()); + } + + // range overload + { + std::array out; + std::ranges::subrange r1{In1{i1.data()}, Sent1{In1{i1.data() + i1.size()}}}; + std::ranges::subrange r2{In2{i2.data()}, Sent2{In2{i2.data() + i2.size()}}}; + std::same_as> decltype(auto) result = + std::ranges::merge(r1, r2, Out{out.data()}); + assert(std::ranges::equal(out, expected)); + + assert(base(result.in1) == i1.data() + i1.size()); + assert(base(result.in2) == i2.data() + i2.size()); + assert(base(result.out) == out.data() + out.size()); + } +} + +template +constexpr void runAllMergeCorrectTests(type_list) { + (testMergeCorrectImpl(Sigs{}), ...); +} + +struct TracedCopyMove { + int copy_ctor = 0; + int move_ctor = 0; + int copy_assign = 0; + int move_assign = 0; + + int data = 0; + + constexpr TracedCopyMove() = default; + constexpr TracedCopyMove(int i) : data(i) {} + constexpr TracedCopyMove(const TracedCopyMove& other) + : copy_ctor(other.copy_ctor + 1), + move_ctor(other.move_ctor), + copy_assign(other.copy_assign), + move_assign(other.move_assign), + data(other.data) {} + + constexpr TracedCopyMove(TracedCopyMove&& other) + : copy_ctor(other.copy_ctor), + move_ctor(other.move_ctor + 1), + copy_assign(other.copy_assign), + move_assign(other.move_assign), + data(other.data) {} + + constexpr TracedCopyMove& operator=(const TracedCopyMove& other) { + copy_ctor = other.copy_ctor; + move_ctor = other.move_ctor; + copy_assign = other.copy_assign + 1; + move_assign = other.move_assign; + data = other.data; + return *this; + } + + constexpr TracedCopyMove& operator=(TracedCopyMove&& other) { + copy_ctor = other.copy_ctor; + move_ctor = other.move_ctor; + copy_assign = other.copy_assign; + move_assign = other.move_assign + 1; + data = other.data; + return *this; + } + + friend constexpr bool operator==(const TracedCopyMove& x, const TracedCopyMove& y) { return x.data == y.data; } + friend constexpr auto operator<=>(const TracedCopyMove& x, const TracedCopyMove& y) { return x.data <=> y.data; } +}; + +struct IntAndID { + int data; + int id; + + friend constexpr auto operator==(const IntAndID& x, const IntAndID& y) { return x.data == y.data; } + friend constexpr auto operator<=>(const IntAndID& x, const IntAndID& y) { return x.data <=> y.data; } +}; + +constexpr bool test() { + // Test merge produces correct result for different kinds of iterators + { + using In1s = type_list< + cpp20_input_iterator, + forward_iterator, + bidirectional_iterator, + random_access_iterator, + contiguous_iterator >; + + using In2s = type_list< + cpp20_input_iterator, + forward_iterator, + bidirectional_iterator, + random_access_iterator, + contiguous_iterator >; + + using Outs = type_list< + cpp17_output_iterator, + cpp20_output_iterator, + cpp17_input_iterator, + cpp20_input_iterator, + forward_iterator, + bidirectional_iterator, + random_access_iterator, + contiguous_iterator>; + runAllMergeCorrectTests(cartesian_product(In1s{}, In2s{}, Outs{})); + } + + // check that ranges::dangling is returned for non-borrowed_range and iterator_t is returned for borrowed_range + { + std::array r1{3, 6, 7, 9}; + std::array out; + std::same_as< + std::ranges:: + merge_result&>, std::ranges::dangling, int*>> decltype(auto) + result = std::ranges::merge(r1, std::array{2, 3, 4}, out.data()); + assert(result.in1 == r1.end()); + assert(result.out == out.data() + out.size()); + assert(std::ranges::equal(out, std::array{2, 3, 3, 4, 6, 7, 9})); + } + + // check that every element is copied exactly once + { + std::array r1{TracedCopyMove{3}, TracedCopyMove{5}, TracedCopyMove{8}}; + std::array r2{TracedCopyMove{1}, TracedCopyMove{3}, TracedCopyMove{8}}; + using iter = std::ranges::iterator_t&>; + + const auto verify = [&](const auto& result, const auto& out) { + assert(result.in1 == r1.end()); + assert(result.in2 == r2.end()); + assert(result.out == out.end()); + assert(std::ranges::equal(out, std::array{1, 3, 3, 5, 8, 8})); + + std::ranges::all_of(out, [](const TracedCopyMove& e) { + return e.copy_assign == 1 && e.move_assign == 0 && e.copy_ctor == 0 && e.move_ctor == 0; + }); + }; + + // iterator overload + { + std::array out; + std::same_as> decltype(auto) result = + std::ranges::merge(r1.begin(), r1.end(), r2.begin(), r2.end(), out.data()); + verify(result, out); + } + + // range overload + { + std::array out; + std::same_as> decltype(auto) result = + std::ranges::merge(r1, r2, out.data()); + verify(result, out); + } + } + + // Algorithm is stable: equal elements should merged in the original order + { + std::array r1{IntAndID{0, 0}, IntAndID{0, 1}, IntAndID{0, 2}}; + std::array r2{IntAndID{1, 0}, IntAndID{1, 1}, IntAndID{1, 2}}; + + const auto verify = [&](const std::array& out) { + assert(std::ranges::equal(out, std::array{0, 0, 0, 1, 1, 1}, std::ranges::equal_to{}, &IntAndID::data)); + + // ID should be in their original order + assert(std::ranges::equal(out, std::array{0, 1, 2, 0, 1, 2}, std::ranges::equal_to{}, &IntAndID::id)); + }; + + // iteartor overload + { + std::array out; + std::ranges::merge(r1.begin(), r1.end(), r2.begin(), r2.end(), out.data()); + verify(out); + } + + // range overload + { + std::array out; + std::ranges::merge(r1, r2, out.data()); + verify(out); + } + } + + // Equal elements in R1 should be merged before equal elements in R2 + { + std::array r1{IntAndID{0, 1}, IntAndID{1, 1}, IntAndID{2, 1}}; + std::array r2{IntAndID{0, 2}, IntAndID{1, 2}, IntAndID{2, 2}}; + const auto verify = [&](const std::array& out) { + assert(std::ranges::equal(out, std::array{0, 0, 1, 1, 2, 2}, std::ranges::equal_to{}, &IntAndID::data)); + + // ID 1 (from R1) should be in front of ID 2 (from R2) + assert(std::ranges::equal(out, std::array{1, 2, 1, 2, 1, 2}, std::ranges::equal_to{}, &IntAndID::id)); + }; + + // iteartor overload + { + std::array out; + std::ranges::merge(r1.begin(), r1.end(), r2.begin(), r2.end(), out.data()); + verify(out); + } + + // range overload + { + std::array out; + std::ranges::merge(r1, r2, out.data()); + verify(out); + } + } + + struct Data { + int data; + }; + // Test custom comparator + { + std::array r1{Data{4}, Data{8}, Data{12}}; + std::array r2{Data{5}, Data{9}}; + using Iter1 = std::ranges::iterator_t; + using Iter2 = std::ranges::iterator_t; + + const auto verify = [&](const auto& result, const std::array& out) { + assert(std::ranges::equal(out, std::array{4, 5, 8, 9, 12}, std::ranges::equal_to{}, &Data::data)); + + assert(result.in1 == r1.end()); + assert(result.in2 == r2.end()); + assert(result.out == out.end()); + }; + + // iteartor overload + { + std::array out; + std::same_as> decltype(auto) result = + std::ranges::merge(r1.begin(), r1.end(), r2.begin(), r2.end(), out.data(), [](const Data& x, const Data& y) { + return x.data < y.data; + }); + verify(result, out); + } + + // range overload + { + std::array out; + std::same_as> decltype(auto) result = + std::ranges::merge(r1, r2, out.data(), [](const Data& x, const Data& y) { return x.data < y.data; }); + verify(result, out); + } + } + + // Test Projection + { + std::array r1{Data{4}, Data{8}, Data{12}}; + std::array r2{Data{5}, Data{9}}; + using Iter1 = std::ranges::iterator_t; + using Iter2 = std::ranges::iterator_t; + + const auto verify = [&](const auto& result, const std::array& out) { + assert(std::ranges::equal(out, std::array{4, 5, 8, 9, 12}, std::ranges::equal_to{}, &Data::data)); + + assert(result.in1 == r1.end()); + assert(result.in2 == r2.end()); + assert(result.out == out.end()); + }; + + const auto proj = &Data::data; + + // iteartor overload + { + std::array out; + std::same_as> decltype(auto) result = + std::ranges::merge(r1.begin(), r1.end(), r2.begin(), r2.end(), out.data(), std::ranges::less{}, proj, proj); + verify(result, out); + } + + // range overload + { + std::array out; + std::same_as> decltype(auto) result = + std::ranges::merge(r1, r2, out.data(), std::ranges::less{}, proj, proj); + verify(result, out); + } + } + + 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 @@ -98,7 +98,7 @@ //static_assert(test(std::ranges::make_heap, a)); static_assert(test(std::ranges::max, a)); static_assert(test(std::ranges::max_element, a)); -//static_assert(test(std::ranges::merge, a, a, a)); +static_assert(test(std::ranges::merge, a, a, a)); static_assert(test(std::ranges::min, a)); static_assert(test(std::ranges::min_element, a)); static_assert(test(std::ranges::minmax, a));