blob: f439396dcc838878d41ac918f2e53411b16d18ab [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- algorithm ---------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_ALGORITHM
12#define _LIBCPP_ALGORITHM
13
14/*
15 algorithm synopsis
16
17#include <initializer_list>
18
19namespace std
20{
21
22template <class InputIterator, class Predicate>
23 bool
24 all_of(InputIterator first, InputIterator last, Predicate pred);
25
26template <class InputIterator, class Predicate>
27 bool
28 any_of(InputIterator first, InputIterator last, Predicate pred);
29
30template <class InputIterator, class Predicate>
31 bool
32 none_of(InputIterator first, InputIterator last, Predicate pred);
33
34template <class InputIterator, class Function>
35 Function
36 for_each(InputIterator first, InputIterator last, Function f);
37
38template <class InputIterator, class T>
39 InputIterator
40 find(InputIterator first, InputIterator last, const T& value);
41
42template <class InputIterator, class Predicate>
43 InputIterator
44 find_if(InputIterator first, InputIterator last, Predicate pred);
45
46template<class InputIterator, class Predicate>
47 InputIterator
48 find_if_not(InputIterator first, InputIterator last, Predicate pred);
49
50template <class ForwardIterator1, class ForwardIterator2>
51 ForwardIterator1
52 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
53 ForwardIterator2 first2, ForwardIterator2 last2);
54
55template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
56 ForwardIterator1
57 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
58 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
59
60template <class ForwardIterator1, class ForwardIterator2>
61 ForwardIterator1
62 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
63 ForwardIterator2 first2, ForwardIterator2 last2);
64
65template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
66 ForwardIterator1
67 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
68 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
69
70template <class ForwardIterator>
71 ForwardIterator
72 adjacent_find(ForwardIterator first, ForwardIterator last);
73
74template <class ForwardIterator, class BinaryPredicate>
75 ForwardIterator
76 adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
77
78template <class InputIterator, class T>
79 typename iterator_traits<InputIterator>::difference_type
80 count(InputIterator first, InputIterator last, const T& value);
81
82template <class InputIterator, class Predicate>
83 typename iterator_traits<InputIterator>::difference_type
84 count_if(InputIterator first, InputIterator last, Predicate pred);
85
86template <class InputIterator1, class InputIterator2>
87 pair<InputIterator1, InputIterator2>
88 mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
89
Marshall Clowb30abdd2013-05-09 21:14:23 +000090template <class InputIterator1, class InputIterator2>
91 pair<InputIterator1, InputIterator2>
92 mismatch(InputIterator1 first1, InputIterator1 last1,
93 InputIterator2 first2, InputIterator2 last2); // **C++14**
94
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000095template <class InputIterator1, class InputIterator2, class BinaryPredicate>
96 pair<InputIterator1, InputIterator2>
97 mismatch(InputIterator1 first1, InputIterator1 last1,
98 InputIterator2 first2, BinaryPredicate pred);
99
Marshall Clowb30abdd2013-05-09 21:14:23 +0000100template <class InputIterator1, class InputIterator2, class BinaryPredicate>
101 pair<InputIterator1, InputIterator2>
102 mismatch(InputIterator1 first1, InputIterator1 last1,
103 InputIterator2 first2, InputIterator2 last2,
104 BinaryPredicate pred); // **C++14**
105
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000106template <class InputIterator1, class InputIterator2>
107 bool
108 equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
109
Marshall Clowb30abdd2013-05-09 21:14:23 +0000110template <class InputIterator1, class InputIterator2>
111 bool
112 equal(InputIterator1 first1, InputIterator1 last1,
113 InputIterator2 first2, InputIterator2 last2); // **C++14**
114
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000115template <class InputIterator1, class InputIterator2, class BinaryPredicate>
116 bool
117 equal(InputIterator1 first1, InputIterator1 last1,
118 InputIterator2 first2, BinaryPredicate pred);
119
Marshall Clowb30abdd2013-05-09 21:14:23 +0000120template <class InputIterator1, class InputIterator2, class BinaryPredicate>
121 bool
122 equal(InputIterator1 first1, InputIterator1 last1,
123 InputIterator2 first2, InputIterator2 last2,
124 BinaryPredicate pred); // **C++14**
125
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000126template<class ForwardIterator1, class ForwardIterator2>
127 bool
128 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
129 ForwardIterator2 first2);
130
Marshall Clowb30abdd2013-05-09 21:14:23 +0000131template<class ForwardIterator1, class ForwardIterator2>
132 bool
133 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
134 ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
135
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000136template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
137 bool
138 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
139 ForwardIterator2 first2, BinaryPredicate pred);
140
Marshall Clowb30abdd2013-05-09 21:14:23 +0000141template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
142 bool
143 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
144 ForwardIterator2 first2, ForwardIterator2 last2,
145 BinaryPredicate pred); // **C++14**
146
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000147template <class ForwardIterator1, class ForwardIterator2>
148 ForwardIterator1
149 search(ForwardIterator1 first1, ForwardIterator1 last1,
150 ForwardIterator2 first2, ForwardIterator2 last2);
151
152template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
153 ForwardIterator1
154 search(ForwardIterator1 first1, ForwardIterator1 last1,
155 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
156
157template <class ForwardIterator, class Size, class T>
158 ForwardIterator
159 search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
160
161template <class ForwardIterator, class Size, class T, class BinaryPredicate>
162 ForwardIterator
163 search_n(ForwardIterator first, ForwardIterator last,
164 Size count, const T& value, BinaryPredicate pred);
165
166template <class InputIterator, class OutputIterator>
167 OutputIterator
168 copy(InputIterator first, InputIterator last, OutputIterator result);
169
170template<class InputIterator, class OutputIterator, class Predicate>
171 OutputIterator
172 copy_if(InputIterator first, InputIterator last,
173 OutputIterator result, Predicate pred);
174
175template<class InputIterator, class Size, class OutputIterator>
176 OutputIterator
177 copy_n(InputIterator first, Size n, OutputIterator result);
178
179template <class BidirectionalIterator1, class BidirectionalIterator2>
180 BidirectionalIterator2
181 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
182 BidirectionalIterator2 result);
183
184template <class ForwardIterator1, class ForwardIterator2>
185 ForwardIterator2
186 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
187
188template <class ForwardIterator1, class ForwardIterator2>
189 void
190 iter_swap(ForwardIterator1 a, ForwardIterator2 b);
191
192template <class InputIterator, class OutputIterator, class UnaryOperation>
193 OutputIterator
194 transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
195
196template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
197 OutputIterator
198 transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
199 OutputIterator result, BinaryOperation binary_op);
200
201template <class ForwardIterator, class T>
202 void
203 replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
204
205template <class ForwardIterator, class Predicate, class T>
206 void
207 replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
208
209template <class InputIterator, class OutputIterator, class T>
210 OutputIterator
211 replace_copy(InputIterator first, InputIterator last, OutputIterator result,
212 const T& old_value, const T& new_value);
213
214template <class InputIterator, class OutputIterator, class Predicate, class T>
215 OutputIterator
216 replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
217
218template <class ForwardIterator, class T>
219 void
220 fill(ForwardIterator first, ForwardIterator last, const T& value);
221
222template <class OutputIterator, class Size, class T>
223 OutputIterator
224 fill_n(OutputIterator first, Size n, const T& value);
225
226template <class ForwardIterator, class Generator>
227 void
228 generate(ForwardIterator first, ForwardIterator last, Generator gen);
229
230template <class OutputIterator, class Size, class Generator>
231 OutputIterator
232 generate_n(OutputIterator first, Size n, Generator gen);
233
234template <class ForwardIterator, class T>
235 ForwardIterator
236 remove(ForwardIterator first, ForwardIterator last, const T& value);
237
238template <class ForwardIterator, class Predicate>
239 ForwardIterator
240 remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
241
242template <class InputIterator, class OutputIterator, class T>
243 OutputIterator
244 remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
245
246template <class InputIterator, class OutputIterator, class Predicate>
247 OutputIterator
248 remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
249
250template <class ForwardIterator>
251 ForwardIterator
252 unique(ForwardIterator first, ForwardIterator last);
253
254template <class ForwardIterator, class BinaryPredicate>
255 ForwardIterator
256 unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
257
258template <class InputIterator, class OutputIterator>
259 OutputIterator
260 unique_copy(InputIterator first, InputIterator last, OutputIterator result);
261
262template <class InputIterator, class OutputIterator, class BinaryPredicate>
263 OutputIterator
264 unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
265
266template <class BidirectionalIterator>
267 void
268 reverse(BidirectionalIterator first, BidirectionalIterator last);
269
270template <class BidirectionalIterator, class OutputIterator>
271 OutputIterator
272 reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
273
274template <class ForwardIterator>
275 ForwardIterator
276 rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
277
278template <class ForwardIterator, class OutputIterator>
279 OutputIterator
280 rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
281
282template <class RandomAccessIterator>
283 void
Marshall Clow3fef95b2014-03-03 06:14:19 +0000284 random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000285
286template <class RandomAccessIterator, class RandomNumberGenerator>
287 void
Marshall Clow3fef95b2014-03-03 06:14:19 +0000288 random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
289 RandomNumberGenerator& rand); // deprecated in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290
Howard Hinnantc3267212010-05-26 17:49:34 +0000291template<class RandomAccessIterator, class UniformRandomNumberGenerator>
292 void shuffle(RandomAccessIterator first, RandomAccessIterator last,
Howard Hinnant278bf2d2010-11-18 01:47:02 +0000293 UniformRandomNumberGenerator&& g);
Howard Hinnantc3267212010-05-26 17:49:34 +0000294
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000295template <class InputIterator, class Predicate>
296 bool
297 is_partitioned(InputIterator first, InputIterator last, Predicate pred);
298
299template <class ForwardIterator, class Predicate>
300 ForwardIterator
301 partition(ForwardIterator first, ForwardIterator last, Predicate pred);
302
303template <class InputIterator, class OutputIterator1,
304 class OutputIterator2, class Predicate>
305 pair<OutputIterator1, OutputIterator2>
306 partition_copy(InputIterator first, InputIterator last,
307 OutputIterator1 out_true, OutputIterator2 out_false,
308 Predicate pred);
309
310template <class ForwardIterator, class Predicate>
311 ForwardIterator
312 stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
313
314template<class ForwardIterator, class Predicate>
315 ForwardIterator
316 partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
317
318template <class ForwardIterator>
319 bool
320 is_sorted(ForwardIterator first, ForwardIterator last);
321
322template <class ForwardIterator, class Compare>
323 bool
324 is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
325
326template<class ForwardIterator>
327 ForwardIterator
328 is_sorted_until(ForwardIterator first, ForwardIterator last);
329
330template <class ForwardIterator, class Compare>
331 ForwardIterator
332 is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
333
334template <class RandomAccessIterator>
335 void
336 sort(RandomAccessIterator first, RandomAccessIterator last);
337
338template <class RandomAccessIterator, class Compare>
339 void
340 sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
341
342template <class RandomAccessIterator>
343 void
344 stable_sort(RandomAccessIterator first, RandomAccessIterator last);
345
346template <class RandomAccessIterator, class Compare>
347 void
348 stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
349
350template <class RandomAccessIterator>
351 void
352 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
353
354template <class RandomAccessIterator, class Compare>
355 void
356 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
357
358template <class InputIterator, class RandomAccessIterator>
359 RandomAccessIterator
360 partial_sort_copy(InputIterator first, InputIterator last,
361 RandomAccessIterator result_first, RandomAccessIterator result_last);
362
363template <class InputIterator, class RandomAccessIterator, class Compare>
364 RandomAccessIterator
365 partial_sort_copy(InputIterator first, InputIterator last,
366 RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
367
368template <class RandomAccessIterator>
369 void
370 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
371
372template <class RandomAccessIterator, class Compare>
373 void
374 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
375
376template <class ForwardIterator, class T>
377 ForwardIterator
378 lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
379
380template <class ForwardIterator, class T, class Compare>
381 ForwardIterator
382 lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
383
384template <class ForwardIterator, class T>
385 ForwardIterator
386 upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
387
388template <class ForwardIterator, class T, class Compare>
389 ForwardIterator
390 upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
391
392template <class ForwardIterator, class T>
393 pair<ForwardIterator, ForwardIterator>
394 equal_range(ForwardIterator first, ForwardIterator last, const T& value);
395
396template <class ForwardIterator, class T, class Compare>
397 pair<ForwardIterator, ForwardIterator>
398 equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
399
400template <class ForwardIterator, class T>
401 bool
402 binary_search(ForwardIterator first, ForwardIterator last, const T& value);
403
404template <class ForwardIterator, class T, class Compare>
405 bool
406 binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
407
408template <class InputIterator1, class InputIterator2, class OutputIterator>
409 OutputIterator
410 merge(InputIterator1 first1, InputIterator1 last1,
411 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
412
413template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
414 OutputIterator
415 merge(InputIterator1 first1, InputIterator1 last1,
416 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
417
418template <class BidirectionalIterator>
419 void
420 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
421
422template <class BidirectionalIterator, class Compare>
423 void
424 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
425
426template <class InputIterator1, class InputIterator2>
427 bool
428 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
429
430template <class InputIterator1, class InputIterator2, class Compare>
431 bool
432 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
433
434template <class InputIterator1, class InputIterator2, class OutputIterator>
435 OutputIterator
436 set_union(InputIterator1 first1, InputIterator1 last1,
437 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
438
439template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
440 OutputIterator
441 set_union(InputIterator1 first1, InputIterator1 last1,
442 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
443
444template <class InputIterator1, class InputIterator2, class OutputIterator>
445 OutputIterator
446 set_intersection(InputIterator1 first1, InputIterator1 last1,
447 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
448
449template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
450 OutputIterator
451 set_intersection(InputIterator1 first1, InputIterator1 last1,
452 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
453
454template <class InputIterator1, class InputIterator2, class OutputIterator>
455 OutputIterator
456 set_difference(InputIterator1 first1, InputIterator1 last1,
457 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
458
459template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
460 OutputIterator
461 set_difference(InputIterator1 first1, InputIterator1 last1,
462 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
463
464template <class InputIterator1, class InputIterator2, class OutputIterator>
465 OutputIterator
466 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
467 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
468
469template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
470 OutputIterator
471 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
472 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
473
474template <class RandomAccessIterator>
475 void
476 push_heap(RandomAccessIterator first, RandomAccessIterator last);
477
478template <class RandomAccessIterator, class Compare>
479 void
480 push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
481
482template <class RandomAccessIterator>
483 void
484 pop_heap(RandomAccessIterator first, RandomAccessIterator last);
485
486template <class RandomAccessIterator, class Compare>
487 void
488 pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
489
490template <class RandomAccessIterator>
491 void
492 make_heap(RandomAccessIterator first, RandomAccessIterator last);
493
494template <class RandomAccessIterator, class Compare>
495 void
496 make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
497
498template <class RandomAccessIterator>
499 void
500 sort_heap(RandomAccessIterator first, RandomAccessIterator last);
501
502template <class RandomAccessIterator, class Compare>
503 void
504 sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
505
Howard Hinnant324bb032010-08-22 00:02:43 +0000506template <class RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000507 bool
Howard Hinnant324bb032010-08-22 00:02:43 +0000508 is_heap(RandomAccessIterator first, RandomAccessiterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000509
Howard Hinnant324bb032010-08-22 00:02:43 +0000510template <class RandomAccessIterator, class Compare>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000511 bool
Howard Hinnant324bb032010-08-22 00:02:43 +0000512 is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000513
Howard Hinnant324bb032010-08-22 00:02:43 +0000514template <class RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000515 RandomAccessIterator
Howard Hinnant324bb032010-08-22 00:02:43 +0000516 is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000517
Howard Hinnant324bb032010-08-22 00:02:43 +0000518template <class RandomAccessIterator, class Compare>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000519 RandomAccessIterator
Howard Hinnant324bb032010-08-22 00:02:43 +0000520 is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000521
Howard Hinnant98e5d972010-08-21 20:10:01 +0000522template <class ForwardIterator>
523 ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +0000524 min_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000525
526template <class ForwardIterator, class Compare>
527 ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +0000528 min_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000529
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000530template <class T>
531 const T&
Marshall Clow9d9463a2014-02-19 16:51:35 +0000532 min(const T& a, const T& b); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000533
534template <class T, class Compare>
535 const T&
Marshall Clow9d9463a2014-02-19 16:51:35 +0000536 min(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000537
Howard Hinnant98e5d972010-08-21 20:10:01 +0000538template<class T>
539 T
Marshall Clow9d9463a2014-02-19 16:51:35 +0000540 min(initializer_list<T> t); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000541
542template<class T, class Compare>
543 T
Marshall Clow9d9463a2014-02-19 16:51:35 +0000544 min(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000545
Marshall Clow3e0808e2016-03-07 22:43:49 +0000546template<class T>
547 constexpr const T& clamp( const T& v, const T& lo, const T& hi ); // C++17
548
549template<class T, class Compare>
550 constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp ); // C++17
551
Howard Hinnant98e5d972010-08-21 20:10:01 +0000552template <class ForwardIterator>
553 ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +0000554 max_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000555
556template <class ForwardIterator, class Compare>
557 ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +0000558 max_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000559
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000560template <class T>
561 const T&
Marshall Clow9d9463a2014-02-19 16:51:35 +0000562 max(const T& a, const T& b); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000563
564template <class T, class Compare>
565 const T&
Marshall Clow9d9463a2014-02-19 16:51:35 +0000566 max(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000567
Howard Hinnant98e5d972010-08-21 20:10:01 +0000568template<class T>
569 T
Marshall Clow9d9463a2014-02-19 16:51:35 +0000570 max(initializer_list<T> t); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000571
Howard Hinnant98e5d972010-08-21 20:10:01 +0000572template<class T, class Compare>
573 T
Marshall Clow9d9463a2014-02-19 16:51:35 +0000574 max(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000575
Howard Hinnant98e5d972010-08-21 20:10:01 +0000576template<class ForwardIterator>
577 pair<ForwardIterator, ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +0000578 minmax_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000579
Howard Hinnant98e5d972010-08-21 20:10:01 +0000580template<class ForwardIterator, class Compare>
581 pair<ForwardIterator, ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +0000582 minmax_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000583
584template<class T>
585 pair<const T&, const T&>
Marshall Clow9d9463a2014-02-19 16:51:35 +0000586 minmax(const T& a, const T& b); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000587
588template<class T, class Compare>
589 pair<const T&, const T&>
Marshall Clow9d9463a2014-02-19 16:51:35 +0000590 minmax(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000591
592template<class T>
593 pair<T, T>
Marshall Clow9d9463a2014-02-19 16:51:35 +0000594 minmax(initializer_list<T> t); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000595
596template<class T, class Compare>
597 pair<T, T>
Marshall Clow9d9463a2014-02-19 16:51:35 +0000598 minmax(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000599
600template <class InputIterator1, class InputIterator2>
601 bool
602 lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
603
604template <class InputIterator1, class InputIterator2, class Compare>
605 bool
606 lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
607 InputIterator2 first2, InputIterator2 last2, Compare comp);
608
609template <class BidirectionalIterator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000610 bool
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000611 next_permutation(BidirectionalIterator first, BidirectionalIterator last);
612
613template <class BidirectionalIterator, class Compare>
614 bool
615 next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
616
617template <class BidirectionalIterator>
618 bool
619 prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
620
621template <class BidirectionalIterator, class Compare>
622 bool
623 prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
624
625} // std
626
627*/
628
629#include <__config>
630#include <initializer_list>
631#include <type_traits>
632#include <cstring>
633#include <utility>
634#include <memory>
635#include <iterator>
Howard Hinnantca8eb832012-07-26 17:09:09 +0000636#include <cstddef>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000637
Howard Hinnant7f764502013-08-14 18:00:20 +0000638#if defined(__IBMCPP__)
639#include "support/ibm/support.h"
640#endif
Howard Hinnantef5aa932013-09-17 01:34:47 +0000641#if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
642#include "support/win32/support.h"
643#endif
Howard Hinnant7f764502013-08-14 18:00:20 +0000644
Howard Hinnant66c6f972011-11-29 16:45:27 +0000645#include <__undef_min_max>
646
Eric Fiselierb9536102014-08-10 23:53:08 +0000647#include <__debug>
648
Howard Hinnant08e17472011-10-17 20:05:10 +0000649#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000650#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000651#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000652
653_LIBCPP_BEGIN_NAMESPACE_STD
654
Marshall Clow9d9463a2014-02-19 16:51:35 +0000655// I'd like to replace these with _VSTD::equal_to<void>, but can't because:
656// * That only works with C++14 and later, and
657// * We haven't included <functional> here.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000658template <class _T1, class _T2 = _T1>
659struct __equal_to
660{
661 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
662 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
663 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
664 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
665};
666
667template <class _T1>
668struct __equal_to<_T1, _T1>
669{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000670 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
671 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000672};
673
674template <class _T1>
675struct __equal_to<const _T1, _T1>
676{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000677 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
678 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000679};
680
681template <class _T1>
682struct __equal_to<_T1, const _T1>
683{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000684 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
685 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000686};
687
688template <class _T1, class _T2 = _T1>
689struct __less
690{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000691 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
692 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
693
694 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
695 bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
696
697 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
698 bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
699
700 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
701 bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000702};
703
704template <class _T1>
705struct __less<_T1, _T1>
706{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000707 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
708 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000709};
710
711template <class _T1>
712struct __less<const _T1, _T1>
713{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000714 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
715 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000716};
717
718template <class _T1>
719struct __less<_T1, const _T1>
720{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000721 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
722 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723};
724
725template <class _Predicate>
726class __negate
727{
728private:
729 _Predicate __p_;
730public:
731 _LIBCPP_INLINE_VISIBILITY __negate() {}
732
733 _LIBCPP_INLINE_VISIBILITY
734 explicit __negate(_Predicate __p) : __p_(__p) {}
735
736 template <class _T1>
737 _LIBCPP_INLINE_VISIBILITY
738 bool operator()(const _T1& __x) {return !__p_(__x);}
739
740 template <class _T1, class _T2>
741 _LIBCPP_INLINE_VISIBILITY
742 bool operator()(const _T1& __x, const _T2& __y) {return !__p_(__x, __y);}
743};
744
Howard Hinnant5e571422013-08-23 20:10:18 +0000745#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000746
747template <class _Compare>
748struct __debug_less
749{
750 _Compare __comp_;
751 __debug_less(_Compare& __c) : __comp_(__c) {}
752 template <class _Tp, class _Up>
753 bool operator()(const _Tp& __x, const _Up& __y)
754 {
755 bool __r = __comp_(__x, __y);
756 if (__r)
Howard Hinnant7a563db2011-09-14 18:33:51 +0000757 _LIBCPP_ASSERT(!__comp_(__y, __x), "Comparator does not induce a strict weak ordering");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000758 return __r;
759 }
760};
761
Howard Hinnant5e571422013-08-23 20:10:18 +0000762#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000763
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000764// Precondition: __x != 0
Howard Hinnantec3773c2011-12-01 20:21:04 +0000765inline _LIBCPP_INLINE_VISIBILITY
766unsigned
767__ctz(unsigned __x)
768{
769 return static_cast<unsigned>(__builtin_ctz(__x));
770}
771
772inline _LIBCPP_INLINE_VISIBILITY
773unsigned long
774__ctz(unsigned long __x)
775{
776 return static_cast<unsigned long>(__builtin_ctzl(__x));
777}
778
779inline _LIBCPP_INLINE_VISIBILITY
780unsigned long long
781__ctz(unsigned long long __x)
782{
783 return static_cast<unsigned long long>(__builtin_ctzll(__x));
784}
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000785
786// Precondition: __x != 0
Howard Hinnantec3773c2011-12-01 20:21:04 +0000787inline _LIBCPP_INLINE_VISIBILITY
788unsigned
789__clz(unsigned __x)
790{
791 return static_cast<unsigned>(__builtin_clz(__x));
792}
793
794inline _LIBCPP_INLINE_VISIBILITY
795unsigned long
796__clz(unsigned long __x)
797{
798 return static_cast<unsigned long>(__builtin_clzl (__x));
799}
800
801inline _LIBCPP_INLINE_VISIBILITY
802unsigned long long
803__clz(unsigned long long __x)
804{
805 return static_cast<unsigned long long>(__builtin_clzll(__x));
806}
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000807
808inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) {return __builtin_popcount (__x);}
809inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) {return __builtin_popcountl (__x);}
810inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {return __builtin_popcountll(__x);}
811
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000812// all_of
813
814template <class _InputIterator, class _Predicate>
815inline _LIBCPP_INLINE_VISIBILITY
816bool
817all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
818{
819 for (; __first != __last; ++__first)
820 if (!__pred(*__first))
821 return false;
822 return true;
823}
824
825// any_of
826
827template <class _InputIterator, class _Predicate>
828inline _LIBCPP_INLINE_VISIBILITY
829bool
830any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
831{
832 for (; __first != __last; ++__first)
833 if (__pred(*__first))
834 return true;
835 return false;
836}
837
838// none_of
839
840template <class _InputIterator, class _Predicate>
841inline _LIBCPP_INLINE_VISIBILITY
842bool
843none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
844{
845 for (; __first != __last; ++__first)
846 if (__pred(*__first))
847 return false;
848 return true;
849}
850
851// for_each
852
853template <class _InputIterator, class _Function>
854inline _LIBCPP_INLINE_VISIBILITY
855_Function
856for_each(_InputIterator __first, _InputIterator __last, _Function __f)
857{
858 for (; __first != __last; ++__first)
859 __f(*__first);
Dimitry Andric89663502015-08-19 06:43:33 +0000860 return _LIBCPP_EXPLICIT_MOVE(__f); // explicitly moved for (emulated) C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000861}
862
863// find
864
865template <class _InputIterator, class _Tp>
866inline _LIBCPP_INLINE_VISIBILITY
867_InputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +0000868find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000869{
870 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +0000871 if (*__first == __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000872 break;
873 return __first;
874}
875
876// find_if
877
878template <class _InputIterator, class _Predicate>
879inline _LIBCPP_INLINE_VISIBILITY
880_InputIterator
881find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
882{
883 for (; __first != __last; ++__first)
884 if (__pred(*__first))
885 break;
886 return __first;
887}
888
889// find_if_not
890
891template<class _InputIterator, class _Predicate>
892inline _LIBCPP_INLINE_VISIBILITY
893_InputIterator
894find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
895{
896 for (; __first != __last; ++__first)
897 if (!__pred(*__first))
898 break;
899 return __first;
900}
901
902// find_end
903
904template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
905_ForwardIterator1
906__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
907 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
908 forward_iterator_tag, forward_iterator_tag)
909{
910 // modeled after search algorithm
911 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
912 if (__first2 == __last2)
913 return __r;
914 while (true)
915 {
916 while (true)
917 {
918 if (__first1 == __last1) // if source exhausted return last correct answer
919 return __r; // (or __last1 if never found)
920 if (__pred(*__first1, *__first2))
921 break;
922 ++__first1;
923 }
924 // *__first1 matches *__first2, now match elements after here
925 _ForwardIterator1 __m1 = __first1;
926 _ForwardIterator2 __m2 = __first2;
927 while (true)
928 {
929 if (++__m2 == __last2)
930 { // Pattern exhaused, record answer and search for another one
931 __r = __first1;
932 ++__first1;
933 break;
934 }
935 if (++__m1 == __last1) // Source exhausted, return last answer
936 return __r;
937 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
938 {
939 ++__first1;
940 break;
941 } // else there is a match, check next elements
942 }
943 }
944}
945
946template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
947_BidirectionalIterator1
948__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
949 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
950 bidirectional_iterator_tag, bidirectional_iterator_tag)
951{
952 // modeled after search algorithm (in reverse)
953 if (__first2 == __last2)
954 return __last1; // Everything matches an empty sequence
955 _BidirectionalIterator1 __l1 = __last1;
956 _BidirectionalIterator2 __l2 = __last2;
957 --__l2;
958 while (true)
959 {
960 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
961 while (true)
962 {
963 if (__first1 == __l1) // return __last1 if no element matches *__first2
964 return __last1;
965 if (__pred(*--__l1, *__l2))
966 break;
967 }
968 // *__l1 matches *__l2, now match elements before here
969 _BidirectionalIterator1 __m1 = __l1;
970 _BidirectionalIterator2 __m2 = __l2;
971 while (true)
972 {
973 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
974 return __m1;
975 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
976 return __last1;
977 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
978 {
979 break;
980 } // else there is a match, check next elements
981 }
982 }
983}
984
985template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow37025e12014-06-10 18:51:55 +0000986_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000987__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
988 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
989 random_access_iterator_tag, random_access_iterator_tag)
990{
991 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
992 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
993 if (__len2 == 0)
994 return __last1;
995 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
996 if (__len1 < __len2)
997 return __last1;
998 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
999 _RandomAccessIterator1 __l1 = __last1;
1000 _RandomAccessIterator2 __l2 = __last2;
1001 --__l2;
1002 while (true)
1003 {
1004 while (true)
1005 {
1006 if (__s == __l1)
1007 return __last1;
1008 if (__pred(*--__l1, *__l2))
1009 break;
1010 }
1011 _RandomAccessIterator1 __m1 = __l1;
1012 _RandomAccessIterator2 __m2 = __l2;
1013 while (true)
1014 {
1015 if (__m2 == __first2)
1016 return __m1;
1017 // no need to check range on __m1 because __s guarantees we have enough source
1018 if (!__pred(*--__m1, *--__m2))
1019 {
1020 break;
1021 }
1022 }
1023 }
1024}
1025
1026template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1027inline _LIBCPP_INLINE_VISIBILITY
1028_ForwardIterator1
1029find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1030 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1031{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001032 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001033 (__first1, __last1, __first2, __last2, __pred,
1034 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1035 typename iterator_traits<_ForwardIterator2>::iterator_category());
1036}
1037
1038template <class _ForwardIterator1, class _ForwardIterator2>
1039inline _LIBCPP_INLINE_VISIBILITY
1040_ForwardIterator1
1041find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1042 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1043{
1044 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1045 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001046 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001047}
1048
1049// find_first_of
1050
1051template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow37025e12014-06-10 18:51:55 +00001052_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1053__find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001054 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1055{
1056 for (; __first1 != __last1; ++__first1)
1057 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1058 if (__pred(*__first1, *__j))
1059 return __first1;
1060 return __last1;
1061}
1062
Marshall Clow37025e12014-06-10 18:51:55 +00001063
1064template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1065inline _LIBCPP_INLINE_VISIBILITY
1066_ForwardIterator1
1067find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1068 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1069{
1070 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1071}
1072
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001073template <class _ForwardIterator1, class _ForwardIterator2>
1074inline _LIBCPP_INLINE_VISIBILITY
1075_ForwardIterator1
1076find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1077 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1078{
1079 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1080 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Marshall Clow37025e12014-06-10 18:51:55 +00001081 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001082}
1083
1084// adjacent_find
1085
1086template <class _ForwardIterator, class _BinaryPredicate>
1087inline _LIBCPP_INLINE_VISIBILITY
1088_ForwardIterator
1089adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1090{
1091 if (__first != __last)
1092 {
1093 _ForwardIterator __i = __first;
1094 while (++__i != __last)
1095 {
1096 if (__pred(*__first, *__i))
1097 return __first;
1098 __first = __i;
1099 }
1100 }
1101 return __last;
1102}
1103
1104template <class _ForwardIterator>
1105inline _LIBCPP_INLINE_VISIBILITY
1106_ForwardIterator
1107adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1108{
1109 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001110 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001111}
1112
1113// count
1114
1115template <class _InputIterator, class _Tp>
1116inline _LIBCPP_INLINE_VISIBILITY
1117typename iterator_traits<_InputIterator>::difference_type
Howard Hinnant78b68282011-10-22 20:59:45 +00001118count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001119{
1120 typename iterator_traits<_InputIterator>::difference_type __r(0);
1121 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +00001122 if (*__first == __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001123 ++__r;
1124 return __r;
1125}
1126
1127// count_if
1128
1129template <class _InputIterator, class _Predicate>
1130inline _LIBCPP_INLINE_VISIBILITY
1131typename iterator_traits<_InputIterator>::difference_type
1132count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1133{
1134 typename iterator_traits<_InputIterator>::difference_type __r(0);
1135 for (; __first != __last; ++__first)
1136 if (__pred(*__first))
1137 ++__r;
1138 return __r;
1139}
1140
1141// mismatch
1142
1143template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1144inline _LIBCPP_INLINE_VISIBILITY
1145pair<_InputIterator1, _InputIterator2>
1146mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1147 _InputIterator2 __first2, _BinaryPredicate __pred)
1148{
Marshall Clowd402a4d2014-09-16 20:40:05 +00001149 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001150 if (!__pred(*__first1, *__first2))
1151 break;
1152 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1153}
1154
1155template <class _InputIterator1, class _InputIterator2>
1156inline _LIBCPP_INLINE_VISIBILITY
1157pair<_InputIterator1, _InputIterator2>
1158mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1159{
1160 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1161 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001162 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001163}
1164
Marshall Clowb30abdd2013-05-09 21:14:23 +00001165#if _LIBCPP_STD_VER > 11
1166template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1167inline _LIBCPP_INLINE_VISIBILITY
1168pair<_InputIterator1, _InputIterator2>
1169mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1170 _InputIterator2 __first2, _InputIterator2 __last2,
1171 _BinaryPredicate __pred)
1172{
Marshall Clowd402a4d2014-09-16 20:40:05 +00001173 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001174 if (!__pred(*__first1, *__first2))
1175 break;
1176 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1177}
1178
1179template <class _InputIterator1, class _InputIterator2>
1180inline _LIBCPP_INLINE_VISIBILITY
1181pair<_InputIterator1, _InputIterator2>
1182mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1183 _InputIterator2 __first2, _InputIterator2 __last2)
1184{
1185 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1186 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1187 return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1188}
1189#endif
1190
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001191// equal
1192
1193template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1194inline _LIBCPP_INLINE_VISIBILITY
1195bool
1196equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1197{
Eric Fiselierb9919752014-10-27 19:28:20 +00001198 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001199 if (!__pred(*__first1, *__first2))
1200 return false;
1201 return true;
1202}
1203
1204template <class _InputIterator1, class _InputIterator2>
1205inline _LIBCPP_INLINE_VISIBILITY
1206bool
1207equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1208{
1209 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1210 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001211 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001212}
1213
Marshall Clowb30abdd2013-05-09 21:14:23 +00001214#if _LIBCPP_STD_VER > 11
1215template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
1216inline _LIBCPP_INLINE_VISIBILITY
1217bool
1218__equal(_InputIterator1 __first1, _InputIterator1 __last1,
1219 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1220 input_iterator_tag, input_iterator_tag )
1221{
Eric Fiselierb9919752014-10-27 19:28:20 +00001222 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001223 if (!__pred(*__first1, *__first2))
1224 return false;
1225 return __first1 == __last1 && __first2 == __last2;
1226}
1227
1228template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1229inline _LIBCPP_INLINE_VISIBILITY
1230bool
1231__equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1232 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1233 random_access_iterator_tag, random_access_iterator_tag )
1234{
1235 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1236 return false;
1237 return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1238 typename add_lvalue_reference<_BinaryPredicate>::type>
1239 (__first1, __last1, __first2, __pred );
1240}
1241
1242template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1243inline _LIBCPP_INLINE_VISIBILITY
1244bool
1245equal(_InputIterator1 __first1, _InputIterator1 __last1,
1246 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1247{
1248 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
1249 (__first1, __last1, __first2, __last2, __pred,
1250 typename iterator_traits<_InputIterator1>::iterator_category(),
1251 typename iterator_traits<_InputIterator2>::iterator_category());
1252}
1253
1254template <class _InputIterator1, class _InputIterator2>
1255inline _LIBCPP_INLINE_VISIBILITY
1256bool
1257equal(_InputIterator1 __first1, _InputIterator1 __last1,
1258 _InputIterator2 __first2, _InputIterator2 __last2)
1259{
1260 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1261 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1262 return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1263 typename iterator_traits<_InputIterator1>::iterator_category(),
1264 typename iterator_traits<_InputIterator2>::iterator_category());
1265}
1266#endif
1267
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001268// is_permutation
1269
1270template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1271bool
1272is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1273 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1274{
1275 // shorten sequences as much as possible by lopping of any equal parts
Eric Fiselierb9919752014-10-27 19:28:20 +00001276 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001277 if (!__pred(*__first1, *__first2))
1278 goto __not_done;
1279 return true;
1280__not_done:
1281 // __first1 != __last1 && *__first1 != *__first2
1282 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001283 _D1 __l1 = _VSTD::distance(__first1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001284 if (__l1 == _D1(1))
1285 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001286 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001287 // For each element in [f1, l1) see if there are the same number of
1288 // equal elements in [f2, l2)
1289 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1290 {
1291 // Have we already counted the number of *__i in [f1, l1)?
1292 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1293 if (__pred(*__j, *__i))
1294 goto __next_iter;
1295 {
1296 // Count number of *__i in [f2, l2)
1297 _D1 __c2 = 0;
1298 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1299 if (__pred(*__i, *__j))
1300 ++__c2;
1301 if (__c2 == 0)
1302 return false;
1303 // Count number of *__i in [__i, l1) (we can start with 1)
1304 _D1 __c1 = 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001305 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001306 if (__pred(*__i, *__j))
1307 ++__c1;
1308 if (__c1 != __c2)
1309 return false;
1310 }
1311__next_iter:;
1312 }
1313 return true;
1314}
1315
1316template<class _ForwardIterator1, class _ForwardIterator2>
1317inline _LIBCPP_INLINE_VISIBILITY
1318bool
1319is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1320 _ForwardIterator2 __first2)
1321{
1322 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1323 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001324 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001325}
1326
Marshall Clowb30abdd2013-05-09 21:14:23 +00001327#if _LIBCPP_STD_VER > 11
1328template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1329bool
1330__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1331 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1332 _BinaryPredicate __pred,
1333 forward_iterator_tag, forward_iterator_tag )
1334{
1335 // shorten sequences as much as possible by lopping of any equal parts
Eric Fiselier62a0e012014-10-27 20:26:25 +00001336 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001337 if (!__pred(*__first1, *__first2))
1338 goto __not_done;
1339 return __first1 == __last1 && __first2 == __last2;
1340__not_done:
1341 // __first1 != __last1 && __first2 != __last2 && *__first1 != *__first2
1342 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1343 _D1 __l1 = _VSTD::distance(__first1, __last1);
1344
1345 typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
Marshall Clow9f8f5242013-05-10 00:16:10 +00001346 _D2 __l2 = _VSTD::distance(__first2, __last2);
Marshall Clowb30abdd2013-05-09 21:14:23 +00001347 if (__l1 != __l2)
1348 return false;
1349
1350 // For each element in [f1, l1) see if there are the same number of
1351 // equal elements in [f2, l2)
1352 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1353 {
1354 // Have we already counted the number of *__i in [f1, l1)?
1355 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1356 if (__pred(*__j, *__i))
1357 goto __next_iter;
1358 {
1359 // Count number of *__i in [f2, l2)
1360 _D1 __c2 = 0;
1361 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1362 if (__pred(*__i, *__j))
1363 ++__c2;
1364 if (__c2 == 0)
1365 return false;
1366 // Count number of *__i in [__i, l1) (we can start with 1)
1367 _D1 __c1 = 1;
1368 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1369 if (__pred(*__i, *__j))
1370 ++__c1;
1371 if (__c1 != __c2)
1372 return false;
1373 }
1374__next_iter:;
1375 }
1376 return true;
1377}
1378
1379template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1380bool
1381__is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
1382 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
1383 _BinaryPredicate __pred,
1384 random_access_iterator_tag, random_access_iterator_tag )
1385{
1386 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1387 return false;
1388 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1389 typename add_lvalue_reference<_BinaryPredicate>::type>
1390 (__first1, __last1, __first2, __pred );
1391}
1392
1393template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1394inline _LIBCPP_INLINE_VISIBILITY
1395bool
1396is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1397 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1398 _BinaryPredicate __pred )
1399{
1400 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1401 (__first1, __last1, __first2, __last2, __pred,
1402 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1403 typename iterator_traits<_ForwardIterator2>::iterator_category());
1404}
1405
1406template<class _ForwardIterator1, class _ForwardIterator2>
1407inline _LIBCPP_INLINE_VISIBILITY
1408bool
1409is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1410 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1411{
1412 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1413 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1414 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1415 __equal_to<__v1, __v2>(),
1416 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1417 typename iterator_traits<_ForwardIterator2>::iterator_category());
1418}
1419#endif
1420
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001421// search
1422
1423template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clowf6d6b512016-03-08 15:12:52 +00001424pair<_ForwardIterator1, _ForwardIterator1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001425__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1426 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
1427 forward_iterator_tag, forward_iterator_tag)
1428{
1429 if (__first2 == __last2)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001430 return make_pair(__first1, __first1); // Everything matches an empty sequence
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001431 while (true)
1432 {
1433 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
1434 while (true)
1435 {
1436 if (__first1 == __last1) // return __last1 if no element matches *__first2
Marshall Clowf6d6b512016-03-08 15:12:52 +00001437 return make_pair(__last1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001438 if (__pred(*__first1, *__first2))
1439 break;
1440 ++__first1;
1441 }
1442 // *__first1 matches *__first2, now match elements after here
1443 _ForwardIterator1 __m1 = __first1;
1444 _ForwardIterator2 __m2 = __first2;
1445 while (true)
1446 {
1447 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001448 return make_pair(__first1, __m1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001449 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
Marshall Clowf6d6b512016-03-08 15:12:52 +00001450 return make_pair(__last1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001451 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
1452 {
1453 ++__first1;
1454 break;
1455 } // else there is a match, check next elements
1456 }
1457 }
1458}
1459
1460template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clowf6d6b512016-03-08 15:12:52 +00001461_LIBCPP_CONSTEXPR_AFTER_CXX11
1462pair<_RandomAccessIterator1, _RandomAccessIterator1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
Marshall Clowf6d6b512016-03-08 15:12:52 +00001464 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001465 random_access_iterator_tag, random_access_iterator_tag)
1466{
Marshall Clowf6d6b512016-03-08 15:12:52 +00001467 typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
1468 typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001469 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
Marshall Clowf6d6b512016-03-08 15:12:52 +00001470 const _D2 __len2 = __last2 - __first2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001471 if (__len2 == 0)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001472 return make_pair(__first1, __first1);
1473 const _D1 __len1 = __last1 - __first1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001474 if (__len1 < __len2)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001475 return make_pair(__last1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001476 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
1477 while (true)
1478 {
1479#if !_LIBCPP_UNROLL_LOOPS
1480 while (true)
1481 {
1482 if (__first1 == __s)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001483 return make_pair(__last1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001484 if (__pred(*__first1, *__first2))
1485 break;
1486 ++__first1;
1487 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001488#else // !_LIBCPP_UNROLL_LOOPS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001489 for (_D1 __loop_unroll = (__s - __first1) / 4; __loop_unroll > 0; --__loop_unroll)
1490 {
1491 if (__pred(*__first1, *__first2))
1492 goto __phase2;
1493 if (__pred(*++__first1, *__first2))
1494 goto __phase2;
1495 if (__pred(*++__first1, *__first2))
1496 goto __phase2;
1497 if (__pred(*++__first1, *__first2))
1498 goto __phase2;
1499 ++__first1;
1500 }
1501 switch (__s - __first1)
1502 {
1503 case 3:
1504 if (__pred(*__first1, *__first2))
1505 break;
1506 ++__first1;
1507 case 2:
1508 if (__pred(*__first1, *__first2))
1509 break;
1510 ++__first1;
1511 case 1:
1512 if (__pred(*__first1, *__first2))
1513 break;
1514 case 0:
Marshall Clowf6d6b512016-03-08 15:12:52 +00001515 return make_pair(__last1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001516 }
1517 __phase2:
Howard Hinnant324bb032010-08-22 00:02:43 +00001518#endif // !_LIBCPP_UNROLL_LOOPS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001519 _RandomAccessIterator1 __m1 = __first1;
1520 _RandomAccessIterator2 __m2 = __first2;
1521#if !_LIBCPP_UNROLL_LOOPS
1522 while (true)
1523 {
1524 if (++__m2 == __last2)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001525 return make_pair(__first1, __first1 + __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001526 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
1527 if (!__pred(*__m1, *__m2))
1528 {
1529 ++__first1;
1530 break;
1531 }
1532 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001533#else // !_LIBCPP_UNROLL_LOOPS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001534 ++__m2;
1535 ++__m1;
1536 for (_D2 __loop_unroll = (__last2 - __m2) / 4; __loop_unroll > 0; --__loop_unroll)
1537 {
1538 if (!__pred(*__m1, *__m2))
1539 goto __continue;
1540 if (!__pred(*++__m1, *++__m2))
1541 goto __continue;
1542 if (!__pred(*++__m1, *++__m2))
1543 goto __continue;
1544 if (!__pred(*++__m1, *++__m2))
1545 goto __continue;
1546 ++__m1;
1547 ++__m2;
1548 }
1549 switch (__last2 - __m2)
1550 {
1551 case 3:
1552 if (!__pred(*__m1, *__m2))
1553 break;
1554 ++__m1;
1555 ++__m2;
1556 case 2:
1557 if (!__pred(*__m1, *__m2))
1558 break;
1559 ++__m1;
1560 ++__m2;
1561 case 1:
1562 if (!__pred(*__m1, *__m2))
1563 break;
1564 case 0:
Marshall Clowf6d6b512016-03-08 15:12:52 +00001565 return make_pair(__first1, __first1 + __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001566 }
1567 __continue:
1568 ++__first1;
Howard Hinnant324bb032010-08-22 00:02:43 +00001569#endif // !_LIBCPP_UNROLL_LOOPS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001570 }
1571}
1572
1573template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1574inline _LIBCPP_INLINE_VISIBILITY
1575_ForwardIterator1
1576search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1577 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1578{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001579 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001580 (__first1, __last1, __first2, __last2, __pred,
Marshall Clowf6d6b512016-03-08 15:12:52 +00001581 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1582 typename iterator_traits<_ForwardIterator2>::iterator_category())
1583 .first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001584}
1585
1586template <class _ForwardIterator1, class _ForwardIterator2>
1587inline _LIBCPP_INLINE_VISIBILITY
1588_ForwardIterator1
1589search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1590 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1591{
Marshall Clowf6d6b512016-03-08 15:12:52 +00001592 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1593 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001594 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001595}
1596
1597// search_n
1598
1599template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
1600_ForwardIterator
1601__search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001602 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001603{
1604 if (__count <= 0)
1605 return __first;
1606 while (true)
1607 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001608 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001609 while (true)
1610 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001611 if (__first == __last) // return __last if no element matches __value_
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001612 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001613 if (__pred(*__first, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001614 break;
1615 ++__first;
1616 }
Howard Hinnant78b68282011-10-22 20:59:45 +00001617 // *__first matches __value_, now match elements after here
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618 _ForwardIterator __m = __first;
1619 _Size __c(0);
1620 while (true)
1621 {
1622 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1623 return __first;
1624 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1625 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001626 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001627 {
1628 __first = __m;
1629 ++__first;
1630 break;
1631 } // else there is a match, check next elements
1632 }
1633 }
1634}
1635
1636template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
1637_RandomAccessIterator
1638__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001639 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640{
1641 if (__count <= 0)
1642 return __first;
1643 _Size __len = static_cast<_Size>(__last - __first);
1644 if (__len < __count)
1645 return __last;
1646 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1647 while (true)
1648 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001649 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001650 while (true)
1651 {
Howard Hinnant128f7bf2013-04-04 15:40:48 +00001652 if (__first >= __s) // return __last if no element matches __value_
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001653 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001654 if (__pred(*__first, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001655 break;
1656 ++__first;
1657 }
Howard Hinnant78b68282011-10-22 20:59:45 +00001658 // *__first matches __value_, now match elements after here
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001659 _RandomAccessIterator __m = __first;
1660 _Size __c(0);
1661 while (true)
1662 {
1663 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1664 return __first;
1665 ++__m; // no need to check range on __m because __s guarantees we have enough source
Howard Hinnant78b68282011-10-22 20:59:45 +00001666 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001667 {
1668 __first = __m;
1669 ++__first;
1670 break;
1671 } // else there is a match, check next elements
1672 }
1673 }
1674}
1675
1676template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
1677inline _LIBCPP_INLINE_VISIBILITY
1678_ForwardIterator
1679search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001680 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001681{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001682 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001683 (__first, __last, __convert_to_integral(__count), __value_, __pred,
1684 typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001685}
1686
1687template <class _ForwardIterator, class _Size, class _Tp>
1688inline _LIBCPP_INLINE_VISIBILITY
1689_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00001690search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001691{
1692 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001693 return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
1694 __value_, __equal_to<__v, _Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001695}
1696
1697// copy
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001698template <class _Iter>
1699inline _LIBCPP_INLINE_VISIBILITY
1700_Iter
1701__unwrap_iter(_Iter __i)
1702{
1703 return __i;
1704}
1705
1706template <class _Tp>
1707inline _LIBCPP_INLINE_VISIBILITY
1708typename enable_if
1709<
Howard Hinnant1468b662010-11-19 22:17:28 +00001710 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001711 _Tp*
1712>::type
1713__unwrap_iter(move_iterator<_Tp*> __i)
1714{
1715 return __i.base();
1716}
1717
Howard Hinnant499cea12013-08-23 17:37:05 +00001718#if _LIBCPP_DEBUG_LEVEL < 2
1719
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001720template <class _Tp>
1721inline _LIBCPP_INLINE_VISIBILITY
1722typename enable_if
1723<
Howard Hinnant1468b662010-11-19 22:17:28 +00001724 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001725 _Tp*
1726>::type
1727__unwrap_iter(__wrap_iter<_Tp*> __i)
1728{
1729 return __i.base();
1730}
1731
Howard Hinnant499cea12013-08-23 17:37:05 +00001732#endif // _LIBCPP_DEBUG_LEVEL < 2
1733
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001734template <class _InputIterator, class _OutputIterator>
1735inline _LIBCPP_INLINE_VISIBILITY
1736_OutputIterator
1737__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1738{
Eric Fiselierb9919752014-10-27 19:28:20 +00001739 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001740 *__result = *__first;
1741 return __result;
1742}
1743
1744template <class _Tp, class _Up>
1745inline _LIBCPP_INLINE_VISIBILITY
1746typename enable_if
1747<
1748 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001749 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001750 _Up*
1751>::type
1752__copy(_Tp* __first, _Tp* __last, _Up* __result)
1753{
1754 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001755 if (__n > 0)
1756 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001757 return __result + __n;
1758}
1759
1760template <class _InputIterator, class _OutputIterator>
1761inline _LIBCPP_INLINE_VISIBILITY
1762_OutputIterator
1763copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1764{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001765 return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001766}
1767
1768// copy_backward
1769
Howard Hinnantb73568d2013-02-06 21:03:39 +00001770template <class _BidirectionalIterator, class _OutputIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001771inline _LIBCPP_INLINE_VISIBILITY
1772_OutputIterator
Howard Hinnantb73568d2013-02-06 21:03:39 +00001773__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001774{
1775 while (__first != __last)
1776 *--__result = *--__last;
1777 return __result;
1778}
1779
1780template <class _Tp, class _Up>
1781inline _LIBCPP_INLINE_VISIBILITY
1782typename enable_if
1783<
1784 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001785 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001786 _Up*
1787>::type
1788__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1789{
1790 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001791 if (__n > 0)
1792 {
1793 __result -= __n;
1794 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1795 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001796 return __result;
1797}
1798
1799template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1800inline _LIBCPP_INLINE_VISIBILITY
1801_BidirectionalIterator2
1802copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1803 _BidirectionalIterator2 __result)
1804{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001805 return _VSTD::__copy_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001806}
1807
1808// copy_if
1809
1810template<class _InputIterator, class _OutputIterator, class _Predicate>
1811inline _LIBCPP_INLINE_VISIBILITY
1812_OutputIterator
1813copy_if(_InputIterator __first, _InputIterator __last,
1814 _OutputIterator __result, _Predicate __pred)
1815{
1816 for (; __first != __last; ++__first)
1817 {
1818 if (__pred(*__first))
1819 {
1820 *__result = *__first;
1821 ++__result;
1822 }
1823 }
1824 return __result;
1825}
1826
1827// copy_n
1828
1829template<class _InputIterator, class _Size, class _OutputIterator>
1830inline _LIBCPP_INLINE_VISIBILITY
1831typename enable_if
1832<
1833 __is_input_iterator<_InputIterator>::value &&
1834 !__is_random_access_iterator<_InputIterator>::value,
1835 _OutputIterator
1836>::type
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001837copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001838{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001839 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1840 _IntegralSize __n = __orig_n;
Howard Hinnant171869e2011-02-27 20:55:39 +00001841 if (__n > 0)
1842 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001843 *__result = *__first;
Howard Hinnant171869e2011-02-27 20:55:39 +00001844 ++__result;
1845 for (--__n; __n > 0; --__n)
1846 {
1847 ++__first;
1848 *__result = *__first;
1849 ++__result;
1850 }
1851 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001852 return __result;
1853}
1854
1855template<class _InputIterator, class _Size, class _OutputIterator>
1856inline _LIBCPP_INLINE_VISIBILITY
1857typename enable_if
1858<
1859 __is_random_access_iterator<_InputIterator>::value,
1860 _OutputIterator
1861>::type
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001862copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001863{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001864 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1865 _IntegralSize __n = __orig_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001866 return _VSTD::copy(__first, __first + __n, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001867}
1868
1869// move
1870
1871template <class _InputIterator, class _OutputIterator>
1872inline _LIBCPP_INLINE_VISIBILITY
1873_OutputIterator
1874__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1875{
Eric Fiselierb9919752014-10-27 19:28:20 +00001876 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001877 *__result = _VSTD::move(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001878 return __result;
1879}
1880
1881template <class _Tp, class _Up>
1882inline _LIBCPP_INLINE_VISIBILITY
1883typename enable_if
1884<
1885 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001886 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001887 _Up*
1888>::type
1889__move(_Tp* __first, _Tp* __last, _Up* __result)
1890{
1891 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001892 if (__n > 0)
1893 _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);
Marshall Clow708b86b2015-06-02 13:52:16 +00001928 if (__n > 0)
1929 {
1930 __result -= __n;
1931 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1932 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001933 return __result;
1934}
1935
1936template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1937inline _LIBCPP_INLINE_VISIBILITY
1938_BidirectionalIterator2
1939move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1940 _BidirectionalIterator2 __result)
1941{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001942 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001943}
1944
1945// iter_swap
1946
Howard Hinnante9b2c2d2011-05-27 15:04:19 +00001947// moved to <type_traits> for better swap / noexcept support
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001948
1949// transform
1950
1951template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
1952inline _LIBCPP_INLINE_VISIBILITY
1953_OutputIterator
1954transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1955{
Eric Fiselierb9919752014-10-27 19:28:20 +00001956 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001957 *__result = __op(*__first);
1958 return __result;
1959}
1960
1961template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
1962inline _LIBCPP_INLINE_VISIBILITY
1963_OutputIterator
1964transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1965 _OutputIterator __result, _BinaryOperation __binary_op)
1966{
Eric Fiselierb9919752014-10-27 19:28:20 +00001967 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001968 *__result = __binary_op(*__first1, *__first2);
1969 return __result;
1970}
1971
1972// replace
1973
1974template <class _ForwardIterator, class _Tp>
1975inline _LIBCPP_INLINE_VISIBILITY
1976void
1977replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1978{
1979 for (; __first != __last; ++__first)
1980 if (*__first == __old_value)
1981 *__first = __new_value;
1982}
1983
1984// replace_if
1985
1986template <class _ForwardIterator, class _Predicate, class _Tp>
1987inline _LIBCPP_INLINE_VISIBILITY
1988void
1989replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1990{
1991 for (; __first != __last; ++__first)
1992 if (__pred(*__first))
1993 *__first = __new_value;
1994}
1995
1996// replace_copy
1997
1998template <class _InputIterator, class _OutputIterator, class _Tp>
1999inline _LIBCPP_INLINE_VISIBILITY
2000_OutputIterator
2001replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2002 const _Tp& __old_value, const _Tp& __new_value)
2003{
Eric Fiselierb9919752014-10-27 19:28:20 +00002004 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002005 if (*__first == __old_value)
2006 *__result = __new_value;
2007 else
2008 *__result = *__first;
2009 return __result;
2010}
2011
2012// replace_copy_if
2013
2014template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
2015inline _LIBCPP_INLINE_VISIBILITY
2016_OutputIterator
2017replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2018 _Predicate __pred, const _Tp& __new_value)
2019{
Eric Fiselierb9919752014-10-27 19:28:20 +00002020 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002021 if (__pred(*__first))
2022 *__result = __new_value;
2023 else
2024 *__result = *__first;
2025 return __result;
2026}
2027
2028// fill_n
2029
2030template <class _OutputIterator, class _Size, class _Tp>
2031inline _LIBCPP_INLINE_VISIBILITY
2032_OutputIterator
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002033__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002034{
Eric Fiselierb9919752014-10-27 19:28:20 +00002035 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnant78b68282011-10-22 20:59:45 +00002036 *__first = __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002037 return __first;
2038}
2039
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002040template <class _Tp, class _Size, class _Up>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002041inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002042typename enable_if
2043<
2044 is_integral<_Tp>::value && sizeof(_Tp) == 1 &&
2045 !is_same<_Tp, bool>::value &&
2046 is_integral<_Up>::value && sizeof(_Up) == 1,
2047 _Tp*
2048>::type
2049__fill_n(_Tp* __first, _Size __n,_Up __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002050{
2051 if (__n > 0)
Howard Hinnant78b68282011-10-22 20:59:45 +00002052 _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002053 return __first + __n;
2054}
2055
2056template <class _OutputIterator, class _Size, class _Tp>
2057inline _LIBCPP_INLINE_VISIBILITY
2058_OutputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002059fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002060{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002061 return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002062}
2063
2064// fill
2065
2066template <class _ForwardIterator, class _Tp>
2067inline _LIBCPP_INLINE_VISIBILITY
2068void
Howard Hinnant78b68282011-10-22 20:59:45 +00002069__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070{
2071 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +00002072 *__first = __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002073}
2074
2075template <class _RandomAccessIterator, class _Tp>
2076inline _LIBCPP_INLINE_VISIBILITY
2077void
Howard Hinnant78b68282011-10-22 20:59:45 +00002078__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002079{
Howard Hinnant78b68282011-10-22 20:59:45 +00002080 _VSTD::fill_n(__first, __last - __first, __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002081}
2082
2083template <class _ForwardIterator, class _Tp>
2084inline _LIBCPP_INLINE_VISIBILITY
2085void
Howard Hinnant78b68282011-10-22 20:59:45 +00002086fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002087{
Howard Hinnant78b68282011-10-22 20:59:45 +00002088 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002089}
2090
2091// generate
2092
2093template <class _ForwardIterator, class _Generator>
2094inline _LIBCPP_INLINE_VISIBILITY
2095void
2096generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2097{
2098 for (; __first != __last; ++__first)
2099 *__first = __gen();
2100}
2101
2102// generate_n
2103
2104template <class _OutputIterator, class _Size, class _Generator>
2105inline _LIBCPP_INLINE_VISIBILITY
2106_OutputIterator
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002107generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002108{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002109 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
2110 _IntegralSize __n = __orig_n;
Eric Fiselierb9919752014-10-27 19:28:20 +00002111 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002112 *__first = __gen();
2113 return __first;
2114}
2115
2116// remove
2117
2118template <class _ForwardIterator, class _Tp>
2119_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002120remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002121{
Howard Hinnant78b68282011-10-22 20:59:45 +00002122 __first = _VSTD::find(__first, __last, __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002123 if (__first != __last)
2124 {
2125 _ForwardIterator __i = __first;
2126 while (++__i != __last)
2127 {
Howard Hinnant78b68282011-10-22 20:59:45 +00002128 if (!(*__i == __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002129 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002130 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002131 ++__first;
2132 }
2133 }
2134 }
2135 return __first;
2136}
2137
2138// remove_if
2139
2140template <class _ForwardIterator, class _Predicate>
2141_ForwardIterator
2142remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2143{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002144 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002145 (__first, __last, __pred);
2146 if (__first != __last)
2147 {
2148 _ForwardIterator __i = __first;
2149 while (++__i != __last)
2150 {
2151 if (!__pred(*__i))
2152 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002153 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002154 ++__first;
2155 }
2156 }
2157 }
2158 return __first;
2159}
2160
2161// remove_copy
2162
2163template <class _InputIterator, class _OutputIterator, class _Tp>
2164inline _LIBCPP_INLINE_VISIBILITY
2165_OutputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002166remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002167{
2168 for (; __first != __last; ++__first)
2169 {
Howard Hinnant78b68282011-10-22 20:59:45 +00002170 if (!(*__first == __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002171 {
2172 *__result = *__first;
2173 ++__result;
2174 }
2175 }
2176 return __result;
2177}
2178
2179// remove_copy_if
2180
2181template <class _InputIterator, class _OutputIterator, class _Predicate>
2182inline _LIBCPP_INLINE_VISIBILITY
2183_OutputIterator
2184remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2185{
2186 for (; __first != __last; ++__first)
2187 {
2188 if (!__pred(*__first))
2189 {
2190 *__result = *__first;
2191 ++__result;
2192 }
2193 }
2194 return __result;
2195}
2196
2197// unique
2198
2199template <class _ForwardIterator, class _BinaryPredicate>
2200_ForwardIterator
2201unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2202{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002203 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002204 (__first, __last, __pred);
2205 if (__first != __last)
2206 {
2207 // ... a a ? ...
2208 // f i
2209 _ForwardIterator __i = __first;
2210 for (++__i; ++__i != __last;)
2211 if (!__pred(*__first, *__i))
Howard Hinnant0949eed2011-06-30 21:18:19 +00002212 *++__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002213 ++__first;
2214 }
2215 return __first;
2216}
2217
2218template <class _ForwardIterator>
2219inline _LIBCPP_INLINE_VISIBILITY
2220_ForwardIterator
2221unique(_ForwardIterator __first, _ForwardIterator __last)
2222{
2223 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002224 return _VSTD::unique(__first, __last, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002225}
2226
2227// unique_copy
2228
2229template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
2230_OutputIterator
2231__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2232 input_iterator_tag, output_iterator_tag)
2233{
2234 if (__first != __last)
2235 {
2236 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2237 *__result = __t;
2238 ++__result;
2239 while (++__first != __last)
2240 {
2241 if (!__pred(__t, *__first))
2242 {
2243 __t = *__first;
2244 *__result = __t;
2245 ++__result;
2246 }
2247 }
2248 }
2249 return __result;
2250}
2251
2252template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
2253_OutputIterator
2254__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2255 forward_iterator_tag, output_iterator_tag)
2256{
2257 if (__first != __last)
2258 {
2259 _ForwardIterator __i = __first;
2260 *__result = *__i;
2261 ++__result;
2262 while (++__first != __last)
2263 {
2264 if (!__pred(*__i, *__first))
2265 {
2266 *__result = *__first;
2267 ++__result;
2268 __i = __first;
2269 }
2270 }
2271 }
2272 return __result;
2273}
2274
2275template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
2276_ForwardIterator
2277__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2278 input_iterator_tag, forward_iterator_tag)
2279{
2280 if (__first != __last)
2281 {
2282 *__result = *__first;
2283 while (++__first != __last)
2284 if (!__pred(*__result, *__first))
2285 *++__result = *__first;
2286 ++__result;
2287 }
2288 return __result;
2289}
2290
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002291template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
2292inline _LIBCPP_INLINE_VISIBILITY
2293_OutputIterator
2294unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2295{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002296 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002297 (__first, __last, __result, __pred,
2298 typename iterator_traits<_InputIterator>::iterator_category(),
2299 typename iterator_traits<_OutputIterator>::iterator_category());
2300}
2301
2302template <class _InputIterator, class _OutputIterator>
2303inline _LIBCPP_INLINE_VISIBILITY
2304_OutputIterator
2305unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2306{
2307 typedef typename iterator_traits<_InputIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002308 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002309}
2310
2311// reverse
2312
2313template <class _BidirectionalIterator>
2314inline _LIBCPP_INLINE_VISIBILITY
2315void
2316__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2317{
2318 while (__first != __last)
2319 {
2320 if (__first == --__last)
2321 break;
Marshall Clowc010bd62015-11-02 21:34:25 +00002322 _VSTD::iter_swap(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002323 ++__first;
2324 }
2325}
2326
2327template <class _RandomAccessIterator>
2328inline _LIBCPP_INLINE_VISIBILITY
2329void
2330__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2331{
2332 if (__first != __last)
2333 for (; __first < --__last; ++__first)
Marshall Clowc010bd62015-11-02 21:34:25 +00002334 _VSTD::iter_swap(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002335}
2336
2337template <class _BidirectionalIterator>
2338inline _LIBCPP_INLINE_VISIBILITY
2339void
2340reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2341{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002342 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002343}
2344
2345// reverse_copy
2346
2347template <class _BidirectionalIterator, class _OutputIterator>
2348inline _LIBCPP_INLINE_VISIBILITY
2349_OutputIterator
2350reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2351{
2352 for (; __first != __last; ++__result)
2353 *__result = *--__last;
2354 return __result;
2355}
2356
2357// rotate
2358
2359template <class _ForwardIterator>
2360_ForwardIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002361__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002362{
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002363 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2364 value_type __tmp = _VSTD::move(*__first);
2365 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2366 *__lm1 = _VSTD::move(__tmp);
2367 return __lm1;
2368}
2369
2370template <class _BidirectionalIterator>
2371_BidirectionalIterator
2372__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2373{
2374 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2375 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2376 value_type __tmp = _VSTD::move(*__lm1);
2377 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2378 *__first = _VSTD::move(__tmp);
2379 return __fp1;
2380}
2381
2382template <class _ForwardIterator>
2383_ForwardIterator
2384__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2385{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002386 _ForwardIterator __i = __middle;
2387 while (true)
2388 {
2389 swap(*__first, *__i);
2390 ++__first;
2391 if (++__i == __last)
2392 break;
2393 if (__first == __middle)
2394 __middle = __i;
2395 }
2396 _ForwardIterator __r = __first;
2397 if (__first != __middle)
2398 {
2399 __i = __middle;
2400 while (true)
2401 {
2402 swap(*__first, *__i);
2403 ++__first;
2404 if (++__i == __last)
2405 {
2406 if (__first == __middle)
2407 break;
2408 __i = __middle;
2409 }
2410 else if (__first == __middle)
2411 __middle = __i;
2412 }
2413 }
2414 return __r;
2415}
2416
2417template<typename _Integral>
2418inline _LIBCPP_INLINE_VISIBILITY
2419_Integral
2420__gcd(_Integral __x, _Integral __y)
2421{
2422 do
2423 {
2424 _Integral __t = __x % __y;
2425 __x = __y;
2426 __y = __t;
2427 } while (__y);
2428 return __x;
2429}
2430
2431template<typename _RandomAccessIterator>
2432_RandomAccessIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002433__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002434{
2435 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2436 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant324bb032010-08-22 00:02:43 +00002437
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002438 const difference_type __m1 = __middle - __first;
2439 const difference_type __m2 = __last - __middle;
2440 if (__m1 == __m2)
2441 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002442 _VSTD::swap_ranges(__first, __middle, __middle);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002443 return __middle;
2444 }
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002445 const difference_type __g = _VSTD::__gcd(__m1, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002446 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2447 {
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002448 value_type __t(_VSTD::move(*--__p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002449 _RandomAccessIterator __p1 = __p;
2450 _RandomAccessIterator __p2 = __p1 + __m1;
2451 do
2452 {
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002453 *__p1 = _VSTD::move(*__p2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002454 __p1 = __p2;
2455 const difference_type __d = __last - __p2;
2456 if (__m1 < __d)
2457 __p2 += __m1;
2458 else
2459 __p2 = __first + (__m1 - __d);
2460 } while (__p2 != __p);
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002461 *__p1 = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002462 }
2463 return __first + __m2;
2464}
2465
2466template <class _ForwardIterator>
2467inline _LIBCPP_INLINE_VISIBILITY
2468_ForwardIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002469__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2470 _VSTD::forward_iterator_tag)
2471{
2472 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2473 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2474 {
2475 if (_VSTD::next(__first) == __middle)
2476 return _VSTD::__rotate_left(__first, __last);
2477 }
2478 return _VSTD::__rotate_forward(__first, __middle, __last);
2479}
2480
2481template <class _BidirectionalIterator>
2482inline _LIBCPP_INLINE_VISIBILITY
2483_BidirectionalIterator
2484__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2485 _VSTD::bidirectional_iterator_tag)
2486{
2487 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2488 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2489 {
2490 if (_VSTD::next(__first) == __middle)
2491 return _VSTD::__rotate_left(__first, __last);
2492 if (_VSTD::next(__middle) == __last)
2493 return _VSTD::__rotate_right(__first, __last);
2494 }
2495 return _VSTD::__rotate_forward(__first, __middle, __last);
2496}
2497
2498template <class _RandomAccessIterator>
2499inline _LIBCPP_INLINE_VISIBILITY
2500_RandomAccessIterator
2501__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2502 _VSTD::random_access_iterator_tag)
2503{
2504 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2505 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2506 {
2507 if (_VSTD::next(__first) == __middle)
2508 return _VSTD::__rotate_left(__first, __last);
2509 if (_VSTD::next(__middle) == __last)
2510 return _VSTD::__rotate_right(__first, __last);
2511 return _VSTD::__rotate_gcd(__first, __middle, __last);
2512 }
2513 return _VSTD::__rotate_forward(__first, __middle, __last);
2514}
2515
2516template <class _ForwardIterator>
2517inline _LIBCPP_INLINE_VISIBILITY
2518_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002519rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2520{
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002521 if (__first == __middle)
2522 return __last;
2523 if (__middle == __last)
2524 return __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002525 return _VSTD::__rotate(__first, __middle, __last,
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002526 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002527}
2528
2529// rotate_copy
2530
2531template <class _ForwardIterator, class _OutputIterator>
2532inline _LIBCPP_INLINE_VISIBILITY
2533_OutputIterator
2534rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2535{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002536 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002537}
2538
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002539// min_element
2540
2541template <class _ForwardIterator, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002542inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002543_ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +00002544min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002545{
2546 if (__first != __last)
2547 {
2548 _ForwardIterator __i = __first;
2549 while (++__i != __last)
2550 if (__comp(*__i, *__first))
2551 __first = __i;
2552 }
2553 return __first;
2554}
2555
2556template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002557inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002558_ForwardIterator
2559min_element(_ForwardIterator __first, _ForwardIterator __last)
2560{
Marshall Clow928735a2015-05-10 13:53:31 +00002561 return _VSTD::min_element(__first, __last,
Howard Hinnant98e5d972010-08-21 20:10:01 +00002562 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2563}
2564
2565// min
2566
2567template <class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002568inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002569const _Tp&
2570min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2571{
2572 return __comp(__b, __a) ? __b : __a;
2573}
2574
2575template <class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002576inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002577const _Tp&
2578min(const _Tp& __a, const _Tp& __b)
2579{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002580 return _VSTD::min(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002581}
2582
Howard Hinnante3e32912011-08-12 21:56:02 +00002583#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2584
Howard Hinnant98e5d972010-08-21 20:10:01 +00002585template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002586inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002587_Tp
2588min(initializer_list<_Tp> __t, _Compare __comp)
2589{
Marshall Clow928735a2015-05-10 13:53:31 +00002590 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002591}
2592
2593template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002594inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002595_Tp
2596min(initializer_list<_Tp> __t)
2597{
Marshall Clow928735a2015-05-10 13:53:31 +00002598 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002599}
2600
Howard Hinnante3e32912011-08-12 21:56:02 +00002601#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2602
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002603// max_element
2604
2605template <class _ForwardIterator, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002606inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002607_ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +00002608max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002609{
2610 if (__first != __last)
2611 {
2612 _ForwardIterator __i = __first;
2613 while (++__i != __last)
2614 if (__comp(*__first, *__i))
2615 __first = __i;
2616 }
2617 return __first;
2618}
2619
Marshall Clow9d9463a2014-02-19 16:51:35 +00002620
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002621template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002622inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002623_ForwardIterator
2624max_element(_ForwardIterator __first, _ForwardIterator __last)
2625{
Marshall Clow928735a2015-05-10 13:53:31 +00002626 return _VSTD::max_element(__first, __last,
Howard Hinnant98e5d972010-08-21 20:10:01 +00002627 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2628}
2629
2630// max
2631
2632template <class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002633inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002634const _Tp&
2635max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2636{
2637 return __comp(__a, __b) ? __b : __a;
2638}
2639
2640template <class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002641inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002642const _Tp&
2643max(const _Tp& __a, const _Tp& __b)
2644{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002645 return _VSTD::max(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002646}
2647
Howard Hinnante3e32912011-08-12 21:56:02 +00002648#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2649
Howard Hinnant98e5d972010-08-21 20:10:01 +00002650template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002651inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002652_Tp
2653max(initializer_list<_Tp> __t, _Compare __comp)
2654{
Marshall Clow928735a2015-05-10 13:53:31 +00002655 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002656}
2657
2658template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002659inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002660_Tp
2661max(initializer_list<_Tp> __t)
2662{
Marshall Clow928735a2015-05-10 13:53:31 +00002663 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002664}
2665
Howard Hinnante3e32912011-08-12 21:56:02 +00002666#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2667
Marshall Clow3e0808e2016-03-07 22:43:49 +00002668#if _LIBCPP_STD_VER > 14
2669// clamp
2670template<class _Tp, class _Compare>
2671inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2672const _Tp&
2673clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
2674{
2675 _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
2676 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
2677
2678}
2679
2680template<class _Tp>
2681inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2682const _Tp&
2683clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
2684{
2685 return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
2686}
2687#endif
2688
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002689// minmax_element
2690
2691template <class _ForwardIterator, class _Compare>
Marshall Clow928735a2015-05-10 13:53:31 +00002692_LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002693std::pair<_ForwardIterator, _ForwardIterator>
2694minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2695{
2696 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2697 if (__first != __last)
2698 {
2699 if (++__first != __last)
2700 {
2701 if (__comp(*__first, *__result.first))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002702 __result.first = __first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002703 else
2704 __result.second = __first;
2705 while (++__first != __last)
2706 {
2707 _ForwardIterator __i = __first;
2708 if (++__first == __last)
2709 {
2710 if (__comp(*__i, *__result.first))
2711 __result.first = __i;
2712 else if (!__comp(*__i, *__result.second))
2713 __result.second = __i;
2714 break;
2715 }
2716 else
2717 {
2718 if (__comp(*__first, *__i))
2719 {
2720 if (__comp(*__first, *__result.first))
2721 __result.first = __first;
2722 if (!__comp(*__i, *__result.second))
2723 __result.second = __i;
2724 }
2725 else
2726 {
2727 if (__comp(*__i, *__result.first))
2728 __result.first = __i;
2729 if (!__comp(*__first, *__result.second))
2730 __result.second = __first;
2731 }
2732 }
2733 }
2734 }
2735 }
2736 return __result;
2737}
2738
2739template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002740inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002741std::pair<_ForwardIterator, _ForwardIterator>
2742minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2743{
Marshall Clow9d9463a2014-02-19 16:51:35 +00002744 return _VSTD::minmax_element(__first, __last,
2745 __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002746}
2747
Howard Hinnant98e5d972010-08-21 20:10:01 +00002748// minmax
2749
2750template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002751inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002752pair<const _Tp&, const _Tp&>
2753minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2754{
2755 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2756 pair<const _Tp&, const _Tp&>(__a, __b);
2757}
2758
2759template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002760inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002761pair<const _Tp&, const _Tp&>
2762minmax(const _Tp& __a, const _Tp& __b)
2763{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002764 return _VSTD::minmax(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002765}
2766
Howard Hinnante3e32912011-08-12 21:56:02 +00002767#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2768
Howard Hinnant98e5d972010-08-21 20:10:01 +00002769template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002770inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002771pair<_Tp, _Tp>
2772minmax(initializer_list<_Tp> __t, _Compare __comp)
2773{
Marshall Clow9d9463a2014-02-19 16:51:35 +00002774 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2775 _Iter __first = __t.begin();
2776 _Iter __last = __t.end();
Marshall Clow3024f862015-02-11 15:41:34 +00002777 std::pair<_Tp, _Tp> __result(*__first, *__first);
Marshall Clow9d9463a2014-02-19 16:51:35 +00002778
2779 ++__first;
2780 if (__t.size() % 2 == 0)
2781 {
2782 if (__comp(*__first, __result.first))
2783 __result.first = *__first;
2784 else
2785 __result.second = *__first;
2786 ++__first;
2787 }
2788
2789 while (__first != __last)
2790 {
2791 _Tp __prev = *__first++;
Marshall Clow3024f862015-02-11 15:41:34 +00002792 if (__comp(*__first, __prev)) {
2793 if ( __comp(*__first, __result.first)) __result.first = *__first;
2794 if (!__comp(__prev, __result.second)) __result.second = __prev;
Marshall Clow9d9463a2014-02-19 16:51:35 +00002795 }
2796 else {
Marshall Clow3024f862015-02-11 15:41:34 +00002797 if ( __comp(__prev, __result.first)) __result.first = __prev;
2798 if (!__comp(*__first, __result.second)) __result.second = *__first;
Marshall Clow9d9463a2014-02-19 16:51:35 +00002799 }
2800
2801 __first++;
2802 }
2803 return __result;
2804}
2805
2806template<class _Tp>
2807inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2808pair<_Tp, _Tp>
2809minmax(initializer_list<_Tp> __t)
2810{
2811 return _VSTD::minmax(__t, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002812}
2813
Howard Hinnante3e32912011-08-12 21:56:02 +00002814#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2815
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002816// random_shuffle
2817
Howard Hinnantc3267212010-05-26 17:49:34 +00002818// __independent_bits_engine
2819
Howard Hinnant99968442011-11-29 18:15:50 +00002820template <unsigned long long _Xp, size_t _Rp>
Howard Hinnantc3267212010-05-26 17:49:34 +00002821struct __log2_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002822{
Howard Hinnant99968442011-11-29 18:15:50 +00002823 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2824 : __log2_imp<_Xp, _Rp - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002825};
2826
Howard Hinnant99968442011-11-29 18:15:50 +00002827template <unsigned long long _Xp>
2828struct __log2_imp<_Xp, 0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002829{
Howard Hinnantc3267212010-05-26 17:49:34 +00002830 static const size_t value = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002831};
2832
Howard Hinnant99968442011-11-29 18:15:50 +00002833template <size_t _Rp>
2834struct __log2_imp<0, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002835{
Howard Hinnant99968442011-11-29 18:15:50 +00002836 static const size_t value = _Rp + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002837};
2838
Howard Hinnant99968442011-11-29 18:15:50 +00002839template <class _UI, _UI _Xp>
Howard Hinnantc3267212010-05-26 17:49:34 +00002840struct __log2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002841{
Howard Hinnant99968442011-11-29 18:15:50 +00002842 static const size_t value = __log2_imp<_Xp,
Howard Hinnantc3267212010-05-26 17:49:34 +00002843 sizeof(_UI) * __CHAR_BIT__ - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002844};
2845
Howard Hinnantc3267212010-05-26 17:49:34 +00002846template<class _Engine, class _UIntType>
2847class __independent_bits_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002848{
Howard Hinnantc3267212010-05-26 17:49:34 +00002849public:
2850 // types
2851 typedef _UIntType result_type;
2852
2853private:
2854 typedef typename _Engine::result_type _Engine_result_type;
2855 typedef typename conditional
2856 <
2857 sizeof(_Engine_result_type) <= sizeof(result_type),
2858 result_type,
2859 _Engine_result_type
2860 >::type _Working_result_type;
2861
2862 _Engine& __e_;
2863 size_t __w_;
2864 size_t __w0_;
2865 size_t __n_;
2866 size_t __n0_;
2867 _Working_result_type __y0_;
2868 _Working_result_type __y1_;
2869 _Engine_result_type __mask0_;
2870 _Engine_result_type __mask1_;
2871
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002872#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnant99968442011-11-29 18:15:50 +00002873 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002874 + _Working_result_type(1);
2875#else
2876 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2877 + _Working_result_type(1);
2878#endif
2879 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2880 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2881 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
Howard Hinnantc3267212010-05-26 17:49:34 +00002882
2883public:
2884 // constructors and seeding functions
2885 __independent_bits_engine(_Engine& __e, size_t __w);
2886
2887 // generating functions
Howard Hinnant99968442011-11-29 18:15:50 +00002888 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantc3267212010-05-26 17:49:34 +00002889
2890private:
2891 result_type __eval(false_type);
2892 result_type __eval(true_type);
2893};
2894
2895template<class _Engine, class _UIntType>
2896__independent_bits_engine<_Engine, _UIntType>
2897 ::__independent_bits_engine(_Engine& __e, size_t __w)
2898 : __e_(__e),
2899 __w_(__w)
2900{
2901 __n_ = __w_ / __m + (__w_ % __m != 0);
2902 __w0_ = __w_ / __n_;
Howard Hinnant99968442011-11-29 18:15:50 +00002903 if (_Rp == 0)
2904 __y0_ = _Rp;
Howard Hinnantc3267212010-05-26 17:49:34 +00002905 else if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00002906 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002907 else
2908 __y0_ = 0;
Howard Hinnant99968442011-11-29 18:15:50 +00002909 if (_Rp - __y0_ > __y0_ / __n_)
Howard Hinnantc3267212010-05-26 17:49:34 +00002910 {
2911 ++__n_;
2912 __w0_ = __w_ / __n_;
2913 if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00002914 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002915 else
2916 __y0_ = 0;
2917 }
2918 __n0_ = __n_ - __w_ % __n_;
2919 if (__w0_ < _WDt - 1)
Howard Hinnant99968442011-11-29 18:15:50 +00002920 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
Howard Hinnantc3267212010-05-26 17:49:34 +00002921 else
2922 __y1_ = 0;
2923 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2924 _Engine_result_type(0);
2925 __mask1_ = __w0_ < _EDt - 1 ?
2926 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2927 _Engine_result_type(~0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002928}
2929
Howard Hinnantc3267212010-05-26 17:49:34 +00002930template<class _Engine, class _UIntType>
2931inline
2932_UIntType
2933__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002934{
Howard Hinnantc3267212010-05-26 17:49:34 +00002935 return static_cast<result_type>(__e_() & __mask0_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002936}
2937
Howard Hinnantc3267212010-05-26 17:49:34 +00002938template<class _Engine, class _UIntType>
2939_UIntType
2940__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002941{
Howard Hinnant99968442011-11-29 18:15:50 +00002942 result_type _Sp = 0;
Howard Hinnantc3267212010-05-26 17:49:34 +00002943 for (size_t __k = 0; __k < __n0_; ++__k)
2944 {
2945 _Engine_result_type __u;
2946 do
2947 {
2948 __u = __e_() - _Engine::min();
2949 } while (__u >= __y0_);
Howard Hinnant8faa95f2011-10-27 16:12:10 +00002950 if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00002951 _Sp <<= __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002952 else
Howard Hinnant99968442011-11-29 18:15:50 +00002953 _Sp = 0;
2954 _Sp += __u & __mask0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002955 }
2956 for (size_t __k = __n0_; __k < __n_; ++__k)
2957 {
2958 _Engine_result_type __u;
2959 do
2960 {
2961 __u = __e_() - _Engine::min();
2962 } while (__u >= __y1_);
Howard Hinnant8faa95f2011-10-27 16:12:10 +00002963 if (__w0_ < _WDt - 1)
Howard Hinnant99968442011-11-29 18:15:50 +00002964 _Sp <<= __w0_ + 1;
Howard Hinnantc3267212010-05-26 17:49:34 +00002965 else
Howard Hinnant99968442011-11-29 18:15:50 +00002966 _Sp = 0;
2967 _Sp += __u & __mask1_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002968 }
Howard Hinnant99968442011-11-29 18:15:50 +00002969 return _Sp;
Howard Hinnantc3267212010-05-26 17:49:34 +00002970}
2971
2972// uniform_int_distribution
2973
2974template<class _IntType = int>
2975class uniform_int_distribution
2976{
2977public:
2978 // types
2979 typedef _IntType result_type;
2980
2981 class param_type
2982 {
2983 result_type __a_;
2984 result_type __b_;
2985 public:
2986 typedef uniform_int_distribution distribution_type;
2987
2988 explicit param_type(result_type __a = 0,
2989 result_type __b = numeric_limits<result_type>::max())
2990 : __a_(__a), __b_(__b) {}
2991
2992 result_type a() const {return __a_;}
2993 result_type b() const {return __b_;}
2994
2995 friend bool operator==(const param_type& __x, const param_type& __y)
2996 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2997 friend bool operator!=(const param_type& __x, const param_type& __y)
2998 {return !(__x == __y);}
2999 };
3000
3001private:
3002 param_type __p_;
3003
3004public:
3005 // constructors and reset functions
3006 explicit uniform_int_distribution(result_type __a = 0,
3007 result_type __b = numeric_limits<result_type>::max())
3008 : __p_(param_type(__a, __b)) {}
3009 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
3010 void reset() {}
3011
3012 // generating functions
3013 template<class _URNG> result_type operator()(_URNG& __g)
3014 {return (*this)(__g, __p_);}
3015 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3016
3017 // property functions
3018 result_type a() const {return __p_.a();}
3019 result_type b() const {return __p_.b();}
3020
3021 param_type param() const {return __p_;}
3022 void param(const param_type& __p) {__p_ = __p;}
3023
3024 result_type min() const {return a();}
3025 result_type max() const {return b();}
3026
3027 friend bool operator==(const uniform_int_distribution& __x,
3028 const uniform_int_distribution& __y)
3029 {return __x.__p_ == __y.__p_;}
3030 friend bool operator!=(const uniform_int_distribution& __x,
3031 const uniform_int_distribution& __y)
3032 {return !(__x == __y);}
3033};
3034
3035template<class _IntType>
3036template<class _URNG>
3037typename uniform_int_distribution<_IntType>::result_type
3038uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3039{
3040 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3041 uint32_t, uint64_t>::type _UIntType;
Howard Hinnant99968442011-11-29 18:15:50 +00003042 const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
3043 if (_Rp == 1)
Howard Hinnantc3267212010-05-26 17:49:34 +00003044 return __p.a();
3045 const size_t _Dt = numeric_limits<_UIntType>::digits;
3046 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
Howard Hinnant99968442011-11-29 18:15:50 +00003047 if (_Rp == 0)
Howard Hinnantc3267212010-05-26 17:49:34 +00003048 return static_cast<result_type>(_Eng(__g, _Dt)());
Howard Hinnant99968442011-11-29 18:15:50 +00003049 size_t __w = _Dt - __clz(_Rp) - 1;
Marshall Clow0934c752015-07-30 18:26:34 +00003050 if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
Howard Hinnantc3267212010-05-26 17:49:34 +00003051 ++__w;
3052 _Eng __e(__g, __w);
3053 _UIntType __u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003054 do
Howard Hinnantc3267212010-05-26 17:49:34 +00003055 {
3056 __u = __e();
Howard Hinnant99968442011-11-29 18:15:50 +00003057 } while (__u >= _Rp);
Howard Hinnantc3267212010-05-26 17:49:34 +00003058 return static_cast<result_type>(__u + __p.a());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003059}
3060
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003061class _LIBCPP_TYPE_VIS __rs_default;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003062
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003063_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantc3267212010-05-26 17:49:34 +00003064
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003065class _LIBCPP_TYPE_VIS __rs_default
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003066{
Howard Hinnantc3267212010-05-26 17:49:34 +00003067 static unsigned __c_;
3068
3069 __rs_default();
3070public:
Marshall Clow5920cfc2013-02-07 22:12:02 +00003071 typedef uint_fast32_t result_type;
Howard Hinnantc3267212010-05-26 17:49:34 +00003072
3073 static const result_type _Min = 0;
3074 static const result_type _Max = 0xFFFFFFFF;
3075
3076 __rs_default(const __rs_default&);
3077 ~__rs_default();
3078
3079 result_type operator()();
3080
Howard Hinnant27b4fd32012-04-02 00:40:41 +00003081 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
3082 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
Howard Hinnantc3267212010-05-26 17:49:34 +00003083
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003084 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003085};
3086
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003087_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003088
3089template <class _RandomAccessIterator>
3090void
3091random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
3092{
3093 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnant99968442011-11-29 18:15:50 +00003094 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3095 typedef typename _Dp::param_type _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003096 difference_type __d = __last - __first;
3097 if (__d > 1)
3098 {
Howard Hinnant99968442011-11-29 18:15:50 +00003099 _Dp __uid;
Howard Hinnantc3267212010-05-26 17:49:34 +00003100 __rs_default __g = __rs_get();
3101 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003102 {
Howard Hinnant99968442011-11-29 18:15:50 +00003103 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant4e599482010-10-22 15:26:39 +00003104 if (__i != difference_type(0))
3105 swap(*__first, *(__first + __i));
3106 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003107 }
3108}
3109
3110template <class _RandomAccessIterator, class _RandomNumberGenerator>
3111void
3112random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnant73d21a42010-09-04 23:28:19 +00003113#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003114 _RandomNumberGenerator&& __rand)
3115#else
3116 _RandomNumberGenerator& __rand)
3117#endif
3118{
3119 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3120 difference_type __d = __last - __first;
3121 if (__d > 1)
3122 {
3123 for (--__last; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003124 {
3125 difference_type __i = __rand(__d);
3126 swap(*__first, *(__first + __i));
3127 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003128 }
3129}
3130
Howard Hinnantc3267212010-05-26 17:49:34 +00003131template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3132 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnant278bf2d2010-11-18 01:47:02 +00003133#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3134 _UniformRandomNumberGenerator&& __g)
3135#else
Howard Hinnantc3267212010-05-26 17:49:34 +00003136 _UniformRandomNumberGenerator& __g)
Howard Hinnant278bf2d2010-11-18 01:47:02 +00003137#endif
Howard Hinnantc3267212010-05-26 17:49:34 +00003138{
3139 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnant99968442011-11-29 18:15:50 +00003140 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3141 typedef typename _Dp::param_type _Pp;
Howard Hinnantc3267212010-05-26 17:49:34 +00003142 difference_type __d = __last - __first;
3143 if (__d > 1)
3144 {
Howard Hinnant99968442011-11-29 18:15:50 +00003145 _Dp __uid;
Howard Hinnantc3267212010-05-26 17:49:34 +00003146 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003147 {
Howard Hinnant99968442011-11-29 18:15:50 +00003148 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant4e599482010-10-22 15:26:39 +00003149 if (__i != difference_type(0))
3150 swap(*__first, *(__first + __i));
3151 }
Howard Hinnantc3267212010-05-26 17:49:34 +00003152 }
3153}
3154
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003155template <class _InputIterator, class _Predicate>
3156bool
3157is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3158{
3159 for (; __first != __last; ++__first)
3160 if (!__pred(*__first))
3161 break;
Marshall Clowa0ec4b72015-02-02 18:16:35 +00003162 if ( __first == __last )
3163 return true;
3164 ++__first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003165 for (; __first != __last; ++__first)
3166 if (__pred(*__first))
3167 return false;
3168 return true;
3169}
3170
3171// partition
3172
3173template <class _Predicate, class _ForwardIterator>
3174_ForwardIterator
3175__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3176{
3177 while (true)
3178 {
3179 if (__first == __last)
3180 return __first;
3181 if (!__pred(*__first))
3182 break;
3183 ++__first;
3184 }
3185 for (_ForwardIterator __p = __first; ++__p != __last;)
3186 {
3187 if (__pred(*__p))
3188 {
3189 swap(*__first, *__p);
3190 ++__first;
3191 }
3192 }
3193 return __first;
3194}
3195
3196template <class _Predicate, class _BidirectionalIterator>
3197_BidirectionalIterator
3198__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3199 bidirectional_iterator_tag)
3200{
3201 while (true)
3202 {
3203 while (true)
3204 {
3205 if (__first == __last)
3206 return __first;
3207 if (!__pred(*__first))
3208 break;
3209 ++__first;
3210 }
3211 do
3212 {
3213 if (__first == --__last)
3214 return __first;
3215 } while (!__pred(*__last));
3216 swap(*__first, *__last);
3217 ++__first;
3218 }
3219}
3220
3221template <class _ForwardIterator, class _Predicate>
3222inline _LIBCPP_INLINE_VISIBILITY
3223_ForwardIterator
3224partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3225{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003226 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003227 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3228}
3229
3230// partition_copy
3231
3232template <class _InputIterator, class _OutputIterator1,
3233 class _OutputIterator2, class _Predicate>
3234pair<_OutputIterator1, _OutputIterator2>
3235partition_copy(_InputIterator __first, _InputIterator __last,
3236 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3237 _Predicate __pred)
3238{
3239 for (; __first != __last; ++__first)
3240 {
3241 if (__pred(*__first))
3242 {
3243 *__out_true = *__first;
3244 ++__out_true;
3245 }
3246 else
3247 {
3248 *__out_false = *__first;
3249 ++__out_false;
3250 }
3251 }
3252 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3253}
3254
3255// partition_point
3256
3257template<class _ForwardIterator, class _Predicate>
3258_ForwardIterator
3259partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3260{
3261 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003262 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003263 while (__len != 0)
3264 {
3265 difference_type __l2 = __len / 2;
3266 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003267 _VSTD::advance(__m, __l2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003268 if (__pred(*__m))
3269 {
3270 __first = ++__m;
3271 __len -= __l2 + 1;
3272 }
3273 else
3274 __len = __l2;
3275 }
3276 return __first;
3277}
3278
3279// stable_partition
3280
3281template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3282_ForwardIterator
3283__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3284 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3285{
3286 // *__first is known to be false
3287 // __len >= 1
3288 if (__len == 1)
3289 return __first;
3290 if (__len == 2)
3291 {
3292 _ForwardIterator __m = __first;
3293 if (__pred(*++__m))
3294 {
3295 swap(*__first, *__m);
3296 return __m;
3297 }
3298 return __first;
3299 }
3300 if (__len <= __p.second)
3301 { // The buffer is big enough to use
3302 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3303 __destruct_n __d(0);
3304 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3305 // Move the falses into the temporary buffer, and the trues to the front of the line
3306 // Update __first to always point to the end of the trues
3307 value_type* __t = __p.first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003308 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003309 __d.__incr((value_type*)0);
3310 ++__t;
3311 _ForwardIterator __i = __first;
3312 while (++__i != __last)
3313 {
3314 if (__pred(*__i))
3315 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003316 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003317 ++__first;
3318 }
3319 else
3320 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003321 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003322 __d.__incr((value_type*)0);
3323 ++__t;
3324 }
3325 }
3326 // All trues now at start of range, all falses in buffer
3327 // Move falses back into range, but don't mess up __first which points to first false
3328 __i = __first;
3329 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003330 *__i = _VSTD::move(*__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003331 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3332 return __first;
3333 }
3334 // Else not enough buffer, do in place
3335 // __len >= 3
3336 _ForwardIterator __m = __first;
3337 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003338 _VSTD::advance(__m, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003339 // recurse on [__first, __m), *__first know to be false
3340 // F?????????????????
3341 // f m l
3342 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3343 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3344 // TTTFFFFF??????????
3345 // f ff m l
3346 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3347 _ForwardIterator __m1 = __m;
3348 _ForwardIterator __second_false = __last;
3349 _Distance __len_half = __len - __len2;
3350 while (__pred(*__m1))
3351 {
3352 if (++__m1 == __last)
3353 goto __second_half_done;
3354 --__len_half;
3355 }
3356 // TTTFFFFFTTTF??????
3357 // f ff m m1 l
3358 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3359__second_half_done:
3360 // TTTFFFFFTTTTTFFFFF
3361 // f ff m sf l
Howard Hinnant0949eed2011-06-30 21:18:19 +00003362 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003363 // TTTTTTTTFFFFFFFFFF
3364 // |
3365}
3366
3367struct __return_temporary_buffer
3368{
3369 template <class _Tp>
Howard Hinnant0949eed2011-06-30 21:18:19 +00003370 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003371};
3372
3373template <class _Predicate, class _ForwardIterator>
3374_ForwardIterator
3375__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3376 forward_iterator_tag)
3377{
3378 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3379 // Either prove all true and return __first or point to first false
3380 while (true)
3381 {
3382 if (__first == __last)
3383 return __first;
3384 if (!__pred(*__first))
3385 break;
3386 ++__first;
3387 }
3388 // We now have a reduced range [__first, __last)
3389 // *__first is known to be false
3390 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3391 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003392 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003393 pair<value_type*, ptrdiff_t> __p(0, 0);
3394 unique_ptr<value_type, __return_temporary_buffer> __h;
3395 if (__len >= __alloc_limit)
3396 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003397 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003398 __h.reset(__p.first);
3399 }
3400 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3401 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3402}
3403
3404template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3405_BidirectionalIterator
3406__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3407 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3408{
3409 // *__first is known to be false
3410 // *__last is known to be true
3411 // __len >= 2
3412 if (__len == 2)
3413 {
3414 swap(*__first, *__last);
3415 return __last;
3416 }
3417 if (__len == 3)
3418 {
3419 _BidirectionalIterator __m = __first;
3420 if (__pred(*++__m))
3421 {
3422 swap(*__first, *__m);
3423 swap(*__m, *__last);
3424 return __last;
3425 }
3426 swap(*__m, *__last);
3427 swap(*__first, *__m);
3428 return __m;
3429 }
3430 if (__len <= __p.second)
3431 { // The buffer is big enough to use
3432 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3433 __destruct_n __d(0);
3434 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3435 // Move the falses into the temporary buffer, and the trues to the front of the line
3436 // Update __first to always point to the end of the trues
3437 value_type* __t = __p.first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003438 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003439 __d.__incr((value_type*)0);
3440 ++__t;
3441 _BidirectionalIterator __i = __first;
3442 while (++__i != __last)
3443 {
3444 if (__pred(*__i))
3445 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003446 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003447 ++__first;
3448 }
3449 else
3450 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003451 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003452 __d.__incr((value_type*)0);
3453 ++__t;
3454 }
3455 }
3456 // move *__last, known to be true
Howard Hinnant0949eed2011-06-30 21:18:19 +00003457 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003458 __i = ++__first;
3459 // All trues now at start of range, all falses in buffer
3460 // Move falses back into range, but don't mess up __first which points to first false
3461 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003462 *__i = _VSTD::move(*__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003463 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3464 return __first;
3465 }
3466 // Else not enough buffer, do in place
3467 // __len >= 4
3468 _BidirectionalIterator __m = __first;
3469 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003470 _VSTD::advance(__m, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003471 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3472 // F????????????????T
3473 // f m l
3474 _BidirectionalIterator __m1 = __m;
3475 _BidirectionalIterator __first_false = __first;
3476 _Distance __len_half = __len2;
3477 while (!__pred(*--__m1))
3478 {
3479 if (__m1 == __first)
3480 goto __first_half_done;
3481 --__len_half;
3482 }
3483 // F???TFFF?????????T
3484 // f m1 m l
3485 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3486 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3487__first_half_done:
3488 // TTTFFFFF?????????T
3489 // f ff m l
3490 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3491 __m1 = __m;
3492 _BidirectionalIterator __second_false = __last;
3493 ++__second_false;
3494 __len_half = __len - __len2;
3495 while (__pred(*__m1))
3496 {
3497 if (++__m1 == __last)
3498 goto __second_half_done;
3499 --__len_half;
3500 }
3501 // TTTFFFFFTTTF?????T
3502 // f ff m m1 l
3503 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3504__second_half_done:
3505 // TTTFFFFFTTTTTFFFFF
3506 // f ff m sf l
Howard Hinnant0949eed2011-06-30 21:18:19 +00003507 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003508 // TTTTTTTTFFFFFFFFFF
3509 // |
3510}
3511
3512template <class _Predicate, class _BidirectionalIterator>
3513_BidirectionalIterator
3514__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3515 bidirectional_iterator_tag)
3516{
3517 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3518 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3519 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3520 // Either prove all true and return __first or point to first false
3521 while (true)
3522 {
3523 if (__first == __last)
3524 return __first;
3525 if (!__pred(*__first))
3526 break;
3527 ++__first;
3528 }
3529 // __first points to first false, everything prior to __first is already set.
3530 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3531 do
3532 {
3533 if (__first == --__last)
3534 return __first;
3535 } while (!__pred(*__last));
3536 // We now have a reduced range [__first, __last]
3537 // *__first is known to be false
3538 // *__last is known to be true
3539 // __len >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003540 difference_type __len = _VSTD::distance(__first, __last) + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003541 pair<value_type*, ptrdiff_t> __p(0, 0);
3542 unique_ptr<value_type, __return_temporary_buffer> __h;
3543 if (__len >= __alloc_limit)
3544 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003545 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003546 __h.reset(__p.first);
3547 }
3548 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3549 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3550}
3551
3552template <class _ForwardIterator, class _Predicate>
3553inline _LIBCPP_INLINE_VISIBILITY
3554_ForwardIterator
3555stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3556{
3557 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3558 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3559}
3560
3561// is_sorted_until
3562
3563template <class _ForwardIterator, class _Compare>
3564_ForwardIterator
3565is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3566{
3567 if (__first != __last)
3568 {
3569 _ForwardIterator __i = __first;
3570 while (++__i != __last)
3571 {
3572 if (__comp(*__i, *__first))
3573 return __i;
3574 __first = __i;
3575 }
3576 }
3577 return __last;
3578}
3579
Howard Hinnant324bb032010-08-22 00:02:43 +00003580template<class _ForwardIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003581inline _LIBCPP_INLINE_VISIBILITY
3582_ForwardIterator
3583is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3584{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003585 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003586}
3587
3588// is_sorted
3589
3590template <class _ForwardIterator, class _Compare>
3591inline _LIBCPP_INLINE_VISIBILITY
3592bool
3593is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3594{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003595 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003596}
3597
Howard Hinnant324bb032010-08-22 00:02:43 +00003598template<class _ForwardIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003599inline _LIBCPP_INLINE_VISIBILITY
3600bool
3601is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3602{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003603 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003604}
3605
3606// sort
3607
3608// stable, 2-3 compares, 0-2 swaps
3609
3610template <class _Compare, class _ForwardIterator>
3611unsigned
3612__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3613{
3614 unsigned __r = 0;
3615 if (!__c(*__y, *__x)) // if x <= y
3616 {
3617 if (!__c(*__z, *__y)) // if y <= z
3618 return __r; // x <= y && y <= z
3619 // x <= y && y > z
3620 swap(*__y, *__z); // x <= z && y < z
3621 __r = 1;
3622 if (__c(*__y, *__x)) // if x > y
3623 {
3624 swap(*__x, *__y); // x < y && y <= z
3625 __r = 2;
3626 }
3627 return __r; // x <= y && y < z
3628 }
3629 if (__c(*__z, *__y)) // x > y, if y > z
3630 {
3631 swap(*__x, *__z); // x < y && y < z
3632 __r = 1;
3633 return __r;
3634 }
3635 swap(*__x, *__y); // x > y && y <= z
3636 __r = 1; // x < y && x <= z
3637 if (__c(*__z, *__y)) // if y > z
3638 {
3639 swap(*__y, *__z); // x <= y && y < z
3640 __r = 2;
3641 }
3642 return __r;
3643} // x <= y && y <= z
3644
3645// stable, 3-6 compares, 0-5 swaps
3646
3647template <class _Compare, class _ForwardIterator>
3648unsigned
3649__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3650 _ForwardIterator __x4, _Compare __c)
3651{
3652 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3653 if (__c(*__x4, *__x3))
3654 {
3655 swap(*__x3, *__x4);
3656 ++__r;
3657 if (__c(*__x3, *__x2))
3658 {
3659 swap(*__x2, *__x3);
3660 ++__r;
3661 if (__c(*__x2, *__x1))
3662 {
3663 swap(*__x1, *__x2);
3664 ++__r;
3665 }
3666 }
3667 }
3668 return __r;
3669}
3670
3671// stable, 4-10 compares, 0-9 swaps
3672
3673template <class _Compare, class _ForwardIterator>
3674unsigned
3675__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3676 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3677{
3678 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3679 if (__c(*__x5, *__x4))
3680 {
3681 swap(*__x4, *__x5);
3682 ++__r;
3683 if (__c(*__x4, *__x3))
3684 {
3685 swap(*__x3, *__x4);
3686 ++__r;
3687 if (__c(*__x3, *__x2))
3688 {
3689 swap(*__x2, *__x3);
3690 ++__r;
3691 if (__c(*__x2, *__x1))
3692 {
3693 swap(*__x1, *__x2);
3694 ++__r;
3695 }
3696 }
3697 }
3698 }
3699 return __r;
3700}
3701
3702// Assumes size > 0
3703template <class _Compare, class _BirdirectionalIterator>
3704void
3705__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3706{
3707 _BirdirectionalIterator __lm1 = __last;
3708 for (--__lm1; __first != __lm1; ++__first)
3709 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003710 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003711 typename add_lvalue_reference<_Compare>::type>
3712 (__first, __last, __comp);
3713 if (__i != __first)
3714 swap(*__first, *__i);
3715 }
3716}
3717
3718template <class _Compare, class _BirdirectionalIterator>
3719void
3720__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3721{
3722 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3723 if (__first != __last)
3724 {
3725 _BirdirectionalIterator __i = __first;
3726 for (++__i; __i != __last; ++__i)
3727 {
3728 _BirdirectionalIterator __j = __i;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003729 value_type __t(_VSTD::move(*__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003730 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003731 *__j = _VSTD::move(*__k);
3732 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003733 }
3734 }
3735}
3736
3737template <class _Compare, class _RandomAccessIterator>
3738void
3739__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3740{
3741 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3742 _RandomAccessIterator __j = __first+2;
3743 __sort3<_Compare>(__first, __first+1, __j, __comp);
3744 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3745 {
3746 if (__comp(*__i, *__j))
3747 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003748 value_type __t(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003749 _RandomAccessIterator __k = __j;
3750 __j = __i;
3751 do
3752 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003753 *__j = _VSTD::move(*__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003754 __j = __k;
3755 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003756 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003757 }
3758 __j = __i;
3759 }
3760}
3761
3762template <class _Compare, class _RandomAccessIterator>
3763bool
3764__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3765{
3766 switch (__last - __first)
3767 {
3768 case 0:
3769 case 1:
3770 return true;
3771 case 2:
3772 if (__comp(*--__last, *__first))
3773 swap(*__first, *__last);
3774 return true;
3775 case 3:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003776 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003777 return true;
3778 case 4:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003779 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003780 return true;
3781 case 5:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003782 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003783 return true;
3784 }
3785 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3786 _RandomAccessIterator __j = __first+2;
3787 __sort3<_Compare>(__first, __first+1, __j, __comp);
3788 const unsigned __limit = 8;
3789 unsigned __count = 0;
3790 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3791 {
3792 if (__comp(*__i, *__j))
3793 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003794 value_type __t(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003795 _RandomAccessIterator __k = __j;
3796 __j = __i;
3797 do
3798 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003799 *__j = _VSTD::move(*__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003800 __j = __k;
3801 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003802 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003803 if (++__count == __limit)
3804 return ++__i == __last;
3805 }
3806 __j = __i;
3807 }
3808 return true;
3809}
3810
3811template <class _Compare, class _BirdirectionalIterator>
3812void
3813__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3814 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3815{
3816 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3817 if (__first1 != __last1)
3818 {
3819 __destruct_n __d(0);
3820 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3821 value_type* __last2 = __first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003822 ::new(__last2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003823 __d.__incr((value_type*)0);
3824 for (++__last2; ++__first1 != __last1; ++__last2)
3825 {
3826 value_type* __j2 = __last2;
3827 value_type* __i2 = __j2;
3828 if (__comp(*__first1, *--__i2))
3829 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003830 ::new(__j2) value_type(_VSTD::move(*__i2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003831 __d.__incr((value_type*)0);
3832 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003833 *__j2 = _VSTD::move(*__i2);
3834 *__j2 = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003835 }
3836 else
3837 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003838 ::new(__j2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003839 __d.__incr((value_type*)0);
3840 }
3841 }
3842 __h.release();
3843 }
3844}
3845
3846template <class _Compare, class _RandomAccessIterator>
3847void
3848__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3849{
3850 // _Compare is known to be a reference type
3851 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3852 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant1468b662010-11-19 22:17:28 +00003853 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3854 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003855 while (true)
3856 {
3857 __restart:
3858 difference_type __len = __last - __first;
3859 switch (__len)
3860 {
3861 case 0:
3862 case 1:
3863 return;
3864 case 2:
3865 if (__comp(*--__last, *__first))
3866 swap(*__first, *__last);
3867 return;
3868 case 3:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003869 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003870 return;
3871 case 4:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003872 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003873 return;
3874 case 5:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003875 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003876 return;
3877 }
3878 if (__len <= __limit)
3879 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003880 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003881 return;
3882 }
3883 // __len > 5
3884 _RandomAccessIterator __m = __first;
3885 _RandomAccessIterator __lm1 = __last;
3886 --__lm1;
3887 unsigned __n_swaps;
3888 {
3889 difference_type __delta;
3890 if (__len >= 1000)
3891 {
3892 __delta = __len/2;
3893 __m += __delta;
3894 __delta /= 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003895 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003896 }
3897 else
3898 {
3899 __delta = __len/2;
3900 __m += __delta;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003901 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003902 }
3903 }
3904 // *__m is median
3905 // partition [__first, __m) < *__m and *__m <= [__m, __last)
3906 // (this inhibits tossing elements equivalent to __m around unnecessarily)
3907 _RandomAccessIterator __i = __first;
3908 _RandomAccessIterator __j = __lm1;
3909 // j points beyond range to be tested, *__m is known to be <= *__lm1
3910 // The search going up is known to be guarded but the search coming down isn't.
3911 // Prime the downward search with a guard.
3912 if (!__comp(*__i, *__m)) // if *__first == *__m
3913 {
3914 // *__first == *__m, *__first doesn't go in first part
3915 // manually guard downward moving __j against __i
3916 while (true)
3917 {
3918 if (__i == --__j)
3919 {
3920 // *__first == *__m, *__m <= all other elements
3921 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
3922 ++__i; // __first + 1
3923 __j = __last;
3924 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
3925 {
3926 while (true)
3927 {
3928 if (__i == __j)
3929 return; // [__first, __last) all equivalent elements
3930 if (__comp(*__first, *__i))
3931 {
3932 swap(*__i, *__j);
3933 ++__n_swaps;
3934 ++__i;
3935 break;
3936 }
3937 ++__i;
3938 }
3939 }
3940 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
3941 if (__i == __j)
3942 return;
3943 while (true)
3944 {
3945 while (!__comp(*__first, *__i))
3946 ++__i;
3947 while (__comp(*__first, *--__j))
3948 ;
3949 if (__i >= __j)
3950 break;
3951 swap(*__i, *__j);
3952 ++__n_swaps;
3953 ++__i;
3954 }
3955 // [__first, __i) == *__first and *__first < [__i, __last)
3956 // The first part is sorted, sort the secod part
Howard Hinnant0949eed2011-06-30 21:18:19 +00003957 // _VSTD::__sort<_Compare>(__i, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003958 __first = __i;
3959 goto __restart;
3960 }
3961 if (__comp(*__j, *__m))
3962 {
3963 swap(*__i, *__j);
3964 ++__n_swaps;
3965 break; // found guard for downward moving __j, now use unguarded partition
3966 }
3967 }
3968 }
3969 // It is known that *__i < *__m
3970 ++__i;
3971 // j points beyond range to be tested, *__m is known to be <= *__lm1
3972 // if not yet partitioned...
3973 if (__i < __j)
3974 {
3975 // known that *(__i - 1) < *__m
3976 // known that __i <= __m
3977 while (true)
3978 {
3979 // __m still guards upward moving __i
3980 while (__comp(*__i, *__m))
3981 ++__i;
3982 // It is now known that a guard exists for downward moving __j
3983 while (!__comp(*--__j, *__m))
3984 ;
3985 if (__i > __j)
3986 break;
3987 swap(*__i, *__j);
3988 ++__n_swaps;
3989 // It is known that __m != __j
3990 // If __m just moved, follow it
3991 if (__m == __i)
3992 __m = __j;
3993 ++__i;
3994 }
3995 }
3996 // [__first, __i) < *__m and *__m <= [__i, __last)
3997 if (__i != __m && __comp(*__m, *__i))
3998 {
3999 swap(*__i, *__m);
4000 ++__n_swaps;
4001 }
4002 // [__first, __i) < *__i and *__i <= [__i+1, __last)
4003 // If we were given a perfect partition, see if insertion sort is quick...
4004 if (__n_swaps == 0)
4005 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004006 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
4007 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004008 {
4009 if (__fs)
4010 return;
4011 __last = __i;
4012 continue;
4013 }
4014 else
4015 {
4016 if (__fs)
4017 {
4018 __first = ++__i;
4019 continue;
4020 }
4021 }
4022 }
4023 // sort smaller range with recursive call and larger with tail recursion elimination
4024 if (__i - __first < __last - __i)
4025 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004026 _VSTD::__sort<_Compare>(__first, __i, __comp);
4027 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004028 __first = ++__i;
4029 }
4030 else
4031 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004032 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4033 // _VSTD::__sort<_Compare>(__first, __i, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004034 __last = __i;
4035 }
4036 }
4037}
4038
4039// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
4040template <class _RandomAccessIterator, class _Compare>
4041inline _LIBCPP_INLINE_VISIBILITY
4042void
4043sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4044{
Howard Hinnant5e571422013-08-23 20:10:18 +00004045#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004046 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4047 __debug_less<_Compare> __c(__comp);
4048 __sort<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004049#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004050 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4051 __sort<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004052#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004053}
4054
4055template <class _RandomAccessIterator>
4056inline _LIBCPP_INLINE_VISIBILITY
4057void
4058sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4059{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004060 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004061}
4062
4063template <class _Tp>
4064inline _LIBCPP_INLINE_VISIBILITY
4065void
4066sort(_Tp** __first, _Tp** __last)
4067{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004068 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004069}
4070
4071template <class _Tp>
4072inline _LIBCPP_INLINE_VISIBILITY
4073void
4074sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4075{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004076 _VSTD::sort(__first.base(), __last.base());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004077}
4078
Howard Hinnant7a563db2011-09-14 18:33:51 +00004079template <class _Tp, class _Compare>
4080inline _LIBCPP_INLINE_VISIBILITY
4081void
4082sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4083{
4084 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4085 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4086}
4087
Howard Hinnante9df0a52013-08-01 18:17:34 +00004088#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +00004089#pragma warning( push )
4090#pragma warning( disable: 4231)
Howard Hinnante9df0a52013-08-01 18:17:34 +00004091#endif // _LIBCPP_MSVC
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004092_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4093_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4094_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4095_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4096_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4097_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4098_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4099_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4100_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4101_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4102_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4103_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>&))
4104_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4105_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4106_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 +00004107
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004108_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4109_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4110_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4111_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4112_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4113_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4114_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4115_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4116_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4117_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4118_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4119_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>&))
4120_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4121_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4122_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 +00004123
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004124_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 +00004125#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +00004126#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34 +00004127#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004128
4129// lower_bound
4130
4131template <class _Compare, class _ForwardIterator, class _Tp>
4132_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004133__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004134{
4135 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004136 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004137 while (__len != 0)
4138 {
4139 difference_type __l2 = __len / 2;
4140 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004141 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004142 if (__comp(*__m, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004143 {
4144 __first = ++__m;
4145 __len -= __l2 + 1;
4146 }
4147 else
4148 __len = __l2;
4149 }
4150 return __first;
4151}
4152
4153template <class _ForwardIterator, class _Tp, class _Compare>
4154inline _LIBCPP_INLINE_VISIBILITY
4155_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004156lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004157{
Howard Hinnant5e571422013-08-23 20:10:18 +00004158#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004159 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4160 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004161 return __lower_bound<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004162#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004163 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004164 return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004165#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004166}
4167
4168template <class _ForwardIterator, class _Tp>
4169inline _LIBCPP_INLINE_VISIBILITY
4170_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004171lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004172{
Howard Hinnant78b68282011-10-22 20:59:45 +00004173 return _VSTD::lower_bound(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004174 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4175}
4176
4177// upper_bound
4178
4179template <class _Compare, class _ForwardIterator, class _Tp>
4180_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004181__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004182{
4183 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004184 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004185 while (__len != 0)
4186 {
4187 difference_type __l2 = __len / 2;
4188 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004189 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004190 if (__comp(__value_, *__m))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004191 __len = __l2;
4192 else
4193 {
4194 __first = ++__m;
4195 __len -= __l2 + 1;
4196 }
4197 }
4198 return __first;
4199}
4200
4201template <class _ForwardIterator, class _Tp, class _Compare>
4202inline _LIBCPP_INLINE_VISIBILITY
4203_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004204upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004205{
Howard Hinnant5e571422013-08-23 20:10:18 +00004206#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004207 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4208 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004209 return __upper_bound<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004210#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004211 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004212 return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004213#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004214}
4215
4216template <class _ForwardIterator, class _Tp>
4217inline _LIBCPP_INLINE_VISIBILITY
4218_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004219upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004220{
Howard Hinnant78b68282011-10-22 20:59:45 +00004221 return _VSTD::upper_bound(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004222 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4223}
4224
4225// equal_range
4226
4227template <class _Compare, class _ForwardIterator, class _Tp>
4228pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004229__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004230{
4231 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004232 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004233 while (__len != 0)
4234 {
4235 difference_type __l2 = __len / 2;
4236 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004237 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004238 if (__comp(*__m, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004239 {
4240 __first = ++__m;
4241 __len -= __l2 + 1;
4242 }
Howard Hinnant78b68282011-10-22 20:59:45 +00004243 else if (__comp(__value_, *__m))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004244 {
4245 __last = __m;
4246 __len = __l2;
4247 }
4248 else
4249 {
4250 _ForwardIterator __mp1 = __m;
4251 return pair<_ForwardIterator, _ForwardIterator>
4252 (
Howard Hinnant78b68282011-10-22 20:59:45 +00004253 __lower_bound<_Compare>(__first, __m, __value_, __comp),
4254 __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004255 );
4256 }
4257 }
4258 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4259}
4260
4261template <class _ForwardIterator, class _Tp, class _Compare>
4262inline _LIBCPP_INLINE_VISIBILITY
4263pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004264equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004265{
Howard Hinnant5e571422013-08-23 20:10:18 +00004266#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004267 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4268 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004269 return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004270#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004271 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004272 return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004273#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004274}
4275
4276template <class _ForwardIterator, class _Tp>
4277inline _LIBCPP_INLINE_VISIBILITY
4278pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004279equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004280{
Howard Hinnant78b68282011-10-22 20:59:45 +00004281 return _VSTD::equal_range(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004282 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4283}
4284
4285// binary_search
4286
4287template <class _Compare, class _ForwardIterator, class _Tp>
4288inline _LIBCPP_INLINE_VISIBILITY
4289bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004290__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004291{
Howard Hinnant78b68282011-10-22 20:59:45 +00004292 __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4293 return __first != __last && !__comp(__value_, *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004294}
4295
4296template <class _ForwardIterator, class _Tp, class _Compare>
4297inline _LIBCPP_INLINE_VISIBILITY
4298bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004299binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004300{
Howard Hinnant5e571422013-08-23 20:10:18 +00004301#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004302 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4303 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004304 return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004305#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004306 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004307 return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004308#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004309}
4310
4311template <class _ForwardIterator, class _Tp>
4312inline _LIBCPP_INLINE_VISIBILITY
4313bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004314binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004315{
Howard Hinnant78b68282011-10-22 20:59:45 +00004316 return _VSTD::binary_search(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004317 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4318}
4319
4320// merge
4321
4322template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4323_OutputIterator
4324__merge(_InputIterator1 __first1, _InputIterator1 __last1,
4325 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4326{
4327 for (; __first1 != __last1; ++__result)
4328 {
4329 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004330 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004331 if (__comp(*__first2, *__first1))
4332 {
4333 *__result = *__first2;
4334 ++__first2;
4335 }
4336 else
4337 {
4338 *__result = *__first1;
4339 ++__first1;
4340 }
4341 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00004342 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004343}
4344
4345template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4346inline _LIBCPP_INLINE_VISIBILITY
4347_OutputIterator
4348merge(_InputIterator1 __first1, _InputIterator1 __last1,
4349 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4350{
Howard Hinnant5e571422013-08-23 20:10:18 +00004351#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004352 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4353 __debug_less<_Compare> __c(__comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004354 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004355#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004356 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004357 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004358#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004359}
4360
4361template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4362inline _LIBCPP_INLINE_VISIBILITY
4363_OutputIterator
4364merge(_InputIterator1 __first1, _InputIterator1 __last1,
4365 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4366{
4367 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4368 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4369 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4370}
4371
4372// inplace_merge
4373
Marshall Clowa3795762015-07-29 16:25:45 +00004374template <class _Compare, class _InputIterator1, class _InputIterator2,
4375 class _OutputIterator>
4376void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4377 _InputIterator2 __first2, _InputIterator2 __last2,
4378 _OutputIterator __result, _Compare __comp)
4379{
4380 for (; __first1 != __last1; ++__result)
4381 {
4382 if (__first2 == __last2)
4383 {
4384 _VSTD::move(__first1, __last1, __result);
4385 return;
4386 }
4387
4388 if (__comp(*__first2, *__first1))
4389 {
4390 *__result = _VSTD::move(*__first2);
4391 ++__first2;
4392 }
4393 else
4394 {
4395 *__result = _VSTD::move(*__first1);
4396 ++__first1;
4397 }
4398 }
4399 // __first2 through __last2 are already in the right spot.
4400}
4401
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004402template <class _Compare, class _BidirectionalIterator>
4403void
4404__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4405 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4406 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4407 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4408{
4409 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004410 __destruct_n __d(0);
4411 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4412 if (__len1 <= __len2)
4413 {
4414 value_type* __p = __buff;
Eric Fiselierb9919752014-10-27 19:28:20 +00004415 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004416 ::new(__p) value_type(_VSTD::move(*__i));
Marshall Clowa3795762015-07-29 16:25:45 +00004417 __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004418 }
4419 else
4420 {
4421 value_type* __p = __buff;
Eric Fiselierb9919752014-10-27 19:28:20 +00004422 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004423 ::new(__p) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004424 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4425 typedef reverse_iterator<value_type*> _Rv;
Marshall Clowa3795762015-07-29 16:25:45 +00004426 __half_inplace_merge(_Rv(__p), _Rv(__buff),
4427 _RBi(__middle), _RBi(__first),
4428 _RBi(__last), __negate<_Compare>(__comp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004429 }
4430}
4431
4432template <class _Compare, class _BidirectionalIterator>
4433void
4434__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4435 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4436 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4437 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4438{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004439 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4440 while (true)
4441 {
4442 // if __middle == __last, we're done
4443 if (__len2 == 0)
4444 return;
Marshall Clowe809f4c2015-02-02 16:44:11 +00004445 if (__len1 <= __buff_size || __len2 <= __buff_size)
4446 return __buffered_inplace_merge<_Compare>
4447 (__first, __middle, __last, __comp, __len1, __len2, __buff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004448 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
Eric Fiselierb9919752014-10-27 19:28:20 +00004449 for (; true; ++__first, (void) --__len1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004450 {
4451 if (__len1 == 0)
4452 return;
4453 if (__comp(*__middle, *__first))
4454 break;
4455 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004456 // __first < __middle < __last
4457 // *__first > *__middle
4458 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4459 // all elements in:
4460 // [__first, __m1) <= [__middle, __m2)
4461 // [__middle, __m2) < [__m1, __middle)
4462 // [__m1, __middle) <= [__m2, __last)
4463 // and __m1 or __m2 is in the middle of its range
4464 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4465 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4466 difference_type __len11; // distance(__first, __m1)
4467 difference_type __len21; // distance(__middle, __m2)
4468 // binary search smaller range
4469 if (__len1 < __len2)
4470 { // __len >= 1, __len2 >= 2
4471 __len21 = __len2 / 2;
4472 __m2 = __middle;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004473 _VSTD::advance(__m2, __len21);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004474 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004475 __len11 = _VSTD::distance(__first, __m1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004476 }
4477 else
4478 {
4479 if (__len1 == 1)
4480 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4481 // It is known *__first > *__middle
4482 swap(*__first, *__middle);
4483 return;
4484 }
4485 // __len1 >= 2, __len2 >= 1
4486 __len11 = __len1 / 2;
4487 __m1 = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004488 _VSTD::advance(__m1, __len11);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004489 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004490 __len21 = _VSTD::distance(__middle, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004491 }
4492 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4493 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4494 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4495 // swap middle two partitions
Howard Hinnant0949eed2011-06-30 21:18:19 +00004496 __middle = _VSTD::rotate(__m1, __middle, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004497 // __len12 and __len21 now have swapped meanings
4498 // merge smaller range with recurisve call and larger with tail recursion elimination
4499 if (__len11 + __len21 < __len12 + __len22)
4500 {
4501 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4502// __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4503 __first = __middle;
4504 __middle = __m2;
4505 __len1 = __len12;
4506 __len2 = __len22;
4507 }
4508 else
4509 {
4510 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4511// __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4512 __last = __middle;
4513 __middle = __m1;
4514 __len1 = __len11;
4515 __len2 = __len21;
4516 }
4517 }
4518}
4519
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004520template <class _BidirectionalIterator, class _Compare>
4521inline _LIBCPP_INLINE_VISIBILITY
4522void
4523inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4524 _Compare __comp)
4525{
4526 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4527 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004528 difference_type __len1 = _VSTD::distance(__first, __middle);
4529 difference_type __len2 = _VSTD::distance(__middle, __last);
4530 difference_type __buf_size = _VSTD::min(__len1, __len2);
Marshall Clow4c2684c2015-02-02 17:35:53 +00004531 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4532 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
4533
Howard Hinnant5e571422013-08-23 20:10:18 +00004534#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004535 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4536 __debug_less<_Compare> __c(__comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004537 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004538 __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004539#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004540 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004541 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004542 __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004543#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004544}
4545
4546template <class _BidirectionalIterator>
4547inline _LIBCPP_INLINE_VISIBILITY
4548void
4549inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4550{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004551 _VSTD::inplace_merge(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004552 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4553}
4554
4555// stable_sort
4556
4557template <class _Compare, class _InputIterator1, class _InputIterator2>
4558void
4559__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4560 _InputIterator2 __first2, _InputIterator2 __last2,
4561 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4562{
4563 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4564 __destruct_n __d(0);
4565 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4566 for (; true; ++__result)
4567 {
4568 if (__first1 == __last1)
4569 {
4570 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
Howard Hinnant0949eed2011-06-30 21:18:19 +00004571 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004572 __h.release();
4573 return;
4574 }
4575 if (__first2 == __last2)
4576 {
4577 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
Howard Hinnant0949eed2011-06-30 21:18:19 +00004578 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004579 __h.release();
4580 return;
4581 }
4582 if (__comp(*__first2, *__first1))
4583 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004584 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004585 __d.__incr((value_type*)0);
4586 ++__first2;
4587 }
4588 else
4589 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004590 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004591 __d.__incr((value_type*)0);
4592 ++__first1;
4593 }
4594 }
4595}
4596
4597template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4598void
4599__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4600 _InputIterator2 __first2, _InputIterator2 __last2,
4601 _OutputIterator __result, _Compare __comp)
4602{
4603 for (; __first1 != __last1; ++__result)
4604 {
4605 if (__first2 == __last2)
4606 {
4607 for (; __first1 != __last1; ++__first1, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004608 *__result = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004609 return;
4610 }
4611 if (__comp(*__first2, *__first1))
4612 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004613 *__result = _VSTD::move(*__first2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004614 ++__first2;
4615 }
4616 else
4617 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004618 *__result = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004619 ++__first1;
4620 }
4621 }
4622 for (; __first2 != __last2; ++__first2, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004623 *__result = _VSTD::move(*__first2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004624}
4625
4626template <class _Compare, class _RandomAccessIterator>
4627void
4628__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4629 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4630 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4631
4632template <class _Compare, class _RandomAccessIterator>
4633void
4634__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4635 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4636 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4637{
4638 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4639 switch (__len)
4640 {
4641 case 0:
4642 return;
4643 case 1:
Howard Hinnant0949eed2011-06-30 21:18:19 +00004644 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004645 return;
4646 case 2:
4647 __destruct_n __d(0);
4648 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4649 if (__comp(*--__last1, *__first1))
4650 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004651 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004652 __d.__incr((value_type*)0);
4653 ++__first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004654 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004655 }
4656 else
4657 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004658 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004659 __d.__incr((value_type*)0);
4660 ++__first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004661 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004662 }
4663 __h2.release();
4664 return;
4665 }
4666 if (__len <= 8)
4667 {
4668 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4669 return;
4670 }
4671 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4672 _RandomAccessIterator __m = __first1 + __l2;
4673 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4674 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4675 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4676}
4677
4678template <class _Tp>
4679struct __stable_sort_switch
4680{
Howard Hinnant1468b662010-11-19 22:17:28 +00004681 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004682};
4683
4684template <class _Compare, class _RandomAccessIterator>
4685void
4686__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4687 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4688 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4689{
4690 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4691 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4692 switch (__len)
4693 {
4694 case 0:
4695 case 1:
4696 return;
4697 case 2:
4698 if (__comp(*--__last, *__first))
4699 swap(*__first, *__last);
4700 return;
4701 }
4702 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4703 {
4704 __insertion_sort<_Compare>(__first, __last, __comp);
4705 return;
4706 }
4707 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4708 _RandomAccessIterator __m = __first + __l2;
4709 if (__len <= __buff_size)
4710 {
4711 __destruct_n __d(0);
4712 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4713 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4714 __d.__set(__l2, (value_type*)0);
4715 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4716 __d.__set(__len, (value_type*)0);
4717 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4718// __merge<_Compare>(move_iterator<value_type*>(__buff),
4719// move_iterator<value_type*>(__buff + __l2),
4720// move_iterator<_RandomAccessIterator>(__buff + __l2),
4721// move_iterator<_RandomAccessIterator>(__buff + __len),
4722// __first, __comp);
4723 return;
4724 }
4725 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4726 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4727 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4728}
4729
4730template <class _RandomAccessIterator, class _Compare>
4731inline _LIBCPP_INLINE_VISIBILITY
4732void
4733stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4734{
4735 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4736 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4737 difference_type __len = __last - __first;
4738 pair<value_type*, ptrdiff_t> __buf(0, 0);
4739 unique_ptr<value_type, __return_temporary_buffer> __h;
4740 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4741 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004742 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004743 __h.reset(__buf.first);
4744 }
Howard Hinnant5e571422013-08-23 20:10:18 +00004745#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004746 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4747 __debug_less<_Compare> __c(__comp);
4748 __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004749#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004750 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4751 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004752#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004753}
4754
4755template <class _RandomAccessIterator>
4756inline _LIBCPP_INLINE_VISIBILITY
4757void
4758stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4759{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004760 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004761}
4762
4763// is_heap_until
4764
4765template <class _RandomAccessIterator, class _Compare>
4766_RandomAccessIterator
4767is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4768{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004769 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004770 difference_type __len = __last - __first;
4771 difference_type __p = 0;
4772 difference_type __c = 1;
4773 _RandomAccessIterator __pp = __first;
4774 while (__c < __len)
4775 {
4776 _RandomAccessIterator __cp = __first + __c;
4777 if (__comp(*__pp, *__cp))
4778 return __cp;
4779 ++__c;
4780 ++__cp;
4781 if (__c == __len)
4782 return __last;
4783 if (__comp(*__pp, *__cp))
4784 return __cp;
4785 ++__p;
4786 ++__pp;
4787 __c = 2 * __p + 1;
4788 }
4789 return __last;
4790}
4791
Howard Hinnant324bb032010-08-22 00:02:43 +00004792template<class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004793inline _LIBCPP_INLINE_VISIBILITY
4794_RandomAccessIterator
4795is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4796{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004797 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004798}
4799
4800// is_heap
4801
4802template <class _RandomAccessIterator, class _Compare>
4803inline _LIBCPP_INLINE_VISIBILITY
4804bool
4805is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4806{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004807 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004808}
4809
Howard Hinnant324bb032010-08-22 00:02:43 +00004810template<class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004811inline _LIBCPP_INLINE_VISIBILITY
4812bool
4813is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4814{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004815 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004816}
4817
4818// push_heap
4819
4820template <class _Compare, class _RandomAccessIterator>
4821void
David Majnemercb8757a2014-07-22 06:07:09 +00004822__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4823 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004824{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004825 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4826 if (__len > 1)
4827 {
4828 __len = (__len - 2) / 2;
4829 _RandomAccessIterator __ptr = __first + __len;
4830 if (__comp(*__ptr, *--__last))
4831 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004832 value_type __t(_VSTD::move(*__last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004833 do
4834 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004835 *__last = _VSTD::move(*__ptr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004836 __last = __ptr;
4837 if (__len == 0)
4838 break;
4839 __len = (__len - 1) / 2;
4840 __ptr = __first + __len;
4841 } while (__comp(*__ptr, __t));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004842 *__last = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004843 }
4844 }
4845}
4846
4847template <class _RandomAccessIterator, class _Compare>
4848inline _LIBCPP_INLINE_VISIBILITY
4849void
4850push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4851{
Howard Hinnant5e571422013-08-23 20:10:18 +00004852#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004853 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4854 __debug_less<_Compare> __c(__comp);
David Majnemercb8757a2014-07-22 06:07:09 +00004855 __sift_up<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004856#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004857 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
David Majnemercb8757a2014-07-22 06:07:09 +00004858 __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004859#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004860}
4861
4862template <class _RandomAccessIterator>
4863inline _LIBCPP_INLINE_VISIBILITY
4864void
4865push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4866{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004867 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004868}
4869
4870// pop_heap
4871
4872template <class _Compare, class _RandomAccessIterator>
David Majnemercb8757a2014-07-22 06:07:09 +00004873void
4874__sift_down(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4875 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4876 _RandomAccessIterator __start)
4877{
4878 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4879 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4880 // left-child of __start is at 2 * __start + 1
4881 // right-child of __start is at 2 * __start + 2
4882 difference_type __child = __start - __first;
4883
4884 if (__len < 2 || (__len - 2) / 2 < __child)
4885 return;
4886
4887 __child = 2 * __child + 1;
4888 _RandomAccessIterator __child_i = __first + __child;
4889
4890 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4891 // right-child exists and is greater than left-child
4892 ++__child_i;
4893 ++__child;
4894 }
4895
4896 // check if we are in heap-order
4897 if (__comp(*__child_i, *__start))
4898 // we are, __start is larger than it's largest child
4899 return;
4900
4901 value_type __top(_VSTD::move(*__start));
4902 do
4903 {
4904 // we are not in heap-order, swap the parent with it's largest child
4905 *__start = _VSTD::move(*__child_i);
4906 __start = __child_i;
4907
4908 if ((__len - 2) / 2 < __child)
4909 break;
4910
4911 // recompute the child based off of the updated parent
4912 __child = 2 * __child + 1;
4913 __child_i = __first + __child;
4914
4915 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4916 // right-child exists and is greater than left-child
4917 ++__child_i;
4918 ++__child;
4919 }
4920
4921 // check if we are in heap-order
4922 } while (!__comp(*__child_i, __top));
4923 *__start = _VSTD::move(__top);
4924}
4925
4926template <class _Compare, class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004927inline _LIBCPP_INLINE_VISIBILITY
4928void
4929__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4930 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4931{
4932 if (__len > 1)
4933 {
4934 swap(*__first, *--__last);
David Majnemercb8757a2014-07-22 06:07:09 +00004935 __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004936 }
4937}
4938
4939template <class _RandomAccessIterator, class _Compare>
4940inline _LIBCPP_INLINE_VISIBILITY
4941void
4942pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4943{
Howard Hinnant5e571422013-08-23 20:10:18 +00004944#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004945 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4946 __debug_less<_Compare> __c(__comp);
4947 __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004948#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004949 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4950 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004951#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004952}
4953
4954template <class _RandomAccessIterator>
4955inline _LIBCPP_INLINE_VISIBILITY
4956void
4957pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4958{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004959 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004960}
4961
4962// make_heap
4963
4964template <class _Compare, class _RandomAccessIterator>
4965void
4966__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4967{
4968 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4969 difference_type __n = __last - __first;
4970 if (__n > 1)
4971 {
David Majnemercb8757a2014-07-22 06:07:09 +00004972 // start from the first parent, there is no need to consider children
4973 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
4974 {
4975 __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
4976 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004977 }
4978}
4979
4980template <class _RandomAccessIterator, class _Compare>
4981inline _LIBCPP_INLINE_VISIBILITY
4982void
4983make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4984{
Howard Hinnant5e571422013-08-23 20:10:18 +00004985#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004986 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4987 __debug_less<_Compare> __c(__comp);
4988 __make_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004989#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004990 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4991 __make_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004992#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004993}
4994
4995template <class _RandomAccessIterator>
4996inline _LIBCPP_INLINE_VISIBILITY
4997void
4998make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4999{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005000 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005001}
5002
5003// sort_heap
5004
5005template <class _Compare, class _RandomAccessIterator>
5006void
5007__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5008{
5009 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5010 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
5011 __pop_heap<_Compare>(__first, __last, __comp, __n);
5012}
5013
5014template <class _RandomAccessIterator, class _Compare>
5015inline _LIBCPP_INLINE_VISIBILITY
5016void
5017sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5018{
Howard Hinnant5e571422013-08-23 20:10:18 +00005019#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005020 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5021 __debug_less<_Compare> __c(__comp);
5022 __sort_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005023#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005024 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5025 __sort_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005026#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005027}
5028
5029template <class _RandomAccessIterator>
5030inline _LIBCPP_INLINE_VISIBILITY
5031void
5032sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5033{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005034 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005035}
5036
5037// partial_sort
5038
5039template <class _Compare, class _RandomAccessIterator>
5040void
5041__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5042 _Compare __comp)
5043{
5044 __make_heap<_Compare>(__first, __middle, __comp);
5045 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5046 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5047 {
5048 if (__comp(*__i, *__first))
5049 {
5050 swap(*__i, *__first);
David Majnemercb8757a2014-07-22 06:07:09 +00005051 __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005052 }
5053 }
5054 __sort_heap<_Compare>(__first, __middle, __comp);
5055}
5056
5057template <class _RandomAccessIterator, class _Compare>
5058inline _LIBCPP_INLINE_VISIBILITY
5059void
5060partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5061 _Compare __comp)
5062{
Howard Hinnant5e571422013-08-23 20:10:18 +00005063#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005064 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5065 __debug_less<_Compare> __c(__comp);
5066 __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005067#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005068 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5069 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005070#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005071}
5072
5073template <class _RandomAccessIterator>
5074inline _LIBCPP_INLINE_VISIBILITY
5075void
5076partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5077{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005078 _VSTD::partial_sort(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005079 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5080}
5081
5082// partial_sort_copy
5083
5084template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5085_RandomAccessIterator
5086__partial_sort_copy(_InputIterator __first, _InputIterator __last,
5087 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5088{
5089 _RandomAccessIterator __r = __result_first;
5090 if (__r != __result_last)
5091 {
Eric Fiselierb9919752014-10-27 19:28:20 +00005092 for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005093 *__r = *__first;
5094 __make_heap<_Compare>(__result_first, __r, __comp);
David Majnemercb8757a2014-07-22 06:07:09 +00005095 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005096 for (; __first != __last; ++__first)
5097 if (__comp(*__first, *__result_first))
5098 {
5099 *__result_first = *__first;
David Majnemercb8757a2014-07-22 06:07:09 +00005100 __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005101 }
5102 __sort_heap<_Compare>(__result_first, __r, __comp);
5103 }
5104 return __r;
5105}
5106
5107template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5108inline _LIBCPP_INLINE_VISIBILITY
5109_RandomAccessIterator
5110partial_sort_copy(_InputIterator __first, _InputIterator __last,
5111 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5112{
Howard Hinnant5e571422013-08-23 20:10:18 +00005113#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005114 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5115 __debug_less<_Compare> __c(__comp);
5116 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005117#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005118 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5119 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005120#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005121}
5122
5123template <class _InputIterator, class _RandomAccessIterator>
5124inline _LIBCPP_INLINE_VISIBILITY
5125_RandomAccessIterator
5126partial_sort_copy(_InputIterator __first, _InputIterator __last,
5127 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5128{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005129 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005130 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5131}
5132
5133// nth_element
5134
5135template <class _Compare, class _RandomAccessIterator>
5136void
5137__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5138{
5139 // _Compare is known to be a reference type
5140 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5141 const difference_type __limit = 7;
5142 while (true)
5143 {
5144 __restart:
Howard Hinnant8292d742011-12-29 17:45:35 +00005145 if (__nth == __last)
5146 return;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005147 difference_type __len = __last - __first;
5148 switch (__len)
5149 {
5150 case 0:
5151 case 1:
5152 return;
5153 case 2:
5154 if (__comp(*--__last, *__first))
5155 swap(*__first, *__last);
5156 return;
5157 case 3:
5158 {
5159 _RandomAccessIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00005160 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005161 return;
5162 }
5163 }
5164 if (__len <= __limit)
5165 {
5166 __selection_sort<_Compare>(__first, __last, __comp);
5167 return;
5168 }
5169 // __len > __limit >= 3
5170 _RandomAccessIterator __m = __first + __len/2;
5171 _RandomAccessIterator __lm1 = __last;
Howard Hinnant0949eed2011-06-30 21:18:19 +00005172 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005173 // *__m is median
5174 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5175 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5176 _RandomAccessIterator __i = __first;
5177 _RandomAccessIterator __j = __lm1;
5178 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5179 // The search going up is known to be guarded but the search coming down isn't.
5180 // Prime the downward search with a guard.
5181 if (!__comp(*__i, *__m)) // if *__first == *__m
5182 {
5183 // *__first == *__m, *__first doesn't go in first part
5184 // manually guard downward moving __j against __i
5185 while (true)
5186 {
5187 if (__i == --__j)
5188 {
5189 // *__first == *__m, *__m <= all other elements
5190 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5191 ++__i; // __first + 1
5192 __j = __last;
5193 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5194 {
5195 while (true)
5196 {
5197 if (__i == __j)
5198 return; // [__first, __last) all equivalent elements
5199 if (__comp(*__first, *__i))
5200 {
5201 swap(*__i, *__j);
5202 ++__n_swaps;
5203 ++__i;
5204 break;
5205 }
5206 ++__i;
5207 }
5208 }
5209 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5210 if (__i == __j)
5211 return;
5212 while (true)
5213 {
5214 while (!__comp(*__first, *__i))
5215 ++__i;
5216 while (__comp(*__first, *--__j))
5217 ;
5218 if (__i >= __j)
5219 break;
5220 swap(*__i, *__j);
5221 ++__n_swaps;
5222 ++__i;
5223 }
5224 // [__first, __i) == *__first and *__first < [__i, __last)
5225 // The first part is sorted,
5226 if (__nth < __i)
5227 return;
5228 // __nth_element the secod part
5229 // __nth_element<_Compare>(__i, __nth, __last, __comp);
5230 __first = __i;
5231 goto __restart;
5232 }
5233 if (__comp(*__j, *__m))
5234 {
5235 swap(*__i, *__j);
5236 ++__n_swaps;
5237 break; // found guard for downward moving __j, now use unguarded partition
5238 }
5239 }
5240 }
5241 ++__i;
5242 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5243 // if not yet partitioned...
5244 if (__i < __j)
5245 {
5246 // known that *(__i - 1) < *__m
5247 while (true)
5248 {
5249 // __m still guards upward moving __i
5250 while (__comp(*__i, *__m))
5251 ++__i;
5252 // It is now known that a guard exists for downward moving __j
5253 while (!__comp(*--__j, *__m))
5254 ;
5255 if (__i >= __j)
5256 break;
5257 swap(*__i, *__j);
5258 ++__n_swaps;
5259 // It is known that __m != __j
5260 // If __m just moved, follow it
5261 if (__m == __i)
5262 __m = __j;
5263 ++__i;
5264 }
5265 }
5266 // [__first, __i) < *__m and *__m <= [__i, __last)
5267 if (__i != __m && __comp(*__m, *__i))
5268 {
5269 swap(*__i, *__m);
5270 ++__n_swaps;
5271 }
5272 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5273 if (__nth == __i)
5274 return;
5275 if (__n_swaps == 0)
5276 {
5277 // We were given a perfectly partitioned sequence. Coincidence?
5278 if (__nth < __i)
5279 {
5280 // Check for [__first, __i) already sorted
5281 __j = __m = __first;
5282 while (++__j != __i)
5283 {
5284 if (__comp(*__j, *__m))
5285 // not yet sorted, so sort
5286 goto not_sorted;
5287 __m = __j;
5288 }
5289 // [__first, __i) sorted
5290 return;
5291 }
5292 else
5293 {
5294 // Check for [__i, __last) already sorted
5295 __j = __m = __i;
5296 while (++__j != __last)
5297 {
5298 if (__comp(*__j, *__m))
5299 // not yet sorted, so sort
5300 goto not_sorted;
5301 __m = __j;
5302 }
5303 // [__i, __last) sorted
5304 return;
5305 }
5306 }
5307not_sorted:
5308 // __nth_element on range containing __nth
5309 if (__nth < __i)
5310 {
5311 // __nth_element<_Compare>(__first, __nth, __i, __comp);
5312 __last = __i;
5313 }
5314 else
5315 {
5316 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5317 __first = ++__i;
5318 }
5319 }
5320}
5321
5322template <class _RandomAccessIterator, class _Compare>
5323inline _LIBCPP_INLINE_VISIBILITY
5324void
5325nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5326{
Howard Hinnant5e571422013-08-23 20:10:18 +00005327#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005328 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5329 __debug_less<_Compare> __c(__comp);
5330 __nth_element<_Comp_ref>(__first, __nth, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005331#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005332 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5333 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005334#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005335}
5336
5337template <class _RandomAccessIterator>
5338inline _LIBCPP_INLINE_VISIBILITY
5339void
5340nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5341{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005342 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005343}
5344
5345// includes
5346
5347template <class _Compare, class _InputIterator1, class _InputIterator2>
5348bool
5349__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5350 _Compare __comp)
5351{
5352 for (; __first2 != __last2; ++__first1)
5353 {
5354 if (__first1 == __last1 || __comp(*__first2, *__first1))
5355 return false;
5356 if (!__comp(*__first1, *__first2))
5357 ++__first2;
5358 }
5359 return true;
5360}
5361
5362template <class _InputIterator1, class _InputIterator2, class _Compare>
5363inline _LIBCPP_INLINE_VISIBILITY
5364bool
5365includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5366 _Compare __comp)
5367{
Howard Hinnant5e571422013-08-23 20:10:18 +00005368#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005369 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5370 __debug_less<_Compare> __c(__comp);
5371 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005372#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005373 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5374 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005375#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005376}
5377
5378template <class _InputIterator1, class _InputIterator2>
5379inline _LIBCPP_INLINE_VISIBILITY
5380bool
5381includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5382{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005383 return _VSTD::includes(__first1, __last1, __first2, __last2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005384 __less<typename iterator_traits<_InputIterator1>::value_type,
5385 typename iterator_traits<_InputIterator2>::value_type>());
5386}
5387
5388// set_union
5389
5390template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5391_OutputIterator
5392__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5393 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5394{
5395 for (; __first1 != __last1; ++__result)
5396 {
5397 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005398 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005399 if (__comp(*__first2, *__first1))
5400 {
5401 *__result = *__first2;
5402 ++__first2;
5403 }
5404 else
5405 {
5406 *__result = *__first1;
5407 if (!__comp(*__first1, *__first2))
5408 ++__first2;
5409 ++__first1;
5410 }
5411 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00005412 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005413}
5414
5415template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5416inline _LIBCPP_INLINE_VISIBILITY
5417_OutputIterator
5418set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5419 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5420{
Howard Hinnant5e571422013-08-23 20:10:18 +00005421#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005422 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5423 __debug_less<_Compare> __c(__comp);
5424 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005425#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005426 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5427 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005428#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005429}
5430
5431template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5432inline _LIBCPP_INLINE_VISIBILITY
5433_OutputIterator
5434set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5435 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5436{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005437 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005438 __less<typename iterator_traits<_InputIterator1>::value_type,
5439 typename iterator_traits<_InputIterator2>::value_type>());
5440}
5441
5442// set_intersection
5443
5444template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5445_OutputIterator
5446__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5447 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5448{
5449 while (__first1 != __last1 && __first2 != __last2)
5450 {
5451 if (__comp(*__first1, *__first2))
5452 ++__first1;
5453 else
5454 {
5455 if (!__comp(*__first2, *__first1))
5456 {
5457 *__result = *__first1;
5458 ++__result;
5459 ++__first1;
5460 }
5461 ++__first2;
5462 }
5463 }
5464 return __result;
5465}
5466
5467template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5468inline _LIBCPP_INLINE_VISIBILITY
5469_OutputIterator
5470set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5471 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5472{
Howard Hinnant5e571422013-08-23 20:10:18 +00005473#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005474 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5475 __debug_less<_Compare> __c(__comp);
5476 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005477#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005478 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5479 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005480#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005481}
5482
5483template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5484inline _LIBCPP_INLINE_VISIBILITY
5485_OutputIterator
5486set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5487 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5488{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005489 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005490 __less<typename iterator_traits<_InputIterator1>::value_type,
5491 typename iterator_traits<_InputIterator2>::value_type>());
5492}
5493
5494// set_difference
5495
5496template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5497_OutputIterator
5498__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5499 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5500{
5501 while (__first1 != __last1)
5502 {
5503 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005504 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005505 if (__comp(*__first1, *__first2))
5506 {
5507 *__result = *__first1;
5508 ++__result;
5509 ++__first1;
5510 }
5511 else
5512 {
5513 if (!__comp(*__first2, *__first1))
5514 ++__first1;
5515 ++__first2;
5516 }
5517 }
5518 return __result;
5519}
5520
5521template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5522inline _LIBCPP_INLINE_VISIBILITY
5523_OutputIterator
5524set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5525 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5526{
Howard Hinnant5e571422013-08-23 20:10:18 +00005527#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005528 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5529 __debug_less<_Compare> __c(__comp);
5530 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005531#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005532 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5533 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005534#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005535}
5536
5537template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5538inline _LIBCPP_INLINE_VISIBILITY
5539_OutputIterator
5540set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5541 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5542{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005543 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005544 __less<typename iterator_traits<_InputIterator1>::value_type,
5545 typename iterator_traits<_InputIterator2>::value_type>());
5546}
5547
5548// set_symmetric_difference
5549
5550template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5551_OutputIterator
5552__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5553 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5554{
5555 while (__first1 != __last1)
5556 {
5557 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005558 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005559 if (__comp(*__first1, *__first2))
5560 {
5561 *__result = *__first1;
5562 ++__result;
5563 ++__first1;
5564 }
5565 else
5566 {
5567 if (__comp(*__first2, *__first1))
5568 {
5569 *__result = *__first2;
5570 ++__result;
5571 }
5572 else
5573 ++__first1;
5574 ++__first2;
5575 }
5576 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00005577 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005578}
5579
5580template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5581inline _LIBCPP_INLINE_VISIBILITY
5582_OutputIterator
5583set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5584 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5585{
Howard Hinnant5e571422013-08-23 20:10:18 +00005586#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005587 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5588 __debug_less<_Compare> __c(__comp);
5589 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005590#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005591 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5592 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005593#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005594}
5595
5596template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5597inline _LIBCPP_INLINE_VISIBILITY
5598_OutputIterator
5599set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5600 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5601{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005602 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005603 __less<typename iterator_traits<_InputIterator1>::value_type,
5604 typename iterator_traits<_InputIterator2>::value_type>());
5605}
5606
5607// lexicographical_compare
5608
5609template <class _Compare, class _InputIterator1, class _InputIterator2>
5610bool
5611__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5612 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5613{
Eric Fiselierb9919752014-10-27 19:28:20 +00005614 for (; __first2 != __last2; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005615 {
5616 if (__first1 == __last1 || __comp(*__first1, *__first2))
5617 return true;
5618 if (__comp(*__first2, *__first1))
5619 return false;
5620 }
5621 return false;
5622}
5623
5624template <class _InputIterator1, class _InputIterator2, class _Compare>
5625inline _LIBCPP_INLINE_VISIBILITY
5626bool
5627lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5628 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5629{
Howard Hinnant5e571422013-08-23 20:10:18 +00005630#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005631 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5632 __debug_less<_Compare> __c(__comp);
5633 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005634#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005635 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5636 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005637#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005638}
5639
5640template <class _InputIterator1, class _InputIterator2>
5641inline _LIBCPP_INLINE_VISIBILITY
5642bool
5643lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5644 _InputIterator2 __first2, _InputIterator2 __last2)
5645{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005646 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005647 __less<typename iterator_traits<_InputIterator1>::value_type,
5648 typename iterator_traits<_InputIterator2>::value_type>());
5649}
5650
5651// next_permutation
5652
5653template <class _Compare, class _BidirectionalIterator>
5654bool
5655__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5656{
5657 _BidirectionalIterator __i = __last;
5658 if (__first == __last || __first == --__i)
5659 return false;
5660 while (true)
5661 {
5662 _BidirectionalIterator __ip1 = __i;
5663 if (__comp(*--__i, *__ip1))
5664 {
5665 _BidirectionalIterator __j = __last;
5666 while (!__comp(*__i, *--__j))
5667 ;
5668 swap(*__i, *__j);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005669 _VSTD::reverse(__ip1, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005670 return true;
5671 }
5672 if (__i == __first)
5673 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005674 _VSTD::reverse(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005675 return false;
5676 }
5677 }
5678}
5679
5680template <class _BidirectionalIterator, class _Compare>
5681inline _LIBCPP_INLINE_VISIBILITY
5682bool
5683next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5684{
Howard Hinnant5e571422013-08-23 20:10:18 +00005685#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005686 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5687 __debug_less<_Compare> __c(__comp);
5688 return __next_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005689#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005690 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5691 return __next_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005692#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005693}
5694
5695template <class _BidirectionalIterator>
5696inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005697bool
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005698next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5699{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005700 return _VSTD::next_permutation(__first, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005701 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5702}
5703
5704// prev_permutation
5705
5706template <class _Compare, class _BidirectionalIterator>
5707bool
5708__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5709{
5710 _BidirectionalIterator __i = __last;
5711 if (__first == __last || __first == --__i)
5712 return false;
5713 while (true)
5714 {
5715 _BidirectionalIterator __ip1 = __i;
5716 if (__comp(*__ip1, *--__i))
5717 {
5718 _BidirectionalIterator __j = __last;
5719 while (!__comp(*--__j, *__i))
5720 ;
5721 swap(*__i, *__j);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005722 _VSTD::reverse(__ip1, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005723 return true;
5724 }
5725 if (__i == __first)
5726 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005727 _VSTD::reverse(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005728 return false;
5729 }
5730 }
5731}
5732
5733template <class _BidirectionalIterator, class _Compare>
5734inline _LIBCPP_INLINE_VISIBILITY
5735bool
5736prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5737{
Howard Hinnant5e571422013-08-23 20:10:18 +00005738#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005739 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5740 __debug_less<_Compare> __c(__comp);
5741 return __prev_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005742#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005743 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5744 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005745#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005746}
5747
5748template <class _BidirectionalIterator>
5749inline _LIBCPP_INLINE_VISIBILITY
5750bool
5751prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5752{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005753 return _VSTD::prev_permutation(__first, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005754 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5755}
5756
5757template <class _Tp>
5758inline _LIBCPP_INLINE_VISIBILITY
5759typename enable_if
5760<
5761 is_integral<_Tp>::value,
5762 _Tp
5763>::type
5764__rotate_left(_Tp __t, _Tp __n = 1)
5765{
5766 const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5767 __n &= __bits;
5768 return static_cast<_Tp>((__t << __n) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> (__bits - __n)));
5769}
5770
5771template <class _Tp>
5772inline _LIBCPP_INLINE_VISIBILITY
5773typename enable_if
5774<
5775 is_integral<_Tp>::value,
5776 _Tp
5777>::type
5778__rotate_right(_Tp __t, _Tp __n = 1)
5779{
5780 const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1);
5781 __n &= __bits;
5782 return static_cast<_Tp>((__t << (__bits - __n)) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> __n));
5783}
5784
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005785_LIBCPP_END_NAMESPACE_STD
5786
5787#endif // _LIBCPP_ALGORITHM