blob: 08ca23ff61687435375f43f0a872d05c481093da [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- algorithm ---------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_ALGORITHM
12#define _LIBCPP_ALGORITHM
13
14/*
15 algorithm synopsis
16
17#include <initializer_list>
18
19namespace std
20{
21
22template <class InputIterator, class Predicate>
23 bool
24 all_of(InputIterator first, InputIterator last, Predicate pred);
25
26template <class InputIterator, class Predicate>
27 bool
28 any_of(InputIterator first, InputIterator last, Predicate pred);
29
30template <class InputIterator, class Predicate>
31 bool
32 none_of(InputIterator first, InputIterator last, Predicate pred);
33
34template <class InputIterator, class Function>
35 Function
36 for_each(InputIterator first, InputIterator last, Function f);
37
38template <class InputIterator, class T>
39 InputIterator
40 find(InputIterator first, InputIterator last, const T& value);
41
42template <class InputIterator, class Predicate>
43 InputIterator
44 find_if(InputIterator first, InputIterator last, Predicate pred);
45
46template<class InputIterator, class Predicate>
47 InputIterator
48 find_if_not(InputIterator first, InputIterator last, Predicate pred);
49
50template <class ForwardIterator1, class ForwardIterator2>
51 ForwardIterator1
52 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
53 ForwardIterator2 first2, ForwardIterator2 last2);
54
55template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
56 ForwardIterator1
57 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
58 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
59
60template <class ForwardIterator1, class ForwardIterator2>
61 ForwardIterator1
62 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
63 ForwardIterator2 first2, ForwardIterator2 last2);
64
65template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
66 ForwardIterator1
67 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
68 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
69
70template <class ForwardIterator>
71 ForwardIterator
72 adjacent_find(ForwardIterator first, ForwardIterator last);
73
74template <class ForwardIterator, class BinaryPredicate>
75 ForwardIterator
76 adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
77
78template <class InputIterator, class T>
79 typename iterator_traits<InputIterator>::difference_type
80 count(InputIterator first, InputIterator last, const T& value);
81
82template <class InputIterator, class Predicate>
83 typename iterator_traits<InputIterator>::difference_type
84 count_if(InputIterator first, InputIterator last, Predicate pred);
85
86template <class InputIterator1, class InputIterator2>
87 pair<InputIterator1, InputIterator2>
88 mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
89
Marshall Clowb30abdd2013-05-09 21:14:23 +000090template <class InputIterator1, class InputIterator2>
91 pair<InputIterator1, InputIterator2>
Aditya Kumarfdb4f172016-08-25 11:52:38 +000092 mismatch(InputIterator1 first1, InputIterator1 last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +000093 InputIterator2 first2, InputIterator2 last2); // **C++14**
94
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000095template <class InputIterator1, class InputIterator2, class BinaryPredicate>
96 pair<InputIterator1, InputIterator2>
97 mismatch(InputIterator1 first1, InputIterator1 last1,
98 InputIterator2 first2, BinaryPredicate pred);
99
Marshall Clowb30abdd2013-05-09 21:14:23 +0000100template <class InputIterator1, class InputIterator2, class BinaryPredicate>
101 pair<InputIterator1, InputIterator2>
102 mismatch(InputIterator1 first1, InputIterator1 last1,
103 InputIterator2 first2, InputIterator2 last2,
104 BinaryPredicate pred); // **C++14**
105
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000106template <class InputIterator1, class InputIterator2>
107 bool
108 equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
109
Marshall Clowb30abdd2013-05-09 21:14:23 +0000110template <class InputIterator1, class InputIterator2>
111 bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +0000112 equal(InputIterator1 first1, InputIterator1 last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +0000113 InputIterator2 first2, InputIterator2 last2); // **C++14**
114
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000115template <class InputIterator1, class InputIterator2, class BinaryPredicate>
116 bool
117 equal(InputIterator1 first1, InputIterator1 last1,
118 InputIterator2 first2, BinaryPredicate pred);
119
Marshall Clowb30abdd2013-05-09 21:14:23 +0000120template <class InputIterator1, class InputIterator2, class BinaryPredicate>
121 bool
122 equal(InputIterator1 first1, InputIterator1 last1,
123 InputIterator2 first2, InputIterator2 last2,
124 BinaryPredicate pred); // **C++14**
125
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000126template<class ForwardIterator1, class ForwardIterator2>
127 bool
128 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
129 ForwardIterator2 first2);
130
Marshall Clowb30abdd2013-05-09 21:14:23 +0000131template<class ForwardIterator1, class ForwardIterator2>
132 bool
133 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
134 ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
135
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000136template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
137 bool
138 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
139 ForwardIterator2 first2, BinaryPredicate pred);
140
Marshall Clowb30abdd2013-05-09 21:14:23 +0000141template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
142 bool
143 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
144 ForwardIterator2 first2, ForwardIterator2 last2,
145 BinaryPredicate pred); // **C++14**
146
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000147template <class ForwardIterator1, class ForwardIterator2>
148 ForwardIterator1
149 search(ForwardIterator1 first1, ForwardIterator1 last1,
150 ForwardIterator2 first2, ForwardIterator2 last2);
151
152template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
153 ForwardIterator1
154 search(ForwardIterator1 first1, ForwardIterator1 last1,
155 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
156
157template <class ForwardIterator, class Size, class T>
158 ForwardIterator
159 search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
160
161template <class ForwardIterator, class Size, class T, class BinaryPredicate>
162 ForwardIterator
163 search_n(ForwardIterator first, ForwardIterator last,
164 Size count, const T& value, BinaryPredicate pred);
165
166template <class InputIterator, class OutputIterator>
167 OutputIterator
168 copy(InputIterator first, InputIterator last, OutputIterator result);
169
170template<class InputIterator, class OutputIterator, class Predicate>
171 OutputIterator
172 copy_if(InputIterator first, InputIterator last,
173 OutputIterator result, Predicate pred);
174
175template<class InputIterator, class Size, class OutputIterator>
176 OutputIterator
177 copy_n(InputIterator first, Size n, OutputIterator result);
178
179template <class BidirectionalIterator1, class BidirectionalIterator2>
180 BidirectionalIterator2
181 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
182 BidirectionalIterator2 result);
183
184template <class ForwardIterator1, class ForwardIterator2>
185 ForwardIterator2
186 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
187
188template <class ForwardIterator1, class ForwardIterator2>
189 void
190 iter_swap(ForwardIterator1 a, ForwardIterator2 b);
191
192template <class InputIterator, class OutputIterator, class UnaryOperation>
193 OutputIterator
194 transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
195
196template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
197 OutputIterator
198 transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
199 OutputIterator result, BinaryOperation binary_op);
200
201template <class ForwardIterator, class T>
202 void
203 replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
204
205template <class ForwardIterator, class Predicate, class T>
206 void
207 replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
208
209template <class InputIterator, class OutputIterator, class T>
210 OutputIterator
211 replace_copy(InputIterator first, InputIterator last, OutputIterator result,
212 const T& old_value, const T& new_value);
213
214template <class InputIterator, class OutputIterator, class Predicate, class T>
215 OutputIterator
216 replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
217
218template <class ForwardIterator, class T>
219 void
220 fill(ForwardIterator first, ForwardIterator last, const T& value);
221
222template <class OutputIterator, class Size, class T>
223 OutputIterator
224 fill_n(OutputIterator first, Size n, const T& value);
225
226template <class ForwardIterator, class Generator>
227 void
228 generate(ForwardIterator first, ForwardIterator last, Generator gen);
229
230template <class OutputIterator, class Size, class Generator>
231 OutputIterator
232 generate_n(OutputIterator first, Size n, Generator gen);
233
234template <class ForwardIterator, class T>
235 ForwardIterator
236 remove(ForwardIterator first, ForwardIterator last, const T& value);
237
238template <class ForwardIterator, class Predicate>
239 ForwardIterator
240 remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
241
242template <class InputIterator, class OutputIterator, class T>
243 OutputIterator
244 remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
245
246template <class InputIterator, class OutputIterator, class Predicate>
247 OutputIterator
248 remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
249
250template <class ForwardIterator>
251 ForwardIterator
252 unique(ForwardIterator first, ForwardIterator last);
253
254template <class ForwardIterator, class BinaryPredicate>
255 ForwardIterator
256 unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
257
258template <class InputIterator, class OutputIterator>
259 OutputIterator
260 unique_copy(InputIterator first, InputIterator last, OutputIterator result);
261
262template <class InputIterator, class OutputIterator, class BinaryPredicate>
263 OutputIterator
264 unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
265
266template <class BidirectionalIterator>
267 void
268 reverse(BidirectionalIterator first, BidirectionalIterator last);
269
270template <class BidirectionalIterator, class OutputIterator>
271 OutputIterator
272 reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
273
274template <class ForwardIterator>
275 ForwardIterator
276 rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
277
278template <class ForwardIterator, class OutputIterator>
279 OutputIterator
280 rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
281
282template <class RandomAccessIterator>
283 void
Marshall Clow03b862f2017-03-23 13:43:37 +0000284 random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14, removed in C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000285
286template <class RandomAccessIterator, class RandomNumberGenerator>
287 void
Marshall Clow3fef95b2014-03-03 06:14:19 +0000288 random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
Marshall Clow03b862f2017-03-23 13:43:37 +0000289 RandomNumberGenerator& rand); // deprecated in C++14, removed in C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290
Eric Fiselier917af0a2016-08-28 22:14:37 +0000291template<class PopulationIterator, class SampleIterator,
292 class Distance, class UniformRandomBitGenerator>
293 SampleIterator sample(PopulationIterator first, PopulationIterator last,
294 SampleIterator out, Distance n,
295 UniformRandomBitGenerator&& g); // C++17
296
Howard Hinnantc3267212010-05-26 17:49:34 +0000297template<class RandomAccessIterator, class UniformRandomNumberGenerator>
298 void shuffle(RandomAccessIterator first, RandomAccessIterator last,
Howard Hinnant278bf2d2010-11-18 01:47:02 +0000299 UniformRandomNumberGenerator&& g);
Howard Hinnantc3267212010-05-26 17:49:34 +0000300
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000301template <class InputIterator, class Predicate>
302 bool
303 is_partitioned(InputIterator first, InputIterator last, Predicate pred);
304
305template <class ForwardIterator, class Predicate>
306 ForwardIterator
307 partition(ForwardIterator first, ForwardIterator last, Predicate pred);
308
309template <class InputIterator, class OutputIterator1,
310 class OutputIterator2, class Predicate>
311 pair<OutputIterator1, OutputIterator2>
312 partition_copy(InputIterator first, InputIterator last,
313 OutputIterator1 out_true, OutputIterator2 out_false,
314 Predicate pred);
315
316template <class ForwardIterator, class Predicate>
317 ForwardIterator
318 stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
319
320template<class ForwardIterator, class Predicate>
321 ForwardIterator
322 partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
323
324template <class ForwardIterator>
325 bool
326 is_sorted(ForwardIterator first, ForwardIterator last);
327
328template <class ForwardIterator, class Compare>
329 bool
330 is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
331
332template<class ForwardIterator>
333 ForwardIterator
334 is_sorted_until(ForwardIterator first, ForwardIterator last);
335
336template <class ForwardIterator, class Compare>
337 ForwardIterator
338 is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
339
340template <class RandomAccessIterator>
341 void
342 sort(RandomAccessIterator first, RandomAccessIterator last);
343
344template <class RandomAccessIterator, class Compare>
345 void
346 sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
347
348template <class RandomAccessIterator>
349 void
350 stable_sort(RandomAccessIterator first, RandomAccessIterator last);
351
352template <class RandomAccessIterator, class Compare>
353 void
354 stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
355
356template <class RandomAccessIterator>
357 void
358 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
359
360template <class RandomAccessIterator, class Compare>
361 void
362 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
363
364template <class InputIterator, class RandomAccessIterator>
365 RandomAccessIterator
366 partial_sort_copy(InputIterator first, InputIterator last,
367 RandomAccessIterator result_first, RandomAccessIterator result_last);
368
369template <class InputIterator, class RandomAccessIterator, class Compare>
370 RandomAccessIterator
371 partial_sort_copy(InputIterator first, InputIterator last,
372 RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
373
374template <class RandomAccessIterator>
375 void
376 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
377
378template <class RandomAccessIterator, class Compare>
379 void
380 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
381
382template <class ForwardIterator, class T>
383 ForwardIterator
384 lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
385
386template <class ForwardIterator, class T, class Compare>
387 ForwardIterator
388 lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
389
390template <class ForwardIterator, class T>
391 ForwardIterator
392 upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
393
394template <class ForwardIterator, class T, class Compare>
395 ForwardIterator
396 upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
397
398template <class ForwardIterator, class T>
399 pair<ForwardIterator, ForwardIterator>
400 equal_range(ForwardIterator first, ForwardIterator last, const T& value);
401
402template <class ForwardIterator, class T, class Compare>
403 pair<ForwardIterator, ForwardIterator>
404 equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
405
406template <class ForwardIterator, class T>
407 bool
408 binary_search(ForwardIterator first, ForwardIterator last, const T& value);
409
410template <class ForwardIterator, class T, class Compare>
411 bool
412 binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
413
414template <class InputIterator1, class InputIterator2, class OutputIterator>
415 OutputIterator
416 merge(InputIterator1 first1, InputIterator1 last1,
417 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
418
419template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
420 OutputIterator
421 merge(InputIterator1 first1, InputIterator1 last1,
422 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
423
424template <class BidirectionalIterator>
425 void
426 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
427
428template <class BidirectionalIterator, class Compare>
429 void
430 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
431
432template <class InputIterator1, class InputIterator2>
433 bool
434 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
435
436template <class InputIterator1, class InputIterator2, class Compare>
437 bool
438 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
439
440template <class InputIterator1, class InputIterator2, class OutputIterator>
441 OutputIterator
442 set_union(InputIterator1 first1, InputIterator1 last1,
443 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
444
445template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
446 OutputIterator
447 set_union(InputIterator1 first1, InputIterator1 last1,
448 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
449
450template <class InputIterator1, class InputIterator2, class OutputIterator>
451 OutputIterator
452 set_intersection(InputIterator1 first1, InputIterator1 last1,
453 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
454
455template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
456 OutputIterator
457 set_intersection(InputIterator1 first1, InputIterator1 last1,
458 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
459
460template <class InputIterator1, class InputIterator2, class OutputIterator>
461 OutputIterator
462 set_difference(InputIterator1 first1, InputIterator1 last1,
463 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
464
465template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
466 OutputIterator
467 set_difference(InputIterator1 first1, InputIterator1 last1,
468 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
469
470template <class InputIterator1, class InputIterator2, class OutputIterator>
471 OutputIterator
472 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
473 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
474
475template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
476 OutputIterator
477 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
478 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
479
480template <class RandomAccessIterator>
481 void
482 push_heap(RandomAccessIterator first, RandomAccessIterator last);
483
484template <class RandomAccessIterator, class Compare>
485 void
486 push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
487
488template <class RandomAccessIterator>
489 void
490 pop_heap(RandomAccessIterator first, RandomAccessIterator last);
491
492template <class RandomAccessIterator, class Compare>
493 void
494 pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
495
496template <class RandomAccessIterator>
497 void
498 make_heap(RandomAccessIterator first, RandomAccessIterator last);
499
500template <class RandomAccessIterator, class Compare>
501 void
502 make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
503
504template <class RandomAccessIterator>
505 void
506 sort_heap(RandomAccessIterator first, RandomAccessIterator last);
507
508template <class RandomAccessIterator, class Compare>
509 void
510 sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
511
Howard Hinnant324bb032010-08-22 00:02:43 +0000512template <class RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000513 bool
Howard Hinnant324bb032010-08-22 00:02:43 +0000514 is_heap(RandomAccessIterator first, RandomAccessiterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000515
Howard Hinnant324bb032010-08-22 00:02:43 +0000516template <class RandomAccessIterator, class Compare>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000517 bool
Howard Hinnant324bb032010-08-22 00:02:43 +0000518 is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000519
Howard Hinnant324bb032010-08-22 00:02:43 +0000520template <class RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000521 RandomAccessIterator
Howard Hinnant324bb032010-08-22 00:02:43 +0000522 is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000523
Howard Hinnant324bb032010-08-22 00:02:43 +0000524template <class RandomAccessIterator, class Compare>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000525 RandomAccessIterator
Howard Hinnant324bb032010-08-22 00:02:43 +0000526 is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000527
Howard Hinnant98e5d972010-08-21 20:10:01 +0000528template <class ForwardIterator>
529 ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +0000530 min_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000531
532template <class ForwardIterator, class Compare>
533 ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +0000534 min_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000535
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000536template <class T>
537 const T&
Marshall Clow9d9463a2014-02-19 16:51:35 +0000538 min(const T& a, const T& b); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000539
540template <class T, class Compare>
541 const T&
Marshall Clow9d9463a2014-02-19 16:51:35 +0000542 min(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000543
Howard Hinnant98e5d972010-08-21 20:10:01 +0000544template<class T>
545 T
Marshall Clow9d9463a2014-02-19 16:51:35 +0000546 min(initializer_list<T> t); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000547
548template<class T, class Compare>
549 T
Marshall Clow9d9463a2014-02-19 16:51:35 +0000550 min(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000551
Marshall Clow3e0808e2016-03-07 22:43:49 +0000552template<class T>
553 constexpr const T& clamp( const T& v, const T& lo, const T& hi ); // C++17
554
555template<class T, class Compare>
556 constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp ); // C++17
557
Howard Hinnant98e5d972010-08-21 20:10:01 +0000558template <class ForwardIterator>
559 ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +0000560 max_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000561
562template <class ForwardIterator, class Compare>
563 ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +0000564 max_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000565
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000566template <class T>
567 const T&
Marshall Clow9d9463a2014-02-19 16:51:35 +0000568 max(const T& a, const T& b); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000569
570template <class T, class Compare>
571 const T&
Marshall Clow9d9463a2014-02-19 16:51:35 +0000572 max(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000573
Howard Hinnant98e5d972010-08-21 20:10:01 +0000574template<class T>
575 T
Marshall Clow9d9463a2014-02-19 16:51:35 +0000576 max(initializer_list<T> t); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000577
Howard Hinnant98e5d972010-08-21 20:10:01 +0000578template<class T, class Compare>
579 T
Marshall Clow9d9463a2014-02-19 16:51:35 +0000580 max(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000581
Howard Hinnant98e5d972010-08-21 20:10:01 +0000582template<class ForwardIterator>
583 pair<ForwardIterator, ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +0000584 minmax_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000585
Howard Hinnant98e5d972010-08-21 20:10:01 +0000586template<class ForwardIterator, class Compare>
587 pair<ForwardIterator, ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +0000588 minmax_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000589
590template<class T>
591 pair<const T&, const T&>
Marshall Clow9d9463a2014-02-19 16:51:35 +0000592 minmax(const T& a, const T& b); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000593
594template<class T, class Compare>
595 pair<const T&, const T&>
Marshall Clow9d9463a2014-02-19 16:51:35 +0000596 minmax(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000597
598template<class T>
599 pair<T, T>
Marshall Clow9d9463a2014-02-19 16:51:35 +0000600 minmax(initializer_list<T> t); // constexpr in C++14
Howard Hinnant98e5d972010-08-21 20:10:01 +0000601
602template<class T, class Compare>
603 pair<T, T>
Marshall Clow9d9463a2014-02-19 16:51:35 +0000604 minmax(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000605
606template <class InputIterator1, class InputIterator2>
607 bool
608 lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
609
610template <class InputIterator1, class InputIterator2, class Compare>
611 bool
612 lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
613 InputIterator2 first2, InputIterator2 last2, Compare comp);
614
615template <class BidirectionalIterator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000616 bool
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000617 next_permutation(BidirectionalIterator first, BidirectionalIterator last);
618
619template <class BidirectionalIterator, class Compare>
620 bool
621 next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
622
623template <class BidirectionalIterator>
624 bool
625 prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
626
627template <class BidirectionalIterator, class Compare>
628 bool
629 prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
630
631} // std
632
633*/
634
635#include <__config>
636#include <initializer_list>
637#include <type_traits>
638#include <cstring>
Eric Fiselier8f1e73d2016-04-21 23:38:59 +0000639#include <utility> // needed to provide swap_ranges.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000640#include <memory>
641#include <iterator>
Howard Hinnantca8eb832012-07-26 17:09:09 +0000642#include <cstddef>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000643
Howard Hinnant7f764502013-08-14 18:00:20 +0000644#if defined(__IBMCPP__)
645#include "support/ibm/support.h"
646#endif
Eric Fiselier41af64a2017-05-10 20:57:45 +0000647#if defined(_LIBCPP_COMPILER_MSVC)
Eric Fiselierca91fd02017-05-10 21:30:04 +0000648#include <intrin.h>
Howard Hinnantef5aa932013-09-17 01:34:47 +0000649#endif
Howard Hinnant7f764502013-08-14 18:00:20 +0000650
Howard Hinnant66c6f972011-11-29 16:45:27 +0000651#include <__undef_min_max>
652
Eric Fiselierb9536102014-08-10 23:53:08 +0000653#include <__debug>
654
Howard Hinnant08e17472011-10-17 20:05:10 +0000655#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000656#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000657#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000658
659_LIBCPP_BEGIN_NAMESPACE_STD
660
Marshall Clow9d9463a2014-02-19 16:51:35 +0000661// I'd like to replace these with _VSTD::equal_to<void>, but can't because:
662// * That only works with C++14 and later, and
663// * We haven't included <functional> here.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664template <class _T1, class _T2 = _T1>
665struct __equal_to
666{
667 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
668 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
669 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
670 _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
671};
672
673template <class _T1>
674struct __equal_to<_T1, _T1>
675{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000676 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
677 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000678};
679
680template <class _T1>
681struct __equal_to<const _T1, _T1>
682{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000683 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
684 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000685};
686
687template <class _T1>
688struct __equal_to<_T1, const _T1>
689{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000690 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
691 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000692};
693
694template <class _T1, class _T2 = _T1>
695struct __less
696{
Aditya Kumarfdb4f172016-08-25 11:52:38 +0000697 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow9d9463a2014-02-19 16:51:35 +0000698 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
699
700 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
701 bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
702
703 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
704 bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
705
706 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
707 bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000708};
709
710template <class _T1>
711struct __less<_T1, _T1>
712{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000713 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
714 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000715};
716
717template <class _T1>
718struct __less<const _T1, _T1>
719{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000720 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
721 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000722};
723
724template <class _T1>
725struct __less<_T1, const _T1>
726{
Marshall Clow9d9463a2014-02-19 16:51:35 +0000727 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
728 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000729};
730
731template <class _Predicate>
732class __negate
733{
734private:
735 _Predicate __p_;
736public:
737 _LIBCPP_INLINE_VISIBILITY __negate() {}
738
739 _LIBCPP_INLINE_VISIBILITY
740 explicit __negate(_Predicate __p) : __p_(__p) {}
741
742 template <class _T1>
743 _LIBCPP_INLINE_VISIBILITY
744 bool operator()(const _T1& __x) {return !__p_(__x);}
745
746 template <class _T1, class _T2>
747 _LIBCPP_INLINE_VISIBILITY
748 bool operator()(const _T1& __x, const _T2& __y) {return !__p_(__x, __y);}
749};
750
Howard Hinnant5e571422013-08-23 20:10:18 +0000751#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000752
753template <class _Compare>
754struct __debug_less
755{
756 _Compare __comp_;
757 __debug_less(_Compare& __c) : __comp_(__c) {}
Eric Fiselier99029f12016-07-19 23:27:18 +0000758
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000759 template <class _Tp, class _Up>
760 bool operator()(const _Tp& __x, const _Up& __y)
761 {
762 bool __r = __comp_(__x, __y);
763 if (__r)
Eric Fiselier99029f12016-07-19 23:27:18 +0000764 __do_compare_assert(0, __y, __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765 return __r;
766 }
Eric Fiselier99029f12016-07-19 23:27:18 +0000767
768 template <class _LHS, class _RHS>
769 inline _LIBCPP_INLINE_VISIBILITY
770 decltype((void)_VSTD::declval<_Compare&>()(
771 _VSTD::declval<_LHS const&>(), _VSTD::declval<_RHS const&>()))
772 __do_compare_assert(int, _LHS const& __l, _RHS const& __r) {
773 _LIBCPP_ASSERT(!__comp_(__l, __r),
774 "Comparator does not induce a strict weak ordering");
775 }
776
777 template <class _LHS, class _RHS>
778 inline _LIBCPP_INLINE_VISIBILITY
779 void __do_compare_assert(long, _LHS const&, _RHS const&) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000780};
781
Howard Hinnant5e571422013-08-23 20:10:18 +0000782#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000783
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000784// Precondition: __x != 0
Howard Hinnantec3773c2011-12-01 20:21:04 +0000785inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000786unsigned __ctz(unsigned __x) {
787#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000788 return static_cast<unsigned>(__builtin_ctz(__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000789#else
790 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
791 static_assert(sizeof(unsigned long) == 4, "");
792 unsigned long where;
793 // Search from LSB to MSB for first set bit.
794 // Returns zero if no set bit is found.
795 if (_BitScanForward(&where, mask))
796 return where;
797 return 32;
798#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +0000799}
800
801inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000802unsigned long __ctz(unsigned long __x) {
803#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000804 return static_cast<unsigned long>(__builtin_ctzl(__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000805#else
806 static_assert(sizeof(unsigned long) == sizeof(unsigned), "");
807 return __ctz(static_cast<unsigned>(__x));
808#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +0000809}
810
811inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000812unsigned long long __ctz(unsigned long long __x) {
813#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000814 return static_cast<unsigned long long>(__builtin_ctzll(__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000815#else
816 unsigned long where;
817// Search from LSB to MSB for first set bit.
818// Returns zero if no set bit is found.
819#if defined(_LIBCPP_HAS_BITSCAN64)
820 (defined(_M_AMD64) || defined(__x86_64__))
821 if (_BitScanForward64(&where, mask))
822 return static_cast<int>(where);
823#else
824 // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
825 // Scan the Low Word.
826 if (_BitScanForward(&where, static_cast<unsigned long>(mask)))
827 return where;
828 // Scan the High Word.
829 if (_BitScanForward(&where, static_cast<unsigned long>(mask >> 32)))
830 return where + 32; // Create a bit offset from the LSB.
831#endif
832 return 64;
833#endif // _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000834}
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000835
836// Precondition: __x != 0
Howard Hinnantec3773c2011-12-01 20:21:04 +0000837inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000838unsigned __clz(unsigned __x) {
839#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000840 return static_cast<unsigned>(__builtin_clz(__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000841#else
842 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
843 static_assert(sizeof(unsigned long) == 4, "");
844 unsigned long where;
845 // Search from LSB to MSB for first set bit.
846 // Returns zero if no set bit is found.
847 if (_BitScanReverse(&where, mask))
848 return 31 - where;
849 return 32; // Undefined Behavior.
850#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +0000851}
852
853inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000854unsigned long __clz(unsigned long __x) {
855#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000856 return static_cast<unsigned long>(__builtin_clzl (__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000857#else
858 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
859 return __clz(static_cast<unsigned>(__x));
860#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +0000861}
862
863inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierca91fd02017-05-10 21:30:04 +0000864unsigned long long __clz(unsigned long long __x) {
865#ifndef _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000866 return static_cast<unsigned long long>(__builtin_clzll(__x));
Eric Fiselierca91fd02017-05-10 21:30:04 +0000867#else
868 unsigned long where;
869// BitScanReverse scans from MSB to LSB for first set bit.
870// Returns 0 if no set bit is found.
871#if defined(_LIBCPP_HAS_BITSCAN64)
872 if (_BitScanReverse64(&where, mask))
873 return static_cast<int>(63 - where);
874#else
875 // Scan the high 32 bits.
876 if (_BitScanReverse(&where, static_cast<unsigned long>(mask >> 32)))
877 return 63 - (where + 32); // Create a bit offset from the MSB.
878 // Scan the low 32 bits.
879 if (_BitScanReverse(&where, static_cast<unsigned long>(mask)))
880 return 63 - where;
881#endif
882 return 64; // Undefined Behavior.
883#endif // _LIBCPP_COMPILER_MSVC
Howard Hinnantec3773c2011-12-01 20:21:04 +0000884}
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000885
Eric Fiselierca91fd02017-05-10 21:30:04 +0000886inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) {
887#ifndef _LIBCPP_COMPILER_MSVC
888 return __builtin_popcount (__x);
889#else
890 static_assert(sizeof(unsigned) == 4, "");
891 return __popcnt(__x);
892#endif
893}
894
895inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) {
896#ifndef _LIBCPP_COMPILER_MSVC
897 return __builtin_popcountl (__x);
898#else
899 static_assert(sizeof(unsigned long) == 4, "");
900 return __popcnt(__x);
901#endif
902}
903
904inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {
905#ifndef _LIBCPP_COMPILER_MSVC
906 return __builtin_popcountll(__x);
907#else
908 static_assert(sizeof(unsigned long long) == 8, "");
909 return __popcnt64(__x);
910#endif
911}
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000912
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000913// all_of
914
915template <class _InputIterator, class _Predicate>
916inline _LIBCPP_INLINE_VISIBILITY
917bool
918all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
919{
920 for (; __first != __last; ++__first)
921 if (!__pred(*__first))
922 return false;
923 return true;
924}
925
926// any_of
927
928template <class _InputIterator, class _Predicate>
929inline _LIBCPP_INLINE_VISIBILITY
930bool
931any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
932{
933 for (; __first != __last; ++__first)
934 if (__pred(*__first))
935 return true;
936 return false;
937}
938
939// none_of
940
941template <class _InputIterator, class _Predicate>
942inline _LIBCPP_INLINE_VISIBILITY
943bool
944none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
945{
946 for (; __first != __last; ++__first)
947 if (__pred(*__first))
948 return false;
949 return true;
950}
951
952// for_each
953
954template <class _InputIterator, class _Function>
955inline _LIBCPP_INLINE_VISIBILITY
956_Function
957for_each(_InputIterator __first, _InputIterator __last, _Function __f)
958{
959 for (; __first != __last; ++__first)
960 __f(*__first);
Marshall Clowdb7fa112016-11-14 18:22:19 +0000961 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000962}
963
964// find
965
966template <class _InputIterator, class _Tp>
967inline _LIBCPP_INLINE_VISIBILITY
968_InputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +0000969find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000970{
971 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +0000972 if (*__first == __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000973 break;
974 return __first;
975}
976
977// find_if
978
979template <class _InputIterator, class _Predicate>
980inline _LIBCPP_INLINE_VISIBILITY
981_InputIterator
982find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
983{
984 for (; __first != __last; ++__first)
985 if (__pred(*__first))
986 break;
987 return __first;
988}
989
990// find_if_not
991
992template<class _InputIterator, class _Predicate>
993inline _LIBCPP_INLINE_VISIBILITY
994_InputIterator
995find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
996{
997 for (; __first != __last; ++__first)
998 if (!__pred(*__first))
999 break;
1000 return __first;
1001}
1002
1003// find_end
1004
1005template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1006_ForwardIterator1
1007__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1008 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
1009 forward_iterator_tag, forward_iterator_tag)
1010{
1011 // modeled after search algorithm
1012 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
1013 if (__first2 == __last2)
1014 return __r;
1015 while (true)
1016 {
1017 while (true)
1018 {
1019 if (__first1 == __last1) // if source exhausted return last correct answer
1020 return __r; // (or __last1 if never found)
1021 if (__pred(*__first1, *__first2))
1022 break;
1023 ++__first1;
1024 }
1025 // *__first1 matches *__first2, now match elements after here
1026 _ForwardIterator1 __m1 = __first1;
1027 _ForwardIterator2 __m2 = __first2;
1028 while (true)
1029 {
1030 if (++__m2 == __last2)
1031 { // Pattern exhaused, record answer and search for another one
1032 __r = __first1;
1033 ++__first1;
1034 break;
1035 }
1036 if (++__m1 == __last1) // Source exhausted, return last answer
1037 return __r;
1038 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
1039 {
1040 ++__first1;
1041 break;
1042 } // else there is a match, check next elements
1043 }
1044 }
1045}
1046
1047template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
1048_BidirectionalIterator1
1049__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
1050 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
1051 bidirectional_iterator_tag, bidirectional_iterator_tag)
1052{
1053 // modeled after search algorithm (in reverse)
1054 if (__first2 == __last2)
1055 return __last1; // Everything matches an empty sequence
1056 _BidirectionalIterator1 __l1 = __last1;
1057 _BidirectionalIterator2 __l2 = __last2;
1058 --__l2;
1059 while (true)
1060 {
1061 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
1062 while (true)
1063 {
1064 if (__first1 == __l1) // return __last1 if no element matches *__first2
1065 return __last1;
1066 if (__pred(*--__l1, *__l2))
1067 break;
1068 }
1069 // *__l1 matches *__l2, now match elements before here
1070 _BidirectionalIterator1 __m1 = __l1;
1071 _BidirectionalIterator2 __m2 = __l2;
1072 while (true)
1073 {
1074 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
1075 return __m1;
1076 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
1077 return __last1;
1078 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
1079 {
1080 break;
1081 } // else there is a match, check next elements
1082 }
1083 }
1084}
1085
1086template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow37025e12014-06-10 18:51:55 +00001087_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001088__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1089 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1090 random_access_iterator_tag, random_access_iterator_tag)
1091{
1092 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1093 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
1094 if (__len2 == 0)
1095 return __last1;
1096 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
1097 if (__len1 < __len2)
1098 return __last1;
1099 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
1100 _RandomAccessIterator1 __l1 = __last1;
1101 _RandomAccessIterator2 __l2 = __last2;
1102 --__l2;
1103 while (true)
1104 {
1105 while (true)
1106 {
1107 if (__s == __l1)
1108 return __last1;
1109 if (__pred(*--__l1, *__l2))
1110 break;
1111 }
1112 _RandomAccessIterator1 __m1 = __l1;
1113 _RandomAccessIterator2 __m2 = __l2;
1114 while (true)
1115 {
1116 if (__m2 == __first2)
1117 return __m1;
1118 // no need to check range on __m1 because __s guarantees we have enough source
1119 if (!__pred(*--__m1, *--__m2))
1120 {
1121 break;
1122 }
1123 }
1124 }
1125}
1126
1127template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1128inline _LIBCPP_INLINE_VISIBILITY
1129_ForwardIterator1
1130find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1131 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1132{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001133 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001134 (__first1, __last1, __first2, __last2, __pred,
1135 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1136 typename iterator_traits<_ForwardIterator2>::iterator_category());
1137}
1138
1139template <class _ForwardIterator1, class _ForwardIterator2>
1140inline _LIBCPP_INLINE_VISIBILITY
1141_ForwardIterator1
1142find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1143 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1144{
1145 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1146 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001147 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001148}
1149
1150// find_first_of
1151
1152template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow37025e12014-06-10 18:51:55 +00001153_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1154__find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001155 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1156{
1157 for (; __first1 != __last1; ++__first1)
1158 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1159 if (__pred(*__first1, *__j))
1160 return __first1;
1161 return __last1;
1162}
1163
Marshall Clow37025e12014-06-10 18:51:55 +00001164
1165template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1166inline _LIBCPP_INLINE_VISIBILITY
1167_ForwardIterator1
1168find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1169 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1170{
1171 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1172}
1173
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001174template <class _ForwardIterator1, class _ForwardIterator2>
1175inline _LIBCPP_INLINE_VISIBILITY
1176_ForwardIterator1
1177find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1178 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1179{
1180 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1181 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Marshall Clow37025e12014-06-10 18:51:55 +00001182 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001183}
1184
1185// adjacent_find
1186
1187template <class _ForwardIterator, class _BinaryPredicate>
1188inline _LIBCPP_INLINE_VISIBILITY
1189_ForwardIterator
1190adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1191{
1192 if (__first != __last)
1193 {
1194 _ForwardIterator __i = __first;
1195 while (++__i != __last)
1196 {
1197 if (__pred(*__first, *__i))
1198 return __first;
1199 __first = __i;
1200 }
1201 }
1202 return __last;
1203}
1204
1205template <class _ForwardIterator>
1206inline _LIBCPP_INLINE_VISIBILITY
1207_ForwardIterator
1208adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1209{
1210 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001211 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001212}
1213
1214// count
1215
1216template <class _InputIterator, class _Tp>
1217inline _LIBCPP_INLINE_VISIBILITY
1218typename iterator_traits<_InputIterator>::difference_type
Howard Hinnant78b68282011-10-22 20:59:45 +00001219count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001220{
1221 typename iterator_traits<_InputIterator>::difference_type __r(0);
1222 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +00001223 if (*__first == __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001224 ++__r;
1225 return __r;
1226}
1227
1228// count_if
1229
1230template <class _InputIterator, class _Predicate>
1231inline _LIBCPP_INLINE_VISIBILITY
1232typename iterator_traits<_InputIterator>::difference_type
1233count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1234{
1235 typename iterator_traits<_InputIterator>::difference_type __r(0);
1236 for (; __first != __last; ++__first)
1237 if (__pred(*__first))
1238 ++__r;
1239 return __r;
1240}
1241
1242// mismatch
1243
1244template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1245inline _LIBCPP_INLINE_VISIBILITY
1246pair<_InputIterator1, _InputIterator2>
1247mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1248 _InputIterator2 __first2, _BinaryPredicate __pred)
1249{
Marshall Clowd402a4d2014-09-16 20:40:05 +00001250 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001251 if (!__pred(*__first1, *__first2))
1252 break;
1253 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1254}
1255
1256template <class _InputIterator1, class _InputIterator2>
1257inline _LIBCPP_INLINE_VISIBILITY
1258pair<_InputIterator1, _InputIterator2>
1259mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1260{
1261 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1262 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001263 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001264}
1265
Marshall Clowb30abdd2013-05-09 21:14:23 +00001266#if _LIBCPP_STD_VER > 11
1267template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1268inline _LIBCPP_INLINE_VISIBILITY
1269pair<_InputIterator1, _InputIterator2>
1270mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1271 _InputIterator2 __first2, _InputIterator2 __last2,
1272 _BinaryPredicate __pred)
1273{
Marshall Clowd402a4d2014-09-16 20:40:05 +00001274 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001275 if (!__pred(*__first1, *__first2))
1276 break;
1277 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1278}
1279
1280template <class _InputIterator1, class _InputIterator2>
1281inline _LIBCPP_INLINE_VISIBILITY
1282pair<_InputIterator1, _InputIterator2>
1283mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1284 _InputIterator2 __first2, _InputIterator2 __last2)
1285{
1286 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1287 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1288 return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1289}
1290#endif
1291
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001292// equal
1293
1294template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1295inline _LIBCPP_INLINE_VISIBILITY
1296bool
1297equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1298{
Eric Fiselierb9919752014-10-27 19:28:20 +00001299 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001300 if (!__pred(*__first1, *__first2))
1301 return false;
1302 return true;
1303}
1304
1305template <class _InputIterator1, class _InputIterator2>
1306inline _LIBCPP_INLINE_VISIBILITY
1307bool
1308equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1309{
1310 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1311 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001312 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001313}
1314
Marshall Clowb30abdd2013-05-09 21:14:23 +00001315#if _LIBCPP_STD_VER > 11
1316template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
1317inline _LIBCPP_INLINE_VISIBILITY
1318bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001319__equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001320 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1321 input_iterator_tag, input_iterator_tag )
1322{
Eric Fiselierb9919752014-10-27 19:28:20 +00001323 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001324 if (!__pred(*__first1, *__first2))
1325 return false;
1326 return __first1 == __last1 && __first2 == __last2;
1327}
1328
1329template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1330inline _LIBCPP_INLINE_VISIBILITY
1331bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001332__equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1333 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001334 random_access_iterator_tag, random_access_iterator_tag )
1335{
1336 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1337 return false;
1338 return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1339 typename add_lvalue_reference<_BinaryPredicate>::type>
1340 (__first1, __last1, __first2, __pred );
1341}
1342
1343template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1344inline _LIBCPP_INLINE_VISIBILITY
1345bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001346equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001347 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1348{
1349 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001350 (__first1, __last1, __first2, __last2, __pred,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001351 typename iterator_traits<_InputIterator1>::iterator_category(),
1352 typename iterator_traits<_InputIterator2>::iterator_category());
1353}
1354
1355template <class _InputIterator1, class _InputIterator2>
1356inline _LIBCPP_INLINE_VISIBILITY
1357bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001358equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001359 _InputIterator2 __first2, _InputIterator2 __last2)
1360{
1361 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1362 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1363 return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1364 typename iterator_traits<_InputIterator1>::iterator_category(),
1365 typename iterator_traits<_InputIterator2>::iterator_category());
1366}
1367#endif
1368
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001369// is_permutation
1370
1371template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1372bool
1373is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1374 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1375{
1376 // shorten sequences as much as possible by lopping of any equal parts
Eric Fiselierb9919752014-10-27 19:28:20 +00001377 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001378 if (!__pred(*__first1, *__first2))
1379 goto __not_done;
1380 return true;
1381__not_done:
1382 // __first1 != __last1 && *__first1 != *__first2
1383 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001384 _D1 __l1 = _VSTD::distance(__first1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001385 if (__l1 == _D1(1))
1386 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001387 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001388 // For each element in [f1, l1) see if there are the same number of
1389 // equal elements in [f2, l2)
1390 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1391 {
1392 // Have we already counted the number of *__i in [f1, l1)?
1393 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1394 if (__pred(*__j, *__i))
1395 goto __next_iter;
1396 {
1397 // Count number of *__i in [f2, l2)
1398 _D1 __c2 = 0;
1399 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1400 if (__pred(*__i, *__j))
1401 ++__c2;
1402 if (__c2 == 0)
1403 return false;
1404 // Count number of *__i in [__i, l1) (we can start with 1)
1405 _D1 __c1 = 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001406 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001407 if (__pred(*__i, *__j))
1408 ++__c1;
1409 if (__c1 != __c2)
1410 return false;
1411 }
1412__next_iter:;
1413 }
1414 return true;
1415}
1416
1417template<class _ForwardIterator1, class _ForwardIterator2>
1418inline _LIBCPP_INLINE_VISIBILITY
1419bool
1420is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1421 _ForwardIterator2 __first2)
1422{
1423 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1424 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001425 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001426}
1427
Marshall Clowb30abdd2013-05-09 21:14:23 +00001428#if _LIBCPP_STD_VER > 11
1429template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1430bool
1431__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001432 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001433 _BinaryPredicate __pred,
1434 forward_iterator_tag, forward_iterator_tag )
1435{
1436 // shorten sequences as much as possible by lopping of any equal parts
Eric Fiselier62a0e012014-10-27 20:26:25 +00001437 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001438 if (!__pred(*__first1, *__first2))
1439 goto __not_done;
1440 return __first1 == __last1 && __first2 == __last2;
1441__not_done:
1442 // __first1 != __last1 && __first2 != __last2 && *__first1 != *__first2
1443 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1444 _D1 __l1 = _VSTD::distance(__first1, __last1);
1445
1446 typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
Marshall Clow9f8f5242013-05-10 00:16:10 +00001447 _D2 __l2 = _VSTD::distance(__first2, __last2);
Marshall Clowb30abdd2013-05-09 21:14:23 +00001448 if (__l1 != __l2)
1449 return false;
1450
1451 // For each element in [f1, l1) see if there are the same number of
1452 // equal elements in [f2, l2)
1453 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1454 {
1455 // Have we already counted the number of *__i in [f1, l1)?
1456 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1457 if (__pred(*__j, *__i))
1458 goto __next_iter;
1459 {
1460 // Count number of *__i in [f2, l2)
1461 _D1 __c2 = 0;
1462 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1463 if (__pred(*__i, *__j))
1464 ++__c2;
1465 if (__c2 == 0)
1466 return false;
1467 // Count number of *__i in [__i, l1) (we can start with 1)
1468 _D1 __c1 = 1;
1469 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1470 if (__pred(*__i, *__j))
1471 ++__c1;
1472 if (__c1 != __c2)
1473 return false;
1474 }
1475__next_iter:;
1476 }
1477 return true;
1478}
1479
1480template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1481bool
1482__is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001483 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001484 _BinaryPredicate __pred,
1485 random_access_iterator_tag, random_access_iterator_tag )
1486{
1487 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1488 return false;
1489 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1490 typename add_lvalue_reference<_BinaryPredicate>::type>
1491 (__first1, __last1, __first2, __pred );
1492}
1493
1494template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1495inline _LIBCPP_INLINE_VISIBILITY
1496bool
1497is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1498 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1499 _BinaryPredicate __pred )
1500{
1501 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1502 (__first1, __last1, __first2, __last2, __pred,
1503 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1504 typename iterator_traits<_ForwardIterator2>::iterator_category());
1505}
1506
1507template<class _ForwardIterator1, class _ForwardIterator2>
1508inline _LIBCPP_INLINE_VISIBILITY
1509bool
1510is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1511 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1512{
1513 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1514 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1515 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1516 __equal_to<__v1, __v2>(),
1517 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1518 typename iterator_traits<_ForwardIterator2>::iterator_category());
1519}
1520#endif
1521
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001522// search
1523
1524template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clowf6d6b512016-03-08 15:12:52 +00001525pair<_ForwardIterator1, _ForwardIterator1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001526__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1527 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
1528 forward_iterator_tag, forward_iterator_tag)
1529{
1530 if (__first2 == __last2)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001531 return make_pair(__first1, __first1); // Everything matches an empty sequence
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001532 while (true)
1533 {
1534 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
1535 while (true)
1536 {
1537 if (__first1 == __last1) // return __last1 if no element matches *__first2
Marshall Clowf6d6b512016-03-08 15:12:52 +00001538 return make_pair(__last1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001539 if (__pred(*__first1, *__first2))
1540 break;
1541 ++__first1;
1542 }
1543 // *__first1 matches *__first2, now match elements after here
1544 _ForwardIterator1 __m1 = __first1;
1545 _ForwardIterator2 __m2 = __first2;
1546 while (true)
1547 {
1548 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001549 return make_pair(__first1, __m1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001550 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
Marshall Clowf6d6b512016-03-08 15:12:52 +00001551 return make_pair(__last1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001552 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
1553 {
1554 ++__first1;
1555 break;
1556 } // else there is a match, check next elements
1557 }
1558 }
1559}
1560
1561template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001562_LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowf6d6b512016-03-08 15:12:52 +00001563pair<_RandomAccessIterator1, _RandomAccessIterator1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001564__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
Marshall Clowf6d6b512016-03-08 15:12:52 +00001565 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001566 random_access_iterator_tag, random_access_iterator_tag)
1567{
Marshall Clowf6d6b512016-03-08 15:12:52 +00001568 typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
1569 typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001570 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
Marshall Clowf6d6b512016-03-08 15:12:52 +00001571 const _D2 __len2 = __last2 - __first2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001572 if (__len2 == 0)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001573 return make_pair(__first1, __first1);
1574 const _D1 __len1 = __last1 - __first1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001575 if (__len1 < __len2)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001576 return make_pair(__last1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001577 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
Aditya Kumar42997952016-11-29 14:43:42 +00001578
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001579 while (true)
1580 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001581 while (true)
1582 {
1583 if (__first1 == __s)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001584 return make_pair(__last1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001585 if (__pred(*__first1, *__first2))
1586 break;
1587 ++__first1;
1588 }
Aditya Kumar42997952016-11-29 14:43:42 +00001589
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001590 _RandomAccessIterator1 __m1 = __first1;
1591 _RandomAccessIterator2 __m2 = __first2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001592 while (true)
1593 {
1594 if (++__m2 == __last2)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001595 return make_pair(__first1, __first1 + __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001596 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
1597 if (!__pred(*__m1, *__m2))
1598 {
1599 ++__first1;
1600 break;
1601 }
1602 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001603 }
1604}
1605
1606template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1607inline _LIBCPP_INLINE_VISIBILITY
1608_ForwardIterator1
1609search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1610 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1611{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001612 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001613 (__first1, __last1, __first2, __last2, __pred,
Marshall Clowf6d6b512016-03-08 15:12:52 +00001614 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1615 typename iterator_traits<_ForwardIterator2>::iterator_category())
1616 .first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001617}
1618
1619template <class _ForwardIterator1, class _ForwardIterator2>
1620inline _LIBCPP_INLINE_VISIBILITY
1621_ForwardIterator1
1622search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1623 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1624{
Marshall Clowf6d6b512016-03-08 15:12:52 +00001625 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1626 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001627 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001628}
1629
1630// search_n
1631
1632template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
1633_ForwardIterator
1634__search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001635 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001636{
1637 if (__count <= 0)
1638 return __first;
1639 while (true)
1640 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001641 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001642 while (true)
1643 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001644 if (__first == __last) // return __last if no element matches __value_
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001645 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001646 if (__pred(*__first, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001647 break;
1648 ++__first;
1649 }
Howard Hinnant78b68282011-10-22 20:59:45 +00001650 // *__first matches __value_, now match elements after here
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001651 _ForwardIterator __m = __first;
1652 _Size __c(0);
1653 while (true)
1654 {
1655 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1656 return __first;
1657 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1658 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001659 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001660 {
1661 __first = __m;
1662 ++__first;
1663 break;
1664 } // else there is a match, check next elements
1665 }
1666 }
1667}
1668
1669template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
1670_RandomAccessIterator
1671__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001672 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001673{
1674 if (__count <= 0)
1675 return __first;
1676 _Size __len = static_cast<_Size>(__last - __first);
1677 if (__len < __count)
1678 return __last;
1679 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1680 while (true)
1681 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001682 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001683 while (true)
1684 {
Howard Hinnant128f7bf2013-04-04 15:40:48 +00001685 if (__first >= __s) // return __last if no element matches __value_
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001686 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001687 if (__pred(*__first, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001688 break;
1689 ++__first;
1690 }
Howard Hinnant78b68282011-10-22 20:59:45 +00001691 // *__first matches __value_, now match elements after here
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001692 _RandomAccessIterator __m = __first;
1693 _Size __c(0);
1694 while (true)
1695 {
1696 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1697 return __first;
1698 ++__m; // no need to check range on __m because __s guarantees we have enough source
Howard Hinnant78b68282011-10-22 20:59:45 +00001699 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001700 {
1701 __first = __m;
1702 ++__first;
1703 break;
1704 } // else there is a match, check next elements
1705 }
1706 }
1707}
1708
1709template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
1710inline _LIBCPP_INLINE_VISIBILITY
1711_ForwardIterator
1712search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001713 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001714{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001715 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001716 (__first, __last, __convert_to_integral(__count), __value_, __pred,
1717 typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001718}
1719
1720template <class _ForwardIterator, class _Size, class _Tp>
1721inline _LIBCPP_INLINE_VISIBILITY
1722_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00001723search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001724{
1725 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001726 return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
1727 __value_, __equal_to<__v, _Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001728}
1729
1730// copy
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001731template <class _Iter>
1732inline _LIBCPP_INLINE_VISIBILITY
1733_Iter
1734__unwrap_iter(_Iter __i)
1735{
1736 return __i;
1737}
1738
1739template <class _Tp>
1740inline _LIBCPP_INLINE_VISIBILITY
1741typename enable_if
1742<
Howard Hinnant1468b662010-11-19 22:17:28 +00001743 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001744 _Tp*
1745>::type
1746__unwrap_iter(move_iterator<_Tp*> __i)
1747{
1748 return __i.base();
1749}
1750
Howard Hinnant499cea12013-08-23 17:37:05 +00001751#if _LIBCPP_DEBUG_LEVEL < 2
1752
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001753template <class _Tp>
1754inline _LIBCPP_INLINE_VISIBILITY
1755typename enable_if
1756<
Howard Hinnant1468b662010-11-19 22:17:28 +00001757 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001758 _Tp*
1759>::type
1760__unwrap_iter(__wrap_iter<_Tp*> __i)
1761{
1762 return __i.base();
1763}
1764
Eric Fiselier2c8aa052016-12-28 05:35:32 +00001765#else
1766
1767template <class _Tp>
1768inline _LIBCPP_INLINE_VISIBILITY
1769typename enable_if
1770<
1771 is_trivially_copy_assignable<_Tp>::value,
1772 __wrap_iter<_Tp*>
1773>::type
1774__unwrap_iter(__wrap_iter<_Tp*> __i)
1775{
1776 return __i;
1777}
1778
Howard Hinnant499cea12013-08-23 17:37:05 +00001779#endif // _LIBCPP_DEBUG_LEVEL < 2
1780
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001781template <class _InputIterator, class _OutputIterator>
1782inline _LIBCPP_INLINE_VISIBILITY
1783_OutputIterator
1784__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1785{
Eric Fiselierb9919752014-10-27 19:28:20 +00001786 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001787 *__result = *__first;
1788 return __result;
1789}
1790
1791template <class _Tp, class _Up>
1792inline _LIBCPP_INLINE_VISIBILITY
1793typename enable_if
1794<
1795 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001796 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001797 _Up*
1798>::type
1799__copy(_Tp* __first, _Tp* __last, _Up* __result)
1800{
1801 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001802 if (__n > 0)
1803 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001804 return __result + __n;
1805}
1806
1807template <class _InputIterator, class _OutputIterator>
1808inline _LIBCPP_INLINE_VISIBILITY
1809_OutputIterator
1810copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1811{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001812 return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001813}
1814
1815// copy_backward
1816
Howard Hinnantb73568d2013-02-06 21:03:39 +00001817template <class _BidirectionalIterator, class _OutputIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001818inline _LIBCPP_INLINE_VISIBILITY
1819_OutputIterator
Howard Hinnantb73568d2013-02-06 21:03:39 +00001820__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001821{
1822 while (__first != __last)
1823 *--__result = *--__last;
1824 return __result;
1825}
1826
1827template <class _Tp, class _Up>
1828inline _LIBCPP_INLINE_VISIBILITY
1829typename enable_if
1830<
1831 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001832 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001833 _Up*
1834>::type
1835__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1836{
1837 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001838 if (__n > 0)
1839 {
1840 __result -= __n;
1841 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1842 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001843 return __result;
1844}
1845
1846template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1847inline _LIBCPP_INLINE_VISIBILITY
1848_BidirectionalIterator2
1849copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1850 _BidirectionalIterator2 __result)
1851{
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00001852 return _VSTD::__copy_backward(__unwrap_iter(__first),
1853 __unwrap_iter(__last),
1854 __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001855}
1856
1857// copy_if
1858
1859template<class _InputIterator, class _OutputIterator, class _Predicate>
1860inline _LIBCPP_INLINE_VISIBILITY
1861_OutputIterator
1862copy_if(_InputIterator __first, _InputIterator __last,
1863 _OutputIterator __result, _Predicate __pred)
1864{
1865 for (; __first != __last; ++__first)
1866 {
1867 if (__pred(*__first))
1868 {
1869 *__result = *__first;
1870 ++__result;
1871 }
1872 }
1873 return __result;
1874}
1875
1876// copy_n
1877
1878template<class _InputIterator, class _Size, class _OutputIterator>
1879inline _LIBCPP_INLINE_VISIBILITY
1880typename enable_if
1881<
1882 __is_input_iterator<_InputIterator>::value &&
1883 !__is_random_access_iterator<_InputIterator>::value,
1884 _OutputIterator
1885>::type
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001886copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001887{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001888 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1889 _IntegralSize __n = __orig_n;
Howard Hinnant171869e2011-02-27 20:55:39 +00001890 if (__n > 0)
1891 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001892 *__result = *__first;
Howard Hinnant171869e2011-02-27 20:55:39 +00001893 ++__result;
1894 for (--__n; __n > 0; --__n)
1895 {
1896 ++__first;
1897 *__result = *__first;
1898 ++__result;
1899 }
1900 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001901 return __result;
1902}
1903
1904template<class _InputIterator, class _Size, class _OutputIterator>
1905inline _LIBCPP_INLINE_VISIBILITY
1906typename enable_if
1907<
1908 __is_random_access_iterator<_InputIterator>::value,
1909 _OutputIterator
1910>::type
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001911copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001912{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001913 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1914 _IntegralSize __n = __orig_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001915 return _VSTD::copy(__first, __first + __n, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001916}
1917
1918// move
1919
1920template <class _InputIterator, class _OutputIterator>
1921inline _LIBCPP_INLINE_VISIBILITY
1922_OutputIterator
1923__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1924{
Eric Fiselierb9919752014-10-27 19:28:20 +00001925 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001926 *__result = _VSTD::move(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001927 return __result;
1928}
1929
1930template <class _Tp, class _Up>
1931inline _LIBCPP_INLINE_VISIBILITY
1932typename enable_if
1933<
1934 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001935 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001936 _Up*
1937>::type
1938__move(_Tp* __first, _Tp* __last, _Up* __result)
1939{
1940 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001941 if (__n > 0)
1942 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001943 return __result + __n;
1944}
1945
1946template <class _InputIterator, class _OutputIterator>
1947inline _LIBCPP_INLINE_VISIBILITY
1948_OutputIterator
1949move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1950{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001951 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001952}
1953
1954// move_backward
1955
1956template <class _InputIterator, class _OutputIterator>
1957inline _LIBCPP_INLINE_VISIBILITY
1958_OutputIterator
1959__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1960{
1961 while (__first != __last)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001962 *--__result = _VSTD::move(*--__last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001963 return __result;
1964}
1965
1966template <class _Tp, class _Up>
1967inline _LIBCPP_INLINE_VISIBILITY
1968typename enable_if
1969<
1970 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001971 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001972 _Up*
1973>::type
1974__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1975{
1976 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001977 if (__n > 0)
1978 {
1979 __result -= __n;
1980 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1981 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001982 return __result;
1983}
1984
1985template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1986inline _LIBCPP_INLINE_VISIBILITY
1987_BidirectionalIterator2
1988move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1989 _BidirectionalIterator2 __result)
1990{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001991 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001992}
1993
1994// iter_swap
1995
Howard Hinnante9b2c2d2011-05-27 15:04:19 +00001996// moved to <type_traits> for better swap / noexcept support
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001997
1998// transform
1999
2000template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
2001inline _LIBCPP_INLINE_VISIBILITY
2002_OutputIterator
2003transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
2004{
Eric Fiselierb9919752014-10-27 19:28:20 +00002005 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002006 *__result = __op(*__first);
2007 return __result;
2008}
2009
2010template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
2011inline _LIBCPP_INLINE_VISIBILITY
2012_OutputIterator
2013transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
2014 _OutputIterator __result, _BinaryOperation __binary_op)
2015{
Eric Fiselierb9919752014-10-27 19:28:20 +00002016 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002017 *__result = __binary_op(*__first1, *__first2);
2018 return __result;
2019}
2020
2021// replace
2022
2023template <class _ForwardIterator, class _Tp>
2024inline _LIBCPP_INLINE_VISIBILITY
2025void
2026replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
2027{
2028 for (; __first != __last; ++__first)
2029 if (*__first == __old_value)
2030 *__first = __new_value;
2031}
2032
2033// replace_if
2034
2035template <class _ForwardIterator, class _Predicate, class _Tp>
2036inline _LIBCPP_INLINE_VISIBILITY
2037void
2038replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
2039{
2040 for (; __first != __last; ++__first)
2041 if (__pred(*__first))
2042 *__first = __new_value;
2043}
2044
2045// replace_copy
2046
2047template <class _InputIterator, class _OutputIterator, class _Tp>
2048inline _LIBCPP_INLINE_VISIBILITY
2049_OutputIterator
2050replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2051 const _Tp& __old_value, const _Tp& __new_value)
2052{
Eric Fiselierb9919752014-10-27 19:28:20 +00002053 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002054 if (*__first == __old_value)
2055 *__result = __new_value;
2056 else
2057 *__result = *__first;
2058 return __result;
2059}
2060
2061// replace_copy_if
2062
2063template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
2064inline _LIBCPP_INLINE_VISIBILITY
2065_OutputIterator
2066replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2067 _Predicate __pred, const _Tp& __new_value)
2068{
Eric Fiselierb9919752014-10-27 19:28:20 +00002069 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070 if (__pred(*__first))
2071 *__result = __new_value;
2072 else
2073 *__result = *__first;
2074 return __result;
2075}
2076
2077// fill_n
2078
2079template <class _OutputIterator, class _Size, class _Tp>
2080inline _LIBCPP_INLINE_VISIBILITY
2081_OutputIterator
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002082__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002083{
Eric Fiselierb9919752014-10-27 19:28:20 +00002084 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnant78b68282011-10-22 20:59:45 +00002085 *__first = __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002086 return __first;
2087}
2088
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002089template <class _Tp, class _Size, class _Up>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002090inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002091typename enable_if
2092<
2093 is_integral<_Tp>::value && sizeof(_Tp) == 1 &&
2094 !is_same<_Tp, bool>::value &&
2095 is_integral<_Up>::value && sizeof(_Up) == 1,
2096 _Tp*
2097>::type
2098__fill_n(_Tp* __first, _Size __n,_Up __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002099{
2100 if (__n > 0)
Howard Hinnant78b68282011-10-22 20:59:45 +00002101 _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002102 return __first + __n;
2103}
2104
2105template <class _OutputIterator, class _Size, class _Tp>
2106inline _LIBCPP_INLINE_VISIBILITY
2107_OutputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002108fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002109{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002110 return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002111}
2112
2113// fill
2114
2115template <class _ForwardIterator, class _Tp>
2116inline _LIBCPP_INLINE_VISIBILITY
2117void
Howard Hinnant78b68282011-10-22 20:59:45 +00002118__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002119{
2120 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +00002121 *__first = __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002122}
2123
2124template <class _RandomAccessIterator, class _Tp>
2125inline _LIBCPP_INLINE_VISIBILITY
2126void
Howard Hinnant78b68282011-10-22 20:59:45 +00002127__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002128{
Howard Hinnant78b68282011-10-22 20:59:45 +00002129 _VSTD::fill_n(__first, __last - __first, __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002130}
2131
2132template <class _ForwardIterator, class _Tp>
2133inline _LIBCPP_INLINE_VISIBILITY
2134void
Howard Hinnant78b68282011-10-22 20:59:45 +00002135fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002136{
Howard Hinnant78b68282011-10-22 20:59:45 +00002137 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002138}
2139
2140// generate
2141
2142template <class _ForwardIterator, class _Generator>
2143inline _LIBCPP_INLINE_VISIBILITY
2144void
2145generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2146{
2147 for (; __first != __last; ++__first)
2148 *__first = __gen();
2149}
2150
2151// generate_n
2152
2153template <class _OutputIterator, class _Size, class _Generator>
2154inline _LIBCPP_INLINE_VISIBILITY
2155_OutputIterator
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002156generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002157{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002158 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
2159 _IntegralSize __n = __orig_n;
Eric Fiselierb9919752014-10-27 19:28:20 +00002160 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002161 *__first = __gen();
2162 return __first;
2163}
2164
2165// remove
2166
2167template <class _ForwardIterator, class _Tp>
2168_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002169remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002170{
Howard Hinnant78b68282011-10-22 20:59:45 +00002171 __first = _VSTD::find(__first, __last, __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002172 if (__first != __last)
2173 {
2174 _ForwardIterator __i = __first;
2175 while (++__i != __last)
2176 {
Howard Hinnant78b68282011-10-22 20:59:45 +00002177 if (!(*__i == __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002178 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002179 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002180 ++__first;
2181 }
2182 }
2183 }
2184 return __first;
2185}
2186
2187// remove_if
2188
2189template <class _ForwardIterator, class _Predicate>
2190_ForwardIterator
2191remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2192{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002193 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002194 (__first, __last, __pred);
2195 if (__first != __last)
2196 {
2197 _ForwardIterator __i = __first;
2198 while (++__i != __last)
2199 {
2200 if (!__pred(*__i))
2201 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002202 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002203 ++__first;
2204 }
2205 }
2206 }
2207 return __first;
2208}
2209
2210// remove_copy
2211
2212template <class _InputIterator, class _OutputIterator, class _Tp>
2213inline _LIBCPP_INLINE_VISIBILITY
2214_OutputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002215remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002216{
2217 for (; __first != __last; ++__first)
2218 {
Howard Hinnant78b68282011-10-22 20:59:45 +00002219 if (!(*__first == __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002220 {
2221 *__result = *__first;
2222 ++__result;
2223 }
2224 }
2225 return __result;
2226}
2227
2228// remove_copy_if
2229
2230template <class _InputIterator, class _OutputIterator, class _Predicate>
2231inline _LIBCPP_INLINE_VISIBILITY
2232_OutputIterator
2233remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2234{
2235 for (; __first != __last; ++__first)
2236 {
2237 if (!__pred(*__first))
2238 {
2239 *__result = *__first;
2240 ++__result;
2241 }
2242 }
2243 return __result;
2244}
2245
2246// unique
2247
2248template <class _ForwardIterator, class _BinaryPredicate>
2249_ForwardIterator
2250unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2251{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002252 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002253 (__first, __last, __pred);
2254 if (__first != __last)
2255 {
2256 // ... a a ? ...
2257 // f i
2258 _ForwardIterator __i = __first;
2259 for (++__i; ++__i != __last;)
2260 if (!__pred(*__first, *__i))
Howard Hinnant0949eed2011-06-30 21:18:19 +00002261 *++__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002262 ++__first;
2263 }
2264 return __first;
2265}
2266
2267template <class _ForwardIterator>
2268inline _LIBCPP_INLINE_VISIBILITY
2269_ForwardIterator
2270unique(_ForwardIterator __first, _ForwardIterator __last)
2271{
2272 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002273 return _VSTD::unique(__first, __last, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002274}
2275
2276// unique_copy
2277
2278template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
2279_OutputIterator
2280__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2281 input_iterator_tag, output_iterator_tag)
2282{
2283 if (__first != __last)
2284 {
2285 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2286 *__result = __t;
2287 ++__result;
2288 while (++__first != __last)
2289 {
2290 if (!__pred(__t, *__first))
2291 {
2292 __t = *__first;
2293 *__result = __t;
2294 ++__result;
2295 }
2296 }
2297 }
2298 return __result;
2299}
2300
2301template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
2302_OutputIterator
2303__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2304 forward_iterator_tag, output_iterator_tag)
2305{
2306 if (__first != __last)
2307 {
2308 _ForwardIterator __i = __first;
2309 *__result = *__i;
2310 ++__result;
2311 while (++__first != __last)
2312 {
2313 if (!__pred(*__i, *__first))
2314 {
2315 *__result = *__first;
2316 ++__result;
2317 __i = __first;
2318 }
2319 }
2320 }
2321 return __result;
2322}
2323
2324template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
2325_ForwardIterator
2326__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2327 input_iterator_tag, forward_iterator_tag)
2328{
2329 if (__first != __last)
2330 {
2331 *__result = *__first;
2332 while (++__first != __last)
2333 if (!__pred(*__result, *__first))
2334 *++__result = *__first;
2335 ++__result;
2336 }
2337 return __result;
2338}
2339
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002340template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
2341inline _LIBCPP_INLINE_VISIBILITY
2342_OutputIterator
2343unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2344{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002345 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002346 (__first, __last, __result, __pred,
2347 typename iterator_traits<_InputIterator>::iterator_category(),
2348 typename iterator_traits<_OutputIterator>::iterator_category());
2349}
2350
2351template <class _InputIterator, class _OutputIterator>
2352inline _LIBCPP_INLINE_VISIBILITY
2353_OutputIterator
2354unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2355{
2356 typedef typename iterator_traits<_InputIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002357 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002358}
2359
2360// reverse
2361
2362template <class _BidirectionalIterator>
2363inline _LIBCPP_INLINE_VISIBILITY
2364void
2365__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2366{
2367 while (__first != __last)
2368 {
2369 if (__first == --__last)
2370 break;
Marshall Clowc010bd62015-11-02 21:34:25 +00002371 _VSTD::iter_swap(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002372 ++__first;
2373 }
2374}
2375
2376template <class _RandomAccessIterator>
2377inline _LIBCPP_INLINE_VISIBILITY
2378void
2379__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2380{
2381 if (__first != __last)
2382 for (; __first < --__last; ++__first)
Marshall Clowc010bd62015-11-02 21:34:25 +00002383 _VSTD::iter_swap(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002384}
2385
2386template <class _BidirectionalIterator>
2387inline _LIBCPP_INLINE_VISIBILITY
2388void
2389reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2390{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002391 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002392}
2393
2394// reverse_copy
2395
2396template <class _BidirectionalIterator, class _OutputIterator>
2397inline _LIBCPP_INLINE_VISIBILITY
2398_OutputIterator
2399reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2400{
2401 for (; __first != __last; ++__result)
2402 *__result = *--__last;
2403 return __result;
2404}
2405
2406// rotate
2407
2408template <class _ForwardIterator>
2409_ForwardIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002410__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002411{
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002412 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2413 value_type __tmp = _VSTD::move(*__first);
2414 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2415 *__lm1 = _VSTD::move(__tmp);
2416 return __lm1;
2417}
2418
2419template <class _BidirectionalIterator>
2420_BidirectionalIterator
2421__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2422{
2423 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2424 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2425 value_type __tmp = _VSTD::move(*__lm1);
2426 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2427 *__first = _VSTD::move(__tmp);
2428 return __fp1;
2429}
2430
2431template <class _ForwardIterator>
2432_ForwardIterator
2433__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2434{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002435 _ForwardIterator __i = __middle;
2436 while (true)
2437 {
2438 swap(*__first, *__i);
2439 ++__first;
2440 if (++__i == __last)
2441 break;
2442 if (__first == __middle)
2443 __middle = __i;
2444 }
2445 _ForwardIterator __r = __first;
2446 if (__first != __middle)
2447 {
2448 __i = __middle;
2449 while (true)
2450 {
2451 swap(*__first, *__i);
2452 ++__first;
2453 if (++__i == __last)
2454 {
2455 if (__first == __middle)
2456 break;
2457 __i = __middle;
2458 }
2459 else if (__first == __middle)
2460 __middle = __i;
2461 }
2462 }
2463 return __r;
2464}
2465
2466template<typename _Integral>
2467inline _LIBCPP_INLINE_VISIBILITY
2468_Integral
Marshall Clow1c1e91d2016-07-26 14:29:45 +00002469__algo_gcd(_Integral __x, _Integral __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002470{
2471 do
2472 {
2473 _Integral __t = __x % __y;
2474 __x = __y;
2475 __y = __t;
2476 } while (__y);
2477 return __x;
2478}
2479
2480template<typename _RandomAccessIterator>
2481_RandomAccessIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002482__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002483{
2484 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2485 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant324bb032010-08-22 00:02:43 +00002486
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002487 const difference_type __m1 = __middle - __first;
2488 const difference_type __m2 = __last - __middle;
2489 if (__m1 == __m2)
2490 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002491 _VSTD::swap_ranges(__first, __middle, __middle);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002492 return __middle;
2493 }
Marshall Clow1c1e91d2016-07-26 14:29:45 +00002494 const difference_type __g = _VSTD::__algo_gcd(__m1, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002495 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2496 {
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002497 value_type __t(_VSTD::move(*--__p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002498 _RandomAccessIterator __p1 = __p;
2499 _RandomAccessIterator __p2 = __p1 + __m1;
2500 do
2501 {
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002502 *__p1 = _VSTD::move(*__p2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002503 __p1 = __p2;
2504 const difference_type __d = __last - __p2;
2505 if (__m1 < __d)
2506 __p2 += __m1;
2507 else
2508 __p2 = __first + (__m1 - __d);
2509 } while (__p2 != __p);
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002510 *__p1 = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002511 }
2512 return __first + __m2;
2513}
2514
2515template <class _ForwardIterator>
2516inline _LIBCPP_INLINE_VISIBILITY
2517_ForwardIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002518__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2519 _VSTD::forward_iterator_tag)
2520{
2521 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2522 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2523 {
2524 if (_VSTD::next(__first) == __middle)
2525 return _VSTD::__rotate_left(__first, __last);
2526 }
2527 return _VSTD::__rotate_forward(__first, __middle, __last);
2528}
2529
2530template <class _BidirectionalIterator>
2531inline _LIBCPP_INLINE_VISIBILITY
2532_BidirectionalIterator
2533__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2534 _VSTD::bidirectional_iterator_tag)
2535{
2536 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2537 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2538 {
2539 if (_VSTD::next(__first) == __middle)
2540 return _VSTD::__rotate_left(__first, __last);
2541 if (_VSTD::next(__middle) == __last)
2542 return _VSTD::__rotate_right(__first, __last);
2543 }
2544 return _VSTD::__rotate_forward(__first, __middle, __last);
2545}
2546
2547template <class _RandomAccessIterator>
2548inline _LIBCPP_INLINE_VISIBILITY
2549_RandomAccessIterator
2550__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2551 _VSTD::random_access_iterator_tag)
2552{
2553 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2554 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2555 {
2556 if (_VSTD::next(__first) == __middle)
2557 return _VSTD::__rotate_left(__first, __last);
2558 if (_VSTD::next(__middle) == __last)
2559 return _VSTD::__rotate_right(__first, __last);
2560 return _VSTD::__rotate_gcd(__first, __middle, __last);
2561 }
2562 return _VSTD::__rotate_forward(__first, __middle, __last);
2563}
2564
2565template <class _ForwardIterator>
2566inline _LIBCPP_INLINE_VISIBILITY
2567_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002568rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2569{
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002570 if (__first == __middle)
2571 return __last;
2572 if (__middle == __last)
2573 return __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002574 return _VSTD::__rotate(__first, __middle, __last,
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002575 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002576}
2577
2578// rotate_copy
2579
2580template <class _ForwardIterator, class _OutputIterator>
2581inline _LIBCPP_INLINE_VISIBILITY
2582_OutputIterator
2583rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2584{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002585 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002586}
2587
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002588// min_element
2589
2590template <class _ForwardIterator, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002591inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002592_ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +00002593min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002594{
2595 if (__first != __last)
2596 {
2597 _ForwardIterator __i = __first;
2598 while (++__i != __last)
2599 if (__comp(*__i, *__first))
2600 __first = __i;
2601 }
2602 return __first;
2603}
2604
2605template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002606inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002607_ForwardIterator
2608min_element(_ForwardIterator __first, _ForwardIterator __last)
2609{
Marshall Clow928735a2015-05-10 13:53:31 +00002610 return _VSTD::min_element(__first, __last,
Howard Hinnant98e5d972010-08-21 20:10:01 +00002611 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2612}
2613
2614// min
2615
2616template <class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002617inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002618const _Tp&
2619min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2620{
2621 return __comp(__b, __a) ? __b : __a;
2622}
2623
2624template <class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002625inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002626const _Tp&
2627min(const _Tp& __a, const _Tp& __b)
2628{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002629 return _VSTD::min(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002630}
2631
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002632#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002633
Howard Hinnant98e5d972010-08-21 20:10:01 +00002634template<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 +00002636_Tp
2637min(initializer_list<_Tp> __t, _Compare __comp)
2638{
Marshall Clow928735a2015-05-10 13:53:31 +00002639 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002640}
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 +00002644_Tp
2645min(initializer_list<_Tp> __t)
2646{
Marshall Clow928735a2015-05-10 13:53:31 +00002647 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002648}
2649
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002650#endif // _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002651
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002652// max_element
2653
2654template <class _ForwardIterator, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002655inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002656_ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +00002657max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002658{
2659 if (__first != __last)
2660 {
2661 _ForwardIterator __i = __first;
2662 while (++__i != __last)
2663 if (__comp(*__first, *__i))
2664 __first = __i;
2665 }
2666 return __first;
2667}
2668
Marshall Clow9d9463a2014-02-19 16:51:35 +00002669
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002670template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002671inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002672_ForwardIterator
2673max_element(_ForwardIterator __first, _ForwardIterator __last)
2674{
Marshall Clow928735a2015-05-10 13:53:31 +00002675 return _VSTD::max_element(__first, __last,
Howard Hinnant98e5d972010-08-21 20:10:01 +00002676 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2677}
2678
2679// max
2680
2681template <class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002682inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002683const _Tp&
2684max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2685{
2686 return __comp(__a, __b) ? __b : __a;
2687}
2688
2689template <class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002690inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002691const _Tp&
2692max(const _Tp& __a, const _Tp& __b)
2693{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002694 return _VSTD::max(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002695}
2696
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002697#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002698
Howard Hinnant98e5d972010-08-21 20:10:01 +00002699template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002700inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002701_Tp
2702max(initializer_list<_Tp> __t, _Compare __comp)
2703{
Marshall Clow928735a2015-05-10 13:53:31 +00002704 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002705}
2706
2707template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002708inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002709_Tp
2710max(initializer_list<_Tp> __t)
2711{
Marshall Clow928735a2015-05-10 13:53:31 +00002712 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002713}
2714
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002715#endif // _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002716
Marshall Clow3e0808e2016-03-07 22:43:49 +00002717#if _LIBCPP_STD_VER > 14
2718// clamp
2719template<class _Tp, class _Compare>
2720inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2721const _Tp&
2722clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
2723{
2724 _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
2725 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
2726
2727}
2728
2729template<class _Tp>
2730inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2731const _Tp&
2732clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
2733{
2734 return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
2735}
2736#endif
2737
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002738// minmax_element
2739
2740template <class _ForwardIterator, class _Compare>
Marshall Clow928735a2015-05-10 13:53:31 +00002741_LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002742std::pair<_ForwardIterator, _ForwardIterator>
2743minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2744{
2745 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2746 if (__first != __last)
2747 {
2748 if (++__first != __last)
2749 {
2750 if (__comp(*__first, *__result.first))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002751 __result.first = __first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002752 else
2753 __result.second = __first;
2754 while (++__first != __last)
2755 {
2756 _ForwardIterator __i = __first;
2757 if (++__first == __last)
2758 {
2759 if (__comp(*__i, *__result.first))
2760 __result.first = __i;
2761 else if (!__comp(*__i, *__result.second))
2762 __result.second = __i;
2763 break;
2764 }
2765 else
2766 {
2767 if (__comp(*__first, *__i))
2768 {
2769 if (__comp(*__first, *__result.first))
2770 __result.first = __first;
2771 if (!__comp(*__i, *__result.second))
2772 __result.second = __i;
2773 }
2774 else
2775 {
2776 if (__comp(*__i, *__result.first))
2777 __result.first = __i;
2778 if (!__comp(*__first, *__result.second))
2779 __result.second = __first;
2780 }
2781 }
2782 }
2783 }
2784 }
2785 return __result;
2786}
2787
2788template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002789inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002790std::pair<_ForwardIterator, _ForwardIterator>
2791minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2792{
Marshall Clow9d9463a2014-02-19 16:51:35 +00002793 return _VSTD::minmax_element(__first, __last,
2794 __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002795}
2796
Howard Hinnant98e5d972010-08-21 20:10:01 +00002797// minmax
2798
2799template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002800inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002801pair<const _Tp&, const _Tp&>
2802minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2803{
2804 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2805 pair<const _Tp&, const _Tp&>(__a, __b);
2806}
2807
2808template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002809inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002810pair<const _Tp&, const _Tp&>
2811minmax(const _Tp& __a, const _Tp& __b)
2812{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002813 return _VSTD::minmax(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002814}
2815
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002816#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002817
Howard Hinnant98e5d972010-08-21 20:10:01 +00002818template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002819inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002820pair<_Tp, _Tp>
2821minmax(initializer_list<_Tp> __t, _Compare __comp)
2822{
Marshall Clow9d9463a2014-02-19 16:51:35 +00002823 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2824 _Iter __first = __t.begin();
2825 _Iter __last = __t.end();
Marshall Clow3024f862015-02-11 15:41:34 +00002826 std::pair<_Tp, _Tp> __result(*__first, *__first);
Marshall Clow9d9463a2014-02-19 16:51:35 +00002827
2828 ++__first;
2829 if (__t.size() % 2 == 0)
2830 {
2831 if (__comp(*__first, __result.first))
2832 __result.first = *__first;
2833 else
2834 __result.second = *__first;
2835 ++__first;
2836 }
Aditya Kumarfdb4f172016-08-25 11:52:38 +00002837
Marshall Clow9d9463a2014-02-19 16:51:35 +00002838 while (__first != __last)
2839 {
2840 _Tp __prev = *__first++;
Marshall Clow3024f862015-02-11 15:41:34 +00002841 if (__comp(*__first, __prev)) {
2842 if ( __comp(*__first, __result.first)) __result.first = *__first;
2843 if (!__comp(__prev, __result.second)) __result.second = __prev;
Marshall Clow9d9463a2014-02-19 16:51:35 +00002844 }
2845 else {
Marshall Clow3024f862015-02-11 15:41:34 +00002846 if ( __comp(__prev, __result.first)) __result.first = __prev;
2847 if (!__comp(*__first, __result.second)) __result.second = *__first;
Marshall Clow9d9463a2014-02-19 16:51:35 +00002848 }
Aditya Kumarfdb4f172016-08-25 11:52:38 +00002849
Marshall Clow9d9463a2014-02-19 16:51:35 +00002850 __first++;
2851 }
2852 return __result;
2853}
2854
2855template<class _Tp>
2856inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2857pair<_Tp, _Tp>
2858minmax(initializer_list<_Tp> __t)
2859{
2860 return _VSTD::minmax(__t, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002861}
2862
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002863#endif // _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00002864
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002865// random_shuffle
2866
Howard Hinnantc3267212010-05-26 17:49:34 +00002867// __independent_bits_engine
2868
Howard Hinnant99968442011-11-29 18:15:50 +00002869template <unsigned long long _Xp, size_t _Rp>
Howard Hinnantc3267212010-05-26 17:49:34 +00002870struct __log2_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002871{
Howard Hinnant99968442011-11-29 18:15:50 +00002872 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2873 : __log2_imp<_Xp, _Rp - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002874};
2875
Howard Hinnant99968442011-11-29 18:15:50 +00002876template <unsigned long long _Xp>
2877struct __log2_imp<_Xp, 0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002878{
Howard Hinnantc3267212010-05-26 17:49:34 +00002879 static const size_t value = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002880};
2881
Howard Hinnant99968442011-11-29 18:15:50 +00002882template <size_t _Rp>
2883struct __log2_imp<0, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002884{
Howard Hinnant99968442011-11-29 18:15:50 +00002885 static const size_t value = _Rp + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002886};
2887
Howard Hinnant99968442011-11-29 18:15:50 +00002888template <class _UI, _UI _Xp>
Howard Hinnantc3267212010-05-26 17:49:34 +00002889struct __log2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002890{
Howard Hinnant99968442011-11-29 18:15:50 +00002891 static const size_t value = __log2_imp<_Xp,
Howard Hinnantc3267212010-05-26 17:49:34 +00002892 sizeof(_UI) * __CHAR_BIT__ - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002893};
2894
Howard Hinnantc3267212010-05-26 17:49:34 +00002895template<class _Engine, class _UIntType>
2896class __independent_bits_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002897{
Howard Hinnantc3267212010-05-26 17:49:34 +00002898public:
2899 // types
2900 typedef _UIntType result_type;
2901
2902private:
2903 typedef typename _Engine::result_type _Engine_result_type;
2904 typedef typename conditional
2905 <
2906 sizeof(_Engine_result_type) <= sizeof(result_type),
2907 result_type,
2908 _Engine_result_type
2909 >::type _Working_result_type;
2910
2911 _Engine& __e_;
2912 size_t __w_;
2913 size_t __w0_;
2914 size_t __n_;
2915 size_t __n0_;
2916 _Working_result_type __y0_;
2917 _Working_result_type __y1_;
2918 _Engine_result_type __mask0_;
2919 _Engine_result_type __mask1_;
2920
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00002921#ifdef _LIBCPP_CXX03_LANG
Howard Hinnant99968442011-11-29 18:15:50 +00002922 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002923 + _Working_result_type(1);
2924#else
2925 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2926 + _Working_result_type(1);
2927#endif
2928 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2929 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2930 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
Howard Hinnantc3267212010-05-26 17:49:34 +00002931
2932public:
2933 // constructors and seeding functions
2934 __independent_bits_engine(_Engine& __e, size_t __w);
2935
2936 // generating functions
Howard Hinnant99968442011-11-29 18:15:50 +00002937 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantc3267212010-05-26 17:49:34 +00002938
2939private:
2940 result_type __eval(false_type);
2941 result_type __eval(true_type);
2942};
2943
2944template<class _Engine, class _UIntType>
2945__independent_bits_engine<_Engine, _UIntType>
2946 ::__independent_bits_engine(_Engine& __e, size_t __w)
2947 : __e_(__e),
2948 __w_(__w)
2949{
2950 __n_ = __w_ / __m + (__w_ % __m != 0);
2951 __w0_ = __w_ / __n_;
Howard Hinnant99968442011-11-29 18:15:50 +00002952 if (_Rp == 0)
2953 __y0_ = _Rp;
Howard Hinnantc3267212010-05-26 17:49:34 +00002954 else if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00002955 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002956 else
2957 __y0_ = 0;
Howard Hinnant99968442011-11-29 18:15:50 +00002958 if (_Rp - __y0_ > __y0_ / __n_)
Howard Hinnantc3267212010-05-26 17:49:34 +00002959 {
2960 ++__n_;
2961 __w0_ = __w_ / __n_;
2962 if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00002963 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002964 else
2965 __y0_ = 0;
2966 }
2967 __n0_ = __n_ - __w_ % __n_;
2968 if (__w0_ < _WDt - 1)
Howard Hinnant99968442011-11-29 18:15:50 +00002969 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
Howard Hinnantc3267212010-05-26 17:49:34 +00002970 else
2971 __y1_ = 0;
2972 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2973 _Engine_result_type(0);
2974 __mask1_ = __w0_ < _EDt - 1 ?
2975 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2976 _Engine_result_type(~0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002977}
2978
Howard Hinnantc3267212010-05-26 17:49:34 +00002979template<class _Engine, class _UIntType>
2980inline
2981_UIntType
2982__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002983{
Howard Hinnantc3267212010-05-26 17:49:34 +00002984 return static_cast<result_type>(__e_() & __mask0_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002985}
2986
Howard Hinnantc3267212010-05-26 17:49:34 +00002987template<class _Engine, class _UIntType>
2988_UIntType
2989__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002990{
Howard Hinnant99968442011-11-29 18:15:50 +00002991 result_type _Sp = 0;
Howard Hinnantc3267212010-05-26 17:49:34 +00002992 for (size_t __k = 0; __k < __n0_; ++__k)
2993 {
2994 _Engine_result_type __u;
2995 do
2996 {
2997 __u = __e_() - _Engine::min();
2998 } while (__u >= __y0_);
Howard Hinnant8faa95f2011-10-27 16:12:10 +00002999 if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00003000 _Sp <<= __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00003001 else
Howard Hinnant99968442011-11-29 18:15:50 +00003002 _Sp = 0;
3003 _Sp += __u & __mask0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00003004 }
3005 for (size_t __k = __n0_; __k < __n_; ++__k)
3006 {
3007 _Engine_result_type __u;
3008 do
3009 {
3010 __u = __e_() - _Engine::min();
3011 } while (__u >= __y1_);
Howard Hinnant8faa95f2011-10-27 16:12:10 +00003012 if (__w0_ < _WDt - 1)
Howard Hinnant99968442011-11-29 18:15:50 +00003013 _Sp <<= __w0_ + 1;
Howard Hinnantc3267212010-05-26 17:49:34 +00003014 else
Howard Hinnant99968442011-11-29 18:15:50 +00003015 _Sp = 0;
3016 _Sp += __u & __mask1_;
Howard Hinnantc3267212010-05-26 17:49:34 +00003017 }
Howard Hinnant99968442011-11-29 18:15:50 +00003018 return _Sp;
Howard Hinnantc3267212010-05-26 17:49:34 +00003019}
3020
3021// uniform_int_distribution
3022
3023template<class _IntType = int>
3024class uniform_int_distribution
3025{
3026public:
3027 // types
3028 typedef _IntType result_type;
3029
3030 class param_type
3031 {
3032 result_type __a_;
3033 result_type __b_;
3034 public:
3035 typedef uniform_int_distribution distribution_type;
3036
3037 explicit param_type(result_type __a = 0,
3038 result_type __b = numeric_limits<result_type>::max())
3039 : __a_(__a), __b_(__b) {}
3040
3041 result_type a() const {return __a_;}
3042 result_type b() const {return __b_;}
3043
3044 friend bool operator==(const param_type& __x, const param_type& __y)
3045 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3046 friend bool operator!=(const param_type& __x, const param_type& __y)
3047 {return !(__x == __y);}
3048 };
3049
3050private:
3051 param_type __p_;
3052
3053public:
3054 // constructors and reset functions
3055 explicit uniform_int_distribution(result_type __a = 0,
3056 result_type __b = numeric_limits<result_type>::max())
3057 : __p_(param_type(__a, __b)) {}
3058 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
3059 void reset() {}
3060
3061 // generating functions
3062 template<class _URNG> result_type operator()(_URNG& __g)
3063 {return (*this)(__g, __p_);}
3064 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3065
3066 // property functions
3067 result_type a() const {return __p_.a();}
3068 result_type b() const {return __p_.b();}
3069
3070 param_type param() const {return __p_;}
3071 void param(const param_type& __p) {__p_ = __p;}
3072
3073 result_type min() const {return a();}
3074 result_type max() const {return b();}
3075
3076 friend bool operator==(const uniform_int_distribution& __x,
3077 const uniform_int_distribution& __y)
3078 {return __x.__p_ == __y.__p_;}
3079 friend bool operator!=(const uniform_int_distribution& __x,
3080 const uniform_int_distribution& __y)
3081 {return !(__x == __y);}
3082};
3083
3084template<class _IntType>
3085template<class _URNG>
3086typename uniform_int_distribution<_IntType>::result_type
3087uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3088{
3089 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3090 uint32_t, uint64_t>::type _UIntType;
Howard Hinnant99968442011-11-29 18:15:50 +00003091 const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
3092 if (_Rp == 1)
Howard Hinnantc3267212010-05-26 17:49:34 +00003093 return __p.a();
3094 const size_t _Dt = numeric_limits<_UIntType>::digits;
3095 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
Howard Hinnant99968442011-11-29 18:15:50 +00003096 if (_Rp == 0)
Howard Hinnantc3267212010-05-26 17:49:34 +00003097 return static_cast<result_type>(_Eng(__g, _Dt)());
Howard Hinnant99968442011-11-29 18:15:50 +00003098 size_t __w = _Dt - __clz(_Rp) - 1;
Marshall Clow0934c752015-07-30 18:26:34 +00003099 if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
Howard Hinnantc3267212010-05-26 17:49:34 +00003100 ++__w;
3101 _Eng __e(__g, __w);
3102 _UIntType __u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003103 do
Howard Hinnantc3267212010-05-26 17:49:34 +00003104 {
3105 __u = __e();
Howard Hinnant99968442011-11-29 18:15:50 +00003106 } while (__u >= _Rp);
Howard Hinnantc3267212010-05-26 17:49:34 +00003107 return static_cast<result_type>(__u + __p.a());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003108}
3109
Eric Fiselier6547dc82017-04-03 23:23:44 +00003110#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \
3111 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003112class _LIBCPP_TYPE_VIS __rs_default;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003113
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003114_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantc3267212010-05-26 17:49:34 +00003115
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003116class _LIBCPP_TYPE_VIS __rs_default
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003117{
Howard Hinnantc3267212010-05-26 17:49:34 +00003118 static unsigned __c_;
3119
3120 __rs_default();
3121public:
Marshall Clow5920cfc2013-02-07 22:12:02 +00003122 typedef uint_fast32_t result_type;
Howard Hinnantc3267212010-05-26 17:49:34 +00003123
3124 static const result_type _Min = 0;
3125 static const result_type _Max = 0xFFFFFFFF;
3126
3127 __rs_default(const __rs_default&);
3128 ~__rs_default();
3129
3130 result_type operator()();
3131
Howard Hinnant27b4fd32012-04-02 00:40:41 +00003132 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
3133 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
Howard Hinnantc3267212010-05-26 17:49:34 +00003134
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003135 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003136};
3137
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003138_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003139
3140template <class _RandomAccessIterator>
3141void
3142random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
3143{
3144 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnant99968442011-11-29 18:15:50 +00003145 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3146 typedef typename _Dp::param_type _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003147 difference_type __d = __last - __first;
3148 if (__d > 1)
3149 {
Howard Hinnant99968442011-11-29 18:15:50 +00003150 _Dp __uid;
Howard Hinnantc3267212010-05-26 17:49:34 +00003151 __rs_default __g = __rs_get();
3152 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003153 {
Howard Hinnant99968442011-11-29 18:15:50 +00003154 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant4e599482010-10-22 15:26:39 +00003155 if (__i != difference_type(0))
3156 swap(*__first, *(__first + __i));
3157 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003158 }
3159}
3160
3161template <class _RandomAccessIterator, class _RandomNumberGenerator>
3162void
3163random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00003164#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003165 _RandomNumberGenerator&& __rand)
3166#else
3167 _RandomNumberGenerator& __rand)
3168#endif
3169{
3170 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3171 difference_type __d = __last - __first;
3172 if (__d > 1)
3173 {
3174 for (--__last; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003175 {
3176 difference_type __i = __rand(__d);
3177 swap(*__first, *(__first + __i));
3178 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003179 }
3180}
Marshall Clow03b862f2017-03-23 13:43:37 +00003181#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003182
Eric Fiselier917af0a2016-08-28 22:14:37 +00003183template <class _PopulationIterator, class _SampleIterator, class _Distance,
3184 class _UniformRandomNumberGenerator>
3185_LIBCPP_INLINE_VISIBILITY
3186_SampleIterator __sample(_PopulationIterator __first,
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003187 _PopulationIterator __last, _SampleIterator __output,
Eric Fiselier917af0a2016-08-28 22:14:37 +00003188 _Distance __n,
3189 _UniformRandomNumberGenerator & __g,
3190 input_iterator_tag) {
3191
3192 _Distance __k = 0;
3193 for (; __first != __last && __k < __n; ++__first, (void)++__k)
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003194 __output[__k] = *__first;
Eric Fiselier917af0a2016-08-28 22:14:37 +00003195 _Distance __sz = __k;
3196 for (; __first != __last; ++__first, (void)++__k) {
3197 _Distance __r = _VSTD::uniform_int_distribution<_Distance>(0, __k)(__g);
3198 if (__r < __sz)
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003199 __output[__r] = *__first;
Eric Fiselier917af0a2016-08-28 22:14:37 +00003200 }
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003201 return __output + _VSTD::min(__n, __k);
Eric Fiselier917af0a2016-08-28 22:14:37 +00003202}
3203
3204template <class _PopulationIterator, class _SampleIterator, class _Distance,
3205 class _UniformRandomNumberGenerator>
3206_LIBCPP_INLINE_VISIBILITY
3207_SampleIterator __sample(_PopulationIterator __first,
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003208 _PopulationIterator __last, _SampleIterator __output,
Eric Fiselier917af0a2016-08-28 22:14:37 +00003209 _Distance __n,
3210 _UniformRandomNumberGenerator& __g,
3211 forward_iterator_tag) {
3212 _Distance __unsampled_sz = _VSTD::distance(__first, __last);
3213 for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
3214 _Distance __r =
3215 _VSTD::uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
3216 if (__r < __n) {
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003217 *__output++ = *__first;
Eric Fiselier917af0a2016-08-28 22:14:37 +00003218 --__n;
3219 }
3220 }
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003221 return __output;
Eric Fiselier917af0a2016-08-28 22:14:37 +00003222}
3223
3224template <class _PopulationIterator, class _SampleIterator, class _Distance,
3225 class _UniformRandomNumberGenerator>
3226_LIBCPP_INLINE_VISIBILITY
3227_SampleIterator __sample(_PopulationIterator __first,
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003228 _PopulationIterator __last, _SampleIterator __output,
Eric Fiselier917af0a2016-08-28 22:14:37 +00003229 _Distance __n, _UniformRandomNumberGenerator& __g) {
3230 typedef typename iterator_traits<_PopulationIterator>::iterator_category
3231 _PopCategory;
3232 typedef typename iterator_traits<_PopulationIterator>::difference_type
3233 _Difference;
3234 static_assert(__is_forward_iterator<_PopulationIterator>::value ||
3235 __is_random_access_iterator<_SampleIterator>::value,
3236 "SampleIterator must meet the requirements of RandomAccessIterator");
3237 typedef typename common_type<_Distance, _Difference>::type _CommonType;
3238 _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
3239 return _VSTD::__sample(
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003240 __first, __last, __output, _CommonType(__n),
Eric Fiselier917af0a2016-08-28 22:14:37 +00003241 __g, _PopCategory());
3242}
3243
3244#if _LIBCPP_STD_VER > 14
3245template <class _PopulationIterator, class _SampleIterator, class _Distance,
3246 class _UniformRandomNumberGenerator>
3247inline _LIBCPP_INLINE_VISIBILITY
3248_SampleIterator sample(_PopulationIterator __first,
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003249 _PopulationIterator __last, _SampleIterator __output,
Eric Fiselier917af0a2016-08-28 22:14:37 +00003250 _Distance __n, _UniformRandomNumberGenerator&& __g) {
Eric Fiselierc60e8fc2017-01-07 11:27:06 +00003251 return _VSTD::__sample(__first, __last, __output, __n, __g);
Eric Fiselier917af0a2016-08-28 22:14:37 +00003252}
3253#endif // _LIBCPP_STD_VER > 14
3254
Howard Hinnantc3267212010-05-26 17:49:34 +00003255template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3256 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselier6b17a7b2017-04-18 23:26:47 +00003257#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant278bf2d2010-11-18 01:47:02 +00003258 _UniformRandomNumberGenerator&& __g)
3259#else
Howard Hinnantc3267212010-05-26 17:49:34 +00003260 _UniformRandomNumberGenerator& __g)
Howard Hinnant278bf2d2010-11-18 01:47:02 +00003261#endif
Howard Hinnantc3267212010-05-26 17:49:34 +00003262{
3263 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnant99968442011-11-29 18:15:50 +00003264 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3265 typedef typename _Dp::param_type _Pp;
Howard Hinnantc3267212010-05-26 17:49:34 +00003266 difference_type __d = __last - __first;
3267 if (__d > 1)
3268 {
Howard Hinnant99968442011-11-29 18:15:50 +00003269 _Dp __uid;
Howard Hinnantc3267212010-05-26 17:49:34 +00003270 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003271 {
Howard Hinnant99968442011-11-29 18:15:50 +00003272 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant4e599482010-10-22 15:26:39 +00003273 if (__i != difference_type(0))
3274 swap(*__first, *(__first + __i));
3275 }
Howard Hinnantc3267212010-05-26 17:49:34 +00003276 }
3277}
3278
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003279template <class _InputIterator, class _Predicate>
3280bool
3281is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3282{
3283 for (; __first != __last; ++__first)
3284 if (!__pred(*__first))
3285 break;
Marshall Clowa0ec4b72015-02-02 18:16:35 +00003286 if ( __first == __last )
3287 return true;
3288 ++__first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003289 for (; __first != __last; ++__first)
3290 if (__pred(*__first))
3291 return false;
3292 return true;
3293}
3294
3295// partition
3296
3297template <class _Predicate, class _ForwardIterator>
3298_ForwardIterator
3299__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3300{
3301 while (true)
3302 {
3303 if (__first == __last)
3304 return __first;
3305 if (!__pred(*__first))
3306 break;
3307 ++__first;
3308 }
3309 for (_ForwardIterator __p = __first; ++__p != __last;)
3310 {
3311 if (__pred(*__p))
3312 {
3313 swap(*__first, *__p);
3314 ++__first;
3315 }
3316 }
3317 return __first;
3318}
3319
3320template <class _Predicate, class _BidirectionalIterator>
3321_BidirectionalIterator
3322__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3323 bidirectional_iterator_tag)
3324{
3325 while (true)
3326 {
3327 while (true)
3328 {
3329 if (__first == __last)
3330 return __first;
3331 if (!__pred(*__first))
3332 break;
3333 ++__first;
3334 }
3335 do
3336 {
3337 if (__first == --__last)
3338 return __first;
3339 } while (!__pred(*__last));
3340 swap(*__first, *__last);
3341 ++__first;
3342 }
3343}
3344
3345template <class _ForwardIterator, class _Predicate>
3346inline _LIBCPP_INLINE_VISIBILITY
3347_ForwardIterator
3348partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3349{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003350 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003351 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3352}
3353
3354// partition_copy
3355
3356template <class _InputIterator, class _OutputIterator1,
3357 class _OutputIterator2, class _Predicate>
3358pair<_OutputIterator1, _OutputIterator2>
3359partition_copy(_InputIterator __first, _InputIterator __last,
3360 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3361 _Predicate __pred)
3362{
3363 for (; __first != __last; ++__first)
3364 {
3365 if (__pred(*__first))
3366 {
3367 *__out_true = *__first;
3368 ++__out_true;
3369 }
3370 else
3371 {
3372 *__out_false = *__first;
3373 ++__out_false;
3374 }
3375 }
3376 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3377}
3378
3379// partition_point
3380
3381template<class _ForwardIterator, class _Predicate>
3382_ForwardIterator
3383partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3384{
3385 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003386 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003387 while (__len != 0)
3388 {
3389 difference_type __l2 = __len / 2;
3390 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003391 _VSTD::advance(__m, __l2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003392 if (__pred(*__m))
3393 {
3394 __first = ++__m;
3395 __len -= __l2 + 1;
3396 }
3397 else
3398 __len = __l2;
3399 }
3400 return __first;
3401}
3402
3403// stable_partition
3404
3405template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3406_ForwardIterator
3407__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3408 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3409{
3410 // *__first is known to be false
3411 // __len >= 1
3412 if (__len == 1)
3413 return __first;
3414 if (__len == 2)
3415 {
3416 _ForwardIterator __m = __first;
3417 if (__pred(*++__m))
3418 {
3419 swap(*__first, *__m);
3420 return __m;
3421 }
3422 return __first;
3423 }
3424 if (__len <= __p.second)
3425 { // The buffer is big enough to use
3426 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3427 __destruct_n __d(0);
3428 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3429 // Move the falses into the temporary buffer, and the trues to the front of the line
3430 // Update __first to always point to the end of the trues
3431 value_type* __t = __p.first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003432 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003433 __d.__incr((value_type*)0);
3434 ++__t;
3435 _ForwardIterator __i = __first;
3436 while (++__i != __last)
3437 {
3438 if (__pred(*__i))
3439 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003440 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003441 ++__first;
3442 }
3443 else
3444 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003445 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003446 __d.__incr((value_type*)0);
3447 ++__t;
3448 }
3449 }
3450 // All trues now at start of range, all falses in buffer
3451 // Move falses back into range, but don't mess up __first which points to first false
3452 __i = __first;
3453 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003454 *__i = _VSTD::move(*__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003455 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3456 return __first;
3457 }
3458 // Else not enough buffer, do in place
3459 // __len >= 3
3460 _ForwardIterator __m = __first;
3461 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003462 _VSTD::advance(__m, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003463 // recurse on [__first, __m), *__first know to be false
3464 // F?????????????????
3465 // f m l
3466 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3467 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3468 // TTTFFFFF??????????
3469 // f ff m l
3470 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3471 _ForwardIterator __m1 = __m;
3472 _ForwardIterator __second_false = __last;
3473 _Distance __len_half = __len - __len2;
3474 while (__pred(*__m1))
3475 {
3476 if (++__m1 == __last)
3477 goto __second_half_done;
3478 --__len_half;
3479 }
3480 // TTTFFFFFTTTF??????
3481 // f ff m m1 l
3482 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3483__second_half_done:
3484 // TTTFFFFFTTTTTFFFFF
3485 // f ff m sf l
Howard Hinnant0949eed2011-06-30 21:18:19 +00003486 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003487 // TTTTTTTTFFFFFFFFFF
3488 // |
3489}
3490
3491struct __return_temporary_buffer
3492{
3493 template <class _Tp>
Howard Hinnant0949eed2011-06-30 21:18:19 +00003494 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003495};
3496
3497template <class _Predicate, class _ForwardIterator>
3498_ForwardIterator
3499__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3500 forward_iterator_tag)
3501{
3502 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3503 // Either prove all true and return __first or point to first false
3504 while (true)
3505 {
3506 if (__first == __last)
3507 return __first;
3508 if (!__pred(*__first))
3509 break;
3510 ++__first;
3511 }
3512 // We now have a reduced range [__first, __last)
3513 // *__first is known to be false
3514 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3515 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003516 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003517 pair<value_type*, ptrdiff_t> __p(0, 0);
3518 unique_ptr<value_type, __return_temporary_buffer> __h;
3519 if (__len >= __alloc_limit)
3520 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003521 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003522 __h.reset(__p.first);
3523 }
3524 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3525 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3526}
3527
3528template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3529_BidirectionalIterator
3530__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3531 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3532{
3533 // *__first is known to be false
3534 // *__last is known to be true
3535 // __len >= 2
3536 if (__len == 2)
3537 {
3538 swap(*__first, *__last);
3539 return __last;
3540 }
3541 if (__len == 3)
3542 {
3543 _BidirectionalIterator __m = __first;
3544 if (__pred(*++__m))
3545 {
3546 swap(*__first, *__m);
3547 swap(*__m, *__last);
3548 return __last;
3549 }
3550 swap(*__m, *__last);
3551 swap(*__first, *__m);
3552 return __m;
3553 }
3554 if (__len <= __p.second)
3555 { // The buffer is big enough to use
3556 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3557 __destruct_n __d(0);
3558 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3559 // Move the falses into the temporary buffer, and the trues to the front of the line
3560 // Update __first to always point to the end of the trues
3561 value_type* __t = __p.first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003562 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003563 __d.__incr((value_type*)0);
3564 ++__t;
3565 _BidirectionalIterator __i = __first;
3566 while (++__i != __last)
3567 {
3568 if (__pred(*__i))
3569 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003570 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003571 ++__first;
3572 }
3573 else
3574 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003575 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003576 __d.__incr((value_type*)0);
3577 ++__t;
3578 }
3579 }
3580 // move *__last, known to be true
Howard Hinnant0949eed2011-06-30 21:18:19 +00003581 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003582 __i = ++__first;
3583 // All trues now at start of range, all falses in buffer
3584 // Move falses back into range, but don't mess up __first which points to first false
3585 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003586 *__i = _VSTD::move(*__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003587 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3588 return __first;
3589 }
3590 // Else not enough buffer, do in place
3591 // __len >= 4
3592 _BidirectionalIterator __m = __first;
3593 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003594 _VSTD::advance(__m, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003595 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3596 // F????????????????T
3597 // f m l
3598 _BidirectionalIterator __m1 = __m;
3599 _BidirectionalIterator __first_false = __first;
3600 _Distance __len_half = __len2;
3601 while (!__pred(*--__m1))
3602 {
3603 if (__m1 == __first)
3604 goto __first_half_done;
3605 --__len_half;
3606 }
3607 // F???TFFF?????????T
3608 // f m1 m l
3609 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3610 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3611__first_half_done:
3612 // TTTFFFFF?????????T
3613 // f ff m l
3614 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3615 __m1 = __m;
3616 _BidirectionalIterator __second_false = __last;
3617 ++__second_false;
3618 __len_half = __len - __len2;
3619 while (__pred(*__m1))
3620 {
3621 if (++__m1 == __last)
3622 goto __second_half_done;
3623 --__len_half;
3624 }
3625 // TTTFFFFFTTTF?????T
3626 // f ff m m1 l
3627 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3628__second_half_done:
3629 // TTTFFFFFTTTTTFFFFF
3630 // f ff m sf l
Howard Hinnant0949eed2011-06-30 21:18:19 +00003631 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003632 // TTTTTTTTFFFFFFFFFF
3633 // |
3634}
3635
3636template <class _Predicate, class _BidirectionalIterator>
3637_BidirectionalIterator
3638__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3639 bidirectional_iterator_tag)
3640{
3641 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3642 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3643 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3644 // Either prove all true and return __first or point to first false
3645 while (true)
3646 {
3647 if (__first == __last)
3648 return __first;
3649 if (!__pred(*__first))
3650 break;
3651 ++__first;
3652 }
3653 // __first points to first false, everything prior to __first is already set.
3654 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3655 do
3656 {
3657 if (__first == --__last)
3658 return __first;
3659 } while (!__pred(*__last));
3660 // We now have a reduced range [__first, __last]
3661 // *__first is known to be false
3662 // *__last is known to be true
3663 // __len >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003664 difference_type __len = _VSTD::distance(__first, __last) + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003665 pair<value_type*, ptrdiff_t> __p(0, 0);
3666 unique_ptr<value_type, __return_temporary_buffer> __h;
3667 if (__len >= __alloc_limit)
3668 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003669 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003670 __h.reset(__p.first);
3671 }
3672 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3673 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3674}
3675
3676template <class _ForwardIterator, class _Predicate>
3677inline _LIBCPP_INLINE_VISIBILITY
3678_ForwardIterator
3679stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3680{
3681 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3682 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3683}
3684
3685// is_sorted_until
3686
3687template <class _ForwardIterator, class _Compare>
3688_ForwardIterator
3689is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3690{
3691 if (__first != __last)
3692 {
3693 _ForwardIterator __i = __first;
3694 while (++__i != __last)
3695 {
3696 if (__comp(*__i, *__first))
3697 return __i;
3698 __first = __i;
3699 }
3700 }
3701 return __last;
3702}
3703
Howard Hinnant324bb032010-08-22 00:02:43 +00003704template<class _ForwardIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003705inline _LIBCPP_INLINE_VISIBILITY
3706_ForwardIterator
3707is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3708{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003709 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003710}
3711
3712// is_sorted
3713
3714template <class _ForwardIterator, class _Compare>
3715inline _LIBCPP_INLINE_VISIBILITY
3716bool
3717is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3718{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003719 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003720}
3721
Howard Hinnant324bb032010-08-22 00:02:43 +00003722template<class _ForwardIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003723inline _LIBCPP_INLINE_VISIBILITY
3724bool
3725is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3726{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003727 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003728}
3729
3730// sort
3731
3732// stable, 2-3 compares, 0-2 swaps
3733
3734template <class _Compare, class _ForwardIterator>
3735unsigned
3736__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3737{
3738 unsigned __r = 0;
3739 if (!__c(*__y, *__x)) // if x <= y
3740 {
3741 if (!__c(*__z, *__y)) // if y <= z
3742 return __r; // x <= y && y <= z
3743 // x <= y && y > z
3744 swap(*__y, *__z); // x <= z && y < z
3745 __r = 1;
3746 if (__c(*__y, *__x)) // if x > y
3747 {
3748 swap(*__x, *__y); // x < y && y <= z
3749 __r = 2;
3750 }
3751 return __r; // x <= y && y < z
3752 }
3753 if (__c(*__z, *__y)) // x > y, if y > z
3754 {
3755 swap(*__x, *__z); // x < y && y < z
3756 __r = 1;
3757 return __r;
3758 }
3759 swap(*__x, *__y); // x > y && y <= z
3760 __r = 1; // x < y && x <= z
3761 if (__c(*__z, *__y)) // if y > z
3762 {
3763 swap(*__y, *__z); // x <= y && y < z
3764 __r = 2;
3765 }
3766 return __r;
3767} // x <= y && y <= z
3768
3769// stable, 3-6 compares, 0-5 swaps
3770
3771template <class _Compare, class _ForwardIterator>
3772unsigned
3773__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3774 _ForwardIterator __x4, _Compare __c)
3775{
3776 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3777 if (__c(*__x4, *__x3))
3778 {
3779 swap(*__x3, *__x4);
3780 ++__r;
3781 if (__c(*__x3, *__x2))
3782 {
3783 swap(*__x2, *__x3);
3784 ++__r;
3785 if (__c(*__x2, *__x1))
3786 {
3787 swap(*__x1, *__x2);
3788 ++__r;
3789 }
3790 }
3791 }
3792 return __r;
3793}
3794
3795// stable, 4-10 compares, 0-9 swaps
3796
3797template <class _Compare, class _ForwardIterator>
3798unsigned
3799__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3800 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3801{
3802 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3803 if (__c(*__x5, *__x4))
3804 {
3805 swap(*__x4, *__x5);
3806 ++__r;
3807 if (__c(*__x4, *__x3))
3808 {
3809 swap(*__x3, *__x4);
3810 ++__r;
3811 if (__c(*__x3, *__x2))
3812 {
3813 swap(*__x2, *__x3);
3814 ++__r;
3815 if (__c(*__x2, *__x1))
3816 {
3817 swap(*__x1, *__x2);
3818 ++__r;
3819 }
3820 }
3821 }
3822 }
3823 return __r;
3824}
3825
3826// Assumes size > 0
3827template <class _Compare, class _BirdirectionalIterator>
3828void
3829__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3830{
3831 _BirdirectionalIterator __lm1 = __last;
3832 for (--__lm1; __first != __lm1; ++__first)
3833 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003834 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003835 typename add_lvalue_reference<_Compare>::type>
3836 (__first, __last, __comp);
3837 if (__i != __first)
3838 swap(*__first, *__i);
3839 }
3840}
3841
3842template <class _Compare, class _BirdirectionalIterator>
3843void
3844__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3845{
3846 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3847 if (__first != __last)
3848 {
3849 _BirdirectionalIterator __i = __first;
3850 for (++__i; __i != __last; ++__i)
3851 {
3852 _BirdirectionalIterator __j = __i;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003853 value_type __t(_VSTD::move(*__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003854 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003855 *__j = _VSTD::move(*__k);
3856 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003857 }
3858 }
3859}
3860
3861template <class _Compare, class _RandomAccessIterator>
3862void
3863__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3864{
3865 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3866 _RandomAccessIterator __j = __first+2;
3867 __sort3<_Compare>(__first, __first+1, __j, __comp);
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 }
3882 __j = __i;
3883 }
3884}
3885
3886template <class _Compare, class _RandomAccessIterator>
3887bool
3888__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3889{
3890 switch (__last - __first)
3891 {
3892 case 0:
3893 case 1:
3894 return true;
3895 case 2:
3896 if (__comp(*--__last, *__first))
3897 swap(*__first, *__last);
3898 return true;
3899 case 3:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003900 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003901 return true;
3902 case 4:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003903 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003904 return true;
3905 case 5:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003906 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003907 return true;
3908 }
3909 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3910 _RandomAccessIterator __j = __first+2;
3911 __sort3<_Compare>(__first, __first+1, __j, __comp);
3912 const unsigned __limit = 8;
3913 unsigned __count = 0;
3914 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3915 {
3916 if (__comp(*__i, *__j))
3917 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003918 value_type __t(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003919 _RandomAccessIterator __k = __j;
3920 __j = __i;
3921 do
3922 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003923 *__j = _VSTD::move(*__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003924 __j = __k;
3925 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003926 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003927 if (++__count == __limit)
3928 return ++__i == __last;
3929 }
3930 __j = __i;
3931 }
3932 return true;
3933}
3934
3935template <class _Compare, class _BirdirectionalIterator>
3936void
3937__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3938 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3939{
3940 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3941 if (__first1 != __last1)
3942 {
3943 __destruct_n __d(0);
3944 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3945 value_type* __last2 = __first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003946 ::new(__last2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003947 __d.__incr((value_type*)0);
3948 for (++__last2; ++__first1 != __last1; ++__last2)
3949 {
3950 value_type* __j2 = __last2;
3951 value_type* __i2 = __j2;
3952 if (__comp(*__first1, *--__i2))
3953 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003954 ::new(__j2) value_type(_VSTD::move(*__i2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003955 __d.__incr((value_type*)0);
3956 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003957 *__j2 = _VSTD::move(*__i2);
3958 *__j2 = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003959 }
3960 else
3961 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003962 ::new(__j2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003963 __d.__incr((value_type*)0);
3964 }
3965 }
3966 __h.release();
3967 }
3968}
3969
3970template <class _Compare, class _RandomAccessIterator>
3971void
3972__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3973{
3974 // _Compare is known to be a reference type
3975 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3976 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant1468b662010-11-19 22:17:28 +00003977 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3978 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003979 while (true)
3980 {
3981 __restart:
3982 difference_type __len = __last - __first;
3983 switch (__len)
3984 {
3985 case 0:
3986 case 1:
3987 return;
3988 case 2:
3989 if (__comp(*--__last, *__first))
3990 swap(*__first, *__last);
3991 return;
3992 case 3:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003993 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003994 return;
3995 case 4:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003996 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003997 return;
3998 case 5:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003999 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004000 return;
4001 }
4002 if (__len <= __limit)
4003 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004004 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004005 return;
4006 }
4007 // __len > 5
4008 _RandomAccessIterator __m = __first;
4009 _RandomAccessIterator __lm1 = __last;
4010 --__lm1;
4011 unsigned __n_swaps;
4012 {
4013 difference_type __delta;
4014 if (__len >= 1000)
4015 {
4016 __delta = __len/2;
4017 __m += __delta;
4018 __delta /= 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004019 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004020 }
4021 else
4022 {
4023 __delta = __len/2;
4024 __m += __delta;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004025 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004026 }
4027 }
4028 // *__m is median
4029 // partition [__first, __m) < *__m and *__m <= [__m, __last)
4030 // (this inhibits tossing elements equivalent to __m around unnecessarily)
4031 _RandomAccessIterator __i = __first;
4032 _RandomAccessIterator __j = __lm1;
4033 // j points beyond range to be tested, *__m is known to be <= *__lm1
4034 // The search going up is known to be guarded but the search coming down isn't.
4035 // Prime the downward search with a guard.
4036 if (!__comp(*__i, *__m)) // if *__first == *__m
4037 {
4038 // *__first == *__m, *__first doesn't go in first part
4039 // manually guard downward moving __j against __i
4040 while (true)
4041 {
4042 if (__i == --__j)
4043 {
4044 // *__first == *__m, *__m <= all other elements
4045 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
4046 ++__i; // __first + 1
4047 __j = __last;
4048 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
4049 {
4050 while (true)
4051 {
4052 if (__i == __j)
4053 return; // [__first, __last) all equivalent elements
4054 if (__comp(*__first, *__i))
4055 {
4056 swap(*__i, *__j);
4057 ++__n_swaps;
4058 ++__i;
4059 break;
4060 }
4061 ++__i;
4062 }
4063 }
4064 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
4065 if (__i == __j)
4066 return;
4067 while (true)
4068 {
4069 while (!__comp(*__first, *__i))
4070 ++__i;
4071 while (__comp(*__first, *--__j))
4072 ;
4073 if (__i >= __j)
4074 break;
4075 swap(*__i, *__j);
4076 ++__n_swaps;
4077 ++__i;
4078 }
4079 // [__first, __i) == *__first and *__first < [__i, __last)
4080 // The first part is sorted, sort the secod part
Howard Hinnant0949eed2011-06-30 21:18:19 +00004081 // _VSTD::__sort<_Compare>(__i, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004082 __first = __i;
4083 goto __restart;
4084 }
4085 if (__comp(*__j, *__m))
4086 {
4087 swap(*__i, *__j);
4088 ++__n_swaps;
4089 break; // found guard for downward moving __j, now use unguarded partition
4090 }
4091 }
4092 }
4093 // It is known that *__i < *__m
4094 ++__i;
4095 // j points beyond range to be tested, *__m is known to be <= *__lm1
4096 // if not yet partitioned...
4097 if (__i < __j)
4098 {
4099 // known that *(__i - 1) < *__m
4100 // known that __i <= __m
4101 while (true)
4102 {
4103 // __m still guards upward moving __i
4104 while (__comp(*__i, *__m))
4105 ++__i;
4106 // It is now known that a guard exists for downward moving __j
4107 while (!__comp(*--__j, *__m))
4108 ;
4109 if (__i > __j)
4110 break;
4111 swap(*__i, *__j);
4112 ++__n_swaps;
4113 // It is known that __m != __j
4114 // If __m just moved, follow it
4115 if (__m == __i)
4116 __m = __j;
4117 ++__i;
4118 }
4119 }
4120 // [__first, __i) < *__m and *__m <= [__i, __last)
4121 if (__i != __m && __comp(*__m, *__i))
4122 {
4123 swap(*__i, *__m);
4124 ++__n_swaps;
4125 }
4126 // [__first, __i) < *__i and *__i <= [__i+1, __last)
4127 // If we were given a perfect partition, see if insertion sort is quick...
4128 if (__n_swaps == 0)
4129 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004130 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
4131 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004132 {
4133 if (__fs)
4134 return;
4135 __last = __i;
4136 continue;
4137 }
4138 else
4139 {
4140 if (__fs)
4141 {
4142 __first = ++__i;
4143 continue;
4144 }
4145 }
4146 }
4147 // sort smaller range with recursive call and larger with tail recursion elimination
4148 if (__i - __first < __last - __i)
4149 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004150 _VSTD::__sort<_Compare>(__first, __i, __comp);
4151 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004152 __first = ++__i;
4153 }
4154 else
4155 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004156 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4157 // _VSTD::__sort<_Compare>(__first, __i, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004158 __last = __i;
4159 }
4160 }
4161}
4162
4163// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
4164template <class _RandomAccessIterator, class _Compare>
4165inline _LIBCPP_INLINE_VISIBILITY
4166void
4167sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4168{
Howard Hinnant5e571422013-08-23 20:10:18 +00004169#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004170 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4171 __debug_less<_Compare> __c(__comp);
4172 __sort<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004173#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004174 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4175 __sort<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004176#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004177}
4178
4179template <class _RandomAccessIterator>
4180inline _LIBCPP_INLINE_VISIBILITY
4181void
4182sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4183{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004184 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004185}
4186
4187template <class _Tp>
4188inline _LIBCPP_INLINE_VISIBILITY
4189void
4190sort(_Tp** __first, _Tp** __last)
4191{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004192 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004193}
4194
4195template <class _Tp>
4196inline _LIBCPP_INLINE_VISIBILITY
4197void
4198sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4199{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004200 _VSTD::sort(__first.base(), __last.base());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004201}
4202
Howard Hinnant7a563db2011-09-14 18:33:51 +00004203template <class _Tp, class _Compare>
4204inline _LIBCPP_INLINE_VISIBILITY
4205void
4206sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4207{
4208 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4209 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4210}
4211
Howard Hinnante9df0a52013-08-01 18:17:34 +00004212#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +00004213#pragma warning( push )
4214#pragma warning( disable: 4231)
Howard Hinnante9df0a52013-08-01 18:17:34 +00004215#endif // _LIBCPP_MSVC
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004216_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4217_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4218_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4219_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4220_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4221_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4222_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4223_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4224_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4225_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4226_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4227_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>&))
4228_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4229_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4230_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 +00004231
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004232_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4233_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4234_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4235_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4236_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4237_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4238_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4239_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4240_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4241_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4242_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4243_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>&))
4244_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4245_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4246_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 +00004247
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004248_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&))
Howard Hinnante9df0a52013-08-01 18:17:34 +00004249#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +00004250#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34 +00004251#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004252
4253// lower_bound
4254
4255template <class _Compare, class _ForwardIterator, class _Tp>
4256_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004257__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004258{
4259 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004260 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004261 while (__len != 0)
4262 {
4263 difference_type __l2 = __len / 2;
4264 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004265 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004266 if (__comp(*__m, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004267 {
4268 __first = ++__m;
4269 __len -= __l2 + 1;
4270 }
4271 else
4272 __len = __l2;
4273 }
4274 return __first;
4275}
4276
4277template <class _ForwardIterator, class _Tp, class _Compare>
4278inline _LIBCPP_INLINE_VISIBILITY
4279_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004280lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004281{
Howard Hinnant5e571422013-08-23 20:10:18 +00004282#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004283 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4284 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004285 return __lower_bound<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004286#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004287 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004288 return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004289#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004290}
4291
4292template <class _ForwardIterator, class _Tp>
4293inline _LIBCPP_INLINE_VISIBILITY
4294_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004295lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004296{
Howard Hinnant78b68282011-10-22 20:59:45 +00004297 return _VSTD::lower_bound(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004298 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4299}
4300
4301// upper_bound
4302
4303template <class _Compare, class _ForwardIterator, class _Tp>
4304_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004305__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004306{
4307 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004308 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004309 while (__len != 0)
4310 {
4311 difference_type __l2 = __len / 2;
4312 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004313 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004314 if (__comp(__value_, *__m))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004315 __len = __l2;
4316 else
4317 {
4318 __first = ++__m;
4319 __len -= __l2 + 1;
4320 }
4321 }
4322 return __first;
4323}
4324
4325template <class _ForwardIterator, class _Tp, class _Compare>
4326inline _LIBCPP_INLINE_VISIBILITY
4327_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004328upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004329{
Howard Hinnant5e571422013-08-23 20:10:18 +00004330#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004331 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4332 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004333 return __upper_bound<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004334#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004335 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004336 return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004337#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004338}
4339
4340template <class _ForwardIterator, class _Tp>
4341inline _LIBCPP_INLINE_VISIBILITY
4342_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004343upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004344{
Howard Hinnant78b68282011-10-22 20:59:45 +00004345 return _VSTD::upper_bound(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004346 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4347}
4348
4349// equal_range
4350
4351template <class _Compare, class _ForwardIterator, class _Tp>
4352pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004353__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004354{
4355 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004356 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004357 while (__len != 0)
4358 {
4359 difference_type __l2 = __len / 2;
4360 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004361 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004362 if (__comp(*__m, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004363 {
4364 __first = ++__m;
4365 __len -= __l2 + 1;
4366 }
Howard Hinnant78b68282011-10-22 20:59:45 +00004367 else if (__comp(__value_, *__m))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004368 {
4369 __last = __m;
4370 __len = __l2;
4371 }
4372 else
4373 {
4374 _ForwardIterator __mp1 = __m;
4375 return pair<_ForwardIterator, _ForwardIterator>
4376 (
Howard Hinnant78b68282011-10-22 20:59:45 +00004377 __lower_bound<_Compare>(__first, __m, __value_, __comp),
4378 __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004379 );
4380 }
4381 }
4382 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4383}
4384
4385template <class _ForwardIterator, class _Tp, class _Compare>
4386inline _LIBCPP_INLINE_VISIBILITY
4387pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004388equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004389{
Howard Hinnant5e571422013-08-23 20:10:18 +00004390#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004391 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4392 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004393 return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004394#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004395 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004396 return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004397#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004398}
4399
4400template <class _ForwardIterator, class _Tp>
4401inline _LIBCPP_INLINE_VISIBILITY
4402pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004403equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004404{
Howard Hinnant78b68282011-10-22 20:59:45 +00004405 return _VSTD::equal_range(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004406 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4407}
4408
4409// binary_search
4410
4411template <class _Compare, class _ForwardIterator, class _Tp>
4412inline _LIBCPP_INLINE_VISIBILITY
4413bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004414__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004415{
Howard Hinnant78b68282011-10-22 20:59:45 +00004416 __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4417 return __first != __last && !__comp(__value_, *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004418}
4419
4420template <class _ForwardIterator, class _Tp, class _Compare>
4421inline _LIBCPP_INLINE_VISIBILITY
4422bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004423binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004424{
Howard Hinnant5e571422013-08-23 20:10:18 +00004425#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004426 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4427 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004428 return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004429#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004430 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004431 return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004432#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004433}
4434
4435template <class _ForwardIterator, class _Tp>
4436inline _LIBCPP_INLINE_VISIBILITY
4437bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004438binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004439{
Howard Hinnant78b68282011-10-22 20:59:45 +00004440 return _VSTD::binary_search(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004441 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4442}
4443
4444// merge
4445
4446template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4447_OutputIterator
4448__merge(_InputIterator1 __first1, _InputIterator1 __last1,
4449 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4450{
4451 for (; __first1 != __last1; ++__result)
4452 {
4453 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004454 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004455 if (__comp(*__first2, *__first1))
4456 {
4457 *__result = *__first2;
4458 ++__first2;
4459 }
4460 else
4461 {
4462 *__result = *__first1;
4463 ++__first1;
4464 }
4465 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00004466 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004467}
4468
4469template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4470inline _LIBCPP_INLINE_VISIBILITY
4471_OutputIterator
4472merge(_InputIterator1 __first1, _InputIterator1 __last1,
4473 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4474{
Howard Hinnant5e571422013-08-23 20:10:18 +00004475#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004476 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4477 __debug_less<_Compare> __c(__comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004478 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004479#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004480 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004481 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004482#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004483}
4484
4485template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4486inline _LIBCPP_INLINE_VISIBILITY
4487_OutputIterator
4488merge(_InputIterator1 __first1, _InputIterator1 __last1,
4489 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4490{
4491 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4492 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4493 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4494}
4495
4496// inplace_merge
4497
Marshall Clowa3795762015-07-29 16:25:45 +00004498template <class _Compare, class _InputIterator1, class _InputIterator2,
4499 class _OutputIterator>
4500void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4501 _InputIterator2 __first2, _InputIterator2 __last2,
4502 _OutputIterator __result, _Compare __comp)
4503{
4504 for (; __first1 != __last1; ++__result)
4505 {
4506 if (__first2 == __last2)
4507 {
4508 _VSTD::move(__first1, __last1, __result);
4509 return;
4510 }
4511
4512 if (__comp(*__first2, *__first1))
4513 {
4514 *__result = _VSTD::move(*__first2);
4515 ++__first2;
4516 }
4517 else
4518 {
4519 *__result = _VSTD::move(*__first1);
4520 ++__first1;
4521 }
4522 }
4523 // __first2 through __last2 are already in the right spot.
4524}
4525
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004526template <class _Compare, class _BidirectionalIterator>
4527void
4528__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4529 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4530 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4531 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4532{
4533 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004534 __destruct_n __d(0);
4535 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4536 if (__len1 <= __len2)
4537 {
4538 value_type* __p = __buff;
Eric Fiselierb9919752014-10-27 19:28:20 +00004539 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004540 ::new(__p) value_type(_VSTD::move(*__i));
Marshall Clowa3795762015-07-29 16:25:45 +00004541 __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004542 }
4543 else
4544 {
4545 value_type* __p = __buff;
Eric Fiselierb9919752014-10-27 19:28:20 +00004546 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004547 ::new(__p) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004548 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4549 typedef reverse_iterator<value_type*> _Rv;
Aditya Kumarfdb4f172016-08-25 11:52:38 +00004550 __half_inplace_merge(_Rv(__p), _Rv(__buff),
Marshall Clowa3795762015-07-29 16:25:45 +00004551 _RBi(__middle), _RBi(__first),
4552 _RBi(__last), __negate<_Compare>(__comp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004553 }
4554}
4555
4556template <class _Compare, class _BidirectionalIterator>
4557void
4558__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4559 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4560 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4561 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4562{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004563 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4564 while (true)
4565 {
4566 // if __middle == __last, we're done
4567 if (__len2 == 0)
4568 return;
Marshall Clowe809f4c2015-02-02 16:44:11 +00004569 if (__len1 <= __buff_size || __len2 <= __buff_size)
4570 return __buffered_inplace_merge<_Compare>
4571 (__first, __middle, __last, __comp, __len1, __len2, __buff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004572 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
Eric Fiselierb9919752014-10-27 19:28:20 +00004573 for (; true; ++__first, (void) --__len1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004574 {
4575 if (__len1 == 0)
4576 return;
4577 if (__comp(*__middle, *__first))
4578 break;
4579 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004580 // __first < __middle < __last
4581 // *__first > *__middle
4582 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4583 // all elements in:
4584 // [__first, __m1) <= [__middle, __m2)
4585 // [__middle, __m2) < [__m1, __middle)
4586 // [__m1, __middle) <= [__m2, __last)
4587 // and __m1 or __m2 is in the middle of its range
4588 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4589 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4590 difference_type __len11; // distance(__first, __m1)
4591 difference_type __len21; // distance(__middle, __m2)
4592 // binary search smaller range
4593 if (__len1 < __len2)
4594 { // __len >= 1, __len2 >= 2
4595 __len21 = __len2 / 2;
4596 __m2 = __middle;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004597 _VSTD::advance(__m2, __len21);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004598 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004599 __len11 = _VSTD::distance(__first, __m1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004600 }
4601 else
4602 {
4603 if (__len1 == 1)
4604 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4605 // It is known *__first > *__middle
4606 swap(*__first, *__middle);
4607 return;
4608 }
4609 // __len1 >= 2, __len2 >= 1
4610 __len11 = __len1 / 2;
4611 __m1 = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004612 _VSTD::advance(__m1, __len11);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004613 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004614 __len21 = _VSTD::distance(__middle, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004615 }
4616 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4617 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4618 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4619 // swap middle two partitions
Howard Hinnant0949eed2011-06-30 21:18:19 +00004620 __middle = _VSTD::rotate(__m1, __middle, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004621 // __len12 and __len21 now have swapped meanings
4622 // merge smaller range with recurisve call and larger with tail recursion elimination
4623 if (__len11 + __len21 < __len12 + __len22)
4624 {
4625 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4626// __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4627 __first = __middle;
4628 __middle = __m2;
4629 __len1 = __len12;
4630 __len2 = __len22;
4631 }
4632 else
4633 {
4634 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4635// __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4636 __last = __middle;
4637 __middle = __m1;
4638 __len1 = __len11;
4639 __len2 = __len21;
4640 }
4641 }
4642}
4643
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004644template <class _BidirectionalIterator, class _Compare>
4645inline _LIBCPP_INLINE_VISIBILITY
4646void
4647inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4648 _Compare __comp)
4649{
4650 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4651 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004652 difference_type __len1 = _VSTD::distance(__first, __middle);
4653 difference_type __len2 = _VSTD::distance(__middle, __last);
4654 difference_type __buf_size = _VSTD::min(__len1, __len2);
Marshall Clow4c2684c2015-02-02 17:35:53 +00004655 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4656 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
4657
Howard Hinnant5e571422013-08-23 20:10:18 +00004658#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004659 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4660 __debug_less<_Compare> __c(__comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004661 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004662 __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004663#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004664 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004665 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004666 __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004667#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004668}
4669
4670template <class _BidirectionalIterator>
4671inline _LIBCPP_INLINE_VISIBILITY
4672void
4673inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4674{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004675 _VSTD::inplace_merge(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004676 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4677}
4678
4679// stable_sort
4680
4681template <class _Compare, class _InputIterator1, class _InputIterator2>
4682void
4683__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4684 _InputIterator2 __first2, _InputIterator2 __last2,
4685 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4686{
4687 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4688 __destruct_n __d(0);
4689 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4690 for (; true; ++__result)
4691 {
4692 if (__first1 == __last1)
4693 {
4694 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
Howard Hinnant0949eed2011-06-30 21:18:19 +00004695 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004696 __h.release();
4697 return;
4698 }
4699 if (__first2 == __last2)
4700 {
4701 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
Howard Hinnant0949eed2011-06-30 21:18:19 +00004702 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004703 __h.release();
4704 return;
4705 }
4706 if (__comp(*__first2, *__first1))
4707 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004708 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004709 __d.__incr((value_type*)0);
4710 ++__first2;
4711 }
4712 else
4713 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004714 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004715 __d.__incr((value_type*)0);
4716 ++__first1;
4717 }
4718 }
4719}
4720
4721template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4722void
4723__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4724 _InputIterator2 __first2, _InputIterator2 __last2,
4725 _OutputIterator __result, _Compare __comp)
4726{
4727 for (; __first1 != __last1; ++__result)
4728 {
4729 if (__first2 == __last2)
4730 {
4731 for (; __first1 != __last1; ++__first1, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004732 *__result = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004733 return;
4734 }
4735 if (__comp(*__first2, *__first1))
4736 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004737 *__result = _VSTD::move(*__first2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004738 ++__first2;
4739 }
4740 else
4741 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004742 *__result = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004743 ++__first1;
4744 }
4745 }
4746 for (; __first2 != __last2; ++__first2, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004747 *__result = _VSTD::move(*__first2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004748}
4749
4750template <class _Compare, class _RandomAccessIterator>
4751void
4752__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4753 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4754 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4755
4756template <class _Compare, class _RandomAccessIterator>
4757void
4758__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4759 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4760 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4761{
4762 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4763 switch (__len)
4764 {
4765 case 0:
4766 return;
4767 case 1:
Howard Hinnant0949eed2011-06-30 21:18:19 +00004768 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004769 return;
4770 case 2:
4771 __destruct_n __d(0);
4772 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4773 if (__comp(*--__last1, *__first1))
4774 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004775 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004776 __d.__incr((value_type*)0);
4777 ++__first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004778 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004779 }
4780 else
4781 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004782 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004783 __d.__incr((value_type*)0);
4784 ++__first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004785 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004786 }
4787 __h2.release();
4788 return;
4789 }
4790 if (__len <= 8)
4791 {
4792 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4793 return;
4794 }
4795 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4796 _RandomAccessIterator __m = __first1 + __l2;
4797 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4798 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4799 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4800}
4801
4802template <class _Tp>
4803struct __stable_sort_switch
4804{
Howard Hinnant1468b662010-11-19 22:17:28 +00004805 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004806};
4807
4808template <class _Compare, class _RandomAccessIterator>
4809void
4810__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4811 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4812 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4813{
4814 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4815 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4816 switch (__len)
4817 {
4818 case 0:
4819 case 1:
4820 return;
4821 case 2:
4822 if (__comp(*--__last, *__first))
4823 swap(*__first, *__last);
4824 return;
4825 }
4826 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4827 {
4828 __insertion_sort<_Compare>(__first, __last, __comp);
4829 return;
4830 }
4831 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4832 _RandomAccessIterator __m = __first + __l2;
4833 if (__len <= __buff_size)
4834 {
4835 __destruct_n __d(0);
4836 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4837 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4838 __d.__set(__l2, (value_type*)0);
4839 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4840 __d.__set(__len, (value_type*)0);
4841 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4842// __merge<_Compare>(move_iterator<value_type*>(__buff),
4843// move_iterator<value_type*>(__buff + __l2),
4844// move_iterator<_RandomAccessIterator>(__buff + __l2),
4845// move_iterator<_RandomAccessIterator>(__buff + __len),
4846// __first, __comp);
4847 return;
4848 }
4849 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4850 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4851 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4852}
4853
4854template <class _RandomAccessIterator, class _Compare>
4855inline _LIBCPP_INLINE_VISIBILITY
4856void
4857stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4858{
4859 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4860 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4861 difference_type __len = __last - __first;
4862 pair<value_type*, ptrdiff_t> __buf(0, 0);
4863 unique_ptr<value_type, __return_temporary_buffer> __h;
4864 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4865 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004866 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004867 __h.reset(__buf.first);
4868 }
Howard Hinnant5e571422013-08-23 20:10:18 +00004869#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004870 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4871 __debug_less<_Compare> __c(__comp);
4872 __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004873#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004874 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4875 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004876#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004877}
4878
4879template <class _RandomAccessIterator>
4880inline _LIBCPP_INLINE_VISIBILITY
4881void
4882stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4883{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004884 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004885}
4886
4887// is_heap_until
4888
4889template <class _RandomAccessIterator, class _Compare>
4890_RandomAccessIterator
4891is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4892{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004893 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004894 difference_type __len = __last - __first;
4895 difference_type __p = 0;
4896 difference_type __c = 1;
4897 _RandomAccessIterator __pp = __first;
4898 while (__c < __len)
4899 {
4900 _RandomAccessIterator __cp = __first + __c;
4901 if (__comp(*__pp, *__cp))
4902 return __cp;
4903 ++__c;
4904 ++__cp;
4905 if (__c == __len)
4906 return __last;
4907 if (__comp(*__pp, *__cp))
4908 return __cp;
4909 ++__p;
4910 ++__pp;
4911 __c = 2 * __p + 1;
4912 }
4913 return __last;
4914}
4915
Howard Hinnant324bb032010-08-22 00:02:43 +00004916template<class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004917inline _LIBCPP_INLINE_VISIBILITY
4918_RandomAccessIterator
4919is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4920{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004921 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004922}
4923
4924// is_heap
4925
4926template <class _RandomAccessIterator, class _Compare>
4927inline _LIBCPP_INLINE_VISIBILITY
4928bool
4929is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4930{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004931 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004932}
4933
Howard Hinnant324bb032010-08-22 00:02:43 +00004934template<class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004935inline _LIBCPP_INLINE_VISIBILITY
4936bool
4937is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4938{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004939 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004940}
4941
4942// push_heap
4943
4944template <class _Compare, class _RandomAccessIterator>
4945void
David Majnemercb8757a2014-07-22 06:07:09 +00004946__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4947 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004948{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004949 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4950 if (__len > 1)
4951 {
4952 __len = (__len - 2) / 2;
4953 _RandomAccessIterator __ptr = __first + __len;
4954 if (__comp(*__ptr, *--__last))
4955 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004956 value_type __t(_VSTD::move(*__last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004957 do
4958 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004959 *__last = _VSTD::move(*__ptr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004960 __last = __ptr;
4961 if (__len == 0)
4962 break;
4963 __len = (__len - 1) / 2;
4964 __ptr = __first + __len;
4965 } while (__comp(*__ptr, __t));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004966 *__last = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004967 }
4968 }
4969}
4970
4971template <class _RandomAccessIterator, class _Compare>
4972inline _LIBCPP_INLINE_VISIBILITY
4973void
4974push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4975{
Howard Hinnant5e571422013-08-23 20:10:18 +00004976#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004977 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4978 __debug_less<_Compare> __c(__comp);
David Majnemercb8757a2014-07-22 06:07:09 +00004979 __sift_up<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004980#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004981 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
David Majnemercb8757a2014-07-22 06:07:09 +00004982 __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004983#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004984}
4985
4986template <class _RandomAccessIterator>
4987inline _LIBCPP_INLINE_VISIBILITY
4988void
4989push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4990{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004991 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004992}
4993
4994// pop_heap
4995
4996template <class _Compare, class _RandomAccessIterator>
David Majnemercb8757a2014-07-22 06:07:09 +00004997void
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00004998__sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
4999 _Compare __comp,
David Majnemercb8757a2014-07-22 06:07:09 +00005000 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
5001 _RandomAccessIterator __start)
5002{
5003 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5004 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
5005 // left-child of __start is at 2 * __start + 1
5006 // right-child of __start is at 2 * __start + 2
5007 difference_type __child = __start - __first;
5008
5009 if (__len < 2 || (__len - 2) / 2 < __child)
5010 return;
5011
5012 __child = 2 * __child + 1;
5013 _RandomAccessIterator __child_i = __first + __child;
5014
5015 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
5016 // right-child exists and is greater than left-child
5017 ++__child_i;
5018 ++__child;
5019 }
5020
5021 // check if we are in heap-order
5022 if (__comp(*__child_i, *__start))
5023 // we are, __start is larger than it's largest child
5024 return;
5025
5026 value_type __top(_VSTD::move(*__start));
5027 do
5028 {
5029 // we are not in heap-order, swap the parent with it's largest child
5030 *__start = _VSTD::move(*__child_i);
5031 __start = __child_i;
5032
5033 if ((__len - 2) / 2 < __child)
5034 break;
5035
5036 // recompute the child based off of the updated parent
5037 __child = 2 * __child + 1;
5038 __child_i = __first + __child;
5039
5040 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
5041 // right-child exists and is greater than left-child
5042 ++__child_i;
5043 ++__child;
5044 }
5045
5046 // check if we are in heap-order
5047 } while (!__comp(*__child_i, __top));
5048 *__start = _VSTD::move(__top);
5049}
5050
5051template <class _Compare, class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005052inline _LIBCPP_INLINE_VISIBILITY
5053void
5054__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
5055 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
5056{
5057 if (__len > 1)
5058 {
5059 swap(*__first, *--__last);
David Majnemercb8757a2014-07-22 06:07:09 +00005060 __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005061 }
5062}
5063
5064template <class _RandomAccessIterator, class _Compare>
5065inline _LIBCPP_INLINE_VISIBILITY
5066void
5067pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5068{
Howard Hinnant5e571422013-08-23 20:10:18 +00005069#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005070 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5071 __debug_less<_Compare> __c(__comp);
5072 __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00005073#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005074 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5075 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00005076#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005077}
5078
5079template <class _RandomAccessIterator>
5080inline _LIBCPP_INLINE_VISIBILITY
5081void
5082pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5083{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005084 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005085}
5086
5087// make_heap
5088
5089template <class _Compare, class _RandomAccessIterator>
5090void
5091__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5092{
5093 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5094 difference_type __n = __last - __first;
5095 if (__n > 1)
5096 {
David Majnemercb8757a2014-07-22 06:07:09 +00005097 // start from the first parent, there is no need to consider children
5098 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
5099 {
5100 __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
5101 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005102 }
5103}
5104
5105template <class _RandomAccessIterator, class _Compare>
5106inline _LIBCPP_INLINE_VISIBILITY
5107void
5108make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5109{
Howard Hinnant5e571422013-08-23 20:10:18 +00005110#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005111 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5112 __debug_less<_Compare> __c(__comp);
5113 __make_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005114#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005115 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5116 __make_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005117#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005118}
5119
5120template <class _RandomAccessIterator>
5121inline _LIBCPP_INLINE_VISIBILITY
5122void
5123make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5124{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005125 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005126}
5127
5128// sort_heap
5129
5130template <class _Compare, class _RandomAccessIterator>
5131void
5132__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5133{
5134 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5135 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
5136 __pop_heap<_Compare>(__first, __last, __comp, __n);
5137}
5138
5139template <class _RandomAccessIterator, class _Compare>
5140inline _LIBCPP_INLINE_VISIBILITY
5141void
5142sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5143{
Howard Hinnant5e571422013-08-23 20:10:18 +00005144#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005145 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5146 __debug_less<_Compare> __c(__comp);
5147 __sort_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005148#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005149 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5150 __sort_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005151#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005152}
5153
5154template <class _RandomAccessIterator>
5155inline _LIBCPP_INLINE_VISIBILITY
5156void
5157sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5158{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005159 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005160}
5161
5162// partial_sort
5163
5164template <class _Compare, class _RandomAccessIterator>
5165void
5166__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5167 _Compare __comp)
5168{
5169 __make_heap<_Compare>(__first, __middle, __comp);
5170 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5171 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5172 {
5173 if (__comp(*__i, *__first))
5174 {
5175 swap(*__i, *__first);
David Majnemercb8757a2014-07-22 06:07:09 +00005176 __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005177 }
5178 }
5179 __sort_heap<_Compare>(__first, __middle, __comp);
5180}
5181
5182template <class _RandomAccessIterator, class _Compare>
5183inline _LIBCPP_INLINE_VISIBILITY
5184void
5185partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5186 _Compare __comp)
5187{
Howard Hinnant5e571422013-08-23 20:10:18 +00005188#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005189 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5190 __debug_less<_Compare> __c(__comp);
5191 __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005192#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005193 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5194 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005195#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005196}
5197
5198template <class _RandomAccessIterator>
5199inline _LIBCPP_INLINE_VISIBILITY
5200void
5201partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5202{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005203 _VSTD::partial_sort(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005204 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5205}
5206
5207// partial_sort_copy
5208
5209template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5210_RandomAccessIterator
5211__partial_sort_copy(_InputIterator __first, _InputIterator __last,
5212 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5213{
5214 _RandomAccessIterator __r = __result_first;
5215 if (__r != __result_last)
5216 {
Eric Fiselierb9919752014-10-27 19:28:20 +00005217 for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005218 *__r = *__first;
5219 __make_heap<_Compare>(__result_first, __r, __comp);
David Majnemercb8757a2014-07-22 06:07:09 +00005220 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005221 for (; __first != __last; ++__first)
5222 if (__comp(*__first, *__result_first))
5223 {
5224 *__result_first = *__first;
David Majnemercb8757a2014-07-22 06:07:09 +00005225 __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005226 }
5227 __sort_heap<_Compare>(__result_first, __r, __comp);
5228 }
5229 return __r;
5230}
5231
5232template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5233inline _LIBCPP_INLINE_VISIBILITY
5234_RandomAccessIterator
5235partial_sort_copy(_InputIterator __first, _InputIterator __last,
5236 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5237{
Howard Hinnant5e571422013-08-23 20:10:18 +00005238#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005239 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5240 __debug_less<_Compare> __c(__comp);
5241 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005242#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005243 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5244 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005245#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005246}
5247
5248template <class _InputIterator, class _RandomAccessIterator>
5249inline _LIBCPP_INLINE_VISIBILITY
5250_RandomAccessIterator
5251partial_sort_copy(_InputIterator __first, _InputIterator __last,
5252 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5253{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005254 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005255 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5256}
5257
5258// nth_element
5259
5260template <class _Compare, class _RandomAccessIterator>
5261void
5262__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5263{
5264 // _Compare is known to be a reference type
5265 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5266 const difference_type __limit = 7;
5267 while (true)
5268 {
5269 __restart:
Howard Hinnant8292d742011-12-29 17:45:35 +00005270 if (__nth == __last)
5271 return;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005272 difference_type __len = __last - __first;
5273 switch (__len)
5274 {
5275 case 0:
5276 case 1:
5277 return;
5278 case 2:
5279 if (__comp(*--__last, *__first))
5280 swap(*__first, *__last);
5281 return;
5282 case 3:
5283 {
5284 _RandomAccessIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00005285 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005286 return;
5287 }
5288 }
5289 if (__len <= __limit)
5290 {
5291 __selection_sort<_Compare>(__first, __last, __comp);
5292 return;
5293 }
5294 // __len > __limit >= 3
5295 _RandomAccessIterator __m = __first + __len/2;
5296 _RandomAccessIterator __lm1 = __last;
Howard Hinnant0949eed2011-06-30 21:18:19 +00005297 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005298 // *__m is median
5299 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5300 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5301 _RandomAccessIterator __i = __first;
5302 _RandomAccessIterator __j = __lm1;
5303 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5304 // The search going up is known to be guarded but the search coming down isn't.
5305 // Prime the downward search with a guard.
5306 if (!__comp(*__i, *__m)) // if *__first == *__m
5307 {
5308 // *__first == *__m, *__first doesn't go in first part
5309 // manually guard downward moving __j against __i
5310 while (true)
5311 {
5312 if (__i == --__j)
5313 {
5314 // *__first == *__m, *__m <= all other elements
5315 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5316 ++__i; // __first + 1
5317 __j = __last;
5318 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5319 {
5320 while (true)
5321 {
5322 if (__i == __j)
5323 return; // [__first, __last) all equivalent elements
5324 if (__comp(*__first, *__i))
5325 {
5326 swap(*__i, *__j);
5327 ++__n_swaps;
5328 ++__i;
5329 break;
5330 }
5331 ++__i;
5332 }
5333 }
5334 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5335 if (__i == __j)
5336 return;
5337 while (true)
5338 {
5339 while (!__comp(*__first, *__i))
5340 ++__i;
5341 while (__comp(*__first, *--__j))
5342 ;
5343 if (__i >= __j)
5344 break;
5345 swap(*__i, *__j);
5346 ++__n_swaps;
5347 ++__i;
5348 }
5349 // [__first, __i) == *__first and *__first < [__i, __last)
5350 // The first part is sorted,
5351 if (__nth < __i)
5352 return;
5353 // __nth_element the secod part
5354 // __nth_element<_Compare>(__i, __nth, __last, __comp);
5355 __first = __i;
5356 goto __restart;
5357 }
5358 if (__comp(*__j, *__m))
5359 {
5360 swap(*__i, *__j);
5361 ++__n_swaps;
5362 break; // found guard for downward moving __j, now use unguarded partition
5363 }
5364 }
5365 }
5366 ++__i;
5367 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5368 // if not yet partitioned...
5369 if (__i < __j)
5370 {
5371 // known that *(__i - 1) < *__m
5372 while (true)
5373 {
5374 // __m still guards upward moving __i
5375 while (__comp(*__i, *__m))
5376 ++__i;
5377 // It is now known that a guard exists for downward moving __j
5378 while (!__comp(*--__j, *__m))
5379 ;
5380 if (__i >= __j)
5381 break;
5382 swap(*__i, *__j);
5383 ++__n_swaps;
5384 // It is known that __m != __j
5385 // If __m just moved, follow it
5386 if (__m == __i)
5387 __m = __j;
5388 ++__i;
5389 }
5390 }
5391 // [__first, __i) < *__m and *__m <= [__i, __last)
5392 if (__i != __m && __comp(*__m, *__i))
5393 {
5394 swap(*__i, *__m);
5395 ++__n_swaps;
5396 }
5397 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5398 if (__nth == __i)
5399 return;
5400 if (__n_swaps == 0)
5401 {
5402 // We were given a perfectly partitioned sequence. Coincidence?
5403 if (__nth < __i)
5404 {
5405 // Check for [__first, __i) already sorted
5406 __j = __m = __first;
5407 while (++__j != __i)
5408 {
5409 if (__comp(*__j, *__m))
5410 // not yet sorted, so sort
5411 goto not_sorted;
5412 __m = __j;
5413 }
5414 // [__first, __i) sorted
5415 return;
5416 }
5417 else
5418 {
5419 // Check for [__i, __last) already sorted
5420 __j = __m = __i;
5421 while (++__j != __last)
5422 {
5423 if (__comp(*__j, *__m))
5424 // not yet sorted, so sort
5425 goto not_sorted;
5426 __m = __j;
5427 }
5428 // [__i, __last) sorted
5429 return;
5430 }
5431 }
5432not_sorted:
5433 // __nth_element on range containing __nth
5434 if (__nth < __i)
5435 {
5436 // __nth_element<_Compare>(__first, __nth, __i, __comp);
5437 __last = __i;
5438 }
5439 else
5440 {
5441 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5442 __first = ++__i;
5443 }
5444 }
5445}
5446
5447template <class _RandomAccessIterator, class _Compare>
5448inline _LIBCPP_INLINE_VISIBILITY
5449void
5450nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5451{
Howard Hinnant5e571422013-08-23 20:10:18 +00005452#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005453 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5454 __debug_less<_Compare> __c(__comp);
5455 __nth_element<_Comp_ref>(__first, __nth, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005456#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005457 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5458 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005459#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005460}
5461
5462template <class _RandomAccessIterator>
5463inline _LIBCPP_INLINE_VISIBILITY
5464void
5465nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5466{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005467 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005468}
5469
5470// includes
5471
5472template <class _Compare, class _InputIterator1, class _InputIterator2>
5473bool
5474__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5475 _Compare __comp)
5476{
5477 for (; __first2 != __last2; ++__first1)
5478 {
5479 if (__first1 == __last1 || __comp(*__first2, *__first1))
5480 return false;
5481 if (!__comp(*__first1, *__first2))
5482 ++__first2;
5483 }
5484 return true;
5485}
5486
5487template <class _InputIterator1, class _InputIterator2, class _Compare>
5488inline _LIBCPP_INLINE_VISIBILITY
5489bool
5490includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5491 _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 __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __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 __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __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>
5504inline _LIBCPP_INLINE_VISIBILITY
5505bool
5506includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5507{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005508 return _VSTD::includes(__first1, __last1, __first2, __last2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005509 __less<typename iterator_traits<_InputIterator1>::value_type,
5510 typename iterator_traits<_InputIterator2>::value_type>());
5511}
5512
5513// set_union
5514
5515template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5516_OutputIterator
5517__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5518 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5519{
5520 for (; __first1 != __last1; ++__result)
5521 {
5522 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005523 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005524 if (__comp(*__first2, *__first1))
5525 {
5526 *__result = *__first2;
5527 ++__first2;
5528 }
5529 else
5530 {
5531 *__result = *__first1;
5532 if (!__comp(*__first1, *__first2))
5533 ++__first2;
5534 ++__first1;
5535 }
5536 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00005537 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005538}
5539
5540template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5541inline _LIBCPP_INLINE_VISIBILITY
5542_OutputIterator
5543set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5544 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5545{
Howard Hinnant5e571422013-08-23 20:10:18 +00005546#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005547 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5548 __debug_less<_Compare> __c(__comp);
5549 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005550#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005551 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5552 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005553#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005554}
5555
5556template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5557inline _LIBCPP_INLINE_VISIBILITY
5558_OutputIterator
5559set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5560 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5561{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005562 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005563 __less<typename iterator_traits<_InputIterator1>::value_type,
5564 typename iterator_traits<_InputIterator2>::value_type>());
5565}
5566
5567// set_intersection
5568
5569template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5570_OutputIterator
5571__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5572 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5573{
5574 while (__first1 != __last1 && __first2 != __last2)
5575 {
5576 if (__comp(*__first1, *__first2))
5577 ++__first1;
5578 else
5579 {
5580 if (!__comp(*__first2, *__first1))
5581 {
5582 *__result = *__first1;
5583 ++__result;
5584 ++__first1;
5585 }
5586 ++__first2;
5587 }
5588 }
5589 return __result;
5590}
5591
5592template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5593inline _LIBCPP_INLINE_VISIBILITY
5594_OutputIterator
5595set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5596 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5597{
Howard Hinnant5e571422013-08-23 20:10:18 +00005598#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005599 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5600 __debug_less<_Compare> __c(__comp);
5601 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005602#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005603 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5604 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005605#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005606}
5607
5608template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5609inline _LIBCPP_INLINE_VISIBILITY
5610_OutputIterator
5611set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5612 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5613{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005614 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005615 __less<typename iterator_traits<_InputIterator1>::value_type,
5616 typename iterator_traits<_InputIterator2>::value_type>());
5617}
5618
5619// set_difference
5620
5621template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5622_OutputIterator
5623__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5624 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5625{
5626 while (__first1 != __last1)
5627 {
5628 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005629 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005630 if (__comp(*__first1, *__first2))
5631 {
5632 *__result = *__first1;
5633 ++__result;
5634 ++__first1;
5635 }
5636 else
5637 {
5638 if (!__comp(*__first2, *__first1))
5639 ++__first1;
5640 ++__first2;
5641 }
5642 }
5643 return __result;
5644}
5645
5646template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5647inline _LIBCPP_INLINE_VISIBILITY
5648_OutputIterator
5649set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5650 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5651{
Howard Hinnant5e571422013-08-23 20:10:18 +00005652#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005653 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5654 __debug_less<_Compare> __c(__comp);
5655 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005656#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005657 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5658 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005659#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005660}
5661
5662template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5663inline _LIBCPP_INLINE_VISIBILITY
5664_OutputIterator
5665set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5666 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5667{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005668 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005669 __less<typename iterator_traits<_InputIterator1>::value_type,
5670 typename iterator_traits<_InputIterator2>::value_type>());
5671}
5672
5673// set_symmetric_difference
5674
5675template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5676_OutputIterator
5677__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5678 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5679{
5680 while (__first1 != __last1)
5681 {
5682 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005683 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005684 if (__comp(*__first1, *__first2))
5685 {
5686 *__result = *__first1;
5687 ++__result;
5688 ++__first1;
5689 }
5690 else
5691 {
5692 if (__comp(*__first2, *__first1))
5693 {
5694 *__result = *__first2;
5695 ++__result;
5696 }
5697 else
5698 ++__first1;
5699 ++__first2;
5700 }
5701 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00005702 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005703}
5704
5705template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5706inline _LIBCPP_INLINE_VISIBILITY
5707_OutputIterator
5708set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5709 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5710{
Howard Hinnant5e571422013-08-23 20:10:18 +00005711#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005712 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5713 __debug_less<_Compare> __c(__comp);
5714 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005715#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005716 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5717 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005718#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005719}
5720
5721template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5722inline _LIBCPP_INLINE_VISIBILITY
5723_OutputIterator
5724set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5725 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5726{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005727 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005728 __less<typename iterator_traits<_InputIterator1>::value_type,
5729 typename iterator_traits<_InputIterator2>::value_type>());
5730}
5731
5732// lexicographical_compare
5733
5734template <class _Compare, class _InputIterator1, class _InputIterator2>
5735bool
5736__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5737 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5738{
Eric Fiselierb9919752014-10-27 19:28:20 +00005739 for (; __first2 != __last2; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005740 {
5741 if (__first1 == __last1 || __comp(*__first1, *__first2))
5742 return true;
5743 if (__comp(*__first2, *__first1))
5744 return false;
5745 }
5746 return false;
5747}
5748
5749template <class _InputIterator1, class _InputIterator2, class _Compare>
5750inline _LIBCPP_INLINE_VISIBILITY
5751bool
5752lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5753 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5754{
Howard Hinnant5e571422013-08-23 20:10:18 +00005755#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005756 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5757 __debug_less<_Compare> __c(__comp);
5758 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005759#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005760 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5761 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005762#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005763}
5764
5765template <class _InputIterator1, class _InputIterator2>
5766inline _LIBCPP_INLINE_VISIBILITY
5767bool
5768lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5769 _InputIterator2 __first2, _InputIterator2 __last2)
5770{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005771 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005772 __less<typename iterator_traits<_InputIterator1>::value_type,
5773 typename iterator_traits<_InputIterator2>::value_type>());
5774}
5775
5776// next_permutation
5777
5778template <class _Compare, class _BidirectionalIterator>
5779bool
5780__next_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(*--__i, *__ip1))
5789 {
5790 _BidirectionalIterator __j = __last;
5791 while (!__comp(*__i, *--__j))
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
5808next_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 __next_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 __next_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
Howard Hinnant324bb032010-08-22 00:02:43 +00005822bool
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005823next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5824{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005825 return _VSTD::next_permutation(__first, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005826 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5827}
5828
5829// prev_permutation
5830
5831template <class _Compare, class _BidirectionalIterator>
5832bool
5833__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5834{
5835 _BidirectionalIterator __i = __last;
5836 if (__first == __last || __first == --__i)
5837 return false;
5838 while (true)
5839 {
5840 _BidirectionalIterator __ip1 = __i;
5841 if (__comp(*__ip1, *--__i))
5842 {
5843 _BidirectionalIterator __j = __last;
5844 while (!__comp(*--__j, *__i))
5845 ;
5846 swap(*__i, *__j);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005847 _VSTD::reverse(__ip1, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005848 return true;
5849 }
5850 if (__i == __first)
5851 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005852 _VSTD::reverse(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005853 return false;
5854 }
5855 }
5856}
5857
5858template <class _BidirectionalIterator, class _Compare>
5859inline _LIBCPP_INLINE_VISIBILITY
5860bool
5861prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5862{
Howard Hinnant5e571422013-08-23 20:10:18 +00005863#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005864 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5865 __debug_less<_Compare> __c(__comp);
5866 return __prev_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005867#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005868 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5869 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005870#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005871}
5872
5873template <class _BidirectionalIterator>
5874inline _LIBCPP_INLINE_VISIBILITY
5875bool
5876prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5877{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005878 return _VSTD::prev_permutation(__first, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005879 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5880}
5881
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005882_LIBCPP_END_NAMESPACE_STD
5883
5884#endif // _LIBCPP_ALGORITHM