blob: a179acf63f2f192cf99f55de7dfff0195adf6bcf [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
524 min_element(ForwardIterator first, ForwardIterator last);
525
526template <class ForwardIterator, class Compare>
527 ForwardIterator
528 min_element(ForwardIterator first, ForwardIterator last, Compare comp);
529
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
548 max_element(ForwardIterator first, ForwardIterator last);
549
550template <class ForwardIterator, class Compare>
551 ForwardIterator
552 max_element(ForwardIterator first, ForwardIterator last, Compare comp);
553
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>
572 minmax_element(ForwardIterator first, ForwardIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000573
Howard Hinnant98e5d972010-08-21 20:10:01 +0000574template<class ForwardIterator, class Compare>
575 pair<ForwardIterator, ForwardIterator>
576 minmax_element(ForwardIterator first, ForwardIterator last, Compare comp);
577
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>
Howard Hinnant78b68282011-10-22 20:59:45 +00001675 (__first, __last, __count, __value_, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676}
1677
1678template <class _ForwardIterator, class _Size, class _Tp>
1679inline _LIBCPP_INLINE_VISIBILITY
1680_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00001681search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001682{
1683 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant78b68282011-10-22 20:59:45 +00001684 return _VSTD::search_n(__first, __last, __count, __value_, __equal_to<__v, _Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001685}
1686
1687// copy
1688
1689template <class _Iter>
1690struct __libcpp_is_trivial_iterator
1691{
1692 static const bool value = is_pointer<_Iter>::value;
1693};
1694
1695template <class _Iter>
1696struct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
1697{
1698 static const bool value = is_pointer<_Iter>::value;
1699};
1700
1701template <class _Iter>
1702struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
1703{
1704 static const bool value = is_pointer<_Iter>::value;
1705};
1706
1707template <class _Iter>
1708inline _LIBCPP_INLINE_VISIBILITY
1709_Iter
1710__unwrap_iter(_Iter __i)
1711{
1712 return __i;
1713}
1714
1715template <class _Tp>
1716inline _LIBCPP_INLINE_VISIBILITY
1717typename enable_if
1718<
Howard Hinnant1468b662010-11-19 22:17:28 +00001719 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001720 _Tp*
1721>::type
1722__unwrap_iter(move_iterator<_Tp*> __i)
1723{
1724 return __i.base();
1725}
1726
Howard Hinnant499cea12013-08-23 17:37:05 +00001727#if _LIBCPP_DEBUG_LEVEL < 2
1728
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001729template <class _Tp>
1730inline _LIBCPP_INLINE_VISIBILITY
1731typename enable_if
1732<
Howard Hinnant1468b662010-11-19 22:17:28 +00001733 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001734 _Tp*
1735>::type
1736__unwrap_iter(__wrap_iter<_Tp*> __i)
1737{
1738 return __i.base();
1739}
1740
Howard Hinnant499cea12013-08-23 17:37:05 +00001741#endif // _LIBCPP_DEBUG_LEVEL < 2
1742
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001743template <class _InputIterator, class _OutputIterator>
1744inline _LIBCPP_INLINE_VISIBILITY
1745_OutputIterator
1746__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1747{
Eric Fiselierb9919752014-10-27 19:28:20 +00001748 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001749 *__result = *__first;
1750 return __result;
1751}
1752
1753template <class _Tp, class _Up>
1754inline _LIBCPP_INLINE_VISIBILITY
1755typename enable_if
1756<
1757 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001758 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001759 _Up*
1760>::type
1761__copy(_Tp* __first, _Tp* __last, _Up* __result)
1762{
1763 const size_t __n = static_cast<size_t>(__last - __first);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001764 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001765 return __result + __n;
1766}
1767
1768template <class _InputIterator, class _OutputIterator>
1769inline _LIBCPP_INLINE_VISIBILITY
1770_OutputIterator
1771copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1772{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001773 return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001774}
1775
1776// copy_backward
1777
Howard Hinnantb73568d2013-02-06 21:03:39 +00001778template <class _BidirectionalIterator, class _OutputIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001779inline _LIBCPP_INLINE_VISIBILITY
1780_OutputIterator
Howard Hinnantb73568d2013-02-06 21:03:39 +00001781__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001782{
1783 while (__first != __last)
1784 *--__result = *--__last;
1785 return __result;
1786}
1787
1788template <class _Tp, class _Up>
1789inline _LIBCPP_INLINE_VISIBILITY
1790typename enable_if
1791<
1792 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001793 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001794 _Up*
1795>::type
1796__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1797{
1798 const size_t __n = static_cast<size_t>(__last - __first);
1799 __result -= __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001800 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001801 return __result;
1802}
1803
1804template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1805inline _LIBCPP_INLINE_VISIBILITY
1806_BidirectionalIterator2
1807copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1808 _BidirectionalIterator2 __result)
1809{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001810 return _VSTD::__copy_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001811}
1812
1813// copy_if
1814
1815template<class _InputIterator, class _OutputIterator, class _Predicate>
1816inline _LIBCPP_INLINE_VISIBILITY
1817_OutputIterator
1818copy_if(_InputIterator __first, _InputIterator __last,
1819 _OutputIterator __result, _Predicate __pred)
1820{
1821 for (; __first != __last; ++__first)
1822 {
1823 if (__pred(*__first))
1824 {
1825 *__result = *__first;
1826 ++__result;
1827 }
1828 }
1829 return __result;
1830}
1831
1832// copy_n
1833
1834template<class _InputIterator, class _Size, class _OutputIterator>
1835inline _LIBCPP_INLINE_VISIBILITY
1836typename enable_if
1837<
1838 __is_input_iterator<_InputIterator>::value &&
1839 !__is_random_access_iterator<_InputIterator>::value,
1840 _OutputIterator
1841>::type
1842copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1843{
Howard Hinnant171869e2011-02-27 20:55:39 +00001844 if (__n > 0)
1845 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001846 *__result = *__first;
Howard Hinnant171869e2011-02-27 20:55:39 +00001847 ++__result;
1848 for (--__n; __n > 0; --__n)
1849 {
1850 ++__first;
1851 *__result = *__first;
1852 ++__result;
1853 }
1854 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001855 return __result;
1856}
1857
1858template<class _InputIterator, class _Size, class _OutputIterator>
1859inline _LIBCPP_INLINE_VISIBILITY
1860typename enable_if
1861<
1862 __is_random_access_iterator<_InputIterator>::value,
1863 _OutputIterator
1864>::type
1865copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
1866{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001867 return _VSTD::copy(__first, __first + __n, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001868}
1869
1870// move
1871
1872template <class _InputIterator, class _OutputIterator>
1873inline _LIBCPP_INLINE_VISIBILITY
1874_OutputIterator
1875__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1876{
Eric Fiselierb9919752014-10-27 19:28:20 +00001877 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001878 *__result = _VSTD::move(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001879 return __result;
1880}
1881
1882template <class _Tp, class _Up>
1883inline _LIBCPP_INLINE_VISIBILITY
1884typename enable_if
1885<
1886 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001887 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001888 _Up*
1889>::type
1890__move(_Tp* __first, _Tp* __last, _Up* __result)
1891{
1892 const size_t __n = static_cast<size_t>(__last - __first);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001893 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001894 return __result + __n;
1895}
1896
1897template <class _InputIterator, class _OutputIterator>
1898inline _LIBCPP_INLINE_VISIBILITY
1899_OutputIterator
1900move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1901{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001902 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001903}
1904
1905// move_backward
1906
1907template <class _InputIterator, class _OutputIterator>
1908inline _LIBCPP_INLINE_VISIBILITY
1909_OutputIterator
1910__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1911{
1912 while (__first != __last)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001913 *--__result = _VSTD::move(*--__last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001914 return __result;
1915}
1916
1917template <class _Tp, class _Up>
1918inline _LIBCPP_INLINE_VISIBILITY
1919typename enable_if
1920<
1921 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001922 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001923 _Up*
1924>::type
1925__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1926{
1927 const size_t __n = static_cast<size_t>(__last - __first);
1928 __result -= __n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001929 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001930 return __result;
1931}
1932
1933template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1934inline _LIBCPP_INLINE_VISIBILITY
1935_BidirectionalIterator2
1936move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1937 _BidirectionalIterator2 __result)
1938{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001939 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001940}
1941
1942// iter_swap
1943
Howard Hinnante9b2c2d2011-05-27 15:04:19 +00001944// moved to <type_traits> for better swap / noexcept support
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001945
1946// transform
1947
1948template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
1949inline _LIBCPP_INLINE_VISIBILITY
1950_OutputIterator
1951transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1952{
Eric Fiselierb9919752014-10-27 19:28:20 +00001953 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001954 *__result = __op(*__first);
1955 return __result;
1956}
1957
1958template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
1959inline _LIBCPP_INLINE_VISIBILITY
1960_OutputIterator
1961transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1962 _OutputIterator __result, _BinaryOperation __binary_op)
1963{
Eric Fiselierb9919752014-10-27 19:28:20 +00001964 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001965 *__result = __binary_op(*__first1, *__first2);
1966 return __result;
1967}
1968
1969// replace
1970
1971template <class _ForwardIterator, class _Tp>
1972inline _LIBCPP_INLINE_VISIBILITY
1973void
1974replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1975{
1976 for (; __first != __last; ++__first)
1977 if (*__first == __old_value)
1978 *__first = __new_value;
1979}
1980
1981// replace_if
1982
1983template <class _ForwardIterator, class _Predicate, class _Tp>
1984inline _LIBCPP_INLINE_VISIBILITY
1985void
1986replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1987{
1988 for (; __first != __last; ++__first)
1989 if (__pred(*__first))
1990 *__first = __new_value;
1991}
1992
1993// replace_copy
1994
1995template <class _InputIterator, class _OutputIterator, class _Tp>
1996inline _LIBCPP_INLINE_VISIBILITY
1997_OutputIterator
1998replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1999 const _Tp& __old_value, const _Tp& __new_value)
2000{
Eric Fiselierb9919752014-10-27 19:28:20 +00002001 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002002 if (*__first == __old_value)
2003 *__result = __new_value;
2004 else
2005 *__result = *__first;
2006 return __result;
2007}
2008
2009// replace_copy_if
2010
2011template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
2012inline _LIBCPP_INLINE_VISIBILITY
2013_OutputIterator
2014replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2015 _Predicate __pred, const _Tp& __new_value)
2016{
Eric Fiselierb9919752014-10-27 19:28:20 +00002017 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002018 if (__pred(*__first))
2019 *__result = __new_value;
2020 else
2021 *__result = *__first;
2022 return __result;
2023}
2024
2025// fill_n
2026
2027template <class _OutputIterator, class _Size, class _Tp>
2028inline _LIBCPP_INLINE_VISIBILITY
2029_OutputIterator
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002030__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002031{
Eric Fiselierb9919752014-10-27 19:28:20 +00002032 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnant78b68282011-10-22 20:59:45 +00002033 *__first = __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002034 return __first;
2035}
2036
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002037template <class _Tp, class _Size, class _Up>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002038inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002039typename enable_if
2040<
2041 is_integral<_Tp>::value && sizeof(_Tp) == 1 &&
2042 !is_same<_Tp, bool>::value &&
2043 is_integral<_Up>::value && sizeof(_Up) == 1,
2044 _Tp*
2045>::type
2046__fill_n(_Tp* __first, _Size __n,_Up __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002047{
2048 if (__n > 0)
Howard Hinnant78b68282011-10-22 20:59:45 +00002049 _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002050 return __first + __n;
2051}
2052
2053template <class _OutputIterator, class _Size, class _Tp>
2054inline _LIBCPP_INLINE_VISIBILITY
2055_OutputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002056fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002057{
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002058 return _VSTD::__fill_n(__first, __n, __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002059}
2060
2061// fill
2062
2063template <class _ForwardIterator, class _Tp>
2064inline _LIBCPP_INLINE_VISIBILITY
2065void
Howard Hinnant78b68282011-10-22 20:59:45 +00002066__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002067{
2068 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +00002069 *__first = __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070}
2071
2072template <class _RandomAccessIterator, class _Tp>
2073inline _LIBCPP_INLINE_VISIBILITY
2074void
Howard Hinnant78b68282011-10-22 20:59:45 +00002075__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002076{
Howard Hinnant78b68282011-10-22 20:59:45 +00002077 _VSTD::fill_n(__first, __last - __first, __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002078}
2079
2080template <class _ForwardIterator, class _Tp>
2081inline _LIBCPP_INLINE_VISIBILITY
2082void
Howard Hinnant78b68282011-10-22 20:59:45 +00002083fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002084{
Howard Hinnant78b68282011-10-22 20:59:45 +00002085 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002086}
2087
2088// generate
2089
2090template <class _ForwardIterator, class _Generator>
2091inline _LIBCPP_INLINE_VISIBILITY
2092void
2093generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2094{
2095 for (; __first != __last; ++__first)
2096 *__first = __gen();
2097}
2098
2099// generate_n
2100
2101template <class _OutputIterator, class _Size, class _Generator>
2102inline _LIBCPP_INLINE_VISIBILITY
2103_OutputIterator
2104generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
2105{
Eric Fiselierb9919752014-10-27 19:28:20 +00002106 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002107 *__first = __gen();
2108 return __first;
2109}
2110
2111// remove
2112
2113template <class _ForwardIterator, class _Tp>
2114_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002115remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002116{
Howard Hinnant78b68282011-10-22 20:59:45 +00002117 __first = _VSTD::find(__first, __last, __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002118 if (__first != __last)
2119 {
2120 _ForwardIterator __i = __first;
2121 while (++__i != __last)
2122 {
Howard Hinnant78b68282011-10-22 20:59:45 +00002123 if (!(*__i == __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002124 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002125 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002126 ++__first;
2127 }
2128 }
2129 }
2130 return __first;
2131}
2132
2133// remove_if
2134
2135template <class _ForwardIterator, class _Predicate>
2136_ForwardIterator
2137remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2138{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002139 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002140 (__first, __last, __pred);
2141 if (__first != __last)
2142 {
2143 _ForwardIterator __i = __first;
2144 while (++__i != __last)
2145 {
2146 if (!__pred(*__i))
2147 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002148 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002149 ++__first;
2150 }
2151 }
2152 }
2153 return __first;
2154}
2155
2156// remove_copy
2157
2158template <class _InputIterator, class _OutputIterator, class _Tp>
2159inline _LIBCPP_INLINE_VISIBILITY
2160_OutputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002161remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002162{
2163 for (; __first != __last; ++__first)
2164 {
Howard Hinnant78b68282011-10-22 20:59:45 +00002165 if (!(*__first == __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002166 {
2167 *__result = *__first;
2168 ++__result;
2169 }
2170 }
2171 return __result;
2172}
2173
2174// remove_copy_if
2175
2176template <class _InputIterator, class _OutputIterator, class _Predicate>
2177inline _LIBCPP_INLINE_VISIBILITY
2178_OutputIterator
2179remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2180{
2181 for (; __first != __last; ++__first)
2182 {
2183 if (!__pred(*__first))
2184 {
2185 *__result = *__first;
2186 ++__result;
2187 }
2188 }
2189 return __result;
2190}
2191
2192// unique
2193
2194template <class _ForwardIterator, class _BinaryPredicate>
2195_ForwardIterator
2196unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2197{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002198 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002199 (__first, __last, __pred);
2200 if (__first != __last)
2201 {
2202 // ... a a ? ...
2203 // f i
2204 _ForwardIterator __i = __first;
2205 for (++__i; ++__i != __last;)
2206 if (!__pred(*__first, *__i))
Howard Hinnant0949eed2011-06-30 21:18:19 +00002207 *++__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002208 ++__first;
2209 }
2210 return __first;
2211}
2212
2213template <class _ForwardIterator>
2214inline _LIBCPP_INLINE_VISIBILITY
2215_ForwardIterator
2216unique(_ForwardIterator __first, _ForwardIterator __last)
2217{
2218 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002219 return _VSTD::unique(__first, __last, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002220}
2221
2222// unique_copy
2223
2224template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
2225_OutputIterator
2226__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2227 input_iterator_tag, output_iterator_tag)
2228{
2229 if (__first != __last)
2230 {
2231 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2232 *__result = __t;
2233 ++__result;
2234 while (++__first != __last)
2235 {
2236 if (!__pred(__t, *__first))
2237 {
2238 __t = *__first;
2239 *__result = __t;
2240 ++__result;
2241 }
2242 }
2243 }
2244 return __result;
2245}
2246
2247template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
2248_OutputIterator
2249__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2250 forward_iterator_tag, output_iterator_tag)
2251{
2252 if (__first != __last)
2253 {
2254 _ForwardIterator __i = __first;
2255 *__result = *__i;
2256 ++__result;
2257 while (++__first != __last)
2258 {
2259 if (!__pred(*__i, *__first))
2260 {
2261 *__result = *__first;
2262 ++__result;
2263 __i = __first;
2264 }
2265 }
2266 }
2267 return __result;
2268}
2269
2270template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
2271_ForwardIterator
2272__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2273 input_iterator_tag, forward_iterator_tag)
2274{
2275 if (__first != __last)
2276 {
2277 *__result = *__first;
2278 while (++__first != __last)
2279 if (!__pred(*__result, *__first))
2280 *++__result = *__first;
2281 ++__result;
2282 }
2283 return __result;
2284}
2285
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002286template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
2287inline _LIBCPP_INLINE_VISIBILITY
2288_OutputIterator
2289unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2290{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002291 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002292 (__first, __last, __result, __pred,
2293 typename iterator_traits<_InputIterator>::iterator_category(),
2294 typename iterator_traits<_OutputIterator>::iterator_category());
2295}
2296
2297template <class _InputIterator, class _OutputIterator>
2298inline _LIBCPP_INLINE_VISIBILITY
2299_OutputIterator
2300unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2301{
2302 typedef typename iterator_traits<_InputIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002303 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002304}
2305
2306// reverse
2307
2308template <class _BidirectionalIterator>
2309inline _LIBCPP_INLINE_VISIBILITY
2310void
2311__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2312{
2313 while (__first != __last)
2314 {
2315 if (__first == --__last)
2316 break;
2317 swap(*__first, *__last);
2318 ++__first;
2319 }
2320}
2321
2322template <class _RandomAccessIterator>
2323inline _LIBCPP_INLINE_VISIBILITY
2324void
2325__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2326{
2327 if (__first != __last)
2328 for (; __first < --__last; ++__first)
2329 swap(*__first, *__last);
2330}
2331
2332template <class _BidirectionalIterator>
2333inline _LIBCPP_INLINE_VISIBILITY
2334void
2335reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2336{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002337 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002338}
2339
2340// reverse_copy
2341
2342template <class _BidirectionalIterator, class _OutputIterator>
2343inline _LIBCPP_INLINE_VISIBILITY
2344_OutputIterator
2345reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2346{
2347 for (; __first != __last; ++__result)
2348 *__result = *--__last;
2349 return __result;
2350}
2351
2352// rotate
2353
2354template <class _ForwardIterator>
2355_ForwardIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002356__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002357{
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002358 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2359 value_type __tmp = _VSTD::move(*__first);
2360 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2361 *__lm1 = _VSTD::move(__tmp);
2362 return __lm1;
2363}
2364
2365template <class _BidirectionalIterator>
2366_BidirectionalIterator
2367__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2368{
2369 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2370 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2371 value_type __tmp = _VSTD::move(*__lm1);
2372 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2373 *__first = _VSTD::move(__tmp);
2374 return __fp1;
2375}
2376
2377template <class _ForwardIterator>
2378_ForwardIterator
2379__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2380{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002381 _ForwardIterator __i = __middle;
2382 while (true)
2383 {
2384 swap(*__first, *__i);
2385 ++__first;
2386 if (++__i == __last)
2387 break;
2388 if (__first == __middle)
2389 __middle = __i;
2390 }
2391 _ForwardIterator __r = __first;
2392 if (__first != __middle)
2393 {
2394 __i = __middle;
2395 while (true)
2396 {
2397 swap(*__first, *__i);
2398 ++__first;
2399 if (++__i == __last)
2400 {
2401 if (__first == __middle)
2402 break;
2403 __i = __middle;
2404 }
2405 else if (__first == __middle)
2406 __middle = __i;
2407 }
2408 }
2409 return __r;
2410}
2411
2412template<typename _Integral>
2413inline _LIBCPP_INLINE_VISIBILITY
2414_Integral
2415__gcd(_Integral __x, _Integral __y)
2416{
2417 do
2418 {
2419 _Integral __t = __x % __y;
2420 __x = __y;
2421 __y = __t;
2422 } while (__y);
2423 return __x;
2424}
2425
2426template<typename _RandomAccessIterator>
2427_RandomAccessIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002428__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002429{
2430 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2431 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant324bb032010-08-22 00:02:43 +00002432
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002433 const difference_type __m1 = __middle - __first;
2434 const difference_type __m2 = __last - __middle;
2435 if (__m1 == __m2)
2436 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002437 _VSTD::swap_ranges(__first, __middle, __middle);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002438 return __middle;
2439 }
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002440 const difference_type __g = _VSTD::__gcd(__m1, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002441 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2442 {
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002443 value_type __t(_VSTD::move(*--__p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002444 _RandomAccessIterator __p1 = __p;
2445 _RandomAccessIterator __p2 = __p1 + __m1;
2446 do
2447 {
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002448 *__p1 = _VSTD::move(*__p2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002449 __p1 = __p2;
2450 const difference_type __d = __last - __p2;
2451 if (__m1 < __d)
2452 __p2 += __m1;
2453 else
2454 __p2 = __first + (__m1 - __d);
2455 } while (__p2 != __p);
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002456 *__p1 = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002457 }
2458 return __first + __m2;
2459}
2460
2461template <class _ForwardIterator>
2462inline _LIBCPP_INLINE_VISIBILITY
2463_ForwardIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002464__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2465 _VSTD::forward_iterator_tag)
2466{
2467 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2468 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2469 {
2470 if (_VSTD::next(__first) == __middle)
2471 return _VSTD::__rotate_left(__first, __last);
2472 }
2473 return _VSTD::__rotate_forward(__first, __middle, __last);
2474}
2475
2476template <class _BidirectionalIterator>
2477inline _LIBCPP_INLINE_VISIBILITY
2478_BidirectionalIterator
2479__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2480 _VSTD::bidirectional_iterator_tag)
2481{
2482 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2483 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2484 {
2485 if (_VSTD::next(__first) == __middle)
2486 return _VSTD::__rotate_left(__first, __last);
2487 if (_VSTD::next(__middle) == __last)
2488 return _VSTD::__rotate_right(__first, __last);
2489 }
2490 return _VSTD::__rotate_forward(__first, __middle, __last);
2491}
2492
2493template <class _RandomAccessIterator>
2494inline _LIBCPP_INLINE_VISIBILITY
2495_RandomAccessIterator
2496__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2497 _VSTD::random_access_iterator_tag)
2498{
2499 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2500 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2501 {
2502 if (_VSTD::next(__first) == __middle)
2503 return _VSTD::__rotate_left(__first, __last);
2504 if (_VSTD::next(__middle) == __last)
2505 return _VSTD::__rotate_right(__first, __last);
2506 return _VSTD::__rotate_gcd(__first, __middle, __last);
2507 }
2508 return _VSTD::__rotate_forward(__first, __middle, __last);
2509}
2510
2511template <class _ForwardIterator>
2512inline _LIBCPP_INLINE_VISIBILITY
2513_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002514rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2515{
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002516 if (__first == __middle)
2517 return __last;
2518 if (__middle == __last)
2519 return __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002520 return _VSTD::__rotate(__first, __middle, __last,
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002521 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002522}
2523
2524// rotate_copy
2525
2526template <class _ForwardIterator, class _OutputIterator>
2527inline _LIBCPP_INLINE_VISIBILITY
2528_OutputIterator
2529rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2530{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002531 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002532}
2533
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002534// min_element
2535
2536template <class _ForwardIterator, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002537inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002538_ForwardIterator
Marshall Clow9d9463a2014-02-19 16:51:35 +00002539__min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002540{
2541 if (__first != __last)
2542 {
2543 _ForwardIterator __i = __first;
2544 while (++__i != __last)
2545 if (__comp(*__i, *__first))
2546 __first = __i;
2547 }
2548 return __first;
2549}
2550
Marshall Clow9d9463a2014-02-19 16:51:35 +00002551template <class _ForwardIterator, class _Compare>
2552inline _LIBCPP_INLINE_VISIBILITY
2553_ForwardIterator
2554min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2555{
2556 return __min_element(__first, __last, __comp);
2557}
2558
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002559template <class _ForwardIterator>
2560inline _LIBCPP_INLINE_VISIBILITY
2561_ForwardIterator
2562min_element(_ForwardIterator __first, _ForwardIterator __last)
2563{
Marshall Clow9d9463a2014-02-19 16:51:35 +00002564 return __min_element(__first, __last,
Howard Hinnant98e5d972010-08-21 20:10:01 +00002565 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2566}
2567
2568// min
2569
2570template <class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002571inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002572const _Tp&
2573min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2574{
2575 return __comp(__b, __a) ? __b : __a;
2576}
2577
2578template <class _Tp>
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)
2582{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002583 return _VSTD::min(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002584}
2585
Howard Hinnante3e32912011-08-12 21:56:02 +00002586#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2587
Howard Hinnant98e5d972010-08-21 20:10:01 +00002588template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002589inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002590_Tp
2591min(initializer_list<_Tp> __t, _Compare __comp)
2592{
Marshall Clow9d9463a2014-02-19 16:51:35 +00002593 return *__min_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002594}
2595
2596template<class _Tp>
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)
2600{
Marshall Clow9d9463a2014-02-19 16:51:35 +00002601 return *__min_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002602}
2603
Howard Hinnante3e32912011-08-12 21:56:02 +00002604#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2605
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002606// max_element
2607
2608template <class _ForwardIterator, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002609inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002610_ForwardIterator
Marshall Clow9d9463a2014-02-19 16:51:35 +00002611__max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002612{
2613 if (__first != __last)
2614 {
2615 _ForwardIterator __i = __first;
2616 while (++__i != __last)
2617 if (__comp(*__first, *__i))
2618 __first = __i;
2619 }
2620 return __first;
2621}
2622
Marshall Clow9d9463a2014-02-19 16:51:35 +00002623
2624template <class _ForwardIterator, class _Compare>
2625inline _LIBCPP_INLINE_VISIBILITY
2626_ForwardIterator
2627max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2628{
2629 return __max_element(__first, __last, __comp);
2630}
2631
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002632template <class _ForwardIterator>
2633inline _LIBCPP_INLINE_VISIBILITY
2634_ForwardIterator
2635max_element(_ForwardIterator __first, _ForwardIterator __last)
2636{
Marshall Clow9d9463a2014-02-19 16:51:35 +00002637 return __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 Clow9d9463a2014-02-19 16:51:35 +00002666 return *__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 Clow9d9463a2014-02-19 16:51:35 +00002674 return *__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>
2682std::pair<_ForwardIterator, _ForwardIterator>
2683minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2684{
2685 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2686 if (__first != __last)
2687 {
2688 if (++__first != __last)
2689 {
2690 if (__comp(*__first, *__result.first))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002691 __result.first = __first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002692 else
2693 __result.second = __first;
2694 while (++__first != __last)
2695 {
2696 _ForwardIterator __i = __first;
2697 if (++__first == __last)
2698 {
2699 if (__comp(*__i, *__result.first))
2700 __result.first = __i;
2701 else if (!__comp(*__i, *__result.second))
2702 __result.second = __i;
2703 break;
2704 }
2705 else
2706 {
2707 if (__comp(*__first, *__i))
2708 {
2709 if (__comp(*__first, *__result.first))
2710 __result.first = __first;
2711 if (!__comp(*__i, *__result.second))
2712 __result.second = __i;
2713 }
2714 else
2715 {
2716 if (__comp(*__i, *__result.first))
2717 __result.first = __i;
2718 if (!__comp(*__first, *__result.second))
2719 __result.second = __first;
2720 }
2721 }
2722 }
2723 }
2724 }
2725 return __result;
2726}
2727
2728template <class _ForwardIterator>
2729inline _LIBCPP_INLINE_VISIBILITY
2730std::pair<_ForwardIterator, _ForwardIterator>
2731minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2732{
Marshall Clow9d9463a2014-02-19 16:51:35 +00002733 return _VSTD::minmax_element(__first, __last,
2734 __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002735}
2736
Howard Hinnant98e5d972010-08-21 20:10:01 +00002737// minmax
2738
2739template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002740inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002741pair<const _Tp&, const _Tp&>
2742minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2743{
2744 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2745 pair<const _Tp&, const _Tp&>(__a, __b);
2746}
2747
2748template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002749inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002750pair<const _Tp&, const _Tp&>
2751minmax(const _Tp& __a, const _Tp& __b)
2752{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002753 return _VSTD::minmax(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002754}
2755
Howard Hinnante3e32912011-08-12 21:56:02 +00002756#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2757
Howard Hinnant98e5d972010-08-21 20:10:01 +00002758template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002759inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002760pair<_Tp, _Tp>
2761minmax(initializer_list<_Tp> __t, _Compare __comp)
2762{
Marshall Clow9d9463a2014-02-19 16:51:35 +00002763 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2764 _Iter __first = __t.begin();
2765 _Iter __last = __t.end();
2766 std::pair<_Tp, _Tp> __result ( *__first, *__first );
2767
2768 ++__first;
2769 if (__t.size() % 2 == 0)
2770 {
2771 if (__comp(*__first, __result.first))
2772 __result.first = *__first;
2773 else
2774 __result.second = *__first;
2775 ++__first;
2776 }
2777
2778 while (__first != __last)
2779 {
2780 _Tp __prev = *__first++;
2781 if (__comp(__prev, *__first)) {
2782 if (__comp(__prev, __result.first)) __result.first = __prev;
2783 if (__comp(__result.second, *__first)) __result.second = *__first;
2784 }
2785 else {
2786 if (__comp(*__first, __result.first)) __result.first = *__first;
2787 if (__comp(__result.second, __prev)) __result.second = __prev;
2788 }
2789
2790 __first++;
2791 }
2792 return __result;
2793}
2794
2795template<class _Tp>
2796inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2797pair<_Tp, _Tp>
2798minmax(initializer_list<_Tp> __t)
2799{
2800 return _VSTD::minmax(__t, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002801}
2802
Howard Hinnante3e32912011-08-12 21:56:02 +00002803#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2804
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002805// random_shuffle
2806
Howard Hinnantc3267212010-05-26 17:49:34 +00002807// __independent_bits_engine
2808
Howard Hinnant99968442011-11-29 18:15:50 +00002809template <unsigned long long _Xp, size_t _Rp>
Howard Hinnantc3267212010-05-26 17:49:34 +00002810struct __log2_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002811{
Howard Hinnant99968442011-11-29 18:15:50 +00002812 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2813 : __log2_imp<_Xp, _Rp - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002814};
2815
Howard Hinnant99968442011-11-29 18:15:50 +00002816template <unsigned long long _Xp>
2817struct __log2_imp<_Xp, 0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002818{
Howard Hinnantc3267212010-05-26 17:49:34 +00002819 static const size_t value = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002820};
2821
Howard Hinnant99968442011-11-29 18:15:50 +00002822template <size_t _Rp>
2823struct __log2_imp<0, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002824{
Howard Hinnant99968442011-11-29 18:15:50 +00002825 static const size_t value = _Rp + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002826};
2827
Howard Hinnant99968442011-11-29 18:15:50 +00002828template <class _UI, _UI _Xp>
Howard Hinnantc3267212010-05-26 17:49:34 +00002829struct __log2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002830{
Howard Hinnant99968442011-11-29 18:15:50 +00002831 static const size_t value = __log2_imp<_Xp,
Howard Hinnantc3267212010-05-26 17:49:34 +00002832 sizeof(_UI) * __CHAR_BIT__ - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002833};
2834
Howard Hinnantc3267212010-05-26 17:49:34 +00002835template<class _Engine, class _UIntType>
2836class __independent_bits_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002837{
Howard Hinnantc3267212010-05-26 17:49:34 +00002838public:
2839 // types
2840 typedef _UIntType result_type;
2841
2842private:
2843 typedef typename _Engine::result_type _Engine_result_type;
2844 typedef typename conditional
2845 <
2846 sizeof(_Engine_result_type) <= sizeof(result_type),
2847 result_type,
2848 _Engine_result_type
2849 >::type _Working_result_type;
2850
2851 _Engine& __e_;
2852 size_t __w_;
2853 size_t __w0_;
2854 size_t __n_;
2855 size_t __n0_;
2856 _Working_result_type __y0_;
2857 _Working_result_type __y1_;
2858 _Engine_result_type __mask0_;
2859 _Engine_result_type __mask1_;
2860
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002861#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnant99968442011-11-29 18:15:50 +00002862 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002863 + _Working_result_type(1);
2864#else
2865 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2866 + _Working_result_type(1);
2867#endif
2868 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2869 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2870 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
Howard Hinnantc3267212010-05-26 17:49:34 +00002871
2872public:
2873 // constructors and seeding functions
2874 __independent_bits_engine(_Engine& __e, size_t __w);
2875
2876 // generating functions
Howard Hinnant99968442011-11-29 18:15:50 +00002877 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantc3267212010-05-26 17:49:34 +00002878
2879private:
2880 result_type __eval(false_type);
2881 result_type __eval(true_type);
2882};
2883
2884template<class _Engine, class _UIntType>
2885__independent_bits_engine<_Engine, _UIntType>
2886 ::__independent_bits_engine(_Engine& __e, size_t __w)
2887 : __e_(__e),
2888 __w_(__w)
2889{
2890 __n_ = __w_ / __m + (__w_ % __m != 0);
2891 __w0_ = __w_ / __n_;
Howard Hinnant99968442011-11-29 18:15:50 +00002892 if (_Rp == 0)
2893 __y0_ = _Rp;
Howard Hinnantc3267212010-05-26 17:49:34 +00002894 else if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00002895 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002896 else
2897 __y0_ = 0;
Howard Hinnant99968442011-11-29 18:15:50 +00002898 if (_Rp - __y0_ > __y0_ / __n_)
Howard Hinnantc3267212010-05-26 17:49:34 +00002899 {
2900 ++__n_;
2901 __w0_ = __w_ / __n_;
2902 if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00002903 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002904 else
2905 __y0_ = 0;
2906 }
2907 __n0_ = __n_ - __w_ % __n_;
2908 if (__w0_ < _WDt - 1)
Howard Hinnant99968442011-11-29 18:15:50 +00002909 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
Howard Hinnantc3267212010-05-26 17:49:34 +00002910 else
2911 __y1_ = 0;
2912 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2913 _Engine_result_type(0);
2914 __mask1_ = __w0_ < _EDt - 1 ?
2915 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2916 _Engine_result_type(~0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002917}
2918
Howard Hinnantc3267212010-05-26 17:49:34 +00002919template<class _Engine, class _UIntType>
2920inline
2921_UIntType
2922__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002923{
Howard Hinnantc3267212010-05-26 17:49:34 +00002924 return static_cast<result_type>(__e_() & __mask0_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002925}
2926
Howard Hinnantc3267212010-05-26 17:49:34 +00002927template<class _Engine, class _UIntType>
2928_UIntType
2929__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002930{
Howard Hinnant99968442011-11-29 18:15:50 +00002931 result_type _Sp = 0;
Howard Hinnantc3267212010-05-26 17:49:34 +00002932 for (size_t __k = 0; __k < __n0_; ++__k)
2933 {
2934 _Engine_result_type __u;
2935 do
2936 {
2937 __u = __e_() - _Engine::min();
2938 } while (__u >= __y0_);
Howard Hinnant8faa95f2011-10-27 16:12:10 +00002939 if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00002940 _Sp <<= __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002941 else
Howard Hinnant99968442011-11-29 18:15:50 +00002942 _Sp = 0;
2943 _Sp += __u & __mask0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002944 }
2945 for (size_t __k = __n0_; __k < __n_; ++__k)
2946 {
2947 _Engine_result_type __u;
2948 do
2949 {
2950 __u = __e_() - _Engine::min();
2951 } while (__u >= __y1_);
Howard Hinnant8faa95f2011-10-27 16:12:10 +00002952 if (__w0_ < _WDt - 1)
Howard Hinnant99968442011-11-29 18:15:50 +00002953 _Sp <<= __w0_ + 1;
Howard Hinnantc3267212010-05-26 17:49:34 +00002954 else
Howard Hinnant99968442011-11-29 18:15:50 +00002955 _Sp = 0;
2956 _Sp += __u & __mask1_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002957 }
Howard Hinnant99968442011-11-29 18:15:50 +00002958 return _Sp;
Howard Hinnantc3267212010-05-26 17:49:34 +00002959}
2960
2961// uniform_int_distribution
2962
2963template<class _IntType = int>
2964class uniform_int_distribution
2965{
2966public:
2967 // types
2968 typedef _IntType result_type;
2969
2970 class param_type
2971 {
2972 result_type __a_;
2973 result_type __b_;
2974 public:
2975 typedef uniform_int_distribution distribution_type;
2976
2977 explicit param_type(result_type __a = 0,
2978 result_type __b = numeric_limits<result_type>::max())
2979 : __a_(__a), __b_(__b) {}
2980
2981 result_type a() const {return __a_;}
2982 result_type b() const {return __b_;}
2983
2984 friend bool operator==(const param_type& __x, const param_type& __y)
2985 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2986 friend bool operator!=(const param_type& __x, const param_type& __y)
2987 {return !(__x == __y);}
2988 };
2989
2990private:
2991 param_type __p_;
2992
2993public:
2994 // constructors and reset functions
2995 explicit uniform_int_distribution(result_type __a = 0,
2996 result_type __b = numeric_limits<result_type>::max())
2997 : __p_(param_type(__a, __b)) {}
2998 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
2999 void reset() {}
3000
3001 // generating functions
3002 template<class _URNG> result_type operator()(_URNG& __g)
3003 {return (*this)(__g, __p_);}
3004 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3005
3006 // property functions
3007 result_type a() const {return __p_.a();}
3008 result_type b() const {return __p_.b();}
3009
3010 param_type param() const {return __p_;}
3011 void param(const param_type& __p) {__p_ = __p;}
3012
3013 result_type min() const {return a();}
3014 result_type max() const {return b();}
3015
3016 friend bool operator==(const uniform_int_distribution& __x,
3017 const uniform_int_distribution& __y)
3018 {return __x.__p_ == __y.__p_;}
3019 friend bool operator!=(const uniform_int_distribution& __x,
3020 const uniform_int_distribution& __y)
3021 {return !(__x == __y);}
3022};
3023
3024template<class _IntType>
3025template<class _URNG>
3026typename uniform_int_distribution<_IntType>::result_type
3027uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3028{
3029 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3030 uint32_t, uint64_t>::type _UIntType;
Howard Hinnant99968442011-11-29 18:15:50 +00003031 const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
3032 if (_Rp == 1)
Howard Hinnantc3267212010-05-26 17:49:34 +00003033 return __p.a();
3034 const size_t _Dt = numeric_limits<_UIntType>::digits;
3035 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
Howard Hinnant99968442011-11-29 18:15:50 +00003036 if (_Rp == 0)
Howard Hinnantc3267212010-05-26 17:49:34 +00003037 return static_cast<result_type>(_Eng(__g, _Dt)());
Howard Hinnant99968442011-11-29 18:15:50 +00003038 size_t __w = _Dt - __clz(_Rp) - 1;
3039 if ((_Rp & (_UIntType(~0) >> (_Dt - __w))) != 0)
Howard Hinnantc3267212010-05-26 17:49:34 +00003040 ++__w;
3041 _Eng __e(__g, __w);
3042 _UIntType __u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003043 do
Howard Hinnantc3267212010-05-26 17:49:34 +00003044 {
3045 __u = __e();
Howard Hinnant99968442011-11-29 18:15:50 +00003046 } while (__u >= _Rp);
Howard Hinnantc3267212010-05-26 17:49:34 +00003047 return static_cast<result_type>(__u + __p.a());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003048}
3049
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003050class _LIBCPP_TYPE_VIS __rs_default;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003051
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003052_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantc3267212010-05-26 17:49:34 +00003053
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003054class _LIBCPP_TYPE_VIS __rs_default
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003055{
Howard Hinnantc3267212010-05-26 17:49:34 +00003056 static unsigned __c_;
3057
3058 __rs_default();
3059public:
Marshall Clow5920cfc2013-02-07 22:12:02 +00003060 typedef uint_fast32_t result_type;
Howard Hinnantc3267212010-05-26 17:49:34 +00003061
3062 static const result_type _Min = 0;
3063 static const result_type _Max = 0xFFFFFFFF;
3064
3065 __rs_default(const __rs_default&);
3066 ~__rs_default();
3067
3068 result_type operator()();
3069
Howard Hinnant27b4fd32012-04-02 00:40:41 +00003070 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
3071 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
Howard Hinnantc3267212010-05-26 17:49:34 +00003072
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003073 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003074};
3075
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003076_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003077
3078template <class _RandomAccessIterator>
3079void
3080random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
3081{
3082 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnant99968442011-11-29 18:15:50 +00003083 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3084 typedef typename _Dp::param_type _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003085 difference_type __d = __last - __first;
3086 if (__d > 1)
3087 {
Howard Hinnant99968442011-11-29 18:15:50 +00003088 _Dp __uid;
Howard Hinnantc3267212010-05-26 17:49:34 +00003089 __rs_default __g = __rs_get();
3090 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003091 {
Howard Hinnant99968442011-11-29 18:15:50 +00003092 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant4e599482010-10-22 15:26:39 +00003093 if (__i != difference_type(0))
3094 swap(*__first, *(__first + __i));
3095 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003096 }
3097}
3098
3099template <class _RandomAccessIterator, class _RandomNumberGenerator>
3100void
3101random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnant73d21a42010-09-04 23:28:19 +00003102#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003103 _RandomNumberGenerator&& __rand)
3104#else
3105 _RandomNumberGenerator& __rand)
3106#endif
3107{
3108 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3109 difference_type __d = __last - __first;
3110 if (__d > 1)
3111 {
3112 for (--__last; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003113 {
3114 difference_type __i = __rand(__d);
3115 swap(*__first, *(__first + __i));
3116 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003117 }
3118}
3119
Howard Hinnantc3267212010-05-26 17:49:34 +00003120template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3121 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnant278bf2d2010-11-18 01:47:02 +00003122#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3123 _UniformRandomNumberGenerator&& __g)
3124#else
Howard Hinnantc3267212010-05-26 17:49:34 +00003125 _UniformRandomNumberGenerator& __g)
Howard Hinnant278bf2d2010-11-18 01:47:02 +00003126#endif
Howard Hinnantc3267212010-05-26 17:49:34 +00003127{
3128 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnant99968442011-11-29 18:15:50 +00003129 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3130 typedef typename _Dp::param_type _Pp;
Howard Hinnantc3267212010-05-26 17:49:34 +00003131 difference_type __d = __last - __first;
3132 if (__d > 1)
3133 {
Howard Hinnant99968442011-11-29 18:15:50 +00003134 _Dp __uid;
Howard Hinnantc3267212010-05-26 17:49:34 +00003135 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003136 {
Howard Hinnant99968442011-11-29 18:15:50 +00003137 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant4e599482010-10-22 15:26:39 +00003138 if (__i != difference_type(0))
3139 swap(*__first, *(__first + __i));
3140 }
Howard Hinnantc3267212010-05-26 17:49:34 +00003141 }
3142}
3143
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003144template <class _InputIterator, class _Predicate>
3145bool
3146is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3147{
3148 for (; __first != __last; ++__first)
3149 if (!__pred(*__first))
3150 break;
3151 for (; __first != __last; ++__first)
3152 if (__pred(*__first))
3153 return false;
3154 return true;
3155}
3156
3157// partition
3158
3159template <class _Predicate, class _ForwardIterator>
3160_ForwardIterator
3161__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3162{
3163 while (true)
3164 {
3165 if (__first == __last)
3166 return __first;
3167 if (!__pred(*__first))
3168 break;
3169 ++__first;
3170 }
3171 for (_ForwardIterator __p = __first; ++__p != __last;)
3172 {
3173 if (__pred(*__p))
3174 {
3175 swap(*__first, *__p);
3176 ++__first;
3177 }
3178 }
3179 return __first;
3180}
3181
3182template <class _Predicate, class _BidirectionalIterator>
3183_BidirectionalIterator
3184__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3185 bidirectional_iterator_tag)
3186{
3187 while (true)
3188 {
3189 while (true)
3190 {
3191 if (__first == __last)
3192 return __first;
3193 if (!__pred(*__first))
3194 break;
3195 ++__first;
3196 }
3197 do
3198 {
3199 if (__first == --__last)
3200 return __first;
3201 } while (!__pred(*__last));
3202 swap(*__first, *__last);
3203 ++__first;
3204 }
3205}
3206
3207template <class _ForwardIterator, class _Predicate>
3208inline _LIBCPP_INLINE_VISIBILITY
3209_ForwardIterator
3210partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3211{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003212 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003213 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3214}
3215
3216// partition_copy
3217
3218template <class _InputIterator, class _OutputIterator1,
3219 class _OutputIterator2, class _Predicate>
3220pair<_OutputIterator1, _OutputIterator2>
3221partition_copy(_InputIterator __first, _InputIterator __last,
3222 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3223 _Predicate __pred)
3224{
3225 for (; __first != __last; ++__first)
3226 {
3227 if (__pred(*__first))
3228 {
3229 *__out_true = *__first;
3230 ++__out_true;
3231 }
3232 else
3233 {
3234 *__out_false = *__first;
3235 ++__out_false;
3236 }
3237 }
3238 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3239}
3240
3241// partition_point
3242
3243template<class _ForwardIterator, class _Predicate>
3244_ForwardIterator
3245partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3246{
3247 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003248 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003249 while (__len != 0)
3250 {
3251 difference_type __l2 = __len / 2;
3252 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003253 _VSTD::advance(__m, __l2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003254 if (__pred(*__m))
3255 {
3256 __first = ++__m;
3257 __len -= __l2 + 1;
3258 }
3259 else
3260 __len = __l2;
3261 }
3262 return __first;
3263}
3264
3265// stable_partition
3266
3267template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3268_ForwardIterator
3269__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3270 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3271{
3272 // *__first is known to be false
3273 // __len >= 1
3274 if (__len == 1)
3275 return __first;
3276 if (__len == 2)
3277 {
3278 _ForwardIterator __m = __first;
3279 if (__pred(*++__m))
3280 {
3281 swap(*__first, *__m);
3282 return __m;
3283 }
3284 return __first;
3285 }
3286 if (__len <= __p.second)
3287 { // The buffer is big enough to use
3288 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3289 __destruct_n __d(0);
3290 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3291 // Move the falses into the temporary buffer, and the trues to the front of the line
3292 // Update __first to always point to the end of the trues
3293 value_type* __t = __p.first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003294 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003295 __d.__incr((value_type*)0);
3296 ++__t;
3297 _ForwardIterator __i = __first;
3298 while (++__i != __last)
3299 {
3300 if (__pred(*__i))
3301 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003302 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003303 ++__first;
3304 }
3305 else
3306 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003307 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003308 __d.__incr((value_type*)0);
3309 ++__t;
3310 }
3311 }
3312 // All trues now at start of range, all falses in buffer
3313 // Move falses back into range, but don't mess up __first which points to first false
3314 __i = __first;
3315 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003316 *__i = _VSTD::move(*__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003317 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3318 return __first;
3319 }
3320 // Else not enough buffer, do in place
3321 // __len >= 3
3322 _ForwardIterator __m = __first;
3323 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003324 _VSTD::advance(__m, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003325 // recurse on [__first, __m), *__first know to be false
3326 // F?????????????????
3327 // f m l
3328 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3329 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3330 // TTTFFFFF??????????
3331 // f ff m l
3332 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3333 _ForwardIterator __m1 = __m;
3334 _ForwardIterator __second_false = __last;
3335 _Distance __len_half = __len - __len2;
3336 while (__pred(*__m1))
3337 {
3338 if (++__m1 == __last)
3339 goto __second_half_done;
3340 --__len_half;
3341 }
3342 // TTTFFFFFTTTF??????
3343 // f ff m m1 l
3344 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3345__second_half_done:
3346 // TTTFFFFFTTTTTFFFFF
3347 // f ff m sf l
Howard Hinnant0949eed2011-06-30 21:18:19 +00003348 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003349 // TTTTTTTTFFFFFFFFFF
3350 // |
3351}
3352
3353struct __return_temporary_buffer
3354{
3355 template <class _Tp>
Howard Hinnant0949eed2011-06-30 21:18:19 +00003356 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003357};
3358
3359template <class _Predicate, class _ForwardIterator>
3360_ForwardIterator
3361__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3362 forward_iterator_tag)
3363{
3364 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3365 // Either prove all true and return __first or point to first false
3366 while (true)
3367 {
3368 if (__first == __last)
3369 return __first;
3370 if (!__pred(*__first))
3371 break;
3372 ++__first;
3373 }
3374 // We now have a reduced range [__first, __last)
3375 // *__first is known to be false
3376 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3377 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003378 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003379 pair<value_type*, ptrdiff_t> __p(0, 0);
3380 unique_ptr<value_type, __return_temporary_buffer> __h;
3381 if (__len >= __alloc_limit)
3382 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003383 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003384 __h.reset(__p.first);
3385 }
3386 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3387 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3388}
3389
3390template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3391_BidirectionalIterator
3392__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3393 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3394{
3395 // *__first is known to be false
3396 // *__last is known to be true
3397 // __len >= 2
3398 if (__len == 2)
3399 {
3400 swap(*__first, *__last);
3401 return __last;
3402 }
3403 if (__len == 3)
3404 {
3405 _BidirectionalIterator __m = __first;
3406 if (__pred(*++__m))
3407 {
3408 swap(*__first, *__m);
3409 swap(*__m, *__last);
3410 return __last;
3411 }
3412 swap(*__m, *__last);
3413 swap(*__first, *__m);
3414 return __m;
3415 }
3416 if (__len <= __p.second)
3417 { // The buffer is big enough to use
3418 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3419 __destruct_n __d(0);
3420 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3421 // Move the falses into the temporary buffer, and the trues to the front of the line
3422 // Update __first to always point to the end of the trues
3423 value_type* __t = __p.first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003424 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003425 __d.__incr((value_type*)0);
3426 ++__t;
3427 _BidirectionalIterator __i = __first;
3428 while (++__i != __last)
3429 {
3430 if (__pred(*__i))
3431 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003432 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003433 ++__first;
3434 }
3435 else
3436 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003437 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003438 __d.__incr((value_type*)0);
3439 ++__t;
3440 }
3441 }
3442 // move *__last, known to be true
Howard Hinnant0949eed2011-06-30 21:18:19 +00003443 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003444 __i = ++__first;
3445 // All trues now at start of range, all falses in buffer
3446 // Move falses back into range, but don't mess up __first which points to first false
3447 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003448 *__i = _VSTD::move(*__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003449 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3450 return __first;
3451 }
3452 // Else not enough buffer, do in place
3453 // __len >= 4
3454 _BidirectionalIterator __m = __first;
3455 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003456 _VSTD::advance(__m, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003457 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3458 // F????????????????T
3459 // f m l
3460 _BidirectionalIterator __m1 = __m;
3461 _BidirectionalIterator __first_false = __first;
3462 _Distance __len_half = __len2;
3463 while (!__pred(*--__m1))
3464 {
3465 if (__m1 == __first)
3466 goto __first_half_done;
3467 --__len_half;
3468 }
3469 // F???TFFF?????????T
3470 // f m1 m l
3471 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3472 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3473__first_half_done:
3474 // TTTFFFFF?????????T
3475 // f ff m l
3476 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3477 __m1 = __m;
3478 _BidirectionalIterator __second_false = __last;
3479 ++__second_false;
3480 __len_half = __len - __len2;
3481 while (__pred(*__m1))
3482 {
3483 if (++__m1 == __last)
3484 goto __second_half_done;
3485 --__len_half;
3486 }
3487 // TTTFFFFFTTTF?????T
3488 // f ff m m1 l
3489 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3490__second_half_done:
3491 // TTTFFFFFTTTTTFFFFF
3492 // f ff m sf l
Howard Hinnant0949eed2011-06-30 21:18:19 +00003493 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003494 // TTTTTTTTFFFFFFFFFF
3495 // |
3496}
3497
3498template <class _Predicate, class _BidirectionalIterator>
3499_BidirectionalIterator
3500__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3501 bidirectional_iterator_tag)
3502{
3503 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3504 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3505 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3506 // Either prove all true and return __first or point to first false
3507 while (true)
3508 {
3509 if (__first == __last)
3510 return __first;
3511 if (!__pred(*__first))
3512 break;
3513 ++__first;
3514 }
3515 // __first points to first false, everything prior to __first is already set.
3516 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3517 do
3518 {
3519 if (__first == --__last)
3520 return __first;
3521 } while (!__pred(*__last));
3522 // We now have a reduced range [__first, __last]
3523 // *__first is known to be false
3524 // *__last is known to be true
3525 // __len >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003526 difference_type __len = _VSTD::distance(__first, __last) + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003527 pair<value_type*, ptrdiff_t> __p(0, 0);
3528 unique_ptr<value_type, __return_temporary_buffer> __h;
3529 if (__len >= __alloc_limit)
3530 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003531 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003532 __h.reset(__p.first);
3533 }
3534 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3535 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3536}
3537
3538template <class _ForwardIterator, class _Predicate>
3539inline _LIBCPP_INLINE_VISIBILITY
3540_ForwardIterator
3541stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3542{
3543 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3544 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3545}
3546
3547// is_sorted_until
3548
3549template <class _ForwardIterator, class _Compare>
3550_ForwardIterator
3551is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3552{
3553 if (__first != __last)
3554 {
3555 _ForwardIterator __i = __first;
3556 while (++__i != __last)
3557 {
3558 if (__comp(*__i, *__first))
3559 return __i;
3560 __first = __i;
3561 }
3562 }
3563 return __last;
3564}
3565
Howard Hinnant324bb032010-08-22 00:02:43 +00003566template<class _ForwardIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003567inline _LIBCPP_INLINE_VISIBILITY
3568_ForwardIterator
3569is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3570{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003571 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003572}
3573
3574// is_sorted
3575
3576template <class _ForwardIterator, class _Compare>
3577inline _LIBCPP_INLINE_VISIBILITY
3578bool
3579is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3580{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003581 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003582}
3583
Howard Hinnant324bb032010-08-22 00:02:43 +00003584template<class _ForwardIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003585inline _LIBCPP_INLINE_VISIBILITY
3586bool
3587is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3588{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003589 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003590}
3591
3592// sort
3593
3594// stable, 2-3 compares, 0-2 swaps
3595
3596template <class _Compare, class _ForwardIterator>
3597unsigned
3598__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3599{
3600 unsigned __r = 0;
3601 if (!__c(*__y, *__x)) // if x <= y
3602 {
3603 if (!__c(*__z, *__y)) // if y <= z
3604 return __r; // x <= y && y <= z
3605 // x <= y && y > z
3606 swap(*__y, *__z); // x <= z && y < z
3607 __r = 1;
3608 if (__c(*__y, *__x)) // if x > y
3609 {
3610 swap(*__x, *__y); // x < y && y <= z
3611 __r = 2;
3612 }
3613 return __r; // x <= y && y < z
3614 }
3615 if (__c(*__z, *__y)) // x > y, if y > z
3616 {
3617 swap(*__x, *__z); // x < y && y < z
3618 __r = 1;
3619 return __r;
3620 }
3621 swap(*__x, *__y); // x > y && y <= z
3622 __r = 1; // x < y && x <= z
3623 if (__c(*__z, *__y)) // if y > z
3624 {
3625 swap(*__y, *__z); // x <= y && y < z
3626 __r = 2;
3627 }
3628 return __r;
3629} // x <= y && y <= z
3630
3631// stable, 3-6 compares, 0-5 swaps
3632
3633template <class _Compare, class _ForwardIterator>
3634unsigned
3635__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3636 _ForwardIterator __x4, _Compare __c)
3637{
3638 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3639 if (__c(*__x4, *__x3))
3640 {
3641 swap(*__x3, *__x4);
3642 ++__r;
3643 if (__c(*__x3, *__x2))
3644 {
3645 swap(*__x2, *__x3);
3646 ++__r;
3647 if (__c(*__x2, *__x1))
3648 {
3649 swap(*__x1, *__x2);
3650 ++__r;
3651 }
3652 }
3653 }
3654 return __r;
3655}
3656
3657// stable, 4-10 compares, 0-9 swaps
3658
3659template <class _Compare, class _ForwardIterator>
3660unsigned
3661__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3662 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3663{
3664 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3665 if (__c(*__x5, *__x4))
3666 {
3667 swap(*__x4, *__x5);
3668 ++__r;
3669 if (__c(*__x4, *__x3))
3670 {
3671 swap(*__x3, *__x4);
3672 ++__r;
3673 if (__c(*__x3, *__x2))
3674 {
3675 swap(*__x2, *__x3);
3676 ++__r;
3677 if (__c(*__x2, *__x1))
3678 {
3679 swap(*__x1, *__x2);
3680 ++__r;
3681 }
3682 }
3683 }
3684 }
3685 return __r;
3686}
3687
3688// Assumes size > 0
3689template <class _Compare, class _BirdirectionalIterator>
3690void
3691__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3692{
3693 _BirdirectionalIterator __lm1 = __last;
3694 for (--__lm1; __first != __lm1; ++__first)
3695 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003696 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003697 typename add_lvalue_reference<_Compare>::type>
3698 (__first, __last, __comp);
3699 if (__i != __first)
3700 swap(*__first, *__i);
3701 }
3702}
3703
3704template <class _Compare, class _BirdirectionalIterator>
3705void
3706__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3707{
3708 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3709 if (__first != __last)
3710 {
3711 _BirdirectionalIterator __i = __first;
3712 for (++__i; __i != __last; ++__i)
3713 {
3714 _BirdirectionalIterator __j = __i;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003715 value_type __t(_VSTD::move(*__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003716 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003717 *__j = _VSTD::move(*__k);
3718 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003719 }
3720 }
3721}
3722
3723template <class _Compare, class _RandomAccessIterator>
3724void
3725__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3726{
3727 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3728 _RandomAccessIterator __j = __first+2;
3729 __sort3<_Compare>(__first, __first+1, __j, __comp);
3730 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3731 {
3732 if (__comp(*__i, *__j))
3733 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003734 value_type __t(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003735 _RandomAccessIterator __k = __j;
3736 __j = __i;
3737 do
3738 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003739 *__j = _VSTD::move(*__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003740 __j = __k;
3741 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003742 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003743 }
3744 __j = __i;
3745 }
3746}
3747
3748template <class _Compare, class _RandomAccessIterator>
3749bool
3750__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3751{
3752 switch (__last - __first)
3753 {
3754 case 0:
3755 case 1:
3756 return true;
3757 case 2:
3758 if (__comp(*--__last, *__first))
3759 swap(*__first, *__last);
3760 return true;
3761 case 3:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003762 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003763 return true;
3764 case 4:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003765 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003766 return true;
3767 case 5:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003768 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003769 return true;
3770 }
3771 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3772 _RandomAccessIterator __j = __first+2;
3773 __sort3<_Compare>(__first, __first+1, __j, __comp);
3774 const unsigned __limit = 8;
3775 unsigned __count = 0;
3776 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3777 {
3778 if (__comp(*__i, *__j))
3779 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003780 value_type __t(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003781 _RandomAccessIterator __k = __j;
3782 __j = __i;
3783 do
3784 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003785 *__j = _VSTD::move(*__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003786 __j = __k;
3787 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003788 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003789 if (++__count == __limit)
3790 return ++__i == __last;
3791 }
3792 __j = __i;
3793 }
3794 return true;
3795}
3796
3797template <class _Compare, class _BirdirectionalIterator>
3798void
3799__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3800 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3801{
3802 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3803 if (__first1 != __last1)
3804 {
3805 __destruct_n __d(0);
3806 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3807 value_type* __last2 = __first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003808 ::new(__last2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003809 __d.__incr((value_type*)0);
3810 for (++__last2; ++__first1 != __last1; ++__last2)
3811 {
3812 value_type* __j2 = __last2;
3813 value_type* __i2 = __j2;
3814 if (__comp(*__first1, *--__i2))
3815 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003816 ::new(__j2) value_type(_VSTD::move(*__i2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003817 __d.__incr((value_type*)0);
3818 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003819 *__j2 = _VSTD::move(*__i2);
3820 *__j2 = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003821 }
3822 else
3823 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003824 ::new(__j2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003825 __d.__incr((value_type*)0);
3826 }
3827 }
3828 __h.release();
3829 }
3830}
3831
3832template <class _Compare, class _RandomAccessIterator>
3833void
3834__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3835{
3836 // _Compare is known to be a reference type
3837 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3838 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant1468b662010-11-19 22:17:28 +00003839 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3840 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003841 while (true)
3842 {
3843 __restart:
3844 difference_type __len = __last - __first;
3845 switch (__len)
3846 {
3847 case 0:
3848 case 1:
3849 return;
3850 case 2:
3851 if (__comp(*--__last, *__first))
3852 swap(*__first, *__last);
3853 return;
3854 case 3:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003855 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003856 return;
3857 case 4:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003858 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003859 return;
3860 case 5:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003861 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003862 return;
3863 }
3864 if (__len <= __limit)
3865 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003866 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003867 return;
3868 }
3869 // __len > 5
3870 _RandomAccessIterator __m = __first;
3871 _RandomAccessIterator __lm1 = __last;
3872 --__lm1;
3873 unsigned __n_swaps;
3874 {
3875 difference_type __delta;
3876 if (__len >= 1000)
3877 {
3878 __delta = __len/2;
3879 __m += __delta;
3880 __delta /= 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003881 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003882 }
3883 else
3884 {
3885 __delta = __len/2;
3886 __m += __delta;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003887 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003888 }
3889 }
3890 // *__m is median
3891 // partition [__first, __m) < *__m and *__m <= [__m, __last)
3892 // (this inhibits tossing elements equivalent to __m around unnecessarily)
3893 _RandomAccessIterator __i = __first;
3894 _RandomAccessIterator __j = __lm1;
3895 // j points beyond range to be tested, *__m is known to be <= *__lm1
3896 // The search going up is known to be guarded but the search coming down isn't.
3897 // Prime the downward search with a guard.
3898 if (!__comp(*__i, *__m)) // if *__first == *__m
3899 {
3900 // *__first == *__m, *__first doesn't go in first part
3901 // manually guard downward moving __j against __i
3902 while (true)
3903 {
3904 if (__i == --__j)
3905 {
3906 // *__first == *__m, *__m <= all other elements
3907 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
3908 ++__i; // __first + 1
3909 __j = __last;
3910 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
3911 {
3912 while (true)
3913 {
3914 if (__i == __j)
3915 return; // [__first, __last) all equivalent elements
3916 if (__comp(*__first, *__i))
3917 {
3918 swap(*__i, *__j);
3919 ++__n_swaps;
3920 ++__i;
3921 break;
3922 }
3923 ++__i;
3924 }
3925 }
3926 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
3927 if (__i == __j)
3928 return;
3929 while (true)
3930 {
3931 while (!__comp(*__first, *__i))
3932 ++__i;
3933 while (__comp(*__first, *--__j))
3934 ;
3935 if (__i >= __j)
3936 break;
3937 swap(*__i, *__j);
3938 ++__n_swaps;
3939 ++__i;
3940 }
3941 // [__first, __i) == *__first and *__first < [__i, __last)
3942 // The first part is sorted, sort the secod part
Howard Hinnant0949eed2011-06-30 21:18:19 +00003943 // _VSTD::__sort<_Compare>(__i, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003944 __first = __i;
3945 goto __restart;
3946 }
3947 if (__comp(*__j, *__m))
3948 {
3949 swap(*__i, *__j);
3950 ++__n_swaps;
3951 break; // found guard for downward moving __j, now use unguarded partition
3952 }
3953 }
3954 }
3955 // It is known that *__i < *__m
3956 ++__i;
3957 // j points beyond range to be tested, *__m is known to be <= *__lm1
3958 // if not yet partitioned...
3959 if (__i < __j)
3960 {
3961 // known that *(__i - 1) < *__m
3962 // known that __i <= __m
3963 while (true)
3964 {
3965 // __m still guards upward moving __i
3966 while (__comp(*__i, *__m))
3967 ++__i;
3968 // It is now known that a guard exists for downward moving __j
3969 while (!__comp(*--__j, *__m))
3970 ;
3971 if (__i > __j)
3972 break;
3973 swap(*__i, *__j);
3974 ++__n_swaps;
3975 // It is known that __m != __j
3976 // If __m just moved, follow it
3977 if (__m == __i)
3978 __m = __j;
3979 ++__i;
3980 }
3981 }
3982 // [__first, __i) < *__m and *__m <= [__i, __last)
3983 if (__i != __m && __comp(*__m, *__i))
3984 {
3985 swap(*__i, *__m);
3986 ++__n_swaps;
3987 }
3988 // [__first, __i) < *__i and *__i <= [__i+1, __last)
3989 // If we were given a perfect partition, see if insertion sort is quick...
3990 if (__n_swaps == 0)
3991 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003992 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
3993 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003994 {
3995 if (__fs)
3996 return;
3997 __last = __i;
3998 continue;
3999 }
4000 else
4001 {
4002 if (__fs)
4003 {
4004 __first = ++__i;
4005 continue;
4006 }
4007 }
4008 }
4009 // sort smaller range with recursive call and larger with tail recursion elimination
4010 if (__i - __first < __last - __i)
4011 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004012 _VSTD::__sort<_Compare>(__first, __i, __comp);
4013 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004014 __first = ++__i;
4015 }
4016 else
4017 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004018 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4019 // _VSTD::__sort<_Compare>(__first, __i, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004020 __last = __i;
4021 }
4022 }
4023}
4024
4025// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
4026template <class _RandomAccessIterator, class _Compare>
4027inline _LIBCPP_INLINE_VISIBILITY
4028void
4029sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4030{
Howard Hinnant5e571422013-08-23 20:10:18 +00004031#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004032 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4033 __debug_less<_Compare> __c(__comp);
4034 __sort<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004035#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004036 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4037 __sort<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004038#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004039}
4040
4041template <class _RandomAccessIterator>
4042inline _LIBCPP_INLINE_VISIBILITY
4043void
4044sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4045{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004046 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004047}
4048
4049template <class _Tp>
4050inline _LIBCPP_INLINE_VISIBILITY
4051void
4052sort(_Tp** __first, _Tp** __last)
4053{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004054 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004055}
4056
4057template <class _Tp>
4058inline _LIBCPP_INLINE_VISIBILITY
4059void
4060sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4061{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004062 _VSTD::sort(__first.base(), __last.base());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004063}
4064
Howard Hinnant7a563db2011-09-14 18:33:51 +00004065template <class _Tp, class _Compare>
4066inline _LIBCPP_INLINE_VISIBILITY
4067void
4068sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4069{
4070 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4071 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4072}
4073
Howard Hinnante9df0a52013-08-01 18:17:34 +00004074#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +00004075#pragma warning( push )
4076#pragma warning( disable: 4231)
Howard Hinnante9df0a52013-08-01 18:17:34 +00004077#endif // _LIBCPP_MSVC
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004078_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4079_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4080_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4081_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4082_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4083_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4084_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4085_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4086_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4087_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4088_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4089_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>&))
4090_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4091_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4092_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 +00004093
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004094_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4095_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4096_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4097_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4098_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4099_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4100_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4101_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4102_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4103_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4104_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4105_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>&))
4106_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4107_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4108_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 +00004109
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004110_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 +00004111#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +00004112#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34 +00004113#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004114
4115// lower_bound
4116
4117template <class _Compare, class _ForwardIterator, class _Tp>
4118_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004119__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004120{
4121 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004122 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004123 while (__len != 0)
4124 {
4125 difference_type __l2 = __len / 2;
4126 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004127 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004128 if (__comp(*__m, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004129 {
4130 __first = ++__m;
4131 __len -= __l2 + 1;
4132 }
4133 else
4134 __len = __l2;
4135 }
4136 return __first;
4137}
4138
4139template <class _ForwardIterator, class _Tp, class _Compare>
4140inline _LIBCPP_INLINE_VISIBILITY
4141_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004142lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004143{
Howard Hinnant5e571422013-08-23 20:10:18 +00004144#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004145 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4146 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004147 return __lower_bound<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004148#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004149 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004150 return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004151#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004152}
4153
4154template <class _ForwardIterator, class _Tp>
4155inline _LIBCPP_INLINE_VISIBILITY
4156_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004157lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004158{
Howard Hinnant78b68282011-10-22 20:59:45 +00004159 return _VSTD::lower_bound(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004160 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4161}
4162
4163// upper_bound
4164
4165template <class _Compare, class _ForwardIterator, class _Tp>
4166_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004167__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004168{
4169 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004170 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004171 while (__len != 0)
4172 {
4173 difference_type __l2 = __len / 2;
4174 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004175 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004176 if (__comp(__value_, *__m))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004177 __len = __l2;
4178 else
4179 {
4180 __first = ++__m;
4181 __len -= __l2 + 1;
4182 }
4183 }
4184 return __first;
4185}
4186
4187template <class _ForwardIterator, class _Tp, class _Compare>
4188inline _LIBCPP_INLINE_VISIBILITY
4189_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004190upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004191{
Howard Hinnant5e571422013-08-23 20:10:18 +00004192#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004193 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4194 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004195 return __upper_bound<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004196#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004197 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004198 return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004199#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004200}
4201
4202template <class _ForwardIterator, class _Tp>
4203inline _LIBCPP_INLINE_VISIBILITY
4204_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004205upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004206{
Howard Hinnant78b68282011-10-22 20:59:45 +00004207 return _VSTD::upper_bound(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004208 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4209}
4210
4211// equal_range
4212
4213template <class _Compare, class _ForwardIterator, class _Tp>
4214pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004215__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004216{
4217 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004218 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004219 while (__len != 0)
4220 {
4221 difference_type __l2 = __len / 2;
4222 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004223 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004224 if (__comp(*__m, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004225 {
4226 __first = ++__m;
4227 __len -= __l2 + 1;
4228 }
Howard Hinnant78b68282011-10-22 20:59:45 +00004229 else if (__comp(__value_, *__m))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004230 {
4231 __last = __m;
4232 __len = __l2;
4233 }
4234 else
4235 {
4236 _ForwardIterator __mp1 = __m;
4237 return pair<_ForwardIterator, _ForwardIterator>
4238 (
Howard Hinnant78b68282011-10-22 20:59:45 +00004239 __lower_bound<_Compare>(__first, __m, __value_, __comp),
4240 __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004241 );
4242 }
4243 }
4244 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4245}
4246
4247template <class _ForwardIterator, class _Tp, class _Compare>
4248inline _LIBCPP_INLINE_VISIBILITY
4249pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004250equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004251{
Howard Hinnant5e571422013-08-23 20:10:18 +00004252#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004253 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4254 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004255 return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004256#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004257 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004258 return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004259#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004260}
4261
4262template <class _ForwardIterator, class _Tp>
4263inline _LIBCPP_INLINE_VISIBILITY
4264pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004265equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004266{
Howard Hinnant78b68282011-10-22 20:59:45 +00004267 return _VSTD::equal_range(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004268 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4269}
4270
4271// binary_search
4272
4273template <class _Compare, class _ForwardIterator, class _Tp>
4274inline _LIBCPP_INLINE_VISIBILITY
4275bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004276__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004277{
Howard Hinnant78b68282011-10-22 20:59:45 +00004278 __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4279 return __first != __last && !__comp(__value_, *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004280}
4281
4282template <class _ForwardIterator, class _Tp, class _Compare>
4283inline _LIBCPP_INLINE_VISIBILITY
4284bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004285binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004286{
Howard Hinnant5e571422013-08-23 20:10:18 +00004287#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004288 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4289 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004290 return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004291#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004292 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004293 return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004294#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004295}
4296
4297template <class _ForwardIterator, class _Tp>
4298inline _LIBCPP_INLINE_VISIBILITY
4299bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004300binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004301{
Howard Hinnant78b68282011-10-22 20:59:45 +00004302 return _VSTD::binary_search(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004303 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4304}
4305
4306// merge
4307
4308template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4309_OutputIterator
4310__merge(_InputIterator1 __first1, _InputIterator1 __last1,
4311 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4312{
4313 for (; __first1 != __last1; ++__result)
4314 {
4315 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004316 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004317 if (__comp(*__first2, *__first1))
4318 {
4319 *__result = *__first2;
4320 ++__first2;
4321 }
4322 else
4323 {
4324 *__result = *__first1;
4325 ++__first1;
4326 }
4327 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00004328 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004329}
4330
4331template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4332inline _LIBCPP_INLINE_VISIBILITY
4333_OutputIterator
4334merge(_InputIterator1 __first1, _InputIterator1 __last1,
4335 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4336{
Howard Hinnant5e571422013-08-23 20:10:18 +00004337#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004338 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4339 __debug_less<_Compare> __c(__comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004340 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004341#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004342 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004343 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004344#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004345}
4346
4347template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4348inline _LIBCPP_INLINE_VISIBILITY
4349_OutputIterator
4350merge(_InputIterator1 __first1, _InputIterator1 __last1,
4351 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4352{
4353 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4354 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4355 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4356}
4357
4358// inplace_merge
4359
4360template <class _Compare, class _BidirectionalIterator>
4361void
4362__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4363 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4364 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4365 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4366{
4367 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4368 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4369 typedef typename iterator_traits<_BidirectionalIterator>::pointer pointer;
4370 __destruct_n __d(0);
4371 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4372 if (__len1 <= __len2)
4373 {
4374 value_type* __p = __buff;
Eric Fiselierb9919752014-10-27 19:28:20 +00004375 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004376 ::new(__p) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004377 __merge<_Compare>(move_iterator<value_type*>(__buff),
4378 move_iterator<value_type*>(__p),
4379 move_iterator<_BidirectionalIterator>(__middle),
4380 move_iterator<_BidirectionalIterator>(__last),
4381 __first, __comp);
4382 }
4383 else
4384 {
4385 value_type* __p = __buff;
Eric Fiselierb9919752014-10-27 19:28:20 +00004386 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004387 ::new(__p) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004388 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4389 typedef reverse_iterator<value_type*> _Rv;
4390 __merge(move_iterator<_RBi>(_RBi(__middle)), move_iterator<_RBi>(_RBi(__first)),
4391 move_iterator<_Rv>(_Rv(__p)), move_iterator<_Rv>(_Rv(__buff)),
4392 _RBi(__last), __negate<_Compare>(__comp));
4393 }
4394}
4395
4396template <class _Compare, class _BidirectionalIterator>
4397void
4398__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4399 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4400 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4401 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4402{
4403 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4404 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4405 while (true)
4406 {
4407 // if __middle == __last, we're done
4408 if (__len2 == 0)
4409 return;
Marshall Clowe809f4c2015-02-02 16:44:11 +00004410 if (__len1 <= __buff_size || __len2 <= __buff_size)
4411 return __buffered_inplace_merge<_Compare>
4412 (__first, __middle, __last, __comp, __len1, __len2, __buff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004413 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
Eric Fiselierb9919752014-10-27 19:28:20 +00004414 for (; true; ++__first, (void) --__len1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004415 {
4416 if (__len1 == 0)
4417 return;
4418 if (__comp(*__middle, *__first))
4419 break;
4420 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004421 // __first < __middle < __last
4422 // *__first > *__middle
4423 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4424 // all elements in:
4425 // [__first, __m1) <= [__middle, __m2)
4426 // [__middle, __m2) < [__m1, __middle)
4427 // [__m1, __middle) <= [__m2, __last)
4428 // and __m1 or __m2 is in the middle of its range
4429 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4430 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4431 difference_type __len11; // distance(__first, __m1)
4432 difference_type __len21; // distance(__middle, __m2)
4433 // binary search smaller range
4434 if (__len1 < __len2)
4435 { // __len >= 1, __len2 >= 2
4436 __len21 = __len2 / 2;
4437 __m2 = __middle;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004438 _VSTD::advance(__m2, __len21);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004439 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004440 __len11 = _VSTD::distance(__first, __m1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004441 }
4442 else
4443 {
4444 if (__len1 == 1)
4445 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4446 // It is known *__first > *__middle
4447 swap(*__first, *__middle);
4448 return;
4449 }
4450 // __len1 >= 2, __len2 >= 1
4451 __len11 = __len1 / 2;
4452 __m1 = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004453 _VSTD::advance(__m1, __len11);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004454 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004455 __len21 = _VSTD::distance(__middle, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004456 }
4457 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4458 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4459 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4460 // swap middle two partitions
Howard Hinnant0949eed2011-06-30 21:18:19 +00004461 __middle = _VSTD::rotate(__m1, __middle, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004462 // __len12 and __len21 now have swapped meanings
4463 // merge smaller range with recurisve call and larger with tail recursion elimination
4464 if (__len11 + __len21 < __len12 + __len22)
4465 {
4466 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4467// __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4468 __first = __middle;
4469 __middle = __m2;
4470 __len1 = __len12;
4471 __len2 = __len22;
4472 }
4473 else
4474 {
4475 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4476// __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4477 __last = __middle;
4478 __middle = __m1;
4479 __len1 = __len11;
4480 __len2 = __len21;
4481 }
4482 }
4483}
4484
4485template <class _Tp>
4486struct __inplace_merge_switch
4487{
Howard Hinnant1468b662010-11-19 22:17:28 +00004488 static const unsigned value = is_trivially_copy_assignable<_Tp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004489};
4490
4491template <class _BidirectionalIterator, class _Compare>
4492inline _LIBCPP_INLINE_VISIBILITY
4493void
4494inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4495 _Compare __comp)
4496{
4497 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4498 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004499 difference_type __len1 = _VSTD::distance(__first, __middle);
4500 difference_type __len2 = _VSTD::distance(__middle, __last);
4501 difference_type __buf_size = _VSTD::min(__len1, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004502 pair<value_type*, ptrdiff_t> __buf(0, 0);
4503 unique_ptr<value_type, __return_temporary_buffer> __h;
4504 if (__inplace_merge_switch<value_type>::value && __buf_size > 8)
4505 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004506 __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004507 __h.reset(__buf.first);
4508 }
Howard Hinnant5e571422013-08-23 20:10:18 +00004509#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004510 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4511 __debug_less<_Compare> __c(__comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004512 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004513 __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004514#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004515 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004516 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004517 __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004518#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004519}
4520
4521template <class _BidirectionalIterator>
4522inline _LIBCPP_INLINE_VISIBILITY
4523void
4524inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4525{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004526 _VSTD::inplace_merge(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004527 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4528}
4529
4530// stable_sort
4531
4532template <class _Compare, class _InputIterator1, class _InputIterator2>
4533void
4534__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4535 _InputIterator2 __first2, _InputIterator2 __last2,
4536 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4537{
4538 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4539 __destruct_n __d(0);
4540 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4541 for (; true; ++__result)
4542 {
4543 if (__first1 == __last1)
4544 {
4545 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
Howard Hinnant0949eed2011-06-30 21:18:19 +00004546 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004547 __h.release();
4548 return;
4549 }
4550 if (__first2 == __last2)
4551 {
4552 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
Howard Hinnant0949eed2011-06-30 21:18:19 +00004553 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004554 __h.release();
4555 return;
4556 }
4557 if (__comp(*__first2, *__first1))
4558 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004559 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004560 __d.__incr((value_type*)0);
4561 ++__first2;
4562 }
4563 else
4564 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004565 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004566 __d.__incr((value_type*)0);
4567 ++__first1;
4568 }
4569 }
4570}
4571
4572template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4573void
4574__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4575 _InputIterator2 __first2, _InputIterator2 __last2,
4576 _OutputIterator __result, _Compare __comp)
4577{
4578 for (; __first1 != __last1; ++__result)
4579 {
4580 if (__first2 == __last2)
4581 {
4582 for (; __first1 != __last1; ++__first1, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004583 *__result = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004584 return;
4585 }
4586 if (__comp(*__first2, *__first1))
4587 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004588 *__result = _VSTD::move(*__first2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004589 ++__first2;
4590 }
4591 else
4592 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004593 *__result = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004594 ++__first1;
4595 }
4596 }
4597 for (; __first2 != __last2; ++__first2, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004598 *__result = _VSTD::move(*__first2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004599}
4600
4601template <class _Compare, class _RandomAccessIterator>
4602void
4603__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4604 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4605 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4606
4607template <class _Compare, class _RandomAccessIterator>
4608void
4609__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4610 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4611 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4612{
4613 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4614 switch (__len)
4615 {
4616 case 0:
4617 return;
4618 case 1:
Howard Hinnant0949eed2011-06-30 21:18:19 +00004619 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004620 return;
4621 case 2:
4622 __destruct_n __d(0);
4623 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4624 if (__comp(*--__last1, *__first1))
4625 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004626 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004627 __d.__incr((value_type*)0);
4628 ++__first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004629 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004630 }
4631 else
4632 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004633 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004634 __d.__incr((value_type*)0);
4635 ++__first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004636 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004637 }
4638 __h2.release();
4639 return;
4640 }
4641 if (__len <= 8)
4642 {
4643 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4644 return;
4645 }
4646 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4647 _RandomAccessIterator __m = __first1 + __l2;
4648 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4649 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4650 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4651}
4652
4653template <class _Tp>
4654struct __stable_sort_switch
4655{
Howard Hinnant1468b662010-11-19 22:17:28 +00004656 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004657};
4658
4659template <class _Compare, class _RandomAccessIterator>
4660void
4661__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4662 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4663 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4664{
4665 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4666 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4667 switch (__len)
4668 {
4669 case 0:
4670 case 1:
4671 return;
4672 case 2:
4673 if (__comp(*--__last, *__first))
4674 swap(*__first, *__last);
4675 return;
4676 }
4677 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4678 {
4679 __insertion_sort<_Compare>(__first, __last, __comp);
4680 return;
4681 }
4682 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4683 _RandomAccessIterator __m = __first + __l2;
4684 if (__len <= __buff_size)
4685 {
4686 __destruct_n __d(0);
4687 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4688 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4689 __d.__set(__l2, (value_type*)0);
4690 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4691 __d.__set(__len, (value_type*)0);
4692 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4693// __merge<_Compare>(move_iterator<value_type*>(__buff),
4694// move_iterator<value_type*>(__buff + __l2),
4695// move_iterator<_RandomAccessIterator>(__buff + __l2),
4696// move_iterator<_RandomAccessIterator>(__buff + __len),
4697// __first, __comp);
4698 return;
4699 }
4700 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4701 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4702 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4703}
4704
4705template <class _RandomAccessIterator, class _Compare>
4706inline _LIBCPP_INLINE_VISIBILITY
4707void
4708stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4709{
4710 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4711 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4712 difference_type __len = __last - __first;
4713 pair<value_type*, ptrdiff_t> __buf(0, 0);
4714 unique_ptr<value_type, __return_temporary_buffer> __h;
4715 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4716 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004717 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004718 __h.reset(__buf.first);
4719 }
Howard Hinnant5e571422013-08-23 20:10:18 +00004720#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004721 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4722 __debug_less<_Compare> __c(__comp);
4723 __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004724#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004725 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4726 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004727#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004728}
4729
4730template <class _RandomAccessIterator>
4731inline _LIBCPP_INLINE_VISIBILITY
4732void
4733stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4734{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004735 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004736}
4737
4738// is_heap_until
4739
4740template <class _RandomAccessIterator, class _Compare>
4741_RandomAccessIterator
4742is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4743{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004744 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004745 difference_type __len = __last - __first;
4746 difference_type __p = 0;
4747 difference_type __c = 1;
4748 _RandomAccessIterator __pp = __first;
4749 while (__c < __len)
4750 {
4751 _RandomAccessIterator __cp = __first + __c;
4752 if (__comp(*__pp, *__cp))
4753 return __cp;
4754 ++__c;
4755 ++__cp;
4756 if (__c == __len)
4757 return __last;
4758 if (__comp(*__pp, *__cp))
4759 return __cp;
4760 ++__p;
4761 ++__pp;
4762 __c = 2 * __p + 1;
4763 }
4764 return __last;
4765}
4766
Howard Hinnant324bb032010-08-22 00:02:43 +00004767template<class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004768inline _LIBCPP_INLINE_VISIBILITY
4769_RandomAccessIterator
4770is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4771{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004772 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004773}
4774
4775// is_heap
4776
4777template <class _RandomAccessIterator, class _Compare>
4778inline _LIBCPP_INLINE_VISIBILITY
4779bool
4780is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4781{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004782 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004783}
4784
Howard Hinnant324bb032010-08-22 00:02:43 +00004785template<class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004786inline _LIBCPP_INLINE_VISIBILITY
4787bool
4788is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4789{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004790 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004791}
4792
4793// push_heap
4794
4795template <class _Compare, class _RandomAccessIterator>
4796void
David Majnemercb8757a2014-07-22 06:07:09 +00004797__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4798 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004799{
4800 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4801 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4802 if (__len > 1)
4803 {
4804 __len = (__len - 2) / 2;
4805 _RandomAccessIterator __ptr = __first + __len;
4806 if (__comp(*__ptr, *--__last))
4807 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004808 value_type __t(_VSTD::move(*__last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004809 do
4810 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004811 *__last = _VSTD::move(*__ptr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004812 __last = __ptr;
4813 if (__len == 0)
4814 break;
4815 __len = (__len - 1) / 2;
4816 __ptr = __first + __len;
4817 } while (__comp(*__ptr, __t));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004818 *__last = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004819 }
4820 }
4821}
4822
4823template <class _RandomAccessIterator, class _Compare>
4824inline _LIBCPP_INLINE_VISIBILITY
4825void
4826push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4827{
Howard Hinnant5e571422013-08-23 20:10:18 +00004828#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004829 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4830 __debug_less<_Compare> __c(__comp);
David Majnemercb8757a2014-07-22 06:07:09 +00004831 __sift_up<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004832#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004833 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
David Majnemercb8757a2014-07-22 06:07:09 +00004834 __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004835#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004836}
4837
4838template <class _RandomAccessIterator>
4839inline _LIBCPP_INLINE_VISIBILITY
4840void
4841push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4842{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004843 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004844}
4845
4846// pop_heap
4847
4848template <class _Compare, class _RandomAccessIterator>
David Majnemercb8757a2014-07-22 06:07:09 +00004849void
4850__sift_down(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4851 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4852 _RandomAccessIterator __start)
4853{
4854 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4855 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4856 // left-child of __start is at 2 * __start + 1
4857 // right-child of __start is at 2 * __start + 2
4858 difference_type __child = __start - __first;
4859
4860 if (__len < 2 || (__len - 2) / 2 < __child)
4861 return;
4862
4863 __child = 2 * __child + 1;
4864 _RandomAccessIterator __child_i = __first + __child;
4865
4866 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4867 // right-child exists and is greater than left-child
4868 ++__child_i;
4869 ++__child;
4870 }
4871
4872 // check if we are in heap-order
4873 if (__comp(*__child_i, *__start))
4874 // we are, __start is larger than it's largest child
4875 return;
4876
4877 value_type __top(_VSTD::move(*__start));
4878 do
4879 {
4880 // we are not in heap-order, swap the parent with it's largest child
4881 *__start = _VSTD::move(*__child_i);
4882 __start = __child_i;
4883
4884 if ((__len - 2) / 2 < __child)
4885 break;
4886
4887 // recompute the child based off of the updated parent
4888 __child = 2 * __child + 1;
4889 __child_i = __first + __child;
4890
4891 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4892 // right-child exists and is greater than left-child
4893 ++__child_i;
4894 ++__child;
4895 }
4896
4897 // check if we are in heap-order
4898 } while (!__comp(*__child_i, __top));
4899 *__start = _VSTD::move(__top);
4900}
4901
4902template <class _Compare, class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004903inline _LIBCPP_INLINE_VISIBILITY
4904void
4905__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4906 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4907{
4908 if (__len > 1)
4909 {
4910 swap(*__first, *--__last);
David Majnemercb8757a2014-07-22 06:07:09 +00004911 __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004912 }
4913}
4914
4915template <class _RandomAccessIterator, class _Compare>
4916inline _LIBCPP_INLINE_VISIBILITY
4917void
4918pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4919{
Howard Hinnant5e571422013-08-23 20:10:18 +00004920#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004921 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4922 __debug_less<_Compare> __c(__comp);
4923 __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004924#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004925 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4926 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004927#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004928}
4929
4930template <class _RandomAccessIterator>
4931inline _LIBCPP_INLINE_VISIBILITY
4932void
4933pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4934{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004935 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004936}
4937
4938// make_heap
4939
4940template <class _Compare, class _RandomAccessIterator>
4941void
4942__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4943{
4944 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4945 difference_type __n = __last - __first;
4946 if (__n > 1)
4947 {
David Majnemercb8757a2014-07-22 06:07:09 +00004948 // start from the first parent, there is no need to consider children
4949 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
4950 {
4951 __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
4952 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004953 }
4954}
4955
4956template <class _RandomAccessIterator, class _Compare>
4957inline _LIBCPP_INLINE_VISIBILITY
4958void
4959make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4960{
Howard Hinnant5e571422013-08-23 20:10:18 +00004961#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004962 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4963 __debug_less<_Compare> __c(__comp);
4964 __make_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004965#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004966 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4967 __make_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004968#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004969}
4970
4971template <class _RandomAccessIterator>
4972inline _LIBCPP_INLINE_VISIBILITY
4973void
4974make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4975{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004976 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004977}
4978
4979// sort_heap
4980
4981template <class _Compare, class _RandomAccessIterator>
4982void
4983__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4984{
4985 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4986 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
4987 __pop_heap<_Compare>(__first, __last, __comp, __n);
4988}
4989
4990template <class _RandomAccessIterator, class _Compare>
4991inline _LIBCPP_INLINE_VISIBILITY
4992void
4993sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4994{
Howard Hinnant5e571422013-08-23 20:10:18 +00004995#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004996 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4997 __debug_less<_Compare> __c(__comp);
4998 __sort_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004999#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005000 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5001 __sort_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005002#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005003}
5004
5005template <class _RandomAccessIterator>
5006inline _LIBCPP_INLINE_VISIBILITY
5007void
5008sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5009{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005010 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005011}
5012
5013// partial_sort
5014
5015template <class _Compare, class _RandomAccessIterator>
5016void
5017__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5018 _Compare __comp)
5019{
5020 __make_heap<_Compare>(__first, __middle, __comp);
5021 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5022 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5023 {
5024 if (__comp(*__i, *__first))
5025 {
5026 swap(*__i, *__first);
David Majnemercb8757a2014-07-22 06:07:09 +00005027 __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005028 }
5029 }
5030 __sort_heap<_Compare>(__first, __middle, __comp);
5031}
5032
5033template <class _RandomAccessIterator, class _Compare>
5034inline _LIBCPP_INLINE_VISIBILITY
5035void
5036partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5037 _Compare __comp)
5038{
Howard Hinnant5e571422013-08-23 20:10:18 +00005039#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005040 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5041 __debug_less<_Compare> __c(__comp);
5042 __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005043#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005044 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5045 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005046#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005047}
5048
5049template <class _RandomAccessIterator>
5050inline _LIBCPP_INLINE_VISIBILITY
5051void
5052partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5053{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005054 _VSTD::partial_sort(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005055 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5056}
5057
5058// partial_sort_copy
5059
5060template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5061_RandomAccessIterator
5062__partial_sort_copy(_InputIterator __first, _InputIterator __last,
5063 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5064{
5065 _RandomAccessIterator __r = __result_first;
5066 if (__r != __result_last)
5067 {
Eric Fiselierb9919752014-10-27 19:28:20 +00005068 for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005069 *__r = *__first;
5070 __make_heap<_Compare>(__result_first, __r, __comp);
David Majnemercb8757a2014-07-22 06:07:09 +00005071 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005072 for (; __first != __last; ++__first)
5073 if (__comp(*__first, *__result_first))
5074 {
5075 *__result_first = *__first;
David Majnemercb8757a2014-07-22 06:07:09 +00005076 __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005077 }
5078 __sort_heap<_Compare>(__result_first, __r, __comp);
5079 }
5080 return __r;
5081}
5082
5083template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5084inline _LIBCPP_INLINE_VISIBILITY
5085_RandomAccessIterator
5086partial_sort_copy(_InputIterator __first, _InputIterator __last,
5087 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5088{
Howard Hinnant5e571422013-08-23 20:10:18 +00005089#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005090 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5091 __debug_less<_Compare> __c(__comp);
5092 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005093#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005094 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5095 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005096#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005097}
5098
5099template <class _InputIterator, class _RandomAccessIterator>
5100inline _LIBCPP_INLINE_VISIBILITY
5101_RandomAccessIterator
5102partial_sort_copy(_InputIterator __first, _InputIterator __last,
5103 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5104{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005105 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005106 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5107}
5108
5109// nth_element
5110
5111template <class _Compare, class _RandomAccessIterator>
5112void
5113__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5114{
5115 // _Compare is known to be a reference type
5116 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5117 const difference_type __limit = 7;
5118 while (true)
5119 {
5120 __restart:
Howard Hinnant8292d742011-12-29 17:45:35 +00005121 if (__nth == __last)
5122 return;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005123 difference_type __len = __last - __first;
5124 switch (__len)
5125 {
5126 case 0:
5127 case 1:
5128 return;
5129 case 2:
5130 if (__comp(*--__last, *__first))
5131 swap(*__first, *__last);
5132 return;
5133 case 3:
5134 {
5135 _RandomAccessIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00005136 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005137 return;
5138 }
5139 }
5140 if (__len <= __limit)
5141 {
5142 __selection_sort<_Compare>(__first, __last, __comp);
5143 return;
5144 }
5145 // __len > __limit >= 3
5146 _RandomAccessIterator __m = __first + __len/2;
5147 _RandomAccessIterator __lm1 = __last;
Howard Hinnant0949eed2011-06-30 21:18:19 +00005148 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005149 // *__m is median
5150 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5151 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5152 _RandomAccessIterator __i = __first;
5153 _RandomAccessIterator __j = __lm1;
5154 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5155 // The search going up is known to be guarded but the search coming down isn't.
5156 // Prime the downward search with a guard.
5157 if (!__comp(*__i, *__m)) // if *__first == *__m
5158 {
5159 // *__first == *__m, *__first doesn't go in first part
5160 // manually guard downward moving __j against __i
5161 while (true)
5162 {
5163 if (__i == --__j)
5164 {
5165 // *__first == *__m, *__m <= all other elements
5166 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5167 ++__i; // __first + 1
5168 __j = __last;
5169 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5170 {
5171 while (true)
5172 {
5173 if (__i == __j)
5174 return; // [__first, __last) all equivalent elements
5175 if (__comp(*__first, *__i))
5176 {
5177 swap(*__i, *__j);
5178 ++__n_swaps;
5179 ++__i;
5180 break;
5181 }
5182 ++__i;
5183 }
5184 }
5185 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5186 if (__i == __j)
5187 return;
5188 while (true)
5189 {
5190 while (!__comp(*__first, *__i))
5191 ++__i;
5192 while (__comp(*__first, *--__j))
5193 ;
5194 if (__i >= __j)
5195 break;
5196 swap(*__i, *__j);
5197 ++__n_swaps;
5198 ++__i;
5199 }
5200 // [__first, __i) == *__first and *__first < [__i, __last)
5201 // The first part is sorted,
5202 if (__nth < __i)
5203 return;
5204 // __nth_element the secod part
5205 // __nth_element<_Compare>(__i, __nth, __last, __comp);
5206 __first = __i;
5207 goto __restart;
5208 }
5209 if (__comp(*__j, *__m))
5210 {
5211 swap(*__i, *__j);
5212 ++__n_swaps;
5213 break; // found guard for downward moving __j, now use unguarded partition
5214 }
5215 }
5216 }
5217 ++__i;
5218 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5219 // if not yet partitioned...
5220 if (__i < __j)
5221 {
5222 // known that *(__i - 1) < *__m
5223 while (true)
5224 {
5225 // __m still guards upward moving __i
5226 while (__comp(*__i, *__m))
5227 ++__i;
5228 // It is now known that a guard exists for downward moving __j
5229 while (!__comp(*--__j, *__m))
5230 ;
5231 if (__i >= __j)
5232 break;
5233 swap(*__i, *__j);
5234 ++__n_swaps;
5235 // It is known that __m != __j
5236 // If __m just moved, follow it
5237 if (__m == __i)
5238 __m = __j;
5239 ++__i;
5240 }
5241 }
5242 // [__first, __i) < *__m and *__m <= [__i, __last)
5243 if (__i != __m && __comp(*__m, *__i))
5244 {
5245 swap(*__i, *__m);
5246 ++__n_swaps;
5247 }
5248 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5249 if (__nth == __i)
5250 return;
5251 if (__n_swaps == 0)
5252 {
5253 // We were given a perfectly partitioned sequence. Coincidence?
5254 if (__nth < __i)
5255 {
5256 // Check for [__first, __i) already sorted
5257 __j = __m = __first;
5258 while (++__j != __i)
5259 {
5260 if (__comp(*__j, *__m))
5261 // not yet sorted, so sort
5262 goto not_sorted;
5263 __m = __j;
5264 }
5265 // [__first, __i) sorted
5266 return;
5267 }
5268 else
5269 {
5270 // Check for [__i, __last) already sorted
5271 __j = __m = __i;
5272 while (++__j != __last)
5273 {
5274 if (__comp(*__j, *__m))
5275 // not yet sorted, so sort
5276 goto not_sorted;
5277 __m = __j;
5278 }
5279 // [__i, __last) sorted
5280 return;
5281 }
5282 }
5283not_sorted:
5284 // __nth_element on range containing __nth
5285 if (__nth < __i)
5286 {
5287 // __nth_element<_Compare>(__first, __nth, __i, __comp);
5288 __last = __i;
5289 }
5290 else
5291 {
5292 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5293 __first = ++__i;
5294 }
5295 }
5296}
5297
5298template <class _RandomAccessIterator, class _Compare>
5299inline _LIBCPP_INLINE_VISIBILITY
5300void
5301nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5302{
Howard Hinnant5e571422013-08-23 20:10:18 +00005303#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005304 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5305 __debug_less<_Compare> __c(__comp);
5306 __nth_element<_Comp_ref>(__first, __nth, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005307#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005308 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5309 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005310#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005311}
5312
5313template <class _RandomAccessIterator>
5314inline _LIBCPP_INLINE_VISIBILITY
5315void
5316nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5317{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005318 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005319}
5320
5321// includes
5322
5323template <class _Compare, class _InputIterator1, class _InputIterator2>
5324bool
5325__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5326 _Compare __comp)
5327{
5328 for (; __first2 != __last2; ++__first1)
5329 {
5330 if (__first1 == __last1 || __comp(*__first2, *__first1))
5331 return false;
5332 if (!__comp(*__first1, *__first2))
5333 ++__first2;
5334 }
5335 return true;
5336}
5337
5338template <class _InputIterator1, class _InputIterator2, class _Compare>
5339inline _LIBCPP_INLINE_VISIBILITY
5340bool
5341includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5342 _Compare __comp)
5343{
Howard Hinnant5e571422013-08-23 20:10:18 +00005344#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005345 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5346 __debug_less<_Compare> __c(__comp);
5347 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005348#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005349 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5350 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005351#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005352}
5353
5354template <class _InputIterator1, class _InputIterator2>
5355inline _LIBCPP_INLINE_VISIBILITY
5356bool
5357includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5358{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005359 return _VSTD::includes(__first1, __last1, __first2, __last2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005360 __less<typename iterator_traits<_InputIterator1>::value_type,
5361 typename iterator_traits<_InputIterator2>::value_type>());
5362}
5363
5364// set_union
5365
5366template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5367_OutputIterator
5368__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5369 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5370{
5371 for (; __first1 != __last1; ++__result)
5372 {
5373 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005374 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005375 if (__comp(*__first2, *__first1))
5376 {
5377 *__result = *__first2;
5378 ++__first2;
5379 }
5380 else
5381 {
5382 *__result = *__first1;
5383 if (!__comp(*__first1, *__first2))
5384 ++__first2;
5385 ++__first1;
5386 }
5387 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00005388 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005389}
5390
5391template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5392inline _LIBCPP_INLINE_VISIBILITY
5393_OutputIterator
5394set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5395 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5396{
Howard Hinnant5e571422013-08-23 20:10:18 +00005397#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005398 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5399 __debug_less<_Compare> __c(__comp);
5400 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005401#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005402 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5403 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005404#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005405}
5406
5407template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5408inline _LIBCPP_INLINE_VISIBILITY
5409_OutputIterator
5410set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5411 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5412{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005413 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005414 __less<typename iterator_traits<_InputIterator1>::value_type,
5415 typename iterator_traits<_InputIterator2>::value_type>());
5416}
5417
5418// set_intersection
5419
5420template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5421_OutputIterator
5422__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5423 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5424{
5425 while (__first1 != __last1 && __first2 != __last2)
5426 {
5427 if (__comp(*__first1, *__first2))
5428 ++__first1;
5429 else
5430 {
5431 if (!__comp(*__first2, *__first1))
5432 {
5433 *__result = *__first1;
5434 ++__result;
5435 ++__first1;
5436 }
5437 ++__first2;
5438 }
5439 }
5440 return __result;
5441}
5442
5443template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5444inline _LIBCPP_INLINE_VISIBILITY
5445_OutputIterator
5446set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5447 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5448{
Howard Hinnant5e571422013-08-23 20:10:18 +00005449#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005450 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5451 __debug_less<_Compare> __c(__comp);
5452 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005453#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005454 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5455 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005456#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005457}
5458
5459template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5460inline _LIBCPP_INLINE_VISIBILITY
5461_OutputIterator
5462set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5463 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5464{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005465 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005466 __less<typename iterator_traits<_InputIterator1>::value_type,
5467 typename iterator_traits<_InputIterator2>::value_type>());
5468}
5469
5470// set_difference
5471
5472template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5473_OutputIterator
5474__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5475 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5476{
5477 while (__first1 != __last1)
5478 {
5479 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005480 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005481 if (__comp(*__first1, *__first2))
5482 {
5483 *__result = *__first1;
5484 ++__result;
5485 ++__first1;
5486 }
5487 else
5488 {
5489 if (!__comp(*__first2, *__first1))
5490 ++__first1;
5491 ++__first2;
5492 }
5493 }
5494 return __result;
5495}
5496
5497template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5498inline _LIBCPP_INLINE_VISIBILITY
5499_OutputIterator
5500set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5501 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5502{
Howard Hinnant5e571422013-08-23 20:10:18 +00005503#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005504 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5505 __debug_less<_Compare> __c(__comp);
5506 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005507#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005508 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5509 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005510#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005511}
5512
5513template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5514inline _LIBCPP_INLINE_VISIBILITY
5515_OutputIterator
5516set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5517 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5518{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005519 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005520 __less<typename iterator_traits<_InputIterator1>::value_type,
5521 typename iterator_traits<_InputIterator2>::value_type>());
5522}
5523
5524// set_symmetric_difference
5525
5526template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5527_OutputIterator
5528__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5529 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5530{
5531 while (__first1 != __last1)
5532 {
5533 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005534 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005535 if (__comp(*__first1, *__first2))
5536 {
5537 *__result = *__first1;
5538 ++__result;
5539 ++__first1;
5540 }
5541 else
5542 {
5543 if (__comp(*__first2, *__first1))
5544 {
5545 *__result = *__first2;
5546 ++__result;
5547 }
5548 else
5549 ++__first1;
5550 ++__first2;
5551 }
5552 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00005553 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005554}
5555
5556template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5557inline _LIBCPP_INLINE_VISIBILITY
5558_OutputIterator
5559set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5560 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5561{
Howard Hinnant5e571422013-08-23 20:10:18 +00005562#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005563 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5564 __debug_less<_Compare> __c(__comp);
5565 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005566#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005567 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5568 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005569#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005570}
5571
5572template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5573inline _LIBCPP_INLINE_VISIBILITY
5574_OutputIterator
5575set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5576 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5577{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005578 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005579 __less<typename iterator_traits<_InputIterator1>::value_type,
5580 typename iterator_traits<_InputIterator2>::value_type>());
5581}
5582
5583// lexicographical_compare
5584
5585template <class _Compare, class _InputIterator1, class _InputIterator2>
5586bool
5587__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5588 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5589{
Eric Fiselierb9919752014-10-27 19:28:20 +00005590 for (; __first2 != __last2; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005591 {
5592 if (__first1 == __last1 || __comp(*__first1, *__first2))
5593 return true;
5594 if (__comp(*__first2, *__first1))
5595 return false;
5596 }
5597 return false;
5598}
5599
5600template <class _InputIterator1, class _InputIterator2, class _Compare>
5601inline _LIBCPP_INLINE_VISIBILITY
5602bool
5603lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5604 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5605{
Howard Hinnant5e571422013-08-23 20:10:18 +00005606#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005607 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5608 __debug_less<_Compare> __c(__comp);
5609 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005610#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005611 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5612 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005613#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005614}
5615
5616template <class _InputIterator1, class _InputIterator2>
5617inline _LIBCPP_INLINE_VISIBILITY
5618bool
5619lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5620 _InputIterator2 __first2, _InputIterator2 __last2)
5621{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005622 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005623 __less<typename iterator_traits<_InputIterator1>::value_type,
5624 typename iterator_traits<_InputIterator2>::value_type>());
5625}
5626
5627// next_permutation
5628
5629template <class _Compare, class _BidirectionalIterator>
5630bool
5631__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5632{
5633 _BidirectionalIterator __i = __last;
5634 if (__first == __last || __first == --__i)
5635 return false;
5636 while (true)
5637 {
5638 _BidirectionalIterator __ip1 = __i;
5639 if (__comp(*--__i, *__ip1))
5640 {
5641 _BidirectionalIterator __j = __last;
5642 while (!__comp(*__i, *--__j))
5643 ;
5644 swap(*__i, *__j);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005645 _VSTD::reverse(__ip1, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005646 return true;
5647 }
5648 if (__i == __first)
5649 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005650 _VSTD::reverse(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005651 return false;
5652 }
5653 }
5654}
5655
5656template <class _BidirectionalIterator, class _Compare>
5657inline _LIBCPP_INLINE_VISIBILITY
5658bool
5659next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5660{
Howard Hinnant5e571422013-08-23 20:10:18 +00005661#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005662 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5663 __debug_less<_Compare> __c(__comp);
5664 return __next_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005665#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005666 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5667 return __next_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005668#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005669}
5670
5671template <class _BidirectionalIterator>
5672inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005673bool
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005674next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5675{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005676 return _VSTD::next_permutation(__first, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005677 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5678}
5679
5680// prev_permutation
5681
5682template <class _Compare, class _BidirectionalIterator>
5683bool
5684__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5685{
5686 _BidirectionalIterator __i = __last;
5687 if (__first == __last || __first == --__i)
5688 return false;
5689 while (true)
5690 {
5691 _BidirectionalIterator __ip1 = __i;
5692 if (__comp(*__ip1, *--__i))
5693 {
5694 _BidirectionalIterator __j = __last;
5695 while (!__comp(*--__j, *__i))
5696 ;
5697 swap(*__i, *__j);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005698 _VSTD::reverse(__ip1, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005699 return true;
5700 }
5701 if (__i == __first)
5702 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005703 _VSTD::reverse(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005704 return false;
5705 }
5706 }
5707}
5708
5709template <class _BidirectionalIterator, class _Compare>
5710inline _LIBCPP_INLINE_VISIBILITY
5711bool
5712prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5713{
Howard Hinnant5e571422013-08-23 20:10:18 +00005714#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005715 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5716 __debug_less<_Compare> __c(__comp);
5717 return __prev_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005718#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005719 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5720 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005721#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005722}
5723
5724template <class _BidirectionalIterator>
5725inline _LIBCPP_INLINE_VISIBILITY
5726bool
5727prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5728{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005729 return _VSTD::prev_permutation(__first, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005730 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5731}
5732
5733template <class _Tp>
5734inline _LIBCPP_INLINE_VISIBILITY
5735typename enable_if
5736<
5737 is_integral<_Tp>::value,
5738 _Tp
5739>::type
5740__rotate_left(_Tp __t, _Tp __n = 1)
5741{
5742 const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5743 __n &= __bits;
5744 return static_cast<_Tp>((__t << __n) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> (__bits - __n)));
5745}
5746
5747template <class _Tp>
5748inline _LIBCPP_INLINE_VISIBILITY
5749typename enable_if
5750<
5751 is_integral<_Tp>::value,
5752 _Tp
5753>::type
5754__rotate_right(_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 << (__bits - __n)) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> __n));
5759}
5760
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005761_LIBCPP_END_NAMESPACE_STD
5762
5763#endif // _LIBCPP_ALGORITHM