blob: c527860569b988e1b3c3c4b6ae3e4ae224320d86 [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
Marshall Clow10411c12017-05-25 02:29:54 +000038template<class InputIterator, class Size, class Function>
39 InputIterator for_each_n(InputIterator first, Size n, Function f); // C++17
40
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000041template <class InputIterator, class T>
42 InputIterator
43 find(InputIterator first, InputIterator last, const T& value);
44
45template <class InputIterator, class Predicate>
46 InputIterator
47 find_if(InputIterator first, InputIterator last, Predicate pred);
48
49template<class InputIterator, class Predicate>
50 InputIterator
51 find_if_not(InputIterator first, InputIterator last, Predicate pred);
52
53template <class ForwardIterator1, class ForwardIterator2>
54 ForwardIterator1
55 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
56 ForwardIterator2 first2, ForwardIterator2 last2);
57
58template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
59 ForwardIterator1
60 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
61 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
62
63template <class ForwardIterator1, class ForwardIterator2>
64 ForwardIterator1
65 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
66 ForwardIterator2 first2, ForwardIterator2 last2);
67
68template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
69 ForwardIterator1
70 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
71 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
72
73template <class ForwardIterator>
74 ForwardIterator
75 adjacent_find(ForwardIterator first, ForwardIterator last);
76
77template <class ForwardIterator, class BinaryPredicate>
78 ForwardIterator
79 adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
80
81template <class InputIterator, class T>
82 typename iterator_traits<InputIterator>::difference_type
83 count(InputIterator first, InputIterator last, const T& value);
84
85template <class InputIterator, class Predicate>
86 typename iterator_traits<InputIterator>::difference_type
87 count_if(InputIterator first, InputIterator last, Predicate pred);
88
89template <class InputIterator1, class InputIterator2>
90 pair<InputIterator1, InputIterator2>
91 mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
92
Marshall Clowb30abdd2013-05-09 21:14:23 +000093template <class InputIterator1, class InputIterator2>
94 pair<InputIterator1, InputIterator2>
Aditya Kumarfdb4f172016-08-25 11:52:38 +000095 mismatch(InputIterator1 first1, InputIterator1 last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +000096 InputIterator2 first2, InputIterator2 last2); // **C++14**
97
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098template <class InputIterator1, class InputIterator2, class BinaryPredicate>
99 pair<InputIterator1, InputIterator2>
100 mismatch(InputIterator1 first1, InputIterator1 last1,
101 InputIterator2 first2, BinaryPredicate pred);
102
Marshall Clowb30abdd2013-05-09 21:14:23 +0000103template <class InputIterator1, class InputIterator2, class BinaryPredicate>
104 pair<InputIterator1, InputIterator2>
105 mismatch(InputIterator1 first1, InputIterator1 last1,
106 InputIterator2 first2, InputIterator2 last2,
107 BinaryPredicate pred); // **C++14**
108
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000109template <class InputIterator1, class InputIterator2>
110 bool
111 equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
112
Marshall Clowb30abdd2013-05-09 21:14:23 +0000113template <class InputIterator1, class InputIterator2>
114 bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +0000115 equal(InputIterator1 first1, InputIterator1 last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +0000116 InputIterator2 first2, InputIterator2 last2); // **C++14**
117
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000118template <class InputIterator1, class InputIterator2, class BinaryPredicate>
119 bool
120 equal(InputIterator1 first1, InputIterator1 last1,
121 InputIterator2 first2, BinaryPredicate pred);
122
Marshall Clowb30abdd2013-05-09 21:14:23 +0000123template <class InputIterator1, class InputIterator2, class BinaryPredicate>
124 bool
125 equal(InputIterator1 first1, InputIterator1 last1,
126 InputIterator2 first2, InputIterator2 last2,
127 BinaryPredicate pred); // **C++14**
128
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129template<class ForwardIterator1, class ForwardIterator2>
130 bool
131 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
132 ForwardIterator2 first2);
133
Marshall Clowb30abdd2013-05-09 21:14:23 +0000134template<class ForwardIterator1, class ForwardIterator2>
135 bool
136 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
137 ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
138
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000139template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
140 bool
141 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
142 ForwardIterator2 first2, BinaryPredicate pred);
143
Marshall Clowb30abdd2013-05-09 21:14:23 +0000144template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
145 bool
146 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
147 ForwardIterator2 first2, ForwardIterator2 last2,
148 BinaryPredicate pred); // **C++14**
149
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000150template <class ForwardIterator1, class ForwardIterator2>
151 ForwardIterator1
152 search(ForwardIterator1 first1, ForwardIterator1 last1,
153 ForwardIterator2 first2, ForwardIterator2 last2);
154
155template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
156 ForwardIterator1
157 search(ForwardIterator1 first1, ForwardIterator1 last1,
158 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
159
160template <class ForwardIterator, class Size, class T>
161 ForwardIterator
162 search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
163
164template <class ForwardIterator, class Size, class T, class BinaryPredicate>
165 ForwardIterator
166 search_n(ForwardIterator first, ForwardIterator last,
167 Size count, const T& value, BinaryPredicate pred);
168
169template <class InputIterator, class OutputIterator>
170 OutputIterator
171 copy(InputIterator first, InputIterator last, OutputIterator result);
172
173template<class InputIterator, class OutputIterator, class Predicate>
174 OutputIterator
175 copy_if(InputIterator first, InputIterator last,
176 OutputIterator result, Predicate pred);
177
178template<class InputIterator, class Size, class OutputIterator>
179 OutputIterator
180 copy_n(InputIterator first, Size n, OutputIterator result);
181
182template <class BidirectionalIterator1, class BidirectionalIterator2>
183 BidirectionalIterator2
184 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
185 BidirectionalIterator2 result);
186
187template <class ForwardIterator1, class ForwardIterator2>
188 ForwardIterator2
189 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
190
191template <class ForwardIterator1, class ForwardIterator2>
192 void
193 iter_swap(ForwardIterator1 a, ForwardIterator2 b);
194
195template <class InputIterator, class OutputIterator, class UnaryOperation>
196 OutputIterator
197 transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
198
199template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
200 OutputIterator
201 transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
202 OutputIterator result, BinaryOperation binary_op);
203
204template <class ForwardIterator, class T>
205 void
206 replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
207
208template <class ForwardIterator, class Predicate, class T>
209 void
210 replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
211
212template <class InputIterator, class OutputIterator, class T>
213 OutputIterator
214 replace_copy(InputIterator first, InputIterator last, OutputIterator result,
215 const T& old_value, const T& new_value);
216
217template <class InputIterator, class OutputIterator, class Predicate, class T>
218 OutputIterator
219 replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
220
221template <class ForwardIterator, class T>
222 void
223 fill(ForwardIterator first, ForwardIterator last, const T& value);
224
225template <class OutputIterator, class Size, class T>
226 OutputIterator
227 fill_n(OutputIterator first, Size n, const T& value);
228
229template <class ForwardIterator, class Generator>
230 void
231 generate(ForwardIterator first, ForwardIterator last, Generator gen);
232
233template <class OutputIterator, class Size, class Generator>
234 OutputIterator
235 generate_n(OutputIterator first, Size n, Generator gen);
236
237template <class ForwardIterator, class T>
238 ForwardIterator
239 remove(ForwardIterator first, ForwardIterator last, const T& value);
240
241template <class ForwardIterator, class Predicate>
242 ForwardIterator
243 remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
244
245template <class InputIterator, class OutputIterator, class T>
246 OutputIterator
247 remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
248
249template <class InputIterator, class OutputIterator, class Predicate>
250 OutputIterator
251 remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
252
253template <class ForwardIterator>
254 ForwardIterator
255 unique(ForwardIterator first, ForwardIterator last);
256
257template <class ForwardIterator, class BinaryPredicate>
258 ForwardIterator
259 unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
260
261template <class InputIterator, class OutputIterator>
262 OutputIterator
263 unique_copy(InputIterator first, InputIterator last, OutputIterator result);
264
265template <class InputIterator, class OutputIterator, class BinaryPredicate>
266 OutputIterator
267 unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
268
269template <class BidirectionalIterator>
270 void
271 reverse(BidirectionalIterator first, BidirectionalIterator last);
272
273template <class BidirectionalIterator, class OutputIterator>
274 OutputIterator
275 reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
276
277template <class ForwardIterator>
278 ForwardIterator
279 rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
280
281template <class ForwardIterator, class OutputIterator>
282 OutputIterator
283 rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
284
285template <class RandomAccessIterator>
286 void
Marshall Clow03b862f2017-03-23 13:43:37 +0000287 random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14, removed in C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000288
289template <class RandomAccessIterator, class RandomNumberGenerator>
290 void
Marshall Clow3fef95b2014-03-03 06:14:19 +0000291 random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
Marshall Clow03b862f2017-03-23 13:43:37 +0000292 RandomNumberGenerator& rand); // deprecated in C++14, removed in C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000293
Eric Fiselier917af0a2016-08-28 22:14:37 +0000294template<class PopulationIterator, class SampleIterator,
295 class Distance, class UniformRandomBitGenerator>
296 SampleIterator sample(PopulationIterator first, PopulationIterator last,
297 SampleIterator out, Distance n,
298 UniformRandomBitGenerator&& g); // C++17
299
Howard Hinnantc3267212010-05-26 17:49:34 +0000300template<class RandomAccessIterator, class UniformRandomNumberGenerator>
301 void shuffle(RandomAccessIterator first, RandomAccessIterator last,
Howard Hinnant278bf2d2010-11-18 01:47:02 +0000302 UniformRandomNumberGenerator&& g);
Howard Hinnantc3267212010-05-26 17:49:34 +0000303
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000304template <class InputIterator, class Predicate>
305 bool
306 is_partitioned(InputIterator first, InputIterator last, Predicate pred);
307
308template <class ForwardIterator, class Predicate>
309 ForwardIterator
310 partition(ForwardIterator first, ForwardIterator last, Predicate pred);
311
312template <class InputIterator, class OutputIterator1,
313 class OutputIterator2, class Predicate>
314 pair<OutputIterator1, OutputIterator2>
315 partition_copy(InputIterator first, InputIterator last,
316 OutputIterator1 out_true, OutputIterator2 out_false,
317 Predicate pred);
318
319template <class ForwardIterator, class Predicate>
320 ForwardIterator
321 stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
322
323template<class ForwardIterator, class Predicate>
324 ForwardIterator
325 partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
326
327template <class ForwardIterator>
328 bool
329 is_sorted(ForwardIterator first, ForwardIterator last);
330
331template <class ForwardIterator, class Compare>
332 bool
333 is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
334
335template<class ForwardIterator>
336 ForwardIterator
337 is_sorted_until(ForwardIterator first, ForwardIterator last);
338
339template <class ForwardIterator, class Compare>
340 ForwardIterator
341 is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
342
343template <class RandomAccessIterator>
344 void
345 sort(RandomAccessIterator first, RandomAccessIterator last);
346
347template <class RandomAccessIterator, class Compare>
348 void
349 sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
350
351template <class RandomAccessIterator>
352 void
353 stable_sort(RandomAccessIterator first, RandomAccessIterator last);
354
355template <class RandomAccessIterator, class Compare>
356 void
357 stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
358
359template <class RandomAccessIterator>
360 void
361 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
362
363template <class RandomAccessIterator, class Compare>
364 void
365 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
366
367template <class InputIterator, class RandomAccessIterator>
368 RandomAccessIterator
369 partial_sort_copy(InputIterator first, InputIterator last,
370 RandomAccessIterator result_first, RandomAccessIterator result_last);
371
372template <class InputIterator, class RandomAccessIterator, class Compare>
373 RandomAccessIterator
374 partial_sort_copy(InputIterator first, InputIterator last,
375 RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
376
377template <class RandomAccessIterator>
378 void
379 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
380
381template <class RandomAccessIterator, class Compare>
382 void
383 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
384
385template <class ForwardIterator, class T>
386 ForwardIterator
387 lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
388
389template <class ForwardIterator, class T, class Compare>
390 ForwardIterator
391 lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
392
393template <class ForwardIterator, class T>
394 ForwardIterator
395 upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
396
397template <class ForwardIterator, class T, class Compare>
398 ForwardIterator
399 upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
400
401template <class ForwardIterator, class T>
402 pair<ForwardIterator, ForwardIterator>
403 equal_range(ForwardIterator first, ForwardIterator last, const T& value);
404
405template <class ForwardIterator, class T, class Compare>
406 pair<ForwardIterator, ForwardIterator>
407 equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
408
409template <class ForwardIterator, class T>
410 bool
411 binary_search(ForwardIterator first, ForwardIterator last, const T& value);
412
413template <class ForwardIterator, class T, class Compare>
414 bool
415 binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
416
417template <class InputIterator1, class InputIterator2, class OutputIterator>
418 OutputIterator
419 merge(InputIterator1 first1, InputIterator1 last1,
420 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
421
422template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
423 OutputIterator
424 merge(InputIterator1 first1, InputIterator1 last1,
425 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
426
427template <class BidirectionalIterator>
428 void
429 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
430
431template <class BidirectionalIterator, class Compare>
432 void
433 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
434
435template <class InputIterator1, class InputIterator2>
436 bool
437 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
438
439template <class InputIterator1, class InputIterator2, class Compare>
440 bool
441 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
442
443template <class InputIterator1, class InputIterator2, class OutputIterator>
444 OutputIterator
445 set_union(InputIterator1 first1, InputIterator1 last1,
446 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
447
448template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
449 OutputIterator
450 set_union(InputIterator1 first1, InputIterator1 last1,
451 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
452
453template <class InputIterator1, class InputIterator2, class OutputIterator>
454 OutputIterator
455 set_intersection(InputIterator1 first1, InputIterator1 last1,
456 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
457
458template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
459 OutputIterator
460 set_intersection(InputIterator1 first1, InputIterator1 last1,
461 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
462
463template <class InputIterator1, class InputIterator2, class OutputIterator>
464 OutputIterator
465 set_difference(InputIterator1 first1, InputIterator1 last1,
466 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
467
468template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
469 OutputIterator
470 set_difference(InputIterator1 first1, InputIterator1 last1,
471 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
472
473template <class InputIterator1, class InputIterator2, class OutputIterator>
474 OutputIterator
475 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
476 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
477
478template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
479 OutputIterator
480 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
481 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
482
483template <class RandomAccessIterator>
484 void
485 push_heap(RandomAccessIterator first, RandomAccessIterator last);
486
487template <class RandomAccessIterator, class Compare>
488 void
489 push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
490
491template <class RandomAccessIterator>
492 void
493 pop_heap(RandomAccessIterator first, RandomAccessIterator last);
494
495template <class RandomAccessIterator, class Compare>
496 void
497 pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
498
499template <class RandomAccessIterator>
500 void
501 make_heap(RandomAccessIterator first, RandomAccessIterator last);
502
503template <class RandomAccessIterator, class Compare>
504 void
505 make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
506
507template <class RandomAccessIterator>
508 void
509 sort_heap(RandomAccessIterator first, RandomAccessIterator last);
510
511template <class RandomAccessIterator, class Compare>
512 void
513 sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
514
Howard Hinnant324bb032010-08-22 00:02:43 +0000515template <class RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000516 bool
Howard Hinnant324bb032010-08-22 00:02:43 +0000517 is_heap(RandomAccessIterator first, RandomAccessiterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000518
Howard Hinnant324bb032010-08-22 00:02:43 +0000519template <class RandomAccessIterator, class Compare>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000520 bool
Howard Hinnant324bb032010-08-22 00:02:43 +0000521 is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000522
Howard Hinnant324bb032010-08-22 00:02:43 +0000523template <class RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000524 RandomAccessIterator
Howard Hinnant324bb032010-08-22 00:02:43 +0000525 is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000526
Howard Hinnant324bb032010-08-22 00:02:43 +0000527template <class RandomAccessIterator, class Compare>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000528 RandomAccessIterator
Howard Hinnant324bb032010-08-22 00:02:43 +0000529 is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000530
Howard Hinnant98e5d972010-08-21 20:10:01 +0000531template <class ForwardIterator>
532 ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +0000533 min_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000534
535template <class ForwardIterator, class Compare>
536 ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +0000537 min_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000538
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000539template <class T>
540 const T&
Marshall Clow9d9463a2014-02-19 16:51:35 +0000541 min(const T& a, const T& b); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000542
543template <class T, class Compare>
544 const T&
Marshall Clow9d9463a2014-02-19 16:51:35 +0000545 min(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000546
Howard Hinnant98e5d972010-08-21 20:10:01 +0000547template<class T>
548 T
Marshall Clow9d9463a2014-02-19 16:51:35 +0000549 min(initializer_list<T> t); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000550
551template<class T, class Compare>
552 T
Marshall Clow9d9463a2014-02-19 16:51:35 +0000553 min(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000554
Marshall Clow3e0808e2016-03-07 22:43:49 +0000555template<class T>
556 constexpr const T& clamp( const T& v, const T& lo, const T& hi ); // C++17
557
558template<class T, class Compare>
559 constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp ); // C++17
560
Howard Hinnant98e5d972010-08-21 20:10:01 +0000561template <class ForwardIterator>
562 ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +0000563 max_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000564
565template <class ForwardIterator, class Compare>
566 ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +0000567 max_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000568
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000569template <class T>
570 const T&
Marshall Clow9d9463a2014-02-19 16:51:35 +0000571 max(const T& a, const T& b); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000572
573template <class T, class Compare>
574 const T&
Marshall Clow9d9463a2014-02-19 16:51:35 +0000575 max(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000576
Howard Hinnant98e5d972010-08-21 20:10:01 +0000577template<class T>
578 T
Marshall Clow9d9463a2014-02-19 16:51:35 +0000579 max(initializer_list<T> t); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000580
Howard Hinnant98e5d972010-08-21 20:10:01 +0000581template<class T, class Compare>
582 T
Marshall Clow9d9463a2014-02-19 16:51:35 +0000583 max(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000584
Howard Hinnant98e5d972010-08-21 20:10:01 +0000585template<class ForwardIterator>
586 pair<ForwardIterator, ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +0000587 minmax_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000588
Howard Hinnant98e5d972010-08-21 20:10:01 +0000589template<class ForwardIterator, class Compare>
590 pair<ForwardIterator, ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +0000591 minmax_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000592
593template<class T>
594 pair<const T&, const T&>
Marshall Clow9d9463a2014-02-19 16:51:35 +0000595 minmax(const T& a, const T& b); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000596
597template<class T, class Compare>
598 pair<const T&, const T&>
Marshall Clow9d9463a2014-02-19 16:51:35 +0000599 minmax(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000600
601template<class T>
602 pair<T, T>
Marshall Clow9d9463a2014-02-19 16:51:35 +0000603 minmax(initializer_list<T> t); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000604
605template<class T, class Compare>
606 pair<T, T>
Marshall Clow9d9463a2014-02-19 16:51:35 +0000607 minmax(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000608
609template <class InputIterator1, class InputIterator2>
610 bool
611 lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
612
613template <class InputIterator1, class InputIterator2, class Compare>
614 bool
615 lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
616 InputIterator2 first2, InputIterator2 last2, Compare comp);
617
618template <class BidirectionalIterator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000619 bool
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000620 next_permutation(BidirectionalIterator first, BidirectionalIterator last);
621
622template <class BidirectionalIterator, class Compare>
623 bool
624 next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
625
626template <class BidirectionalIterator>
627 bool
628 prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
629
630template <class BidirectionalIterator, class Compare>
631 bool
632 prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
633
634} // std
635
636*/
637
638#include <__config>
639#include <initializer_list>
640#include <type_traits>
641#include <cstring>
Eric Fiselier8f1e73d2016-04-21 23:38:59 +0000642#include <utility> // needed to provide swap_ranges.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000643#include <memory>
Marshall Clow33406332018-01-08 19:18:00 +0000644#include <functional>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000645#include <iterator>
Howard Hinnantca8eb832012-07-26 17:09:09 +0000646#include <cstddef>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000647
Howard Hinnant7f764502013-08-14 18:00:20 +0000648#if defined(__IBMCPP__)
649#include "support/ibm/support.h"
650#endif
Eric Fiselier41af64a2017-05-10 20:57:45 +0000651#if defined(_LIBCPP_COMPILER_MSVC)
Eric Fiselierca91fd02017-05-10 21:30:04 +0000652#include <intrin.h>
Howard Hinnantef5aa932013-09-17 01:34:47 +0000653#endif
Howard Hinnant7f764502013-08-14 18:00:20 +0000654
Eric Fiselierb9536102014-08-10 23:53:08 +0000655#include <__debug>
656
Howard Hinnant08e17472011-10-17 20:05:10 +0000657#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000658#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000659#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000660
Eric Fiselier018a3d52017-05-31 22:07:49 +0000661_LIBCPP_PUSH_MACROS
662#include <__undef_macros>
663
664
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000665_LIBCPP_BEGIN_NAMESPACE_STD
666
Marshall Clow9d9463a2014-02-19 16:51:35 +0000667// I'd like to replace these with _VSTD::equal_to<void>, but can't because:
668// * That only works with C++14 and later, and
669// * We haven't included <functional> here.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000670template <class _T1, class _T2 = _T1>
671struct __equal_to
672{
673 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
674 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
675 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
676 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
677};
678
679template <class _T1>
680struct __equal_to<_T1, _T1>
681{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000682 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
683 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000684};
685
686template <class _T1>
687struct __equal_to<const _T1, _T1>
688{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000689 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
690 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000691};
692
693template <class _T1>
694struct __equal_to<_T1, const _T1>
695{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000696 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
697 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000698};
699
700template <class _T1, class _T2 = _T1>
701struct __less
702{
Aditya Kumarfdb4f172016-08-25 11:52:38 +0000703 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow9d9463a2014-02-19 16:51:35 +0000704 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
705
706 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
707 bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
708
709 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
710 bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
711
712 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
713 bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000714};
715
716template <class _T1>
717struct __less<_T1, _T1>
718{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000719 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
720 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000721};
722
723template <class _T1>
724struct __less<const _T1, _T1>
725{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000726 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
727 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000728};
729
730template <class _T1>
731struct __less<_T1, const _T1>
732{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000733 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
734 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000735};
736
737template <class _Predicate>
Marshall Clow25a78dc2017-08-28 23:16:13 +0000738class __invert // invert the sense of a comparison
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000739{
740private:
741 _Predicate __p_;
742public:
Marshall Clow25a78dc2017-08-28 23:16:13 +0000743 _LIBCPP_INLINE_VISIBILITY __invert() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000744
745 _LIBCPP_INLINE_VISIBILITY
Marshall Clow25a78dc2017-08-28 23:16:13 +0000746 explicit __invert(_Predicate __p) : __p_(__p) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000747
748 template <class _T1>
749 _LIBCPP_INLINE_VISIBILITY
750 bool operator()(const _T1& __x) {return !__p_(__x);}
751
752 template <class _T1, class _T2>
753 _LIBCPP_INLINE_VISIBILITY
Marshall Clow25a78dc2017-08-28 23:16:13 +0000754 bool operator()(const _T1& __x, const _T2& __y) {return __p_(__y, __x);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000755};
756
Howard Hinnant5e571422013-08-23 20:10:18 +0000757#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000758
759template <class _Compare>
760struct __debug_less
761{
762 _Compare __comp_;
763 __debug_less(_Compare& __c) : __comp_(__c) {}
Eric Fiselier99029f12016-07-19 23:27:18 +0000764
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765 template <class _Tp, class _Up>
766 bool operator()(const _Tp& __x, const _Up& __y)
767 {
768 bool __r = __comp_(__x, __y);
769 if (__r)
Eric Fiselier99029f12016-07-19 23:27:18 +0000770 __do_compare_assert(0, __y, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000771 return __r;
772 }
Eric Fiselier99029f12016-07-19 23:27:18 +0000773
774 template <class _LHS, class _RHS>
775 inline _LIBCPP_INLINE_VISIBILITY
776 decltype((void)_VSTD::declval<_Compare&>()(
777 _VSTD::declval<_LHS const&>(), _VSTD::declval<_RHS const&>()))
778 __do_compare_assert(int, _LHS const& __l, _RHS const& __r) {
779 _LIBCPP_ASSERT(!__comp_(__l, __r),
780 "Comparator does not induce a strict weak ordering");
781 }
782
783 template <class _LHS, class _RHS>
784 inline _LIBCPP_INLINE_VISIBILITY
785 void __do_compare_assert(long, _LHS const&, _RHS const&) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000786};
787
Howard Hinnant5e571422013-08-23 20:10:18 +0000788#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000789
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000790// Precondition: __x != 0
Howard Hinnantec3773c2011-12-01 20:21:04 +0000791inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000792unsigned __ctz(unsigned __x) {
793#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000794 return static_cast<unsigned>(__builtin_ctz(__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000795#else
796 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
797 static_assert(sizeof(unsigned long) == 4, "");
798 unsigned long where;
799 // Search from LSB to MSB for first set bit.
800 // Returns zero if no set bit is found.
Shoaib Meenai54b78ff2017-12-05 17:46:23 +0000801 if (_BitScanForward(&where, __x))
Eric Fiselierca91fd02017-05-10 21:30:04 +0000802 return where;
803 return 32;
804#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +0000805}
806
807inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000808unsigned long __ctz(unsigned long __x) {
809#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000810 return static_cast<unsigned long>(__builtin_ctzl(__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000811#else
812 static_assert(sizeof(unsigned long) == sizeof(unsigned), "");
813 return __ctz(static_cast<unsigned>(__x));
814#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +0000815}
816
817inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000818unsigned long long __ctz(unsigned long long __x) {
819#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000820 return static_cast<unsigned long long>(__builtin_ctzll(__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000821#else
822 unsigned long where;
823// Search from LSB to MSB for first set bit.
824// Returns zero if no set bit is found.
825#if defined(_LIBCPP_HAS_BITSCAN64)
826 (defined(_M_AMD64) || defined(__x86_64__))
Shoaib Meenai54b78ff2017-12-05 17:46:23 +0000827 if (_BitScanForward64(&where, __x))
Eric Fiselierca91fd02017-05-10 21:30:04 +0000828 return static_cast<int>(where);
829#else
830 // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
831 // Scan the Low Word.
Shoaib Meenai54b78ff2017-12-05 17:46:23 +0000832 if (_BitScanForward(&where, static_cast<unsigned long>(__x)))
Eric Fiselierca91fd02017-05-10 21:30:04 +0000833 return where;
834 // Scan the High Word.
Shoaib Meenai54b78ff2017-12-05 17:46:23 +0000835 if (_BitScanForward(&where, static_cast<unsigned long>(__x >> 32)))
Eric Fiselierca91fd02017-05-10 21:30:04 +0000836 return where + 32; // Create a bit offset from the LSB.
837#endif
838 return 64;
839#endif // _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000840}
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000841
842// Precondition: __x != 0
Howard Hinnantec3773c2011-12-01 20:21:04 +0000843inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000844unsigned __clz(unsigned __x) {
845#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000846 return static_cast<unsigned>(__builtin_clz(__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000847#else
848 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
849 static_assert(sizeof(unsigned long) == 4, "");
850 unsigned long where;
851 // Search from LSB to MSB for first set bit.
852 // Returns zero if no set bit is found.
Shoaib Meenai54b78ff2017-12-05 17:46:23 +0000853 if (_BitScanReverse(&where, __x))
Eric Fiselierca91fd02017-05-10 21:30:04 +0000854 return 31 - where;
855 return 32; // Undefined Behavior.
856#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +0000857}
858
859inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000860unsigned long __clz(unsigned long __x) {
861#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000862 return static_cast<unsigned long>(__builtin_clzl (__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000863#else
864 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
865 return __clz(static_cast<unsigned>(__x));
866#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +0000867}
868
869inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000870unsigned long long __clz(unsigned long long __x) {
871#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000872 return static_cast<unsigned long long>(__builtin_clzll(__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000873#else
874 unsigned long where;
875// BitScanReverse scans from MSB to LSB for first set bit.
876// Returns 0 if no set bit is found.
877#if defined(_LIBCPP_HAS_BITSCAN64)
Shoaib Meenai54b78ff2017-12-05 17:46:23 +0000878 if (_BitScanReverse64(&where, __x))
Eric Fiselierca91fd02017-05-10 21:30:04 +0000879 return static_cast<int>(63 - where);
880#else
881 // Scan the high 32 bits.
Shoaib Meenai54b78ff2017-12-05 17:46:23 +0000882 if (_BitScanReverse(&where, static_cast<unsigned long>(__x >> 32)))
Eric Fiselierca91fd02017-05-10 21:30:04 +0000883 return 63 - (where + 32); // Create a bit offset from the MSB.
884 // Scan the low 32 bits.
Shoaib Meenai54b78ff2017-12-05 17:46:23 +0000885 if (_BitScanReverse(&where, static_cast<unsigned long>(__x)))
Eric Fiselierca91fd02017-05-10 21:30:04 +0000886 return 63 - where;
887#endif
888 return 64; // Undefined Behavior.
889#endif // _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000890}
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000891
Eric Fiselierca91fd02017-05-10 21:30:04 +0000892inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) {
893#ifndef _LIBCPP_COMPILER_MSVC
894 return __builtin_popcount (__x);
895#else
896 static_assert(sizeof(unsigned) == 4, "");
897 return __popcnt(__x);
898#endif
899}
900
901inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) {
902#ifndef _LIBCPP_COMPILER_MSVC
903 return __builtin_popcountl (__x);
904#else
905 static_assert(sizeof(unsigned long) == 4, "");
906 return __popcnt(__x);
907#endif
908}
909
910inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {
911#ifndef _LIBCPP_COMPILER_MSVC
912 return __builtin_popcountll(__x);
913#else
914 static_assert(sizeof(unsigned long long) == 8, "");
915 return __popcnt64(__x);
916#endif
917}
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000918
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000919// all_of
920
921template <class _InputIterator, class _Predicate>
922inline _LIBCPP_INLINE_VISIBILITY
923bool
924all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
925{
926 for (; __first != __last; ++__first)
927 if (!__pred(*__first))
928 return false;
929 return true;
930}
931
932// any_of
933
934template <class _InputIterator, class _Predicate>
935inline _LIBCPP_INLINE_VISIBILITY
936bool
937any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
938{
939 for (; __first != __last; ++__first)
940 if (__pred(*__first))
941 return true;
942 return false;
943}
944
945// none_of
946
947template <class _InputIterator, class _Predicate>
948inline _LIBCPP_INLINE_VISIBILITY
949bool
950none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
951{
952 for (; __first != __last; ++__first)
953 if (__pred(*__first))
954 return false;
955 return true;
956}
957
958// for_each
959
960template <class _InputIterator, class _Function>
961inline _LIBCPP_INLINE_VISIBILITY
962_Function
963for_each(_InputIterator __first, _InputIterator __last, _Function __f)
964{
965 for (; __first != __last; ++__first)
966 __f(*__first);
Marshall Clowdb7fa112016-11-14 18:22:19 +0000967 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000968}
969
Marshall Clowefae8792017-05-25 13:40:57 +0000970#if _LIBCPP_STD_VER > 14
Marshall Clow10411c12017-05-25 02:29:54 +0000971// for_each_n
972
973template <class _InputIterator, class _Size, class _Function>
974inline _LIBCPP_INLINE_VISIBILITY
975_InputIterator
976for_each_n(_InputIterator __first, _Size __orig_n, _Function __f)
977{
978 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
979 _IntegralSize __n = __orig_n;
980 while (__n > 0)
981 {
982 __f(*__first);
983 ++__first;
984 --__n;
985 }
986 return __first;
987}
Marshall Clowefae8792017-05-25 13:40:57 +0000988#endif
Marshall Clow10411c12017-05-25 02:29:54 +0000989
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990// find
991
992template <class _InputIterator, class _Tp>
993inline _LIBCPP_INLINE_VISIBILITY
994_InputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +0000995find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000996{
997 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +0000998 if (*__first == __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000999 break;
1000 return __first;
1001}
1002
1003// find_if
1004
1005template <class _InputIterator, class _Predicate>
1006inline _LIBCPP_INLINE_VISIBILITY
1007_InputIterator
1008find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1009{
1010 for (; __first != __last; ++__first)
1011 if (__pred(*__first))
1012 break;
1013 return __first;
1014}
1015
1016// find_if_not
1017
1018template<class _InputIterator, class _Predicate>
1019inline _LIBCPP_INLINE_VISIBILITY
1020_InputIterator
1021find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1022{
1023 for (; __first != __last; ++__first)
1024 if (!__pred(*__first))
1025 break;
1026 return __first;
1027}
1028
1029// find_end
1030
1031template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1032_ForwardIterator1
1033__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1034 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
1035 forward_iterator_tag, forward_iterator_tag)
1036{
1037 // modeled after search algorithm
1038 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
1039 if (__first2 == __last2)
1040 return __r;
1041 while (true)
1042 {
1043 while (true)
1044 {
1045 if (__first1 == __last1) // if source exhausted return last correct answer
1046 return __r; // (or __last1 if never found)
1047 if (__pred(*__first1, *__first2))
1048 break;
1049 ++__first1;
1050 }
1051 // *__first1 matches *__first2, now match elements after here
1052 _ForwardIterator1 __m1 = __first1;
1053 _ForwardIterator2 __m2 = __first2;
1054 while (true)
1055 {
1056 if (++__m2 == __last2)
1057 { // Pattern exhaused, record answer and search for another one
1058 __r = __first1;
1059 ++__first1;
1060 break;
1061 }
1062 if (++__m1 == __last1) // Source exhausted, return last answer
1063 return __r;
1064 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
1065 {
1066 ++__first1;
1067 break;
1068 } // else there is a match, check next elements
1069 }
1070 }
1071}
1072
1073template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
1074_BidirectionalIterator1
1075__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
1076 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
1077 bidirectional_iterator_tag, bidirectional_iterator_tag)
1078{
1079 // modeled after search algorithm (in reverse)
1080 if (__first2 == __last2)
1081 return __last1; // Everything matches an empty sequence
1082 _BidirectionalIterator1 __l1 = __last1;
1083 _BidirectionalIterator2 __l2 = __last2;
1084 --__l2;
1085 while (true)
1086 {
1087 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
1088 while (true)
1089 {
1090 if (__first1 == __l1) // return __last1 if no element matches *__first2
1091 return __last1;
1092 if (__pred(*--__l1, *__l2))
1093 break;
1094 }
1095 // *__l1 matches *__l2, now match elements before here
1096 _BidirectionalIterator1 __m1 = __l1;
1097 _BidirectionalIterator2 __m2 = __l2;
1098 while (true)
1099 {
1100 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
1101 return __m1;
1102 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
1103 return __last1;
1104 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
1105 {
1106 break;
1107 } // else there is a match, check next elements
1108 }
1109 }
1110}
1111
1112template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow37025e12014-06-10 18:51:55 +00001113_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001114__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1115 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1116 random_access_iterator_tag, random_access_iterator_tag)
1117{
1118 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1119 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
1120 if (__len2 == 0)
1121 return __last1;
1122 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
1123 if (__len1 < __len2)
1124 return __last1;
1125 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
1126 _RandomAccessIterator1 __l1 = __last1;
1127 _RandomAccessIterator2 __l2 = __last2;
1128 --__l2;
1129 while (true)
1130 {
1131 while (true)
1132 {
1133 if (__s == __l1)
1134 return __last1;
1135 if (__pred(*--__l1, *__l2))
1136 break;
1137 }
1138 _RandomAccessIterator1 __m1 = __l1;
1139 _RandomAccessIterator2 __m2 = __l2;
1140 while (true)
1141 {
1142 if (__m2 == __first2)
1143 return __m1;
1144 // no need to check range on __m1 because __s guarantees we have enough source
1145 if (!__pred(*--__m1, *--__m2))
1146 {
1147 break;
1148 }
1149 }
1150 }
1151}
1152
1153template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1154inline _LIBCPP_INLINE_VISIBILITY
1155_ForwardIterator1
1156find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1157 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1158{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001159 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001160 (__first1, __last1, __first2, __last2, __pred,
1161 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1162 typename iterator_traits<_ForwardIterator2>::iterator_category());
1163}
1164
1165template <class _ForwardIterator1, class _ForwardIterator2>
1166inline _LIBCPP_INLINE_VISIBILITY
1167_ForwardIterator1
1168find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1169 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1170{
1171 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1172 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001173 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001174}
1175
1176// find_first_of
1177
1178template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow37025e12014-06-10 18:51:55 +00001179_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1180__find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001181 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1182{
1183 for (; __first1 != __last1; ++__first1)
1184 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1185 if (__pred(*__first1, *__j))
1186 return __first1;
1187 return __last1;
1188}
1189
Marshall Clow37025e12014-06-10 18:51:55 +00001190
1191template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1192inline _LIBCPP_INLINE_VISIBILITY
1193_ForwardIterator1
1194find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1195 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1196{
1197 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1198}
1199
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001200template <class _ForwardIterator1, class _ForwardIterator2>
1201inline _LIBCPP_INLINE_VISIBILITY
1202_ForwardIterator1
1203find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1204 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1205{
1206 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1207 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Marshall Clow37025e12014-06-10 18:51:55 +00001208 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001209}
1210
1211// adjacent_find
1212
1213template <class _ForwardIterator, class _BinaryPredicate>
1214inline _LIBCPP_INLINE_VISIBILITY
1215_ForwardIterator
1216adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1217{
1218 if (__first != __last)
1219 {
1220 _ForwardIterator __i = __first;
1221 while (++__i != __last)
1222 {
1223 if (__pred(*__first, *__i))
1224 return __first;
1225 __first = __i;
1226 }
1227 }
1228 return __last;
1229}
1230
1231template <class _ForwardIterator>
1232inline _LIBCPP_INLINE_VISIBILITY
1233_ForwardIterator
1234adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1235{
1236 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001237 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001238}
1239
1240// count
1241
1242template <class _InputIterator, class _Tp>
1243inline _LIBCPP_INLINE_VISIBILITY
1244typename iterator_traits<_InputIterator>::difference_type
Howard Hinnant78b68282011-10-22 20:59:45 +00001245count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001246{
1247 typename iterator_traits<_InputIterator>::difference_type __r(0);
1248 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +00001249 if (*__first == __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001250 ++__r;
1251 return __r;
1252}
1253
1254// count_if
1255
1256template <class _InputIterator, class _Predicate>
1257inline _LIBCPP_INLINE_VISIBILITY
1258typename iterator_traits<_InputIterator>::difference_type
1259count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1260{
1261 typename iterator_traits<_InputIterator>::difference_type __r(0);
1262 for (; __first != __last; ++__first)
1263 if (__pred(*__first))
1264 ++__r;
1265 return __r;
1266}
1267
1268// mismatch
1269
1270template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1271inline _LIBCPP_INLINE_VISIBILITY
1272pair<_InputIterator1, _InputIterator2>
1273mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1274 _InputIterator2 __first2, _BinaryPredicate __pred)
1275{
Marshall Clowd402a4d2014-09-16 20:40:05 +00001276 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001277 if (!__pred(*__first1, *__first2))
1278 break;
1279 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1280}
1281
1282template <class _InputIterator1, class _InputIterator2>
1283inline _LIBCPP_INLINE_VISIBILITY
1284pair<_InputIterator1, _InputIterator2>
1285mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1286{
1287 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1288 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001289 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001290}
1291
Marshall Clowb30abdd2013-05-09 21:14:23 +00001292#if _LIBCPP_STD_VER > 11
1293template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1294inline _LIBCPP_INLINE_VISIBILITY
1295pair<_InputIterator1, _InputIterator2>
1296mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1297 _InputIterator2 __first2, _InputIterator2 __last2,
1298 _BinaryPredicate __pred)
1299{
Marshall Clowd402a4d2014-09-16 20:40:05 +00001300 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001301 if (!__pred(*__first1, *__first2))
1302 break;
1303 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1304}
1305
1306template <class _InputIterator1, class _InputIterator2>
1307inline _LIBCPP_INLINE_VISIBILITY
1308pair<_InputIterator1, _InputIterator2>
1309mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1310 _InputIterator2 __first2, _InputIterator2 __last2)
1311{
1312 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1313 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1314 return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1315}
1316#endif
1317
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001318// equal
1319
1320template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1321inline _LIBCPP_INLINE_VISIBILITY
1322bool
1323equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1324{
Eric Fiselierb9919752014-10-27 19:28:20 +00001325 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326 if (!__pred(*__first1, *__first2))
1327 return false;
1328 return true;
1329}
1330
1331template <class _InputIterator1, class _InputIterator2>
1332inline _LIBCPP_INLINE_VISIBILITY
1333bool
1334equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1335{
1336 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1337 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001338 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001339}
1340
Marshall Clowb30abdd2013-05-09 21:14:23 +00001341#if _LIBCPP_STD_VER > 11
1342template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
1343inline _LIBCPP_INLINE_VISIBILITY
1344bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001345__equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001346 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1347 input_iterator_tag, input_iterator_tag )
1348{
Eric Fiselierb9919752014-10-27 19:28:20 +00001349 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001350 if (!__pred(*__first1, *__first2))
1351 return false;
1352 return __first1 == __last1 && __first2 == __last2;
1353}
1354
1355template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1356inline _LIBCPP_INLINE_VISIBILITY
1357bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001358__equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1359 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001360 random_access_iterator_tag, random_access_iterator_tag )
1361{
1362 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1363 return false;
1364 return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1365 typename add_lvalue_reference<_BinaryPredicate>::type>
1366 (__first1, __last1, __first2, __pred );
1367}
1368
1369template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1370inline _LIBCPP_INLINE_VISIBILITY
1371bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001372equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001373 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1374{
1375 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001376 (__first1, __last1, __first2, __last2, __pred,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001377 typename iterator_traits<_InputIterator1>::iterator_category(),
1378 typename iterator_traits<_InputIterator2>::iterator_category());
1379}
1380
1381template <class _InputIterator1, class _InputIterator2>
1382inline _LIBCPP_INLINE_VISIBILITY
1383bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001384equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001385 _InputIterator2 __first2, _InputIterator2 __last2)
1386{
1387 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1388 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1389 return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1390 typename iterator_traits<_InputIterator1>::iterator_category(),
1391 typename iterator_traits<_InputIterator2>::iterator_category());
1392}
1393#endif
1394
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001395// is_permutation
1396
1397template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1398bool
1399is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1400 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1401{
1402 // shorten sequences as much as possible by lopping of any equal parts
Eric Fiselierb9919752014-10-27 19:28:20 +00001403 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001404 if (!__pred(*__first1, *__first2))
1405 goto __not_done;
1406 return true;
1407__not_done:
1408 // __first1 != __last1 && *__first1 != *__first2
1409 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001410 _D1 __l1 = _VSTD::distance(__first1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001411 if (__l1 == _D1(1))
1412 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001413 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001414 // For each element in [f1, l1) see if there are the same number of
1415 // equal elements in [f2, l2)
1416 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1417 {
1418 // Have we already counted the number of *__i in [f1, l1)?
1419 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1420 if (__pred(*__j, *__i))
1421 goto __next_iter;
1422 {
1423 // Count number of *__i in [f2, l2)
1424 _D1 __c2 = 0;
1425 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1426 if (__pred(*__i, *__j))
1427 ++__c2;
1428 if (__c2 == 0)
1429 return false;
1430 // Count number of *__i in [__i, l1) (we can start with 1)
1431 _D1 __c1 = 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001432 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001433 if (__pred(*__i, *__j))
1434 ++__c1;
1435 if (__c1 != __c2)
1436 return false;
1437 }
1438__next_iter:;
1439 }
1440 return true;
1441}
1442
1443template<class _ForwardIterator1, class _ForwardIterator2>
1444inline _LIBCPP_INLINE_VISIBILITY
1445bool
1446is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1447 _ForwardIterator2 __first2)
1448{
1449 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1450 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001451 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001452}
1453
Marshall Clowb30abdd2013-05-09 21:14:23 +00001454#if _LIBCPP_STD_VER > 11
1455template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1456bool
1457__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001458 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001459 _BinaryPredicate __pred,
1460 forward_iterator_tag, forward_iterator_tag )
1461{
1462 // shorten sequences as much as possible by lopping of any equal parts
Eric Fiselier62a0e012014-10-27 20:26:25 +00001463 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001464 if (!__pred(*__first1, *__first2))
1465 goto __not_done;
1466 return __first1 == __last1 && __first2 == __last2;
1467__not_done:
1468 // __first1 != __last1 && __first2 != __last2 && *__first1 != *__first2
1469 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1470 _D1 __l1 = _VSTD::distance(__first1, __last1);
1471
1472 typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
Marshall Clow9f8f5242013-05-10 00:16:10 +00001473 _D2 __l2 = _VSTD::distance(__first2, __last2);
Marshall Clowb30abdd2013-05-09 21:14:23 +00001474 if (__l1 != __l2)
1475 return false;
1476
1477 // For each element in [f1, l1) see if there are the same number of
1478 // equal elements in [f2, l2)
1479 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1480 {
1481 // Have we already counted the number of *__i in [f1, l1)?
1482 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1483 if (__pred(*__j, *__i))
1484 goto __next_iter;
1485 {
1486 // Count number of *__i in [f2, l2)
1487 _D1 __c2 = 0;
1488 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1489 if (__pred(*__i, *__j))
1490 ++__c2;
1491 if (__c2 == 0)
1492 return false;
1493 // Count number of *__i in [__i, l1) (we can start with 1)
1494 _D1 __c1 = 1;
1495 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1496 if (__pred(*__i, *__j))
1497 ++__c1;
1498 if (__c1 != __c2)
1499 return false;
1500 }
1501__next_iter:;
1502 }
1503 return true;
1504}
1505
1506template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1507bool
1508__is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001509 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001510 _BinaryPredicate __pred,
1511 random_access_iterator_tag, random_access_iterator_tag )
1512{
1513 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1514 return false;
1515 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1516 typename add_lvalue_reference<_BinaryPredicate>::type>
1517 (__first1, __last1, __first2, __pred );
1518}
1519
1520template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1521inline _LIBCPP_INLINE_VISIBILITY
1522bool
1523is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1524 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1525 _BinaryPredicate __pred )
1526{
1527 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1528 (__first1, __last1, __first2, __last2, __pred,
1529 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1530 typename iterator_traits<_ForwardIterator2>::iterator_category());
1531}
1532
1533template<class _ForwardIterator1, class _ForwardIterator2>
1534inline _LIBCPP_INLINE_VISIBILITY
1535bool
1536is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1537 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1538{
1539 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1540 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1541 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1542 __equal_to<__v1, __v2>(),
1543 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1544 typename iterator_traits<_ForwardIterator2>::iterator_category());
1545}
1546#endif
1547
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001548// search
Marshall Clow33406332018-01-08 19:18:00 +00001549// __search is in <functional>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001550
1551template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1552inline _LIBCPP_INLINE_VISIBILITY
1553_ForwardIterator1
1554search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1555 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1556{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001557 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001558 (__first1, __last1, __first2, __last2, __pred,
Marshall Clowf6d6b512016-03-08 15:12:52 +00001559 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1560 typename iterator_traits<_ForwardIterator2>::iterator_category())
1561 .first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001562}
1563
1564template <class _ForwardIterator1, class _ForwardIterator2>
1565inline _LIBCPP_INLINE_VISIBILITY
1566_ForwardIterator1
1567search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1568 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1569{
Marshall Clowf6d6b512016-03-08 15:12:52 +00001570 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1571 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001572 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001573}
1574
Marshall Clow33406332018-01-08 19:18:00 +00001575
1576#if _LIBCPP_STD_VER > 14
1577template <class _ForwardIterator, class _Searcher>
1578_LIBCPP_INLINE_VISIBILITY
1579_ForwardIterator search(_ForwardIterator __f, _ForwardIterator __l, const _Searcher &__s)
1580{ return __s(__f, __l).first; }
1581#endif
1582
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001583// search_n
1584
1585template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
1586_ForwardIterator
1587__search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001588 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001589{
1590 if (__count <= 0)
1591 return __first;
1592 while (true)
1593 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001594 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001595 while (true)
1596 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001597 if (__first == __last) // return __last if no element matches __value_
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001598 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001599 if (__pred(*__first, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001600 break;
1601 ++__first;
1602 }
Howard Hinnant78b68282011-10-22 20:59:45 +00001603 // *__first matches __value_, now match elements after here
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001604 _ForwardIterator __m = __first;
1605 _Size __c(0);
1606 while (true)
1607 {
1608 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1609 return __first;
1610 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1611 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001612 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001613 {
1614 __first = __m;
1615 ++__first;
1616 break;
1617 } // else there is a match, check next elements
1618 }
1619 }
1620}
1621
1622template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
1623_RandomAccessIterator
1624__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001625 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001626{
1627 if (__count <= 0)
1628 return __first;
1629 _Size __len = static_cast<_Size>(__last - __first);
1630 if (__len < __count)
1631 return __last;
1632 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1633 while (true)
1634 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001635 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001636 while (true)
1637 {
Howard Hinnant128f7bf2013-04-04 15:40:48 +00001638 if (__first >= __s) // return __last if no element matches __value_
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001639 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001640 if (__pred(*__first, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001641 break;
1642 ++__first;
1643 }
Howard Hinnant78b68282011-10-22 20:59:45 +00001644 // *__first matches __value_, now match elements after here
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001645 _RandomAccessIterator __m = __first;
1646 _Size __c(0);
1647 while (true)
1648 {
1649 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1650 return __first;
1651 ++__m; // no need to check range on __m because __s guarantees we have enough source
Howard Hinnant78b68282011-10-22 20:59:45 +00001652 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001653 {
1654 __first = __m;
1655 ++__first;
1656 break;
1657 } // else there is a match, check next elements
1658 }
1659 }
1660}
1661
1662template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
1663inline _LIBCPP_INLINE_VISIBILITY
1664_ForwardIterator
1665search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001666 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001667{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001668 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001669 (__first, __last, __convert_to_integral(__count), __value_, __pred,
1670 typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001671}
1672
1673template <class _ForwardIterator, class _Size, class _Tp>
1674inline _LIBCPP_INLINE_VISIBILITY
1675_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00001676search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001677{
1678 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001679 return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
1680 __value_, __equal_to<__v, _Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001681}
1682
1683// copy
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001684template <class _Iter>
1685inline _LIBCPP_INLINE_VISIBILITY
1686_Iter
1687__unwrap_iter(_Iter __i)
1688{
1689 return __i;
1690}
1691
1692template <class _Tp>
Marshall Clow798ecd62017-05-25 14:20:26 +00001693inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001694typename enable_if
1695<
Howard Hinnant1468b662010-11-19 22:17:28 +00001696 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001697 _Tp*
1698>::type
1699__unwrap_iter(move_iterator<_Tp*> __i)
1700{
1701 return __i.base();
1702}
1703
Howard Hinnant499cea12013-08-23 17:37:05 +00001704#if _LIBCPP_DEBUG_LEVEL < 2
1705
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001706template <class _Tp>
Marshall Clow798ecd62017-05-25 14:20:26 +00001707inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001708typename 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(__wrap_iter<_Tp*> __i)
1714{
1715 return __i.base();
1716}
1717
Eric Fiselier2c8aa052016-12-28 05:35:32 +00001718#else
1719
1720template <class _Tp>
Marshall Clow798ecd62017-05-25 14:20:26 +00001721inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2c8aa052016-12-28 05:35:32 +00001722typename enable_if
1723<
1724 is_trivially_copy_assignable<_Tp>::value,
1725 __wrap_iter<_Tp*>
1726>::type
1727__unwrap_iter(__wrap_iter<_Tp*> __i)
1728{
1729 return __i;
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{
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00001805 return _VSTD::__copy_backward(__unwrap_iter(__first),
1806 __unwrap_iter(__last),
1807 __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001808}
1809
1810// copy_if
1811
1812template<class _InputIterator, class _OutputIterator, class _Predicate>
1813inline _LIBCPP_INLINE_VISIBILITY
1814_OutputIterator
1815copy_if(_InputIterator __first, _InputIterator __last,
1816 _OutputIterator __result, _Predicate __pred)
1817{
1818 for (; __first != __last; ++__first)
1819 {
1820 if (__pred(*__first))
1821 {
1822 *__result = *__first;
1823 ++__result;
1824 }
1825 }
1826 return __result;
1827}
1828
1829// copy_n
1830
1831template<class _InputIterator, class _Size, class _OutputIterator>
1832inline _LIBCPP_INLINE_VISIBILITY
1833typename enable_if
1834<
1835 __is_input_iterator<_InputIterator>::value &&
1836 !__is_random_access_iterator<_InputIterator>::value,
1837 _OutputIterator
1838>::type
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001839copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001840{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001841 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1842 _IntegralSize __n = __orig_n;
Howard Hinnant171869e2011-02-27 20:55:39 +00001843 if (__n > 0)
1844 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001845 *__result = *__first;
Howard Hinnant171869e2011-02-27 20:55:39 +00001846 ++__result;
1847 for (--__n; __n > 0; --__n)
1848 {
1849 ++__first;
1850 *__result = *__first;
1851 ++__result;
1852 }
1853 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001854 return __result;
1855}
1856
1857template<class _InputIterator, class _Size, class _OutputIterator>
1858inline _LIBCPP_INLINE_VISIBILITY
1859typename enable_if
1860<
1861 __is_random_access_iterator<_InputIterator>::value,
1862 _OutputIterator
1863>::type
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001864copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001865{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001866 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1867 _IntegralSize __n = __orig_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001868 return _VSTD::copy(__first, __first + __n, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001869}
1870
1871// move
1872
1873template <class _InputIterator, class _OutputIterator>
1874inline _LIBCPP_INLINE_VISIBILITY
1875_OutputIterator
1876__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1877{
Eric Fiselierb9919752014-10-27 19:28:20 +00001878 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001879 *__result = _VSTD::move(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001880 return __result;
1881}
1882
1883template <class _Tp, class _Up>
1884inline _LIBCPP_INLINE_VISIBILITY
1885typename enable_if
1886<
1887 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001888 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001889 _Up*
1890>::type
1891__move(_Tp* __first, _Tp* __last, _Up* __result)
1892{
1893 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001894 if (__n > 0)
1895 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001896 return __result + __n;
1897}
1898
1899template <class _InputIterator, class _OutputIterator>
1900inline _LIBCPP_INLINE_VISIBILITY
1901_OutputIterator
1902move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1903{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001904 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001905}
1906
1907// move_backward
1908
1909template <class _InputIterator, class _OutputIterator>
1910inline _LIBCPP_INLINE_VISIBILITY
1911_OutputIterator
1912__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1913{
1914 while (__first != __last)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001915 *--__result = _VSTD::move(*--__last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001916 return __result;
1917}
1918
1919template <class _Tp, class _Up>
1920inline _LIBCPP_INLINE_VISIBILITY
1921typename enable_if
1922<
1923 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001924 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001925 _Up*
1926>::type
1927__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1928{
1929 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001930 if (__n > 0)
1931 {
1932 __result -= __n;
1933 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1934 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001935 return __result;
1936}
1937
1938template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1939inline _LIBCPP_INLINE_VISIBILITY
1940_BidirectionalIterator2
1941move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1942 _BidirectionalIterator2 __result)
1943{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001944 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001945}
1946
1947// iter_swap
1948
Howard Hinnante9b2c2d2011-05-27 15:04:19 +00001949// moved to <type_traits> for better swap / noexcept support
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001950
1951// transform
1952
1953template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
1954inline _LIBCPP_INLINE_VISIBILITY
1955_OutputIterator
1956transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1957{
Eric Fiselierb9919752014-10-27 19:28:20 +00001958 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001959 *__result = __op(*__first);
1960 return __result;
1961}
1962
1963template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
1964inline _LIBCPP_INLINE_VISIBILITY
1965_OutputIterator
1966transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1967 _OutputIterator __result, _BinaryOperation __binary_op)
1968{
Eric Fiselierb9919752014-10-27 19:28:20 +00001969 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001970 *__result = __binary_op(*__first1, *__first2);
1971 return __result;
1972}
1973
1974// replace
1975
1976template <class _ForwardIterator, class _Tp>
1977inline _LIBCPP_INLINE_VISIBILITY
1978void
1979replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1980{
1981 for (; __first != __last; ++__first)
1982 if (*__first == __old_value)
1983 *__first = __new_value;
1984}
1985
1986// replace_if
1987
1988template <class _ForwardIterator, class _Predicate, class _Tp>
1989inline _LIBCPP_INLINE_VISIBILITY
1990void
1991replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1992{
1993 for (; __first != __last; ++__first)
1994 if (__pred(*__first))
1995 *__first = __new_value;
1996}
1997
1998// replace_copy
1999
2000template <class _InputIterator, class _OutputIterator, class _Tp>
2001inline _LIBCPP_INLINE_VISIBILITY
2002_OutputIterator
2003replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2004 const _Tp& __old_value, const _Tp& __new_value)
2005{
Eric Fiselierb9919752014-10-27 19:28:20 +00002006 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002007 if (*__first == __old_value)
2008 *__result = __new_value;
2009 else
2010 *__result = *__first;
2011 return __result;
2012}
2013
2014// replace_copy_if
2015
2016template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
2017inline _LIBCPP_INLINE_VISIBILITY
2018_OutputIterator
2019replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2020 _Predicate __pred, const _Tp& __new_value)
2021{
Eric Fiselierb9919752014-10-27 19:28:20 +00002022 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002023 if (__pred(*__first))
2024 *__result = __new_value;
2025 else
2026 *__result = *__first;
2027 return __result;
2028}
2029
2030// fill_n
2031
2032template <class _OutputIterator, class _Size, class _Tp>
2033inline _LIBCPP_INLINE_VISIBILITY
2034_OutputIterator
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002035__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002036{
Eric Fiselierb9919752014-10-27 19:28:20 +00002037 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnant78b68282011-10-22 20:59:45 +00002038 *__first = __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002039 return __first;
2040}
2041
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002042template <class _Tp, class _Size, class _Up>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002043inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002044typename enable_if
2045<
2046 is_integral<_Tp>::value && sizeof(_Tp) == 1 &&
2047 !is_same<_Tp, bool>::value &&
2048 is_integral<_Up>::value && sizeof(_Up) == 1,
2049 _Tp*
2050>::type
2051__fill_n(_Tp* __first, _Size __n,_Up __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002052{
2053 if (__n > 0)
Howard Hinnant78b68282011-10-22 20:59:45 +00002054 _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002055 return __first + __n;
2056}
2057
2058template <class _OutputIterator, class _Size, class _Tp>
2059inline _LIBCPP_INLINE_VISIBILITY
2060_OutputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002061fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002062{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002063 return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002064}
2065
2066// fill
2067
2068template <class _ForwardIterator, class _Tp>
2069inline _LIBCPP_INLINE_VISIBILITY
2070void
Howard Hinnant78b68282011-10-22 20:59:45 +00002071__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002072{
2073 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +00002074 *__first = __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002075}
2076
2077template <class _RandomAccessIterator, class _Tp>
2078inline _LIBCPP_INLINE_VISIBILITY
2079void
Howard Hinnant78b68282011-10-22 20:59:45 +00002080__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002081{
Howard Hinnant78b68282011-10-22 20:59:45 +00002082 _VSTD::fill_n(__first, __last - __first, __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002083}
2084
2085template <class _ForwardIterator, class _Tp>
2086inline _LIBCPP_INLINE_VISIBILITY
2087void
Howard Hinnant78b68282011-10-22 20:59:45 +00002088fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002089{
Howard Hinnant78b68282011-10-22 20:59:45 +00002090 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002091}
2092
2093// generate
2094
2095template <class _ForwardIterator, class _Generator>
2096inline _LIBCPP_INLINE_VISIBILITY
2097void
2098generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2099{
2100 for (; __first != __last; ++__first)
2101 *__first = __gen();
2102}
2103
2104// generate_n
2105
2106template <class _OutputIterator, class _Size, class _Generator>
2107inline _LIBCPP_INLINE_VISIBILITY
2108_OutputIterator
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002109generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002110{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002111 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
2112 _IntegralSize __n = __orig_n;
Eric Fiselierb9919752014-10-27 19:28:20 +00002113 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002114 *__first = __gen();
2115 return __first;
2116}
2117
2118// remove
2119
2120template <class _ForwardIterator, class _Tp>
2121_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002122remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002123{
Howard Hinnant78b68282011-10-22 20:59:45 +00002124 __first = _VSTD::find(__first, __last, __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002125 if (__first != __last)
2126 {
2127 _ForwardIterator __i = __first;
2128 while (++__i != __last)
2129 {
Howard Hinnant78b68282011-10-22 20:59:45 +00002130 if (!(*__i == __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002131 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002132 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002133 ++__first;
2134 }
2135 }
2136 }
2137 return __first;
2138}
2139
2140// remove_if
2141
2142template <class _ForwardIterator, class _Predicate>
2143_ForwardIterator
2144remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2145{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002146 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002147 (__first, __last, __pred);
2148 if (__first != __last)
2149 {
2150 _ForwardIterator __i = __first;
2151 while (++__i != __last)
2152 {
2153 if (!__pred(*__i))
2154 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002155 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002156 ++__first;
2157 }
2158 }
2159 }
2160 return __first;
2161}
2162
2163// remove_copy
2164
2165template <class _InputIterator, class _OutputIterator, class _Tp>
2166inline _LIBCPP_INLINE_VISIBILITY
2167_OutputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002168remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002169{
2170 for (; __first != __last; ++__first)
2171 {
Howard Hinnant78b68282011-10-22 20:59:45 +00002172 if (!(*__first == __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002173 {
2174 *__result = *__first;
2175 ++__result;
2176 }
2177 }
2178 return __result;
2179}
2180
2181// remove_copy_if
2182
2183template <class _InputIterator, class _OutputIterator, class _Predicate>
2184inline _LIBCPP_INLINE_VISIBILITY
2185_OutputIterator
2186remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2187{
2188 for (; __first != __last; ++__first)
2189 {
2190 if (!__pred(*__first))
2191 {
2192 *__result = *__first;
2193 ++__result;
2194 }
2195 }
2196 return __result;
2197}
2198
2199// unique
2200
2201template <class _ForwardIterator, class _BinaryPredicate>
2202_ForwardIterator
2203unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2204{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002205 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002206 (__first, __last, __pred);
2207 if (__first != __last)
2208 {
2209 // ... a a ? ...
2210 // f i
2211 _ForwardIterator __i = __first;
2212 for (++__i; ++__i != __last;)
2213 if (!__pred(*__first, *__i))
Howard Hinnant0949eed2011-06-30 21:18:19 +00002214 *++__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002215 ++__first;
2216 }
2217 return __first;
2218}
2219
2220template <class _ForwardIterator>
2221inline _LIBCPP_INLINE_VISIBILITY
2222_ForwardIterator
2223unique(_ForwardIterator __first, _ForwardIterator __last)
2224{
2225 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002226 return _VSTD::unique(__first, __last, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002227}
2228
2229// unique_copy
2230
2231template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
2232_OutputIterator
2233__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2234 input_iterator_tag, output_iterator_tag)
2235{
2236 if (__first != __last)
2237 {
2238 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2239 *__result = __t;
2240 ++__result;
2241 while (++__first != __last)
2242 {
2243 if (!__pred(__t, *__first))
2244 {
2245 __t = *__first;
2246 *__result = __t;
2247 ++__result;
2248 }
2249 }
2250 }
2251 return __result;
2252}
2253
2254template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
2255_OutputIterator
2256__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2257 forward_iterator_tag, output_iterator_tag)
2258{
2259 if (__first != __last)
2260 {
2261 _ForwardIterator __i = __first;
2262 *__result = *__i;
2263 ++__result;
2264 while (++__first != __last)
2265 {
2266 if (!__pred(*__i, *__first))
2267 {
2268 *__result = *__first;
2269 ++__result;
2270 __i = __first;
2271 }
2272 }
2273 }
2274 return __result;
2275}
2276
2277template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
2278_ForwardIterator
2279__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2280 input_iterator_tag, forward_iterator_tag)
2281{
2282 if (__first != __last)
2283 {
2284 *__result = *__first;
2285 while (++__first != __last)
2286 if (!__pred(*__result, *__first))
2287 *++__result = *__first;
2288 ++__result;
2289 }
2290 return __result;
2291}
2292
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002293template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
2294inline _LIBCPP_INLINE_VISIBILITY
2295_OutputIterator
2296unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2297{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002298 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002299 (__first, __last, __result, __pred,
2300 typename iterator_traits<_InputIterator>::iterator_category(),
2301 typename iterator_traits<_OutputIterator>::iterator_category());
2302}
2303
2304template <class _InputIterator, class _OutputIterator>
2305inline _LIBCPP_INLINE_VISIBILITY
2306_OutputIterator
2307unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2308{
2309 typedef typename iterator_traits<_InputIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002310 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002311}
2312
2313// reverse
2314
2315template <class _BidirectionalIterator>
2316inline _LIBCPP_INLINE_VISIBILITY
2317void
2318__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2319{
2320 while (__first != __last)
2321 {
2322 if (__first == --__last)
2323 break;
Marshall Clowc010bd62015-11-02 21:34:25 +00002324 _VSTD::iter_swap(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002325 ++__first;
2326 }
2327}
2328
2329template <class _RandomAccessIterator>
2330inline _LIBCPP_INLINE_VISIBILITY
2331void
2332__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2333{
2334 if (__first != __last)
2335 for (; __first < --__last; ++__first)
Marshall Clowc010bd62015-11-02 21:34:25 +00002336 _VSTD::iter_swap(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002337}
2338
2339template <class _BidirectionalIterator>
2340inline _LIBCPP_INLINE_VISIBILITY
2341void
2342reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2343{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002344 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002345}
2346
2347// reverse_copy
2348
2349template <class _BidirectionalIterator, class _OutputIterator>
2350inline _LIBCPP_INLINE_VISIBILITY
2351_OutputIterator
2352reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2353{
2354 for (; __first != __last; ++__result)
2355 *__result = *--__last;
2356 return __result;
2357}
2358
2359// rotate
2360
2361template <class _ForwardIterator>
2362_ForwardIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002363__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002364{
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002365 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2366 value_type __tmp = _VSTD::move(*__first);
2367 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2368 *__lm1 = _VSTD::move(__tmp);
2369 return __lm1;
2370}
2371
2372template <class _BidirectionalIterator>
2373_BidirectionalIterator
2374__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2375{
2376 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2377 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2378 value_type __tmp = _VSTD::move(*__lm1);
2379 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2380 *__first = _VSTD::move(__tmp);
2381 return __fp1;
2382}
2383
2384template <class _ForwardIterator>
2385_ForwardIterator
2386__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2387{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002388 _ForwardIterator __i = __middle;
2389 while (true)
2390 {
2391 swap(*__first, *__i);
2392 ++__first;
2393 if (++__i == __last)
2394 break;
2395 if (__first == __middle)
2396 __middle = __i;
2397 }
2398 _ForwardIterator __r = __first;
2399 if (__first != __middle)
2400 {
2401 __i = __middle;
2402 while (true)
2403 {
2404 swap(*__first, *__i);
2405 ++__first;
2406 if (++__i == __last)
2407 {
2408 if (__first == __middle)
2409 break;
2410 __i = __middle;
2411 }
2412 else if (__first == __middle)
2413 __middle = __i;
2414 }
2415 }
2416 return __r;
2417}
2418
2419template<typename _Integral>
2420inline _LIBCPP_INLINE_VISIBILITY
2421_Integral
Marshall Clow1c1e91d2016-07-26 14:29:45 +00002422__algo_gcd(_Integral __x, _Integral __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002423{
2424 do
2425 {
2426 _Integral __t = __x % __y;
2427 __x = __y;
2428 __y = __t;
2429 } while (__y);
2430 return __x;
2431}
2432
2433template<typename _RandomAccessIterator>
2434_RandomAccessIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002435__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002436{
2437 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2438 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant324bb032010-08-22 00:02:43 +00002439
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002440 const difference_type __m1 = __middle - __first;
2441 const difference_type __m2 = __last - __middle;
2442 if (__m1 == __m2)
2443 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002444 _VSTD::swap_ranges(__first, __middle, __middle);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002445 return __middle;
2446 }
Marshall Clow1c1e91d2016-07-26 14:29:45 +00002447 const difference_type __g = _VSTD::__algo_gcd(__m1, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002448 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2449 {
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002450 value_type __t(_VSTD::move(*--__p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002451 _RandomAccessIterator __p1 = __p;
2452 _RandomAccessIterator __p2 = __p1 + __m1;
2453 do
2454 {
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002455 *__p1 = _VSTD::move(*__p2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002456 __p1 = __p2;
2457 const difference_type __d = __last - __p2;
2458 if (__m1 < __d)
2459 __p2 += __m1;
2460 else
2461 __p2 = __first + (__m1 - __d);
2462 } while (__p2 != __p);
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002463 *__p1 = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002464 }
2465 return __first + __m2;
2466}
2467
2468template <class _ForwardIterator>
2469inline _LIBCPP_INLINE_VISIBILITY
2470_ForwardIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002471__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2472 _VSTD::forward_iterator_tag)
2473{
2474 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2475 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2476 {
2477 if (_VSTD::next(__first) == __middle)
2478 return _VSTD::__rotate_left(__first, __last);
2479 }
2480 return _VSTD::__rotate_forward(__first, __middle, __last);
2481}
2482
2483template <class _BidirectionalIterator>
2484inline _LIBCPP_INLINE_VISIBILITY
2485_BidirectionalIterator
2486__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2487 _VSTD::bidirectional_iterator_tag)
2488{
2489 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2490 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2491 {
2492 if (_VSTD::next(__first) == __middle)
2493 return _VSTD::__rotate_left(__first, __last);
2494 if (_VSTD::next(__middle) == __last)
2495 return _VSTD::__rotate_right(__first, __last);
2496 }
2497 return _VSTD::__rotate_forward(__first, __middle, __last);
2498}
2499
2500template <class _RandomAccessIterator>
2501inline _LIBCPP_INLINE_VISIBILITY
2502_RandomAccessIterator
2503__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2504 _VSTD::random_access_iterator_tag)
2505{
2506 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2507 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2508 {
2509 if (_VSTD::next(__first) == __middle)
2510 return _VSTD::__rotate_left(__first, __last);
2511 if (_VSTD::next(__middle) == __last)
2512 return _VSTD::__rotate_right(__first, __last);
2513 return _VSTD::__rotate_gcd(__first, __middle, __last);
2514 }
2515 return _VSTD::__rotate_forward(__first, __middle, __last);
2516}
2517
2518template <class _ForwardIterator>
2519inline _LIBCPP_INLINE_VISIBILITY
2520_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002521rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2522{
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002523 if (__first == __middle)
2524 return __last;
2525 if (__middle == __last)
2526 return __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002527 return _VSTD::__rotate(__first, __middle, __last,
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002528 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002529}
2530
2531// rotate_copy
2532
2533template <class _ForwardIterator, class _OutputIterator>
2534inline _LIBCPP_INLINE_VISIBILITY
2535_OutputIterator
2536rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2537{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002538 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002539}
2540
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002541// min_element
2542
2543template <class _ForwardIterator, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002544inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002545_ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +00002546min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002547{
2548 if (__first != __last)
2549 {
2550 _ForwardIterator __i = __first;
2551 while (++__i != __last)
2552 if (__comp(*__i, *__first))
2553 __first = __i;
2554 }
2555 return __first;
2556}
2557
2558template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002559inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002560_ForwardIterator
2561min_element(_ForwardIterator __first, _ForwardIterator __last)
2562{
Marshall Clow928735a2015-05-10 13:53:31 +00002563 return _VSTD::min_element(__first, __last,
Howard Hinnant98e5d972010-08-21 20:10:01 +00002564 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2565}
2566
2567// min
2568
2569template <class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002570inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002571const _Tp&
2572min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2573{
2574 return __comp(__b, __a) ? __b : __a;
2575}
2576
2577template <class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002578inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002579const _Tp&
2580min(const _Tp& __a, const _Tp& __b)
2581{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002582 return _VSTD::min(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002583}
2584
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002585#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002586
Howard Hinnant98e5d972010-08-21 20:10:01 +00002587template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002588inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002589_Tp
2590min(initializer_list<_Tp> __t, _Compare __comp)
2591{
Marshall Clow928735a2015-05-10 13:53:31 +00002592 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002593}
2594
2595template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002596inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002597_Tp
2598min(initializer_list<_Tp> __t)
2599{
Marshall Clow928735a2015-05-10 13:53:31 +00002600 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002601}
2602
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002603#endif // _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002604
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002605// max_element
2606
2607template <class _ForwardIterator, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002608inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002609_ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +00002610max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002611{
2612 if (__first != __last)
2613 {
2614 _ForwardIterator __i = __first;
2615 while (++__i != __last)
2616 if (__comp(*__first, *__i))
2617 __first = __i;
2618 }
2619 return __first;
2620}
2621
Marshall Clow9d9463a2014-02-19 16:51:35 +00002622
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002623template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002624inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002625_ForwardIterator
2626max_element(_ForwardIterator __first, _ForwardIterator __last)
2627{
Marshall Clow928735a2015-05-10 13:53:31 +00002628 return _VSTD::max_element(__first, __last,
Howard Hinnant98e5d972010-08-21 20:10:01 +00002629 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2630}
2631
2632// max
2633
2634template <class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002635inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002636const _Tp&
2637max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2638{
2639 return __comp(__a, __b) ? __b : __a;
2640}
2641
2642template <class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002643inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002644const _Tp&
2645max(const _Tp& __a, const _Tp& __b)
2646{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002647 return _VSTD::max(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002648}
2649
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002650#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002651
Howard Hinnant98e5d972010-08-21 20:10:01 +00002652template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002653inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002654_Tp
2655max(initializer_list<_Tp> __t, _Compare __comp)
2656{
Marshall Clow928735a2015-05-10 13:53:31 +00002657 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002658}
2659
2660template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002661inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002662_Tp
2663max(initializer_list<_Tp> __t)
2664{
Marshall Clow928735a2015-05-10 13:53:31 +00002665 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002666}
2667
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002668#endif // _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002669
Marshall Clow3e0808e2016-03-07 22:43:49 +00002670#if _LIBCPP_STD_VER > 14
2671// clamp
2672template<class _Tp, class _Compare>
2673inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2674const _Tp&
2675clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
2676{
2677 _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
2678 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
2679
2680}
2681
2682template<class _Tp>
2683inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2684const _Tp&
2685clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
2686{
2687 return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
2688}
2689#endif
2690
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002691// minmax_element
2692
2693template <class _ForwardIterator, class _Compare>
Marshall Clow928735a2015-05-10 13:53:31 +00002694_LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002695std::pair<_ForwardIterator, _ForwardIterator>
2696minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2697{
2698 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2699 if (__first != __last)
2700 {
2701 if (++__first != __last)
2702 {
2703 if (__comp(*__first, *__result.first))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002704 __result.first = __first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002705 else
2706 __result.second = __first;
2707 while (++__first != __last)
2708 {
2709 _ForwardIterator __i = __first;
2710 if (++__first == __last)
2711 {
2712 if (__comp(*__i, *__result.first))
2713 __result.first = __i;
2714 else if (!__comp(*__i, *__result.second))
2715 __result.second = __i;
2716 break;
2717 }
2718 else
2719 {
2720 if (__comp(*__first, *__i))
2721 {
2722 if (__comp(*__first, *__result.first))
2723 __result.first = __first;
2724 if (!__comp(*__i, *__result.second))
2725 __result.second = __i;
2726 }
2727 else
2728 {
2729 if (__comp(*__i, *__result.first))
2730 __result.first = __i;
2731 if (!__comp(*__first, *__result.second))
2732 __result.second = __first;
2733 }
2734 }
2735 }
2736 }
2737 }
2738 return __result;
2739}
2740
2741template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002742inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002743std::pair<_ForwardIterator, _ForwardIterator>
2744minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2745{
Marshall Clow9d9463a2014-02-19 16:51:35 +00002746 return _VSTD::minmax_element(__first, __last,
2747 __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002748}
2749
Howard Hinnant98e5d972010-08-21 20:10:01 +00002750// minmax
2751
2752template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002753inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002754pair<const _Tp&, const _Tp&>
2755minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2756{
2757 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2758 pair<const _Tp&, const _Tp&>(__a, __b);
2759}
2760
2761template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002762inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002763pair<const _Tp&, const _Tp&>
2764minmax(const _Tp& __a, const _Tp& __b)
2765{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002766 return _VSTD::minmax(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002767}
2768
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002769#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002770
Howard Hinnant98e5d972010-08-21 20:10:01 +00002771template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002772inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002773pair<_Tp, _Tp>
2774minmax(initializer_list<_Tp> __t, _Compare __comp)
2775{
Marshall Clow9d9463a2014-02-19 16:51:35 +00002776 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2777 _Iter __first = __t.begin();
2778 _Iter __last = __t.end();
Marshall Clow3024f862015-02-11 15:41:34 +00002779 std::pair<_Tp, _Tp> __result(*__first, *__first);
Marshall Clow9d9463a2014-02-19 16:51:35 +00002780
2781 ++__first;
2782 if (__t.size() % 2 == 0)
2783 {
2784 if (__comp(*__first, __result.first))
2785 __result.first = *__first;
2786 else
2787 __result.second = *__first;
2788 ++__first;
2789 }
Aditya Kumarfdb4f172016-08-25 11:52:38 +00002790
Marshall Clow9d9463a2014-02-19 16:51:35 +00002791 while (__first != __last)
2792 {
2793 _Tp __prev = *__first++;
Marshall Clow3024f862015-02-11 15:41:34 +00002794 if (__comp(*__first, __prev)) {
2795 if ( __comp(*__first, __result.first)) __result.first = *__first;
2796 if (!__comp(__prev, __result.second)) __result.second = __prev;
Marshall Clow9d9463a2014-02-19 16:51:35 +00002797 }
2798 else {
Marshall Clow3024f862015-02-11 15:41:34 +00002799 if ( __comp(__prev, __result.first)) __result.first = __prev;
2800 if (!__comp(*__first, __result.second)) __result.second = *__first;
Marshall Clow9d9463a2014-02-19 16:51:35 +00002801 }
Aditya Kumarfdb4f172016-08-25 11:52:38 +00002802
Marshall Clow9d9463a2014-02-19 16:51:35 +00002803 __first++;
2804 }
2805 return __result;
2806}
2807
2808template<class _Tp>
2809inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2810pair<_Tp, _Tp>
2811minmax(initializer_list<_Tp> __t)
2812{
2813 return _VSTD::minmax(__t, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002814}
2815
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002816#endif // _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002817
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002818// random_shuffle
2819
Howard Hinnantc3267212010-05-26 17:49:34 +00002820// __independent_bits_engine
2821
Howard Hinnant99968442011-11-29 18:15:50 +00002822template <unsigned long long _Xp, size_t _Rp>
Howard Hinnantc3267212010-05-26 17:49:34 +00002823struct __log2_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002824{
Howard Hinnant99968442011-11-29 18:15:50 +00002825 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2826 : __log2_imp<_Xp, _Rp - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002827};
2828
Howard Hinnant99968442011-11-29 18:15:50 +00002829template <unsigned long long _Xp>
2830struct __log2_imp<_Xp, 0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002831{
Howard Hinnantc3267212010-05-26 17:49:34 +00002832 static const size_t value = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002833};
2834
Howard Hinnant99968442011-11-29 18:15:50 +00002835template <size_t _Rp>
2836struct __log2_imp<0, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002837{
Howard Hinnant99968442011-11-29 18:15:50 +00002838 static const size_t value = _Rp + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002839};
2840
Eric Fiselierac633a22017-05-31 21:20:18 +00002841template <class _UIntType, _UIntType _Xp>
Howard Hinnantc3267212010-05-26 17:49:34 +00002842struct __log2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002843{
Howard Hinnant99968442011-11-29 18:15:50 +00002844 static const size_t value = __log2_imp<_Xp,
Eric Fiselierac633a22017-05-31 21:20:18 +00002845 sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002846};
2847
Howard Hinnantc3267212010-05-26 17:49:34 +00002848template<class _Engine, class _UIntType>
2849class __independent_bits_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002850{
Howard Hinnantc3267212010-05-26 17:49:34 +00002851public:
2852 // types
2853 typedef _UIntType result_type;
2854
2855private:
2856 typedef typename _Engine::result_type _Engine_result_type;
2857 typedef typename conditional
2858 <
2859 sizeof(_Engine_result_type) <= sizeof(result_type),
2860 result_type,
2861 _Engine_result_type
2862 >::type _Working_result_type;
2863
2864 _Engine& __e_;
2865 size_t __w_;
2866 size_t __w0_;
2867 size_t __n_;
2868 size_t __n0_;
2869 _Working_result_type __y0_;
2870 _Working_result_type __y1_;
2871 _Engine_result_type __mask0_;
2872 _Engine_result_type __mask1_;
2873
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002874#ifdef _LIBCPP_CXX03_LANG
Howard Hinnant99968442011-11-29 18:15:50 +00002875 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002876 + _Working_result_type(1);
2877#else
2878 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2879 + _Working_result_type(1);
2880#endif
2881 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2882 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2883 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
Howard Hinnantc3267212010-05-26 17:49:34 +00002884
2885public:
2886 // constructors and seeding functions
2887 __independent_bits_engine(_Engine& __e, size_t __w);
2888
2889 // generating functions
Howard Hinnant99968442011-11-29 18:15:50 +00002890 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantc3267212010-05-26 17:49:34 +00002891
2892private:
Marshall Clow1e32db72017-09-20 19:38:43 +00002893 result_type __eval(false_type);
2894 result_type __eval(true_type);
Howard Hinnantc3267212010-05-26 17:49:34 +00002895};
2896
2897template<class _Engine, class _UIntType>
2898__independent_bits_engine<_Engine, _UIntType>
2899 ::__independent_bits_engine(_Engine& __e, size_t __w)
2900 : __e_(__e),
2901 __w_(__w)
2902{
2903 __n_ = __w_ / __m + (__w_ % __m != 0);
2904 __w0_ = __w_ / __n_;
Howard Hinnant99968442011-11-29 18:15:50 +00002905 if (_Rp == 0)
2906 __y0_ = _Rp;
Howard Hinnantc3267212010-05-26 17:49:34 +00002907 else if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00002908 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002909 else
2910 __y0_ = 0;
Howard Hinnant99968442011-11-29 18:15:50 +00002911 if (_Rp - __y0_ > __y0_ / __n_)
Howard Hinnantc3267212010-05-26 17:49:34 +00002912 {
2913 ++__n_;
2914 __w0_ = __w_ / __n_;
2915 if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00002916 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002917 else
2918 __y0_ = 0;
2919 }
2920 __n0_ = __n_ - __w_ % __n_;
2921 if (__w0_ < _WDt - 1)
Howard Hinnant99968442011-11-29 18:15:50 +00002922 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
Howard Hinnantc3267212010-05-26 17:49:34 +00002923 else
2924 __y1_ = 0;
2925 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2926 _Engine_result_type(0);
2927 __mask1_ = __w0_ < _EDt - 1 ?
2928 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2929 _Engine_result_type(~0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002930}
2931
Howard Hinnantc3267212010-05-26 17:49:34 +00002932template<class _Engine, class _UIntType>
2933inline
2934_UIntType
Marshall Clow1e32db72017-09-20 19:38:43 +00002935__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002936{
Howard Hinnantc3267212010-05-26 17:49:34 +00002937 return static_cast<result_type>(__e_() & __mask0_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002938}
2939
Howard Hinnantc3267212010-05-26 17:49:34 +00002940template<class _Engine, class _UIntType>
2941_UIntType
Marshall Clow1e32db72017-09-20 19:38:43 +00002942__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002943{
Marshall Clowe2b68322017-09-20 17:34:11 +00002944 const size_t _WRt = numeric_limits<result_type>::digits;
Howard Hinnant99968442011-11-29 18:15:50 +00002945 result_type _Sp = 0;
Howard Hinnantc3267212010-05-26 17:49:34 +00002946 for (size_t __k = 0; __k < __n0_; ++__k)
2947 {
2948 _Engine_result_type __u;
2949 do
2950 {
2951 __u = __e_() - _Engine::min();
2952 } while (__u >= __y0_);
Marshall Clowe2b68322017-09-20 17:34:11 +00002953 if (__w0_ < _WRt)
Howard Hinnant99968442011-11-29 18:15:50 +00002954 _Sp <<= __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002955 else
Howard Hinnant99968442011-11-29 18:15:50 +00002956 _Sp = 0;
2957 _Sp += __u & __mask0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002958 }
2959 for (size_t __k = __n0_; __k < __n_; ++__k)
2960 {
2961 _Engine_result_type __u;
2962 do
2963 {
2964 __u = __e_() - _Engine::min();
2965 } while (__u >= __y1_);
Marshall Clowe2b68322017-09-20 17:34:11 +00002966 if (__w0_ < _WRt - 1)
Howard Hinnant99968442011-11-29 18:15:50 +00002967 _Sp <<= __w0_ + 1;
Howard Hinnantc3267212010-05-26 17:49:34 +00002968 else
Howard Hinnant99968442011-11-29 18:15:50 +00002969 _Sp = 0;
2970 _Sp += __u & __mask1_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002971 }
Howard Hinnant99968442011-11-29 18:15:50 +00002972 return _Sp;
Howard Hinnantc3267212010-05-26 17:49:34 +00002973}
2974
2975// uniform_int_distribution
2976
2977template<class _IntType = int>
2978class uniform_int_distribution
2979{
2980public:
2981 // types
2982 typedef _IntType result_type;
2983
2984 class param_type
2985 {
2986 result_type __a_;
2987 result_type __b_;
2988 public:
2989 typedef uniform_int_distribution distribution_type;
2990
2991 explicit param_type(result_type __a = 0,
2992 result_type __b = numeric_limits<result_type>::max())
2993 : __a_(__a), __b_(__b) {}
2994
2995 result_type a() const {return __a_;}
2996 result_type b() const {return __b_;}
2997
2998 friend bool operator==(const param_type& __x, const param_type& __y)
2999 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3000 friend bool operator!=(const param_type& __x, const param_type& __y)
3001 {return !(__x == __y);}
3002 };
3003
3004private:
3005 param_type __p_;
3006
3007public:
3008 // constructors and reset functions
3009 explicit uniform_int_distribution(result_type __a = 0,
3010 result_type __b = numeric_limits<result_type>::max())
3011 : __p_(param_type(__a, __b)) {}
3012 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
3013 void reset() {}
3014
3015 // generating functions
3016 template<class _URNG> result_type operator()(_URNG& __g)
3017 {return (*this)(__g, __p_);}
3018 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3019
3020 // property functions
3021 result_type a() const {return __p_.a();}
3022 result_type b() const {return __p_.b();}
3023
3024 param_type param() const {return __p_;}
3025 void param(const param_type& __p) {__p_ = __p;}
3026
3027 result_type min() const {return a();}
3028 result_type max() const {return b();}
3029
3030 friend bool operator==(const uniform_int_distribution& __x,
3031 const uniform_int_distribution& __y)
3032 {return __x.__p_ == __y.__p_;}
3033 friend bool operator!=(const uniform_int_distribution& __x,
3034 const uniform_int_distribution& __y)
3035 {return !(__x == __y);}
3036};
3037
3038template<class _IntType>
3039template<class _URNG>
3040typename uniform_int_distribution<_IntType>::result_type
3041uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3042{
3043 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3044 uint32_t, uint64_t>::type _UIntType;
Howard Hinnant99968442011-11-29 18:15:50 +00003045 const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
3046 if (_Rp == 1)
Howard Hinnantc3267212010-05-26 17:49:34 +00003047 return __p.a();
3048 const size_t _Dt = numeric_limits<_UIntType>::digits;
3049 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
Howard Hinnant99968442011-11-29 18:15:50 +00003050 if (_Rp == 0)
Howard Hinnantc3267212010-05-26 17:49:34 +00003051 return static_cast<result_type>(_Eng(__g, _Dt)());
Howard Hinnant99968442011-11-29 18:15:50 +00003052 size_t __w = _Dt - __clz(_Rp) - 1;
Marshall Clow0934c752015-07-30 18:26:34 +00003053 if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
Howard Hinnantc3267212010-05-26 17:49:34 +00003054 ++__w;
3055 _Eng __e(__g, __w);
3056 _UIntType __u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003057 do
Howard Hinnantc3267212010-05-26 17:49:34 +00003058 {
3059 __u = __e();
Howard Hinnant99968442011-11-29 18:15:50 +00003060 } while (__u >= _Rp);
Howard Hinnantc3267212010-05-26 17:49:34 +00003061 return static_cast<result_type>(__u + __p.a());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003062}
3063
Eric Fiselier6547dc82017-04-03 23:23:44 +00003064#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \
3065 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003066class _LIBCPP_TYPE_VIS __rs_default;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003067
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003068_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantc3267212010-05-26 17:49:34 +00003069
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003070class _LIBCPP_TYPE_VIS __rs_default
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003071{
Howard Hinnantc3267212010-05-26 17:49:34 +00003072 static unsigned __c_;
3073
3074 __rs_default();
3075public:
Marshall Clow5920cfc2013-02-07 22:12:02 +00003076 typedef uint_fast32_t result_type;
Howard Hinnantc3267212010-05-26 17:49:34 +00003077
3078 static const result_type _Min = 0;
3079 static const result_type _Max = 0xFFFFFFFF;
3080
3081 __rs_default(const __rs_default&);
3082 ~__rs_default();
3083
3084 result_type operator()();
3085
Howard Hinnant27b4fd32012-04-02 00:40:41 +00003086 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
3087 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
Howard Hinnantc3267212010-05-26 17:49:34 +00003088
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003089 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003090};
3091
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003092_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003093
3094template <class _RandomAccessIterator>
3095void
3096random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
3097{
3098 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnant99968442011-11-29 18:15:50 +00003099 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3100 typedef typename _Dp::param_type _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003101 difference_type __d = __last - __first;
3102 if (__d > 1)
3103 {
Howard Hinnant99968442011-11-29 18:15:50 +00003104 _Dp __uid;
Howard Hinnantc3267212010-05-26 17:49:34 +00003105 __rs_default __g = __rs_get();
3106 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003107 {
Howard Hinnant99968442011-11-29 18:15:50 +00003108 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant4e599482010-10-22 15:26:39 +00003109 if (__i != difference_type(0))
3110 swap(*__first, *(__first + __i));
3111 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003112 }
3113}
3114
3115template <class _RandomAccessIterator, class _RandomNumberGenerator>
3116void
3117random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00003118#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003119 _RandomNumberGenerator&& __rand)
3120#else
3121 _RandomNumberGenerator& __rand)
3122#endif
3123{
3124 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3125 difference_type __d = __last - __first;
3126 if (__d > 1)
3127 {
3128 for (--__last; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003129 {
3130 difference_type __i = __rand(__d);
3131 swap(*__first, *(__first + __i));
3132 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003133 }
3134}
Marshall Clow03b862f2017-03-23 13:43:37 +00003135#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003136
Eric Fiselier917af0a2016-08-28 22:14:37 +00003137template <class _PopulationIterator, class _SampleIterator, class _Distance,
3138 class _UniformRandomNumberGenerator>
3139_LIBCPP_INLINE_VISIBILITY
3140_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsone208d082017-11-14 11:14:25 +00003141 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier917af0a2016-08-28 22:14:37 +00003142 _Distance __n,
3143 _UniformRandomNumberGenerator & __g,
3144 input_iterator_tag) {
3145
3146 _Distance __k = 0;
3147 for (; __first != __last && __k < __n; ++__first, (void)++__k)
Alexander Richardsone208d082017-11-14 11:14:25 +00003148 __output_iter[__k] = *__first;
Eric Fiselier917af0a2016-08-28 22:14:37 +00003149 _Distance __sz = __k;
3150 for (; __first != __last; ++__first, (void)++__k) {
3151 _Distance __r = _VSTD::uniform_int_distribution<_Distance>(0, __k)(__g);
3152 if (__r < __sz)
Alexander Richardsone208d082017-11-14 11:14:25 +00003153 __output_iter[__r] = *__first;
Eric Fiselier917af0a2016-08-28 22:14:37 +00003154 }
Alexander Richardsone208d082017-11-14 11:14:25 +00003155 return __output_iter + _VSTD::min(__n, __k);
Eric Fiselier917af0a2016-08-28 22:14:37 +00003156}
3157
3158template <class _PopulationIterator, class _SampleIterator, class _Distance,
3159 class _UniformRandomNumberGenerator>
3160_LIBCPP_INLINE_VISIBILITY
3161_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsone208d082017-11-14 11:14:25 +00003162 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier917af0a2016-08-28 22:14:37 +00003163 _Distance __n,
3164 _UniformRandomNumberGenerator& __g,
3165 forward_iterator_tag) {
3166 _Distance __unsampled_sz = _VSTD::distance(__first, __last);
3167 for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
3168 _Distance __r =
3169 _VSTD::uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
3170 if (__r < __n) {
Alexander Richardsone208d082017-11-14 11:14:25 +00003171 *__output_iter++ = *__first;
Eric Fiselier917af0a2016-08-28 22:14:37 +00003172 --__n;
3173 }
3174 }
Alexander Richardsone208d082017-11-14 11:14:25 +00003175 return __output_iter;
Eric Fiselier917af0a2016-08-28 22:14:37 +00003176}
3177
3178template <class _PopulationIterator, class _SampleIterator, class _Distance,
3179 class _UniformRandomNumberGenerator>
3180_LIBCPP_INLINE_VISIBILITY
3181_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsone208d082017-11-14 11:14:25 +00003182 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier917af0a2016-08-28 22:14:37 +00003183 _Distance __n, _UniformRandomNumberGenerator& __g) {
3184 typedef typename iterator_traits<_PopulationIterator>::iterator_category
3185 _PopCategory;
3186 typedef typename iterator_traits<_PopulationIterator>::difference_type
3187 _Difference;
3188 static_assert(__is_forward_iterator<_PopulationIterator>::value ||
3189 __is_random_access_iterator<_SampleIterator>::value,
3190 "SampleIterator must meet the requirements of RandomAccessIterator");
3191 typedef typename common_type<_Distance, _Difference>::type _CommonType;
3192 _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
3193 return _VSTD::__sample(
Alexander Richardsone208d082017-11-14 11:14:25 +00003194 __first, __last, __output_iter, _CommonType(__n),
Eric Fiselier917af0a2016-08-28 22:14:37 +00003195 __g, _PopCategory());
3196}
3197
3198#if _LIBCPP_STD_VER > 14
3199template <class _PopulationIterator, class _SampleIterator, class _Distance,
3200 class _UniformRandomNumberGenerator>
3201inline _LIBCPP_INLINE_VISIBILITY
3202_SampleIterator sample(_PopulationIterator __first,
Alexander Richardsone208d082017-11-14 11:14:25 +00003203 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier917af0a2016-08-28 22:14:37 +00003204 _Distance __n, _UniformRandomNumberGenerator&& __g) {
Alexander Richardsone208d082017-11-14 11:14:25 +00003205 return _VSTD::__sample(__first, __last, __output_iter, __n, __g);
Eric Fiselier917af0a2016-08-28 22:14:37 +00003206}
3207#endif // _LIBCPP_STD_VER > 14
3208
Howard Hinnantc3267212010-05-26 17:49:34 +00003209template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3210 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00003211#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant278bf2d2010-11-18 01:47:02 +00003212 _UniformRandomNumberGenerator&& __g)
3213#else
Howard Hinnantc3267212010-05-26 17:49:34 +00003214 _UniformRandomNumberGenerator& __g)
Howard Hinnant278bf2d2010-11-18 01:47:02 +00003215#endif
Howard Hinnantc3267212010-05-26 17:49:34 +00003216{
3217 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnant99968442011-11-29 18:15:50 +00003218 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3219 typedef typename _Dp::param_type _Pp;
Howard Hinnantc3267212010-05-26 17:49:34 +00003220 difference_type __d = __last - __first;
3221 if (__d > 1)
3222 {
Howard Hinnant99968442011-11-29 18:15:50 +00003223 _Dp __uid;
Howard Hinnantc3267212010-05-26 17:49:34 +00003224 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003225 {
Howard Hinnant99968442011-11-29 18:15:50 +00003226 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant4e599482010-10-22 15:26:39 +00003227 if (__i != difference_type(0))
3228 swap(*__first, *(__first + __i));
3229 }
Howard Hinnantc3267212010-05-26 17:49:34 +00003230 }
3231}
3232
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003233template <class _InputIterator, class _Predicate>
3234bool
3235is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3236{
3237 for (; __first != __last; ++__first)
3238 if (!__pred(*__first))
3239 break;
Marshall Clowa0ec4b72015-02-02 18:16:35 +00003240 if ( __first == __last )
3241 return true;
3242 ++__first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003243 for (; __first != __last; ++__first)
3244 if (__pred(*__first))
3245 return false;
3246 return true;
3247}
3248
3249// partition
3250
3251template <class _Predicate, class _ForwardIterator>
3252_ForwardIterator
3253__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3254{
3255 while (true)
3256 {
3257 if (__first == __last)
3258 return __first;
3259 if (!__pred(*__first))
3260 break;
3261 ++__first;
3262 }
3263 for (_ForwardIterator __p = __first; ++__p != __last;)
3264 {
3265 if (__pred(*__p))
3266 {
3267 swap(*__first, *__p);
3268 ++__first;
3269 }
3270 }
3271 return __first;
3272}
3273
3274template <class _Predicate, class _BidirectionalIterator>
3275_BidirectionalIterator
3276__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3277 bidirectional_iterator_tag)
3278{
3279 while (true)
3280 {
3281 while (true)
3282 {
3283 if (__first == __last)
3284 return __first;
3285 if (!__pred(*__first))
3286 break;
3287 ++__first;
3288 }
3289 do
3290 {
3291 if (__first == --__last)
3292 return __first;
3293 } while (!__pred(*__last));
3294 swap(*__first, *__last);
3295 ++__first;
3296 }
3297}
3298
3299template <class _ForwardIterator, class _Predicate>
3300inline _LIBCPP_INLINE_VISIBILITY
3301_ForwardIterator
3302partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3303{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003304 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003305 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3306}
3307
3308// partition_copy
3309
3310template <class _InputIterator, class _OutputIterator1,
3311 class _OutputIterator2, class _Predicate>
3312pair<_OutputIterator1, _OutputIterator2>
3313partition_copy(_InputIterator __first, _InputIterator __last,
3314 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3315 _Predicate __pred)
3316{
3317 for (; __first != __last; ++__first)
3318 {
3319 if (__pred(*__first))
3320 {
3321 *__out_true = *__first;
3322 ++__out_true;
3323 }
3324 else
3325 {
3326 *__out_false = *__first;
3327 ++__out_false;
3328 }
3329 }
3330 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3331}
3332
3333// partition_point
3334
3335template<class _ForwardIterator, class _Predicate>
3336_ForwardIterator
3337partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3338{
3339 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003340 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003341 while (__len != 0)
3342 {
3343 difference_type __l2 = __len / 2;
3344 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003345 _VSTD::advance(__m, __l2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003346 if (__pred(*__m))
3347 {
3348 __first = ++__m;
3349 __len -= __l2 + 1;
3350 }
3351 else
3352 __len = __l2;
3353 }
3354 return __first;
3355}
3356
3357// stable_partition
3358
3359template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3360_ForwardIterator
3361__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3362 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3363{
3364 // *__first is known to be false
3365 // __len >= 1
3366 if (__len == 1)
3367 return __first;
3368 if (__len == 2)
3369 {
3370 _ForwardIterator __m = __first;
3371 if (__pred(*++__m))
3372 {
3373 swap(*__first, *__m);
3374 return __m;
3375 }
3376 return __first;
3377 }
3378 if (__len <= __p.second)
3379 { // The buffer is big enough to use
3380 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3381 __destruct_n __d(0);
3382 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3383 // Move the falses into the temporary buffer, and the trues to the front of the line
3384 // Update __first to always point to the end of the trues
3385 value_type* __t = __p.first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003386 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003387 __d.__incr((value_type*)0);
3388 ++__t;
3389 _ForwardIterator __i = __first;
3390 while (++__i != __last)
3391 {
3392 if (__pred(*__i))
3393 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003394 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003395 ++__first;
3396 }
3397 else
3398 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003399 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003400 __d.__incr((value_type*)0);
3401 ++__t;
3402 }
3403 }
3404 // All trues now at start of range, all falses in buffer
3405 // Move falses back into range, but don't mess up __first which points to first false
3406 __i = __first;
3407 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003408 *__i = _VSTD::move(*__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003409 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3410 return __first;
3411 }
3412 // Else not enough buffer, do in place
3413 // __len >= 3
3414 _ForwardIterator __m = __first;
3415 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003416 _VSTD::advance(__m, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003417 // recurse on [__first, __m), *__first know to be false
3418 // F?????????????????
3419 // f m l
3420 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3421 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3422 // TTTFFFFF??????????
3423 // f ff m l
3424 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3425 _ForwardIterator __m1 = __m;
3426 _ForwardIterator __second_false = __last;
3427 _Distance __len_half = __len - __len2;
3428 while (__pred(*__m1))
3429 {
3430 if (++__m1 == __last)
3431 goto __second_half_done;
3432 --__len_half;
3433 }
3434 // TTTFFFFFTTTF??????
3435 // f ff m m1 l
3436 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3437__second_half_done:
3438 // TTTFFFFFTTTTTFFFFF
3439 // f ff m sf l
Howard Hinnant0949eed2011-06-30 21:18:19 +00003440 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003441 // TTTTTTTTFFFFFFFFFF
3442 // |
3443}
3444
3445struct __return_temporary_buffer
3446{
3447 template <class _Tp>
Howard Hinnant0949eed2011-06-30 21:18:19 +00003448 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003449};
3450
3451template <class _Predicate, class _ForwardIterator>
3452_ForwardIterator
3453__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3454 forward_iterator_tag)
3455{
3456 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3457 // Either prove all true and return __first or point to first false
3458 while (true)
3459 {
3460 if (__first == __last)
3461 return __first;
3462 if (!__pred(*__first))
3463 break;
3464 ++__first;
3465 }
3466 // We now have a reduced range [__first, __last)
3467 // *__first is known to be false
3468 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3469 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003470 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003471 pair<value_type*, ptrdiff_t> __p(0, 0);
3472 unique_ptr<value_type, __return_temporary_buffer> __h;
3473 if (__len >= __alloc_limit)
3474 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003475 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003476 __h.reset(__p.first);
3477 }
3478 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3479 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3480}
3481
3482template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3483_BidirectionalIterator
3484__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3485 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3486{
3487 // *__first is known to be false
3488 // *__last is known to be true
3489 // __len >= 2
3490 if (__len == 2)
3491 {
3492 swap(*__first, *__last);
3493 return __last;
3494 }
3495 if (__len == 3)
3496 {
3497 _BidirectionalIterator __m = __first;
3498 if (__pred(*++__m))
3499 {
3500 swap(*__first, *__m);
3501 swap(*__m, *__last);
3502 return __last;
3503 }
3504 swap(*__m, *__last);
3505 swap(*__first, *__m);
3506 return __m;
3507 }
3508 if (__len <= __p.second)
3509 { // The buffer is big enough to use
3510 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3511 __destruct_n __d(0);
3512 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3513 // Move the falses into the temporary buffer, and the trues to the front of the line
3514 // Update __first to always point to the end of the trues
3515 value_type* __t = __p.first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003516 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003517 __d.__incr((value_type*)0);
3518 ++__t;
3519 _BidirectionalIterator __i = __first;
3520 while (++__i != __last)
3521 {
3522 if (__pred(*__i))
3523 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003524 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003525 ++__first;
3526 }
3527 else
3528 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003529 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003530 __d.__incr((value_type*)0);
3531 ++__t;
3532 }
3533 }
3534 // move *__last, known to be true
Howard Hinnant0949eed2011-06-30 21:18:19 +00003535 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003536 __i = ++__first;
3537 // All trues now at start of range, all falses in buffer
3538 // Move falses back into range, but don't mess up __first which points to first false
3539 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003540 *__i = _VSTD::move(*__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003541 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3542 return __first;
3543 }
3544 // Else not enough buffer, do in place
3545 // __len >= 4
3546 _BidirectionalIterator __m = __first;
3547 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003548 _VSTD::advance(__m, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003549 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3550 // F????????????????T
3551 // f m l
3552 _BidirectionalIterator __m1 = __m;
3553 _BidirectionalIterator __first_false = __first;
3554 _Distance __len_half = __len2;
3555 while (!__pred(*--__m1))
3556 {
3557 if (__m1 == __first)
3558 goto __first_half_done;
3559 --__len_half;
3560 }
3561 // F???TFFF?????????T
3562 // f m1 m l
3563 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3564 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3565__first_half_done:
3566 // TTTFFFFF?????????T
3567 // f ff m l
3568 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3569 __m1 = __m;
3570 _BidirectionalIterator __second_false = __last;
3571 ++__second_false;
3572 __len_half = __len - __len2;
3573 while (__pred(*__m1))
3574 {
3575 if (++__m1 == __last)
3576 goto __second_half_done;
3577 --__len_half;
3578 }
3579 // TTTFFFFFTTTF?????T
3580 // f ff m m1 l
3581 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3582__second_half_done:
3583 // TTTFFFFFTTTTTFFFFF
3584 // f ff m sf l
Howard Hinnant0949eed2011-06-30 21:18:19 +00003585 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003586 // TTTTTTTTFFFFFFFFFF
3587 // |
3588}
3589
3590template <class _Predicate, class _BidirectionalIterator>
3591_BidirectionalIterator
3592__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3593 bidirectional_iterator_tag)
3594{
3595 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3596 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3597 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3598 // Either prove all true and return __first or point to first false
3599 while (true)
3600 {
3601 if (__first == __last)
3602 return __first;
3603 if (!__pred(*__first))
3604 break;
3605 ++__first;
3606 }
3607 // __first points to first false, everything prior to __first is already set.
3608 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3609 do
3610 {
3611 if (__first == --__last)
3612 return __first;
3613 } while (!__pred(*__last));
3614 // We now have a reduced range [__first, __last]
3615 // *__first is known to be false
3616 // *__last is known to be true
3617 // __len >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003618 difference_type __len = _VSTD::distance(__first, __last) + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003619 pair<value_type*, ptrdiff_t> __p(0, 0);
3620 unique_ptr<value_type, __return_temporary_buffer> __h;
3621 if (__len >= __alloc_limit)
3622 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003623 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003624 __h.reset(__p.first);
3625 }
3626 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3627 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3628}
3629
3630template <class _ForwardIterator, class _Predicate>
3631inline _LIBCPP_INLINE_VISIBILITY
3632_ForwardIterator
3633stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3634{
3635 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3636 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3637}
3638
3639// is_sorted_until
3640
3641template <class _ForwardIterator, class _Compare>
3642_ForwardIterator
3643is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3644{
3645 if (__first != __last)
3646 {
3647 _ForwardIterator __i = __first;
3648 while (++__i != __last)
3649 {
3650 if (__comp(*__i, *__first))
3651 return __i;
3652 __first = __i;
3653 }
3654 }
3655 return __last;
3656}
3657
Howard Hinnant324bb032010-08-22 00:02:43 +00003658template<class _ForwardIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003659inline _LIBCPP_INLINE_VISIBILITY
3660_ForwardIterator
3661is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3662{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003663 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003664}
3665
3666// is_sorted
3667
3668template <class _ForwardIterator, class _Compare>
3669inline _LIBCPP_INLINE_VISIBILITY
3670bool
3671is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3672{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003673 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003674}
3675
Howard Hinnant324bb032010-08-22 00:02:43 +00003676template<class _ForwardIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003677inline _LIBCPP_INLINE_VISIBILITY
3678bool
3679is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3680{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003681 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003682}
3683
3684// sort
3685
3686// stable, 2-3 compares, 0-2 swaps
3687
3688template <class _Compare, class _ForwardIterator>
3689unsigned
3690__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3691{
3692 unsigned __r = 0;
3693 if (!__c(*__y, *__x)) // if x <= y
3694 {
3695 if (!__c(*__z, *__y)) // if y <= z
3696 return __r; // x <= y && y <= z
3697 // x <= y && y > z
3698 swap(*__y, *__z); // x <= z && y < z
3699 __r = 1;
3700 if (__c(*__y, *__x)) // if x > y
3701 {
3702 swap(*__x, *__y); // x < y && y <= z
3703 __r = 2;
3704 }
3705 return __r; // x <= y && y < z
3706 }
3707 if (__c(*__z, *__y)) // x > y, if y > z
3708 {
3709 swap(*__x, *__z); // x < y && y < z
3710 __r = 1;
3711 return __r;
3712 }
3713 swap(*__x, *__y); // x > y && y <= z
3714 __r = 1; // x < y && x <= z
3715 if (__c(*__z, *__y)) // if y > z
3716 {
3717 swap(*__y, *__z); // x <= y && y < z
3718 __r = 2;
3719 }
3720 return __r;
3721} // x <= y && y <= z
3722
3723// stable, 3-6 compares, 0-5 swaps
3724
3725template <class _Compare, class _ForwardIterator>
3726unsigned
3727__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3728 _ForwardIterator __x4, _Compare __c)
3729{
3730 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3731 if (__c(*__x4, *__x3))
3732 {
3733 swap(*__x3, *__x4);
3734 ++__r;
3735 if (__c(*__x3, *__x2))
3736 {
3737 swap(*__x2, *__x3);
3738 ++__r;
3739 if (__c(*__x2, *__x1))
3740 {
3741 swap(*__x1, *__x2);
3742 ++__r;
3743 }
3744 }
3745 }
3746 return __r;
3747}
3748
3749// stable, 4-10 compares, 0-9 swaps
3750
3751template <class _Compare, class _ForwardIterator>
3752unsigned
3753__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3754 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3755{
3756 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3757 if (__c(*__x5, *__x4))
3758 {
3759 swap(*__x4, *__x5);
3760 ++__r;
3761 if (__c(*__x4, *__x3))
3762 {
3763 swap(*__x3, *__x4);
3764 ++__r;
3765 if (__c(*__x3, *__x2))
3766 {
3767 swap(*__x2, *__x3);
3768 ++__r;
3769 if (__c(*__x2, *__x1))
3770 {
3771 swap(*__x1, *__x2);
3772 ++__r;
3773 }
3774 }
3775 }
3776 }
3777 return __r;
3778}
3779
3780// Assumes size > 0
3781template <class _Compare, class _BirdirectionalIterator>
3782void
3783__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3784{
3785 _BirdirectionalIterator __lm1 = __last;
3786 for (--__lm1; __first != __lm1; ++__first)
3787 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003788 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003789 typename add_lvalue_reference<_Compare>::type>
3790 (__first, __last, __comp);
3791 if (__i != __first)
3792 swap(*__first, *__i);
3793 }
3794}
3795
3796template <class _Compare, class _BirdirectionalIterator>
3797void
3798__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3799{
3800 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3801 if (__first != __last)
3802 {
3803 _BirdirectionalIterator __i = __first;
3804 for (++__i; __i != __last; ++__i)
3805 {
3806 _BirdirectionalIterator __j = __i;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003807 value_type __t(_VSTD::move(*__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003808 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003809 *__j = _VSTD::move(*__k);
3810 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003811 }
3812 }
3813}
3814
3815template <class _Compare, class _RandomAccessIterator>
3816void
3817__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3818{
3819 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3820 _RandomAccessIterator __j = __first+2;
3821 __sort3<_Compare>(__first, __first+1, __j, __comp);
3822 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3823 {
3824 if (__comp(*__i, *__j))
3825 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003826 value_type __t(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003827 _RandomAccessIterator __k = __j;
3828 __j = __i;
3829 do
3830 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003831 *__j = _VSTD::move(*__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003832 __j = __k;
3833 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003834 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003835 }
3836 __j = __i;
3837 }
3838}
3839
3840template <class _Compare, class _RandomAccessIterator>
3841bool
3842__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3843{
3844 switch (__last - __first)
3845 {
3846 case 0:
3847 case 1:
3848 return true;
3849 case 2:
3850 if (__comp(*--__last, *__first))
3851 swap(*__first, *__last);
3852 return true;
3853 case 3:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003854 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003855 return true;
3856 case 4:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003857 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003858 return true;
3859 case 5:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003860 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003861 return true;
3862 }
3863 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3864 _RandomAccessIterator __j = __first+2;
3865 __sort3<_Compare>(__first, __first+1, __j, __comp);
3866 const unsigned __limit = 8;
3867 unsigned __count = 0;
3868 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3869 {
3870 if (__comp(*__i, *__j))
3871 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003872 value_type __t(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003873 _RandomAccessIterator __k = __j;
3874 __j = __i;
3875 do
3876 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003877 *__j = _VSTD::move(*__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003878 __j = __k;
3879 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003880 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003881 if (++__count == __limit)
3882 return ++__i == __last;
3883 }
3884 __j = __i;
3885 }
3886 return true;
3887}
3888
3889template <class _Compare, class _BirdirectionalIterator>
3890void
3891__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3892 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3893{
3894 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3895 if (__first1 != __last1)
3896 {
3897 __destruct_n __d(0);
3898 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3899 value_type* __last2 = __first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003900 ::new(__last2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003901 __d.__incr((value_type*)0);
3902 for (++__last2; ++__first1 != __last1; ++__last2)
3903 {
3904 value_type* __j2 = __last2;
3905 value_type* __i2 = __j2;
3906 if (__comp(*__first1, *--__i2))
3907 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003908 ::new(__j2) value_type(_VSTD::move(*__i2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003909 __d.__incr((value_type*)0);
3910 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003911 *__j2 = _VSTD::move(*__i2);
3912 *__j2 = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003913 }
3914 else
3915 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003916 ::new(__j2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003917 __d.__incr((value_type*)0);
3918 }
3919 }
3920 __h.release();
3921 }
3922}
3923
3924template <class _Compare, class _RandomAccessIterator>
3925void
3926__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3927{
3928 // _Compare is known to be a reference type
3929 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3930 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant1468b662010-11-19 22:17:28 +00003931 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3932 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003933 while (true)
3934 {
3935 __restart:
3936 difference_type __len = __last - __first;
3937 switch (__len)
3938 {
3939 case 0:
3940 case 1:
3941 return;
3942 case 2:
3943 if (__comp(*--__last, *__first))
3944 swap(*__first, *__last);
3945 return;
3946 case 3:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003947 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003948 return;
3949 case 4:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003950 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003951 return;
3952 case 5:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003953 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003954 return;
3955 }
3956 if (__len <= __limit)
3957 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003958 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003959 return;
3960 }
3961 // __len > 5
3962 _RandomAccessIterator __m = __first;
3963 _RandomAccessIterator __lm1 = __last;
3964 --__lm1;
3965 unsigned __n_swaps;
3966 {
3967 difference_type __delta;
3968 if (__len >= 1000)
3969 {
3970 __delta = __len/2;
3971 __m += __delta;
3972 __delta /= 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003973 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003974 }
3975 else
3976 {
3977 __delta = __len/2;
3978 __m += __delta;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003979 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003980 }
3981 }
3982 // *__m is median
3983 // partition [__first, __m) < *__m and *__m <= [__m, __last)
3984 // (this inhibits tossing elements equivalent to __m around unnecessarily)
3985 _RandomAccessIterator __i = __first;
3986 _RandomAccessIterator __j = __lm1;
3987 // j points beyond range to be tested, *__m is known to be <= *__lm1
3988 // The search going up is known to be guarded but the search coming down isn't.
3989 // Prime the downward search with a guard.
3990 if (!__comp(*__i, *__m)) // if *__first == *__m
3991 {
3992 // *__first == *__m, *__first doesn't go in first part
3993 // manually guard downward moving __j against __i
3994 while (true)
3995 {
3996 if (__i == --__j)
3997 {
3998 // *__first == *__m, *__m <= all other elements
3999 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
4000 ++__i; // __first + 1
4001 __j = __last;
4002 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
4003 {
4004 while (true)
4005 {
4006 if (__i == __j)
4007 return; // [__first, __last) all equivalent elements
4008 if (__comp(*__first, *__i))
4009 {
4010 swap(*__i, *__j);
4011 ++__n_swaps;
4012 ++__i;
4013 break;
4014 }
4015 ++__i;
4016 }
4017 }
4018 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
4019 if (__i == __j)
4020 return;
4021 while (true)
4022 {
4023 while (!__comp(*__first, *__i))
4024 ++__i;
4025 while (__comp(*__first, *--__j))
4026 ;
4027 if (__i >= __j)
4028 break;
4029 swap(*__i, *__j);
4030 ++__n_swaps;
4031 ++__i;
4032 }
4033 // [__first, __i) == *__first and *__first < [__i, __last)
4034 // The first part is sorted, sort the secod part
Howard Hinnant0949eed2011-06-30 21:18:19 +00004035 // _VSTD::__sort<_Compare>(__i, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004036 __first = __i;
4037 goto __restart;
4038 }
4039 if (__comp(*__j, *__m))
4040 {
4041 swap(*__i, *__j);
4042 ++__n_swaps;
4043 break; // found guard for downward moving __j, now use unguarded partition
4044 }
4045 }
4046 }
4047 // It is known that *__i < *__m
4048 ++__i;
4049 // j points beyond range to be tested, *__m is known to be <= *__lm1
4050 // if not yet partitioned...
4051 if (__i < __j)
4052 {
4053 // known that *(__i - 1) < *__m
4054 // known that __i <= __m
4055 while (true)
4056 {
4057 // __m still guards upward moving __i
4058 while (__comp(*__i, *__m))
4059 ++__i;
4060 // It is now known that a guard exists for downward moving __j
4061 while (!__comp(*--__j, *__m))
4062 ;
4063 if (__i > __j)
4064 break;
4065 swap(*__i, *__j);
4066 ++__n_swaps;
4067 // It is known that __m != __j
4068 // If __m just moved, follow it
4069 if (__m == __i)
4070 __m = __j;
4071 ++__i;
4072 }
4073 }
4074 // [__first, __i) < *__m and *__m <= [__i, __last)
4075 if (__i != __m && __comp(*__m, *__i))
4076 {
4077 swap(*__i, *__m);
4078 ++__n_swaps;
4079 }
4080 // [__first, __i) < *__i and *__i <= [__i+1, __last)
4081 // If we were given a perfect partition, see if insertion sort is quick...
4082 if (__n_swaps == 0)
4083 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004084 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
4085 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004086 {
4087 if (__fs)
4088 return;
4089 __last = __i;
4090 continue;
4091 }
4092 else
4093 {
4094 if (__fs)
4095 {
4096 __first = ++__i;
4097 continue;
4098 }
4099 }
4100 }
4101 // sort smaller range with recursive call and larger with tail recursion elimination
4102 if (__i - __first < __last - __i)
4103 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004104 _VSTD::__sort<_Compare>(__first, __i, __comp);
4105 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004106 __first = ++__i;
4107 }
4108 else
4109 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004110 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4111 // _VSTD::__sort<_Compare>(__first, __i, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004112 __last = __i;
4113 }
4114 }
4115}
4116
4117// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
4118template <class _RandomAccessIterator, class _Compare>
4119inline _LIBCPP_INLINE_VISIBILITY
4120void
4121sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4122{
Howard Hinnant5e571422013-08-23 20:10:18 +00004123#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004124 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4125 __debug_less<_Compare> __c(__comp);
4126 __sort<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004127#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004128 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4129 __sort<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004130#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004131}
4132
4133template <class _RandomAccessIterator>
4134inline _LIBCPP_INLINE_VISIBILITY
4135void
4136sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4137{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004138 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004139}
4140
4141template <class _Tp>
4142inline _LIBCPP_INLINE_VISIBILITY
4143void
4144sort(_Tp** __first, _Tp** __last)
4145{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004146 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004147}
4148
4149template <class _Tp>
4150inline _LIBCPP_INLINE_VISIBILITY
4151void
4152sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4153{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004154 _VSTD::sort(__first.base(), __last.base());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004155}
4156
Howard Hinnant7a563db2011-09-14 18:33:51 +00004157template <class _Tp, class _Compare>
4158inline _LIBCPP_INLINE_VISIBILITY
4159void
4160sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4161{
4162 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4163 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4164}
4165
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004166_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4167_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4168_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4169_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4170_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4171_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4172_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4173_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4174_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4175_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4176_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4177_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>&))
4178_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4179_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4180_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 +00004181
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004182_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4183_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4184_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4185_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4186_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4187_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4188_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4189_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4190_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4191_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4192_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4193_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>&))
4194_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4195_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4196_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 +00004197
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004198_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 Hinnantbc8d3f92010-05-11 19:42:16 +00004199
4200// lower_bound
4201
4202template <class _Compare, class _ForwardIterator, class _Tp>
4203_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004204__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004205{
4206 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004207 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004208 while (__len != 0)
4209 {
4210 difference_type __l2 = __len / 2;
4211 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004212 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004213 if (__comp(*__m, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004214 {
4215 __first = ++__m;
4216 __len -= __l2 + 1;
4217 }
4218 else
4219 __len = __l2;
4220 }
4221 return __first;
4222}
4223
4224template <class _ForwardIterator, class _Tp, class _Compare>
4225inline _LIBCPP_INLINE_VISIBILITY
4226_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004227lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004228{
Howard Hinnant5e571422013-08-23 20:10:18 +00004229#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004230 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4231 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004232 return __lower_bound<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004233#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004234 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004235 return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004236#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004237}
4238
4239template <class _ForwardIterator, class _Tp>
4240inline _LIBCPP_INLINE_VISIBILITY
4241_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004242lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004243{
Howard Hinnant78b68282011-10-22 20:59:45 +00004244 return _VSTD::lower_bound(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004245 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4246}
4247
4248// upper_bound
4249
4250template <class _Compare, class _ForwardIterator, class _Tp>
4251_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004252__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004253{
4254 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004255 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004256 while (__len != 0)
4257 {
4258 difference_type __l2 = __len / 2;
4259 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004260 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004261 if (__comp(__value_, *__m))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004262 __len = __l2;
4263 else
4264 {
4265 __first = ++__m;
4266 __len -= __l2 + 1;
4267 }
4268 }
4269 return __first;
4270}
4271
4272template <class _ForwardIterator, class _Tp, class _Compare>
4273inline _LIBCPP_INLINE_VISIBILITY
4274_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004275upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004276{
Howard Hinnant5e571422013-08-23 20:10:18 +00004277#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004278 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4279 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004280 return __upper_bound<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004281#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004282 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004283 return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004284#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004285}
4286
4287template <class _ForwardIterator, class _Tp>
4288inline _LIBCPP_INLINE_VISIBILITY
4289_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004290upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004291{
Howard Hinnant78b68282011-10-22 20:59:45 +00004292 return _VSTD::upper_bound(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004293 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4294}
4295
4296// equal_range
4297
4298template <class _Compare, class _ForwardIterator, class _Tp>
4299pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004300__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004301{
4302 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004303 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004304 while (__len != 0)
4305 {
4306 difference_type __l2 = __len / 2;
4307 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004308 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004309 if (__comp(*__m, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004310 {
4311 __first = ++__m;
4312 __len -= __l2 + 1;
4313 }
Howard Hinnant78b68282011-10-22 20:59:45 +00004314 else if (__comp(__value_, *__m))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004315 {
4316 __last = __m;
4317 __len = __l2;
4318 }
4319 else
4320 {
4321 _ForwardIterator __mp1 = __m;
4322 return pair<_ForwardIterator, _ForwardIterator>
4323 (
Howard Hinnant78b68282011-10-22 20:59:45 +00004324 __lower_bound<_Compare>(__first, __m, __value_, __comp),
4325 __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004326 );
4327 }
4328 }
4329 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4330}
4331
4332template <class _ForwardIterator, class _Tp, class _Compare>
4333inline _LIBCPP_INLINE_VISIBILITY
4334pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004335equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004336{
Howard Hinnant5e571422013-08-23 20:10:18 +00004337#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004338 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4339 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004340 return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004341#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004342 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004343 return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004344#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004345}
4346
4347template <class _ForwardIterator, class _Tp>
4348inline _LIBCPP_INLINE_VISIBILITY
4349pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004350equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004351{
Howard Hinnant78b68282011-10-22 20:59:45 +00004352 return _VSTD::equal_range(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004353 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4354}
4355
4356// binary_search
4357
4358template <class _Compare, class _ForwardIterator, class _Tp>
4359inline _LIBCPP_INLINE_VISIBILITY
4360bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004361__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004362{
Howard Hinnant78b68282011-10-22 20:59:45 +00004363 __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4364 return __first != __last && !__comp(__value_, *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004365}
4366
4367template <class _ForwardIterator, class _Tp, class _Compare>
4368inline _LIBCPP_INLINE_VISIBILITY
4369bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004370binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004371{
Howard Hinnant5e571422013-08-23 20:10:18 +00004372#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004373 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4374 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004375 return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004376#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004377 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004378 return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004379#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004380}
4381
4382template <class _ForwardIterator, class _Tp>
4383inline _LIBCPP_INLINE_VISIBILITY
4384bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004385binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004386{
Howard Hinnant78b68282011-10-22 20:59:45 +00004387 return _VSTD::binary_search(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004388 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4389}
4390
4391// merge
4392
4393template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4394_OutputIterator
4395__merge(_InputIterator1 __first1, _InputIterator1 __last1,
4396 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4397{
4398 for (; __first1 != __last1; ++__result)
4399 {
4400 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004401 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004402 if (__comp(*__first2, *__first1))
4403 {
4404 *__result = *__first2;
4405 ++__first2;
4406 }
4407 else
4408 {
4409 *__result = *__first1;
4410 ++__first1;
4411 }
4412 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00004413 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004414}
4415
4416template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4417inline _LIBCPP_INLINE_VISIBILITY
4418_OutputIterator
4419merge(_InputIterator1 __first1, _InputIterator1 __last1,
4420 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4421{
Howard Hinnant5e571422013-08-23 20:10:18 +00004422#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004423 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4424 __debug_less<_Compare> __c(__comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004425 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004426#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004427 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004428 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004429#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004430}
4431
4432template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4433inline _LIBCPP_INLINE_VISIBILITY
4434_OutputIterator
4435merge(_InputIterator1 __first1, _InputIterator1 __last1,
4436 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4437{
4438 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4439 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4440 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4441}
4442
4443// inplace_merge
4444
Marshall Clowa3795762015-07-29 16:25:45 +00004445template <class _Compare, class _InputIterator1, class _InputIterator2,
4446 class _OutputIterator>
4447void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4448 _InputIterator2 __first2, _InputIterator2 __last2,
4449 _OutputIterator __result, _Compare __comp)
4450{
4451 for (; __first1 != __last1; ++__result)
4452 {
4453 if (__first2 == __last2)
4454 {
4455 _VSTD::move(__first1, __last1, __result);
4456 return;
4457 }
4458
4459 if (__comp(*__first2, *__first1))
4460 {
4461 *__result = _VSTD::move(*__first2);
4462 ++__first2;
4463 }
4464 else
4465 {
4466 *__result = _VSTD::move(*__first1);
4467 ++__first1;
4468 }
4469 }
4470 // __first2 through __last2 are already in the right spot.
4471}
4472
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004473template <class _Compare, class _BidirectionalIterator>
4474void
4475__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4476 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4477 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4478 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4479{
4480 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004481 __destruct_n __d(0);
4482 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4483 if (__len1 <= __len2)
4484 {
4485 value_type* __p = __buff;
Eric Fiselierb9919752014-10-27 19:28:20 +00004486 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004487 ::new(__p) value_type(_VSTD::move(*__i));
Marshall Clowa3795762015-07-29 16:25:45 +00004488 __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004489 }
4490 else
4491 {
4492 value_type* __p = __buff;
Eric Fiselierb9919752014-10-27 19:28:20 +00004493 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004494 ::new(__p) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004495 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4496 typedef reverse_iterator<value_type*> _Rv;
Aditya Kumarfdb4f172016-08-25 11:52:38 +00004497 __half_inplace_merge(_Rv(__p), _Rv(__buff),
Marshall Clowa3795762015-07-29 16:25:45 +00004498 _RBi(__middle), _RBi(__first),
Marshall Clow25a78dc2017-08-28 23:16:13 +00004499 _RBi(__last), __invert<_Compare>(__comp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004500 }
4501}
4502
4503template <class _Compare, class _BidirectionalIterator>
4504void
4505__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4506 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4507 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4508 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4509{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004510 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4511 while (true)
4512 {
4513 // if __middle == __last, we're done
4514 if (__len2 == 0)
4515 return;
Marshall Clowe809f4c2015-02-02 16:44:11 +00004516 if (__len1 <= __buff_size || __len2 <= __buff_size)
4517 return __buffered_inplace_merge<_Compare>
4518 (__first, __middle, __last, __comp, __len1, __len2, __buff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004519 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
Eric Fiselierb9919752014-10-27 19:28:20 +00004520 for (; true; ++__first, (void) --__len1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004521 {
4522 if (__len1 == 0)
4523 return;
4524 if (__comp(*__middle, *__first))
4525 break;
4526 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004527 // __first < __middle < __last
4528 // *__first > *__middle
4529 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4530 // all elements in:
4531 // [__first, __m1) <= [__middle, __m2)
4532 // [__middle, __m2) < [__m1, __middle)
4533 // [__m1, __middle) <= [__m2, __last)
4534 // and __m1 or __m2 is in the middle of its range
4535 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4536 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4537 difference_type __len11; // distance(__first, __m1)
4538 difference_type __len21; // distance(__middle, __m2)
4539 // binary search smaller range
4540 if (__len1 < __len2)
4541 { // __len >= 1, __len2 >= 2
4542 __len21 = __len2 / 2;
4543 __m2 = __middle;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004544 _VSTD::advance(__m2, __len21);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004545 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004546 __len11 = _VSTD::distance(__first, __m1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004547 }
4548 else
4549 {
4550 if (__len1 == 1)
4551 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4552 // It is known *__first > *__middle
4553 swap(*__first, *__middle);
4554 return;
4555 }
4556 // __len1 >= 2, __len2 >= 1
4557 __len11 = __len1 / 2;
4558 __m1 = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004559 _VSTD::advance(__m1, __len11);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004560 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004561 __len21 = _VSTD::distance(__middle, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004562 }
4563 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4564 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4565 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4566 // swap middle two partitions
Howard Hinnant0949eed2011-06-30 21:18:19 +00004567 __middle = _VSTD::rotate(__m1, __middle, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004568 // __len12 and __len21 now have swapped meanings
4569 // merge smaller range with recurisve call and larger with tail recursion elimination
4570 if (__len11 + __len21 < __len12 + __len22)
4571 {
4572 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4573// __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4574 __first = __middle;
4575 __middle = __m2;
4576 __len1 = __len12;
4577 __len2 = __len22;
4578 }
4579 else
4580 {
4581 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4582// __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4583 __last = __middle;
4584 __middle = __m1;
4585 __len1 = __len11;
4586 __len2 = __len21;
4587 }
4588 }
4589}
4590
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004591template <class _BidirectionalIterator, class _Compare>
4592inline _LIBCPP_INLINE_VISIBILITY
4593void
4594inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4595 _Compare __comp)
4596{
4597 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4598 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004599 difference_type __len1 = _VSTD::distance(__first, __middle);
4600 difference_type __len2 = _VSTD::distance(__middle, __last);
4601 difference_type __buf_size = _VSTD::min(__len1, __len2);
Marshall Clow4c2684c2015-02-02 17:35:53 +00004602 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4603 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
4604
Howard Hinnant5e571422013-08-23 20:10:18 +00004605#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004606 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4607 __debug_less<_Compare> __c(__comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004608 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004609 __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004610#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004611 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004612 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004613 __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004614#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004615}
4616
4617template <class _BidirectionalIterator>
4618inline _LIBCPP_INLINE_VISIBILITY
4619void
4620inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4621{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004622 _VSTD::inplace_merge(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004623 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4624}
4625
4626// stable_sort
4627
4628template <class _Compare, class _InputIterator1, class _InputIterator2>
4629void
4630__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4631 _InputIterator2 __first2, _InputIterator2 __last2,
4632 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4633{
4634 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4635 __destruct_n __d(0);
4636 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4637 for (; true; ++__result)
4638 {
4639 if (__first1 == __last1)
4640 {
4641 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
Howard Hinnant0949eed2011-06-30 21:18:19 +00004642 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004643 __h.release();
4644 return;
4645 }
4646 if (__first2 == __last2)
4647 {
4648 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
Howard Hinnant0949eed2011-06-30 21:18:19 +00004649 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004650 __h.release();
4651 return;
4652 }
4653 if (__comp(*__first2, *__first1))
4654 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004655 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004656 __d.__incr((value_type*)0);
4657 ++__first2;
4658 }
4659 else
4660 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004661 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004662 __d.__incr((value_type*)0);
4663 ++__first1;
4664 }
4665 }
4666}
4667
4668template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4669void
4670__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4671 _InputIterator2 __first2, _InputIterator2 __last2,
4672 _OutputIterator __result, _Compare __comp)
4673{
4674 for (; __first1 != __last1; ++__result)
4675 {
4676 if (__first2 == __last2)
4677 {
4678 for (; __first1 != __last1; ++__first1, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004679 *__result = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004680 return;
4681 }
4682 if (__comp(*__first2, *__first1))
4683 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004684 *__result = _VSTD::move(*__first2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004685 ++__first2;
4686 }
4687 else
4688 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004689 *__result = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004690 ++__first1;
4691 }
4692 }
4693 for (; __first2 != __last2; ++__first2, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004694 *__result = _VSTD::move(*__first2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004695}
4696
4697template <class _Compare, class _RandomAccessIterator>
4698void
4699__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4700 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4701 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4702
4703template <class _Compare, class _RandomAccessIterator>
4704void
4705__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4706 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4707 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4708{
4709 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4710 switch (__len)
4711 {
4712 case 0:
4713 return;
4714 case 1:
Howard Hinnant0949eed2011-06-30 21:18:19 +00004715 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004716 return;
4717 case 2:
4718 __destruct_n __d(0);
4719 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4720 if (__comp(*--__last1, *__first1))
4721 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004722 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004723 __d.__incr((value_type*)0);
4724 ++__first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004725 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004726 }
4727 else
4728 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004729 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004730 __d.__incr((value_type*)0);
4731 ++__first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004732 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004733 }
4734 __h2.release();
4735 return;
4736 }
4737 if (__len <= 8)
4738 {
4739 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4740 return;
4741 }
4742 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4743 _RandomAccessIterator __m = __first1 + __l2;
4744 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4745 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4746 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4747}
4748
4749template <class _Tp>
4750struct __stable_sort_switch
4751{
Howard Hinnant1468b662010-11-19 22:17:28 +00004752 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004753};
4754
4755template <class _Compare, class _RandomAccessIterator>
4756void
4757__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4758 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4759 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4760{
4761 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4762 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4763 switch (__len)
4764 {
4765 case 0:
4766 case 1:
4767 return;
4768 case 2:
4769 if (__comp(*--__last, *__first))
4770 swap(*__first, *__last);
4771 return;
4772 }
4773 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4774 {
4775 __insertion_sort<_Compare>(__first, __last, __comp);
4776 return;
4777 }
4778 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4779 _RandomAccessIterator __m = __first + __l2;
4780 if (__len <= __buff_size)
4781 {
4782 __destruct_n __d(0);
4783 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4784 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4785 __d.__set(__l2, (value_type*)0);
4786 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4787 __d.__set(__len, (value_type*)0);
4788 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4789// __merge<_Compare>(move_iterator<value_type*>(__buff),
4790// move_iterator<value_type*>(__buff + __l2),
4791// move_iterator<_RandomAccessIterator>(__buff + __l2),
4792// move_iterator<_RandomAccessIterator>(__buff + __len),
4793// __first, __comp);
4794 return;
4795 }
4796 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4797 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4798 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4799}
4800
4801template <class _RandomAccessIterator, class _Compare>
4802inline _LIBCPP_INLINE_VISIBILITY
4803void
4804stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4805{
4806 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4807 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4808 difference_type __len = __last - __first;
4809 pair<value_type*, ptrdiff_t> __buf(0, 0);
4810 unique_ptr<value_type, __return_temporary_buffer> __h;
4811 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4812 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004813 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004814 __h.reset(__buf.first);
4815 }
Howard Hinnant5e571422013-08-23 20:10:18 +00004816#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004817 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4818 __debug_less<_Compare> __c(__comp);
4819 __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004820#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004821 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4822 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004823#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004824}
4825
4826template <class _RandomAccessIterator>
4827inline _LIBCPP_INLINE_VISIBILITY
4828void
4829stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4830{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004831 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004832}
4833
4834// is_heap_until
4835
4836template <class _RandomAccessIterator, class _Compare>
4837_RandomAccessIterator
4838is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4839{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004840 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004841 difference_type __len = __last - __first;
4842 difference_type __p = 0;
4843 difference_type __c = 1;
4844 _RandomAccessIterator __pp = __first;
4845 while (__c < __len)
4846 {
4847 _RandomAccessIterator __cp = __first + __c;
4848 if (__comp(*__pp, *__cp))
4849 return __cp;
4850 ++__c;
4851 ++__cp;
4852 if (__c == __len)
4853 return __last;
4854 if (__comp(*__pp, *__cp))
4855 return __cp;
4856 ++__p;
4857 ++__pp;
4858 __c = 2 * __p + 1;
4859 }
4860 return __last;
4861}
4862
Howard Hinnant324bb032010-08-22 00:02:43 +00004863template<class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004864inline _LIBCPP_INLINE_VISIBILITY
4865_RandomAccessIterator
4866is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4867{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004868 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004869}
4870
4871// is_heap
4872
4873template <class _RandomAccessIterator, class _Compare>
4874inline _LIBCPP_INLINE_VISIBILITY
4875bool
4876is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4877{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004878 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004879}
4880
Howard Hinnant324bb032010-08-22 00:02:43 +00004881template<class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004882inline _LIBCPP_INLINE_VISIBILITY
4883bool
4884is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4885{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004886 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004887}
4888
4889// push_heap
4890
4891template <class _Compare, class _RandomAccessIterator>
4892void
David Majnemercb8757a2014-07-22 06:07:09 +00004893__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4894 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004895{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004896 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4897 if (__len > 1)
4898 {
4899 __len = (__len - 2) / 2;
4900 _RandomAccessIterator __ptr = __first + __len;
4901 if (__comp(*__ptr, *--__last))
4902 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004903 value_type __t(_VSTD::move(*__last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004904 do
4905 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004906 *__last = _VSTD::move(*__ptr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004907 __last = __ptr;
4908 if (__len == 0)
4909 break;
4910 __len = (__len - 1) / 2;
4911 __ptr = __first + __len;
4912 } while (__comp(*__ptr, __t));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004913 *__last = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004914 }
4915 }
4916}
4917
4918template <class _RandomAccessIterator, class _Compare>
4919inline _LIBCPP_INLINE_VISIBILITY
4920void
4921push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4922{
Howard Hinnant5e571422013-08-23 20:10:18 +00004923#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004924 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4925 __debug_less<_Compare> __c(__comp);
David Majnemercb8757a2014-07-22 06:07:09 +00004926 __sift_up<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004927#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004928 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
David Majnemercb8757a2014-07-22 06:07:09 +00004929 __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004930#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004931}
4932
4933template <class _RandomAccessIterator>
4934inline _LIBCPP_INLINE_VISIBILITY
4935void
4936push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4937{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004938 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004939}
4940
4941// pop_heap
4942
4943template <class _Compare, class _RandomAccessIterator>
David Majnemercb8757a2014-07-22 06:07:09 +00004944void
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00004945__sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
4946 _Compare __comp,
David Majnemercb8757a2014-07-22 06:07:09 +00004947 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4948 _RandomAccessIterator __start)
4949{
4950 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4951 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4952 // left-child of __start is at 2 * __start + 1
4953 // right-child of __start is at 2 * __start + 2
4954 difference_type __child = __start - __first;
4955
4956 if (__len < 2 || (__len - 2) / 2 < __child)
4957 return;
4958
4959 __child = 2 * __child + 1;
4960 _RandomAccessIterator __child_i = __first + __child;
4961
4962 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4963 // right-child exists and is greater than left-child
4964 ++__child_i;
4965 ++__child;
4966 }
4967
4968 // check if we are in heap-order
4969 if (__comp(*__child_i, *__start))
4970 // we are, __start is larger than it's largest child
4971 return;
4972
4973 value_type __top(_VSTD::move(*__start));
4974 do
4975 {
4976 // we are not in heap-order, swap the parent with it's largest child
4977 *__start = _VSTD::move(*__child_i);
4978 __start = __child_i;
4979
4980 if ((__len - 2) / 2 < __child)
4981 break;
4982
4983 // recompute the child based off of the updated parent
4984 __child = 2 * __child + 1;
4985 __child_i = __first + __child;
4986
4987 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4988 // right-child exists and is greater than left-child
4989 ++__child_i;
4990 ++__child;
4991 }
4992
4993 // check if we are in heap-order
4994 } while (!__comp(*__child_i, __top));
4995 *__start = _VSTD::move(__top);
4996}
4997
4998template <class _Compare, class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004999inline _LIBCPP_INLINE_VISIBILITY
5000void
5001__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
5002 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
5003{
5004 if (__len > 1)
5005 {
5006 swap(*__first, *--__last);
David Majnemercb8757a2014-07-22 06:07:09 +00005007 __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005008 }
5009}
5010
5011template <class _RandomAccessIterator, class _Compare>
5012inline _LIBCPP_INLINE_VISIBILITY
5013void
5014pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5015{
Howard Hinnant5e571422013-08-23 20:10:18 +00005016#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005017 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5018 __debug_less<_Compare> __c(__comp);
5019 __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00005020#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005021 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5022 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00005023#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005024}
5025
5026template <class _RandomAccessIterator>
5027inline _LIBCPP_INLINE_VISIBILITY
5028void
5029pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5030{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005031 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005032}
5033
5034// make_heap
5035
5036template <class _Compare, class _RandomAccessIterator>
5037void
5038__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5039{
5040 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5041 difference_type __n = __last - __first;
5042 if (__n > 1)
5043 {
David Majnemercb8757a2014-07-22 06:07:09 +00005044 // start from the first parent, there is no need to consider children
5045 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
5046 {
5047 __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
5048 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005049 }
5050}
5051
5052template <class _RandomAccessIterator, class _Compare>
5053inline _LIBCPP_INLINE_VISIBILITY
5054void
5055make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5056{
Howard Hinnant5e571422013-08-23 20:10:18 +00005057#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005058 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5059 __debug_less<_Compare> __c(__comp);
5060 __make_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005061#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005062 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5063 __make_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005064#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005065}
5066
5067template <class _RandomAccessIterator>
5068inline _LIBCPP_INLINE_VISIBILITY
5069void
5070make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5071{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005072 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005073}
5074
5075// sort_heap
5076
5077template <class _Compare, class _RandomAccessIterator>
5078void
5079__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5080{
5081 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5082 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
5083 __pop_heap<_Compare>(__first, __last, __comp, __n);
5084}
5085
5086template <class _RandomAccessIterator, class _Compare>
5087inline _LIBCPP_INLINE_VISIBILITY
5088void
5089sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5090{
Howard Hinnant5e571422013-08-23 20:10:18 +00005091#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005092 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5093 __debug_less<_Compare> __c(__comp);
5094 __sort_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005095#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005096 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5097 __sort_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005098#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005099}
5100
5101template <class _RandomAccessIterator>
5102inline _LIBCPP_INLINE_VISIBILITY
5103void
5104sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5105{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005106 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005107}
5108
5109// partial_sort
5110
5111template <class _Compare, class _RandomAccessIterator>
5112void
5113__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5114 _Compare __comp)
5115{
5116 __make_heap<_Compare>(__first, __middle, __comp);
5117 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5118 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5119 {
5120 if (__comp(*__i, *__first))
5121 {
5122 swap(*__i, *__first);
David Majnemercb8757a2014-07-22 06:07:09 +00005123 __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005124 }
5125 }
5126 __sort_heap<_Compare>(__first, __middle, __comp);
5127}
5128
5129template <class _RandomAccessIterator, class _Compare>
5130inline _LIBCPP_INLINE_VISIBILITY
5131void
5132partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5133 _Compare __comp)
5134{
Howard Hinnant5e571422013-08-23 20:10:18 +00005135#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005136 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5137 __debug_less<_Compare> __c(__comp);
5138 __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005139#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005140 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5141 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005142#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005143}
5144
5145template <class _RandomAccessIterator>
5146inline _LIBCPP_INLINE_VISIBILITY
5147void
5148partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5149{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005150 _VSTD::partial_sort(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005151 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5152}
5153
5154// partial_sort_copy
5155
5156template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5157_RandomAccessIterator
5158__partial_sort_copy(_InputIterator __first, _InputIterator __last,
5159 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5160{
5161 _RandomAccessIterator __r = __result_first;
5162 if (__r != __result_last)
5163 {
Eric Fiselierb9919752014-10-27 19:28:20 +00005164 for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005165 *__r = *__first;
5166 __make_heap<_Compare>(__result_first, __r, __comp);
David Majnemercb8757a2014-07-22 06:07:09 +00005167 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005168 for (; __first != __last; ++__first)
5169 if (__comp(*__first, *__result_first))
5170 {
5171 *__result_first = *__first;
David Majnemercb8757a2014-07-22 06:07:09 +00005172 __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005173 }
5174 __sort_heap<_Compare>(__result_first, __r, __comp);
5175 }
5176 return __r;
5177}
5178
5179template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5180inline _LIBCPP_INLINE_VISIBILITY
5181_RandomAccessIterator
5182partial_sort_copy(_InputIterator __first, _InputIterator __last,
5183 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5184{
Howard Hinnant5e571422013-08-23 20:10:18 +00005185#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005186 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5187 __debug_less<_Compare> __c(__comp);
5188 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005189#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005190 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5191 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005192#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005193}
5194
5195template <class _InputIterator, class _RandomAccessIterator>
5196inline _LIBCPP_INLINE_VISIBILITY
5197_RandomAccessIterator
5198partial_sort_copy(_InputIterator __first, _InputIterator __last,
5199 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5200{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005201 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005202 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5203}
5204
5205// nth_element
5206
5207template <class _Compare, class _RandomAccessIterator>
5208void
5209__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5210{
5211 // _Compare is known to be a reference type
5212 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5213 const difference_type __limit = 7;
5214 while (true)
5215 {
5216 __restart:
Howard Hinnant8292d742011-12-29 17:45:35 +00005217 if (__nth == __last)
5218 return;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005219 difference_type __len = __last - __first;
5220 switch (__len)
5221 {
5222 case 0:
5223 case 1:
5224 return;
5225 case 2:
5226 if (__comp(*--__last, *__first))
5227 swap(*__first, *__last);
5228 return;
5229 case 3:
5230 {
5231 _RandomAccessIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00005232 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005233 return;
5234 }
5235 }
5236 if (__len <= __limit)
5237 {
5238 __selection_sort<_Compare>(__first, __last, __comp);
5239 return;
5240 }
5241 // __len > __limit >= 3
5242 _RandomAccessIterator __m = __first + __len/2;
5243 _RandomAccessIterator __lm1 = __last;
Howard Hinnant0949eed2011-06-30 21:18:19 +00005244 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005245 // *__m is median
5246 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5247 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5248 _RandomAccessIterator __i = __first;
5249 _RandomAccessIterator __j = __lm1;
5250 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5251 // The search going up is known to be guarded but the search coming down isn't.
5252 // Prime the downward search with a guard.
5253 if (!__comp(*__i, *__m)) // if *__first == *__m
5254 {
5255 // *__first == *__m, *__first doesn't go in first part
5256 // manually guard downward moving __j against __i
5257 while (true)
5258 {
5259 if (__i == --__j)
5260 {
5261 // *__first == *__m, *__m <= all other elements
5262 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5263 ++__i; // __first + 1
5264 __j = __last;
5265 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5266 {
5267 while (true)
5268 {
5269 if (__i == __j)
5270 return; // [__first, __last) all equivalent elements
5271 if (__comp(*__first, *__i))
5272 {
5273 swap(*__i, *__j);
5274 ++__n_swaps;
5275 ++__i;
5276 break;
5277 }
5278 ++__i;
5279 }
5280 }
5281 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5282 if (__i == __j)
5283 return;
5284 while (true)
5285 {
5286 while (!__comp(*__first, *__i))
5287 ++__i;
5288 while (__comp(*__first, *--__j))
5289 ;
5290 if (__i >= __j)
5291 break;
5292 swap(*__i, *__j);
5293 ++__n_swaps;
5294 ++__i;
5295 }
5296 // [__first, __i) == *__first and *__first < [__i, __last)
5297 // The first part is sorted,
5298 if (__nth < __i)
5299 return;
5300 // __nth_element the secod part
5301 // __nth_element<_Compare>(__i, __nth, __last, __comp);
5302 __first = __i;
5303 goto __restart;
5304 }
5305 if (__comp(*__j, *__m))
5306 {
5307 swap(*__i, *__j);
5308 ++__n_swaps;
5309 break; // found guard for downward moving __j, now use unguarded partition
5310 }
5311 }
5312 }
5313 ++__i;
5314 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5315 // if not yet partitioned...
5316 if (__i < __j)
5317 {
5318 // known that *(__i - 1) < *__m
5319 while (true)
5320 {
5321 // __m still guards upward moving __i
5322 while (__comp(*__i, *__m))
5323 ++__i;
5324 // It is now known that a guard exists for downward moving __j
5325 while (!__comp(*--__j, *__m))
5326 ;
5327 if (__i >= __j)
5328 break;
5329 swap(*__i, *__j);
5330 ++__n_swaps;
5331 // It is known that __m != __j
5332 // If __m just moved, follow it
5333 if (__m == __i)
5334 __m = __j;
5335 ++__i;
5336 }
5337 }
5338 // [__first, __i) < *__m and *__m <= [__i, __last)
5339 if (__i != __m && __comp(*__m, *__i))
5340 {
5341 swap(*__i, *__m);
5342 ++__n_swaps;
5343 }
5344 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5345 if (__nth == __i)
5346 return;
5347 if (__n_swaps == 0)
5348 {
5349 // We were given a perfectly partitioned sequence. Coincidence?
5350 if (__nth < __i)
5351 {
5352 // Check for [__first, __i) already sorted
5353 __j = __m = __first;
5354 while (++__j != __i)
5355 {
5356 if (__comp(*__j, *__m))
5357 // not yet sorted, so sort
5358 goto not_sorted;
5359 __m = __j;
5360 }
5361 // [__first, __i) sorted
5362 return;
5363 }
5364 else
5365 {
5366 // Check for [__i, __last) already sorted
5367 __j = __m = __i;
5368 while (++__j != __last)
5369 {
5370 if (__comp(*__j, *__m))
5371 // not yet sorted, so sort
5372 goto not_sorted;
5373 __m = __j;
5374 }
5375 // [__i, __last) sorted
5376 return;
5377 }
5378 }
5379not_sorted:
5380 // __nth_element on range containing __nth
5381 if (__nth < __i)
5382 {
5383 // __nth_element<_Compare>(__first, __nth, __i, __comp);
5384 __last = __i;
5385 }
5386 else
5387 {
5388 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5389 __first = ++__i;
5390 }
5391 }
5392}
5393
5394template <class _RandomAccessIterator, class _Compare>
5395inline _LIBCPP_INLINE_VISIBILITY
5396void
5397nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5398{
Howard Hinnant5e571422013-08-23 20:10:18 +00005399#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005400 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5401 __debug_less<_Compare> __c(__comp);
5402 __nth_element<_Comp_ref>(__first, __nth, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005403#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005404 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5405 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005406#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005407}
5408
5409template <class _RandomAccessIterator>
5410inline _LIBCPP_INLINE_VISIBILITY
5411void
5412nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5413{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005414 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005415}
5416
5417// includes
5418
5419template <class _Compare, class _InputIterator1, class _InputIterator2>
5420bool
5421__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5422 _Compare __comp)
5423{
5424 for (; __first2 != __last2; ++__first1)
5425 {
5426 if (__first1 == __last1 || __comp(*__first2, *__first1))
5427 return false;
5428 if (!__comp(*__first1, *__first2))
5429 ++__first2;
5430 }
5431 return true;
5432}
5433
5434template <class _InputIterator1, class _InputIterator2, class _Compare>
5435inline _LIBCPP_INLINE_VISIBILITY
5436bool
5437includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5438 _Compare __comp)
5439{
Howard Hinnant5e571422013-08-23 20:10:18 +00005440#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005441 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5442 __debug_less<_Compare> __c(__comp);
5443 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005444#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005445 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5446 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005447#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005448}
5449
5450template <class _InputIterator1, class _InputIterator2>
5451inline _LIBCPP_INLINE_VISIBILITY
5452bool
5453includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5454{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005455 return _VSTD::includes(__first1, __last1, __first2, __last2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005456 __less<typename iterator_traits<_InputIterator1>::value_type,
5457 typename iterator_traits<_InputIterator2>::value_type>());
5458}
5459
5460// set_union
5461
5462template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5463_OutputIterator
5464__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5465 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5466{
5467 for (; __first1 != __last1; ++__result)
5468 {
5469 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005470 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005471 if (__comp(*__first2, *__first1))
5472 {
5473 *__result = *__first2;
5474 ++__first2;
5475 }
5476 else
5477 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005478 if (!__comp(*__first1, *__first2))
5479 ++__first2;
Marshall Clowc3fa9652017-10-30 15:50:00 +00005480 *__result = *__first1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005481 ++__first1;
5482 }
5483 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00005484 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005485}
5486
5487template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5488inline _LIBCPP_INLINE_VISIBILITY
5489_OutputIterator
5490set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5491 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5492{
Howard Hinnant5e571422013-08-23 20:10:18 +00005493#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005494 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5495 __debug_less<_Compare> __c(__comp);
5496 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005497#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005498 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5499 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005500#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005501}
5502
5503template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5504inline _LIBCPP_INLINE_VISIBILITY
5505_OutputIterator
5506set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5507 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5508{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005509 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005510 __less<typename iterator_traits<_InputIterator1>::value_type,
5511 typename iterator_traits<_InputIterator2>::value_type>());
5512}
5513
5514// set_intersection
5515
5516template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5517_OutputIterator
5518__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5519 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5520{
5521 while (__first1 != __last1 && __first2 != __last2)
5522 {
5523 if (__comp(*__first1, *__first2))
5524 ++__first1;
5525 else
5526 {
5527 if (!__comp(*__first2, *__first1))
5528 {
5529 *__result = *__first1;
5530 ++__result;
5531 ++__first1;
5532 }
5533 ++__first2;
5534 }
5535 }
5536 return __result;
5537}
5538
5539template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5540inline _LIBCPP_INLINE_VISIBILITY
5541_OutputIterator
5542set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5543 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5544{
Howard Hinnant5e571422013-08-23 20:10:18 +00005545#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005546 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5547 __debug_less<_Compare> __c(__comp);
5548 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005549#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005550 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5551 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005552#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005553}
5554
5555template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5556inline _LIBCPP_INLINE_VISIBILITY
5557_OutputIterator
5558set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5559 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5560{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005561 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005562 __less<typename iterator_traits<_InputIterator1>::value_type,
5563 typename iterator_traits<_InputIterator2>::value_type>());
5564}
5565
5566// set_difference
5567
5568template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5569_OutputIterator
5570__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5571 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5572{
5573 while (__first1 != __last1)
5574 {
5575 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005576 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005577 if (__comp(*__first1, *__first2))
5578 {
5579 *__result = *__first1;
5580 ++__result;
5581 ++__first1;
5582 }
5583 else
5584 {
5585 if (!__comp(*__first2, *__first1))
5586 ++__first1;
5587 ++__first2;
5588 }
5589 }
5590 return __result;
5591}
5592
5593template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5594inline _LIBCPP_INLINE_VISIBILITY
5595_OutputIterator
5596set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5597 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5598{
Howard Hinnant5e571422013-08-23 20:10:18 +00005599#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005600 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5601 __debug_less<_Compare> __c(__comp);
5602 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005603#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005604 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5605 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005606#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005607}
5608
5609template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5610inline _LIBCPP_INLINE_VISIBILITY
5611_OutputIterator
5612set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5613 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5614{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005615 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005616 __less<typename iterator_traits<_InputIterator1>::value_type,
5617 typename iterator_traits<_InputIterator2>::value_type>());
5618}
5619
5620// set_symmetric_difference
5621
5622template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5623_OutputIterator
5624__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5625 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5626{
5627 while (__first1 != __last1)
5628 {
5629 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005630 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005631 if (__comp(*__first1, *__first2))
5632 {
5633 *__result = *__first1;
5634 ++__result;
5635 ++__first1;
5636 }
5637 else
5638 {
5639 if (__comp(*__first2, *__first1))
5640 {
5641 *__result = *__first2;
5642 ++__result;
5643 }
5644 else
5645 ++__first1;
5646 ++__first2;
5647 }
5648 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00005649 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005650}
5651
5652template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5653inline _LIBCPP_INLINE_VISIBILITY
5654_OutputIterator
5655set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5656 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5657{
Howard Hinnant5e571422013-08-23 20:10:18 +00005658#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005659 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5660 __debug_less<_Compare> __c(__comp);
5661 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005662#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005663 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5664 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005665#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005666}
5667
5668template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5669inline _LIBCPP_INLINE_VISIBILITY
5670_OutputIterator
5671set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5672 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5673{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005674 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005675 __less<typename iterator_traits<_InputIterator1>::value_type,
5676 typename iterator_traits<_InputIterator2>::value_type>());
5677}
5678
5679// lexicographical_compare
5680
5681template <class _Compare, class _InputIterator1, class _InputIterator2>
5682bool
5683__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5684 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5685{
Eric Fiselierb9919752014-10-27 19:28:20 +00005686 for (; __first2 != __last2; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005687 {
5688 if (__first1 == __last1 || __comp(*__first1, *__first2))
5689 return true;
5690 if (__comp(*__first2, *__first1))
5691 return false;
5692 }
5693 return false;
5694}
5695
5696template <class _InputIterator1, class _InputIterator2, class _Compare>
5697inline _LIBCPP_INLINE_VISIBILITY
5698bool
5699lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5700 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5701{
Howard Hinnant5e571422013-08-23 20:10:18 +00005702#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005703 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5704 __debug_less<_Compare> __c(__comp);
5705 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005706#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005707 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5708 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005709#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005710}
5711
5712template <class _InputIterator1, class _InputIterator2>
5713inline _LIBCPP_INLINE_VISIBILITY
5714bool
5715lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5716 _InputIterator2 __first2, _InputIterator2 __last2)
5717{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005718 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005719 __less<typename iterator_traits<_InputIterator1>::value_type,
5720 typename iterator_traits<_InputIterator2>::value_type>());
5721}
5722
5723// next_permutation
5724
5725template <class _Compare, class _BidirectionalIterator>
5726bool
5727__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5728{
5729 _BidirectionalIterator __i = __last;
5730 if (__first == __last || __first == --__i)
5731 return false;
5732 while (true)
5733 {
5734 _BidirectionalIterator __ip1 = __i;
5735 if (__comp(*--__i, *__ip1))
5736 {
5737 _BidirectionalIterator __j = __last;
5738 while (!__comp(*__i, *--__j))
5739 ;
5740 swap(*__i, *__j);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005741 _VSTD::reverse(__ip1, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005742 return true;
5743 }
5744 if (__i == __first)
5745 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005746 _VSTD::reverse(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005747 return false;
5748 }
5749 }
5750}
5751
5752template <class _BidirectionalIterator, class _Compare>
5753inline _LIBCPP_INLINE_VISIBILITY
5754bool
5755next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5756{
Howard Hinnant5e571422013-08-23 20:10:18 +00005757#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005758 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5759 __debug_less<_Compare> __c(__comp);
5760 return __next_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005761#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005762 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5763 return __next_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005764#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005765}
5766
5767template <class _BidirectionalIterator>
5768inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005769bool
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005770next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5771{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005772 return _VSTD::next_permutation(__first, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005773 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5774}
5775
5776// prev_permutation
5777
5778template <class _Compare, class _BidirectionalIterator>
5779bool
5780__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5781{
5782 _BidirectionalIterator __i = __last;
5783 if (__first == __last || __first == --__i)
5784 return false;
5785 while (true)
5786 {
5787 _BidirectionalIterator __ip1 = __i;
5788 if (__comp(*__ip1, *--__i))
5789 {
5790 _BidirectionalIterator __j = __last;
5791 while (!__comp(*--__j, *__i))
5792 ;
5793 swap(*__i, *__j);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005794 _VSTD::reverse(__ip1, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005795 return true;
5796 }
5797 if (__i == __first)
5798 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005799 _VSTD::reverse(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005800 return false;
5801 }
5802 }
5803}
5804
5805template <class _BidirectionalIterator, class _Compare>
5806inline _LIBCPP_INLINE_VISIBILITY
5807bool
5808prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5809{
Howard Hinnant5e571422013-08-23 20:10:18 +00005810#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005811 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5812 __debug_less<_Compare> __c(__comp);
5813 return __prev_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005814#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005815 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5816 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005817#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005818}
5819
5820template <class _BidirectionalIterator>
5821inline _LIBCPP_INLINE_VISIBILITY
5822bool
5823prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5824{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005825 return _VSTD::prev_permutation(__first, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005826 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5827}
5828
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005829_LIBCPP_END_NAMESPACE_STD
5830
Eric Fiselier018a3d52017-05-31 22:07:49 +00005831_LIBCPP_POP_MACROS
5832
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005833#endif // _LIBCPP_ALGORITHM