blob: 7b0c53e05a5a8be716b13ed6edab1743d5de2e4e [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
Marshall Clowb30abdd2013-05-09 21:14:23 +000090template <class InputIterator1, class InputIterator2>
91 pair<InputIterator1, InputIterator2>
92 mismatch(InputIterator1 first1, InputIterator1 last1,
93 InputIterator2 first2, InputIterator2 last2); // **C++14**
94
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000095template <class InputIterator1, class InputIterator2, class BinaryPredicate>
96 pair<InputIterator1, InputIterator2>
97 mismatch(InputIterator1 first1, InputIterator1 last1,
98 InputIterator2 first2, BinaryPredicate pred);
99
Marshall Clowb30abdd2013-05-09 21:14:23 +0000100template <class InputIterator1, class InputIterator2, class BinaryPredicate>
101 pair<InputIterator1, InputIterator2>
102 mismatch(InputIterator1 first1, InputIterator1 last1,
103 InputIterator2 first2, InputIterator2 last2,
104 BinaryPredicate pred); // **C++14**
105
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000106template <class InputIterator1, class InputIterator2>
107 bool
108 equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
109
Marshall Clowb30abdd2013-05-09 21:14:23 +0000110template <class InputIterator1, class InputIterator2>
111 bool
112 equal(InputIterator1 first1, InputIterator1 last1,
113 InputIterator2 first2, InputIterator2 last2); // **C++14**
114
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000115template <class InputIterator1, class InputIterator2, class BinaryPredicate>
116 bool
117 equal(InputIterator1 first1, InputIterator1 last1,
118 InputIterator2 first2, BinaryPredicate pred);
119
Marshall Clowb30abdd2013-05-09 21:14:23 +0000120template <class InputIterator1, class InputIterator2, class BinaryPredicate>
121 bool
122 equal(InputIterator1 first1, InputIterator1 last1,
123 InputIterator2 first2, InputIterator2 last2,
124 BinaryPredicate pred); // **C++14**
125
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000126template<class ForwardIterator1, class ForwardIterator2>
127 bool
128 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
129 ForwardIterator2 first2);
130
Marshall Clowb30abdd2013-05-09 21:14:23 +0000131template<class ForwardIterator1, class ForwardIterator2>
132 bool
133 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
134 ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
135
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000136template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
137 bool
138 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
139 ForwardIterator2 first2, BinaryPredicate pred);
140
Marshall Clowb30abdd2013-05-09 21:14:23 +0000141template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
142 bool
143 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
144 ForwardIterator2 first2, ForwardIterator2 last2,
145 BinaryPredicate pred); // **C++14**
146
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000147template <class ForwardIterator1, class ForwardIterator2>
148 ForwardIterator1
149 search(ForwardIterator1 first1, ForwardIterator1 last1,
150 ForwardIterator2 first2, ForwardIterator2 last2);
151
152template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
153 ForwardIterator1
154 search(ForwardIterator1 first1, ForwardIterator1 last1,
155 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
156
157template <class ForwardIterator, class Size, class T>
158 ForwardIterator
159 search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
160
161template <class ForwardIterator, class Size, class T, class BinaryPredicate>
162 ForwardIterator
163 search_n(ForwardIterator first, ForwardIterator last,
164 Size count, const T& value, BinaryPredicate pred);
165
166template <class InputIterator, class OutputIterator>
167 OutputIterator
168 copy(InputIterator first, InputIterator last, OutputIterator result);
169
170template<class InputIterator, class OutputIterator, class Predicate>
171 OutputIterator
172 copy_if(InputIterator first, InputIterator last,
173 OutputIterator result, Predicate pred);
174
175template<class InputIterator, class Size, class OutputIterator>
176 OutputIterator
177 copy_n(InputIterator first, Size n, OutputIterator result);
178
179template <class BidirectionalIterator1, class BidirectionalIterator2>
180 BidirectionalIterator2
181 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
182 BidirectionalIterator2 result);
183
184template <class ForwardIterator1, class ForwardIterator2>
185 ForwardIterator2
186 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
187
188template <class ForwardIterator1, class ForwardIterator2>
189 void
190 iter_swap(ForwardIterator1 a, ForwardIterator2 b);
191
192template <class InputIterator, class OutputIterator, class UnaryOperation>
193 OutputIterator
194 transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
195
196template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
197 OutputIterator
198 transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
199 OutputIterator result, BinaryOperation binary_op);
200
201template <class ForwardIterator, class T>
202 void
203 replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
204
205template <class ForwardIterator, class Predicate, class T>
206 void
207 replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
208
209template <class InputIterator, class OutputIterator, class T>
210 OutputIterator
211 replace_copy(InputIterator first, InputIterator last, OutputIterator result,
212 const T& old_value, const T& new_value);
213
214template <class InputIterator, class OutputIterator, class Predicate, class T>
215 OutputIterator
216 replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
217
218template <class ForwardIterator, class T>
219 void
220 fill(ForwardIterator first, ForwardIterator last, const T& value);
221
222template <class OutputIterator, class Size, class T>
223 OutputIterator
224 fill_n(OutputIterator first, Size n, const T& value);
225
226template <class ForwardIterator, class Generator>
227 void
228 generate(ForwardIterator first, ForwardIterator last, Generator gen);
229
230template <class OutputIterator, class Size, class Generator>
231 OutputIterator
232 generate_n(OutputIterator first, Size n, Generator gen);
233
234template <class ForwardIterator, class T>
235 ForwardIterator
236 remove(ForwardIterator first, ForwardIterator last, const T& value);
237
238template <class ForwardIterator, class Predicate>
239 ForwardIterator
240 remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
241
242template <class InputIterator, class OutputIterator, class T>
243 OutputIterator
244 remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
245
246template <class InputIterator, class OutputIterator, class Predicate>
247 OutputIterator
248 remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
249
250template <class ForwardIterator>
251 ForwardIterator
252 unique(ForwardIterator first, ForwardIterator last);
253
254template <class ForwardIterator, class BinaryPredicate>
255 ForwardIterator
256 unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
257
258template <class InputIterator, class OutputIterator>
259 OutputIterator
260 unique_copy(InputIterator first, InputIterator last, OutputIterator result);
261
262template <class InputIterator, class OutputIterator, class BinaryPredicate>
263 OutputIterator
264 unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
265
266template <class BidirectionalIterator>
267 void
268 reverse(BidirectionalIterator first, BidirectionalIterator last);
269
270template <class BidirectionalIterator, class OutputIterator>
271 OutputIterator
272 reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
273
274template <class ForwardIterator>
275 ForwardIterator
276 rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
277
278template <class ForwardIterator, class OutputIterator>
279 OutputIterator
280 rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
281
282template <class RandomAccessIterator>
283 void
Marshall Clow3fef95b2014-03-03 06:14:19 +0000284 random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000285
286template <class RandomAccessIterator, class RandomNumberGenerator>
287 void
Marshall Clow3fef95b2014-03-03 06:14:19 +0000288 random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
289 RandomNumberGenerator& rand); // deprecated in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290
Howard Hinnantc3267212010-05-26 17:49:34 +0000291template<class RandomAccessIterator, class UniformRandomNumberGenerator>
292 void shuffle(RandomAccessIterator first, RandomAccessIterator last,
Howard Hinnant278bf2d2010-11-18 01:47:02 +0000293 UniformRandomNumberGenerator&& g);
Howard Hinnantc3267212010-05-26 17:49:34 +0000294
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000295template <class InputIterator, class Predicate>
296 bool
297 is_partitioned(InputIterator first, InputIterator last, Predicate pred);
298
299template <class ForwardIterator, class Predicate>
300 ForwardIterator
301 partition(ForwardIterator first, ForwardIterator last, Predicate pred);
302
303template <class InputIterator, class OutputIterator1,
304 class OutputIterator2, class Predicate>
305 pair<OutputIterator1, OutputIterator2>
306 partition_copy(InputIterator first, InputIterator last,
307 OutputIterator1 out_true, OutputIterator2 out_false,
308 Predicate pred);
309
310template <class ForwardIterator, class Predicate>
311 ForwardIterator
312 stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
313
314template<class ForwardIterator, class Predicate>
315 ForwardIterator
316 partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
317
318template <class ForwardIterator>
319 bool
320 is_sorted(ForwardIterator first, ForwardIterator last);
321
322template <class ForwardIterator, class Compare>
323 bool
324 is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
325
326template<class ForwardIterator>
327 ForwardIterator
328 is_sorted_until(ForwardIterator first, ForwardIterator last);
329
330template <class ForwardIterator, class Compare>
331 ForwardIterator
332 is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
333
334template <class RandomAccessIterator>
335 void
336 sort(RandomAccessIterator first, RandomAccessIterator last);
337
338template <class RandomAccessIterator, class Compare>
339 void
340 sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
341
342template <class RandomAccessIterator>
343 void
344 stable_sort(RandomAccessIterator first, RandomAccessIterator last);
345
346template <class RandomAccessIterator, class Compare>
347 void
348 stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
349
350template <class RandomAccessIterator>
351 void
352 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
353
354template <class RandomAccessIterator, class Compare>
355 void
356 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
357
358template <class InputIterator, class RandomAccessIterator>
359 RandomAccessIterator
360 partial_sort_copy(InputIterator first, InputIterator last,
361 RandomAccessIterator result_first, RandomAccessIterator result_last);
362
363template <class InputIterator, class RandomAccessIterator, class Compare>
364 RandomAccessIterator
365 partial_sort_copy(InputIterator first, InputIterator last,
366 RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
367
368template <class RandomAccessIterator>
369 void
370 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
371
372template <class RandomAccessIterator, class Compare>
373 void
374 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
375
376template <class ForwardIterator, class T>
377 ForwardIterator
378 lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
379
380template <class ForwardIterator, class T, class Compare>
381 ForwardIterator
382 lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
383
384template <class ForwardIterator, class T>
385 ForwardIterator
386 upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
387
388template <class ForwardIterator, class T, class Compare>
389 ForwardIterator
390 upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
391
392template <class ForwardIterator, class T>
393 pair<ForwardIterator, ForwardIterator>
394 equal_range(ForwardIterator first, ForwardIterator last, const T& value);
395
396template <class ForwardIterator, class T, class Compare>
397 pair<ForwardIterator, ForwardIterator>
398 equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
399
400template <class ForwardIterator, class T>
401 bool
402 binary_search(ForwardIterator first, ForwardIterator last, const T& value);
403
404template <class ForwardIterator, class T, class Compare>
405 bool
406 binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
407
408template <class InputIterator1, class InputIterator2, class OutputIterator>
409 OutputIterator
410 merge(InputIterator1 first1, InputIterator1 last1,
411 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
412
413template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
414 OutputIterator
415 merge(InputIterator1 first1, InputIterator1 last1,
416 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
417
418template <class BidirectionalIterator>
419 void
420 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
421
422template <class BidirectionalIterator, class Compare>
423 void
424 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
425
426template <class InputIterator1, class InputIterator2>
427 bool
428 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
429
430template <class InputIterator1, class InputIterator2, class Compare>
431 bool
432 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
433
434template <class InputIterator1, class InputIterator2, class OutputIterator>
435 OutputIterator
436 set_union(InputIterator1 first1, InputIterator1 last1,
437 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
438
439template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
440 OutputIterator
441 set_union(InputIterator1 first1, InputIterator1 last1,
442 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
443
444template <class InputIterator1, class InputIterator2, class OutputIterator>
445 OutputIterator
446 set_intersection(InputIterator1 first1, InputIterator1 last1,
447 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
448
449template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
450 OutputIterator
451 set_intersection(InputIterator1 first1, InputIterator1 last1,
452 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
453
454template <class InputIterator1, class InputIterator2, class OutputIterator>
455 OutputIterator
456 set_difference(InputIterator1 first1, InputIterator1 last1,
457 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
458
459template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
460 OutputIterator
461 set_difference(InputIterator1 first1, InputIterator1 last1,
462 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
463
464template <class InputIterator1, class InputIterator2, class OutputIterator>
465 OutputIterator
466 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
467 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
468
469template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
470 OutputIterator
471 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
472 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
473
474template <class RandomAccessIterator>
475 void
476 push_heap(RandomAccessIterator first, RandomAccessIterator last);
477
478template <class RandomAccessIterator, class Compare>
479 void
480 push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
481
482template <class RandomAccessIterator>
483 void
484 pop_heap(RandomAccessIterator first, RandomAccessIterator last);
485
486template <class RandomAccessIterator, class Compare>
487 void
488 pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
489
490template <class RandomAccessIterator>
491 void
492 make_heap(RandomAccessIterator first, RandomAccessIterator last);
493
494template <class RandomAccessIterator, class Compare>
495 void
496 make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
497
498template <class RandomAccessIterator>
499 void
500 sort_heap(RandomAccessIterator first, RandomAccessIterator last);
501
502template <class RandomAccessIterator, class Compare>
503 void
504 sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
505
Howard Hinnant324bb032010-08-22 00:02:43 +0000506template <class RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000507 bool
Howard Hinnant324bb032010-08-22 00:02:43 +0000508 is_heap(RandomAccessIterator first, RandomAccessiterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000509
Howard Hinnant324bb032010-08-22 00:02:43 +0000510template <class RandomAccessIterator, class Compare>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000511 bool
Howard Hinnant324bb032010-08-22 00:02:43 +0000512 is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000513
Howard Hinnant324bb032010-08-22 00:02:43 +0000514template <class RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000515 RandomAccessIterator
Howard Hinnant324bb032010-08-22 00:02:43 +0000516 is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000517
Howard Hinnant324bb032010-08-22 00:02:43 +0000518template <class RandomAccessIterator, class Compare>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000519 RandomAccessIterator
Howard Hinnant324bb032010-08-22 00:02:43 +0000520 is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000521
Howard Hinnant98e5d972010-08-21 20:10:01 +0000522template <class ForwardIterator>
523 ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +0000524 min_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000525
526template <class ForwardIterator, class Compare>
527 ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +0000528 min_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000529
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000530template <class T>
531 const T&
Marshall Clow9d9463a2014-02-19 16:51:35 +0000532 min(const T& a, const T& b); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000533
534template <class T, class Compare>
535 const T&
Marshall Clow9d9463a2014-02-19 16:51:35 +0000536 min(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000537
Howard Hinnant98e5d972010-08-21 20:10:01 +0000538template<class T>
539 T
Marshall Clow9d9463a2014-02-19 16:51:35 +0000540 min(initializer_list<T> t); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000541
542template<class T, class Compare>
543 T
Marshall Clow9d9463a2014-02-19 16:51:35 +0000544 min(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000545
546template <class ForwardIterator>
547 ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +0000548 max_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000549
550template <class ForwardIterator, class Compare>
551 ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +0000552 max_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000553
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000554template <class T>
555 const T&
Marshall Clow9d9463a2014-02-19 16:51:35 +0000556 max(const T& a, const T& b); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000557
558template <class T, class Compare>
559 const T&
Marshall Clow9d9463a2014-02-19 16:51:35 +0000560 max(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000561
Howard Hinnant98e5d972010-08-21 20:10:01 +0000562template<class T>
563 T
Marshall Clow9d9463a2014-02-19 16:51:35 +0000564 max(initializer_list<T> t); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000565
Howard Hinnant98e5d972010-08-21 20:10:01 +0000566template<class T, class Compare>
567 T
Marshall Clow9d9463a2014-02-19 16:51:35 +0000568 max(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000569
Howard Hinnant98e5d972010-08-21 20:10:01 +0000570template<class ForwardIterator>
571 pair<ForwardIterator, ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +0000572 minmax_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000573
Howard Hinnant98e5d972010-08-21 20:10:01 +0000574template<class ForwardIterator, class Compare>
575 pair<ForwardIterator, ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +0000576 minmax_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000577
578template<class T>
579 pair<const T&, const T&>
Marshall Clow9d9463a2014-02-19 16:51:35 +0000580 minmax(const T& a, const T& b); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000581
582template<class T, class Compare>
583 pair<const T&, const T&>
Marshall Clow9d9463a2014-02-19 16:51:35 +0000584 minmax(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000585
586template<class T>
587 pair<T, T>
Marshall Clow9d9463a2014-02-19 16:51:35 +0000588 minmax(initializer_list<T> t); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000589
590template<class T, class Compare>
591 pair<T, T>
Marshall Clow9d9463a2014-02-19 16:51:35 +0000592 minmax(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000593
594template <class InputIterator1, class InputIterator2>
595 bool
596 lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
597
598template <class InputIterator1, class InputIterator2, class Compare>
599 bool
600 lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
601 InputIterator2 first2, InputIterator2 last2, Compare comp);
602
603template <class BidirectionalIterator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000604 bool
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000605 next_permutation(BidirectionalIterator first, BidirectionalIterator last);
606
607template <class BidirectionalIterator, class Compare>
608 bool
609 next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
610
611template <class BidirectionalIterator>
612 bool
613 prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
614
615template <class BidirectionalIterator, class Compare>
616 bool
617 prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
618
619} // std
620
621*/
622
623#include <__config>
624#include <initializer_list>
625#include <type_traits>
626#include <cstring>
627#include <utility>
628#include <memory>
629#include <iterator>
Howard Hinnantca8eb832012-07-26 17:09:09 +0000630#include <cstddef>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000631
Howard Hinnant7f764502013-08-14 18:00:20 +0000632#if defined(__IBMCPP__)
633#include "support/ibm/support.h"
634#endif
Howard Hinnantef5aa932013-09-17 01:34:47 +0000635#if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
636#include "support/win32/support.h"
637#endif
Howard Hinnant7f764502013-08-14 18:00:20 +0000638
Howard Hinnant66c6f972011-11-29 16:45:27 +0000639#include <__undef_min_max>
640
Eric Fiselierb9536102014-08-10 23:53:08 +0000641#include <__debug>
642
Howard Hinnant08e17472011-10-17 20:05:10 +0000643#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000644#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000645#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000646
647_LIBCPP_BEGIN_NAMESPACE_STD
648
Marshall Clow9d9463a2014-02-19 16:51:35 +0000649// I'd like to replace these with _VSTD::equal_to<void>, but can't because:
650// * That only works with C++14 and later, and
651// * We haven't included <functional> here.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000652template <class _T1, class _T2 = _T1>
653struct __equal_to
654{
655 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
656 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
657 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
658 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
659};
660
661template <class _T1>
662struct __equal_to<_T1, _T1>
663{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000664 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
665 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000666};
667
668template <class _T1>
669struct __equal_to<const _T1, _T1>
670{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000671 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
672 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000673};
674
675template <class _T1>
676struct __equal_to<_T1, const _T1>
677{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000678 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
679 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000680};
681
682template <class _T1, class _T2 = _T1>
683struct __less
684{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000685 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
686 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
687
688 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
689 bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
690
691 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
692 bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
693
694 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
695 bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000696};
697
698template <class _T1>
699struct __less<_T1, _T1>
700{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000701 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
702 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000703};
704
705template <class _T1>
706struct __less<const _T1, _T1>
707{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000708 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
709 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000710};
711
712template <class _T1>
713struct __less<_T1, const _T1>
714{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000715 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
716 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000717};
718
719template <class _Predicate>
720class __negate
721{
722private:
723 _Predicate __p_;
724public:
725 _LIBCPP_INLINE_VISIBILITY __negate() {}
726
727 _LIBCPP_INLINE_VISIBILITY
728 explicit __negate(_Predicate __p) : __p_(__p) {}
729
730 template <class _T1>
731 _LIBCPP_INLINE_VISIBILITY
732 bool operator()(const _T1& __x) {return !__p_(__x);}
733
734 template <class _T1, class _T2>
735 _LIBCPP_INLINE_VISIBILITY
736 bool operator()(const _T1& __x, const _T2& __y) {return !__p_(__x, __y);}
737};
738
Howard Hinnant5e571422013-08-23 20:10:18 +0000739#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000740
741template <class _Compare>
742struct __debug_less
743{
744 _Compare __comp_;
745 __debug_less(_Compare& __c) : __comp_(__c) {}
746 template <class _Tp, class _Up>
747 bool operator()(const _Tp& __x, const _Up& __y)
748 {
749 bool __r = __comp_(__x, __y);
750 if (__r)
Howard Hinnant7a563db2011-09-14 18:33:51 +0000751 _LIBCPP_ASSERT(!__comp_(__y, __x), "Comparator does not induce a strict weak ordering");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000752 return __r;
753 }
754};
755
Howard Hinnant5e571422013-08-23 20:10:18 +0000756#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000757
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000758// Precondition: __x != 0
Howard Hinnantec3773c2011-12-01 20:21:04 +0000759inline _LIBCPP_INLINE_VISIBILITY
760unsigned
761__ctz(unsigned __x)
762{
763 return static_cast<unsigned>(__builtin_ctz(__x));
764}
765
766inline _LIBCPP_INLINE_VISIBILITY
767unsigned long
768__ctz(unsigned long __x)
769{
770 return static_cast<unsigned long>(__builtin_ctzl(__x));
771}
772
773inline _LIBCPP_INLINE_VISIBILITY
774unsigned long long
775__ctz(unsigned long long __x)
776{
777 return static_cast<unsigned long long>(__builtin_ctzll(__x));
778}
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000779
780// Precondition: __x != 0
Howard Hinnantec3773c2011-12-01 20:21:04 +0000781inline _LIBCPP_INLINE_VISIBILITY
782unsigned
783__clz(unsigned __x)
784{
785 return static_cast<unsigned>(__builtin_clz(__x));
786}
787
788inline _LIBCPP_INLINE_VISIBILITY
789unsigned long
790__clz(unsigned long __x)
791{
792 return static_cast<unsigned long>(__builtin_clzl (__x));
793}
794
795inline _LIBCPP_INLINE_VISIBILITY
796unsigned long long
797__clz(unsigned long long __x)
798{
799 return static_cast<unsigned long long>(__builtin_clzll(__x));
800}
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000801
802inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) {return __builtin_popcount (__x);}
803inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) {return __builtin_popcountl (__x);}
804inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {return __builtin_popcountll(__x);}
805
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000806// all_of
807
808template <class _InputIterator, class _Predicate>
809inline _LIBCPP_INLINE_VISIBILITY
810bool
811all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
812{
813 for (; __first != __last; ++__first)
814 if (!__pred(*__first))
815 return false;
816 return true;
817}
818
819// any_of
820
821template <class _InputIterator, class _Predicate>
822inline _LIBCPP_INLINE_VISIBILITY
823bool
824any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
825{
826 for (; __first != __last; ++__first)
827 if (__pred(*__first))
828 return true;
829 return false;
830}
831
832// none_of
833
834template <class _InputIterator, class _Predicate>
835inline _LIBCPP_INLINE_VISIBILITY
836bool
837none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
838{
839 for (; __first != __last; ++__first)
840 if (__pred(*__first))
841 return false;
842 return true;
843}
844
845// for_each
846
847template <class _InputIterator, class _Function>
848inline _LIBCPP_INLINE_VISIBILITY
849_Function
850for_each(_InputIterator __first, _InputIterator __last, _Function __f)
851{
852 for (; __first != __last; ++__first)
853 __f(*__first);
Howard Hinnant9a894d92013-08-22 18:29:50 +0000854 return _VSTD::move(__f); // explicitly moved for (emulated) C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000855}
856
857// find
858
859template <class _InputIterator, class _Tp>
860inline _LIBCPP_INLINE_VISIBILITY
861_InputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +0000862find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000863{
864 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +0000865 if (*__first == __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000866 break;
867 return __first;
868}
869
870// find_if
871
872template <class _InputIterator, class _Predicate>
873inline _LIBCPP_INLINE_VISIBILITY
874_InputIterator
875find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
876{
877 for (; __first != __last; ++__first)
878 if (__pred(*__first))
879 break;
880 return __first;
881}
882
883// find_if_not
884
885template<class _InputIterator, class _Predicate>
886inline _LIBCPP_INLINE_VISIBILITY
887_InputIterator
888find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
889{
890 for (; __first != __last; ++__first)
891 if (!__pred(*__first))
892 break;
893 return __first;
894}
895
896// find_end
897
898template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
899_ForwardIterator1
900__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
901 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
902 forward_iterator_tag, forward_iterator_tag)
903{
904 // modeled after search algorithm
905 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
906 if (__first2 == __last2)
907 return __r;
908 while (true)
909 {
910 while (true)
911 {
912 if (__first1 == __last1) // if source exhausted return last correct answer
913 return __r; // (or __last1 if never found)
914 if (__pred(*__first1, *__first2))
915 break;
916 ++__first1;
917 }
918 // *__first1 matches *__first2, now match elements after here
919 _ForwardIterator1 __m1 = __first1;
920 _ForwardIterator2 __m2 = __first2;
921 while (true)
922 {
923 if (++__m2 == __last2)
924 { // Pattern exhaused, record answer and search for another one
925 __r = __first1;
926 ++__first1;
927 break;
928 }
929 if (++__m1 == __last1) // Source exhausted, return last answer
930 return __r;
931 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
932 {
933 ++__first1;
934 break;
935 } // else there is a match, check next elements
936 }
937 }
938}
939
940template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
941_BidirectionalIterator1
942__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
943 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
944 bidirectional_iterator_tag, bidirectional_iterator_tag)
945{
946 // modeled after search algorithm (in reverse)
947 if (__first2 == __last2)
948 return __last1; // Everything matches an empty sequence
949 _BidirectionalIterator1 __l1 = __last1;
950 _BidirectionalIterator2 __l2 = __last2;
951 --__l2;
952 while (true)
953 {
954 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
955 while (true)
956 {
957 if (__first1 == __l1) // return __last1 if no element matches *__first2
958 return __last1;
959 if (__pred(*--__l1, *__l2))
960 break;
961 }
962 // *__l1 matches *__l2, now match elements before here
963 _BidirectionalIterator1 __m1 = __l1;
964 _BidirectionalIterator2 __m2 = __l2;
965 while (true)
966 {
967 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
968 return __m1;
969 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
970 return __last1;
971 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
972 {
973 break;
974 } // else there is a match, check next elements
975 }
976 }
977}
978
979template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow37025e12014-06-10 18:51:55 +0000980_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000981__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
982 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
983 random_access_iterator_tag, random_access_iterator_tag)
984{
985 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
986 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
987 if (__len2 == 0)
988 return __last1;
989 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
990 if (__len1 < __len2)
991 return __last1;
992 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
993 _RandomAccessIterator1 __l1 = __last1;
994 _RandomAccessIterator2 __l2 = __last2;
995 --__l2;
996 while (true)
997 {
998 while (true)
999 {
1000 if (__s == __l1)
1001 return __last1;
1002 if (__pred(*--__l1, *__l2))
1003 break;
1004 }
1005 _RandomAccessIterator1 __m1 = __l1;
1006 _RandomAccessIterator2 __m2 = __l2;
1007 while (true)
1008 {
1009 if (__m2 == __first2)
1010 return __m1;
1011 // no need to check range on __m1 because __s guarantees we have enough source
1012 if (!__pred(*--__m1, *--__m2))
1013 {
1014 break;
1015 }
1016 }
1017 }
1018}
1019
1020template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1021inline _LIBCPP_INLINE_VISIBILITY
1022_ForwardIterator1
1023find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1024 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1025{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001026 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001027 (__first1, __last1, __first2, __last2, __pred,
1028 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1029 typename iterator_traits<_ForwardIterator2>::iterator_category());
1030}
1031
1032template <class _ForwardIterator1, class _ForwardIterator2>
1033inline _LIBCPP_INLINE_VISIBILITY
1034_ForwardIterator1
1035find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1036 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1037{
1038 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1039 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001040 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001041}
1042
1043// find_first_of
1044
1045template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow37025e12014-06-10 18:51:55 +00001046_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1047__find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001048 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1049{
1050 for (; __first1 != __last1; ++__first1)
1051 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1052 if (__pred(*__first1, *__j))
1053 return __first1;
1054 return __last1;
1055}
1056
Marshall Clow37025e12014-06-10 18:51:55 +00001057
1058template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1059inline _LIBCPP_INLINE_VISIBILITY
1060_ForwardIterator1
1061find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1062 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1063{
1064 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1065}
1066
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067template <class _ForwardIterator1, class _ForwardIterator2>
1068inline _LIBCPP_INLINE_VISIBILITY
1069_ForwardIterator1
1070find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1071 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1072{
1073 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1074 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Marshall Clow37025e12014-06-10 18:51:55 +00001075 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001076}
1077
1078// adjacent_find
1079
1080template <class _ForwardIterator, class _BinaryPredicate>
1081inline _LIBCPP_INLINE_VISIBILITY
1082_ForwardIterator
1083adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1084{
1085 if (__first != __last)
1086 {
1087 _ForwardIterator __i = __first;
1088 while (++__i != __last)
1089 {
1090 if (__pred(*__first, *__i))
1091 return __first;
1092 __first = __i;
1093 }
1094 }
1095 return __last;
1096}
1097
1098template <class _ForwardIterator>
1099inline _LIBCPP_INLINE_VISIBILITY
1100_ForwardIterator
1101adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1102{
1103 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001104 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001105}
1106
1107// count
1108
1109template <class _InputIterator, class _Tp>
1110inline _LIBCPP_INLINE_VISIBILITY
1111typename iterator_traits<_InputIterator>::difference_type
Howard Hinnant78b68282011-10-22 20:59:45 +00001112count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001113{
1114 typename iterator_traits<_InputIterator>::difference_type __r(0);
1115 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +00001116 if (*__first == __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001117 ++__r;
1118 return __r;
1119}
1120
1121// count_if
1122
1123template <class _InputIterator, class _Predicate>
1124inline _LIBCPP_INLINE_VISIBILITY
1125typename iterator_traits<_InputIterator>::difference_type
1126count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1127{
1128 typename iterator_traits<_InputIterator>::difference_type __r(0);
1129 for (; __first != __last; ++__first)
1130 if (__pred(*__first))
1131 ++__r;
1132 return __r;
1133}
1134
1135// mismatch
1136
1137template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1138inline _LIBCPP_INLINE_VISIBILITY
1139pair<_InputIterator1, _InputIterator2>
1140mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1141 _InputIterator2 __first2, _BinaryPredicate __pred)
1142{
Marshall Clowd402a4d2014-09-16 20:40:05 +00001143 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001144 if (!__pred(*__first1, *__first2))
1145 break;
1146 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1147}
1148
1149template <class _InputIterator1, class _InputIterator2>
1150inline _LIBCPP_INLINE_VISIBILITY
1151pair<_InputIterator1, _InputIterator2>
1152mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1153{
1154 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1155 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001156 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001157}
1158
Marshall Clowb30abdd2013-05-09 21:14:23 +00001159#if _LIBCPP_STD_VER > 11
1160template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1161inline _LIBCPP_INLINE_VISIBILITY
1162pair<_InputIterator1, _InputIterator2>
1163mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1164 _InputIterator2 __first2, _InputIterator2 __last2,
1165 _BinaryPredicate __pred)
1166{
Marshall Clowd402a4d2014-09-16 20:40:05 +00001167 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001168 if (!__pred(*__first1, *__first2))
1169 break;
1170 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1171}
1172
1173template <class _InputIterator1, class _InputIterator2>
1174inline _LIBCPP_INLINE_VISIBILITY
1175pair<_InputIterator1, _InputIterator2>
1176mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1177 _InputIterator2 __first2, _InputIterator2 __last2)
1178{
1179 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1180 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1181 return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1182}
1183#endif
1184
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001185// equal
1186
1187template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1188inline _LIBCPP_INLINE_VISIBILITY
1189bool
1190equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1191{
Eric Fiselierb9919752014-10-27 19:28:20 +00001192 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001193 if (!__pred(*__first1, *__first2))
1194 return false;
1195 return true;
1196}
1197
1198template <class _InputIterator1, class _InputIterator2>
1199inline _LIBCPP_INLINE_VISIBILITY
1200bool
1201equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1202{
1203 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1204 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001205 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001206}
1207
Marshall Clowb30abdd2013-05-09 21:14:23 +00001208#if _LIBCPP_STD_VER > 11
1209template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
1210inline _LIBCPP_INLINE_VISIBILITY
1211bool
1212__equal(_InputIterator1 __first1, _InputIterator1 __last1,
1213 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1214 input_iterator_tag, input_iterator_tag )
1215{
Eric Fiselierb9919752014-10-27 19:28:20 +00001216 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001217 if (!__pred(*__first1, *__first2))
1218 return false;
1219 return __first1 == __last1 && __first2 == __last2;
1220}
1221
1222template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1223inline _LIBCPP_INLINE_VISIBILITY
1224bool
1225__equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1226 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1227 random_access_iterator_tag, random_access_iterator_tag )
1228{
1229 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1230 return false;
1231 return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1232 typename add_lvalue_reference<_BinaryPredicate>::type>
1233 (__first1, __last1, __first2, __pred );
1234}
1235
1236template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1237inline _LIBCPP_INLINE_VISIBILITY
1238bool
1239equal(_InputIterator1 __first1, _InputIterator1 __last1,
1240 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1241{
1242 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
1243 (__first1, __last1, __first2, __last2, __pred,
1244 typename iterator_traits<_InputIterator1>::iterator_category(),
1245 typename iterator_traits<_InputIterator2>::iterator_category());
1246}
1247
1248template <class _InputIterator1, class _InputIterator2>
1249inline _LIBCPP_INLINE_VISIBILITY
1250bool
1251equal(_InputIterator1 __first1, _InputIterator1 __last1,
1252 _InputIterator2 __first2, _InputIterator2 __last2)
1253{
1254 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1255 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1256 return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1257 typename iterator_traits<_InputIterator1>::iterator_category(),
1258 typename iterator_traits<_InputIterator2>::iterator_category());
1259}
1260#endif
1261
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001262// is_permutation
1263
1264template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1265bool
1266is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1267 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1268{
1269 // shorten sequences as much as possible by lopping of any equal parts
Eric Fiselierb9919752014-10-27 19:28:20 +00001270 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001271 if (!__pred(*__first1, *__first2))
1272 goto __not_done;
1273 return true;
1274__not_done:
1275 // __first1 != __last1 && *__first1 != *__first2
1276 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001277 _D1 __l1 = _VSTD::distance(__first1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001278 if (__l1 == _D1(1))
1279 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001280 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001281 // For each element in [f1, l1) see if there are the same number of
1282 // equal elements in [f2, l2)
1283 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1284 {
1285 // Have we already counted the number of *__i in [f1, l1)?
1286 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1287 if (__pred(*__j, *__i))
1288 goto __next_iter;
1289 {
1290 // Count number of *__i in [f2, l2)
1291 _D1 __c2 = 0;
1292 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1293 if (__pred(*__i, *__j))
1294 ++__c2;
1295 if (__c2 == 0)
1296 return false;
1297 // Count number of *__i in [__i, l1) (we can start with 1)
1298 _D1 __c1 = 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001299 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001300 if (__pred(*__i, *__j))
1301 ++__c1;
1302 if (__c1 != __c2)
1303 return false;
1304 }
1305__next_iter:;
1306 }
1307 return true;
1308}
1309
1310template<class _ForwardIterator1, class _ForwardIterator2>
1311inline _LIBCPP_INLINE_VISIBILITY
1312bool
1313is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1314 _ForwardIterator2 __first2)
1315{
1316 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1317 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001318 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001319}
1320
Marshall Clowb30abdd2013-05-09 21:14:23 +00001321#if _LIBCPP_STD_VER > 11
1322template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1323bool
1324__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1325 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1326 _BinaryPredicate __pred,
1327 forward_iterator_tag, forward_iterator_tag )
1328{
1329 // shorten sequences as much as possible by lopping of any equal parts
Eric Fiselier62a0e012014-10-27 20:26:25 +00001330 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001331 if (!__pred(*__first1, *__first2))
1332 goto __not_done;
1333 return __first1 == __last1 && __first2 == __last2;
1334__not_done:
1335 // __first1 != __last1 && __first2 != __last2 && *__first1 != *__first2
1336 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1337 _D1 __l1 = _VSTD::distance(__first1, __last1);
1338
1339 typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
Marshall Clow9f8f5242013-05-10 00:16:10 +00001340 _D2 __l2 = _VSTD::distance(__first2, __last2);
Marshall Clowb30abdd2013-05-09 21:14:23 +00001341 if (__l1 != __l2)
1342 return false;
1343
1344 // For each element in [f1, l1) see if there are the same number of
1345 // equal elements in [f2, l2)
1346 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1347 {
1348 // Have we already counted the number of *__i in [f1, l1)?
1349 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1350 if (__pred(*__j, *__i))
1351 goto __next_iter;
1352 {
1353 // Count number of *__i in [f2, l2)
1354 _D1 __c2 = 0;
1355 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1356 if (__pred(*__i, *__j))
1357 ++__c2;
1358 if (__c2 == 0)
1359 return false;
1360 // Count number of *__i in [__i, l1) (we can start with 1)
1361 _D1 __c1 = 1;
1362 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1363 if (__pred(*__i, *__j))
1364 ++__c1;
1365 if (__c1 != __c2)
1366 return false;
1367 }
1368__next_iter:;
1369 }
1370 return true;
1371}
1372
1373template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1374bool
1375__is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
1376 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
1377 _BinaryPredicate __pred,
1378 random_access_iterator_tag, random_access_iterator_tag )
1379{
1380 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1381 return false;
1382 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1383 typename add_lvalue_reference<_BinaryPredicate>::type>
1384 (__first1, __last1, __first2, __pred );
1385}
1386
1387template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1388inline _LIBCPP_INLINE_VISIBILITY
1389bool
1390is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1391 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1392 _BinaryPredicate __pred )
1393{
1394 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1395 (__first1, __last1, __first2, __last2, __pred,
1396 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1397 typename iterator_traits<_ForwardIterator2>::iterator_category());
1398}
1399
1400template<class _ForwardIterator1, class _ForwardIterator2>
1401inline _LIBCPP_INLINE_VISIBILITY
1402bool
1403is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1404 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1405{
1406 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1407 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1408 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1409 __equal_to<__v1, __v2>(),
1410 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1411 typename iterator_traits<_ForwardIterator2>::iterator_category());
1412}
1413#endif
1414
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001415// search
1416
1417template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1418_ForwardIterator1
1419__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1420 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
1421 forward_iterator_tag, forward_iterator_tag)
1422{
1423 if (__first2 == __last2)
1424 return __first1; // Everything matches an empty sequence
1425 while (true)
1426 {
1427 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
1428 while (true)
1429 {
1430 if (__first1 == __last1) // return __last1 if no element matches *__first2
1431 return __last1;
1432 if (__pred(*__first1, *__first2))
1433 break;
1434 ++__first1;
1435 }
1436 // *__first1 matches *__first2, now match elements after here
1437 _ForwardIterator1 __m1 = __first1;
1438 _ForwardIterator2 __m2 = __first2;
1439 while (true)
1440 {
1441 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
1442 return __first1;
1443 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
1444 return __last1;
1445 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
1446 {
1447 ++__first1;
1448 break;
1449 } // else there is a match, check next elements
1450 }
1451 }
1452}
1453
1454template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow37025e12014-06-10 18:51:55 +00001455_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001456__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1457 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1458 random_access_iterator_tag, random_access_iterator_tag)
1459{
1460 typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _D1;
1461 typedef typename std::iterator_traits<_RandomAccessIterator2>::difference_type _D2;
1462 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1463 _D2 __len2 = __last2 - __first2;
1464 if (__len2 == 0)
1465 return __first1;
1466 _D1 __len1 = __last1 - __first1;
1467 if (__len1 < __len2)
1468 return __last1;
1469 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
1470 while (true)
1471 {
1472#if !_LIBCPP_UNROLL_LOOPS
1473 while (true)
1474 {
1475 if (__first1 == __s)
1476 return __last1;
1477 if (__pred(*__first1, *__first2))
1478 break;
1479 ++__first1;
1480 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001481#else // !_LIBCPP_UNROLL_LOOPS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001482 for (_D1 __loop_unroll = (__s - __first1) / 4; __loop_unroll > 0; --__loop_unroll)
1483 {
1484 if (__pred(*__first1, *__first2))
1485 goto __phase2;
1486 if (__pred(*++__first1, *__first2))
1487 goto __phase2;
1488 if (__pred(*++__first1, *__first2))
1489 goto __phase2;
1490 if (__pred(*++__first1, *__first2))
1491 goto __phase2;
1492 ++__first1;
1493 }
1494 switch (__s - __first1)
1495 {
1496 case 3:
1497 if (__pred(*__first1, *__first2))
1498 break;
1499 ++__first1;
1500 case 2:
1501 if (__pred(*__first1, *__first2))
1502 break;
1503 ++__first1;
1504 case 1:
1505 if (__pred(*__first1, *__first2))
1506 break;
1507 case 0:
1508 return __last1;
1509 }
1510 __phase2:
Howard Hinnant324bb032010-08-22 00:02:43 +00001511#endif // !_LIBCPP_UNROLL_LOOPS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001512 _RandomAccessIterator1 __m1 = __first1;
1513 _RandomAccessIterator2 __m2 = __first2;
1514#if !_LIBCPP_UNROLL_LOOPS
1515 while (true)
1516 {
1517 if (++__m2 == __last2)
1518 return __first1;
1519 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
1520 if (!__pred(*__m1, *__m2))
1521 {
1522 ++__first1;
1523 break;
1524 }
1525 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001526#else // !_LIBCPP_UNROLL_LOOPS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001527 ++__m2;
1528 ++__m1;
1529 for (_D2 __loop_unroll = (__last2 - __m2) / 4; __loop_unroll > 0; --__loop_unroll)
1530 {
1531 if (!__pred(*__m1, *__m2))
1532 goto __continue;
1533 if (!__pred(*++__m1, *++__m2))
1534 goto __continue;
1535 if (!__pred(*++__m1, *++__m2))
1536 goto __continue;
1537 if (!__pred(*++__m1, *++__m2))
1538 goto __continue;
1539 ++__m1;
1540 ++__m2;
1541 }
1542 switch (__last2 - __m2)
1543 {
1544 case 3:
1545 if (!__pred(*__m1, *__m2))
1546 break;
1547 ++__m1;
1548 ++__m2;
1549 case 2:
1550 if (!__pred(*__m1, *__m2))
1551 break;
1552 ++__m1;
1553 ++__m2;
1554 case 1:
1555 if (!__pred(*__m1, *__m2))
1556 break;
1557 case 0:
1558 return __first1;
1559 }
1560 __continue:
1561 ++__first1;
Howard Hinnant324bb032010-08-22 00:02:43 +00001562#endif // !_LIBCPP_UNROLL_LOOPS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001563 }
1564}
1565
1566template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1567inline _LIBCPP_INLINE_VISIBILITY
1568_ForwardIterator1
1569search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1570 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1571{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001572 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001573 (__first1, __last1, __first2, __last2, __pred,
1574 typename std::iterator_traits<_ForwardIterator1>::iterator_category(),
1575 typename std::iterator_traits<_ForwardIterator2>::iterator_category());
1576}
1577
1578template <class _ForwardIterator1, class _ForwardIterator2>
1579inline _LIBCPP_INLINE_VISIBILITY
1580_ForwardIterator1
1581search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1582 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1583{
1584 typedef typename std::iterator_traits<_ForwardIterator1>::value_type __v1;
1585 typedef typename std::iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001586 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001587}
1588
1589// search_n
1590
1591template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
1592_ForwardIterator
1593__search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001594 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001595{
1596 if (__count <= 0)
1597 return __first;
1598 while (true)
1599 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001600 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001601 while (true)
1602 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001603 if (__first == __last) // return __last if no element matches __value_
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001604 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001605 if (__pred(*__first, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001606 break;
1607 ++__first;
1608 }
Howard Hinnant78b68282011-10-22 20:59:45 +00001609 // *__first matches __value_, now match elements after here
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001610 _ForwardIterator __m = __first;
1611 _Size __c(0);
1612 while (true)
1613 {
1614 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1615 return __first;
1616 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1617 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001618 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001619 {
1620 __first = __m;
1621 ++__first;
1622 break;
1623 } // else there is a match, check next elements
1624 }
1625 }
1626}
1627
1628template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
1629_RandomAccessIterator
1630__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001631 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001632{
1633 if (__count <= 0)
1634 return __first;
1635 _Size __len = static_cast<_Size>(__last - __first);
1636 if (__len < __count)
1637 return __last;
1638 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1639 while (true)
1640 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001641 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001642 while (true)
1643 {
Howard Hinnant128f7bf2013-04-04 15:40:48 +00001644 if (__first >= __s) // return __last if no element matches __value_
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001645 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001646 if (__pred(*__first, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001647 break;
1648 ++__first;
1649 }
Howard Hinnant78b68282011-10-22 20:59:45 +00001650 // *__first matches __value_, now match elements after here
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001651 _RandomAccessIterator __m = __first;
1652 _Size __c(0);
1653 while (true)
1654 {
1655 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1656 return __first;
1657 ++__m; // no need to check range on __m because __s guarantees we have enough source
Howard Hinnant78b68282011-10-22 20:59:45 +00001658 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001659 {
1660 __first = __m;
1661 ++__first;
1662 break;
1663 } // else there is a match, check next elements
1664 }
1665 }
1666}
1667
1668template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
1669inline _LIBCPP_INLINE_VISIBILITY
1670_ForwardIterator
1671search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001672 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001673{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001674 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001675 (__first, __last, __convert_to_integral(__count), __value_, __pred,
1676 typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001677}
1678
1679template <class _ForwardIterator, class _Size, class _Tp>
1680inline _LIBCPP_INLINE_VISIBILITY
1681_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00001682search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001683{
1684 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001685 return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
1686 __value_, __equal_to<__v, _Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001687}
1688
1689// copy
1690
1691template <class _Iter>
1692struct __libcpp_is_trivial_iterator
1693{
1694 static const bool value = is_pointer<_Iter>::value;
1695};
1696
1697template <class _Iter>
1698struct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
1699{
1700 static const bool value = is_pointer<_Iter>::value;
1701};
1702
1703template <class _Iter>
1704struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
1705{
1706 static const bool value = is_pointer<_Iter>::value;
1707};
1708
1709template <class _Iter>
1710inline _LIBCPP_INLINE_VISIBILITY
1711_Iter
1712__unwrap_iter(_Iter __i)
1713{
1714 return __i;
1715}
1716
1717template <class _Tp>
1718inline _LIBCPP_INLINE_VISIBILITY
1719typename enable_if
1720<
Howard Hinnant1468b662010-11-19 22:17:28 +00001721 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001722 _Tp*
1723>::type
1724__unwrap_iter(move_iterator<_Tp*> __i)
1725{
1726 return __i.base();
1727}
1728
Howard Hinnant499cea12013-08-23 17:37:05 +00001729#if _LIBCPP_DEBUG_LEVEL < 2
1730
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001731template <class _Tp>
1732inline _LIBCPP_INLINE_VISIBILITY
1733typename enable_if
1734<
Howard Hinnant1468b662010-11-19 22:17:28 +00001735 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001736 _Tp*
1737>::type
1738__unwrap_iter(__wrap_iter<_Tp*> __i)
1739{
1740 return __i.base();
1741}
1742
Howard Hinnant499cea12013-08-23 17:37:05 +00001743#endif // _LIBCPP_DEBUG_LEVEL < 2
1744
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001745template <class _InputIterator, class _OutputIterator>
1746inline _LIBCPP_INLINE_VISIBILITY
1747_OutputIterator
1748__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1749{
Eric Fiselierb9919752014-10-27 19:28:20 +00001750 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001751 *__result = *__first;
1752 return __result;
1753}
1754
1755template <class _Tp, class _Up>
1756inline _LIBCPP_INLINE_VISIBILITY
1757typename enable_if
1758<
1759 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001760 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001761 _Up*
1762>::type
1763__copy(_Tp* __first, _Tp* __last, _Up* __result)
1764{
1765 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001766 if (__n > 0)
1767 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001768 return __result + __n;
1769}
1770
1771template <class _InputIterator, class _OutputIterator>
1772inline _LIBCPP_INLINE_VISIBILITY
1773_OutputIterator
1774copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1775{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001776 return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001777}
1778
1779// copy_backward
1780
Howard Hinnantb73568d2013-02-06 21:03:39 +00001781template <class _BidirectionalIterator, class _OutputIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001782inline _LIBCPP_INLINE_VISIBILITY
1783_OutputIterator
Howard Hinnantb73568d2013-02-06 21:03:39 +00001784__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001785{
1786 while (__first != __last)
1787 *--__result = *--__last;
1788 return __result;
1789}
1790
1791template <class _Tp, class _Up>
1792inline _LIBCPP_INLINE_VISIBILITY
1793typename enable_if
1794<
1795 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001796 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001797 _Up*
1798>::type
1799__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1800{
1801 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001802 if (__n > 0)
1803 {
1804 __result -= __n;
1805 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1806 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001807 return __result;
1808}
1809
1810template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1811inline _LIBCPP_INLINE_VISIBILITY
1812_BidirectionalIterator2
1813copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1814 _BidirectionalIterator2 __result)
1815{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001816 return _VSTD::__copy_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001817}
1818
1819// copy_if
1820
1821template<class _InputIterator, class _OutputIterator, class _Predicate>
1822inline _LIBCPP_INLINE_VISIBILITY
1823_OutputIterator
1824copy_if(_InputIterator __first, _InputIterator __last,
1825 _OutputIterator __result, _Predicate __pred)
1826{
1827 for (; __first != __last; ++__first)
1828 {
1829 if (__pred(*__first))
1830 {
1831 *__result = *__first;
1832 ++__result;
1833 }
1834 }
1835 return __result;
1836}
1837
1838// copy_n
1839
1840template<class _InputIterator, class _Size, class _OutputIterator>
1841inline _LIBCPP_INLINE_VISIBILITY
1842typename enable_if
1843<
1844 __is_input_iterator<_InputIterator>::value &&
1845 !__is_random_access_iterator<_InputIterator>::value,
1846 _OutputIterator
1847>::type
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001848copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001849{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001850 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1851 _IntegralSize __n = __orig_n;
Howard Hinnant171869e2011-02-27 20:55:39 +00001852 if (__n > 0)
1853 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001854 *__result = *__first;
Howard Hinnant171869e2011-02-27 20:55:39 +00001855 ++__result;
1856 for (--__n; __n > 0; --__n)
1857 {
1858 ++__first;
1859 *__result = *__first;
1860 ++__result;
1861 }
1862 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001863 return __result;
1864}
1865
1866template<class _InputIterator, class _Size, class _OutputIterator>
1867inline _LIBCPP_INLINE_VISIBILITY
1868typename enable_if
1869<
1870 __is_random_access_iterator<_InputIterator>::value,
1871 _OutputIterator
1872>::type
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001873copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001874{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001875 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1876 _IntegralSize __n = __orig_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001877 return _VSTD::copy(__first, __first + __n, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001878}
1879
1880// move
1881
1882template <class _InputIterator, class _OutputIterator>
1883inline _LIBCPP_INLINE_VISIBILITY
1884_OutputIterator
1885__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1886{
Eric Fiselierb9919752014-10-27 19:28:20 +00001887 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001888 *__result = _VSTD::move(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001889 return __result;
1890}
1891
1892template <class _Tp, class _Up>
1893inline _LIBCPP_INLINE_VISIBILITY
1894typename enable_if
1895<
1896 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001897 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001898 _Up*
1899>::type
1900__move(_Tp* __first, _Tp* __last, _Up* __result)
1901{
1902 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001903 if (__n > 0)
1904 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001905 return __result + __n;
1906}
1907
1908template <class _InputIterator, class _OutputIterator>
1909inline _LIBCPP_INLINE_VISIBILITY
1910_OutputIterator
1911move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1912{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001913 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001914}
1915
1916// move_backward
1917
1918template <class _InputIterator, class _OutputIterator>
1919inline _LIBCPP_INLINE_VISIBILITY
1920_OutputIterator
1921__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1922{
1923 while (__first != __last)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001924 *--__result = _VSTD::move(*--__last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001925 return __result;
1926}
1927
1928template <class _Tp, class _Up>
1929inline _LIBCPP_INLINE_VISIBILITY
1930typename enable_if
1931<
1932 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001933 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001934 _Up*
1935>::type
1936__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1937{
1938 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001939 if (__n > 0)
1940 {
1941 __result -= __n;
1942 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1943 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001944 return __result;
1945}
1946
1947template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1948inline _LIBCPP_INLINE_VISIBILITY
1949_BidirectionalIterator2
1950move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1951 _BidirectionalIterator2 __result)
1952{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001953 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001954}
1955
1956// iter_swap
1957
Howard Hinnante9b2c2d2011-05-27 15:04:19 +00001958// moved to <type_traits> for better swap / noexcept support
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001959
1960// transform
1961
1962template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
1963inline _LIBCPP_INLINE_VISIBILITY
1964_OutputIterator
1965transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1966{
Eric Fiselierb9919752014-10-27 19:28:20 +00001967 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001968 *__result = __op(*__first);
1969 return __result;
1970}
1971
1972template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
1973inline _LIBCPP_INLINE_VISIBILITY
1974_OutputIterator
1975transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1976 _OutputIterator __result, _BinaryOperation __binary_op)
1977{
Eric Fiselierb9919752014-10-27 19:28:20 +00001978 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001979 *__result = __binary_op(*__first1, *__first2);
1980 return __result;
1981}
1982
1983// replace
1984
1985template <class _ForwardIterator, class _Tp>
1986inline _LIBCPP_INLINE_VISIBILITY
1987void
1988replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1989{
1990 for (; __first != __last; ++__first)
1991 if (*__first == __old_value)
1992 *__first = __new_value;
1993}
1994
1995// replace_if
1996
1997template <class _ForwardIterator, class _Predicate, class _Tp>
1998inline _LIBCPP_INLINE_VISIBILITY
1999void
2000replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
2001{
2002 for (; __first != __last; ++__first)
2003 if (__pred(*__first))
2004 *__first = __new_value;
2005}
2006
2007// replace_copy
2008
2009template <class _InputIterator, class _OutputIterator, class _Tp>
2010inline _LIBCPP_INLINE_VISIBILITY
2011_OutputIterator
2012replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2013 const _Tp& __old_value, const _Tp& __new_value)
2014{
Eric Fiselierb9919752014-10-27 19:28:20 +00002015 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002016 if (*__first == __old_value)
2017 *__result = __new_value;
2018 else
2019 *__result = *__first;
2020 return __result;
2021}
2022
2023// replace_copy_if
2024
2025template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
2026inline _LIBCPP_INLINE_VISIBILITY
2027_OutputIterator
2028replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2029 _Predicate __pred, const _Tp& __new_value)
2030{
Eric Fiselierb9919752014-10-27 19:28:20 +00002031 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002032 if (__pred(*__first))
2033 *__result = __new_value;
2034 else
2035 *__result = *__first;
2036 return __result;
2037}
2038
2039// fill_n
2040
2041template <class _OutputIterator, class _Size, class _Tp>
2042inline _LIBCPP_INLINE_VISIBILITY
2043_OutputIterator
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002044__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002045{
Eric Fiselierb9919752014-10-27 19:28:20 +00002046 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnant78b68282011-10-22 20:59:45 +00002047 *__first = __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002048 return __first;
2049}
2050
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002051template <class _Tp, class _Size, class _Up>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002052inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002053typename enable_if
2054<
2055 is_integral<_Tp>::value && sizeof(_Tp) == 1 &&
2056 !is_same<_Tp, bool>::value &&
2057 is_integral<_Up>::value && sizeof(_Up) == 1,
2058 _Tp*
2059>::type
2060__fill_n(_Tp* __first, _Size __n,_Up __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002061{
2062 if (__n > 0)
Howard Hinnant78b68282011-10-22 20:59:45 +00002063 _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002064 return __first + __n;
2065}
2066
2067template <class _OutputIterator, class _Size, class _Tp>
2068inline _LIBCPP_INLINE_VISIBILITY
2069_OutputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002070fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002071{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002072 return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002073}
2074
2075// fill
2076
2077template <class _ForwardIterator, class _Tp>
2078inline _LIBCPP_INLINE_VISIBILITY
2079void
Howard Hinnant78b68282011-10-22 20:59:45 +00002080__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002081{
2082 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +00002083 *__first = __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002084}
2085
2086template <class _RandomAccessIterator, class _Tp>
2087inline _LIBCPP_INLINE_VISIBILITY
2088void
Howard Hinnant78b68282011-10-22 20:59:45 +00002089__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002090{
Howard Hinnant78b68282011-10-22 20:59:45 +00002091 _VSTD::fill_n(__first, __last - __first, __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002092}
2093
2094template <class _ForwardIterator, class _Tp>
2095inline _LIBCPP_INLINE_VISIBILITY
2096void
Howard Hinnant78b68282011-10-22 20:59:45 +00002097fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002098{
Howard Hinnant78b68282011-10-22 20:59:45 +00002099 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002100}
2101
2102// generate
2103
2104template <class _ForwardIterator, class _Generator>
2105inline _LIBCPP_INLINE_VISIBILITY
2106void
2107generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2108{
2109 for (; __first != __last; ++__first)
2110 *__first = __gen();
2111}
2112
2113// generate_n
2114
2115template <class _OutputIterator, class _Size, class _Generator>
2116inline _LIBCPP_INLINE_VISIBILITY
2117_OutputIterator
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002118generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002119{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002120 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
2121 _IntegralSize __n = __orig_n;
Eric Fiselierb9919752014-10-27 19:28:20 +00002122 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002123 *__first = __gen();
2124 return __first;
2125}
2126
2127// remove
2128
2129template <class _ForwardIterator, class _Tp>
2130_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002131remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002132{
Howard Hinnant78b68282011-10-22 20:59:45 +00002133 __first = _VSTD::find(__first, __last, __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002134 if (__first != __last)
2135 {
2136 _ForwardIterator __i = __first;
2137 while (++__i != __last)
2138 {
Howard Hinnant78b68282011-10-22 20:59:45 +00002139 if (!(*__i == __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002140 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002141 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002142 ++__first;
2143 }
2144 }
2145 }
2146 return __first;
2147}
2148
2149// remove_if
2150
2151template <class _ForwardIterator, class _Predicate>
2152_ForwardIterator
2153remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2154{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002155 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002156 (__first, __last, __pred);
2157 if (__first != __last)
2158 {
2159 _ForwardIterator __i = __first;
2160 while (++__i != __last)
2161 {
2162 if (!__pred(*__i))
2163 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002164 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002165 ++__first;
2166 }
2167 }
2168 }
2169 return __first;
2170}
2171
2172// remove_copy
2173
2174template <class _InputIterator, class _OutputIterator, class _Tp>
2175inline _LIBCPP_INLINE_VISIBILITY
2176_OutputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002177remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002178{
2179 for (; __first != __last; ++__first)
2180 {
Howard Hinnant78b68282011-10-22 20:59:45 +00002181 if (!(*__first == __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002182 {
2183 *__result = *__first;
2184 ++__result;
2185 }
2186 }
2187 return __result;
2188}
2189
2190// remove_copy_if
2191
2192template <class _InputIterator, class _OutputIterator, class _Predicate>
2193inline _LIBCPP_INLINE_VISIBILITY
2194_OutputIterator
2195remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2196{
2197 for (; __first != __last; ++__first)
2198 {
2199 if (!__pred(*__first))
2200 {
2201 *__result = *__first;
2202 ++__result;
2203 }
2204 }
2205 return __result;
2206}
2207
2208// unique
2209
2210template <class _ForwardIterator, class _BinaryPredicate>
2211_ForwardIterator
2212unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2213{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002214 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002215 (__first, __last, __pred);
2216 if (__first != __last)
2217 {
2218 // ... a a ? ...
2219 // f i
2220 _ForwardIterator __i = __first;
2221 for (++__i; ++__i != __last;)
2222 if (!__pred(*__first, *__i))
Howard Hinnant0949eed2011-06-30 21:18:19 +00002223 *++__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002224 ++__first;
2225 }
2226 return __first;
2227}
2228
2229template <class _ForwardIterator>
2230inline _LIBCPP_INLINE_VISIBILITY
2231_ForwardIterator
2232unique(_ForwardIterator __first, _ForwardIterator __last)
2233{
2234 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002235 return _VSTD::unique(__first, __last, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002236}
2237
2238// unique_copy
2239
2240template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
2241_OutputIterator
2242__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2243 input_iterator_tag, output_iterator_tag)
2244{
2245 if (__first != __last)
2246 {
2247 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2248 *__result = __t;
2249 ++__result;
2250 while (++__first != __last)
2251 {
2252 if (!__pred(__t, *__first))
2253 {
2254 __t = *__first;
2255 *__result = __t;
2256 ++__result;
2257 }
2258 }
2259 }
2260 return __result;
2261}
2262
2263template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
2264_OutputIterator
2265__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2266 forward_iterator_tag, output_iterator_tag)
2267{
2268 if (__first != __last)
2269 {
2270 _ForwardIterator __i = __first;
2271 *__result = *__i;
2272 ++__result;
2273 while (++__first != __last)
2274 {
2275 if (!__pred(*__i, *__first))
2276 {
2277 *__result = *__first;
2278 ++__result;
2279 __i = __first;
2280 }
2281 }
2282 }
2283 return __result;
2284}
2285
2286template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
2287_ForwardIterator
2288__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2289 input_iterator_tag, forward_iterator_tag)
2290{
2291 if (__first != __last)
2292 {
2293 *__result = *__first;
2294 while (++__first != __last)
2295 if (!__pred(*__result, *__first))
2296 *++__result = *__first;
2297 ++__result;
2298 }
2299 return __result;
2300}
2301
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002302template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
2303inline _LIBCPP_INLINE_VISIBILITY
2304_OutputIterator
2305unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2306{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002307 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002308 (__first, __last, __result, __pred,
2309 typename iterator_traits<_InputIterator>::iterator_category(),
2310 typename iterator_traits<_OutputIterator>::iterator_category());
2311}
2312
2313template <class _InputIterator, class _OutputIterator>
2314inline _LIBCPP_INLINE_VISIBILITY
2315_OutputIterator
2316unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2317{
2318 typedef typename iterator_traits<_InputIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002319 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002320}
2321
2322// reverse
2323
2324template <class _BidirectionalIterator>
2325inline _LIBCPP_INLINE_VISIBILITY
2326void
2327__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2328{
2329 while (__first != __last)
2330 {
2331 if (__first == --__last)
2332 break;
2333 swap(*__first, *__last);
2334 ++__first;
2335 }
2336}
2337
2338template <class _RandomAccessIterator>
2339inline _LIBCPP_INLINE_VISIBILITY
2340void
2341__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2342{
2343 if (__first != __last)
2344 for (; __first < --__last; ++__first)
2345 swap(*__first, *__last);
2346}
2347
2348template <class _BidirectionalIterator>
2349inline _LIBCPP_INLINE_VISIBILITY
2350void
2351reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2352{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002353 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002354}
2355
2356// reverse_copy
2357
2358template <class _BidirectionalIterator, class _OutputIterator>
2359inline _LIBCPP_INLINE_VISIBILITY
2360_OutputIterator
2361reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2362{
2363 for (; __first != __last; ++__result)
2364 *__result = *--__last;
2365 return __result;
2366}
2367
2368// rotate
2369
2370template <class _ForwardIterator>
2371_ForwardIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002372__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002373{
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002374 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2375 value_type __tmp = _VSTD::move(*__first);
2376 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2377 *__lm1 = _VSTD::move(__tmp);
2378 return __lm1;
2379}
2380
2381template <class _BidirectionalIterator>
2382_BidirectionalIterator
2383__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2384{
2385 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2386 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2387 value_type __tmp = _VSTD::move(*__lm1);
2388 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2389 *__first = _VSTD::move(__tmp);
2390 return __fp1;
2391}
2392
2393template <class _ForwardIterator>
2394_ForwardIterator
2395__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2396{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002397 _ForwardIterator __i = __middle;
2398 while (true)
2399 {
2400 swap(*__first, *__i);
2401 ++__first;
2402 if (++__i == __last)
2403 break;
2404 if (__first == __middle)
2405 __middle = __i;
2406 }
2407 _ForwardIterator __r = __first;
2408 if (__first != __middle)
2409 {
2410 __i = __middle;
2411 while (true)
2412 {
2413 swap(*__first, *__i);
2414 ++__first;
2415 if (++__i == __last)
2416 {
2417 if (__first == __middle)
2418 break;
2419 __i = __middle;
2420 }
2421 else if (__first == __middle)
2422 __middle = __i;
2423 }
2424 }
2425 return __r;
2426}
2427
2428template<typename _Integral>
2429inline _LIBCPP_INLINE_VISIBILITY
2430_Integral
2431__gcd(_Integral __x, _Integral __y)
2432{
2433 do
2434 {
2435 _Integral __t = __x % __y;
2436 __x = __y;
2437 __y = __t;
2438 } while (__y);
2439 return __x;
2440}
2441
2442template<typename _RandomAccessIterator>
2443_RandomAccessIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002444__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002445{
2446 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2447 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant324bb032010-08-22 00:02:43 +00002448
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002449 const difference_type __m1 = __middle - __first;
2450 const difference_type __m2 = __last - __middle;
2451 if (__m1 == __m2)
2452 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002453 _VSTD::swap_ranges(__first, __middle, __middle);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002454 return __middle;
2455 }
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002456 const difference_type __g = _VSTD::__gcd(__m1, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002457 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2458 {
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002459 value_type __t(_VSTD::move(*--__p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002460 _RandomAccessIterator __p1 = __p;
2461 _RandomAccessIterator __p2 = __p1 + __m1;
2462 do
2463 {
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002464 *__p1 = _VSTD::move(*__p2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002465 __p1 = __p2;
2466 const difference_type __d = __last - __p2;
2467 if (__m1 < __d)
2468 __p2 += __m1;
2469 else
2470 __p2 = __first + (__m1 - __d);
2471 } while (__p2 != __p);
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002472 *__p1 = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002473 }
2474 return __first + __m2;
2475}
2476
2477template <class _ForwardIterator>
2478inline _LIBCPP_INLINE_VISIBILITY
2479_ForwardIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002480__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2481 _VSTD::forward_iterator_tag)
2482{
2483 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2484 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2485 {
2486 if (_VSTD::next(__first) == __middle)
2487 return _VSTD::__rotate_left(__first, __last);
2488 }
2489 return _VSTD::__rotate_forward(__first, __middle, __last);
2490}
2491
2492template <class _BidirectionalIterator>
2493inline _LIBCPP_INLINE_VISIBILITY
2494_BidirectionalIterator
2495__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2496 _VSTD::bidirectional_iterator_tag)
2497{
2498 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2499 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2500 {
2501 if (_VSTD::next(__first) == __middle)
2502 return _VSTD::__rotate_left(__first, __last);
2503 if (_VSTD::next(__middle) == __last)
2504 return _VSTD::__rotate_right(__first, __last);
2505 }
2506 return _VSTD::__rotate_forward(__first, __middle, __last);
2507}
2508
2509template <class _RandomAccessIterator>
2510inline _LIBCPP_INLINE_VISIBILITY
2511_RandomAccessIterator
2512__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2513 _VSTD::random_access_iterator_tag)
2514{
2515 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2516 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2517 {
2518 if (_VSTD::next(__first) == __middle)
2519 return _VSTD::__rotate_left(__first, __last);
2520 if (_VSTD::next(__middle) == __last)
2521 return _VSTD::__rotate_right(__first, __last);
2522 return _VSTD::__rotate_gcd(__first, __middle, __last);
2523 }
2524 return _VSTD::__rotate_forward(__first, __middle, __last);
2525}
2526
2527template <class _ForwardIterator>
2528inline _LIBCPP_INLINE_VISIBILITY
2529_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002530rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2531{
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002532 if (__first == __middle)
2533 return __last;
2534 if (__middle == __last)
2535 return __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002536 return _VSTD::__rotate(__first, __middle, __last,
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002537 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002538}
2539
2540// rotate_copy
2541
2542template <class _ForwardIterator, class _OutputIterator>
2543inline _LIBCPP_INLINE_VISIBILITY
2544_OutputIterator
2545rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2546{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002547 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002548}
2549
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002550// min_element
2551
2552template <class _ForwardIterator, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002553inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002554_ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +00002555min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002556{
2557 if (__first != __last)
2558 {
2559 _ForwardIterator __i = __first;
2560 while (++__i != __last)
2561 if (__comp(*__i, *__first))
2562 __first = __i;
2563 }
2564 return __first;
2565}
2566
2567template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002568inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002569_ForwardIterator
2570min_element(_ForwardIterator __first, _ForwardIterator __last)
2571{
Marshall Clow928735a2015-05-10 13:53:31 +00002572 return _VSTD::min_element(__first, __last,
Howard Hinnant98e5d972010-08-21 20:10:01 +00002573 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2574}
2575
2576// min
2577
2578template <class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002579inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002580const _Tp&
2581min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2582{
2583 return __comp(__b, __a) ? __b : __a;
2584}
2585
2586template <class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002587inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002588const _Tp&
2589min(const _Tp& __a, const _Tp& __b)
2590{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002591 return _VSTD::min(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002592}
2593
Howard Hinnante3e32912011-08-12 21:56:02 +00002594#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2595
Howard Hinnant98e5d972010-08-21 20:10:01 +00002596template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002597inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002598_Tp
2599min(initializer_list<_Tp> __t, _Compare __comp)
2600{
Marshall Clow928735a2015-05-10 13:53:31 +00002601 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002602}
2603
2604template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002605inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002606_Tp
2607min(initializer_list<_Tp> __t)
2608{
Marshall Clow928735a2015-05-10 13:53:31 +00002609 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002610}
2611
Howard Hinnante3e32912011-08-12 21:56:02 +00002612#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2613
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002614// max_element
2615
2616template <class _ForwardIterator, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002617inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002618_ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +00002619max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002620{
2621 if (__first != __last)
2622 {
2623 _ForwardIterator __i = __first;
2624 while (++__i != __last)
2625 if (__comp(*__first, *__i))
2626 __first = __i;
2627 }
2628 return __first;
2629}
2630
Marshall Clow9d9463a2014-02-19 16:51:35 +00002631
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002632template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002633inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002634_ForwardIterator
2635max_element(_ForwardIterator __first, _ForwardIterator __last)
2636{
Marshall Clow928735a2015-05-10 13:53:31 +00002637 return _VSTD::max_element(__first, __last,
Howard Hinnant98e5d972010-08-21 20:10:01 +00002638 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2639}
2640
2641// max
2642
2643template <class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002644inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002645const _Tp&
2646max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2647{
2648 return __comp(__a, __b) ? __b : __a;
2649}
2650
2651template <class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002652inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002653const _Tp&
2654max(const _Tp& __a, const _Tp& __b)
2655{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002656 return _VSTD::max(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002657}
2658
Howard Hinnante3e32912011-08-12 21:56:02 +00002659#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2660
Howard Hinnant98e5d972010-08-21 20:10:01 +00002661template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002662inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002663_Tp
2664max(initializer_list<_Tp> __t, _Compare __comp)
2665{
Marshall Clow928735a2015-05-10 13:53:31 +00002666 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002667}
2668
2669template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002670inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002671_Tp
2672max(initializer_list<_Tp> __t)
2673{
Marshall Clow928735a2015-05-10 13:53:31 +00002674 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002675}
2676
Howard Hinnante3e32912011-08-12 21:56:02 +00002677#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2678
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002679// minmax_element
2680
2681template <class _ForwardIterator, class _Compare>
Marshall Clow928735a2015-05-10 13:53:31 +00002682_LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002683std::pair<_ForwardIterator, _ForwardIterator>
2684minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2685{
2686 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2687 if (__first != __last)
2688 {
2689 if (++__first != __last)
2690 {
2691 if (__comp(*__first, *__result.first))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002692 __result.first = __first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002693 else
2694 __result.second = __first;
2695 while (++__first != __last)
2696 {
2697 _ForwardIterator __i = __first;
2698 if (++__first == __last)
2699 {
2700 if (__comp(*__i, *__result.first))
2701 __result.first = __i;
2702 else if (!__comp(*__i, *__result.second))
2703 __result.second = __i;
2704 break;
2705 }
2706 else
2707 {
2708 if (__comp(*__first, *__i))
2709 {
2710 if (__comp(*__first, *__result.first))
2711 __result.first = __first;
2712 if (!__comp(*__i, *__result.second))
2713 __result.second = __i;
2714 }
2715 else
2716 {
2717 if (__comp(*__i, *__result.first))
2718 __result.first = __i;
2719 if (!__comp(*__first, *__result.second))
2720 __result.second = __first;
2721 }
2722 }
2723 }
2724 }
2725 }
2726 return __result;
2727}
2728
2729template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002730inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002731std::pair<_ForwardIterator, _ForwardIterator>
2732minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2733{
Marshall Clow9d9463a2014-02-19 16:51:35 +00002734 return _VSTD::minmax_element(__first, __last,
2735 __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002736}
2737
Howard Hinnant98e5d972010-08-21 20:10:01 +00002738// minmax
2739
2740template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002741inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002742pair<const _Tp&, const _Tp&>
2743minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2744{
2745 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2746 pair<const _Tp&, const _Tp&>(__a, __b);
2747}
2748
2749template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002750inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002751pair<const _Tp&, const _Tp&>
2752minmax(const _Tp& __a, const _Tp& __b)
2753{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002754 return _VSTD::minmax(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002755}
2756
Howard Hinnante3e32912011-08-12 21:56:02 +00002757#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2758
Howard Hinnant98e5d972010-08-21 20:10:01 +00002759template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002760inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002761pair<_Tp, _Tp>
2762minmax(initializer_list<_Tp> __t, _Compare __comp)
2763{
Marshall Clow9d9463a2014-02-19 16:51:35 +00002764 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2765 _Iter __first = __t.begin();
2766 _Iter __last = __t.end();
Marshall Clow3024f862015-02-11 15:41:34 +00002767 std::pair<_Tp, _Tp> __result(*__first, *__first);
Marshall Clow9d9463a2014-02-19 16:51:35 +00002768
2769 ++__first;
2770 if (__t.size() % 2 == 0)
2771 {
2772 if (__comp(*__first, __result.first))
2773 __result.first = *__first;
2774 else
2775 __result.second = *__first;
2776 ++__first;
2777 }
2778
2779 while (__first != __last)
2780 {
2781 _Tp __prev = *__first++;
Marshall Clow3024f862015-02-11 15:41:34 +00002782 if (__comp(*__first, __prev)) {
2783 if ( __comp(*__first, __result.first)) __result.first = *__first;
2784 if (!__comp(__prev, __result.second)) __result.second = __prev;
Marshall Clow9d9463a2014-02-19 16:51:35 +00002785 }
2786 else {
Marshall Clow3024f862015-02-11 15:41:34 +00002787 if ( __comp(__prev, __result.first)) __result.first = __prev;
2788 if (!__comp(*__first, __result.second)) __result.second = *__first;
Marshall Clow9d9463a2014-02-19 16:51:35 +00002789 }
2790
2791 __first++;
2792 }
2793 return __result;
2794}
2795
2796template<class _Tp>
2797inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2798pair<_Tp, _Tp>
2799minmax(initializer_list<_Tp> __t)
2800{
2801 return _VSTD::minmax(__t, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002802}
2803
Howard Hinnante3e32912011-08-12 21:56:02 +00002804#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2805
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002806// random_shuffle
2807
Howard Hinnantc3267212010-05-26 17:49:34 +00002808// __independent_bits_engine
2809
Howard Hinnant99968442011-11-29 18:15:50 +00002810template <unsigned long long _Xp, size_t _Rp>
Howard Hinnantc3267212010-05-26 17:49:34 +00002811struct __log2_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002812{
Howard Hinnant99968442011-11-29 18:15:50 +00002813 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2814 : __log2_imp<_Xp, _Rp - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002815};
2816
Howard Hinnant99968442011-11-29 18:15:50 +00002817template <unsigned long long _Xp>
2818struct __log2_imp<_Xp, 0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002819{
Howard Hinnantc3267212010-05-26 17:49:34 +00002820 static const size_t value = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002821};
2822
Howard Hinnant99968442011-11-29 18:15:50 +00002823template <size_t _Rp>
2824struct __log2_imp<0, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002825{
Howard Hinnant99968442011-11-29 18:15:50 +00002826 static const size_t value = _Rp + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002827};
2828
Howard Hinnant99968442011-11-29 18:15:50 +00002829template <class _UI, _UI _Xp>
Howard Hinnantc3267212010-05-26 17:49:34 +00002830struct __log2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002831{
Howard Hinnant99968442011-11-29 18:15:50 +00002832 static const size_t value = __log2_imp<_Xp,
Howard Hinnantc3267212010-05-26 17:49:34 +00002833 sizeof(_UI) * __CHAR_BIT__ - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002834};
2835
Howard Hinnantc3267212010-05-26 17:49:34 +00002836template<class _Engine, class _UIntType>
2837class __independent_bits_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002838{
Howard Hinnantc3267212010-05-26 17:49:34 +00002839public:
2840 // types
2841 typedef _UIntType result_type;
2842
2843private:
2844 typedef typename _Engine::result_type _Engine_result_type;
2845 typedef typename conditional
2846 <
2847 sizeof(_Engine_result_type) <= sizeof(result_type),
2848 result_type,
2849 _Engine_result_type
2850 >::type _Working_result_type;
2851
2852 _Engine& __e_;
2853 size_t __w_;
2854 size_t __w0_;
2855 size_t __n_;
2856 size_t __n0_;
2857 _Working_result_type __y0_;
2858 _Working_result_type __y1_;
2859 _Engine_result_type __mask0_;
2860 _Engine_result_type __mask1_;
2861
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002862#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnant99968442011-11-29 18:15:50 +00002863 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002864 + _Working_result_type(1);
2865#else
2866 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2867 + _Working_result_type(1);
2868#endif
2869 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2870 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2871 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
Howard Hinnantc3267212010-05-26 17:49:34 +00002872
2873public:
2874 // constructors and seeding functions
2875 __independent_bits_engine(_Engine& __e, size_t __w);
2876
2877 // generating functions
Howard Hinnant99968442011-11-29 18:15:50 +00002878 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantc3267212010-05-26 17:49:34 +00002879
2880private:
2881 result_type __eval(false_type);
2882 result_type __eval(true_type);
2883};
2884
2885template<class _Engine, class _UIntType>
2886__independent_bits_engine<_Engine, _UIntType>
2887 ::__independent_bits_engine(_Engine& __e, size_t __w)
2888 : __e_(__e),
2889 __w_(__w)
2890{
2891 __n_ = __w_ / __m + (__w_ % __m != 0);
2892 __w0_ = __w_ / __n_;
Howard Hinnant99968442011-11-29 18:15:50 +00002893 if (_Rp == 0)
2894 __y0_ = _Rp;
Howard Hinnantc3267212010-05-26 17:49:34 +00002895 else if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00002896 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002897 else
2898 __y0_ = 0;
Howard Hinnant99968442011-11-29 18:15:50 +00002899 if (_Rp - __y0_ > __y0_ / __n_)
Howard Hinnantc3267212010-05-26 17:49:34 +00002900 {
2901 ++__n_;
2902 __w0_ = __w_ / __n_;
2903 if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00002904 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002905 else
2906 __y0_ = 0;
2907 }
2908 __n0_ = __n_ - __w_ % __n_;
2909 if (__w0_ < _WDt - 1)
Howard Hinnant99968442011-11-29 18:15:50 +00002910 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
Howard Hinnantc3267212010-05-26 17:49:34 +00002911 else
2912 __y1_ = 0;
2913 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2914 _Engine_result_type(0);
2915 __mask1_ = __w0_ < _EDt - 1 ?
2916 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2917 _Engine_result_type(~0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002918}
2919
Howard Hinnantc3267212010-05-26 17:49:34 +00002920template<class _Engine, class _UIntType>
2921inline
2922_UIntType
2923__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002924{
Howard Hinnantc3267212010-05-26 17:49:34 +00002925 return static_cast<result_type>(__e_() & __mask0_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002926}
2927
Howard Hinnantc3267212010-05-26 17:49:34 +00002928template<class _Engine, class _UIntType>
2929_UIntType
2930__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002931{
Howard Hinnant99968442011-11-29 18:15:50 +00002932 result_type _Sp = 0;
Howard Hinnantc3267212010-05-26 17:49:34 +00002933 for (size_t __k = 0; __k < __n0_; ++__k)
2934 {
2935 _Engine_result_type __u;
2936 do
2937 {
2938 __u = __e_() - _Engine::min();
2939 } while (__u >= __y0_);
Howard Hinnant8faa95f2011-10-27 16:12:10 +00002940 if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00002941 _Sp <<= __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002942 else
Howard Hinnant99968442011-11-29 18:15:50 +00002943 _Sp = 0;
2944 _Sp += __u & __mask0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002945 }
2946 for (size_t __k = __n0_; __k < __n_; ++__k)
2947 {
2948 _Engine_result_type __u;
2949 do
2950 {
2951 __u = __e_() - _Engine::min();
2952 } while (__u >= __y1_);
Howard Hinnant8faa95f2011-10-27 16:12:10 +00002953 if (__w0_ < _WDt - 1)
Howard Hinnant99968442011-11-29 18:15:50 +00002954 _Sp <<= __w0_ + 1;
Howard Hinnantc3267212010-05-26 17:49:34 +00002955 else
Howard Hinnant99968442011-11-29 18:15:50 +00002956 _Sp = 0;
2957 _Sp += __u & __mask1_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002958 }
Howard Hinnant99968442011-11-29 18:15:50 +00002959 return _Sp;
Howard Hinnantc3267212010-05-26 17:49:34 +00002960}
2961
2962// uniform_int_distribution
2963
2964template<class _IntType = int>
2965class uniform_int_distribution
2966{
2967public:
2968 // types
2969 typedef _IntType result_type;
2970
2971 class param_type
2972 {
2973 result_type __a_;
2974 result_type __b_;
2975 public:
2976 typedef uniform_int_distribution distribution_type;
2977
2978 explicit param_type(result_type __a = 0,
2979 result_type __b = numeric_limits<result_type>::max())
2980 : __a_(__a), __b_(__b) {}
2981
2982 result_type a() const {return __a_;}
2983 result_type b() const {return __b_;}
2984
2985 friend bool operator==(const param_type& __x, const param_type& __y)
2986 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2987 friend bool operator!=(const param_type& __x, const param_type& __y)
2988 {return !(__x == __y);}
2989 };
2990
2991private:
2992 param_type __p_;
2993
2994public:
2995 // constructors and reset functions
2996 explicit uniform_int_distribution(result_type __a = 0,
2997 result_type __b = numeric_limits<result_type>::max())
2998 : __p_(param_type(__a, __b)) {}
2999 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
3000 void reset() {}
3001
3002 // generating functions
3003 template<class _URNG> result_type operator()(_URNG& __g)
3004 {return (*this)(__g, __p_);}
3005 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3006
3007 // property functions
3008 result_type a() const {return __p_.a();}
3009 result_type b() const {return __p_.b();}
3010
3011 param_type param() const {return __p_;}
3012 void param(const param_type& __p) {__p_ = __p;}
3013
3014 result_type min() const {return a();}
3015 result_type max() const {return b();}
3016
3017 friend bool operator==(const uniform_int_distribution& __x,
3018 const uniform_int_distribution& __y)
3019 {return __x.__p_ == __y.__p_;}
3020 friend bool operator!=(const uniform_int_distribution& __x,
3021 const uniform_int_distribution& __y)
3022 {return !(__x == __y);}
3023};
3024
3025template<class _IntType>
3026template<class _URNG>
3027typename uniform_int_distribution<_IntType>::result_type
3028uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3029{
3030 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3031 uint32_t, uint64_t>::type _UIntType;
Howard Hinnant99968442011-11-29 18:15:50 +00003032 const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
3033 if (_Rp == 1)
Howard Hinnantc3267212010-05-26 17:49:34 +00003034 return __p.a();
3035 const size_t _Dt = numeric_limits<_UIntType>::digits;
3036 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
Howard Hinnant99968442011-11-29 18:15:50 +00003037 if (_Rp == 0)
Howard Hinnantc3267212010-05-26 17:49:34 +00003038 return static_cast<result_type>(_Eng(__g, _Dt)());
Howard Hinnant99968442011-11-29 18:15:50 +00003039 size_t __w = _Dt - __clz(_Rp) - 1;
3040 if ((_Rp & (_UIntType(~0) >> (_Dt - __w))) != 0)
Howard Hinnantc3267212010-05-26 17:49:34 +00003041 ++__w;
3042 _Eng __e(__g, __w);
3043 _UIntType __u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003044 do
Howard Hinnantc3267212010-05-26 17:49:34 +00003045 {
3046 __u = __e();
Howard Hinnant99968442011-11-29 18:15:50 +00003047 } while (__u >= _Rp);
Howard Hinnantc3267212010-05-26 17:49:34 +00003048 return static_cast<result_type>(__u + __p.a());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003049}
3050
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003051class _LIBCPP_TYPE_VIS __rs_default;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003052
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003053_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantc3267212010-05-26 17:49:34 +00003054
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003055class _LIBCPP_TYPE_VIS __rs_default
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003056{
Howard Hinnantc3267212010-05-26 17:49:34 +00003057 static unsigned __c_;
3058
3059 __rs_default();
3060public:
Marshall Clow5920cfc2013-02-07 22:12:02 +00003061 typedef uint_fast32_t result_type;
Howard Hinnantc3267212010-05-26 17:49:34 +00003062
3063 static const result_type _Min = 0;
3064 static const result_type _Max = 0xFFFFFFFF;
3065
3066 __rs_default(const __rs_default&);
3067 ~__rs_default();
3068
3069 result_type operator()();
3070
Howard Hinnant27b4fd32012-04-02 00:40:41 +00003071 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
3072 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
Howard Hinnantc3267212010-05-26 17:49:34 +00003073
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003074 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003075};
3076
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003077_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003078
3079template <class _RandomAccessIterator>
3080void
3081random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
3082{
3083 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnant99968442011-11-29 18:15:50 +00003084 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3085 typedef typename _Dp::param_type _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003086 difference_type __d = __last - __first;
3087 if (__d > 1)
3088 {
Howard Hinnant99968442011-11-29 18:15:50 +00003089 _Dp __uid;
Howard Hinnantc3267212010-05-26 17:49:34 +00003090 __rs_default __g = __rs_get();
3091 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003092 {
Howard Hinnant99968442011-11-29 18:15:50 +00003093 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant4e599482010-10-22 15:26:39 +00003094 if (__i != difference_type(0))
3095 swap(*__first, *(__first + __i));
3096 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003097 }
3098}
3099
3100template <class _RandomAccessIterator, class _RandomNumberGenerator>
3101void
3102random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnant73d21a42010-09-04 23:28:19 +00003103#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003104 _RandomNumberGenerator&& __rand)
3105#else
3106 _RandomNumberGenerator& __rand)
3107#endif
3108{
3109 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3110 difference_type __d = __last - __first;
3111 if (__d > 1)
3112 {
3113 for (--__last; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003114 {
3115 difference_type __i = __rand(__d);
3116 swap(*__first, *(__first + __i));
3117 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003118 }
3119}
3120
Howard Hinnantc3267212010-05-26 17:49:34 +00003121template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3122 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnant278bf2d2010-11-18 01:47:02 +00003123#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3124 _UniformRandomNumberGenerator&& __g)
3125#else
Howard Hinnantc3267212010-05-26 17:49:34 +00003126 _UniformRandomNumberGenerator& __g)
Howard Hinnant278bf2d2010-11-18 01:47:02 +00003127#endif
Howard Hinnantc3267212010-05-26 17:49:34 +00003128{
3129 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnant99968442011-11-29 18:15:50 +00003130 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3131 typedef typename _Dp::param_type _Pp;
Howard Hinnantc3267212010-05-26 17:49:34 +00003132 difference_type __d = __last - __first;
3133 if (__d > 1)
3134 {
Howard Hinnant99968442011-11-29 18:15:50 +00003135 _Dp __uid;
Howard Hinnantc3267212010-05-26 17:49:34 +00003136 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003137 {
Howard Hinnant99968442011-11-29 18:15:50 +00003138 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant4e599482010-10-22 15:26:39 +00003139 if (__i != difference_type(0))
3140 swap(*__first, *(__first + __i));
3141 }
Howard Hinnantc3267212010-05-26 17:49:34 +00003142 }
3143}
3144
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003145template <class _InputIterator, class _Predicate>
3146bool
3147is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3148{
3149 for (; __first != __last; ++__first)
3150 if (!__pred(*__first))
3151 break;
Marshall Clowa0ec4b72015-02-02 18:16:35 +00003152 if ( __first == __last )
3153 return true;
3154 ++__first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003155 for (; __first != __last; ++__first)
3156 if (__pred(*__first))
3157 return false;
3158 return true;
3159}
3160
3161// partition
3162
3163template <class _Predicate, class _ForwardIterator>
3164_ForwardIterator
3165__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3166{
3167 while (true)
3168 {
3169 if (__first == __last)
3170 return __first;
3171 if (!__pred(*__first))
3172 break;
3173 ++__first;
3174 }
3175 for (_ForwardIterator __p = __first; ++__p != __last;)
3176 {
3177 if (__pred(*__p))
3178 {
3179 swap(*__first, *__p);
3180 ++__first;
3181 }
3182 }
3183 return __first;
3184}
3185
3186template <class _Predicate, class _BidirectionalIterator>
3187_BidirectionalIterator
3188__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3189 bidirectional_iterator_tag)
3190{
3191 while (true)
3192 {
3193 while (true)
3194 {
3195 if (__first == __last)
3196 return __first;
3197 if (!__pred(*__first))
3198 break;
3199 ++__first;
3200 }
3201 do
3202 {
3203 if (__first == --__last)
3204 return __first;
3205 } while (!__pred(*__last));
3206 swap(*__first, *__last);
3207 ++__first;
3208 }
3209}
3210
3211template <class _ForwardIterator, class _Predicate>
3212inline _LIBCPP_INLINE_VISIBILITY
3213_ForwardIterator
3214partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3215{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003216 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003217 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3218}
3219
3220// partition_copy
3221
3222template <class _InputIterator, class _OutputIterator1,
3223 class _OutputIterator2, class _Predicate>
3224pair<_OutputIterator1, _OutputIterator2>
3225partition_copy(_InputIterator __first, _InputIterator __last,
3226 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3227 _Predicate __pred)
3228{
3229 for (; __first != __last; ++__first)
3230 {
3231 if (__pred(*__first))
3232 {
3233 *__out_true = *__first;
3234 ++__out_true;
3235 }
3236 else
3237 {
3238 *__out_false = *__first;
3239 ++__out_false;
3240 }
3241 }
3242 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3243}
3244
3245// partition_point
3246
3247template<class _ForwardIterator, class _Predicate>
3248_ForwardIterator
3249partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3250{
3251 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003252 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003253 while (__len != 0)
3254 {
3255 difference_type __l2 = __len / 2;
3256 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003257 _VSTD::advance(__m, __l2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003258 if (__pred(*__m))
3259 {
3260 __first = ++__m;
3261 __len -= __l2 + 1;
3262 }
3263 else
3264 __len = __l2;
3265 }
3266 return __first;
3267}
3268
3269// stable_partition
3270
3271template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3272_ForwardIterator
3273__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3274 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3275{
3276 // *__first is known to be false
3277 // __len >= 1
3278 if (__len == 1)
3279 return __first;
3280 if (__len == 2)
3281 {
3282 _ForwardIterator __m = __first;
3283 if (__pred(*++__m))
3284 {
3285 swap(*__first, *__m);
3286 return __m;
3287 }
3288 return __first;
3289 }
3290 if (__len <= __p.second)
3291 { // The buffer is big enough to use
3292 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3293 __destruct_n __d(0);
3294 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3295 // Move the falses into the temporary buffer, and the trues to the front of the line
3296 // Update __first to always point to the end of the trues
3297 value_type* __t = __p.first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003298 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003299 __d.__incr((value_type*)0);
3300 ++__t;
3301 _ForwardIterator __i = __first;
3302 while (++__i != __last)
3303 {
3304 if (__pred(*__i))
3305 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003306 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003307 ++__first;
3308 }
3309 else
3310 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003311 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003312 __d.__incr((value_type*)0);
3313 ++__t;
3314 }
3315 }
3316 // All trues now at start of range, all falses in buffer
3317 // Move falses back into range, but don't mess up __first which points to first false
3318 __i = __first;
3319 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003320 *__i = _VSTD::move(*__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003321 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3322 return __first;
3323 }
3324 // Else not enough buffer, do in place
3325 // __len >= 3
3326 _ForwardIterator __m = __first;
3327 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003328 _VSTD::advance(__m, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003329 // recurse on [__first, __m), *__first know to be false
3330 // F?????????????????
3331 // f m l
3332 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3333 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3334 // TTTFFFFF??????????
3335 // f ff m l
3336 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3337 _ForwardIterator __m1 = __m;
3338 _ForwardIterator __second_false = __last;
3339 _Distance __len_half = __len - __len2;
3340 while (__pred(*__m1))
3341 {
3342 if (++__m1 == __last)
3343 goto __second_half_done;
3344 --__len_half;
3345 }
3346 // TTTFFFFFTTTF??????
3347 // f ff m m1 l
3348 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3349__second_half_done:
3350 // TTTFFFFFTTTTTFFFFF
3351 // f ff m sf l
Howard Hinnant0949eed2011-06-30 21:18:19 +00003352 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003353 // TTTTTTTTFFFFFFFFFF
3354 // |
3355}
3356
3357struct __return_temporary_buffer
3358{
3359 template <class _Tp>
Howard Hinnant0949eed2011-06-30 21:18:19 +00003360 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003361};
3362
3363template <class _Predicate, class _ForwardIterator>
3364_ForwardIterator
3365__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3366 forward_iterator_tag)
3367{
3368 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3369 // Either prove all true and return __first or point to first false
3370 while (true)
3371 {
3372 if (__first == __last)
3373 return __first;
3374 if (!__pred(*__first))
3375 break;
3376 ++__first;
3377 }
3378 // We now have a reduced range [__first, __last)
3379 // *__first is known to be false
3380 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3381 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003382 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003383 pair<value_type*, ptrdiff_t> __p(0, 0);
3384 unique_ptr<value_type, __return_temporary_buffer> __h;
3385 if (__len >= __alloc_limit)
3386 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003387 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003388 __h.reset(__p.first);
3389 }
3390 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3391 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3392}
3393
3394template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3395_BidirectionalIterator
3396__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3397 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3398{
3399 // *__first is known to be false
3400 // *__last is known to be true
3401 // __len >= 2
3402 if (__len == 2)
3403 {
3404 swap(*__first, *__last);
3405 return __last;
3406 }
3407 if (__len == 3)
3408 {
3409 _BidirectionalIterator __m = __first;
3410 if (__pred(*++__m))
3411 {
3412 swap(*__first, *__m);
3413 swap(*__m, *__last);
3414 return __last;
3415 }
3416 swap(*__m, *__last);
3417 swap(*__first, *__m);
3418 return __m;
3419 }
3420 if (__len <= __p.second)
3421 { // The buffer is big enough to use
3422 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3423 __destruct_n __d(0);
3424 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3425 // Move the falses into the temporary buffer, and the trues to the front of the line
3426 // Update __first to always point to the end of the trues
3427 value_type* __t = __p.first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003428 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003429 __d.__incr((value_type*)0);
3430 ++__t;
3431 _BidirectionalIterator __i = __first;
3432 while (++__i != __last)
3433 {
3434 if (__pred(*__i))
3435 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003436 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003437 ++__first;
3438 }
3439 else
3440 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003441 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003442 __d.__incr((value_type*)0);
3443 ++__t;
3444 }
3445 }
3446 // move *__last, known to be true
Howard Hinnant0949eed2011-06-30 21:18:19 +00003447 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003448 __i = ++__first;
3449 // All trues now at start of range, all falses in buffer
3450 // Move falses back into range, but don't mess up __first which points to first false
3451 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003452 *__i = _VSTD::move(*__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003453 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3454 return __first;
3455 }
3456 // Else not enough buffer, do in place
3457 // __len >= 4
3458 _BidirectionalIterator __m = __first;
3459 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003460 _VSTD::advance(__m, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003461 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3462 // F????????????????T
3463 // f m l
3464 _BidirectionalIterator __m1 = __m;
3465 _BidirectionalIterator __first_false = __first;
3466 _Distance __len_half = __len2;
3467 while (!__pred(*--__m1))
3468 {
3469 if (__m1 == __first)
3470 goto __first_half_done;
3471 --__len_half;
3472 }
3473 // F???TFFF?????????T
3474 // f m1 m l
3475 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3476 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3477__first_half_done:
3478 // TTTFFFFF?????????T
3479 // f ff m l
3480 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3481 __m1 = __m;
3482 _BidirectionalIterator __second_false = __last;
3483 ++__second_false;
3484 __len_half = __len - __len2;
3485 while (__pred(*__m1))
3486 {
3487 if (++__m1 == __last)
3488 goto __second_half_done;
3489 --__len_half;
3490 }
3491 // TTTFFFFFTTTF?????T
3492 // f ff m m1 l
3493 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3494__second_half_done:
3495 // TTTFFFFFTTTTTFFFFF
3496 // f ff m sf l
Howard Hinnant0949eed2011-06-30 21:18:19 +00003497 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003498 // TTTTTTTTFFFFFFFFFF
3499 // |
3500}
3501
3502template <class _Predicate, class _BidirectionalIterator>
3503_BidirectionalIterator
3504__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3505 bidirectional_iterator_tag)
3506{
3507 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3508 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3509 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3510 // Either prove all true and return __first or point to first false
3511 while (true)
3512 {
3513 if (__first == __last)
3514 return __first;
3515 if (!__pred(*__first))
3516 break;
3517 ++__first;
3518 }
3519 // __first points to first false, everything prior to __first is already set.
3520 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3521 do
3522 {
3523 if (__first == --__last)
3524 return __first;
3525 } while (!__pred(*__last));
3526 // We now have a reduced range [__first, __last]
3527 // *__first is known to be false
3528 // *__last is known to be true
3529 // __len >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003530 difference_type __len = _VSTD::distance(__first, __last) + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003531 pair<value_type*, ptrdiff_t> __p(0, 0);
3532 unique_ptr<value_type, __return_temporary_buffer> __h;
3533 if (__len >= __alloc_limit)
3534 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003535 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003536 __h.reset(__p.first);
3537 }
3538 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3539 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3540}
3541
3542template <class _ForwardIterator, class _Predicate>
3543inline _LIBCPP_INLINE_VISIBILITY
3544_ForwardIterator
3545stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3546{
3547 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3548 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3549}
3550
3551// is_sorted_until
3552
3553template <class _ForwardIterator, class _Compare>
3554_ForwardIterator
3555is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3556{
3557 if (__first != __last)
3558 {
3559 _ForwardIterator __i = __first;
3560 while (++__i != __last)
3561 {
3562 if (__comp(*__i, *__first))
3563 return __i;
3564 __first = __i;
3565 }
3566 }
3567 return __last;
3568}
3569
Howard Hinnant324bb032010-08-22 00:02:43 +00003570template<class _ForwardIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003571inline _LIBCPP_INLINE_VISIBILITY
3572_ForwardIterator
3573is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3574{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003575 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003576}
3577
3578// is_sorted
3579
3580template <class _ForwardIterator, class _Compare>
3581inline _LIBCPP_INLINE_VISIBILITY
3582bool
3583is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3584{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003585 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003586}
3587
Howard Hinnant324bb032010-08-22 00:02:43 +00003588template<class _ForwardIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003589inline _LIBCPP_INLINE_VISIBILITY
3590bool
3591is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3592{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003593 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003594}
3595
3596// sort
3597
3598// stable, 2-3 compares, 0-2 swaps
3599
3600template <class _Compare, class _ForwardIterator>
3601unsigned
3602__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3603{
3604 unsigned __r = 0;
3605 if (!__c(*__y, *__x)) // if x <= y
3606 {
3607 if (!__c(*__z, *__y)) // if y <= z
3608 return __r; // x <= y && y <= z
3609 // x <= y && y > z
3610 swap(*__y, *__z); // x <= z && y < z
3611 __r = 1;
3612 if (__c(*__y, *__x)) // if x > y
3613 {
3614 swap(*__x, *__y); // x < y && y <= z
3615 __r = 2;
3616 }
3617 return __r; // x <= y && y < z
3618 }
3619 if (__c(*__z, *__y)) // x > y, if y > z
3620 {
3621 swap(*__x, *__z); // x < y && y < z
3622 __r = 1;
3623 return __r;
3624 }
3625 swap(*__x, *__y); // x > y && y <= z
3626 __r = 1; // x < y && x <= z
3627 if (__c(*__z, *__y)) // if y > z
3628 {
3629 swap(*__y, *__z); // x <= y && y < z
3630 __r = 2;
3631 }
3632 return __r;
3633} // x <= y && y <= z
3634
3635// stable, 3-6 compares, 0-5 swaps
3636
3637template <class _Compare, class _ForwardIterator>
3638unsigned
3639__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3640 _ForwardIterator __x4, _Compare __c)
3641{
3642 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3643 if (__c(*__x4, *__x3))
3644 {
3645 swap(*__x3, *__x4);
3646 ++__r;
3647 if (__c(*__x3, *__x2))
3648 {
3649 swap(*__x2, *__x3);
3650 ++__r;
3651 if (__c(*__x2, *__x1))
3652 {
3653 swap(*__x1, *__x2);
3654 ++__r;
3655 }
3656 }
3657 }
3658 return __r;
3659}
3660
3661// stable, 4-10 compares, 0-9 swaps
3662
3663template <class _Compare, class _ForwardIterator>
3664unsigned
3665__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3666 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3667{
3668 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3669 if (__c(*__x5, *__x4))
3670 {
3671 swap(*__x4, *__x5);
3672 ++__r;
3673 if (__c(*__x4, *__x3))
3674 {
3675 swap(*__x3, *__x4);
3676 ++__r;
3677 if (__c(*__x3, *__x2))
3678 {
3679 swap(*__x2, *__x3);
3680 ++__r;
3681 if (__c(*__x2, *__x1))
3682 {
3683 swap(*__x1, *__x2);
3684 ++__r;
3685 }
3686 }
3687 }
3688 }
3689 return __r;
3690}
3691
3692// Assumes size > 0
3693template <class _Compare, class _BirdirectionalIterator>
3694void
3695__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3696{
3697 _BirdirectionalIterator __lm1 = __last;
3698 for (--__lm1; __first != __lm1; ++__first)
3699 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003700 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003701 typename add_lvalue_reference<_Compare>::type>
3702 (__first, __last, __comp);
3703 if (__i != __first)
3704 swap(*__first, *__i);
3705 }
3706}
3707
3708template <class _Compare, class _BirdirectionalIterator>
3709void
3710__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3711{
3712 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3713 if (__first != __last)
3714 {
3715 _BirdirectionalIterator __i = __first;
3716 for (++__i; __i != __last; ++__i)
3717 {
3718 _BirdirectionalIterator __j = __i;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003719 value_type __t(_VSTD::move(*__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003720 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003721 *__j = _VSTD::move(*__k);
3722 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003723 }
3724 }
3725}
3726
3727template <class _Compare, class _RandomAccessIterator>
3728void
3729__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3730{
3731 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3732 _RandomAccessIterator __j = __first+2;
3733 __sort3<_Compare>(__first, __first+1, __j, __comp);
3734 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3735 {
3736 if (__comp(*__i, *__j))
3737 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003738 value_type __t(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003739 _RandomAccessIterator __k = __j;
3740 __j = __i;
3741 do
3742 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003743 *__j = _VSTD::move(*__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003744 __j = __k;
3745 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003746 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003747 }
3748 __j = __i;
3749 }
3750}
3751
3752template <class _Compare, class _RandomAccessIterator>
3753bool
3754__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3755{
3756 switch (__last - __first)
3757 {
3758 case 0:
3759 case 1:
3760 return true;
3761 case 2:
3762 if (__comp(*--__last, *__first))
3763 swap(*__first, *__last);
3764 return true;
3765 case 3:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003766 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003767 return true;
3768 case 4:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003769 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003770 return true;
3771 case 5:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003772 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003773 return true;
3774 }
3775 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3776 _RandomAccessIterator __j = __first+2;
3777 __sort3<_Compare>(__first, __first+1, __j, __comp);
3778 const unsigned __limit = 8;
3779 unsigned __count = 0;
3780 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3781 {
3782 if (__comp(*__i, *__j))
3783 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003784 value_type __t(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003785 _RandomAccessIterator __k = __j;
3786 __j = __i;
3787 do
3788 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003789 *__j = _VSTD::move(*__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003790 __j = __k;
3791 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003792 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003793 if (++__count == __limit)
3794 return ++__i == __last;
3795 }
3796 __j = __i;
3797 }
3798 return true;
3799}
3800
3801template <class _Compare, class _BirdirectionalIterator>
3802void
3803__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3804 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3805{
3806 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3807 if (__first1 != __last1)
3808 {
3809 __destruct_n __d(0);
3810 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3811 value_type* __last2 = __first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003812 ::new(__last2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003813 __d.__incr((value_type*)0);
3814 for (++__last2; ++__first1 != __last1; ++__last2)
3815 {
3816 value_type* __j2 = __last2;
3817 value_type* __i2 = __j2;
3818 if (__comp(*__first1, *--__i2))
3819 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003820 ::new(__j2) value_type(_VSTD::move(*__i2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003821 __d.__incr((value_type*)0);
3822 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003823 *__j2 = _VSTD::move(*__i2);
3824 *__j2 = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003825 }
3826 else
3827 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003828 ::new(__j2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003829 __d.__incr((value_type*)0);
3830 }
3831 }
3832 __h.release();
3833 }
3834}
3835
3836template <class _Compare, class _RandomAccessIterator>
3837void
3838__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3839{
3840 // _Compare is known to be a reference type
3841 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3842 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant1468b662010-11-19 22:17:28 +00003843 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3844 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003845 while (true)
3846 {
3847 __restart:
3848 difference_type __len = __last - __first;
3849 switch (__len)
3850 {
3851 case 0:
3852 case 1:
3853 return;
3854 case 2:
3855 if (__comp(*--__last, *__first))
3856 swap(*__first, *__last);
3857 return;
3858 case 3:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003859 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003860 return;
3861 case 4:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003862 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003863 return;
3864 case 5:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003865 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003866 return;
3867 }
3868 if (__len <= __limit)
3869 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003870 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003871 return;
3872 }
3873 // __len > 5
3874 _RandomAccessIterator __m = __first;
3875 _RandomAccessIterator __lm1 = __last;
3876 --__lm1;
3877 unsigned __n_swaps;
3878 {
3879 difference_type __delta;
3880 if (__len >= 1000)
3881 {
3882 __delta = __len/2;
3883 __m += __delta;
3884 __delta /= 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003885 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003886 }
3887 else
3888 {
3889 __delta = __len/2;
3890 __m += __delta;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003891 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003892 }
3893 }
3894 // *__m is median
3895 // partition [__first, __m) < *__m and *__m <= [__m, __last)
3896 // (this inhibits tossing elements equivalent to __m around unnecessarily)
3897 _RandomAccessIterator __i = __first;
3898 _RandomAccessIterator __j = __lm1;
3899 // j points beyond range to be tested, *__m is known to be <= *__lm1
3900 // The search going up is known to be guarded but the search coming down isn't.
3901 // Prime the downward search with a guard.
3902 if (!__comp(*__i, *__m)) // if *__first == *__m
3903 {
3904 // *__first == *__m, *__first doesn't go in first part
3905 // manually guard downward moving __j against __i
3906 while (true)
3907 {
3908 if (__i == --__j)
3909 {
3910 // *__first == *__m, *__m <= all other elements
3911 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
3912 ++__i; // __first + 1
3913 __j = __last;
3914 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
3915 {
3916 while (true)
3917 {
3918 if (__i == __j)
3919 return; // [__first, __last) all equivalent elements
3920 if (__comp(*__first, *__i))
3921 {
3922 swap(*__i, *__j);
3923 ++__n_swaps;
3924 ++__i;
3925 break;
3926 }
3927 ++__i;
3928 }
3929 }
3930 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
3931 if (__i == __j)
3932 return;
3933 while (true)
3934 {
3935 while (!__comp(*__first, *__i))
3936 ++__i;
3937 while (__comp(*__first, *--__j))
3938 ;
3939 if (__i >= __j)
3940 break;
3941 swap(*__i, *__j);
3942 ++__n_swaps;
3943 ++__i;
3944 }
3945 // [__first, __i) == *__first and *__first < [__i, __last)
3946 // The first part is sorted, sort the secod part
Howard Hinnant0949eed2011-06-30 21:18:19 +00003947 // _VSTD::__sort<_Compare>(__i, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003948 __first = __i;
3949 goto __restart;
3950 }
3951 if (__comp(*__j, *__m))
3952 {
3953 swap(*__i, *__j);
3954 ++__n_swaps;
3955 break; // found guard for downward moving __j, now use unguarded partition
3956 }
3957 }
3958 }
3959 // It is known that *__i < *__m
3960 ++__i;
3961 // j points beyond range to be tested, *__m is known to be <= *__lm1
3962 // if not yet partitioned...
3963 if (__i < __j)
3964 {
3965 // known that *(__i - 1) < *__m
3966 // known that __i <= __m
3967 while (true)
3968 {
3969 // __m still guards upward moving __i
3970 while (__comp(*__i, *__m))
3971 ++__i;
3972 // It is now known that a guard exists for downward moving __j
3973 while (!__comp(*--__j, *__m))
3974 ;
3975 if (__i > __j)
3976 break;
3977 swap(*__i, *__j);
3978 ++__n_swaps;
3979 // It is known that __m != __j
3980 // If __m just moved, follow it
3981 if (__m == __i)
3982 __m = __j;
3983 ++__i;
3984 }
3985 }
3986 // [__first, __i) < *__m and *__m <= [__i, __last)
3987 if (__i != __m && __comp(*__m, *__i))
3988 {
3989 swap(*__i, *__m);
3990 ++__n_swaps;
3991 }
3992 // [__first, __i) < *__i and *__i <= [__i+1, __last)
3993 // If we were given a perfect partition, see if insertion sort is quick...
3994 if (__n_swaps == 0)
3995 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003996 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
3997 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003998 {
3999 if (__fs)
4000 return;
4001 __last = __i;
4002 continue;
4003 }
4004 else
4005 {
4006 if (__fs)
4007 {
4008 __first = ++__i;
4009 continue;
4010 }
4011 }
4012 }
4013 // sort smaller range with recursive call and larger with tail recursion elimination
4014 if (__i - __first < __last - __i)
4015 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004016 _VSTD::__sort<_Compare>(__first, __i, __comp);
4017 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004018 __first = ++__i;
4019 }
4020 else
4021 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004022 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4023 // _VSTD::__sort<_Compare>(__first, __i, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004024 __last = __i;
4025 }
4026 }
4027}
4028
4029// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
4030template <class _RandomAccessIterator, class _Compare>
4031inline _LIBCPP_INLINE_VISIBILITY
4032void
4033sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4034{
Howard Hinnant5e571422013-08-23 20:10:18 +00004035#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004036 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4037 __debug_less<_Compare> __c(__comp);
4038 __sort<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004039#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004040 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4041 __sort<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004042#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004043}
4044
4045template <class _RandomAccessIterator>
4046inline _LIBCPP_INLINE_VISIBILITY
4047void
4048sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4049{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004050 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004051}
4052
4053template <class _Tp>
4054inline _LIBCPP_INLINE_VISIBILITY
4055void
4056sort(_Tp** __first, _Tp** __last)
4057{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004058 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004059}
4060
4061template <class _Tp>
4062inline _LIBCPP_INLINE_VISIBILITY
4063void
4064sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4065{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004066 _VSTD::sort(__first.base(), __last.base());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004067}
4068
Howard Hinnant7a563db2011-09-14 18:33:51 +00004069template <class _Tp, class _Compare>
4070inline _LIBCPP_INLINE_VISIBILITY
4071void
4072sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4073{
4074 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4075 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4076}
4077
Howard Hinnante9df0a52013-08-01 18:17:34 +00004078#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +00004079#pragma warning( push )
4080#pragma warning( disable: 4231)
Howard Hinnante9df0a52013-08-01 18:17:34 +00004081#endif // _LIBCPP_MSVC
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004082_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4083_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4084_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4085_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4086_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4087_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4088_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4089_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4090_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4091_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4092_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4093_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&))
4094_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4095_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4096_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004097
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004098_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4099_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4100_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4101_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4102_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4103_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4104_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4105_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4106_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4107_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4108_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4109_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&))
4110_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4111_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4112_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004113
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004114_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&))
Howard Hinnante9df0a52013-08-01 18:17:34 +00004115#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +00004116#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34 +00004117#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004118
4119// lower_bound
4120
4121template <class _Compare, class _ForwardIterator, class _Tp>
4122_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004123__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004124{
4125 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004126 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004127 while (__len != 0)
4128 {
4129 difference_type __l2 = __len / 2;
4130 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004131 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004132 if (__comp(*__m, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004133 {
4134 __first = ++__m;
4135 __len -= __l2 + 1;
4136 }
4137 else
4138 __len = __l2;
4139 }
4140 return __first;
4141}
4142
4143template <class _ForwardIterator, class _Tp, class _Compare>
4144inline _LIBCPP_INLINE_VISIBILITY
4145_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004146lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004147{
Howard Hinnant5e571422013-08-23 20:10:18 +00004148#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004149 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4150 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004151 return __lower_bound<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004152#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004153 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004154 return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004155#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004156}
4157
4158template <class _ForwardIterator, class _Tp>
4159inline _LIBCPP_INLINE_VISIBILITY
4160_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004161lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004162{
Howard Hinnant78b68282011-10-22 20:59:45 +00004163 return _VSTD::lower_bound(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004164 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4165}
4166
4167// upper_bound
4168
4169template <class _Compare, class _ForwardIterator, class _Tp>
4170_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004171__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004172{
4173 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004174 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004175 while (__len != 0)
4176 {
4177 difference_type __l2 = __len / 2;
4178 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004179 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004180 if (__comp(__value_, *__m))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004181 __len = __l2;
4182 else
4183 {
4184 __first = ++__m;
4185 __len -= __l2 + 1;
4186 }
4187 }
4188 return __first;
4189}
4190
4191template <class _ForwardIterator, class _Tp, class _Compare>
4192inline _LIBCPP_INLINE_VISIBILITY
4193_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004194upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004195{
Howard Hinnant5e571422013-08-23 20:10:18 +00004196#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004197 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4198 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004199 return __upper_bound<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004200#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004201 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004202 return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004203#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004204}
4205
4206template <class _ForwardIterator, class _Tp>
4207inline _LIBCPP_INLINE_VISIBILITY
4208_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004209upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004210{
Howard Hinnant78b68282011-10-22 20:59:45 +00004211 return _VSTD::upper_bound(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004212 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4213}
4214
4215// equal_range
4216
4217template <class _Compare, class _ForwardIterator, class _Tp>
4218pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004219__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004220{
4221 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004222 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004223 while (__len != 0)
4224 {
4225 difference_type __l2 = __len / 2;
4226 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004227 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004228 if (__comp(*__m, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004229 {
4230 __first = ++__m;
4231 __len -= __l2 + 1;
4232 }
Howard Hinnant78b68282011-10-22 20:59:45 +00004233 else if (__comp(__value_, *__m))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004234 {
4235 __last = __m;
4236 __len = __l2;
4237 }
4238 else
4239 {
4240 _ForwardIterator __mp1 = __m;
4241 return pair<_ForwardIterator, _ForwardIterator>
4242 (
Howard Hinnant78b68282011-10-22 20:59:45 +00004243 __lower_bound<_Compare>(__first, __m, __value_, __comp),
4244 __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004245 );
4246 }
4247 }
4248 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4249}
4250
4251template <class _ForwardIterator, class _Tp, class _Compare>
4252inline _LIBCPP_INLINE_VISIBILITY
4253pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004254equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004255{
Howard Hinnant5e571422013-08-23 20:10:18 +00004256#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004257 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4258 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004259 return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004260#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004261 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004262 return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004263#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004264}
4265
4266template <class _ForwardIterator, class _Tp>
4267inline _LIBCPP_INLINE_VISIBILITY
4268pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004269equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004270{
Howard Hinnant78b68282011-10-22 20:59:45 +00004271 return _VSTD::equal_range(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004272 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4273}
4274
4275// binary_search
4276
4277template <class _Compare, class _ForwardIterator, class _Tp>
4278inline _LIBCPP_INLINE_VISIBILITY
4279bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004280__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004281{
Howard Hinnant78b68282011-10-22 20:59:45 +00004282 __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4283 return __first != __last && !__comp(__value_, *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004284}
4285
4286template <class _ForwardIterator, class _Tp, class _Compare>
4287inline _LIBCPP_INLINE_VISIBILITY
4288bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004289binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004290{
Howard Hinnant5e571422013-08-23 20:10:18 +00004291#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004292 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4293 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004294 return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004295#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004296 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004297 return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004298#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004299}
4300
4301template <class _ForwardIterator, class _Tp>
4302inline _LIBCPP_INLINE_VISIBILITY
4303bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004304binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004305{
Howard Hinnant78b68282011-10-22 20:59:45 +00004306 return _VSTD::binary_search(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004307 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4308}
4309
4310// merge
4311
4312template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4313_OutputIterator
4314__merge(_InputIterator1 __first1, _InputIterator1 __last1,
4315 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4316{
4317 for (; __first1 != __last1; ++__result)
4318 {
4319 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004320 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004321 if (__comp(*__first2, *__first1))
4322 {
4323 *__result = *__first2;
4324 ++__first2;
4325 }
4326 else
4327 {
4328 *__result = *__first1;
4329 ++__first1;
4330 }
4331 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00004332 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004333}
4334
4335template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4336inline _LIBCPP_INLINE_VISIBILITY
4337_OutputIterator
4338merge(_InputIterator1 __first1, _InputIterator1 __last1,
4339 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4340{
Howard Hinnant5e571422013-08-23 20:10:18 +00004341#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004342 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4343 __debug_less<_Compare> __c(__comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004344 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004345#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004346 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004347 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004348#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004349}
4350
4351template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4352inline _LIBCPP_INLINE_VISIBILITY
4353_OutputIterator
4354merge(_InputIterator1 __first1, _InputIterator1 __last1,
4355 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4356{
4357 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4358 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4359 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4360}
4361
4362// inplace_merge
4363
Marshall Clowa3795762015-07-29 16:25:45 +00004364template <class _Compare, class _InputIterator1, class _InputIterator2,
4365 class _OutputIterator>
4366void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4367 _InputIterator2 __first2, _InputIterator2 __last2,
4368 _OutputIterator __result, _Compare __comp)
4369{
4370 for (; __first1 != __last1; ++__result)
4371 {
4372 if (__first2 == __last2)
4373 {
4374 _VSTD::move(__first1, __last1, __result);
4375 return;
4376 }
4377
4378 if (__comp(*__first2, *__first1))
4379 {
4380 *__result = _VSTD::move(*__first2);
4381 ++__first2;
4382 }
4383 else
4384 {
4385 *__result = _VSTD::move(*__first1);
4386 ++__first1;
4387 }
4388 }
4389 // __first2 through __last2 are already in the right spot.
4390}
4391
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004392template <class _Compare, class _BidirectionalIterator>
4393void
4394__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4395 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4396 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4397 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4398{
4399 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004400 __destruct_n __d(0);
4401 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4402 if (__len1 <= __len2)
4403 {
4404 value_type* __p = __buff;
Eric Fiselierb9919752014-10-27 19:28:20 +00004405 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004406 ::new(__p) value_type(_VSTD::move(*__i));
Marshall Clowa3795762015-07-29 16:25:45 +00004407 __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004408 }
4409 else
4410 {
4411 value_type* __p = __buff;
Eric Fiselierb9919752014-10-27 19:28:20 +00004412 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004413 ::new(__p) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004414 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4415 typedef reverse_iterator<value_type*> _Rv;
Marshall Clowa3795762015-07-29 16:25:45 +00004416 __half_inplace_merge(_Rv(__p), _Rv(__buff),
4417 _RBi(__middle), _RBi(__first),
4418 _RBi(__last), __negate<_Compare>(__comp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004419 }
4420}
4421
4422template <class _Compare, class _BidirectionalIterator>
4423void
4424__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4425 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4426 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4427 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4428{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004429 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4430 while (true)
4431 {
4432 // if __middle == __last, we're done
4433 if (__len2 == 0)
4434 return;
Marshall Clowe809f4c2015-02-02 16:44:11 +00004435 if (__len1 <= __buff_size || __len2 <= __buff_size)
4436 return __buffered_inplace_merge<_Compare>
4437 (__first, __middle, __last, __comp, __len1, __len2, __buff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004438 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
Eric Fiselierb9919752014-10-27 19:28:20 +00004439 for (; true; ++__first, (void) --__len1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004440 {
4441 if (__len1 == 0)
4442 return;
4443 if (__comp(*__middle, *__first))
4444 break;
4445 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004446 // __first < __middle < __last
4447 // *__first > *__middle
4448 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4449 // all elements in:
4450 // [__first, __m1) <= [__middle, __m2)
4451 // [__middle, __m2) < [__m1, __middle)
4452 // [__m1, __middle) <= [__m2, __last)
4453 // and __m1 or __m2 is in the middle of its range
4454 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4455 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4456 difference_type __len11; // distance(__first, __m1)
4457 difference_type __len21; // distance(__middle, __m2)
4458 // binary search smaller range
4459 if (__len1 < __len2)
4460 { // __len >= 1, __len2 >= 2
4461 __len21 = __len2 / 2;
4462 __m2 = __middle;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004463 _VSTD::advance(__m2, __len21);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004464 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004465 __len11 = _VSTD::distance(__first, __m1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004466 }
4467 else
4468 {
4469 if (__len1 == 1)
4470 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4471 // It is known *__first > *__middle
4472 swap(*__first, *__middle);
4473 return;
4474 }
4475 // __len1 >= 2, __len2 >= 1
4476 __len11 = __len1 / 2;
4477 __m1 = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004478 _VSTD::advance(__m1, __len11);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004479 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004480 __len21 = _VSTD::distance(__middle, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004481 }
4482 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4483 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4484 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4485 // swap middle two partitions
Howard Hinnant0949eed2011-06-30 21:18:19 +00004486 __middle = _VSTD::rotate(__m1, __middle, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004487 // __len12 and __len21 now have swapped meanings
4488 // merge smaller range with recurisve call and larger with tail recursion elimination
4489 if (__len11 + __len21 < __len12 + __len22)
4490 {
4491 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4492// __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4493 __first = __middle;
4494 __middle = __m2;
4495 __len1 = __len12;
4496 __len2 = __len22;
4497 }
4498 else
4499 {
4500 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4501// __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4502 __last = __middle;
4503 __middle = __m1;
4504 __len1 = __len11;
4505 __len2 = __len21;
4506 }
4507 }
4508}
4509
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004510template <class _BidirectionalIterator, class _Compare>
4511inline _LIBCPP_INLINE_VISIBILITY
4512void
4513inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4514 _Compare __comp)
4515{
4516 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4517 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004518 difference_type __len1 = _VSTD::distance(__first, __middle);
4519 difference_type __len2 = _VSTD::distance(__middle, __last);
4520 difference_type __buf_size = _VSTD::min(__len1, __len2);
Marshall Clow4c2684c2015-02-02 17:35:53 +00004521 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4522 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
4523
Howard Hinnant5e571422013-08-23 20:10:18 +00004524#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004525 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4526 __debug_less<_Compare> __c(__comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004527 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004528 __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004529#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004530 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004531 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004532 __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004533#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004534}
4535
4536template <class _BidirectionalIterator>
4537inline _LIBCPP_INLINE_VISIBILITY
4538void
4539inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4540{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004541 _VSTD::inplace_merge(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004542 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4543}
4544
4545// stable_sort
4546
4547template <class _Compare, class _InputIterator1, class _InputIterator2>
4548void
4549__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4550 _InputIterator2 __first2, _InputIterator2 __last2,
4551 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4552{
4553 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4554 __destruct_n __d(0);
4555 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4556 for (; true; ++__result)
4557 {
4558 if (__first1 == __last1)
4559 {
4560 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
Howard Hinnant0949eed2011-06-30 21:18:19 +00004561 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004562 __h.release();
4563 return;
4564 }
4565 if (__first2 == __last2)
4566 {
4567 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
Howard Hinnant0949eed2011-06-30 21:18:19 +00004568 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004569 __h.release();
4570 return;
4571 }
4572 if (__comp(*__first2, *__first1))
4573 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004574 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004575 __d.__incr((value_type*)0);
4576 ++__first2;
4577 }
4578 else
4579 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004580 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004581 __d.__incr((value_type*)0);
4582 ++__first1;
4583 }
4584 }
4585}
4586
4587template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4588void
4589__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4590 _InputIterator2 __first2, _InputIterator2 __last2,
4591 _OutputIterator __result, _Compare __comp)
4592{
4593 for (; __first1 != __last1; ++__result)
4594 {
4595 if (__first2 == __last2)
4596 {
4597 for (; __first1 != __last1; ++__first1, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004598 *__result = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004599 return;
4600 }
4601 if (__comp(*__first2, *__first1))
4602 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004603 *__result = _VSTD::move(*__first2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004604 ++__first2;
4605 }
4606 else
4607 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004608 *__result = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004609 ++__first1;
4610 }
4611 }
4612 for (; __first2 != __last2; ++__first2, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004613 *__result = _VSTD::move(*__first2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004614}
4615
4616template <class _Compare, class _RandomAccessIterator>
4617void
4618__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4619 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4620 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4621
4622template <class _Compare, class _RandomAccessIterator>
4623void
4624__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4625 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4626 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4627{
4628 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4629 switch (__len)
4630 {
4631 case 0:
4632 return;
4633 case 1:
Howard Hinnant0949eed2011-06-30 21:18:19 +00004634 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004635 return;
4636 case 2:
4637 __destruct_n __d(0);
4638 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4639 if (__comp(*--__last1, *__first1))
4640 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004641 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004642 __d.__incr((value_type*)0);
4643 ++__first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004644 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004645 }
4646 else
4647 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004648 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004649 __d.__incr((value_type*)0);
4650 ++__first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004651 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004652 }
4653 __h2.release();
4654 return;
4655 }
4656 if (__len <= 8)
4657 {
4658 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4659 return;
4660 }
4661 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4662 _RandomAccessIterator __m = __first1 + __l2;
4663 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4664 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4665 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4666}
4667
4668template <class _Tp>
4669struct __stable_sort_switch
4670{
Howard Hinnant1468b662010-11-19 22:17:28 +00004671 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004672};
4673
4674template <class _Compare, class _RandomAccessIterator>
4675void
4676__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4677 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4678 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4679{
4680 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4681 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4682 switch (__len)
4683 {
4684 case 0:
4685 case 1:
4686 return;
4687 case 2:
4688 if (__comp(*--__last, *__first))
4689 swap(*__first, *__last);
4690 return;
4691 }
4692 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4693 {
4694 __insertion_sort<_Compare>(__first, __last, __comp);
4695 return;
4696 }
4697 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4698 _RandomAccessIterator __m = __first + __l2;
4699 if (__len <= __buff_size)
4700 {
4701 __destruct_n __d(0);
4702 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4703 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4704 __d.__set(__l2, (value_type*)0);
4705 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4706 __d.__set(__len, (value_type*)0);
4707 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4708// __merge<_Compare>(move_iterator<value_type*>(__buff),
4709// move_iterator<value_type*>(__buff + __l2),
4710// move_iterator<_RandomAccessIterator>(__buff + __l2),
4711// move_iterator<_RandomAccessIterator>(__buff + __len),
4712// __first, __comp);
4713 return;
4714 }
4715 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4716 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4717 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4718}
4719
4720template <class _RandomAccessIterator, class _Compare>
4721inline _LIBCPP_INLINE_VISIBILITY
4722void
4723stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4724{
4725 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4726 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4727 difference_type __len = __last - __first;
4728 pair<value_type*, ptrdiff_t> __buf(0, 0);
4729 unique_ptr<value_type, __return_temporary_buffer> __h;
4730 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4731 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004732 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004733 __h.reset(__buf.first);
4734 }
Howard Hinnant5e571422013-08-23 20:10:18 +00004735#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004736 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4737 __debug_less<_Compare> __c(__comp);
4738 __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004739#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004740 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4741 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004742#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004743}
4744
4745template <class _RandomAccessIterator>
4746inline _LIBCPP_INLINE_VISIBILITY
4747void
4748stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4749{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004750 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004751}
4752
4753// is_heap_until
4754
4755template <class _RandomAccessIterator, class _Compare>
4756_RandomAccessIterator
4757is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4758{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004759 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004760 difference_type __len = __last - __first;
4761 difference_type __p = 0;
4762 difference_type __c = 1;
4763 _RandomAccessIterator __pp = __first;
4764 while (__c < __len)
4765 {
4766 _RandomAccessIterator __cp = __first + __c;
4767 if (__comp(*__pp, *__cp))
4768 return __cp;
4769 ++__c;
4770 ++__cp;
4771 if (__c == __len)
4772 return __last;
4773 if (__comp(*__pp, *__cp))
4774 return __cp;
4775 ++__p;
4776 ++__pp;
4777 __c = 2 * __p + 1;
4778 }
4779 return __last;
4780}
4781
Howard Hinnant324bb032010-08-22 00:02:43 +00004782template<class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004783inline _LIBCPP_INLINE_VISIBILITY
4784_RandomAccessIterator
4785is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4786{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004787 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004788}
4789
4790// is_heap
4791
4792template <class _RandomAccessIterator, class _Compare>
4793inline _LIBCPP_INLINE_VISIBILITY
4794bool
4795is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4796{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004797 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004798}
4799
Howard Hinnant324bb032010-08-22 00:02:43 +00004800template<class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004801inline _LIBCPP_INLINE_VISIBILITY
4802bool
4803is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4804{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004805 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004806}
4807
4808// push_heap
4809
4810template <class _Compare, class _RandomAccessIterator>
4811void
David Majnemercb8757a2014-07-22 06:07:09 +00004812__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4813 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004814{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004815 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4816 if (__len > 1)
4817 {
4818 __len = (__len - 2) / 2;
4819 _RandomAccessIterator __ptr = __first + __len;
4820 if (__comp(*__ptr, *--__last))
4821 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004822 value_type __t(_VSTD::move(*__last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004823 do
4824 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004825 *__last = _VSTD::move(*__ptr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004826 __last = __ptr;
4827 if (__len == 0)
4828 break;
4829 __len = (__len - 1) / 2;
4830 __ptr = __first + __len;
4831 } while (__comp(*__ptr, __t));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004832 *__last = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004833 }
4834 }
4835}
4836
4837template <class _RandomAccessIterator, class _Compare>
4838inline _LIBCPP_INLINE_VISIBILITY
4839void
4840push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4841{
Howard Hinnant5e571422013-08-23 20:10:18 +00004842#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004843 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4844 __debug_less<_Compare> __c(__comp);
David Majnemercb8757a2014-07-22 06:07:09 +00004845 __sift_up<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004846#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004847 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
David Majnemercb8757a2014-07-22 06:07:09 +00004848 __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004849#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004850}
4851
4852template <class _RandomAccessIterator>
4853inline _LIBCPP_INLINE_VISIBILITY
4854void
4855push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4856{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004857 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004858}
4859
4860// pop_heap
4861
4862template <class _Compare, class _RandomAccessIterator>
David Majnemercb8757a2014-07-22 06:07:09 +00004863void
4864__sift_down(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4865 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4866 _RandomAccessIterator __start)
4867{
4868 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4869 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4870 // left-child of __start is at 2 * __start + 1
4871 // right-child of __start is at 2 * __start + 2
4872 difference_type __child = __start - __first;
4873
4874 if (__len < 2 || (__len - 2) / 2 < __child)
4875 return;
4876
4877 __child = 2 * __child + 1;
4878 _RandomAccessIterator __child_i = __first + __child;
4879
4880 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4881 // right-child exists and is greater than left-child
4882 ++__child_i;
4883 ++__child;
4884 }
4885
4886 // check if we are in heap-order
4887 if (__comp(*__child_i, *__start))
4888 // we are, __start is larger than it's largest child
4889 return;
4890
4891 value_type __top(_VSTD::move(*__start));
4892 do
4893 {
4894 // we are not in heap-order, swap the parent with it's largest child
4895 *__start = _VSTD::move(*__child_i);
4896 __start = __child_i;
4897
4898 if ((__len - 2) / 2 < __child)
4899 break;
4900
4901 // recompute the child based off of the updated parent
4902 __child = 2 * __child + 1;
4903 __child_i = __first + __child;
4904
4905 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4906 // right-child exists and is greater than left-child
4907 ++__child_i;
4908 ++__child;
4909 }
4910
4911 // check if we are in heap-order
4912 } while (!__comp(*__child_i, __top));
4913 *__start = _VSTD::move(__top);
4914}
4915
4916template <class _Compare, class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004917inline _LIBCPP_INLINE_VISIBILITY
4918void
4919__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4920 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4921{
4922 if (__len > 1)
4923 {
4924 swap(*__first, *--__last);
David Majnemercb8757a2014-07-22 06:07:09 +00004925 __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004926 }
4927}
4928
4929template <class _RandomAccessIterator, class _Compare>
4930inline _LIBCPP_INLINE_VISIBILITY
4931void
4932pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4933{
Howard Hinnant5e571422013-08-23 20:10:18 +00004934#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004935 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4936 __debug_less<_Compare> __c(__comp);
4937 __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004938#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004939 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4940 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004941#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004942}
4943
4944template <class _RandomAccessIterator>
4945inline _LIBCPP_INLINE_VISIBILITY
4946void
4947pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4948{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004949 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004950}
4951
4952// make_heap
4953
4954template <class _Compare, class _RandomAccessIterator>
4955void
4956__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4957{
4958 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4959 difference_type __n = __last - __first;
4960 if (__n > 1)
4961 {
David Majnemercb8757a2014-07-22 06:07:09 +00004962 // start from the first parent, there is no need to consider children
4963 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
4964 {
4965 __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
4966 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004967 }
4968}
4969
4970template <class _RandomAccessIterator, class _Compare>
4971inline _LIBCPP_INLINE_VISIBILITY
4972void
4973make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4974{
Howard Hinnant5e571422013-08-23 20:10:18 +00004975#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004976 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4977 __debug_less<_Compare> __c(__comp);
4978 __make_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004979#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004980 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4981 __make_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004982#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004983}
4984
4985template <class _RandomAccessIterator>
4986inline _LIBCPP_INLINE_VISIBILITY
4987void
4988make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4989{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004990 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004991}
4992
4993// sort_heap
4994
4995template <class _Compare, class _RandomAccessIterator>
4996void
4997__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4998{
4999 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5000 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
5001 __pop_heap<_Compare>(__first, __last, __comp, __n);
5002}
5003
5004template <class _RandomAccessIterator, class _Compare>
5005inline _LIBCPP_INLINE_VISIBILITY
5006void
5007sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5008{
Howard Hinnant5e571422013-08-23 20:10:18 +00005009#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005010 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5011 __debug_less<_Compare> __c(__comp);
5012 __sort_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005013#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005014 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5015 __sort_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005016#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005017}
5018
5019template <class _RandomAccessIterator>
5020inline _LIBCPP_INLINE_VISIBILITY
5021void
5022sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5023{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005024 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005025}
5026
5027// partial_sort
5028
5029template <class _Compare, class _RandomAccessIterator>
5030void
5031__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5032 _Compare __comp)
5033{
5034 __make_heap<_Compare>(__first, __middle, __comp);
5035 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5036 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5037 {
5038 if (__comp(*__i, *__first))
5039 {
5040 swap(*__i, *__first);
David Majnemercb8757a2014-07-22 06:07:09 +00005041 __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005042 }
5043 }
5044 __sort_heap<_Compare>(__first, __middle, __comp);
5045}
5046
5047template <class _RandomAccessIterator, class _Compare>
5048inline _LIBCPP_INLINE_VISIBILITY
5049void
5050partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5051 _Compare __comp)
5052{
Howard Hinnant5e571422013-08-23 20:10:18 +00005053#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005054 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5055 __debug_less<_Compare> __c(__comp);
5056 __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005057#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005058 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5059 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005060#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005061}
5062
5063template <class _RandomAccessIterator>
5064inline _LIBCPP_INLINE_VISIBILITY
5065void
5066partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5067{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005068 _VSTD::partial_sort(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005069 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5070}
5071
5072// partial_sort_copy
5073
5074template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5075_RandomAccessIterator
5076__partial_sort_copy(_InputIterator __first, _InputIterator __last,
5077 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5078{
5079 _RandomAccessIterator __r = __result_first;
5080 if (__r != __result_last)
5081 {
Eric Fiselierb9919752014-10-27 19:28:20 +00005082 for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005083 *__r = *__first;
5084 __make_heap<_Compare>(__result_first, __r, __comp);
David Majnemercb8757a2014-07-22 06:07:09 +00005085 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005086 for (; __first != __last; ++__first)
5087 if (__comp(*__first, *__result_first))
5088 {
5089 *__result_first = *__first;
David Majnemercb8757a2014-07-22 06:07:09 +00005090 __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005091 }
5092 __sort_heap<_Compare>(__result_first, __r, __comp);
5093 }
5094 return __r;
5095}
5096
5097template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5098inline _LIBCPP_INLINE_VISIBILITY
5099_RandomAccessIterator
5100partial_sort_copy(_InputIterator __first, _InputIterator __last,
5101 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5102{
Howard Hinnant5e571422013-08-23 20:10:18 +00005103#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005104 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5105 __debug_less<_Compare> __c(__comp);
5106 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005107#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005108 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5109 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005110#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005111}
5112
5113template <class _InputIterator, class _RandomAccessIterator>
5114inline _LIBCPP_INLINE_VISIBILITY
5115_RandomAccessIterator
5116partial_sort_copy(_InputIterator __first, _InputIterator __last,
5117 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5118{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005119 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005120 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5121}
5122
5123// nth_element
5124
5125template <class _Compare, class _RandomAccessIterator>
5126void
5127__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5128{
5129 // _Compare is known to be a reference type
5130 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5131 const difference_type __limit = 7;
5132 while (true)
5133 {
5134 __restart:
Howard Hinnant8292d742011-12-29 17:45:35 +00005135 if (__nth == __last)
5136 return;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005137 difference_type __len = __last - __first;
5138 switch (__len)
5139 {
5140 case 0:
5141 case 1:
5142 return;
5143 case 2:
5144 if (__comp(*--__last, *__first))
5145 swap(*__first, *__last);
5146 return;
5147 case 3:
5148 {
5149 _RandomAccessIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00005150 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005151 return;
5152 }
5153 }
5154 if (__len <= __limit)
5155 {
5156 __selection_sort<_Compare>(__first, __last, __comp);
5157 return;
5158 }
5159 // __len > __limit >= 3
5160 _RandomAccessIterator __m = __first + __len/2;
5161 _RandomAccessIterator __lm1 = __last;
Howard Hinnant0949eed2011-06-30 21:18:19 +00005162 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005163 // *__m is median
5164 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5165 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5166 _RandomAccessIterator __i = __first;
5167 _RandomAccessIterator __j = __lm1;
5168 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5169 // The search going up is known to be guarded but the search coming down isn't.
5170 // Prime the downward search with a guard.
5171 if (!__comp(*__i, *__m)) // if *__first == *__m
5172 {
5173 // *__first == *__m, *__first doesn't go in first part
5174 // manually guard downward moving __j against __i
5175 while (true)
5176 {
5177 if (__i == --__j)
5178 {
5179 // *__first == *__m, *__m <= all other elements
5180 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5181 ++__i; // __first + 1
5182 __j = __last;
5183 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5184 {
5185 while (true)
5186 {
5187 if (__i == __j)
5188 return; // [__first, __last) all equivalent elements
5189 if (__comp(*__first, *__i))
5190 {
5191 swap(*__i, *__j);
5192 ++__n_swaps;
5193 ++__i;
5194 break;
5195 }
5196 ++__i;
5197 }
5198 }
5199 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5200 if (__i == __j)
5201 return;
5202 while (true)
5203 {
5204 while (!__comp(*__first, *__i))
5205 ++__i;
5206 while (__comp(*__first, *--__j))
5207 ;
5208 if (__i >= __j)
5209 break;
5210 swap(*__i, *__j);
5211 ++__n_swaps;
5212 ++__i;
5213 }
5214 // [__first, __i) == *__first and *__first < [__i, __last)
5215 // The first part is sorted,
5216 if (__nth < __i)
5217 return;
5218 // __nth_element the secod part
5219 // __nth_element<_Compare>(__i, __nth, __last, __comp);
5220 __first = __i;
5221 goto __restart;
5222 }
5223 if (__comp(*__j, *__m))
5224 {
5225 swap(*__i, *__j);
5226 ++__n_swaps;
5227 break; // found guard for downward moving __j, now use unguarded partition
5228 }
5229 }
5230 }
5231 ++__i;
5232 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5233 // if not yet partitioned...
5234 if (__i < __j)
5235 {
5236 // known that *(__i - 1) < *__m
5237 while (true)
5238 {
5239 // __m still guards upward moving __i
5240 while (__comp(*__i, *__m))
5241 ++__i;
5242 // It is now known that a guard exists for downward moving __j
5243 while (!__comp(*--__j, *__m))
5244 ;
5245 if (__i >= __j)
5246 break;
5247 swap(*__i, *__j);
5248 ++__n_swaps;
5249 // It is known that __m != __j
5250 // If __m just moved, follow it
5251 if (__m == __i)
5252 __m = __j;
5253 ++__i;
5254 }
5255 }
5256 // [__first, __i) < *__m and *__m <= [__i, __last)
5257 if (__i != __m && __comp(*__m, *__i))
5258 {
5259 swap(*__i, *__m);
5260 ++__n_swaps;
5261 }
5262 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5263 if (__nth == __i)
5264 return;
5265 if (__n_swaps == 0)
5266 {
5267 // We were given a perfectly partitioned sequence. Coincidence?
5268 if (__nth < __i)
5269 {
5270 // Check for [__first, __i) already sorted
5271 __j = __m = __first;
5272 while (++__j != __i)
5273 {
5274 if (__comp(*__j, *__m))
5275 // not yet sorted, so sort
5276 goto not_sorted;
5277 __m = __j;
5278 }
5279 // [__first, __i) sorted
5280 return;
5281 }
5282 else
5283 {
5284 // Check for [__i, __last) already sorted
5285 __j = __m = __i;
5286 while (++__j != __last)
5287 {
5288 if (__comp(*__j, *__m))
5289 // not yet sorted, so sort
5290 goto not_sorted;
5291 __m = __j;
5292 }
5293 // [__i, __last) sorted
5294 return;
5295 }
5296 }
5297not_sorted:
5298 // __nth_element on range containing __nth
5299 if (__nth < __i)
5300 {
5301 // __nth_element<_Compare>(__first, __nth, __i, __comp);
5302 __last = __i;
5303 }
5304 else
5305 {
5306 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5307 __first = ++__i;
5308 }
5309 }
5310}
5311
5312template <class _RandomAccessIterator, class _Compare>
5313inline _LIBCPP_INLINE_VISIBILITY
5314void
5315nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5316{
Howard Hinnant5e571422013-08-23 20:10:18 +00005317#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005318 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5319 __debug_less<_Compare> __c(__comp);
5320 __nth_element<_Comp_ref>(__first, __nth, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005321#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005322 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5323 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005324#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005325}
5326
5327template <class _RandomAccessIterator>
5328inline _LIBCPP_INLINE_VISIBILITY
5329void
5330nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5331{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005332 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005333}
5334
5335// includes
5336
5337template <class _Compare, class _InputIterator1, class _InputIterator2>
5338bool
5339__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5340 _Compare __comp)
5341{
5342 for (; __first2 != __last2; ++__first1)
5343 {
5344 if (__first1 == __last1 || __comp(*__first2, *__first1))
5345 return false;
5346 if (!__comp(*__first1, *__first2))
5347 ++__first2;
5348 }
5349 return true;
5350}
5351
5352template <class _InputIterator1, class _InputIterator2, class _Compare>
5353inline _LIBCPP_INLINE_VISIBILITY
5354bool
5355includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5356 _Compare __comp)
5357{
Howard Hinnant5e571422013-08-23 20:10:18 +00005358#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005359 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5360 __debug_less<_Compare> __c(__comp);
5361 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005362#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005363 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5364 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005365#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005366}
5367
5368template <class _InputIterator1, class _InputIterator2>
5369inline _LIBCPP_INLINE_VISIBILITY
5370bool
5371includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5372{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005373 return _VSTD::includes(__first1, __last1, __first2, __last2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005374 __less<typename iterator_traits<_InputIterator1>::value_type,
5375 typename iterator_traits<_InputIterator2>::value_type>());
5376}
5377
5378// set_union
5379
5380template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5381_OutputIterator
5382__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5383 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5384{
5385 for (; __first1 != __last1; ++__result)
5386 {
5387 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005388 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005389 if (__comp(*__first2, *__first1))
5390 {
5391 *__result = *__first2;
5392 ++__first2;
5393 }
5394 else
5395 {
5396 *__result = *__first1;
5397 if (!__comp(*__first1, *__first2))
5398 ++__first2;
5399 ++__first1;
5400 }
5401 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00005402 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005403}
5404
5405template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5406inline _LIBCPP_INLINE_VISIBILITY
5407_OutputIterator
5408set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5409 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5410{
Howard Hinnant5e571422013-08-23 20:10:18 +00005411#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005412 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5413 __debug_less<_Compare> __c(__comp);
5414 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005415#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005416 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5417 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005418#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005419}
5420
5421template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5422inline _LIBCPP_INLINE_VISIBILITY
5423_OutputIterator
5424set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5425 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5426{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005427 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005428 __less<typename iterator_traits<_InputIterator1>::value_type,
5429 typename iterator_traits<_InputIterator2>::value_type>());
5430}
5431
5432// set_intersection
5433
5434template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5435_OutputIterator
5436__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5437 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5438{
5439 while (__first1 != __last1 && __first2 != __last2)
5440 {
5441 if (__comp(*__first1, *__first2))
5442 ++__first1;
5443 else
5444 {
5445 if (!__comp(*__first2, *__first1))
5446 {
5447 *__result = *__first1;
5448 ++__result;
5449 ++__first1;
5450 }
5451 ++__first2;
5452 }
5453 }
5454 return __result;
5455}
5456
5457template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5458inline _LIBCPP_INLINE_VISIBILITY
5459_OutputIterator
5460set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5461 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5462{
Howard Hinnant5e571422013-08-23 20:10:18 +00005463#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005464 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5465 __debug_less<_Compare> __c(__comp);
5466 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005467#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005468 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5469 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005470#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005471}
5472
5473template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5474inline _LIBCPP_INLINE_VISIBILITY
5475_OutputIterator
5476set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5477 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5478{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005479 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005480 __less<typename iterator_traits<_InputIterator1>::value_type,
5481 typename iterator_traits<_InputIterator2>::value_type>());
5482}
5483
5484// set_difference
5485
5486template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5487_OutputIterator
5488__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5489 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5490{
5491 while (__first1 != __last1)
5492 {
5493 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005494 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005495 if (__comp(*__first1, *__first2))
5496 {
5497 *__result = *__first1;
5498 ++__result;
5499 ++__first1;
5500 }
5501 else
5502 {
5503 if (!__comp(*__first2, *__first1))
5504 ++__first1;
5505 ++__first2;
5506 }
5507 }
5508 return __result;
5509}
5510
5511template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5512inline _LIBCPP_INLINE_VISIBILITY
5513_OutputIterator
5514set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5515 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5516{
Howard Hinnant5e571422013-08-23 20:10:18 +00005517#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005518 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5519 __debug_less<_Compare> __c(__comp);
5520 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005521#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005522 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5523 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005524#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005525}
5526
5527template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5528inline _LIBCPP_INLINE_VISIBILITY
5529_OutputIterator
5530set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5531 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5532{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005533 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005534 __less<typename iterator_traits<_InputIterator1>::value_type,
5535 typename iterator_traits<_InputIterator2>::value_type>());
5536}
5537
5538// set_symmetric_difference
5539
5540template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5541_OutputIterator
5542__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5543 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5544{
5545 while (__first1 != __last1)
5546 {
5547 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005548 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005549 if (__comp(*__first1, *__first2))
5550 {
5551 *__result = *__first1;
5552 ++__result;
5553 ++__first1;
5554 }
5555 else
5556 {
5557 if (__comp(*__first2, *__first1))
5558 {
5559 *__result = *__first2;
5560 ++__result;
5561 }
5562 else
5563 ++__first1;
5564 ++__first2;
5565 }
5566 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00005567 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005568}
5569
5570template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5571inline _LIBCPP_INLINE_VISIBILITY
5572_OutputIterator
5573set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5574 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5575{
Howard Hinnant5e571422013-08-23 20:10:18 +00005576#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005577 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5578 __debug_less<_Compare> __c(__comp);
5579 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005580#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005581 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5582 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005583#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005584}
5585
5586template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5587inline _LIBCPP_INLINE_VISIBILITY
5588_OutputIterator
5589set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5590 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5591{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005592 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005593 __less<typename iterator_traits<_InputIterator1>::value_type,
5594 typename iterator_traits<_InputIterator2>::value_type>());
5595}
5596
5597// lexicographical_compare
5598
5599template <class _Compare, class _InputIterator1, class _InputIterator2>
5600bool
5601__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5602 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5603{
Eric Fiselierb9919752014-10-27 19:28:20 +00005604 for (; __first2 != __last2; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005605 {
5606 if (__first1 == __last1 || __comp(*__first1, *__first2))
5607 return true;
5608 if (__comp(*__first2, *__first1))
5609 return false;
5610 }
5611 return false;
5612}
5613
5614template <class _InputIterator1, class _InputIterator2, class _Compare>
5615inline _LIBCPP_INLINE_VISIBILITY
5616bool
5617lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5618 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5619{
Howard Hinnant5e571422013-08-23 20:10:18 +00005620#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005621 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5622 __debug_less<_Compare> __c(__comp);
5623 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005624#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005625 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5626 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005627#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005628}
5629
5630template <class _InputIterator1, class _InputIterator2>
5631inline _LIBCPP_INLINE_VISIBILITY
5632bool
5633lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5634 _InputIterator2 __first2, _InputIterator2 __last2)
5635{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005636 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005637 __less<typename iterator_traits<_InputIterator1>::value_type,
5638 typename iterator_traits<_InputIterator2>::value_type>());
5639}
5640
5641// next_permutation
5642
5643template <class _Compare, class _BidirectionalIterator>
5644bool
5645__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5646{
5647 _BidirectionalIterator __i = __last;
5648 if (__first == __last || __first == --__i)
5649 return false;
5650 while (true)
5651 {
5652 _BidirectionalIterator __ip1 = __i;
5653 if (__comp(*--__i, *__ip1))
5654 {
5655 _BidirectionalIterator __j = __last;
5656 while (!__comp(*__i, *--__j))
5657 ;
5658 swap(*__i, *__j);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005659 _VSTD::reverse(__ip1, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005660 return true;
5661 }
5662 if (__i == __first)
5663 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005664 _VSTD::reverse(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005665 return false;
5666 }
5667 }
5668}
5669
5670template <class _BidirectionalIterator, class _Compare>
5671inline _LIBCPP_INLINE_VISIBILITY
5672bool
5673next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5674{
Howard Hinnant5e571422013-08-23 20:10:18 +00005675#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005676 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5677 __debug_less<_Compare> __c(__comp);
5678 return __next_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005679#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005680 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5681 return __next_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005682#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005683}
5684
5685template <class _BidirectionalIterator>
5686inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005687bool
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005688next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5689{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005690 return _VSTD::next_permutation(__first, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005691 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5692}
5693
5694// prev_permutation
5695
5696template <class _Compare, class _BidirectionalIterator>
5697bool
5698__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5699{
5700 _BidirectionalIterator __i = __last;
5701 if (__first == __last || __first == --__i)
5702 return false;
5703 while (true)
5704 {
5705 _BidirectionalIterator __ip1 = __i;
5706 if (__comp(*__ip1, *--__i))
5707 {
5708 _BidirectionalIterator __j = __last;
5709 while (!__comp(*--__j, *__i))
5710 ;
5711 swap(*__i, *__j);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005712 _VSTD::reverse(__ip1, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005713 return true;
5714 }
5715 if (__i == __first)
5716 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005717 _VSTD::reverse(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005718 return false;
5719 }
5720 }
5721}
5722
5723template <class _BidirectionalIterator, class _Compare>
5724inline _LIBCPP_INLINE_VISIBILITY
5725bool
5726prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5727{
Howard Hinnant5e571422013-08-23 20:10:18 +00005728#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005729 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5730 __debug_less<_Compare> __c(__comp);
5731 return __prev_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005732#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005733 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5734 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005735#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005736}
5737
5738template <class _BidirectionalIterator>
5739inline _LIBCPP_INLINE_VISIBILITY
5740bool
5741prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5742{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005743 return _VSTD::prev_permutation(__first, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005744 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5745}
5746
5747template <class _Tp>
5748inline _LIBCPP_INLINE_VISIBILITY
5749typename enable_if
5750<
5751 is_integral<_Tp>::value,
5752 _Tp
5753>::type
5754__rotate_left(_Tp __t, _Tp __n = 1)
5755{
5756 const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5757 __n &= __bits;
5758 return static_cast<_Tp>((__t << __n) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> (__bits - __n)));
5759}
5760
5761template <class _Tp>
5762inline _LIBCPP_INLINE_VISIBILITY
5763typename enable_if
5764<
5765 is_integral<_Tp>::value,
5766 _Tp
5767>::type
5768__rotate_right(_Tp __t, _Tp __n = 1)
5769{
5770 const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5771 __n &= __bits;
5772 return static_cast<_Tp>((__t << (__bits - __n)) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> __n));
5773}
5774
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005775_LIBCPP_END_NAMESPACE_STD
5776
5777#endif // _LIBCPP_ALGORITHM