blob: d6906a209e85cd03f2993112b627e225742ef026 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- algorithm ---------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_ALGORITHM
12#define _LIBCPP_ALGORITHM
13
14/*
15 algorithm synopsis
16
17#include <initializer_list>
18
19namespace std
20{
21
22template <class InputIterator, class Predicate>
23 bool
24 all_of(InputIterator first, InputIterator last, Predicate pred);
25
26template <class InputIterator, class Predicate>
27 bool
28 any_of(InputIterator first, InputIterator last, Predicate pred);
29
30template <class InputIterator, class Predicate>
31 bool
32 none_of(InputIterator first, InputIterator last, Predicate pred);
33
34template <class InputIterator, class Function>
35 Function
36 for_each(InputIterator first, InputIterator last, Function f);
37
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,
Howard Hinnant278bf2d2010-11-18 01:47:02 +0000259 UniformRandomNumberGenerator&& g);
Howard Hinnantc3267212010-05-26 17:49:34 +0000260
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
Howard Hinnant324bb032010-08-22 00:02:43 +0000472template <class RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000473 bool
Howard Hinnant324bb032010-08-22 00:02:43 +0000474 is_heap(RandomAccessIterator first, RandomAccessiterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000475
Howard Hinnant324bb032010-08-22 00:02:43 +0000476template <class RandomAccessIterator, class Compare>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000477 bool
Howard Hinnant324bb032010-08-22 00:02:43 +0000478 is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000479
Howard Hinnant324bb032010-08-22 00:02:43 +0000480template <class RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000481 RandomAccessIterator
Howard Hinnant324bb032010-08-22 00:02:43 +0000482 is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000483
Howard Hinnant324bb032010-08-22 00:02:43 +0000484template <class RandomAccessIterator, class Compare>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000485 RandomAccessIterator
Howard Hinnant324bb032010-08-22 00:02:43 +0000486 is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000487
Howard Hinnant98e5d972010-08-21 20:10:01 +0000488template <class ForwardIterator>
489 ForwardIterator
490 min_element(ForwardIterator first, ForwardIterator last);
491
492template <class ForwardIterator, class Compare>
493 ForwardIterator
494 min_element(ForwardIterator first, ForwardIterator last, Compare comp);
495
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000496template <class T>
497 const T&
498 min(const T& a, const T& b);
499
500template <class T, class Compare>
501 const T&
502 min(const T& a, const T& b, Compare comp);
503
Howard Hinnant98e5d972010-08-21 20:10:01 +0000504template<class T>
505 T
506 min(initializer_list<T> t);
507
508template<class T, class Compare>
509 T
510 min(initializer_list<T> t, 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
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000520template <class T>
521 const T&
522 max(const T& a, const T& b);
523
524template <class T, class Compare>
525 const T&
526 max(const T& a, const T& b, Compare comp);
527
Howard Hinnant98e5d972010-08-21 20:10:01 +0000528template<class T>
529 T
530 max(initializer_list<T> t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000531
Howard Hinnant98e5d972010-08-21 20:10:01 +0000532template<class T, class Compare>
533 T
534 max(initializer_list<T> t, Compare comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000535
Howard Hinnant98e5d972010-08-21 20:10:01 +0000536template<class ForwardIterator>
537 pair<ForwardIterator, ForwardIterator>
538 minmax_element(ForwardIterator first, ForwardIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000539
Howard Hinnant98e5d972010-08-21 20:10:01 +0000540template<class ForwardIterator, class Compare>
541 pair<ForwardIterator, ForwardIterator>
542 minmax_element(ForwardIterator first, ForwardIterator last, Compare comp);
543
544template<class T>
545 pair<const T&, const T&>
546 minmax(const T& a, const T& b);
547
548template<class T, class Compare>
549 pair<const T&, const T&>
550 minmax(const T& a, const T& b, Compare comp);
551
552template<class T>
553 pair<T, T>
554 minmax(initializer_list<T> t);
555
556template<class T, class Compare>
557 pair<T, T>
558 minmax(initializer_list<T> t, Compare comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000559
560template <class InputIterator1, class InputIterator2>
561 bool
562 lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
563
564template <class InputIterator1, class InputIterator2, class Compare>
565 bool
566 lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
567 InputIterator2 first2, InputIterator2 last2, Compare comp);
568
569template <class BidirectionalIterator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000570 bool
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000571 next_permutation(BidirectionalIterator first, BidirectionalIterator last);
572
573template <class BidirectionalIterator, class Compare>
574 bool
575 next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
576
577template <class BidirectionalIterator>
578 bool
579 prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
580
581template <class BidirectionalIterator, class Compare>
582 bool
583 prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
584
585} // std
586
587*/
588
589#include <__config>
590#include <initializer_list>
591#include <type_traits>
592#include <cstring>
593#include <utility>
594#include <memory>
595#include <iterator>
Howard Hinnantadff4892010-05-24 17:49:41 +0000596#include <cstdlib>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000597
598#pragma GCC system_header
599
600_LIBCPP_BEGIN_NAMESPACE_STD
601
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000602template <class _T1, class _T2 = _T1>
603struct __equal_to
604{
605 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
606 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
607 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
608 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
609};
610
611template <class _T1>
612struct __equal_to<_T1, _T1>
613{
614 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
615};
616
617template <class _T1>
618struct __equal_to<const _T1, _T1>
619{
620 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
621};
622
623template <class _T1>
624struct __equal_to<_T1, const _T1>
625{
626 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
627};
628
629template <class _T1, class _T2 = _T1>
630struct __less
631{
632 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
633 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
634 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
635 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
636};
637
638template <class _T1>
639struct __less<_T1, _T1>
640{
641 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
642};
643
644template <class _T1>
645struct __less<const _T1, _T1>
646{
647 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
648};
649
650template <class _T1>
651struct __less<_T1, const _T1>
652{
653 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
654};
655
656template <class _Predicate>
657class __negate
658{
659private:
660 _Predicate __p_;
661public:
662 _LIBCPP_INLINE_VISIBILITY __negate() {}
663
664 _LIBCPP_INLINE_VISIBILITY
665 explicit __negate(_Predicate __p) : __p_(__p) {}
666
667 template <class _T1>
668 _LIBCPP_INLINE_VISIBILITY
669 bool operator()(const _T1& __x) {return !__p_(__x);}
670
671 template <class _T1, class _T2>
672 _LIBCPP_INLINE_VISIBILITY
673 bool operator()(const _T1& __x, const _T2& __y) {return !__p_(__x, __y);}
674};
675
Howard Hinnant7a563db2011-09-14 18:33:51 +0000676#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000677
678template <class _Compare>
679struct __debug_less
680{
681 _Compare __comp_;
682 __debug_less(_Compare& __c) : __comp_(__c) {}
683 template <class _Tp, class _Up>
684 bool operator()(const _Tp& __x, const _Up& __y)
685 {
686 bool __r = __comp_(__x, __y);
687 if (__r)
Howard Hinnant7a563db2011-09-14 18:33:51 +0000688 _LIBCPP_ASSERT(!__comp_(__y, __x), "Comparator does not induce a strict weak ordering");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000689 return __r;
690 }
691};
692
Howard Hinnant7a563db2011-09-14 18:33:51 +0000693#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000694
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000695// Precondition: __x != 0
696inline _LIBCPP_INLINE_VISIBILITY unsigned __ctz(unsigned __x) {return __builtin_ctz (__x);}
697inline _LIBCPP_INLINE_VISIBILITY unsigned long __ctz(unsigned long __x) {return __builtin_ctzl (__x);}
698inline _LIBCPP_INLINE_VISIBILITY unsigned long long __ctz(unsigned long long __x) {return __builtin_ctzll(__x);}
699
700// Precondition: __x != 0
701inline _LIBCPP_INLINE_VISIBILITY unsigned __clz(unsigned __x) {return __builtin_clz (__x);}
702inline _LIBCPP_INLINE_VISIBILITY unsigned long __clz(unsigned long __x) {return __builtin_clzl (__x);}
703inline _LIBCPP_INLINE_VISIBILITY unsigned long long __clz(unsigned long long __x) {return __builtin_clzll(__x);}
704
705inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) {return __builtin_popcount (__x);}
706inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) {return __builtin_popcountl (__x);}
707inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {return __builtin_popcountll(__x);}
708
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000709// all_of
710
711template <class _InputIterator, class _Predicate>
712inline _LIBCPP_INLINE_VISIBILITY
713bool
714all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
715{
716 for (; __first != __last; ++__first)
717 if (!__pred(*__first))
718 return false;
719 return true;
720}
721
722// any_of
723
724template <class _InputIterator, class _Predicate>
725inline _LIBCPP_INLINE_VISIBILITY
726bool
727any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
728{
729 for (; __first != __last; ++__first)
730 if (__pred(*__first))
731 return true;
732 return false;
733}
734
735// none_of
736
737template <class _InputIterator, class _Predicate>
738inline _LIBCPP_INLINE_VISIBILITY
739bool
740none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
741{
742 for (; __first != __last; ++__first)
743 if (__pred(*__first))
744 return false;
745 return true;
746}
747
748// for_each
749
750template <class _InputIterator, class _Function>
751inline _LIBCPP_INLINE_VISIBILITY
752_Function
753for_each(_InputIterator __first, _InputIterator __last, _Function __f)
754{
755 for (; __first != __last; ++__first)
756 __f(*__first);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000757 return _VSTD::move(__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000758}
759
760// find
761
762template <class _InputIterator, class _Tp>
763inline _LIBCPP_INLINE_VISIBILITY
764_InputIterator
765find(_InputIterator __first, _InputIterator __last, const _Tp& __value)
766{
767 for (; __first != __last; ++__first)
768 if (*__first == __value)
769 break;
770 return __first;
771}
772
773// find_if
774
775template <class _InputIterator, class _Predicate>
776inline _LIBCPP_INLINE_VISIBILITY
777_InputIterator
778find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
779{
780 for (; __first != __last; ++__first)
781 if (__pred(*__first))
782 break;
783 return __first;
784}
785
786// find_if_not
787
788template<class _InputIterator, class _Predicate>
789inline _LIBCPP_INLINE_VISIBILITY
790_InputIterator
791find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
792{
793 for (; __first != __last; ++__first)
794 if (!__pred(*__first))
795 break;
796 return __first;
797}
798
799// find_end
800
801template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
802_ForwardIterator1
803__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
804 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
805 forward_iterator_tag, forward_iterator_tag)
806{
807 // modeled after search algorithm
808 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
809 if (__first2 == __last2)
810 return __r;
811 while (true)
812 {
813 while (true)
814 {
815 if (__first1 == __last1) // if source exhausted return last correct answer
816 return __r; // (or __last1 if never found)
817 if (__pred(*__first1, *__first2))
818 break;
819 ++__first1;
820 }
821 // *__first1 matches *__first2, now match elements after here
822 _ForwardIterator1 __m1 = __first1;
823 _ForwardIterator2 __m2 = __first2;
824 while (true)
825 {
826 if (++__m2 == __last2)
827 { // Pattern exhaused, record answer and search for another one
828 __r = __first1;
829 ++__first1;
830 break;
831 }
832 if (++__m1 == __last1) // Source exhausted, return last answer
833 return __r;
834 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
835 {
836 ++__first1;
837 break;
838 } // else there is a match, check next elements
839 }
840 }
841}
842
843template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
844_BidirectionalIterator1
845__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
846 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
847 bidirectional_iterator_tag, bidirectional_iterator_tag)
848{
849 // modeled after search algorithm (in reverse)
850 if (__first2 == __last2)
851 return __last1; // Everything matches an empty sequence
852 _BidirectionalIterator1 __l1 = __last1;
853 _BidirectionalIterator2 __l2 = __last2;
854 --__l2;
855 while (true)
856 {
857 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
858 while (true)
859 {
860 if (__first1 == __l1) // return __last1 if no element matches *__first2
861 return __last1;
862 if (__pred(*--__l1, *__l2))
863 break;
864 }
865 // *__l1 matches *__l2, now match elements before here
866 _BidirectionalIterator1 __m1 = __l1;
867 _BidirectionalIterator2 __m2 = __l2;
868 while (true)
869 {
870 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
871 return __m1;
872 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
873 return __last1;
874 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
875 {
876 break;
877 } // else there is a match, check next elements
878 }
879 }
880}
881
882template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
883_RandomAccessIterator1
884__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
885 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
886 random_access_iterator_tag, random_access_iterator_tag)
887{
888 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
889 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
890 if (__len2 == 0)
891 return __last1;
892 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
893 if (__len1 < __len2)
894 return __last1;
895 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
896 _RandomAccessIterator1 __l1 = __last1;
897 _RandomAccessIterator2 __l2 = __last2;
898 --__l2;
899 while (true)
900 {
901 while (true)
902 {
903 if (__s == __l1)
904 return __last1;
905 if (__pred(*--__l1, *__l2))
906 break;
907 }
908 _RandomAccessIterator1 __m1 = __l1;
909 _RandomAccessIterator2 __m2 = __l2;
910 while (true)
911 {
912 if (__m2 == __first2)
913 return __m1;
914 // no need to check range on __m1 because __s guarantees we have enough source
915 if (!__pred(*--__m1, *--__m2))
916 {
917 break;
918 }
919 }
920 }
921}
922
923template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
924inline _LIBCPP_INLINE_VISIBILITY
925_ForwardIterator1
926find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
927 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
928{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000929 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000930 (__first1, __last1, __first2, __last2, __pred,
931 typename iterator_traits<_ForwardIterator1>::iterator_category(),
932 typename iterator_traits<_ForwardIterator2>::iterator_category());
933}
934
935template <class _ForwardIterator1, class _ForwardIterator2>
936inline _LIBCPP_INLINE_VISIBILITY
937_ForwardIterator1
938find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
939 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
940{
941 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
942 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000943 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000944}
945
946// find_first_of
947
948template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
949_ForwardIterator1
950find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
951 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
952{
953 for (; __first1 != __last1; ++__first1)
954 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
955 if (__pred(*__first1, *__j))
956 return __first1;
957 return __last1;
958}
959
960template <class _ForwardIterator1, class _ForwardIterator2>
961inline _LIBCPP_INLINE_VISIBILITY
962_ForwardIterator1
963find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
964 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
965{
966 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
967 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000968 return _VSTD::find_first_of(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000969}
970
971// adjacent_find
972
973template <class _ForwardIterator, class _BinaryPredicate>
974inline _LIBCPP_INLINE_VISIBILITY
975_ForwardIterator
976adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
977{
978 if (__first != __last)
979 {
980 _ForwardIterator __i = __first;
981 while (++__i != __last)
982 {
983 if (__pred(*__first, *__i))
984 return __first;
985 __first = __i;
986 }
987 }
988 return __last;
989}
990
991template <class _ForwardIterator>
992inline _LIBCPP_INLINE_VISIBILITY
993_ForwardIterator
994adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
995{
996 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000997 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000998}
999
1000// count
1001
1002template <class _InputIterator, class _Tp>
1003inline _LIBCPP_INLINE_VISIBILITY
1004typename iterator_traits<_InputIterator>::difference_type
1005count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
1006{
1007 typename iterator_traits<_InputIterator>::difference_type __r(0);
1008 for (; __first != __last; ++__first)
1009 if (*__first == __value)
1010 ++__r;
1011 return __r;
1012}
1013
1014// count_if
1015
1016template <class _InputIterator, class _Predicate>
1017inline _LIBCPP_INLINE_VISIBILITY
1018typename iterator_traits<_InputIterator>::difference_type
1019count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1020{
1021 typename iterator_traits<_InputIterator>::difference_type __r(0);
1022 for (; __first != __last; ++__first)
1023 if (__pred(*__first))
1024 ++__r;
1025 return __r;
1026}
1027
1028// mismatch
1029
1030template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1031inline _LIBCPP_INLINE_VISIBILITY
1032pair<_InputIterator1, _InputIterator2>
1033mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1034 _InputIterator2 __first2, _BinaryPredicate __pred)
1035{
1036 for (; __first1 != __last1; ++__first1, ++__first2)
1037 if (!__pred(*__first1, *__first2))
1038 break;
1039 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1040}
1041
1042template <class _InputIterator1, class _InputIterator2>
1043inline _LIBCPP_INLINE_VISIBILITY
1044pair<_InputIterator1, _InputIterator2>
1045mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1046{
1047 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1048 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001049 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001050}
1051
1052// equal
1053
1054template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1055inline _LIBCPP_INLINE_VISIBILITY
1056bool
1057equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1058{
1059 for (; __first1 != __last1; ++__first1, ++__first2)
1060 if (!__pred(*__first1, *__first2))
1061 return false;
1062 return true;
1063}
1064
1065template <class _InputIterator1, class _InputIterator2>
1066inline _LIBCPP_INLINE_VISIBILITY
1067bool
1068equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1069{
1070 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1071 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001072 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001073}
1074
1075// is_permutation
1076
1077template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1078bool
1079is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1080 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1081{
1082 // shorten sequences as much as possible by lopping of any equal parts
1083 for (; __first1 != __last1; ++__first1, ++__first2)
1084 if (!__pred(*__first1, *__first2))
1085 goto __not_done;
1086 return true;
1087__not_done:
1088 // __first1 != __last1 && *__first1 != *__first2
1089 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001090 _D1 __l1 = _VSTD::distance(__first1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001091 if (__l1 == _D1(1))
1092 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001093 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001094 // For each element in [f1, l1) see if there are the same number of
1095 // equal elements in [f2, l2)
1096 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1097 {
1098 // Have we already counted the number of *__i in [f1, l1)?
1099 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1100 if (__pred(*__j, *__i))
1101 goto __next_iter;
1102 {
1103 // Count number of *__i in [f2, l2)
1104 _D1 __c2 = 0;
1105 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1106 if (__pred(*__i, *__j))
1107 ++__c2;
1108 if (__c2 == 0)
1109 return false;
1110 // Count number of *__i in [__i, l1) (we can start with 1)
1111 _D1 __c1 = 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001112 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001113 if (__pred(*__i, *__j))
1114 ++__c1;
1115 if (__c1 != __c2)
1116 return false;
1117 }
1118__next_iter:;
1119 }
1120 return true;
1121}
1122
1123template<class _ForwardIterator1, class _ForwardIterator2>
1124inline _LIBCPP_INLINE_VISIBILITY
1125bool
1126is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1127 _ForwardIterator2 __first2)
1128{
1129 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1130 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001131 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001132}
1133
1134// search
1135
1136template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1137_ForwardIterator1
1138__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1139 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
1140 forward_iterator_tag, forward_iterator_tag)
1141{
1142 if (__first2 == __last2)
1143 return __first1; // Everything matches an empty sequence
1144 while (true)
1145 {
1146 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
1147 while (true)
1148 {
1149 if (__first1 == __last1) // return __last1 if no element matches *__first2
1150 return __last1;
1151 if (__pred(*__first1, *__first2))
1152 break;
1153 ++__first1;
1154 }
1155 // *__first1 matches *__first2, now match elements after here
1156 _ForwardIterator1 __m1 = __first1;
1157 _ForwardIterator2 __m2 = __first2;
1158 while (true)
1159 {
1160 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
1161 return __first1;
1162 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
1163 return __last1;
1164 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
1165 {
1166 ++__first1;
1167 break;
1168 } // else there is a match, check next elements
1169 }
1170 }
1171}
1172
1173template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1174_RandomAccessIterator1
1175__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1176 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1177 random_access_iterator_tag, random_access_iterator_tag)
1178{
1179 typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _D1;
1180 typedef typename std::iterator_traits<_RandomAccessIterator2>::difference_type _D2;
1181 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1182 _D2 __len2 = __last2 - __first2;
1183 if (__len2 == 0)
1184 return __first1;
1185 _D1 __len1 = __last1 - __first1;
1186 if (__len1 < __len2)
1187 return __last1;
1188 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
1189 while (true)
1190 {
1191#if !_LIBCPP_UNROLL_LOOPS
1192 while (true)
1193 {
1194 if (__first1 == __s)
1195 return __last1;
1196 if (__pred(*__first1, *__first2))
1197 break;
1198 ++__first1;
1199 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001200#else // !_LIBCPP_UNROLL_LOOPS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001201 for (_D1 __loop_unroll = (__s - __first1) / 4; __loop_unroll > 0; --__loop_unroll)
1202 {
1203 if (__pred(*__first1, *__first2))
1204 goto __phase2;
1205 if (__pred(*++__first1, *__first2))
1206 goto __phase2;
1207 if (__pred(*++__first1, *__first2))
1208 goto __phase2;
1209 if (__pred(*++__first1, *__first2))
1210 goto __phase2;
1211 ++__first1;
1212 }
1213 switch (__s - __first1)
1214 {
1215 case 3:
1216 if (__pred(*__first1, *__first2))
1217 break;
1218 ++__first1;
1219 case 2:
1220 if (__pred(*__first1, *__first2))
1221 break;
1222 ++__first1;
1223 case 1:
1224 if (__pred(*__first1, *__first2))
1225 break;
1226 case 0:
1227 return __last1;
1228 }
1229 __phase2:
Howard Hinnant324bb032010-08-22 00:02:43 +00001230#endif // !_LIBCPP_UNROLL_LOOPS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001231 _RandomAccessIterator1 __m1 = __first1;
1232 _RandomAccessIterator2 __m2 = __first2;
1233#if !_LIBCPP_UNROLL_LOOPS
1234 while (true)
1235 {
1236 if (++__m2 == __last2)
1237 return __first1;
1238 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
1239 if (!__pred(*__m1, *__m2))
1240 {
1241 ++__first1;
1242 break;
1243 }
1244 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001245#else // !_LIBCPP_UNROLL_LOOPS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001246 ++__m2;
1247 ++__m1;
1248 for (_D2 __loop_unroll = (__last2 - __m2) / 4; __loop_unroll > 0; --__loop_unroll)
1249 {
1250 if (!__pred(*__m1, *__m2))
1251 goto __continue;
1252 if (!__pred(*++__m1, *++__m2))
1253 goto __continue;
1254 if (!__pred(*++__m1, *++__m2))
1255 goto __continue;
1256 if (!__pred(*++__m1, *++__m2))
1257 goto __continue;
1258 ++__m1;
1259 ++__m2;
1260 }
1261 switch (__last2 - __m2)
1262 {
1263 case 3:
1264 if (!__pred(*__m1, *__m2))
1265 break;
1266 ++__m1;
1267 ++__m2;
1268 case 2:
1269 if (!__pred(*__m1, *__m2))
1270 break;
1271 ++__m1;
1272 ++__m2;
1273 case 1:
1274 if (!__pred(*__m1, *__m2))
1275 break;
1276 case 0:
1277 return __first1;
1278 }
1279 __continue:
1280 ++__first1;
Howard Hinnant324bb032010-08-22 00:02:43 +00001281#endif // !_LIBCPP_UNROLL_LOOPS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001282 }
1283}
1284
1285template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1286inline _LIBCPP_INLINE_VISIBILITY
1287_ForwardIterator1
1288search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1289 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1290{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001291 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001292 (__first1, __last1, __first2, __last2, __pred,
1293 typename std::iterator_traits<_ForwardIterator1>::iterator_category(),
1294 typename std::iterator_traits<_ForwardIterator2>::iterator_category());
1295}
1296
1297template <class _ForwardIterator1, class _ForwardIterator2>
1298inline _LIBCPP_INLINE_VISIBILITY
1299_ForwardIterator1
1300search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1301 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1302{
1303 typedef typename std::iterator_traits<_ForwardIterator1>::value_type __v1;
1304 typedef typename std::iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001305 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001306}
1307
1308// search_n
1309
1310template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
1311_ForwardIterator
1312__search_n(_ForwardIterator __first, _ForwardIterator __last,
1313 _Size __count, const _Tp& __value, _BinaryPredicate __pred, forward_iterator_tag)
1314{
1315 if (__count <= 0)
1316 return __first;
1317 while (true)
1318 {
1319 // Find first element in sequence that matchs __value, with a mininum of loop checks
1320 while (true)
1321 {
1322 if (__first == __last) // return __last if no element matches __value
1323 return __last;
1324 if (__pred(*__first, __value))
1325 break;
1326 ++__first;
1327 }
1328 // *__first matches __value, now match elements after here
1329 _ForwardIterator __m = __first;
1330 _Size __c(0);
1331 while (true)
1332 {
1333 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1334 return __first;
1335 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1336 return __last;
1337 if (!__pred(*__m, __value)) // if there is a mismatch, restart with a new __first
1338 {
1339 __first = __m;
1340 ++__first;
1341 break;
1342 } // else there is a match, check next elements
1343 }
1344 }
1345}
1346
1347template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
1348_RandomAccessIterator
1349__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
1350 _Size __count, const _Tp& __value, _BinaryPredicate __pred, random_access_iterator_tag)
1351{
1352 if (__count <= 0)
1353 return __first;
1354 _Size __len = static_cast<_Size>(__last - __first);
1355 if (__len < __count)
1356 return __last;
1357 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1358 while (true)
1359 {
1360 // Find first element in sequence that matchs __value, with a mininum of loop checks
1361 while (true)
1362 {
1363 if (__first == __s) // return __last if no element matches __value
1364 return __last;
1365 if (__pred(*__first, __value))
1366 break;
1367 ++__first;
1368 }
1369 // *__first matches __value, now match elements after here
1370 _RandomAccessIterator __m = __first;
1371 _Size __c(0);
1372 while (true)
1373 {
1374 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1375 return __first;
1376 ++__m; // no need to check range on __m because __s guarantees we have enough source
1377 if (!__pred(*__m, __value)) // if there is a mismatch, restart with a new __first
1378 {
1379 __first = __m;
1380 ++__first;
1381 break;
1382 } // else there is a match, check next elements
1383 }
1384 }
1385}
1386
1387template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
1388inline _LIBCPP_INLINE_VISIBILITY
1389_ForwardIterator
1390search_n(_ForwardIterator __first, _ForwardIterator __last,
1391 _Size __count, const _Tp& __value, _BinaryPredicate __pred)
1392{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001393 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001394 (__first, __last, __count, __value, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
1395}
1396
1397template <class _ForwardIterator, class _Size, class _Tp>
1398inline _LIBCPP_INLINE_VISIBILITY
1399_ForwardIterator
1400search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value)
1401{
1402 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001403 return _VSTD::search_n(__first, __last, __count, __value, __equal_to<__v, _Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001404}
1405
1406// copy
1407
1408template <class _Iter>
1409struct __libcpp_is_trivial_iterator
1410{
1411 static const bool value = is_pointer<_Iter>::value;
1412};
1413
1414template <class _Iter>
1415struct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
1416{
1417 static const bool value = is_pointer<_Iter>::value;
1418};
1419
1420template <class _Iter>
1421struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
1422{
1423 static const bool value = is_pointer<_Iter>::value;
1424};
1425
1426template <class _Iter>
1427inline _LIBCPP_INLINE_VISIBILITY
1428_Iter
1429__unwrap_iter(_Iter __i)
1430{
1431 return __i;
1432}
1433
1434template <class _Tp>
1435inline _LIBCPP_INLINE_VISIBILITY
1436typename enable_if
1437<
Howard Hinnant1468b662010-11-19 22:17:28 +00001438 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001439 _Tp*
1440>::type
1441__unwrap_iter(move_iterator<_Tp*> __i)
1442{
1443 return __i.base();
1444}
1445
1446template <class _Tp>
1447inline _LIBCPP_INLINE_VISIBILITY
1448typename enable_if
1449<
Howard Hinnant1468b662010-11-19 22:17:28 +00001450 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001451 _Tp*
1452>::type
1453__unwrap_iter(__wrap_iter<_Tp*> __i)
1454{
1455 return __i.base();
1456}
1457
1458template <class _InputIterator, class _OutputIterator>
1459inline _LIBCPP_INLINE_VISIBILITY
1460_OutputIterator
1461__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1462{
1463 for (; __first != __last; ++__first, ++__result)
1464 *__result = *__first;
1465 return __result;
1466}
1467
1468template <class _Tp, class _Up>
1469inline _LIBCPP_INLINE_VISIBILITY
1470typename enable_if
1471<
1472 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001473 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001474 _Up*
1475>::type
1476__copy(_Tp* __first, _Tp* __last, _Up* __result)
1477{
1478 const size_t __n = static_cast<size_t>(__last - __first);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001479 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001480 return __result + __n;
1481}
1482
1483template <class _InputIterator, class _OutputIterator>
1484inline _LIBCPP_INLINE_VISIBILITY
1485_OutputIterator
1486copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1487{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001488 return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001489}
1490
1491// copy_backward
1492
1493template <class _InputIterator, class _OutputIterator>
1494inline _LIBCPP_INLINE_VISIBILITY
1495_OutputIterator
1496__copy_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1497{
1498 while (__first != __last)
1499 *--__result = *--__last;
1500 return __result;
1501}
1502
1503template <class _Tp, class _Up>
1504inline _LIBCPP_INLINE_VISIBILITY
1505typename enable_if
1506<
1507 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001508 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001509 _Up*
1510>::type
1511__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1512{
1513 const size_t __n = static_cast<size_t>(__last - __first);
1514 __result -= __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001515 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001516 return __result;
1517}
1518
1519template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1520inline _LIBCPP_INLINE_VISIBILITY
1521_BidirectionalIterator2
1522copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1523 _BidirectionalIterator2 __result)
1524{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001525 return _VSTD::__copy_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001526}
1527
1528// copy_if
1529
1530template<class _InputIterator, class _OutputIterator, class _Predicate>
1531inline _LIBCPP_INLINE_VISIBILITY
1532_OutputIterator
1533copy_if(_InputIterator __first, _InputIterator __last,
1534 _OutputIterator __result, _Predicate __pred)
1535{
1536 for (; __first != __last; ++__first)
1537 {
1538 if (__pred(*__first))
1539 {
1540 *__result = *__first;
1541 ++__result;
1542 }
1543 }
1544 return __result;
1545}
1546
1547// copy_n
1548
1549template<class _InputIterator, class _Size, class _OutputIterator>
1550inline _LIBCPP_INLINE_VISIBILITY
1551typename enable_if
1552<
1553 __is_input_iterator<_InputIterator>::value &&
1554 !__is_random_access_iterator<_InputIterator>::value,
1555 _OutputIterator
1556>::type
1557copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1558{
Howard Hinnant171869e2011-02-27 20:55:39 +00001559 if (__n > 0)
1560 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001561 *__result = *__first;
Howard Hinnant171869e2011-02-27 20:55:39 +00001562 ++__result;
1563 for (--__n; __n > 0; --__n)
1564 {
1565 ++__first;
1566 *__result = *__first;
1567 ++__result;
1568 }
1569 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001570 return __result;
1571}
1572
1573template<class _InputIterator, class _Size, class _OutputIterator>
1574inline _LIBCPP_INLINE_VISIBILITY
1575typename enable_if
1576<
1577 __is_random_access_iterator<_InputIterator>::value,
1578 _OutputIterator
1579>::type
1580copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1581{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001582 return _VSTD::copy(__first, __first + __n, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001583}
1584
1585// move
1586
1587template <class _InputIterator, class _OutputIterator>
1588inline _LIBCPP_INLINE_VISIBILITY
1589_OutputIterator
1590__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1591{
1592 for (; __first != __last; ++__first, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001593 *__result = _VSTD::move(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001594 return __result;
1595}
1596
1597template <class _Tp, class _Up>
1598inline _LIBCPP_INLINE_VISIBILITY
1599typename enable_if
1600<
1601 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001602 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001603 _Up*
1604>::type
1605__move(_Tp* __first, _Tp* __last, _Up* __result)
1606{
1607 const size_t __n = static_cast<size_t>(__last - __first);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001608 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001609 return __result + __n;
1610}
1611
1612template <class _InputIterator, class _OutputIterator>
1613inline _LIBCPP_INLINE_VISIBILITY
1614_OutputIterator
1615move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1616{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001617 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618}
1619
1620// move_backward
1621
1622template <class _InputIterator, class _OutputIterator>
1623inline _LIBCPP_INLINE_VISIBILITY
1624_OutputIterator
1625__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1626{
1627 while (__first != __last)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001628 *--__result = _VSTD::move(*--__last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001629 return __result;
1630}
1631
1632template <class _Tp, class _Up>
1633inline _LIBCPP_INLINE_VISIBILITY
1634typename enable_if
1635<
1636 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001637 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001638 _Up*
1639>::type
1640__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1641{
1642 const size_t __n = static_cast<size_t>(__last - __first);
1643 __result -= __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001644 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001645 return __result;
1646}
1647
1648template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1649inline _LIBCPP_INLINE_VISIBILITY
1650_BidirectionalIterator2
1651move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1652 _BidirectionalIterator2 __result)
1653{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001654 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001655}
1656
1657// iter_swap
1658
Howard Hinnante9b2c2d2011-05-27 15:04:19 +00001659// moved to <type_traits> for better swap / noexcept support
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001660
1661// transform
1662
1663template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
1664inline _LIBCPP_INLINE_VISIBILITY
1665_OutputIterator
1666transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1667{
1668 for (; __first != __last; ++__first, ++__result)
1669 *__result = __op(*__first);
1670 return __result;
1671}
1672
1673template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
1674inline _LIBCPP_INLINE_VISIBILITY
1675_OutputIterator
1676transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1677 _OutputIterator __result, _BinaryOperation __binary_op)
1678{
1679 for (; __first1 != __last1; ++__first1, ++__first2, ++__result)
1680 *__result = __binary_op(*__first1, *__first2);
1681 return __result;
1682}
1683
1684// replace
1685
1686template <class _ForwardIterator, class _Tp>
1687inline _LIBCPP_INLINE_VISIBILITY
1688void
1689replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1690{
1691 for (; __first != __last; ++__first)
1692 if (*__first == __old_value)
1693 *__first = __new_value;
1694}
1695
1696// replace_if
1697
1698template <class _ForwardIterator, class _Predicate, class _Tp>
1699inline _LIBCPP_INLINE_VISIBILITY
1700void
1701replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1702{
1703 for (; __first != __last; ++__first)
1704 if (__pred(*__first))
1705 *__first = __new_value;
1706}
1707
1708// replace_copy
1709
1710template <class _InputIterator, class _OutputIterator, class _Tp>
1711inline _LIBCPP_INLINE_VISIBILITY
1712_OutputIterator
1713replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1714 const _Tp& __old_value, const _Tp& __new_value)
1715{
1716 for (; __first != __last; ++__first, ++__result)
1717 if (*__first == __old_value)
1718 *__result = __new_value;
1719 else
1720 *__result = *__first;
1721 return __result;
1722}
1723
1724// replace_copy_if
1725
1726template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
1727inline _LIBCPP_INLINE_VISIBILITY
1728_OutputIterator
1729replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1730 _Predicate __pred, const _Tp& __new_value)
1731{
1732 for (; __first != __last; ++__first, ++__result)
1733 if (__pred(*__first))
1734 *__result = __new_value;
1735 else
1736 *__result = *__first;
1737 return __result;
1738}
1739
1740// fill_n
1741
1742template <class _OutputIterator, class _Size, class _Tp>
1743inline _LIBCPP_INLINE_VISIBILITY
1744_OutputIterator
1745__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value, false_type)
1746{
1747 for (; __n > 0; ++__first, --__n)
1748 *__first = __value;
1749 return __first;
1750}
1751
1752template <class _OutputIterator, class _Size, class _Tp>
1753inline _LIBCPP_INLINE_VISIBILITY
1754_OutputIterator
1755__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value, true_type)
1756{
1757 if (__n > 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001758 _VSTD::memset(__first, (unsigned char)__value, (size_t)(__n));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001759 return __first + __n;
1760}
1761
1762template <class _OutputIterator, class _Size, class _Tp>
1763inline _LIBCPP_INLINE_VISIBILITY
1764_OutputIterator
1765fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
1766{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001767 return _VSTD::__fill_n(__first, __n, __value, integral_constant<bool,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001768 is_pointer<_OutputIterator>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001769 is_trivially_copy_assignable<_Tp>::value &&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001770 sizeof(_Tp) == 1>());
1771}
1772
1773// fill
1774
1775template <class _ForwardIterator, class _Tp>
1776inline _LIBCPP_INLINE_VISIBILITY
1777void
1778__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, forward_iterator_tag)
1779{
1780 for (; __first != __last; ++__first)
1781 *__first = __value;
1782}
1783
1784template <class _RandomAccessIterator, class _Tp>
1785inline _LIBCPP_INLINE_VISIBILITY
1786void
1787__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value, random_access_iterator_tag)
1788{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001789 _VSTD::fill_n(__first, __last - __first, __value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001790}
1791
1792template <class _ForwardIterator, class _Tp>
1793inline _LIBCPP_INLINE_VISIBILITY
1794void
1795fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
1796{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001797 _VSTD::__fill(__first, __last, __value, typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001798}
1799
1800// generate
1801
1802template <class _ForwardIterator, class _Generator>
1803inline _LIBCPP_INLINE_VISIBILITY
1804void
1805generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
1806{
1807 for (; __first != __last; ++__first)
1808 *__first = __gen();
1809}
1810
1811// generate_n
1812
1813template <class _OutputIterator, class _Size, class _Generator>
1814inline _LIBCPP_INLINE_VISIBILITY
1815_OutputIterator
1816generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
1817{
1818 for (; __n > 0; ++__first, --__n)
1819 *__first = __gen();
1820 return __first;
1821}
1822
1823// remove
1824
1825template <class _ForwardIterator, class _Tp>
1826_ForwardIterator
1827remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
1828{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001829 __first = _VSTD::find(__first, __last, __value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001830 if (__first != __last)
1831 {
1832 _ForwardIterator __i = __first;
1833 while (++__i != __last)
1834 {
1835 if (!(*__i == __value))
1836 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001837 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001838 ++__first;
1839 }
1840 }
1841 }
1842 return __first;
1843}
1844
1845// remove_if
1846
1847template <class _ForwardIterator, class _Predicate>
1848_ForwardIterator
1849remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
1850{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001851 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001852 (__first, __last, __pred);
1853 if (__first != __last)
1854 {
1855 _ForwardIterator __i = __first;
1856 while (++__i != __last)
1857 {
1858 if (!__pred(*__i))
1859 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001860 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001861 ++__first;
1862 }
1863 }
1864 }
1865 return __first;
1866}
1867
1868// remove_copy
1869
1870template <class _InputIterator, class _OutputIterator, class _Tp>
1871inline _LIBCPP_INLINE_VISIBILITY
1872_OutputIterator
1873remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value)
1874{
1875 for (; __first != __last; ++__first)
1876 {
1877 if (!(*__first == __value))
1878 {
1879 *__result = *__first;
1880 ++__result;
1881 }
1882 }
1883 return __result;
1884}
1885
1886// remove_copy_if
1887
1888template <class _InputIterator, class _OutputIterator, class _Predicate>
1889inline _LIBCPP_INLINE_VISIBILITY
1890_OutputIterator
1891remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
1892{
1893 for (; __first != __last; ++__first)
1894 {
1895 if (!__pred(*__first))
1896 {
1897 *__result = *__first;
1898 ++__result;
1899 }
1900 }
1901 return __result;
1902}
1903
1904// unique
1905
1906template <class _ForwardIterator, class _BinaryPredicate>
1907_ForwardIterator
1908unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1909{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001910 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001911 (__first, __last, __pred);
1912 if (__first != __last)
1913 {
1914 // ... a a ? ...
1915 // f i
1916 _ForwardIterator __i = __first;
1917 for (++__i; ++__i != __last;)
1918 if (!__pred(*__first, *__i))
Howard Hinnant0949eed2011-06-30 21:18:19 +00001919 *++__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001920 ++__first;
1921 }
1922 return __first;
1923}
1924
1925template <class _ForwardIterator>
1926inline _LIBCPP_INLINE_VISIBILITY
1927_ForwardIterator
1928unique(_ForwardIterator __first, _ForwardIterator __last)
1929{
1930 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001931 return _VSTD::unique(__first, __last, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001932}
1933
1934// unique_copy
1935
1936template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
1937_OutputIterator
1938__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
1939 input_iterator_tag, output_iterator_tag)
1940{
1941 if (__first != __last)
1942 {
1943 typename iterator_traits<_InputIterator>::value_type __t(*__first);
1944 *__result = __t;
1945 ++__result;
1946 while (++__first != __last)
1947 {
1948 if (!__pred(__t, *__first))
1949 {
1950 __t = *__first;
1951 *__result = __t;
1952 ++__result;
1953 }
1954 }
1955 }
1956 return __result;
1957}
1958
1959template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
1960_OutputIterator
1961__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
1962 forward_iterator_tag, output_iterator_tag)
1963{
1964 if (__first != __last)
1965 {
1966 _ForwardIterator __i = __first;
1967 *__result = *__i;
1968 ++__result;
1969 while (++__first != __last)
1970 {
1971 if (!__pred(*__i, *__first))
1972 {
1973 *__result = *__first;
1974 ++__result;
1975 __i = __first;
1976 }
1977 }
1978 }
1979 return __result;
1980}
1981
1982template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
1983_ForwardIterator
1984__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
1985 input_iterator_tag, forward_iterator_tag)
1986{
1987 if (__first != __last)
1988 {
1989 *__result = *__first;
1990 while (++__first != __last)
1991 if (!__pred(*__result, *__first))
1992 *++__result = *__first;
1993 ++__result;
1994 }
1995 return __result;
1996}
1997
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001998template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
1999inline _LIBCPP_INLINE_VISIBILITY
2000_OutputIterator
2001unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2002{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002003 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002004 (__first, __last, __result, __pred,
2005 typename iterator_traits<_InputIterator>::iterator_category(),
2006 typename iterator_traits<_OutputIterator>::iterator_category());
2007}
2008
2009template <class _InputIterator, class _OutputIterator>
2010inline _LIBCPP_INLINE_VISIBILITY
2011_OutputIterator
2012unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2013{
2014 typedef typename iterator_traits<_InputIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002015 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002016}
2017
2018// reverse
2019
2020template <class _BidirectionalIterator>
2021inline _LIBCPP_INLINE_VISIBILITY
2022void
2023__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2024{
2025 while (__first != __last)
2026 {
2027 if (__first == --__last)
2028 break;
2029 swap(*__first, *__last);
2030 ++__first;
2031 }
2032}
2033
2034template <class _RandomAccessIterator>
2035inline _LIBCPP_INLINE_VISIBILITY
2036void
2037__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2038{
2039 if (__first != __last)
2040 for (; __first < --__last; ++__first)
2041 swap(*__first, *__last);
2042}
2043
2044template <class _BidirectionalIterator>
2045inline _LIBCPP_INLINE_VISIBILITY
2046void
2047reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2048{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002049 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002050}
2051
2052// reverse_copy
2053
2054template <class _BidirectionalIterator, class _OutputIterator>
2055inline _LIBCPP_INLINE_VISIBILITY
2056_OutputIterator
2057reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2058{
2059 for (; __first != __last; ++__result)
2060 *__result = *--__last;
2061 return __result;
2062}
2063
2064// rotate
2065
2066template <class _ForwardIterator>
2067_ForwardIterator
2068__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, false_type)
2069{
2070 if (__first == __middle)
2071 return __last;
2072 if (__middle == __last)
2073 return __first;
2074 _ForwardIterator __i = __middle;
2075 while (true)
2076 {
2077 swap(*__first, *__i);
2078 ++__first;
2079 if (++__i == __last)
2080 break;
2081 if (__first == __middle)
2082 __middle = __i;
2083 }
2084 _ForwardIterator __r = __first;
2085 if (__first != __middle)
2086 {
2087 __i = __middle;
2088 while (true)
2089 {
2090 swap(*__first, *__i);
2091 ++__first;
2092 if (++__i == __last)
2093 {
2094 if (__first == __middle)
2095 break;
2096 __i = __middle;
2097 }
2098 else if (__first == __middle)
2099 __middle = __i;
2100 }
2101 }
2102 return __r;
2103}
2104
2105template<typename _Integral>
2106inline _LIBCPP_INLINE_VISIBILITY
2107_Integral
2108__gcd(_Integral __x, _Integral __y)
2109{
2110 do
2111 {
2112 _Integral __t = __x % __y;
2113 __x = __y;
2114 __y = __t;
2115 } while (__y);
2116 return __x;
2117}
2118
2119template<typename _RandomAccessIterator>
2120_RandomAccessIterator
2121__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last, true_type)
2122{
2123 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2124 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant324bb032010-08-22 00:02:43 +00002125
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002126 if (__first == __middle)
2127 return __last;
2128 if (__middle == __last)
2129 return __first;
2130 const difference_type __m1 = __middle - __first;
2131 const difference_type __m2 = __last - __middle;
2132 if (__m1 == __m2)
2133 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002134 _VSTD::swap_ranges(__first, __middle, __middle);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002135 return __middle;
2136 }
2137 const difference_type __g = __gcd(__m1, __m2);
2138 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2139 {
2140 value_type __t(*--__p);
2141 _RandomAccessIterator __p1 = __p;
2142 _RandomAccessIterator __p2 = __p1 + __m1;
2143 do
2144 {
2145 *__p1 = *__p2;
2146 __p1 = __p2;
2147 const difference_type __d = __last - __p2;
2148 if (__m1 < __d)
2149 __p2 += __m1;
2150 else
2151 __p2 = __first + (__m1 - __d);
2152 } while (__p2 != __p);
2153 *__p1 = __t;
2154 }
2155 return __first + __m2;
2156}
2157
2158template <class _ForwardIterator>
2159inline _LIBCPP_INLINE_VISIBILITY
2160_ForwardIterator
2161rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2162{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002163 return _VSTD::__rotate(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002164 integral_constant
2165 <
2166 bool,
2167 is_convertible
2168 <
2169 typename iterator_traits<_ForwardIterator>::iterator_category,
2170 random_access_iterator_tag
2171 >::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00002172 is_trivially_copy_assignable
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002173 <
2174 typename iterator_traits<_ForwardIterator>::value_type
2175 >::value
2176 >());
2177}
2178
2179// rotate_copy
2180
2181template <class _ForwardIterator, class _OutputIterator>
2182inline _LIBCPP_INLINE_VISIBILITY
2183_OutputIterator
2184rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2185{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002186 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002187}
2188
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002189// min_element
2190
2191template <class _ForwardIterator, class _Compare>
2192inline _LIBCPP_INLINE_VISIBILITY
2193_ForwardIterator
2194min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2195{
2196 if (__first != __last)
2197 {
2198 _ForwardIterator __i = __first;
2199 while (++__i != __last)
2200 if (__comp(*__i, *__first))
2201 __first = __i;
2202 }
2203 return __first;
2204}
2205
2206template <class _ForwardIterator>
2207inline _LIBCPP_INLINE_VISIBILITY
2208_ForwardIterator
2209min_element(_ForwardIterator __first, _ForwardIterator __last)
2210{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002211 return _VSTD::min_element(__first, __last,
Howard Hinnant98e5d972010-08-21 20:10:01 +00002212 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2213}
2214
2215// min
2216
2217template <class _Tp, class _Compare>
2218inline _LIBCPP_INLINE_VISIBILITY
2219const _Tp&
2220min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2221{
2222 return __comp(__b, __a) ? __b : __a;
2223}
2224
2225template <class _Tp>
2226inline _LIBCPP_INLINE_VISIBILITY
2227const _Tp&
2228min(const _Tp& __a, const _Tp& __b)
2229{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002230 return _VSTD::min(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002231}
2232
Howard Hinnante3e32912011-08-12 21:56:02 +00002233#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2234
Howard Hinnant98e5d972010-08-21 20:10:01 +00002235template<class _Tp, class _Compare>
2236inline _LIBCPP_INLINE_VISIBILITY
2237_Tp
2238min(initializer_list<_Tp> __t, _Compare __comp)
2239{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002240 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002241}
2242
2243template<class _Tp>
2244inline _LIBCPP_INLINE_VISIBILITY
2245_Tp
2246min(initializer_list<_Tp> __t)
2247{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002248 return *_VSTD::min_element(__t.begin(), __t.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002249}
2250
Howard Hinnante3e32912011-08-12 21:56:02 +00002251#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2252
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002253// max_element
2254
2255template <class _ForwardIterator, class _Compare>
2256inline _LIBCPP_INLINE_VISIBILITY
2257_ForwardIterator
2258max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2259{
2260 if (__first != __last)
2261 {
2262 _ForwardIterator __i = __first;
2263 while (++__i != __last)
2264 if (__comp(*__first, *__i))
2265 __first = __i;
2266 }
2267 return __first;
2268}
2269
2270template <class _ForwardIterator>
2271inline _LIBCPP_INLINE_VISIBILITY
2272_ForwardIterator
2273max_element(_ForwardIterator __first, _ForwardIterator __last)
2274{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002275 return _VSTD::max_element(__first, __last,
Howard Hinnant98e5d972010-08-21 20:10:01 +00002276 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2277}
2278
2279// max
2280
2281template <class _Tp, class _Compare>
2282inline _LIBCPP_INLINE_VISIBILITY
2283const _Tp&
2284max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2285{
2286 return __comp(__a, __b) ? __b : __a;
2287}
2288
2289template <class _Tp>
2290inline _LIBCPP_INLINE_VISIBILITY
2291const _Tp&
2292max(const _Tp& __a, const _Tp& __b)
2293{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002294 return _VSTD::max(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002295}
2296
Howard Hinnante3e32912011-08-12 21:56:02 +00002297#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2298
Howard Hinnant98e5d972010-08-21 20:10:01 +00002299template<class _Tp, class _Compare>
2300inline _LIBCPP_INLINE_VISIBILITY
2301_Tp
2302max(initializer_list<_Tp> __t, _Compare __comp)
2303{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002304 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002305}
2306
2307template<class _Tp>
2308inline _LIBCPP_INLINE_VISIBILITY
2309_Tp
2310max(initializer_list<_Tp> __t)
2311{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002312 return *_VSTD::max_element(__t.begin(), __t.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002313}
2314
Howard Hinnante3e32912011-08-12 21:56:02 +00002315#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2316
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002317// minmax_element
2318
2319template <class _ForwardIterator, class _Compare>
2320std::pair<_ForwardIterator, _ForwardIterator>
2321minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2322{
2323 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2324 if (__first != __last)
2325 {
2326 if (++__first != __last)
2327 {
2328 if (__comp(*__first, *__result.first))
2329 {
2330 __result.second = __result.first;
2331 __result.first = __first;
2332 }
2333 else
2334 __result.second = __first;
2335 while (++__first != __last)
2336 {
2337 _ForwardIterator __i = __first;
2338 if (++__first == __last)
2339 {
2340 if (__comp(*__i, *__result.first))
2341 __result.first = __i;
2342 else if (!__comp(*__i, *__result.second))
2343 __result.second = __i;
2344 break;
2345 }
2346 else
2347 {
2348 if (__comp(*__first, *__i))
2349 {
2350 if (__comp(*__first, *__result.first))
2351 __result.first = __first;
2352 if (!__comp(*__i, *__result.second))
2353 __result.second = __i;
2354 }
2355 else
2356 {
2357 if (__comp(*__i, *__result.first))
2358 __result.first = __i;
2359 if (!__comp(*__first, *__result.second))
2360 __result.second = __first;
2361 }
2362 }
2363 }
2364 }
2365 }
2366 return __result;
2367}
2368
2369template <class _ForwardIterator>
2370inline _LIBCPP_INLINE_VISIBILITY
2371std::pair<_ForwardIterator, _ForwardIterator>
2372minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2373{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002374 return _VSTD::minmax_element(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002375}
2376
Howard Hinnant98e5d972010-08-21 20:10:01 +00002377// minmax
2378
2379template<class _Tp, class _Compare>
2380inline _LIBCPP_INLINE_VISIBILITY
2381pair<const _Tp&, const _Tp&>
2382minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2383{
2384 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2385 pair<const _Tp&, const _Tp&>(__a, __b);
2386}
2387
2388template<class _Tp>
2389inline _LIBCPP_INLINE_VISIBILITY
2390pair<const _Tp&, const _Tp&>
2391minmax(const _Tp& __a, const _Tp& __b)
2392{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002393 return _VSTD::minmax(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002394}
2395
Howard Hinnante3e32912011-08-12 21:56:02 +00002396#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2397
Howard Hinnant98e5d972010-08-21 20:10:01 +00002398template<class _Tp>
2399inline _LIBCPP_INLINE_VISIBILITY
2400pair<_Tp, _Tp>
2401minmax(initializer_list<_Tp> __t)
2402{
2403 pair<const _Tp*, const _Tp*> __p =
Howard Hinnant0949eed2011-06-30 21:18:19 +00002404 _VSTD::minmax_element(__t.begin(), __t.end());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002405 return pair<_Tp, _Tp>(*__p.first, *__p.second);
2406}
2407
2408template<class _Tp, class _Compare>
2409inline _LIBCPP_INLINE_VISIBILITY
2410pair<_Tp, _Tp>
2411minmax(initializer_list<_Tp> __t, _Compare __comp)
2412{
2413 pair<const _Tp*, const _Tp*> __p =
Howard Hinnant0949eed2011-06-30 21:18:19 +00002414 _VSTD::minmax_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002415 return pair<_Tp, _Tp>(*__p.first, *__p.second);
2416}
2417
Howard Hinnante3e32912011-08-12 21:56:02 +00002418#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2419
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002420// random_shuffle
2421
Howard Hinnantc3267212010-05-26 17:49:34 +00002422// __independent_bits_engine
2423
2424template <unsigned long long _X, size_t _R>
2425struct __log2_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002426{
Howard Hinnantc3267212010-05-26 17:49:34 +00002427 static const size_t value = _X & ((unsigned long long)(1) << _R) ? _R
2428 : __log2_imp<_X, _R - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002429};
2430
Howard Hinnantc3267212010-05-26 17:49:34 +00002431template <unsigned long long _X>
2432struct __log2_imp<_X, 0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002433{
Howard Hinnantc3267212010-05-26 17:49:34 +00002434 static const size_t value = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002435};
2436
Howard Hinnantc3267212010-05-26 17:49:34 +00002437template <size_t _R>
2438struct __log2_imp<0, _R>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002439{
Howard Hinnantc3267212010-05-26 17:49:34 +00002440 static const size_t value = _R + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002441};
2442
Howard Hinnantc3267212010-05-26 17:49:34 +00002443template <class _UI, _UI _X>
2444struct __log2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002445{
Howard Hinnantc3267212010-05-26 17:49:34 +00002446 static const size_t value = __log2_imp<_X,
2447 sizeof(_UI) * __CHAR_BIT__ - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002448};
2449
Howard Hinnantc3267212010-05-26 17:49:34 +00002450template<class _Engine, class _UIntType>
2451class __independent_bits_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002452{
Howard Hinnantc3267212010-05-26 17:49:34 +00002453public:
2454 // types
2455 typedef _UIntType result_type;
2456
2457private:
2458 typedef typename _Engine::result_type _Engine_result_type;
2459 typedef typename conditional
2460 <
2461 sizeof(_Engine_result_type) <= sizeof(result_type),
2462 result_type,
2463 _Engine_result_type
2464 >::type _Working_result_type;
2465
2466 _Engine& __e_;
2467 size_t __w_;
2468 size_t __w0_;
2469 size_t __n_;
2470 size_t __n0_;
2471 _Working_result_type __y0_;
2472 _Working_result_type __y1_;
2473 _Engine_result_type __mask0_;
2474 _Engine_result_type __mask1_;
2475
2476 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
2477 + _Working_result_type(1);
2478 static const size_t __m = __log2<_Working_result_type, _R>::value;
2479 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2480 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2481
2482public:
2483 // constructors and seeding functions
2484 __independent_bits_engine(_Engine& __e, size_t __w);
2485
2486 // generating functions
2487 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2488
2489private:
2490 result_type __eval(false_type);
2491 result_type __eval(true_type);
2492};
2493
2494template<class _Engine, class _UIntType>
2495__independent_bits_engine<_Engine, _UIntType>
2496 ::__independent_bits_engine(_Engine& __e, size_t __w)
2497 : __e_(__e),
2498 __w_(__w)
2499{
2500 __n_ = __w_ / __m + (__w_ % __m != 0);
2501 __w0_ = __w_ / __n_;
2502 if (_R == 0)
2503 __y0_ = _R;
2504 else if (__w0_ < _WDt)
2505 __y0_ = (_R >> __w0_) << __w0_;
2506 else
2507 __y0_ = 0;
2508 if (_R - __y0_ > __y0_ / __n_)
2509 {
2510 ++__n_;
2511 __w0_ = __w_ / __n_;
2512 if (__w0_ < _WDt)
2513 __y0_ = (_R >> __w0_) << __w0_;
2514 else
2515 __y0_ = 0;
2516 }
2517 __n0_ = __n_ - __w_ % __n_;
2518 if (__w0_ < _WDt - 1)
2519 __y1_ = (_R >> (__w0_ + 1)) << (__w0_ + 1);
2520 else
2521 __y1_ = 0;
2522 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2523 _Engine_result_type(0);
2524 __mask1_ = __w0_ < _EDt - 1 ?
2525 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2526 _Engine_result_type(~0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002527}
2528
Howard Hinnantc3267212010-05-26 17:49:34 +00002529template<class _Engine, class _UIntType>
2530inline
2531_UIntType
2532__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002533{
Howard Hinnantc3267212010-05-26 17:49:34 +00002534 return static_cast<result_type>(__e_() & __mask0_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002535}
2536
Howard Hinnantc3267212010-05-26 17:49:34 +00002537template<class _Engine, class _UIntType>
2538_UIntType
2539__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002540{
Howard Hinnantc3267212010-05-26 17:49:34 +00002541 result_type _S = 0;
2542 for (size_t __k = 0; __k < __n0_; ++__k)
2543 {
2544 _Engine_result_type __u;
2545 do
2546 {
2547 __u = __e_() - _Engine::min();
2548 } while (__u >= __y0_);
2549 if (__w0_ < _EDt)
2550 _S <<= __w0_;
2551 else
2552 _S = 0;
2553 _S += __u & __mask0_;
2554 }
2555 for (size_t __k = __n0_; __k < __n_; ++__k)
2556 {
2557 _Engine_result_type __u;
2558 do
2559 {
2560 __u = __e_() - _Engine::min();
2561 } while (__u >= __y1_);
2562 if (__w0_ < _EDt - 1)
2563 _S <<= __w0_ + 1;
2564 else
2565 _S = 0;
2566 _S += __u & __mask1_;
2567 }
2568 return _S;
2569}
2570
2571// uniform_int_distribution
2572
2573template<class _IntType = int>
2574class uniform_int_distribution
2575{
2576public:
2577 // types
2578 typedef _IntType result_type;
2579
2580 class param_type
2581 {
2582 result_type __a_;
2583 result_type __b_;
2584 public:
2585 typedef uniform_int_distribution distribution_type;
2586
2587 explicit param_type(result_type __a = 0,
2588 result_type __b = numeric_limits<result_type>::max())
2589 : __a_(__a), __b_(__b) {}
2590
2591 result_type a() const {return __a_;}
2592 result_type b() const {return __b_;}
2593
2594 friend bool operator==(const param_type& __x, const param_type& __y)
2595 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2596 friend bool operator!=(const param_type& __x, const param_type& __y)
2597 {return !(__x == __y);}
2598 };
2599
2600private:
2601 param_type __p_;
2602
2603public:
2604 // constructors and reset functions
2605 explicit uniform_int_distribution(result_type __a = 0,
2606 result_type __b = numeric_limits<result_type>::max())
2607 : __p_(param_type(__a, __b)) {}
2608 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
2609 void reset() {}
2610
2611 // generating functions
2612 template<class _URNG> result_type operator()(_URNG& __g)
2613 {return (*this)(__g, __p_);}
2614 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2615
2616 // property functions
2617 result_type a() const {return __p_.a();}
2618 result_type b() const {return __p_.b();}
2619
2620 param_type param() const {return __p_;}
2621 void param(const param_type& __p) {__p_ = __p;}
2622
2623 result_type min() const {return a();}
2624 result_type max() const {return b();}
2625
2626 friend bool operator==(const uniform_int_distribution& __x,
2627 const uniform_int_distribution& __y)
2628 {return __x.__p_ == __y.__p_;}
2629 friend bool operator!=(const uniform_int_distribution& __x,
2630 const uniform_int_distribution& __y)
2631 {return !(__x == __y);}
2632};
2633
2634template<class _IntType>
2635template<class _URNG>
2636typename uniform_int_distribution<_IntType>::result_type
2637uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
2638{
2639 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
2640 uint32_t, uint64_t>::type _UIntType;
2641 const _UIntType _R = __p.b() - __p.a() + _UIntType(1);
2642 if (_R == 1)
2643 return __p.a();
2644 const size_t _Dt = numeric_limits<_UIntType>::digits;
2645 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
2646 if (_R == 0)
2647 return static_cast<result_type>(_Eng(__g, _Dt)());
2648 size_t __w = _Dt - __clz(_R) - 1;
2649 if ((_R & (_UIntType(~0) >> (_Dt - __w))) != 0)
2650 ++__w;
2651 _Eng __e(__g, __w);
2652 _UIntType __u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002653 do
Howard Hinnantc3267212010-05-26 17:49:34 +00002654 {
2655 __u = __e();
2656 } while (__u >= _R);
2657 return static_cast<result_type>(__u + __p.a());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002658}
2659
Howard Hinnantc3267212010-05-26 17:49:34 +00002660class __rs_default;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002661
Howard Hinnantc3267212010-05-26 17:49:34 +00002662__rs_default __rs_get();
2663
2664class __rs_default
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002665{
Howard Hinnantc3267212010-05-26 17:49:34 +00002666 static unsigned __c_;
2667
2668 __rs_default();
2669public:
2670 typedef unsigned result_type;
2671
2672 static const result_type _Min = 0;
2673 static const result_type _Max = 0xFFFFFFFF;
2674
2675 __rs_default(const __rs_default&);
2676 ~__rs_default();
2677
2678 result_type operator()();
2679
2680 static const/*expr*/ result_type min() {return _Min;}
2681 static const/*expr*/ result_type max() {return _Max;}
2682
2683 friend __rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002684};
2685
Howard Hinnantc3267212010-05-26 17:49:34 +00002686__rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002687
2688template <class _RandomAccessIterator>
2689void
2690random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
2691{
2692 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc3267212010-05-26 17:49:34 +00002693 typedef uniform_int_distribution<ptrdiff_t> _D;
2694 typedef typename _D::param_type _P;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002695 difference_type __d = __last - __first;
2696 if (__d > 1)
2697 {
Howard Hinnantc3267212010-05-26 17:49:34 +00002698 _D __uid;
2699 __rs_default __g = __rs_get();
2700 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00002701 {
2702 difference_type __i = __uid(__g, _P(0, __d));
2703 if (__i != difference_type(0))
2704 swap(*__first, *(__first + __i));
2705 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002706 }
2707}
2708
2709template <class _RandomAccessIterator, class _RandomNumberGenerator>
2710void
2711random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnant73d21a42010-09-04 23:28:19 +00002712#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002713 _RandomNumberGenerator&& __rand)
2714#else
2715 _RandomNumberGenerator& __rand)
2716#endif
2717{
2718 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2719 difference_type __d = __last - __first;
2720 if (__d > 1)
2721 {
2722 for (--__last; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00002723 {
2724 difference_type __i = __rand(__d);
2725 swap(*__first, *(__first + __i));
2726 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002727 }
2728}
2729
Howard Hinnantc3267212010-05-26 17:49:34 +00002730template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
2731 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnant278bf2d2010-11-18 01:47:02 +00002732#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2733 _UniformRandomNumberGenerator&& __g)
2734#else
Howard Hinnantc3267212010-05-26 17:49:34 +00002735 _UniformRandomNumberGenerator& __g)
Howard Hinnant278bf2d2010-11-18 01:47:02 +00002736#endif
Howard Hinnantc3267212010-05-26 17:49:34 +00002737{
2738 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2739 typedef uniform_int_distribution<ptrdiff_t> _D;
2740 typedef typename _D::param_type _P;
2741 difference_type __d = __last - __first;
2742 if (__d > 1)
2743 {
2744 _D __uid;
2745 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00002746 {
2747 difference_type __i = __uid(__g, _P(0, __d));
2748 if (__i != difference_type(0))
2749 swap(*__first, *(__first + __i));
2750 }
Howard Hinnantc3267212010-05-26 17:49:34 +00002751 }
2752}
2753
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002754template <class _InputIterator, class _Predicate>
2755bool
2756is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
2757{
2758 for (; __first != __last; ++__first)
2759 if (!__pred(*__first))
2760 break;
2761 for (; __first != __last; ++__first)
2762 if (__pred(*__first))
2763 return false;
2764 return true;
2765}
2766
2767// partition
2768
2769template <class _Predicate, class _ForwardIterator>
2770_ForwardIterator
2771__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
2772{
2773 while (true)
2774 {
2775 if (__first == __last)
2776 return __first;
2777 if (!__pred(*__first))
2778 break;
2779 ++__first;
2780 }
2781 for (_ForwardIterator __p = __first; ++__p != __last;)
2782 {
2783 if (__pred(*__p))
2784 {
2785 swap(*__first, *__p);
2786 ++__first;
2787 }
2788 }
2789 return __first;
2790}
2791
2792template <class _Predicate, class _BidirectionalIterator>
2793_BidirectionalIterator
2794__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
2795 bidirectional_iterator_tag)
2796{
2797 while (true)
2798 {
2799 while (true)
2800 {
2801 if (__first == __last)
2802 return __first;
2803 if (!__pred(*__first))
2804 break;
2805 ++__first;
2806 }
2807 do
2808 {
2809 if (__first == --__last)
2810 return __first;
2811 } while (!__pred(*__last));
2812 swap(*__first, *__last);
2813 ++__first;
2814 }
2815}
2816
2817template <class _ForwardIterator, class _Predicate>
2818inline _LIBCPP_INLINE_VISIBILITY
2819_ForwardIterator
2820partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2821{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002822 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002823 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
2824}
2825
2826// partition_copy
2827
2828template <class _InputIterator, class _OutputIterator1,
2829 class _OutputIterator2, class _Predicate>
2830pair<_OutputIterator1, _OutputIterator2>
2831partition_copy(_InputIterator __first, _InputIterator __last,
2832 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
2833 _Predicate __pred)
2834{
2835 for (; __first != __last; ++__first)
2836 {
2837 if (__pred(*__first))
2838 {
2839 *__out_true = *__first;
2840 ++__out_true;
2841 }
2842 else
2843 {
2844 *__out_false = *__first;
2845 ++__out_false;
2846 }
2847 }
2848 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
2849}
2850
2851// partition_point
2852
2853template<class _ForwardIterator, class _Predicate>
2854_ForwardIterator
2855partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2856{
2857 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002858 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002859 while (__len != 0)
2860 {
2861 difference_type __l2 = __len / 2;
2862 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002863 _VSTD::advance(__m, __l2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002864 if (__pred(*__m))
2865 {
2866 __first = ++__m;
2867 __len -= __l2 + 1;
2868 }
2869 else
2870 __len = __l2;
2871 }
2872 return __first;
2873}
2874
2875// stable_partition
2876
2877template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
2878_ForwardIterator
2879__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
2880 _Distance __len, _Pair __p, forward_iterator_tag __fit)
2881{
2882 // *__first is known to be false
2883 // __len >= 1
2884 if (__len == 1)
2885 return __first;
2886 if (__len == 2)
2887 {
2888 _ForwardIterator __m = __first;
2889 if (__pred(*++__m))
2890 {
2891 swap(*__first, *__m);
2892 return __m;
2893 }
2894 return __first;
2895 }
2896 if (__len <= __p.second)
2897 { // The buffer is big enough to use
2898 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2899 __destruct_n __d(0);
2900 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
2901 // Move the falses into the temporary buffer, and the trues to the front of the line
2902 // Update __first to always point to the end of the trues
2903 value_type* __t = __p.first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002904 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002905 __d.__incr((value_type*)0);
2906 ++__t;
2907 _ForwardIterator __i = __first;
2908 while (++__i != __last)
2909 {
2910 if (__pred(*__i))
2911 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002912 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002913 ++__first;
2914 }
2915 else
2916 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002917 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002918 __d.__incr((value_type*)0);
2919 ++__t;
2920 }
2921 }
2922 // All trues now at start of range, all falses in buffer
2923 // Move falses back into range, but don't mess up __first which points to first false
2924 __i = __first;
2925 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002926 *__i = _VSTD::move(*__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002927 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
2928 return __first;
2929 }
2930 // Else not enough buffer, do in place
2931 // __len >= 3
2932 _ForwardIterator __m = __first;
2933 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00002934 _VSTD::advance(__m, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002935 // recurse on [__first, __m), *__first know to be false
2936 // F?????????????????
2937 // f m l
2938 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
2939 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
2940 // TTTFFFFF??????????
2941 // f ff m l
2942 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
2943 _ForwardIterator __m1 = __m;
2944 _ForwardIterator __second_false = __last;
2945 _Distance __len_half = __len - __len2;
2946 while (__pred(*__m1))
2947 {
2948 if (++__m1 == __last)
2949 goto __second_half_done;
2950 --__len_half;
2951 }
2952 // TTTFFFFFTTTF??????
2953 // f ff m m1 l
2954 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
2955__second_half_done:
2956 // TTTFFFFFTTTTTFFFFF
2957 // f ff m sf l
Howard Hinnant0949eed2011-06-30 21:18:19 +00002958 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002959 // TTTTTTTTFFFFFFFFFF
2960 // |
2961}
2962
2963struct __return_temporary_buffer
2964{
2965 template <class _Tp>
Howard Hinnant0949eed2011-06-30 21:18:19 +00002966 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002967};
2968
2969template <class _Predicate, class _ForwardIterator>
2970_ForwardIterator
2971__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
2972 forward_iterator_tag)
2973{
2974 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
2975 // Either prove all true and return __first or point to first false
2976 while (true)
2977 {
2978 if (__first == __last)
2979 return __first;
2980 if (!__pred(*__first))
2981 break;
2982 ++__first;
2983 }
2984 // We now have a reduced range [__first, __last)
2985 // *__first is known to be false
2986 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
2987 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002988 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002989 pair<value_type*, ptrdiff_t> __p(0, 0);
2990 unique_ptr<value_type, __return_temporary_buffer> __h;
2991 if (__len >= __alloc_limit)
2992 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002993 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002994 __h.reset(__p.first);
2995 }
2996 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
2997 (__first, __last, __pred, __len, __p, forward_iterator_tag());
2998}
2999
3000template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3001_BidirectionalIterator
3002__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3003 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3004{
3005 // *__first is known to be false
3006 // *__last is known to be true
3007 // __len >= 2
3008 if (__len == 2)
3009 {
3010 swap(*__first, *__last);
3011 return __last;
3012 }
3013 if (__len == 3)
3014 {
3015 _BidirectionalIterator __m = __first;
3016 if (__pred(*++__m))
3017 {
3018 swap(*__first, *__m);
3019 swap(*__m, *__last);
3020 return __last;
3021 }
3022 swap(*__m, *__last);
3023 swap(*__first, *__m);
3024 return __m;
3025 }
3026 if (__len <= __p.second)
3027 { // The buffer is big enough to use
3028 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3029 __destruct_n __d(0);
3030 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3031 // Move the falses into the temporary buffer, and the trues to the front of the line
3032 // Update __first to always point to the end of the trues
3033 value_type* __t = __p.first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003034 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003035 __d.__incr((value_type*)0);
3036 ++__t;
3037 _BidirectionalIterator __i = __first;
3038 while (++__i != __last)
3039 {
3040 if (__pred(*__i))
3041 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003042 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003043 ++__first;
3044 }
3045 else
3046 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003047 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003048 __d.__incr((value_type*)0);
3049 ++__t;
3050 }
3051 }
3052 // move *__last, known to be true
Howard Hinnant0949eed2011-06-30 21:18:19 +00003053 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003054 __i = ++__first;
3055 // All trues now at start of range, all falses in buffer
3056 // Move falses back into range, but don't mess up __first which points to first false
3057 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003058 *__i = _VSTD::move(*__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003059 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3060 return __first;
3061 }
3062 // Else not enough buffer, do in place
3063 // __len >= 4
3064 _BidirectionalIterator __m = __first;
3065 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003066 _VSTD::advance(__m, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003067 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3068 // F????????????????T
3069 // f m l
3070 _BidirectionalIterator __m1 = __m;
3071 _BidirectionalIterator __first_false = __first;
3072 _Distance __len_half = __len2;
3073 while (!__pred(*--__m1))
3074 {
3075 if (__m1 == __first)
3076 goto __first_half_done;
3077 --__len_half;
3078 }
3079 // F???TFFF?????????T
3080 // f m1 m l
3081 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3082 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3083__first_half_done:
3084 // TTTFFFFF?????????T
3085 // f ff m l
3086 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3087 __m1 = __m;
3088 _BidirectionalIterator __second_false = __last;
3089 ++__second_false;
3090 __len_half = __len - __len2;
3091 while (__pred(*__m1))
3092 {
3093 if (++__m1 == __last)
3094 goto __second_half_done;
3095 --__len_half;
3096 }
3097 // TTTFFFFFTTTF?????T
3098 // f ff m m1 l
3099 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3100__second_half_done:
3101 // TTTFFFFFTTTTTFFFFF
3102 // f ff m sf l
Howard Hinnant0949eed2011-06-30 21:18:19 +00003103 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003104 // TTTTTTTTFFFFFFFFFF
3105 // |
3106}
3107
3108template <class _Predicate, class _BidirectionalIterator>
3109_BidirectionalIterator
3110__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3111 bidirectional_iterator_tag)
3112{
3113 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3114 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3115 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3116 // Either prove all true and return __first or point to first false
3117 while (true)
3118 {
3119 if (__first == __last)
3120 return __first;
3121 if (!__pred(*__first))
3122 break;
3123 ++__first;
3124 }
3125 // __first points to first false, everything prior to __first is already set.
3126 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3127 do
3128 {
3129 if (__first == --__last)
3130 return __first;
3131 } while (!__pred(*__last));
3132 // We now have a reduced range [__first, __last]
3133 // *__first is known to be false
3134 // *__last is known to be true
3135 // __len >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003136 difference_type __len = _VSTD::distance(__first, __last) + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003137 pair<value_type*, ptrdiff_t> __p(0, 0);
3138 unique_ptr<value_type, __return_temporary_buffer> __h;
3139 if (__len >= __alloc_limit)
3140 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003141 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003142 __h.reset(__p.first);
3143 }
3144 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3145 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3146}
3147
3148template <class _ForwardIterator, class _Predicate>
3149inline _LIBCPP_INLINE_VISIBILITY
3150_ForwardIterator
3151stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3152{
3153 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3154 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3155}
3156
3157// is_sorted_until
3158
3159template <class _ForwardIterator, class _Compare>
3160_ForwardIterator
3161is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3162{
3163 if (__first != __last)
3164 {
3165 _ForwardIterator __i = __first;
3166 while (++__i != __last)
3167 {
3168 if (__comp(*__i, *__first))
3169 return __i;
3170 __first = __i;
3171 }
3172 }
3173 return __last;
3174}
3175
Howard Hinnant324bb032010-08-22 00:02:43 +00003176template<class _ForwardIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003177inline _LIBCPP_INLINE_VISIBILITY
3178_ForwardIterator
3179is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3180{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003181 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003182}
3183
3184// is_sorted
3185
3186template <class _ForwardIterator, class _Compare>
3187inline _LIBCPP_INLINE_VISIBILITY
3188bool
3189is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3190{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003191 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003192}
3193
Howard Hinnant324bb032010-08-22 00:02:43 +00003194template<class _ForwardIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003195inline _LIBCPP_INLINE_VISIBILITY
3196bool
3197is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3198{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003199 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003200}
3201
3202// sort
3203
3204// stable, 2-3 compares, 0-2 swaps
3205
3206template <class _Compare, class _ForwardIterator>
3207unsigned
3208__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3209{
3210 unsigned __r = 0;
3211 if (!__c(*__y, *__x)) // if x <= y
3212 {
3213 if (!__c(*__z, *__y)) // if y <= z
3214 return __r; // x <= y && y <= z
3215 // x <= y && y > z
3216 swap(*__y, *__z); // x <= z && y < z
3217 __r = 1;
3218 if (__c(*__y, *__x)) // if x > y
3219 {
3220 swap(*__x, *__y); // x < y && y <= z
3221 __r = 2;
3222 }
3223 return __r; // x <= y && y < z
3224 }
3225 if (__c(*__z, *__y)) // x > y, if y > z
3226 {
3227 swap(*__x, *__z); // x < y && y < z
3228 __r = 1;
3229 return __r;
3230 }
3231 swap(*__x, *__y); // x > y && y <= z
3232 __r = 1; // x < y && x <= z
3233 if (__c(*__z, *__y)) // if y > z
3234 {
3235 swap(*__y, *__z); // x <= y && y < z
3236 __r = 2;
3237 }
3238 return __r;
3239} // x <= y && y <= z
3240
3241// stable, 3-6 compares, 0-5 swaps
3242
3243template <class _Compare, class _ForwardIterator>
3244unsigned
3245__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3246 _ForwardIterator __x4, _Compare __c)
3247{
3248 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3249 if (__c(*__x4, *__x3))
3250 {
3251 swap(*__x3, *__x4);
3252 ++__r;
3253 if (__c(*__x3, *__x2))
3254 {
3255 swap(*__x2, *__x3);
3256 ++__r;
3257 if (__c(*__x2, *__x1))
3258 {
3259 swap(*__x1, *__x2);
3260 ++__r;
3261 }
3262 }
3263 }
3264 return __r;
3265}
3266
3267// stable, 4-10 compares, 0-9 swaps
3268
3269template <class _Compare, class _ForwardIterator>
3270unsigned
3271__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3272 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3273{
3274 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3275 if (__c(*__x5, *__x4))
3276 {
3277 swap(*__x4, *__x5);
3278 ++__r;
3279 if (__c(*__x4, *__x3))
3280 {
3281 swap(*__x3, *__x4);
3282 ++__r;
3283 if (__c(*__x3, *__x2))
3284 {
3285 swap(*__x2, *__x3);
3286 ++__r;
3287 if (__c(*__x2, *__x1))
3288 {
3289 swap(*__x1, *__x2);
3290 ++__r;
3291 }
3292 }
3293 }
3294 }
3295 return __r;
3296}
3297
3298// Assumes size > 0
3299template <class _Compare, class _BirdirectionalIterator>
3300void
3301__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3302{
3303 _BirdirectionalIterator __lm1 = __last;
3304 for (--__lm1; __first != __lm1; ++__first)
3305 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003306 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003307 typename add_lvalue_reference<_Compare>::type>
3308 (__first, __last, __comp);
3309 if (__i != __first)
3310 swap(*__first, *__i);
3311 }
3312}
3313
3314template <class _Compare, class _BirdirectionalIterator>
3315void
3316__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3317{
3318 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3319 if (__first != __last)
3320 {
3321 _BirdirectionalIterator __i = __first;
3322 for (++__i; __i != __last; ++__i)
3323 {
3324 _BirdirectionalIterator __j = __i;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003325 value_type __t(_VSTD::move(*__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003326 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003327 *__j = _VSTD::move(*__k);
3328 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003329 }
3330 }
3331}
3332
3333template <class _Compare, class _RandomAccessIterator>
3334void
3335__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3336{
3337 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3338 _RandomAccessIterator __j = __first+2;
3339 __sort3<_Compare>(__first, __first+1, __j, __comp);
3340 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3341 {
3342 if (__comp(*__i, *__j))
3343 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003344 value_type __t(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003345 _RandomAccessIterator __k = __j;
3346 __j = __i;
3347 do
3348 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003349 *__j = _VSTD::move(*__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003350 __j = __k;
3351 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003352 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003353 }
3354 __j = __i;
3355 }
3356}
3357
3358template <class _Compare, class _RandomAccessIterator>
3359bool
3360__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3361{
3362 switch (__last - __first)
3363 {
3364 case 0:
3365 case 1:
3366 return true;
3367 case 2:
3368 if (__comp(*--__last, *__first))
3369 swap(*__first, *__last);
3370 return true;
3371 case 3:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003372 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003373 return true;
3374 case 4:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003375 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003376 return true;
3377 case 5:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003378 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003379 return true;
3380 }
3381 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3382 _RandomAccessIterator __j = __first+2;
3383 __sort3<_Compare>(__first, __first+1, __j, __comp);
3384 const unsigned __limit = 8;
3385 unsigned __count = 0;
3386 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3387 {
3388 if (__comp(*__i, *__j))
3389 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003390 value_type __t(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003391 _RandomAccessIterator __k = __j;
3392 __j = __i;
3393 do
3394 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003395 *__j = _VSTD::move(*__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003396 __j = __k;
3397 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003398 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003399 if (++__count == __limit)
3400 return ++__i == __last;
3401 }
3402 __j = __i;
3403 }
3404 return true;
3405}
3406
3407template <class _Compare, class _BirdirectionalIterator>
3408void
3409__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3410 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3411{
3412 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3413 if (__first1 != __last1)
3414 {
3415 __destruct_n __d(0);
3416 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3417 value_type* __last2 = __first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003418 ::new(__last2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003419 __d.__incr((value_type*)0);
3420 for (++__last2; ++__first1 != __last1; ++__last2)
3421 {
3422 value_type* __j2 = __last2;
3423 value_type* __i2 = __j2;
3424 if (__comp(*__first1, *--__i2))
3425 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003426 ::new(__j2) value_type(_VSTD::move(*__i2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003427 __d.__incr((value_type*)0);
3428 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003429 *__j2 = _VSTD::move(*__i2);
3430 *__j2 = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003431 }
3432 else
3433 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003434 ::new(__j2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003435 __d.__incr((value_type*)0);
3436 }
3437 }
3438 __h.release();
3439 }
3440}
3441
3442template <class _Compare, class _RandomAccessIterator>
3443void
3444__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3445{
3446 // _Compare is known to be a reference type
3447 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3448 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant1468b662010-11-19 22:17:28 +00003449 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3450 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003451 while (true)
3452 {
3453 __restart:
3454 difference_type __len = __last - __first;
3455 switch (__len)
3456 {
3457 case 0:
3458 case 1:
3459 return;
3460 case 2:
3461 if (__comp(*--__last, *__first))
3462 swap(*__first, *__last);
3463 return;
3464 case 3:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003465 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003466 return;
3467 case 4:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003468 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003469 return;
3470 case 5:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003471 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003472 return;
3473 }
3474 if (__len <= __limit)
3475 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003476 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003477 return;
3478 }
3479 // __len > 5
3480 _RandomAccessIterator __m = __first;
3481 _RandomAccessIterator __lm1 = __last;
3482 --__lm1;
3483 unsigned __n_swaps;
3484 {
3485 difference_type __delta;
3486 if (__len >= 1000)
3487 {
3488 __delta = __len/2;
3489 __m += __delta;
3490 __delta /= 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003491 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003492 }
3493 else
3494 {
3495 __delta = __len/2;
3496 __m += __delta;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003497 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003498 }
3499 }
3500 // *__m is median
3501 // partition [__first, __m) < *__m and *__m <= [__m, __last)
3502 // (this inhibits tossing elements equivalent to __m around unnecessarily)
3503 _RandomAccessIterator __i = __first;
3504 _RandomAccessIterator __j = __lm1;
3505 // j points beyond range to be tested, *__m is known to be <= *__lm1
3506 // The search going up is known to be guarded but the search coming down isn't.
3507 // Prime the downward search with a guard.
3508 if (!__comp(*__i, *__m)) // if *__first == *__m
3509 {
3510 // *__first == *__m, *__first doesn't go in first part
3511 // manually guard downward moving __j against __i
3512 while (true)
3513 {
3514 if (__i == --__j)
3515 {
3516 // *__first == *__m, *__m <= all other elements
3517 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
3518 ++__i; // __first + 1
3519 __j = __last;
3520 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
3521 {
3522 while (true)
3523 {
3524 if (__i == __j)
3525 return; // [__first, __last) all equivalent elements
3526 if (__comp(*__first, *__i))
3527 {
3528 swap(*__i, *__j);
3529 ++__n_swaps;
3530 ++__i;
3531 break;
3532 }
3533 ++__i;
3534 }
3535 }
3536 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
3537 if (__i == __j)
3538 return;
3539 while (true)
3540 {
3541 while (!__comp(*__first, *__i))
3542 ++__i;
3543 while (__comp(*__first, *--__j))
3544 ;
3545 if (__i >= __j)
3546 break;
3547 swap(*__i, *__j);
3548 ++__n_swaps;
3549 ++__i;
3550 }
3551 // [__first, __i) == *__first and *__first < [__i, __last)
3552 // The first part is sorted, sort the secod part
Howard Hinnant0949eed2011-06-30 21:18:19 +00003553 // _VSTD::__sort<_Compare>(__i, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003554 __first = __i;
3555 goto __restart;
3556 }
3557 if (__comp(*__j, *__m))
3558 {
3559 swap(*__i, *__j);
3560 ++__n_swaps;
3561 break; // found guard for downward moving __j, now use unguarded partition
3562 }
3563 }
3564 }
3565 // It is known that *__i < *__m
3566 ++__i;
3567 // j points beyond range to be tested, *__m is known to be <= *__lm1
3568 // if not yet partitioned...
3569 if (__i < __j)
3570 {
3571 // known that *(__i - 1) < *__m
3572 // known that __i <= __m
3573 while (true)
3574 {
3575 // __m still guards upward moving __i
3576 while (__comp(*__i, *__m))
3577 ++__i;
3578 // It is now known that a guard exists for downward moving __j
3579 while (!__comp(*--__j, *__m))
3580 ;
3581 if (__i > __j)
3582 break;
3583 swap(*__i, *__j);
3584 ++__n_swaps;
3585 // It is known that __m != __j
3586 // If __m just moved, follow it
3587 if (__m == __i)
3588 __m = __j;
3589 ++__i;
3590 }
3591 }
3592 // [__first, __i) < *__m and *__m <= [__i, __last)
3593 if (__i != __m && __comp(*__m, *__i))
3594 {
3595 swap(*__i, *__m);
3596 ++__n_swaps;
3597 }
3598 // [__first, __i) < *__i and *__i <= [__i+1, __last)
3599 // If we were given a perfect partition, see if insertion sort is quick...
3600 if (__n_swaps == 0)
3601 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003602 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
3603 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003604 {
3605 if (__fs)
3606 return;
3607 __last = __i;
3608 continue;
3609 }
3610 else
3611 {
3612 if (__fs)
3613 {
3614 __first = ++__i;
3615 continue;
3616 }
3617 }
3618 }
3619 // sort smaller range with recursive call and larger with tail recursion elimination
3620 if (__i - __first < __last - __i)
3621 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003622 _VSTD::__sort<_Compare>(__first, __i, __comp);
3623 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003624 __first = ++__i;
3625 }
3626 else
3627 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003628 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
3629 // _VSTD::__sort<_Compare>(__first, __i, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003630 __last = __i;
3631 }
3632 }
3633}
3634
3635// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
3636template <class _RandomAccessIterator, class _Compare>
3637inline _LIBCPP_INLINE_VISIBILITY
3638void
3639sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3640{
Howard Hinnant7a563db2011-09-14 18:33:51 +00003641#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003642 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3643 __debug_less<_Compare> __c(__comp);
3644 __sort<_Comp_ref>(__first, __last, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003645#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003646 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3647 __sort<_Comp_ref>(__first, __last, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003648#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003649}
3650
3651template <class _RandomAccessIterator>
3652inline _LIBCPP_INLINE_VISIBILITY
3653void
3654sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
3655{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003656 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003657}
3658
3659template <class _Tp>
3660inline _LIBCPP_INLINE_VISIBILITY
3661void
3662sort(_Tp** __first, _Tp** __last)
3663{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003664 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003665}
3666
3667template <class _Tp>
3668inline _LIBCPP_INLINE_VISIBILITY
3669void
3670sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
3671{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003672 _VSTD::sort(__first.base(), __last.base());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003673}
3674
Howard Hinnant7a563db2011-09-14 18:33:51 +00003675template <class _Tp, class _Compare>
3676inline _LIBCPP_INLINE_VISIBILITY
3677void
3678sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
3679{
3680 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3681 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
3682}
3683
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003684extern template void __sort<__less<char>&, char*>(char*, char*, __less<char>&);
3685extern template void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&);
3686extern template void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&);
3687extern template void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&);
3688extern template void __sort<__less<short>&, short*>(short*, short*, __less<short>&);
3689extern template void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&);
3690extern template void __sort<__less<int>&, int*>(int*, int*, __less<int>&);
3691extern template void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&);
3692extern template void __sort<__less<long>&, long*>(long*, long*, __less<long>&);
3693extern template void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&);
3694extern template void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&);
3695extern template void __sort<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&);
3696extern template void __sort<__less<float>&, float*>(float*, float*, __less<float>&);
3697extern template void __sort<__less<double>&, double*>(double*, double*, __less<double>&);
3698extern template void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&);
3699
3700extern template bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&);
3701extern template bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&);
3702extern template bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&);
3703extern template bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&);
3704extern template bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&);
3705extern template bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&);
3706extern template bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&);
3707extern template bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&);
3708extern template bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&);
3709extern template bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&);
3710extern template bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&);
3711extern template bool __insertion_sort_incomplete<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&);
3712extern template bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&);
3713extern template bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&);
3714extern template bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&);
3715
3716extern template unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&);
3717
3718// lower_bound
3719
3720template <class _Compare, class _ForwardIterator, class _Tp>
3721_ForwardIterator
3722__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3723{
3724 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003725 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003726 while (__len != 0)
3727 {
3728 difference_type __l2 = __len / 2;
3729 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003730 _VSTD::advance(__m, __l2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003731 if (__comp(*__m, __value))
3732 {
3733 __first = ++__m;
3734 __len -= __l2 + 1;
3735 }
3736 else
3737 __len = __l2;
3738 }
3739 return __first;
3740}
3741
3742template <class _ForwardIterator, class _Tp, class _Compare>
3743inline _LIBCPP_INLINE_VISIBILITY
3744_ForwardIterator
3745lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3746{
Howard Hinnant7a563db2011-09-14 18:33:51 +00003747#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003748 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3749 __debug_less<_Compare> __c(__comp);
3750 return __lower_bound<_Comp_ref>(__first, __last, __value, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003751#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003752 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3753 return __lower_bound<_Comp_ref>(__first, __last, __value, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003754#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003755}
3756
3757template <class _ForwardIterator, class _Tp>
3758inline _LIBCPP_INLINE_VISIBILITY
3759_ForwardIterator
3760lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
3761{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003762 return _VSTD::lower_bound(__first, __last, __value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003763 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
3764}
3765
3766// upper_bound
3767
3768template <class _Compare, class _ForwardIterator, class _Tp>
3769_ForwardIterator
3770__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3771{
3772 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003773 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003774 while (__len != 0)
3775 {
3776 difference_type __l2 = __len / 2;
3777 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003778 _VSTD::advance(__m, __l2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003779 if (__comp(__value, *__m))
3780 __len = __l2;
3781 else
3782 {
3783 __first = ++__m;
3784 __len -= __l2 + 1;
3785 }
3786 }
3787 return __first;
3788}
3789
3790template <class _ForwardIterator, class _Tp, class _Compare>
3791inline _LIBCPP_INLINE_VISIBILITY
3792_ForwardIterator
3793upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3794{
Howard Hinnant7a563db2011-09-14 18:33:51 +00003795#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003796 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3797 __debug_less<_Compare> __c(__comp);
3798 return __upper_bound<_Comp_ref>(__first, __last, __value, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003799#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003800 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3801 return __upper_bound<_Comp_ref>(__first, __last, __value, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003802#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003803}
3804
3805template <class _ForwardIterator, class _Tp>
3806inline _LIBCPP_INLINE_VISIBILITY
3807_ForwardIterator
3808upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
3809{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003810 return _VSTD::upper_bound(__first, __last, __value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003811 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
3812}
3813
3814// equal_range
3815
3816template <class _Compare, class _ForwardIterator, class _Tp>
3817pair<_ForwardIterator, _ForwardIterator>
3818__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3819{
3820 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003821 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003822 while (__len != 0)
3823 {
3824 difference_type __l2 = __len / 2;
3825 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003826 _VSTD::advance(__m, __l2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003827 if (__comp(*__m, __value))
3828 {
3829 __first = ++__m;
3830 __len -= __l2 + 1;
3831 }
3832 else if (__comp(__value, *__m))
3833 {
3834 __last = __m;
3835 __len = __l2;
3836 }
3837 else
3838 {
3839 _ForwardIterator __mp1 = __m;
3840 return pair<_ForwardIterator, _ForwardIterator>
3841 (
3842 __lower_bound<_Compare>(__first, __m, __value, __comp),
3843 __upper_bound<_Compare>(++__mp1, __last, __value, __comp)
3844 );
3845 }
3846 }
3847 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
3848}
3849
3850template <class _ForwardIterator, class _Tp, class _Compare>
3851inline _LIBCPP_INLINE_VISIBILITY
3852pair<_ForwardIterator, _ForwardIterator>
3853equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3854{
Howard Hinnant7a563db2011-09-14 18:33:51 +00003855#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003856 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3857 __debug_less<_Compare> __c(__comp);
3858 return __equal_range<_Comp_ref>(__first, __last, __value, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003859#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003860 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3861 return __equal_range<_Comp_ref>(__first, __last, __value, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003862#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003863}
3864
3865template <class _ForwardIterator, class _Tp>
3866inline _LIBCPP_INLINE_VISIBILITY
3867pair<_ForwardIterator, _ForwardIterator>
3868equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
3869{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003870 return _VSTD::equal_range(__first, __last, __value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003871 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
3872}
3873
3874// binary_search
3875
3876template <class _Compare, class _ForwardIterator, class _Tp>
3877inline _LIBCPP_INLINE_VISIBILITY
3878bool
3879__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3880{
3881 __first = __lower_bound<_Compare>(__first, __last, __value, __comp);
3882 return __first != __last && !__comp(__value, *__first);
3883}
3884
3885template <class _ForwardIterator, class _Tp, class _Compare>
3886inline _LIBCPP_INLINE_VISIBILITY
3887bool
3888binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3889{
Howard Hinnant7a563db2011-09-14 18:33:51 +00003890#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003891 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3892 __debug_less<_Compare> __c(__comp);
3893 return __binary_search<_Comp_ref>(__first, __last, __value, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003894#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003895 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3896 return __binary_search<_Comp_ref>(__first, __last, __value, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003897#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003898}
3899
3900template <class _ForwardIterator, class _Tp>
3901inline _LIBCPP_INLINE_VISIBILITY
3902bool
3903binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
3904{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003905 return _VSTD::binary_search(__first, __last, __value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003906 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
3907}
3908
3909// merge
3910
3911template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
3912_OutputIterator
3913__merge(_InputIterator1 __first1, _InputIterator1 __last1,
3914 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
3915{
3916 for (; __first1 != __last1; ++__result)
3917 {
3918 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003919 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003920 if (__comp(*__first2, *__first1))
3921 {
3922 *__result = *__first2;
3923 ++__first2;
3924 }
3925 else
3926 {
3927 *__result = *__first1;
3928 ++__first1;
3929 }
3930 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003931 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003932}
3933
3934template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
3935inline _LIBCPP_INLINE_VISIBILITY
3936_OutputIterator
3937merge(_InputIterator1 __first1, _InputIterator1 __last1,
3938 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
3939{
Howard Hinnant7a563db2011-09-14 18:33:51 +00003940#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003941 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3942 __debug_less<_Compare> __c(__comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003943 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003944#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003945 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003946 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003947#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003948}
3949
3950template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
3951inline _LIBCPP_INLINE_VISIBILITY
3952_OutputIterator
3953merge(_InputIterator1 __first1, _InputIterator1 __last1,
3954 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
3955{
3956 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
3957 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
3958 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
3959}
3960
3961// inplace_merge
3962
3963template <class _Compare, class _BidirectionalIterator>
3964void
3965__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
3966 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
3967 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
3968 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
3969{
3970 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3971 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3972 typedef typename iterator_traits<_BidirectionalIterator>::pointer pointer;
3973 __destruct_n __d(0);
3974 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
3975 if (__len1 <= __len2)
3976 {
3977 value_type* __p = __buff;
3978 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), ++__i, ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003979 ::new(__p) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003980 __merge<_Compare>(move_iterator<value_type*>(__buff),
3981 move_iterator<value_type*>(__p),
3982 move_iterator<_BidirectionalIterator>(__middle),
3983 move_iterator<_BidirectionalIterator>(__last),
3984 __first, __comp);
3985 }
3986 else
3987 {
3988 value_type* __p = __buff;
3989 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), ++__i, ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003990 ::new(__p) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003991 typedef reverse_iterator<_BidirectionalIterator> _RBi;
3992 typedef reverse_iterator<value_type*> _Rv;
3993 __merge(move_iterator<_RBi>(_RBi(__middle)), move_iterator<_RBi>(_RBi(__first)),
3994 move_iterator<_Rv>(_Rv(__p)), move_iterator<_Rv>(_Rv(__buff)),
3995 _RBi(__last), __negate<_Compare>(__comp));
3996 }
3997}
3998
3999template <class _Compare, class _BidirectionalIterator>
4000void
4001__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4002 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4003 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4004 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4005{
4006 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4007 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4008 while (true)
4009 {
4010 // if __middle == __last, we're done
4011 if (__len2 == 0)
4012 return;
4013 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
4014 for (; true; ++__first, --__len1)
4015 {
4016 if (__len1 == 0)
4017 return;
4018 if (__comp(*__middle, *__first))
4019 break;
4020 }
4021 if (__len1 <= __buff_size || __len2 <= __buff_size)
4022 {
4023 __buffered_inplace_merge<_Compare>(__first, __middle, __last, __comp, __len1, __len2, __buff);
4024 return;
4025 }
4026 // __first < __middle < __last
4027 // *__first > *__middle
4028 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4029 // all elements in:
4030 // [__first, __m1) <= [__middle, __m2)
4031 // [__middle, __m2) < [__m1, __middle)
4032 // [__m1, __middle) <= [__m2, __last)
4033 // and __m1 or __m2 is in the middle of its range
4034 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4035 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4036 difference_type __len11; // distance(__first, __m1)
4037 difference_type __len21; // distance(__middle, __m2)
4038 // binary search smaller range
4039 if (__len1 < __len2)
4040 { // __len >= 1, __len2 >= 2
4041 __len21 = __len2 / 2;
4042 __m2 = __middle;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004043 _VSTD::advance(__m2, __len21);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004044 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004045 __len11 = _VSTD::distance(__first, __m1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004046 }
4047 else
4048 {
4049 if (__len1 == 1)
4050 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4051 // It is known *__first > *__middle
4052 swap(*__first, *__middle);
4053 return;
4054 }
4055 // __len1 >= 2, __len2 >= 1
4056 __len11 = __len1 / 2;
4057 __m1 = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004058 _VSTD::advance(__m1, __len11);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004059 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004060 __len21 = _VSTD::distance(__middle, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004061 }
4062 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4063 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4064 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4065 // swap middle two partitions
Howard Hinnant0949eed2011-06-30 21:18:19 +00004066 __middle = _VSTD::rotate(__m1, __middle, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004067 // __len12 and __len21 now have swapped meanings
4068 // merge smaller range with recurisve call and larger with tail recursion elimination
4069 if (__len11 + __len21 < __len12 + __len22)
4070 {
4071 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4072// __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4073 __first = __middle;
4074 __middle = __m2;
4075 __len1 = __len12;
4076 __len2 = __len22;
4077 }
4078 else
4079 {
4080 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4081// __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4082 __last = __middle;
4083 __middle = __m1;
4084 __len1 = __len11;
4085 __len2 = __len21;
4086 }
4087 }
4088}
4089
4090template <class _Tp>
4091struct __inplace_merge_switch
4092{
Howard Hinnant1468b662010-11-19 22:17:28 +00004093 static const unsigned value = is_trivially_copy_assignable<_Tp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004094};
4095
4096template <class _BidirectionalIterator, class _Compare>
4097inline _LIBCPP_INLINE_VISIBILITY
4098void
4099inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4100 _Compare __comp)
4101{
4102 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4103 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004104 difference_type __len1 = _VSTD::distance(__first, __middle);
4105 difference_type __len2 = _VSTD::distance(__middle, __last);
4106 difference_type __buf_size = _VSTD::min(__len1, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004107 pair<value_type*, ptrdiff_t> __buf(0, 0);
4108 unique_ptr<value_type, __return_temporary_buffer> __h;
4109 if (__inplace_merge_switch<value_type>::value && __buf_size > 8)
4110 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004111 __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004112 __h.reset(__buf.first);
4113 }
Howard Hinnant7a563db2011-09-14 18:33:51 +00004114#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004115 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4116 __debug_less<_Compare> __c(__comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004117 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004118 __buf.first, __buf.second);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004119#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004120 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004121 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004122 __buf.first, __buf.second);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004123#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004124}
4125
4126template <class _BidirectionalIterator>
4127inline _LIBCPP_INLINE_VISIBILITY
4128void
4129inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4130{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004131 _VSTD::inplace_merge(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004132 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4133}
4134
4135// stable_sort
4136
4137template <class _Compare, class _InputIterator1, class _InputIterator2>
4138void
4139__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4140 _InputIterator2 __first2, _InputIterator2 __last2,
4141 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4142{
4143 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4144 __destruct_n __d(0);
4145 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4146 for (; true; ++__result)
4147 {
4148 if (__first1 == __last1)
4149 {
4150 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
Howard Hinnant0949eed2011-06-30 21:18:19 +00004151 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004152 __h.release();
4153 return;
4154 }
4155 if (__first2 == __last2)
4156 {
4157 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
Howard Hinnant0949eed2011-06-30 21:18:19 +00004158 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004159 __h.release();
4160 return;
4161 }
4162 if (__comp(*__first2, *__first1))
4163 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004164 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004165 __d.__incr((value_type*)0);
4166 ++__first2;
4167 }
4168 else
4169 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004170 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004171 __d.__incr((value_type*)0);
4172 ++__first1;
4173 }
4174 }
4175}
4176
4177template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4178void
4179__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4180 _InputIterator2 __first2, _InputIterator2 __last2,
4181 _OutputIterator __result, _Compare __comp)
4182{
4183 for (; __first1 != __last1; ++__result)
4184 {
4185 if (__first2 == __last2)
4186 {
4187 for (; __first1 != __last1; ++__first1, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004188 *__result = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004189 return;
4190 }
4191 if (__comp(*__first2, *__first1))
4192 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004193 *__result = _VSTD::move(*__first2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004194 ++__first2;
4195 }
4196 else
4197 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004198 *__result = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004199 ++__first1;
4200 }
4201 }
4202 for (; __first2 != __last2; ++__first2, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004203 *__result = _VSTD::move(*__first2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004204}
4205
4206template <class _Compare, class _RandomAccessIterator>
4207void
4208__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4209 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4210 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4211
4212template <class _Compare, class _RandomAccessIterator>
4213void
4214__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4215 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4216 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4217{
4218 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4219 switch (__len)
4220 {
4221 case 0:
4222 return;
4223 case 1:
Howard Hinnant0949eed2011-06-30 21:18:19 +00004224 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004225 return;
4226 case 2:
4227 __destruct_n __d(0);
4228 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4229 if (__comp(*--__last1, *__first1))
4230 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004231 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004232 __d.__incr((value_type*)0);
4233 ++__first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004234 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004235 }
4236 else
4237 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004238 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004239 __d.__incr((value_type*)0);
4240 ++__first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004241 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004242 }
4243 __h2.release();
4244 return;
4245 }
4246 if (__len <= 8)
4247 {
4248 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4249 return;
4250 }
4251 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4252 _RandomAccessIterator __m = __first1 + __l2;
4253 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4254 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4255 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4256}
4257
4258template <class _Tp>
4259struct __stable_sort_switch
4260{
Howard Hinnant1468b662010-11-19 22:17:28 +00004261 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004262};
4263
4264template <class _Compare, class _RandomAccessIterator>
4265void
4266__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4267 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4268 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4269{
4270 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4271 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4272 switch (__len)
4273 {
4274 case 0:
4275 case 1:
4276 return;
4277 case 2:
4278 if (__comp(*--__last, *__first))
4279 swap(*__first, *__last);
4280 return;
4281 }
4282 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4283 {
4284 __insertion_sort<_Compare>(__first, __last, __comp);
4285 return;
4286 }
4287 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4288 _RandomAccessIterator __m = __first + __l2;
4289 if (__len <= __buff_size)
4290 {
4291 __destruct_n __d(0);
4292 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4293 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4294 __d.__set(__l2, (value_type*)0);
4295 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4296 __d.__set(__len, (value_type*)0);
4297 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4298// __merge<_Compare>(move_iterator<value_type*>(__buff),
4299// move_iterator<value_type*>(__buff + __l2),
4300// move_iterator<_RandomAccessIterator>(__buff + __l2),
4301// move_iterator<_RandomAccessIterator>(__buff + __len),
4302// __first, __comp);
4303 return;
4304 }
4305 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4306 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4307 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4308}
4309
4310template <class _RandomAccessIterator, class _Compare>
4311inline _LIBCPP_INLINE_VISIBILITY
4312void
4313stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4314{
4315 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4316 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4317 difference_type __len = __last - __first;
4318 pair<value_type*, ptrdiff_t> __buf(0, 0);
4319 unique_ptr<value_type, __return_temporary_buffer> __h;
4320 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4321 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004322 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004323 __h.reset(__buf.first);
4324 }
Howard Hinnant7a563db2011-09-14 18:33:51 +00004325#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004326 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4327 __debug_less<_Compare> __c(__comp);
4328 __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004329#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004330 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4331 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004332#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004333}
4334
4335template <class _RandomAccessIterator>
4336inline _LIBCPP_INLINE_VISIBILITY
4337void
4338stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4339{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004340 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004341}
4342
4343// is_heap_until
4344
4345template <class _RandomAccessIterator, class _Compare>
4346_RandomAccessIterator
4347is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4348{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004349 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004350 difference_type __len = __last - __first;
4351 difference_type __p = 0;
4352 difference_type __c = 1;
4353 _RandomAccessIterator __pp = __first;
4354 while (__c < __len)
4355 {
4356 _RandomAccessIterator __cp = __first + __c;
4357 if (__comp(*__pp, *__cp))
4358 return __cp;
4359 ++__c;
4360 ++__cp;
4361 if (__c == __len)
4362 return __last;
4363 if (__comp(*__pp, *__cp))
4364 return __cp;
4365 ++__p;
4366 ++__pp;
4367 __c = 2 * __p + 1;
4368 }
4369 return __last;
4370}
4371
Howard Hinnant324bb032010-08-22 00:02:43 +00004372template<class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004373inline _LIBCPP_INLINE_VISIBILITY
4374_RandomAccessIterator
4375is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4376{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004377 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004378}
4379
4380// is_heap
4381
4382template <class _RandomAccessIterator, class _Compare>
4383inline _LIBCPP_INLINE_VISIBILITY
4384bool
4385is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4386{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004387 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004388}
4389
Howard Hinnant324bb032010-08-22 00:02:43 +00004390template<class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004391inline _LIBCPP_INLINE_VISIBILITY
4392bool
4393is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4394{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004395 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004396}
4397
4398// push_heap
4399
4400template <class _Compare, class _RandomAccessIterator>
4401void
4402__push_heap_front(_RandomAccessIterator __first, _RandomAccessIterator, _Compare __comp,
4403 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4404{
4405 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4406 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4407 if (__len > 1)
4408 {
4409 difference_type __p = 0;
4410 _RandomAccessIterator __pp = __first;
4411 difference_type __c = 2;
4412 _RandomAccessIterator __cp = __first + __c;
4413 if (__c == __len || __comp(*__cp, *(__cp - 1)))
4414 {
4415 --__c;
4416 --__cp;
4417 }
4418 if (__comp(*__pp, *__cp))
4419 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004420 value_type __t(_VSTD::move(*__pp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004421 do
4422 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004423 *__pp = _VSTD::move(*__cp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004424 __pp = __cp;
4425 __p = __c;
4426 __c = (__p + 1) * 2;
4427 if (__c > __len)
4428 break;
4429 __cp = __first + __c;
4430 if (__c == __len || __comp(*__cp, *(__cp - 1)))
4431 {
4432 --__c;
4433 --__cp;
4434 }
4435 } while (__comp(__t, *__cp));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004436 *__pp = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004437 }
4438 }
4439}
4440
4441template <class _Compare, class _RandomAccessIterator>
4442void
4443__push_heap_back(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4444 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4445{
4446 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4447 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4448 if (__len > 1)
4449 {
4450 __len = (__len - 2) / 2;
4451 _RandomAccessIterator __ptr = __first + __len;
4452 if (__comp(*__ptr, *--__last))
4453 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004454 value_type __t(_VSTD::move(*__last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004455 do
4456 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004457 *__last = _VSTD::move(*__ptr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004458 __last = __ptr;
4459 if (__len == 0)
4460 break;
4461 __len = (__len - 1) / 2;
4462 __ptr = __first + __len;
4463 } while (__comp(*__ptr, __t));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004464 *__last = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004465 }
4466 }
4467}
4468
4469template <class _RandomAccessIterator, class _Compare>
4470inline _LIBCPP_INLINE_VISIBILITY
4471void
4472push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4473{
Howard Hinnant7a563db2011-09-14 18:33:51 +00004474#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004475 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4476 __debug_less<_Compare> __c(__comp);
4477 __push_heap_back<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004478#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004479 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4480 __push_heap_back<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004481#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004482}
4483
4484template <class _RandomAccessIterator>
4485inline _LIBCPP_INLINE_VISIBILITY
4486void
4487push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4488{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004489 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004490}
4491
4492// pop_heap
4493
4494template <class _Compare, class _RandomAccessIterator>
4495inline _LIBCPP_INLINE_VISIBILITY
4496void
4497__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4498 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4499{
4500 if (__len > 1)
4501 {
4502 swap(*__first, *--__last);
4503 __push_heap_front<_Compare>(__first, __last, __comp, __len-1);
4504 }
4505}
4506
4507template <class _RandomAccessIterator, class _Compare>
4508inline _LIBCPP_INLINE_VISIBILITY
4509void
4510pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4511{
Howard Hinnant7a563db2011-09-14 18:33:51 +00004512#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004513 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4514 __debug_less<_Compare> __c(__comp);
4515 __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004516#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004517 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4518 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004519#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004520}
4521
4522template <class _RandomAccessIterator>
4523inline _LIBCPP_INLINE_VISIBILITY
4524void
4525pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4526{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004527 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004528}
4529
4530// make_heap
4531
4532template <class _Compare, class _RandomAccessIterator>
4533void
4534__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4535{
4536 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4537 difference_type __n = __last - __first;
4538 if (__n > 1)
4539 {
4540 __last = __first;
4541 ++__last;
4542 for (difference_type __i = 1; __i < __n;)
4543 __push_heap_back<_Compare>(__first, ++__last, __comp, ++__i);
4544 }
4545}
4546
4547template <class _RandomAccessIterator, class _Compare>
4548inline _LIBCPP_INLINE_VISIBILITY
4549void
4550make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4551{
Howard Hinnant7a563db2011-09-14 18:33:51 +00004552#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004553 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4554 __debug_less<_Compare> __c(__comp);
4555 __make_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004556#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004557 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4558 __make_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004559#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004560}
4561
4562template <class _RandomAccessIterator>
4563inline _LIBCPP_INLINE_VISIBILITY
4564void
4565make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4566{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004567 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004568}
4569
4570// sort_heap
4571
4572template <class _Compare, class _RandomAccessIterator>
4573void
4574__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4575{
4576 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4577 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
4578 __pop_heap<_Compare>(__first, __last, __comp, __n);
4579}
4580
4581template <class _RandomAccessIterator, class _Compare>
4582inline _LIBCPP_INLINE_VISIBILITY
4583void
4584sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4585{
Howard Hinnant7a563db2011-09-14 18:33:51 +00004586#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004587 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4588 __debug_less<_Compare> __c(__comp);
4589 __sort_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004590#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004591 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4592 __sort_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004593#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004594}
4595
4596template <class _RandomAccessIterator>
4597inline _LIBCPP_INLINE_VISIBILITY
4598void
4599sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4600{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004601 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004602}
4603
4604// partial_sort
4605
4606template <class _Compare, class _RandomAccessIterator>
4607void
4608__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
4609 _Compare __comp)
4610{
4611 __make_heap<_Compare>(__first, __middle, __comp);
4612 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
4613 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
4614 {
4615 if (__comp(*__i, *__first))
4616 {
4617 swap(*__i, *__first);
4618 __push_heap_front<_Compare>(__first, __middle, __comp, __len);
4619 }
4620 }
4621 __sort_heap<_Compare>(__first, __middle, __comp);
4622}
4623
4624template <class _RandomAccessIterator, class _Compare>
4625inline _LIBCPP_INLINE_VISIBILITY
4626void
4627partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
4628 _Compare __comp)
4629{
Howard Hinnant7a563db2011-09-14 18:33:51 +00004630#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004631 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4632 __debug_less<_Compare> __c(__comp);
4633 __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004634#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004635 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4636 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004637#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004638}
4639
4640template <class _RandomAccessIterator>
4641inline _LIBCPP_INLINE_VISIBILITY
4642void
4643partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
4644{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004645 _VSTD::partial_sort(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004646 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4647}
4648
4649// partial_sort_copy
4650
4651template <class _Compare, class _InputIterator, class _RandomAccessIterator>
4652_RandomAccessIterator
4653__partial_sort_copy(_InputIterator __first, _InputIterator __last,
4654 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
4655{
4656 _RandomAccessIterator __r = __result_first;
4657 if (__r != __result_last)
4658 {
4659 typename iterator_traits<_RandomAccessIterator>::difference_type __len = 0;
4660 for (; __first != __last && __r != __result_last; ++__first, ++__r, ++__len)
4661 *__r = *__first;
4662 __make_heap<_Compare>(__result_first, __r, __comp);
4663 for (; __first != __last; ++__first)
4664 if (__comp(*__first, *__result_first))
4665 {
4666 *__result_first = *__first;
4667 __push_heap_front<_Compare>(__result_first, __r, __comp, __len);
4668 }
4669 __sort_heap<_Compare>(__result_first, __r, __comp);
4670 }
4671 return __r;
4672}
4673
4674template <class _InputIterator, class _RandomAccessIterator, class _Compare>
4675inline _LIBCPP_INLINE_VISIBILITY
4676_RandomAccessIterator
4677partial_sort_copy(_InputIterator __first, _InputIterator __last,
4678 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
4679{
Howard Hinnant7a563db2011-09-14 18:33:51 +00004680#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004681 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4682 __debug_less<_Compare> __c(__comp);
4683 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004684#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004685 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4686 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004687#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004688}
4689
4690template <class _InputIterator, class _RandomAccessIterator>
4691inline _LIBCPP_INLINE_VISIBILITY
4692_RandomAccessIterator
4693partial_sort_copy(_InputIterator __first, _InputIterator __last,
4694 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
4695{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004696 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004697 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4698}
4699
4700// nth_element
4701
4702template <class _Compare, class _RandomAccessIterator>
4703void
4704__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
4705{
4706 // _Compare is known to be a reference type
4707 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4708 const difference_type __limit = 7;
4709 while (true)
4710 {
4711 __restart:
4712 difference_type __len = __last - __first;
4713 switch (__len)
4714 {
4715 case 0:
4716 case 1:
4717 return;
4718 case 2:
4719 if (__comp(*--__last, *__first))
4720 swap(*__first, *__last);
4721 return;
4722 case 3:
4723 {
4724 _RandomAccessIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004725 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004726 return;
4727 }
4728 }
4729 if (__len <= __limit)
4730 {
4731 __selection_sort<_Compare>(__first, __last, __comp);
4732 return;
4733 }
4734 // __len > __limit >= 3
4735 _RandomAccessIterator __m = __first + __len/2;
4736 _RandomAccessIterator __lm1 = __last;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004737 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004738 // *__m is median
4739 // partition [__first, __m) < *__m and *__m <= [__m, __last)
4740 // (this inhibits tossing elements equivalent to __m around unnecessarily)
4741 _RandomAccessIterator __i = __first;
4742 _RandomAccessIterator __j = __lm1;
4743 // j points beyond range to be tested, *__lm1 is known to be <= *__m
4744 // The search going up is known to be guarded but the search coming down isn't.
4745 // Prime the downward search with a guard.
4746 if (!__comp(*__i, *__m)) // if *__first == *__m
4747 {
4748 // *__first == *__m, *__first doesn't go in first part
4749 // manually guard downward moving __j against __i
4750 while (true)
4751 {
4752 if (__i == --__j)
4753 {
4754 // *__first == *__m, *__m <= all other elements
4755 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
4756 ++__i; // __first + 1
4757 __j = __last;
4758 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
4759 {
4760 while (true)
4761 {
4762 if (__i == __j)
4763 return; // [__first, __last) all equivalent elements
4764 if (__comp(*__first, *__i))
4765 {
4766 swap(*__i, *__j);
4767 ++__n_swaps;
4768 ++__i;
4769 break;
4770 }
4771 ++__i;
4772 }
4773 }
4774 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
4775 if (__i == __j)
4776 return;
4777 while (true)
4778 {
4779 while (!__comp(*__first, *__i))
4780 ++__i;
4781 while (__comp(*__first, *--__j))
4782 ;
4783 if (__i >= __j)
4784 break;
4785 swap(*__i, *__j);
4786 ++__n_swaps;
4787 ++__i;
4788 }
4789 // [__first, __i) == *__first and *__first < [__i, __last)
4790 // The first part is sorted,
4791 if (__nth < __i)
4792 return;
4793 // __nth_element the secod part
4794 // __nth_element<_Compare>(__i, __nth, __last, __comp);
4795 __first = __i;
4796 goto __restart;
4797 }
4798 if (__comp(*__j, *__m))
4799 {
4800 swap(*__i, *__j);
4801 ++__n_swaps;
4802 break; // found guard for downward moving __j, now use unguarded partition
4803 }
4804 }
4805 }
4806 ++__i;
4807 // j points beyond range to be tested, *__lm1 is known to be <= *__m
4808 // if not yet partitioned...
4809 if (__i < __j)
4810 {
4811 // known that *(__i - 1) < *__m
4812 while (true)
4813 {
4814 // __m still guards upward moving __i
4815 while (__comp(*__i, *__m))
4816 ++__i;
4817 // It is now known that a guard exists for downward moving __j
4818 while (!__comp(*--__j, *__m))
4819 ;
4820 if (__i >= __j)
4821 break;
4822 swap(*__i, *__j);
4823 ++__n_swaps;
4824 // It is known that __m != __j
4825 // If __m just moved, follow it
4826 if (__m == __i)
4827 __m = __j;
4828 ++__i;
4829 }
4830 }
4831 // [__first, __i) < *__m and *__m <= [__i, __last)
4832 if (__i != __m && __comp(*__m, *__i))
4833 {
4834 swap(*__i, *__m);
4835 ++__n_swaps;
4836 }
4837 // [__first, __i) < *__i and *__i <= [__i+1, __last)
4838 if (__nth == __i)
4839 return;
4840 if (__n_swaps == 0)
4841 {
4842 // We were given a perfectly partitioned sequence. Coincidence?
4843 if (__nth < __i)
4844 {
4845 // Check for [__first, __i) already sorted
4846 __j = __m = __first;
4847 while (++__j != __i)
4848 {
4849 if (__comp(*__j, *__m))
4850 // not yet sorted, so sort
4851 goto not_sorted;
4852 __m = __j;
4853 }
4854 // [__first, __i) sorted
4855 return;
4856 }
4857 else
4858 {
4859 // Check for [__i, __last) already sorted
4860 __j = __m = __i;
4861 while (++__j != __last)
4862 {
4863 if (__comp(*__j, *__m))
4864 // not yet sorted, so sort
4865 goto not_sorted;
4866 __m = __j;
4867 }
4868 // [__i, __last) sorted
4869 return;
4870 }
4871 }
4872not_sorted:
4873 // __nth_element on range containing __nth
4874 if (__nth < __i)
4875 {
4876 // __nth_element<_Compare>(__first, __nth, __i, __comp);
4877 __last = __i;
4878 }
4879 else
4880 {
4881 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
4882 __first = ++__i;
4883 }
4884 }
4885}
4886
4887template <class _RandomAccessIterator, class _Compare>
4888inline _LIBCPP_INLINE_VISIBILITY
4889void
4890nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
4891{
Howard Hinnant7a563db2011-09-14 18:33:51 +00004892#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004893 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4894 __debug_less<_Compare> __c(__comp);
4895 __nth_element<_Comp_ref>(__first, __nth, __last, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004896#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004897 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4898 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004899#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004900}
4901
4902template <class _RandomAccessIterator>
4903inline _LIBCPP_INLINE_VISIBILITY
4904void
4905nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
4906{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004907 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004908}
4909
4910// includes
4911
4912template <class _Compare, class _InputIterator1, class _InputIterator2>
4913bool
4914__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
4915 _Compare __comp)
4916{
4917 for (; __first2 != __last2; ++__first1)
4918 {
4919 if (__first1 == __last1 || __comp(*__first2, *__first1))
4920 return false;
4921 if (!__comp(*__first1, *__first2))
4922 ++__first2;
4923 }
4924 return true;
4925}
4926
4927template <class _InputIterator1, class _InputIterator2, class _Compare>
4928inline _LIBCPP_INLINE_VISIBILITY
4929bool
4930includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
4931 _Compare __comp)
4932{
Howard Hinnant7a563db2011-09-14 18:33:51 +00004933#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004934 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4935 __debug_less<_Compare> __c(__comp);
4936 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004937#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004938 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4939 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004940#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004941}
4942
4943template <class _InputIterator1, class _InputIterator2>
4944inline _LIBCPP_INLINE_VISIBILITY
4945bool
4946includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
4947{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004948 return _VSTD::includes(__first1, __last1, __first2, __last2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004949 __less<typename iterator_traits<_InputIterator1>::value_type,
4950 typename iterator_traits<_InputIterator2>::value_type>());
4951}
4952
4953// set_union
4954
4955template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4956_OutputIterator
4957__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
4958 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4959{
4960 for (; __first1 != __last1; ++__result)
4961 {
4962 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004963 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004964 if (__comp(*__first2, *__first1))
4965 {
4966 *__result = *__first2;
4967 ++__first2;
4968 }
4969 else
4970 {
4971 *__result = *__first1;
4972 if (!__comp(*__first1, *__first2))
4973 ++__first2;
4974 ++__first1;
4975 }
4976 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00004977 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004978}
4979
4980template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4981inline _LIBCPP_INLINE_VISIBILITY
4982_OutputIterator
4983set_union(_InputIterator1 __first1, _InputIterator1 __last1,
4984 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4985{
Howard Hinnant7a563db2011-09-14 18:33:51 +00004986#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004987 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4988 __debug_less<_Compare> __c(__comp);
4989 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004990#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004991 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4992 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004993#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004994}
4995
4996template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4997inline _LIBCPP_INLINE_VISIBILITY
4998_OutputIterator
4999set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5000 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5001{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005002 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005003 __less<typename iterator_traits<_InputIterator1>::value_type,
5004 typename iterator_traits<_InputIterator2>::value_type>());
5005}
5006
5007// set_intersection
5008
5009template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5010_OutputIterator
5011__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5012 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5013{
5014 while (__first1 != __last1 && __first2 != __last2)
5015 {
5016 if (__comp(*__first1, *__first2))
5017 ++__first1;
5018 else
5019 {
5020 if (!__comp(*__first2, *__first1))
5021 {
5022 *__result = *__first1;
5023 ++__result;
5024 ++__first1;
5025 }
5026 ++__first2;
5027 }
5028 }
5029 return __result;
5030}
5031
5032template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5033inline _LIBCPP_INLINE_VISIBILITY
5034_OutputIterator
5035set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5036 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5037{
Howard Hinnant7a563db2011-09-14 18:33:51 +00005038#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005039 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5040 __debug_less<_Compare> __c(__comp);
5041 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005042#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005043 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5044 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005045#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005046}
5047
5048template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5049inline _LIBCPP_INLINE_VISIBILITY
5050_OutputIterator
5051set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5052 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5053{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005054 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005055 __less<typename iterator_traits<_InputIterator1>::value_type,
5056 typename iterator_traits<_InputIterator2>::value_type>());
5057}
5058
5059// set_difference
5060
5061template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5062_OutputIterator
5063__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5064 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5065{
5066 while (__first1 != __last1)
5067 {
5068 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005069 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005070 if (__comp(*__first1, *__first2))
5071 {
5072 *__result = *__first1;
5073 ++__result;
5074 ++__first1;
5075 }
5076 else
5077 {
5078 if (!__comp(*__first2, *__first1))
5079 ++__first1;
5080 ++__first2;
5081 }
5082 }
5083 return __result;
5084}
5085
5086template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5087inline _LIBCPP_INLINE_VISIBILITY
5088_OutputIterator
5089set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5090 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5091{
Howard Hinnant7a563db2011-09-14 18:33:51 +00005092#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005093 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5094 __debug_less<_Compare> __c(__comp);
5095 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005096#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005097 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5098 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005099#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005100}
5101
5102template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5103inline _LIBCPP_INLINE_VISIBILITY
5104_OutputIterator
5105set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5106 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5107{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005108 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005109 __less<typename iterator_traits<_InputIterator1>::value_type,
5110 typename iterator_traits<_InputIterator2>::value_type>());
5111}
5112
5113// set_symmetric_difference
5114
5115template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5116_OutputIterator
5117__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5118 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5119{
5120 while (__first1 != __last1)
5121 {
5122 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005123 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005124 if (__comp(*__first1, *__first2))
5125 {
5126 *__result = *__first1;
5127 ++__result;
5128 ++__first1;
5129 }
5130 else
5131 {
5132 if (__comp(*__first2, *__first1))
5133 {
5134 *__result = *__first2;
5135 ++__result;
5136 }
5137 else
5138 ++__first1;
5139 ++__first2;
5140 }
5141 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00005142 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005143}
5144
5145template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5146inline _LIBCPP_INLINE_VISIBILITY
5147_OutputIterator
5148set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5149 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5150{
Howard Hinnant7a563db2011-09-14 18:33:51 +00005151#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005152 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5153 __debug_less<_Compare> __c(__comp);
5154 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005155#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005156 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5157 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005158#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005159}
5160
5161template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5162inline _LIBCPP_INLINE_VISIBILITY
5163_OutputIterator
5164set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5165 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5166{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005167 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005168 __less<typename iterator_traits<_InputIterator1>::value_type,
5169 typename iterator_traits<_InputIterator2>::value_type>());
5170}
5171
5172// lexicographical_compare
5173
5174template <class _Compare, class _InputIterator1, class _InputIterator2>
5175bool
5176__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5177 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5178{
5179 for (; __first2 != __last2; ++__first1, ++__first2)
5180 {
5181 if (__first1 == __last1 || __comp(*__first1, *__first2))
5182 return true;
5183 if (__comp(*__first2, *__first1))
5184 return false;
5185 }
5186 return false;
5187}
5188
5189template <class _InputIterator1, class _InputIterator2, class _Compare>
5190inline _LIBCPP_INLINE_VISIBILITY
5191bool
5192lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5193 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5194{
Howard Hinnant7a563db2011-09-14 18:33:51 +00005195#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005196 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5197 __debug_less<_Compare> __c(__comp);
5198 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005199#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005200 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5201 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005202#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005203}
5204
5205template <class _InputIterator1, class _InputIterator2>
5206inline _LIBCPP_INLINE_VISIBILITY
5207bool
5208lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5209 _InputIterator2 __first2, _InputIterator2 __last2)
5210{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005211 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005212 __less<typename iterator_traits<_InputIterator1>::value_type,
5213 typename iterator_traits<_InputIterator2>::value_type>());
5214}
5215
5216// next_permutation
5217
5218template <class _Compare, class _BidirectionalIterator>
5219bool
5220__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5221{
5222 _BidirectionalIterator __i = __last;
5223 if (__first == __last || __first == --__i)
5224 return false;
5225 while (true)
5226 {
5227 _BidirectionalIterator __ip1 = __i;
5228 if (__comp(*--__i, *__ip1))
5229 {
5230 _BidirectionalIterator __j = __last;
5231 while (!__comp(*__i, *--__j))
5232 ;
5233 swap(*__i, *__j);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005234 _VSTD::reverse(__ip1, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005235 return true;
5236 }
5237 if (__i == __first)
5238 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005239 _VSTD::reverse(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005240 return false;
5241 }
5242 }
5243}
5244
5245template <class _BidirectionalIterator, class _Compare>
5246inline _LIBCPP_INLINE_VISIBILITY
5247bool
5248next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5249{
Howard Hinnant7a563db2011-09-14 18:33:51 +00005250#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005251 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5252 __debug_less<_Compare> __c(__comp);
5253 return __next_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005254#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005255 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5256 return __next_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005257#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005258}
5259
5260template <class _BidirectionalIterator>
5261inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005262bool
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005263next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5264{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005265 return _VSTD::next_permutation(__first, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005266 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5267}
5268
5269// prev_permutation
5270
5271template <class _Compare, class _BidirectionalIterator>
5272bool
5273__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5274{
5275 _BidirectionalIterator __i = __last;
5276 if (__first == __last || __first == --__i)
5277 return false;
5278 while (true)
5279 {
5280 _BidirectionalIterator __ip1 = __i;
5281 if (__comp(*__ip1, *--__i))
5282 {
5283 _BidirectionalIterator __j = __last;
5284 while (!__comp(*--__j, *__i))
5285 ;
5286 swap(*__i, *__j);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005287 _VSTD::reverse(__ip1, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005288 return true;
5289 }
5290 if (__i == __first)
5291 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005292 _VSTD::reverse(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005293 return false;
5294 }
5295 }
5296}
5297
5298template <class _BidirectionalIterator, class _Compare>
5299inline _LIBCPP_INLINE_VISIBILITY
5300bool
5301prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5302{
Howard Hinnant7a563db2011-09-14 18:33:51 +00005303#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005304 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5305 __debug_less<_Compare> __c(__comp);
5306 return __prev_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005307#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005308 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5309 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005310#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005311}
5312
5313template <class _BidirectionalIterator>
5314inline _LIBCPP_INLINE_VISIBILITY
5315bool
5316prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5317{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005318 return _VSTD::prev_permutation(__first, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005319 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5320}
5321
5322template <class _Tp>
5323inline _LIBCPP_INLINE_VISIBILITY
5324typename enable_if
5325<
5326 is_integral<_Tp>::value,
5327 _Tp
5328>::type
5329__rotate_left(_Tp __t, _Tp __n = 1)
5330{
5331 const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5332 __n &= __bits;
5333 return static_cast<_Tp>((__t << __n) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> (__bits - __n)));
5334}
5335
5336template <class _Tp>
5337inline _LIBCPP_INLINE_VISIBILITY
5338typename enable_if
5339<
5340 is_integral<_Tp>::value,
5341 _Tp
5342>::type
5343__rotate_right(_Tp __t, _Tp __n = 1)
5344{
5345 const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5346 __n &= __bits;
5347 return static_cast<_Tp>((__t << (__bits - __n)) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> __n));
5348}
5349
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005350_LIBCPP_END_NAMESPACE_STD
5351
5352#endif // _LIBCPP_ALGORITHM