1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 
11 // <algorithm>
12 
13 // template<input_iterator I1, sentinel_for<I1> S1,
14 //          random_access_iterator I2, sentinel_for<I2> S2,
15 //          class Comp = ranges::less, class Proj1 = identity, class Proj2 = identity>
16 //   requires indirectly_copyable<I1, I2> && sortable<I2, Comp, Proj2> &&
17 //            indirect_strict_weak_order<Comp, projected<I1, Proj1>, projected<I2, Proj2>>
18 //   constexpr partial_sort_copy_result<I1, I2>
19 //     partial_sort_copy(I1 first, S1 last, I2 result_first, S2 result_last,
20 //                       Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {});               // Since C++20
21 //
22 // template<input_range R1, random_access_range R2, class Comp = ranges::less,
23 //          class Proj1 = identity, class Proj2 = identity>
24 //   requires indirectly_copyable<iterator_t<R1>, iterator_t<R2>> &&
25 //            sortable<iterator_t<R2>, Comp, Proj2> &&
26 //            indirect_strict_weak_order<Comp, projected<iterator_t<R1>, Proj1>,
27 //                                       projected<iterator_t<R2>, Proj2>>
28 //   constexpr partial_sort_copy_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>>
29 //     partial_sort_copy(R1&& r, R2&& result_r, Comp comp = {},
30 //                       Proj1 proj1 = {}, Proj2 proj2 = {});                               // Since C++20
31 
32 #include <algorithm>
33 #include <array>
34 #include <concepts>
35 #include <functional>
36 #include <ranges>
37 #include <utility>
38 
39 #include "MoveOnly.h"
40 #include "almost_satisfies_types.h"
41 #include "test_iterators.h"
42 
43 // Test constraints of the (iterator, sentinel) overload.
44 // ======================================================
45 
46 template <class Iter1 = int*, class Sent1 = int*, class Iter2 = int*, class Sent2 = int*,
47           class Comp = std::ranges::less>
48 concept HasPartialSortCopyIter =
49     requires(Iter1&& first1, Sent1&& last1, Iter2&& first2, Sent2&& last2, Comp&& comp) {
50       std::ranges::partial_sort_copy(std::forward<Iter1>(first1), std::forward<Sent1>(last1),
51           std::forward<Iter2>(first2), std::forward<Sent2>(last2), std::forward<Comp>(comp));
52     };
53 
54 static_assert(HasPartialSortCopyIter<int*, int*, int*, int*, std::ranges::less>);
55 
56 // !input_iterator<I1>
57 static_assert(!HasPartialSortCopyIter<InputIteratorNotDerivedFrom>);
58 static_assert(!HasPartialSortCopyIter<InputIteratorNotIndirectlyReadable>);
59 static_assert(!HasPartialSortCopyIter<InputIteratorNotInputOrOutputIterator>);
60 
61 // !sentinel_for<S1, I1>
62 static_assert(!HasPartialSortCopyIter<int*, SentinelForNotSemiregular>);
63 static_assert(!HasPartialSortCopyIter<int*, SentinelForNotWeaklyEqualityComparableWith>);
64 
65 // !random_access_iterator<I2>
66 static_assert(!HasPartialSortCopyIter<int*, int*, RandomAccessIteratorNotDerivedFrom>);
67 static_assert(!HasPartialSortCopyIter<int*, int*, RandomAccessIteratorBadIndex>);
68 
69 // !sentinel_for<S2, I2>
70 static_assert(!HasPartialSortCopyIter<int*, int*, int*, SentinelForNotSemiregular>);
71 static_assert(!HasPartialSortCopyIter<int*, int*, int*, SentinelForNotWeaklyEqualityComparableWith>);
72 
73 // !indirect_unary_predicate<projected<I, Proj>>
74 static_assert(!HasPartialSortCopyIter<int*, int*, int*, int*, IndirectUnaryPredicateNotPredicate>);
75 static_assert(!HasPartialSortCopyIter<int*, int*, int*, int*, IndirectUnaryPredicateNotCopyConstructible>);
76 
77 // !indirectly_copyable<I1, I2>
78 static_assert(!HasPartialSortCopyIter<int*, int*, MoveOnly*>);
79 
80 // !sortable<I2, Comp, Proj2>
81 static_assert(!HasPartialSortCopyIter<int*, int*, const int*, const int*>);
82 
83 struct NoComparator {};
84 // !indirect_strict_weak_order<Comp, projected<I1, Proj1>, projected<I2, Proj2>>
85 static_assert(!HasPartialSortCopyIter<NoComparator*, NoComparator*, NoComparator*, NoComparator*>);
86 
87 // Test constraints of the (range) overload.
88 // ======================================================
89 
90 template <class T>
91 using R = UncheckedRange<T>;
92 
93 template <class Range1 = R<int*>, class Range2 = R<int*>, class Comp = std::ranges::less>
94 concept HasPartialSortCopyRange =
95     requires(Range1&& range1, Range2&& range2, Comp&& comp) {
96       std::ranges::partial_sort_copy(std::forward<Range1>(range1), std::forward<Range2>(range2),
97           std::forward<Comp>(comp));
98     };
99 
100 static_assert(HasPartialSortCopyRange<R<int*>, R<int*>, std::ranges::less>);
101 
102 // !input_range<R1>
103 static_assert(!HasPartialSortCopyRange<InputRangeNotDerivedFrom>);
104 static_assert(!HasPartialSortCopyRange<InputRangeNotIndirectlyReadable>);
105 static_assert(!HasPartialSortCopyRange<InputRangeNotInputOrOutputIterator>);
106 
107 // !random_access_range<R2>
108 static_assert(!HasPartialSortCopyRange<R<int*>, RandomAccessRangeNotDerivedFrom>);
109 static_assert(!HasPartialSortCopyRange<R<int*>, RandomAccessRangeBadIndex>);
110 
111 // !indirectly_copyable<iterator_t<R1>, iterator_t<R2>>
112 static_assert(!HasPartialSortCopyRange<R<int*>, R<MoveOnly*>>);
113 
114 // !sortable<iterator_t<R2>, Comp, Proj2>
115 static_assert(!HasPartialSortCopyRange<R<int*>, R<const int*>>);
116 
117 // !indirect_strict_weak_order<Comp, projected<iterator_t<R1>, Proj1>, projected<iterator_t<R2>, Proj2>>
118 static_assert(!HasPartialSortCopyRange<R<NoComparator*>, R<NoComparator*>>);
119 
120 static_assert(std::is_same_v<std::ranges::partial_sort_copy_result<int, int>, std::ranges::in_out_result<int, int>>);
121 
122 template <class Iter, class Sent, class OutIter, class OutSent, std::size_t N>
test_one(std::array<int,N> input,std::size_t input_size,size_t output_size,std::array<int,N> sorted)123 constexpr void test_one(
124     std::array<int, N> input, std::size_t input_size, size_t output_size, std::array<int, N> sorted) {
125   assert(input_size <= N);
126   assert(output_size <= N + 1); // To support testing the case where output size exceeds input size.
127 
128   using ResultT = std::ranges::partial_sort_copy_result<Iter, OutIter>;
129   // To support testing the case where output size exceeds input size; also makes sure calling `out.data() + int()` is
130   // valid.
131   constexpr std::size_t OutputSize = N + 1;
132   std::size_t result_size = std::ranges::min(input_size, output_size);
133 
134   auto begin = input.data();
135   auto end = input.data() + input_size;
136 
137   { // (iterator, sentinel) overload.
138     std::array<int, OutputSize> out;
139     auto out_begin = out.data();
140     auto out_end = out.data() + output_size;
141 
142     std::same_as<ResultT> decltype(auto) result = std::ranges::partial_sort_copy(
143         Iter(begin), Sent(Iter(end)), OutIter(out_begin), OutSent(OutIter(out_end)));
144 
145     assert(base(result.in) == input.data() + input_size);
146     assert(base(result.out) == out.data() + result_size);
147 
148     assert(std::ranges::equal(std::ranges::subrange(out.begin(), out.begin() + result_size),
149            std::ranges::subrange(sorted.begin(), sorted.begin() + result_size)));
150   }
151 
152   { // (range) overload.
153     std::array<int, OutputSize> out;
154     auto out_begin = out.data();
155     auto out_end = out.data() + output_size;
156     auto in_range = std::ranges::subrange(Iter(begin), Sent(Iter(end)));
157     auto out_range = std::ranges::subrange(OutIter(out_begin), OutSent(OutIter(out_end)));
158 
159     std::same_as<ResultT> decltype(auto) result = std::ranges::partial_sort_copy(in_range, out_range);
160 
161     assert(base(result.in) == input.data() + input_size);
162     assert(base(result.out) == out.data() + result_size);
163 
164     assert(std::ranges::equal(std::ranges::subrange(out.begin(), out.begin() + result_size),
165            std::ranges::subrange(sorted.begin(), sorted.begin() + result_size)));
166   }
167 
168 }
169 
170 template <class Iter, class Sent, class OutIter, class OutSent, std::size_t N>
test_all_subsequences(const std::array<int,N> input)171 constexpr void test_all_subsequences(const std::array<int, N> input) {
172   auto sorted = input;
173   std::sort(sorted.begin(), sorted.end());
174 
175   // Whole input, increasing output size. Also check the case when `output_size` exceeds input size.
176   for (std::size_t out_size = 0; out_size <= N + 1; ++out_size) {
177     test_one<Iter, Sent, OutIter, OutSent>(input, N, out_size, sorted);
178   }
179 }
180 
181 template <class InIter, class Sent1, class OutIter, class Sent2>
test_iterators_in_sent1_out_sent2()182 constexpr void test_iterators_in_sent1_out_sent2() {
183   // Empty sequence.
184   test_all_subsequences<InIter, Sent1, OutIter, Sent2, 0>({});
185 
186   // 1-element sequence.
187   test_all_subsequences<InIter, Sent1, OutIter, Sent2>(std::array{1});
188 
189   // 2-element sequence.
190   test_all_subsequences<InIter, Sent1, OutIter, Sent2>(std::array{2, 1});
191 
192   // 3-element sequence.
193   test_all_subsequences<InIter, Sent1, OutIter, Sent2>(std::array{2, 1, 3});
194 
195   // Longer sequence.
196   test_all_subsequences<InIter, Sent1, OutIter, Sent2>(std::array{2, 1, 3, 6, 8, 4, 11, 5});
197 
198   // Longer sequence with duplicates.
199   test_all_subsequences<InIter, Sent1, OutIter, Sent2>(std::array{2, 1, 3, 6, 2, 8, 6});
200 
201   // All elements are the same.
202   test_all_subsequences<InIter, Sent1, OutIter, Sent2>(std::array{1, 1, 1});
203 
204   // Already sorted.
205   test_all_subsequences<InIter, Sent1, OutIter, Sent2>(std::array{1, 2, 3, 4, 5});
206 
207   // Descending.
208   test_all_subsequences<InIter, Sent1, OutIter, Sent2>(std::array{5, 4, 3, 2, 1});
209 }
210 
211 template <class InIter, class Sent1, class OutIter>
test_iterators_in_sent1_out()212 constexpr void test_iterators_in_sent1_out() {
213   test_iterators_in_sent1_out_sent2<InIter, Sent1, OutIter, OutIter>();
214   test_iterators_in_sent1_out_sent2<InIter, Sent1, OutIter, sentinel_wrapper<OutIter>>();
215 }
216 
217 template <class InIter, class Sent1>
test_iterators_in_sent1()218 constexpr void test_iterators_in_sent1() {
219   test_iterators_in_sent1_out<InIter, Sent1, random_access_iterator<int*>>();
220 }
221 
222 template <class InIter>
test_iterators_in()223 constexpr void test_iterators_in() {
224   if constexpr (std::sentinel_for<InIter, InIter>) {
225     test_iterators_in_sent1<InIter, InIter>();
226   }
227   test_iterators_in_sent1<InIter, sentinel_wrapper<InIter>>();
228 }
229 
test_iterators()230 constexpr void test_iterators() {
231   test_iterators_in<cpp20_input_iterator<int*>>();
232   // Omitting other iterator types to reduce the combinatorial explosion.
233   test_iterators_in<random_access_iterator<int*>>();
234   test_iterators_in<const int*>();
235 }
236 
test()237 constexpr bool test() {
238   test_iterators();
239 
240   { // A custom comparator works.
241     const std::array in = {1, 2, 3, 4, 5};
242     std::ranges::greater comp;
243 
244     {
245       std::array<int, 2> out;
246 
247       auto result = std::ranges::partial_sort_copy(in.begin(), in.end(), out.begin(), out.end(), comp);
248       assert(std::ranges::equal(std::ranges::subrange(out.begin(), result.out), std::array{5, 4}));
249     }
250 
251     {
252       std::array<int, 2> out;
253 
254       auto result = std::ranges::partial_sort_copy(in, out, comp);
255       assert(std::ranges::equal(std::ranges::subrange(out.begin(), result.out), std::array{5, 4}));
256     }
257   }
258 
259   { // A custom projection works.
260     struct A {
261       int a;
262 
263       constexpr A() = default;
264       constexpr A(int value) : a(value) {}
265       constexpr auto operator<=>(const A&) const = default;
266     };
267 
268     const std::array in = {2, 1, 3};
269     auto proj1 = [](int value) { return value * -1; };
270     auto proj2 = [](A value) { return value.a * -1; };
271     // The projections negate the argument, so the array will appear to be sorted in descending order: [3, 2, 1]
272     // (the projections make it appear to the algorithm as [-3, -2, -1]).
273 
274     {
275       std::array<A, 2> out;
276 
277       // No projections: ascending order.
278       auto result = std::ranges::partial_sort_copy(in.begin(), in.end(), out.begin(), out.end(), {});
279       assert(std::ranges::equal(std::ranges::subrange(out.begin(), result.out), std::array{1, 2}));
280       // Using projections: descending order.
281       result = std::ranges::partial_sort_copy(in.begin(), in.end(), out.begin(), out.end(), {}, proj1, proj2);
282       assert(std::ranges::equal(std::ranges::subrange(out.begin(), result.out), std::array{3, 2}));
283     }
284 
285     {
286       std::array<int, 2> out;
287 
288       auto result = std::ranges::partial_sort_copy(in, out, {});
289       assert(std::ranges::equal(std::ranges::subrange(out.begin(), result.out), std::array{1, 2}));
290       result = std::ranges::partial_sort_copy(in, out, {}, proj1, proj2);
291       assert(std::ranges::equal(std::ranges::subrange(out.begin(), result.out), std::array{3, 2}));
292     }
293   }
294 
295   return true;
296 }
297 
main(int,char **)298 int main(int, char**) {
299   test();
300   static_assert(test());
301 
302   return 0;
303 }
304