blob: 282083a89c2bff480047d78e792fdefba7d9f564 [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>
23 bool
24 all_of(InputIterator first, InputIterator last, Predicate pred);
25
26template <class InputIterator, class Predicate>
27 bool
28 any_of(InputIterator first, InputIterator last, Predicate pred);
29
30template <class InputIterator, class Predicate>
31 bool
32 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>
42 InputIterator
43 find(InputIterator first, InputIterator last, const T& value);
44
45template <class InputIterator, class Predicate>
46 InputIterator
47 find_if(InputIterator first, InputIterator last, Predicate pred);
48
49template<class InputIterator, class Predicate>
50 InputIterator
51 find_if_not(InputIterator first, InputIterator last, Predicate pred);
52
53template <class ForwardIterator1, class ForwardIterator2>
54 ForwardIterator1
55 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
56 ForwardIterator2 first2, ForwardIterator2 last2);
57
58template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
59 ForwardIterator1
60 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
61 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
62
63template <class ForwardIterator1, class ForwardIterator2>
64 ForwardIterator1
65 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
66 ForwardIterator2 first2, ForwardIterator2 last2);
67
68template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
69 ForwardIterator1
70 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
71 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
72
73template <class ForwardIterator>
74 ForwardIterator
75 adjacent_find(ForwardIterator first, ForwardIterator last);
76
77template <class ForwardIterator, class BinaryPredicate>
78 ForwardIterator
79 adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
80
81template <class InputIterator, class T>
82 typename iterator_traits<InputIterator>::difference_type
83 count(InputIterator first, InputIterator last, const T& value);
84
85template <class InputIterator, class Predicate>
86 typename iterator_traits<InputIterator>::difference_type
87 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>
130 bool
131 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
132 ForwardIterator2 first2);
133
Marshall Clowb30abdd2013-05-09 21:14:23 +0000134template<class ForwardIterator1, class ForwardIterator2>
135 bool
136 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>
140 bool
141 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>
145 bool
146 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>
305 bool
306 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>
324 ForwardIterator
325 partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
326
327template <class ForwardIterator>
328 bool
329 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>
336 ForwardIterator
337 is_sorted_until(ForwardIterator first, ForwardIterator last);
338
339template <class ForwardIterator, class Compare>
340 ForwardIterator
341 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>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000516 bool
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>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000520 bool
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>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000524 RandomAccessIterator
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>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000528 RandomAccessIterator
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>
644#include <iterator>
Howard Hinnantca8eb832012-07-26 17:09:09 +0000645#include <cstddef>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000646
Howard Hinnant7f764502013-08-14 18:00:20 +0000647#if defined(__IBMCPP__)
648#include "support/ibm/support.h"
649#endif
Eric Fiselier41af64a2017-05-10 20:57:45 +0000650#if defined(_LIBCPP_COMPILER_MSVC)
Eric Fiselierca91fd02017-05-10 21:30:04 +0000651#include <intrin.h>
Howard Hinnantef5aa932013-09-17 01:34:47 +0000652#endif
Howard Hinnant7f764502013-08-14 18:00:20 +0000653
Eric Fiselierb9536102014-08-10 23:53:08 +0000654#include <__debug>
655
Howard Hinnant08e17472011-10-17 20:05:10 +0000656#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000657#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000658#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000659
Eric Fiselier018a3d52017-05-31 22:07:49 +0000660_LIBCPP_PUSH_MACROS
661#include <__undef_macros>
662
663
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664_LIBCPP_BEGIN_NAMESPACE_STD
665
Marshall Clow9d9463a2014-02-19 16:51:35 +0000666// I'd like to replace these with _VSTD::equal_to<void>, but can't because:
667// * That only works with C++14 and later, and
668// * We haven't included <functional> here.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000669template <class _T1, class _T2 = _T1>
670struct __equal_to
671{
672 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
673 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
674 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
675 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
676};
677
678template <class _T1>
679struct __equal_to<_T1, _T1>
680{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000681 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
682 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000683};
684
685template <class _T1>
686struct __equal_to<const _T1, _T1>
687{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000688 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
689 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000690};
691
692template <class _T1>
693struct __equal_to<_T1, const _T1>
694{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000695 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
696 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000697};
698
699template <class _T1, class _T2 = _T1>
700struct __less
701{
Aditya Kumarfdb4f172016-08-25 11:52:38 +0000702 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow9d9463a2014-02-19 16:51:35 +0000703 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
704
705 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
706 bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
707
708 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
709 bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
710
711 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
712 bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000713};
714
715template <class _T1>
716struct __less<_T1, _T1>
717{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000718 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
719 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000720};
721
722template <class _T1>
723struct __less<const _T1, _T1>
724{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000725 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
726 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000727};
728
729template <class _T1>
730struct __less<_T1, const _T1>
731{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000732 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
733 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000734};
735
736template <class _Predicate>
Marshall Clow25a78dc2017-08-28 23:16:13 +0000737class __invert // invert the sense of a comparison
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000738{
739private:
740 _Predicate __p_;
741public:
Marshall Clow25a78dc2017-08-28 23:16:13 +0000742 _LIBCPP_INLINE_VISIBILITY __invert() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000743
744 _LIBCPP_INLINE_VISIBILITY
Marshall Clow25a78dc2017-08-28 23:16:13 +0000745 explicit __invert(_Predicate __p) : __p_(__p) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000746
747 template <class _T1>
748 _LIBCPP_INLINE_VISIBILITY
749 bool operator()(const _T1& __x) {return !__p_(__x);}
750
751 template <class _T1, class _T2>
752 _LIBCPP_INLINE_VISIBILITY
Marshall Clow25a78dc2017-08-28 23:16:13 +0000753 bool operator()(const _T1& __x, const _T2& __y) {return __p_(__y, __x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000754};
755
Howard Hinnant5e571422013-08-23 20:10:18 +0000756#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000757
758template <class _Compare>
759struct __debug_less
760{
761 _Compare __comp_;
762 __debug_less(_Compare& __c) : __comp_(__c) {}
Eric Fiselier99029f12016-07-19 23:27:18 +0000763
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000764 template <class _Tp, class _Up>
765 bool operator()(const _Tp& __x, const _Up& __y)
766 {
767 bool __r = __comp_(__x, __y);
768 if (__r)
Eric Fiselier99029f12016-07-19 23:27:18 +0000769 __do_compare_assert(0, __y, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000770 return __r;
771 }
Eric Fiselier99029f12016-07-19 23:27:18 +0000772
773 template <class _LHS, class _RHS>
774 inline _LIBCPP_INLINE_VISIBILITY
775 decltype((void)_VSTD::declval<_Compare&>()(
776 _VSTD::declval<_LHS const&>(), _VSTD::declval<_RHS const&>()))
777 __do_compare_assert(int, _LHS const& __l, _RHS const& __r) {
778 _LIBCPP_ASSERT(!__comp_(__l, __r),
779 "Comparator does not induce a strict weak ordering");
780 }
781
782 template <class _LHS, class _RHS>
783 inline _LIBCPP_INLINE_VISIBILITY
784 void __do_compare_assert(long, _LHS const&, _RHS const&) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000785};
786
Howard Hinnant5e571422013-08-23 20:10:18 +0000787#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000788
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000789// Precondition: __x != 0
Howard Hinnantec3773c2011-12-01 20:21:04 +0000790inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000791unsigned __ctz(unsigned __x) {
792#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000793 return static_cast<unsigned>(__builtin_ctz(__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000794#else
795 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
796 static_assert(sizeof(unsigned long) == 4, "");
797 unsigned long where;
798 // Search from LSB to MSB for first set bit.
799 // Returns zero if no set bit is found.
800 if (_BitScanForward(&where, mask))
801 return where;
802 return 32;
803#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +0000804}
805
806inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000807unsigned long __ctz(unsigned long __x) {
808#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000809 return static_cast<unsigned long>(__builtin_ctzl(__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000810#else
811 static_assert(sizeof(unsigned long) == sizeof(unsigned), "");
812 return __ctz(static_cast<unsigned>(__x));
813#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +0000814}
815
816inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000817unsigned long long __ctz(unsigned long long __x) {
818#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000819 return static_cast<unsigned long long>(__builtin_ctzll(__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000820#else
821 unsigned long where;
822// Search from LSB to MSB for first set bit.
823// Returns zero if no set bit is found.
824#if defined(_LIBCPP_HAS_BITSCAN64)
825 (defined(_M_AMD64) || defined(__x86_64__))
826 if (_BitScanForward64(&where, mask))
827 return static_cast<int>(where);
828#else
829 // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
830 // Scan the Low Word.
831 if (_BitScanForward(&where, static_cast<unsigned long>(mask)))
832 return where;
833 // Scan the High Word.
834 if (_BitScanForward(&where, static_cast<unsigned long>(mask >> 32)))
835 return where + 32; // Create a bit offset from the LSB.
836#endif
837 return 64;
838#endif // _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000839}
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000840
841// Precondition: __x != 0
Howard Hinnantec3773c2011-12-01 20:21:04 +0000842inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000843unsigned __clz(unsigned __x) {
844#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000845 return static_cast<unsigned>(__builtin_clz(__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000846#else
847 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
848 static_assert(sizeof(unsigned long) == 4, "");
849 unsigned long where;
850 // Search from LSB to MSB for first set bit.
851 // Returns zero if no set bit is found.
852 if (_BitScanReverse(&where, mask))
853 return 31 - where;
854 return 32; // Undefined Behavior.
855#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +0000856}
857
858inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000859unsigned long __clz(unsigned long __x) {
860#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000861 return static_cast<unsigned long>(__builtin_clzl (__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000862#else
863 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
864 return __clz(static_cast<unsigned>(__x));
865#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +0000866}
867
868inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000869unsigned long long __clz(unsigned long long __x) {
870#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000871 return static_cast<unsigned long long>(__builtin_clzll(__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000872#else
873 unsigned long where;
874// BitScanReverse scans from MSB to LSB for first set bit.
875// Returns 0 if no set bit is found.
876#if defined(_LIBCPP_HAS_BITSCAN64)
877 if (_BitScanReverse64(&where, mask))
878 return static_cast<int>(63 - where);
879#else
880 // Scan the high 32 bits.
881 if (_BitScanReverse(&where, static_cast<unsigned long>(mask >> 32)))
882 return 63 - (where + 32); // Create a bit offset from the MSB.
883 // Scan the low 32 bits.
884 if (_BitScanReverse(&where, static_cast<unsigned long>(mask)))
885 return 63 - where;
886#endif
887 return 64; // Undefined Behavior.
888#endif // _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000889}
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000890
Eric Fiselierca91fd02017-05-10 21:30:04 +0000891inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) {
892#ifndef _LIBCPP_COMPILER_MSVC
893 return __builtin_popcount (__x);
894#else
895 static_assert(sizeof(unsigned) == 4, "");
896 return __popcnt(__x);
897#endif
898}
899
900inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) {
901#ifndef _LIBCPP_COMPILER_MSVC
902 return __builtin_popcountl (__x);
903#else
904 static_assert(sizeof(unsigned long) == 4, "");
905 return __popcnt(__x);
906#endif
907}
908
909inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {
910#ifndef _LIBCPP_COMPILER_MSVC
911 return __builtin_popcountll(__x);
912#else
913 static_assert(sizeof(unsigned long long) == 8, "");
914 return __popcnt64(__x);
915#endif
916}
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000917
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000918// all_of
919
920template <class _InputIterator, class _Predicate>
921inline _LIBCPP_INLINE_VISIBILITY
922bool
923all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
924{
925 for (; __first != __last; ++__first)
926 if (!__pred(*__first))
927 return false;
928 return true;
929}
930
931// any_of
932
933template <class _InputIterator, class _Predicate>
934inline _LIBCPP_INLINE_VISIBILITY
935bool
936any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
937{
938 for (; __first != __last; ++__first)
939 if (__pred(*__first))
940 return true;
941 return false;
942}
943
944// none_of
945
946template <class _InputIterator, class _Predicate>
947inline _LIBCPP_INLINE_VISIBILITY
948bool
949none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
950{
951 for (; __first != __last; ++__first)
952 if (__pred(*__first))
953 return false;
954 return true;
955}
956
957// for_each
958
959template <class _InputIterator, class _Function>
960inline _LIBCPP_INLINE_VISIBILITY
961_Function
962for_each(_InputIterator __first, _InputIterator __last, _Function __f)
963{
964 for (; __first != __last; ++__first)
965 __f(*__first);
Marshall Clowdb7fa112016-11-14 18:22:19 +0000966 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000967}
968
Marshall Clowefae8792017-05-25 13:40:57 +0000969#if _LIBCPP_STD_VER > 14
Marshall Clow10411c12017-05-25 02:29:54 +0000970// for_each_n
971
972template <class _InputIterator, class _Size, class _Function>
973inline _LIBCPP_INLINE_VISIBILITY
974_InputIterator
975for_each_n(_InputIterator __first, _Size __orig_n, _Function __f)
976{
977 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
978 _IntegralSize __n = __orig_n;
979 while (__n > 0)
980 {
981 __f(*__first);
982 ++__first;
983 --__n;
984 }
985 return __first;
986}
Marshall Clowefae8792017-05-25 13:40:57 +0000987#endif
Marshall Clow10411c12017-05-25 02:29:54 +0000988
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000989// find
990
991template <class _InputIterator, class _Tp>
992inline _LIBCPP_INLINE_VISIBILITY
993_InputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +0000994find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000995{
996 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +0000997 if (*__first == __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000998 break;
999 return __first;
1000}
1001
1002// find_if
1003
1004template <class _InputIterator, class _Predicate>
1005inline _LIBCPP_INLINE_VISIBILITY
1006_InputIterator
1007find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1008{
1009 for (; __first != __last; ++__first)
1010 if (__pred(*__first))
1011 break;
1012 return __first;
1013}
1014
1015// find_if_not
1016
1017template<class _InputIterator, class _Predicate>
1018inline _LIBCPP_INLINE_VISIBILITY
1019_InputIterator
1020find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1021{
1022 for (; __first != __last; ++__first)
1023 if (!__pred(*__first))
1024 break;
1025 return __first;
1026}
1027
1028// find_end
1029
1030template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1031_ForwardIterator1
1032__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1033 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
1034 forward_iterator_tag, forward_iterator_tag)
1035{
1036 // modeled after search algorithm
1037 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
1038 if (__first2 == __last2)
1039 return __r;
1040 while (true)
1041 {
1042 while (true)
1043 {
1044 if (__first1 == __last1) // if source exhausted return last correct answer
1045 return __r; // (or __last1 if never found)
1046 if (__pred(*__first1, *__first2))
1047 break;
1048 ++__first1;
1049 }
1050 // *__first1 matches *__first2, now match elements after here
1051 _ForwardIterator1 __m1 = __first1;
1052 _ForwardIterator2 __m2 = __first2;
1053 while (true)
1054 {
1055 if (++__m2 == __last2)
1056 { // Pattern exhaused, record answer and search for another one
1057 __r = __first1;
1058 ++__first1;
1059 break;
1060 }
1061 if (++__m1 == __last1) // Source exhausted, return last answer
1062 return __r;
1063 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
1064 {
1065 ++__first1;
1066 break;
1067 } // else there is a match, check next elements
1068 }
1069 }
1070}
1071
1072template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
1073_BidirectionalIterator1
1074__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
1075 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
1076 bidirectional_iterator_tag, bidirectional_iterator_tag)
1077{
1078 // modeled after search algorithm (in reverse)
1079 if (__first2 == __last2)
1080 return __last1; // Everything matches an empty sequence
1081 _BidirectionalIterator1 __l1 = __last1;
1082 _BidirectionalIterator2 __l2 = __last2;
1083 --__l2;
1084 while (true)
1085 {
1086 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
1087 while (true)
1088 {
1089 if (__first1 == __l1) // return __last1 if no element matches *__first2
1090 return __last1;
1091 if (__pred(*--__l1, *__l2))
1092 break;
1093 }
1094 // *__l1 matches *__l2, now match elements before here
1095 _BidirectionalIterator1 __m1 = __l1;
1096 _BidirectionalIterator2 __m2 = __l2;
1097 while (true)
1098 {
1099 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
1100 return __m1;
1101 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
1102 return __last1;
1103 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
1104 {
1105 break;
1106 } // else there is a match, check next elements
1107 }
1108 }
1109}
1110
1111template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow37025e12014-06-10 18:51:55 +00001112_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001113__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1114 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1115 random_access_iterator_tag, random_access_iterator_tag)
1116{
1117 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1118 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
1119 if (__len2 == 0)
1120 return __last1;
1121 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
1122 if (__len1 < __len2)
1123 return __last1;
1124 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
1125 _RandomAccessIterator1 __l1 = __last1;
1126 _RandomAccessIterator2 __l2 = __last2;
1127 --__l2;
1128 while (true)
1129 {
1130 while (true)
1131 {
1132 if (__s == __l1)
1133 return __last1;
1134 if (__pred(*--__l1, *__l2))
1135 break;
1136 }
1137 _RandomAccessIterator1 __m1 = __l1;
1138 _RandomAccessIterator2 __m2 = __l2;
1139 while (true)
1140 {
1141 if (__m2 == __first2)
1142 return __m1;
1143 // no need to check range on __m1 because __s guarantees we have enough source
1144 if (!__pred(*--__m1, *--__m2))
1145 {
1146 break;
1147 }
1148 }
1149 }
1150}
1151
1152template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1153inline _LIBCPP_INLINE_VISIBILITY
1154_ForwardIterator1
1155find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1156 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1157{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001158 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001159 (__first1, __last1, __first2, __last2, __pred,
1160 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1161 typename iterator_traits<_ForwardIterator2>::iterator_category());
1162}
1163
1164template <class _ForwardIterator1, class _ForwardIterator2>
1165inline _LIBCPP_INLINE_VISIBILITY
1166_ForwardIterator1
1167find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1168 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1169{
1170 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1171 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001172 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001173}
1174
1175// find_first_of
1176
1177template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow37025e12014-06-10 18:51:55 +00001178_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1179__find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001180 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1181{
1182 for (; __first1 != __last1; ++__first1)
1183 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1184 if (__pred(*__first1, *__j))
1185 return __first1;
1186 return __last1;
1187}
1188
Marshall Clow37025e12014-06-10 18:51:55 +00001189
1190template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1191inline _LIBCPP_INLINE_VISIBILITY
1192_ForwardIterator1
1193find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1194 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1195{
1196 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1197}
1198
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001199template <class _ForwardIterator1, class _ForwardIterator2>
1200inline _LIBCPP_INLINE_VISIBILITY
1201_ForwardIterator1
1202find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1203 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1204{
1205 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1206 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Marshall Clow37025e12014-06-10 18:51:55 +00001207 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001208}
1209
1210// adjacent_find
1211
1212template <class _ForwardIterator, class _BinaryPredicate>
1213inline _LIBCPP_INLINE_VISIBILITY
1214_ForwardIterator
1215adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1216{
1217 if (__first != __last)
1218 {
1219 _ForwardIterator __i = __first;
1220 while (++__i != __last)
1221 {
1222 if (__pred(*__first, *__i))
1223 return __first;
1224 __first = __i;
1225 }
1226 }
1227 return __last;
1228}
1229
1230template <class _ForwardIterator>
1231inline _LIBCPP_INLINE_VISIBILITY
1232_ForwardIterator
1233adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1234{
1235 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001236 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001237}
1238
1239// count
1240
1241template <class _InputIterator, class _Tp>
1242inline _LIBCPP_INLINE_VISIBILITY
1243typename iterator_traits<_InputIterator>::difference_type
Howard Hinnant78b68282011-10-22 20:59:45 +00001244count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001245{
1246 typename iterator_traits<_InputIterator>::difference_type __r(0);
1247 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +00001248 if (*__first == __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001249 ++__r;
1250 return __r;
1251}
1252
1253// count_if
1254
1255template <class _InputIterator, class _Predicate>
1256inline _LIBCPP_INLINE_VISIBILITY
1257typename iterator_traits<_InputIterator>::difference_type
1258count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1259{
1260 typename iterator_traits<_InputIterator>::difference_type __r(0);
1261 for (; __first != __last; ++__first)
1262 if (__pred(*__first))
1263 ++__r;
1264 return __r;
1265}
1266
1267// mismatch
1268
1269template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1270inline _LIBCPP_INLINE_VISIBILITY
1271pair<_InputIterator1, _InputIterator2>
1272mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1273 _InputIterator2 __first2, _BinaryPredicate __pred)
1274{
Marshall Clowd402a4d2014-09-16 20:40:05 +00001275 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001276 if (!__pred(*__first1, *__first2))
1277 break;
1278 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1279}
1280
1281template <class _InputIterator1, class _InputIterator2>
1282inline _LIBCPP_INLINE_VISIBILITY
1283pair<_InputIterator1, _InputIterator2>
1284mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1285{
1286 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1287 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001288 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001289}
1290
Marshall Clowb30abdd2013-05-09 21:14:23 +00001291#if _LIBCPP_STD_VER > 11
1292template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1293inline _LIBCPP_INLINE_VISIBILITY
1294pair<_InputIterator1, _InputIterator2>
1295mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1296 _InputIterator2 __first2, _InputIterator2 __last2,
1297 _BinaryPredicate __pred)
1298{
Marshall Clowd402a4d2014-09-16 20:40:05 +00001299 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001300 if (!__pred(*__first1, *__first2))
1301 break;
1302 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1303}
1304
1305template <class _InputIterator1, class _InputIterator2>
1306inline _LIBCPP_INLINE_VISIBILITY
1307pair<_InputIterator1, _InputIterator2>
1308mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1309 _InputIterator2 __first2, _InputIterator2 __last2)
1310{
1311 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1312 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1313 return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1314}
1315#endif
1316
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001317// equal
1318
1319template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1320inline _LIBCPP_INLINE_VISIBILITY
1321bool
1322equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1323{
Eric Fiselierb9919752014-10-27 19:28:20 +00001324 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001325 if (!__pred(*__first1, *__first2))
1326 return false;
1327 return true;
1328}
1329
1330template <class _InputIterator1, class _InputIterator2>
1331inline _LIBCPP_INLINE_VISIBILITY
1332bool
1333equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1334{
1335 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1336 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001337 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001338}
1339
Marshall Clowb30abdd2013-05-09 21:14:23 +00001340#if _LIBCPP_STD_VER > 11
1341template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
1342inline _LIBCPP_INLINE_VISIBILITY
1343bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001344__equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001345 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1346 input_iterator_tag, input_iterator_tag )
1347{
Eric Fiselierb9919752014-10-27 19:28:20 +00001348 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001349 if (!__pred(*__first1, *__first2))
1350 return false;
1351 return __first1 == __last1 && __first2 == __last2;
1352}
1353
1354template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1355inline _LIBCPP_INLINE_VISIBILITY
1356bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001357__equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1358 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001359 random_access_iterator_tag, random_access_iterator_tag )
1360{
1361 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1362 return false;
1363 return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1364 typename add_lvalue_reference<_BinaryPredicate>::type>
1365 (__first1, __last1, __first2, __pred );
1366}
1367
1368template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1369inline _LIBCPP_INLINE_VISIBILITY
1370bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001371equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001372 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1373{
1374 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001375 (__first1, __last1, __first2, __last2, __pred,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001376 typename iterator_traits<_InputIterator1>::iterator_category(),
1377 typename iterator_traits<_InputIterator2>::iterator_category());
1378}
1379
1380template <class _InputIterator1, class _InputIterator2>
1381inline _LIBCPP_INLINE_VISIBILITY
1382bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001383equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001384 _InputIterator2 __first2, _InputIterator2 __last2)
1385{
1386 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1387 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1388 return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1389 typename iterator_traits<_InputIterator1>::iterator_category(),
1390 typename iterator_traits<_InputIterator2>::iterator_category());
1391}
1392#endif
1393
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001394// is_permutation
1395
1396template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1397bool
1398is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1399 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1400{
1401 // shorten sequences as much as possible by lopping of any equal parts
Eric Fiselierb9919752014-10-27 19:28:20 +00001402 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001403 if (!__pred(*__first1, *__first2))
1404 goto __not_done;
1405 return true;
1406__not_done:
1407 // __first1 != __last1 && *__first1 != *__first2
1408 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001409 _D1 __l1 = _VSTD::distance(__first1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001410 if (__l1 == _D1(1))
1411 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001412 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001413 // For each element in [f1, l1) see if there are the same number of
1414 // equal elements in [f2, l2)
1415 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1416 {
1417 // Have we already counted the number of *__i in [f1, l1)?
1418 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1419 if (__pred(*__j, *__i))
1420 goto __next_iter;
1421 {
1422 // Count number of *__i in [f2, l2)
1423 _D1 __c2 = 0;
1424 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1425 if (__pred(*__i, *__j))
1426 ++__c2;
1427 if (__c2 == 0)
1428 return false;
1429 // Count number of *__i in [__i, l1) (we can start with 1)
1430 _D1 __c1 = 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001431 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001432 if (__pred(*__i, *__j))
1433 ++__c1;
1434 if (__c1 != __c2)
1435 return false;
1436 }
1437__next_iter:;
1438 }
1439 return true;
1440}
1441
1442template<class _ForwardIterator1, class _ForwardIterator2>
1443inline _LIBCPP_INLINE_VISIBILITY
1444bool
1445is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1446 _ForwardIterator2 __first2)
1447{
1448 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1449 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001450 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001451}
1452
Marshall Clowb30abdd2013-05-09 21:14:23 +00001453#if _LIBCPP_STD_VER > 11
1454template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1455bool
1456__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001457 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001458 _BinaryPredicate __pred,
1459 forward_iterator_tag, forward_iterator_tag )
1460{
1461 // shorten sequences as much as possible by lopping of any equal parts
Eric Fiselier62a0e012014-10-27 20:26:25 +00001462 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001463 if (!__pred(*__first1, *__first2))
1464 goto __not_done;
1465 return __first1 == __last1 && __first2 == __last2;
1466__not_done:
1467 // __first1 != __last1 && __first2 != __last2 && *__first1 != *__first2
1468 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 {
1480 // Have we already counted the number of *__i in [f1, l1)?
1481 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1482 if (__pred(*__j, *__i))
1483 goto __next_iter;
1484 {
1485 // Count number of *__i in [f2, l2)
1486 _D1 __c2 = 0;
1487 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1488 if (__pred(*__i, *__j))
1489 ++__c2;
1490 if (__c2 == 0)
1491 return false;
1492 // Count number of *__i in [__i, l1) (we can start with 1)
1493 _D1 __c1 = 1;
1494 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1495 if (__pred(*__i, *__j))
1496 ++__c1;
1497 if (__c1 != __c2)
1498 return false;
1499 }
1500__next_iter:;
1501 }
1502 return true;
1503}
1504
1505template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1506bool
1507__is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001508 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001509 _BinaryPredicate __pred,
1510 random_access_iterator_tag, random_access_iterator_tag )
1511{
1512 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1513 return false;
1514 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1515 typename add_lvalue_reference<_BinaryPredicate>::type>
1516 (__first1, __last1, __first2, __pred );
1517}
1518
1519template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1520inline _LIBCPP_INLINE_VISIBILITY
1521bool
1522is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1523 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1524 _BinaryPredicate __pred )
1525{
1526 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1527 (__first1, __last1, __first2, __last2, __pred,
1528 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1529 typename iterator_traits<_ForwardIterator2>::iterator_category());
1530}
1531
1532template<class _ForwardIterator1, class _ForwardIterator2>
1533inline _LIBCPP_INLINE_VISIBILITY
1534bool
1535is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1536 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1537{
1538 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1539 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1540 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1541 __equal_to<__v1, __v2>(),
1542 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1543 typename iterator_traits<_ForwardIterator2>::iterator_category());
1544}
1545#endif
1546
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001547// search
1548
1549template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clowf6d6b512016-03-08 15:12:52 +00001550pair<_ForwardIterator1, _ForwardIterator1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001551__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1552 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
1553 forward_iterator_tag, forward_iterator_tag)
1554{
1555 if (__first2 == __last2)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001556 return make_pair(__first1, __first1); // Everything matches an empty sequence
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001557 while (true)
1558 {
1559 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
1560 while (true)
1561 {
1562 if (__first1 == __last1) // return __last1 if no element matches *__first2
Marshall Clowf6d6b512016-03-08 15:12:52 +00001563 return make_pair(__last1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001564 if (__pred(*__first1, *__first2))
1565 break;
1566 ++__first1;
1567 }
1568 // *__first1 matches *__first2, now match elements after here
1569 _ForwardIterator1 __m1 = __first1;
1570 _ForwardIterator2 __m2 = __first2;
1571 while (true)
1572 {
1573 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001574 return make_pair(__first1, __m1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001575 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
Marshall Clowf6d6b512016-03-08 15:12:52 +00001576 return make_pair(__last1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001577 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
1578 {
1579 ++__first1;
1580 break;
1581 } // else there is a match, check next elements
1582 }
1583 }
1584}
1585
1586template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001587_LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowf6d6b512016-03-08 15:12:52 +00001588pair<_RandomAccessIterator1, _RandomAccessIterator1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001589__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
Marshall Clowf6d6b512016-03-08 15:12:52 +00001590 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001591 random_access_iterator_tag, random_access_iterator_tag)
1592{
Marshall Clowf6d6b512016-03-08 15:12:52 +00001593 typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
1594 typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001595 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
Marshall Clowf6d6b512016-03-08 15:12:52 +00001596 const _D2 __len2 = __last2 - __first2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597 if (__len2 == 0)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001598 return make_pair(__first1, __first1);
1599 const _D1 __len1 = __last1 - __first1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001600 if (__len1 < __len2)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001601 return make_pair(__last1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001602 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
Aditya Kumar42997952016-11-29 14:43:42 +00001603
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001604 while (true)
1605 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001606 while (true)
1607 {
1608 if (__first1 == __s)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001609 return make_pair(__last1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001610 if (__pred(*__first1, *__first2))
1611 break;
1612 ++__first1;
1613 }
Aditya Kumar42997952016-11-29 14:43:42 +00001614
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001615 _RandomAccessIterator1 __m1 = __first1;
1616 _RandomAccessIterator2 __m2 = __first2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001617 while (true)
1618 {
1619 if (++__m2 == __last2)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001620 return make_pair(__first1, __first1 + __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001621 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
1622 if (!__pred(*__m1, *__m2))
1623 {
1624 ++__first1;
1625 break;
1626 }
1627 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001628 }
1629}
1630
1631template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1632inline _LIBCPP_INLINE_VISIBILITY
1633_ForwardIterator1
1634search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1635 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1636{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001637 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001638 (__first1, __last1, __first2, __last2, __pred,
Marshall Clowf6d6b512016-03-08 15:12:52 +00001639 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1640 typename iterator_traits<_ForwardIterator2>::iterator_category())
1641 .first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001642}
1643
1644template <class _ForwardIterator1, class _ForwardIterator2>
1645inline _LIBCPP_INLINE_VISIBILITY
1646_ForwardIterator1
1647search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1648 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1649{
Marshall Clowf6d6b512016-03-08 15:12:52 +00001650 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1651 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001652 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001653}
1654
1655// search_n
1656
1657template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
1658_ForwardIterator
1659__search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001660 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001661{
1662 if (__count <= 0)
1663 return __first;
1664 while (true)
1665 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001666 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001667 while (true)
1668 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001669 if (__first == __last) // return __last if no element matches __value_
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001670 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001671 if (__pred(*__first, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001672 break;
1673 ++__first;
1674 }
Howard Hinnant78b68282011-10-22 20:59:45 +00001675 // *__first matches __value_, now match elements after here
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676 _ForwardIterator __m = __first;
1677 _Size __c(0);
1678 while (true)
1679 {
1680 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1681 return __first;
1682 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1683 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001684 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001685 {
1686 __first = __m;
1687 ++__first;
1688 break;
1689 } // else there is a match, check next elements
1690 }
1691 }
1692}
1693
1694template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
1695_RandomAccessIterator
1696__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001697 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001698{
1699 if (__count <= 0)
1700 return __first;
1701 _Size __len = static_cast<_Size>(__last - __first);
1702 if (__len < __count)
1703 return __last;
1704 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1705 while (true)
1706 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001707 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001708 while (true)
1709 {
Howard Hinnant128f7bf2013-04-04 15:40:48 +00001710 if (__first >= __s) // return __last if no element matches __value_
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001711 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001712 if (__pred(*__first, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001713 break;
1714 ++__first;
1715 }
Howard Hinnant78b68282011-10-22 20:59:45 +00001716 // *__first matches __value_, now match elements after here
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001717 _RandomAccessIterator __m = __first;
1718 _Size __c(0);
1719 while (true)
1720 {
1721 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1722 return __first;
1723 ++__m; // no need to check range on __m because __s guarantees we have enough source
Howard Hinnant78b68282011-10-22 20:59:45 +00001724 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001725 {
1726 __first = __m;
1727 ++__first;
1728 break;
1729 } // else there is a match, check next elements
1730 }
1731 }
1732}
1733
1734template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
1735inline _LIBCPP_INLINE_VISIBILITY
1736_ForwardIterator
1737search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001738 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001739{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001740 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001741 (__first, __last, __convert_to_integral(__count), __value_, __pred,
1742 typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001743}
1744
1745template <class _ForwardIterator, class _Size, class _Tp>
1746inline _LIBCPP_INLINE_VISIBILITY
1747_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00001748search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001749{
1750 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001751 return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
1752 __value_, __equal_to<__v, _Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001753}
1754
1755// copy
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001756template <class _Iter>
1757inline _LIBCPP_INLINE_VISIBILITY
1758_Iter
1759__unwrap_iter(_Iter __i)
1760{
1761 return __i;
1762}
1763
1764template <class _Tp>
Marshall Clow798ecd62017-05-25 14:20:26 +00001765inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001766typename enable_if
1767<
Howard Hinnant1468b662010-11-19 22:17:28 +00001768 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001769 _Tp*
1770>::type
1771__unwrap_iter(move_iterator<_Tp*> __i)
1772{
1773 return __i.base();
1774}
1775
Howard Hinnant499cea12013-08-23 17:37:05 +00001776#if _LIBCPP_DEBUG_LEVEL < 2
1777
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001778template <class _Tp>
Marshall Clow798ecd62017-05-25 14:20:26 +00001779inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001780typename enable_if
1781<
Howard Hinnant1468b662010-11-19 22:17:28 +00001782 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001783 _Tp*
1784>::type
1785__unwrap_iter(__wrap_iter<_Tp*> __i)
1786{
1787 return __i.base();
1788}
1789
Eric Fiselier2c8aa052016-12-28 05:35:32 +00001790#else
1791
1792template <class _Tp>
Marshall Clow798ecd62017-05-25 14:20:26 +00001793inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2c8aa052016-12-28 05:35:32 +00001794typename enable_if
1795<
1796 is_trivially_copy_assignable<_Tp>::value,
1797 __wrap_iter<_Tp*>
1798>::type
1799__unwrap_iter(__wrap_iter<_Tp*> __i)
1800{
1801 return __i;
1802}
1803
Howard Hinnant499cea12013-08-23 17:37:05 +00001804#endif // _LIBCPP_DEBUG_LEVEL < 2
1805
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001806template <class _InputIterator, class _OutputIterator>
1807inline _LIBCPP_INLINE_VISIBILITY
1808_OutputIterator
1809__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1810{
Eric Fiselierb9919752014-10-27 19:28:20 +00001811 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001812 *__result = *__first;
1813 return __result;
1814}
1815
1816template <class _Tp, class _Up>
1817inline _LIBCPP_INLINE_VISIBILITY
1818typename enable_if
1819<
1820 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001821 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001822 _Up*
1823>::type
1824__copy(_Tp* __first, _Tp* __last, _Up* __result)
1825{
1826 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001827 if (__n > 0)
1828 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829 return __result + __n;
1830}
1831
1832template <class _InputIterator, class _OutputIterator>
1833inline _LIBCPP_INLINE_VISIBILITY
1834_OutputIterator
1835copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1836{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001837 return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001838}
1839
1840// copy_backward
1841
Howard Hinnantb73568d2013-02-06 21:03:39 +00001842template <class _BidirectionalIterator, class _OutputIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001843inline _LIBCPP_INLINE_VISIBILITY
1844_OutputIterator
Howard Hinnantb73568d2013-02-06 21:03:39 +00001845__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001846{
1847 while (__first != __last)
1848 *--__result = *--__last;
1849 return __result;
1850}
1851
1852template <class _Tp, class _Up>
1853inline _LIBCPP_INLINE_VISIBILITY
1854typename enable_if
1855<
1856 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001857 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001858 _Up*
1859>::type
1860__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1861{
1862 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001863 if (__n > 0)
1864 {
1865 __result -= __n;
1866 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1867 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001868 return __result;
1869}
1870
1871template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1872inline _LIBCPP_INLINE_VISIBILITY
1873_BidirectionalIterator2
1874copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1875 _BidirectionalIterator2 __result)
1876{
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00001877 return _VSTD::__copy_backward(__unwrap_iter(__first),
1878 __unwrap_iter(__last),
1879 __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001880}
1881
1882// copy_if
1883
1884template<class _InputIterator, class _OutputIterator, class _Predicate>
1885inline _LIBCPP_INLINE_VISIBILITY
1886_OutputIterator
1887copy_if(_InputIterator __first, _InputIterator __last,
1888 _OutputIterator __result, _Predicate __pred)
1889{
1890 for (; __first != __last; ++__first)
1891 {
1892 if (__pred(*__first))
1893 {
1894 *__result = *__first;
1895 ++__result;
1896 }
1897 }
1898 return __result;
1899}
1900
1901// copy_n
1902
1903template<class _InputIterator, class _Size, class _OutputIterator>
1904inline _LIBCPP_INLINE_VISIBILITY
1905typename enable_if
1906<
1907 __is_input_iterator<_InputIterator>::value &&
1908 !__is_random_access_iterator<_InputIterator>::value,
1909 _OutputIterator
1910>::type
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001911copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001912{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001913 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1914 _IntegralSize __n = __orig_n;
Howard Hinnant171869e2011-02-27 20:55:39 +00001915 if (__n > 0)
1916 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001917 *__result = *__first;
Howard Hinnant171869e2011-02-27 20:55:39 +00001918 ++__result;
1919 for (--__n; __n > 0; --__n)
1920 {
1921 ++__first;
1922 *__result = *__first;
1923 ++__result;
1924 }
1925 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001926 return __result;
1927}
1928
1929template<class _InputIterator, class _Size, class _OutputIterator>
1930inline _LIBCPP_INLINE_VISIBILITY
1931typename enable_if
1932<
1933 __is_random_access_iterator<_InputIterator>::value,
1934 _OutputIterator
1935>::type
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001936copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001937{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001938 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1939 _IntegralSize __n = __orig_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001940 return _VSTD::copy(__first, __first + __n, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001941}
1942
1943// move
1944
1945template <class _InputIterator, class _OutputIterator>
1946inline _LIBCPP_INLINE_VISIBILITY
1947_OutputIterator
1948__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1949{
Eric Fiselierb9919752014-10-27 19:28:20 +00001950 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001951 *__result = _VSTD::move(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001952 return __result;
1953}
1954
1955template <class _Tp, class _Up>
1956inline _LIBCPP_INLINE_VISIBILITY
1957typename enable_if
1958<
1959 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001960 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001961 _Up*
1962>::type
1963__move(_Tp* __first, _Tp* __last, _Up* __result)
1964{
1965 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001966 if (__n > 0)
1967 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001968 return __result + __n;
1969}
1970
1971template <class _InputIterator, class _OutputIterator>
1972inline _LIBCPP_INLINE_VISIBILITY
1973_OutputIterator
1974move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1975{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001976 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001977}
1978
1979// move_backward
1980
1981template <class _InputIterator, class _OutputIterator>
1982inline _LIBCPP_INLINE_VISIBILITY
1983_OutputIterator
1984__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1985{
1986 while (__first != __last)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001987 *--__result = _VSTD::move(*--__last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001988 return __result;
1989}
1990
1991template <class _Tp, class _Up>
1992inline _LIBCPP_INLINE_VISIBILITY
1993typename enable_if
1994<
1995 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001996 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001997 _Up*
1998>::type
1999__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
2000{
2001 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00002002 if (__n > 0)
2003 {
2004 __result -= __n;
2005 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
2006 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002007 return __result;
2008}
2009
2010template <class _BidirectionalIterator1, class _BidirectionalIterator2>
2011inline _LIBCPP_INLINE_VISIBILITY
2012_BidirectionalIterator2
2013move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
2014 _BidirectionalIterator2 __result)
2015{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002016 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002017}
2018
2019// iter_swap
2020
Howard Hinnante9b2c2d2011-05-27 15:04:19 +00002021// moved to <type_traits> for better swap / noexcept support
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002022
2023// transform
2024
2025template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
2026inline _LIBCPP_INLINE_VISIBILITY
2027_OutputIterator
2028transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
2029{
Eric Fiselierb9919752014-10-27 19:28:20 +00002030 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002031 *__result = __op(*__first);
2032 return __result;
2033}
2034
2035template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
2036inline _LIBCPP_INLINE_VISIBILITY
2037_OutputIterator
2038transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
2039 _OutputIterator __result, _BinaryOperation __binary_op)
2040{
Eric Fiselierb9919752014-10-27 19:28:20 +00002041 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002042 *__result = __binary_op(*__first1, *__first2);
2043 return __result;
2044}
2045
2046// replace
2047
2048template <class _ForwardIterator, class _Tp>
2049inline _LIBCPP_INLINE_VISIBILITY
2050void
2051replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
2052{
2053 for (; __first != __last; ++__first)
2054 if (*__first == __old_value)
2055 *__first = __new_value;
2056}
2057
2058// replace_if
2059
2060template <class _ForwardIterator, class _Predicate, class _Tp>
2061inline _LIBCPP_INLINE_VISIBILITY
2062void
2063replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
2064{
2065 for (; __first != __last; ++__first)
2066 if (__pred(*__first))
2067 *__first = __new_value;
2068}
2069
2070// replace_copy
2071
2072template <class _InputIterator, class _OutputIterator, class _Tp>
2073inline _LIBCPP_INLINE_VISIBILITY
2074_OutputIterator
2075replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2076 const _Tp& __old_value, const _Tp& __new_value)
2077{
Eric Fiselierb9919752014-10-27 19:28:20 +00002078 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002079 if (*__first == __old_value)
2080 *__result = __new_value;
2081 else
2082 *__result = *__first;
2083 return __result;
2084}
2085
2086// replace_copy_if
2087
2088template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
2089inline _LIBCPP_INLINE_VISIBILITY
2090_OutputIterator
2091replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2092 _Predicate __pred, const _Tp& __new_value)
2093{
Eric Fiselierb9919752014-10-27 19:28:20 +00002094 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002095 if (__pred(*__first))
2096 *__result = __new_value;
2097 else
2098 *__result = *__first;
2099 return __result;
2100}
2101
2102// fill_n
2103
2104template <class _OutputIterator, class _Size, class _Tp>
2105inline _LIBCPP_INLINE_VISIBILITY
2106_OutputIterator
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002107__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002108{
Eric Fiselierb9919752014-10-27 19:28:20 +00002109 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnant78b68282011-10-22 20:59:45 +00002110 *__first = __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002111 return __first;
2112}
2113
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002114template <class _Tp, class _Size, class _Up>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002115inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002116typename enable_if
2117<
2118 is_integral<_Tp>::value && sizeof(_Tp) == 1 &&
2119 !is_same<_Tp, bool>::value &&
2120 is_integral<_Up>::value && sizeof(_Up) == 1,
2121 _Tp*
2122>::type
2123__fill_n(_Tp* __first, _Size __n,_Up __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002124{
2125 if (__n > 0)
Howard Hinnant78b68282011-10-22 20:59:45 +00002126 _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002127 return __first + __n;
2128}
2129
2130template <class _OutputIterator, class _Size, class _Tp>
2131inline _LIBCPP_INLINE_VISIBILITY
2132_OutputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002133fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002134{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002135 return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002136}
2137
2138// fill
2139
2140template <class _ForwardIterator, class _Tp>
2141inline _LIBCPP_INLINE_VISIBILITY
2142void
Howard Hinnant78b68282011-10-22 20:59:45 +00002143__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002144{
2145 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +00002146 *__first = __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002147}
2148
2149template <class _RandomAccessIterator, class _Tp>
2150inline _LIBCPP_INLINE_VISIBILITY
2151void
Howard Hinnant78b68282011-10-22 20:59:45 +00002152__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002153{
Howard Hinnant78b68282011-10-22 20:59:45 +00002154 _VSTD::fill_n(__first, __last - __first, __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002155}
2156
2157template <class _ForwardIterator, class _Tp>
2158inline _LIBCPP_INLINE_VISIBILITY
2159void
Howard Hinnant78b68282011-10-22 20:59:45 +00002160fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002161{
Howard Hinnant78b68282011-10-22 20:59:45 +00002162 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002163}
2164
2165// generate
2166
2167template <class _ForwardIterator, class _Generator>
2168inline _LIBCPP_INLINE_VISIBILITY
2169void
2170generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2171{
2172 for (; __first != __last; ++__first)
2173 *__first = __gen();
2174}
2175
2176// generate_n
2177
2178template <class _OutputIterator, class _Size, class _Generator>
2179inline _LIBCPP_INLINE_VISIBILITY
2180_OutputIterator
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002181generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002182{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002183 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
2184 _IntegralSize __n = __orig_n;
Eric Fiselierb9919752014-10-27 19:28:20 +00002185 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002186 *__first = __gen();
2187 return __first;
2188}
2189
2190// remove
2191
2192template <class _ForwardIterator, class _Tp>
2193_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002194remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002195{
Howard Hinnant78b68282011-10-22 20:59:45 +00002196 __first = _VSTD::find(__first, __last, __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002197 if (__first != __last)
2198 {
2199 _ForwardIterator __i = __first;
2200 while (++__i != __last)
2201 {
Howard Hinnant78b68282011-10-22 20:59:45 +00002202 if (!(*__i == __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002203 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002204 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002205 ++__first;
2206 }
2207 }
2208 }
2209 return __first;
2210}
2211
2212// remove_if
2213
2214template <class _ForwardIterator, class _Predicate>
2215_ForwardIterator
2216remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2217{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002218 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002219 (__first, __last, __pred);
2220 if (__first != __last)
2221 {
2222 _ForwardIterator __i = __first;
2223 while (++__i != __last)
2224 {
2225 if (!__pred(*__i))
2226 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002227 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002228 ++__first;
2229 }
2230 }
2231 }
2232 return __first;
2233}
2234
2235// remove_copy
2236
2237template <class _InputIterator, class _OutputIterator, class _Tp>
2238inline _LIBCPP_INLINE_VISIBILITY
2239_OutputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002240remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002241{
2242 for (; __first != __last; ++__first)
2243 {
Howard Hinnant78b68282011-10-22 20:59:45 +00002244 if (!(*__first == __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002245 {
2246 *__result = *__first;
2247 ++__result;
2248 }
2249 }
2250 return __result;
2251}
2252
2253// remove_copy_if
2254
2255template <class _InputIterator, class _OutputIterator, class _Predicate>
2256inline _LIBCPP_INLINE_VISIBILITY
2257_OutputIterator
2258remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2259{
2260 for (; __first != __last; ++__first)
2261 {
2262 if (!__pred(*__first))
2263 {
2264 *__result = *__first;
2265 ++__result;
2266 }
2267 }
2268 return __result;
2269}
2270
2271// unique
2272
2273template <class _ForwardIterator, class _BinaryPredicate>
2274_ForwardIterator
2275unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2276{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002277 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002278 (__first, __last, __pred);
2279 if (__first != __last)
2280 {
2281 // ... a a ? ...
2282 // f i
2283 _ForwardIterator __i = __first;
2284 for (++__i; ++__i != __last;)
2285 if (!__pred(*__first, *__i))
Howard Hinnant0949eed2011-06-30 21:18:19 +00002286 *++__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002287 ++__first;
2288 }
2289 return __first;
2290}
2291
2292template <class _ForwardIterator>
2293inline _LIBCPP_INLINE_VISIBILITY
2294_ForwardIterator
2295unique(_ForwardIterator __first, _ForwardIterator __last)
2296{
2297 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002298 return _VSTD::unique(__first, __last, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002299}
2300
2301// unique_copy
2302
2303template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
2304_OutputIterator
2305__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2306 input_iterator_tag, output_iterator_tag)
2307{
2308 if (__first != __last)
2309 {
2310 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2311 *__result = __t;
2312 ++__result;
2313 while (++__first != __last)
2314 {
2315 if (!__pred(__t, *__first))
2316 {
2317 __t = *__first;
2318 *__result = __t;
2319 ++__result;
2320 }
2321 }
2322 }
2323 return __result;
2324}
2325
2326template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
2327_OutputIterator
2328__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2329 forward_iterator_tag, output_iterator_tag)
2330{
2331 if (__first != __last)
2332 {
2333 _ForwardIterator __i = __first;
2334 *__result = *__i;
2335 ++__result;
2336 while (++__first != __last)
2337 {
2338 if (!__pred(*__i, *__first))
2339 {
2340 *__result = *__first;
2341 ++__result;
2342 __i = __first;
2343 }
2344 }
2345 }
2346 return __result;
2347}
2348
2349template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
2350_ForwardIterator
2351__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2352 input_iterator_tag, forward_iterator_tag)
2353{
2354 if (__first != __last)
2355 {
2356 *__result = *__first;
2357 while (++__first != __last)
2358 if (!__pred(*__result, *__first))
2359 *++__result = *__first;
2360 ++__result;
2361 }
2362 return __result;
2363}
2364
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002365template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
2366inline _LIBCPP_INLINE_VISIBILITY
2367_OutputIterator
2368unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2369{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002370 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002371 (__first, __last, __result, __pred,
2372 typename iterator_traits<_InputIterator>::iterator_category(),
2373 typename iterator_traits<_OutputIterator>::iterator_category());
2374}
2375
2376template <class _InputIterator, class _OutputIterator>
2377inline _LIBCPP_INLINE_VISIBILITY
2378_OutputIterator
2379unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2380{
2381 typedef typename iterator_traits<_InputIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002382 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002383}
2384
2385// reverse
2386
2387template <class _BidirectionalIterator>
2388inline _LIBCPP_INLINE_VISIBILITY
2389void
2390__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2391{
2392 while (__first != __last)
2393 {
2394 if (__first == --__last)
2395 break;
Marshall Clowc010bd62015-11-02 21:34:25 +00002396 _VSTD::iter_swap(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002397 ++__first;
2398 }
2399}
2400
2401template <class _RandomAccessIterator>
2402inline _LIBCPP_INLINE_VISIBILITY
2403void
2404__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2405{
2406 if (__first != __last)
2407 for (; __first < --__last; ++__first)
Marshall Clowc010bd62015-11-02 21:34:25 +00002408 _VSTD::iter_swap(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002409}
2410
2411template <class _BidirectionalIterator>
2412inline _LIBCPP_INLINE_VISIBILITY
2413void
2414reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2415{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002416 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002417}
2418
2419// reverse_copy
2420
2421template <class _BidirectionalIterator, class _OutputIterator>
2422inline _LIBCPP_INLINE_VISIBILITY
2423_OutputIterator
2424reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2425{
2426 for (; __first != __last; ++__result)
2427 *__result = *--__last;
2428 return __result;
2429}
2430
2431// rotate
2432
2433template <class _ForwardIterator>
2434_ForwardIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002435__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002436{
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002437 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2438 value_type __tmp = _VSTD::move(*__first);
2439 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2440 *__lm1 = _VSTD::move(__tmp);
2441 return __lm1;
2442}
2443
2444template <class _BidirectionalIterator>
2445_BidirectionalIterator
2446__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2447{
2448 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2449 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2450 value_type __tmp = _VSTD::move(*__lm1);
2451 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2452 *__first = _VSTD::move(__tmp);
2453 return __fp1;
2454}
2455
2456template <class _ForwardIterator>
2457_ForwardIterator
2458__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2459{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002460 _ForwardIterator __i = __middle;
2461 while (true)
2462 {
2463 swap(*__first, *__i);
2464 ++__first;
2465 if (++__i == __last)
2466 break;
2467 if (__first == __middle)
2468 __middle = __i;
2469 }
2470 _ForwardIterator __r = __first;
2471 if (__first != __middle)
2472 {
2473 __i = __middle;
2474 while (true)
2475 {
2476 swap(*__first, *__i);
2477 ++__first;
2478 if (++__i == __last)
2479 {
2480 if (__first == __middle)
2481 break;
2482 __i = __middle;
2483 }
2484 else if (__first == __middle)
2485 __middle = __i;
2486 }
2487 }
2488 return __r;
2489}
2490
2491template<typename _Integral>
2492inline _LIBCPP_INLINE_VISIBILITY
2493_Integral
Marshall Clow1c1e91d2016-07-26 14:29:45 +00002494__algo_gcd(_Integral __x, _Integral __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002495{
2496 do
2497 {
2498 _Integral __t = __x % __y;
2499 __x = __y;
2500 __y = __t;
2501 } while (__y);
2502 return __x;
2503}
2504
2505template<typename _RandomAccessIterator>
2506_RandomAccessIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002507__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002508{
2509 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2510 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant324bb032010-08-22 00:02:43 +00002511
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002512 const difference_type __m1 = __middle - __first;
2513 const difference_type __m2 = __last - __middle;
2514 if (__m1 == __m2)
2515 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002516 _VSTD::swap_ranges(__first, __middle, __middle);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002517 return __middle;
2518 }
Marshall Clow1c1e91d2016-07-26 14:29:45 +00002519 const difference_type __g = _VSTD::__algo_gcd(__m1, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002520 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2521 {
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002522 value_type __t(_VSTD::move(*--__p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002523 _RandomAccessIterator __p1 = __p;
2524 _RandomAccessIterator __p2 = __p1 + __m1;
2525 do
2526 {
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002527 *__p1 = _VSTD::move(*__p2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002528 __p1 = __p2;
2529 const difference_type __d = __last - __p2;
2530 if (__m1 < __d)
2531 __p2 += __m1;
2532 else
2533 __p2 = __first + (__m1 - __d);
2534 } while (__p2 != __p);
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002535 *__p1 = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002536 }
2537 return __first + __m2;
2538}
2539
2540template <class _ForwardIterator>
2541inline _LIBCPP_INLINE_VISIBILITY
2542_ForwardIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002543__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2544 _VSTD::forward_iterator_tag)
2545{
2546 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2547 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2548 {
2549 if (_VSTD::next(__first) == __middle)
2550 return _VSTD::__rotate_left(__first, __last);
2551 }
2552 return _VSTD::__rotate_forward(__first, __middle, __last);
2553}
2554
2555template <class _BidirectionalIterator>
2556inline _LIBCPP_INLINE_VISIBILITY
2557_BidirectionalIterator
2558__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2559 _VSTD::bidirectional_iterator_tag)
2560{
2561 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2562 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2563 {
2564 if (_VSTD::next(__first) == __middle)
2565 return _VSTD::__rotate_left(__first, __last);
2566 if (_VSTD::next(__middle) == __last)
2567 return _VSTD::__rotate_right(__first, __last);
2568 }
2569 return _VSTD::__rotate_forward(__first, __middle, __last);
2570}
2571
2572template <class _RandomAccessIterator>
2573inline _LIBCPP_INLINE_VISIBILITY
2574_RandomAccessIterator
2575__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2576 _VSTD::random_access_iterator_tag)
2577{
2578 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2579 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2580 {
2581 if (_VSTD::next(__first) == __middle)
2582 return _VSTD::__rotate_left(__first, __last);
2583 if (_VSTD::next(__middle) == __last)
2584 return _VSTD::__rotate_right(__first, __last);
2585 return _VSTD::__rotate_gcd(__first, __middle, __last);
2586 }
2587 return _VSTD::__rotate_forward(__first, __middle, __last);
2588}
2589
2590template <class _ForwardIterator>
2591inline _LIBCPP_INLINE_VISIBILITY
2592_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002593rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2594{
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002595 if (__first == __middle)
2596 return __last;
2597 if (__middle == __last)
2598 return __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002599 return _VSTD::__rotate(__first, __middle, __last,
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002600 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002601}
2602
2603// rotate_copy
2604
2605template <class _ForwardIterator, class _OutputIterator>
2606inline _LIBCPP_INLINE_VISIBILITY
2607_OutputIterator
2608rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2609{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002610 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002611}
2612
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002613// min_element
2614
2615template <class _ForwardIterator, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002616inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002617_ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +00002618min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002619{
2620 if (__first != __last)
2621 {
2622 _ForwardIterator __i = __first;
2623 while (++__i != __last)
2624 if (__comp(*__i, *__first))
2625 __first = __i;
2626 }
2627 return __first;
2628}
2629
2630template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002631inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002632_ForwardIterator
2633min_element(_ForwardIterator __first, _ForwardIterator __last)
2634{
Marshall Clow928735a2015-05-10 13:53:31 +00002635 return _VSTD::min_element(__first, __last,
Howard Hinnant98e5d972010-08-21 20:10:01 +00002636 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2637}
2638
2639// min
2640
2641template <class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002642inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002643const _Tp&
2644min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2645{
2646 return __comp(__b, __a) ? __b : __a;
2647}
2648
2649template <class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002650inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002651const _Tp&
2652min(const _Tp& __a, const _Tp& __b)
2653{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002654 return _VSTD::min(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002655}
2656
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002657#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002658
Howard Hinnant98e5d972010-08-21 20:10:01 +00002659template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002660inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002661_Tp
2662min(initializer_list<_Tp> __t, _Compare __comp)
2663{
Marshall Clow928735a2015-05-10 13:53:31 +00002664 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002665}
2666
2667template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002668inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002669_Tp
2670min(initializer_list<_Tp> __t)
2671{
Marshall Clow928735a2015-05-10 13:53:31 +00002672 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002673}
2674
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002675#endif // _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002676
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002677// max_element
2678
2679template <class _ForwardIterator, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002680inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002681_ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +00002682max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002683{
2684 if (__first != __last)
2685 {
2686 _ForwardIterator __i = __first;
2687 while (++__i != __last)
2688 if (__comp(*__first, *__i))
2689 __first = __i;
2690 }
2691 return __first;
2692}
2693
Marshall Clow9d9463a2014-02-19 16:51:35 +00002694
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002695template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002696inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002697_ForwardIterator
2698max_element(_ForwardIterator __first, _ForwardIterator __last)
2699{
Marshall Clow928735a2015-05-10 13:53:31 +00002700 return _VSTD::max_element(__first, __last,
Howard Hinnant98e5d972010-08-21 20:10:01 +00002701 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2702}
2703
2704// max
2705
2706template <class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002707inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002708const _Tp&
2709max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2710{
2711 return __comp(__a, __b) ? __b : __a;
2712}
2713
2714template <class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002715inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002716const _Tp&
2717max(const _Tp& __a, const _Tp& __b)
2718{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002719 return _VSTD::max(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002720}
2721
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002722#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002723
Howard Hinnant98e5d972010-08-21 20:10:01 +00002724template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002725inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002726_Tp
2727max(initializer_list<_Tp> __t, _Compare __comp)
2728{
Marshall Clow928735a2015-05-10 13:53:31 +00002729 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002730}
2731
2732template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002733inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002734_Tp
2735max(initializer_list<_Tp> __t)
2736{
Marshall Clow928735a2015-05-10 13:53:31 +00002737 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002738}
2739
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002740#endif // _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002741
Marshall Clow3e0808e2016-03-07 22:43:49 +00002742#if _LIBCPP_STD_VER > 14
2743// clamp
2744template<class _Tp, class _Compare>
2745inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2746const _Tp&
2747clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
2748{
2749 _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
2750 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
2751
2752}
2753
2754template<class _Tp>
2755inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2756const _Tp&
2757clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
2758{
2759 return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
2760}
2761#endif
2762
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002763// minmax_element
2764
2765template <class _ForwardIterator, class _Compare>
Marshall Clow928735a2015-05-10 13:53:31 +00002766_LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002767std::pair<_ForwardIterator, _ForwardIterator>
2768minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2769{
2770 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2771 if (__first != __last)
2772 {
2773 if (++__first != __last)
2774 {
2775 if (__comp(*__first, *__result.first))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002776 __result.first = __first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002777 else
2778 __result.second = __first;
2779 while (++__first != __last)
2780 {
2781 _ForwardIterator __i = __first;
2782 if (++__first == __last)
2783 {
2784 if (__comp(*__i, *__result.first))
2785 __result.first = __i;
2786 else if (!__comp(*__i, *__result.second))
2787 __result.second = __i;
2788 break;
2789 }
2790 else
2791 {
2792 if (__comp(*__first, *__i))
2793 {
2794 if (__comp(*__first, *__result.first))
2795 __result.first = __first;
2796 if (!__comp(*__i, *__result.second))
2797 __result.second = __i;
2798 }
2799 else
2800 {
2801 if (__comp(*__i, *__result.first))
2802 __result.first = __i;
2803 if (!__comp(*__first, *__result.second))
2804 __result.second = __first;
2805 }
2806 }
2807 }
2808 }
2809 }
2810 return __result;
2811}
2812
2813template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002814inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002815std::pair<_ForwardIterator, _ForwardIterator>
2816minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2817{
Marshall Clow9d9463a2014-02-19 16:51:35 +00002818 return _VSTD::minmax_element(__first, __last,
2819 __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002820}
2821
Howard Hinnant98e5d972010-08-21 20:10:01 +00002822// minmax
2823
2824template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002825inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002826pair<const _Tp&, const _Tp&>
2827minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2828{
2829 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2830 pair<const _Tp&, const _Tp&>(__a, __b);
2831}
2832
2833template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002834inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002835pair<const _Tp&, const _Tp&>
2836minmax(const _Tp& __a, const _Tp& __b)
2837{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002838 return _VSTD::minmax(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002839}
2840
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002841#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002842
Howard Hinnant98e5d972010-08-21 20:10:01 +00002843template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002844inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002845pair<_Tp, _Tp>
2846minmax(initializer_list<_Tp> __t, _Compare __comp)
2847{
Marshall Clow9d9463a2014-02-19 16:51:35 +00002848 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2849 _Iter __first = __t.begin();
2850 _Iter __last = __t.end();
Marshall Clow3024f862015-02-11 15:41:34 +00002851 std::pair<_Tp, _Tp> __result(*__first, *__first);
Marshall Clow9d9463a2014-02-19 16:51:35 +00002852
2853 ++__first;
2854 if (__t.size() % 2 == 0)
2855 {
2856 if (__comp(*__first, __result.first))
2857 __result.first = *__first;
2858 else
2859 __result.second = *__first;
2860 ++__first;
2861 }
Aditya Kumarfdb4f172016-08-25 11:52:38 +00002862
Marshall Clow9d9463a2014-02-19 16:51:35 +00002863 while (__first != __last)
2864 {
2865 _Tp __prev = *__first++;
Marshall Clow3024f862015-02-11 15:41:34 +00002866 if (__comp(*__first, __prev)) {
2867 if ( __comp(*__first, __result.first)) __result.first = *__first;
2868 if (!__comp(__prev, __result.second)) __result.second = __prev;
Marshall Clow9d9463a2014-02-19 16:51:35 +00002869 }
2870 else {
Marshall Clow3024f862015-02-11 15:41:34 +00002871 if ( __comp(__prev, __result.first)) __result.first = __prev;
2872 if (!__comp(*__first, __result.second)) __result.second = *__first;
Marshall Clow9d9463a2014-02-19 16:51:35 +00002873 }
Aditya Kumarfdb4f172016-08-25 11:52:38 +00002874
Marshall Clow9d9463a2014-02-19 16:51:35 +00002875 __first++;
2876 }
2877 return __result;
2878}
2879
2880template<class _Tp>
2881inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2882pair<_Tp, _Tp>
2883minmax(initializer_list<_Tp> __t)
2884{
2885 return _VSTD::minmax(__t, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002886}
2887
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002888#endif // _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002889
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002890// random_shuffle
2891
Howard Hinnantc3267212010-05-26 17:49:34 +00002892// __independent_bits_engine
2893
Howard Hinnant99968442011-11-29 18:15:50 +00002894template <unsigned long long _Xp, size_t _Rp>
Howard Hinnantc3267212010-05-26 17:49:34 +00002895struct __log2_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002896{
Howard Hinnant99968442011-11-29 18:15:50 +00002897 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2898 : __log2_imp<_Xp, _Rp - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002899};
2900
Howard Hinnant99968442011-11-29 18:15:50 +00002901template <unsigned long long _Xp>
2902struct __log2_imp<_Xp, 0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002903{
Howard Hinnantc3267212010-05-26 17:49:34 +00002904 static const size_t value = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002905};
2906
Howard Hinnant99968442011-11-29 18:15:50 +00002907template <size_t _Rp>
2908struct __log2_imp<0, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002909{
Howard Hinnant99968442011-11-29 18:15:50 +00002910 static const size_t value = _Rp + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002911};
2912
Eric Fiselierac633a22017-05-31 21:20:18 +00002913template <class _UIntType, _UIntType _Xp>
Howard Hinnantc3267212010-05-26 17:49:34 +00002914struct __log2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002915{
Howard Hinnant99968442011-11-29 18:15:50 +00002916 static const size_t value = __log2_imp<_Xp,
Eric Fiselierac633a22017-05-31 21:20:18 +00002917 sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002918};
2919
Howard Hinnantc3267212010-05-26 17:49:34 +00002920template<class _Engine, class _UIntType>
2921class __independent_bits_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002922{
Howard Hinnantc3267212010-05-26 17:49:34 +00002923public:
2924 // types
2925 typedef _UIntType result_type;
2926
2927private:
2928 typedef typename _Engine::result_type _Engine_result_type;
2929 typedef typename conditional
2930 <
2931 sizeof(_Engine_result_type) <= sizeof(result_type),
2932 result_type,
2933 _Engine_result_type
2934 >::type _Working_result_type;
2935
2936 _Engine& __e_;
2937 size_t __w_;
2938 size_t __w0_;
2939 size_t __n_;
2940 size_t __n0_;
2941 _Working_result_type __y0_;
2942 _Working_result_type __y1_;
2943 _Engine_result_type __mask0_;
2944 _Engine_result_type __mask1_;
2945
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002946#ifdef _LIBCPP_CXX03_LANG
Howard Hinnant99968442011-11-29 18:15:50 +00002947 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002948 + _Working_result_type(1);
2949#else
2950 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2951 + _Working_result_type(1);
2952#endif
2953 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2954 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2955 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
Howard Hinnantc3267212010-05-26 17:49:34 +00002956
2957public:
2958 // constructors and seeding functions
2959 __independent_bits_engine(_Engine& __e, size_t __w);
2960
2961 // generating functions
Howard Hinnant99968442011-11-29 18:15:50 +00002962 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantc3267212010-05-26 17:49:34 +00002963
2964private:
Marshall Clow1e32db72017-09-20 19:38:43 +00002965 result_type __eval(false_type);
2966 result_type __eval(true_type);
Howard Hinnantc3267212010-05-26 17:49:34 +00002967};
2968
2969template<class _Engine, class _UIntType>
2970__independent_bits_engine<_Engine, _UIntType>
2971 ::__independent_bits_engine(_Engine& __e, size_t __w)
2972 : __e_(__e),
2973 __w_(__w)
2974{
2975 __n_ = __w_ / __m + (__w_ % __m != 0);
2976 __w0_ = __w_ / __n_;
Howard Hinnant99968442011-11-29 18:15:50 +00002977 if (_Rp == 0)
2978 __y0_ = _Rp;
Howard Hinnantc3267212010-05-26 17:49:34 +00002979 else if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00002980 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002981 else
2982 __y0_ = 0;
Howard Hinnant99968442011-11-29 18:15:50 +00002983 if (_Rp - __y0_ > __y0_ / __n_)
Howard Hinnantc3267212010-05-26 17:49:34 +00002984 {
2985 ++__n_;
2986 __w0_ = __w_ / __n_;
2987 if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00002988 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002989 else
2990 __y0_ = 0;
2991 }
2992 __n0_ = __n_ - __w_ % __n_;
2993 if (__w0_ < _WDt - 1)
Howard Hinnant99968442011-11-29 18:15:50 +00002994 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
Howard Hinnantc3267212010-05-26 17:49:34 +00002995 else
2996 __y1_ = 0;
2997 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2998 _Engine_result_type(0);
2999 __mask1_ = __w0_ < _EDt - 1 ?
3000 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
3001 _Engine_result_type(~0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003002}
3003
Howard Hinnantc3267212010-05-26 17:49:34 +00003004template<class _Engine, class _UIntType>
3005inline
3006_UIntType
Marshall Clow1e32db72017-09-20 19:38:43 +00003007__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003008{
Howard Hinnantc3267212010-05-26 17:49:34 +00003009 return static_cast<result_type>(__e_() & __mask0_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003010}
3011
Howard Hinnantc3267212010-05-26 17:49:34 +00003012template<class _Engine, class _UIntType>
3013_UIntType
Marshall Clow1e32db72017-09-20 19:38:43 +00003014__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003015{
Marshall Clowe2b68322017-09-20 17:34:11 +00003016 const size_t _WRt = numeric_limits<result_type>::digits;
Howard Hinnant99968442011-11-29 18:15:50 +00003017 result_type _Sp = 0;
Howard Hinnantc3267212010-05-26 17:49:34 +00003018 for (size_t __k = 0; __k < __n0_; ++__k)
3019 {
3020 _Engine_result_type __u;
3021 do
3022 {
3023 __u = __e_() - _Engine::min();
3024 } while (__u >= __y0_);
Marshall Clowe2b68322017-09-20 17:34:11 +00003025 if (__w0_ < _WRt)
Howard Hinnant99968442011-11-29 18:15:50 +00003026 _Sp <<= __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00003027 else
Howard Hinnant99968442011-11-29 18:15:50 +00003028 _Sp = 0;
3029 _Sp += __u & __mask0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00003030 }
3031 for (size_t __k = __n0_; __k < __n_; ++__k)
3032 {
3033 _Engine_result_type __u;
3034 do
3035 {
3036 __u = __e_() - _Engine::min();
3037 } while (__u >= __y1_);
Marshall Clowe2b68322017-09-20 17:34:11 +00003038 if (__w0_ < _WRt - 1)
Howard Hinnant99968442011-11-29 18:15:50 +00003039 _Sp <<= __w0_ + 1;
Howard Hinnantc3267212010-05-26 17:49:34 +00003040 else
Howard Hinnant99968442011-11-29 18:15:50 +00003041 _Sp = 0;
3042 _Sp += __u & __mask1_;
Howard Hinnantc3267212010-05-26 17:49:34 +00003043 }
Howard Hinnant99968442011-11-29 18:15:50 +00003044 return _Sp;
Howard Hinnantc3267212010-05-26 17:49:34 +00003045}
3046
3047// uniform_int_distribution
3048
3049template<class _IntType = int>
3050class uniform_int_distribution
3051{
3052public:
3053 // types
3054 typedef _IntType result_type;
3055
3056 class param_type
3057 {
3058 result_type __a_;
3059 result_type __b_;
3060 public:
3061 typedef uniform_int_distribution distribution_type;
3062
3063 explicit param_type(result_type __a = 0,
3064 result_type __b = numeric_limits<result_type>::max())
3065 : __a_(__a), __b_(__b) {}
3066
3067 result_type a() const {return __a_;}
3068 result_type b() const {return __b_;}
3069
3070 friend bool operator==(const param_type& __x, const param_type& __y)
3071 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3072 friend bool operator!=(const param_type& __x, const param_type& __y)
3073 {return !(__x == __y);}
3074 };
3075
3076private:
3077 param_type __p_;
3078
3079public:
3080 // constructors and reset functions
3081 explicit uniform_int_distribution(result_type __a = 0,
3082 result_type __b = numeric_limits<result_type>::max())
3083 : __p_(param_type(__a, __b)) {}
3084 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
3085 void reset() {}
3086
3087 // generating functions
3088 template<class _URNG> result_type operator()(_URNG& __g)
3089 {return (*this)(__g, __p_);}
3090 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3091
3092 // property functions
3093 result_type a() const {return __p_.a();}
3094 result_type b() const {return __p_.b();}
3095
3096 param_type param() const {return __p_;}
3097 void param(const param_type& __p) {__p_ = __p;}
3098
3099 result_type min() const {return a();}
3100 result_type max() const {return b();}
3101
3102 friend bool operator==(const uniform_int_distribution& __x,
3103 const uniform_int_distribution& __y)
3104 {return __x.__p_ == __y.__p_;}
3105 friend bool operator!=(const uniform_int_distribution& __x,
3106 const uniform_int_distribution& __y)
3107 {return !(__x == __y);}
3108};
3109
3110template<class _IntType>
3111template<class _URNG>
3112typename uniform_int_distribution<_IntType>::result_type
3113uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3114{
3115 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3116 uint32_t, uint64_t>::type _UIntType;
Howard Hinnant99968442011-11-29 18:15:50 +00003117 const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
3118 if (_Rp == 1)
Howard Hinnantc3267212010-05-26 17:49:34 +00003119 return __p.a();
3120 const size_t _Dt = numeric_limits<_UIntType>::digits;
3121 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
Howard Hinnant99968442011-11-29 18:15:50 +00003122 if (_Rp == 0)
Howard Hinnantc3267212010-05-26 17:49:34 +00003123 return static_cast<result_type>(_Eng(__g, _Dt)());
Howard Hinnant99968442011-11-29 18:15:50 +00003124 size_t __w = _Dt - __clz(_Rp) - 1;
Marshall Clow0934c752015-07-30 18:26:34 +00003125 if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
Howard Hinnantc3267212010-05-26 17:49:34 +00003126 ++__w;
3127 _Eng __e(__g, __w);
3128 _UIntType __u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003129 do
Howard Hinnantc3267212010-05-26 17:49:34 +00003130 {
3131 __u = __e();
Howard Hinnant99968442011-11-29 18:15:50 +00003132 } while (__u >= _Rp);
Howard Hinnantc3267212010-05-26 17:49:34 +00003133 return static_cast<result_type>(__u + __p.a());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003134}
3135
Eric Fiselier6547dc82017-04-03 23:23:44 +00003136#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \
3137 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003138class _LIBCPP_TYPE_VIS __rs_default;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003139
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003140_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantc3267212010-05-26 17:49:34 +00003141
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003142class _LIBCPP_TYPE_VIS __rs_default
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003143{
Howard Hinnantc3267212010-05-26 17:49:34 +00003144 static unsigned __c_;
3145
3146 __rs_default();
3147public:
Marshall Clow5920cfc2013-02-07 22:12:02 +00003148 typedef uint_fast32_t result_type;
Howard Hinnantc3267212010-05-26 17:49:34 +00003149
3150 static const result_type _Min = 0;
3151 static const result_type _Max = 0xFFFFFFFF;
3152
3153 __rs_default(const __rs_default&);
3154 ~__rs_default();
3155
3156 result_type operator()();
3157
Howard Hinnant27b4fd32012-04-02 00:40:41 +00003158 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
3159 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
Howard Hinnantc3267212010-05-26 17:49:34 +00003160
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003161 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003162};
3163
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003164_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003165
3166template <class _RandomAccessIterator>
3167void
3168random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
3169{
3170 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnant99968442011-11-29 18:15:50 +00003171 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3172 typedef typename _Dp::param_type _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003173 difference_type __d = __last - __first;
3174 if (__d > 1)
3175 {
Howard Hinnant99968442011-11-29 18:15:50 +00003176 _Dp __uid;
Howard Hinnantc3267212010-05-26 17:49:34 +00003177 __rs_default __g = __rs_get();
3178 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003179 {
Howard Hinnant99968442011-11-29 18:15:50 +00003180 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant4e599482010-10-22 15:26:39 +00003181 if (__i != difference_type(0))
3182 swap(*__first, *(__first + __i));
3183 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003184 }
3185}
3186
3187template <class _RandomAccessIterator, class _RandomNumberGenerator>
3188void
3189random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00003190#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003191 _RandomNumberGenerator&& __rand)
3192#else
3193 _RandomNumberGenerator& __rand)
3194#endif
3195{
3196 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3197 difference_type __d = __last - __first;
3198 if (__d > 1)
3199 {
3200 for (--__last; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003201 {
3202 difference_type __i = __rand(__d);
3203 swap(*__first, *(__first + __i));
3204 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003205 }
3206}
Marshall Clow03b862f2017-03-23 13:43:37 +00003207#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003208
Eric Fiselier917af0a2016-08-28 22:14:37 +00003209template <class _PopulationIterator, class _SampleIterator, class _Distance,
3210 class _UniformRandomNumberGenerator>
3211_LIBCPP_INLINE_VISIBILITY
3212_SampleIterator __sample(_PopulationIterator __first,
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003213 _PopulationIterator __last, _SampleIterator __output,
Eric Fiselier917af0a2016-08-28 22:14:37 +00003214 _Distance __n,
3215 _UniformRandomNumberGenerator & __g,
3216 input_iterator_tag) {
3217
3218 _Distance __k = 0;
3219 for (; __first != __last && __k < __n; ++__first, (void)++__k)
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003220 __output[__k] = *__first;
Eric Fiselier917af0a2016-08-28 22:14:37 +00003221 _Distance __sz = __k;
3222 for (; __first != __last; ++__first, (void)++__k) {
3223 _Distance __r = _VSTD::uniform_int_distribution<_Distance>(0, __k)(__g);
3224 if (__r < __sz)
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003225 __output[__r] = *__first;
Eric Fiselier917af0a2016-08-28 22:14:37 +00003226 }
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003227 return __output + _VSTD::min(__n, __k);
Eric Fiselier917af0a2016-08-28 22:14:37 +00003228}
3229
3230template <class _PopulationIterator, class _SampleIterator, class _Distance,
3231 class _UniformRandomNumberGenerator>
3232_LIBCPP_INLINE_VISIBILITY
3233_SampleIterator __sample(_PopulationIterator __first,
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003234 _PopulationIterator __last, _SampleIterator __output,
Eric Fiselier917af0a2016-08-28 22:14:37 +00003235 _Distance __n,
3236 _UniformRandomNumberGenerator& __g,
3237 forward_iterator_tag) {
3238 _Distance __unsampled_sz = _VSTD::distance(__first, __last);
3239 for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
3240 _Distance __r =
3241 _VSTD::uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
3242 if (__r < __n) {
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003243 *__output++ = *__first;
Eric Fiselier917af0a2016-08-28 22:14:37 +00003244 --__n;
3245 }
3246 }
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003247 return __output;
Eric Fiselier917af0a2016-08-28 22:14:37 +00003248}
3249
3250template <class _PopulationIterator, class _SampleIterator, class _Distance,
3251 class _UniformRandomNumberGenerator>
3252_LIBCPP_INLINE_VISIBILITY
3253_SampleIterator __sample(_PopulationIterator __first,
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003254 _PopulationIterator __last, _SampleIterator __output,
Eric Fiselier917af0a2016-08-28 22:14:37 +00003255 _Distance __n, _UniformRandomNumberGenerator& __g) {
3256 typedef typename iterator_traits<_PopulationIterator>::iterator_category
3257 _PopCategory;
3258 typedef typename iterator_traits<_PopulationIterator>::difference_type
3259 _Difference;
3260 static_assert(__is_forward_iterator<_PopulationIterator>::value ||
3261 __is_random_access_iterator<_SampleIterator>::value,
3262 "SampleIterator must meet the requirements of RandomAccessIterator");
3263 typedef typename common_type<_Distance, _Difference>::type _CommonType;
3264 _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
3265 return _VSTD::__sample(
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003266 __first, __last, __output, _CommonType(__n),
Eric Fiselier917af0a2016-08-28 22:14:37 +00003267 __g, _PopCategory());
3268}
3269
3270#if _LIBCPP_STD_VER > 14
3271template <class _PopulationIterator, class _SampleIterator, class _Distance,
3272 class _UniformRandomNumberGenerator>
3273inline _LIBCPP_INLINE_VISIBILITY
3274_SampleIterator sample(_PopulationIterator __first,
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003275 _PopulationIterator __last, _SampleIterator __output,
Eric Fiselier917af0a2016-08-28 22:14:37 +00003276 _Distance __n, _UniformRandomNumberGenerator&& __g) {
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003277 return _VSTD::__sample(__first, __last, __output, __n, __g);
Eric Fiselier917af0a2016-08-28 22:14:37 +00003278}
3279#endif // _LIBCPP_STD_VER > 14
3280
Howard Hinnantc3267212010-05-26 17:49:34 +00003281template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3282 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00003283#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant278bf2d2010-11-18 01:47:02 +00003284 _UniformRandomNumberGenerator&& __g)
3285#else
Howard Hinnantc3267212010-05-26 17:49:34 +00003286 _UniformRandomNumberGenerator& __g)
Howard Hinnant278bf2d2010-11-18 01:47:02 +00003287#endif
Howard Hinnantc3267212010-05-26 17:49:34 +00003288{
3289 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnant99968442011-11-29 18:15:50 +00003290 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3291 typedef typename _Dp::param_type _Pp;
Howard Hinnantc3267212010-05-26 17:49:34 +00003292 difference_type __d = __last - __first;
3293 if (__d > 1)
3294 {
Howard Hinnant99968442011-11-29 18:15:50 +00003295 _Dp __uid;
Howard Hinnantc3267212010-05-26 17:49:34 +00003296 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003297 {
Howard Hinnant99968442011-11-29 18:15:50 +00003298 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant4e599482010-10-22 15:26:39 +00003299 if (__i != difference_type(0))
3300 swap(*__first, *(__first + __i));
3301 }
Howard Hinnantc3267212010-05-26 17:49:34 +00003302 }
3303}
3304
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003305template <class _InputIterator, class _Predicate>
3306bool
3307is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3308{
3309 for (; __first != __last; ++__first)
3310 if (!__pred(*__first))
3311 break;
Marshall Clowa0ec4b72015-02-02 18:16:35 +00003312 if ( __first == __last )
3313 return true;
3314 ++__first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003315 for (; __first != __last; ++__first)
3316 if (__pred(*__first))
3317 return false;
3318 return true;
3319}
3320
3321// partition
3322
3323template <class _Predicate, class _ForwardIterator>
3324_ForwardIterator
3325__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3326{
3327 while (true)
3328 {
3329 if (__first == __last)
3330 return __first;
3331 if (!__pred(*__first))
3332 break;
3333 ++__first;
3334 }
3335 for (_ForwardIterator __p = __first; ++__p != __last;)
3336 {
3337 if (__pred(*__p))
3338 {
3339 swap(*__first, *__p);
3340 ++__first;
3341 }
3342 }
3343 return __first;
3344}
3345
3346template <class _Predicate, class _BidirectionalIterator>
3347_BidirectionalIterator
3348__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3349 bidirectional_iterator_tag)
3350{
3351 while (true)
3352 {
3353 while (true)
3354 {
3355 if (__first == __last)
3356 return __first;
3357 if (!__pred(*__first))
3358 break;
3359 ++__first;
3360 }
3361 do
3362 {
3363 if (__first == --__last)
3364 return __first;
3365 } while (!__pred(*__last));
3366 swap(*__first, *__last);
3367 ++__first;
3368 }
3369}
3370
3371template <class _ForwardIterator, class _Predicate>
3372inline _LIBCPP_INLINE_VISIBILITY
3373_ForwardIterator
3374partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3375{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003376 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003377 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3378}
3379
3380// partition_copy
3381
3382template <class _InputIterator, class _OutputIterator1,
3383 class _OutputIterator2, class _Predicate>
3384pair<_OutputIterator1, _OutputIterator2>
3385partition_copy(_InputIterator __first, _InputIterator __last,
3386 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3387 _Predicate __pred)
3388{
3389 for (; __first != __last; ++__first)
3390 {
3391 if (__pred(*__first))
3392 {
3393 *__out_true = *__first;
3394 ++__out_true;
3395 }
3396 else
3397 {
3398 *__out_false = *__first;
3399 ++__out_false;
3400 }
3401 }
3402 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3403}
3404
3405// partition_point
3406
3407template<class _ForwardIterator, class _Predicate>
3408_ForwardIterator
3409partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3410{
3411 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003412 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003413 while (__len != 0)
3414 {
3415 difference_type __l2 = __len / 2;
3416 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003417 _VSTD::advance(__m, __l2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003418 if (__pred(*__m))
3419 {
3420 __first = ++__m;
3421 __len -= __l2 + 1;
3422 }
3423 else
3424 __len = __l2;
3425 }
3426 return __first;
3427}
3428
3429// stable_partition
3430
3431template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3432_ForwardIterator
3433__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3434 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3435{
3436 // *__first is known to be false
3437 // __len >= 1
3438 if (__len == 1)
3439 return __first;
3440 if (__len == 2)
3441 {
3442 _ForwardIterator __m = __first;
3443 if (__pred(*++__m))
3444 {
3445 swap(*__first, *__m);
3446 return __m;
3447 }
3448 return __first;
3449 }
3450 if (__len <= __p.second)
3451 { // The buffer is big enough to use
3452 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3453 __destruct_n __d(0);
3454 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3455 // Move the falses into the temporary buffer, and the trues to the front of the line
3456 // Update __first to always point to the end of the trues
3457 value_type* __t = __p.first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003458 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003459 __d.__incr((value_type*)0);
3460 ++__t;
3461 _ForwardIterator __i = __first;
3462 while (++__i != __last)
3463 {
3464 if (__pred(*__i))
3465 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003466 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003467 ++__first;
3468 }
3469 else
3470 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003471 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003472 __d.__incr((value_type*)0);
3473 ++__t;
3474 }
3475 }
3476 // All trues now at start of range, all falses in buffer
3477 // Move falses back into range, but don't mess up __first which points to first false
3478 __i = __first;
3479 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003480 *__i = _VSTD::move(*__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003481 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3482 return __first;
3483 }
3484 // Else not enough buffer, do in place
3485 // __len >= 3
3486 _ForwardIterator __m = __first;
3487 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003488 _VSTD::advance(__m, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003489 // recurse on [__first, __m), *__first know to be false
3490 // F?????????????????
3491 // f m l
3492 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3493 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3494 // TTTFFFFF??????????
3495 // f ff m l
3496 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3497 _ForwardIterator __m1 = __m;
3498 _ForwardIterator __second_false = __last;
3499 _Distance __len_half = __len - __len2;
3500 while (__pred(*__m1))
3501 {
3502 if (++__m1 == __last)
3503 goto __second_half_done;
3504 --__len_half;
3505 }
3506 // TTTFFFFFTTTF??????
3507 // f ff m m1 l
3508 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3509__second_half_done:
3510 // TTTFFFFFTTTTTFFFFF
3511 // f ff m sf l
Howard Hinnant0949eed2011-06-30 21:18:19 +00003512 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003513 // TTTTTTTTFFFFFFFFFF
3514 // |
3515}
3516
3517struct __return_temporary_buffer
3518{
3519 template <class _Tp>
Howard Hinnant0949eed2011-06-30 21:18:19 +00003520 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003521};
3522
3523template <class _Predicate, class _ForwardIterator>
3524_ForwardIterator
3525__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3526 forward_iterator_tag)
3527{
3528 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3529 // Either prove all true and return __first or point to first false
3530 while (true)
3531 {
3532 if (__first == __last)
3533 return __first;
3534 if (!__pred(*__first))
3535 break;
3536 ++__first;
3537 }
3538 // We now have a reduced range [__first, __last)
3539 // *__first is known to be false
3540 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3541 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003542 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003543 pair<value_type*, ptrdiff_t> __p(0, 0);
3544 unique_ptr<value_type, __return_temporary_buffer> __h;
3545 if (__len >= __alloc_limit)
3546 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003547 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003548 __h.reset(__p.first);
3549 }
3550 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3551 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3552}
3553
3554template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3555_BidirectionalIterator
3556__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3557 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3558{
3559 // *__first is known to be false
3560 // *__last is known to be true
3561 // __len >= 2
3562 if (__len == 2)
3563 {
3564 swap(*__first, *__last);
3565 return __last;
3566 }
3567 if (__len == 3)
3568 {
3569 _BidirectionalIterator __m = __first;
3570 if (__pred(*++__m))
3571 {
3572 swap(*__first, *__m);
3573 swap(*__m, *__last);
3574 return __last;
3575 }
3576 swap(*__m, *__last);
3577 swap(*__first, *__m);
3578 return __m;
3579 }
3580 if (__len <= __p.second)
3581 { // The buffer is big enough to use
3582 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3583 __destruct_n __d(0);
3584 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3585 // Move the falses into the temporary buffer, and the trues to the front of the line
3586 // Update __first to always point to the end of the trues
3587 value_type* __t = __p.first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003588 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003589 __d.__incr((value_type*)0);
3590 ++__t;
3591 _BidirectionalIterator __i = __first;
3592 while (++__i != __last)
3593 {
3594 if (__pred(*__i))
3595 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003596 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003597 ++__first;
3598 }
3599 else
3600 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003601 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003602 __d.__incr((value_type*)0);
3603 ++__t;
3604 }
3605 }
3606 // move *__last, known to be true
Howard Hinnant0949eed2011-06-30 21:18:19 +00003607 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003608 __i = ++__first;
3609 // All trues now at start of range, all falses in buffer
3610 // Move falses back into range, but don't mess up __first which points to first false
3611 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003612 *__i = _VSTD::move(*__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003613 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3614 return __first;
3615 }
3616 // Else not enough buffer, do in place
3617 // __len >= 4
3618 _BidirectionalIterator __m = __first;
3619 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003620 _VSTD::advance(__m, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003621 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3622 // F????????????????T
3623 // f m l
3624 _BidirectionalIterator __m1 = __m;
3625 _BidirectionalIterator __first_false = __first;
3626 _Distance __len_half = __len2;
3627 while (!__pred(*--__m1))
3628 {
3629 if (__m1 == __first)
3630 goto __first_half_done;
3631 --__len_half;
3632 }
3633 // F???TFFF?????????T
3634 // f m1 m l
3635 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3636 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3637__first_half_done:
3638 // TTTFFFFF?????????T
3639 // f ff m l
3640 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3641 __m1 = __m;
3642 _BidirectionalIterator __second_false = __last;
3643 ++__second_false;
3644 __len_half = __len - __len2;
3645 while (__pred(*__m1))
3646 {
3647 if (++__m1 == __last)
3648 goto __second_half_done;
3649 --__len_half;
3650 }
3651 // TTTFFFFFTTTF?????T
3652 // f ff m m1 l
3653 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3654__second_half_done:
3655 // TTTFFFFFTTTTTFFFFF
3656 // f ff m sf l
Howard Hinnant0949eed2011-06-30 21:18:19 +00003657 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003658 // TTTTTTTTFFFFFFFFFF
3659 // |
3660}
3661
3662template <class _Predicate, class _BidirectionalIterator>
3663_BidirectionalIterator
3664__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3665 bidirectional_iterator_tag)
3666{
3667 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3668 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3669 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3670 // Either prove all true and return __first or point to first false
3671 while (true)
3672 {
3673 if (__first == __last)
3674 return __first;
3675 if (!__pred(*__first))
3676 break;
3677 ++__first;
3678 }
3679 // __first points to first false, everything prior to __first is already set.
3680 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3681 do
3682 {
3683 if (__first == --__last)
3684 return __first;
3685 } while (!__pred(*__last));
3686 // We now have a reduced range [__first, __last]
3687 // *__first is known to be false
3688 // *__last is known to be true
3689 // __len >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003690 difference_type __len = _VSTD::distance(__first, __last) + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003691 pair<value_type*, ptrdiff_t> __p(0, 0);
3692 unique_ptr<value_type, __return_temporary_buffer> __h;
3693 if (__len >= __alloc_limit)
3694 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003695 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003696 __h.reset(__p.first);
3697 }
3698 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3699 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3700}
3701
3702template <class _ForwardIterator, class _Predicate>
3703inline _LIBCPP_INLINE_VISIBILITY
3704_ForwardIterator
3705stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3706{
3707 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3708 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3709}
3710
3711// is_sorted_until
3712
3713template <class _ForwardIterator, class _Compare>
3714_ForwardIterator
3715is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3716{
3717 if (__first != __last)
3718 {
3719 _ForwardIterator __i = __first;
3720 while (++__i != __last)
3721 {
3722 if (__comp(*__i, *__first))
3723 return __i;
3724 __first = __i;
3725 }
3726 }
3727 return __last;
3728}
3729
Howard Hinnant324bb032010-08-22 00:02:43 +00003730template<class _ForwardIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003731inline _LIBCPP_INLINE_VISIBILITY
3732_ForwardIterator
3733is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3734{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003735 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003736}
3737
3738// is_sorted
3739
3740template <class _ForwardIterator, class _Compare>
3741inline _LIBCPP_INLINE_VISIBILITY
3742bool
3743is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3744{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003745 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003746}
3747
Howard Hinnant324bb032010-08-22 00:02:43 +00003748template<class _ForwardIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003749inline _LIBCPP_INLINE_VISIBILITY
3750bool
3751is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3752{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003753 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003754}
3755
3756// sort
3757
3758// stable, 2-3 compares, 0-2 swaps
3759
3760template <class _Compare, class _ForwardIterator>
3761unsigned
3762__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3763{
3764 unsigned __r = 0;
3765 if (!__c(*__y, *__x)) // if x <= y
3766 {
3767 if (!__c(*__z, *__y)) // if y <= z
3768 return __r; // x <= y && y <= z
3769 // x <= y && y > z
3770 swap(*__y, *__z); // x <= z && y < z
3771 __r = 1;
3772 if (__c(*__y, *__x)) // if x > y
3773 {
3774 swap(*__x, *__y); // x < y && y <= z
3775 __r = 2;
3776 }
3777 return __r; // x <= y && y < z
3778 }
3779 if (__c(*__z, *__y)) // x > y, if y > z
3780 {
3781 swap(*__x, *__z); // x < y && y < z
3782 __r = 1;
3783 return __r;
3784 }
3785 swap(*__x, *__y); // x > y && y <= z
3786 __r = 1; // x < y && x <= z
3787 if (__c(*__z, *__y)) // if y > z
3788 {
3789 swap(*__y, *__z); // x <= y && y < z
3790 __r = 2;
3791 }
3792 return __r;
3793} // x <= y && y <= z
3794
3795// stable, 3-6 compares, 0-5 swaps
3796
3797template <class _Compare, class _ForwardIterator>
3798unsigned
3799__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3800 _ForwardIterator __x4, _Compare __c)
3801{
3802 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3803 if (__c(*__x4, *__x3))
3804 {
3805 swap(*__x3, *__x4);
3806 ++__r;
3807 if (__c(*__x3, *__x2))
3808 {
3809 swap(*__x2, *__x3);
3810 ++__r;
3811 if (__c(*__x2, *__x1))
3812 {
3813 swap(*__x1, *__x2);
3814 ++__r;
3815 }
3816 }
3817 }
3818 return __r;
3819}
3820
3821// stable, 4-10 compares, 0-9 swaps
3822
3823template <class _Compare, class _ForwardIterator>
3824unsigned
3825__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3826 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3827{
3828 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3829 if (__c(*__x5, *__x4))
3830 {
3831 swap(*__x4, *__x5);
3832 ++__r;
3833 if (__c(*__x4, *__x3))
3834 {
3835 swap(*__x3, *__x4);
3836 ++__r;
3837 if (__c(*__x3, *__x2))
3838 {
3839 swap(*__x2, *__x3);
3840 ++__r;
3841 if (__c(*__x2, *__x1))
3842 {
3843 swap(*__x1, *__x2);
3844 ++__r;
3845 }
3846 }
3847 }
3848 }
3849 return __r;
3850}
3851
3852// Assumes size > 0
3853template <class _Compare, class _BirdirectionalIterator>
3854void
3855__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3856{
3857 _BirdirectionalIterator __lm1 = __last;
3858 for (--__lm1; __first != __lm1; ++__first)
3859 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003860 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003861 typename add_lvalue_reference<_Compare>::type>
3862 (__first, __last, __comp);
3863 if (__i != __first)
3864 swap(*__first, *__i);
3865 }
3866}
3867
3868template <class _Compare, class _BirdirectionalIterator>
3869void
3870__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3871{
3872 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3873 if (__first != __last)
3874 {
3875 _BirdirectionalIterator __i = __first;
3876 for (++__i; __i != __last; ++__i)
3877 {
3878 _BirdirectionalIterator __j = __i;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003879 value_type __t(_VSTD::move(*__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003880 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003881 *__j = _VSTD::move(*__k);
3882 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003883 }
3884 }
3885}
3886
3887template <class _Compare, class _RandomAccessIterator>
3888void
3889__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3890{
3891 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3892 _RandomAccessIterator __j = __first+2;
3893 __sort3<_Compare>(__first, __first+1, __j, __comp);
3894 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3895 {
3896 if (__comp(*__i, *__j))
3897 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003898 value_type __t(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003899 _RandomAccessIterator __k = __j;
3900 __j = __i;
3901 do
3902 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003903 *__j = _VSTD::move(*__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003904 __j = __k;
3905 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003906 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003907 }
3908 __j = __i;
3909 }
3910}
3911
3912template <class _Compare, class _RandomAccessIterator>
3913bool
3914__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3915{
3916 switch (__last - __first)
3917 {
3918 case 0:
3919 case 1:
3920 return true;
3921 case 2:
3922 if (__comp(*--__last, *__first))
3923 swap(*__first, *__last);
3924 return true;
3925 case 3:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003926 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003927 return true;
3928 case 4:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003929 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003930 return true;
3931 case 5:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003932 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003933 return true;
3934 }
3935 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3936 _RandomAccessIterator __j = __first+2;
3937 __sort3<_Compare>(__first, __first+1, __j, __comp);
3938 const unsigned __limit = 8;
3939 unsigned __count = 0;
3940 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3941 {
3942 if (__comp(*__i, *__j))
3943 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003944 value_type __t(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003945 _RandomAccessIterator __k = __j;
3946 __j = __i;
3947 do
3948 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003949 *__j = _VSTD::move(*__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003950 __j = __k;
3951 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003952 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003953 if (++__count == __limit)
3954 return ++__i == __last;
3955 }
3956 __j = __i;
3957 }
3958 return true;
3959}
3960
3961template <class _Compare, class _BirdirectionalIterator>
3962void
3963__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3964 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3965{
3966 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3967 if (__first1 != __last1)
3968 {
3969 __destruct_n __d(0);
3970 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3971 value_type* __last2 = __first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003972 ::new(__last2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003973 __d.__incr((value_type*)0);
3974 for (++__last2; ++__first1 != __last1; ++__last2)
3975 {
3976 value_type* __j2 = __last2;
3977 value_type* __i2 = __j2;
3978 if (__comp(*__first1, *--__i2))
3979 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003980 ::new(__j2) value_type(_VSTD::move(*__i2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003981 __d.__incr((value_type*)0);
3982 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003983 *__j2 = _VSTD::move(*__i2);
3984 *__j2 = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003985 }
3986 else
3987 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003988 ::new(__j2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003989 __d.__incr((value_type*)0);
3990 }
3991 }
3992 __h.release();
3993 }
3994}
3995
3996template <class _Compare, class _RandomAccessIterator>
3997void
3998__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3999{
4000 // _Compare is known to be a reference type
4001 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4002 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant1468b662010-11-19 22:17:28 +00004003 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
4004 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004005 while (true)
4006 {
4007 __restart:
4008 difference_type __len = __last - __first;
4009 switch (__len)
4010 {
4011 case 0:
4012 case 1:
4013 return;
4014 case 2:
4015 if (__comp(*--__last, *__first))
4016 swap(*__first, *__last);
4017 return;
4018 case 3:
Howard Hinnant0949eed2011-06-30 21:18:19 +00004019 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004020 return;
4021 case 4:
Howard Hinnant0949eed2011-06-30 21:18:19 +00004022 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004023 return;
4024 case 5:
Howard Hinnant0949eed2011-06-30 21:18:19 +00004025 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004026 return;
4027 }
4028 if (__len <= __limit)
4029 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004030 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004031 return;
4032 }
4033 // __len > 5
4034 _RandomAccessIterator __m = __first;
4035 _RandomAccessIterator __lm1 = __last;
4036 --__lm1;
4037 unsigned __n_swaps;
4038 {
4039 difference_type __delta;
4040 if (__len >= 1000)
4041 {
4042 __delta = __len/2;
4043 __m += __delta;
4044 __delta /= 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004045 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004046 }
4047 else
4048 {
4049 __delta = __len/2;
4050 __m += __delta;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004051 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004052 }
4053 }
4054 // *__m is median
4055 // partition [__first, __m) < *__m and *__m <= [__m, __last)
4056 // (this inhibits tossing elements equivalent to __m around unnecessarily)
4057 _RandomAccessIterator __i = __first;
4058 _RandomAccessIterator __j = __lm1;
4059 // j points beyond range to be tested, *__m is known to be <= *__lm1
4060 // The search going up is known to be guarded but the search coming down isn't.
4061 // Prime the downward search with a guard.
4062 if (!__comp(*__i, *__m)) // if *__first == *__m
4063 {
4064 // *__first == *__m, *__first doesn't go in first part
4065 // manually guard downward moving __j against __i
4066 while (true)
4067 {
4068 if (__i == --__j)
4069 {
4070 // *__first == *__m, *__m <= all other elements
4071 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
4072 ++__i; // __first + 1
4073 __j = __last;
4074 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
4075 {
4076 while (true)
4077 {
4078 if (__i == __j)
4079 return; // [__first, __last) all equivalent elements
4080 if (__comp(*__first, *__i))
4081 {
4082 swap(*__i, *__j);
4083 ++__n_swaps;
4084 ++__i;
4085 break;
4086 }
4087 ++__i;
4088 }
4089 }
4090 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
4091 if (__i == __j)
4092 return;
4093 while (true)
4094 {
4095 while (!__comp(*__first, *__i))
4096 ++__i;
4097 while (__comp(*__first, *--__j))
4098 ;
4099 if (__i >= __j)
4100 break;
4101 swap(*__i, *__j);
4102 ++__n_swaps;
4103 ++__i;
4104 }
4105 // [__first, __i) == *__first and *__first < [__i, __last)
4106 // The first part is sorted, sort the secod part
Howard Hinnant0949eed2011-06-30 21:18:19 +00004107 // _VSTD::__sort<_Compare>(__i, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004108 __first = __i;
4109 goto __restart;
4110 }
4111 if (__comp(*__j, *__m))
4112 {
4113 swap(*__i, *__j);
4114 ++__n_swaps;
4115 break; // found guard for downward moving __j, now use unguarded partition
4116 }
4117 }
4118 }
4119 // It is known that *__i < *__m
4120 ++__i;
4121 // j points beyond range to be tested, *__m is known to be <= *__lm1
4122 // if not yet partitioned...
4123 if (__i < __j)
4124 {
4125 // known that *(__i - 1) < *__m
4126 // known that __i <= __m
4127 while (true)
4128 {
4129 // __m still guards upward moving __i
4130 while (__comp(*__i, *__m))
4131 ++__i;
4132 // It is now known that a guard exists for downward moving __j
4133 while (!__comp(*--__j, *__m))
4134 ;
4135 if (__i > __j)
4136 break;
4137 swap(*__i, *__j);
4138 ++__n_swaps;
4139 // It is known that __m != __j
4140 // If __m just moved, follow it
4141 if (__m == __i)
4142 __m = __j;
4143 ++__i;
4144 }
4145 }
4146 // [__first, __i) < *__m and *__m <= [__i, __last)
4147 if (__i != __m && __comp(*__m, *__i))
4148 {
4149 swap(*__i, *__m);
4150 ++__n_swaps;
4151 }
4152 // [__first, __i) < *__i and *__i <= [__i+1, __last)
4153 // If we were given a perfect partition, see if insertion sort is quick...
4154 if (__n_swaps == 0)
4155 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004156 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
4157 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004158 {
4159 if (__fs)
4160 return;
4161 __last = __i;
4162 continue;
4163 }
4164 else
4165 {
4166 if (__fs)
4167 {
4168 __first = ++__i;
4169 continue;
4170 }
4171 }
4172 }
4173 // sort smaller range with recursive call and larger with tail recursion elimination
4174 if (__i - __first < __last - __i)
4175 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004176 _VSTD::__sort<_Compare>(__first, __i, __comp);
4177 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004178 __first = ++__i;
4179 }
4180 else
4181 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004182 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4183 // _VSTD::__sort<_Compare>(__first, __i, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004184 __last = __i;
4185 }
4186 }
4187}
4188
4189// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
4190template <class _RandomAccessIterator, class _Compare>
4191inline _LIBCPP_INLINE_VISIBILITY
4192void
4193sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4194{
Howard Hinnant5e571422013-08-23 20:10:18 +00004195#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004196 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4197 __debug_less<_Compare> __c(__comp);
4198 __sort<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004199#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004200 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4201 __sort<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004202#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004203}
4204
4205template <class _RandomAccessIterator>
4206inline _LIBCPP_INLINE_VISIBILITY
4207void
4208sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4209{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004210 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004211}
4212
4213template <class _Tp>
4214inline _LIBCPP_INLINE_VISIBILITY
4215void
4216sort(_Tp** __first, _Tp** __last)
4217{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004218 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004219}
4220
4221template <class _Tp>
4222inline _LIBCPP_INLINE_VISIBILITY
4223void
4224sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4225{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004226 _VSTD::sort(__first.base(), __last.base());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004227}
4228
Howard Hinnant7a563db2011-09-14 18:33:51 +00004229template <class _Tp, class _Compare>
4230inline _LIBCPP_INLINE_VISIBILITY
4231void
4232sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4233{
4234 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4235 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4236}
4237
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004238_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4239_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4240_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4241_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4242_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4243_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4244_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4245_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4246_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4247_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4248_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4249_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>&))
4250_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4251_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4252_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 +00004253
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004254_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4255_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4256_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4257_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4258_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4259_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4260_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4261_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4262_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4263_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4264_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4265_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>&))
4266_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4267_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4268_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 +00004269
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004270_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 +00004271
4272// lower_bound
4273
4274template <class _Compare, class _ForwardIterator, class _Tp>
4275_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004276__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004277{
4278 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004279 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004280 while (__len != 0)
4281 {
4282 difference_type __l2 = __len / 2;
4283 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004284 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004285 if (__comp(*__m, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004286 {
4287 __first = ++__m;
4288 __len -= __l2 + 1;
4289 }
4290 else
4291 __len = __l2;
4292 }
4293 return __first;
4294}
4295
4296template <class _ForwardIterator, class _Tp, class _Compare>
4297inline _LIBCPP_INLINE_VISIBILITY
4298_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004299lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004300{
Howard Hinnant5e571422013-08-23 20:10:18 +00004301#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004302 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4303 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004304 return __lower_bound<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004305#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004306 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004307 return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004308#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004309}
4310
4311template <class _ForwardIterator, class _Tp>
4312inline _LIBCPP_INLINE_VISIBILITY
4313_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004314lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004315{
Howard Hinnant78b68282011-10-22 20:59:45 +00004316 return _VSTD::lower_bound(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004317 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4318}
4319
4320// upper_bound
4321
4322template <class _Compare, class _ForwardIterator, class _Tp>
4323_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004324__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004325{
4326 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004327 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004328 while (__len != 0)
4329 {
4330 difference_type __l2 = __len / 2;
4331 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004332 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004333 if (__comp(__value_, *__m))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004334 __len = __l2;
4335 else
4336 {
4337 __first = ++__m;
4338 __len -= __l2 + 1;
4339 }
4340 }
4341 return __first;
4342}
4343
4344template <class _ForwardIterator, class _Tp, class _Compare>
4345inline _LIBCPP_INLINE_VISIBILITY
4346_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004347upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004348{
Howard Hinnant5e571422013-08-23 20:10:18 +00004349#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004350 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4351 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004352 return __upper_bound<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004353#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004354 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004355 return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004356#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004357}
4358
4359template <class _ForwardIterator, class _Tp>
4360inline _LIBCPP_INLINE_VISIBILITY
4361_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004362upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004363{
Howard Hinnant78b68282011-10-22 20:59:45 +00004364 return _VSTD::upper_bound(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004365 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4366}
4367
4368// equal_range
4369
4370template <class _Compare, class _ForwardIterator, class _Tp>
4371pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004372__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004373{
4374 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004375 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004376 while (__len != 0)
4377 {
4378 difference_type __l2 = __len / 2;
4379 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004380 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004381 if (__comp(*__m, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004382 {
4383 __first = ++__m;
4384 __len -= __l2 + 1;
4385 }
Howard Hinnant78b68282011-10-22 20:59:45 +00004386 else if (__comp(__value_, *__m))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004387 {
4388 __last = __m;
4389 __len = __l2;
4390 }
4391 else
4392 {
4393 _ForwardIterator __mp1 = __m;
4394 return pair<_ForwardIterator, _ForwardIterator>
4395 (
Howard Hinnant78b68282011-10-22 20:59:45 +00004396 __lower_bound<_Compare>(__first, __m, __value_, __comp),
4397 __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004398 );
4399 }
4400 }
4401 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4402}
4403
4404template <class _ForwardIterator, class _Tp, class _Compare>
4405inline _LIBCPP_INLINE_VISIBILITY
4406pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004407equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004408{
Howard Hinnant5e571422013-08-23 20:10:18 +00004409#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004410 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4411 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004412 return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004413#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004414 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004415 return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004416#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004417}
4418
4419template <class _ForwardIterator, class _Tp>
4420inline _LIBCPP_INLINE_VISIBILITY
4421pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004422equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004423{
Howard Hinnant78b68282011-10-22 20:59:45 +00004424 return _VSTD::equal_range(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004425 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4426}
4427
4428// binary_search
4429
4430template <class _Compare, class _ForwardIterator, class _Tp>
4431inline _LIBCPP_INLINE_VISIBILITY
4432bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004433__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004434{
Howard Hinnant78b68282011-10-22 20:59:45 +00004435 __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4436 return __first != __last && !__comp(__value_, *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004437}
4438
4439template <class _ForwardIterator, class _Tp, class _Compare>
4440inline _LIBCPP_INLINE_VISIBILITY
4441bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004442binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004443{
Howard Hinnant5e571422013-08-23 20:10:18 +00004444#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004445 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4446 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004447 return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004448#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004449 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004450 return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004451#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004452}
4453
4454template <class _ForwardIterator, class _Tp>
4455inline _LIBCPP_INLINE_VISIBILITY
4456bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004457binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004458{
Howard Hinnant78b68282011-10-22 20:59:45 +00004459 return _VSTD::binary_search(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004460 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4461}
4462
4463// merge
4464
4465template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4466_OutputIterator
4467__merge(_InputIterator1 __first1, _InputIterator1 __last1,
4468 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4469{
4470 for (; __first1 != __last1; ++__result)
4471 {
4472 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004473 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004474 if (__comp(*__first2, *__first1))
4475 {
4476 *__result = *__first2;
4477 ++__first2;
4478 }
4479 else
4480 {
4481 *__result = *__first1;
4482 ++__first1;
4483 }
4484 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00004485 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004486}
4487
4488template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4489inline _LIBCPP_INLINE_VISIBILITY
4490_OutputIterator
4491merge(_InputIterator1 __first1, _InputIterator1 __last1,
4492 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4493{
Howard Hinnant5e571422013-08-23 20:10:18 +00004494#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004495 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4496 __debug_less<_Compare> __c(__comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004497 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004498#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004499 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004500 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004501#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004502}
4503
4504template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4505inline _LIBCPP_INLINE_VISIBILITY
4506_OutputIterator
4507merge(_InputIterator1 __first1, _InputIterator1 __last1,
4508 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4509{
4510 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4511 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4512 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4513}
4514
4515// inplace_merge
4516
Marshall Clowa3795762015-07-29 16:25:45 +00004517template <class _Compare, class _InputIterator1, class _InputIterator2,
4518 class _OutputIterator>
4519void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4520 _InputIterator2 __first2, _InputIterator2 __last2,
4521 _OutputIterator __result, _Compare __comp)
4522{
4523 for (; __first1 != __last1; ++__result)
4524 {
4525 if (__first2 == __last2)
4526 {
4527 _VSTD::move(__first1, __last1, __result);
4528 return;
4529 }
4530
4531 if (__comp(*__first2, *__first1))
4532 {
4533 *__result = _VSTD::move(*__first2);
4534 ++__first2;
4535 }
4536 else
4537 {
4538 *__result = _VSTD::move(*__first1);
4539 ++__first1;
4540 }
4541 }
4542 // __first2 through __last2 are already in the right spot.
4543}
4544
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004545template <class _Compare, class _BidirectionalIterator>
4546void
4547__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4548 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4549 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4550 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4551{
4552 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004553 __destruct_n __d(0);
4554 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4555 if (__len1 <= __len2)
4556 {
4557 value_type* __p = __buff;
Eric Fiselierb9919752014-10-27 19:28:20 +00004558 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004559 ::new(__p) value_type(_VSTD::move(*__i));
Marshall Clowa3795762015-07-29 16:25:45 +00004560 __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004561 }
4562 else
4563 {
4564 value_type* __p = __buff;
Eric Fiselierb9919752014-10-27 19:28:20 +00004565 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004566 ::new(__p) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004567 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4568 typedef reverse_iterator<value_type*> _Rv;
Aditya Kumarfdb4f172016-08-25 11:52:38 +00004569 __half_inplace_merge(_Rv(__p), _Rv(__buff),
Marshall Clowa3795762015-07-29 16:25:45 +00004570 _RBi(__middle), _RBi(__first),
Marshall Clow25a78dc2017-08-28 23:16:13 +00004571 _RBi(__last), __invert<_Compare>(__comp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004572 }
4573}
4574
4575template <class _Compare, class _BidirectionalIterator>
4576void
4577__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4578 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4579 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4580 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4581{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004582 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4583 while (true)
4584 {
4585 // if __middle == __last, we're done
4586 if (__len2 == 0)
4587 return;
Marshall Clowe809f4c2015-02-02 16:44:11 +00004588 if (__len1 <= __buff_size || __len2 <= __buff_size)
4589 return __buffered_inplace_merge<_Compare>
4590 (__first, __middle, __last, __comp, __len1, __len2, __buff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004591 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
Eric Fiselierb9919752014-10-27 19:28:20 +00004592 for (; true; ++__first, (void) --__len1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004593 {
4594 if (__len1 == 0)
4595 return;
4596 if (__comp(*__middle, *__first))
4597 break;
4598 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004599 // __first < __middle < __last
4600 // *__first > *__middle
4601 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4602 // all elements in:
4603 // [__first, __m1) <= [__middle, __m2)
4604 // [__middle, __m2) < [__m1, __middle)
4605 // [__m1, __middle) <= [__m2, __last)
4606 // and __m1 or __m2 is in the middle of its range
4607 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4608 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4609 difference_type __len11; // distance(__first, __m1)
4610 difference_type __len21; // distance(__middle, __m2)
4611 // binary search smaller range
4612 if (__len1 < __len2)
4613 { // __len >= 1, __len2 >= 2
4614 __len21 = __len2 / 2;
4615 __m2 = __middle;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004616 _VSTD::advance(__m2, __len21);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004617 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004618 __len11 = _VSTD::distance(__first, __m1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004619 }
4620 else
4621 {
4622 if (__len1 == 1)
4623 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4624 // It is known *__first > *__middle
4625 swap(*__first, *__middle);
4626 return;
4627 }
4628 // __len1 >= 2, __len2 >= 1
4629 __len11 = __len1 / 2;
4630 __m1 = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004631 _VSTD::advance(__m1, __len11);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004632 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004633 __len21 = _VSTD::distance(__middle, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004634 }
4635 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4636 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4637 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4638 // swap middle two partitions
Howard Hinnant0949eed2011-06-30 21:18:19 +00004639 __middle = _VSTD::rotate(__m1, __middle, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004640 // __len12 and __len21 now have swapped meanings
4641 // merge smaller range with recurisve call and larger with tail recursion elimination
4642 if (__len11 + __len21 < __len12 + __len22)
4643 {
4644 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4645// __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4646 __first = __middle;
4647 __middle = __m2;
4648 __len1 = __len12;
4649 __len2 = __len22;
4650 }
4651 else
4652 {
4653 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4654// __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4655 __last = __middle;
4656 __middle = __m1;
4657 __len1 = __len11;
4658 __len2 = __len21;
4659 }
4660 }
4661}
4662
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004663template <class _BidirectionalIterator, class _Compare>
4664inline _LIBCPP_INLINE_VISIBILITY
4665void
4666inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4667 _Compare __comp)
4668{
4669 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4670 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004671 difference_type __len1 = _VSTD::distance(__first, __middle);
4672 difference_type __len2 = _VSTD::distance(__middle, __last);
4673 difference_type __buf_size = _VSTD::min(__len1, __len2);
Marshall Clow4c2684c2015-02-02 17:35:53 +00004674 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4675 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
4676
Howard Hinnant5e571422013-08-23 20:10:18 +00004677#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004678 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4679 __debug_less<_Compare> __c(__comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004680 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004681 __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004682#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004683 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004684 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004685 __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004686#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004687}
4688
4689template <class _BidirectionalIterator>
4690inline _LIBCPP_INLINE_VISIBILITY
4691void
4692inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4693{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004694 _VSTD::inplace_merge(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004695 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4696}
4697
4698// stable_sort
4699
4700template <class _Compare, class _InputIterator1, class _InputIterator2>
4701void
4702__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4703 _InputIterator2 __first2, _InputIterator2 __last2,
4704 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4705{
4706 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4707 __destruct_n __d(0);
4708 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4709 for (; true; ++__result)
4710 {
4711 if (__first1 == __last1)
4712 {
4713 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
Howard Hinnant0949eed2011-06-30 21:18:19 +00004714 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004715 __h.release();
4716 return;
4717 }
4718 if (__first2 == __last2)
4719 {
4720 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
Howard Hinnant0949eed2011-06-30 21:18:19 +00004721 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004722 __h.release();
4723 return;
4724 }
4725 if (__comp(*__first2, *__first1))
4726 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004727 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004728 __d.__incr((value_type*)0);
4729 ++__first2;
4730 }
4731 else
4732 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004733 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004734 __d.__incr((value_type*)0);
4735 ++__first1;
4736 }
4737 }
4738}
4739
4740template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4741void
4742__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4743 _InputIterator2 __first2, _InputIterator2 __last2,
4744 _OutputIterator __result, _Compare __comp)
4745{
4746 for (; __first1 != __last1; ++__result)
4747 {
4748 if (__first2 == __last2)
4749 {
4750 for (; __first1 != __last1; ++__first1, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004751 *__result = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004752 return;
4753 }
4754 if (__comp(*__first2, *__first1))
4755 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004756 *__result = _VSTD::move(*__first2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004757 ++__first2;
4758 }
4759 else
4760 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004761 *__result = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004762 ++__first1;
4763 }
4764 }
4765 for (; __first2 != __last2; ++__first2, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004766 *__result = _VSTD::move(*__first2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004767}
4768
4769template <class _Compare, class _RandomAccessIterator>
4770void
4771__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4772 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4773 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4774
4775template <class _Compare, class _RandomAccessIterator>
4776void
4777__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4778 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4779 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4780{
4781 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4782 switch (__len)
4783 {
4784 case 0:
4785 return;
4786 case 1:
Howard Hinnant0949eed2011-06-30 21:18:19 +00004787 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004788 return;
4789 case 2:
4790 __destruct_n __d(0);
4791 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4792 if (__comp(*--__last1, *__first1))
4793 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004794 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004795 __d.__incr((value_type*)0);
4796 ++__first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004797 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004798 }
4799 else
4800 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004801 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004802 __d.__incr((value_type*)0);
4803 ++__first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004804 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004805 }
4806 __h2.release();
4807 return;
4808 }
4809 if (__len <= 8)
4810 {
4811 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4812 return;
4813 }
4814 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4815 _RandomAccessIterator __m = __first1 + __l2;
4816 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4817 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4818 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4819}
4820
4821template <class _Tp>
4822struct __stable_sort_switch
4823{
Howard Hinnant1468b662010-11-19 22:17:28 +00004824 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004825};
4826
4827template <class _Compare, class _RandomAccessIterator>
4828void
4829__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4830 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4831 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4832{
4833 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4834 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4835 switch (__len)
4836 {
4837 case 0:
4838 case 1:
4839 return;
4840 case 2:
4841 if (__comp(*--__last, *__first))
4842 swap(*__first, *__last);
4843 return;
4844 }
4845 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4846 {
4847 __insertion_sort<_Compare>(__first, __last, __comp);
4848 return;
4849 }
4850 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4851 _RandomAccessIterator __m = __first + __l2;
4852 if (__len <= __buff_size)
4853 {
4854 __destruct_n __d(0);
4855 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4856 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4857 __d.__set(__l2, (value_type*)0);
4858 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4859 __d.__set(__len, (value_type*)0);
4860 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4861// __merge<_Compare>(move_iterator<value_type*>(__buff),
4862// move_iterator<value_type*>(__buff + __l2),
4863// move_iterator<_RandomAccessIterator>(__buff + __l2),
4864// move_iterator<_RandomAccessIterator>(__buff + __len),
4865// __first, __comp);
4866 return;
4867 }
4868 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4869 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4870 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4871}
4872
4873template <class _RandomAccessIterator, class _Compare>
4874inline _LIBCPP_INLINE_VISIBILITY
4875void
4876stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4877{
4878 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4879 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4880 difference_type __len = __last - __first;
4881 pair<value_type*, ptrdiff_t> __buf(0, 0);
4882 unique_ptr<value_type, __return_temporary_buffer> __h;
4883 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4884 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004885 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004886 __h.reset(__buf.first);
4887 }
Howard Hinnant5e571422013-08-23 20:10:18 +00004888#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004889 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4890 __debug_less<_Compare> __c(__comp);
4891 __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004892#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004893 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4894 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004895#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004896}
4897
4898template <class _RandomAccessIterator>
4899inline _LIBCPP_INLINE_VISIBILITY
4900void
4901stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4902{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004903 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004904}
4905
4906// is_heap_until
4907
4908template <class _RandomAccessIterator, class _Compare>
4909_RandomAccessIterator
4910is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4911{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004912 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004913 difference_type __len = __last - __first;
4914 difference_type __p = 0;
4915 difference_type __c = 1;
4916 _RandomAccessIterator __pp = __first;
4917 while (__c < __len)
4918 {
4919 _RandomAccessIterator __cp = __first + __c;
4920 if (__comp(*__pp, *__cp))
4921 return __cp;
4922 ++__c;
4923 ++__cp;
4924 if (__c == __len)
4925 return __last;
4926 if (__comp(*__pp, *__cp))
4927 return __cp;
4928 ++__p;
4929 ++__pp;
4930 __c = 2 * __p + 1;
4931 }
4932 return __last;
4933}
4934
Howard Hinnant324bb032010-08-22 00:02:43 +00004935template<class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004936inline _LIBCPP_INLINE_VISIBILITY
4937_RandomAccessIterator
4938is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4939{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004940 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004941}
4942
4943// is_heap
4944
4945template <class _RandomAccessIterator, class _Compare>
4946inline _LIBCPP_INLINE_VISIBILITY
4947bool
4948is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4949{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004950 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004951}
4952
Howard Hinnant324bb032010-08-22 00:02:43 +00004953template<class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004954inline _LIBCPP_INLINE_VISIBILITY
4955bool
4956is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4957{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004958 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004959}
4960
4961// push_heap
4962
4963template <class _Compare, class _RandomAccessIterator>
4964void
David Majnemercb8757a2014-07-22 06:07:09 +00004965__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4966 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004967{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004968 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4969 if (__len > 1)
4970 {
4971 __len = (__len - 2) / 2;
4972 _RandomAccessIterator __ptr = __first + __len;
4973 if (__comp(*__ptr, *--__last))
4974 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004975 value_type __t(_VSTD::move(*__last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004976 do
4977 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004978 *__last = _VSTD::move(*__ptr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004979 __last = __ptr;
4980 if (__len == 0)
4981 break;
4982 __len = (__len - 1) / 2;
4983 __ptr = __first + __len;
4984 } while (__comp(*__ptr, __t));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004985 *__last = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004986 }
4987 }
4988}
4989
4990template <class _RandomAccessIterator, class _Compare>
4991inline _LIBCPP_INLINE_VISIBILITY
4992void
4993push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4994{
Howard Hinnant5e571422013-08-23 20:10:18 +00004995#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004996 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4997 __debug_less<_Compare> __c(__comp);
David Majnemercb8757a2014-07-22 06:07:09 +00004998 __sift_up<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004999#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005000 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
David Majnemercb8757a2014-07-22 06:07:09 +00005001 __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00005002#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005003}
5004
5005template <class _RandomAccessIterator>
5006inline _LIBCPP_INLINE_VISIBILITY
5007void
5008push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5009{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005010 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005011}
5012
5013// pop_heap
5014
5015template <class _Compare, class _RandomAccessIterator>
David Majnemercb8757a2014-07-22 06:07:09 +00005016void
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00005017__sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
5018 _Compare __comp,
David Majnemercb8757a2014-07-22 06:07:09 +00005019 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
5020 _RandomAccessIterator __start)
5021{
5022 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5023 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
5024 // left-child of __start is at 2 * __start + 1
5025 // right-child of __start is at 2 * __start + 2
5026 difference_type __child = __start - __first;
5027
5028 if (__len < 2 || (__len - 2) / 2 < __child)
5029 return;
5030
5031 __child = 2 * __child + 1;
5032 _RandomAccessIterator __child_i = __first + __child;
5033
5034 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
5035 // right-child exists and is greater than left-child
5036 ++__child_i;
5037 ++__child;
5038 }
5039
5040 // check if we are in heap-order
5041 if (__comp(*__child_i, *__start))
5042 // we are, __start is larger than it's largest child
5043 return;
5044
5045 value_type __top(_VSTD::move(*__start));
5046 do
5047 {
5048 // we are not in heap-order, swap the parent with it's largest child
5049 *__start = _VSTD::move(*__child_i);
5050 __start = __child_i;
5051
5052 if ((__len - 2) / 2 < __child)
5053 break;
5054
5055 // recompute the child based off of the updated parent
5056 __child = 2 * __child + 1;
5057 __child_i = __first + __child;
5058
5059 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
5060 // right-child exists and is greater than left-child
5061 ++__child_i;
5062 ++__child;
5063 }
5064
5065 // check if we are in heap-order
5066 } while (!__comp(*__child_i, __top));
5067 *__start = _VSTD::move(__top);
5068}
5069
5070template <class _Compare, class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005071inline _LIBCPP_INLINE_VISIBILITY
5072void
5073__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
5074 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
5075{
5076 if (__len > 1)
5077 {
5078 swap(*__first, *--__last);
David Majnemercb8757a2014-07-22 06:07:09 +00005079 __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005080 }
5081}
5082
5083template <class _RandomAccessIterator, class _Compare>
5084inline _LIBCPP_INLINE_VISIBILITY
5085void
5086pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5087{
Howard Hinnant5e571422013-08-23 20:10:18 +00005088#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005089 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5090 __debug_less<_Compare> __c(__comp);
5091 __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00005092#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005093 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5094 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00005095#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005096}
5097
5098template <class _RandomAccessIterator>
5099inline _LIBCPP_INLINE_VISIBILITY
5100void
5101pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5102{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005103 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005104}
5105
5106// make_heap
5107
5108template <class _Compare, class _RandomAccessIterator>
5109void
5110__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5111{
5112 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5113 difference_type __n = __last - __first;
5114 if (__n > 1)
5115 {
David Majnemercb8757a2014-07-22 06:07:09 +00005116 // start from the first parent, there is no need to consider children
5117 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
5118 {
5119 __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
5120 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005121 }
5122}
5123
5124template <class _RandomAccessIterator, class _Compare>
5125inline _LIBCPP_INLINE_VISIBILITY
5126void
5127make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5128{
Howard Hinnant5e571422013-08-23 20:10:18 +00005129#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005130 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5131 __debug_less<_Compare> __c(__comp);
5132 __make_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005133#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005134 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5135 __make_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005136#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005137}
5138
5139template <class _RandomAccessIterator>
5140inline _LIBCPP_INLINE_VISIBILITY
5141void
5142make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5143{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005144 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005145}
5146
5147// sort_heap
5148
5149template <class _Compare, class _RandomAccessIterator>
5150void
5151__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5152{
5153 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5154 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
5155 __pop_heap<_Compare>(__first, __last, __comp, __n);
5156}
5157
5158template <class _RandomAccessIterator, class _Compare>
5159inline _LIBCPP_INLINE_VISIBILITY
5160void
5161sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5162{
Howard Hinnant5e571422013-08-23 20:10:18 +00005163#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005164 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5165 __debug_less<_Compare> __c(__comp);
5166 __sort_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005167#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005168 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5169 __sort_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005170#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005171}
5172
5173template <class _RandomAccessIterator>
5174inline _LIBCPP_INLINE_VISIBILITY
5175void
5176sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5177{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005178 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005179}
5180
5181// partial_sort
5182
5183template <class _Compare, class _RandomAccessIterator>
5184void
5185__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5186 _Compare __comp)
5187{
5188 __make_heap<_Compare>(__first, __middle, __comp);
5189 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5190 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5191 {
5192 if (__comp(*__i, *__first))
5193 {
5194 swap(*__i, *__first);
David Majnemercb8757a2014-07-22 06:07:09 +00005195 __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005196 }
5197 }
5198 __sort_heap<_Compare>(__first, __middle, __comp);
5199}
5200
5201template <class _RandomAccessIterator, class _Compare>
5202inline _LIBCPP_INLINE_VISIBILITY
5203void
5204partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5205 _Compare __comp)
5206{
Howard Hinnant5e571422013-08-23 20:10:18 +00005207#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005208 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5209 __debug_less<_Compare> __c(__comp);
5210 __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005211#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005212 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5213 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005214#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005215}
5216
5217template <class _RandomAccessIterator>
5218inline _LIBCPP_INLINE_VISIBILITY
5219void
5220partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5221{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005222 _VSTD::partial_sort(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005223 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5224}
5225
5226// partial_sort_copy
5227
5228template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5229_RandomAccessIterator
5230__partial_sort_copy(_InputIterator __first, _InputIterator __last,
5231 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5232{
5233 _RandomAccessIterator __r = __result_first;
5234 if (__r != __result_last)
5235 {
Eric Fiselierb9919752014-10-27 19:28:20 +00005236 for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005237 *__r = *__first;
5238 __make_heap<_Compare>(__result_first, __r, __comp);
David Majnemercb8757a2014-07-22 06:07:09 +00005239 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005240 for (; __first != __last; ++__first)
5241 if (__comp(*__first, *__result_first))
5242 {
5243 *__result_first = *__first;
David Majnemercb8757a2014-07-22 06:07:09 +00005244 __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005245 }
5246 __sort_heap<_Compare>(__result_first, __r, __comp);
5247 }
5248 return __r;
5249}
5250
5251template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5252inline _LIBCPP_INLINE_VISIBILITY
5253_RandomAccessIterator
5254partial_sort_copy(_InputIterator __first, _InputIterator __last,
5255 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5256{
Howard Hinnant5e571422013-08-23 20:10:18 +00005257#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005258 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5259 __debug_less<_Compare> __c(__comp);
5260 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005261#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005262 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5263 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005264#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005265}
5266
5267template <class _InputIterator, class _RandomAccessIterator>
5268inline _LIBCPP_INLINE_VISIBILITY
5269_RandomAccessIterator
5270partial_sort_copy(_InputIterator __first, _InputIterator __last,
5271 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5272{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005273 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005274 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5275}
5276
5277// nth_element
5278
5279template <class _Compare, class _RandomAccessIterator>
5280void
5281__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5282{
5283 // _Compare is known to be a reference type
5284 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5285 const difference_type __limit = 7;
5286 while (true)
5287 {
5288 __restart:
Howard Hinnant8292d742011-12-29 17:45:35 +00005289 if (__nth == __last)
5290 return;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005291 difference_type __len = __last - __first;
5292 switch (__len)
5293 {
5294 case 0:
5295 case 1:
5296 return;
5297 case 2:
5298 if (__comp(*--__last, *__first))
5299 swap(*__first, *__last);
5300 return;
5301 case 3:
5302 {
5303 _RandomAccessIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00005304 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005305 return;
5306 }
5307 }
5308 if (__len <= __limit)
5309 {
5310 __selection_sort<_Compare>(__first, __last, __comp);
5311 return;
5312 }
5313 // __len > __limit >= 3
5314 _RandomAccessIterator __m = __first + __len/2;
5315 _RandomAccessIterator __lm1 = __last;
Howard Hinnant0949eed2011-06-30 21:18:19 +00005316 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005317 // *__m is median
5318 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5319 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5320 _RandomAccessIterator __i = __first;
5321 _RandomAccessIterator __j = __lm1;
5322 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5323 // The search going up is known to be guarded but the search coming down isn't.
5324 // Prime the downward search with a guard.
5325 if (!__comp(*__i, *__m)) // if *__first == *__m
5326 {
5327 // *__first == *__m, *__first doesn't go in first part
5328 // manually guard downward moving __j against __i
5329 while (true)
5330 {
5331 if (__i == --__j)
5332 {
5333 // *__first == *__m, *__m <= all other elements
5334 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5335 ++__i; // __first + 1
5336 __j = __last;
5337 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5338 {
5339 while (true)
5340 {
5341 if (__i == __j)
5342 return; // [__first, __last) all equivalent elements
5343 if (__comp(*__first, *__i))
5344 {
5345 swap(*__i, *__j);
5346 ++__n_swaps;
5347 ++__i;
5348 break;
5349 }
5350 ++__i;
5351 }
5352 }
5353 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5354 if (__i == __j)
5355 return;
5356 while (true)
5357 {
5358 while (!__comp(*__first, *__i))
5359 ++__i;
5360 while (__comp(*__first, *--__j))
5361 ;
5362 if (__i >= __j)
5363 break;
5364 swap(*__i, *__j);
5365 ++__n_swaps;
5366 ++__i;
5367 }
5368 // [__first, __i) == *__first and *__first < [__i, __last)
5369 // The first part is sorted,
5370 if (__nth < __i)
5371 return;
5372 // __nth_element the secod part
5373 // __nth_element<_Compare>(__i, __nth, __last, __comp);
5374 __first = __i;
5375 goto __restart;
5376 }
5377 if (__comp(*__j, *__m))
5378 {
5379 swap(*__i, *__j);
5380 ++__n_swaps;
5381 break; // found guard for downward moving __j, now use unguarded partition
5382 }
5383 }
5384 }
5385 ++__i;
5386 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5387 // if not yet partitioned...
5388 if (__i < __j)
5389 {
5390 // known that *(__i - 1) < *__m
5391 while (true)
5392 {
5393 // __m still guards upward moving __i
5394 while (__comp(*__i, *__m))
5395 ++__i;
5396 // It is now known that a guard exists for downward moving __j
5397 while (!__comp(*--__j, *__m))
5398 ;
5399 if (__i >= __j)
5400 break;
5401 swap(*__i, *__j);
5402 ++__n_swaps;
5403 // It is known that __m != __j
5404 // If __m just moved, follow it
5405 if (__m == __i)
5406 __m = __j;
5407 ++__i;
5408 }
5409 }
5410 // [__first, __i) < *__m and *__m <= [__i, __last)
5411 if (__i != __m && __comp(*__m, *__i))
5412 {
5413 swap(*__i, *__m);
5414 ++__n_swaps;
5415 }
5416 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5417 if (__nth == __i)
5418 return;
5419 if (__n_swaps == 0)
5420 {
5421 // We were given a perfectly partitioned sequence. Coincidence?
5422 if (__nth < __i)
5423 {
5424 // Check for [__first, __i) already sorted
5425 __j = __m = __first;
5426 while (++__j != __i)
5427 {
5428 if (__comp(*__j, *__m))
5429 // not yet sorted, so sort
5430 goto not_sorted;
5431 __m = __j;
5432 }
5433 // [__first, __i) sorted
5434 return;
5435 }
5436 else
5437 {
5438 // Check for [__i, __last) already sorted
5439 __j = __m = __i;
5440 while (++__j != __last)
5441 {
5442 if (__comp(*__j, *__m))
5443 // not yet sorted, so sort
5444 goto not_sorted;
5445 __m = __j;
5446 }
5447 // [__i, __last) sorted
5448 return;
5449 }
5450 }
5451not_sorted:
5452 // __nth_element on range containing __nth
5453 if (__nth < __i)
5454 {
5455 // __nth_element<_Compare>(__first, __nth, __i, __comp);
5456 __last = __i;
5457 }
5458 else
5459 {
5460 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5461 __first = ++__i;
5462 }
5463 }
5464}
5465
5466template <class _RandomAccessIterator, class _Compare>
5467inline _LIBCPP_INLINE_VISIBILITY
5468void
5469nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5470{
Howard Hinnant5e571422013-08-23 20:10:18 +00005471#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005472 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5473 __debug_less<_Compare> __c(__comp);
5474 __nth_element<_Comp_ref>(__first, __nth, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005475#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005476 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5477 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005478#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005479}
5480
5481template <class _RandomAccessIterator>
5482inline _LIBCPP_INLINE_VISIBILITY
5483void
5484nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5485{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005486 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005487}
5488
5489// includes
5490
5491template <class _Compare, class _InputIterator1, class _InputIterator2>
5492bool
5493__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5494 _Compare __comp)
5495{
5496 for (; __first2 != __last2; ++__first1)
5497 {
5498 if (__first1 == __last1 || __comp(*__first2, *__first1))
5499 return false;
5500 if (!__comp(*__first1, *__first2))
5501 ++__first2;
5502 }
5503 return true;
5504}
5505
5506template <class _InputIterator1, class _InputIterator2, class _Compare>
5507inline _LIBCPP_INLINE_VISIBILITY
5508bool
5509includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5510 _Compare __comp)
5511{
Howard Hinnant5e571422013-08-23 20:10:18 +00005512#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005513 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5514 __debug_less<_Compare> __c(__comp);
5515 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005516#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005517 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5518 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005519#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005520}
5521
5522template <class _InputIterator1, class _InputIterator2>
5523inline _LIBCPP_INLINE_VISIBILITY
5524bool
5525includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5526{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005527 return _VSTD::includes(__first1, __last1, __first2, __last2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005528 __less<typename iterator_traits<_InputIterator1>::value_type,
5529 typename iterator_traits<_InputIterator2>::value_type>());
5530}
5531
5532// set_union
5533
5534template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5535_OutputIterator
5536__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5537 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5538{
5539 for (; __first1 != __last1; ++__result)
5540 {
5541 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005542 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005543 if (__comp(*__first2, *__first1))
5544 {
5545 *__result = *__first2;
5546 ++__first2;
5547 }
5548 else
5549 {
5550 *__result = *__first1;
5551 if (!__comp(*__first1, *__first2))
5552 ++__first2;
5553 ++__first1;
5554 }
5555 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00005556 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005557}
5558
5559template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5560inline _LIBCPP_INLINE_VISIBILITY
5561_OutputIterator
5562set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5563 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5564{
Howard Hinnant5e571422013-08-23 20:10:18 +00005565#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005566 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5567 __debug_less<_Compare> __c(__comp);
5568 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005569#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005570 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5571 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005572#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005573}
5574
5575template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5576inline _LIBCPP_INLINE_VISIBILITY
5577_OutputIterator
5578set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5579 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5580{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005581 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005582 __less<typename iterator_traits<_InputIterator1>::value_type,
5583 typename iterator_traits<_InputIterator2>::value_type>());
5584}
5585
5586// set_intersection
5587
5588template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5589_OutputIterator
5590__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5591 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5592{
5593 while (__first1 != __last1 && __first2 != __last2)
5594 {
5595 if (__comp(*__first1, *__first2))
5596 ++__first1;
5597 else
5598 {
5599 if (!__comp(*__first2, *__first1))
5600 {
5601 *__result = *__first1;
5602 ++__result;
5603 ++__first1;
5604 }
5605 ++__first2;
5606 }
5607 }
5608 return __result;
5609}
5610
5611template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5612inline _LIBCPP_INLINE_VISIBILITY
5613_OutputIterator
5614set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5615 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5616{
Howard Hinnant5e571422013-08-23 20:10:18 +00005617#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005618 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5619 __debug_less<_Compare> __c(__comp);
5620 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005621#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005622 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5623 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005624#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005625}
5626
5627template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5628inline _LIBCPP_INLINE_VISIBILITY
5629_OutputIterator
5630set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5631 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5632{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005633 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005634 __less<typename iterator_traits<_InputIterator1>::value_type,
5635 typename iterator_traits<_InputIterator2>::value_type>());
5636}
5637
5638// set_difference
5639
5640template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5641_OutputIterator
5642__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5643 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5644{
5645 while (__first1 != __last1)
5646 {
5647 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005648 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005649 if (__comp(*__first1, *__first2))
5650 {
5651 *__result = *__first1;
5652 ++__result;
5653 ++__first1;
5654 }
5655 else
5656 {
5657 if (!__comp(*__first2, *__first1))
5658 ++__first1;
5659 ++__first2;
5660 }
5661 }
5662 return __result;
5663}
5664
5665template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5666inline _LIBCPP_INLINE_VISIBILITY
5667_OutputIterator
5668set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5669 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5670{
Howard Hinnant5e571422013-08-23 20:10:18 +00005671#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005672 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5673 __debug_less<_Compare> __c(__comp);
5674 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005675#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005676 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5677 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005678#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005679}
5680
5681template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5682inline _LIBCPP_INLINE_VISIBILITY
5683_OutputIterator
5684set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5685 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5686{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005687 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005688 __less<typename iterator_traits<_InputIterator1>::value_type,
5689 typename iterator_traits<_InputIterator2>::value_type>());
5690}
5691
5692// set_symmetric_difference
5693
5694template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5695_OutputIterator
5696__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5697 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5698{
5699 while (__first1 != __last1)
5700 {
5701 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005702 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005703 if (__comp(*__first1, *__first2))
5704 {
5705 *__result = *__first1;
5706 ++__result;
5707 ++__first1;
5708 }
5709 else
5710 {
5711 if (__comp(*__first2, *__first1))
5712 {
5713 *__result = *__first2;
5714 ++__result;
5715 }
5716 else
5717 ++__first1;
5718 ++__first2;
5719 }
5720 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00005721 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005722}
5723
5724template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5725inline _LIBCPP_INLINE_VISIBILITY
5726_OutputIterator
5727set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5728 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5729{
Howard Hinnant5e571422013-08-23 20:10:18 +00005730#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005731 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5732 __debug_less<_Compare> __c(__comp);
5733 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005734#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005735 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5736 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005737#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005738}
5739
5740template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5741inline _LIBCPP_INLINE_VISIBILITY
5742_OutputIterator
5743set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5744 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5745{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005746 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005747 __less<typename iterator_traits<_InputIterator1>::value_type,
5748 typename iterator_traits<_InputIterator2>::value_type>());
5749}
5750
5751// lexicographical_compare
5752
5753template <class _Compare, class _InputIterator1, class _InputIterator2>
5754bool
5755__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5756 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5757{
Eric Fiselierb9919752014-10-27 19:28:20 +00005758 for (; __first2 != __last2; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005759 {
5760 if (__first1 == __last1 || __comp(*__first1, *__first2))
5761 return true;
5762 if (__comp(*__first2, *__first1))
5763 return false;
5764 }
5765 return false;
5766}
5767
5768template <class _InputIterator1, class _InputIterator2, class _Compare>
5769inline _LIBCPP_INLINE_VISIBILITY
5770bool
5771lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5772 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5773{
Howard Hinnant5e571422013-08-23 20:10:18 +00005774#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005775 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5776 __debug_less<_Compare> __c(__comp);
5777 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005778#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005779 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5780 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005781#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005782}
5783
5784template <class _InputIterator1, class _InputIterator2>
5785inline _LIBCPP_INLINE_VISIBILITY
5786bool
5787lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5788 _InputIterator2 __first2, _InputIterator2 __last2)
5789{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005790 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005791 __less<typename iterator_traits<_InputIterator1>::value_type,
5792 typename iterator_traits<_InputIterator2>::value_type>());
5793}
5794
5795// next_permutation
5796
5797template <class _Compare, class _BidirectionalIterator>
5798bool
5799__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5800{
5801 _BidirectionalIterator __i = __last;
5802 if (__first == __last || __first == --__i)
5803 return false;
5804 while (true)
5805 {
5806 _BidirectionalIterator __ip1 = __i;
5807 if (__comp(*--__i, *__ip1))
5808 {
5809 _BidirectionalIterator __j = __last;
5810 while (!__comp(*__i, *--__j))
5811 ;
5812 swap(*__i, *__j);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005813 _VSTD::reverse(__ip1, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005814 return true;
5815 }
5816 if (__i == __first)
5817 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005818 _VSTD::reverse(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005819 return false;
5820 }
5821 }
5822}
5823
5824template <class _BidirectionalIterator, class _Compare>
5825inline _LIBCPP_INLINE_VISIBILITY
5826bool
5827next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5828{
Howard Hinnant5e571422013-08-23 20:10:18 +00005829#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005830 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5831 __debug_less<_Compare> __c(__comp);
5832 return __next_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005833#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005834 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5835 return __next_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005836#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005837}
5838
5839template <class _BidirectionalIterator>
5840inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005841bool
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005842next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5843{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005844 return _VSTD::next_permutation(__first, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005845 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5846}
5847
5848// prev_permutation
5849
5850template <class _Compare, class _BidirectionalIterator>
5851bool
5852__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5853{
5854 _BidirectionalIterator __i = __last;
5855 if (__first == __last || __first == --__i)
5856 return false;
5857 while (true)
5858 {
5859 _BidirectionalIterator __ip1 = __i;
5860 if (__comp(*__ip1, *--__i))
5861 {
5862 _BidirectionalIterator __j = __last;
5863 while (!__comp(*--__j, *__i))
5864 ;
5865 swap(*__i, *__j);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005866 _VSTD::reverse(__ip1, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005867 return true;
5868 }
5869 if (__i == __first)
5870 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005871 _VSTD::reverse(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005872 return false;
5873 }
5874 }
5875}
5876
5877template <class _BidirectionalIterator, class _Compare>
5878inline _LIBCPP_INLINE_VISIBILITY
5879bool
5880prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5881{
Howard Hinnant5e571422013-08-23 20:10:18 +00005882#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005883 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5884 __debug_less<_Compare> __c(__comp);
5885 return __prev_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005886#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005887 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5888 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005889#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005890}
5891
5892template <class _BidirectionalIterator>
5893inline _LIBCPP_INLINE_VISIBILITY
5894bool
5895prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5896{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005897 return _VSTD::prev_permutation(__first, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005898 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5899}
5900
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005901_LIBCPP_END_NAMESPACE_STD
5902
Eric Fiselier018a3d52017-05-31 22:07:49 +00005903_LIBCPP_POP_MACROS
5904
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005905#endif // _LIBCPP_ALGORITHM