blob: 44a3d107b84f10e94b8d62340ea8785f98e8e397 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- algorithm ---------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_ALGORITHM
12#define _LIBCPP_ALGORITHM
13
14/*
15 algorithm synopsis
16
17#include <initializer_list>
18
19namespace std
20{
21
22template <class InputIterator, class Predicate>
Marshall Clowde4b2862018-01-15 17:20:36 +000023 constexpr bool // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000024 all_of(InputIterator first, InputIterator last, Predicate pred);
25
26template <class InputIterator, class Predicate>
Marshall Clowde4b2862018-01-15 17:20:36 +000027 constexpr bool // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000028 any_of(InputIterator first, InputIterator last, Predicate pred);
29
30template <class InputIterator, class Predicate>
Marshall Clowde4b2862018-01-15 17:20:36 +000031 constexpr bool // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000032 none_of(InputIterator first, InputIterator last, Predicate pred);
33
34template <class InputIterator, class Function>
35 Function
36 for_each(InputIterator first, InputIterator last, Function f);
37
Marshall Clow10411c12017-05-25 02:29:54 +000038template<class InputIterator, class Size, class Function>
39 InputIterator for_each_n(InputIterator first, Size n, Function f); // C++17
40
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000041template <class InputIterator, class T>
Marshall Clow6a1c2ff2018-01-15 19:26:05 +000042 constexpr InputIterator // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000043 find(InputIterator first, InputIterator last, const T& value);
44
45template <class InputIterator, class Predicate>
Marshall Clow6a1c2ff2018-01-15 19:26:05 +000046 constexpr InputIterator // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000047 find_if(InputIterator first, InputIterator last, Predicate pred);
48
49template<class InputIterator, class Predicate>
Marshall Clow6a1c2ff2018-01-15 19:26:05 +000050 InputIterator // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000051 find_if_not(InputIterator first, InputIterator last, Predicate pred);
52
53template <class ForwardIterator1, class ForwardIterator2>
Marshall Clow6a1c2ff2018-01-15 19:26:05 +000054 ForwardIterator1 // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000055 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
56 ForwardIterator2 first2, ForwardIterator2 last2);
57
58template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clow6a1c2ff2018-01-15 19:26:05 +000059 ForwardIterator1 // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000060 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
61 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
62
63template <class ForwardIterator1, class ForwardIterator2>
Marshall Clow6a1c2ff2018-01-15 19:26:05 +000064 constexpr ForwardIterator1 // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000065 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
66 ForwardIterator2 first2, ForwardIterator2 last2);
67
68template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clow6a1c2ff2018-01-15 19:26:05 +000069 constexpr ForwardIterator1 // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000070 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
71 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
72
73template <class ForwardIterator>
Marshall Clow6a1c2ff2018-01-15 19:26:05 +000074 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000075 adjacent_find(ForwardIterator first, ForwardIterator last);
76
77template <class ForwardIterator, class BinaryPredicate>
Marshall Clow6a1c2ff2018-01-15 19:26:05 +000078 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000079 adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
80
81template <class InputIterator, class T>
Marshall Clow0dc8ca72018-01-15 19:40:34 +000082 constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083 count(InputIterator first, InputIterator last, const T& value);
84
85template <class InputIterator, class Predicate>
Marshall Clow0dc8ca72018-01-15 19:40:34 +000086 constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000087 count_if(InputIterator first, InputIterator last, Predicate pred);
88
89template <class InputIterator1, class InputIterator2>
90 pair<InputIterator1, InputIterator2>
91 mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
92
Marshall Clowb30abdd2013-05-09 21:14:23 +000093template <class InputIterator1, class InputIterator2>
94 pair<InputIterator1, InputIterator2>
Aditya Kumarfdb4f172016-08-25 11:52:38 +000095 mismatch(InputIterator1 first1, InputIterator1 last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +000096 InputIterator2 first2, InputIterator2 last2); // **C++14**
97
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098template <class InputIterator1, class InputIterator2, class BinaryPredicate>
99 pair<InputIterator1, InputIterator2>
100 mismatch(InputIterator1 first1, InputIterator1 last1,
101 InputIterator2 first2, BinaryPredicate pred);
102
Marshall Clowb30abdd2013-05-09 21:14:23 +0000103template <class InputIterator1, class InputIterator2, class BinaryPredicate>
104 pair<InputIterator1, InputIterator2>
105 mismatch(InputIterator1 first1, InputIterator1 last1,
106 InputIterator2 first2, InputIterator2 last2,
107 BinaryPredicate pred); // **C++14**
108
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000109template <class InputIterator1, class InputIterator2>
110 bool
111 equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
112
Marshall Clowb30abdd2013-05-09 21:14:23 +0000113template <class InputIterator1, class InputIterator2>
114 bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +0000115 equal(InputIterator1 first1, InputIterator1 last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +0000116 InputIterator2 first2, InputIterator2 last2); // **C++14**
117
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000118template <class InputIterator1, class InputIterator2, class BinaryPredicate>
119 bool
120 equal(InputIterator1 first1, InputIterator1 last1,
121 InputIterator2 first2, BinaryPredicate pred);
122
Marshall Clowb30abdd2013-05-09 21:14:23 +0000123template <class InputIterator1, class InputIterator2, class BinaryPredicate>
124 bool
125 equal(InputIterator1 first1, InputIterator1 last1,
126 InputIterator2 first2, InputIterator2 last2,
127 BinaryPredicate pred); // **C++14**
128
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129template<class ForwardIterator1, class ForwardIterator2>
Marshall Clow8d0d8252018-01-15 16:16:32 +0000130 constexpr bool // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000131 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
132 ForwardIterator2 first2);
133
Marshall Clowb30abdd2013-05-09 21:14:23 +0000134template<class ForwardIterator1, class ForwardIterator2>
Marshall Clow8d0d8252018-01-15 16:16:32 +0000135 constexpr bool // constexpr in C++20
Marshall Clowb30abdd2013-05-09 21:14:23 +0000136 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
137 ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
138
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000139template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clow8d0d8252018-01-15 16:16:32 +0000140 constexpr bool // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
142 ForwardIterator2 first2, BinaryPredicate pred);
143
Marshall Clowb30abdd2013-05-09 21:14:23 +0000144template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clow8d0d8252018-01-15 16:16:32 +0000145 constexpr bool // constexpr in C++20
Marshall Clowb30abdd2013-05-09 21:14:23 +0000146 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
147 ForwardIterator2 first2, ForwardIterator2 last2,
148 BinaryPredicate pred); // **C++14**
149
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000150template <class ForwardIterator1, class ForwardIterator2>
151 ForwardIterator1
152 search(ForwardIterator1 first1, ForwardIterator1 last1,
153 ForwardIterator2 first2, ForwardIterator2 last2);
154
155template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
156 ForwardIterator1
157 search(ForwardIterator1 first1, ForwardIterator1 last1,
158 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
159
160template <class ForwardIterator, class Size, class T>
161 ForwardIterator
162 search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
163
164template <class ForwardIterator, class Size, class T, class BinaryPredicate>
165 ForwardIterator
166 search_n(ForwardIterator first, ForwardIterator last,
167 Size count, const T& value, BinaryPredicate pred);
168
169template <class InputIterator, class OutputIterator>
170 OutputIterator
171 copy(InputIterator first, InputIterator last, OutputIterator result);
172
173template<class InputIterator, class OutputIterator, class Predicate>
174 OutputIterator
175 copy_if(InputIterator first, InputIterator last,
176 OutputIterator result, Predicate pred);
177
178template<class InputIterator, class Size, class OutputIterator>
179 OutputIterator
180 copy_n(InputIterator first, Size n, OutputIterator result);
181
182template <class BidirectionalIterator1, class BidirectionalIterator2>
183 BidirectionalIterator2
184 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
185 BidirectionalIterator2 result);
186
187template <class ForwardIterator1, class ForwardIterator2>
188 ForwardIterator2
189 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
190
191template <class ForwardIterator1, class ForwardIterator2>
192 void
193 iter_swap(ForwardIterator1 a, ForwardIterator2 b);
194
195template <class InputIterator, class OutputIterator, class UnaryOperation>
196 OutputIterator
197 transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
198
199template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
200 OutputIterator
201 transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
202 OutputIterator result, BinaryOperation binary_op);
203
204template <class ForwardIterator, class T>
205 void
206 replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
207
208template <class ForwardIterator, class Predicate, class T>
209 void
210 replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
211
212template <class InputIterator, class OutputIterator, class T>
213 OutputIterator
214 replace_copy(InputIterator first, InputIterator last, OutputIterator result,
215 const T& old_value, const T& new_value);
216
217template <class InputIterator, class OutputIterator, class Predicate, class T>
218 OutputIterator
219 replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
220
221template <class ForwardIterator, class T>
222 void
223 fill(ForwardIterator first, ForwardIterator last, const T& value);
224
225template <class OutputIterator, class Size, class T>
226 OutputIterator
227 fill_n(OutputIterator first, Size n, const T& value);
228
229template <class ForwardIterator, class Generator>
230 void
231 generate(ForwardIterator first, ForwardIterator last, Generator gen);
232
233template <class OutputIterator, class Size, class Generator>
234 OutputIterator
235 generate_n(OutputIterator first, Size n, Generator gen);
236
237template <class ForwardIterator, class T>
238 ForwardIterator
239 remove(ForwardIterator first, ForwardIterator last, const T& value);
240
241template <class ForwardIterator, class Predicate>
242 ForwardIterator
243 remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
244
245template <class InputIterator, class OutputIterator, class T>
246 OutputIterator
247 remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
248
249template <class InputIterator, class OutputIterator, class Predicate>
250 OutputIterator
251 remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
252
253template <class ForwardIterator>
254 ForwardIterator
255 unique(ForwardIterator first, ForwardIterator last);
256
257template <class ForwardIterator, class BinaryPredicate>
258 ForwardIterator
259 unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
260
261template <class InputIterator, class OutputIterator>
262 OutputIterator
263 unique_copy(InputIterator first, InputIterator last, OutputIterator result);
264
265template <class InputIterator, class OutputIterator, class BinaryPredicate>
266 OutputIterator
267 unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
268
269template <class BidirectionalIterator>
270 void
271 reverse(BidirectionalIterator first, BidirectionalIterator last);
272
273template <class BidirectionalIterator, class OutputIterator>
274 OutputIterator
275 reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
276
277template <class ForwardIterator>
278 ForwardIterator
279 rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
280
281template <class ForwardIterator, class OutputIterator>
282 OutputIterator
283 rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
284
285template <class RandomAccessIterator>
286 void
Marshall Clow03b862f2017-03-23 13:43:37 +0000287 random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14, removed in C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000288
289template <class RandomAccessIterator, class RandomNumberGenerator>
290 void
Marshall Clow3fef95b2014-03-03 06:14:19 +0000291 random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
Marshall Clow03b862f2017-03-23 13:43:37 +0000292 RandomNumberGenerator& rand); // deprecated in C++14, removed in C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000293
Eric Fiselier917af0a2016-08-28 22:14:37 +0000294template<class PopulationIterator, class SampleIterator,
295 class Distance, class UniformRandomBitGenerator>
296 SampleIterator sample(PopulationIterator first, PopulationIterator last,
297 SampleIterator out, Distance n,
298 UniformRandomBitGenerator&& g); // C++17
299
Howard Hinnantc3267212010-05-26 17:49:34 +0000300template<class RandomAccessIterator, class UniformRandomNumberGenerator>
301 void shuffle(RandomAccessIterator first, RandomAccessIterator last,
Howard Hinnant278bf2d2010-11-18 01:47:02 +0000302 UniformRandomNumberGenerator&& g);
Howard Hinnantc3267212010-05-26 17:49:34 +0000303
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000304template <class InputIterator, class Predicate>
Marshall Clow8d0d8252018-01-15 16:16:32 +0000305 constexpr bool // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000306 is_partitioned(InputIterator first, InputIterator last, Predicate pred);
307
308template <class ForwardIterator, class Predicate>
309 ForwardIterator
310 partition(ForwardIterator first, ForwardIterator last, Predicate pred);
311
312template <class InputIterator, class OutputIterator1,
313 class OutputIterator2, class Predicate>
314 pair<OutputIterator1, OutputIterator2>
315 partition_copy(InputIterator first, InputIterator last,
316 OutputIterator1 out_true, OutputIterator2 out_false,
317 Predicate pred);
318
319template <class ForwardIterator, class Predicate>
320 ForwardIterator
321 stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
322
323template<class ForwardIterator, class Predicate>
Marshall Clowbb8010f2018-01-15 17:53:34 +0000324 ForwardIterator // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000325 partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
326
327template <class ForwardIterator>
Marshall Clow8d0d8252018-01-15 16:16:32 +0000328 constexpr bool // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000329 is_sorted(ForwardIterator first, ForwardIterator last);
330
331template <class ForwardIterator, class Compare>
332 bool
333 is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
334
335template<class ForwardIterator>
Marshall Clow0dc8ca72018-01-15 19:40:34 +0000336 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337 is_sorted_until(ForwardIterator first, ForwardIterator last);
338
339template <class ForwardIterator, class Compare>
Marshall Clow0dc8ca72018-01-15 19:40:34 +0000340 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000341 is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
342
343template <class RandomAccessIterator>
344 void
345 sort(RandomAccessIterator first, RandomAccessIterator last);
346
347template <class RandomAccessIterator, class Compare>
348 void
349 sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
350
351template <class RandomAccessIterator>
352 void
353 stable_sort(RandomAccessIterator first, RandomAccessIterator last);
354
355template <class RandomAccessIterator, class Compare>
356 void
357 stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
358
359template <class RandomAccessIterator>
360 void
361 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
362
363template <class RandomAccessIterator, class Compare>
364 void
365 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
366
367template <class InputIterator, class RandomAccessIterator>
368 RandomAccessIterator
369 partial_sort_copy(InputIterator first, InputIterator last,
370 RandomAccessIterator result_first, RandomAccessIterator result_last);
371
372template <class InputIterator, class RandomAccessIterator, class Compare>
373 RandomAccessIterator
374 partial_sort_copy(InputIterator first, InputIterator last,
375 RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
376
377template <class RandomAccessIterator>
378 void
379 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
380
381template <class RandomAccessIterator, class Compare>
382 void
383 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
384
385template <class ForwardIterator, class T>
386 ForwardIterator
387 lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
388
389template <class ForwardIterator, class T, class Compare>
390 ForwardIterator
391 lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
392
393template <class ForwardIterator, class T>
394 ForwardIterator
395 upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
396
397template <class ForwardIterator, class T, class Compare>
398 ForwardIterator
399 upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
400
401template <class ForwardIterator, class T>
402 pair<ForwardIterator, ForwardIterator>
403 equal_range(ForwardIterator first, ForwardIterator last, const T& value);
404
405template <class ForwardIterator, class T, class Compare>
406 pair<ForwardIterator, ForwardIterator>
407 equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
408
409template <class ForwardIterator, class T>
410 bool
411 binary_search(ForwardIterator first, ForwardIterator last, const T& value);
412
413template <class ForwardIterator, class T, class Compare>
414 bool
415 binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
416
417template <class InputIterator1, class InputIterator2, class OutputIterator>
418 OutputIterator
419 merge(InputIterator1 first1, InputIterator1 last1,
420 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
421
422template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
423 OutputIterator
424 merge(InputIterator1 first1, InputIterator1 last1,
425 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
426
427template <class BidirectionalIterator>
428 void
429 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
430
431template <class BidirectionalIterator, class Compare>
432 void
433 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
434
435template <class InputIterator1, class InputIterator2>
436 bool
437 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
438
439template <class InputIterator1, class InputIterator2, class Compare>
440 bool
441 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
442
443template <class InputIterator1, class InputIterator2, class OutputIterator>
444 OutputIterator
445 set_union(InputIterator1 first1, InputIterator1 last1,
446 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
447
448template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
449 OutputIterator
450 set_union(InputIterator1 first1, InputIterator1 last1,
451 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
452
453template <class InputIterator1, class InputIterator2, class OutputIterator>
454 OutputIterator
455 set_intersection(InputIterator1 first1, InputIterator1 last1,
456 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
457
458template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
459 OutputIterator
460 set_intersection(InputIterator1 first1, InputIterator1 last1,
461 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
462
463template <class InputIterator1, class InputIterator2, class OutputIterator>
464 OutputIterator
465 set_difference(InputIterator1 first1, InputIterator1 last1,
466 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
467
468template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
469 OutputIterator
470 set_difference(InputIterator1 first1, InputIterator1 last1,
471 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
472
473template <class InputIterator1, class InputIterator2, class OutputIterator>
474 OutputIterator
475 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
476 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
477
478template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
479 OutputIterator
480 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
481 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
482
483template <class RandomAccessIterator>
484 void
485 push_heap(RandomAccessIterator first, RandomAccessIterator last);
486
487template <class RandomAccessIterator, class Compare>
488 void
489 push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
490
491template <class RandomAccessIterator>
492 void
493 pop_heap(RandomAccessIterator first, RandomAccessIterator last);
494
495template <class RandomAccessIterator, class Compare>
496 void
497 pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
498
499template <class RandomAccessIterator>
500 void
501 make_heap(RandomAccessIterator first, RandomAccessIterator last);
502
503template <class RandomAccessIterator, class Compare>
504 void
505 make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
506
507template <class RandomAccessIterator>
508 void
509 sort_heap(RandomAccessIterator first, RandomAccessIterator last);
510
511template <class RandomAccessIterator, class Compare>
512 void
513 sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
514
Howard Hinnant324bb032010-08-22 00:02:43 +0000515template <class RandomAccessIterator>
Marshall Clow8d0d8252018-01-15 16:16:32 +0000516 constexpr bool // constexpr in C++20
Howard Hinnant324bb032010-08-22 00:02:43 +0000517 is_heap(RandomAccessIterator first, RandomAccessiterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000518
Howard Hinnant324bb032010-08-22 00:02:43 +0000519template <class RandomAccessIterator, class Compare>
Marshall Clow8d0d8252018-01-15 16:16:32 +0000520 constexpr bool // constexpr in C++20
Howard Hinnant324bb032010-08-22 00:02:43 +0000521 is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000522
Howard Hinnant324bb032010-08-22 00:02:43 +0000523template <class RandomAccessIterator>
Marshall Clow8d0d8252018-01-15 16:16:32 +0000524 constexpr RandomAccessIterator // constexpr in C++20
Howard Hinnant324bb032010-08-22 00:02:43 +0000525 is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000526
Howard Hinnant324bb032010-08-22 00:02:43 +0000527template <class RandomAccessIterator, class Compare>
Marshall Clow8d0d8252018-01-15 16:16:32 +0000528 constexpr RandomAccessIterator // constexpr in C++20
Howard Hinnant324bb032010-08-22 00:02:43 +0000529 is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000530
Howard Hinnant98e5d972010-08-21 20:10:01 +0000531template <class ForwardIterator>
532 ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +0000533 min_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000534
535template <class ForwardIterator, class Compare>
536 ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +0000537 min_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000538
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000539template <class T>
540 const T&
Marshall Clow9d9463a2014-02-19 16:51:35 +0000541 min(const T& a, const T& b); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000542
543template <class T, class Compare>
544 const T&
Marshall Clow9d9463a2014-02-19 16:51:35 +0000545 min(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000546
Howard Hinnant98e5d972010-08-21 20:10:01 +0000547template<class T>
548 T
Marshall Clow9d9463a2014-02-19 16:51:35 +0000549 min(initializer_list<T> t); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000550
551template<class T, class Compare>
552 T
Marshall Clow9d9463a2014-02-19 16:51:35 +0000553 min(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000554
Marshall Clow3e0808e2016-03-07 22:43:49 +0000555template<class T>
556 constexpr const T& clamp( const T& v, const T& lo, const T& hi ); // C++17
557
558template<class T, class Compare>
559 constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp ); // C++17
560
Howard Hinnant98e5d972010-08-21 20:10:01 +0000561template <class ForwardIterator>
562 ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +0000563 max_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000564
565template <class ForwardIterator, class Compare>
566 ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +0000567 max_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000568
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000569template <class T>
570 const T&
Marshall Clow9d9463a2014-02-19 16:51:35 +0000571 max(const T& a, const T& b); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000572
573template <class T, class Compare>
574 const T&
Marshall Clow9d9463a2014-02-19 16:51:35 +0000575 max(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000576
Howard Hinnant98e5d972010-08-21 20:10:01 +0000577template<class T>
578 T
Marshall Clow9d9463a2014-02-19 16:51:35 +0000579 max(initializer_list<T> t); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000580
Howard Hinnant98e5d972010-08-21 20:10:01 +0000581template<class T, class Compare>
582 T
Marshall Clow9d9463a2014-02-19 16:51:35 +0000583 max(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000584
Howard Hinnant98e5d972010-08-21 20:10:01 +0000585template<class ForwardIterator>
586 pair<ForwardIterator, ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +0000587 minmax_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000588
Howard Hinnant98e5d972010-08-21 20:10:01 +0000589template<class ForwardIterator, class Compare>
590 pair<ForwardIterator, ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +0000591 minmax_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000592
593template<class T>
594 pair<const T&, const T&>
Marshall Clow9d9463a2014-02-19 16:51:35 +0000595 minmax(const T& a, const T& b); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000596
597template<class T, class Compare>
598 pair<const T&, const T&>
Marshall Clow9d9463a2014-02-19 16:51:35 +0000599 minmax(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000600
601template<class T>
602 pair<T, T>
Marshall Clow9d9463a2014-02-19 16:51:35 +0000603 minmax(initializer_list<T> t); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000604
605template<class T, class Compare>
606 pair<T, T>
Marshall Clow9d9463a2014-02-19 16:51:35 +0000607 minmax(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000608
609template <class InputIterator1, class InputIterator2>
610 bool
611 lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
612
613template <class InputIterator1, class InputIterator2, class Compare>
614 bool
615 lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
616 InputIterator2 first2, InputIterator2 last2, Compare comp);
617
618template <class BidirectionalIterator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000619 bool
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000620 next_permutation(BidirectionalIterator first, BidirectionalIterator last);
621
622template <class BidirectionalIterator, class Compare>
623 bool
624 next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
625
626template <class BidirectionalIterator>
627 bool
628 prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
629
630template <class BidirectionalIterator, class Compare>
631 bool
632 prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
633
634} // std
635
636*/
637
638#include <__config>
639#include <initializer_list>
640#include <type_traits>
641#include <cstring>
Eric Fiselier8f1e73d2016-04-21 23:38:59 +0000642#include <utility> // needed to provide swap_ranges.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000643#include <memory>
Marshall Clow33406332018-01-08 19:18:00 +0000644#include <functional>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000645#include <iterator>
Howard Hinnantca8eb832012-07-26 17:09:09 +0000646#include <cstddef>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000647
Howard Hinnant7f764502013-08-14 18:00:20 +0000648#if defined(__IBMCPP__)
649#include "support/ibm/support.h"
650#endif
Eric Fiselier41af64a2017-05-10 20:57:45 +0000651#if defined(_LIBCPP_COMPILER_MSVC)
Eric Fiselierca91fd02017-05-10 21:30:04 +0000652#include <intrin.h>
Howard Hinnantef5aa932013-09-17 01:34:47 +0000653#endif
Howard Hinnant7f764502013-08-14 18:00:20 +0000654
Eric Fiselierb9536102014-08-10 23:53:08 +0000655#include <__debug>
656
Howard Hinnant08e17472011-10-17 20:05:10 +0000657#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000658#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000659#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000660
Eric Fiselier018a3d52017-05-31 22:07:49 +0000661_LIBCPP_PUSH_MACROS
662#include <__undef_macros>
663
664
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000665_LIBCPP_BEGIN_NAMESPACE_STD
666
Marshall Clow9d9463a2014-02-19 16:51:35 +0000667// I'd like to replace these with _VSTD::equal_to<void>, but can't because:
668// * That only works with C++14 and later, and
669// * We haven't included <functional> here.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000670template <class _T1, class _T2 = _T1>
671struct __equal_to
672{
673 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
674 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
675 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
676 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
677};
678
679template <class _T1>
680struct __equal_to<_T1, _T1>
681{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000682 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
683 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000684};
685
686template <class _T1>
687struct __equal_to<const _T1, _T1>
688{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000689 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
690 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000691};
692
693template <class _T1>
694struct __equal_to<_T1, const _T1>
695{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000696 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
697 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000698};
699
700template <class _T1, class _T2 = _T1>
701struct __less
702{
Aditya Kumarfdb4f172016-08-25 11:52:38 +0000703 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow9d9463a2014-02-19 16:51:35 +0000704 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
705
706 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
707 bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
708
709 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
710 bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
711
712 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
713 bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000714};
715
716template <class _T1>
717struct __less<_T1, _T1>
718{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000719 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
720 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000721};
722
723template <class _T1>
724struct __less<const _T1, _T1>
725{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000726 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
727 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000728};
729
730template <class _T1>
731struct __less<_T1, const _T1>
732{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000733 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
734 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000735};
736
737template <class _Predicate>
Marshall Clow25a78dc2017-08-28 23:16:13 +0000738class __invert // invert the sense of a comparison
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000739{
740private:
741 _Predicate __p_;
742public:
Marshall Clow25a78dc2017-08-28 23:16:13 +0000743 _LIBCPP_INLINE_VISIBILITY __invert() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000744
745 _LIBCPP_INLINE_VISIBILITY
Marshall Clow25a78dc2017-08-28 23:16:13 +0000746 explicit __invert(_Predicate __p) : __p_(__p) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000747
748 template <class _T1>
749 _LIBCPP_INLINE_VISIBILITY
750 bool operator()(const _T1& __x) {return !__p_(__x);}
751
752 template <class _T1, class _T2>
753 _LIBCPP_INLINE_VISIBILITY
Marshall Clow25a78dc2017-08-28 23:16:13 +0000754 bool operator()(const _T1& __x, const _T2& __y) {return __p_(__y, __x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000755};
756
Howard Hinnant5e571422013-08-23 20:10:18 +0000757#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000758
759template <class _Compare>
760struct __debug_less
761{
762 _Compare __comp_;
763 __debug_less(_Compare& __c) : __comp_(__c) {}
Eric Fiselier99029f12016-07-19 23:27:18 +0000764
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765 template <class _Tp, class _Up>
766 bool operator()(const _Tp& __x, const _Up& __y)
767 {
768 bool __r = __comp_(__x, __y);
769 if (__r)
Eric Fiselier99029f12016-07-19 23:27:18 +0000770 __do_compare_assert(0, __y, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000771 return __r;
772 }
Eric Fiselier99029f12016-07-19 23:27:18 +0000773
774 template <class _LHS, class _RHS>
775 inline _LIBCPP_INLINE_VISIBILITY
776 decltype((void)_VSTD::declval<_Compare&>()(
777 _VSTD::declval<_LHS const&>(), _VSTD::declval<_RHS const&>()))
778 __do_compare_assert(int, _LHS const& __l, _RHS const& __r) {
779 _LIBCPP_ASSERT(!__comp_(__l, __r),
780 "Comparator does not induce a strict weak ordering");
781 }
782
783 template <class _LHS, class _RHS>
784 inline _LIBCPP_INLINE_VISIBILITY
785 void __do_compare_assert(long, _LHS const&, _RHS const&) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000786};
787
Howard Hinnant5e571422013-08-23 20:10:18 +0000788#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000789
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000790// Precondition: __x != 0
Howard Hinnantec3773c2011-12-01 20:21:04 +0000791inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000792unsigned __ctz(unsigned __x) {
793#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000794 return static_cast<unsigned>(__builtin_ctz(__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000795#else
796 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
797 static_assert(sizeof(unsigned long) == 4, "");
798 unsigned long where;
799 // Search from LSB to MSB for first set bit.
800 // Returns zero if no set bit is found.
Shoaib Meenai54b78ff2017-12-05 17:46:23 +0000801 if (_BitScanForward(&where, __x))
Eric Fiselierca91fd02017-05-10 21:30:04 +0000802 return where;
803 return 32;
804#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +0000805}
806
807inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000808unsigned long __ctz(unsigned long __x) {
809#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000810 return static_cast<unsigned long>(__builtin_ctzl(__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000811#else
812 static_assert(sizeof(unsigned long) == sizeof(unsigned), "");
813 return __ctz(static_cast<unsigned>(__x));
814#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +0000815}
816
817inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000818unsigned long long __ctz(unsigned long long __x) {
819#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000820 return static_cast<unsigned long long>(__builtin_ctzll(__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000821#else
822 unsigned long where;
823// Search from LSB to MSB for first set bit.
824// Returns zero if no set bit is found.
825#if defined(_LIBCPP_HAS_BITSCAN64)
826 (defined(_M_AMD64) || defined(__x86_64__))
Shoaib Meenai54b78ff2017-12-05 17:46:23 +0000827 if (_BitScanForward64(&where, __x))
Eric Fiselierca91fd02017-05-10 21:30:04 +0000828 return static_cast<int>(where);
829#else
830 // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
831 // Scan the Low Word.
Shoaib Meenai54b78ff2017-12-05 17:46:23 +0000832 if (_BitScanForward(&where, static_cast<unsigned long>(__x)))
Eric Fiselierca91fd02017-05-10 21:30:04 +0000833 return where;
834 // Scan the High Word.
Shoaib Meenai54b78ff2017-12-05 17:46:23 +0000835 if (_BitScanForward(&where, static_cast<unsigned long>(__x >> 32)))
Eric Fiselierca91fd02017-05-10 21:30:04 +0000836 return where + 32; // Create a bit offset from the LSB.
837#endif
838 return 64;
839#endif // _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000840}
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000841
842// Precondition: __x != 0
Howard Hinnantec3773c2011-12-01 20:21:04 +0000843inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000844unsigned __clz(unsigned __x) {
845#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000846 return static_cast<unsigned>(__builtin_clz(__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000847#else
848 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
849 static_assert(sizeof(unsigned long) == 4, "");
850 unsigned long where;
851 // Search from LSB to MSB for first set bit.
852 // Returns zero if no set bit is found.
Shoaib Meenai54b78ff2017-12-05 17:46:23 +0000853 if (_BitScanReverse(&where, __x))
Eric Fiselierca91fd02017-05-10 21:30:04 +0000854 return 31 - where;
855 return 32; // Undefined Behavior.
856#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +0000857}
858
859inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000860unsigned long __clz(unsigned long __x) {
861#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000862 return static_cast<unsigned long>(__builtin_clzl (__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000863#else
864 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
865 return __clz(static_cast<unsigned>(__x));
866#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +0000867}
868
869inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000870unsigned long long __clz(unsigned long long __x) {
871#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000872 return static_cast<unsigned long long>(__builtin_clzll(__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000873#else
874 unsigned long where;
875// BitScanReverse scans from MSB to LSB for first set bit.
876// Returns 0 if no set bit is found.
877#if defined(_LIBCPP_HAS_BITSCAN64)
Shoaib Meenai54b78ff2017-12-05 17:46:23 +0000878 if (_BitScanReverse64(&where, __x))
Eric Fiselierca91fd02017-05-10 21:30:04 +0000879 return static_cast<int>(63 - where);
880#else
881 // Scan the high 32 bits.
Shoaib Meenai54b78ff2017-12-05 17:46:23 +0000882 if (_BitScanReverse(&where, static_cast<unsigned long>(__x >> 32)))
Eric Fiselierca91fd02017-05-10 21:30:04 +0000883 return 63 - (where + 32); // Create a bit offset from the MSB.
884 // Scan the low 32 bits.
Shoaib Meenai54b78ff2017-12-05 17:46:23 +0000885 if (_BitScanReverse(&where, static_cast<unsigned long>(__x)))
Eric Fiselierca91fd02017-05-10 21:30:04 +0000886 return 63 - where;
887#endif
888 return 64; // Undefined Behavior.
889#endif // _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000890}
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000891
Eric Fiselierca91fd02017-05-10 21:30:04 +0000892inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) {
893#ifndef _LIBCPP_COMPILER_MSVC
894 return __builtin_popcount (__x);
895#else
896 static_assert(sizeof(unsigned) == 4, "");
897 return __popcnt(__x);
898#endif
899}
900
901inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) {
902#ifndef _LIBCPP_COMPILER_MSVC
903 return __builtin_popcountl (__x);
904#else
905 static_assert(sizeof(unsigned long) == 4, "");
906 return __popcnt(__x);
907#endif
908}
909
910inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {
911#ifndef _LIBCPP_COMPILER_MSVC
912 return __builtin_popcountll(__x);
913#else
914 static_assert(sizeof(unsigned long long) == 8, "");
915 return __popcnt64(__x);
916#endif
917}
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000918
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000919// all_of
920
921template <class _InputIterator, class _Predicate>
Marshall Clowde4b2862018-01-15 17:20:36 +0000922inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000923bool
924all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
925{
926 for (; __first != __last; ++__first)
927 if (!__pred(*__first))
928 return false;
929 return true;
930}
931
932// any_of
933
934template <class _InputIterator, class _Predicate>
Marshall Clowde4b2862018-01-15 17:20:36 +0000935inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000936bool
937any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
938{
939 for (; __first != __last; ++__first)
940 if (__pred(*__first))
941 return true;
942 return false;
943}
944
945// none_of
946
947template <class _InputIterator, class _Predicate>
Marshall Clowde4b2862018-01-15 17:20:36 +0000948inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000949bool
950none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
951{
952 for (; __first != __last; ++__first)
953 if (__pred(*__first))
954 return false;
955 return true;
956}
957
958// for_each
959
960template <class _InputIterator, class _Function>
961inline _LIBCPP_INLINE_VISIBILITY
962_Function
963for_each(_InputIterator __first, _InputIterator __last, _Function __f)
964{
965 for (; __first != __last; ++__first)
966 __f(*__first);
Marshall Clowdb7fa112016-11-14 18:22:19 +0000967 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000968}
969
Marshall Clowefae8792017-05-25 13:40:57 +0000970#if _LIBCPP_STD_VER > 14
Marshall Clow10411c12017-05-25 02:29:54 +0000971// for_each_n
972
973template <class _InputIterator, class _Size, class _Function>
974inline _LIBCPP_INLINE_VISIBILITY
975_InputIterator
976for_each_n(_InputIterator __first, _Size __orig_n, _Function __f)
977{
978 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
979 _IntegralSize __n = __orig_n;
980 while (__n > 0)
981 {
982 __f(*__first);
983 ++__first;
984 --__n;
985 }
986 return __first;
987}
Marshall Clowefae8792017-05-25 13:40:57 +0000988#endif
Marshall Clow10411c12017-05-25 02:29:54 +0000989
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990// find
991
992template <class _InputIterator, class _Tp>
Marshall Clow8d0d8252018-01-15 16:16:32 +0000993inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000994_InputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +0000995find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000996{
997 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +0000998 if (*__first == __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000999 break;
1000 return __first;
1001}
1002
1003// find_if
1004
1005template <class _InputIterator, class _Predicate>
Marshall Clow8d0d8252018-01-15 16:16:32 +00001006inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001007_InputIterator
1008find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1009{
1010 for (; __first != __last; ++__first)
1011 if (__pred(*__first))
1012 break;
1013 return __first;
1014}
1015
1016// find_if_not
1017
1018template<class _InputIterator, class _Predicate>
Marshall Clow6a1c2ff2018-01-15 19:26:05 +00001019inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001020_InputIterator
1021find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1022{
1023 for (; __first != __last; ++__first)
1024 if (!__pred(*__first))
1025 break;
1026 return __first;
1027}
1028
1029// find_end
1030
1031template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow6a1c2ff2018-01-15 19:26:05 +00001032_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001033__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1034 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
1035 forward_iterator_tag, forward_iterator_tag)
1036{
1037 // modeled after search algorithm
1038 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
1039 if (__first2 == __last2)
1040 return __r;
1041 while (true)
1042 {
1043 while (true)
1044 {
1045 if (__first1 == __last1) // if source exhausted return last correct answer
1046 return __r; // (or __last1 if never found)
1047 if (__pred(*__first1, *__first2))
1048 break;
1049 ++__first1;
1050 }
1051 // *__first1 matches *__first2, now match elements after here
1052 _ForwardIterator1 __m1 = __first1;
1053 _ForwardIterator2 __m2 = __first2;
1054 while (true)
1055 {
1056 if (++__m2 == __last2)
1057 { // Pattern exhaused, record answer and search for another one
1058 __r = __first1;
1059 ++__first1;
1060 break;
1061 }
1062 if (++__m1 == __last1) // Source exhausted, return last answer
1063 return __r;
1064 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
1065 {
1066 ++__first1;
1067 break;
1068 } // else there is a match, check next elements
1069 }
1070 }
1071}
1072
1073template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
Marshall Clow6a1c2ff2018-01-15 19:26:05 +00001074_LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator1
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001075__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
1076 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
1077 bidirectional_iterator_tag, bidirectional_iterator_tag)
1078{
1079 // modeled after search algorithm (in reverse)
1080 if (__first2 == __last2)
1081 return __last1; // Everything matches an empty sequence
1082 _BidirectionalIterator1 __l1 = __last1;
1083 _BidirectionalIterator2 __l2 = __last2;
1084 --__l2;
1085 while (true)
1086 {
1087 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
1088 while (true)
1089 {
1090 if (__first1 == __l1) // return __last1 if no element matches *__first2
1091 return __last1;
1092 if (__pred(*--__l1, *__l2))
1093 break;
1094 }
1095 // *__l1 matches *__l2, now match elements before here
1096 _BidirectionalIterator1 __m1 = __l1;
1097 _BidirectionalIterator2 __m2 = __l2;
1098 while (true)
1099 {
1100 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
1101 return __m1;
1102 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
1103 return __last1;
1104 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
1105 {
1106 break;
1107 } // else there is a match, check next elements
1108 }
1109 }
1110}
1111
1112template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow37025e12014-06-10 18:51:55 +00001113_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001114__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1115 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1116 random_access_iterator_tag, random_access_iterator_tag)
1117{
1118 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1119 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
1120 if (__len2 == 0)
1121 return __last1;
1122 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
1123 if (__len1 < __len2)
1124 return __last1;
1125 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
1126 _RandomAccessIterator1 __l1 = __last1;
1127 _RandomAccessIterator2 __l2 = __last2;
1128 --__l2;
1129 while (true)
1130 {
1131 while (true)
1132 {
1133 if (__s == __l1)
1134 return __last1;
1135 if (__pred(*--__l1, *__l2))
1136 break;
1137 }
1138 _RandomAccessIterator1 __m1 = __l1;
1139 _RandomAccessIterator2 __m2 = __l2;
1140 while (true)
1141 {
1142 if (__m2 == __first2)
1143 return __m1;
1144 // no need to check range on __m1 because __s guarantees we have enough source
1145 if (!__pred(*--__m1, *--__m2))
1146 {
1147 break;
1148 }
1149 }
1150 }
1151}
1152
1153template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow6a1c2ff2018-01-15 19:26:05 +00001154inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001155_ForwardIterator1
1156find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1157 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1158{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001159 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001160 (__first1, __last1, __first2, __last2, __pred,
1161 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1162 typename iterator_traits<_ForwardIterator2>::iterator_category());
1163}
1164
1165template <class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow6a1c2ff2018-01-15 19:26:05 +00001166inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001167_ForwardIterator1
1168find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1169 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1170{
1171 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1172 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001173 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001174}
1175
1176// find_first_of
1177
1178template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow37025e12014-06-10 18:51:55 +00001179_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1180__find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001181 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1182{
1183 for (; __first1 != __last1; ++__first1)
1184 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1185 if (__pred(*__first1, *__j))
1186 return __first1;
1187 return __last1;
1188}
1189
Marshall Clow37025e12014-06-10 18:51:55 +00001190
1191template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow6a1c2ff2018-01-15 19:26:05 +00001192inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow37025e12014-06-10 18:51:55 +00001193_ForwardIterator1
1194find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1195 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1196{
1197 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1198}
1199
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001200template <class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow6a1c2ff2018-01-15 19:26:05 +00001201inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001202_ForwardIterator1
1203find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1204 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1205{
1206 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1207 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Marshall Clow37025e12014-06-10 18:51:55 +00001208 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001209}
1210
1211// adjacent_find
1212
1213template <class _ForwardIterator, class _BinaryPredicate>
Marshall Clow6a1c2ff2018-01-15 19:26:05 +00001214inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001215_ForwardIterator
1216adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1217{
1218 if (__first != __last)
1219 {
1220 _ForwardIterator __i = __first;
1221 while (++__i != __last)
1222 {
1223 if (__pred(*__first, *__i))
1224 return __first;
1225 __first = __i;
1226 }
1227 }
1228 return __last;
1229}
1230
1231template <class _ForwardIterator>
Marshall Clow6a1c2ff2018-01-15 19:26:05 +00001232inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001233_ForwardIterator
1234adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1235{
1236 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001237 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001238}
1239
1240// count
1241
1242template <class _InputIterator, class _Tp>
Marshall Clow0dc8ca72018-01-15 19:40:34 +00001243inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001244typename iterator_traits<_InputIterator>::difference_type
Howard Hinnant78b68282011-10-22 20:59:45 +00001245count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001246{
1247 typename iterator_traits<_InputIterator>::difference_type __r(0);
1248 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +00001249 if (*__first == __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001250 ++__r;
1251 return __r;
1252}
1253
1254// count_if
1255
1256template <class _InputIterator, class _Predicate>
Marshall Clow0dc8ca72018-01-15 19:40:34 +00001257inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001258typename iterator_traits<_InputIterator>::difference_type
1259count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1260{
1261 typename iterator_traits<_InputIterator>::difference_type __r(0);
1262 for (; __first != __last; ++__first)
1263 if (__pred(*__first))
1264 ++__r;
1265 return __r;
1266}
1267
1268// mismatch
1269
1270template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1271inline _LIBCPP_INLINE_VISIBILITY
1272pair<_InputIterator1, _InputIterator2>
1273mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1274 _InputIterator2 __first2, _BinaryPredicate __pred)
1275{
Marshall Clowd402a4d2014-09-16 20:40:05 +00001276 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001277 if (!__pred(*__first1, *__first2))
1278 break;
1279 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1280}
1281
1282template <class _InputIterator1, class _InputIterator2>
1283inline _LIBCPP_INLINE_VISIBILITY
1284pair<_InputIterator1, _InputIterator2>
1285mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1286{
1287 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1288 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001289 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001290}
1291
Marshall Clowb30abdd2013-05-09 21:14:23 +00001292#if _LIBCPP_STD_VER > 11
1293template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1294inline _LIBCPP_INLINE_VISIBILITY
1295pair<_InputIterator1, _InputIterator2>
1296mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1297 _InputIterator2 __first2, _InputIterator2 __last2,
1298 _BinaryPredicate __pred)
1299{
Marshall Clowd402a4d2014-09-16 20:40:05 +00001300 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001301 if (!__pred(*__first1, *__first2))
1302 break;
1303 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1304}
1305
1306template <class _InputIterator1, class _InputIterator2>
1307inline _LIBCPP_INLINE_VISIBILITY
1308pair<_InputIterator1, _InputIterator2>
1309mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1310 _InputIterator2 __first2, _InputIterator2 __last2)
1311{
1312 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1313 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1314 return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1315}
1316#endif
1317
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001318// equal
1319
1320template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1321inline _LIBCPP_INLINE_VISIBILITY
1322bool
1323equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1324{
Eric Fiselierb9919752014-10-27 19:28:20 +00001325 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326 if (!__pred(*__first1, *__first2))
1327 return false;
1328 return true;
1329}
1330
1331template <class _InputIterator1, class _InputIterator2>
1332inline _LIBCPP_INLINE_VISIBILITY
1333bool
1334equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1335{
1336 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1337 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001338 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001339}
1340
Marshall Clowb30abdd2013-05-09 21:14:23 +00001341#if _LIBCPP_STD_VER > 11
1342template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
1343inline _LIBCPP_INLINE_VISIBILITY
1344bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001345__equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001346 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1347 input_iterator_tag, input_iterator_tag )
1348{
Eric Fiselierb9919752014-10-27 19:28:20 +00001349 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001350 if (!__pred(*__first1, *__first2))
1351 return false;
1352 return __first1 == __last1 && __first2 == __last2;
1353}
1354
1355template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1356inline _LIBCPP_INLINE_VISIBILITY
1357bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001358__equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1359 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001360 random_access_iterator_tag, random_access_iterator_tag )
1361{
1362 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1363 return false;
1364 return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1365 typename add_lvalue_reference<_BinaryPredicate>::type>
1366 (__first1, __last1, __first2, __pred );
1367}
1368
1369template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1370inline _LIBCPP_INLINE_VISIBILITY
1371bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001372equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001373 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1374{
1375 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001376 (__first1, __last1, __first2, __last2, __pred,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001377 typename iterator_traits<_InputIterator1>::iterator_category(),
1378 typename iterator_traits<_InputIterator2>::iterator_category());
1379}
1380
1381template <class _InputIterator1, class _InputIterator2>
1382inline _LIBCPP_INLINE_VISIBILITY
1383bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001384equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001385 _InputIterator2 __first2, _InputIterator2 __last2)
1386{
1387 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1388 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1389 return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1390 typename iterator_traits<_InputIterator1>::iterator_category(),
1391 typename iterator_traits<_InputIterator2>::iterator_category());
1392}
1393#endif
1394
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001395// is_permutation
1396
1397template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow8d0d8252018-01-15 16:16:32 +00001398_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001399is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1400 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1401{
Marshall Clow8d0d8252018-01-15 16:16:32 +00001402// shorten sequences as much as possible by lopping of any equal prefix
Eric Fiselierb9919752014-10-27 19:28:20 +00001403 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001404 if (!__pred(*__first1, *__first2))
Marshall Clow8d0d8252018-01-15 16:16:32 +00001405 break;
1406 if (__first1 == __last1)
1407 return true;
1408
1409// __first1 != __last1 && *__first1 != *__first2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001410 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001411 _D1 __l1 = _VSTD::distance(__first1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001412 if (__l1 == _D1(1))
1413 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001414 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001415 // For each element in [f1, l1) see if there are the same number of
1416 // equal elements in [f2, l2)
1417 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1418 {
Marshall Clow8d0d8252018-01-15 16:16:32 +00001419 // Have we already counted the number of *__i in [f1, l1)?
1420 if (find(__first1, __i, *__i) == __i) {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001421 // Count number of *__i in [f2, l2)
1422 _D1 __c2 = 0;
1423 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1424 if (__pred(*__i, *__j))
1425 ++__c2;
1426 if (__c2 == 0)
1427 return false;
1428 // Count number of *__i in [__i, l1) (we can start with 1)
1429 _D1 __c1 = 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001430 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001431 if (__pred(*__i, *__j))
1432 ++__c1;
1433 if (__c1 != __c2)
1434 return false;
1435 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001436 }
1437 return true;
1438}
1439
1440template<class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow8d0d8252018-01-15 16:16:32 +00001441inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001442bool
1443is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1444 _ForwardIterator2 __first2)
1445{
1446 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1447 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001448 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001449}
1450
Marshall Clowb30abdd2013-05-09 21:14:23 +00001451#if _LIBCPP_STD_VER > 11
1452template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow8d0d8252018-01-15 16:16:32 +00001453_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Marshall Clowb30abdd2013-05-09 21:14:23 +00001454__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001455 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001456 _BinaryPredicate __pred,
1457 forward_iterator_tag, forward_iterator_tag )
1458{
Marshall Clow8d0d8252018-01-15 16:16:32 +00001459// shorten sequences as much as possible by lopping of any equal prefix
Eric Fiselier62a0e012014-10-27 20:26:25 +00001460 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001461 if (!__pred(*__first1, *__first2))
Marshall Clow8d0d8252018-01-15 16:16:32 +00001462 break;
1463 if (__first1 == __last1)
1464 return __first2 == __last2;
1465 else if (__first2 == __last2)
1466 return false;
1467
Marshall Clowb30abdd2013-05-09 21:14:23 +00001468 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1469 _D1 __l1 = _VSTD::distance(__first1, __last1);
1470
1471 typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
Marshall Clow9f8f5242013-05-10 00:16:10 +00001472 _D2 __l2 = _VSTD::distance(__first2, __last2);
Marshall Clowb30abdd2013-05-09 21:14:23 +00001473 if (__l1 != __l2)
1474 return false;
1475
1476 // For each element in [f1, l1) see if there are the same number of
1477 // equal elements in [f2, l2)
1478 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1479 {
Marshall Clow8d0d8252018-01-15 16:16:32 +00001480 // Have we already counted the number of *__i in [f1, l1)?
1481 if (find(__first1, __i, *__i) == __i) {
Marshall Clowb30abdd2013-05-09 21:14:23 +00001482 // Count number of *__i in [f2, l2)
1483 _D1 __c2 = 0;
1484 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1485 if (__pred(*__i, *__j))
1486 ++__c2;
1487 if (__c2 == 0)
1488 return false;
1489 // Count number of *__i in [__i, l1) (we can start with 1)
1490 _D1 __c1 = 1;
1491 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1492 if (__pred(*__i, *__j))
1493 ++__c1;
1494 if (__c1 != __c2)
1495 return false;
1496 }
Marshall Clowb30abdd2013-05-09 21:14:23 +00001497 }
1498 return true;
1499}
1500
1501template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow8d0d8252018-01-15 16:16:32 +00001502_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Marshall Clowb30abdd2013-05-09 21:14:23 +00001503__is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001504 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001505 _BinaryPredicate __pred,
1506 random_access_iterator_tag, random_access_iterator_tag )
1507{
1508 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1509 return false;
1510 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1511 typename add_lvalue_reference<_BinaryPredicate>::type>
1512 (__first1, __last1, __first2, __pred );
1513}
1514
1515template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow8d0d8252018-01-15 16:16:32 +00001516inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowb30abdd2013-05-09 21:14:23 +00001517bool
1518is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1519 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1520 _BinaryPredicate __pred )
1521{
1522 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1523 (__first1, __last1, __first2, __last2, __pred,
1524 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1525 typename iterator_traits<_ForwardIterator2>::iterator_category());
1526}
1527
1528template<class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow8d0d8252018-01-15 16:16:32 +00001529inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowb30abdd2013-05-09 21:14:23 +00001530bool
1531is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1532 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1533{
1534 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1535 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1536 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1537 __equal_to<__v1, __v2>(),
1538 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1539 typename iterator_traits<_ForwardIterator2>::iterator_category());
1540}
1541#endif
1542
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001543// search
Marshall Clow33406332018-01-08 19:18:00 +00001544// __search is in <functional>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001545
1546template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1547inline _LIBCPP_INLINE_VISIBILITY
1548_ForwardIterator1
1549search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1550 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1551{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001552 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001553 (__first1, __last1, __first2, __last2, __pred,
Marshall Clowf6d6b512016-03-08 15:12:52 +00001554 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1555 typename iterator_traits<_ForwardIterator2>::iterator_category())
1556 .first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001557}
1558
1559template <class _ForwardIterator1, class _ForwardIterator2>
1560inline _LIBCPP_INLINE_VISIBILITY
1561_ForwardIterator1
1562search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1563 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1564{
Marshall Clowf6d6b512016-03-08 15:12:52 +00001565 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1566 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001567 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001568}
1569
Marshall Clow33406332018-01-08 19:18:00 +00001570
1571#if _LIBCPP_STD_VER > 14
1572template <class _ForwardIterator, class _Searcher>
1573_LIBCPP_INLINE_VISIBILITY
1574_ForwardIterator search(_ForwardIterator __f, _ForwardIterator __l, const _Searcher &__s)
1575{ return __s(__f, __l).first; }
1576#endif
1577
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001578// search_n
1579
1580template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
1581_ForwardIterator
1582__search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001583 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001584{
1585 if (__count <= 0)
1586 return __first;
1587 while (true)
1588 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001589 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001590 while (true)
1591 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001592 if (__first == __last) // return __last if no element matches __value_
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001593 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001594 if (__pred(*__first, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001595 break;
1596 ++__first;
1597 }
Howard Hinnant78b68282011-10-22 20:59:45 +00001598 // *__first matches __value_, now match elements after here
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001599 _ForwardIterator __m = __first;
1600 _Size __c(0);
1601 while (true)
1602 {
1603 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1604 return __first;
1605 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1606 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001607 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001608 {
1609 __first = __m;
1610 ++__first;
1611 break;
1612 } // else there is a match, check next elements
1613 }
1614 }
1615}
1616
1617template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
1618_RandomAccessIterator
1619__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001620 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001621{
1622 if (__count <= 0)
1623 return __first;
1624 _Size __len = static_cast<_Size>(__last - __first);
1625 if (__len < __count)
1626 return __last;
1627 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1628 while (true)
1629 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001630 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001631 while (true)
1632 {
Howard Hinnant128f7bf2013-04-04 15:40:48 +00001633 if (__first >= __s) // return __last if no element matches __value_
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001634 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001635 if (__pred(*__first, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001636 break;
1637 ++__first;
1638 }
Howard Hinnant78b68282011-10-22 20:59:45 +00001639 // *__first matches __value_, now match elements after here
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640 _RandomAccessIterator __m = __first;
1641 _Size __c(0);
1642 while (true)
1643 {
1644 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1645 return __first;
1646 ++__m; // no need to check range on __m because __s guarantees we have enough source
Howard Hinnant78b68282011-10-22 20:59:45 +00001647 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001648 {
1649 __first = __m;
1650 ++__first;
1651 break;
1652 } // else there is a match, check next elements
1653 }
1654 }
1655}
1656
1657template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
1658inline _LIBCPP_INLINE_VISIBILITY
1659_ForwardIterator
1660search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001661 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001662{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001663 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001664 (__first, __last, __convert_to_integral(__count), __value_, __pred,
1665 typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001666}
1667
1668template <class _ForwardIterator, class _Size, class _Tp>
1669inline _LIBCPP_INLINE_VISIBILITY
1670_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00001671search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001672{
1673 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001674 return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
1675 __value_, __equal_to<__v, _Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676}
1677
1678// copy
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001679template <class _Iter>
1680inline _LIBCPP_INLINE_VISIBILITY
1681_Iter
1682__unwrap_iter(_Iter __i)
1683{
1684 return __i;
1685}
1686
1687template <class _Tp>
Marshall Clow798ecd62017-05-25 14:20:26 +00001688inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001689typename enable_if
1690<
Howard Hinnant1468b662010-11-19 22:17:28 +00001691 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001692 _Tp*
1693>::type
1694__unwrap_iter(move_iterator<_Tp*> __i)
1695{
1696 return __i.base();
1697}
1698
Howard Hinnant499cea12013-08-23 17:37:05 +00001699#if _LIBCPP_DEBUG_LEVEL < 2
1700
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001701template <class _Tp>
Marshall Clow798ecd62017-05-25 14:20:26 +00001702inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001703typename enable_if
1704<
Howard Hinnant1468b662010-11-19 22:17:28 +00001705 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001706 _Tp*
1707>::type
1708__unwrap_iter(__wrap_iter<_Tp*> __i)
1709{
1710 return __i.base();
1711}
1712
Eric Fiselier2c8aa052016-12-28 05:35:32 +00001713#else
1714
1715template <class _Tp>
Marshall Clow798ecd62017-05-25 14:20:26 +00001716inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2c8aa052016-12-28 05:35:32 +00001717typename enable_if
1718<
1719 is_trivially_copy_assignable<_Tp>::value,
1720 __wrap_iter<_Tp*>
1721>::type
1722__unwrap_iter(__wrap_iter<_Tp*> __i)
1723{
1724 return __i;
1725}
1726
Howard Hinnant499cea12013-08-23 17:37:05 +00001727#endif // _LIBCPP_DEBUG_LEVEL < 2
1728
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001729template <class _InputIterator, class _OutputIterator>
1730inline _LIBCPP_INLINE_VISIBILITY
1731_OutputIterator
1732__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1733{
Eric Fiselierb9919752014-10-27 19:28:20 +00001734 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001735 *__result = *__first;
1736 return __result;
1737}
1738
1739template <class _Tp, class _Up>
1740inline _LIBCPP_INLINE_VISIBILITY
1741typename enable_if
1742<
1743 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001744 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001745 _Up*
1746>::type
1747__copy(_Tp* __first, _Tp* __last, _Up* __result)
1748{
1749 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001750 if (__n > 0)
1751 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001752 return __result + __n;
1753}
1754
1755template <class _InputIterator, class _OutputIterator>
1756inline _LIBCPP_INLINE_VISIBILITY
1757_OutputIterator
1758copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1759{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001760 return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001761}
1762
1763// copy_backward
1764
Howard Hinnantb73568d2013-02-06 21:03:39 +00001765template <class _BidirectionalIterator, class _OutputIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001766inline _LIBCPP_INLINE_VISIBILITY
1767_OutputIterator
Howard Hinnantb73568d2013-02-06 21:03:39 +00001768__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001769{
1770 while (__first != __last)
1771 *--__result = *--__last;
1772 return __result;
1773}
1774
1775template <class _Tp, class _Up>
1776inline _LIBCPP_INLINE_VISIBILITY
1777typename enable_if
1778<
1779 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001780 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001781 _Up*
1782>::type
1783__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1784{
1785 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001786 if (__n > 0)
1787 {
1788 __result -= __n;
1789 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1790 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001791 return __result;
1792}
1793
1794template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1795inline _LIBCPP_INLINE_VISIBILITY
1796_BidirectionalIterator2
1797copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1798 _BidirectionalIterator2 __result)
1799{
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00001800 return _VSTD::__copy_backward(__unwrap_iter(__first),
1801 __unwrap_iter(__last),
1802 __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001803}
1804
1805// copy_if
1806
1807template<class _InputIterator, class _OutputIterator, class _Predicate>
1808inline _LIBCPP_INLINE_VISIBILITY
1809_OutputIterator
1810copy_if(_InputIterator __first, _InputIterator __last,
1811 _OutputIterator __result, _Predicate __pred)
1812{
1813 for (; __first != __last; ++__first)
1814 {
1815 if (__pred(*__first))
1816 {
1817 *__result = *__first;
1818 ++__result;
1819 }
1820 }
1821 return __result;
1822}
1823
1824// copy_n
1825
1826template<class _InputIterator, class _Size, class _OutputIterator>
1827inline _LIBCPP_INLINE_VISIBILITY
1828typename enable_if
1829<
1830 __is_input_iterator<_InputIterator>::value &&
1831 !__is_random_access_iterator<_InputIterator>::value,
1832 _OutputIterator
1833>::type
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001834copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001835{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001836 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1837 _IntegralSize __n = __orig_n;
Howard Hinnant171869e2011-02-27 20:55:39 +00001838 if (__n > 0)
1839 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001840 *__result = *__first;
Howard Hinnant171869e2011-02-27 20:55:39 +00001841 ++__result;
1842 for (--__n; __n > 0; --__n)
1843 {
1844 ++__first;
1845 *__result = *__first;
1846 ++__result;
1847 }
1848 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001849 return __result;
1850}
1851
1852template<class _InputIterator, class _Size, class _OutputIterator>
1853inline _LIBCPP_INLINE_VISIBILITY
1854typename enable_if
1855<
1856 __is_random_access_iterator<_InputIterator>::value,
1857 _OutputIterator
1858>::type
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001859copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001860{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001861 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1862 _IntegralSize __n = __orig_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001863 return _VSTD::copy(__first, __first + __n, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001864}
1865
1866// move
1867
1868template <class _InputIterator, class _OutputIterator>
1869inline _LIBCPP_INLINE_VISIBILITY
1870_OutputIterator
1871__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1872{
Eric Fiselierb9919752014-10-27 19:28:20 +00001873 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001874 *__result = _VSTD::move(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001875 return __result;
1876}
1877
1878template <class _Tp, class _Up>
1879inline _LIBCPP_INLINE_VISIBILITY
1880typename enable_if
1881<
1882 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001883 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001884 _Up*
1885>::type
1886__move(_Tp* __first, _Tp* __last, _Up* __result)
1887{
1888 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001889 if (__n > 0)
1890 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001891 return __result + __n;
1892}
1893
1894template <class _InputIterator, class _OutputIterator>
1895inline _LIBCPP_INLINE_VISIBILITY
1896_OutputIterator
1897move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1898{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001899 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001900}
1901
1902// move_backward
1903
1904template <class _InputIterator, class _OutputIterator>
1905inline _LIBCPP_INLINE_VISIBILITY
1906_OutputIterator
1907__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1908{
1909 while (__first != __last)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001910 *--__result = _VSTD::move(*--__last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001911 return __result;
1912}
1913
1914template <class _Tp, class _Up>
1915inline _LIBCPP_INLINE_VISIBILITY
1916typename enable_if
1917<
1918 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001919 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001920 _Up*
1921>::type
1922__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1923{
1924 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001925 if (__n > 0)
1926 {
1927 __result -= __n;
1928 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1929 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001930 return __result;
1931}
1932
1933template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1934inline _LIBCPP_INLINE_VISIBILITY
1935_BidirectionalIterator2
1936move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1937 _BidirectionalIterator2 __result)
1938{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001939 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001940}
1941
1942// iter_swap
1943
Howard Hinnante9b2c2d2011-05-27 15:04:19 +00001944// moved to <type_traits> for better swap / noexcept support
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001945
1946// transform
1947
1948template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
1949inline _LIBCPP_INLINE_VISIBILITY
1950_OutputIterator
1951transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1952{
Eric Fiselierb9919752014-10-27 19:28:20 +00001953 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001954 *__result = __op(*__first);
1955 return __result;
1956}
1957
1958template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
1959inline _LIBCPP_INLINE_VISIBILITY
1960_OutputIterator
1961transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1962 _OutputIterator __result, _BinaryOperation __binary_op)
1963{
Eric Fiselierb9919752014-10-27 19:28:20 +00001964 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001965 *__result = __binary_op(*__first1, *__first2);
1966 return __result;
1967}
1968
1969// replace
1970
1971template <class _ForwardIterator, class _Tp>
1972inline _LIBCPP_INLINE_VISIBILITY
1973void
1974replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1975{
1976 for (; __first != __last; ++__first)
1977 if (*__first == __old_value)
1978 *__first = __new_value;
1979}
1980
1981// replace_if
1982
1983template <class _ForwardIterator, class _Predicate, class _Tp>
1984inline _LIBCPP_INLINE_VISIBILITY
1985void
1986replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1987{
1988 for (; __first != __last; ++__first)
1989 if (__pred(*__first))
1990 *__first = __new_value;
1991}
1992
1993// replace_copy
1994
1995template <class _InputIterator, class _OutputIterator, class _Tp>
1996inline _LIBCPP_INLINE_VISIBILITY
1997_OutputIterator
1998replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1999 const _Tp& __old_value, const _Tp& __new_value)
2000{
Eric Fiselierb9919752014-10-27 19:28:20 +00002001 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002002 if (*__first == __old_value)
2003 *__result = __new_value;
2004 else
2005 *__result = *__first;
2006 return __result;
2007}
2008
2009// replace_copy_if
2010
2011template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
2012inline _LIBCPP_INLINE_VISIBILITY
2013_OutputIterator
2014replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2015 _Predicate __pred, const _Tp& __new_value)
2016{
Eric Fiselierb9919752014-10-27 19:28:20 +00002017 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002018 if (__pred(*__first))
2019 *__result = __new_value;
2020 else
2021 *__result = *__first;
2022 return __result;
2023}
2024
2025// fill_n
2026
2027template <class _OutputIterator, class _Size, class _Tp>
2028inline _LIBCPP_INLINE_VISIBILITY
2029_OutputIterator
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002030__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002031{
Eric Fiselierb9919752014-10-27 19:28:20 +00002032 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnant78b68282011-10-22 20:59:45 +00002033 *__first = __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002034 return __first;
2035}
2036
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002037template <class _Tp, class _Size, class _Up>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002038inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002039typename enable_if
2040<
2041 is_integral<_Tp>::value && sizeof(_Tp) == 1 &&
2042 !is_same<_Tp, bool>::value &&
2043 is_integral<_Up>::value && sizeof(_Up) == 1,
2044 _Tp*
2045>::type
2046__fill_n(_Tp* __first, _Size __n,_Up __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002047{
2048 if (__n > 0)
Howard Hinnant78b68282011-10-22 20:59:45 +00002049 _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002050 return __first + __n;
2051}
2052
2053template <class _OutputIterator, class _Size, class _Tp>
2054inline _LIBCPP_INLINE_VISIBILITY
2055_OutputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002056fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002057{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002058 return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002059}
2060
2061// fill
2062
2063template <class _ForwardIterator, class _Tp>
2064inline _LIBCPP_INLINE_VISIBILITY
2065void
Howard Hinnant78b68282011-10-22 20:59:45 +00002066__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002067{
2068 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +00002069 *__first = __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070}
2071
2072template <class _RandomAccessIterator, class _Tp>
2073inline _LIBCPP_INLINE_VISIBILITY
2074void
Howard Hinnant78b68282011-10-22 20:59:45 +00002075__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002076{
Howard Hinnant78b68282011-10-22 20:59:45 +00002077 _VSTD::fill_n(__first, __last - __first, __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002078}
2079
2080template <class _ForwardIterator, class _Tp>
2081inline _LIBCPP_INLINE_VISIBILITY
2082void
Howard Hinnant78b68282011-10-22 20:59:45 +00002083fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002084{
Howard Hinnant78b68282011-10-22 20:59:45 +00002085 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002086}
2087
2088// generate
2089
2090template <class _ForwardIterator, class _Generator>
2091inline _LIBCPP_INLINE_VISIBILITY
2092void
2093generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2094{
2095 for (; __first != __last; ++__first)
2096 *__first = __gen();
2097}
2098
2099// generate_n
2100
2101template <class _OutputIterator, class _Size, class _Generator>
2102inline _LIBCPP_INLINE_VISIBILITY
2103_OutputIterator
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002104generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002105{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002106 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
2107 _IntegralSize __n = __orig_n;
Eric Fiselierb9919752014-10-27 19:28:20 +00002108 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002109 *__first = __gen();
2110 return __first;
2111}
2112
2113// remove
2114
2115template <class _ForwardIterator, class _Tp>
2116_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002117remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002118{
Howard Hinnant78b68282011-10-22 20:59:45 +00002119 __first = _VSTD::find(__first, __last, __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002120 if (__first != __last)
2121 {
2122 _ForwardIterator __i = __first;
2123 while (++__i != __last)
2124 {
Howard Hinnant78b68282011-10-22 20:59:45 +00002125 if (!(*__i == __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002126 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002127 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002128 ++__first;
2129 }
2130 }
2131 }
2132 return __first;
2133}
2134
2135// remove_if
2136
2137template <class _ForwardIterator, class _Predicate>
2138_ForwardIterator
2139remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2140{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002141 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002142 (__first, __last, __pred);
2143 if (__first != __last)
2144 {
2145 _ForwardIterator __i = __first;
2146 while (++__i != __last)
2147 {
2148 if (!__pred(*__i))
2149 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002150 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002151 ++__first;
2152 }
2153 }
2154 }
2155 return __first;
2156}
2157
2158// remove_copy
2159
2160template <class _InputIterator, class _OutputIterator, class _Tp>
2161inline _LIBCPP_INLINE_VISIBILITY
2162_OutputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002163remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002164{
2165 for (; __first != __last; ++__first)
2166 {
Howard Hinnant78b68282011-10-22 20:59:45 +00002167 if (!(*__first == __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002168 {
2169 *__result = *__first;
2170 ++__result;
2171 }
2172 }
2173 return __result;
2174}
2175
2176// remove_copy_if
2177
2178template <class _InputIterator, class _OutputIterator, class _Predicate>
2179inline _LIBCPP_INLINE_VISIBILITY
2180_OutputIterator
2181remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2182{
2183 for (; __first != __last; ++__first)
2184 {
2185 if (!__pred(*__first))
2186 {
2187 *__result = *__first;
2188 ++__result;
2189 }
2190 }
2191 return __result;
2192}
2193
2194// unique
2195
2196template <class _ForwardIterator, class _BinaryPredicate>
2197_ForwardIterator
2198unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2199{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002200 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002201 (__first, __last, __pred);
2202 if (__first != __last)
2203 {
2204 // ... a a ? ...
2205 // f i
2206 _ForwardIterator __i = __first;
2207 for (++__i; ++__i != __last;)
2208 if (!__pred(*__first, *__i))
Howard Hinnant0949eed2011-06-30 21:18:19 +00002209 *++__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002210 ++__first;
2211 }
2212 return __first;
2213}
2214
2215template <class _ForwardIterator>
2216inline _LIBCPP_INLINE_VISIBILITY
2217_ForwardIterator
2218unique(_ForwardIterator __first, _ForwardIterator __last)
2219{
2220 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002221 return _VSTD::unique(__first, __last, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002222}
2223
2224// unique_copy
2225
2226template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
2227_OutputIterator
2228__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2229 input_iterator_tag, output_iterator_tag)
2230{
2231 if (__first != __last)
2232 {
2233 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2234 *__result = __t;
2235 ++__result;
2236 while (++__first != __last)
2237 {
2238 if (!__pred(__t, *__first))
2239 {
2240 __t = *__first;
2241 *__result = __t;
2242 ++__result;
2243 }
2244 }
2245 }
2246 return __result;
2247}
2248
2249template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
2250_OutputIterator
2251__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2252 forward_iterator_tag, output_iterator_tag)
2253{
2254 if (__first != __last)
2255 {
2256 _ForwardIterator __i = __first;
2257 *__result = *__i;
2258 ++__result;
2259 while (++__first != __last)
2260 {
2261 if (!__pred(*__i, *__first))
2262 {
2263 *__result = *__first;
2264 ++__result;
2265 __i = __first;
2266 }
2267 }
2268 }
2269 return __result;
2270}
2271
2272template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
2273_ForwardIterator
2274__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2275 input_iterator_tag, forward_iterator_tag)
2276{
2277 if (__first != __last)
2278 {
2279 *__result = *__first;
2280 while (++__first != __last)
2281 if (!__pred(*__result, *__first))
2282 *++__result = *__first;
2283 ++__result;
2284 }
2285 return __result;
2286}
2287
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002288template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
2289inline _LIBCPP_INLINE_VISIBILITY
2290_OutputIterator
2291unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2292{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002293 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002294 (__first, __last, __result, __pred,
2295 typename iterator_traits<_InputIterator>::iterator_category(),
2296 typename iterator_traits<_OutputIterator>::iterator_category());
2297}
2298
2299template <class _InputIterator, class _OutputIterator>
2300inline _LIBCPP_INLINE_VISIBILITY
2301_OutputIterator
2302unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2303{
2304 typedef typename iterator_traits<_InputIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002305 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002306}
2307
2308// reverse
2309
2310template <class _BidirectionalIterator>
2311inline _LIBCPP_INLINE_VISIBILITY
2312void
2313__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2314{
2315 while (__first != __last)
2316 {
2317 if (__first == --__last)
2318 break;
Marshall Clowc010bd62015-11-02 21:34:25 +00002319 _VSTD::iter_swap(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002320 ++__first;
2321 }
2322}
2323
2324template <class _RandomAccessIterator>
2325inline _LIBCPP_INLINE_VISIBILITY
2326void
2327__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2328{
2329 if (__first != __last)
2330 for (; __first < --__last; ++__first)
Marshall Clowc010bd62015-11-02 21:34:25 +00002331 _VSTD::iter_swap(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002332}
2333
2334template <class _BidirectionalIterator>
2335inline _LIBCPP_INLINE_VISIBILITY
2336void
2337reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2338{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002339 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002340}
2341
2342// reverse_copy
2343
2344template <class _BidirectionalIterator, class _OutputIterator>
2345inline _LIBCPP_INLINE_VISIBILITY
2346_OutputIterator
2347reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2348{
2349 for (; __first != __last; ++__result)
2350 *__result = *--__last;
2351 return __result;
2352}
2353
2354// rotate
2355
2356template <class _ForwardIterator>
2357_ForwardIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002358__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002359{
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002360 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2361 value_type __tmp = _VSTD::move(*__first);
2362 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2363 *__lm1 = _VSTD::move(__tmp);
2364 return __lm1;
2365}
2366
2367template <class _BidirectionalIterator>
2368_BidirectionalIterator
2369__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2370{
2371 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2372 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2373 value_type __tmp = _VSTD::move(*__lm1);
2374 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2375 *__first = _VSTD::move(__tmp);
2376 return __fp1;
2377}
2378
2379template <class _ForwardIterator>
2380_ForwardIterator
2381__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2382{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002383 _ForwardIterator __i = __middle;
2384 while (true)
2385 {
2386 swap(*__first, *__i);
2387 ++__first;
2388 if (++__i == __last)
2389 break;
2390 if (__first == __middle)
2391 __middle = __i;
2392 }
2393 _ForwardIterator __r = __first;
2394 if (__first != __middle)
2395 {
2396 __i = __middle;
2397 while (true)
2398 {
2399 swap(*__first, *__i);
2400 ++__first;
2401 if (++__i == __last)
2402 {
2403 if (__first == __middle)
2404 break;
2405 __i = __middle;
2406 }
2407 else if (__first == __middle)
2408 __middle = __i;
2409 }
2410 }
2411 return __r;
2412}
2413
2414template<typename _Integral>
2415inline _LIBCPP_INLINE_VISIBILITY
2416_Integral
Marshall Clow1c1e91d2016-07-26 14:29:45 +00002417__algo_gcd(_Integral __x, _Integral __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002418{
2419 do
2420 {
2421 _Integral __t = __x % __y;
2422 __x = __y;
2423 __y = __t;
2424 } while (__y);
2425 return __x;
2426}
2427
2428template<typename _RandomAccessIterator>
2429_RandomAccessIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002430__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002431{
2432 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2433 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant324bb032010-08-22 00:02:43 +00002434
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002435 const difference_type __m1 = __middle - __first;
2436 const difference_type __m2 = __last - __middle;
2437 if (__m1 == __m2)
2438 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002439 _VSTD::swap_ranges(__first, __middle, __middle);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002440 return __middle;
2441 }
Marshall Clow1c1e91d2016-07-26 14:29:45 +00002442 const difference_type __g = _VSTD::__algo_gcd(__m1, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002443 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2444 {
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002445 value_type __t(_VSTD::move(*--__p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002446 _RandomAccessIterator __p1 = __p;
2447 _RandomAccessIterator __p2 = __p1 + __m1;
2448 do
2449 {
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002450 *__p1 = _VSTD::move(*__p2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002451 __p1 = __p2;
2452 const difference_type __d = __last - __p2;
2453 if (__m1 < __d)
2454 __p2 += __m1;
2455 else
2456 __p2 = __first + (__m1 - __d);
2457 } while (__p2 != __p);
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002458 *__p1 = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002459 }
2460 return __first + __m2;
2461}
2462
2463template <class _ForwardIterator>
2464inline _LIBCPP_INLINE_VISIBILITY
2465_ForwardIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002466__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2467 _VSTD::forward_iterator_tag)
2468{
2469 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2470 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2471 {
2472 if (_VSTD::next(__first) == __middle)
2473 return _VSTD::__rotate_left(__first, __last);
2474 }
2475 return _VSTD::__rotate_forward(__first, __middle, __last);
2476}
2477
2478template <class _BidirectionalIterator>
2479inline _LIBCPP_INLINE_VISIBILITY
2480_BidirectionalIterator
2481__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2482 _VSTD::bidirectional_iterator_tag)
2483{
2484 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2485 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2486 {
2487 if (_VSTD::next(__first) == __middle)
2488 return _VSTD::__rotate_left(__first, __last);
2489 if (_VSTD::next(__middle) == __last)
2490 return _VSTD::__rotate_right(__first, __last);
2491 }
2492 return _VSTD::__rotate_forward(__first, __middle, __last);
2493}
2494
2495template <class _RandomAccessIterator>
2496inline _LIBCPP_INLINE_VISIBILITY
2497_RandomAccessIterator
2498__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2499 _VSTD::random_access_iterator_tag)
2500{
2501 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2502 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2503 {
2504 if (_VSTD::next(__first) == __middle)
2505 return _VSTD::__rotate_left(__first, __last);
2506 if (_VSTD::next(__middle) == __last)
2507 return _VSTD::__rotate_right(__first, __last);
2508 return _VSTD::__rotate_gcd(__first, __middle, __last);
2509 }
2510 return _VSTD::__rotate_forward(__first, __middle, __last);
2511}
2512
2513template <class _ForwardIterator>
2514inline _LIBCPP_INLINE_VISIBILITY
2515_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002516rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2517{
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002518 if (__first == __middle)
2519 return __last;
2520 if (__middle == __last)
2521 return __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002522 return _VSTD::__rotate(__first, __middle, __last,
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002523 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002524}
2525
2526// rotate_copy
2527
2528template <class _ForwardIterator, class _OutputIterator>
2529inline _LIBCPP_INLINE_VISIBILITY
2530_OutputIterator
2531rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2532{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002533 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002534}
2535
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002536// min_element
2537
2538template <class _ForwardIterator, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002539inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002540_ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +00002541min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002542{
2543 if (__first != __last)
2544 {
2545 _ForwardIterator __i = __first;
2546 while (++__i != __last)
2547 if (__comp(*__i, *__first))
2548 __first = __i;
2549 }
2550 return __first;
2551}
2552
2553template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002554inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002555_ForwardIterator
2556min_element(_ForwardIterator __first, _ForwardIterator __last)
2557{
Marshall Clow928735a2015-05-10 13:53:31 +00002558 return _VSTD::min_element(__first, __last,
Howard Hinnant98e5d972010-08-21 20:10:01 +00002559 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2560}
2561
2562// min
2563
2564template <class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002565inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002566const _Tp&
2567min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2568{
2569 return __comp(__b, __a) ? __b : __a;
2570}
2571
2572template <class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002573inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002574const _Tp&
2575min(const _Tp& __a, const _Tp& __b)
2576{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002577 return _VSTD::min(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002578}
2579
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002580#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002581
Howard Hinnant98e5d972010-08-21 20:10:01 +00002582template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002583inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002584_Tp
2585min(initializer_list<_Tp> __t, _Compare __comp)
2586{
Marshall Clow928735a2015-05-10 13:53:31 +00002587 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002588}
2589
2590template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002591inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002592_Tp
2593min(initializer_list<_Tp> __t)
2594{
Marshall Clow928735a2015-05-10 13:53:31 +00002595 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002596}
2597
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002598#endif // _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002599
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002600// max_element
2601
2602template <class _ForwardIterator, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002603inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002604_ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +00002605max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002606{
2607 if (__first != __last)
2608 {
2609 _ForwardIterator __i = __first;
2610 while (++__i != __last)
2611 if (__comp(*__first, *__i))
2612 __first = __i;
2613 }
2614 return __first;
2615}
2616
Marshall Clow9d9463a2014-02-19 16:51:35 +00002617
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002618template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002619inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002620_ForwardIterator
2621max_element(_ForwardIterator __first, _ForwardIterator __last)
2622{
Marshall Clow928735a2015-05-10 13:53:31 +00002623 return _VSTD::max_element(__first, __last,
Howard Hinnant98e5d972010-08-21 20:10:01 +00002624 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2625}
2626
2627// max
2628
2629template <class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002630inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002631const _Tp&
2632max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2633{
2634 return __comp(__a, __b) ? __b : __a;
2635}
2636
2637template <class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002638inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002639const _Tp&
2640max(const _Tp& __a, const _Tp& __b)
2641{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002642 return _VSTD::max(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002643}
2644
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002645#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002646
Howard Hinnant98e5d972010-08-21 20:10:01 +00002647template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002648inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002649_Tp
2650max(initializer_list<_Tp> __t, _Compare __comp)
2651{
Marshall Clow928735a2015-05-10 13:53:31 +00002652 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002653}
2654
2655template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002656inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002657_Tp
2658max(initializer_list<_Tp> __t)
2659{
Marshall Clow928735a2015-05-10 13:53:31 +00002660 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002661}
2662
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002663#endif // _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002664
Marshall Clow3e0808e2016-03-07 22:43:49 +00002665#if _LIBCPP_STD_VER > 14
2666// clamp
2667template<class _Tp, class _Compare>
2668inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2669const _Tp&
2670clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
2671{
2672 _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
2673 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
2674
2675}
2676
2677template<class _Tp>
2678inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2679const _Tp&
2680clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
2681{
2682 return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
2683}
2684#endif
2685
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002686// minmax_element
2687
2688template <class _ForwardIterator, class _Compare>
Marshall Clow928735a2015-05-10 13:53:31 +00002689_LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002690std::pair<_ForwardIterator, _ForwardIterator>
2691minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2692{
2693 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2694 if (__first != __last)
2695 {
2696 if (++__first != __last)
2697 {
2698 if (__comp(*__first, *__result.first))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002699 __result.first = __first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002700 else
2701 __result.second = __first;
2702 while (++__first != __last)
2703 {
2704 _ForwardIterator __i = __first;
2705 if (++__first == __last)
2706 {
2707 if (__comp(*__i, *__result.first))
2708 __result.first = __i;
2709 else if (!__comp(*__i, *__result.second))
2710 __result.second = __i;
2711 break;
2712 }
2713 else
2714 {
2715 if (__comp(*__first, *__i))
2716 {
2717 if (__comp(*__first, *__result.first))
2718 __result.first = __first;
2719 if (!__comp(*__i, *__result.second))
2720 __result.second = __i;
2721 }
2722 else
2723 {
2724 if (__comp(*__i, *__result.first))
2725 __result.first = __i;
2726 if (!__comp(*__first, *__result.second))
2727 __result.second = __first;
2728 }
2729 }
2730 }
2731 }
2732 }
2733 return __result;
2734}
2735
2736template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002737inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002738std::pair<_ForwardIterator, _ForwardIterator>
2739minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2740{
Marshall Clow9d9463a2014-02-19 16:51:35 +00002741 return _VSTD::minmax_element(__first, __last,
2742 __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002743}
2744
Howard Hinnant98e5d972010-08-21 20:10:01 +00002745// minmax
2746
2747template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002748inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002749pair<const _Tp&, const _Tp&>
2750minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2751{
2752 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2753 pair<const _Tp&, const _Tp&>(__a, __b);
2754}
2755
2756template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002757inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002758pair<const _Tp&, const _Tp&>
2759minmax(const _Tp& __a, const _Tp& __b)
2760{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002761 return _VSTD::minmax(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002762}
2763
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002764#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002765
Howard Hinnant98e5d972010-08-21 20:10:01 +00002766template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002767inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002768pair<_Tp, _Tp>
2769minmax(initializer_list<_Tp> __t, _Compare __comp)
2770{
Marshall Clow9d9463a2014-02-19 16:51:35 +00002771 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2772 _Iter __first = __t.begin();
2773 _Iter __last = __t.end();
Marshall Clow3024f862015-02-11 15:41:34 +00002774 std::pair<_Tp, _Tp> __result(*__first, *__first);
Marshall Clow9d9463a2014-02-19 16:51:35 +00002775
2776 ++__first;
2777 if (__t.size() % 2 == 0)
2778 {
2779 if (__comp(*__first, __result.first))
2780 __result.first = *__first;
2781 else
2782 __result.second = *__first;
2783 ++__first;
2784 }
Aditya Kumarfdb4f172016-08-25 11:52:38 +00002785
Marshall Clow9d9463a2014-02-19 16:51:35 +00002786 while (__first != __last)
2787 {
2788 _Tp __prev = *__first++;
Marshall Clow3024f862015-02-11 15:41:34 +00002789 if (__comp(*__first, __prev)) {
2790 if ( __comp(*__first, __result.first)) __result.first = *__first;
2791 if (!__comp(__prev, __result.second)) __result.second = __prev;
Marshall Clow9d9463a2014-02-19 16:51:35 +00002792 }
2793 else {
Marshall Clow3024f862015-02-11 15:41:34 +00002794 if ( __comp(__prev, __result.first)) __result.first = __prev;
2795 if (!__comp(*__first, __result.second)) __result.second = *__first;
Marshall Clow9d9463a2014-02-19 16:51:35 +00002796 }
Aditya Kumarfdb4f172016-08-25 11:52:38 +00002797
Marshall Clow9d9463a2014-02-19 16:51:35 +00002798 __first++;
2799 }
2800 return __result;
2801}
2802
2803template<class _Tp>
2804inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2805pair<_Tp, _Tp>
2806minmax(initializer_list<_Tp> __t)
2807{
2808 return _VSTD::minmax(__t, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002809}
2810
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002811#endif // _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002812
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002813// random_shuffle
2814
Howard Hinnantc3267212010-05-26 17:49:34 +00002815// __independent_bits_engine
2816
Howard Hinnant99968442011-11-29 18:15:50 +00002817template <unsigned long long _Xp, size_t _Rp>
Howard Hinnantc3267212010-05-26 17:49:34 +00002818struct __log2_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002819{
Howard Hinnant99968442011-11-29 18:15:50 +00002820 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2821 : __log2_imp<_Xp, _Rp - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002822};
2823
Howard Hinnant99968442011-11-29 18:15:50 +00002824template <unsigned long long _Xp>
2825struct __log2_imp<_Xp, 0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002826{
Howard Hinnantc3267212010-05-26 17:49:34 +00002827 static const size_t value = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002828};
2829
Howard Hinnant99968442011-11-29 18:15:50 +00002830template <size_t _Rp>
2831struct __log2_imp<0, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002832{
Howard Hinnant99968442011-11-29 18:15:50 +00002833 static const size_t value = _Rp + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002834};
2835
Eric Fiselierac633a22017-05-31 21:20:18 +00002836template <class _UIntType, _UIntType _Xp>
Howard Hinnantc3267212010-05-26 17:49:34 +00002837struct __log2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002838{
Howard Hinnant99968442011-11-29 18:15:50 +00002839 static const size_t value = __log2_imp<_Xp,
Eric Fiselierac633a22017-05-31 21:20:18 +00002840 sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002841};
2842
Howard Hinnantc3267212010-05-26 17:49:34 +00002843template<class _Engine, class _UIntType>
2844class __independent_bits_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002845{
Howard Hinnantc3267212010-05-26 17:49:34 +00002846public:
2847 // types
2848 typedef _UIntType result_type;
2849
2850private:
2851 typedef typename _Engine::result_type _Engine_result_type;
2852 typedef typename conditional
2853 <
2854 sizeof(_Engine_result_type) <= sizeof(result_type),
2855 result_type,
2856 _Engine_result_type
2857 >::type _Working_result_type;
2858
2859 _Engine& __e_;
2860 size_t __w_;
2861 size_t __w0_;
2862 size_t __n_;
2863 size_t __n0_;
2864 _Working_result_type __y0_;
2865 _Working_result_type __y1_;
2866 _Engine_result_type __mask0_;
2867 _Engine_result_type __mask1_;
2868
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002869#ifdef _LIBCPP_CXX03_LANG
Howard Hinnant99968442011-11-29 18:15:50 +00002870 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002871 + _Working_result_type(1);
2872#else
2873 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2874 + _Working_result_type(1);
2875#endif
2876 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2877 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2878 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
Howard Hinnantc3267212010-05-26 17:49:34 +00002879
2880public:
2881 // constructors and seeding functions
2882 __independent_bits_engine(_Engine& __e, size_t __w);
2883
2884 // generating functions
Howard Hinnant99968442011-11-29 18:15:50 +00002885 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantc3267212010-05-26 17:49:34 +00002886
2887private:
Marshall Clow1e32db72017-09-20 19:38:43 +00002888 result_type __eval(false_type);
2889 result_type __eval(true_type);
Howard Hinnantc3267212010-05-26 17:49:34 +00002890};
2891
2892template<class _Engine, class _UIntType>
2893__independent_bits_engine<_Engine, _UIntType>
2894 ::__independent_bits_engine(_Engine& __e, size_t __w)
2895 : __e_(__e),
2896 __w_(__w)
2897{
2898 __n_ = __w_ / __m + (__w_ % __m != 0);
2899 __w0_ = __w_ / __n_;
Howard Hinnant99968442011-11-29 18:15:50 +00002900 if (_Rp == 0)
2901 __y0_ = _Rp;
Howard Hinnantc3267212010-05-26 17:49:34 +00002902 else if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00002903 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002904 else
2905 __y0_ = 0;
Howard Hinnant99968442011-11-29 18:15:50 +00002906 if (_Rp - __y0_ > __y0_ / __n_)
Howard Hinnantc3267212010-05-26 17:49:34 +00002907 {
2908 ++__n_;
2909 __w0_ = __w_ / __n_;
2910 if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00002911 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002912 else
2913 __y0_ = 0;
2914 }
2915 __n0_ = __n_ - __w_ % __n_;
2916 if (__w0_ < _WDt - 1)
Howard Hinnant99968442011-11-29 18:15:50 +00002917 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
Howard Hinnantc3267212010-05-26 17:49:34 +00002918 else
2919 __y1_ = 0;
2920 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2921 _Engine_result_type(0);
2922 __mask1_ = __w0_ < _EDt - 1 ?
2923 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2924 _Engine_result_type(~0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002925}
2926
Howard Hinnantc3267212010-05-26 17:49:34 +00002927template<class _Engine, class _UIntType>
2928inline
2929_UIntType
Marshall Clow1e32db72017-09-20 19:38:43 +00002930__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002931{
Howard Hinnantc3267212010-05-26 17:49:34 +00002932 return static_cast<result_type>(__e_() & __mask0_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002933}
2934
Howard Hinnantc3267212010-05-26 17:49:34 +00002935template<class _Engine, class _UIntType>
2936_UIntType
Marshall Clow1e32db72017-09-20 19:38:43 +00002937__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002938{
Marshall Clowe2b68322017-09-20 17:34:11 +00002939 const size_t _WRt = numeric_limits<result_type>::digits;
Howard Hinnant99968442011-11-29 18:15:50 +00002940 result_type _Sp = 0;
Howard Hinnantc3267212010-05-26 17:49:34 +00002941 for (size_t __k = 0; __k < __n0_; ++__k)
2942 {
2943 _Engine_result_type __u;
2944 do
2945 {
2946 __u = __e_() - _Engine::min();
2947 } while (__u >= __y0_);
Marshall Clowe2b68322017-09-20 17:34:11 +00002948 if (__w0_ < _WRt)
Howard Hinnant99968442011-11-29 18:15:50 +00002949 _Sp <<= __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002950 else
Howard Hinnant99968442011-11-29 18:15:50 +00002951 _Sp = 0;
2952 _Sp += __u & __mask0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002953 }
2954 for (size_t __k = __n0_; __k < __n_; ++__k)
2955 {
2956 _Engine_result_type __u;
2957 do
2958 {
2959 __u = __e_() - _Engine::min();
2960 } while (__u >= __y1_);
Marshall Clowe2b68322017-09-20 17:34:11 +00002961 if (__w0_ < _WRt - 1)
Howard Hinnant99968442011-11-29 18:15:50 +00002962 _Sp <<= __w0_ + 1;
Howard Hinnantc3267212010-05-26 17:49:34 +00002963 else
Howard Hinnant99968442011-11-29 18:15:50 +00002964 _Sp = 0;
2965 _Sp += __u & __mask1_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002966 }
Howard Hinnant99968442011-11-29 18:15:50 +00002967 return _Sp;
Howard Hinnantc3267212010-05-26 17:49:34 +00002968}
2969
2970// uniform_int_distribution
2971
2972template<class _IntType = int>
2973class uniform_int_distribution
2974{
2975public:
2976 // types
2977 typedef _IntType result_type;
2978
2979 class param_type
2980 {
2981 result_type __a_;
2982 result_type __b_;
2983 public:
2984 typedef uniform_int_distribution distribution_type;
2985
2986 explicit param_type(result_type __a = 0,
2987 result_type __b = numeric_limits<result_type>::max())
2988 : __a_(__a), __b_(__b) {}
2989
2990 result_type a() const {return __a_;}
2991 result_type b() const {return __b_;}
2992
2993 friend bool operator==(const param_type& __x, const param_type& __y)
2994 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2995 friend bool operator!=(const param_type& __x, const param_type& __y)
2996 {return !(__x == __y);}
2997 };
2998
2999private:
3000 param_type __p_;
3001
3002public:
3003 // constructors and reset functions
3004 explicit uniform_int_distribution(result_type __a = 0,
3005 result_type __b = numeric_limits<result_type>::max())
3006 : __p_(param_type(__a, __b)) {}
3007 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
3008 void reset() {}
3009
3010 // generating functions
3011 template<class _URNG> result_type operator()(_URNG& __g)
3012 {return (*this)(__g, __p_);}
3013 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3014
3015 // property functions
3016 result_type a() const {return __p_.a();}
3017 result_type b() const {return __p_.b();}
3018
3019 param_type param() const {return __p_;}
3020 void param(const param_type& __p) {__p_ = __p;}
3021
3022 result_type min() const {return a();}
3023 result_type max() const {return b();}
3024
3025 friend bool operator==(const uniform_int_distribution& __x,
3026 const uniform_int_distribution& __y)
3027 {return __x.__p_ == __y.__p_;}
3028 friend bool operator!=(const uniform_int_distribution& __x,
3029 const uniform_int_distribution& __y)
3030 {return !(__x == __y);}
3031};
3032
3033template<class _IntType>
3034template<class _URNG>
3035typename uniform_int_distribution<_IntType>::result_type
3036uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3037{
3038 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3039 uint32_t, uint64_t>::type _UIntType;
Howard Hinnant99968442011-11-29 18:15:50 +00003040 const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
3041 if (_Rp == 1)
Howard Hinnantc3267212010-05-26 17:49:34 +00003042 return __p.a();
3043 const size_t _Dt = numeric_limits<_UIntType>::digits;
3044 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
Howard Hinnant99968442011-11-29 18:15:50 +00003045 if (_Rp == 0)
Howard Hinnantc3267212010-05-26 17:49:34 +00003046 return static_cast<result_type>(_Eng(__g, _Dt)());
Howard Hinnant99968442011-11-29 18:15:50 +00003047 size_t __w = _Dt - __clz(_Rp) - 1;
Marshall Clow0934c752015-07-30 18:26:34 +00003048 if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
Howard Hinnantc3267212010-05-26 17:49:34 +00003049 ++__w;
3050 _Eng __e(__g, __w);
3051 _UIntType __u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003052 do
Howard Hinnantc3267212010-05-26 17:49:34 +00003053 {
3054 __u = __e();
Howard Hinnant99968442011-11-29 18:15:50 +00003055 } while (__u >= _Rp);
Howard Hinnantc3267212010-05-26 17:49:34 +00003056 return static_cast<result_type>(__u + __p.a());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003057}
3058
Eric Fiselier6547dc82017-04-03 23:23:44 +00003059#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \
3060 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003061class _LIBCPP_TYPE_VIS __rs_default;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003062
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003063_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantc3267212010-05-26 17:49:34 +00003064
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003065class _LIBCPP_TYPE_VIS __rs_default
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003066{
Howard Hinnantc3267212010-05-26 17:49:34 +00003067 static unsigned __c_;
3068
3069 __rs_default();
3070public:
Marshall Clow5920cfc2013-02-07 22:12:02 +00003071 typedef uint_fast32_t result_type;
Howard Hinnantc3267212010-05-26 17:49:34 +00003072
3073 static const result_type _Min = 0;
3074 static const result_type _Max = 0xFFFFFFFF;
3075
3076 __rs_default(const __rs_default&);
3077 ~__rs_default();
3078
3079 result_type operator()();
3080
Howard Hinnant27b4fd32012-04-02 00:40:41 +00003081 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
3082 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
Howard Hinnantc3267212010-05-26 17:49:34 +00003083
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003084 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003085};
3086
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003087_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003088
3089template <class _RandomAccessIterator>
3090void
3091random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
3092{
3093 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnant99968442011-11-29 18:15:50 +00003094 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3095 typedef typename _Dp::param_type _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003096 difference_type __d = __last - __first;
3097 if (__d > 1)
3098 {
Howard Hinnant99968442011-11-29 18:15:50 +00003099 _Dp __uid;
Howard Hinnantc3267212010-05-26 17:49:34 +00003100 __rs_default __g = __rs_get();
3101 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003102 {
Howard Hinnant99968442011-11-29 18:15:50 +00003103 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant4e599482010-10-22 15:26:39 +00003104 if (__i != difference_type(0))
3105 swap(*__first, *(__first + __i));
3106 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003107 }
3108}
3109
3110template <class _RandomAccessIterator, class _RandomNumberGenerator>
3111void
3112random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00003113#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003114 _RandomNumberGenerator&& __rand)
3115#else
3116 _RandomNumberGenerator& __rand)
3117#endif
3118{
3119 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3120 difference_type __d = __last - __first;
3121 if (__d > 1)
3122 {
3123 for (--__last; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003124 {
3125 difference_type __i = __rand(__d);
3126 swap(*__first, *(__first + __i));
3127 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003128 }
3129}
Marshall Clow03b862f2017-03-23 13:43:37 +00003130#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003131
Eric Fiselier917af0a2016-08-28 22:14:37 +00003132template <class _PopulationIterator, class _SampleIterator, class _Distance,
3133 class _UniformRandomNumberGenerator>
3134_LIBCPP_INLINE_VISIBILITY
3135_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsone208d082017-11-14 11:14:25 +00003136 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier917af0a2016-08-28 22:14:37 +00003137 _Distance __n,
3138 _UniformRandomNumberGenerator & __g,
3139 input_iterator_tag) {
3140
3141 _Distance __k = 0;
3142 for (; __first != __last && __k < __n; ++__first, (void)++__k)
Alexander Richardsone208d082017-11-14 11:14:25 +00003143 __output_iter[__k] = *__first;
Eric Fiselier917af0a2016-08-28 22:14:37 +00003144 _Distance __sz = __k;
3145 for (; __first != __last; ++__first, (void)++__k) {
3146 _Distance __r = _VSTD::uniform_int_distribution<_Distance>(0, __k)(__g);
3147 if (__r < __sz)
Alexander Richardsone208d082017-11-14 11:14:25 +00003148 __output_iter[__r] = *__first;
Eric Fiselier917af0a2016-08-28 22:14:37 +00003149 }
Alexander Richardsone208d082017-11-14 11:14:25 +00003150 return __output_iter + _VSTD::min(__n, __k);
Eric Fiselier917af0a2016-08-28 22:14:37 +00003151}
3152
3153template <class _PopulationIterator, class _SampleIterator, class _Distance,
3154 class _UniformRandomNumberGenerator>
3155_LIBCPP_INLINE_VISIBILITY
3156_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsone208d082017-11-14 11:14:25 +00003157 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier917af0a2016-08-28 22:14:37 +00003158 _Distance __n,
3159 _UniformRandomNumberGenerator& __g,
3160 forward_iterator_tag) {
3161 _Distance __unsampled_sz = _VSTD::distance(__first, __last);
3162 for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
3163 _Distance __r =
3164 _VSTD::uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
3165 if (__r < __n) {
Alexander Richardsone208d082017-11-14 11:14:25 +00003166 *__output_iter++ = *__first;
Eric Fiselier917af0a2016-08-28 22:14:37 +00003167 --__n;
3168 }
3169 }
Alexander Richardsone208d082017-11-14 11:14:25 +00003170 return __output_iter;
Eric Fiselier917af0a2016-08-28 22:14:37 +00003171}
3172
3173template <class _PopulationIterator, class _SampleIterator, class _Distance,
3174 class _UniformRandomNumberGenerator>
3175_LIBCPP_INLINE_VISIBILITY
3176_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsone208d082017-11-14 11:14:25 +00003177 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier917af0a2016-08-28 22:14:37 +00003178 _Distance __n, _UniformRandomNumberGenerator& __g) {
3179 typedef typename iterator_traits<_PopulationIterator>::iterator_category
3180 _PopCategory;
3181 typedef typename iterator_traits<_PopulationIterator>::difference_type
3182 _Difference;
3183 static_assert(__is_forward_iterator<_PopulationIterator>::value ||
3184 __is_random_access_iterator<_SampleIterator>::value,
3185 "SampleIterator must meet the requirements of RandomAccessIterator");
3186 typedef typename common_type<_Distance, _Difference>::type _CommonType;
3187 _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
3188 return _VSTD::__sample(
Alexander Richardsone208d082017-11-14 11:14:25 +00003189 __first, __last, __output_iter, _CommonType(__n),
Eric Fiselier917af0a2016-08-28 22:14:37 +00003190 __g, _PopCategory());
3191}
3192
3193#if _LIBCPP_STD_VER > 14
3194template <class _PopulationIterator, class _SampleIterator, class _Distance,
3195 class _UniformRandomNumberGenerator>
3196inline _LIBCPP_INLINE_VISIBILITY
3197_SampleIterator sample(_PopulationIterator __first,
Alexander Richardsone208d082017-11-14 11:14:25 +00003198 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier917af0a2016-08-28 22:14:37 +00003199 _Distance __n, _UniformRandomNumberGenerator&& __g) {
Alexander Richardsone208d082017-11-14 11:14:25 +00003200 return _VSTD::__sample(__first, __last, __output_iter, __n, __g);
Eric Fiselier917af0a2016-08-28 22:14:37 +00003201}
3202#endif // _LIBCPP_STD_VER > 14
3203
Howard Hinnantc3267212010-05-26 17:49:34 +00003204template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3205 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00003206#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant278bf2d2010-11-18 01:47:02 +00003207 _UniformRandomNumberGenerator&& __g)
3208#else
Howard Hinnantc3267212010-05-26 17:49:34 +00003209 _UniformRandomNumberGenerator& __g)
Howard Hinnant278bf2d2010-11-18 01:47:02 +00003210#endif
Howard Hinnantc3267212010-05-26 17:49:34 +00003211{
3212 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnant99968442011-11-29 18:15:50 +00003213 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3214 typedef typename _Dp::param_type _Pp;
Howard Hinnantc3267212010-05-26 17:49:34 +00003215 difference_type __d = __last - __first;
3216 if (__d > 1)
3217 {
Howard Hinnant99968442011-11-29 18:15:50 +00003218 _Dp __uid;
Howard Hinnantc3267212010-05-26 17:49:34 +00003219 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003220 {
Howard Hinnant99968442011-11-29 18:15:50 +00003221 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant4e599482010-10-22 15:26:39 +00003222 if (__i != difference_type(0))
3223 swap(*__first, *(__first + __i));
3224 }
Howard Hinnantc3267212010-05-26 17:49:34 +00003225 }
3226}
3227
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003228template <class _InputIterator, class _Predicate>
Marshall Clow8d0d8252018-01-15 16:16:32 +00003229_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003230is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3231{
3232 for (; __first != __last; ++__first)
3233 if (!__pred(*__first))
3234 break;
Marshall Clowa0ec4b72015-02-02 18:16:35 +00003235 if ( __first == __last )
3236 return true;
3237 ++__first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003238 for (; __first != __last; ++__first)
3239 if (__pred(*__first))
3240 return false;
3241 return true;
3242}
3243
3244// partition
3245
3246template <class _Predicate, class _ForwardIterator>
3247_ForwardIterator
3248__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3249{
3250 while (true)
3251 {
3252 if (__first == __last)
3253 return __first;
3254 if (!__pred(*__first))
3255 break;
3256 ++__first;
3257 }
3258 for (_ForwardIterator __p = __first; ++__p != __last;)
3259 {
3260 if (__pred(*__p))
3261 {
3262 swap(*__first, *__p);
3263 ++__first;
3264 }
3265 }
3266 return __first;
3267}
3268
3269template <class _Predicate, class _BidirectionalIterator>
3270_BidirectionalIterator
3271__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3272 bidirectional_iterator_tag)
3273{
3274 while (true)
3275 {
3276 while (true)
3277 {
3278 if (__first == __last)
3279 return __first;
3280 if (!__pred(*__first))
3281 break;
3282 ++__first;
3283 }
3284 do
3285 {
3286 if (__first == --__last)
3287 return __first;
3288 } while (!__pred(*__last));
3289 swap(*__first, *__last);
3290 ++__first;
3291 }
3292}
3293
3294template <class _ForwardIterator, class _Predicate>
3295inline _LIBCPP_INLINE_VISIBILITY
3296_ForwardIterator
3297partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3298{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003299 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003300 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3301}
3302
3303// partition_copy
3304
3305template <class _InputIterator, class _OutputIterator1,
3306 class _OutputIterator2, class _Predicate>
3307pair<_OutputIterator1, _OutputIterator2>
3308partition_copy(_InputIterator __first, _InputIterator __last,
3309 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3310 _Predicate __pred)
3311{
3312 for (; __first != __last; ++__first)
3313 {
3314 if (__pred(*__first))
3315 {
3316 *__out_true = *__first;
3317 ++__out_true;
3318 }
3319 else
3320 {
3321 *__out_false = *__first;
3322 ++__out_false;
3323 }
3324 }
3325 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3326}
3327
3328// partition_point
3329
3330template<class _ForwardIterator, class _Predicate>
Marshall Clowbb8010f2018-01-15 17:53:34 +00003331_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003332partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3333{
3334 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003335 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003336 while (__len != 0)
3337 {
3338 difference_type __l2 = __len / 2;
3339 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003340 _VSTD::advance(__m, __l2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003341 if (__pred(*__m))
3342 {
3343 __first = ++__m;
3344 __len -= __l2 + 1;
3345 }
3346 else
3347 __len = __l2;
3348 }
3349 return __first;
3350}
3351
3352// stable_partition
3353
3354template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3355_ForwardIterator
3356__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3357 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3358{
3359 // *__first is known to be false
3360 // __len >= 1
3361 if (__len == 1)
3362 return __first;
3363 if (__len == 2)
3364 {
3365 _ForwardIterator __m = __first;
3366 if (__pred(*++__m))
3367 {
3368 swap(*__first, *__m);
3369 return __m;
3370 }
3371 return __first;
3372 }
3373 if (__len <= __p.second)
3374 { // The buffer is big enough to use
3375 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3376 __destruct_n __d(0);
3377 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3378 // Move the falses into the temporary buffer, and the trues to the front of the line
3379 // Update __first to always point to the end of the trues
3380 value_type* __t = __p.first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003381 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003382 __d.__incr((value_type*)0);
3383 ++__t;
3384 _ForwardIterator __i = __first;
3385 while (++__i != __last)
3386 {
3387 if (__pred(*__i))
3388 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003389 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003390 ++__first;
3391 }
3392 else
3393 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003394 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003395 __d.__incr((value_type*)0);
3396 ++__t;
3397 }
3398 }
3399 // All trues now at start of range, all falses in buffer
3400 // Move falses back into range, but don't mess up __first which points to first false
3401 __i = __first;
3402 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003403 *__i = _VSTD::move(*__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003404 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3405 return __first;
3406 }
3407 // Else not enough buffer, do in place
3408 // __len >= 3
3409 _ForwardIterator __m = __first;
3410 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003411 _VSTD::advance(__m, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003412 // recurse on [__first, __m), *__first know to be false
3413 // F?????????????????
3414 // f m l
3415 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3416 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3417 // TTTFFFFF??????????
3418 // f ff m l
3419 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3420 _ForwardIterator __m1 = __m;
3421 _ForwardIterator __second_false = __last;
3422 _Distance __len_half = __len - __len2;
3423 while (__pred(*__m1))
3424 {
3425 if (++__m1 == __last)
3426 goto __second_half_done;
3427 --__len_half;
3428 }
3429 // TTTFFFFFTTTF??????
3430 // f ff m m1 l
3431 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3432__second_half_done:
3433 // TTTFFFFFTTTTTFFFFF
3434 // f ff m sf l
Howard Hinnant0949eed2011-06-30 21:18:19 +00003435 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003436 // TTTTTTTTFFFFFFFFFF
3437 // |
3438}
3439
3440struct __return_temporary_buffer
3441{
3442 template <class _Tp>
Howard Hinnant0949eed2011-06-30 21:18:19 +00003443 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003444};
3445
3446template <class _Predicate, class _ForwardIterator>
3447_ForwardIterator
3448__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3449 forward_iterator_tag)
3450{
3451 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3452 // Either prove all true and return __first or point to first false
3453 while (true)
3454 {
3455 if (__first == __last)
3456 return __first;
3457 if (!__pred(*__first))
3458 break;
3459 ++__first;
3460 }
3461 // We now have a reduced range [__first, __last)
3462 // *__first is known to be false
3463 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3464 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003465 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003466 pair<value_type*, ptrdiff_t> __p(0, 0);
3467 unique_ptr<value_type, __return_temporary_buffer> __h;
3468 if (__len >= __alloc_limit)
3469 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003470 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003471 __h.reset(__p.first);
3472 }
3473 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3474 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3475}
3476
3477template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3478_BidirectionalIterator
3479__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3480 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3481{
3482 // *__first is known to be false
3483 // *__last is known to be true
3484 // __len >= 2
3485 if (__len == 2)
3486 {
3487 swap(*__first, *__last);
3488 return __last;
3489 }
3490 if (__len == 3)
3491 {
3492 _BidirectionalIterator __m = __first;
3493 if (__pred(*++__m))
3494 {
3495 swap(*__first, *__m);
3496 swap(*__m, *__last);
3497 return __last;
3498 }
3499 swap(*__m, *__last);
3500 swap(*__first, *__m);
3501 return __m;
3502 }
3503 if (__len <= __p.second)
3504 { // The buffer is big enough to use
3505 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3506 __destruct_n __d(0);
3507 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3508 // Move the falses into the temporary buffer, and the trues to the front of the line
3509 // Update __first to always point to the end of the trues
3510 value_type* __t = __p.first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003511 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003512 __d.__incr((value_type*)0);
3513 ++__t;
3514 _BidirectionalIterator __i = __first;
3515 while (++__i != __last)
3516 {
3517 if (__pred(*__i))
3518 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003519 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003520 ++__first;
3521 }
3522 else
3523 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003524 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003525 __d.__incr((value_type*)0);
3526 ++__t;
3527 }
3528 }
3529 // move *__last, known to be true
Howard Hinnant0949eed2011-06-30 21:18:19 +00003530 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003531 __i = ++__first;
3532 // All trues now at start of range, all falses in buffer
3533 // Move falses back into range, but don't mess up __first which points to first false
3534 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003535 *__i = _VSTD::move(*__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003536 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3537 return __first;
3538 }
3539 // Else not enough buffer, do in place
3540 // __len >= 4
3541 _BidirectionalIterator __m = __first;
3542 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003543 _VSTD::advance(__m, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003544 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3545 // F????????????????T
3546 // f m l
3547 _BidirectionalIterator __m1 = __m;
3548 _BidirectionalIterator __first_false = __first;
3549 _Distance __len_half = __len2;
3550 while (!__pred(*--__m1))
3551 {
3552 if (__m1 == __first)
3553 goto __first_half_done;
3554 --__len_half;
3555 }
3556 // F???TFFF?????????T
3557 // f m1 m l
3558 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3559 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3560__first_half_done:
3561 // TTTFFFFF?????????T
3562 // f ff m l
3563 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3564 __m1 = __m;
3565 _BidirectionalIterator __second_false = __last;
3566 ++__second_false;
3567 __len_half = __len - __len2;
3568 while (__pred(*__m1))
3569 {
3570 if (++__m1 == __last)
3571 goto __second_half_done;
3572 --__len_half;
3573 }
3574 // TTTFFFFFTTTF?????T
3575 // f ff m m1 l
3576 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3577__second_half_done:
3578 // TTTFFFFFTTTTTFFFFF
3579 // f ff m sf l
Howard Hinnant0949eed2011-06-30 21:18:19 +00003580 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003581 // TTTTTTTTFFFFFFFFFF
3582 // |
3583}
3584
3585template <class _Predicate, class _BidirectionalIterator>
3586_BidirectionalIterator
3587__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3588 bidirectional_iterator_tag)
3589{
3590 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3591 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3592 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3593 // Either prove all true and return __first or point to first false
3594 while (true)
3595 {
3596 if (__first == __last)
3597 return __first;
3598 if (!__pred(*__first))
3599 break;
3600 ++__first;
3601 }
3602 // __first points to first false, everything prior to __first is already set.
3603 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3604 do
3605 {
3606 if (__first == --__last)
3607 return __first;
3608 } while (!__pred(*__last));
3609 // We now have a reduced range [__first, __last]
3610 // *__first is known to be false
3611 // *__last is known to be true
3612 // __len >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003613 difference_type __len = _VSTD::distance(__first, __last) + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003614 pair<value_type*, ptrdiff_t> __p(0, 0);
3615 unique_ptr<value_type, __return_temporary_buffer> __h;
3616 if (__len >= __alloc_limit)
3617 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003618 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003619 __h.reset(__p.first);
3620 }
3621 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3622 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3623}
3624
3625template <class _ForwardIterator, class _Predicate>
3626inline _LIBCPP_INLINE_VISIBILITY
3627_ForwardIterator
3628stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3629{
3630 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3631 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3632}
3633
3634// is_sorted_until
3635
3636template <class _ForwardIterator, class _Compare>
Marshall Clow8d0d8252018-01-15 16:16:32 +00003637_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003638is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3639{
3640 if (__first != __last)
3641 {
3642 _ForwardIterator __i = __first;
3643 while (++__i != __last)
3644 {
3645 if (__comp(*__i, *__first))
3646 return __i;
3647 __first = __i;
3648 }
3649 }
3650 return __last;
3651}
3652
Howard Hinnant324bb032010-08-22 00:02:43 +00003653template<class _ForwardIterator>
Marshall Clow8d0d8252018-01-15 16:16:32 +00003654inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003655_ForwardIterator
3656is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3657{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003658 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003659}
3660
3661// is_sorted
3662
3663template <class _ForwardIterator, class _Compare>
Marshall Clow8d0d8252018-01-15 16:16:32 +00003664inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003665bool
3666is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3667{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003668 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003669}
3670
Howard Hinnant324bb032010-08-22 00:02:43 +00003671template<class _ForwardIterator>
Marshall Clow8d0d8252018-01-15 16:16:32 +00003672inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003673bool
3674is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3675{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003676 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003677}
3678
3679// sort
3680
3681// stable, 2-3 compares, 0-2 swaps
3682
3683template <class _Compare, class _ForwardIterator>
3684unsigned
3685__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3686{
3687 unsigned __r = 0;
3688 if (!__c(*__y, *__x)) // if x <= y
3689 {
3690 if (!__c(*__z, *__y)) // if y <= z
3691 return __r; // x <= y && y <= z
3692 // x <= y && y > z
3693 swap(*__y, *__z); // x <= z && y < z
3694 __r = 1;
3695 if (__c(*__y, *__x)) // if x > y
3696 {
3697 swap(*__x, *__y); // x < y && y <= z
3698 __r = 2;
3699 }
3700 return __r; // x <= y && y < z
3701 }
3702 if (__c(*__z, *__y)) // x > y, if y > z
3703 {
3704 swap(*__x, *__z); // x < y && y < z
3705 __r = 1;
3706 return __r;
3707 }
3708 swap(*__x, *__y); // x > y && y <= z
3709 __r = 1; // x < y && x <= z
3710 if (__c(*__z, *__y)) // if y > z
3711 {
3712 swap(*__y, *__z); // x <= y && y < z
3713 __r = 2;
3714 }
3715 return __r;
3716} // x <= y && y <= z
3717
3718// stable, 3-6 compares, 0-5 swaps
3719
3720template <class _Compare, class _ForwardIterator>
3721unsigned
3722__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3723 _ForwardIterator __x4, _Compare __c)
3724{
3725 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3726 if (__c(*__x4, *__x3))
3727 {
3728 swap(*__x3, *__x4);
3729 ++__r;
3730 if (__c(*__x3, *__x2))
3731 {
3732 swap(*__x2, *__x3);
3733 ++__r;
3734 if (__c(*__x2, *__x1))
3735 {
3736 swap(*__x1, *__x2);
3737 ++__r;
3738 }
3739 }
3740 }
3741 return __r;
3742}
3743
3744// stable, 4-10 compares, 0-9 swaps
3745
3746template <class _Compare, class _ForwardIterator>
3747unsigned
3748__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3749 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3750{
3751 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3752 if (__c(*__x5, *__x4))
3753 {
3754 swap(*__x4, *__x5);
3755 ++__r;
3756 if (__c(*__x4, *__x3))
3757 {
3758 swap(*__x3, *__x4);
3759 ++__r;
3760 if (__c(*__x3, *__x2))
3761 {
3762 swap(*__x2, *__x3);
3763 ++__r;
3764 if (__c(*__x2, *__x1))
3765 {
3766 swap(*__x1, *__x2);
3767 ++__r;
3768 }
3769 }
3770 }
3771 }
3772 return __r;
3773}
3774
3775// Assumes size > 0
3776template <class _Compare, class _BirdirectionalIterator>
3777void
3778__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3779{
3780 _BirdirectionalIterator __lm1 = __last;
3781 for (--__lm1; __first != __lm1; ++__first)
3782 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003783 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003784 typename add_lvalue_reference<_Compare>::type>
3785 (__first, __last, __comp);
3786 if (__i != __first)
3787 swap(*__first, *__i);
3788 }
3789}
3790
3791template <class _Compare, class _BirdirectionalIterator>
3792void
3793__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3794{
3795 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3796 if (__first != __last)
3797 {
3798 _BirdirectionalIterator __i = __first;
3799 for (++__i; __i != __last; ++__i)
3800 {
3801 _BirdirectionalIterator __j = __i;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003802 value_type __t(_VSTD::move(*__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003803 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003804 *__j = _VSTD::move(*__k);
3805 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003806 }
3807 }
3808}
3809
3810template <class _Compare, class _RandomAccessIterator>
3811void
3812__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3813{
3814 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3815 _RandomAccessIterator __j = __first+2;
3816 __sort3<_Compare>(__first, __first+1, __j, __comp);
3817 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3818 {
3819 if (__comp(*__i, *__j))
3820 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003821 value_type __t(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003822 _RandomAccessIterator __k = __j;
3823 __j = __i;
3824 do
3825 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003826 *__j = _VSTD::move(*__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003827 __j = __k;
3828 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003829 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003830 }
3831 __j = __i;
3832 }
3833}
3834
3835template <class _Compare, class _RandomAccessIterator>
3836bool
3837__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3838{
3839 switch (__last - __first)
3840 {
3841 case 0:
3842 case 1:
3843 return true;
3844 case 2:
3845 if (__comp(*--__last, *__first))
3846 swap(*__first, *__last);
3847 return true;
3848 case 3:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003849 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003850 return true;
3851 case 4:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003852 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003853 return true;
3854 case 5:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003855 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003856 return true;
3857 }
3858 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3859 _RandomAccessIterator __j = __first+2;
3860 __sort3<_Compare>(__first, __first+1, __j, __comp);
3861 const unsigned __limit = 8;
3862 unsigned __count = 0;
3863 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3864 {
3865 if (__comp(*__i, *__j))
3866 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003867 value_type __t(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003868 _RandomAccessIterator __k = __j;
3869 __j = __i;
3870 do
3871 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003872 *__j = _VSTD::move(*__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003873 __j = __k;
3874 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003875 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003876 if (++__count == __limit)
3877 return ++__i == __last;
3878 }
3879 __j = __i;
3880 }
3881 return true;
3882}
3883
3884template <class _Compare, class _BirdirectionalIterator>
3885void
3886__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3887 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3888{
3889 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3890 if (__first1 != __last1)
3891 {
3892 __destruct_n __d(0);
3893 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3894 value_type* __last2 = __first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003895 ::new(__last2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003896 __d.__incr((value_type*)0);
3897 for (++__last2; ++__first1 != __last1; ++__last2)
3898 {
3899 value_type* __j2 = __last2;
3900 value_type* __i2 = __j2;
3901 if (__comp(*__first1, *--__i2))
3902 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003903 ::new(__j2) value_type(_VSTD::move(*__i2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003904 __d.__incr((value_type*)0);
3905 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003906 *__j2 = _VSTD::move(*__i2);
3907 *__j2 = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003908 }
3909 else
3910 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003911 ::new(__j2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003912 __d.__incr((value_type*)0);
3913 }
3914 }
3915 __h.release();
3916 }
3917}
3918
3919template <class _Compare, class _RandomAccessIterator>
3920void
3921__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3922{
3923 // _Compare is known to be a reference type
3924 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3925 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant1468b662010-11-19 22:17:28 +00003926 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3927 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003928 while (true)
3929 {
3930 __restart:
3931 difference_type __len = __last - __first;
3932 switch (__len)
3933 {
3934 case 0:
3935 case 1:
3936 return;
3937 case 2:
3938 if (__comp(*--__last, *__first))
3939 swap(*__first, *__last);
3940 return;
3941 case 3:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003942 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003943 return;
3944 case 4:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003945 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003946 return;
3947 case 5:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003948 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003949 return;
3950 }
3951 if (__len <= __limit)
3952 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003953 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003954 return;
3955 }
3956 // __len > 5
3957 _RandomAccessIterator __m = __first;
3958 _RandomAccessIterator __lm1 = __last;
3959 --__lm1;
3960 unsigned __n_swaps;
3961 {
3962 difference_type __delta;
3963 if (__len >= 1000)
3964 {
3965 __delta = __len/2;
3966 __m += __delta;
3967 __delta /= 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003968 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003969 }
3970 else
3971 {
3972 __delta = __len/2;
3973 __m += __delta;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003974 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003975 }
3976 }
3977 // *__m is median
3978 // partition [__first, __m) < *__m and *__m <= [__m, __last)
3979 // (this inhibits tossing elements equivalent to __m around unnecessarily)
3980 _RandomAccessIterator __i = __first;
3981 _RandomAccessIterator __j = __lm1;
3982 // j points beyond range to be tested, *__m is known to be <= *__lm1
3983 // The search going up is known to be guarded but the search coming down isn't.
3984 // Prime the downward search with a guard.
3985 if (!__comp(*__i, *__m)) // if *__first == *__m
3986 {
3987 // *__first == *__m, *__first doesn't go in first part
3988 // manually guard downward moving __j against __i
3989 while (true)
3990 {
3991 if (__i == --__j)
3992 {
3993 // *__first == *__m, *__m <= all other elements
3994 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
3995 ++__i; // __first + 1
3996 __j = __last;
3997 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
3998 {
3999 while (true)
4000 {
4001 if (__i == __j)
4002 return; // [__first, __last) all equivalent elements
4003 if (__comp(*__first, *__i))
4004 {
4005 swap(*__i, *__j);
4006 ++__n_swaps;
4007 ++__i;
4008 break;
4009 }
4010 ++__i;
4011 }
4012 }
4013 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
4014 if (__i == __j)
4015 return;
4016 while (true)
4017 {
4018 while (!__comp(*__first, *__i))
4019 ++__i;
4020 while (__comp(*__first, *--__j))
4021 ;
4022 if (__i >= __j)
4023 break;
4024 swap(*__i, *__j);
4025 ++__n_swaps;
4026 ++__i;
4027 }
4028 // [__first, __i) == *__first and *__first < [__i, __last)
4029 // The first part is sorted, sort the secod part
Howard Hinnant0949eed2011-06-30 21:18:19 +00004030 // _VSTD::__sort<_Compare>(__i, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004031 __first = __i;
4032 goto __restart;
4033 }
4034 if (__comp(*__j, *__m))
4035 {
4036 swap(*__i, *__j);
4037 ++__n_swaps;
4038 break; // found guard for downward moving __j, now use unguarded partition
4039 }
4040 }
4041 }
4042 // It is known that *__i < *__m
4043 ++__i;
4044 // j points beyond range to be tested, *__m is known to be <= *__lm1
4045 // if not yet partitioned...
4046 if (__i < __j)
4047 {
4048 // known that *(__i - 1) < *__m
4049 // known that __i <= __m
4050 while (true)
4051 {
4052 // __m still guards upward moving __i
4053 while (__comp(*__i, *__m))
4054 ++__i;
4055 // It is now known that a guard exists for downward moving __j
4056 while (!__comp(*--__j, *__m))
4057 ;
4058 if (__i > __j)
4059 break;
4060 swap(*__i, *__j);
4061 ++__n_swaps;
4062 // It is known that __m != __j
4063 // If __m just moved, follow it
4064 if (__m == __i)
4065 __m = __j;
4066 ++__i;
4067 }
4068 }
4069 // [__first, __i) < *__m and *__m <= [__i, __last)
4070 if (__i != __m && __comp(*__m, *__i))
4071 {
4072 swap(*__i, *__m);
4073 ++__n_swaps;
4074 }
4075 // [__first, __i) < *__i and *__i <= [__i+1, __last)
4076 // If we were given a perfect partition, see if insertion sort is quick...
4077 if (__n_swaps == 0)
4078 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004079 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
4080 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004081 {
4082 if (__fs)
4083 return;
4084 __last = __i;
4085 continue;
4086 }
4087 else
4088 {
4089 if (__fs)
4090 {
4091 __first = ++__i;
4092 continue;
4093 }
4094 }
4095 }
4096 // sort smaller range with recursive call and larger with tail recursion elimination
4097 if (__i - __first < __last - __i)
4098 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004099 _VSTD::__sort<_Compare>(__first, __i, __comp);
4100 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004101 __first = ++__i;
4102 }
4103 else
4104 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004105 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4106 // _VSTD::__sort<_Compare>(__first, __i, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004107 __last = __i;
4108 }
4109 }
4110}
4111
4112// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
4113template <class _RandomAccessIterator, class _Compare>
4114inline _LIBCPP_INLINE_VISIBILITY
4115void
4116sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4117{
Howard Hinnant5e571422013-08-23 20:10:18 +00004118#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004119 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4120 __debug_less<_Compare> __c(__comp);
4121 __sort<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004122#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004123 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4124 __sort<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004125#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004126}
4127
4128template <class _RandomAccessIterator>
4129inline _LIBCPP_INLINE_VISIBILITY
4130void
4131sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4132{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004133 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004134}
4135
4136template <class _Tp>
4137inline _LIBCPP_INLINE_VISIBILITY
4138void
4139sort(_Tp** __first, _Tp** __last)
4140{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004141 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004142}
4143
4144template <class _Tp>
4145inline _LIBCPP_INLINE_VISIBILITY
4146void
4147sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4148{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004149 _VSTD::sort(__first.base(), __last.base());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004150}
4151
Howard Hinnant7a563db2011-09-14 18:33:51 +00004152template <class _Tp, class _Compare>
4153inline _LIBCPP_INLINE_VISIBILITY
4154void
4155sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4156{
4157 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4158 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4159}
4160
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004161_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4162_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4163_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4164_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4165_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4166_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4167_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4168_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4169_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4170_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4171_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4172_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&))
4173_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4174_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4175_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004176
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004177_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4178_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4179_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4180_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4181_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4182_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4183_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4184_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4185_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4186_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4187_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4188_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&))
4189_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4190_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4191_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004192
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004193_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004194
4195// lower_bound
4196
4197template <class _Compare, class _ForwardIterator, class _Tp>
4198_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004199__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004200{
4201 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004202 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004203 while (__len != 0)
4204 {
4205 difference_type __l2 = __len / 2;
4206 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004207 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004208 if (__comp(*__m, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004209 {
4210 __first = ++__m;
4211 __len -= __l2 + 1;
4212 }
4213 else
4214 __len = __l2;
4215 }
4216 return __first;
4217}
4218
4219template <class _ForwardIterator, class _Tp, class _Compare>
4220inline _LIBCPP_INLINE_VISIBILITY
4221_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004222lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004223{
Howard Hinnant5e571422013-08-23 20:10:18 +00004224#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004225 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4226 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004227 return __lower_bound<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004228#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004229 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004230 return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004231#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004232}
4233
4234template <class _ForwardIterator, class _Tp>
4235inline _LIBCPP_INLINE_VISIBILITY
4236_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004237lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004238{
Howard Hinnant78b68282011-10-22 20:59:45 +00004239 return _VSTD::lower_bound(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004240 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4241}
4242
4243// upper_bound
4244
4245template <class _Compare, class _ForwardIterator, class _Tp>
4246_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004247__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004248{
4249 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004250 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004251 while (__len != 0)
4252 {
4253 difference_type __l2 = __len / 2;
4254 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004255 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004256 if (__comp(__value_, *__m))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004257 __len = __l2;
4258 else
4259 {
4260 __first = ++__m;
4261 __len -= __l2 + 1;
4262 }
4263 }
4264 return __first;
4265}
4266
4267template <class _ForwardIterator, class _Tp, class _Compare>
4268inline _LIBCPP_INLINE_VISIBILITY
4269_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004270upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004271{
Howard Hinnant5e571422013-08-23 20:10:18 +00004272#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004273 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4274 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004275 return __upper_bound<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004276#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004277 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004278 return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004279#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004280}
4281
4282template <class _ForwardIterator, class _Tp>
4283inline _LIBCPP_INLINE_VISIBILITY
4284_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004285upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004286{
Howard Hinnant78b68282011-10-22 20:59:45 +00004287 return _VSTD::upper_bound(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004288 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4289}
4290
4291// equal_range
4292
4293template <class _Compare, class _ForwardIterator, class _Tp>
4294pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004295__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004296{
4297 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004298 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004299 while (__len != 0)
4300 {
4301 difference_type __l2 = __len / 2;
4302 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004303 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004304 if (__comp(*__m, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004305 {
4306 __first = ++__m;
4307 __len -= __l2 + 1;
4308 }
Howard Hinnant78b68282011-10-22 20:59:45 +00004309 else if (__comp(__value_, *__m))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004310 {
4311 __last = __m;
4312 __len = __l2;
4313 }
4314 else
4315 {
4316 _ForwardIterator __mp1 = __m;
4317 return pair<_ForwardIterator, _ForwardIterator>
4318 (
Howard Hinnant78b68282011-10-22 20:59:45 +00004319 __lower_bound<_Compare>(__first, __m, __value_, __comp),
4320 __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004321 );
4322 }
4323 }
4324 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4325}
4326
4327template <class _ForwardIterator, class _Tp, class _Compare>
4328inline _LIBCPP_INLINE_VISIBILITY
4329pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004330equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004331{
Howard Hinnant5e571422013-08-23 20:10:18 +00004332#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004333 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4334 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004335 return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004336#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004337 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004338 return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004339#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004340}
4341
4342template <class _ForwardIterator, class _Tp>
4343inline _LIBCPP_INLINE_VISIBILITY
4344pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004345equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004346{
Howard Hinnant78b68282011-10-22 20:59:45 +00004347 return _VSTD::equal_range(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004348 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4349}
4350
4351// binary_search
4352
4353template <class _Compare, class _ForwardIterator, class _Tp>
4354inline _LIBCPP_INLINE_VISIBILITY
4355bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004356__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004357{
Howard Hinnant78b68282011-10-22 20:59:45 +00004358 __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4359 return __first != __last && !__comp(__value_, *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004360}
4361
4362template <class _ForwardIterator, class _Tp, class _Compare>
4363inline _LIBCPP_INLINE_VISIBILITY
4364bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004365binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004366{
Howard Hinnant5e571422013-08-23 20:10:18 +00004367#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004368 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4369 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004370 return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004371#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004372 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004373 return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004374#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004375}
4376
4377template <class _ForwardIterator, class _Tp>
4378inline _LIBCPP_INLINE_VISIBILITY
4379bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004380binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004381{
Howard Hinnant78b68282011-10-22 20:59:45 +00004382 return _VSTD::binary_search(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004383 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4384}
4385
4386// merge
4387
4388template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4389_OutputIterator
4390__merge(_InputIterator1 __first1, _InputIterator1 __last1,
4391 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4392{
4393 for (; __first1 != __last1; ++__result)
4394 {
4395 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004396 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004397 if (__comp(*__first2, *__first1))
4398 {
4399 *__result = *__first2;
4400 ++__first2;
4401 }
4402 else
4403 {
4404 *__result = *__first1;
4405 ++__first1;
4406 }
4407 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00004408 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004409}
4410
4411template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4412inline _LIBCPP_INLINE_VISIBILITY
4413_OutputIterator
4414merge(_InputIterator1 __first1, _InputIterator1 __last1,
4415 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4416{
Howard Hinnant5e571422013-08-23 20:10:18 +00004417#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004418 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4419 __debug_less<_Compare> __c(__comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004420 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004421#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004422 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004423 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004424#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004425}
4426
4427template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4428inline _LIBCPP_INLINE_VISIBILITY
4429_OutputIterator
4430merge(_InputIterator1 __first1, _InputIterator1 __last1,
4431 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4432{
4433 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4434 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4435 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4436}
4437
4438// inplace_merge
4439
Marshall Clowa3795762015-07-29 16:25:45 +00004440template <class _Compare, class _InputIterator1, class _InputIterator2,
4441 class _OutputIterator>
4442void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4443 _InputIterator2 __first2, _InputIterator2 __last2,
4444 _OutputIterator __result, _Compare __comp)
4445{
4446 for (; __first1 != __last1; ++__result)
4447 {
4448 if (__first2 == __last2)
4449 {
4450 _VSTD::move(__first1, __last1, __result);
4451 return;
4452 }
4453
4454 if (__comp(*__first2, *__first1))
4455 {
4456 *__result = _VSTD::move(*__first2);
4457 ++__first2;
4458 }
4459 else
4460 {
4461 *__result = _VSTD::move(*__first1);
4462 ++__first1;
4463 }
4464 }
4465 // __first2 through __last2 are already in the right spot.
4466}
4467
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004468template <class _Compare, class _BidirectionalIterator>
4469void
4470__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4471 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4472 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4473 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4474{
4475 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004476 __destruct_n __d(0);
4477 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4478 if (__len1 <= __len2)
4479 {
4480 value_type* __p = __buff;
Eric Fiselierb9919752014-10-27 19:28:20 +00004481 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004482 ::new(__p) value_type(_VSTD::move(*__i));
Marshall Clowa3795762015-07-29 16:25:45 +00004483 __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004484 }
4485 else
4486 {
4487 value_type* __p = __buff;
Eric Fiselierb9919752014-10-27 19:28:20 +00004488 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004489 ::new(__p) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004490 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4491 typedef reverse_iterator<value_type*> _Rv;
Aditya Kumarfdb4f172016-08-25 11:52:38 +00004492 __half_inplace_merge(_Rv(__p), _Rv(__buff),
Marshall Clowa3795762015-07-29 16:25:45 +00004493 _RBi(__middle), _RBi(__first),
Marshall Clow25a78dc2017-08-28 23:16:13 +00004494 _RBi(__last), __invert<_Compare>(__comp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004495 }
4496}
4497
4498template <class _Compare, class _BidirectionalIterator>
4499void
4500__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4501 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4502 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4503 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4504{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004505 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4506 while (true)
4507 {
4508 // if __middle == __last, we're done
4509 if (__len2 == 0)
4510 return;
Marshall Clowe809f4c2015-02-02 16:44:11 +00004511 if (__len1 <= __buff_size || __len2 <= __buff_size)
4512 return __buffered_inplace_merge<_Compare>
4513 (__first, __middle, __last, __comp, __len1, __len2, __buff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004514 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
Eric Fiselierb9919752014-10-27 19:28:20 +00004515 for (; true; ++__first, (void) --__len1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004516 {
4517 if (__len1 == 0)
4518 return;
4519 if (__comp(*__middle, *__first))
4520 break;
4521 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004522 // __first < __middle < __last
4523 // *__first > *__middle
4524 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4525 // all elements in:
4526 // [__first, __m1) <= [__middle, __m2)
4527 // [__middle, __m2) < [__m1, __middle)
4528 // [__m1, __middle) <= [__m2, __last)
4529 // and __m1 or __m2 is in the middle of its range
4530 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4531 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4532 difference_type __len11; // distance(__first, __m1)
4533 difference_type __len21; // distance(__middle, __m2)
4534 // binary search smaller range
4535 if (__len1 < __len2)
4536 { // __len >= 1, __len2 >= 2
4537 __len21 = __len2 / 2;
4538 __m2 = __middle;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004539 _VSTD::advance(__m2, __len21);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004540 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004541 __len11 = _VSTD::distance(__first, __m1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004542 }
4543 else
4544 {
4545 if (__len1 == 1)
4546 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4547 // It is known *__first > *__middle
4548 swap(*__first, *__middle);
4549 return;
4550 }
4551 // __len1 >= 2, __len2 >= 1
4552 __len11 = __len1 / 2;
4553 __m1 = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004554 _VSTD::advance(__m1, __len11);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004555 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004556 __len21 = _VSTD::distance(__middle, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004557 }
4558 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4559 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4560 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4561 // swap middle two partitions
Howard Hinnant0949eed2011-06-30 21:18:19 +00004562 __middle = _VSTD::rotate(__m1, __middle, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004563 // __len12 and __len21 now have swapped meanings
4564 // merge smaller range with recurisve call and larger with tail recursion elimination
4565 if (__len11 + __len21 < __len12 + __len22)
4566 {
4567 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4568// __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4569 __first = __middle;
4570 __middle = __m2;
4571 __len1 = __len12;
4572 __len2 = __len22;
4573 }
4574 else
4575 {
4576 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4577// __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4578 __last = __middle;
4579 __middle = __m1;
4580 __len1 = __len11;
4581 __len2 = __len21;
4582 }
4583 }
4584}
4585
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004586template <class _BidirectionalIterator, class _Compare>
4587inline _LIBCPP_INLINE_VISIBILITY
4588void
4589inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4590 _Compare __comp)
4591{
4592 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4593 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004594 difference_type __len1 = _VSTD::distance(__first, __middle);
4595 difference_type __len2 = _VSTD::distance(__middle, __last);
4596 difference_type __buf_size = _VSTD::min(__len1, __len2);
Marshall Clow4c2684c2015-02-02 17:35:53 +00004597 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4598 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
4599
Howard Hinnant5e571422013-08-23 20:10:18 +00004600#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004601 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4602 __debug_less<_Compare> __c(__comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004603 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004604 __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004605#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004606 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004607 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004608 __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004609#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004610}
4611
4612template <class _BidirectionalIterator>
4613inline _LIBCPP_INLINE_VISIBILITY
4614void
4615inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4616{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004617 _VSTD::inplace_merge(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004618 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4619}
4620
4621// stable_sort
4622
4623template <class _Compare, class _InputIterator1, class _InputIterator2>
4624void
4625__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4626 _InputIterator2 __first2, _InputIterator2 __last2,
4627 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4628{
4629 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4630 __destruct_n __d(0);
4631 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4632 for (; true; ++__result)
4633 {
4634 if (__first1 == __last1)
4635 {
4636 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
Howard Hinnant0949eed2011-06-30 21:18:19 +00004637 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004638 __h.release();
4639 return;
4640 }
4641 if (__first2 == __last2)
4642 {
4643 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
Howard Hinnant0949eed2011-06-30 21:18:19 +00004644 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004645 __h.release();
4646 return;
4647 }
4648 if (__comp(*__first2, *__first1))
4649 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004650 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004651 __d.__incr((value_type*)0);
4652 ++__first2;
4653 }
4654 else
4655 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004656 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004657 __d.__incr((value_type*)0);
4658 ++__first1;
4659 }
4660 }
4661}
4662
4663template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4664void
4665__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4666 _InputIterator2 __first2, _InputIterator2 __last2,
4667 _OutputIterator __result, _Compare __comp)
4668{
4669 for (; __first1 != __last1; ++__result)
4670 {
4671 if (__first2 == __last2)
4672 {
4673 for (; __first1 != __last1; ++__first1, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004674 *__result = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004675 return;
4676 }
4677 if (__comp(*__first2, *__first1))
4678 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004679 *__result = _VSTD::move(*__first2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004680 ++__first2;
4681 }
4682 else
4683 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004684 *__result = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004685 ++__first1;
4686 }
4687 }
4688 for (; __first2 != __last2; ++__first2, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004689 *__result = _VSTD::move(*__first2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004690}
4691
4692template <class _Compare, class _RandomAccessIterator>
4693void
4694__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4695 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4696 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4697
4698template <class _Compare, class _RandomAccessIterator>
4699void
4700__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4701 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4702 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4703{
4704 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4705 switch (__len)
4706 {
4707 case 0:
4708 return;
4709 case 1:
Howard Hinnant0949eed2011-06-30 21:18:19 +00004710 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004711 return;
4712 case 2:
4713 __destruct_n __d(0);
4714 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4715 if (__comp(*--__last1, *__first1))
4716 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004717 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004718 __d.__incr((value_type*)0);
4719 ++__first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004720 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004721 }
4722 else
4723 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004724 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004725 __d.__incr((value_type*)0);
4726 ++__first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004727 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004728 }
4729 __h2.release();
4730 return;
4731 }
4732 if (__len <= 8)
4733 {
4734 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4735 return;
4736 }
4737 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4738 _RandomAccessIterator __m = __first1 + __l2;
4739 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4740 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4741 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4742}
4743
4744template <class _Tp>
4745struct __stable_sort_switch
4746{
Howard Hinnant1468b662010-11-19 22:17:28 +00004747 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004748};
4749
4750template <class _Compare, class _RandomAccessIterator>
4751void
4752__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4753 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4754 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4755{
4756 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4757 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4758 switch (__len)
4759 {
4760 case 0:
4761 case 1:
4762 return;
4763 case 2:
4764 if (__comp(*--__last, *__first))
4765 swap(*__first, *__last);
4766 return;
4767 }
4768 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4769 {
4770 __insertion_sort<_Compare>(__first, __last, __comp);
4771 return;
4772 }
4773 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4774 _RandomAccessIterator __m = __first + __l2;
4775 if (__len <= __buff_size)
4776 {
4777 __destruct_n __d(0);
4778 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4779 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4780 __d.__set(__l2, (value_type*)0);
4781 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4782 __d.__set(__len, (value_type*)0);
4783 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4784// __merge<_Compare>(move_iterator<value_type*>(__buff),
4785// move_iterator<value_type*>(__buff + __l2),
4786// move_iterator<_RandomAccessIterator>(__buff + __l2),
4787// move_iterator<_RandomAccessIterator>(__buff + __len),
4788// __first, __comp);
4789 return;
4790 }
4791 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4792 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4793 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4794}
4795
4796template <class _RandomAccessIterator, class _Compare>
4797inline _LIBCPP_INLINE_VISIBILITY
4798void
4799stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4800{
4801 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4802 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4803 difference_type __len = __last - __first;
4804 pair<value_type*, ptrdiff_t> __buf(0, 0);
4805 unique_ptr<value_type, __return_temporary_buffer> __h;
4806 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4807 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004808 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004809 __h.reset(__buf.first);
4810 }
Howard Hinnant5e571422013-08-23 20:10:18 +00004811#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004812 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4813 __debug_less<_Compare> __c(__comp);
4814 __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004815#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004816 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4817 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004818#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004819}
4820
4821template <class _RandomAccessIterator>
4822inline _LIBCPP_INLINE_VISIBILITY
4823void
4824stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4825{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004826 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004827}
4828
4829// is_heap_until
4830
4831template <class _RandomAccessIterator, class _Compare>
Marshall Clow8d0d8252018-01-15 16:16:32 +00004832_LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004833is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4834{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004835 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004836 difference_type __len = __last - __first;
4837 difference_type __p = 0;
4838 difference_type __c = 1;
4839 _RandomAccessIterator __pp = __first;
4840 while (__c < __len)
4841 {
4842 _RandomAccessIterator __cp = __first + __c;
4843 if (__comp(*__pp, *__cp))
4844 return __cp;
4845 ++__c;
4846 ++__cp;
4847 if (__c == __len)
4848 return __last;
4849 if (__comp(*__pp, *__cp))
4850 return __cp;
4851 ++__p;
4852 ++__pp;
4853 __c = 2 * __p + 1;
4854 }
4855 return __last;
4856}
4857
Howard Hinnant324bb032010-08-22 00:02:43 +00004858template<class _RandomAccessIterator>
Marshall Clow8d0d8252018-01-15 16:16:32 +00004859inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004860_RandomAccessIterator
4861is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4862{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004863 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004864}
4865
4866// is_heap
4867
4868template <class _RandomAccessIterator, class _Compare>
Marshall Clow8d0d8252018-01-15 16:16:32 +00004869inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004870bool
4871is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4872{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004873 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004874}
4875
Howard Hinnant324bb032010-08-22 00:02:43 +00004876template<class _RandomAccessIterator>
Marshall Clow8d0d8252018-01-15 16:16:32 +00004877inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004878bool
4879is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4880{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004881 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004882}
4883
4884// push_heap
4885
4886template <class _Compare, class _RandomAccessIterator>
4887void
David Majnemercb8757a2014-07-22 06:07:09 +00004888__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4889 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004890{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004891 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4892 if (__len > 1)
4893 {
4894 __len = (__len - 2) / 2;
4895 _RandomAccessIterator __ptr = __first + __len;
4896 if (__comp(*__ptr, *--__last))
4897 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004898 value_type __t(_VSTD::move(*__last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004899 do
4900 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004901 *__last = _VSTD::move(*__ptr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004902 __last = __ptr;
4903 if (__len == 0)
4904 break;
4905 __len = (__len - 1) / 2;
4906 __ptr = __first + __len;
4907 } while (__comp(*__ptr, __t));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004908 *__last = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004909 }
4910 }
4911}
4912
4913template <class _RandomAccessIterator, class _Compare>
4914inline _LIBCPP_INLINE_VISIBILITY
4915void
4916push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4917{
Howard Hinnant5e571422013-08-23 20:10:18 +00004918#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004919 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4920 __debug_less<_Compare> __c(__comp);
David Majnemercb8757a2014-07-22 06:07:09 +00004921 __sift_up<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004922#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004923 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
David Majnemercb8757a2014-07-22 06:07:09 +00004924 __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004925#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004926}
4927
4928template <class _RandomAccessIterator>
4929inline _LIBCPP_INLINE_VISIBILITY
4930void
4931push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4932{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004933 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004934}
4935
4936// pop_heap
4937
4938template <class _Compare, class _RandomAccessIterator>
David Majnemercb8757a2014-07-22 06:07:09 +00004939void
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00004940__sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
4941 _Compare __comp,
David Majnemercb8757a2014-07-22 06:07:09 +00004942 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4943 _RandomAccessIterator __start)
4944{
4945 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4946 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4947 // left-child of __start is at 2 * __start + 1
4948 // right-child of __start is at 2 * __start + 2
4949 difference_type __child = __start - __first;
4950
4951 if (__len < 2 || (__len - 2) / 2 < __child)
4952 return;
4953
4954 __child = 2 * __child + 1;
4955 _RandomAccessIterator __child_i = __first + __child;
4956
4957 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4958 // right-child exists and is greater than left-child
4959 ++__child_i;
4960 ++__child;
4961 }
4962
4963 // check if we are in heap-order
4964 if (__comp(*__child_i, *__start))
4965 // we are, __start is larger than it's largest child
4966 return;
4967
4968 value_type __top(_VSTD::move(*__start));
4969 do
4970 {
4971 // we are not in heap-order, swap the parent with it's largest child
4972 *__start = _VSTD::move(*__child_i);
4973 __start = __child_i;
4974
4975 if ((__len - 2) / 2 < __child)
4976 break;
4977
4978 // recompute the child based off of the updated parent
4979 __child = 2 * __child + 1;
4980 __child_i = __first + __child;
4981
4982 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4983 // right-child exists and is greater than left-child
4984 ++__child_i;
4985 ++__child;
4986 }
4987
4988 // check if we are in heap-order
4989 } while (!__comp(*__child_i, __top));
4990 *__start = _VSTD::move(__top);
4991}
4992
4993template <class _Compare, class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004994inline _LIBCPP_INLINE_VISIBILITY
4995void
4996__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4997 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4998{
4999 if (__len > 1)
5000 {
5001 swap(*__first, *--__last);
David Majnemercb8757a2014-07-22 06:07:09 +00005002 __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005003 }
5004}
5005
5006template <class _RandomAccessIterator, class _Compare>
5007inline _LIBCPP_INLINE_VISIBILITY
5008void
5009pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5010{
Howard Hinnant5e571422013-08-23 20:10:18 +00005011#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005012 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5013 __debug_less<_Compare> __c(__comp);
5014 __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00005015#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005016 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5017 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00005018#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005019}
5020
5021template <class _RandomAccessIterator>
5022inline _LIBCPP_INLINE_VISIBILITY
5023void
5024pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5025{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005026 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005027}
5028
5029// make_heap
5030
5031template <class _Compare, class _RandomAccessIterator>
5032void
5033__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5034{
5035 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5036 difference_type __n = __last - __first;
5037 if (__n > 1)
5038 {
David Majnemercb8757a2014-07-22 06:07:09 +00005039 // start from the first parent, there is no need to consider children
5040 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
5041 {
5042 __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
5043 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005044 }
5045}
5046
5047template <class _RandomAccessIterator, class _Compare>
5048inline _LIBCPP_INLINE_VISIBILITY
5049void
5050make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5051{
Howard Hinnant5e571422013-08-23 20:10:18 +00005052#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005053 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5054 __debug_less<_Compare> __c(__comp);
5055 __make_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005056#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005057 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5058 __make_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005059#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005060}
5061
5062template <class _RandomAccessIterator>
5063inline _LIBCPP_INLINE_VISIBILITY
5064void
5065make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5066{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005067 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005068}
5069
5070// sort_heap
5071
5072template <class _Compare, class _RandomAccessIterator>
5073void
5074__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5075{
5076 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5077 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
5078 __pop_heap<_Compare>(__first, __last, __comp, __n);
5079}
5080
5081template <class _RandomAccessIterator, class _Compare>
5082inline _LIBCPP_INLINE_VISIBILITY
5083void
5084sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5085{
Howard Hinnant5e571422013-08-23 20:10:18 +00005086#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005087 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5088 __debug_less<_Compare> __c(__comp);
5089 __sort_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005090#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005091 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5092 __sort_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005093#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005094}
5095
5096template <class _RandomAccessIterator>
5097inline _LIBCPP_INLINE_VISIBILITY
5098void
5099sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5100{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005101 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005102}
5103
5104// partial_sort
5105
5106template <class _Compare, class _RandomAccessIterator>
5107void
5108__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5109 _Compare __comp)
5110{
5111 __make_heap<_Compare>(__first, __middle, __comp);
5112 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5113 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5114 {
5115 if (__comp(*__i, *__first))
5116 {
5117 swap(*__i, *__first);
David Majnemercb8757a2014-07-22 06:07:09 +00005118 __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005119 }
5120 }
5121 __sort_heap<_Compare>(__first, __middle, __comp);
5122}
5123
5124template <class _RandomAccessIterator, class _Compare>
5125inline _LIBCPP_INLINE_VISIBILITY
5126void
5127partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5128 _Compare __comp)
5129{
Howard Hinnant5e571422013-08-23 20:10:18 +00005130#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005131 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5132 __debug_less<_Compare> __c(__comp);
5133 __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005134#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005135 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5136 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005137#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005138}
5139
5140template <class _RandomAccessIterator>
5141inline _LIBCPP_INLINE_VISIBILITY
5142void
5143partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5144{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005145 _VSTD::partial_sort(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005146 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5147}
5148
5149// partial_sort_copy
5150
5151template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5152_RandomAccessIterator
5153__partial_sort_copy(_InputIterator __first, _InputIterator __last,
5154 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5155{
5156 _RandomAccessIterator __r = __result_first;
5157 if (__r != __result_last)
5158 {
Eric Fiselierb9919752014-10-27 19:28:20 +00005159 for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005160 *__r = *__first;
5161 __make_heap<_Compare>(__result_first, __r, __comp);
David Majnemercb8757a2014-07-22 06:07:09 +00005162 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005163 for (; __first != __last; ++__first)
5164 if (__comp(*__first, *__result_first))
5165 {
5166 *__result_first = *__first;
David Majnemercb8757a2014-07-22 06:07:09 +00005167 __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005168 }
5169 __sort_heap<_Compare>(__result_first, __r, __comp);
5170 }
5171 return __r;
5172}
5173
5174template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5175inline _LIBCPP_INLINE_VISIBILITY
5176_RandomAccessIterator
5177partial_sort_copy(_InputIterator __first, _InputIterator __last,
5178 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5179{
Howard Hinnant5e571422013-08-23 20:10:18 +00005180#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005181 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5182 __debug_less<_Compare> __c(__comp);
5183 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005184#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005185 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5186 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005187#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005188}
5189
5190template <class _InputIterator, class _RandomAccessIterator>
5191inline _LIBCPP_INLINE_VISIBILITY
5192_RandomAccessIterator
5193partial_sort_copy(_InputIterator __first, _InputIterator __last,
5194 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5195{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005196 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005197 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5198}
5199
5200// nth_element
5201
5202template <class _Compare, class _RandomAccessIterator>
5203void
5204__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5205{
5206 // _Compare is known to be a reference type
5207 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5208 const difference_type __limit = 7;
5209 while (true)
5210 {
5211 __restart:
Howard Hinnant8292d742011-12-29 17:45:35 +00005212 if (__nth == __last)
5213 return;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005214 difference_type __len = __last - __first;
5215 switch (__len)
5216 {
5217 case 0:
5218 case 1:
5219 return;
5220 case 2:
5221 if (__comp(*--__last, *__first))
5222 swap(*__first, *__last);
5223 return;
5224 case 3:
5225 {
5226 _RandomAccessIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00005227 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005228 return;
5229 }
5230 }
5231 if (__len <= __limit)
5232 {
5233 __selection_sort<_Compare>(__first, __last, __comp);
5234 return;
5235 }
5236 // __len > __limit >= 3
5237 _RandomAccessIterator __m = __first + __len/2;
5238 _RandomAccessIterator __lm1 = __last;
Howard Hinnant0949eed2011-06-30 21:18:19 +00005239 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005240 // *__m is median
5241 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5242 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5243 _RandomAccessIterator __i = __first;
5244 _RandomAccessIterator __j = __lm1;
5245 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5246 // The search going up is known to be guarded but the search coming down isn't.
5247 // Prime the downward search with a guard.
5248 if (!__comp(*__i, *__m)) // if *__first == *__m
5249 {
5250 // *__first == *__m, *__first doesn't go in first part
5251 // manually guard downward moving __j against __i
5252 while (true)
5253 {
5254 if (__i == --__j)
5255 {
5256 // *__first == *__m, *__m <= all other elements
5257 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5258 ++__i; // __first + 1
5259 __j = __last;
5260 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5261 {
5262 while (true)
5263 {
5264 if (__i == __j)
5265 return; // [__first, __last) all equivalent elements
5266 if (__comp(*__first, *__i))
5267 {
5268 swap(*__i, *__j);
5269 ++__n_swaps;
5270 ++__i;
5271 break;
5272 }
5273 ++__i;
5274 }
5275 }
5276 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5277 if (__i == __j)
5278 return;
5279 while (true)
5280 {
5281 while (!__comp(*__first, *__i))
5282 ++__i;
5283 while (__comp(*__first, *--__j))
5284 ;
5285 if (__i >= __j)
5286 break;
5287 swap(*__i, *__j);
5288 ++__n_swaps;
5289 ++__i;
5290 }
5291 // [__first, __i) == *__first and *__first < [__i, __last)
5292 // The first part is sorted,
5293 if (__nth < __i)
5294 return;
5295 // __nth_element the secod part
5296 // __nth_element<_Compare>(__i, __nth, __last, __comp);
5297 __first = __i;
5298 goto __restart;
5299 }
5300 if (__comp(*__j, *__m))
5301 {
5302 swap(*__i, *__j);
5303 ++__n_swaps;
5304 break; // found guard for downward moving __j, now use unguarded partition
5305 }
5306 }
5307 }
5308 ++__i;
5309 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5310 // if not yet partitioned...
5311 if (__i < __j)
5312 {
5313 // known that *(__i - 1) < *__m
5314 while (true)
5315 {
5316 // __m still guards upward moving __i
5317 while (__comp(*__i, *__m))
5318 ++__i;
5319 // It is now known that a guard exists for downward moving __j
5320 while (!__comp(*--__j, *__m))
5321 ;
5322 if (__i >= __j)
5323 break;
5324 swap(*__i, *__j);
5325 ++__n_swaps;
5326 // It is known that __m != __j
5327 // If __m just moved, follow it
5328 if (__m == __i)
5329 __m = __j;
5330 ++__i;
5331 }
5332 }
5333 // [__first, __i) < *__m and *__m <= [__i, __last)
5334 if (__i != __m && __comp(*__m, *__i))
5335 {
5336 swap(*__i, *__m);
5337 ++__n_swaps;
5338 }
5339 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5340 if (__nth == __i)
5341 return;
5342 if (__n_swaps == 0)
5343 {
5344 // We were given a perfectly partitioned sequence. Coincidence?
5345 if (__nth < __i)
5346 {
5347 // Check for [__first, __i) already sorted
5348 __j = __m = __first;
5349 while (++__j != __i)
5350 {
5351 if (__comp(*__j, *__m))
5352 // not yet sorted, so sort
5353 goto not_sorted;
5354 __m = __j;
5355 }
5356 // [__first, __i) sorted
5357 return;
5358 }
5359 else
5360 {
5361 // Check for [__i, __last) already sorted
5362 __j = __m = __i;
5363 while (++__j != __last)
5364 {
5365 if (__comp(*__j, *__m))
5366 // not yet sorted, so sort
5367 goto not_sorted;
5368 __m = __j;
5369 }
5370 // [__i, __last) sorted
5371 return;
5372 }
5373 }
5374not_sorted:
5375 // __nth_element on range containing __nth
5376 if (__nth < __i)
5377 {
5378 // __nth_element<_Compare>(__first, __nth, __i, __comp);
5379 __last = __i;
5380 }
5381 else
5382 {
5383 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5384 __first = ++__i;
5385 }
5386 }
5387}
5388
5389template <class _RandomAccessIterator, class _Compare>
5390inline _LIBCPP_INLINE_VISIBILITY
5391void
5392nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5393{
Howard Hinnant5e571422013-08-23 20:10:18 +00005394#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005395 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5396 __debug_less<_Compare> __c(__comp);
5397 __nth_element<_Comp_ref>(__first, __nth, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005398#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005399 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5400 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005401#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005402}
5403
5404template <class _RandomAccessIterator>
5405inline _LIBCPP_INLINE_VISIBILITY
5406void
5407nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5408{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005409 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005410}
5411
5412// includes
5413
5414template <class _Compare, class _InputIterator1, class _InputIterator2>
5415bool
5416__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5417 _Compare __comp)
5418{
5419 for (; __first2 != __last2; ++__first1)
5420 {
5421 if (__first1 == __last1 || __comp(*__first2, *__first1))
5422 return false;
5423 if (!__comp(*__first1, *__first2))
5424 ++__first2;
5425 }
5426 return true;
5427}
5428
5429template <class _InputIterator1, class _InputIterator2, class _Compare>
5430inline _LIBCPP_INLINE_VISIBILITY
5431bool
5432includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5433 _Compare __comp)
5434{
Howard Hinnant5e571422013-08-23 20:10:18 +00005435#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005436 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5437 __debug_less<_Compare> __c(__comp);
5438 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005439#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005440 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5441 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005442#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005443}
5444
5445template <class _InputIterator1, class _InputIterator2>
5446inline _LIBCPP_INLINE_VISIBILITY
5447bool
5448includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5449{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005450 return _VSTD::includes(__first1, __last1, __first2, __last2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005451 __less<typename iterator_traits<_InputIterator1>::value_type,
5452 typename iterator_traits<_InputIterator2>::value_type>());
5453}
5454
5455// set_union
5456
5457template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5458_OutputIterator
5459__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5460 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5461{
5462 for (; __first1 != __last1; ++__result)
5463 {
5464 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005465 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005466 if (__comp(*__first2, *__first1))
5467 {
5468 *__result = *__first2;
5469 ++__first2;
5470 }
5471 else
5472 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005473 if (!__comp(*__first1, *__first2))
5474 ++__first2;
Marshall Clowc3fa9652017-10-30 15:50:00 +00005475 *__result = *__first1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005476 ++__first1;
5477 }
5478 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00005479 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005480}
5481
5482template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5483inline _LIBCPP_INLINE_VISIBILITY
5484_OutputIterator
5485set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5486 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5487{
Howard Hinnant5e571422013-08-23 20:10:18 +00005488#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005489 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5490 __debug_less<_Compare> __c(__comp);
5491 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005492#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005493 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5494 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005495#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005496}
5497
5498template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5499inline _LIBCPP_INLINE_VISIBILITY
5500_OutputIterator
5501set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5502 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5503{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005504 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005505 __less<typename iterator_traits<_InputIterator1>::value_type,
5506 typename iterator_traits<_InputIterator2>::value_type>());
5507}
5508
5509// set_intersection
5510
5511template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5512_OutputIterator
5513__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5514 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5515{
5516 while (__first1 != __last1 && __first2 != __last2)
5517 {
5518 if (__comp(*__first1, *__first2))
5519 ++__first1;
5520 else
5521 {
5522 if (!__comp(*__first2, *__first1))
5523 {
5524 *__result = *__first1;
5525 ++__result;
5526 ++__first1;
5527 }
5528 ++__first2;
5529 }
5530 }
5531 return __result;
5532}
5533
5534template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5535inline _LIBCPP_INLINE_VISIBILITY
5536_OutputIterator
5537set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5538 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5539{
Howard Hinnant5e571422013-08-23 20:10:18 +00005540#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005541 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5542 __debug_less<_Compare> __c(__comp);
5543 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005544#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005545 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5546 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005547#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005548}
5549
5550template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5551inline _LIBCPP_INLINE_VISIBILITY
5552_OutputIterator
5553set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5554 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5555{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005556 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005557 __less<typename iterator_traits<_InputIterator1>::value_type,
5558 typename iterator_traits<_InputIterator2>::value_type>());
5559}
5560
5561// set_difference
5562
5563template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5564_OutputIterator
5565__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5566 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5567{
5568 while (__first1 != __last1)
5569 {
5570 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005571 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005572 if (__comp(*__first1, *__first2))
5573 {
5574 *__result = *__first1;
5575 ++__result;
5576 ++__first1;
5577 }
5578 else
5579 {
5580 if (!__comp(*__first2, *__first1))
5581 ++__first1;
5582 ++__first2;
5583 }
5584 }
5585 return __result;
5586}
5587
5588template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5589inline _LIBCPP_INLINE_VISIBILITY
5590_OutputIterator
5591set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5592 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5593{
Howard Hinnant5e571422013-08-23 20:10:18 +00005594#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005595 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5596 __debug_less<_Compare> __c(__comp);
5597 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005598#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005599 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5600 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005601#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005602}
5603
5604template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5605inline _LIBCPP_INLINE_VISIBILITY
5606_OutputIterator
5607set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5608 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5609{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005610 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005611 __less<typename iterator_traits<_InputIterator1>::value_type,
5612 typename iterator_traits<_InputIterator2>::value_type>());
5613}
5614
5615// set_symmetric_difference
5616
5617template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5618_OutputIterator
5619__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5620 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5621{
5622 while (__first1 != __last1)
5623 {
5624 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005625 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005626 if (__comp(*__first1, *__first2))
5627 {
5628 *__result = *__first1;
5629 ++__result;
5630 ++__first1;
5631 }
5632 else
5633 {
5634 if (__comp(*__first2, *__first1))
5635 {
5636 *__result = *__first2;
5637 ++__result;
5638 }
5639 else
5640 ++__first1;
5641 ++__first2;
5642 }
5643 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00005644 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005645}
5646
5647template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5648inline _LIBCPP_INLINE_VISIBILITY
5649_OutputIterator
5650set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5651 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5652{
Howard Hinnant5e571422013-08-23 20:10:18 +00005653#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005654 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5655 __debug_less<_Compare> __c(__comp);
5656 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005657#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005658 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5659 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005660#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005661}
5662
5663template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5664inline _LIBCPP_INLINE_VISIBILITY
5665_OutputIterator
5666set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5667 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5668{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005669 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005670 __less<typename iterator_traits<_InputIterator1>::value_type,
5671 typename iterator_traits<_InputIterator2>::value_type>());
5672}
5673
5674// lexicographical_compare
5675
5676template <class _Compare, class _InputIterator1, class _InputIterator2>
5677bool
5678__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5679 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5680{
Eric Fiselierb9919752014-10-27 19:28:20 +00005681 for (; __first2 != __last2; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005682 {
5683 if (__first1 == __last1 || __comp(*__first1, *__first2))
5684 return true;
5685 if (__comp(*__first2, *__first1))
5686 return false;
5687 }
5688 return false;
5689}
5690
5691template <class _InputIterator1, class _InputIterator2, class _Compare>
5692inline _LIBCPP_INLINE_VISIBILITY
5693bool
5694lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5695 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5696{
Howard Hinnant5e571422013-08-23 20:10:18 +00005697#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005698 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5699 __debug_less<_Compare> __c(__comp);
5700 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005701#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005702 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5703 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005704#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005705}
5706
5707template <class _InputIterator1, class _InputIterator2>
5708inline _LIBCPP_INLINE_VISIBILITY
5709bool
5710lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5711 _InputIterator2 __first2, _InputIterator2 __last2)
5712{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005713 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005714 __less<typename iterator_traits<_InputIterator1>::value_type,
5715 typename iterator_traits<_InputIterator2>::value_type>());
5716}
5717
5718// next_permutation
5719
5720template <class _Compare, class _BidirectionalIterator>
5721bool
5722__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5723{
5724 _BidirectionalIterator __i = __last;
5725 if (__first == __last || __first == --__i)
5726 return false;
5727 while (true)
5728 {
5729 _BidirectionalIterator __ip1 = __i;
5730 if (__comp(*--__i, *__ip1))
5731 {
5732 _BidirectionalIterator __j = __last;
5733 while (!__comp(*__i, *--__j))
5734 ;
5735 swap(*__i, *__j);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005736 _VSTD::reverse(__ip1, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005737 return true;
5738 }
5739 if (__i == __first)
5740 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005741 _VSTD::reverse(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005742 return false;
5743 }
5744 }
5745}
5746
5747template <class _BidirectionalIterator, class _Compare>
5748inline _LIBCPP_INLINE_VISIBILITY
5749bool
5750next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5751{
Howard Hinnant5e571422013-08-23 20:10:18 +00005752#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005753 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5754 __debug_less<_Compare> __c(__comp);
5755 return __next_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005756#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005757 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5758 return __next_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005759#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005760}
5761
5762template <class _BidirectionalIterator>
5763inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005764bool
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005765next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5766{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005767 return _VSTD::next_permutation(__first, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005768 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5769}
5770
5771// prev_permutation
5772
5773template <class _Compare, class _BidirectionalIterator>
5774bool
5775__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5776{
5777 _BidirectionalIterator __i = __last;
5778 if (__first == __last || __first == --__i)
5779 return false;
5780 while (true)
5781 {
5782 _BidirectionalIterator __ip1 = __i;
5783 if (__comp(*__ip1, *--__i))
5784 {
5785 _BidirectionalIterator __j = __last;
5786 while (!__comp(*--__j, *__i))
5787 ;
5788 swap(*__i, *__j);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005789 _VSTD::reverse(__ip1, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005790 return true;
5791 }
5792 if (__i == __first)
5793 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005794 _VSTD::reverse(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005795 return false;
5796 }
5797 }
5798}
5799
5800template <class _BidirectionalIterator, class _Compare>
5801inline _LIBCPP_INLINE_VISIBILITY
5802bool
5803prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5804{
Howard Hinnant5e571422013-08-23 20:10:18 +00005805#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005806 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5807 __debug_less<_Compare> __c(__comp);
5808 return __prev_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005809#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005810 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5811 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005812#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005813}
5814
5815template <class _BidirectionalIterator>
5816inline _LIBCPP_INLINE_VISIBILITY
5817bool
5818prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5819{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005820 return _VSTD::prev_permutation(__first, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005821 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5822}
5823
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005824_LIBCPP_END_NAMESPACE_STD
5825
Eric Fiselier018a3d52017-05-31 22:07:49 +00005826_LIBCPP_POP_MACROS
5827
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005828#endif // _LIBCPP_ALGORITHM