blob: 3a47b8d6c38ce8cb855d56d21de71fe5854490ae [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
Howard Hinnant08e17472011-10-17 20:05:10 +0000598#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000599#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000600#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000601
602_LIBCPP_BEGIN_NAMESPACE_STD
603
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000604template <class _T1, class _T2 = _T1>
605struct __equal_to
606{
607 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
608 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
609 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
610 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
611};
612
613template <class _T1>
614struct __equal_to<_T1, _T1>
615{
616 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
617};
618
619template <class _T1>
620struct __equal_to<const _T1, _T1>
621{
622 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
623};
624
625template <class _T1>
626struct __equal_to<_T1, const _T1>
627{
628 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
629};
630
631template <class _T1, class _T2 = _T1>
632struct __less
633{
634 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
635 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
636 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
637 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
638};
639
640template <class _T1>
641struct __less<_T1, _T1>
642{
643 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
644};
645
646template <class _T1>
647struct __less<const _T1, _T1>
648{
649 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
650};
651
652template <class _T1>
653struct __less<_T1, const _T1>
654{
655 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
656};
657
658template <class _Predicate>
659class __negate
660{
661private:
662 _Predicate __p_;
663public:
664 _LIBCPP_INLINE_VISIBILITY __negate() {}
665
666 _LIBCPP_INLINE_VISIBILITY
667 explicit __negate(_Predicate __p) : __p_(__p) {}
668
669 template <class _T1>
670 _LIBCPP_INLINE_VISIBILITY
671 bool operator()(const _T1& __x) {return !__p_(__x);}
672
673 template <class _T1, class _T2>
674 _LIBCPP_INLINE_VISIBILITY
675 bool operator()(const _T1& __x, const _T2& __y) {return !__p_(__x, __y);}
676};
677
Howard Hinnant7a563db2011-09-14 18:33:51 +0000678#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000679
680template <class _Compare>
681struct __debug_less
682{
683 _Compare __comp_;
684 __debug_less(_Compare& __c) : __comp_(__c) {}
685 template <class _Tp, class _Up>
686 bool operator()(const _Tp& __x, const _Up& __y)
687 {
688 bool __r = __comp_(__x, __y);
689 if (__r)
Howard Hinnant7a563db2011-09-14 18:33:51 +0000690 _LIBCPP_ASSERT(!__comp_(__y, __x), "Comparator does not induce a strict weak ordering");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000691 return __r;
692 }
693};
694
Howard Hinnant7a563db2011-09-14 18:33:51 +0000695#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000696
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000697// Precondition: __x != 0
698inline _LIBCPP_INLINE_VISIBILITY unsigned __ctz(unsigned __x) {return __builtin_ctz (__x);}
699inline _LIBCPP_INLINE_VISIBILITY unsigned long __ctz(unsigned long __x) {return __builtin_ctzl (__x);}
700inline _LIBCPP_INLINE_VISIBILITY unsigned long long __ctz(unsigned long long __x) {return __builtin_ctzll(__x);}
701
702// Precondition: __x != 0
703inline _LIBCPP_INLINE_VISIBILITY unsigned __clz(unsigned __x) {return __builtin_clz (__x);}
704inline _LIBCPP_INLINE_VISIBILITY unsigned long __clz(unsigned long __x) {return __builtin_clzl (__x);}
705inline _LIBCPP_INLINE_VISIBILITY unsigned long long __clz(unsigned long long __x) {return __builtin_clzll(__x);}
706
707inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) {return __builtin_popcount (__x);}
708inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) {return __builtin_popcountl (__x);}
709inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {return __builtin_popcountll(__x);}
710
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000711// all_of
712
713template <class _InputIterator, class _Predicate>
714inline _LIBCPP_INLINE_VISIBILITY
715bool
716all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
717{
718 for (; __first != __last; ++__first)
719 if (!__pred(*__first))
720 return false;
721 return true;
722}
723
724// any_of
725
726template <class _InputIterator, class _Predicate>
727inline _LIBCPP_INLINE_VISIBILITY
728bool
729any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
730{
731 for (; __first != __last; ++__first)
732 if (__pred(*__first))
733 return true;
734 return false;
735}
736
737// none_of
738
739template <class _InputIterator, class _Predicate>
740inline _LIBCPP_INLINE_VISIBILITY
741bool
742none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
743{
744 for (; __first != __last; ++__first)
745 if (__pred(*__first))
746 return false;
747 return true;
748}
749
750// for_each
751
752template <class _InputIterator, class _Function>
753inline _LIBCPP_INLINE_VISIBILITY
754_Function
755for_each(_InputIterator __first, _InputIterator __last, _Function __f)
756{
757 for (; __first != __last; ++__first)
758 __f(*__first);
Howard Hinnant0949eed2011-06-30 21:18:19 +0000759 return _VSTD::move(__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000760}
761
762// find
763
764template <class _InputIterator, class _Tp>
765inline _LIBCPP_INLINE_VISIBILITY
766_InputIterator
767find(_InputIterator __first, _InputIterator __last, const _Tp& __value)
768{
769 for (; __first != __last; ++__first)
770 if (*__first == __value)
771 break;
772 return __first;
773}
774
775// find_if
776
777template <class _InputIterator, class _Predicate>
778inline _LIBCPP_INLINE_VISIBILITY
779_InputIterator
780find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
781{
782 for (; __first != __last; ++__first)
783 if (__pred(*__first))
784 break;
785 return __first;
786}
787
788// find_if_not
789
790template<class _InputIterator, class _Predicate>
791inline _LIBCPP_INLINE_VISIBILITY
792_InputIterator
793find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
794{
795 for (; __first != __last; ++__first)
796 if (!__pred(*__first))
797 break;
798 return __first;
799}
800
801// find_end
802
803template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
804_ForwardIterator1
805__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
806 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
807 forward_iterator_tag, forward_iterator_tag)
808{
809 // modeled after search algorithm
810 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
811 if (__first2 == __last2)
812 return __r;
813 while (true)
814 {
815 while (true)
816 {
817 if (__first1 == __last1) // if source exhausted return last correct answer
818 return __r; // (or __last1 if never found)
819 if (__pred(*__first1, *__first2))
820 break;
821 ++__first1;
822 }
823 // *__first1 matches *__first2, now match elements after here
824 _ForwardIterator1 __m1 = __first1;
825 _ForwardIterator2 __m2 = __first2;
826 while (true)
827 {
828 if (++__m2 == __last2)
829 { // Pattern exhaused, record answer and search for another one
830 __r = __first1;
831 ++__first1;
832 break;
833 }
834 if (++__m1 == __last1) // Source exhausted, return last answer
835 return __r;
836 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
837 {
838 ++__first1;
839 break;
840 } // else there is a match, check next elements
841 }
842 }
843}
844
845template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
846_BidirectionalIterator1
847__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
848 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
849 bidirectional_iterator_tag, bidirectional_iterator_tag)
850{
851 // modeled after search algorithm (in reverse)
852 if (__first2 == __last2)
853 return __last1; // Everything matches an empty sequence
854 _BidirectionalIterator1 __l1 = __last1;
855 _BidirectionalIterator2 __l2 = __last2;
856 --__l2;
857 while (true)
858 {
859 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
860 while (true)
861 {
862 if (__first1 == __l1) // return __last1 if no element matches *__first2
863 return __last1;
864 if (__pred(*--__l1, *__l2))
865 break;
866 }
867 // *__l1 matches *__l2, now match elements before here
868 _BidirectionalIterator1 __m1 = __l1;
869 _BidirectionalIterator2 __m2 = __l2;
870 while (true)
871 {
872 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
873 return __m1;
874 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
875 return __last1;
876 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
877 {
878 break;
879 } // else there is a match, check next elements
880 }
881 }
882}
883
884template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
885_RandomAccessIterator1
886__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
887 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
888 random_access_iterator_tag, random_access_iterator_tag)
889{
890 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
891 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
892 if (__len2 == 0)
893 return __last1;
894 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
895 if (__len1 < __len2)
896 return __last1;
897 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
898 _RandomAccessIterator1 __l1 = __last1;
899 _RandomAccessIterator2 __l2 = __last2;
900 --__l2;
901 while (true)
902 {
903 while (true)
904 {
905 if (__s == __l1)
906 return __last1;
907 if (__pred(*--__l1, *__l2))
908 break;
909 }
910 _RandomAccessIterator1 __m1 = __l1;
911 _RandomAccessIterator2 __m2 = __l2;
912 while (true)
913 {
914 if (__m2 == __first2)
915 return __m1;
916 // no need to check range on __m1 because __s guarantees we have enough source
917 if (!__pred(*--__m1, *--__m2))
918 {
919 break;
920 }
921 }
922 }
923}
924
925template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
926inline _LIBCPP_INLINE_VISIBILITY
927_ForwardIterator1
928find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
929 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
930{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000931 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000932 (__first1, __last1, __first2, __last2, __pred,
933 typename iterator_traits<_ForwardIterator1>::iterator_category(),
934 typename iterator_traits<_ForwardIterator2>::iterator_category());
935}
936
937template <class _ForwardIterator1, class _ForwardIterator2>
938inline _LIBCPP_INLINE_VISIBILITY
939_ForwardIterator1
940find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
941 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
942{
943 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
944 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000945 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000946}
947
948// find_first_of
949
950template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
951_ForwardIterator1
952find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
953 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
954{
955 for (; __first1 != __last1; ++__first1)
956 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
957 if (__pred(*__first1, *__j))
958 return __first1;
959 return __last1;
960}
961
962template <class _ForwardIterator1, class _ForwardIterator2>
963inline _LIBCPP_INLINE_VISIBILITY
964_ForwardIterator1
965find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
966 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
967{
968 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
969 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000970 return _VSTD::find_first_of(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000971}
972
973// adjacent_find
974
975template <class _ForwardIterator, class _BinaryPredicate>
976inline _LIBCPP_INLINE_VISIBILITY
977_ForwardIterator
978adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
979{
980 if (__first != __last)
981 {
982 _ForwardIterator __i = __first;
983 while (++__i != __last)
984 {
985 if (__pred(*__first, *__i))
986 return __first;
987 __first = __i;
988 }
989 }
990 return __last;
991}
992
993template <class _ForwardIterator>
994inline _LIBCPP_INLINE_VISIBILITY
995_ForwardIterator
996adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
997{
998 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000999 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001000}
1001
1002// count
1003
1004template <class _InputIterator, class _Tp>
1005inline _LIBCPP_INLINE_VISIBILITY
1006typename iterator_traits<_InputIterator>::difference_type
1007count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
1008{
1009 typename iterator_traits<_InputIterator>::difference_type __r(0);
1010 for (; __first != __last; ++__first)
1011 if (*__first == __value)
1012 ++__r;
1013 return __r;
1014}
1015
1016// count_if
1017
1018template <class _InputIterator, class _Predicate>
1019inline _LIBCPP_INLINE_VISIBILITY
1020typename iterator_traits<_InputIterator>::difference_type
1021count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1022{
1023 typename iterator_traits<_InputIterator>::difference_type __r(0);
1024 for (; __first != __last; ++__first)
1025 if (__pred(*__first))
1026 ++__r;
1027 return __r;
1028}
1029
1030// mismatch
1031
1032template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1033inline _LIBCPP_INLINE_VISIBILITY
1034pair<_InputIterator1, _InputIterator2>
1035mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1036 _InputIterator2 __first2, _BinaryPredicate __pred)
1037{
1038 for (; __first1 != __last1; ++__first1, ++__first2)
1039 if (!__pred(*__first1, *__first2))
1040 break;
1041 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1042}
1043
1044template <class _InputIterator1, class _InputIterator2>
1045inline _LIBCPP_INLINE_VISIBILITY
1046pair<_InputIterator1, _InputIterator2>
1047mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1048{
1049 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1050 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001051 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001052}
1053
1054// equal
1055
1056template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1057inline _LIBCPP_INLINE_VISIBILITY
1058bool
1059equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1060{
1061 for (; __first1 != __last1; ++__first1, ++__first2)
1062 if (!__pred(*__first1, *__first2))
1063 return false;
1064 return true;
1065}
1066
1067template <class _InputIterator1, class _InputIterator2>
1068inline _LIBCPP_INLINE_VISIBILITY
1069bool
1070equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1071{
1072 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1073 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001074 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001075}
1076
1077// is_permutation
1078
1079template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1080bool
1081is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1082 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1083{
1084 // shorten sequences as much as possible by lopping of any equal parts
1085 for (; __first1 != __last1; ++__first1, ++__first2)
1086 if (!__pred(*__first1, *__first2))
1087 goto __not_done;
1088 return true;
1089__not_done:
1090 // __first1 != __last1 && *__first1 != *__first2
1091 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001092 _D1 __l1 = _VSTD::distance(__first1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001093 if (__l1 == _D1(1))
1094 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001095 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001096 // For each element in [f1, l1) see if there are the same number of
1097 // equal elements in [f2, l2)
1098 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1099 {
1100 // Have we already counted the number of *__i in [f1, l1)?
1101 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1102 if (__pred(*__j, *__i))
1103 goto __next_iter;
1104 {
1105 // Count number of *__i in [f2, l2)
1106 _D1 __c2 = 0;
1107 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1108 if (__pred(*__i, *__j))
1109 ++__c2;
1110 if (__c2 == 0)
1111 return false;
1112 // Count number of *__i in [__i, l1) (we can start with 1)
1113 _D1 __c1 = 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001114 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001115 if (__pred(*__i, *__j))
1116 ++__c1;
1117 if (__c1 != __c2)
1118 return false;
1119 }
1120__next_iter:;
1121 }
1122 return true;
1123}
1124
1125template<class _ForwardIterator1, class _ForwardIterator2>
1126inline _LIBCPP_INLINE_VISIBILITY
1127bool
1128is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1129 _ForwardIterator2 __first2)
1130{
1131 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1132 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001133 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001134}
1135
1136// search
1137
1138template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1139_ForwardIterator1
1140__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1141 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
1142 forward_iterator_tag, forward_iterator_tag)
1143{
1144 if (__first2 == __last2)
1145 return __first1; // Everything matches an empty sequence
1146 while (true)
1147 {
1148 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
1149 while (true)
1150 {
1151 if (__first1 == __last1) // return __last1 if no element matches *__first2
1152 return __last1;
1153 if (__pred(*__first1, *__first2))
1154 break;
1155 ++__first1;
1156 }
1157 // *__first1 matches *__first2, now match elements after here
1158 _ForwardIterator1 __m1 = __first1;
1159 _ForwardIterator2 __m2 = __first2;
1160 while (true)
1161 {
1162 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
1163 return __first1;
1164 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
1165 return __last1;
1166 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
1167 {
1168 ++__first1;
1169 break;
1170 } // else there is a match, check next elements
1171 }
1172 }
1173}
1174
1175template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1176_RandomAccessIterator1
1177__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1178 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1179 random_access_iterator_tag, random_access_iterator_tag)
1180{
1181 typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _D1;
1182 typedef typename std::iterator_traits<_RandomAccessIterator2>::difference_type _D2;
1183 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1184 _D2 __len2 = __last2 - __first2;
1185 if (__len2 == 0)
1186 return __first1;
1187 _D1 __len1 = __last1 - __first1;
1188 if (__len1 < __len2)
1189 return __last1;
1190 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
1191 while (true)
1192 {
1193#if !_LIBCPP_UNROLL_LOOPS
1194 while (true)
1195 {
1196 if (__first1 == __s)
1197 return __last1;
1198 if (__pred(*__first1, *__first2))
1199 break;
1200 ++__first1;
1201 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001202#else // !_LIBCPP_UNROLL_LOOPS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001203 for (_D1 __loop_unroll = (__s - __first1) / 4; __loop_unroll > 0; --__loop_unroll)
1204 {
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 if (__pred(*++__first1, *__first2))
1212 goto __phase2;
1213 ++__first1;
1214 }
1215 switch (__s - __first1)
1216 {
1217 case 3:
1218 if (__pred(*__first1, *__first2))
1219 break;
1220 ++__first1;
1221 case 2:
1222 if (__pred(*__first1, *__first2))
1223 break;
1224 ++__first1;
1225 case 1:
1226 if (__pred(*__first1, *__first2))
1227 break;
1228 case 0:
1229 return __last1;
1230 }
1231 __phase2:
Howard Hinnant324bb032010-08-22 00:02:43 +00001232#endif // !_LIBCPP_UNROLL_LOOPS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001233 _RandomAccessIterator1 __m1 = __first1;
1234 _RandomAccessIterator2 __m2 = __first2;
1235#if !_LIBCPP_UNROLL_LOOPS
1236 while (true)
1237 {
1238 if (++__m2 == __last2)
1239 return __first1;
1240 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
1241 if (!__pred(*__m1, *__m2))
1242 {
1243 ++__first1;
1244 break;
1245 }
1246 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001247#else // !_LIBCPP_UNROLL_LOOPS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001248 ++__m2;
1249 ++__m1;
1250 for (_D2 __loop_unroll = (__last2 - __m2) / 4; __loop_unroll > 0; --__loop_unroll)
1251 {
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 if (!__pred(*++__m1, *++__m2))
1259 goto __continue;
1260 ++__m1;
1261 ++__m2;
1262 }
1263 switch (__last2 - __m2)
1264 {
1265 case 3:
1266 if (!__pred(*__m1, *__m2))
1267 break;
1268 ++__m1;
1269 ++__m2;
1270 case 2:
1271 if (!__pred(*__m1, *__m2))
1272 break;
1273 ++__m1;
1274 ++__m2;
1275 case 1:
1276 if (!__pred(*__m1, *__m2))
1277 break;
1278 case 0:
1279 return __first1;
1280 }
1281 __continue:
1282 ++__first1;
Howard Hinnant324bb032010-08-22 00:02:43 +00001283#endif // !_LIBCPP_UNROLL_LOOPS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001284 }
1285}
1286
1287template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1288inline _LIBCPP_INLINE_VISIBILITY
1289_ForwardIterator1
1290search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1291 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1292{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001293 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001294 (__first1, __last1, __first2, __last2, __pred,
1295 typename std::iterator_traits<_ForwardIterator1>::iterator_category(),
1296 typename std::iterator_traits<_ForwardIterator2>::iterator_category());
1297}
1298
1299template <class _ForwardIterator1, class _ForwardIterator2>
1300inline _LIBCPP_INLINE_VISIBILITY
1301_ForwardIterator1
1302search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1303 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1304{
1305 typedef typename std::iterator_traits<_ForwardIterator1>::value_type __v1;
1306 typedef typename std::iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001307 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001308}
1309
1310// search_n
1311
1312template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
1313_ForwardIterator
1314__search_n(_ForwardIterator __first, _ForwardIterator __last,
1315 _Size __count, const _Tp& __value, _BinaryPredicate __pred, forward_iterator_tag)
1316{
1317 if (__count <= 0)
1318 return __first;
1319 while (true)
1320 {
1321 // Find first element in sequence that matchs __value, with a mininum of loop checks
1322 while (true)
1323 {
1324 if (__first == __last) // return __last if no element matches __value
1325 return __last;
1326 if (__pred(*__first, __value))
1327 break;
1328 ++__first;
1329 }
1330 // *__first matches __value, now match elements after here
1331 _ForwardIterator __m = __first;
1332 _Size __c(0);
1333 while (true)
1334 {
1335 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1336 return __first;
1337 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1338 return __last;
1339 if (!__pred(*__m, __value)) // if there is a mismatch, restart with a new __first
1340 {
1341 __first = __m;
1342 ++__first;
1343 break;
1344 } // else there is a match, check next elements
1345 }
1346 }
1347}
1348
1349template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
1350_RandomAccessIterator
1351__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
1352 _Size __count, const _Tp& __value, _BinaryPredicate __pred, random_access_iterator_tag)
1353{
1354 if (__count <= 0)
1355 return __first;
1356 _Size __len = static_cast<_Size>(__last - __first);
1357 if (__len < __count)
1358 return __last;
1359 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1360 while (true)
1361 {
1362 // Find first element in sequence that matchs __value, with a mininum of loop checks
1363 while (true)
1364 {
1365 if (__first == __s) // return __last if no element matches __value
1366 return __last;
1367 if (__pred(*__first, __value))
1368 break;
1369 ++__first;
1370 }
1371 // *__first matches __value, now match elements after here
1372 _RandomAccessIterator __m = __first;
1373 _Size __c(0);
1374 while (true)
1375 {
1376 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1377 return __first;
1378 ++__m; // no need to check range on __m because __s guarantees we have enough source
1379 if (!__pred(*__m, __value)) // if there is a mismatch, restart with a new __first
1380 {
1381 __first = __m;
1382 ++__first;
1383 break;
1384 } // else there is a match, check next elements
1385 }
1386 }
1387}
1388
1389template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
1390inline _LIBCPP_INLINE_VISIBILITY
1391_ForwardIterator
1392search_n(_ForwardIterator __first, _ForwardIterator __last,
1393 _Size __count, const _Tp& __value, _BinaryPredicate __pred)
1394{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001395 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001396 (__first, __last, __count, __value, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
1397}
1398
1399template <class _ForwardIterator, class _Size, class _Tp>
1400inline _LIBCPP_INLINE_VISIBILITY
1401_ForwardIterator
1402search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value)
1403{
1404 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001405 return _VSTD::search_n(__first, __last, __count, __value, __equal_to<__v, _Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001406}
1407
1408// copy
1409
1410template <class _Iter>
1411struct __libcpp_is_trivial_iterator
1412{
1413 static const bool value = is_pointer<_Iter>::value;
1414};
1415
1416template <class _Iter>
1417struct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
1418{
1419 static const bool value = is_pointer<_Iter>::value;
1420};
1421
1422template <class _Iter>
1423struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
1424{
1425 static const bool value = is_pointer<_Iter>::value;
1426};
1427
1428template <class _Iter>
1429inline _LIBCPP_INLINE_VISIBILITY
1430_Iter
1431__unwrap_iter(_Iter __i)
1432{
1433 return __i;
1434}
1435
1436template <class _Tp>
1437inline _LIBCPP_INLINE_VISIBILITY
1438typename enable_if
1439<
Howard Hinnant1468b662010-11-19 22:17:28 +00001440 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001441 _Tp*
1442>::type
1443__unwrap_iter(move_iterator<_Tp*> __i)
1444{
1445 return __i.base();
1446}
1447
1448template <class _Tp>
1449inline _LIBCPP_INLINE_VISIBILITY
1450typename enable_if
1451<
Howard Hinnant1468b662010-11-19 22:17:28 +00001452 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001453 _Tp*
1454>::type
1455__unwrap_iter(__wrap_iter<_Tp*> __i)
1456{
1457 return __i.base();
1458}
1459
1460template <class _InputIterator, class _OutputIterator>
1461inline _LIBCPP_INLINE_VISIBILITY
1462_OutputIterator
1463__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1464{
1465 for (; __first != __last; ++__first, ++__result)
1466 *__result = *__first;
1467 return __result;
1468}
1469
1470template <class _Tp, class _Up>
1471inline _LIBCPP_INLINE_VISIBILITY
1472typename enable_if
1473<
1474 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001475 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001476 _Up*
1477>::type
1478__copy(_Tp* __first, _Tp* __last, _Up* __result)
1479{
1480 const size_t __n = static_cast<size_t>(__last - __first);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001481 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001482 return __result + __n;
1483}
1484
1485template <class _InputIterator, class _OutputIterator>
1486inline _LIBCPP_INLINE_VISIBILITY
1487_OutputIterator
1488copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1489{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001490 return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491}
1492
1493// copy_backward
1494
1495template <class _InputIterator, class _OutputIterator>
1496inline _LIBCPP_INLINE_VISIBILITY
1497_OutputIterator
1498__copy_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1499{
1500 while (__first != __last)
1501 *--__result = *--__last;
1502 return __result;
1503}
1504
1505template <class _Tp, class _Up>
1506inline _LIBCPP_INLINE_VISIBILITY
1507typename enable_if
1508<
1509 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001510 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001511 _Up*
1512>::type
1513__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1514{
1515 const size_t __n = static_cast<size_t>(__last - __first);
1516 __result -= __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001517 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001518 return __result;
1519}
1520
1521template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1522inline _LIBCPP_INLINE_VISIBILITY
1523_BidirectionalIterator2
1524copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1525 _BidirectionalIterator2 __result)
1526{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001527 return _VSTD::__copy_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001528}
1529
1530// copy_if
1531
1532template<class _InputIterator, class _OutputIterator, class _Predicate>
1533inline _LIBCPP_INLINE_VISIBILITY
1534_OutputIterator
1535copy_if(_InputIterator __first, _InputIterator __last,
1536 _OutputIterator __result, _Predicate __pred)
1537{
1538 for (; __first != __last; ++__first)
1539 {
1540 if (__pred(*__first))
1541 {
1542 *__result = *__first;
1543 ++__result;
1544 }
1545 }
1546 return __result;
1547}
1548
1549// copy_n
1550
1551template<class _InputIterator, class _Size, class _OutputIterator>
1552inline _LIBCPP_INLINE_VISIBILITY
1553typename enable_if
1554<
1555 __is_input_iterator<_InputIterator>::value &&
1556 !__is_random_access_iterator<_InputIterator>::value,
1557 _OutputIterator
1558>::type
1559copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1560{
Howard Hinnant171869e2011-02-27 20:55:39 +00001561 if (__n > 0)
1562 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001563 *__result = *__first;
Howard Hinnant171869e2011-02-27 20:55:39 +00001564 ++__result;
1565 for (--__n; __n > 0; --__n)
1566 {
1567 ++__first;
1568 *__result = *__first;
1569 ++__result;
1570 }
1571 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001572 return __result;
1573}
1574
1575template<class _InputIterator, class _Size, class _OutputIterator>
1576inline _LIBCPP_INLINE_VISIBILITY
1577typename enable_if
1578<
1579 __is_random_access_iterator<_InputIterator>::value,
1580 _OutputIterator
1581>::type
1582copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1583{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001584 return _VSTD::copy(__first, __first + __n, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001585}
1586
1587// move
1588
1589template <class _InputIterator, class _OutputIterator>
1590inline _LIBCPP_INLINE_VISIBILITY
1591_OutputIterator
1592__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1593{
1594 for (; __first != __last; ++__first, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001595 *__result = _VSTD::move(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001596 return __result;
1597}
1598
1599template <class _Tp, class _Up>
1600inline _LIBCPP_INLINE_VISIBILITY
1601typename enable_if
1602<
1603 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001604 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001605 _Up*
1606>::type
1607__move(_Tp* __first, _Tp* __last, _Up* __result)
1608{
1609 const size_t __n = static_cast<size_t>(__last - __first);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001610 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611 return __result + __n;
1612}
1613
1614template <class _InputIterator, class _OutputIterator>
1615inline _LIBCPP_INLINE_VISIBILITY
1616_OutputIterator
1617move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1618{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001619 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001620}
1621
1622// move_backward
1623
1624template <class _InputIterator, class _OutputIterator>
1625inline _LIBCPP_INLINE_VISIBILITY
1626_OutputIterator
1627__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1628{
1629 while (__first != __last)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001630 *--__result = _VSTD::move(*--__last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001631 return __result;
1632}
1633
1634template <class _Tp, class _Up>
1635inline _LIBCPP_INLINE_VISIBILITY
1636typename enable_if
1637<
1638 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001639 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640 _Up*
1641>::type
1642__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1643{
1644 const size_t __n = static_cast<size_t>(__last - __first);
1645 __result -= __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001646 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001647 return __result;
1648}
1649
1650template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1651inline _LIBCPP_INLINE_VISIBILITY
1652_BidirectionalIterator2
1653move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1654 _BidirectionalIterator2 __result)
1655{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001656 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001657}
1658
1659// iter_swap
1660
Howard Hinnante9b2c2d2011-05-27 15:04:19 +00001661// moved to <type_traits> for better swap / noexcept support
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001662
1663// transform
1664
1665template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
1666inline _LIBCPP_INLINE_VISIBILITY
1667_OutputIterator
1668transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1669{
1670 for (; __first != __last; ++__first, ++__result)
1671 *__result = __op(*__first);
1672 return __result;
1673}
1674
1675template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
1676inline _LIBCPP_INLINE_VISIBILITY
1677_OutputIterator
1678transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1679 _OutputIterator __result, _BinaryOperation __binary_op)
1680{
1681 for (; __first1 != __last1; ++__first1, ++__first2, ++__result)
1682 *__result = __binary_op(*__first1, *__first2);
1683 return __result;
1684}
1685
1686// replace
1687
1688template <class _ForwardIterator, class _Tp>
1689inline _LIBCPP_INLINE_VISIBILITY
1690void
1691replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1692{
1693 for (; __first != __last; ++__first)
1694 if (*__first == __old_value)
1695 *__first = __new_value;
1696}
1697
1698// replace_if
1699
1700template <class _ForwardIterator, class _Predicate, class _Tp>
1701inline _LIBCPP_INLINE_VISIBILITY
1702void
1703replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1704{
1705 for (; __first != __last; ++__first)
1706 if (__pred(*__first))
1707 *__first = __new_value;
1708}
1709
1710// replace_copy
1711
1712template <class _InputIterator, class _OutputIterator, class _Tp>
1713inline _LIBCPP_INLINE_VISIBILITY
1714_OutputIterator
1715replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1716 const _Tp& __old_value, const _Tp& __new_value)
1717{
1718 for (; __first != __last; ++__first, ++__result)
1719 if (*__first == __old_value)
1720 *__result = __new_value;
1721 else
1722 *__result = *__first;
1723 return __result;
1724}
1725
1726// replace_copy_if
1727
1728template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
1729inline _LIBCPP_INLINE_VISIBILITY
1730_OutputIterator
1731replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1732 _Predicate __pred, const _Tp& __new_value)
1733{
1734 for (; __first != __last; ++__first, ++__result)
1735 if (__pred(*__first))
1736 *__result = __new_value;
1737 else
1738 *__result = *__first;
1739 return __result;
1740}
1741
1742// fill_n
1743
1744template <class _OutputIterator, class _Size, class _Tp>
1745inline _LIBCPP_INLINE_VISIBILITY
1746_OutputIterator
1747__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value, false_type)
1748{
1749 for (; __n > 0; ++__first, --__n)
1750 *__first = __value;
1751 return __first;
1752}
1753
1754template <class _OutputIterator, class _Size, class _Tp>
1755inline _LIBCPP_INLINE_VISIBILITY
1756_OutputIterator
1757__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value, true_type)
1758{
1759 if (__n > 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001760 _VSTD::memset(__first, (unsigned char)__value, (size_t)(__n));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001761 return __first + __n;
1762}
1763
1764template <class _OutputIterator, class _Size, class _Tp>
1765inline _LIBCPP_INLINE_VISIBILITY
1766_OutputIterator
1767fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
1768{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001769 return _VSTD::__fill_n(__first, __n, __value, integral_constant<bool,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001770 is_pointer<_OutputIterator>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001771 is_trivially_copy_assignable<_Tp>::value &&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001772 sizeof(_Tp) == 1>());
1773}
1774
1775// fill
1776
1777template <class _ForwardIterator, class _Tp>
1778inline _LIBCPP_INLINE_VISIBILITY
1779void
1780__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, forward_iterator_tag)
1781{
1782 for (; __first != __last; ++__first)
1783 *__first = __value;
1784}
1785
1786template <class _RandomAccessIterator, class _Tp>
1787inline _LIBCPP_INLINE_VISIBILITY
1788void
1789__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value, random_access_iterator_tag)
1790{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001791 _VSTD::fill_n(__first, __last - __first, __value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001792}
1793
1794template <class _ForwardIterator, class _Tp>
1795inline _LIBCPP_INLINE_VISIBILITY
1796void
1797fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
1798{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001799 _VSTD::__fill(__first, __last, __value, typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001800}
1801
1802// generate
1803
1804template <class _ForwardIterator, class _Generator>
1805inline _LIBCPP_INLINE_VISIBILITY
1806void
1807generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
1808{
1809 for (; __first != __last; ++__first)
1810 *__first = __gen();
1811}
1812
1813// generate_n
1814
1815template <class _OutputIterator, class _Size, class _Generator>
1816inline _LIBCPP_INLINE_VISIBILITY
1817_OutputIterator
1818generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
1819{
1820 for (; __n > 0; ++__first, --__n)
1821 *__first = __gen();
1822 return __first;
1823}
1824
1825// remove
1826
1827template <class _ForwardIterator, class _Tp>
1828_ForwardIterator
1829remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
1830{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001831 __first = _VSTD::find(__first, __last, __value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001832 if (__first != __last)
1833 {
1834 _ForwardIterator __i = __first;
1835 while (++__i != __last)
1836 {
1837 if (!(*__i == __value))
1838 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001839 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001840 ++__first;
1841 }
1842 }
1843 }
1844 return __first;
1845}
1846
1847// remove_if
1848
1849template <class _ForwardIterator, class _Predicate>
1850_ForwardIterator
1851remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
1852{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001853 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001854 (__first, __last, __pred);
1855 if (__first != __last)
1856 {
1857 _ForwardIterator __i = __first;
1858 while (++__i != __last)
1859 {
1860 if (!__pred(*__i))
1861 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001862 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001863 ++__first;
1864 }
1865 }
1866 }
1867 return __first;
1868}
1869
1870// remove_copy
1871
1872template <class _InputIterator, class _OutputIterator, class _Tp>
1873inline _LIBCPP_INLINE_VISIBILITY
1874_OutputIterator
1875remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value)
1876{
1877 for (; __first != __last; ++__first)
1878 {
1879 if (!(*__first == __value))
1880 {
1881 *__result = *__first;
1882 ++__result;
1883 }
1884 }
1885 return __result;
1886}
1887
1888// remove_copy_if
1889
1890template <class _InputIterator, class _OutputIterator, class _Predicate>
1891inline _LIBCPP_INLINE_VISIBILITY
1892_OutputIterator
1893remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
1894{
1895 for (; __first != __last; ++__first)
1896 {
1897 if (!__pred(*__first))
1898 {
1899 *__result = *__first;
1900 ++__result;
1901 }
1902 }
1903 return __result;
1904}
1905
1906// unique
1907
1908template <class _ForwardIterator, class _BinaryPredicate>
1909_ForwardIterator
1910unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1911{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001912 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001913 (__first, __last, __pred);
1914 if (__first != __last)
1915 {
1916 // ... a a ? ...
1917 // f i
1918 _ForwardIterator __i = __first;
1919 for (++__i; ++__i != __last;)
1920 if (!__pred(*__first, *__i))
Howard Hinnant0949eed2011-06-30 21:18:19 +00001921 *++__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001922 ++__first;
1923 }
1924 return __first;
1925}
1926
1927template <class _ForwardIterator>
1928inline _LIBCPP_INLINE_VISIBILITY
1929_ForwardIterator
1930unique(_ForwardIterator __first, _ForwardIterator __last)
1931{
1932 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001933 return _VSTD::unique(__first, __last, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001934}
1935
1936// unique_copy
1937
1938template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
1939_OutputIterator
1940__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
1941 input_iterator_tag, output_iterator_tag)
1942{
1943 if (__first != __last)
1944 {
1945 typename iterator_traits<_InputIterator>::value_type __t(*__first);
1946 *__result = __t;
1947 ++__result;
1948 while (++__first != __last)
1949 {
1950 if (!__pred(__t, *__first))
1951 {
1952 __t = *__first;
1953 *__result = __t;
1954 ++__result;
1955 }
1956 }
1957 }
1958 return __result;
1959}
1960
1961template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
1962_OutputIterator
1963__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
1964 forward_iterator_tag, output_iterator_tag)
1965{
1966 if (__first != __last)
1967 {
1968 _ForwardIterator __i = __first;
1969 *__result = *__i;
1970 ++__result;
1971 while (++__first != __last)
1972 {
1973 if (!__pred(*__i, *__first))
1974 {
1975 *__result = *__first;
1976 ++__result;
1977 __i = __first;
1978 }
1979 }
1980 }
1981 return __result;
1982}
1983
1984template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
1985_ForwardIterator
1986__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
1987 input_iterator_tag, forward_iterator_tag)
1988{
1989 if (__first != __last)
1990 {
1991 *__result = *__first;
1992 while (++__first != __last)
1993 if (!__pred(*__result, *__first))
1994 *++__result = *__first;
1995 ++__result;
1996 }
1997 return __result;
1998}
1999
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002000template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
2001inline _LIBCPP_INLINE_VISIBILITY
2002_OutputIterator
2003unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2004{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002005 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002006 (__first, __last, __result, __pred,
2007 typename iterator_traits<_InputIterator>::iterator_category(),
2008 typename iterator_traits<_OutputIterator>::iterator_category());
2009}
2010
2011template <class _InputIterator, class _OutputIterator>
2012inline _LIBCPP_INLINE_VISIBILITY
2013_OutputIterator
2014unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2015{
2016 typedef typename iterator_traits<_InputIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002017 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002018}
2019
2020// reverse
2021
2022template <class _BidirectionalIterator>
2023inline _LIBCPP_INLINE_VISIBILITY
2024void
2025__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2026{
2027 while (__first != __last)
2028 {
2029 if (__first == --__last)
2030 break;
2031 swap(*__first, *__last);
2032 ++__first;
2033 }
2034}
2035
2036template <class _RandomAccessIterator>
2037inline _LIBCPP_INLINE_VISIBILITY
2038void
2039__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2040{
2041 if (__first != __last)
2042 for (; __first < --__last; ++__first)
2043 swap(*__first, *__last);
2044}
2045
2046template <class _BidirectionalIterator>
2047inline _LIBCPP_INLINE_VISIBILITY
2048void
2049reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2050{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002051 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002052}
2053
2054// reverse_copy
2055
2056template <class _BidirectionalIterator, class _OutputIterator>
2057inline _LIBCPP_INLINE_VISIBILITY
2058_OutputIterator
2059reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2060{
2061 for (; __first != __last; ++__result)
2062 *__result = *--__last;
2063 return __result;
2064}
2065
2066// rotate
2067
2068template <class _ForwardIterator>
2069_ForwardIterator
2070__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, false_type)
2071{
2072 if (__first == __middle)
2073 return __last;
2074 if (__middle == __last)
2075 return __first;
2076 _ForwardIterator __i = __middle;
2077 while (true)
2078 {
2079 swap(*__first, *__i);
2080 ++__first;
2081 if (++__i == __last)
2082 break;
2083 if (__first == __middle)
2084 __middle = __i;
2085 }
2086 _ForwardIterator __r = __first;
2087 if (__first != __middle)
2088 {
2089 __i = __middle;
2090 while (true)
2091 {
2092 swap(*__first, *__i);
2093 ++__first;
2094 if (++__i == __last)
2095 {
2096 if (__first == __middle)
2097 break;
2098 __i = __middle;
2099 }
2100 else if (__first == __middle)
2101 __middle = __i;
2102 }
2103 }
2104 return __r;
2105}
2106
2107template<typename _Integral>
2108inline _LIBCPP_INLINE_VISIBILITY
2109_Integral
2110__gcd(_Integral __x, _Integral __y)
2111{
2112 do
2113 {
2114 _Integral __t = __x % __y;
2115 __x = __y;
2116 __y = __t;
2117 } while (__y);
2118 return __x;
2119}
2120
2121template<typename _RandomAccessIterator>
2122_RandomAccessIterator
2123__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last, true_type)
2124{
2125 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2126 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant324bb032010-08-22 00:02:43 +00002127
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002128 if (__first == __middle)
2129 return __last;
2130 if (__middle == __last)
2131 return __first;
2132 const difference_type __m1 = __middle - __first;
2133 const difference_type __m2 = __last - __middle;
2134 if (__m1 == __m2)
2135 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002136 _VSTD::swap_ranges(__first, __middle, __middle);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002137 return __middle;
2138 }
2139 const difference_type __g = __gcd(__m1, __m2);
2140 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2141 {
2142 value_type __t(*--__p);
2143 _RandomAccessIterator __p1 = __p;
2144 _RandomAccessIterator __p2 = __p1 + __m1;
2145 do
2146 {
2147 *__p1 = *__p2;
2148 __p1 = __p2;
2149 const difference_type __d = __last - __p2;
2150 if (__m1 < __d)
2151 __p2 += __m1;
2152 else
2153 __p2 = __first + (__m1 - __d);
2154 } while (__p2 != __p);
2155 *__p1 = __t;
2156 }
2157 return __first + __m2;
2158}
2159
2160template <class _ForwardIterator>
2161inline _LIBCPP_INLINE_VISIBILITY
2162_ForwardIterator
2163rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2164{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002165 return _VSTD::__rotate(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002166 integral_constant
2167 <
2168 bool,
2169 is_convertible
2170 <
2171 typename iterator_traits<_ForwardIterator>::iterator_category,
2172 random_access_iterator_tag
2173 >::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00002174 is_trivially_copy_assignable
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002175 <
2176 typename iterator_traits<_ForwardIterator>::value_type
2177 >::value
2178 >());
2179}
2180
2181// rotate_copy
2182
2183template <class _ForwardIterator, class _OutputIterator>
2184inline _LIBCPP_INLINE_VISIBILITY
2185_OutputIterator
2186rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2187{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002188 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002189}
2190
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002191// min_element
2192
2193template <class _ForwardIterator, class _Compare>
2194inline _LIBCPP_INLINE_VISIBILITY
2195_ForwardIterator
2196min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2197{
2198 if (__first != __last)
2199 {
2200 _ForwardIterator __i = __first;
2201 while (++__i != __last)
2202 if (__comp(*__i, *__first))
2203 __first = __i;
2204 }
2205 return __first;
2206}
2207
2208template <class _ForwardIterator>
2209inline _LIBCPP_INLINE_VISIBILITY
2210_ForwardIterator
2211min_element(_ForwardIterator __first, _ForwardIterator __last)
2212{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002213 return _VSTD::min_element(__first, __last,
Howard Hinnant98e5d972010-08-21 20:10:01 +00002214 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2215}
2216
2217// min
2218
2219template <class _Tp, class _Compare>
2220inline _LIBCPP_INLINE_VISIBILITY
2221const _Tp&
2222min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2223{
2224 return __comp(__b, __a) ? __b : __a;
2225}
2226
2227template <class _Tp>
2228inline _LIBCPP_INLINE_VISIBILITY
2229const _Tp&
2230min(const _Tp& __a, const _Tp& __b)
2231{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002232 return _VSTD::min(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002233}
2234
Howard Hinnante3e32912011-08-12 21:56:02 +00002235#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2236
Howard Hinnant98e5d972010-08-21 20:10:01 +00002237template<class _Tp, class _Compare>
2238inline _LIBCPP_INLINE_VISIBILITY
2239_Tp
2240min(initializer_list<_Tp> __t, _Compare __comp)
2241{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002242 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002243}
2244
2245template<class _Tp>
2246inline _LIBCPP_INLINE_VISIBILITY
2247_Tp
2248min(initializer_list<_Tp> __t)
2249{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002250 return *_VSTD::min_element(__t.begin(), __t.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002251}
2252
Howard Hinnante3e32912011-08-12 21:56:02 +00002253#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2254
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002255// max_element
2256
2257template <class _ForwardIterator, class _Compare>
2258inline _LIBCPP_INLINE_VISIBILITY
2259_ForwardIterator
2260max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2261{
2262 if (__first != __last)
2263 {
2264 _ForwardIterator __i = __first;
2265 while (++__i != __last)
2266 if (__comp(*__first, *__i))
2267 __first = __i;
2268 }
2269 return __first;
2270}
2271
2272template <class _ForwardIterator>
2273inline _LIBCPP_INLINE_VISIBILITY
2274_ForwardIterator
2275max_element(_ForwardIterator __first, _ForwardIterator __last)
2276{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002277 return _VSTD::max_element(__first, __last,
Howard Hinnant98e5d972010-08-21 20:10:01 +00002278 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2279}
2280
2281// max
2282
2283template <class _Tp, class _Compare>
2284inline _LIBCPP_INLINE_VISIBILITY
2285const _Tp&
2286max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2287{
2288 return __comp(__a, __b) ? __b : __a;
2289}
2290
2291template <class _Tp>
2292inline _LIBCPP_INLINE_VISIBILITY
2293const _Tp&
2294max(const _Tp& __a, const _Tp& __b)
2295{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002296 return _VSTD::max(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002297}
2298
Howard Hinnante3e32912011-08-12 21:56:02 +00002299#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2300
Howard Hinnant98e5d972010-08-21 20:10:01 +00002301template<class _Tp, class _Compare>
2302inline _LIBCPP_INLINE_VISIBILITY
2303_Tp
2304max(initializer_list<_Tp> __t, _Compare __comp)
2305{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002306 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002307}
2308
2309template<class _Tp>
2310inline _LIBCPP_INLINE_VISIBILITY
2311_Tp
2312max(initializer_list<_Tp> __t)
2313{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002314 return *_VSTD::max_element(__t.begin(), __t.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002315}
2316
Howard Hinnante3e32912011-08-12 21:56:02 +00002317#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2318
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002319// minmax_element
2320
2321template <class _ForwardIterator, class _Compare>
2322std::pair<_ForwardIterator, _ForwardIterator>
2323minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2324{
2325 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2326 if (__first != __last)
2327 {
2328 if (++__first != __last)
2329 {
2330 if (__comp(*__first, *__result.first))
2331 {
2332 __result.second = __result.first;
2333 __result.first = __first;
2334 }
2335 else
2336 __result.second = __first;
2337 while (++__first != __last)
2338 {
2339 _ForwardIterator __i = __first;
2340 if (++__first == __last)
2341 {
2342 if (__comp(*__i, *__result.first))
2343 __result.first = __i;
2344 else if (!__comp(*__i, *__result.second))
2345 __result.second = __i;
2346 break;
2347 }
2348 else
2349 {
2350 if (__comp(*__first, *__i))
2351 {
2352 if (__comp(*__first, *__result.first))
2353 __result.first = __first;
2354 if (!__comp(*__i, *__result.second))
2355 __result.second = __i;
2356 }
2357 else
2358 {
2359 if (__comp(*__i, *__result.first))
2360 __result.first = __i;
2361 if (!__comp(*__first, *__result.second))
2362 __result.second = __first;
2363 }
2364 }
2365 }
2366 }
2367 }
2368 return __result;
2369}
2370
2371template <class _ForwardIterator>
2372inline _LIBCPP_INLINE_VISIBILITY
2373std::pair<_ForwardIterator, _ForwardIterator>
2374minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2375{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002376 return _VSTD::minmax_element(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002377}
2378
Howard Hinnant98e5d972010-08-21 20:10:01 +00002379// minmax
2380
2381template<class _Tp, class _Compare>
2382inline _LIBCPP_INLINE_VISIBILITY
2383pair<const _Tp&, const _Tp&>
2384minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2385{
2386 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2387 pair<const _Tp&, const _Tp&>(__a, __b);
2388}
2389
2390template<class _Tp>
2391inline _LIBCPP_INLINE_VISIBILITY
2392pair<const _Tp&, const _Tp&>
2393minmax(const _Tp& __a, const _Tp& __b)
2394{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002395 return _VSTD::minmax(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002396}
2397
Howard Hinnante3e32912011-08-12 21:56:02 +00002398#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2399
Howard Hinnant98e5d972010-08-21 20:10:01 +00002400template<class _Tp>
2401inline _LIBCPP_INLINE_VISIBILITY
2402pair<_Tp, _Tp>
2403minmax(initializer_list<_Tp> __t)
2404{
2405 pair<const _Tp*, const _Tp*> __p =
Howard Hinnant0949eed2011-06-30 21:18:19 +00002406 _VSTD::minmax_element(__t.begin(), __t.end());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002407 return pair<_Tp, _Tp>(*__p.first, *__p.second);
2408}
2409
2410template<class _Tp, class _Compare>
2411inline _LIBCPP_INLINE_VISIBILITY
2412pair<_Tp, _Tp>
2413minmax(initializer_list<_Tp> __t, _Compare __comp)
2414{
2415 pair<const _Tp*, const _Tp*> __p =
Howard Hinnant0949eed2011-06-30 21:18:19 +00002416 _VSTD::minmax_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002417 return pair<_Tp, _Tp>(*__p.first, *__p.second);
2418}
2419
Howard Hinnante3e32912011-08-12 21:56:02 +00002420#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2421
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002422// random_shuffle
2423
Howard Hinnantc3267212010-05-26 17:49:34 +00002424// __independent_bits_engine
2425
2426template <unsigned long long _X, size_t _R>
2427struct __log2_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002428{
Howard Hinnantc3267212010-05-26 17:49:34 +00002429 static const size_t value = _X & ((unsigned long long)(1) << _R) ? _R
2430 : __log2_imp<_X, _R - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002431};
2432
Howard Hinnantc3267212010-05-26 17:49:34 +00002433template <unsigned long long _X>
2434struct __log2_imp<_X, 0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002435{
Howard Hinnantc3267212010-05-26 17:49:34 +00002436 static const size_t value = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002437};
2438
Howard Hinnantc3267212010-05-26 17:49:34 +00002439template <size_t _R>
2440struct __log2_imp<0, _R>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002441{
Howard Hinnantc3267212010-05-26 17:49:34 +00002442 static const size_t value = _R + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002443};
2444
Howard Hinnantc3267212010-05-26 17:49:34 +00002445template <class _UI, _UI _X>
2446struct __log2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002447{
Howard Hinnantc3267212010-05-26 17:49:34 +00002448 static const size_t value = __log2_imp<_X,
2449 sizeof(_UI) * __CHAR_BIT__ - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002450};
2451
Howard Hinnantc3267212010-05-26 17:49:34 +00002452template<class _Engine, class _UIntType>
2453class __independent_bits_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002454{
Howard Hinnantc3267212010-05-26 17:49:34 +00002455public:
2456 // types
2457 typedef _UIntType result_type;
2458
2459private:
2460 typedef typename _Engine::result_type _Engine_result_type;
2461 typedef typename conditional
2462 <
2463 sizeof(_Engine_result_type) <= sizeof(result_type),
2464 result_type,
2465 _Engine_result_type
2466 >::type _Working_result_type;
2467
2468 _Engine& __e_;
2469 size_t __w_;
2470 size_t __w0_;
2471 size_t __n_;
2472 size_t __n0_;
2473 _Working_result_type __y0_;
2474 _Working_result_type __y1_;
2475 _Engine_result_type __mask0_;
2476 _Engine_result_type __mask1_;
2477
2478 static const _Working_result_type _R = _Engine::_Max - _Engine::_Min
2479 + _Working_result_type(1);
2480 static const size_t __m = __log2<_Working_result_type, _R>::value;
2481 static const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2482 static const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
2483
2484public:
2485 // constructors and seeding functions
2486 __independent_bits_engine(_Engine& __e, size_t __w);
2487
2488 // generating functions
2489 result_type operator()() {return __eval(integral_constant<bool, _R != 0>());}
2490
2491private:
2492 result_type __eval(false_type);
2493 result_type __eval(true_type);
2494};
2495
2496template<class _Engine, class _UIntType>
2497__independent_bits_engine<_Engine, _UIntType>
2498 ::__independent_bits_engine(_Engine& __e, size_t __w)
2499 : __e_(__e),
2500 __w_(__w)
2501{
2502 __n_ = __w_ / __m + (__w_ % __m != 0);
2503 __w0_ = __w_ / __n_;
2504 if (_R == 0)
2505 __y0_ = _R;
2506 else if (__w0_ < _WDt)
2507 __y0_ = (_R >> __w0_) << __w0_;
2508 else
2509 __y0_ = 0;
2510 if (_R - __y0_ > __y0_ / __n_)
2511 {
2512 ++__n_;
2513 __w0_ = __w_ / __n_;
2514 if (__w0_ < _WDt)
2515 __y0_ = (_R >> __w0_) << __w0_;
2516 else
2517 __y0_ = 0;
2518 }
2519 __n0_ = __n_ - __w_ % __n_;
2520 if (__w0_ < _WDt - 1)
2521 __y1_ = (_R >> (__w0_ + 1)) << (__w0_ + 1);
2522 else
2523 __y1_ = 0;
2524 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2525 _Engine_result_type(0);
2526 __mask1_ = __w0_ < _EDt - 1 ?
2527 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2528 _Engine_result_type(~0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002529}
2530
Howard Hinnantc3267212010-05-26 17:49:34 +00002531template<class _Engine, class _UIntType>
2532inline
2533_UIntType
2534__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002535{
Howard Hinnantc3267212010-05-26 17:49:34 +00002536 return static_cast<result_type>(__e_() & __mask0_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002537}
2538
Howard Hinnantc3267212010-05-26 17:49:34 +00002539template<class _Engine, class _UIntType>
2540_UIntType
2541__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002542{
Howard Hinnantc3267212010-05-26 17:49:34 +00002543 result_type _S = 0;
2544 for (size_t __k = 0; __k < __n0_; ++__k)
2545 {
2546 _Engine_result_type __u;
2547 do
2548 {
2549 __u = __e_() - _Engine::min();
2550 } while (__u >= __y0_);
2551 if (__w0_ < _EDt)
2552 _S <<= __w0_;
2553 else
2554 _S = 0;
2555 _S += __u & __mask0_;
2556 }
2557 for (size_t __k = __n0_; __k < __n_; ++__k)
2558 {
2559 _Engine_result_type __u;
2560 do
2561 {
2562 __u = __e_() - _Engine::min();
2563 } while (__u >= __y1_);
2564 if (__w0_ < _EDt - 1)
2565 _S <<= __w0_ + 1;
2566 else
2567 _S = 0;
2568 _S += __u & __mask1_;
2569 }
2570 return _S;
2571}
2572
2573// uniform_int_distribution
2574
2575template<class _IntType = int>
2576class uniform_int_distribution
2577{
2578public:
2579 // types
2580 typedef _IntType result_type;
2581
2582 class param_type
2583 {
2584 result_type __a_;
2585 result_type __b_;
2586 public:
2587 typedef uniform_int_distribution distribution_type;
2588
2589 explicit param_type(result_type __a = 0,
2590 result_type __b = numeric_limits<result_type>::max())
2591 : __a_(__a), __b_(__b) {}
2592
2593 result_type a() const {return __a_;}
2594 result_type b() const {return __b_;}
2595
2596 friend bool operator==(const param_type& __x, const param_type& __y)
2597 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2598 friend bool operator!=(const param_type& __x, const param_type& __y)
2599 {return !(__x == __y);}
2600 };
2601
2602private:
2603 param_type __p_;
2604
2605public:
2606 // constructors and reset functions
2607 explicit uniform_int_distribution(result_type __a = 0,
2608 result_type __b = numeric_limits<result_type>::max())
2609 : __p_(param_type(__a, __b)) {}
2610 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
2611 void reset() {}
2612
2613 // generating functions
2614 template<class _URNG> result_type operator()(_URNG& __g)
2615 {return (*this)(__g, __p_);}
2616 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2617
2618 // property functions
2619 result_type a() const {return __p_.a();}
2620 result_type b() const {return __p_.b();}
2621
2622 param_type param() const {return __p_;}
2623 void param(const param_type& __p) {__p_ = __p;}
2624
2625 result_type min() const {return a();}
2626 result_type max() const {return b();}
2627
2628 friend bool operator==(const uniform_int_distribution& __x,
2629 const uniform_int_distribution& __y)
2630 {return __x.__p_ == __y.__p_;}
2631 friend bool operator!=(const uniform_int_distribution& __x,
2632 const uniform_int_distribution& __y)
2633 {return !(__x == __y);}
2634};
2635
2636template<class _IntType>
2637template<class _URNG>
2638typename uniform_int_distribution<_IntType>::result_type
2639uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
2640{
2641 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
2642 uint32_t, uint64_t>::type _UIntType;
2643 const _UIntType _R = __p.b() - __p.a() + _UIntType(1);
2644 if (_R == 1)
2645 return __p.a();
2646 const size_t _Dt = numeric_limits<_UIntType>::digits;
2647 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
2648 if (_R == 0)
2649 return static_cast<result_type>(_Eng(__g, _Dt)());
2650 size_t __w = _Dt - __clz(_R) - 1;
2651 if ((_R & (_UIntType(~0) >> (_Dt - __w))) != 0)
2652 ++__w;
2653 _Eng __e(__g, __w);
2654 _UIntType __u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002655 do
Howard Hinnantc3267212010-05-26 17:49:34 +00002656 {
2657 __u = __e();
2658 } while (__u >= _R);
2659 return static_cast<result_type>(__u + __p.a());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002660}
2661
Howard Hinnantc3267212010-05-26 17:49:34 +00002662class __rs_default;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002663
Howard Hinnantc3267212010-05-26 17:49:34 +00002664__rs_default __rs_get();
2665
2666class __rs_default
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002667{
Howard Hinnantc3267212010-05-26 17:49:34 +00002668 static unsigned __c_;
2669
2670 __rs_default();
2671public:
2672 typedef unsigned result_type;
2673
2674 static const result_type _Min = 0;
2675 static const result_type _Max = 0xFFFFFFFF;
2676
2677 __rs_default(const __rs_default&);
2678 ~__rs_default();
2679
2680 result_type operator()();
2681
2682 static const/*expr*/ result_type min() {return _Min;}
2683 static const/*expr*/ result_type max() {return _Max;}
2684
2685 friend __rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002686};
2687
Howard Hinnantc3267212010-05-26 17:49:34 +00002688__rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002689
2690template <class _RandomAccessIterator>
2691void
2692random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
2693{
2694 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc3267212010-05-26 17:49:34 +00002695 typedef uniform_int_distribution<ptrdiff_t> _D;
2696 typedef typename _D::param_type _P;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002697 difference_type __d = __last - __first;
2698 if (__d > 1)
2699 {
Howard Hinnantc3267212010-05-26 17:49:34 +00002700 _D __uid;
2701 __rs_default __g = __rs_get();
2702 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00002703 {
2704 difference_type __i = __uid(__g, _P(0, __d));
2705 if (__i != difference_type(0))
2706 swap(*__first, *(__first + __i));
2707 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002708 }
2709}
2710
2711template <class _RandomAccessIterator, class _RandomNumberGenerator>
2712void
2713random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnant73d21a42010-09-04 23:28:19 +00002714#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002715 _RandomNumberGenerator&& __rand)
2716#else
2717 _RandomNumberGenerator& __rand)
2718#endif
2719{
2720 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2721 difference_type __d = __last - __first;
2722 if (__d > 1)
2723 {
2724 for (--__last; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00002725 {
2726 difference_type __i = __rand(__d);
2727 swap(*__first, *(__first + __i));
2728 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002729 }
2730}
2731
Howard Hinnantc3267212010-05-26 17:49:34 +00002732template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
2733 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnant278bf2d2010-11-18 01:47:02 +00002734#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2735 _UniformRandomNumberGenerator&& __g)
2736#else
Howard Hinnantc3267212010-05-26 17:49:34 +00002737 _UniformRandomNumberGenerator& __g)
Howard Hinnant278bf2d2010-11-18 01:47:02 +00002738#endif
Howard Hinnantc3267212010-05-26 17:49:34 +00002739{
2740 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2741 typedef uniform_int_distribution<ptrdiff_t> _D;
2742 typedef typename _D::param_type _P;
2743 difference_type __d = __last - __first;
2744 if (__d > 1)
2745 {
2746 _D __uid;
2747 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00002748 {
2749 difference_type __i = __uid(__g, _P(0, __d));
2750 if (__i != difference_type(0))
2751 swap(*__first, *(__first + __i));
2752 }
Howard Hinnantc3267212010-05-26 17:49:34 +00002753 }
2754}
2755
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002756template <class _InputIterator, class _Predicate>
2757bool
2758is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
2759{
2760 for (; __first != __last; ++__first)
2761 if (!__pred(*__first))
2762 break;
2763 for (; __first != __last; ++__first)
2764 if (__pred(*__first))
2765 return false;
2766 return true;
2767}
2768
2769// partition
2770
2771template <class _Predicate, class _ForwardIterator>
2772_ForwardIterator
2773__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
2774{
2775 while (true)
2776 {
2777 if (__first == __last)
2778 return __first;
2779 if (!__pred(*__first))
2780 break;
2781 ++__first;
2782 }
2783 for (_ForwardIterator __p = __first; ++__p != __last;)
2784 {
2785 if (__pred(*__p))
2786 {
2787 swap(*__first, *__p);
2788 ++__first;
2789 }
2790 }
2791 return __first;
2792}
2793
2794template <class _Predicate, class _BidirectionalIterator>
2795_BidirectionalIterator
2796__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
2797 bidirectional_iterator_tag)
2798{
2799 while (true)
2800 {
2801 while (true)
2802 {
2803 if (__first == __last)
2804 return __first;
2805 if (!__pred(*__first))
2806 break;
2807 ++__first;
2808 }
2809 do
2810 {
2811 if (__first == --__last)
2812 return __first;
2813 } while (!__pred(*__last));
2814 swap(*__first, *__last);
2815 ++__first;
2816 }
2817}
2818
2819template <class _ForwardIterator, class _Predicate>
2820inline _LIBCPP_INLINE_VISIBILITY
2821_ForwardIterator
2822partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2823{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002824 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002825 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
2826}
2827
2828// partition_copy
2829
2830template <class _InputIterator, class _OutputIterator1,
2831 class _OutputIterator2, class _Predicate>
2832pair<_OutputIterator1, _OutputIterator2>
2833partition_copy(_InputIterator __first, _InputIterator __last,
2834 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
2835 _Predicate __pred)
2836{
2837 for (; __first != __last; ++__first)
2838 {
2839 if (__pred(*__first))
2840 {
2841 *__out_true = *__first;
2842 ++__out_true;
2843 }
2844 else
2845 {
2846 *__out_false = *__first;
2847 ++__out_false;
2848 }
2849 }
2850 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
2851}
2852
2853// partition_point
2854
2855template<class _ForwardIterator, class _Predicate>
2856_ForwardIterator
2857partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2858{
2859 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002860 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002861 while (__len != 0)
2862 {
2863 difference_type __l2 = __len / 2;
2864 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002865 _VSTD::advance(__m, __l2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002866 if (__pred(*__m))
2867 {
2868 __first = ++__m;
2869 __len -= __l2 + 1;
2870 }
2871 else
2872 __len = __l2;
2873 }
2874 return __first;
2875}
2876
2877// stable_partition
2878
2879template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
2880_ForwardIterator
2881__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
2882 _Distance __len, _Pair __p, forward_iterator_tag __fit)
2883{
2884 // *__first is known to be false
2885 // __len >= 1
2886 if (__len == 1)
2887 return __first;
2888 if (__len == 2)
2889 {
2890 _ForwardIterator __m = __first;
2891 if (__pred(*++__m))
2892 {
2893 swap(*__first, *__m);
2894 return __m;
2895 }
2896 return __first;
2897 }
2898 if (__len <= __p.second)
2899 { // The buffer is big enough to use
2900 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2901 __destruct_n __d(0);
2902 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
2903 // Move the falses into the temporary buffer, and the trues to the front of the line
2904 // Update __first to always point to the end of the trues
2905 value_type* __t = __p.first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002906 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002907 __d.__incr((value_type*)0);
2908 ++__t;
2909 _ForwardIterator __i = __first;
2910 while (++__i != __last)
2911 {
2912 if (__pred(*__i))
2913 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002914 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002915 ++__first;
2916 }
2917 else
2918 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002919 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002920 __d.__incr((value_type*)0);
2921 ++__t;
2922 }
2923 }
2924 // All trues now at start of range, all falses in buffer
2925 // Move falses back into range, but don't mess up __first which points to first false
2926 __i = __first;
2927 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002928 *__i = _VSTD::move(*__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002929 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
2930 return __first;
2931 }
2932 // Else not enough buffer, do in place
2933 // __len >= 3
2934 _ForwardIterator __m = __first;
2935 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00002936 _VSTD::advance(__m, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002937 // recurse on [__first, __m), *__first know to be false
2938 // F?????????????????
2939 // f m l
2940 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
2941 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
2942 // TTTFFFFF??????????
2943 // f ff m l
2944 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
2945 _ForwardIterator __m1 = __m;
2946 _ForwardIterator __second_false = __last;
2947 _Distance __len_half = __len - __len2;
2948 while (__pred(*__m1))
2949 {
2950 if (++__m1 == __last)
2951 goto __second_half_done;
2952 --__len_half;
2953 }
2954 // TTTFFFFFTTTF??????
2955 // f ff m m1 l
2956 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
2957__second_half_done:
2958 // TTTFFFFFTTTTTFFFFF
2959 // f ff m sf l
Howard Hinnant0949eed2011-06-30 21:18:19 +00002960 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002961 // TTTTTTTTFFFFFFFFFF
2962 // |
2963}
2964
2965struct __return_temporary_buffer
2966{
2967 template <class _Tp>
Howard Hinnant0949eed2011-06-30 21:18:19 +00002968 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002969};
2970
2971template <class _Predicate, class _ForwardIterator>
2972_ForwardIterator
2973__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
2974 forward_iterator_tag)
2975{
2976 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
2977 // Either prove all true and return __first or point to first false
2978 while (true)
2979 {
2980 if (__first == __last)
2981 return __first;
2982 if (!__pred(*__first))
2983 break;
2984 ++__first;
2985 }
2986 // We now have a reduced range [__first, __last)
2987 // *__first is known to be false
2988 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
2989 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002990 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002991 pair<value_type*, ptrdiff_t> __p(0, 0);
2992 unique_ptr<value_type, __return_temporary_buffer> __h;
2993 if (__len >= __alloc_limit)
2994 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002995 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002996 __h.reset(__p.first);
2997 }
2998 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
2999 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3000}
3001
3002template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3003_BidirectionalIterator
3004__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3005 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3006{
3007 // *__first is known to be false
3008 // *__last is known to be true
3009 // __len >= 2
3010 if (__len == 2)
3011 {
3012 swap(*__first, *__last);
3013 return __last;
3014 }
3015 if (__len == 3)
3016 {
3017 _BidirectionalIterator __m = __first;
3018 if (__pred(*++__m))
3019 {
3020 swap(*__first, *__m);
3021 swap(*__m, *__last);
3022 return __last;
3023 }
3024 swap(*__m, *__last);
3025 swap(*__first, *__m);
3026 return __m;
3027 }
3028 if (__len <= __p.second)
3029 { // The buffer is big enough to use
3030 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3031 __destruct_n __d(0);
3032 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3033 // Move the falses into the temporary buffer, and the trues to the front of the line
3034 // Update __first to always point to the end of the trues
3035 value_type* __t = __p.first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003036 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003037 __d.__incr((value_type*)0);
3038 ++__t;
3039 _BidirectionalIterator __i = __first;
3040 while (++__i != __last)
3041 {
3042 if (__pred(*__i))
3043 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003044 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003045 ++__first;
3046 }
3047 else
3048 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003049 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003050 __d.__incr((value_type*)0);
3051 ++__t;
3052 }
3053 }
3054 // move *__last, known to be true
Howard Hinnant0949eed2011-06-30 21:18:19 +00003055 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003056 __i = ++__first;
3057 // All trues now at start of range, all falses in buffer
3058 // Move falses back into range, but don't mess up __first which points to first false
3059 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003060 *__i = _VSTD::move(*__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003061 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3062 return __first;
3063 }
3064 // Else not enough buffer, do in place
3065 // __len >= 4
3066 _BidirectionalIterator __m = __first;
3067 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003068 _VSTD::advance(__m, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003069 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3070 // F????????????????T
3071 // f m l
3072 _BidirectionalIterator __m1 = __m;
3073 _BidirectionalIterator __first_false = __first;
3074 _Distance __len_half = __len2;
3075 while (!__pred(*--__m1))
3076 {
3077 if (__m1 == __first)
3078 goto __first_half_done;
3079 --__len_half;
3080 }
3081 // F???TFFF?????????T
3082 // f m1 m l
3083 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3084 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3085__first_half_done:
3086 // TTTFFFFF?????????T
3087 // f ff m l
3088 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3089 __m1 = __m;
3090 _BidirectionalIterator __second_false = __last;
3091 ++__second_false;
3092 __len_half = __len - __len2;
3093 while (__pred(*__m1))
3094 {
3095 if (++__m1 == __last)
3096 goto __second_half_done;
3097 --__len_half;
3098 }
3099 // TTTFFFFFTTTF?????T
3100 // f ff m m1 l
3101 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3102__second_half_done:
3103 // TTTFFFFFTTTTTFFFFF
3104 // f ff m sf l
Howard Hinnant0949eed2011-06-30 21:18:19 +00003105 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003106 // TTTTTTTTFFFFFFFFFF
3107 // |
3108}
3109
3110template <class _Predicate, class _BidirectionalIterator>
3111_BidirectionalIterator
3112__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3113 bidirectional_iterator_tag)
3114{
3115 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3116 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3117 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3118 // Either prove all true and return __first or point to first false
3119 while (true)
3120 {
3121 if (__first == __last)
3122 return __first;
3123 if (!__pred(*__first))
3124 break;
3125 ++__first;
3126 }
3127 // __first points to first false, everything prior to __first is already set.
3128 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3129 do
3130 {
3131 if (__first == --__last)
3132 return __first;
3133 } while (!__pred(*__last));
3134 // We now have a reduced range [__first, __last]
3135 // *__first is known to be false
3136 // *__last is known to be true
3137 // __len >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003138 difference_type __len = _VSTD::distance(__first, __last) + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003139 pair<value_type*, ptrdiff_t> __p(0, 0);
3140 unique_ptr<value_type, __return_temporary_buffer> __h;
3141 if (__len >= __alloc_limit)
3142 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003143 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003144 __h.reset(__p.first);
3145 }
3146 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3147 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3148}
3149
3150template <class _ForwardIterator, class _Predicate>
3151inline _LIBCPP_INLINE_VISIBILITY
3152_ForwardIterator
3153stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3154{
3155 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3156 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3157}
3158
3159// is_sorted_until
3160
3161template <class _ForwardIterator, class _Compare>
3162_ForwardIterator
3163is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3164{
3165 if (__first != __last)
3166 {
3167 _ForwardIterator __i = __first;
3168 while (++__i != __last)
3169 {
3170 if (__comp(*__i, *__first))
3171 return __i;
3172 __first = __i;
3173 }
3174 }
3175 return __last;
3176}
3177
Howard Hinnant324bb032010-08-22 00:02:43 +00003178template<class _ForwardIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003179inline _LIBCPP_INLINE_VISIBILITY
3180_ForwardIterator
3181is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3182{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003183 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003184}
3185
3186// is_sorted
3187
3188template <class _ForwardIterator, class _Compare>
3189inline _LIBCPP_INLINE_VISIBILITY
3190bool
3191is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3192{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003193 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003194}
3195
Howard Hinnant324bb032010-08-22 00:02:43 +00003196template<class _ForwardIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003197inline _LIBCPP_INLINE_VISIBILITY
3198bool
3199is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3200{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003201 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003202}
3203
3204// sort
3205
3206// stable, 2-3 compares, 0-2 swaps
3207
3208template <class _Compare, class _ForwardIterator>
3209unsigned
3210__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3211{
3212 unsigned __r = 0;
3213 if (!__c(*__y, *__x)) // if x <= y
3214 {
3215 if (!__c(*__z, *__y)) // if y <= z
3216 return __r; // x <= y && y <= z
3217 // x <= y && y > z
3218 swap(*__y, *__z); // x <= z && y < z
3219 __r = 1;
3220 if (__c(*__y, *__x)) // if x > y
3221 {
3222 swap(*__x, *__y); // x < y && y <= z
3223 __r = 2;
3224 }
3225 return __r; // x <= y && y < z
3226 }
3227 if (__c(*__z, *__y)) // x > y, if y > z
3228 {
3229 swap(*__x, *__z); // x < y && y < z
3230 __r = 1;
3231 return __r;
3232 }
3233 swap(*__x, *__y); // x > y && y <= z
3234 __r = 1; // x < y && x <= z
3235 if (__c(*__z, *__y)) // if y > z
3236 {
3237 swap(*__y, *__z); // x <= y && y < z
3238 __r = 2;
3239 }
3240 return __r;
3241} // x <= y && y <= z
3242
3243// stable, 3-6 compares, 0-5 swaps
3244
3245template <class _Compare, class _ForwardIterator>
3246unsigned
3247__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3248 _ForwardIterator __x4, _Compare __c)
3249{
3250 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3251 if (__c(*__x4, *__x3))
3252 {
3253 swap(*__x3, *__x4);
3254 ++__r;
3255 if (__c(*__x3, *__x2))
3256 {
3257 swap(*__x2, *__x3);
3258 ++__r;
3259 if (__c(*__x2, *__x1))
3260 {
3261 swap(*__x1, *__x2);
3262 ++__r;
3263 }
3264 }
3265 }
3266 return __r;
3267}
3268
3269// stable, 4-10 compares, 0-9 swaps
3270
3271template <class _Compare, class _ForwardIterator>
3272unsigned
3273__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3274 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3275{
3276 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3277 if (__c(*__x5, *__x4))
3278 {
3279 swap(*__x4, *__x5);
3280 ++__r;
3281 if (__c(*__x4, *__x3))
3282 {
3283 swap(*__x3, *__x4);
3284 ++__r;
3285 if (__c(*__x3, *__x2))
3286 {
3287 swap(*__x2, *__x3);
3288 ++__r;
3289 if (__c(*__x2, *__x1))
3290 {
3291 swap(*__x1, *__x2);
3292 ++__r;
3293 }
3294 }
3295 }
3296 }
3297 return __r;
3298}
3299
3300// Assumes size > 0
3301template <class _Compare, class _BirdirectionalIterator>
3302void
3303__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3304{
3305 _BirdirectionalIterator __lm1 = __last;
3306 for (--__lm1; __first != __lm1; ++__first)
3307 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003308 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003309 typename add_lvalue_reference<_Compare>::type>
3310 (__first, __last, __comp);
3311 if (__i != __first)
3312 swap(*__first, *__i);
3313 }
3314}
3315
3316template <class _Compare, class _BirdirectionalIterator>
3317void
3318__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3319{
3320 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3321 if (__first != __last)
3322 {
3323 _BirdirectionalIterator __i = __first;
3324 for (++__i; __i != __last; ++__i)
3325 {
3326 _BirdirectionalIterator __j = __i;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003327 value_type __t(_VSTD::move(*__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003328 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003329 *__j = _VSTD::move(*__k);
3330 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003331 }
3332 }
3333}
3334
3335template <class _Compare, class _RandomAccessIterator>
3336void
3337__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3338{
3339 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3340 _RandomAccessIterator __j = __first+2;
3341 __sort3<_Compare>(__first, __first+1, __j, __comp);
3342 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3343 {
3344 if (__comp(*__i, *__j))
3345 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003346 value_type __t(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003347 _RandomAccessIterator __k = __j;
3348 __j = __i;
3349 do
3350 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003351 *__j = _VSTD::move(*__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003352 __j = __k;
3353 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003354 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003355 }
3356 __j = __i;
3357 }
3358}
3359
3360template <class _Compare, class _RandomAccessIterator>
3361bool
3362__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3363{
3364 switch (__last - __first)
3365 {
3366 case 0:
3367 case 1:
3368 return true;
3369 case 2:
3370 if (__comp(*--__last, *__first))
3371 swap(*__first, *__last);
3372 return true;
3373 case 3:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003374 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003375 return true;
3376 case 4:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003377 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003378 return true;
3379 case 5:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003380 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003381 return true;
3382 }
3383 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3384 _RandomAccessIterator __j = __first+2;
3385 __sort3<_Compare>(__first, __first+1, __j, __comp);
3386 const unsigned __limit = 8;
3387 unsigned __count = 0;
3388 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3389 {
3390 if (__comp(*__i, *__j))
3391 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003392 value_type __t(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003393 _RandomAccessIterator __k = __j;
3394 __j = __i;
3395 do
3396 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003397 *__j = _VSTD::move(*__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003398 __j = __k;
3399 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003400 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003401 if (++__count == __limit)
3402 return ++__i == __last;
3403 }
3404 __j = __i;
3405 }
3406 return true;
3407}
3408
3409template <class _Compare, class _BirdirectionalIterator>
3410void
3411__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3412 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3413{
3414 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3415 if (__first1 != __last1)
3416 {
3417 __destruct_n __d(0);
3418 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3419 value_type* __last2 = __first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003420 ::new(__last2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003421 __d.__incr((value_type*)0);
3422 for (++__last2; ++__first1 != __last1; ++__last2)
3423 {
3424 value_type* __j2 = __last2;
3425 value_type* __i2 = __j2;
3426 if (__comp(*__first1, *--__i2))
3427 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003428 ::new(__j2) value_type(_VSTD::move(*__i2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003429 __d.__incr((value_type*)0);
3430 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003431 *__j2 = _VSTD::move(*__i2);
3432 *__j2 = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003433 }
3434 else
3435 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003436 ::new(__j2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003437 __d.__incr((value_type*)0);
3438 }
3439 }
3440 __h.release();
3441 }
3442}
3443
3444template <class _Compare, class _RandomAccessIterator>
3445void
3446__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3447{
3448 // _Compare is known to be a reference type
3449 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3450 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant1468b662010-11-19 22:17:28 +00003451 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3452 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003453 while (true)
3454 {
3455 __restart:
3456 difference_type __len = __last - __first;
3457 switch (__len)
3458 {
3459 case 0:
3460 case 1:
3461 return;
3462 case 2:
3463 if (__comp(*--__last, *__first))
3464 swap(*__first, *__last);
3465 return;
3466 case 3:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003467 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003468 return;
3469 case 4:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003470 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003471 return;
3472 case 5:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003473 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003474 return;
3475 }
3476 if (__len <= __limit)
3477 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003478 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003479 return;
3480 }
3481 // __len > 5
3482 _RandomAccessIterator __m = __first;
3483 _RandomAccessIterator __lm1 = __last;
3484 --__lm1;
3485 unsigned __n_swaps;
3486 {
3487 difference_type __delta;
3488 if (__len >= 1000)
3489 {
3490 __delta = __len/2;
3491 __m += __delta;
3492 __delta /= 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003493 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003494 }
3495 else
3496 {
3497 __delta = __len/2;
3498 __m += __delta;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003499 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003500 }
3501 }
3502 // *__m is median
3503 // partition [__first, __m) < *__m and *__m <= [__m, __last)
3504 // (this inhibits tossing elements equivalent to __m around unnecessarily)
3505 _RandomAccessIterator __i = __first;
3506 _RandomAccessIterator __j = __lm1;
3507 // j points beyond range to be tested, *__m is known to be <= *__lm1
3508 // The search going up is known to be guarded but the search coming down isn't.
3509 // Prime the downward search with a guard.
3510 if (!__comp(*__i, *__m)) // if *__first == *__m
3511 {
3512 // *__first == *__m, *__first doesn't go in first part
3513 // manually guard downward moving __j against __i
3514 while (true)
3515 {
3516 if (__i == --__j)
3517 {
3518 // *__first == *__m, *__m <= all other elements
3519 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
3520 ++__i; // __first + 1
3521 __j = __last;
3522 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
3523 {
3524 while (true)
3525 {
3526 if (__i == __j)
3527 return; // [__first, __last) all equivalent elements
3528 if (__comp(*__first, *__i))
3529 {
3530 swap(*__i, *__j);
3531 ++__n_swaps;
3532 ++__i;
3533 break;
3534 }
3535 ++__i;
3536 }
3537 }
3538 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
3539 if (__i == __j)
3540 return;
3541 while (true)
3542 {
3543 while (!__comp(*__first, *__i))
3544 ++__i;
3545 while (__comp(*__first, *--__j))
3546 ;
3547 if (__i >= __j)
3548 break;
3549 swap(*__i, *__j);
3550 ++__n_swaps;
3551 ++__i;
3552 }
3553 // [__first, __i) == *__first and *__first < [__i, __last)
3554 // The first part is sorted, sort the secod part
Howard Hinnant0949eed2011-06-30 21:18:19 +00003555 // _VSTD::__sort<_Compare>(__i, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003556 __first = __i;
3557 goto __restart;
3558 }
3559 if (__comp(*__j, *__m))
3560 {
3561 swap(*__i, *__j);
3562 ++__n_swaps;
3563 break; // found guard for downward moving __j, now use unguarded partition
3564 }
3565 }
3566 }
3567 // It is known that *__i < *__m
3568 ++__i;
3569 // j points beyond range to be tested, *__m is known to be <= *__lm1
3570 // if not yet partitioned...
3571 if (__i < __j)
3572 {
3573 // known that *(__i - 1) < *__m
3574 // known that __i <= __m
3575 while (true)
3576 {
3577 // __m still guards upward moving __i
3578 while (__comp(*__i, *__m))
3579 ++__i;
3580 // It is now known that a guard exists for downward moving __j
3581 while (!__comp(*--__j, *__m))
3582 ;
3583 if (__i > __j)
3584 break;
3585 swap(*__i, *__j);
3586 ++__n_swaps;
3587 // It is known that __m != __j
3588 // If __m just moved, follow it
3589 if (__m == __i)
3590 __m = __j;
3591 ++__i;
3592 }
3593 }
3594 // [__first, __i) < *__m and *__m <= [__i, __last)
3595 if (__i != __m && __comp(*__m, *__i))
3596 {
3597 swap(*__i, *__m);
3598 ++__n_swaps;
3599 }
3600 // [__first, __i) < *__i and *__i <= [__i+1, __last)
3601 // If we were given a perfect partition, see if insertion sort is quick...
3602 if (__n_swaps == 0)
3603 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003604 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
3605 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003606 {
3607 if (__fs)
3608 return;
3609 __last = __i;
3610 continue;
3611 }
3612 else
3613 {
3614 if (__fs)
3615 {
3616 __first = ++__i;
3617 continue;
3618 }
3619 }
3620 }
3621 // sort smaller range with recursive call and larger with tail recursion elimination
3622 if (__i - __first < __last - __i)
3623 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003624 _VSTD::__sort<_Compare>(__first, __i, __comp);
3625 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003626 __first = ++__i;
3627 }
3628 else
3629 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003630 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
3631 // _VSTD::__sort<_Compare>(__first, __i, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003632 __last = __i;
3633 }
3634 }
3635}
3636
3637// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
3638template <class _RandomAccessIterator, class _Compare>
3639inline _LIBCPP_INLINE_VISIBILITY
3640void
3641sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3642{
Howard Hinnant7a563db2011-09-14 18:33:51 +00003643#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003644 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3645 __debug_less<_Compare> __c(__comp);
3646 __sort<_Comp_ref>(__first, __last, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003647#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003648 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3649 __sort<_Comp_ref>(__first, __last, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003650#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003651}
3652
3653template <class _RandomAccessIterator>
3654inline _LIBCPP_INLINE_VISIBILITY
3655void
3656sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
3657{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003658 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003659}
3660
3661template <class _Tp>
3662inline _LIBCPP_INLINE_VISIBILITY
3663void
3664sort(_Tp** __first, _Tp** __last)
3665{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003666 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003667}
3668
3669template <class _Tp>
3670inline _LIBCPP_INLINE_VISIBILITY
3671void
3672sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
3673{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003674 _VSTD::sort(__first.base(), __last.base());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003675}
3676
Howard Hinnant7a563db2011-09-14 18:33:51 +00003677template <class _Tp, class _Compare>
3678inline _LIBCPP_INLINE_VISIBILITY
3679void
3680sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
3681{
3682 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3683 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
3684}
3685
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003686extern template void __sort<__less<char>&, char*>(char*, char*, __less<char>&);
3687extern template void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&);
3688extern template void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&);
3689extern template void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&);
3690extern template void __sort<__less<short>&, short*>(short*, short*, __less<short>&);
3691extern template void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&);
3692extern template void __sort<__less<int>&, int*>(int*, int*, __less<int>&);
3693extern template void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&);
3694extern template void __sort<__less<long>&, long*>(long*, long*, __less<long>&);
3695extern template void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&);
3696extern template void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&);
3697extern template void __sort<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&);
3698extern template void __sort<__less<float>&, float*>(float*, float*, __less<float>&);
3699extern template void __sort<__less<double>&, double*>(double*, double*, __less<double>&);
3700extern template void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&);
3701
3702extern template bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&);
3703extern template bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&);
3704extern template bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&);
3705extern template bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&);
3706extern template bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&);
3707extern template bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&);
3708extern template bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&);
3709extern template bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&);
3710extern template bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&);
3711extern template bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&);
3712extern template bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&);
3713extern template bool __insertion_sort_incomplete<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&);
3714extern template bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&);
3715extern template bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&);
3716extern template bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&);
3717
3718extern template unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&);
3719
3720// lower_bound
3721
3722template <class _Compare, class _ForwardIterator, class _Tp>
3723_ForwardIterator
3724__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3725{
3726 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003727 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003728 while (__len != 0)
3729 {
3730 difference_type __l2 = __len / 2;
3731 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003732 _VSTD::advance(__m, __l2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003733 if (__comp(*__m, __value))
3734 {
3735 __first = ++__m;
3736 __len -= __l2 + 1;
3737 }
3738 else
3739 __len = __l2;
3740 }
3741 return __first;
3742}
3743
3744template <class _ForwardIterator, class _Tp, class _Compare>
3745inline _LIBCPP_INLINE_VISIBILITY
3746_ForwardIterator
3747lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3748{
Howard Hinnant7a563db2011-09-14 18:33:51 +00003749#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003750 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3751 __debug_less<_Compare> __c(__comp);
3752 return __lower_bound<_Comp_ref>(__first, __last, __value, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003753#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003754 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3755 return __lower_bound<_Comp_ref>(__first, __last, __value, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003756#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003757}
3758
3759template <class _ForwardIterator, class _Tp>
3760inline _LIBCPP_INLINE_VISIBILITY
3761_ForwardIterator
3762lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
3763{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003764 return _VSTD::lower_bound(__first, __last, __value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003765 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
3766}
3767
3768// upper_bound
3769
3770template <class _Compare, class _ForwardIterator, class _Tp>
3771_ForwardIterator
3772__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3773{
3774 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003775 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003776 while (__len != 0)
3777 {
3778 difference_type __l2 = __len / 2;
3779 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003780 _VSTD::advance(__m, __l2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003781 if (__comp(__value, *__m))
3782 __len = __l2;
3783 else
3784 {
3785 __first = ++__m;
3786 __len -= __l2 + 1;
3787 }
3788 }
3789 return __first;
3790}
3791
3792template <class _ForwardIterator, class _Tp, class _Compare>
3793inline _LIBCPP_INLINE_VISIBILITY
3794_ForwardIterator
3795upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3796{
Howard Hinnant7a563db2011-09-14 18:33:51 +00003797#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003798 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3799 __debug_less<_Compare> __c(__comp);
3800 return __upper_bound<_Comp_ref>(__first, __last, __value, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003801#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003802 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3803 return __upper_bound<_Comp_ref>(__first, __last, __value, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003804#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003805}
3806
3807template <class _ForwardIterator, class _Tp>
3808inline _LIBCPP_INLINE_VISIBILITY
3809_ForwardIterator
3810upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
3811{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003812 return _VSTD::upper_bound(__first, __last, __value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003813 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
3814}
3815
3816// equal_range
3817
3818template <class _Compare, class _ForwardIterator, class _Tp>
3819pair<_ForwardIterator, _ForwardIterator>
3820__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3821{
3822 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003823 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003824 while (__len != 0)
3825 {
3826 difference_type __l2 = __len / 2;
3827 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003828 _VSTD::advance(__m, __l2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003829 if (__comp(*__m, __value))
3830 {
3831 __first = ++__m;
3832 __len -= __l2 + 1;
3833 }
3834 else if (__comp(__value, *__m))
3835 {
3836 __last = __m;
3837 __len = __l2;
3838 }
3839 else
3840 {
3841 _ForwardIterator __mp1 = __m;
3842 return pair<_ForwardIterator, _ForwardIterator>
3843 (
3844 __lower_bound<_Compare>(__first, __m, __value, __comp),
3845 __upper_bound<_Compare>(++__mp1, __last, __value, __comp)
3846 );
3847 }
3848 }
3849 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
3850}
3851
3852template <class _ForwardIterator, class _Tp, class _Compare>
3853inline _LIBCPP_INLINE_VISIBILITY
3854pair<_ForwardIterator, _ForwardIterator>
3855equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3856{
Howard Hinnant7a563db2011-09-14 18:33:51 +00003857#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003858 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3859 __debug_less<_Compare> __c(__comp);
3860 return __equal_range<_Comp_ref>(__first, __last, __value, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003861#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003862 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3863 return __equal_range<_Comp_ref>(__first, __last, __value, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003864#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003865}
3866
3867template <class _ForwardIterator, class _Tp>
3868inline _LIBCPP_INLINE_VISIBILITY
3869pair<_ForwardIterator, _ForwardIterator>
3870equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
3871{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003872 return _VSTD::equal_range(__first, __last, __value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003873 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
3874}
3875
3876// binary_search
3877
3878template <class _Compare, class _ForwardIterator, class _Tp>
3879inline _LIBCPP_INLINE_VISIBILITY
3880bool
3881__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3882{
3883 __first = __lower_bound<_Compare>(__first, __last, __value, __comp);
3884 return __first != __last && !__comp(__value, *__first);
3885}
3886
3887template <class _ForwardIterator, class _Tp, class _Compare>
3888inline _LIBCPP_INLINE_VISIBILITY
3889bool
3890binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value, _Compare __comp)
3891{
Howard Hinnant7a563db2011-09-14 18:33:51 +00003892#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003893 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3894 __debug_less<_Compare> __c(__comp);
3895 return __binary_search<_Comp_ref>(__first, __last, __value, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003896#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003897 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3898 return __binary_search<_Comp_ref>(__first, __last, __value, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003899#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003900}
3901
3902template <class _ForwardIterator, class _Tp>
3903inline _LIBCPP_INLINE_VISIBILITY
3904bool
3905binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
3906{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003907 return _VSTD::binary_search(__first, __last, __value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003908 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
3909}
3910
3911// merge
3912
3913template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
3914_OutputIterator
3915__merge(_InputIterator1 __first1, _InputIterator1 __last1,
3916 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
3917{
3918 for (; __first1 != __last1; ++__result)
3919 {
3920 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003921 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003922 if (__comp(*__first2, *__first1))
3923 {
3924 *__result = *__first2;
3925 ++__first2;
3926 }
3927 else
3928 {
3929 *__result = *__first1;
3930 ++__first1;
3931 }
3932 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00003933 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003934}
3935
3936template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
3937inline _LIBCPP_INLINE_VISIBILITY
3938_OutputIterator
3939merge(_InputIterator1 __first1, _InputIterator1 __last1,
3940 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
3941{
Howard Hinnant7a563db2011-09-14 18:33:51 +00003942#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003943 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3944 __debug_less<_Compare> __c(__comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003945 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003946#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003947 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003948 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00003949#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003950}
3951
3952template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
3953inline _LIBCPP_INLINE_VISIBILITY
3954_OutputIterator
3955merge(_InputIterator1 __first1, _InputIterator1 __last1,
3956 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
3957{
3958 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
3959 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
3960 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
3961}
3962
3963// inplace_merge
3964
3965template <class _Compare, class _BidirectionalIterator>
3966void
3967__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
3968 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
3969 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
3970 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
3971{
3972 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3973 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3974 typedef typename iterator_traits<_BidirectionalIterator>::pointer pointer;
3975 __destruct_n __d(0);
3976 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
3977 if (__len1 <= __len2)
3978 {
3979 value_type* __p = __buff;
3980 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), ++__i, ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003981 ::new(__p) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003982 __merge<_Compare>(move_iterator<value_type*>(__buff),
3983 move_iterator<value_type*>(__p),
3984 move_iterator<_BidirectionalIterator>(__middle),
3985 move_iterator<_BidirectionalIterator>(__last),
3986 __first, __comp);
3987 }
3988 else
3989 {
3990 value_type* __p = __buff;
3991 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), ++__i, ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003992 ::new(__p) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003993 typedef reverse_iterator<_BidirectionalIterator> _RBi;
3994 typedef reverse_iterator<value_type*> _Rv;
3995 __merge(move_iterator<_RBi>(_RBi(__middle)), move_iterator<_RBi>(_RBi(__first)),
3996 move_iterator<_Rv>(_Rv(__p)), move_iterator<_Rv>(_Rv(__buff)),
3997 _RBi(__last), __negate<_Compare>(__comp));
3998 }
3999}
4000
4001template <class _Compare, class _BidirectionalIterator>
4002void
4003__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4004 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4005 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4006 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4007{
4008 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4009 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4010 while (true)
4011 {
4012 // if __middle == __last, we're done
4013 if (__len2 == 0)
4014 return;
4015 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
4016 for (; true; ++__first, --__len1)
4017 {
4018 if (__len1 == 0)
4019 return;
4020 if (__comp(*__middle, *__first))
4021 break;
4022 }
4023 if (__len1 <= __buff_size || __len2 <= __buff_size)
4024 {
4025 __buffered_inplace_merge<_Compare>(__first, __middle, __last, __comp, __len1, __len2, __buff);
4026 return;
4027 }
4028 // __first < __middle < __last
4029 // *__first > *__middle
4030 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4031 // all elements in:
4032 // [__first, __m1) <= [__middle, __m2)
4033 // [__middle, __m2) < [__m1, __middle)
4034 // [__m1, __middle) <= [__m2, __last)
4035 // and __m1 or __m2 is in the middle of its range
4036 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4037 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4038 difference_type __len11; // distance(__first, __m1)
4039 difference_type __len21; // distance(__middle, __m2)
4040 // binary search smaller range
4041 if (__len1 < __len2)
4042 { // __len >= 1, __len2 >= 2
4043 __len21 = __len2 / 2;
4044 __m2 = __middle;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004045 _VSTD::advance(__m2, __len21);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004046 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004047 __len11 = _VSTD::distance(__first, __m1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004048 }
4049 else
4050 {
4051 if (__len1 == 1)
4052 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4053 // It is known *__first > *__middle
4054 swap(*__first, *__middle);
4055 return;
4056 }
4057 // __len1 >= 2, __len2 >= 1
4058 __len11 = __len1 / 2;
4059 __m1 = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004060 _VSTD::advance(__m1, __len11);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004061 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004062 __len21 = _VSTD::distance(__middle, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004063 }
4064 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4065 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4066 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4067 // swap middle two partitions
Howard Hinnant0949eed2011-06-30 21:18:19 +00004068 __middle = _VSTD::rotate(__m1, __middle, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004069 // __len12 and __len21 now have swapped meanings
4070 // merge smaller range with recurisve call and larger with tail recursion elimination
4071 if (__len11 + __len21 < __len12 + __len22)
4072 {
4073 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4074// __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4075 __first = __middle;
4076 __middle = __m2;
4077 __len1 = __len12;
4078 __len2 = __len22;
4079 }
4080 else
4081 {
4082 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4083// __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4084 __last = __middle;
4085 __middle = __m1;
4086 __len1 = __len11;
4087 __len2 = __len21;
4088 }
4089 }
4090}
4091
4092template <class _Tp>
4093struct __inplace_merge_switch
4094{
Howard Hinnant1468b662010-11-19 22:17:28 +00004095 static const unsigned value = is_trivially_copy_assignable<_Tp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004096};
4097
4098template <class _BidirectionalIterator, class _Compare>
4099inline _LIBCPP_INLINE_VISIBILITY
4100void
4101inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4102 _Compare __comp)
4103{
4104 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4105 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004106 difference_type __len1 = _VSTD::distance(__first, __middle);
4107 difference_type __len2 = _VSTD::distance(__middle, __last);
4108 difference_type __buf_size = _VSTD::min(__len1, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004109 pair<value_type*, ptrdiff_t> __buf(0, 0);
4110 unique_ptr<value_type, __return_temporary_buffer> __h;
4111 if (__inplace_merge_switch<value_type>::value && __buf_size > 8)
4112 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004113 __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004114 __h.reset(__buf.first);
4115 }
Howard Hinnant7a563db2011-09-14 18:33:51 +00004116#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004117 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4118 __debug_less<_Compare> __c(__comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004119 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004120 __buf.first, __buf.second);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004121#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004122 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004123 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004124 __buf.first, __buf.second);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004125#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004126}
4127
4128template <class _BidirectionalIterator>
4129inline _LIBCPP_INLINE_VISIBILITY
4130void
4131inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4132{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004133 _VSTD::inplace_merge(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004134 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4135}
4136
4137// stable_sort
4138
4139template <class _Compare, class _InputIterator1, class _InputIterator2>
4140void
4141__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4142 _InputIterator2 __first2, _InputIterator2 __last2,
4143 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4144{
4145 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4146 __destruct_n __d(0);
4147 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4148 for (; true; ++__result)
4149 {
4150 if (__first1 == __last1)
4151 {
4152 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
Howard Hinnant0949eed2011-06-30 21:18:19 +00004153 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004154 __h.release();
4155 return;
4156 }
4157 if (__first2 == __last2)
4158 {
4159 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
Howard Hinnant0949eed2011-06-30 21:18:19 +00004160 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004161 __h.release();
4162 return;
4163 }
4164 if (__comp(*__first2, *__first1))
4165 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004166 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004167 __d.__incr((value_type*)0);
4168 ++__first2;
4169 }
4170 else
4171 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004172 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004173 __d.__incr((value_type*)0);
4174 ++__first1;
4175 }
4176 }
4177}
4178
4179template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4180void
4181__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4182 _InputIterator2 __first2, _InputIterator2 __last2,
4183 _OutputIterator __result, _Compare __comp)
4184{
4185 for (; __first1 != __last1; ++__result)
4186 {
4187 if (__first2 == __last2)
4188 {
4189 for (; __first1 != __last1; ++__first1, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004190 *__result = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004191 return;
4192 }
4193 if (__comp(*__first2, *__first1))
4194 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004195 *__result = _VSTD::move(*__first2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004196 ++__first2;
4197 }
4198 else
4199 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004200 *__result = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004201 ++__first1;
4202 }
4203 }
4204 for (; __first2 != __last2; ++__first2, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004205 *__result = _VSTD::move(*__first2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004206}
4207
4208template <class _Compare, class _RandomAccessIterator>
4209void
4210__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4211 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4212 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4213
4214template <class _Compare, class _RandomAccessIterator>
4215void
4216__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4217 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4218 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4219{
4220 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4221 switch (__len)
4222 {
4223 case 0:
4224 return;
4225 case 1:
Howard Hinnant0949eed2011-06-30 21:18:19 +00004226 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004227 return;
4228 case 2:
4229 __destruct_n __d(0);
4230 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4231 if (__comp(*--__last1, *__first1))
4232 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004233 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004234 __d.__incr((value_type*)0);
4235 ++__first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004236 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004237 }
4238 else
4239 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004240 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004241 __d.__incr((value_type*)0);
4242 ++__first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004243 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004244 }
4245 __h2.release();
4246 return;
4247 }
4248 if (__len <= 8)
4249 {
4250 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4251 return;
4252 }
4253 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4254 _RandomAccessIterator __m = __first1 + __l2;
4255 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4256 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4257 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4258}
4259
4260template <class _Tp>
4261struct __stable_sort_switch
4262{
Howard Hinnant1468b662010-11-19 22:17:28 +00004263 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004264};
4265
4266template <class _Compare, class _RandomAccessIterator>
4267void
4268__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4269 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4270 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4271{
4272 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4273 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4274 switch (__len)
4275 {
4276 case 0:
4277 case 1:
4278 return;
4279 case 2:
4280 if (__comp(*--__last, *__first))
4281 swap(*__first, *__last);
4282 return;
4283 }
4284 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4285 {
4286 __insertion_sort<_Compare>(__first, __last, __comp);
4287 return;
4288 }
4289 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4290 _RandomAccessIterator __m = __first + __l2;
4291 if (__len <= __buff_size)
4292 {
4293 __destruct_n __d(0);
4294 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4295 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4296 __d.__set(__l2, (value_type*)0);
4297 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4298 __d.__set(__len, (value_type*)0);
4299 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4300// __merge<_Compare>(move_iterator<value_type*>(__buff),
4301// move_iterator<value_type*>(__buff + __l2),
4302// move_iterator<_RandomAccessIterator>(__buff + __l2),
4303// move_iterator<_RandomAccessIterator>(__buff + __len),
4304// __first, __comp);
4305 return;
4306 }
4307 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4308 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4309 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4310}
4311
4312template <class _RandomAccessIterator, class _Compare>
4313inline _LIBCPP_INLINE_VISIBILITY
4314void
4315stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4316{
4317 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4318 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4319 difference_type __len = __last - __first;
4320 pair<value_type*, ptrdiff_t> __buf(0, 0);
4321 unique_ptr<value_type, __return_temporary_buffer> __h;
4322 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4323 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004324 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004325 __h.reset(__buf.first);
4326 }
Howard Hinnant7a563db2011-09-14 18:33:51 +00004327#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004328 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4329 __debug_less<_Compare> __c(__comp);
4330 __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004331#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004332 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4333 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004334#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004335}
4336
4337template <class _RandomAccessIterator>
4338inline _LIBCPP_INLINE_VISIBILITY
4339void
4340stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4341{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004342 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004343}
4344
4345// is_heap_until
4346
4347template <class _RandomAccessIterator, class _Compare>
4348_RandomAccessIterator
4349is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4350{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004351 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004352 difference_type __len = __last - __first;
4353 difference_type __p = 0;
4354 difference_type __c = 1;
4355 _RandomAccessIterator __pp = __first;
4356 while (__c < __len)
4357 {
4358 _RandomAccessIterator __cp = __first + __c;
4359 if (__comp(*__pp, *__cp))
4360 return __cp;
4361 ++__c;
4362 ++__cp;
4363 if (__c == __len)
4364 return __last;
4365 if (__comp(*__pp, *__cp))
4366 return __cp;
4367 ++__p;
4368 ++__pp;
4369 __c = 2 * __p + 1;
4370 }
4371 return __last;
4372}
4373
Howard Hinnant324bb032010-08-22 00:02:43 +00004374template<class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004375inline _LIBCPP_INLINE_VISIBILITY
4376_RandomAccessIterator
4377is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4378{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004379 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004380}
4381
4382// is_heap
4383
4384template <class _RandomAccessIterator, class _Compare>
4385inline _LIBCPP_INLINE_VISIBILITY
4386bool
4387is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4388{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004389 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004390}
4391
Howard Hinnant324bb032010-08-22 00:02:43 +00004392template<class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004393inline _LIBCPP_INLINE_VISIBILITY
4394bool
4395is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4396{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004397 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004398}
4399
4400// push_heap
4401
4402template <class _Compare, class _RandomAccessIterator>
4403void
4404__push_heap_front(_RandomAccessIterator __first, _RandomAccessIterator, _Compare __comp,
4405 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4406{
4407 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4408 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4409 if (__len > 1)
4410 {
4411 difference_type __p = 0;
4412 _RandomAccessIterator __pp = __first;
4413 difference_type __c = 2;
4414 _RandomAccessIterator __cp = __first + __c;
4415 if (__c == __len || __comp(*__cp, *(__cp - 1)))
4416 {
4417 --__c;
4418 --__cp;
4419 }
4420 if (__comp(*__pp, *__cp))
4421 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004422 value_type __t(_VSTD::move(*__pp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004423 do
4424 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004425 *__pp = _VSTD::move(*__cp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004426 __pp = __cp;
4427 __p = __c;
4428 __c = (__p + 1) * 2;
4429 if (__c > __len)
4430 break;
4431 __cp = __first + __c;
4432 if (__c == __len || __comp(*__cp, *(__cp - 1)))
4433 {
4434 --__c;
4435 --__cp;
4436 }
4437 } while (__comp(__t, *__cp));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004438 *__pp = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004439 }
4440 }
4441}
4442
4443template <class _Compare, class _RandomAccessIterator>
4444void
4445__push_heap_back(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4446 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4447{
4448 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4449 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4450 if (__len > 1)
4451 {
4452 __len = (__len - 2) / 2;
4453 _RandomAccessIterator __ptr = __first + __len;
4454 if (__comp(*__ptr, *--__last))
4455 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004456 value_type __t(_VSTD::move(*__last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004457 do
4458 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004459 *__last = _VSTD::move(*__ptr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004460 __last = __ptr;
4461 if (__len == 0)
4462 break;
4463 __len = (__len - 1) / 2;
4464 __ptr = __first + __len;
4465 } while (__comp(*__ptr, __t));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004466 *__last = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004467 }
4468 }
4469}
4470
4471template <class _RandomAccessIterator, class _Compare>
4472inline _LIBCPP_INLINE_VISIBILITY
4473void
4474push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4475{
Howard Hinnant7a563db2011-09-14 18:33:51 +00004476#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004477 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4478 __debug_less<_Compare> __c(__comp);
4479 __push_heap_back<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004480#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004481 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4482 __push_heap_back<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004483#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004484}
4485
4486template <class _RandomAccessIterator>
4487inline _LIBCPP_INLINE_VISIBILITY
4488void
4489push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4490{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004491 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004492}
4493
4494// pop_heap
4495
4496template <class _Compare, class _RandomAccessIterator>
4497inline _LIBCPP_INLINE_VISIBILITY
4498void
4499__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4500 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4501{
4502 if (__len > 1)
4503 {
4504 swap(*__first, *--__last);
4505 __push_heap_front<_Compare>(__first, __last, __comp, __len-1);
4506 }
4507}
4508
4509template <class _RandomAccessIterator, class _Compare>
4510inline _LIBCPP_INLINE_VISIBILITY
4511void
4512pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4513{
Howard Hinnant7a563db2011-09-14 18:33:51 +00004514#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004515 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4516 __debug_less<_Compare> __c(__comp);
4517 __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004518#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004519 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4520 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004521#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004522}
4523
4524template <class _RandomAccessIterator>
4525inline _LIBCPP_INLINE_VISIBILITY
4526void
4527pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4528{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004529 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004530}
4531
4532// make_heap
4533
4534template <class _Compare, class _RandomAccessIterator>
4535void
4536__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4537{
4538 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4539 difference_type __n = __last - __first;
4540 if (__n > 1)
4541 {
4542 __last = __first;
4543 ++__last;
4544 for (difference_type __i = 1; __i < __n;)
4545 __push_heap_back<_Compare>(__first, ++__last, __comp, ++__i);
4546 }
4547}
4548
4549template <class _RandomAccessIterator, class _Compare>
4550inline _LIBCPP_INLINE_VISIBILITY
4551void
4552make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4553{
Howard Hinnant7a563db2011-09-14 18:33:51 +00004554#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004555 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4556 __debug_less<_Compare> __c(__comp);
4557 __make_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004558#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004559 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4560 __make_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004561#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004562}
4563
4564template <class _RandomAccessIterator>
4565inline _LIBCPP_INLINE_VISIBILITY
4566void
4567make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4568{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004569 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004570}
4571
4572// sort_heap
4573
4574template <class _Compare, class _RandomAccessIterator>
4575void
4576__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4577{
4578 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4579 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
4580 __pop_heap<_Compare>(__first, __last, __comp, __n);
4581}
4582
4583template <class _RandomAccessIterator, class _Compare>
4584inline _LIBCPP_INLINE_VISIBILITY
4585void
4586sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4587{
Howard Hinnant7a563db2011-09-14 18:33:51 +00004588#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004589 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4590 __debug_less<_Compare> __c(__comp);
4591 __sort_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004592#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004593 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4594 __sort_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004595#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004596}
4597
4598template <class _RandomAccessIterator>
4599inline _LIBCPP_INLINE_VISIBILITY
4600void
4601sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4602{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004603 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004604}
4605
4606// partial_sort
4607
4608template <class _Compare, class _RandomAccessIterator>
4609void
4610__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
4611 _Compare __comp)
4612{
4613 __make_heap<_Compare>(__first, __middle, __comp);
4614 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
4615 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
4616 {
4617 if (__comp(*__i, *__first))
4618 {
4619 swap(*__i, *__first);
4620 __push_heap_front<_Compare>(__first, __middle, __comp, __len);
4621 }
4622 }
4623 __sort_heap<_Compare>(__first, __middle, __comp);
4624}
4625
4626template <class _RandomAccessIterator, class _Compare>
4627inline _LIBCPP_INLINE_VISIBILITY
4628void
4629partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
4630 _Compare __comp)
4631{
Howard Hinnant7a563db2011-09-14 18:33:51 +00004632#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004633 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4634 __debug_less<_Compare> __c(__comp);
4635 __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004636#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004637 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4638 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004639#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004640}
4641
4642template <class _RandomAccessIterator>
4643inline _LIBCPP_INLINE_VISIBILITY
4644void
4645partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
4646{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004647 _VSTD::partial_sort(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004648 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4649}
4650
4651// partial_sort_copy
4652
4653template <class _Compare, class _InputIterator, class _RandomAccessIterator>
4654_RandomAccessIterator
4655__partial_sort_copy(_InputIterator __first, _InputIterator __last,
4656 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
4657{
4658 _RandomAccessIterator __r = __result_first;
4659 if (__r != __result_last)
4660 {
4661 typename iterator_traits<_RandomAccessIterator>::difference_type __len = 0;
4662 for (; __first != __last && __r != __result_last; ++__first, ++__r, ++__len)
4663 *__r = *__first;
4664 __make_heap<_Compare>(__result_first, __r, __comp);
4665 for (; __first != __last; ++__first)
4666 if (__comp(*__first, *__result_first))
4667 {
4668 *__result_first = *__first;
4669 __push_heap_front<_Compare>(__result_first, __r, __comp, __len);
4670 }
4671 __sort_heap<_Compare>(__result_first, __r, __comp);
4672 }
4673 return __r;
4674}
4675
4676template <class _InputIterator, class _RandomAccessIterator, class _Compare>
4677inline _LIBCPP_INLINE_VISIBILITY
4678_RandomAccessIterator
4679partial_sort_copy(_InputIterator __first, _InputIterator __last,
4680 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
4681{
Howard Hinnant7a563db2011-09-14 18:33:51 +00004682#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004683 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4684 __debug_less<_Compare> __c(__comp);
4685 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004686#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004687 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4688 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004689#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004690}
4691
4692template <class _InputIterator, class _RandomAccessIterator>
4693inline _LIBCPP_INLINE_VISIBILITY
4694_RandomAccessIterator
4695partial_sort_copy(_InputIterator __first, _InputIterator __last,
4696 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
4697{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004698 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004699 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
4700}
4701
4702// nth_element
4703
4704template <class _Compare, class _RandomAccessIterator>
4705void
4706__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
4707{
4708 // _Compare is known to be a reference type
4709 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4710 const difference_type __limit = 7;
4711 while (true)
4712 {
4713 __restart:
4714 difference_type __len = __last - __first;
4715 switch (__len)
4716 {
4717 case 0:
4718 case 1:
4719 return;
4720 case 2:
4721 if (__comp(*--__last, *__first))
4722 swap(*__first, *__last);
4723 return;
4724 case 3:
4725 {
4726 _RandomAccessIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004727 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004728 return;
4729 }
4730 }
4731 if (__len <= __limit)
4732 {
4733 __selection_sort<_Compare>(__first, __last, __comp);
4734 return;
4735 }
4736 // __len > __limit >= 3
4737 _RandomAccessIterator __m = __first + __len/2;
4738 _RandomAccessIterator __lm1 = __last;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004739 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004740 // *__m is median
4741 // partition [__first, __m) < *__m and *__m <= [__m, __last)
4742 // (this inhibits tossing elements equivalent to __m around unnecessarily)
4743 _RandomAccessIterator __i = __first;
4744 _RandomAccessIterator __j = __lm1;
4745 // j points beyond range to be tested, *__lm1 is known to be <= *__m
4746 // The search going up is known to be guarded but the search coming down isn't.
4747 // Prime the downward search with a guard.
4748 if (!__comp(*__i, *__m)) // if *__first == *__m
4749 {
4750 // *__first == *__m, *__first doesn't go in first part
4751 // manually guard downward moving __j against __i
4752 while (true)
4753 {
4754 if (__i == --__j)
4755 {
4756 // *__first == *__m, *__m <= all other elements
4757 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
4758 ++__i; // __first + 1
4759 __j = __last;
4760 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
4761 {
4762 while (true)
4763 {
4764 if (__i == __j)
4765 return; // [__first, __last) all equivalent elements
4766 if (__comp(*__first, *__i))
4767 {
4768 swap(*__i, *__j);
4769 ++__n_swaps;
4770 ++__i;
4771 break;
4772 }
4773 ++__i;
4774 }
4775 }
4776 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
4777 if (__i == __j)
4778 return;
4779 while (true)
4780 {
4781 while (!__comp(*__first, *__i))
4782 ++__i;
4783 while (__comp(*__first, *--__j))
4784 ;
4785 if (__i >= __j)
4786 break;
4787 swap(*__i, *__j);
4788 ++__n_swaps;
4789 ++__i;
4790 }
4791 // [__first, __i) == *__first and *__first < [__i, __last)
4792 // The first part is sorted,
4793 if (__nth < __i)
4794 return;
4795 // __nth_element the secod part
4796 // __nth_element<_Compare>(__i, __nth, __last, __comp);
4797 __first = __i;
4798 goto __restart;
4799 }
4800 if (__comp(*__j, *__m))
4801 {
4802 swap(*__i, *__j);
4803 ++__n_swaps;
4804 break; // found guard for downward moving __j, now use unguarded partition
4805 }
4806 }
4807 }
4808 ++__i;
4809 // j points beyond range to be tested, *__lm1 is known to be <= *__m
4810 // if not yet partitioned...
4811 if (__i < __j)
4812 {
4813 // known that *(__i - 1) < *__m
4814 while (true)
4815 {
4816 // __m still guards upward moving __i
4817 while (__comp(*__i, *__m))
4818 ++__i;
4819 // It is now known that a guard exists for downward moving __j
4820 while (!__comp(*--__j, *__m))
4821 ;
4822 if (__i >= __j)
4823 break;
4824 swap(*__i, *__j);
4825 ++__n_swaps;
4826 // It is known that __m != __j
4827 // If __m just moved, follow it
4828 if (__m == __i)
4829 __m = __j;
4830 ++__i;
4831 }
4832 }
4833 // [__first, __i) < *__m and *__m <= [__i, __last)
4834 if (__i != __m && __comp(*__m, *__i))
4835 {
4836 swap(*__i, *__m);
4837 ++__n_swaps;
4838 }
4839 // [__first, __i) < *__i and *__i <= [__i+1, __last)
4840 if (__nth == __i)
4841 return;
4842 if (__n_swaps == 0)
4843 {
4844 // We were given a perfectly partitioned sequence. Coincidence?
4845 if (__nth < __i)
4846 {
4847 // Check for [__first, __i) already sorted
4848 __j = __m = __first;
4849 while (++__j != __i)
4850 {
4851 if (__comp(*__j, *__m))
4852 // not yet sorted, so sort
4853 goto not_sorted;
4854 __m = __j;
4855 }
4856 // [__first, __i) sorted
4857 return;
4858 }
4859 else
4860 {
4861 // Check for [__i, __last) already sorted
4862 __j = __m = __i;
4863 while (++__j != __last)
4864 {
4865 if (__comp(*__j, *__m))
4866 // not yet sorted, so sort
4867 goto not_sorted;
4868 __m = __j;
4869 }
4870 // [__i, __last) sorted
4871 return;
4872 }
4873 }
4874not_sorted:
4875 // __nth_element on range containing __nth
4876 if (__nth < __i)
4877 {
4878 // __nth_element<_Compare>(__first, __nth, __i, __comp);
4879 __last = __i;
4880 }
4881 else
4882 {
4883 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
4884 __first = ++__i;
4885 }
4886 }
4887}
4888
4889template <class _RandomAccessIterator, class _Compare>
4890inline _LIBCPP_INLINE_VISIBILITY
4891void
4892nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
4893{
Howard Hinnant7a563db2011-09-14 18:33:51 +00004894#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004895 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4896 __debug_less<_Compare> __c(__comp);
4897 __nth_element<_Comp_ref>(__first, __nth, __last, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004898#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004899 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4900 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004901#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004902}
4903
4904template <class _RandomAccessIterator>
4905inline _LIBCPP_INLINE_VISIBILITY
4906void
4907nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
4908{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004909 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004910}
4911
4912// includes
4913
4914template <class _Compare, class _InputIterator1, class _InputIterator2>
4915bool
4916__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
4917 _Compare __comp)
4918{
4919 for (; __first2 != __last2; ++__first1)
4920 {
4921 if (__first1 == __last1 || __comp(*__first2, *__first1))
4922 return false;
4923 if (!__comp(*__first1, *__first2))
4924 ++__first2;
4925 }
4926 return true;
4927}
4928
4929template <class _InputIterator1, class _InputIterator2, class _Compare>
4930inline _LIBCPP_INLINE_VISIBILITY
4931bool
4932includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
4933 _Compare __comp)
4934{
Howard Hinnant7a563db2011-09-14 18:33:51 +00004935#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004936 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4937 __debug_less<_Compare> __c(__comp);
4938 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004939#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004940 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4941 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004942#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004943}
4944
4945template <class _InputIterator1, class _InputIterator2>
4946inline _LIBCPP_INLINE_VISIBILITY
4947bool
4948includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
4949{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004950 return _VSTD::includes(__first1, __last1, __first2, __last2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004951 __less<typename iterator_traits<_InputIterator1>::value_type,
4952 typename iterator_traits<_InputIterator2>::value_type>());
4953}
4954
4955// set_union
4956
4957template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4958_OutputIterator
4959__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
4960 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4961{
4962 for (; __first1 != __last1; ++__result)
4963 {
4964 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004965 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004966 if (__comp(*__first2, *__first1))
4967 {
4968 *__result = *__first2;
4969 ++__first2;
4970 }
4971 else
4972 {
4973 *__result = *__first1;
4974 if (!__comp(*__first1, *__first2))
4975 ++__first2;
4976 ++__first1;
4977 }
4978 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00004979 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004980}
4981
4982template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4983inline _LIBCPP_INLINE_VISIBILITY
4984_OutputIterator
4985set_union(_InputIterator1 __first1, _InputIterator1 __last1,
4986 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4987{
Howard Hinnant7a563db2011-09-14 18:33:51 +00004988#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004989 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4990 __debug_less<_Compare> __c(__comp);
4991 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004992#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004993 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4994 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00004995#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004996}
4997
4998template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4999inline _LIBCPP_INLINE_VISIBILITY
5000_OutputIterator
5001set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5002 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5003{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005004 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005005 __less<typename iterator_traits<_InputIterator1>::value_type,
5006 typename iterator_traits<_InputIterator2>::value_type>());
5007}
5008
5009// set_intersection
5010
5011template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5012_OutputIterator
5013__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5014 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5015{
5016 while (__first1 != __last1 && __first2 != __last2)
5017 {
5018 if (__comp(*__first1, *__first2))
5019 ++__first1;
5020 else
5021 {
5022 if (!__comp(*__first2, *__first1))
5023 {
5024 *__result = *__first1;
5025 ++__result;
5026 ++__first1;
5027 }
5028 ++__first2;
5029 }
5030 }
5031 return __result;
5032}
5033
5034template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5035inline _LIBCPP_INLINE_VISIBILITY
5036_OutputIterator
5037set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5038 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5039{
Howard Hinnant7a563db2011-09-14 18:33:51 +00005040#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005041 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5042 __debug_less<_Compare> __c(__comp);
5043 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005044#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005045 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5046 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005047#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005048}
5049
5050template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5051inline _LIBCPP_INLINE_VISIBILITY
5052_OutputIterator
5053set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5054 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5055{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005056 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005057 __less<typename iterator_traits<_InputIterator1>::value_type,
5058 typename iterator_traits<_InputIterator2>::value_type>());
5059}
5060
5061// set_difference
5062
5063template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5064_OutputIterator
5065__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5066 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5067{
5068 while (__first1 != __last1)
5069 {
5070 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005071 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005072 if (__comp(*__first1, *__first2))
5073 {
5074 *__result = *__first1;
5075 ++__result;
5076 ++__first1;
5077 }
5078 else
5079 {
5080 if (!__comp(*__first2, *__first1))
5081 ++__first1;
5082 ++__first2;
5083 }
5084 }
5085 return __result;
5086}
5087
5088template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5089inline _LIBCPP_INLINE_VISIBILITY
5090_OutputIterator
5091set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5092 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5093{
Howard Hinnant7a563db2011-09-14 18:33:51 +00005094#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005095 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5096 __debug_less<_Compare> __c(__comp);
5097 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005098#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005099 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5100 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005101#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005102}
5103
5104template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5105inline _LIBCPP_INLINE_VISIBILITY
5106_OutputIterator
5107set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5108 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5109{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005110 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005111 __less<typename iterator_traits<_InputIterator1>::value_type,
5112 typename iterator_traits<_InputIterator2>::value_type>());
5113}
5114
5115// set_symmetric_difference
5116
5117template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5118_OutputIterator
5119__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5120 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5121{
5122 while (__first1 != __last1)
5123 {
5124 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005125 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005126 if (__comp(*__first1, *__first2))
5127 {
5128 *__result = *__first1;
5129 ++__result;
5130 ++__first1;
5131 }
5132 else
5133 {
5134 if (__comp(*__first2, *__first1))
5135 {
5136 *__result = *__first2;
5137 ++__result;
5138 }
5139 else
5140 ++__first1;
5141 ++__first2;
5142 }
5143 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00005144 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005145}
5146
5147template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5148inline _LIBCPP_INLINE_VISIBILITY
5149_OutputIterator
5150set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5151 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5152{
Howard Hinnant7a563db2011-09-14 18:33:51 +00005153#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005154 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5155 __debug_less<_Compare> __c(__comp);
5156 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005157#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005158 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5159 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005160#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005161}
5162
5163template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5164inline _LIBCPP_INLINE_VISIBILITY
5165_OutputIterator
5166set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5167 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5168{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005169 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005170 __less<typename iterator_traits<_InputIterator1>::value_type,
5171 typename iterator_traits<_InputIterator2>::value_type>());
5172}
5173
5174// lexicographical_compare
5175
5176template <class _Compare, class _InputIterator1, class _InputIterator2>
5177bool
5178__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5179 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5180{
5181 for (; __first2 != __last2; ++__first1, ++__first2)
5182 {
5183 if (__first1 == __last1 || __comp(*__first1, *__first2))
5184 return true;
5185 if (__comp(*__first2, *__first1))
5186 return false;
5187 }
5188 return false;
5189}
5190
5191template <class _InputIterator1, class _InputIterator2, class _Compare>
5192inline _LIBCPP_INLINE_VISIBILITY
5193bool
5194lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5195 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5196{
Howard Hinnant7a563db2011-09-14 18:33:51 +00005197#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005198 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5199 __debug_less<_Compare> __c(__comp);
5200 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005201#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005202 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5203 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005204#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005205}
5206
5207template <class _InputIterator1, class _InputIterator2>
5208inline _LIBCPP_INLINE_VISIBILITY
5209bool
5210lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5211 _InputIterator2 __first2, _InputIterator2 __last2)
5212{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005213 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005214 __less<typename iterator_traits<_InputIterator1>::value_type,
5215 typename iterator_traits<_InputIterator2>::value_type>());
5216}
5217
5218// next_permutation
5219
5220template <class _Compare, class _BidirectionalIterator>
5221bool
5222__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5223{
5224 _BidirectionalIterator __i = __last;
5225 if (__first == __last || __first == --__i)
5226 return false;
5227 while (true)
5228 {
5229 _BidirectionalIterator __ip1 = __i;
5230 if (__comp(*--__i, *__ip1))
5231 {
5232 _BidirectionalIterator __j = __last;
5233 while (!__comp(*__i, *--__j))
5234 ;
5235 swap(*__i, *__j);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005236 _VSTD::reverse(__ip1, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005237 return true;
5238 }
5239 if (__i == __first)
5240 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005241 _VSTD::reverse(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005242 return false;
5243 }
5244 }
5245}
5246
5247template <class _BidirectionalIterator, class _Compare>
5248inline _LIBCPP_INLINE_VISIBILITY
5249bool
5250next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5251{
Howard Hinnant7a563db2011-09-14 18:33:51 +00005252#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005253 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5254 __debug_less<_Compare> __c(__comp);
5255 return __next_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005256#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005257 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5258 return __next_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005259#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005260}
5261
5262template <class _BidirectionalIterator>
5263inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005264bool
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005265next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5266{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005267 return _VSTD::next_permutation(__first, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005268 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5269}
5270
5271// prev_permutation
5272
5273template <class _Compare, class _BidirectionalIterator>
5274bool
5275__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5276{
5277 _BidirectionalIterator __i = __last;
5278 if (__first == __last || __first == --__i)
5279 return false;
5280 while (true)
5281 {
5282 _BidirectionalIterator __ip1 = __i;
5283 if (__comp(*__ip1, *--__i))
5284 {
5285 _BidirectionalIterator __j = __last;
5286 while (!__comp(*--__j, *__i))
5287 ;
5288 swap(*__i, *__j);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005289 _VSTD::reverse(__ip1, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005290 return true;
5291 }
5292 if (__i == __first)
5293 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005294 _VSTD::reverse(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005295 return false;
5296 }
5297 }
5298}
5299
5300template <class _BidirectionalIterator, class _Compare>
5301inline _LIBCPP_INLINE_VISIBILITY
5302bool
5303prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5304{
Howard Hinnant7a563db2011-09-14 18:33:51 +00005305#ifdef _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005306 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5307 __debug_less<_Compare> __c(__comp);
5308 return __prev_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005309#else // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005310 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5311 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant7a563db2011-09-14 18:33:51 +00005312#endif // _LIBCPP_DEBUG2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005313}
5314
5315template <class _BidirectionalIterator>
5316inline _LIBCPP_INLINE_VISIBILITY
5317bool
5318prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5319{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005320 return _VSTD::prev_permutation(__first, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005321 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5322}
5323
5324template <class _Tp>
5325inline _LIBCPP_INLINE_VISIBILITY
5326typename enable_if
5327<
5328 is_integral<_Tp>::value,
5329 _Tp
5330>::type
5331__rotate_left(_Tp __t, _Tp __n = 1)
5332{
5333 const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5334 __n &= __bits;
5335 return static_cast<_Tp>((__t << __n) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> (__bits - __n)));
5336}
5337
5338template <class _Tp>
5339inline _LIBCPP_INLINE_VISIBILITY
5340typename enable_if
5341<
5342 is_integral<_Tp>::value,
5343 _Tp
5344>::type
5345__rotate_right(_Tp __t, _Tp __n = 1)
5346{
5347 const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5348 __n &= __bits;
5349 return static_cast<_Tp>((__t << (__bits - __n)) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> __n));
5350}
5351
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005352_LIBCPP_END_NAMESPACE_STD
5353
5354#endif // _LIBCPP_ALGORITHM