blob: 01cee60bf42c239b1809be84cfdff418ff6ea7d6 [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//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
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
38template <class InputIterator, class T>
39 InputIterator
40 find(InputIterator first, InputIterator last, const T& value);
41
42template <class InputIterator, class Predicate>
43 InputIterator
44 find_if(InputIterator first, InputIterator last, Predicate pred);
45
46template<class InputIterator, class Predicate>
47 InputIterator
48 find_if_not(InputIterator first, InputIterator last, Predicate pred);
49
50template <class ForwardIterator1, class ForwardIterator2>
51 ForwardIterator1
52 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
53 ForwardIterator2 first2, ForwardIterator2 last2);
54
55template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
56 ForwardIterator1
57 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
58 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
59
60template <class ForwardIterator1, class ForwardIterator2>
61 ForwardIterator1
62 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
63 ForwardIterator2 first2, ForwardIterator2 last2);
64
65template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
66 ForwardIterator1
67 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
68 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
69
70template <class ForwardIterator>
71 ForwardIterator
72 adjacent_find(ForwardIterator first, ForwardIterator last);
73
74template <class ForwardIterator, class BinaryPredicate>
75 ForwardIterator
76 adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
77
78template <class InputIterator, class T>
79 typename iterator_traits<InputIterator>::difference_type
80 count(InputIterator first, InputIterator last, const T& value);
81
82template <class InputIterator, class Predicate>
83 typename iterator_traits<InputIterator>::difference_type
84 count_if(InputIterator first, InputIterator last, Predicate pred);
85
86template <class InputIterator1, class InputIterator2>
87 pair<InputIterator1, InputIterator2>
88 mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
89
90template <class InputIterator1, class InputIterator2, class BinaryPredicate>
91 pair<InputIterator1, InputIterator2>
92 mismatch(InputIterator1 first1, InputIterator1 last1,
93 InputIterator2 first2, BinaryPredicate pred);
94
95template <class InputIterator1, class InputIterator2>
96 bool
97 equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
98
99template <class InputIterator1, class InputIterator2, class BinaryPredicate>
100 bool
101 equal(InputIterator1 first1, InputIterator1 last1,
102 InputIterator2 first2, BinaryPredicate pred);
103
104template<class ForwardIterator1, class ForwardIterator2>
105 bool
106 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
107 ForwardIterator2 first2);
108
109template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
110 bool
111 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
112 ForwardIterator2 first2, BinaryPredicate pred);
113
114template <class ForwardIterator1, class ForwardIterator2>
115 ForwardIterator1
116 search(ForwardIterator1 first1, ForwardIterator1 last1,
117 ForwardIterator2 first2, ForwardIterator2 last2);
118
119template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
120 ForwardIterator1
121 search(ForwardIterator1 first1, ForwardIterator1 last1,
122 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
123
124template <class ForwardIterator, class Size, class T>
125 ForwardIterator
126 search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
127
128template <class ForwardIterator, class Size, class T, class BinaryPredicate>
129 ForwardIterator
130 search_n(ForwardIterator first, ForwardIterator last,
131 Size count, const T& value, BinaryPredicate pred);
132
133template <class InputIterator, class OutputIterator>
134 OutputIterator
135 copy(InputIterator first, InputIterator last, OutputIterator result);
136
137template<class InputIterator, class OutputIterator, class Predicate>
138 OutputIterator
139 copy_if(InputIterator first, InputIterator last,
140 OutputIterator result, Predicate pred);
141
142template<class InputIterator, class Size, class OutputIterator>
143 OutputIterator
144 copy_n(InputIterator first, Size n, OutputIterator result);
145
146template <class BidirectionalIterator1, class BidirectionalIterator2>
147 BidirectionalIterator2
148 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
149 BidirectionalIterator2 result);
150
151template <class ForwardIterator1, class ForwardIterator2>
152 ForwardIterator2
153 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
154
155template <class ForwardIterator1, class ForwardIterator2>
156 void
157 iter_swap(ForwardIterator1 a, ForwardIterator2 b);
158
159template <class InputIterator, class OutputIterator, class UnaryOperation>
160 OutputIterator
161 transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
162
163template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
164 OutputIterator
165 transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
166 OutputIterator result, BinaryOperation binary_op);
167
168template <class ForwardIterator, class T>
169 void
170 replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
171
172template <class ForwardIterator, class Predicate, class T>
173 void
174 replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
175
176template <class InputIterator, class OutputIterator, class T>
177 OutputIterator
178 replace_copy(InputIterator first, InputIterator last, OutputIterator result,
179 const T& old_value, const T& new_value);
180
181template <class InputIterator, class OutputIterator, class Predicate, class T>
182 OutputIterator
183 replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
184
185template <class ForwardIterator, class T>
186 void
187 fill(ForwardIterator first, ForwardIterator last, const T& value);
188
189template <class OutputIterator, class Size, class T>
190 OutputIterator
191 fill_n(OutputIterator first, Size n, const T& value);
192
193template <class ForwardIterator, class Generator>
194 void
195 generate(ForwardIterator first, ForwardIterator last, Generator gen);
196
197template <class OutputIterator, class Size, class Generator>
198 OutputIterator
199 generate_n(OutputIterator first, Size n, Generator gen);
200
201template <class ForwardIterator, class T>
202 ForwardIterator
203 remove(ForwardIterator first, ForwardIterator last, const T& value);
204
205template <class ForwardIterator, class Predicate>
206 ForwardIterator
207 remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
208
209template <class InputIterator, class OutputIterator, class T>
210 OutputIterator
211 remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
212
213template <class InputIterator, class OutputIterator, class Predicate>
214 OutputIterator
215 remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
216
217template <class ForwardIterator>
218 ForwardIterator
219 unique(ForwardIterator first, ForwardIterator last);
220
221template <class ForwardIterator, class BinaryPredicate>
222 ForwardIterator
223 unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
224
225template <class InputIterator, class OutputIterator>
226 OutputIterator
227 unique_copy(InputIterator first, InputIterator last, OutputIterator result);
228
229template <class InputIterator, class OutputIterator, class BinaryPredicate>
230 OutputIterator
231 unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
232
233template <class BidirectionalIterator>
234 void
235 reverse(BidirectionalIterator first, BidirectionalIterator last);
236
237template <class BidirectionalIterator, class OutputIterator>
238 OutputIterator
239 reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
240
241template <class ForwardIterator>
242 ForwardIterator
243 rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
244
245template <class ForwardIterator, class OutputIterator>
246 OutputIterator
247 rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
248
249template <class RandomAccessIterator>
250 void
251 random_shuffle(RandomAccessIterator first, RandomAccessIterator last);
252
253template <class RandomAccessIterator, class RandomNumberGenerator>
254 void
255 random_shuffle(RandomAccessIterator first, RandomAccessIterator last, RandomNumberGenerator& rand);
256
Howard Hinnantc3267212010-05-26 17:49:34 +0000257template<class RandomAccessIterator, class UniformRandomNumberGenerator>
258 void shuffle(RandomAccessIterator first, RandomAccessIterator last,
259 UniformRandomNumberGenerator& g);
260
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000261template <class InputIterator, class Predicate>
262 bool
263 is_partitioned(InputIterator first, InputIterator last, Predicate pred);
264
265template <class ForwardIterator, class Predicate>
266 ForwardIterator
267 partition(ForwardIterator first, ForwardIterator last, Predicate pred);
268
269template <class InputIterator, class OutputIterator1,
270 class OutputIterator2, class Predicate>
271 pair<OutputIterator1, OutputIterator2>
272 partition_copy(InputIterator first, InputIterator last,
273 OutputIterator1 out_true, OutputIterator2 out_false,
274 Predicate pred);
275
276template <class ForwardIterator, class Predicate>
277 ForwardIterator
278 stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
279
280template<class ForwardIterator, class Predicate>
281 ForwardIterator
282 partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
283
284template <class ForwardIterator>
285 bool
286 is_sorted(ForwardIterator first, ForwardIterator last);
287
288template <class ForwardIterator, class Compare>
289 bool
290 is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
291
292template<class ForwardIterator>
293 ForwardIterator
294 is_sorted_until(ForwardIterator first, ForwardIterator last);
295
296template <class ForwardIterator, class Compare>
297 ForwardIterator
298 is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
299
300template <class RandomAccessIterator>
301 void
302 sort(RandomAccessIterator first, RandomAccessIterator last);
303
304template <class RandomAccessIterator, class Compare>
305 void
306 sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
307
308template <class RandomAccessIterator>
309 void
310 stable_sort(RandomAccessIterator first, RandomAccessIterator last);
311
312template <class RandomAccessIterator, class Compare>
313 void
314 stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
315
316template <class RandomAccessIterator>
317 void
318 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
319
320template <class RandomAccessIterator, class Compare>
321 void
322 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
323
324template <class InputIterator, class RandomAccessIterator>
325 RandomAccessIterator
326 partial_sort_copy(InputIterator first, InputIterator last,
327 RandomAccessIterator result_first, RandomAccessIterator result_last);
328
329template <class InputIterator, class RandomAccessIterator, class Compare>
330 RandomAccessIterator
331 partial_sort_copy(InputIterator first, InputIterator last,
332 RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
333
334template <class RandomAccessIterator>
335 void
336 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
337
338template <class RandomAccessIterator, class Compare>
339 void
340 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
341
342template <class ForwardIterator, class T>
343 ForwardIterator
344 lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
345
346template <class ForwardIterator, class T, class Compare>
347 ForwardIterator
348 lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
349
350template <class ForwardIterator, class T>
351 ForwardIterator
352 upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
353
354template <class ForwardIterator, class T, class Compare>
355 ForwardIterator
356 upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
357
358template <class ForwardIterator, class T>
359 pair<ForwardIterator, ForwardIterator>
360 equal_range(ForwardIterator first, ForwardIterator last, const T& value);
361
362template <class ForwardIterator, class T, class Compare>
363 pair<ForwardIterator, ForwardIterator>
364 equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
365
366template <class ForwardIterator, class T>
367 bool
368 binary_search(ForwardIterator first, ForwardIterator last, const T& value);
369
370template <class ForwardIterator, class T, class Compare>
371 bool
372 binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
373
374template <class InputIterator1, class InputIterator2, class OutputIterator>
375 OutputIterator
376 merge(InputIterator1 first1, InputIterator1 last1,
377 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
378
379template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
380 OutputIterator
381 merge(InputIterator1 first1, InputIterator1 last1,
382 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
383
384template <class BidirectionalIterator>
385 void
386 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
387
388template <class BidirectionalIterator, class Compare>
389 void
390 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
391
392template <class InputIterator1, class InputIterator2>
393 bool
394 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
395
396template <class InputIterator1, class InputIterator2, class Compare>
397 bool
398 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
399
400template <class InputIterator1, class InputIterator2, class OutputIterator>
401 OutputIterator
402 set_union(InputIterator1 first1, InputIterator1 last1,
403 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
404
405template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
406 OutputIterator
407 set_union(InputIterator1 first1, InputIterator1 last1,
408 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
409
410template <class InputIterator1, class InputIterator2, class OutputIterator>
411 OutputIterator
412 set_intersection(InputIterator1 first1, InputIterator1 last1,
413 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
414
415template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
416 OutputIterator
417 set_intersection(InputIterator1 first1, InputIterator1 last1,
418 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
419
420template <class InputIterator1, class InputIterator2, class OutputIterator>
421 OutputIterator
422 set_difference(InputIterator1 first1, InputIterator1 last1,
423 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
424
425template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
426 OutputIterator
427 set_difference(InputIterator1 first1, InputIterator1 last1,
428 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
429
430template <class InputIterator1, class InputIterator2, class OutputIterator>
431 OutputIterator
432 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
433 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
434
435template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
436 OutputIterator
437 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
438 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
439
440template <class RandomAccessIterator>
441 void
442 push_heap(RandomAccessIterator first, RandomAccessIterator last);
443
444template <class RandomAccessIterator, class Compare>
445 void
446 push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
447
448template <class RandomAccessIterator>
449 void
450 pop_heap(RandomAccessIterator first, RandomAccessIterator last);
451
452template <class RandomAccessIterator, class Compare>
453 void
454 pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
455
456template <class RandomAccessIterator>
457 void
458 make_heap(RandomAccessIterator first, RandomAccessIterator last);
459
460template <class RandomAccessIterator, class Compare>
461 void
462 make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
463
464template <class RandomAccessIterator>
465 void
466 sort_heap(RandomAccessIterator first, RandomAccessIterator last);
467
468template <class RandomAccessIterator, class Compare>
469 void
470 sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
471
472template <class RandomAccessIterator>
473 bool
474 is_heap(RandomAccessIterator first, RandomAccessiterator last);
475
476template <class RandomAccessIterator, class Compare>
477 bool
478 is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
479
480template <class RandomAccessIterator>
481 RandomAccessIterator
482 is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
483
484template <class RandomAccessIterator, class Compare>
485 RandomAccessIterator
486 is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
487
488template <class T>
489 const T&
490 min(const T& a, const T& b);
491
492template <class T, class Compare>
493 const T&
494 min(const T& a, const T& b, Compare comp);
495
496template <class T>
497 const T&
498 max(const T& a, const T& b);
499
500template <class T, class Compare>
501 const T&
502 max(const T& a, const T& b, Compare comp);
503
504template <class ForwardIterator>
505 ForwardIterator
506 min_element(ForwardIterator first, ForwardIterator last);
507
508template <class ForwardIterator, class Compare>
509 ForwardIterator
510 min_element(ForwardIterator first, ForwardIterator last, Compare comp);
511
512template <class ForwardIterator>
513 ForwardIterator
514 max_element(ForwardIterator first, ForwardIterator last);
515
516template <class ForwardIterator, class Compare>
517 ForwardIterator
518 max_element(ForwardIterator first, ForwardIterator last, Compare comp);
519
520template <class InputIterator1, class InputIterator2>
521 bool
522 lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
523
524template <class InputIterator1, class InputIterator2, class Compare>
525 bool
526 lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
527 InputIterator2 first2, InputIterator2 last2, Compare comp);
528
529template <class BidirectionalIterator>
530 bool
531 next_permutation(BidirectionalIterator first, BidirectionalIterator last);
532
533template <class BidirectionalIterator, class Compare>
534 bool
535 next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
536
537template <class BidirectionalIterator>
538 bool
539 prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
540
541template <class BidirectionalIterator, class Compare>
542 bool
543 prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
544
545} // std
546
547*/
548
549#include <__config>
550#include <initializer_list>
551#include <type_traits>
552#include <cstring>
553#include <utility>
554#include <memory>
555#include <iterator>
556#ifdef _LIBCPP_DEBUG
557#include <cassert>
558#endif
Howard Hinnantadff4892010-05-24 17:49:41 +0000559#include <cstdlib>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000560
561#pragma GCC system_header
562
563_LIBCPP_BEGIN_NAMESPACE_STD
564
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000565template <class _T1, class _T2 = _T1>
566struct __equal_to
567{
568 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
569 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
570 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
571 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
572};
573
574template <class _T1>
575struct __equal_to<_T1, _T1>
576{
577 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
578};
579
580template <class _T1>
581struct __equal_to<const _T1, _T1>
582{
583 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
584};
585
586template <class _T1>
587struct __equal_to<_T1, const _T1>
588{
589 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
590};
591
592template <class _T1, class _T2 = _T1>
593struct __less
594{
595 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
596 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
597 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
598 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
599};
600
601template <class _T1>
602struct __less<_T1, _T1>
603{
604 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
605};
606
607template <class _T1>
608struct __less<const _T1, _T1>
609{
610 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
611};
612
613template <class _T1>
614struct __less<_T1, const _T1>
615{
616 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
617};
618
619template <class _Predicate>
620class __negate
621{
622private:
623 _Predicate __p_;
624public:
625 _LIBCPP_INLINE_VISIBILITY __negate() {}
626
627 _LIBCPP_INLINE_VISIBILITY
628 explicit __negate(_Predicate __p) : __p_(__p) {}
629
630 template <class _T1>
631 _LIBCPP_INLINE_VISIBILITY
632 bool operator()(const _T1& __x) {return !__p_(__x);}
633
634 template <class _T1, class _T2>
635 _LIBCPP_INLINE_VISIBILITY
636 bool operator()(const _T1& __x, const _T2& __y) {return !__p_(__x, __y);}
637};
638
639#ifdef _LIBCPP_DEBUG
640
641template <class _Compare>
642struct __debug_less
643{
644 _Compare __comp_;
645 __debug_less(_Compare& __c) : __comp_(__c) {}
646 template <class _Tp, class _Up>
647 bool operator()(const _Tp& __x, const _Up& __y)
648 {
649 bool __r = __comp_(__x, __y);
650 if (__r)
651 assert(!__comp_(__y, __x));
652 return __r;
653 }
654};
655
656#endif // _LIBCPP_DEBUG
657
658// all_of
659
660template <class _InputIterator, class _Predicate>
661inline _LIBCPP_INLINE_VISIBILITY
662bool
663all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
664{
665 for (; __first != __last; ++__first)
666 if (!__pred(*__first))
667 return false;
668 return true;
669}
670
671// any_of
672
673template <class _InputIterator, class _Predicate>
674inline _LIBCPP_INLINE_VISIBILITY
675bool
676any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
677{
678 for (; __first != __last; ++__first)
679 if (__pred(*__first))
680 return true;
681 return false;
682}
683
684// none_of
685
686template <class _InputIterator, class _Predicate>
687inline _LIBCPP_INLINE_VISIBILITY
688bool
689none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
690{
691 for (; __first != __last; ++__first)
692 if (__pred(*__first))
693 return false;
694 return true;
695}
696
697// for_each
698
699template <class _InputIterator, class _Function>
700inline _LIBCPP_INLINE_VISIBILITY
701_Function
702for_each(_InputIterator __first, _InputIterator __last, _Function __f)
703{
704 for (; __first != __last; ++__first)
705 __f(*__first);
706 return _STD::move(__f);
707}
708
709// find
710
711template <class _InputIterator, class _Tp>
712inline _LIBCPP_INLINE_VISIBILITY
713_InputIterator
714find(_InputIterator __first, _InputIterator __last, const _Tp& __value)
715{
716 for (; __first != __last; ++__first)
717 if (*__first == __value)
718 break;
719 return __first;
720}
721
722// find_if
723
724template <class _InputIterator, class _Predicate>
725inline _LIBCPP_INLINE_VISIBILITY
726_InputIterator
727find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
728{
729 for (; __first != __last; ++__first)
730 if (__pred(*__first))
731 break;
732 return __first;
733}
734
735// find_if_not
736
737template<class _InputIterator, class _Predicate>
738inline _LIBCPP_INLINE_VISIBILITY
739_InputIterator
740find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
741{
742 for (; __first != __last; ++__first)
743 if (!__pred(*__first))
744 break;
745 return __first;
746}
747
748// find_end
749
750template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
751_ForwardIterator1
752__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
753 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
754 forward_iterator_tag, forward_iterator_tag)
755{
756 // modeled after search algorithm
757 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
758 if (__first2 == __last2)
759 return __r;
760 while (true)
761 {
762 while (true)
763 {
764 if (__first1 == __last1) // if source exhausted return last correct answer
765 return __r; // (or __last1 if never found)
766 if (__pred(*__first1, *__first2))
767 break;
768 ++__first1;
769 }
770 // *__first1 matches *__first2, now match elements after here
771 _ForwardIterator1 __m1 = __first1;
772 _ForwardIterator2 __m2 = __first2;
773 while (true)
774 {
775 if (++__m2 == __last2)
776 { // Pattern exhaused, record answer and search for another one
777 __r = __first1;
778 ++__first1;
779 break;
780 }
781 if (++__m1 == __last1) // Source exhausted, return last answer
782 return __r;
783 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
784 {
785 ++__first1;
786 break;
787 } // else there is a match, check next elements
788 }
789 }
790}
791
792template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
793_BidirectionalIterator1
794__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
795 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
796 bidirectional_iterator_tag, bidirectional_iterator_tag)
797{
798 // modeled after search algorithm (in reverse)
799 if (__first2 == __last2)
800 return __last1; // Everything matches an empty sequence
801 _BidirectionalIterator1 __l1 = __last1;
802 _BidirectionalIterator2 __l2 = __last2;
803 --__l2;
804 while (true)
805 {
806 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
807 while (true)
808 {
809 if (__first1 == __l1) // return __last1 if no element matches *__first2
810 return __last1;
811 if (__pred(*--__l1, *__l2))
812 break;
813 }
814 // *__l1 matches *__l2, now match elements before here
815 _BidirectionalIterator1 __m1 = __l1;
816 _BidirectionalIterator2 __m2 = __l2;
817 while (true)
818 {
819 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
820 return __m1;
821 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
822 return __last1;
823 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
824 {
825 break;
826 } // else there is a match, check next elements
827 }
828 }
829}
830
831template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
832_RandomAccessIterator1
833__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
834 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
835 random_access_iterator_tag, random_access_iterator_tag)
836{
837 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
838 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
839 if (__len2 == 0)
840 return __last1;
841 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
842 if (__len1 < __len2)
843 return __last1;
844 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
845 _RandomAccessIterator1 __l1 = __last1;
846 _RandomAccessIterator2 __l2 = __last2;
847 --__l2;
848 while (true)
849 {
850 while (true)
851 {
852 if (__s == __l1)
853 return __last1;
854 if (__pred(*--__l1, *__l2))
855 break;
856 }
857 _RandomAccessIterator1 __m1 = __l1;
858 _RandomAccessIterator2 __m2 = __l2;
859 while (true)
860 {
861 if (__m2 == __first2)
862 return __m1;
863 // no need to check range on __m1 because __s guarantees we have enough source
864 if (!__pred(*--__m1, *--__m2))
865 {
866 break;
867 }
868 }
869 }
870}
871
872template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
873inline _LIBCPP_INLINE_VISIBILITY
874_ForwardIterator1
875find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
876 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
877{
878 return _STD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
879 (__first1, __last1, __first2, __last2, __pred,
880 typename iterator_traits<_ForwardIterator1>::iterator_category(),
881 typename iterator_traits<_ForwardIterator2>::iterator_category());
882}
883
884template <class _ForwardIterator1, class _ForwardIterator2>
885inline _LIBCPP_INLINE_VISIBILITY
886_ForwardIterator1
887find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
888 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
889{
890 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
891 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
892 return _STD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
893}
894
895// find_first_of
896
897template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
898_ForwardIterator1
899find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
900 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
901{
902 for (; __first1 != __last1; ++__first1)
903 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
904 if (__pred(*__first1, *__j))
905 return __first1;
906 return __last1;
907}
908
909template <class _ForwardIterator1, class _ForwardIterator2>
910inline _LIBCPP_INLINE_VISIBILITY
911_ForwardIterator1
912find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
913 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
914{
915 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
916 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
917 return _STD::find_first_of(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
918}
919
920// adjacent_find
921
922template <class _ForwardIterator, class _BinaryPredicate>
923inline _LIBCPP_INLINE_VISIBILITY
924_ForwardIterator
925adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
926{
927 if (__first != __last)
928 {
929 _ForwardIterator __i = __first;
930 while (++__i != __last)
931 {
932 if (__pred(*__first, *__i))
933 return __first;
934 __first = __i;
935 }
936 }
937 return __last;
938}
939
940template <class _ForwardIterator>
941inline _LIBCPP_INLINE_VISIBILITY
942_ForwardIterator
943adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
944{
945 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
946 return _STD::adjacent_find(__first, __last, __equal_to<__v>());
947}
948
949// count
950
951template <class _InputIterator, class _Tp>
952inline _LIBCPP_INLINE_VISIBILITY
953typename iterator_traits<_InputIterator>::difference_type
954count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
955{
956 typename iterator_traits<_InputIterator>::difference_type __r(0);
957 for (; __first != __last; ++__first)
958 if (*__first == __value)
959 ++__r;
960 return __r;
961}
962
963// count_if
964
965template <class _InputIterator, class _Predicate>
966inline _LIBCPP_INLINE_VISIBILITY
967typename iterator_traits<_InputIterator>::difference_type
968count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
969{
970 typename iterator_traits<_InputIterator>::difference_type __r(0);
971 for (; __first != __last; ++__first)
972 if (__pred(*__first))
973 ++__r;
974 return __r;
975}
976
977// mismatch
978
979template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
980inline _LIBCPP_INLINE_VISIBILITY
981pair<_InputIterator1, _InputIterator2>
982mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
983 _InputIterator2 __first2, _BinaryPredicate __pred)
984{
985 for (; __first1 != __last1; ++__first1, ++__first2)
986 if (!__pred(*__first1, *__first2))
987 break;
988 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
989}
990
991template <class _InputIterator1, class _InputIterator2>
992inline _LIBCPP_INLINE_VISIBILITY
993pair<_InputIterator1, _InputIterator2>
994mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
995{
996 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
997 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
998 return _STD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
999}
1000
1001// equal
1002
1003template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1004inline _LIBCPP_INLINE_VISIBILITY
1005bool
1006equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1007{
1008 for (; __first1 != __last1; ++__first1, ++__first2)
1009 if (!__pred(*__first1, *__first2))
1010 return false;
1011 return true;
1012}
1013
1014template <class _InputIterator1, class _InputIterator2>
1015inline _LIBCPP_INLINE_VISIBILITY
1016bool
1017equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1018{
1019 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1020 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1021 return _STD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1022}
1023
1024// is_permutation
1025
1026template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1027bool
1028is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1029 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1030{
1031 // shorten sequences as much as possible by lopping of any equal parts
1032 for (; __first1 != __last1; ++__first1, ++__first2)
1033 if (!__pred(*__first1, *__first2))
1034 goto __not_done;
1035 return true;
1036__not_done:
1037 // __first1 != __last1 && *__first1 != *__first2
1038 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1039 _D1 __l1 = _STD::distance(__first1, __last1);
1040 if (__l1 == _D1(1))
1041 return false;
1042 _ForwardIterator2 __last2 = _STD::next(__first2, __l1);
1043 // For each element in [f1, l1) see if there are the same number of
1044 // equal elements in [f2, l2)
1045 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1046 {
1047 // Have we already counted the number of *__i in [f1, l1)?
1048 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1049 if (__pred(*__j, *__i))
1050 goto __next_iter;
1051 {
1052 // Count number of *__i in [f2, l2)
1053 _D1 __c2 = 0;
1054 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1055 if (__pred(*__i, *__j))
1056 ++__c2;
1057 if (__c2 == 0)
1058 return false;
1059 // Count number of *__i in [__i, l1) (we can start with 1)
1060 _D1 __c1 = 1;
1061 for (_ForwardIterator1 __j = _STD::next(__i); __j != __last1; ++__j)
1062 if (__pred(*__i, *__j))
1063 ++__c1;
1064 if (__c1 != __c2)
1065 return false;
1066 }
1067__next_iter:;
1068 }
1069 return true;
1070}
1071
1072template<class _ForwardIterator1, class _ForwardIterator2>
1073inline _LIBCPP_INLINE_VISIBILITY
1074bool
1075is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1076 _ForwardIterator2 __first2)
1077{
1078 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1079 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1080 return _STD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
1081}
1082
1083// search
1084
1085template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1086_ForwardIterator1
1087__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1088 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
1089 forward_iterator_tag, forward_iterator_tag)
1090{
1091 if (__first2 == __last2)
1092 return __first1; // Everything matches an empty sequence
1093 while (true)
1094 {
1095 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
1096 while (true)
1097 {
1098 if (__first1 == __last1) // return __last1 if no element matches *__first2
1099 return __last1;
1100 if (__pred(*__first1, *__first2))
1101 break;
1102 ++__first1;
1103 }
1104 // *__first1 matches *__first2, now match elements after here
1105 _ForwardIterator1 __m1 = __first1;
1106 _ForwardIterator2 __m2 = __first2;
1107 while (true)
1108 {
1109 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
1110 return __first1;
1111 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
1112 return __last1;
1113 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
1114 {
1115 ++__first1;
1116 break;
1117 } // else there is a match, check next elements
1118 }
1119 }
1120}
1121
1122template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1123_RandomAccessIterator1
1124__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1125 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1126 random_access_iterator_tag, random_access_iterator_tag)
1127{
1128 typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _D1;
1129 typedef typename std::iterator_traits<_RandomAccessIterator2>::difference_type _D2;
1130 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1131 _D2 __len2 = __last2 - __first2;
1132 if (__len2 == 0)
1133 return __first1;
1134 _D1 __len1 = __last1 - __first1;
1135 if (__len1 < __len2)
1136 return __last1;
1137 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
1138 while (true)
1139 {
1140#if !_LIBCPP_UNROLL_LOOPS
1141 while (true)
1142 {
1143 if (__first1 == __s)
1144 return __last1;
1145 if (__pred(*__first1, *__first2))
1146 break;
1147 ++__first1;
1148 }
1149#else // _LIBCPP_UNROLL_LOOPS
1150 for (_D1 __loop_unroll = (__s - __first1) / 4; __loop_unroll > 0; --__loop_unroll)
1151 {
1152 if (__pred(*__first1, *__first2))
1153 goto __phase2;
1154 if (__pred(*++__first1, *__first2))
1155 goto __phase2;
1156 if (__pred(*++__first1, *__first2))
1157 goto __phase2;
1158 if (__pred(*++__first1, *__first2))
1159 goto __phase2;
1160 ++__first1;
1161 }
1162 switch (__s - __first1)
1163 {
1164 case 3:
1165 if (__pred(*__first1, *__first2))
1166 break;
1167 ++__first1;
1168 case 2:
1169 if (__pred(*__first1, *__first2))
1170 break;
1171 ++__first1;
1172 case 1:
1173 if (__pred(*__first1, *__first2))
1174 break;
1175 case 0:
1176 return __last1;
1177 }
1178 __phase2:
1179#endif // _LIBCPP_UNROLL_LOOPS
1180 _RandomAccessIterator1 __m1 = __first1;
1181 _RandomAccessIterator2 __m2 = __first2;
1182#if !_LIBCPP_UNROLL_LOOPS
1183 while (true)
1184 {
1185 if (++__m2 == __last2)
1186 return __first1;
1187 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
1188 if (!__pred(*__m1, *__m2))
1189 {
1190 ++__first1;
1191 break;
1192 }
1193 }
1194#else // _LIBCPP_UNROLL_LOOPS
1195 ++__m2;
1196 ++__m1;
1197 for (_D2 __loop_unroll = (__last2 - __m2) / 4; __loop_unroll > 0; --__loop_unroll)
1198 {
1199 if (!__pred(*__m1, *__m2))
1200 goto __continue;
1201 if (!__pred(*++__m1, *++__m2))
1202 goto __continue;
1203 if (!__pred(*++__m1, *++__m2))
1204 goto __continue;
1205 if (!__pred(*++__m1, *++__m2))
1206 goto __continue;
1207 ++__m1;
1208 ++__m2;
1209 }
1210 switch (__last2 - __m2)
1211 {
1212 case 3:
1213 if (!__pred(*__m1, *__m2))
1214 break;
1215 ++__m1;
1216 ++__m2;
1217 case 2:
1218 if (!__pred(*__m1, *__m2))
1219 break;
1220 ++__m1;
1221 ++__m2;
1222 case 1:
1223 if (!__pred(*__m1, *__m2))
1224 break;
1225 case 0:
1226 return __first1;
1227 }
1228 __continue:
1229 ++__first1;
1230#endif // _LIBCPP_UNROLL_LOOPS
1231 }
1232}
1233
1234template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1235inline _LIBCPP_INLINE_VISIBILITY
1236_ForwardIterator1
1237search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1238 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1239{
1240 return _STD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
1241 (__first1, __last1, __first2, __last2, __pred,
1242 typename std::iterator_traits<_ForwardIterator1>::iterator_category(),
1243 typename std::iterator_traits<_ForwardIterator2>::iterator_category());
1244}
1245
1246template <class _ForwardIterator1, class _ForwardIterator2>
1247inline _LIBCPP_INLINE_VISIBILITY
1248_ForwardIterator1
1249search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1250 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1251{
1252 typedef typename std::iterator_traits<_ForwardIterator1>::value_type __v1;
1253 typedef typename std::iterator_traits<_ForwardIterator2>::value_type __v2;
1254 return _STD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1255}
1256
1257// search_n
1258
1259template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
1260_ForwardIterator
1261__search_n(_ForwardIterator __first, _ForwardIterator __last,
1262 _Size __count, const _Tp& __value, _BinaryPredicate __pred, forward_iterator_tag)
1263{
1264 if (__count <= 0)
1265 return __first;
1266 while (true)
1267 {
1268 // Find first element in sequence that matchs __value, with a mininum of loop checks
1269 while (true)
1270 {
1271 if (__first == __last) // return __last if no element matches __value
1272 return __last;
1273 if (__pred(*__first, __value))
1274 break;
1275 ++__first;
1276 }
1277 // *__first matches __value, now match elements after here
1278 _ForwardIterator __m = __first;
1279 _Size __c(0);
1280 while (true)
1281 {
1282 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1283 return __first;
1284 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1285 return __last;
1286 if (!__pred(*__m, __value)) // if there is a mismatch, restart with a new __first
1287 {
1288 __first = __m;
1289 ++__first;
1290 break;
1291 } // else there is a match, check next elements
1292 }
1293 }
1294}
1295
1296template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
1297_RandomAccessIterator
1298__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
1299 _Size __count, const _Tp& __value, _BinaryPredicate __pred, random_access_iterator_tag)
1300{
1301 if (__count <= 0)
1302 return __first;
1303 _Size __len = static_cast<_Size>(__last - __first);
1304 if (__len < __count)
1305 return __last;
1306 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1307 while (true)
1308 {
1309 // Find first element in sequence that matchs __value, with a mininum of loop checks
1310 while (true)
1311 {
1312 if (__first == __s) // return __last if no element matches __value
1313 return __last;
1314 if (__pred(*__first, __value))
1315 break;
1316 ++__first;
1317 }
1318 // *__first matches __value, now match elements after here
1319 _RandomAccessIterator __m = __first;
1320 _Size __c(0);
1321 while (true)
1322 {
1323 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1324 return __first;
1325 ++__m; // no need to check range on __m because __s guarantees we have enough source
1326 if (!__pred(*__m, __value)) // if there is a mismatch, restart with a new __first
1327 {
1328 __first = __m;
1329 ++__first;
1330 break;
1331 } // else there is a match, check next elements
1332 }
1333 }
1334}
1335
1336template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
1337inline _LIBCPP_INLINE_VISIBILITY
1338_ForwardIterator
1339search_n(_ForwardIterator __first, _ForwardIterator __last,
1340 _Size __count, const _Tp& __value, _BinaryPredicate __pred)
1341{
1342 return _STD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
1343 (__first, __last, __count, __value, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
1344}
1345
1346template <class _ForwardIterator, class _Size, class _Tp>
1347inline _LIBCPP_INLINE_VISIBILITY
1348_ForwardIterator
1349search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value)
1350{
1351 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1352 return _STD::search_n(__first, __last, __count, __value, __equal_to<__v, _Tp>());
1353}
1354
1355// copy
1356
1357template <class _Iter>
1358struct __libcpp_is_trivial_iterator
1359{
1360 static const bool value = is_pointer<_Iter>::value;
1361};
1362
1363template <class _Iter>
1364struct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
1365{
1366 static const bool value = is_pointer<_Iter>::value;
1367};
1368
1369template <class _Iter>
1370struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
1371{
1372 static const bool value = is_pointer<_Iter>::value;
1373};
1374
1375template <class _Iter>
1376inline _LIBCPP_INLINE_VISIBILITY
1377_Iter
1378__unwrap_iter(_Iter __i)
1379{
1380 return __i;
1381}
1382
1383template <class _Tp>
1384inline _LIBCPP_INLINE_VISIBILITY
1385typename enable_if
1386<
1387 has_trivial_copy_assign<_Tp>::value,
1388 _Tp*
1389>::type
1390__unwrap_iter(move_iterator<_Tp*> __i)
1391{
1392 return __i.base();
1393}
1394
1395template <class _Tp>
1396inline _LIBCPP_INLINE_VISIBILITY
1397typename enable_if
1398<
1399 has_trivial_copy_assign<_Tp>::value,
1400 _Tp*
1401>::type
1402__unwrap_iter(__wrap_iter<_Tp*> __i)
1403{
1404 return __i.base();
1405}
1406
1407template <class _InputIterator, class _OutputIterator>
1408inline _LIBCPP_INLINE_VISIBILITY
1409_OutputIterator
1410__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1411{
1412 for (; __first != __last; ++__first, ++__result)
1413 *__result = *__first;
1414 return __result;
1415}
1416
1417template <class _Tp, class _Up>
1418inline _LIBCPP_INLINE_VISIBILITY
1419typename enable_if
1420<
1421 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1422 has_trivial_copy_assign<_Up>::value,
1423 _Up*
1424>::type
1425__copy(_Tp* __first, _Tp* __last, _Up* __result)
1426{
1427 const size_t __n = static_cast<size_t>(__last - __first);
1428 _STD::memmove(__result, __first, __n * sizeof(_Up));
1429 return __result + __n;
1430}
1431
1432template <class _InputIterator, class _OutputIterator>
1433inline _LIBCPP_INLINE_VISIBILITY
1434_OutputIterator
1435copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1436{
1437 return _STD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1438}
1439
1440// copy_backward
1441
1442template <class _InputIterator, class _OutputIterator>
1443inline _LIBCPP_INLINE_VISIBILITY
1444_OutputIterator
1445__copy_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1446{
1447 while (__first != __last)
1448 *--__result = *--__last;
1449 return __result;
1450}
1451
1452template <class _Tp, class _Up>
1453inline _LIBCPP_INLINE_VISIBILITY
1454typename enable_if
1455<
1456 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1457 has_trivial_copy_assign<_Up>::value,
1458 _Up*
1459>::type
1460__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1461{
1462 const size_t __n = static_cast<size_t>(__last - __first);
1463 __result -= __n;
1464 _STD::memmove(__result, __first, __n * sizeof(_Up));
1465 return __result;
1466}
1467
1468template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1469inline _LIBCPP_INLINE_VISIBILITY
1470_BidirectionalIterator2
1471copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1472 _BidirectionalIterator2 __result)
1473{
1474 return _STD::__copy_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1475}
1476
1477// copy_if
1478
1479template<class _InputIterator, class _OutputIterator, class _Predicate>
1480inline _LIBCPP_INLINE_VISIBILITY
1481_OutputIterator
1482copy_if(_InputIterator __first, _InputIterator __last,
1483 _OutputIterator __result, _Predicate __pred)
1484{
1485 for (; __first != __last; ++__first)
1486 {
1487 if (__pred(*__first))
1488 {
1489 *__result = *__first;
1490 ++__result;
1491 }
1492 }
1493 return __result;
1494}
1495
1496// copy_n
1497
1498template<class _InputIterator, class _Size, class _OutputIterator>
1499inline _LIBCPP_INLINE_VISIBILITY
1500typename enable_if
1501<
1502 __is_input_iterator<_InputIterator>::value &&
1503 !__is_random_access_iterator<_InputIterator>::value,
1504 _OutputIterator
1505>::type
1506copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1507{
1508 for (; __n > 0; --__n, ++__first, ++__result)
1509 *__result = *__first;
1510 return __result;
1511}
1512
1513template<class _InputIterator, class _Size, class _OutputIterator>
1514inline _LIBCPP_INLINE_VISIBILITY
1515typename enable_if
1516<
1517 __is_random_access_iterator<_InputIterator>::value,
1518 _OutputIterator
1519>::type
1520copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1521{
1522 return copy(__first, __first + __n, __result);
1523}
1524
1525// move
1526
1527template <class _InputIterator, class _OutputIterator>
1528inline _LIBCPP_INLINE_VISIBILITY
1529_OutputIterator
1530__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1531{
1532 for (; __first != __last; ++__first, ++__result)
1533 *__result = _STD::move(*__first);
1534 return __result;
1535}
1536
1537template <class _Tp, class _Up>
1538inline _LIBCPP_INLINE_VISIBILITY
1539typename enable_if
1540<
1541 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1542 has_trivial_copy_assign<_Up>::value,
1543 _Up*
1544>::type
1545__move(_Tp* __first, _Tp* __last, _Up* __result)
1546{
1547 const size_t __n = static_cast<size_t>(__last - __first);
1548 _STD::memmove(__result, __first, __n * sizeof(_Up));
1549 return __result + __n;
1550}
1551
1552template <class _InputIterator, class _OutputIterator>
1553inline _LIBCPP_INLINE_VISIBILITY
1554_OutputIterator
1555move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1556{
1557 return _STD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1558}
1559
1560// move_backward
1561
1562template <class _InputIterator, class _OutputIterator>
1563inline _LIBCPP_INLINE_VISIBILITY
1564_OutputIterator
1565__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1566{
1567 while (__first != __last)
1568 *--__result = _STD::move(*--__last);
1569 return __result;
1570}
1571
1572template <class _Tp, class _Up>
1573inline _LIBCPP_INLINE_VISIBILITY
1574typename enable_if
1575<
1576 is_same<typename remove_const<_Tp>::type, _Up>::value &&
1577 has_trivial_copy_assign<_Up>::value,
1578 _Up*
1579>::type
1580__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1581{
1582 const size_t __n = static_cast<size_t>(__last - __first);
1583 __result -= __n;
1584 _STD::memmove(__result, __first, __n * sizeof(_Up));
1585 return __result;
1586}
1587
1588template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1589inline _LIBCPP_INLINE_VISIBILITY
1590_BidirectionalIterator2
1591move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1592 _BidirectionalIterator2 __result)
1593{
1594 return _STD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
1595}
1596
1597// iter_swap
1598
1599template <class _ForwardIterator1, class _ForwardIterator2>
1600inline _LIBCPP_INLINE_VISIBILITY
1601void
1602iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
1603{
1604 swap(*__a, *__b);
1605}
1606
1607// transform
1608
1609template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
1610inline _LIBCPP_INLINE_VISIBILITY
1611_OutputIterator
1612transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1613{
1614 for (; __first != __last; ++__first, ++__result)
1615 *__result = __op(*__first);
1616 return __result;
1617}
1618
1619template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
1620inline _LIBCPP_INLINE_VISIBILITY
1621_OutputIterator
1622transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1623 _OutputIterator __result, _BinaryOperation __binary_op)
1624{
1625 for (; __first1 != __last1; ++__first1, ++__first2, ++__result)
1626 *__result = __binary_op(*__first1, *__first2);
1627 return __result;
1628}
1629
1630// replace
1631
1632template <class _ForwardIterator, class _Tp>
1633inline _LIBCPP_INLINE_VISIBILITY
1634void
1635replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1636{
1637 for (; __first != __last; ++__first)
1638 if (*__first == __old_value)
1639 *__first = __new_value;
1640}
1641
1642// replace_if
1643
1644template <class _ForwardIterator, class _Predicate, class _Tp>
1645inline _LIBCPP_INLINE_VISIBILITY
1646void
1647replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1648{
1649 for (; __first != __last; ++__first)
1650 if (__pred(*__first))
1651 *__first = __new_value;
1652}
1653
1654// replace_copy
1655
1656template <class _InputIterator, class _OutputIterator, class _Tp>
1657inline _LIBCPP_INLINE_VISIBILITY
1658_OutputIterator
1659replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1660 const _Tp& __old_value, const _Tp& __new_value)
1661{
1662 for (; __first != __last; ++__first, ++__result)
1663 if (*__first == __old_value)
1664 *__result = __new_value;
1665 else
1666 *__result = *__first;
1667 return __result;
1668}
1669
1670// replace_copy_if
1671
1672template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
1673inline _LIBCPP_INLINE_VISIBILITY
1674_OutputIterator
1675replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1676 _Predicate __pred, const _Tp& __new_value)
1677{
1678 for (; __first != __last; ++__first, ++__result)
1679 if (__pred(*__first))
1680 *__result = __new_value;
1681 else
1682 *__result = *__first;
1683 return __result;
1684}
1685
1686// fill_n
1687
1688template <class _OutputIterator, class _Size, class _Tp>
1689inline _LIBCPP_INLINE_VISIBILITY
1690_OutputIterator
1691__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value, false_type)
1692{
1693 for (; __n > 0; ++__first, --__n)
1694 *__first = __value;
1695 return __first;
1696}
1697
1698template <class _OutputIterator, class _Size, class _Tp>
1699inline _LIBCPP_INLINE_VISIBILITY
1700_OutputIterator
1701__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value, true_type)
1702{
1703 if (__n > 0)
1704 _STD::memset(__first, (unsigned char)__value, (size_t)(__n));
1705 return __first + __n;
1706}
1707
1708template <class _OutputIterator, class _Size, class _Tp>
1709inline _LIBCPP_INLINE_VISIBILITY
1710_OutputIterator
1711fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
1712{
1713 return _STD::__fill_n(__first, __n, __value, integral_constant<bool,
1714 is_pointer<_OutputIterator>::value &&
1715 has_trivial_copy_assign<_Tp>::value &&
1716 sizeof(_Tp) == 1>());
1717}
1718
1719// fill
1720
1721template <class _ForwardIterator, class _Tp>
1722inline _LIBCPP_INLINE_VISIBILITY
1723void
1724__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, forward_iterator_tag)
1725{
1726 for (; __first != __last; ++__first)
1727 *__first = __value;
1728}
1729
1730template <class _RandomAccessIterator, class _Tp>
1731inline _LIBCPP_INLINE_VISIBILITY
1732void
1733__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value, random_access_iterator_tag)
1734{
1735 _STD::fill_n(__first, __last - __first, __value);
1736}
1737
1738template <class _ForwardIterator, class _Tp>
1739inline _LIBCPP_INLINE_VISIBILITY
1740void
1741fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
1742{
1743 _STD::__fill(__first, __last, __value, typename iterator_traits<_ForwardIterator>::iterator_category());
1744}
1745
1746// generate
1747
1748template <class _ForwardIterator, class _Generator>
1749inline _LIBCPP_INLINE_VISIBILITY
1750void
1751generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
1752{
1753 for (; __first != __last; ++__first)
1754 *__first = __gen();
1755}
1756
1757// generate_n
1758
1759template <class _OutputIterator, class _Size, class _Generator>
1760inline _LIBCPP_INLINE_VISIBILITY
1761_OutputIterator
1762generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
1763{
1764 for (; __n > 0; ++__first, --__n)
1765 *__first = __gen();
1766 return __first;
1767}
1768
1769// remove
1770
1771template <class _ForwardIterator, class _Tp>
1772_ForwardIterator
1773remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
1774{
1775 __first = _STD::find(__first, __last, __value);
1776 if (__first != __last)
1777 {
1778 _ForwardIterator __i = __first;
1779 while (++__i != __last)
1780 {
1781 if (!(*__i == __value))
1782 {
1783 *__first = _STD::move(*__i);
1784 ++__first;
1785 }
1786 }
1787 }
1788 return __first;
1789}
1790
1791// remove_if
1792
1793template <class _ForwardIterator, class _Predicate>
1794_ForwardIterator
1795remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
1796{
1797 __first = _STD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
1798 (__first, __last, __pred);
1799 if (__first != __last)
1800 {
1801 _ForwardIterator __i = __first;
1802 while (++__i != __last)
1803 {
1804 if (!__pred(*__i))
1805 {
1806 *__first = _STD::move(*__i);
1807 ++__first;
1808 }
1809 }
1810 }
1811 return __first;
1812}
1813
1814// remove_copy
1815
1816template <class _InputIterator, class _OutputIterator, class _Tp>
1817inline _LIBCPP_INLINE_VISIBILITY
1818_OutputIterator
1819remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value)
1820{
1821 for (; __first != __last; ++__first)
1822 {
1823 if (!(*__first == __value))
1824 {
1825 *__result = *__first;
1826 ++__result;
1827 }
1828 }
1829 return __result;
1830}
1831
1832// remove_copy_if
1833
1834template <class _InputIterator, class _OutputIterator, class _Predicate>
1835inline _LIBCPP_INLINE_VISIBILITY
1836_OutputIterator
1837remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
1838{
1839 for (; __first != __last; ++__first)
1840 {
1841 if (!__pred(*__first))
1842 {
1843 *__result = *__first;
1844 ++__result;
1845 }
1846 }
1847 return __result;
1848}
1849
1850// unique
1851
1852template <class _ForwardIterator, class _BinaryPredicate>
1853_ForwardIterator
1854unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1855{
1856 __first = _STD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
1857 (__first, __last, __pred);
1858 if (__first != __last)
1859 {
1860 // ... a a ? ...
1861 // f i
1862 _ForwardIterator __i = __first;
1863 for (++__i; ++__i != __last;)
1864 if (!__pred(*__first, *__i))
1865 *++__first = _STD::move(*__i);
1866 ++__first;
1867 }
1868 return __first;
1869}
1870
1871template <class _ForwardIterator>
1872inline _LIBCPP_INLINE_VISIBILITY
1873_ForwardIterator
1874unique(_ForwardIterator __first, _ForwardIterator __last)
1875{
1876 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
1877 return _STD::unique(__first, __last, __equal_to<__v>());
1878}
1879
1880// unique_copy
1881
1882template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
1883_OutputIterator
1884__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
1885 input_iterator_tag, output_iterator_tag)
1886{
1887 if (__first != __last)
1888 {
1889 typename iterator_traits<_InputIterator>::value_type __t(*__first);
1890 *__result = __t;
1891 ++__result;
1892 while (++__first != __last)
1893 {
1894 if (!__pred(__t, *__first))
1895 {
1896 __t = *__first;
1897 *__result = __t;
1898 ++__result;
1899 }
1900 }
1901 }
1902 return __result;
1903}
1904
1905template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
1906_OutputIterator
1907__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
1908 forward_iterator_tag, output_iterator_tag)
1909{
1910 if (__first != __last)
1911 {
1912 _ForwardIterator __i = __first;
1913 *__result = *__i;
1914 ++__result;
1915 while (++__first != __last)
1916 {
1917 if (!__pred(*__i, *__first))
1918 {
1919 *__result = *__first;
1920 ++__result;
1921 __i = __first;
1922 }
1923 }
1924 }
1925 return __result;
1926}
1927
1928template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
1929_ForwardIterator
1930__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
1931 input_iterator_tag, forward_iterator_tag)
1932{
1933 if (__first != __last)
1934 {
1935 *__result = *__first;
1936 while (++__first != __last)
1937 if (!__pred(*__result, *__first))
1938 *++__result = *__first;
1939 ++__result;
1940 }
1941 return __result;
1942}
1943
1944
1945template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
1946inline _LIBCPP_INLINE_VISIBILITY
1947_OutputIterator
1948unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
1949{
1950 return _STD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
1951 (__first, __last, __result, __pred,
1952 typename iterator_traits<_InputIterator>::iterator_category(),
1953 typename iterator_traits<_OutputIterator>::iterator_category());
1954}
1955
1956template <class _InputIterator, class _OutputIterator>
1957inline _LIBCPP_INLINE_VISIBILITY
1958_OutputIterator
1959unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1960{
1961 typedef typename iterator_traits<_InputIterator>::value_type __v;
1962 return _STD::unique_copy(__first, __last, __result, __equal_to<__v>());
1963}
1964
1965// reverse
1966
1967template <class _BidirectionalIterator>
1968inline _LIBCPP_INLINE_VISIBILITY
1969void
1970__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
1971{
1972 while (__first != __last)
1973 {
1974 if (__first == --__last)
1975 break;
1976 swap(*__first, *__last);
1977 ++__first;
1978 }
1979}
1980
1981template <class _RandomAccessIterator>
1982inline _LIBCPP_INLINE_VISIBILITY
1983void
1984__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
1985{
1986 if (__first != __last)
1987 for (; __first < --__last; ++__first)
1988 swap(*__first, *__last);
1989}
1990
1991template <class _BidirectionalIterator>
1992inline _LIBCPP_INLINE_VISIBILITY
1993void
1994reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
1995{
1996 _STD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
1997}
1998
1999// reverse_copy
2000
2001template <class _BidirectionalIterator, class _OutputIterator>
2002inline _LIBCPP_INLINE_VISIBILITY
2003_OutputIterator
2004reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2005{
2006 for (; __first != __last; ++__result)
2007 *__result = *--__last;
2008 return __result;
2009}
2010
2011// rotate
2012
2013template <class _ForwardIterator>
2014_ForwardIterator
2015__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, false_type)
2016{
2017 if (__first == __middle)
2018 return __last;
2019 if (__middle == __last)
2020 return __first;
2021 _ForwardIterator __i = __middle;
2022 while (true)
2023 {
2024 swap(*__first, *__i);
2025 ++__first;
2026 if (++__i == __last)
2027 break;
2028 if (__first == __middle)
2029 __middle = __i;
2030 }
2031 _ForwardIterator __r = __first;
2032 if (__first != __middle)
2033 {
2034 __i = __middle;
2035 while (true)
2036 {
2037 swap(*__first, *__i);
2038 ++__first;
2039 if (++__i == __last)
2040 {
2041 if (__first == __middle)
2042 break;
2043 __i = __middle;
2044 }
2045 else if (__first == __middle)
2046 __middle = __i;
2047 }
2048 }
2049 return __r;
2050}
2051
2052template<typename _Integral>
2053inline _LIBCPP_INLINE_VISIBILITY
2054_Integral
2055__gcd(_Integral __x, _Integral __y)
2056{
2057 do
2058 {
2059 _Integral __t = __x % __y;
2060 __x = __y;
2061 __y = __t;
2062 } while (__y);
2063 return __x;
2064}
2065
2066template<typename _RandomAccessIterator>
2067_RandomAccessIterator
2068__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last, true_type)
2069{
2070 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2071 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
2072
2073 if (__first == __middle)
2074 return __last;
2075 if (__middle == __last)
2076 return __first;
2077 const difference_type __m1 = __middle - __first;
2078 const difference_type __m2 = __last - __middle;
2079 if (__m1 == __m2)
2080 {
2081 _STD::swap_ranges(__first, __middle, __middle);
2082 return __middle;
2083 }
2084 const difference_type __g = __gcd(__m1, __m2);
2085 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2086 {
2087 value_type __t(*--__p);
2088 _RandomAccessIterator __p1 = __p;
2089 _RandomAccessIterator __p2 = __p1 + __m1;
2090 do
2091 {
2092 *__p1 = *__p2;
2093 __p1 = __p2;
2094 const difference_type __d = __last - __p2;
2095 if (__m1 < __d)
2096 __p2 += __m1;
2097 else
2098 __p2 = __first + (__m1 - __d);
2099 } while (__p2 != __p);
2100 *__p1 = __t;
2101 }
2102 return __first + __m2;
2103}
2104
2105template <class _ForwardIterator>
2106inline _LIBCPP_INLINE_VISIBILITY
2107_ForwardIterator
2108rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2109{
2110 return _STD::__rotate(__first, __middle, __last,
2111 integral_constant
2112 <
2113 bool,
2114 is_convertible
2115 <
2116 typename iterator_traits<_ForwardIterator>::iterator_category,
2117 random_access_iterator_tag
2118 >::value &&
2119 has_trivial_copy_assign
2120 <
2121 typename iterator_traits<_ForwardIterator>::value_type
2122 >::value
2123 >());
2124}
2125
2126// rotate_copy
2127
2128template <class _ForwardIterator, class _OutputIterator>
2129inline _LIBCPP_INLINE_VISIBILITY
2130_OutputIterator
2131rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2132{
2133 return _STD::copy(__first, __middle, _STD::copy(__middle, __last, __result));
2134}
2135
2136// min
2137
2138template <class _Tp, class _Compare>
2139inline _LIBCPP_INLINE_VISIBILITY
2140const _Tp&
2141min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2142{
2143 return __comp(__b, __a) ? __b : __a;
2144}
2145
2146template <class _Tp>
2147inline _LIBCPP_INLINE_VISIBILITY
2148const _Tp&
2149min(const _Tp& __a, const _Tp& __b)
2150{
2151 return _STD::min(__a, __b, __less<_Tp>());
2152}
2153
2154// max
2155
2156template <class _Tp, class _Compare>
2157inline _LIBCPP_INLINE_VISIBILITY
2158const _Tp&
2159max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2160{
2161 return __comp(__a, __b) ? __b : __a;
2162}
2163
2164template <class _Tp>
2165inline _LIBCPP_INLINE_VISIBILITY
2166const _Tp&
2167max(const _Tp& __a, const _Tp& __b)
2168{
2169 return _STD::max(__a, __b, __less<_Tp>());
2170}
2171
2172// min_element
2173
2174template <class _ForwardIterator, class _Compare>
2175inline _LIBCPP_INLINE_VISIBILITY
2176_ForwardIterator
2177min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2178{
2179 if (__first != __last)
2180 {
2181 _ForwardIterator __i = __first;
2182 while (++__i != __last)
2183 if (__comp(*__i, *__first))
2184 __first = __i;
2185 }
2186 return __first;
2187}
2188
2189template <class _ForwardIterator>
2190inline _LIBCPP_INLINE_VISIBILITY
2191_ForwardIterator
2192min_element(_ForwardIterator __first, _ForwardIterator __last)
2193{
2194 return _STD::min_element(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
2195}
2196
2197// max_element
2198
2199template <class _ForwardIterator, class _Compare>
2200inline _LIBCPP_INLINE_VISIBILITY
2201_ForwardIterator
2202max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2203{
2204 if (__first != __last)
2205 {
2206 _ForwardIterator __i = __first;
2207 while (++__i != __last)
2208 if (__comp(*__first, *__i))
2209 __first = __i;
2210 }
2211 return __first;
2212}
2213
2214template <class _ForwardIterator>
2215inline _LIBCPP_INLINE_VISIBILITY
2216_ForwardIterator
2217max_element(_ForwardIterator __first, _ForwardIterator __last)
2218{
2219 return _STD::max_element(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
2220}
2221
2222// minmax_element
2223
2224template <class _ForwardIterator, class _Compare>
2225std::pair<_ForwardIterator, _ForwardIterator>
2226minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2227{
2228 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2229 if (__first != __last)
2230 {
2231 if (++__first != __last)
2232 {
2233 if (__comp(*__first, *__result.first))
2234 {
2235 __result.second = __result.first;
2236 __result.first = __first;
2237 }
2238 else
2239 __result.second = __first;
2240 while (++__first != __last)
2241 {
2242 _ForwardIterator __i = __first;
2243 if (++__first == __last)
2244 {
2245 if (__comp(*__i, *__result.first))
2246 __result.first = __i;
2247 else if (!__comp(*__i, *__result.second))
2248 __result.second = __i;
2249 break;
2250 }
2251 else
2252 {
2253 if (__comp(*__first, *__i))
2254 {
2255 if (__comp(*__first, *__result.first))
2256 __result.first = __first;
2257 if (!__comp(*__i, *__result.second))
2258 __result.second = __i;
2259 }
2260 else
2261 {
2262 if (__comp(*__i, *__result.first))
2263 __result.first = __i;
2264 if (!__comp(*__first, *__result.second))
2265 __result.second = __first;
2266 }
2267 }
2268 }
2269 }
2270 }
2271 return __result;
2272}
2273
2274template <class _ForwardIterator>
2275inline _LIBCPP_INLINE_VISIBILITY
2276std::pair<_ForwardIterator, _ForwardIterator>
2277minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2278{
2279 return _STD::minmax_element(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
2280}
2281
2282// random_shuffle
2283
Howard Hinnantc3267212010-05-26 17:49:34 +00002284// __independent_bits_engine
2285
2286template <unsigned long long _X, size_t _R>
2287struct __log2_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002288{
Howard Hinnantc3267212010-05-26 17:49:34 +00002289 static const size_t value = _X & ((unsigned long long)(1) << _R) ? _R
2290 : __log2_imp<_X, _R - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002291};
2292
Howard Hinnantc3267212010-05-26 17:49:34 +00002293template <unsigned long long _X>
2294struct __log2_imp<_X, 0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002295{
Howard Hinnantc3267212010-05-26 17:49:34 +00002296 static const size_t value = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002297};
2298
Howard Hinnantc3267212010-05-26 17:49:34 +00002299template <size_t _R>
2300struct __log2_imp<0, _R>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002301{
Howard Hinnantc3267212010-05-26 17:49:34 +00002302 static const size_t value = _R + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002303};
2304
Howard Hinnantc3267212010-05-26 17:49:34 +00002305template <class _UI, _UI _X>
2306struct __log2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002307{
Howard Hinnantc3267212010-05-26 17:49:34 +00002308 static const size_t value = __log2_imp<_X,
2309 sizeof(_UI) * __CHAR_BIT__ - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002310};
2311
Howard Hinnantc3267212010-05-26 17:49:34 +00002312template<class _Engine, class _UIntType>
2313class __independent_bits_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002314{
Howard Hinnantc3267212010-05-26 17:49:34 +00002315public:
2316 // types
2317 typedef _UIntType result_type;
2318
2319private:
2320 typedef typename _Engine::result_type _Engine_result_type;
2321 typedef typename conditional
2322 <
2323 sizeof(_Engine_result_type) <= sizeof(result_type),
2324 result_type,
2325 _Engine_result_type
2326 >::type _Working_result_type;
2327
2328 _Engine& __e_;
2329 size_t __w_;
2330 size_t __w0_;
2331 size_t __n_;
2332 size_t __n0_;
2333 _Working_result_type __y0_;
2334 _Working_result_type __y1_;
2335 _Engine_result_type __mask0_;
2336 _Engine_result_type __mask1_;
2337
2338 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
2339 + _Working_result_type(1);
2340 static const size_t __m = __log2<_Working_result_type, _R>::value;
2341 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2342 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2343
2344public:
2345 // constructors and seeding functions
2346 __independent_bits_engine(_Engine& __e, size_t __w);
2347
2348 // generating functions
2349 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2350
2351private:
2352 result_type __eval(false_type);
2353 result_type __eval(true_type);
2354};
2355
2356template<class _Engine, class _UIntType>
2357__independent_bits_engine<_Engine, _UIntType>
2358 ::__independent_bits_engine(_Engine& __e, size_t __w)
2359 : __e_(__e),
2360 __w_(__w)
2361{
2362 __n_ = __w_ / __m + (__w_ % __m != 0);
2363 __w0_ = __w_ / __n_;
2364 if (_R == 0)
2365 __y0_ = _R;
2366 else if (__w0_ < _WDt)
2367 __y0_ = (_R >> __w0_) << __w0_;
2368 else
2369 __y0_ = 0;
2370 if (_R - __y0_ > __y0_ / __n_)
2371 {
2372 ++__n_;
2373 __w0_ = __w_ / __n_;
2374 if (__w0_ < _WDt)
2375 __y0_ = (_R >> __w0_) << __w0_;
2376 else
2377 __y0_ = 0;
2378 }
2379 __n0_ = __n_ - __w_ % __n_;
2380 if (__w0_ < _WDt - 1)
2381 __y1_ = (_R >> (__w0_ + 1)) << (__w0_ + 1);
2382 else
2383 __y1_ = 0;
2384 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2385 _Engine_result_type(0);
2386 __mask1_ = __w0_ < _EDt - 1 ?
2387 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2388 _Engine_result_type(~0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002389}
2390
Howard Hinnantc3267212010-05-26 17:49:34 +00002391template<class _Engine, class _UIntType>
2392inline
2393_UIntType
2394__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002395{
Howard Hinnantc3267212010-05-26 17:49:34 +00002396 return static_cast<result_type>(__e_() & __mask0_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002397}
2398
Howard Hinnantc3267212010-05-26 17:49:34 +00002399template<class _Engine, class _UIntType>
2400_UIntType
2401__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002402{
Howard Hinnantc3267212010-05-26 17:49:34 +00002403 result_type _S = 0;
2404 for (size_t __k = 0; __k < __n0_; ++__k)
2405 {
2406 _Engine_result_type __u;
2407 do
2408 {
2409 __u = __e_() - _Engine::min();
2410 } while (__u >= __y0_);
2411 if (__w0_ < _EDt)
2412 _S <<= __w0_;
2413 else
2414 _S = 0;
2415 _S += __u & __mask0_;
2416 }
2417 for (size_t __k = __n0_; __k < __n_; ++__k)
2418 {
2419 _Engine_result_type __u;
2420 do
2421 {
2422 __u = __e_() - _Engine::min();
2423 } while (__u >= __y1_);
2424 if (__w0_ < _EDt - 1)
2425 _S <<= __w0_ + 1;
2426 else
2427 _S = 0;
2428 _S += __u & __mask1_;
2429 }
2430 return _S;
2431}
2432
2433// uniform_int_distribution
2434
2435template<class _IntType = int>
2436class uniform_int_distribution
2437{
2438public:
2439 // types
2440 typedef _IntType result_type;
2441
2442 class param_type
2443 {
2444 result_type __a_;
2445 result_type __b_;
2446 public:
2447 typedef uniform_int_distribution distribution_type;
2448
2449 explicit param_type(result_type __a = 0,
2450 result_type __b = numeric_limits<result_type>::max())
2451 : __a_(__a), __b_(__b) {}
2452
2453 result_type a() const {return __a_;}
2454 result_type b() const {return __b_;}
2455
2456 friend bool operator==(const param_type& __x, const param_type& __y)
2457 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2458 friend bool operator!=(const param_type& __x, const param_type& __y)
2459 {return !(__x == __y);}
2460 };
2461
2462private:
2463 param_type __p_;
2464
2465public:
2466 // constructors and reset functions
2467 explicit uniform_int_distribution(result_type __a = 0,
2468 result_type __b = numeric_limits<result_type>::max())
2469 : __p_(param_type(__a, __b)) {}
2470 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
2471 void reset() {}
2472
2473 // generating functions
2474 template<class _URNG> result_type operator()(_URNG& __g)
2475 {return (*this)(__g, __p_);}
2476 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2477
2478 // property functions
2479 result_type a() const {return __p_.a();}
2480 result_type b() const {return __p_.b();}
2481
2482 param_type param() const {return __p_;}
2483 void param(const param_type& __p) {__p_ = __p;}
2484
2485 result_type min() const {return a();}
2486 result_type max() const {return b();}
2487
2488 friend bool operator==(const uniform_int_distribution& __x,
2489 const uniform_int_distribution& __y)
2490 {return __x.__p_ == __y.__p_;}
2491 friend bool operator!=(const uniform_int_distribution& __x,
2492 const uniform_int_distribution& __y)
2493 {return !(__x == __y);}
2494};
2495
2496template<class _IntType>
2497template<class _URNG>
2498typename uniform_int_distribution<_IntType>::result_type
2499uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
2500{
2501 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
2502 uint32_t, uint64_t>::type _UIntType;
2503 const _UIntType _R = __p.b() - __p.a() + _UIntType(1);
2504 if (_R == 1)
2505 return __p.a();
2506 const size_t _Dt = numeric_limits<_UIntType>::digits;
2507 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
2508 if (_R == 0)
2509 return static_cast<result_type>(_Eng(__g, _Dt)());
2510 size_t __w = _Dt - __clz(_R) - 1;
2511 if ((_R & (_UIntType(~0) >> (_Dt - __w))) != 0)
2512 ++__w;
2513 _Eng __e(__g, __w);
2514 _UIntType __u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002515 do
Howard Hinnantc3267212010-05-26 17:49:34 +00002516 {
2517 __u = __e();
2518 } while (__u >= _R);
2519 return static_cast<result_type>(__u + __p.a());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002520}
2521
Howard Hinnantc3267212010-05-26 17:49:34 +00002522class __rs_default;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002523
Howard Hinnantc3267212010-05-26 17:49:34 +00002524__rs_default __rs_get();
2525
2526class __rs_default
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002527{
Howard Hinnantc3267212010-05-26 17:49:34 +00002528 static unsigned __c_;
2529
2530 __rs_default();
2531public:
2532 typedef unsigned result_type;
2533
2534 static const result_type _Min = 0;
2535 static const result_type _Max = 0xFFFFFFFF;
2536
2537 __rs_default(const __rs_default&);
2538 ~__rs_default();
2539
2540 result_type operator()();
2541
2542 static const/*expr*/ result_type min() {return _Min;}
2543 static const/*expr*/ result_type max() {return _Max;}
2544
2545 friend __rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002546};
2547
Howard Hinnantc3267212010-05-26 17:49:34 +00002548__rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002549
2550template <class _RandomAccessIterator>
2551void
2552random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
2553{
2554 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc3267212010-05-26 17:49:34 +00002555 typedef uniform_int_distribution<ptrdiff_t> _D;
2556 typedef typename _D::param_type _P;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002557 difference_type __d = __last - __first;
2558 if (__d > 1)
2559 {
Howard Hinnantc3267212010-05-26 17:49:34 +00002560 _D __uid;
2561 __rs_default __g = __rs_get();
2562 for (--__last, --__d; __first < __last; ++__first, --__d)
2563 swap(*__first, *(__first + __uid(__g, _P(0, __d))));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002564 }
2565}
2566
2567template <class _RandomAccessIterator, class _RandomNumberGenerator>
2568void
2569random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
2570#ifdef _LIBCPP_MOVE
2571 _RandomNumberGenerator&& __rand)
2572#else
2573 _RandomNumberGenerator& __rand)
2574#endif
2575{
2576 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2577 difference_type __d = __last - __first;
2578 if (__d > 1)
2579 {
2580 for (--__last; __first < __last; ++__first, --__d)
2581 swap(*__first, *(__first + __rand(__d)));
2582 }
2583}
2584
Howard Hinnantc3267212010-05-26 17:49:34 +00002585template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
2586 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
2587 _UniformRandomNumberGenerator& __g)
2588{
2589 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2590 typedef uniform_int_distribution<ptrdiff_t> _D;
2591 typedef typename _D::param_type _P;
2592 difference_type __d = __last - __first;
2593 if (__d > 1)
2594 {
2595 _D __uid;
2596 for (--__last, --__d; __first < __last; ++__first, --__d)
2597 swap(*__first, *(__first + __uid(__g, _P(0, __d))));
2598 }
2599}
2600
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002601template <class _InputIterator, class _Predicate>
2602bool
2603is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
2604{
2605 for (; __first != __last; ++__first)
2606 if (!__pred(*__first))
2607 break;
2608 for (; __first != __last; ++__first)
2609 if (__pred(*__first))
2610 return false;
2611 return true;
2612}
2613
2614// partition
2615
2616template <class _Predicate, class _ForwardIterator>
2617_ForwardIterator
2618__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
2619{
2620 while (true)
2621 {
2622 if (__first == __last)
2623 return __first;
2624 if (!__pred(*__first))
2625 break;
2626 ++__first;
2627 }
2628 for (_ForwardIterator __p = __first; ++__p != __last;)
2629 {
2630 if (__pred(*__p))
2631 {
2632 swap(*__first, *__p);
2633 ++__first;
2634 }
2635 }
2636 return __first;
2637}
2638
2639template <class _Predicate, class _BidirectionalIterator>
2640_BidirectionalIterator
2641__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
2642 bidirectional_iterator_tag)
2643{
2644 while (true)
2645 {
2646 while (true)
2647 {
2648 if (__first == __last)
2649 return __first;
2650 if (!__pred(*__first))
2651 break;
2652 ++__first;
2653 }
2654 do
2655 {
2656 if (__first == --__last)
2657 return __first;
2658 } while (!__pred(*__last));
2659 swap(*__first, *__last);
2660 ++__first;
2661 }
2662}
2663
2664template <class _ForwardIterator, class _Predicate>
2665inline _LIBCPP_INLINE_VISIBILITY
2666_ForwardIterator
2667partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2668{
2669 return _STD::__partition<typename add_lvalue_reference<_Predicate>::type>
2670 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
2671}
2672
2673// partition_copy
2674
2675template <class _InputIterator, class _OutputIterator1,
2676 class _OutputIterator2, class _Predicate>
2677pair<_OutputIterator1, _OutputIterator2>
2678partition_copy(_InputIterator __first, _InputIterator __last,
2679 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
2680 _Predicate __pred)
2681{
2682 for (; __first != __last; ++__first)
2683 {
2684 if (__pred(*__first))
2685 {
2686 *__out_true = *__first;
2687 ++__out_true;
2688 }
2689 else
2690 {
2691 *__out_false = *__first;
2692 ++__out_false;
2693 }
2694 }
2695 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
2696}
2697
2698// partition_point
2699
2700template<class _ForwardIterator, class _Predicate>
2701_ForwardIterator
2702partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2703{
2704 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
2705 difference_type __len = _STD::distance(__first, __last);
2706 while (__len != 0)
2707 {
2708 difference_type __l2 = __len / 2;
2709 _ForwardIterator __m = __first;
2710 _STD::advance(__m, __l2);
2711 if (__pred(*__m))
2712 {
2713 __first = ++__m;
2714 __len -= __l2 + 1;
2715 }
2716 else
2717 __len = __l2;
2718 }
2719 return __first;
2720}
2721
2722// stable_partition
2723
2724template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
2725_ForwardIterator
2726__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
2727 _Distance __len, _Pair __p, forward_iterator_tag __fit)
2728{
2729 // *__first is known to be false
2730 // __len >= 1
2731 if (__len == 1)
2732 return __first;
2733 if (__len == 2)
2734 {
2735 _ForwardIterator __m = __first;
2736 if (__pred(*++__m))
2737 {
2738 swap(*__first, *__m);
2739 return __m;
2740 }
2741 return __first;
2742 }
2743 if (__len <= __p.second)
2744 { // The buffer is big enough to use
2745 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2746 __destruct_n __d(0);
2747 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
2748 // Move the falses into the temporary buffer, and the trues to the front of the line
2749 // Update __first to always point to the end of the trues
2750 value_type* __t = __p.first;
2751 ::new(__t) value_type(_STD::move(*__first));
2752 __d.__incr((value_type*)0);
2753 ++__t;
2754 _ForwardIterator __i = __first;
2755 while (++__i != __last)
2756 {
2757 if (__pred(*__i))
2758 {
2759 *__first = _STD::move(*__i);
2760 ++__first;
2761 }
2762 else
2763 {
2764 ::new(__t) value_type(_STD::move(*__i));
2765 __d.__incr((value_type*)0);
2766 ++__t;
2767 }
2768 }
2769 // All trues now at start of range, all falses in buffer
2770 // Move falses back into range, but don't mess up __first which points to first false
2771 __i = __first;
2772 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
2773 *__i = _STD::move(*__t2);
2774 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
2775 return __first;
2776 }
2777 // Else not enough buffer, do in place
2778 // __len >= 3
2779 _ForwardIterator __m = __first;
2780 _Distance __len2 = __len / 2; // __len2 >= 2
2781 _STD::advance(__m, __len2);
2782 // recurse on [__first, __m), *__first know to be false
2783 // F?????????????????
2784 // f m l
2785 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
2786 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
2787 // TTTFFFFF??????????
2788 // f ff m l
2789 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
2790 _ForwardIterator __m1 = __m;
2791 _ForwardIterator __second_false = __last;
2792 _Distance __len_half = __len - __len2;
2793 while (__pred(*__m1))
2794 {
2795 if (++__m1 == __last)
2796 goto __second_half_done;
2797 --__len_half;
2798 }
2799 // TTTFFFFFTTTF??????
2800 // f ff m m1 l
2801 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
2802__second_half_done:
2803 // TTTFFFFFTTTTTFFFFF
2804 // f ff m sf l
2805 return _STD::rotate(__first_false, __m, __second_false);
2806 // TTTTTTTTFFFFFFFFFF
2807 // |
2808}
2809
2810struct __return_temporary_buffer
2811{
2812 template <class _Tp>
2813 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_STD::return_temporary_buffer(__p);}
2814};
2815
2816template <class _Predicate, class _ForwardIterator>
2817_ForwardIterator
2818__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
2819 forward_iterator_tag)
2820{
2821 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
2822 // Either prove all true and return __first or point to first false
2823 while (true)
2824 {
2825 if (__first == __last)
2826 return __first;
2827 if (!__pred(*__first))
2828 break;
2829 ++__first;
2830 }
2831 // We now have a reduced range [__first, __last)
2832 // *__first is known to be false
2833 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
2834 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2835 difference_type __len = _STD::distance(__first, __last);
2836 pair<value_type*, ptrdiff_t> __p(0, 0);
2837 unique_ptr<value_type, __return_temporary_buffer> __h;
2838 if (__len >= __alloc_limit)
2839 {
2840 __p = _STD::get_temporary_buffer<value_type>(__len);
2841 __h.reset(__p.first);
2842 }
2843 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
2844 (__first, __last, __pred, __len, __p, forward_iterator_tag());
2845}
2846
2847template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
2848_BidirectionalIterator
2849__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
2850 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
2851{
2852 // *__first is known to be false
2853 // *__last is known to be true
2854 // __len >= 2
2855 if (__len == 2)
2856 {
2857 swap(*__first, *__last);
2858 return __last;
2859 }
2860 if (__len == 3)
2861 {
2862 _BidirectionalIterator __m = __first;
2863 if (__pred(*++__m))
2864 {
2865 swap(*__first, *__m);
2866 swap(*__m, *__last);
2867 return __last;
2868 }
2869 swap(*__m, *__last);
2870 swap(*__first, *__m);
2871 return __m;
2872 }
2873 if (__len <= __p.second)
2874 { // The buffer is big enough to use
2875 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2876 __destruct_n __d(0);
2877 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
2878 // Move the falses into the temporary buffer, and the trues to the front of the line
2879 // Update __first to always point to the end of the trues
2880 value_type* __t = __p.first;
2881 ::new(__t) value_type(_STD::move(*__first));
2882 __d.__incr((value_type*)0);
2883 ++__t;
2884 _BidirectionalIterator __i = __first;
2885 while (++__i != __last)
2886 {
2887 if (__pred(*__i))
2888 {
2889 *__first = _STD::move(*__i);
2890 ++__first;
2891 }
2892 else
2893 {
2894 ::new(__t) value_type(_STD::move(*__i));
2895 __d.__incr((value_type*)0);
2896 ++__t;
2897 }
2898 }
2899 // move *__last, known to be true
2900 *__first = _STD::move(*__i);
2901 __i = ++__first;
2902 // All trues now at start of range, all falses in buffer
2903 // Move falses back into range, but don't mess up __first which points to first false
2904 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
2905 *__i = _STD::move(*__t2);
2906 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
2907 return __first;
2908 }
2909 // Else not enough buffer, do in place
2910 // __len >= 4
2911 _BidirectionalIterator __m = __first;
2912 _Distance __len2 = __len / 2; // __len2 >= 2
2913 _STD::advance(__m, __len2);
2914 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
2915 // F????????????????T
2916 // f m l
2917 _BidirectionalIterator __m1 = __m;
2918 _BidirectionalIterator __first_false = __first;
2919 _Distance __len_half = __len2;
2920 while (!__pred(*--__m1))
2921 {
2922 if (__m1 == __first)
2923 goto __first_half_done;
2924 --__len_half;
2925 }
2926 // F???TFFF?????????T
2927 // f m1 m l
2928 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
2929 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
2930__first_half_done:
2931 // TTTFFFFF?????????T
2932 // f ff m l
2933 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
2934 __m1 = __m;
2935 _BidirectionalIterator __second_false = __last;
2936 ++__second_false;
2937 __len_half = __len - __len2;
2938 while (__pred(*__m1))
2939 {
2940 if (++__m1 == __last)
2941 goto __second_half_done;
2942 --__len_half;
2943 }
2944 // TTTFFFFFTTTF?????T
2945 // f ff m m1 l
2946 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
2947__second_half_done:
2948 // TTTFFFFFTTTTTFFFFF
2949 // f ff m sf l
2950 return _STD::rotate(__first_false, __m, __second_false);
2951 // TTTTTTTTFFFFFFFFFF
2952 // |
2953}
2954
2955template <class _Predicate, class _BidirectionalIterator>
2956_BidirectionalIterator
2957__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
2958 bidirectional_iterator_tag)
2959{
2960 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
2961 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2962 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
2963 // Either prove all true and return __first or point to first false
2964 while (true)
2965 {
2966 if (__first == __last)
2967 return __first;
2968 if (!__pred(*__first))
2969 break;
2970 ++__first;
2971 }
2972 // __first points to first false, everything prior to __first is already set.
2973 // Either prove [__first, __last) is all false and return __first, or point __last to last true
2974 do
2975 {
2976 if (__first == --__last)
2977 return __first;
2978 } while (!__pred(*__last));
2979 // We now have a reduced range [__first, __last]
2980 // *__first is known to be false
2981 // *__last is known to be true
2982 // __len >= 2
2983 difference_type __len = _STD::distance(__first, __last) + 1;
2984 pair<value_type*, ptrdiff_t> __p(0, 0);
2985 unique_ptr<value_type, __return_temporary_buffer> __h;
2986 if (__len >= __alloc_limit)
2987 {
2988 __p = _STD::get_temporary_buffer<value_type>(__len);
2989 __h.reset(__p.first);
2990 }
2991 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
2992 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
2993}
2994
2995template <class _ForwardIterator, class _Predicate>
2996inline _LIBCPP_INLINE_VISIBILITY
2997_ForwardIterator
2998stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2999{
3000 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3001 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3002}
3003
3004// is_sorted_until
3005
3006template <class _ForwardIterator, class _Compare>
3007_ForwardIterator
3008is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3009{
3010 if (__first != __last)
3011 {
3012 _ForwardIterator __i = __first;
3013 while (++__i != __last)
3014 {
3015 if (__comp(*__i, *__first))
3016 return __i;
3017 __first = __i;
3018 }
3019 }
3020 return __last;
3021}
3022
3023template<class _ForwardIterator>
3024inline _LIBCPP_INLINE_VISIBILITY
3025_ForwardIterator
3026is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3027{
3028 return _STD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3029}
3030
3031// is_sorted
3032
3033template <class _ForwardIterator, class _Compare>
3034inline _LIBCPP_INLINE_VISIBILITY
3035bool
3036is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3037{
3038 return _STD::is_sorted_until(__first, __last, __comp) == __last;
3039}
3040
3041template<class _ForwardIterator>
3042inline _LIBCPP_INLINE_VISIBILITY
3043bool
3044is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3045{
3046 return _STD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
3047}
3048
3049// sort
3050
3051// stable, 2-3 compares, 0-2 swaps
3052
3053template <class _Compare, class _ForwardIterator>
3054unsigned
3055__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3056{
3057 unsigned __r = 0;
3058 if (!__c(*__y, *__x)) // if x <= y
3059 {
3060 if (!__c(*__z, *__y)) // if y <= z
3061 return __r; // x <= y && y <= z
3062 // x <= y && y > z
3063 swap(*__y, *__z); // x <= z && y < z
3064 __r = 1;
3065 if (__c(*__y, *__x)) // if x > y
3066 {
3067 swap(*__x, *__y); // x < y && y <= z
3068 __r = 2;
3069 }
3070 return __r; // x <= y && y < z
3071 }
3072 if (__c(*__z, *__y)) // x > y, if y > z
3073 {
3074 swap(*__x, *__z); // x < y && y < z
3075 __r = 1;
3076 return __r;
3077 }
3078 swap(*__x, *__y); // x > y && y <= z
3079 __r = 1; // x < y && x <= z
3080 if (__c(*__z, *__y)) // if y > z
3081 {
3082 swap(*__y, *__z); // x <= y && y < z
3083 __r = 2;
3084 }
3085 return __r;
3086} // x <= y && y <= z
3087
3088// stable, 3-6 compares, 0-5 swaps
3089
3090template <class _Compare, class _ForwardIterator>
3091unsigned
3092__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3093 _ForwardIterator __x4, _Compare __c)
3094{
3095 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3096 if (__c(*__x4, *__x3))
3097 {
3098 swap(*__x3, *__x4);
3099 ++__r;
3100 if (__c(*__x3, *__x2))
3101 {
3102 swap(*__x2, *__x3);
3103 ++__r;
3104 if (__c(*__x2, *__x1))
3105 {
3106 swap(*__x1, *__x2);
3107 ++__r;
3108 }
3109 }
3110 }
3111 return __r;
3112}
3113
3114// stable, 4-10 compares, 0-9 swaps
3115
3116template <class _Compare, class _ForwardIterator>
3117unsigned
3118__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3119 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3120{
3121 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3122 if (__c(*__x5, *__x4))
3123 {
3124 swap(*__x4, *__x5);
3125 ++__r;
3126 if (__c(*__x4, *__x3))
3127 {
3128 swap(*__x3, *__x4);
3129 ++__r;
3130 if (__c(*__x3, *__x2))
3131 {
3132 swap(*__x2, *__x3);
3133 ++__r;
3134 if (__c(*__x2, *__x1))
3135 {
3136 swap(*__x1, *__x2);
3137 ++__r;
3138 }
3139 }
3140 }
3141 }
3142 return __r;
3143}
3144
3145// Assumes size > 0
3146template <class _Compare, class _BirdirectionalIterator>
3147void
3148__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3149{
3150 _BirdirectionalIterator __lm1 = __last;
3151 for (--__lm1; __first != __lm1; ++__first)
3152 {
3153 _BirdirectionalIterator __i = _STD::min_element<_BirdirectionalIterator,
3154 typename add_lvalue_reference<_Compare>::type>
3155 (__first, __last, __comp);
3156 if (__i != __first)
3157 swap(*__first, *__i);
3158 }
3159}
3160
3161template <class _Compare, class _BirdirectionalIterator>
3162void
3163__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3164{
3165 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3166 if (__first != __last)
3167 {
3168 _BirdirectionalIterator __i = __first;
3169 for (++__i; __i != __last; ++__i)
3170 {
3171 _BirdirectionalIterator __j = __i;
3172 value_type __t(_STD::move(*__j));
3173 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
3174 *__j = _STD::move(*__k);
3175 *__j = _STD::move(__t);
3176 }
3177 }
3178}
3179
3180template <class _Compare, class _RandomAccessIterator>
3181void
3182__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3183{
3184 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3185 _RandomAccessIterator __j = __first+2;
3186 __sort3<_Compare>(__first, __first+1, __j, __comp);
3187 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3188 {
3189 if (__comp(*__i, *__j))
3190 {
3191 value_type __t(_STD::move(*__i));
3192 _RandomAccessIterator __k = __j;
3193 __j = __i;
3194 do
3195 {
3196 *__j = _STD::move(*__k);
3197 __j = __k;
3198 } while (__j != __first && __comp(__t, *--__k));
3199 *__j = _STD::move(__t);
3200 }
3201 __j = __i;
3202 }
3203}
3204
3205template <class _Compare, class _RandomAccessIterator>
3206bool
3207__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3208{
3209 switch (__last - __first)
3210 {
3211 case 0:
3212 case 1:
3213 return true;
3214 case 2:
3215 if (__comp(*--__last, *__first))
3216 swap(*__first, *__last);
3217 return true;
3218 case 3:
3219 _STD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
3220 return true;
3221 case 4:
3222 _STD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
3223 return true;
3224 case 5:
3225 _STD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
3226 return true;
3227 }
3228 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3229 _RandomAccessIterator __j = __first+2;
3230 __sort3<_Compare>(__first, __first+1, __j, __comp);
3231 const unsigned __limit = 8;
3232 unsigned __count = 0;
3233 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3234 {
3235 if (__comp(*__i, *__j))
3236 {
3237 value_type __t(_STD::move(*__i));
3238 _RandomAccessIterator __k = __j;
3239 __j = __i;
3240 do
3241 {
3242 *__j = _STD::move(*__k);
3243 __j = __k;
3244 } while (__j != __first && __comp(__t, *--__k));
3245 *__j = _STD::move(__t);
3246 if (++__count == __limit)
3247 return ++__i == __last;
3248 }
3249 __j = __i;
3250 }
3251 return true;
3252}
3253
3254template <class _Compare, class _BirdirectionalIterator>
3255void
3256__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3257 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3258{
3259 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3260 if (__first1 != __last1)
3261 {
3262 __destruct_n __d(0);
3263 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3264 value_type* __last2 = __first2;
3265 ::new(__last2) value_type(_STD::move(*__first1));
3266 __d.__incr((value_type*)0);
3267 for (++__last2; ++__first1 != __last1; ++__last2)
3268 {
3269 value_type* __j2 = __last2;
3270 value_type* __i2 = __j2;
3271 if (__comp(*__first1, *--__i2))
3272 {
3273 ::new(__j2) value_type(_STD::move(*__i2));
3274 __d.__incr((value_type*)0);
3275 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
3276 *__j2 = _STD::move(*__i2);
3277 *__j2 = _STD::move(*__first1);
3278 }
3279 else
3280 {
3281 ::new(__j2) value_type(_STD::move(*__first1));
3282 __d.__incr((value_type*)0);
3283 }
3284 }
3285 __h.release();
3286 }
3287}
3288
3289template <class _Compare, class _RandomAccessIterator>
3290void
3291__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3292{
3293 // _Compare is known to be a reference type
3294 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3295 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3296 const difference_type __limit = has_trivial_copy_constructor<value_type>::value &&
3297 has_trivial_copy_assign<value_type>::value ? 30 : 6;
3298 while (true)
3299 {
3300 __restart:
3301 difference_type __len = __last - __first;
3302 switch (__len)
3303 {
3304 case 0:
3305 case 1:
3306 return;
3307 case 2:
3308 if (__comp(*--__last, *__first))
3309 swap(*__first, *__last);
3310 return;
3311 case 3:
3312 _STD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
3313 return;
3314 case 4:
3315 _STD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
3316 return;
3317 case 5:
3318 _STD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
3319 return;
3320 }
3321 if (__len <= __limit)
3322 {
3323 _STD::__insertion_sort_3<_Compare>(__first, __last, __comp);
3324 return;
3325 }
3326 // __len > 5
3327 _RandomAccessIterator __m = __first;
3328 _RandomAccessIterator __lm1 = __last;
3329 --__lm1;
3330 unsigned __n_swaps;
3331 {
3332 difference_type __delta;
3333 if (__len >= 1000)
3334 {
3335 __delta = __len/2;
3336 __m += __delta;
3337 __delta /= 2;
3338 __n_swaps = _STD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
3339 }
3340 else
3341 {
3342 __delta = __len/2;
3343 __m += __delta;
3344 __n_swaps = _STD::__sort3<_Compare>(__first, __m, __lm1, __comp);
3345 }
3346 }
3347 // *__m is median
3348 // partition [__first, __m) < *__m and *__m <= [__m, __last)
3349 // (this inhibits tossing elements equivalent to __m around unnecessarily)
3350 _RandomAccessIterator __i = __first;
3351 _RandomAccessIterator __j = __lm1;
3352 // j points beyond range to be tested, *__m is known to be <= *__lm1
3353 // The search going up is known to be guarded but the search coming down isn't.
3354 // Prime the downward search with a guard.
3355 if (!__comp(*__i, *__m)) // if *__first == *__m
3356 {
3357 // *__first == *__m, *__first doesn't go in first part
3358 // manually guard downward moving __j against __i
3359 while (true)
3360 {
3361 if (__i == --__j)
3362 {
3363 // *__first == *__m, *__m <= all other elements
3364 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
3365 ++__i; // __first + 1
3366 __j = __last;
3367 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
3368 {
3369 while (true)
3370 {
3371 if (__i == __j)
3372 return; // [__first, __last) all equivalent elements
3373 if (__comp(*__first, *__i))
3374 {
3375 swap(*__i, *__j);
3376 ++__n_swaps;
3377 ++__i;
3378 break;
3379 }
3380 ++__i;
3381 }
3382 }
3383 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
3384 if (__i == __j)
3385 return;
3386 while (true)
3387 {
3388 while (!__comp(*__first, *__i))
3389 ++__i;
3390 while (__comp(*__first, *--__j))
3391 ;
3392 if (__i >= __j)
3393 break;
3394 swap(*__i, *__j);
3395 ++__n_swaps;
3396 ++__i;
3397 }
3398 // [__first, __i) == *__first and *__first < [__i, __last)
3399 // The first part is sorted, sort the secod part
3400 // _STD::__sort<_Compare>(__i, __last, __comp);
3401 __first = __i;
3402 goto __restart;
3403 }
3404 if (__comp(*__j, *__m))
3405 {
3406 swap(*__i, *__j);
3407 ++__n_swaps;
3408 break; // found guard for downward moving __j, now use unguarded partition
3409 }
3410 }
3411 }
3412 // It is known that *__i < *__m
3413 ++__i;
3414 // j points beyond range to be tested, *__m is known to be <= *__lm1
3415 // if not yet partitioned...
3416 if (__i < __j)
3417 {
3418 // known that *(__i - 1) < *__m
3419 // known that __i <= __m
3420 while (true)
3421 {
3422 // __m still guards upward moving __i
3423 while (__comp(*__i, *__m))
3424 ++__i;
3425 // It is now known that a guard exists for downward moving __j
3426 while (!__comp(*--__j, *__m))
3427 ;
3428 if (__i > __j)
3429 break;
3430 swap(*__i, *__j);
3431 ++__n_swaps;
3432 // It is known that __m != __j
3433 // If __m just moved, follow it
3434 if (__m == __i)
3435 __m = __j;
3436 ++__i;
3437 }
3438 }
3439 // [__first, __i) < *__m and *__m <= [__i, __last)
3440 if (__i != __m && __comp(*__m, *__i))
3441 {
3442 swap(*__i, *__m);
3443 ++__n_swaps;
3444 }
3445 // [__first, __i) < *__i and *__i <= [__i+1, __last)
3446 // If we were given a perfect partition, see if insertion sort is quick...
3447 if (__n_swaps == 0)
3448 {
3449 bool __fs = _STD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
3450 if (_STD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
3451 {
3452 if (__fs)
3453 return;
3454 __last = __i;
3455 continue;
3456 }
3457 else
3458 {
3459 if (__fs)
3460 {
3461 __first = ++__i;
3462 continue;
3463 }
3464 }
3465 }
3466 // sort smaller range with recursive call and larger with tail recursion elimination
3467 if (__i - __first < __last - __i)
3468 {
3469 _STD::__sort<_Compare>(__first, __i, __comp);
3470 // _STD::__sort<_Compare>(__i+1, __last, __comp);
3471 __first = ++__i;
3472 }
3473 else
3474 {
3475 _STD::__sort<_Compare>(__i+1, __last, __comp);
3476 // _STD::__sort<_Compare>(__first, __i, __comp);
3477 __last = __i;
3478 }
3479 }
3480}
3481
3482// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
3483template <class _RandomAccessIterator, class _Compare>
3484inline _LIBCPP_INLINE_VISIBILITY
3485void
3486sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3487{
3488#ifdef _LIBCPP_DEBUG
3489 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3490 __debug_less<_Compare> __c(__comp);
3491 __sort<_Comp_ref>(__first, __last, __c);
3492#else
3493 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3494 __sort<_Comp_ref>(__first, __last, __comp);
3495#endif
3496}
3497
3498template <class _RandomAccessIterator>
3499inline _LIBCPP_INLINE_VISIBILITY
3500void
3501sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
3502{
3503 _STD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
3504}
3505
3506template <class _Tp>
3507inline _LIBCPP_INLINE_VISIBILITY
3508void
3509sort(_Tp** __first, _Tp** __last)
3510{
3511 _STD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
3512}
3513
3514template <class _Tp>
3515inline _LIBCPP_INLINE_VISIBILITY
3516void
3517sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
3518{
3519 _STD::sort(__first.base(), __last.base());
3520}
3521
3522extern template void __sort<__less<char>&, char*>(char*, char*, __less<char>&);
3523extern template void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&);
3524extern template void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&);
3525extern template void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&);
3526extern template void __sort<__less<short>&, short*>(short*, short*, __less<short>&);
3527extern template void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&);
3528extern template void __sort<__less<int>&, int*>(int*, int*, __less<int>&);
3529extern template void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&);
3530extern template void __sort<__less<long>&, long*>(long*, long*, __less<long>&);
3531extern template void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&);
3532extern template void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&);
3533extern template void __sort<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&);
3534extern template void __sort<__less<float>&, float*>(float*, float*, __less<float>&);
3535extern template void __sort<__less<double>&, double*>(double*, double*, __less<double>&);
3536extern template void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&);
3537
3538extern template bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&);
3539extern template bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&);
3540extern template bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&);
3541extern template bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&);
3542extern template bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&);
3543extern template bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&);
3544extern template bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&);
3545extern template bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&);
3546extern template bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&);
3547extern template bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&);
3548extern template bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&);
3549extern template bool __insertion_sort_incomplete<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&);
3550extern template bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&);
3551extern template bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&);
3552extern template bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&);
3553
3554extern template unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&);
3555
3556// lower_bound
3557
3558template <class _Compare, class _ForwardIterator, class _Tp>
3559_ForwardIterator
3560__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3561{
3562 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3563 difference_type __len = _STD::distance(__first, __last);
3564 while (__len != 0)
3565 {
3566 difference_type __l2 = __len / 2;
3567 _ForwardIterator __m = __first;
3568 _STD::advance(__m, __l2);
3569 if (__comp(*__m, __value))
3570 {
3571 __first = ++__m;
3572 __len -= __l2 + 1;
3573 }
3574 else
3575 __len = __l2;
3576 }
3577 return __first;
3578}
3579
3580template <class _ForwardIterator, class _Tp, class _Compare>
3581inline _LIBCPP_INLINE_VISIBILITY
3582_ForwardIterator
3583lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3584{
3585#ifdef _LIBCPP_DEBUG
3586 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3587 __debug_less<_Compare> __c(__comp);
3588 return __lower_bound<_Comp_ref>(__first, __last, __value, __c);
3589#else
3590 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3591 return __lower_bound<_Comp_ref>(__first, __last, __value, __comp);
3592#endif
3593}
3594
3595template <class _ForwardIterator, class _Tp>
3596inline _LIBCPP_INLINE_VISIBILITY
3597_ForwardIterator
3598lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
3599{
3600 return _STD::lower_bound(__first, __last, __value,
3601 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
3602}
3603
3604// upper_bound
3605
3606template <class _Compare, class _ForwardIterator, class _Tp>
3607_ForwardIterator
3608__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3609{
3610 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3611 difference_type __len = _STD::distance(__first, __last);
3612 while (__len != 0)
3613 {
3614 difference_type __l2 = __len / 2;
3615 _ForwardIterator __m = __first;
3616 _STD::advance(__m, __l2);
3617 if (__comp(__value, *__m))
3618 __len = __l2;
3619 else
3620 {
3621 __first = ++__m;
3622 __len -= __l2 + 1;
3623 }
3624 }
3625 return __first;
3626}
3627
3628template <class _ForwardIterator, class _Tp, class _Compare>
3629inline _LIBCPP_INLINE_VISIBILITY
3630_ForwardIterator
3631upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3632{
3633#ifdef _LIBCPP_DEBUG
3634 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3635 __debug_less<_Compare> __c(__comp);
3636 return __upper_bound<_Comp_ref>(__first, __last, __value, __c);
3637#else
3638 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3639 return __upper_bound<_Comp_ref>(__first, __last, __value, __comp);
3640#endif
3641}
3642
3643template <class _ForwardIterator, class _Tp>
3644inline _LIBCPP_INLINE_VISIBILITY
3645_ForwardIterator
3646upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
3647{
3648 return _STD::upper_bound(__first, __last, __value,
3649 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
3650}
3651
3652// equal_range
3653
3654template <class _Compare, class _ForwardIterator, class _Tp>
3655pair<_ForwardIterator, _ForwardIterator>
3656__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3657{
3658 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3659 difference_type __len = _STD::distance(__first, __last);
3660 while (__len != 0)
3661 {
3662 difference_type __l2 = __len / 2;
3663 _ForwardIterator __m = __first;
3664 _STD::advance(__m, __l2);
3665 if (__comp(*__m, __value))
3666 {
3667 __first = ++__m;
3668 __len -= __l2 + 1;
3669 }
3670 else if (__comp(__value, *__m))
3671 {
3672 __last = __m;
3673 __len = __l2;
3674 }
3675 else
3676 {
3677 _ForwardIterator __mp1 = __m;
3678 return pair<_ForwardIterator, _ForwardIterator>
3679 (
3680 __lower_bound<_Compare>(__first, __m, __value, __comp),
3681 __upper_bound<_Compare>(++__mp1, __last, __value, __comp)
3682 );
3683 }
3684 }
3685 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
3686}
3687
3688template <class _ForwardIterator, class _Tp, class _Compare>
3689inline _LIBCPP_INLINE_VISIBILITY
3690pair<_ForwardIterator, _ForwardIterator>
3691equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3692{
3693#ifdef _LIBCPP_DEBUG
3694 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3695 __debug_less<_Compare> __c(__comp);
3696 return __equal_range<_Comp_ref>(__first, __last, __value, __c);
3697#else
3698 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3699 return __equal_range<_Comp_ref>(__first, __last, __value, __comp);
3700#endif
3701}
3702
3703template <class _ForwardIterator, class _Tp>
3704inline _LIBCPP_INLINE_VISIBILITY
3705pair<_ForwardIterator, _ForwardIterator>
3706equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
3707{
3708 return _STD::equal_range(__first, __last, __value,
3709 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
3710}
3711
3712// binary_search
3713
3714template <class _Compare, class _ForwardIterator, class _Tp>
3715inline _LIBCPP_INLINE_VISIBILITY
3716bool
3717__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3718{
3719 __first = __lower_bound<_Compare>(__first, __last, __value, __comp);
3720 return __first != __last && !__comp(__value, *__first);
3721}
3722
3723template <class _ForwardIterator, class _Tp, class _Compare>
3724inline _LIBCPP_INLINE_VISIBILITY
3725bool
3726binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3727{
3728#ifdef _LIBCPP_DEBUG
3729 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3730 __debug_less<_Compare> __c(__comp);
3731 return __binary_search<_Comp_ref>(__first, __last, __value, __c);
3732#else
3733 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3734 return __binary_search<_Comp_ref>(__first, __last, __value, __comp);
3735#endif
3736}
3737
3738template <class _ForwardIterator, class _Tp>
3739inline _LIBCPP_INLINE_VISIBILITY
3740bool
3741binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
3742{
3743 return _STD::binary_search(__first, __last, __value,
3744 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
3745}
3746
3747// merge
3748
3749template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
3750_OutputIterator
3751__merge(_InputIterator1 __first1, _InputIterator1 __last1,
3752 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
3753{
3754 for (; __first1 != __last1; ++__result)
3755 {
3756 if (__first2 == __last2)
3757 return _STD::copy(__first1, __last1, __result);
3758 if (__comp(*__first2, *__first1))
3759 {
3760 *__result = *__first2;
3761 ++__first2;
3762 }
3763 else
3764 {
3765 *__result = *__first1;
3766 ++__first1;
3767 }
3768 }
3769 return _STD::copy(__first2, __last2, __result);
3770}
3771
3772template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
3773inline _LIBCPP_INLINE_VISIBILITY
3774_OutputIterator
3775merge(_InputIterator1 __first1, _InputIterator1 __last1,
3776 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
3777{
3778#ifdef _LIBCPP_DEBUG
3779 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3780 __debug_less<_Compare> __c(__comp);
3781 return _STD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
3782#else
3783 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3784 return _STD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
3785#endif
3786}
3787
3788template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
3789inline _LIBCPP_INLINE_VISIBILITY
3790_OutputIterator
3791merge(_InputIterator1 __first1, _InputIterator1 __last1,
3792 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
3793{
3794 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
3795 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
3796 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
3797}
3798
3799// inplace_merge
3800
3801template <class _Compare, class _BidirectionalIterator>
3802void
3803__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
3804 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
3805 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
3806 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
3807{
3808 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3809 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3810 typedef typename iterator_traits<_BidirectionalIterator>::pointer pointer;
3811 __destruct_n __d(0);
3812 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
3813 if (__len1 <= __len2)
3814 {
3815 value_type* __p = __buff;
3816 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), ++__i, ++__p)
3817 ::new(__p) value_type(_STD::move(*__i));
3818 __merge<_Compare>(move_iterator<value_type*>(__buff),
3819 move_iterator<value_type*>(__p),
3820 move_iterator<_BidirectionalIterator>(__middle),
3821 move_iterator<_BidirectionalIterator>(__last),
3822 __first, __comp);
3823 }
3824 else
3825 {
3826 value_type* __p = __buff;
3827 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), ++__i, ++__p)
3828 ::new(__p) value_type(_STD::move(*__i));
3829 typedef reverse_iterator<_BidirectionalIterator> _RBi;
3830 typedef reverse_iterator<value_type*> _Rv;
3831 __merge(move_iterator<_RBi>(_RBi(__middle)), move_iterator<_RBi>(_RBi(__first)),
3832 move_iterator<_Rv>(_Rv(__p)), move_iterator<_Rv>(_Rv(__buff)),
3833 _RBi(__last), __negate<_Compare>(__comp));
3834 }
3835}
3836
3837template <class _Compare, class _BidirectionalIterator>
3838void
3839__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
3840 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
3841 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
3842 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
3843{
3844 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3845 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3846 while (true)
3847 {
3848 // if __middle == __last, we're done
3849 if (__len2 == 0)
3850 return;
3851 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
3852 for (; true; ++__first, --__len1)
3853 {
3854 if (__len1 == 0)
3855 return;
3856 if (__comp(*__middle, *__first))
3857 break;
3858 }
3859 if (__len1 <= __buff_size || __len2 <= __buff_size)
3860 {
3861 __buffered_inplace_merge<_Compare>(__first, __middle, __last, __comp, __len1, __len2, __buff);
3862 return;
3863 }
3864 // __first < __middle < __last
3865 // *__first > *__middle
3866 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
3867 // all elements in:
3868 // [__first, __m1) <= [__middle, __m2)
3869 // [__middle, __m2) < [__m1, __middle)
3870 // [__m1, __middle) <= [__m2, __last)
3871 // and __m1 or __m2 is in the middle of its range
3872 _BidirectionalIterator __m1; // "median" of [__first, __middle)
3873 _BidirectionalIterator __m2; // "median" of [__middle, __last)
3874 difference_type __len11; // distance(__first, __m1)
3875 difference_type __len21; // distance(__middle, __m2)
3876 // binary search smaller range
3877 if (__len1 < __len2)
3878 { // __len >= 1, __len2 >= 2
3879 __len21 = __len2 / 2;
3880 __m2 = __middle;
3881 _STD::advance(__m2, __len21);
3882 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
3883 __len11 = _STD::distance(__first, __m1);
3884 }
3885 else
3886 {
3887 if (__len1 == 1)
3888 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
3889 // It is known *__first > *__middle
3890 swap(*__first, *__middle);
3891 return;
3892 }
3893 // __len1 >= 2, __len2 >= 1
3894 __len11 = __len1 / 2;
3895 __m1 = __first;
3896 _STD::advance(__m1, __len11);
3897 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
3898 __len21 = _STD::distance(__middle, __m2);
3899 }
3900 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
3901 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
3902 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
3903 // swap middle two partitions
3904 __middle = _STD::rotate(__m1, __middle, __m2);
3905 // __len12 and __len21 now have swapped meanings
3906 // merge smaller range with recurisve call and larger with tail recursion elimination
3907 if (__len11 + __len21 < __len12 + __len22)
3908 {
3909 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
3910// __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
3911 __first = __middle;
3912 __middle = __m2;
3913 __len1 = __len12;
3914 __len2 = __len22;
3915 }
3916 else
3917 {
3918 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
3919// __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
3920 __last = __middle;
3921 __middle = __m1;
3922 __len1 = __len11;
3923 __len2 = __len21;
3924 }
3925 }
3926}
3927
3928template <class _Tp>
3929struct __inplace_merge_switch
3930{
3931 static const unsigned value = has_trivial_copy_assign<_Tp>::value;
3932};
3933
3934template <class _BidirectionalIterator, class _Compare>
3935inline _LIBCPP_INLINE_VISIBILITY
3936void
3937inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
3938 _Compare __comp)
3939{
3940 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3941 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3942 difference_type __len1 = _STD::distance(__first, __middle);
3943 difference_type __len2 = _STD::distance(__middle, __last);
3944 difference_type __buf_size = _STD::min(__len1, __len2);
3945 pair<value_type*, ptrdiff_t> __buf(0, 0);
3946 unique_ptr<value_type, __return_temporary_buffer> __h;
3947 if (__inplace_merge_switch<value_type>::value && __buf_size > 8)
3948 {
3949 __buf = _STD::get_temporary_buffer<value_type>(__buf_size);
3950 __h.reset(__buf.first);
3951 }
3952#ifdef _LIBCPP_DEBUG
3953 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3954 __debug_less<_Compare> __c(__comp);
3955 return _STD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
3956 __buf.first, __buf.second);
3957#else
3958 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3959 return _STD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
3960 __buf.first, __buf.second);
3961#endif
3962}
3963
3964template <class _BidirectionalIterator>
3965inline _LIBCPP_INLINE_VISIBILITY
3966void
3967inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
3968{
3969 _STD::inplace_merge(__first, __middle, __last,
3970 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
3971}
3972
3973// stable_sort
3974
3975template <class _Compare, class _InputIterator1, class _InputIterator2>
3976void
3977__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
3978 _InputIterator2 __first2, _InputIterator2 __last2,
3979 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
3980{
3981 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
3982 __destruct_n __d(0);
3983 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
3984 for (; true; ++__result)
3985 {
3986 if (__first1 == __last1)
3987 {
3988 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
3989 ::new (__result) value_type(_STD::move(*__first2));
3990 __h.release();
3991 return;
3992 }
3993 if (__first2 == __last2)
3994 {
3995 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
3996 ::new (__result) value_type(_STD::move(*__first1));
3997 __h.release();
3998 return;
3999 }
4000 if (__comp(*__first2, *__first1))
4001 {
4002 ::new (__result) value_type(_STD::move(*__first2));
4003 __d.__incr((value_type*)0);
4004 ++__first2;
4005 }
4006 else
4007 {
4008 ::new (__result) value_type(_STD::move(*__first1));
4009 __d.__incr((value_type*)0);
4010 ++__first1;
4011 }
4012 }
4013}
4014
4015template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4016void
4017__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4018 _InputIterator2 __first2, _InputIterator2 __last2,
4019 _OutputIterator __result, _Compare __comp)
4020{
4021 for (; __first1 != __last1; ++__result)
4022 {
4023 if (__first2 == __last2)
4024 {
4025 for (; __first1 != __last1; ++__first1, ++__result)
4026 *__result = _STD::move(*__first1);
4027 return;
4028 }
4029 if (__comp(*__first2, *__first1))
4030 {
4031 *__result = _STD::move(*__first2);
4032 ++__first2;
4033 }
4034 else
4035 {
4036 *__result = _STD::move(*__first1);
4037 ++__first1;
4038 }
4039 }
4040 for (; __first2 != __last2; ++__first2, ++__result)
4041 *__result = _STD::move(*__first2);
4042}
4043
4044template <class _Compare, class _RandomAccessIterator>
4045void
4046__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4047 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4048 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4049
4050template <class _Compare, class _RandomAccessIterator>
4051void
4052__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4053 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4054 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4055{
4056 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4057 switch (__len)
4058 {
4059 case 0:
4060 return;
4061 case 1:
4062 ::new(__first2) value_type(_STD::move(*__first1));
4063 return;
4064 case 2:
4065 __destruct_n __d(0);
4066 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4067 if (__comp(*--__last1, *__first1))
4068 {
4069 ::new(__first2) value_type(_STD::move(*__last1));
4070 __d.__incr((value_type*)0);
4071 ++__first2;
4072 ::new(__first2) value_type(_STD::move(*__first1));
4073 }
4074 else
4075 {
4076 ::new(__first2) value_type(_STD::move(*__first1));
4077 __d.__incr((value_type*)0);
4078 ++__first2;
4079 ::new(__first2) value_type(_STD::move(*__last1));
4080 }
4081 __h2.release();
4082 return;
4083 }
4084 if (__len <= 8)
4085 {
4086 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4087 return;
4088 }
4089 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4090 _RandomAccessIterator __m = __first1 + __l2;
4091 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4092 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4093 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4094}
4095
4096template <class _Tp>
4097struct __stable_sort_switch
4098{
4099 static const unsigned value = 128*has_trivial_copy_assign<_Tp>::value;
4100};
4101
4102template <class _Compare, class _RandomAccessIterator>
4103void
4104__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4105 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4106 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4107{
4108 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4109 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4110 switch (__len)
4111 {
4112 case 0:
4113 case 1:
4114 return;
4115 case 2:
4116 if (__comp(*--__last, *__first))
4117 swap(*__first, *__last);
4118 return;
4119 }
4120 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4121 {
4122 __insertion_sort<_Compare>(__first, __last, __comp);
4123 return;
4124 }
4125 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4126 _RandomAccessIterator __m = __first + __l2;
4127 if (__len <= __buff_size)
4128 {
4129 __destruct_n __d(0);
4130 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4131 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4132 __d.__set(__l2, (value_type*)0);
4133 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4134 __d.__set(__len, (value_type*)0);
4135 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4136// __merge<_Compare>(move_iterator<value_type*>(__buff),
4137// move_iterator<value_type*>(__buff + __l2),
4138// move_iterator<_RandomAccessIterator>(__buff + __l2),
4139// move_iterator<_RandomAccessIterator>(__buff + __len),
4140// __first, __comp);
4141 return;
4142 }
4143 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4144 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4145 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4146}
4147
4148template <class _RandomAccessIterator, class _Compare>
4149inline _LIBCPP_INLINE_VISIBILITY
4150void
4151stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4152{
4153 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4154 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4155 difference_type __len = __last - __first;
4156 pair<value_type*, ptrdiff_t> __buf(0, 0);
4157 unique_ptr<value_type, __return_temporary_buffer> __h;
4158 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4159 {
4160 __buf = _STD::get_temporary_buffer<value_type>(__len);
4161 __h.reset(__buf.first);
4162 }
4163#ifdef _LIBCPP_DEBUG
4164 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4165 __debug_less<_Compare> __c(__comp);
4166 __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
4167#else
4168 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4169 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
4170#endif
4171}
4172
4173template <class _RandomAccessIterator>
4174inline _LIBCPP_INLINE_VISIBILITY
4175void
4176stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4177{
4178 _STD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4179}
4180
4181// is_heap_until
4182
4183template <class _RandomAccessIterator, class _Compare>
4184_RandomAccessIterator
4185is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4186{
4187 typedef typename _STD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4188 difference_type __len = __last - __first;
4189 difference_type __p = 0;
4190 difference_type __c = 1;
4191 _RandomAccessIterator __pp = __first;
4192 while (__c < __len)
4193 {
4194 _RandomAccessIterator __cp = __first + __c;
4195 if (__comp(*__pp, *__cp))
4196 return __cp;
4197 ++__c;
4198 ++__cp;
4199 if (__c == __len)
4200 return __last;
4201 if (__comp(*__pp, *__cp))
4202 return __cp;
4203 ++__p;
4204 ++__pp;
4205 __c = 2 * __p + 1;
4206 }
4207 return __last;
4208}
4209
4210template<class _RandomAccessIterator>
4211inline _LIBCPP_INLINE_VISIBILITY
4212_RandomAccessIterator
4213is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4214{
4215 return _STD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4216}
4217
4218// is_heap
4219
4220template <class _RandomAccessIterator, class _Compare>
4221inline _LIBCPP_INLINE_VISIBILITY
4222bool
4223is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4224{
4225 return _STD::is_heap_until(__first, __last, __comp) == __last;
4226}
4227
4228template<class _RandomAccessIterator>
4229inline _LIBCPP_INLINE_VISIBILITY
4230bool
4231is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4232{
4233 return _STD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4234}
4235
4236// push_heap
4237
4238template <class _Compare, class _RandomAccessIterator>
4239void
4240__push_heap_front(_RandomAccessIterator __first, _RandomAccessIterator, _Compare __comp,
4241 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4242{
4243 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4244 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4245 if (__len > 1)
4246 {
4247 difference_type __p = 0;
4248 _RandomAccessIterator __pp = __first;
4249 difference_type __c = 2;
4250 _RandomAccessIterator __cp = __first + __c;
4251 if (__c == __len || __comp(*__cp, *(__cp - 1)))
4252 {
4253 --__c;
4254 --__cp;
4255 }
4256 if (__comp(*__pp, *__cp))
4257 {
4258 value_type __t(_STD::move(*__pp));
4259 do
4260 {
4261 *__pp = _STD::move(*__cp);
4262 __pp = __cp;
4263 __p = __c;
4264 __c = (__p + 1) * 2;
4265 if (__c > __len)
4266 break;
4267 __cp = __first + __c;
4268 if (__c == __len || __comp(*__cp, *(__cp - 1)))
4269 {
4270 --__c;
4271 --__cp;
4272 }
4273 } while (__comp(__t, *__cp));
4274 *__pp = _STD::move(__t);
4275 }
4276 }
4277}
4278
4279template <class _Compare, class _RandomAccessIterator>
4280void
4281__push_heap_back(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4282 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4283{
4284 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4285 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4286 if (__len > 1)
4287 {
4288 __len = (__len - 2) / 2;
4289 _RandomAccessIterator __ptr = __first + __len;
4290 if (__comp(*__ptr, *--__last))
4291 {
4292 value_type __t(_STD::move(*__last));
4293 do
4294 {
4295 *__last = _STD::move(*__ptr);
4296 __last = __ptr;
4297 if (__len == 0)
4298 break;
4299 __len = (__len - 1) / 2;
4300 __ptr = __first + __len;
4301 } while (__comp(*__ptr, __t));
4302 *__last = _STD::move(__t);
4303 }
4304 }
4305}
4306
4307template <class _RandomAccessIterator, class _Compare>
4308inline _LIBCPP_INLINE_VISIBILITY
4309void
4310push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4311{
4312#ifdef _LIBCPP_DEBUG
4313 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4314 __debug_less<_Compare> __c(__comp);
4315 __push_heap_back<_Comp_ref>(__first, __last, __c, __last - __first);
4316#else
4317 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4318 __push_heap_back<_Comp_ref>(__first, __last, __comp, __last - __first);
4319#endif
4320}
4321
4322template <class _RandomAccessIterator>
4323inline _LIBCPP_INLINE_VISIBILITY
4324void
4325push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4326{
4327 _STD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4328}
4329
4330// pop_heap
4331
4332template <class _Compare, class _RandomAccessIterator>
4333inline _LIBCPP_INLINE_VISIBILITY
4334void
4335__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4336 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4337{
4338 if (__len > 1)
4339 {
4340 swap(*__first, *--__last);
4341 __push_heap_front<_Compare>(__first, __last, __comp, __len-1);
4342 }
4343}
4344
4345template <class _RandomAccessIterator, class _Compare>
4346inline _LIBCPP_INLINE_VISIBILITY
4347void
4348pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4349{
4350#ifdef _LIBCPP_DEBUG
4351 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4352 __debug_less<_Compare> __c(__comp);
4353 __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
4354#else
4355 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4356 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
4357#endif
4358}
4359
4360template <class _RandomAccessIterator>
4361inline _LIBCPP_INLINE_VISIBILITY
4362void
4363pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4364{
4365 _STD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4366}
4367
4368// make_heap
4369
4370template <class _Compare, class _RandomAccessIterator>
4371void
4372__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4373{
4374 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4375 difference_type __n = __last - __first;
4376 if (__n > 1)
4377 {
4378 __last = __first;
4379 ++__last;
4380 for (difference_type __i = 1; __i < __n;)
4381 __push_heap_back<_Compare>(__first, ++__last, __comp, ++__i);
4382 }
4383}
4384
4385template <class _RandomAccessIterator, class _Compare>
4386inline _LIBCPP_INLINE_VISIBILITY
4387void
4388make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4389{
4390#ifdef _LIBCPP_DEBUG
4391 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4392 __debug_less<_Compare> __c(__comp);
4393 __make_heap<_Comp_ref>(__first, __last, __c);
4394#else
4395 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4396 __make_heap<_Comp_ref>(__first, __last, __comp);
4397#endif
4398}
4399
4400template <class _RandomAccessIterator>
4401inline _LIBCPP_INLINE_VISIBILITY
4402void
4403make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4404{
4405 _STD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4406}
4407
4408// sort_heap
4409
4410template <class _Compare, class _RandomAccessIterator>
4411void
4412__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4413{
4414 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4415 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
4416 __pop_heap<_Compare>(__first, __last, __comp, __n);
4417}
4418
4419template <class _RandomAccessIterator, class _Compare>
4420inline _LIBCPP_INLINE_VISIBILITY
4421void
4422sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4423{
4424#ifdef _LIBCPP_DEBUG
4425 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4426 __debug_less<_Compare> __c(__comp);
4427 __sort_heap<_Comp_ref>(__first, __last, __c);
4428#else
4429 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4430 __sort_heap<_Comp_ref>(__first, __last, __comp);
4431#endif
4432}
4433
4434template <class _RandomAccessIterator>
4435inline _LIBCPP_INLINE_VISIBILITY
4436void
4437sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4438{
4439 _STD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4440}
4441
4442// partial_sort
4443
4444template <class _Compare, class _RandomAccessIterator>
4445void
4446__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
4447 _Compare __comp)
4448{
4449 __make_heap<_Compare>(__first, __middle, __comp);
4450 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
4451 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
4452 {
4453 if (__comp(*__i, *__first))
4454 {
4455 swap(*__i, *__first);
4456 __push_heap_front<_Compare>(__first, __middle, __comp, __len);
4457 }
4458 }
4459 __sort_heap<_Compare>(__first, __middle, __comp);
4460}
4461
4462template <class _RandomAccessIterator, class _Compare>
4463inline _LIBCPP_INLINE_VISIBILITY
4464void
4465partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
4466 _Compare __comp)
4467{
4468#ifdef _LIBCPP_DEBUG
4469 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4470 __debug_less<_Compare> __c(__comp);
4471 __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
4472#else
4473 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4474 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
4475#endif
4476}
4477
4478template <class _RandomAccessIterator>
4479inline _LIBCPP_INLINE_VISIBILITY
4480void
4481partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
4482{
4483 _STD::partial_sort(__first, __middle, __last,
4484 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4485}
4486
4487// partial_sort_copy
4488
4489template <class _Compare, class _InputIterator, class _RandomAccessIterator>
4490_RandomAccessIterator
4491__partial_sort_copy(_InputIterator __first, _InputIterator __last,
4492 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
4493{
4494 _RandomAccessIterator __r = __result_first;
4495 if (__r != __result_last)
4496 {
4497 typename iterator_traits<_RandomAccessIterator>::difference_type __len = 0;
4498 for (; __first != __last && __r != __result_last; ++__first, ++__r, ++__len)
4499 *__r = *__first;
4500 __make_heap<_Compare>(__result_first, __r, __comp);
4501 for (; __first != __last; ++__first)
4502 if (__comp(*__first, *__result_first))
4503 {
4504 *__result_first = *__first;
4505 __push_heap_front<_Compare>(__result_first, __r, __comp, __len);
4506 }
4507 __sort_heap<_Compare>(__result_first, __r, __comp);
4508 }
4509 return __r;
4510}
4511
4512template <class _InputIterator, class _RandomAccessIterator, class _Compare>
4513inline _LIBCPP_INLINE_VISIBILITY
4514_RandomAccessIterator
4515partial_sort_copy(_InputIterator __first, _InputIterator __last,
4516 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
4517{
4518#ifdef _LIBCPP_DEBUG
4519 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4520 __debug_less<_Compare> __c(__comp);
4521 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
4522#else
4523 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4524 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
4525#endif
4526}
4527
4528template <class _InputIterator, class _RandomAccessIterator>
4529inline _LIBCPP_INLINE_VISIBILITY
4530_RandomAccessIterator
4531partial_sort_copy(_InputIterator __first, _InputIterator __last,
4532 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
4533{
4534 return _STD::partial_sort_copy(__first, __last, __result_first, __result_last,
4535 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4536}
4537
4538// nth_element
4539
4540template <class _Compare, class _RandomAccessIterator>
4541void
4542__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
4543{
4544 // _Compare is known to be a reference type
4545 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4546 const difference_type __limit = 7;
4547 while (true)
4548 {
4549 __restart:
4550 difference_type __len = __last - __first;
4551 switch (__len)
4552 {
4553 case 0:
4554 case 1:
4555 return;
4556 case 2:
4557 if (__comp(*--__last, *__first))
4558 swap(*__first, *__last);
4559 return;
4560 case 3:
4561 {
4562 _RandomAccessIterator __m = __first;
4563 _STD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
4564 return;
4565 }
4566 }
4567 if (__len <= __limit)
4568 {
4569 __selection_sort<_Compare>(__first, __last, __comp);
4570 return;
4571 }
4572 // __len > __limit >= 3
4573 _RandomAccessIterator __m = __first + __len/2;
4574 _RandomAccessIterator __lm1 = __last;
4575 unsigned __n_swaps = _STD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
4576 // *__m is median
4577 // partition [__first, __m) < *__m and *__m <= [__m, __last)
4578 // (this inhibits tossing elements equivalent to __m around unnecessarily)
4579 _RandomAccessIterator __i = __first;
4580 _RandomAccessIterator __j = __lm1;
4581 // j points beyond range to be tested, *__lm1 is known to be <= *__m
4582 // The search going up is known to be guarded but the search coming down isn't.
4583 // Prime the downward search with a guard.
4584 if (!__comp(*__i, *__m)) // if *__first == *__m
4585 {
4586 // *__first == *__m, *__first doesn't go in first part
4587 // manually guard downward moving __j against __i
4588 while (true)
4589 {
4590 if (__i == --__j)
4591 {
4592 // *__first == *__m, *__m <= all other elements
4593 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
4594 ++__i; // __first + 1
4595 __j = __last;
4596 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
4597 {
4598 while (true)
4599 {
4600 if (__i == __j)
4601 return; // [__first, __last) all equivalent elements
4602 if (__comp(*__first, *__i))
4603 {
4604 swap(*__i, *__j);
4605 ++__n_swaps;
4606 ++__i;
4607 break;
4608 }
4609 ++__i;
4610 }
4611 }
4612 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
4613 if (__i == __j)
4614 return;
4615 while (true)
4616 {
4617 while (!__comp(*__first, *__i))
4618 ++__i;
4619 while (__comp(*__first, *--__j))
4620 ;
4621 if (__i >= __j)
4622 break;
4623 swap(*__i, *__j);
4624 ++__n_swaps;
4625 ++__i;
4626 }
4627 // [__first, __i) == *__first and *__first < [__i, __last)
4628 // The first part is sorted,
4629 if (__nth < __i)
4630 return;
4631 // __nth_element the secod part
4632 // __nth_element<_Compare>(__i, __nth, __last, __comp);
4633 __first = __i;
4634 goto __restart;
4635 }
4636 if (__comp(*__j, *__m))
4637 {
4638 swap(*__i, *__j);
4639 ++__n_swaps;
4640 break; // found guard for downward moving __j, now use unguarded partition
4641 }
4642 }
4643 }
4644 ++__i;
4645 // j points beyond range to be tested, *__lm1 is known to be <= *__m
4646 // if not yet partitioned...
4647 if (__i < __j)
4648 {
4649 // known that *(__i - 1) < *__m
4650 while (true)
4651 {
4652 // __m still guards upward moving __i
4653 while (__comp(*__i, *__m))
4654 ++__i;
4655 // It is now known that a guard exists for downward moving __j
4656 while (!__comp(*--__j, *__m))
4657 ;
4658 if (__i >= __j)
4659 break;
4660 swap(*__i, *__j);
4661 ++__n_swaps;
4662 // It is known that __m != __j
4663 // If __m just moved, follow it
4664 if (__m == __i)
4665 __m = __j;
4666 ++__i;
4667 }
4668 }
4669 // [__first, __i) < *__m and *__m <= [__i, __last)
4670 if (__i != __m && __comp(*__m, *__i))
4671 {
4672 swap(*__i, *__m);
4673 ++__n_swaps;
4674 }
4675 // [__first, __i) < *__i and *__i <= [__i+1, __last)
4676 if (__nth == __i)
4677 return;
4678 if (__n_swaps == 0)
4679 {
4680 // We were given a perfectly partitioned sequence. Coincidence?
4681 if (__nth < __i)
4682 {
4683 // Check for [__first, __i) already sorted
4684 __j = __m = __first;
4685 while (++__j != __i)
4686 {
4687 if (__comp(*__j, *__m))
4688 // not yet sorted, so sort
4689 goto not_sorted;
4690 __m = __j;
4691 }
4692 // [__first, __i) sorted
4693 return;
4694 }
4695 else
4696 {
4697 // Check for [__i, __last) already sorted
4698 __j = __m = __i;
4699 while (++__j != __last)
4700 {
4701 if (__comp(*__j, *__m))
4702 // not yet sorted, so sort
4703 goto not_sorted;
4704 __m = __j;
4705 }
4706 // [__i, __last) sorted
4707 return;
4708 }
4709 }
4710not_sorted:
4711 // __nth_element on range containing __nth
4712 if (__nth < __i)
4713 {
4714 // __nth_element<_Compare>(__first, __nth, __i, __comp);
4715 __last = __i;
4716 }
4717 else
4718 {
4719 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
4720 __first = ++__i;
4721 }
4722 }
4723}
4724
4725template <class _RandomAccessIterator, class _Compare>
4726inline _LIBCPP_INLINE_VISIBILITY
4727void
4728nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
4729{
4730#ifdef _LIBCPP_DEBUG
4731 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4732 __debug_less<_Compare> __c(__comp);
4733 __nth_element<_Comp_ref>(__first, __nth, __last, __c);
4734#else
4735 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4736 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
4737#endif
4738}
4739
4740template <class _RandomAccessIterator>
4741inline _LIBCPP_INLINE_VISIBILITY
4742void
4743nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
4744{
4745 _STD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4746}
4747
4748// includes
4749
4750template <class _Compare, class _InputIterator1, class _InputIterator2>
4751bool
4752__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
4753 _Compare __comp)
4754{
4755 for (; __first2 != __last2; ++__first1)
4756 {
4757 if (__first1 == __last1 || __comp(*__first2, *__first1))
4758 return false;
4759 if (!__comp(*__first1, *__first2))
4760 ++__first2;
4761 }
4762 return true;
4763}
4764
4765template <class _InputIterator1, class _InputIterator2, class _Compare>
4766inline _LIBCPP_INLINE_VISIBILITY
4767bool
4768includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
4769 _Compare __comp)
4770{
4771#ifdef _LIBCPP_DEBUG
4772 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4773 __debug_less<_Compare> __c(__comp);
4774 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
4775#else
4776 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4777 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
4778#endif
4779}
4780
4781template <class _InputIterator1, class _InputIterator2>
4782inline _LIBCPP_INLINE_VISIBILITY
4783bool
4784includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
4785{
4786 return _STD::includes(__first1, __last1, __first2, __last2,
4787 __less<typename iterator_traits<_InputIterator1>::value_type,
4788 typename iterator_traits<_InputIterator2>::value_type>());
4789}
4790
4791// set_union
4792
4793template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4794_OutputIterator
4795__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
4796 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4797{
4798 for (; __first1 != __last1; ++__result)
4799 {
4800 if (__first2 == __last2)
4801 return _STD::copy(__first1, __last1, __result);
4802 if (__comp(*__first2, *__first1))
4803 {
4804 *__result = *__first2;
4805 ++__first2;
4806 }
4807 else
4808 {
4809 *__result = *__first1;
4810 if (!__comp(*__first1, *__first2))
4811 ++__first2;
4812 ++__first1;
4813 }
4814 }
4815 return _STD::copy(__first2, __last2, __result);
4816}
4817
4818template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4819inline _LIBCPP_INLINE_VISIBILITY
4820_OutputIterator
4821set_union(_InputIterator1 __first1, _InputIterator1 __last1,
4822 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4823{
4824#ifdef _LIBCPP_DEBUG
4825 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4826 __debug_less<_Compare> __c(__comp);
4827 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
4828#else
4829 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4830 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
4831#endif
4832}
4833
4834template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4835inline _LIBCPP_INLINE_VISIBILITY
4836_OutputIterator
4837set_union(_InputIterator1 __first1, _InputIterator1 __last1,
4838 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4839{
4840 return _STD::set_union(__first1, __last1, __first2, __last2, __result,
4841 __less<typename iterator_traits<_InputIterator1>::value_type,
4842 typename iterator_traits<_InputIterator2>::value_type>());
4843}
4844
4845// set_intersection
4846
4847template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4848_OutputIterator
4849__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
4850 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4851{
4852 while (__first1 != __last1 && __first2 != __last2)
4853 {
4854 if (__comp(*__first1, *__first2))
4855 ++__first1;
4856 else
4857 {
4858 if (!__comp(*__first2, *__first1))
4859 {
4860 *__result = *__first1;
4861 ++__result;
4862 ++__first1;
4863 }
4864 ++__first2;
4865 }
4866 }
4867 return __result;
4868}
4869
4870template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4871inline _LIBCPP_INLINE_VISIBILITY
4872_OutputIterator
4873set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
4874 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4875{
4876#ifdef _LIBCPP_DEBUG
4877 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4878 __debug_less<_Compare> __c(__comp);
4879 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
4880#else
4881 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4882 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
4883#endif
4884}
4885
4886template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4887inline _LIBCPP_INLINE_VISIBILITY
4888_OutputIterator
4889set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
4890 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4891{
4892 return _STD::set_intersection(__first1, __last1, __first2, __last2, __result,
4893 __less<typename iterator_traits<_InputIterator1>::value_type,
4894 typename iterator_traits<_InputIterator2>::value_type>());
4895}
4896
4897// set_difference
4898
4899template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4900_OutputIterator
4901__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
4902 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4903{
4904 while (__first1 != __last1)
4905 {
4906 if (__first2 == __last2)
4907 return _STD::copy(__first1, __last1, __result);
4908 if (__comp(*__first1, *__first2))
4909 {
4910 *__result = *__first1;
4911 ++__result;
4912 ++__first1;
4913 }
4914 else
4915 {
4916 if (!__comp(*__first2, *__first1))
4917 ++__first1;
4918 ++__first2;
4919 }
4920 }
4921 return __result;
4922}
4923
4924template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4925inline _LIBCPP_INLINE_VISIBILITY
4926_OutputIterator
4927set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
4928 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4929{
4930#ifdef _LIBCPP_DEBUG
4931 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4932 __debug_less<_Compare> __c(__comp);
4933 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
4934#else
4935 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4936 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
4937#endif
4938}
4939
4940template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4941inline _LIBCPP_INLINE_VISIBILITY
4942_OutputIterator
4943set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
4944 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4945{
4946 return _STD::set_difference(__first1, __last1, __first2, __last2, __result,
4947 __less<typename iterator_traits<_InputIterator1>::value_type,
4948 typename iterator_traits<_InputIterator2>::value_type>());
4949}
4950
4951// set_symmetric_difference
4952
4953template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4954_OutputIterator
4955__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
4956 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4957{
4958 while (__first1 != __last1)
4959 {
4960 if (__first2 == __last2)
4961 return _STD::copy(__first1, __last1, __result);
4962 if (__comp(*__first1, *__first2))
4963 {
4964 *__result = *__first1;
4965 ++__result;
4966 ++__first1;
4967 }
4968 else
4969 {
4970 if (__comp(*__first2, *__first1))
4971 {
4972 *__result = *__first2;
4973 ++__result;
4974 }
4975 else
4976 ++__first1;
4977 ++__first2;
4978 }
4979 }
4980 return _STD::copy(__first2, __last2, __result);
4981}
4982
4983template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4984inline _LIBCPP_INLINE_VISIBILITY
4985_OutputIterator
4986set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
4987 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4988{
4989#ifdef _LIBCPP_DEBUG
4990 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4991 __debug_less<_Compare> __c(__comp);
4992 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
4993#else
4994 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4995 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
4996#endif
4997}
4998
4999template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5000inline _LIBCPP_INLINE_VISIBILITY
5001_OutputIterator
5002set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5003 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5004{
5005 return _STD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
5006 __less<typename iterator_traits<_InputIterator1>::value_type,
5007 typename iterator_traits<_InputIterator2>::value_type>());
5008}
5009
5010// lexicographical_compare
5011
5012template <class _Compare, class _InputIterator1, class _InputIterator2>
5013bool
5014__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5015 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5016{
5017 for (; __first2 != __last2; ++__first1, ++__first2)
5018 {
5019 if (__first1 == __last1 || __comp(*__first1, *__first2))
5020 return true;
5021 if (__comp(*__first2, *__first1))
5022 return false;
5023 }
5024 return false;
5025}
5026
5027template <class _InputIterator1, class _InputIterator2, class _Compare>
5028inline _LIBCPP_INLINE_VISIBILITY
5029bool
5030lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5031 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5032{
5033#ifdef _LIBCPP_DEBUG
5034 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5035 __debug_less<_Compare> __c(__comp);
5036 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
5037#else
5038 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5039 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
5040#endif
5041}
5042
5043template <class _InputIterator1, class _InputIterator2>
5044inline _LIBCPP_INLINE_VISIBILITY
5045bool
5046lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5047 _InputIterator2 __first2, _InputIterator2 __last2)
5048{
5049 return _STD::lexicographical_compare(__first1, __last1, __first2, __last2,
5050 __less<typename iterator_traits<_InputIterator1>::value_type,
5051 typename iterator_traits<_InputIterator2>::value_type>());
5052}
5053
5054// next_permutation
5055
5056template <class _Compare, class _BidirectionalIterator>
5057bool
5058__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5059{
5060 _BidirectionalIterator __i = __last;
5061 if (__first == __last || __first == --__i)
5062 return false;
5063 while (true)
5064 {
5065 _BidirectionalIterator __ip1 = __i;
5066 if (__comp(*--__i, *__ip1))
5067 {
5068 _BidirectionalIterator __j = __last;
5069 while (!__comp(*__i, *--__j))
5070 ;
5071 swap(*__i, *__j);
5072 _STD::reverse(__ip1, __last);
5073 return true;
5074 }
5075 if (__i == __first)
5076 {
5077 _STD::reverse(__first, __last);
5078 return false;
5079 }
5080 }
5081}
5082
5083template <class _BidirectionalIterator, class _Compare>
5084inline _LIBCPP_INLINE_VISIBILITY
5085bool
5086next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5087{
5088#ifdef _LIBCPP_DEBUG
5089 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5090 __debug_less<_Compare> __c(__comp);
5091 return __next_permutation<_Comp_ref>(__first, __last, __c);
5092#else
5093 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5094 return __next_permutation<_Comp_ref>(__first, __last, __comp);
5095#endif
5096}
5097
5098template <class _BidirectionalIterator>
5099inline _LIBCPP_INLINE_VISIBILITY
5100bool
5101next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5102{
5103 return _STD::next_permutation(__first, __last,
5104 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5105}
5106
5107// prev_permutation
5108
5109template <class _Compare, class _BidirectionalIterator>
5110bool
5111__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5112{
5113 _BidirectionalIterator __i = __last;
5114 if (__first == __last || __first == --__i)
5115 return false;
5116 while (true)
5117 {
5118 _BidirectionalIterator __ip1 = __i;
5119 if (__comp(*__ip1, *--__i))
5120 {
5121 _BidirectionalIterator __j = __last;
5122 while (!__comp(*--__j, *__i))
5123 ;
5124 swap(*__i, *__j);
5125 _STD::reverse(__ip1, __last);
5126 return true;
5127 }
5128 if (__i == __first)
5129 {
5130 _STD::reverse(__first, __last);
5131 return false;
5132 }
5133 }
5134}
5135
5136template <class _BidirectionalIterator, class _Compare>
5137inline _LIBCPP_INLINE_VISIBILITY
5138bool
5139prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5140{
5141#ifdef _LIBCPP_DEBUG
5142 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5143 __debug_less<_Compare> __c(__comp);
5144 return __prev_permutation<_Comp_ref>(__first, __last, __c);
5145#else
5146 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5147 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
5148#endif
5149}
5150
5151template <class _BidirectionalIterator>
5152inline _LIBCPP_INLINE_VISIBILITY
5153bool
5154prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5155{
5156 return _STD::prev_permutation(__first, __last,
5157 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5158}
5159
5160template <class _Tp>
5161inline _LIBCPP_INLINE_VISIBILITY
5162typename enable_if
5163<
5164 is_integral<_Tp>::value,
5165 _Tp
5166>::type
5167__rotate_left(_Tp __t, _Tp __n = 1)
5168{
5169 const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5170 __n &= __bits;
5171 return static_cast<_Tp>((__t << __n) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> (__bits - __n)));
5172}
5173
5174template <class _Tp>
5175inline _LIBCPP_INLINE_VISIBILITY
5176typename enable_if
5177<
5178 is_integral<_Tp>::value,
5179 _Tp
5180>::type
5181__rotate_right(_Tp __t, _Tp __n = 1)
5182{
5183 const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5184 __n &= __bits;
5185 return static_cast<_Tp>((__t << (__bits - __n)) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> __n));
5186}
5187
5188// Precondition: __x != 0
5189inline _LIBCPP_INLINE_VISIBILITY unsigned __ctz(unsigned __x) {return __builtin_ctz (__x);}
5190inline _LIBCPP_INLINE_VISIBILITY unsigned long __ctz(unsigned long __x) {return __builtin_ctzl (__x);}
5191inline _LIBCPP_INLINE_VISIBILITY unsigned long long __ctz(unsigned long long __x) {return __builtin_ctzll(__x);}
5192
5193// Precondition: __x != 0
5194inline _LIBCPP_INLINE_VISIBILITY unsigned __clz(unsigned __x) {return __builtin_clz (__x);}
5195inline _LIBCPP_INLINE_VISIBILITY unsigned long __clz(unsigned long __x) {return __builtin_clzl (__x);}
5196inline _LIBCPP_INLINE_VISIBILITY unsigned long long __clz(unsigned long long __x) {return __builtin_clzll(__x);}
5197
5198inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) {return __builtin_popcount (__x);}
5199inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) {return __builtin_popcountl (__x);}
5200inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {return __builtin_popcountll(__x);}
5201
5202_LIBCPP_END_NAMESPACE_STD
5203
5204#endif // _LIBCPP_ALGORITHM