blob: c5a5d0ad880346f9da2187d6d7af84d51c47cff1 [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 Clow3fef95b2014-03-03 06:14:19 +0000284 random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000285
286template <class RandomAccessIterator, class RandomNumberGenerator>
287 void
Marshall Clow3fef95b2014-03-03 06:14:19 +0000288 random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
289 RandomNumberGenerator& rand); // deprecated in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290
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
Howard Hinnantef5aa932013-09-17 01:34:47 +0000647#if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
648#include "support/win32/support.h"
649#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
786unsigned
787__ctz(unsigned __x)
788{
789 return static_cast<unsigned>(__builtin_ctz(__x));
790}
791
792inline _LIBCPP_INLINE_VISIBILITY
793unsigned long
794__ctz(unsigned long __x)
795{
796 return static_cast<unsigned long>(__builtin_ctzl(__x));
797}
798
799inline _LIBCPP_INLINE_VISIBILITY
800unsigned long long
801__ctz(unsigned long long __x)
802{
803 return static_cast<unsigned long long>(__builtin_ctzll(__x));
804}
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000805
806// Precondition: __x != 0
Howard Hinnantec3773c2011-12-01 20:21:04 +0000807inline _LIBCPP_INLINE_VISIBILITY
808unsigned
809__clz(unsigned __x)
810{
811 return static_cast<unsigned>(__builtin_clz(__x));
812}
813
814inline _LIBCPP_INLINE_VISIBILITY
815unsigned long
816__clz(unsigned long __x)
817{
818 return static_cast<unsigned long>(__builtin_clzl (__x));
819}
820
821inline _LIBCPP_INLINE_VISIBILITY
822unsigned long long
823__clz(unsigned long long __x)
824{
825 return static_cast<unsigned long long>(__builtin_clzll(__x));
826}
Howard Hinnant13c98cc2010-05-27 20:06:01 +0000827
828inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) {return __builtin_popcount (__x);}
829inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) {return __builtin_popcountl (__x);}
830inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {return __builtin_popcountll(__x);}
831
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000832// all_of
833
834template <class _InputIterator, class _Predicate>
835inline _LIBCPP_INLINE_VISIBILITY
836bool
837all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
838{
839 for (; __first != __last; ++__first)
840 if (!__pred(*__first))
841 return false;
842 return true;
843}
844
845// any_of
846
847template <class _InputIterator, class _Predicate>
848inline _LIBCPP_INLINE_VISIBILITY
849bool
850any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
851{
852 for (; __first != __last; ++__first)
853 if (__pred(*__first))
854 return true;
855 return false;
856}
857
858// none_of
859
860template <class _InputIterator, class _Predicate>
861inline _LIBCPP_INLINE_VISIBILITY
862bool
863none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
864{
865 for (; __first != __last; ++__first)
866 if (__pred(*__first))
867 return false;
868 return true;
869}
870
871// for_each
872
873template <class _InputIterator, class _Function>
874inline _LIBCPP_INLINE_VISIBILITY
875_Function
876for_each(_InputIterator __first, _InputIterator __last, _Function __f)
877{
878 for (; __first != __last; ++__first)
879 __f(*__first);
Marshall Clowdb7fa112016-11-14 18:22:19 +0000880 return __f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000881}
882
883// find
884
885template <class _InputIterator, class _Tp>
886inline _LIBCPP_INLINE_VISIBILITY
887_InputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +0000888find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000889{
890 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +0000891 if (*__first == __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000892 break;
893 return __first;
894}
895
896// find_if
897
898template <class _InputIterator, class _Predicate>
899inline _LIBCPP_INLINE_VISIBILITY
900_InputIterator
901find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
902{
903 for (; __first != __last; ++__first)
904 if (__pred(*__first))
905 break;
906 return __first;
907}
908
909// find_if_not
910
911template<class _InputIterator, class _Predicate>
912inline _LIBCPP_INLINE_VISIBILITY
913_InputIterator
914find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
915{
916 for (; __first != __last; ++__first)
917 if (!__pred(*__first))
918 break;
919 return __first;
920}
921
922// find_end
923
924template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
925_ForwardIterator1
926__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
927 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
928 forward_iterator_tag, forward_iterator_tag)
929{
930 // modeled after search algorithm
931 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
932 if (__first2 == __last2)
933 return __r;
934 while (true)
935 {
936 while (true)
937 {
938 if (__first1 == __last1) // if source exhausted return last correct answer
939 return __r; // (or __last1 if never found)
940 if (__pred(*__first1, *__first2))
941 break;
942 ++__first1;
943 }
944 // *__first1 matches *__first2, now match elements after here
945 _ForwardIterator1 __m1 = __first1;
946 _ForwardIterator2 __m2 = __first2;
947 while (true)
948 {
949 if (++__m2 == __last2)
950 { // Pattern exhaused, record answer and search for another one
951 __r = __first1;
952 ++__first1;
953 break;
954 }
955 if (++__m1 == __last1) // Source exhausted, return last answer
956 return __r;
957 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
958 {
959 ++__first1;
960 break;
961 } // else there is a match, check next elements
962 }
963 }
964}
965
966template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
967_BidirectionalIterator1
968__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
969 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
970 bidirectional_iterator_tag, bidirectional_iterator_tag)
971{
972 // modeled after search algorithm (in reverse)
973 if (__first2 == __last2)
974 return __last1; // Everything matches an empty sequence
975 _BidirectionalIterator1 __l1 = __last1;
976 _BidirectionalIterator2 __l2 = __last2;
977 --__l2;
978 while (true)
979 {
980 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
981 while (true)
982 {
983 if (__first1 == __l1) // return __last1 if no element matches *__first2
984 return __last1;
985 if (__pred(*--__l1, *__l2))
986 break;
987 }
988 // *__l1 matches *__l2, now match elements before here
989 _BidirectionalIterator1 __m1 = __l1;
990 _BidirectionalIterator2 __m2 = __l2;
991 while (true)
992 {
993 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
994 return __m1;
995 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
996 return __last1;
997 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
998 {
999 break;
1000 } // else there is a match, check next elements
1001 }
1002 }
1003}
1004
1005template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow37025e12014-06-10 18:51:55 +00001006_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001007__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1008 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1009 random_access_iterator_tag, random_access_iterator_tag)
1010{
1011 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1012 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
1013 if (__len2 == 0)
1014 return __last1;
1015 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
1016 if (__len1 < __len2)
1017 return __last1;
1018 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
1019 _RandomAccessIterator1 __l1 = __last1;
1020 _RandomAccessIterator2 __l2 = __last2;
1021 --__l2;
1022 while (true)
1023 {
1024 while (true)
1025 {
1026 if (__s == __l1)
1027 return __last1;
1028 if (__pred(*--__l1, *__l2))
1029 break;
1030 }
1031 _RandomAccessIterator1 __m1 = __l1;
1032 _RandomAccessIterator2 __m2 = __l2;
1033 while (true)
1034 {
1035 if (__m2 == __first2)
1036 return __m1;
1037 // no need to check range on __m1 because __s guarantees we have enough source
1038 if (!__pred(*--__m1, *--__m2))
1039 {
1040 break;
1041 }
1042 }
1043 }
1044}
1045
1046template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1047inline _LIBCPP_INLINE_VISIBILITY
1048_ForwardIterator1
1049find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1050 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1051{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001052 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001053 (__first1, __last1, __first2, __last2, __pred,
1054 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1055 typename iterator_traits<_ForwardIterator2>::iterator_category());
1056}
1057
1058template <class _ForwardIterator1, class _ForwardIterator2>
1059inline _LIBCPP_INLINE_VISIBILITY
1060_ForwardIterator1
1061find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1062 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1063{
1064 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1065 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001066 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067}
1068
1069// find_first_of
1070
1071template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow37025e12014-06-10 18:51:55 +00001072_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1073__find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1075{
1076 for (; __first1 != __last1; ++__first1)
1077 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1078 if (__pred(*__first1, *__j))
1079 return __first1;
1080 return __last1;
1081}
1082
Marshall Clow37025e12014-06-10 18:51:55 +00001083
1084template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1085inline _LIBCPP_INLINE_VISIBILITY
1086_ForwardIterator1
1087find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1088 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1089{
1090 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1091}
1092
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001093template <class _ForwardIterator1, class _ForwardIterator2>
1094inline _LIBCPP_INLINE_VISIBILITY
1095_ForwardIterator1
1096find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1097 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1098{
1099 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1100 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Marshall Clow37025e12014-06-10 18:51:55 +00001101 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001102}
1103
1104// adjacent_find
1105
1106template <class _ForwardIterator, class _BinaryPredicate>
1107inline _LIBCPP_INLINE_VISIBILITY
1108_ForwardIterator
1109adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1110{
1111 if (__first != __last)
1112 {
1113 _ForwardIterator __i = __first;
1114 while (++__i != __last)
1115 {
1116 if (__pred(*__first, *__i))
1117 return __first;
1118 __first = __i;
1119 }
1120 }
1121 return __last;
1122}
1123
1124template <class _ForwardIterator>
1125inline _LIBCPP_INLINE_VISIBILITY
1126_ForwardIterator
1127adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1128{
1129 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001130 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001131}
1132
1133// count
1134
1135template <class _InputIterator, class _Tp>
1136inline _LIBCPP_INLINE_VISIBILITY
1137typename iterator_traits<_InputIterator>::difference_type
Howard Hinnant78b68282011-10-22 20:59:45 +00001138count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001139{
1140 typename iterator_traits<_InputIterator>::difference_type __r(0);
1141 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +00001142 if (*__first == __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001143 ++__r;
1144 return __r;
1145}
1146
1147// count_if
1148
1149template <class _InputIterator, class _Predicate>
1150inline _LIBCPP_INLINE_VISIBILITY
1151typename iterator_traits<_InputIterator>::difference_type
1152count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1153{
1154 typename iterator_traits<_InputIterator>::difference_type __r(0);
1155 for (; __first != __last; ++__first)
1156 if (__pred(*__first))
1157 ++__r;
1158 return __r;
1159}
1160
1161// mismatch
1162
1163template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1164inline _LIBCPP_INLINE_VISIBILITY
1165pair<_InputIterator1, _InputIterator2>
1166mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1167 _InputIterator2 __first2, _BinaryPredicate __pred)
1168{
Marshall Clowd402a4d2014-09-16 20:40:05 +00001169 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001170 if (!__pred(*__first1, *__first2))
1171 break;
1172 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1173}
1174
1175template <class _InputIterator1, class _InputIterator2>
1176inline _LIBCPP_INLINE_VISIBILITY
1177pair<_InputIterator1, _InputIterator2>
1178mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1179{
1180 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1181 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001182 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001183}
1184
Marshall Clowb30abdd2013-05-09 21:14:23 +00001185#if _LIBCPP_STD_VER > 11
1186template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1187inline _LIBCPP_INLINE_VISIBILITY
1188pair<_InputIterator1, _InputIterator2>
1189mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1190 _InputIterator2 __first2, _InputIterator2 __last2,
1191 _BinaryPredicate __pred)
1192{
Marshall Clowd402a4d2014-09-16 20:40:05 +00001193 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001194 if (!__pred(*__first1, *__first2))
1195 break;
1196 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1197}
1198
1199template <class _InputIterator1, class _InputIterator2>
1200inline _LIBCPP_INLINE_VISIBILITY
1201pair<_InputIterator1, _InputIterator2>
1202mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1203 _InputIterator2 __first2, _InputIterator2 __last2)
1204{
1205 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1206 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1207 return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1208}
1209#endif
1210
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001211// equal
1212
1213template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1214inline _LIBCPP_INLINE_VISIBILITY
1215bool
1216equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1217{
Eric Fiselierb9919752014-10-27 19:28:20 +00001218 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001219 if (!__pred(*__first1, *__first2))
1220 return false;
1221 return true;
1222}
1223
1224template <class _InputIterator1, class _InputIterator2>
1225inline _LIBCPP_INLINE_VISIBILITY
1226bool
1227equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1228{
1229 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1230 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001231 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001232}
1233
Marshall Clowb30abdd2013-05-09 21:14:23 +00001234#if _LIBCPP_STD_VER > 11
1235template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
1236inline _LIBCPP_INLINE_VISIBILITY
1237bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001238__equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001239 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1240 input_iterator_tag, input_iterator_tag )
1241{
Eric Fiselierb9919752014-10-27 19:28:20 +00001242 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001243 if (!__pred(*__first1, *__first2))
1244 return false;
1245 return __first1 == __last1 && __first2 == __last2;
1246}
1247
1248template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1249inline _LIBCPP_INLINE_VISIBILITY
1250bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001251__equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1252 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001253 random_access_iterator_tag, random_access_iterator_tag )
1254{
1255 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1256 return false;
1257 return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1258 typename add_lvalue_reference<_BinaryPredicate>::type>
1259 (__first1, __last1, __first2, __pred );
1260}
1261
1262template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
1263inline _LIBCPP_INLINE_VISIBILITY
1264bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001265equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001266 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1267{
1268 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001269 (__first1, __last1, __first2, __last2, __pred,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001270 typename iterator_traits<_InputIterator1>::iterator_category(),
1271 typename iterator_traits<_InputIterator2>::iterator_category());
1272}
1273
1274template <class _InputIterator1, class _InputIterator2>
1275inline _LIBCPP_INLINE_VISIBILITY
1276bool
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001277equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001278 _InputIterator2 __first2, _InputIterator2 __last2)
1279{
1280 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1281 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1282 return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1283 typename iterator_traits<_InputIterator1>::iterator_category(),
1284 typename iterator_traits<_InputIterator2>::iterator_category());
1285}
1286#endif
1287
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001288// is_permutation
1289
1290template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1291bool
1292is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1293 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1294{
1295 // shorten sequences as much as possible by lopping of any equal parts
Eric Fiselierb9919752014-10-27 19:28:20 +00001296 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001297 if (!__pred(*__first1, *__first2))
1298 goto __not_done;
1299 return true;
1300__not_done:
1301 // __first1 != __last1 && *__first1 != *__first2
1302 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001303 _D1 __l1 = _VSTD::distance(__first1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001304 if (__l1 == _D1(1))
1305 return false;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001306 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001307 // For each element in [f1, l1) see if there are the same number of
1308 // equal elements in [f2, l2)
1309 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1310 {
1311 // Have we already counted the number of *__i in [f1, l1)?
1312 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1313 if (__pred(*__j, *__i))
1314 goto __next_iter;
1315 {
1316 // Count number of *__i in [f2, l2)
1317 _D1 __c2 = 0;
1318 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1319 if (__pred(*__i, *__j))
1320 ++__c2;
1321 if (__c2 == 0)
1322 return false;
1323 // Count number of *__i in [__i, l1) (we can start with 1)
1324 _D1 __c1 = 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001325 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326 if (__pred(*__i, *__j))
1327 ++__c1;
1328 if (__c1 != __c2)
1329 return false;
1330 }
1331__next_iter:;
1332 }
1333 return true;
1334}
1335
1336template<class _ForwardIterator1, class _ForwardIterator2>
1337inline _LIBCPP_INLINE_VISIBILITY
1338bool
1339is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1340 _ForwardIterator2 __first2)
1341{
1342 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1343 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001344 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001345}
1346
Marshall Clowb30abdd2013-05-09 21:14:23 +00001347#if _LIBCPP_STD_VER > 11
1348template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
1349bool
1350__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001351 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001352 _BinaryPredicate __pred,
1353 forward_iterator_tag, forward_iterator_tag )
1354{
1355 // shorten sequences as much as possible by lopping of any equal parts
Eric Fiselier62a0e012014-10-27 20:26:25 +00001356 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clowb30abdd2013-05-09 21:14:23 +00001357 if (!__pred(*__first1, *__first2))
1358 goto __not_done;
1359 return __first1 == __last1 && __first2 == __last2;
1360__not_done:
1361 // __first1 != __last1 && __first2 != __last2 && *__first1 != *__first2
1362 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1363 _D1 __l1 = _VSTD::distance(__first1, __last1);
1364
1365 typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
Marshall Clow9f8f5242013-05-10 00:16:10 +00001366 _D2 __l2 = _VSTD::distance(__first2, __last2);
Marshall Clowb30abdd2013-05-09 21:14:23 +00001367 if (__l1 != __l2)
1368 return false;
1369
1370 // For each element in [f1, l1) see if there are the same number of
1371 // equal elements in [f2, l2)
1372 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1373 {
1374 // Have we already counted the number of *__i in [f1, l1)?
1375 for (_ForwardIterator1 __j = __first1; __j != __i; ++__j)
1376 if (__pred(*__j, *__i))
1377 goto __next_iter;
1378 {
1379 // Count number of *__i in [f2, l2)
1380 _D1 __c2 = 0;
1381 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1382 if (__pred(*__i, *__j))
1383 ++__c2;
1384 if (__c2 == 0)
1385 return false;
1386 // Count number of *__i in [__i, l1) (we can start with 1)
1387 _D1 __c1 = 1;
1388 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1389 if (__pred(*__i, *__j))
1390 ++__c1;
1391 if (__c1 != __c2)
1392 return false;
1393 }
1394__next_iter:;
1395 }
1396 return true;
1397}
1398
1399template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
1400bool
1401__is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001402 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
Marshall Clowb30abdd2013-05-09 21:14:23 +00001403 _BinaryPredicate __pred,
1404 random_access_iterator_tag, random_access_iterator_tag )
1405{
1406 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1407 return false;
1408 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1409 typename add_lvalue_reference<_BinaryPredicate>::type>
1410 (__first1, __last1, __first2, __pred );
1411}
1412
1413template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1414inline _LIBCPP_INLINE_VISIBILITY
1415bool
1416is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1417 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1418 _BinaryPredicate __pred )
1419{
1420 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1421 (__first1, __last1, __first2, __last2, __pred,
1422 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1423 typename iterator_traits<_ForwardIterator2>::iterator_category());
1424}
1425
1426template<class _ForwardIterator1, class _ForwardIterator2>
1427inline _LIBCPP_INLINE_VISIBILITY
1428bool
1429is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1430 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1431{
1432 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1433 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1434 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1435 __equal_to<__v1, __v2>(),
1436 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1437 typename iterator_traits<_ForwardIterator2>::iterator_category());
1438}
1439#endif
1440
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001441// search
1442
1443template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clowf6d6b512016-03-08 15:12:52 +00001444pair<_ForwardIterator1, _ForwardIterator1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001445__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1446 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
1447 forward_iterator_tag, forward_iterator_tag)
1448{
1449 if (__first2 == __last2)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001450 return make_pair(__first1, __first1); // Everything matches an empty sequence
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001451 while (true)
1452 {
1453 // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
1454 while (true)
1455 {
1456 if (__first1 == __last1) // return __last1 if no element matches *__first2
Marshall Clowf6d6b512016-03-08 15:12:52 +00001457 return make_pair(__last1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001458 if (__pred(*__first1, *__first2))
1459 break;
1460 ++__first1;
1461 }
1462 // *__first1 matches *__first2, now match elements after here
1463 _ForwardIterator1 __m1 = __first1;
1464 _ForwardIterator2 __m2 = __first2;
1465 while (true)
1466 {
1467 if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001468 return make_pair(__first1, __m1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001469 if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
Marshall Clowf6d6b512016-03-08 15:12:52 +00001470 return make_pair(__last1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001471 if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
1472 {
1473 ++__first1;
1474 break;
1475 } // else there is a match, check next elements
1476 }
1477 }
1478}
1479
1480template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Aditya Kumarfdb4f172016-08-25 11:52:38 +00001481_LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowf6d6b512016-03-08 15:12:52 +00001482pair<_RandomAccessIterator1, _RandomAccessIterator1>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001483__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
Marshall Clowf6d6b512016-03-08 15:12:52 +00001484 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001485 random_access_iterator_tag, random_access_iterator_tag)
1486{
Marshall Clowf6d6b512016-03-08 15:12:52 +00001487 typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
1488 typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001489 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
Marshall Clowf6d6b512016-03-08 15:12:52 +00001490 const _D2 __len2 = __last2 - __first2;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491 if (__len2 == 0)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001492 return make_pair(__first1, __first1);
1493 const _D1 __len1 = __last1 - __first1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001494 if (__len1 < __len2)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001495 return make_pair(__last1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001496 const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
1497 while (true)
1498 {
1499#if !_LIBCPP_UNROLL_LOOPS
1500 while (true)
1501 {
1502 if (__first1 == __s)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001503 return make_pair(__last1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001504 if (__pred(*__first1, *__first2))
1505 break;
1506 ++__first1;
1507 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001508#else // !_LIBCPP_UNROLL_LOOPS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001509 for (_D1 __loop_unroll = (__s - __first1) / 4; __loop_unroll > 0; --__loop_unroll)
1510 {
1511 if (__pred(*__first1, *__first2))
1512 goto __phase2;
1513 if (__pred(*++__first1, *__first2))
1514 goto __phase2;
1515 if (__pred(*++__first1, *__first2))
1516 goto __phase2;
1517 if (__pred(*++__first1, *__first2))
1518 goto __phase2;
1519 ++__first1;
1520 }
1521 switch (__s - __first1)
1522 {
1523 case 3:
1524 if (__pred(*__first1, *__first2))
1525 break;
1526 ++__first1;
1527 case 2:
1528 if (__pred(*__first1, *__first2))
1529 break;
1530 ++__first1;
1531 case 1:
1532 if (__pred(*__first1, *__first2))
1533 break;
1534 case 0:
Marshall Clowf6d6b512016-03-08 15:12:52 +00001535 return make_pair(__last1, __last1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001536 }
1537 __phase2:
Howard Hinnant324bb032010-08-22 00:02:43 +00001538#endif // !_LIBCPP_UNROLL_LOOPS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001539 _RandomAccessIterator1 __m1 = __first1;
1540 _RandomAccessIterator2 __m2 = __first2;
1541#if !_LIBCPP_UNROLL_LOOPS
1542 while (true)
1543 {
1544 if (++__m2 == __last2)
Marshall Clowf6d6b512016-03-08 15:12:52 +00001545 return make_pair(__first1, __first1 + __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001546 ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
1547 if (!__pred(*__m1, *__m2))
1548 {
1549 ++__first1;
1550 break;
1551 }
1552 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001553#else // !_LIBCPP_UNROLL_LOOPS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001554 ++__m2;
1555 ++__m1;
1556 for (_D2 __loop_unroll = (__last2 - __m2) / 4; __loop_unroll > 0; --__loop_unroll)
1557 {
1558 if (!__pred(*__m1, *__m2))
1559 goto __continue;
1560 if (!__pred(*++__m1, *++__m2))
1561 goto __continue;
1562 if (!__pred(*++__m1, *++__m2))
1563 goto __continue;
1564 if (!__pred(*++__m1, *++__m2))
1565 goto __continue;
1566 ++__m1;
1567 ++__m2;
1568 }
1569 switch (__last2 - __m2)
1570 {
1571 case 3:
1572 if (!__pred(*__m1, *__m2))
1573 break;
1574 ++__m1;
1575 ++__m2;
1576 case 2:
1577 if (!__pred(*__m1, *__m2))
1578 break;
1579 ++__m1;
1580 ++__m2;
1581 case 1:
1582 if (!__pred(*__m1, *__m2))
1583 break;
1584 case 0:
Marshall Clowf6d6b512016-03-08 15:12:52 +00001585 return make_pair(__first1, __first1 + __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001586 }
1587 __continue:
1588 ++__first1;
Howard Hinnant324bb032010-08-22 00:02:43 +00001589#endif // !_LIBCPP_UNROLL_LOOPS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001590 }
1591}
1592
1593template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
1594inline _LIBCPP_INLINE_VISIBILITY
1595_ForwardIterator1
1596search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1597 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1598{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001599 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001600 (__first1, __last1, __first2, __last2, __pred,
Marshall Clowf6d6b512016-03-08 15:12:52 +00001601 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1602 typename iterator_traits<_ForwardIterator2>::iterator_category())
1603 .first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001604}
1605
1606template <class _ForwardIterator1, class _ForwardIterator2>
1607inline _LIBCPP_INLINE_VISIBILITY
1608_ForwardIterator1
1609search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1610 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1611{
Marshall Clowf6d6b512016-03-08 15:12:52 +00001612 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1613 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001614 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001615}
1616
1617// search_n
1618
1619template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
1620_ForwardIterator
1621__search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001622 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001623{
1624 if (__count <= 0)
1625 return __first;
1626 while (true)
1627 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001628 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001629 while (true)
1630 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001631 if (__first == __last) // return __last if no element matches __value_
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001632 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001633 if (__pred(*__first, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001634 break;
1635 ++__first;
1636 }
Howard Hinnant78b68282011-10-22 20:59:45 +00001637 // *__first matches __value_, now match elements after here
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001638 _ForwardIterator __m = __first;
1639 _Size __c(0);
1640 while (true)
1641 {
1642 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1643 return __first;
1644 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1645 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001646 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001647 {
1648 __first = __m;
1649 ++__first;
1650 break;
1651 } // else there is a match, check next elements
1652 }
1653 }
1654}
1655
1656template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
1657_RandomAccessIterator
1658__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001659 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001660{
1661 if (__count <= 0)
1662 return __first;
1663 _Size __len = static_cast<_Size>(__last - __first);
1664 if (__len < __count)
1665 return __last;
1666 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1667 while (true)
1668 {
Howard Hinnant78b68282011-10-22 20:59:45 +00001669 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001670 while (true)
1671 {
Howard Hinnant128f7bf2013-04-04 15:40:48 +00001672 if (__first >= __s) // return __last if no element matches __value_
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001673 return __last;
Howard Hinnant78b68282011-10-22 20:59:45 +00001674 if (__pred(*__first, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001675 break;
1676 ++__first;
1677 }
Howard Hinnant78b68282011-10-22 20:59:45 +00001678 // *__first matches __value_, now match elements after here
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001679 _RandomAccessIterator __m = __first;
1680 _Size __c(0);
1681 while (true)
1682 {
1683 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1684 return __first;
1685 ++__m; // no need to check range on __m because __s guarantees we have enough source
Howard Hinnant78b68282011-10-22 20:59:45 +00001686 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001687 {
1688 __first = __m;
1689 ++__first;
1690 break;
1691 } // else there is a match, check next elements
1692 }
1693 }
1694}
1695
1696template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
1697inline _LIBCPP_INLINE_VISIBILITY
1698_ForwardIterator
1699search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant78b68282011-10-22 20:59:45 +00001700 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001701{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001702 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001703 (__first, __last, __convert_to_integral(__count), __value_, __pred,
1704 typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001705}
1706
1707template <class _ForwardIterator, class _Size, class _Tp>
1708inline _LIBCPP_INLINE_VISIBILITY
1709_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00001710search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001711{
1712 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001713 return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
1714 __value_, __equal_to<__v, _Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001715}
1716
1717// copy
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001718template <class _Iter>
1719inline _LIBCPP_INLINE_VISIBILITY
1720_Iter
1721__unwrap_iter(_Iter __i)
1722{
1723 return __i;
1724}
1725
1726template <class _Tp>
1727inline _LIBCPP_INLINE_VISIBILITY
1728typename enable_if
1729<
Howard Hinnant1468b662010-11-19 22:17:28 +00001730 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001731 _Tp*
1732>::type
1733__unwrap_iter(move_iterator<_Tp*> __i)
1734{
1735 return __i.base();
1736}
1737
Howard Hinnant499cea12013-08-23 17:37:05 +00001738#if _LIBCPP_DEBUG_LEVEL < 2
1739
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001740template <class _Tp>
1741inline _LIBCPP_INLINE_VISIBILITY
1742typename enable_if
1743<
Howard Hinnant1468b662010-11-19 22:17:28 +00001744 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001745 _Tp*
1746>::type
1747__unwrap_iter(__wrap_iter<_Tp*> __i)
1748{
1749 return __i.base();
1750}
1751
Howard Hinnant499cea12013-08-23 17:37:05 +00001752#endif // _LIBCPP_DEBUG_LEVEL < 2
1753
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001754template <class _InputIterator, class _OutputIterator>
1755inline _LIBCPP_INLINE_VISIBILITY
1756_OutputIterator
1757__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1758{
Eric Fiselierb9919752014-10-27 19:28:20 +00001759 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001760 *__result = *__first;
1761 return __result;
1762}
1763
1764template <class _Tp, class _Up>
1765inline _LIBCPP_INLINE_VISIBILITY
1766typename enable_if
1767<
1768 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001769 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001770 _Up*
1771>::type
1772__copy(_Tp* __first, _Tp* __last, _Up* __result)
1773{
1774 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001775 if (__n > 0)
1776 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001777 return __result + __n;
1778}
1779
1780template <class _InputIterator, class _OutputIterator>
1781inline _LIBCPP_INLINE_VISIBILITY
1782_OutputIterator
1783copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1784{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001785 return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001786}
1787
1788// copy_backward
1789
Howard Hinnantb73568d2013-02-06 21:03:39 +00001790template <class _BidirectionalIterator, class _OutputIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001791inline _LIBCPP_INLINE_VISIBILITY
1792_OutputIterator
Howard Hinnantb73568d2013-02-06 21:03:39 +00001793__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001794{
1795 while (__first != __last)
1796 *--__result = *--__last;
1797 return __result;
1798}
1799
1800template <class _Tp, class _Up>
1801inline _LIBCPP_INLINE_VISIBILITY
1802typename enable_if
1803<
1804 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001805 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001806 _Up*
1807>::type
1808__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1809{
1810 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001811 if (__n > 0)
1812 {
1813 __result -= __n;
1814 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1815 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001816 return __result;
1817}
1818
1819template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1820inline _LIBCPP_INLINE_VISIBILITY
1821_BidirectionalIterator2
1822copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1823 _BidirectionalIterator2 __result)
1824{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001825 return _VSTD::__copy_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001826}
1827
1828// copy_if
1829
1830template<class _InputIterator, class _OutputIterator, class _Predicate>
1831inline _LIBCPP_INLINE_VISIBILITY
1832_OutputIterator
1833copy_if(_InputIterator __first, _InputIterator __last,
1834 _OutputIterator __result, _Predicate __pred)
1835{
1836 for (; __first != __last; ++__first)
1837 {
1838 if (__pred(*__first))
1839 {
1840 *__result = *__first;
1841 ++__result;
1842 }
1843 }
1844 return __result;
1845}
1846
1847// copy_n
1848
1849template<class _InputIterator, class _Size, class _OutputIterator>
1850inline _LIBCPP_INLINE_VISIBILITY
1851typename enable_if
1852<
1853 __is_input_iterator<_InputIterator>::value &&
1854 !__is_random_access_iterator<_InputIterator>::value,
1855 _OutputIterator
1856>::type
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001857copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001858{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001859 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1860 _IntegralSize __n = __orig_n;
Howard Hinnant171869e2011-02-27 20:55:39 +00001861 if (__n > 0)
1862 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001863 *__result = *__first;
Howard Hinnant171869e2011-02-27 20:55:39 +00001864 ++__result;
1865 for (--__n; __n > 0; --__n)
1866 {
1867 ++__first;
1868 *__result = *__first;
1869 ++__result;
1870 }
1871 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001872 return __result;
1873}
1874
1875template<class _InputIterator, class _Size, class _OutputIterator>
1876inline _LIBCPP_INLINE_VISIBILITY
1877typename enable_if
1878<
1879 __is_random_access_iterator<_InputIterator>::value,
1880 _OutputIterator
1881>::type
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001882copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001883{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00001884 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1885 _IntegralSize __n = __orig_n;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001886 return _VSTD::copy(__first, __first + __n, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001887}
1888
1889// move
1890
1891template <class _InputIterator, class _OutputIterator>
1892inline _LIBCPP_INLINE_VISIBILITY
1893_OutputIterator
1894__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1895{
Eric Fiselierb9919752014-10-27 19:28:20 +00001896 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001897 *__result = _VSTD::move(*__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001898 return __result;
1899}
1900
1901template <class _Tp, class _Up>
1902inline _LIBCPP_INLINE_VISIBILITY
1903typename enable_if
1904<
1905 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001906 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001907 _Up*
1908>::type
1909__move(_Tp* __first, _Tp* __last, _Up* __result)
1910{
1911 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001912 if (__n > 0)
1913 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001914 return __result + __n;
1915}
1916
1917template <class _InputIterator, class _OutputIterator>
1918inline _LIBCPP_INLINE_VISIBILITY
1919_OutputIterator
1920move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1921{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001922 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001923}
1924
1925// move_backward
1926
1927template <class _InputIterator, class _OutputIterator>
1928inline _LIBCPP_INLINE_VISIBILITY
1929_OutputIterator
1930__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1931{
1932 while (__first != __last)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001933 *--__result = _VSTD::move(*--__last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001934 return __result;
1935}
1936
1937template <class _Tp, class _Up>
1938inline _LIBCPP_INLINE_VISIBILITY
1939typename enable_if
1940<
1941 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnant1468b662010-11-19 22:17:28 +00001942 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001943 _Up*
1944>::type
1945__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1946{
1947 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow708b86b2015-06-02 13:52:16 +00001948 if (__n > 0)
1949 {
1950 __result -= __n;
1951 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1952 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001953 return __result;
1954}
1955
1956template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1957inline _LIBCPP_INLINE_VISIBILITY
1958_BidirectionalIterator2
1959move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1960 _BidirectionalIterator2 __result)
1961{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001962 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001963}
1964
1965// iter_swap
1966
Howard Hinnante9b2c2d2011-05-27 15:04:19 +00001967// moved to <type_traits> for better swap / noexcept support
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001968
1969// transform
1970
1971template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
1972inline _LIBCPP_INLINE_VISIBILITY
1973_OutputIterator
1974transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1975{
Eric Fiselierb9919752014-10-27 19:28:20 +00001976 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001977 *__result = __op(*__first);
1978 return __result;
1979}
1980
1981template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
1982inline _LIBCPP_INLINE_VISIBILITY
1983_OutputIterator
1984transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1985 _OutputIterator __result, _BinaryOperation __binary_op)
1986{
Eric Fiselierb9919752014-10-27 19:28:20 +00001987 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001988 *__result = __binary_op(*__first1, *__first2);
1989 return __result;
1990}
1991
1992// replace
1993
1994template <class _ForwardIterator, class _Tp>
1995inline _LIBCPP_INLINE_VISIBILITY
1996void
1997replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1998{
1999 for (; __first != __last; ++__first)
2000 if (*__first == __old_value)
2001 *__first = __new_value;
2002}
2003
2004// replace_if
2005
2006template <class _ForwardIterator, class _Predicate, class _Tp>
2007inline _LIBCPP_INLINE_VISIBILITY
2008void
2009replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
2010{
2011 for (; __first != __last; ++__first)
2012 if (__pred(*__first))
2013 *__first = __new_value;
2014}
2015
2016// replace_copy
2017
2018template <class _InputIterator, class _OutputIterator, class _Tp>
2019inline _LIBCPP_INLINE_VISIBILITY
2020_OutputIterator
2021replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2022 const _Tp& __old_value, const _Tp& __new_value)
2023{
Eric Fiselierb9919752014-10-27 19:28:20 +00002024 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002025 if (*__first == __old_value)
2026 *__result = __new_value;
2027 else
2028 *__result = *__first;
2029 return __result;
2030}
2031
2032// replace_copy_if
2033
2034template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
2035inline _LIBCPP_INLINE_VISIBILITY
2036_OutputIterator
2037replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2038 _Predicate __pred, const _Tp& __new_value)
2039{
Eric Fiselierb9919752014-10-27 19:28:20 +00002040 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002041 if (__pred(*__first))
2042 *__result = __new_value;
2043 else
2044 *__result = *__first;
2045 return __result;
2046}
2047
2048// fill_n
2049
2050template <class _OutputIterator, class _Size, class _Tp>
2051inline _LIBCPP_INLINE_VISIBILITY
2052_OutputIterator
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002053__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002054{
Eric Fiselierb9919752014-10-27 19:28:20 +00002055 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnant78b68282011-10-22 20:59:45 +00002056 *__first = __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002057 return __first;
2058}
2059
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002060template <class _Tp, class _Size, class _Up>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002061inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant56dcf0b2013-08-01 17:29:28 +00002062typename enable_if
2063<
2064 is_integral<_Tp>::value && sizeof(_Tp) == 1 &&
2065 !is_same<_Tp, bool>::value &&
2066 is_integral<_Up>::value && sizeof(_Up) == 1,
2067 _Tp*
2068>::type
2069__fill_n(_Tp* __first, _Size __n,_Up __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070{
2071 if (__n > 0)
Howard Hinnant78b68282011-10-22 20:59:45 +00002072 _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002073 return __first + __n;
2074}
2075
2076template <class _OutputIterator, class _Size, class _Tp>
2077inline _LIBCPP_INLINE_VISIBILITY
2078_OutputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002079fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002080{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002081 return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002082}
2083
2084// fill
2085
2086template <class _ForwardIterator, class _Tp>
2087inline _LIBCPP_INLINE_VISIBILITY
2088void
Howard Hinnant78b68282011-10-22 20:59:45 +00002089__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002090{
2091 for (; __first != __last; ++__first)
Howard Hinnant78b68282011-10-22 20:59:45 +00002092 *__first = __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002093}
2094
2095template <class _RandomAccessIterator, class _Tp>
2096inline _LIBCPP_INLINE_VISIBILITY
2097void
Howard Hinnant78b68282011-10-22 20:59:45 +00002098__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002099{
Howard Hinnant78b68282011-10-22 20:59:45 +00002100 _VSTD::fill_n(__first, __last - __first, __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002101}
2102
2103template <class _ForwardIterator, class _Tp>
2104inline _LIBCPP_INLINE_VISIBILITY
2105void
Howard Hinnant78b68282011-10-22 20:59:45 +00002106fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002107{
Howard Hinnant78b68282011-10-22 20:59:45 +00002108 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002109}
2110
2111// generate
2112
2113template <class _ForwardIterator, class _Generator>
2114inline _LIBCPP_INLINE_VISIBILITY
2115void
2116generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2117{
2118 for (; __first != __last; ++__first)
2119 *__first = __gen();
2120}
2121
2122// generate_n
2123
2124template <class _OutputIterator, class _Size, class _Generator>
2125inline _LIBCPP_INLINE_VISIBILITY
2126_OutputIterator
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002127generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002128{
Eric Fiselier31cb7fe2015-02-10 16:46:42 +00002129 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
2130 _IntegralSize __n = __orig_n;
Eric Fiselierb9919752014-10-27 19:28:20 +00002131 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002132 *__first = __gen();
2133 return __first;
2134}
2135
2136// remove
2137
2138template <class _ForwardIterator, class _Tp>
2139_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002140remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002141{
Howard Hinnant78b68282011-10-22 20:59:45 +00002142 __first = _VSTD::find(__first, __last, __value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002143 if (__first != __last)
2144 {
2145 _ForwardIterator __i = __first;
2146 while (++__i != __last)
2147 {
Howard Hinnant78b68282011-10-22 20:59:45 +00002148 if (!(*__i == __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002149 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002150 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002151 ++__first;
2152 }
2153 }
2154 }
2155 return __first;
2156}
2157
2158// remove_if
2159
2160template <class _ForwardIterator, class _Predicate>
2161_ForwardIterator
2162remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2163{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002164 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002165 (__first, __last, __pred);
2166 if (__first != __last)
2167 {
2168 _ForwardIterator __i = __first;
2169 while (++__i != __last)
2170 {
2171 if (!__pred(*__i))
2172 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002173 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002174 ++__first;
2175 }
2176 }
2177 }
2178 return __first;
2179}
2180
2181// remove_copy
2182
2183template <class _InputIterator, class _OutputIterator, class _Tp>
2184inline _LIBCPP_INLINE_VISIBILITY
2185_OutputIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00002186remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002187{
2188 for (; __first != __last; ++__first)
2189 {
Howard Hinnant78b68282011-10-22 20:59:45 +00002190 if (!(*__first == __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002191 {
2192 *__result = *__first;
2193 ++__result;
2194 }
2195 }
2196 return __result;
2197}
2198
2199// remove_copy_if
2200
2201template <class _InputIterator, class _OutputIterator, class _Predicate>
2202inline _LIBCPP_INLINE_VISIBILITY
2203_OutputIterator
2204remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2205{
2206 for (; __first != __last; ++__first)
2207 {
2208 if (!__pred(*__first))
2209 {
2210 *__result = *__first;
2211 ++__result;
2212 }
2213 }
2214 return __result;
2215}
2216
2217// unique
2218
2219template <class _ForwardIterator, class _BinaryPredicate>
2220_ForwardIterator
2221unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2222{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002223 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002224 (__first, __last, __pred);
2225 if (__first != __last)
2226 {
2227 // ... a a ? ...
2228 // f i
2229 _ForwardIterator __i = __first;
2230 for (++__i; ++__i != __last;)
2231 if (!__pred(*__first, *__i))
Howard Hinnant0949eed2011-06-30 21:18:19 +00002232 *++__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002233 ++__first;
2234 }
2235 return __first;
2236}
2237
2238template <class _ForwardIterator>
2239inline _LIBCPP_INLINE_VISIBILITY
2240_ForwardIterator
2241unique(_ForwardIterator __first, _ForwardIterator __last)
2242{
2243 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002244 return _VSTD::unique(__first, __last, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002245}
2246
2247// unique_copy
2248
2249template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
2250_OutputIterator
2251__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2252 input_iterator_tag, output_iterator_tag)
2253{
2254 if (__first != __last)
2255 {
2256 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2257 *__result = __t;
2258 ++__result;
2259 while (++__first != __last)
2260 {
2261 if (!__pred(__t, *__first))
2262 {
2263 __t = *__first;
2264 *__result = __t;
2265 ++__result;
2266 }
2267 }
2268 }
2269 return __result;
2270}
2271
2272template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
2273_OutputIterator
2274__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2275 forward_iterator_tag, output_iterator_tag)
2276{
2277 if (__first != __last)
2278 {
2279 _ForwardIterator __i = __first;
2280 *__result = *__i;
2281 ++__result;
2282 while (++__first != __last)
2283 {
2284 if (!__pred(*__i, *__first))
2285 {
2286 *__result = *__first;
2287 ++__result;
2288 __i = __first;
2289 }
2290 }
2291 }
2292 return __result;
2293}
2294
2295template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
2296_ForwardIterator
2297__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2298 input_iterator_tag, forward_iterator_tag)
2299{
2300 if (__first != __last)
2301 {
2302 *__result = *__first;
2303 while (++__first != __last)
2304 if (!__pred(*__result, *__first))
2305 *++__result = *__first;
2306 ++__result;
2307 }
2308 return __result;
2309}
2310
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002311template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
2312inline _LIBCPP_INLINE_VISIBILITY
2313_OutputIterator
2314unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2315{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002316 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002317 (__first, __last, __result, __pred,
2318 typename iterator_traits<_InputIterator>::iterator_category(),
2319 typename iterator_traits<_OutputIterator>::iterator_category());
2320}
2321
2322template <class _InputIterator, class _OutputIterator>
2323inline _LIBCPP_INLINE_VISIBILITY
2324_OutputIterator
2325unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2326{
2327 typedef typename iterator_traits<_InputIterator>::value_type __v;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002328 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002329}
2330
2331// reverse
2332
2333template <class _BidirectionalIterator>
2334inline _LIBCPP_INLINE_VISIBILITY
2335void
2336__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2337{
2338 while (__first != __last)
2339 {
2340 if (__first == --__last)
2341 break;
Marshall Clowc010bd62015-11-02 21:34:25 +00002342 _VSTD::iter_swap(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002343 ++__first;
2344 }
2345}
2346
2347template <class _RandomAccessIterator>
2348inline _LIBCPP_INLINE_VISIBILITY
2349void
2350__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2351{
2352 if (__first != __last)
2353 for (; __first < --__last; ++__first)
Marshall Clowc010bd62015-11-02 21:34:25 +00002354 _VSTD::iter_swap(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002355}
2356
2357template <class _BidirectionalIterator>
2358inline _LIBCPP_INLINE_VISIBILITY
2359void
2360reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2361{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002362 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002363}
2364
2365// reverse_copy
2366
2367template <class _BidirectionalIterator, class _OutputIterator>
2368inline _LIBCPP_INLINE_VISIBILITY
2369_OutputIterator
2370reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2371{
2372 for (; __first != __last; ++__result)
2373 *__result = *--__last;
2374 return __result;
2375}
2376
2377// rotate
2378
2379template <class _ForwardIterator>
2380_ForwardIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002381__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002382{
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002383 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2384 value_type __tmp = _VSTD::move(*__first);
2385 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2386 *__lm1 = _VSTD::move(__tmp);
2387 return __lm1;
2388}
2389
2390template <class _BidirectionalIterator>
2391_BidirectionalIterator
2392__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2393{
2394 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2395 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2396 value_type __tmp = _VSTD::move(*__lm1);
2397 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2398 *__first = _VSTD::move(__tmp);
2399 return __fp1;
2400}
2401
2402template <class _ForwardIterator>
2403_ForwardIterator
2404__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2405{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002406 _ForwardIterator __i = __middle;
2407 while (true)
2408 {
2409 swap(*__first, *__i);
2410 ++__first;
2411 if (++__i == __last)
2412 break;
2413 if (__first == __middle)
2414 __middle = __i;
2415 }
2416 _ForwardIterator __r = __first;
2417 if (__first != __middle)
2418 {
2419 __i = __middle;
2420 while (true)
2421 {
2422 swap(*__first, *__i);
2423 ++__first;
2424 if (++__i == __last)
2425 {
2426 if (__first == __middle)
2427 break;
2428 __i = __middle;
2429 }
2430 else if (__first == __middle)
2431 __middle = __i;
2432 }
2433 }
2434 return __r;
2435}
2436
2437template<typename _Integral>
2438inline _LIBCPP_INLINE_VISIBILITY
2439_Integral
Marshall Clow1c1e91d2016-07-26 14:29:45 +00002440__algo_gcd(_Integral __x, _Integral __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002441{
2442 do
2443 {
2444 _Integral __t = __x % __y;
2445 __x = __y;
2446 __y = __t;
2447 } while (__y);
2448 return __x;
2449}
2450
2451template<typename _RandomAccessIterator>
2452_RandomAccessIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002453__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002454{
2455 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2456 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant324bb032010-08-22 00:02:43 +00002457
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002458 const difference_type __m1 = __middle - __first;
2459 const difference_type __m2 = __last - __middle;
2460 if (__m1 == __m2)
2461 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002462 _VSTD::swap_ranges(__first, __middle, __middle);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002463 return __middle;
2464 }
Marshall Clow1c1e91d2016-07-26 14:29:45 +00002465 const difference_type __g = _VSTD::__algo_gcd(__m1, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002466 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2467 {
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002468 value_type __t(_VSTD::move(*--__p));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002469 _RandomAccessIterator __p1 = __p;
2470 _RandomAccessIterator __p2 = __p1 + __m1;
2471 do
2472 {
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002473 *__p1 = _VSTD::move(*__p2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002474 __p1 = __p2;
2475 const difference_type __d = __last - __p2;
2476 if (__m1 < __d)
2477 __p2 += __m1;
2478 else
2479 __p2 = __first + (__m1 - __d);
2480 } while (__p2 != __p);
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002481 *__p1 = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002482 }
2483 return __first + __m2;
2484}
2485
2486template <class _ForwardIterator>
2487inline _LIBCPP_INLINE_VISIBILITY
2488_ForwardIterator
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002489__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2490 _VSTD::forward_iterator_tag)
2491{
2492 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2493 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2494 {
2495 if (_VSTD::next(__first) == __middle)
2496 return _VSTD::__rotate_left(__first, __last);
2497 }
2498 return _VSTD::__rotate_forward(__first, __middle, __last);
2499}
2500
2501template <class _BidirectionalIterator>
2502inline _LIBCPP_INLINE_VISIBILITY
2503_BidirectionalIterator
2504__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2505 _VSTD::bidirectional_iterator_tag)
2506{
2507 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2508 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2509 {
2510 if (_VSTD::next(__first) == __middle)
2511 return _VSTD::__rotate_left(__first, __last);
2512 if (_VSTD::next(__middle) == __last)
2513 return _VSTD::__rotate_right(__first, __last);
2514 }
2515 return _VSTD::__rotate_forward(__first, __middle, __last);
2516}
2517
2518template <class _RandomAccessIterator>
2519inline _LIBCPP_INLINE_VISIBILITY
2520_RandomAccessIterator
2521__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2522 _VSTD::random_access_iterator_tag)
2523{
2524 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2525 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2526 {
2527 if (_VSTD::next(__first) == __middle)
2528 return _VSTD::__rotate_left(__first, __last);
2529 if (_VSTD::next(__middle) == __last)
2530 return _VSTD::__rotate_right(__first, __last);
2531 return _VSTD::__rotate_gcd(__first, __middle, __last);
2532 }
2533 return _VSTD::__rotate_forward(__first, __middle, __last);
2534}
2535
2536template <class _ForwardIterator>
2537inline _LIBCPP_INLINE_VISIBILITY
2538_ForwardIterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002539rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2540{
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002541 if (__first == __middle)
2542 return __last;
2543 if (__middle == __last)
2544 return __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00002545 return _VSTD::__rotate(__first, __middle, __last,
Howard Hinnant4b2f4202012-08-03 18:01:20 +00002546 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002547}
2548
2549// rotate_copy
2550
2551template <class _ForwardIterator, class _OutputIterator>
2552inline _LIBCPP_INLINE_VISIBILITY
2553_OutputIterator
2554rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2555{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002556 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002557}
2558
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002559// min_element
2560
2561template <class _ForwardIterator, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002562inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002563_ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +00002564min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002565{
2566 if (__first != __last)
2567 {
2568 _ForwardIterator __i = __first;
2569 while (++__i != __last)
2570 if (__comp(*__i, *__first))
2571 __first = __i;
2572 }
2573 return __first;
2574}
2575
2576template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002577inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002578_ForwardIterator
2579min_element(_ForwardIterator __first, _ForwardIterator __last)
2580{
Marshall Clow928735a2015-05-10 13:53:31 +00002581 return _VSTD::min_element(__first, __last,
Howard Hinnant98e5d972010-08-21 20:10:01 +00002582 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2583}
2584
2585// min
2586
2587template <class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002588inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002589const _Tp&
2590min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2591{
2592 return __comp(__b, __a) ? __b : __a;
2593}
2594
2595template <class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002596inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002597const _Tp&
2598min(const _Tp& __a, const _Tp& __b)
2599{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002600 return _VSTD::min(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002601}
2602
Howard Hinnante3e32912011-08-12 21:56:02 +00002603#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2604
Howard Hinnant98e5d972010-08-21 20:10:01 +00002605template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002606inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002607_Tp
2608min(initializer_list<_Tp> __t, _Compare __comp)
2609{
Marshall Clow928735a2015-05-10 13:53:31 +00002610 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002611}
2612
2613template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002614inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002615_Tp
2616min(initializer_list<_Tp> __t)
2617{
Marshall Clow928735a2015-05-10 13:53:31 +00002618 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002619}
2620
Howard Hinnante3e32912011-08-12 21:56:02 +00002621#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2622
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002623// max_element
2624
2625template <class _ForwardIterator, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002626inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002627_ForwardIterator
Marshall Clow928735a2015-05-10 13:53:31 +00002628max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002629{
2630 if (__first != __last)
2631 {
2632 _ForwardIterator __i = __first;
2633 while (++__i != __last)
2634 if (__comp(*__first, *__i))
2635 __first = __i;
2636 }
2637 return __first;
2638}
2639
Marshall Clow9d9463a2014-02-19 16:51:35 +00002640
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002641template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002642inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002643_ForwardIterator
2644max_element(_ForwardIterator __first, _ForwardIterator __last)
2645{
Marshall Clow928735a2015-05-10 13:53:31 +00002646 return _VSTD::max_element(__first, __last,
Howard Hinnant98e5d972010-08-21 20:10:01 +00002647 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2648}
2649
2650// max
2651
2652template <class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002653inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002654const _Tp&
2655max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2656{
2657 return __comp(__a, __b) ? __b : __a;
2658}
2659
2660template <class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002661inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002662const _Tp&
2663max(const _Tp& __a, const _Tp& __b)
2664{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002665 return _VSTD::max(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002666}
2667
Howard Hinnante3e32912011-08-12 21:56:02 +00002668#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2669
Howard Hinnant98e5d972010-08-21 20:10:01 +00002670template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002671inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002672_Tp
2673max(initializer_list<_Tp> __t, _Compare __comp)
2674{
Marshall Clow928735a2015-05-10 13:53:31 +00002675 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
Howard Hinnant98e5d972010-08-21 20:10:01 +00002676}
2677
2678template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002679inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002680_Tp
2681max(initializer_list<_Tp> __t)
2682{
Marshall Clow928735a2015-05-10 13:53:31 +00002683 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002684}
2685
Howard Hinnante3e32912011-08-12 21:56:02 +00002686#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2687
Marshall Clow3e0808e2016-03-07 22:43:49 +00002688#if _LIBCPP_STD_VER > 14
2689// clamp
2690template<class _Tp, class _Compare>
2691inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2692const _Tp&
2693clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
2694{
2695 _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
2696 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
2697
2698}
2699
2700template<class _Tp>
2701inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2702const _Tp&
2703clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
2704{
2705 return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
2706}
2707#endif
2708
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002709// minmax_element
2710
2711template <class _ForwardIterator, class _Compare>
Marshall Clow928735a2015-05-10 13:53:31 +00002712_LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002713std::pair<_ForwardIterator, _ForwardIterator>
2714minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2715{
2716 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2717 if (__first != __last)
2718 {
2719 if (++__first != __last)
2720 {
2721 if (__comp(*__first, *__result.first))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002722 __result.first = __first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002723 else
2724 __result.second = __first;
2725 while (++__first != __last)
2726 {
2727 _ForwardIterator __i = __first;
2728 if (++__first == __last)
2729 {
2730 if (__comp(*__i, *__result.first))
2731 __result.first = __i;
2732 else if (!__comp(*__i, *__result.second))
2733 __result.second = __i;
2734 break;
2735 }
2736 else
2737 {
2738 if (__comp(*__first, *__i))
2739 {
2740 if (__comp(*__first, *__result.first))
2741 __result.first = __first;
2742 if (!__comp(*__i, *__result.second))
2743 __result.second = __i;
2744 }
2745 else
2746 {
2747 if (__comp(*__i, *__result.first))
2748 __result.first = __i;
2749 if (!__comp(*__first, *__result.second))
2750 __result.second = __first;
2751 }
2752 }
2753 }
2754 }
2755 }
2756 return __result;
2757}
2758
2759template <class _ForwardIterator>
Marshall Clow928735a2015-05-10 13:53:31 +00002760inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002761std::pair<_ForwardIterator, _ForwardIterator>
2762minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2763{
Marshall Clow9d9463a2014-02-19 16:51:35 +00002764 return _VSTD::minmax_element(__first, __last,
2765 __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002766}
2767
Howard Hinnant98e5d972010-08-21 20:10:01 +00002768// minmax
2769
2770template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002771inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002772pair<const _Tp&, const _Tp&>
2773minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2774{
2775 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2776 pair<const _Tp&, const _Tp&>(__a, __b);
2777}
2778
2779template<class _Tp>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002780inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002781pair<const _Tp&, const _Tp&>
2782minmax(const _Tp& __a, const _Tp& __b)
2783{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002784 return _VSTD::minmax(__a, __b, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002785}
2786
Howard Hinnante3e32912011-08-12 21:56:02 +00002787#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2788
Howard Hinnant98e5d972010-08-21 20:10:01 +00002789template<class _Tp, class _Compare>
Marshall Clow9d9463a2014-02-19 16:51:35 +00002790inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant98e5d972010-08-21 20:10:01 +00002791pair<_Tp, _Tp>
2792minmax(initializer_list<_Tp> __t, _Compare __comp)
2793{
Marshall Clow9d9463a2014-02-19 16:51:35 +00002794 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2795 _Iter __first = __t.begin();
2796 _Iter __last = __t.end();
Marshall Clow3024f862015-02-11 15:41:34 +00002797 std::pair<_Tp, _Tp> __result(*__first, *__first);
Marshall Clow9d9463a2014-02-19 16:51:35 +00002798
2799 ++__first;
2800 if (__t.size() % 2 == 0)
2801 {
2802 if (__comp(*__first, __result.first))
2803 __result.first = *__first;
2804 else
2805 __result.second = *__first;
2806 ++__first;
2807 }
Aditya Kumarfdb4f172016-08-25 11:52:38 +00002808
Marshall Clow9d9463a2014-02-19 16:51:35 +00002809 while (__first != __last)
2810 {
2811 _Tp __prev = *__first++;
Marshall Clow3024f862015-02-11 15:41:34 +00002812 if (__comp(*__first, __prev)) {
2813 if ( __comp(*__first, __result.first)) __result.first = *__first;
2814 if (!__comp(__prev, __result.second)) __result.second = __prev;
Marshall Clow9d9463a2014-02-19 16:51:35 +00002815 }
2816 else {
Marshall Clow3024f862015-02-11 15:41:34 +00002817 if ( __comp(__prev, __result.first)) __result.first = __prev;
2818 if (!__comp(*__first, __result.second)) __result.second = *__first;
Marshall Clow9d9463a2014-02-19 16:51:35 +00002819 }
Aditya Kumarfdb4f172016-08-25 11:52:38 +00002820
Marshall Clow9d9463a2014-02-19 16:51:35 +00002821 __first++;
2822 }
2823 return __result;
2824}
2825
2826template<class _Tp>
2827inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2828pair<_Tp, _Tp>
2829minmax(initializer_list<_Tp> __t)
2830{
2831 return _VSTD::minmax(__t, __less<_Tp>());
Howard Hinnant98e5d972010-08-21 20:10:01 +00002832}
2833
Howard Hinnante3e32912011-08-12 21:56:02 +00002834#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2835
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002836// random_shuffle
2837
Howard Hinnantc3267212010-05-26 17:49:34 +00002838// __independent_bits_engine
2839
Howard Hinnant99968442011-11-29 18:15:50 +00002840template <unsigned long long _Xp, size_t _Rp>
Howard Hinnantc3267212010-05-26 17:49:34 +00002841struct __log2_imp
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002842{
Howard Hinnant99968442011-11-29 18:15:50 +00002843 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2844 : __log2_imp<_Xp, _Rp - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002845};
2846
Howard Hinnant99968442011-11-29 18:15:50 +00002847template <unsigned long long _Xp>
2848struct __log2_imp<_Xp, 0>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002849{
Howard Hinnantc3267212010-05-26 17:49:34 +00002850 static const size_t value = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002851};
2852
Howard Hinnant99968442011-11-29 18:15:50 +00002853template <size_t _Rp>
2854struct __log2_imp<0, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002855{
Howard Hinnant99968442011-11-29 18:15:50 +00002856 static const size_t value = _Rp + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002857};
2858
Howard Hinnant99968442011-11-29 18:15:50 +00002859template <class _UI, _UI _Xp>
Howard Hinnantc3267212010-05-26 17:49:34 +00002860struct __log2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002861{
Howard Hinnant99968442011-11-29 18:15:50 +00002862 static const size_t value = __log2_imp<_Xp,
Howard Hinnantc3267212010-05-26 17:49:34 +00002863 sizeof(_UI) * __CHAR_BIT__ - 1>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002864};
2865
Howard Hinnantc3267212010-05-26 17:49:34 +00002866template<class _Engine, class _UIntType>
2867class __independent_bits_engine
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002868{
Howard Hinnantc3267212010-05-26 17:49:34 +00002869public:
2870 // types
2871 typedef _UIntType result_type;
2872
2873private:
2874 typedef typename _Engine::result_type _Engine_result_type;
2875 typedef typename conditional
2876 <
2877 sizeof(_Engine_result_type) <= sizeof(result_type),
2878 result_type,
2879 _Engine_result_type
2880 >::type _Working_result_type;
2881
2882 _Engine& __e_;
2883 size_t __w_;
2884 size_t __w0_;
2885 size_t __n_;
2886 size_t __n0_;
2887 _Working_result_type __y0_;
2888 _Working_result_type __y1_;
2889 _Engine_result_type __mask0_;
2890 _Engine_result_type __mask1_;
2891
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002892#ifdef _LIBCPP_HAS_NO_CONSTEXPR
Howard Hinnant99968442011-11-29 18:15:50 +00002893 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant8efd3da2012-04-02 21:00:45 +00002894 + _Working_result_type(1);
2895#else
2896 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2897 + _Working_result_type(1);
2898#endif
2899 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2900 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2901 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
Howard Hinnantc3267212010-05-26 17:49:34 +00002902
2903public:
2904 // constructors and seeding functions
2905 __independent_bits_engine(_Engine& __e, size_t __w);
2906
2907 // generating functions
Howard Hinnant99968442011-11-29 18:15:50 +00002908 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantc3267212010-05-26 17:49:34 +00002909
2910private:
2911 result_type __eval(false_type);
2912 result_type __eval(true_type);
2913};
2914
2915template<class _Engine, class _UIntType>
2916__independent_bits_engine<_Engine, _UIntType>
2917 ::__independent_bits_engine(_Engine& __e, size_t __w)
2918 : __e_(__e),
2919 __w_(__w)
2920{
2921 __n_ = __w_ / __m + (__w_ % __m != 0);
2922 __w0_ = __w_ / __n_;
Howard Hinnant99968442011-11-29 18:15:50 +00002923 if (_Rp == 0)
2924 __y0_ = _Rp;
Howard Hinnantc3267212010-05-26 17:49:34 +00002925 else if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00002926 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002927 else
2928 __y0_ = 0;
Howard Hinnant99968442011-11-29 18:15:50 +00002929 if (_Rp - __y0_ > __y0_ / __n_)
Howard Hinnantc3267212010-05-26 17:49:34 +00002930 {
2931 ++__n_;
2932 __w0_ = __w_ / __n_;
2933 if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00002934 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002935 else
2936 __y0_ = 0;
2937 }
2938 __n0_ = __n_ - __w_ % __n_;
2939 if (__w0_ < _WDt - 1)
Howard Hinnant99968442011-11-29 18:15:50 +00002940 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
Howard Hinnantc3267212010-05-26 17:49:34 +00002941 else
2942 __y1_ = 0;
2943 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2944 _Engine_result_type(0);
2945 __mask1_ = __w0_ < _EDt - 1 ?
2946 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2947 _Engine_result_type(~0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002948}
2949
Howard Hinnantc3267212010-05-26 17:49:34 +00002950template<class _Engine, class _UIntType>
2951inline
2952_UIntType
2953__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002954{
Howard Hinnantc3267212010-05-26 17:49:34 +00002955 return static_cast<result_type>(__e_() & __mask0_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002956}
2957
Howard Hinnantc3267212010-05-26 17:49:34 +00002958template<class _Engine, class _UIntType>
2959_UIntType
2960__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002961{
Howard Hinnant99968442011-11-29 18:15:50 +00002962 result_type _Sp = 0;
Howard Hinnantc3267212010-05-26 17:49:34 +00002963 for (size_t __k = 0; __k < __n0_; ++__k)
2964 {
2965 _Engine_result_type __u;
2966 do
2967 {
2968 __u = __e_() - _Engine::min();
2969 } while (__u >= __y0_);
Howard Hinnant8faa95f2011-10-27 16:12:10 +00002970 if (__w0_ < _WDt)
Howard Hinnant99968442011-11-29 18:15:50 +00002971 _Sp <<= __w0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002972 else
Howard Hinnant99968442011-11-29 18:15:50 +00002973 _Sp = 0;
2974 _Sp += __u & __mask0_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002975 }
2976 for (size_t __k = __n0_; __k < __n_; ++__k)
2977 {
2978 _Engine_result_type __u;
2979 do
2980 {
2981 __u = __e_() - _Engine::min();
2982 } while (__u >= __y1_);
Howard Hinnant8faa95f2011-10-27 16:12:10 +00002983 if (__w0_ < _WDt - 1)
Howard Hinnant99968442011-11-29 18:15:50 +00002984 _Sp <<= __w0_ + 1;
Howard Hinnantc3267212010-05-26 17:49:34 +00002985 else
Howard Hinnant99968442011-11-29 18:15:50 +00002986 _Sp = 0;
2987 _Sp += __u & __mask1_;
Howard Hinnantc3267212010-05-26 17:49:34 +00002988 }
Howard Hinnant99968442011-11-29 18:15:50 +00002989 return _Sp;
Howard Hinnantc3267212010-05-26 17:49:34 +00002990}
2991
2992// uniform_int_distribution
2993
2994template<class _IntType = int>
2995class uniform_int_distribution
2996{
2997public:
2998 // types
2999 typedef _IntType result_type;
3000
3001 class param_type
3002 {
3003 result_type __a_;
3004 result_type __b_;
3005 public:
3006 typedef uniform_int_distribution distribution_type;
3007
3008 explicit param_type(result_type __a = 0,
3009 result_type __b = numeric_limits<result_type>::max())
3010 : __a_(__a), __b_(__b) {}
3011
3012 result_type a() const {return __a_;}
3013 result_type b() const {return __b_;}
3014
3015 friend bool operator==(const param_type& __x, const param_type& __y)
3016 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3017 friend bool operator!=(const param_type& __x, const param_type& __y)
3018 {return !(__x == __y);}
3019 };
3020
3021private:
3022 param_type __p_;
3023
3024public:
3025 // constructors and reset functions
3026 explicit uniform_int_distribution(result_type __a = 0,
3027 result_type __b = numeric_limits<result_type>::max())
3028 : __p_(param_type(__a, __b)) {}
3029 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
3030 void reset() {}
3031
3032 // generating functions
3033 template<class _URNG> result_type operator()(_URNG& __g)
3034 {return (*this)(__g, __p_);}
3035 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3036
3037 // property functions
3038 result_type a() const {return __p_.a();}
3039 result_type b() const {return __p_.b();}
3040
3041 param_type param() const {return __p_;}
3042 void param(const param_type& __p) {__p_ = __p;}
3043
3044 result_type min() const {return a();}
3045 result_type max() const {return b();}
3046
3047 friend bool operator==(const uniform_int_distribution& __x,
3048 const uniform_int_distribution& __y)
3049 {return __x.__p_ == __y.__p_;}
3050 friend bool operator!=(const uniform_int_distribution& __x,
3051 const uniform_int_distribution& __y)
3052 {return !(__x == __y);}
3053};
3054
3055template<class _IntType>
3056template<class _URNG>
3057typename uniform_int_distribution<_IntType>::result_type
3058uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
3059{
3060 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3061 uint32_t, uint64_t>::type _UIntType;
Howard Hinnant99968442011-11-29 18:15:50 +00003062 const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
3063 if (_Rp == 1)
Howard Hinnantc3267212010-05-26 17:49:34 +00003064 return __p.a();
3065 const size_t _Dt = numeric_limits<_UIntType>::digits;
3066 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
Howard Hinnant99968442011-11-29 18:15:50 +00003067 if (_Rp == 0)
Howard Hinnantc3267212010-05-26 17:49:34 +00003068 return static_cast<result_type>(_Eng(__g, _Dt)());
Howard Hinnant99968442011-11-29 18:15:50 +00003069 size_t __w = _Dt - __clz(_Rp) - 1;
Marshall Clow0934c752015-07-30 18:26:34 +00003070 if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
Howard Hinnantc3267212010-05-26 17:49:34 +00003071 ++__w;
3072 _Eng __e(__g, __w);
3073 _UIntType __u;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003074 do
Howard Hinnantc3267212010-05-26 17:49:34 +00003075 {
3076 __u = __e();
Howard Hinnant99968442011-11-29 18:15:50 +00003077 } while (__u >= _Rp);
Howard Hinnantc3267212010-05-26 17:49:34 +00003078 return static_cast<result_type>(__u + __p.a());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003079}
3080
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003081class _LIBCPP_TYPE_VIS __rs_default;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003082
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003083_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantc3267212010-05-26 17:49:34 +00003084
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003085class _LIBCPP_TYPE_VIS __rs_default
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003086{
Howard Hinnantc3267212010-05-26 17:49:34 +00003087 static unsigned __c_;
3088
3089 __rs_default();
3090public:
Marshall Clow5920cfc2013-02-07 22:12:02 +00003091 typedef uint_fast32_t result_type;
Howard Hinnantc3267212010-05-26 17:49:34 +00003092
3093 static const result_type _Min = 0;
3094 static const result_type _Max = 0xFFFFFFFF;
3095
3096 __rs_default(const __rs_default&);
3097 ~__rs_default();
3098
3099 result_type operator()();
3100
Howard Hinnant27b4fd32012-04-02 00:40:41 +00003101 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
3102 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
Howard Hinnantc3267212010-05-26 17:49:34 +00003103
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003104 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003105};
3106
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003107_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003108
3109template <class _RandomAccessIterator>
3110void
3111random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
3112{
3113 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnant99968442011-11-29 18:15:50 +00003114 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3115 typedef typename _Dp::param_type _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003116 difference_type __d = __last - __first;
3117 if (__d > 1)
3118 {
Howard Hinnant99968442011-11-29 18:15:50 +00003119 _Dp __uid;
Howard Hinnantc3267212010-05-26 17:49:34 +00003120 __rs_default __g = __rs_get();
3121 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003122 {
Howard Hinnant99968442011-11-29 18:15:50 +00003123 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant4e599482010-10-22 15:26:39 +00003124 if (__i != difference_type(0))
3125 swap(*__first, *(__first + __i));
3126 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003127 }
3128}
3129
3130template <class _RandomAccessIterator, class _RandomNumberGenerator>
3131void
3132random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnant73d21a42010-09-04 23:28:19 +00003133#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003134 _RandomNumberGenerator&& __rand)
3135#else
3136 _RandomNumberGenerator& __rand)
3137#endif
3138{
3139 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3140 difference_type __d = __last - __first;
3141 if (__d > 1)
3142 {
3143 for (--__last; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003144 {
3145 difference_type __i = __rand(__d);
3146 swap(*__first, *(__first + __i));
3147 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003148 }
3149}
3150
Eric Fiselier917af0a2016-08-28 22:14:37 +00003151template <class _PopulationIterator, class _SampleIterator, class _Distance,
3152 class _UniformRandomNumberGenerator>
3153_LIBCPP_INLINE_VISIBILITY
3154_SampleIterator __sample(_PopulationIterator __first,
3155 _PopulationIterator __last, _SampleIterator __out,
3156 _Distance __n,
3157 _UniformRandomNumberGenerator & __g,
3158 input_iterator_tag) {
3159
3160 _Distance __k = 0;
3161 for (; __first != __last && __k < __n; ++__first, (void)++__k)
3162 __out[__k] = *__first;
3163 _Distance __sz = __k;
3164 for (; __first != __last; ++__first, (void)++__k) {
3165 _Distance __r = _VSTD::uniform_int_distribution<_Distance>(0, __k)(__g);
3166 if (__r < __sz)
3167 __out[__r] = *__first;
3168 }
3169 return __out + _VSTD::min(__n, __k);
3170}
3171
3172template <class _PopulationIterator, class _SampleIterator, class _Distance,
3173 class _UniformRandomNumberGenerator>
3174_LIBCPP_INLINE_VISIBILITY
3175_SampleIterator __sample(_PopulationIterator __first,
3176 _PopulationIterator __last, _SampleIterator __out,
3177 _Distance __n,
3178 _UniformRandomNumberGenerator& __g,
3179 forward_iterator_tag) {
3180 _Distance __unsampled_sz = _VSTD::distance(__first, __last);
3181 for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
3182 _Distance __r =
3183 _VSTD::uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
3184 if (__r < __n) {
3185 *__out++ = *__first;
3186 --__n;
3187 }
3188 }
3189 return __out;
3190}
3191
3192template <class _PopulationIterator, class _SampleIterator, class _Distance,
3193 class _UniformRandomNumberGenerator>
3194_LIBCPP_INLINE_VISIBILITY
3195_SampleIterator __sample(_PopulationIterator __first,
3196 _PopulationIterator __last, _SampleIterator __out,
3197 _Distance __n, _UniformRandomNumberGenerator& __g) {
3198 typedef typename iterator_traits<_PopulationIterator>::iterator_category
3199 _PopCategory;
3200 typedef typename iterator_traits<_PopulationIterator>::difference_type
3201 _Difference;
3202 static_assert(__is_forward_iterator<_PopulationIterator>::value ||
3203 __is_random_access_iterator<_SampleIterator>::value,
3204 "SampleIterator must meet the requirements of RandomAccessIterator");
3205 typedef typename common_type<_Distance, _Difference>::type _CommonType;
3206 _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
3207 return _VSTD::__sample(
3208 __first, __last, __out, _CommonType(__n),
3209 __g, _PopCategory());
3210}
3211
3212#if _LIBCPP_STD_VER > 14
3213template <class _PopulationIterator, class _SampleIterator, class _Distance,
3214 class _UniformRandomNumberGenerator>
3215inline _LIBCPP_INLINE_VISIBILITY
3216_SampleIterator sample(_PopulationIterator __first,
3217 _PopulationIterator __last, _SampleIterator __out,
3218 _Distance __n, _UniformRandomNumberGenerator&& __g) {
3219 return _VSTD::__sample(__first, __last, __out, __n, __g);
3220}
3221#endif // _LIBCPP_STD_VER > 14
3222
Howard Hinnantc3267212010-05-26 17:49:34 +00003223template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3224 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnant278bf2d2010-11-18 01:47:02 +00003225#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3226 _UniformRandomNumberGenerator&& __g)
3227#else
Howard Hinnantc3267212010-05-26 17:49:34 +00003228 _UniformRandomNumberGenerator& __g)
Howard Hinnant278bf2d2010-11-18 01:47:02 +00003229#endif
Howard Hinnantc3267212010-05-26 17:49:34 +00003230{
3231 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnant99968442011-11-29 18:15:50 +00003232 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3233 typedef typename _Dp::param_type _Pp;
Howard Hinnantc3267212010-05-26 17:49:34 +00003234 difference_type __d = __last - __first;
3235 if (__d > 1)
3236 {
Howard Hinnant99968442011-11-29 18:15:50 +00003237 _Dp __uid;
Howard Hinnantc3267212010-05-26 17:49:34 +00003238 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant4e599482010-10-22 15:26:39 +00003239 {
Howard Hinnant99968442011-11-29 18:15:50 +00003240 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant4e599482010-10-22 15:26:39 +00003241 if (__i != difference_type(0))
3242 swap(*__first, *(__first + __i));
3243 }
Howard Hinnantc3267212010-05-26 17:49:34 +00003244 }
3245}
3246
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003247template <class _InputIterator, class _Predicate>
3248bool
3249is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3250{
3251 for (; __first != __last; ++__first)
3252 if (!__pred(*__first))
3253 break;
Marshall Clowa0ec4b72015-02-02 18:16:35 +00003254 if ( __first == __last )
3255 return true;
3256 ++__first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003257 for (; __first != __last; ++__first)
3258 if (__pred(*__first))
3259 return false;
3260 return true;
3261}
3262
3263// partition
3264
3265template <class _Predicate, class _ForwardIterator>
3266_ForwardIterator
3267__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3268{
3269 while (true)
3270 {
3271 if (__first == __last)
3272 return __first;
3273 if (!__pred(*__first))
3274 break;
3275 ++__first;
3276 }
3277 for (_ForwardIterator __p = __first; ++__p != __last;)
3278 {
3279 if (__pred(*__p))
3280 {
3281 swap(*__first, *__p);
3282 ++__first;
3283 }
3284 }
3285 return __first;
3286}
3287
3288template <class _Predicate, class _BidirectionalIterator>
3289_BidirectionalIterator
3290__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3291 bidirectional_iterator_tag)
3292{
3293 while (true)
3294 {
3295 while (true)
3296 {
3297 if (__first == __last)
3298 return __first;
3299 if (!__pred(*__first))
3300 break;
3301 ++__first;
3302 }
3303 do
3304 {
3305 if (__first == --__last)
3306 return __first;
3307 } while (!__pred(*__last));
3308 swap(*__first, *__last);
3309 ++__first;
3310 }
3311}
3312
3313template <class _ForwardIterator, class _Predicate>
3314inline _LIBCPP_INLINE_VISIBILITY
3315_ForwardIterator
3316partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3317{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003318 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003319 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3320}
3321
3322// partition_copy
3323
3324template <class _InputIterator, class _OutputIterator1,
3325 class _OutputIterator2, class _Predicate>
3326pair<_OutputIterator1, _OutputIterator2>
3327partition_copy(_InputIterator __first, _InputIterator __last,
3328 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3329 _Predicate __pred)
3330{
3331 for (; __first != __last; ++__first)
3332 {
3333 if (__pred(*__first))
3334 {
3335 *__out_true = *__first;
3336 ++__out_true;
3337 }
3338 else
3339 {
3340 *__out_false = *__first;
3341 ++__out_false;
3342 }
3343 }
3344 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3345}
3346
3347// partition_point
3348
3349template<class _ForwardIterator, class _Predicate>
3350_ForwardIterator
3351partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3352{
3353 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003354 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003355 while (__len != 0)
3356 {
3357 difference_type __l2 = __len / 2;
3358 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003359 _VSTD::advance(__m, __l2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003360 if (__pred(*__m))
3361 {
3362 __first = ++__m;
3363 __len -= __l2 + 1;
3364 }
3365 else
3366 __len = __l2;
3367 }
3368 return __first;
3369}
3370
3371// stable_partition
3372
3373template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3374_ForwardIterator
3375__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3376 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3377{
3378 // *__first is known to be false
3379 // __len >= 1
3380 if (__len == 1)
3381 return __first;
3382 if (__len == 2)
3383 {
3384 _ForwardIterator __m = __first;
3385 if (__pred(*++__m))
3386 {
3387 swap(*__first, *__m);
3388 return __m;
3389 }
3390 return __first;
3391 }
3392 if (__len <= __p.second)
3393 { // The buffer is big enough to use
3394 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3395 __destruct_n __d(0);
3396 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3397 // Move the falses into the temporary buffer, and the trues to the front of the line
3398 // Update __first to always point to the end of the trues
3399 value_type* __t = __p.first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003400 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003401 __d.__incr((value_type*)0);
3402 ++__t;
3403 _ForwardIterator __i = __first;
3404 while (++__i != __last)
3405 {
3406 if (__pred(*__i))
3407 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003408 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003409 ++__first;
3410 }
3411 else
3412 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003413 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003414 __d.__incr((value_type*)0);
3415 ++__t;
3416 }
3417 }
3418 // All trues now at start of range, all falses in buffer
3419 // Move falses back into range, but don't mess up __first which points to first false
3420 __i = __first;
3421 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003422 *__i = _VSTD::move(*__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003423 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3424 return __first;
3425 }
3426 // Else not enough buffer, do in place
3427 // __len >= 3
3428 _ForwardIterator __m = __first;
3429 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003430 _VSTD::advance(__m, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003431 // recurse on [__first, __m), *__first know to be false
3432 // F?????????????????
3433 // f m l
3434 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3435 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3436 // TTTFFFFF??????????
3437 // f ff m l
3438 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3439 _ForwardIterator __m1 = __m;
3440 _ForwardIterator __second_false = __last;
3441 _Distance __len_half = __len - __len2;
3442 while (__pred(*__m1))
3443 {
3444 if (++__m1 == __last)
3445 goto __second_half_done;
3446 --__len_half;
3447 }
3448 // TTTFFFFFTTTF??????
3449 // f ff m m1 l
3450 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3451__second_half_done:
3452 // TTTFFFFFTTTTTFFFFF
3453 // f ff m sf l
Howard Hinnant0949eed2011-06-30 21:18:19 +00003454 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003455 // TTTTTTTTFFFFFFFFFF
3456 // |
3457}
3458
3459struct __return_temporary_buffer
3460{
3461 template <class _Tp>
Howard Hinnant0949eed2011-06-30 21:18:19 +00003462 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003463};
3464
3465template <class _Predicate, class _ForwardIterator>
3466_ForwardIterator
3467__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3468 forward_iterator_tag)
3469{
3470 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3471 // Either prove all true and return __first or point to first false
3472 while (true)
3473 {
3474 if (__first == __last)
3475 return __first;
3476 if (!__pred(*__first))
3477 break;
3478 ++__first;
3479 }
3480 // We now have a reduced range [__first, __last)
3481 // *__first is known to be false
3482 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3483 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003484 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003485 pair<value_type*, ptrdiff_t> __p(0, 0);
3486 unique_ptr<value_type, __return_temporary_buffer> __h;
3487 if (__len >= __alloc_limit)
3488 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003489 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003490 __h.reset(__p.first);
3491 }
3492 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3493 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3494}
3495
3496template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3497_BidirectionalIterator
3498__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3499 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3500{
3501 // *__first is known to be false
3502 // *__last is known to be true
3503 // __len >= 2
3504 if (__len == 2)
3505 {
3506 swap(*__first, *__last);
3507 return __last;
3508 }
3509 if (__len == 3)
3510 {
3511 _BidirectionalIterator __m = __first;
3512 if (__pred(*++__m))
3513 {
3514 swap(*__first, *__m);
3515 swap(*__m, *__last);
3516 return __last;
3517 }
3518 swap(*__m, *__last);
3519 swap(*__first, *__m);
3520 return __m;
3521 }
3522 if (__len <= __p.second)
3523 { // The buffer is big enough to use
3524 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3525 __destruct_n __d(0);
3526 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3527 // Move the falses into the temporary buffer, and the trues to the front of the line
3528 // Update __first to always point to the end of the trues
3529 value_type* __t = __p.first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003530 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003531 __d.__incr((value_type*)0);
3532 ++__t;
3533 _BidirectionalIterator __i = __first;
3534 while (++__i != __last)
3535 {
3536 if (__pred(*__i))
3537 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003538 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003539 ++__first;
3540 }
3541 else
3542 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003543 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003544 __d.__incr((value_type*)0);
3545 ++__t;
3546 }
3547 }
3548 // move *__last, known to be true
Howard Hinnant0949eed2011-06-30 21:18:19 +00003549 *__first = _VSTD::move(*__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003550 __i = ++__first;
3551 // All trues now at start of range, all falses in buffer
3552 // Move falses back into range, but don't mess up __first which points to first false
3553 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003554 *__i = _VSTD::move(*__t2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003555 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3556 return __first;
3557 }
3558 // Else not enough buffer, do in place
3559 // __len >= 4
3560 _BidirectionalIterator __m = __first;
3561 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003562 _VSTD::advance(__m, __len2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003563 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3564 // F????????????????T
3565 // f m l
3566 _BidirectionalIterator __m1 = __m;
3567 _BidirectionalIterator __first_false = __first;
3568 _Distance __len_half = __len2;
3569 while (!__pred(*--__m1))
3570 {
3571 if (__m1 == __first)
3572 goto __first_half_done;
3573 --__len_half;
3574 }
3575 // F???TFFF?????????T
3576 // f m1 m l
3577 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3578 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3579__first_half_done:
3580 // TTTFFFFF?????????T
3581 // f ff m l
3582 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3583 __m1 = __m;
3584 _BidirectionalIterator __second_false = __last;
3585 ++__second_false;
3586 __len_half = __len - __len2;
3587 while (__pred(*__m1))
3588 {
3589 if (++__m1 == __last)
3590 goto __second_half_done;
3591 --__len_half;
3592 }
3593 // TTTFFFFFTTTF?????T
3594 // f ff m m1 l
3595 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3596__second_half_done:
3597 // TTTFFFFFTTTTTFFFFF
3598 // f ff m sf l
Howard Hinnant0949eed2011-06-30 21:18:19 +00003599 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003600 // TTTTTTTTFFFFFFFFFF
3601 // |
3602}
3603
3604template <class _Predicate, class _BidirectionalIterator>
3605_BidirectionalIterator
3606__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3607 bidirectional_iterator_tag)
3608{
3609 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3610 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3611 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3612 // Either prove all true and return __first or point to first false
3613 while (true)
3614 {
3615 if (__first == __last)
3616 return __first;
3617 if (!__pred(*__first))
3618 break;
3619 ++__first;
3620 }
3621 // __first points to first false, everything prior to __first is already set.
3622 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3623 do
3624 {
3625 if (__first == --__last)
3626 return __first;
3627 } while (!__pred(*__last));
3628 // We now have a reduced range [__first, __last]
3629 // *__first is known to be false
3630 // *__last is known to be true
3631 // __len >= 2
Howard Hinnant0949eed2011-06-30 21:18:19 +00003632 difference_type __len = _VSTD::distance(__first, __last) + 1;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003633 pair<value_type*, ptrdiff_t> __p(0, 0);
3634 unique_ptr<value_type, __return_temporary_buffer> __h;
3635 if (__len >= __alloc_limit)
3636 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003637 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003638 __h.reset(__p.first);
3639 }
3640 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3641 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3642}
3643
3644template <class _ForwardIterator, class _Predicate>
3645inline _LIBCPP_INLINE_VISIBILITY
3646_ForwardIterator
3647stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3648{
3649 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3650 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3651}
3652
3653// is_sorted_until
3654
3655template <class _ForwardIterator, class _Compare>
3656_ForwardIterator
3657is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3658{
3659 if (__first != __last)
3660 {
3661 _ForwardIterator __i = __first;
3662 while (++__i != __last)
3663 {
3664 if (__comp(*__i, *__first))
3665 return __i;
3666 __first = __i;
3667 }
3668 }
3669 return __last;
3670}
3671
Howard Hinnant324bb032010-08-22 00:02:43 +00003672template<class _ForwardIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003673inline _LIBCPP_INLINE_VISIBILITY
3674_ForwardIterator
3675is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3676{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003677 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003678}
3679
3680// is_sorted
3681
3682template <class _ForwardIterator, class _Compare>
3683inline _LIBCPP_INLINE_VISIBILITY
3684bool
3685is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3686{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003687 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003688}
3689
Howard Hinnant324bb032010-08-22 00:02:43 +00003690template<class _ForwardIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003691inline _LIBCPP_INLINE_VISIBILITY
3692bool
3693is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3694{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003695 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003696}
3697
3698// sort
3699
3700// stable, 2-3 compares, 0-2 swaps
3701
3702template <class _Compare, class _ForwardIterator>
3703unsigned
3704__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3705{
3706 unsigned __r = 0;
3707 if (!__c(*__y, *__x)) // if x <= y
3708 {
3709 if (!__c(*__z, *__y)) // if y <= z
3710 return __r; // x <= y && y <= z
3711 // x <= y && y > z
3712 swap(*__y, *__z); // x <= z && y < z
3713 __r = 1;
3714 if (__c(*__y, *__x)) // if x > y
3715 {
3716 swap(*__x, *__y); // x < y && y <= z
3717 __r = 2;
3718 }
3719 return __r; // x <= y && y < z
3720 }
3721 if (__c(*__z, *__y)) // x > y, if y > z
3722 {
3723 swap(*__x, *__z); // x < y && y < z
3724 __r = 1;
3725 return __r;
3726 }
3727 swap(*__x, *__y); // x > y && y <= z
3728 __r = 1; // x < y && x <= z
3729 if (__c(*__z, *__y)) // if y > z
3730 {
3731 swap(*__y, *__z); // x <= y && y < z
3732 __r = 2;
3733 }
3734 return __r;
3735} // x <= y && y <= z
3736
3737// stable, 3-6 compares, 0-5 swaps
3738
3739template <class _Compare, class _ForwardIterator>
3740unsigned
3741__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3742 _ForwardIterator __x4, _Compare __c)
3743{
3744 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3745 if (__c(*__x4, *__x3))
3746 {
3747 swap(*__x3, *__x4);
3748 ++__r;
3749 if (__c(*__x3, *__x2))
3750 {
3751 swap(*__x2, *__x3);
3752 ++__r;
3753 if (__c(*__x2, *__x1))
3754 {
3755 swap(*__x1, *__x2);
3756 ++__r;
3757 }
3758 }
3759 }
3760 return __r;
3761}
3762
3763// stable, 4-10 compares, 0-9 swaps
3764
3765template <class _Compare, class _ForwardIterator>
3766unsigned
3767__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3768 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3769{
3770 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3771 if (__c(*__x5, *__x4))
3772 {
3773 swap(*__x4, *__x5);
3774 ++__r;
3775 if (__c(*__x4, *__x3))
3776 {
3777 swap(*__x3, *__x4);
3778 ++__r;
3779 if (__c(*__x3, *__x2))
3780 {
3781 swap(*__x2, *__x3);
3782 ++__r;
3783 if (__c(*__x2, *__x1))
3784 {
3785 swap(*__x1, *__x2);
3786 ++__r;
3787 }
3788 }
3789 }
3790 }
3791 return __r;
3792}
3793
3794// Assumes size > 0
3795template <class _Compare, class _BirdirectionalIterator>
3796void
3797__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3798{
3799 _BirdirectionalIterator __lm1 = __last;
3800 for (--__lm1; __first != __lm1; ++__first)
3801 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003802 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003803 typename add_lvalue_reference<_Compare>::type>
3804 (__first, __last, __comp);
3805 if (__i != __first)
3806 swap(*__first, *__i);
3807 }
3808}
3809
3810template <class _Compare, class _BirdirectionalIterator>
3811void
3812__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3813{
3814 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3815 if (__first != __last)
3816 {
3817 _BirdirectionalIterator __i = __first;
3818 for (++__i; __i != __last; ++__i)
3819 {
3820 _BirdirectionalIterator __j = __i;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003821 value_type __t(_VSTD::move(*__j));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003822 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003823 *__j = _VSTD::move(*__k);
3824 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003825 }
3826 }
3827}
3828
3829template <class _Compare, class _RandomAccessIterator>
3830void
3831__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3832{
3833 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3834 _RandomAccessIterator __j = __first+2;
3835 __sort3<_Compare>(__first, __first+1, __j, __comp);
3836 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3837 {
3838 if (__comp(*__i, *__j))
3839 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003840 value_type __t(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003841 _RandomAccessIterator __k = __j;
3842 __j = __i;
3843 do
3844 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003845 *__j = _VSTD::move(*__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003846 __j = __k;
3847 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003848 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003849 }
3850 __j = __i;
3851 }
3852}
3853
3854template <class _Compare, class _RandomAccessIterator>
3855bool
3856__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3857{
3858 switch (__last - __first)
3859 {
3860 case 0:
3861 case 1:
3862 return true;
3863 case 2:
3864 if (__comp(*--__last, *__first))
3865 swap(*__first, *__last);
3866 return true;
3867 case 3:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003868 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003869 return true;
3870 case 4:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003871 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003872 return true;
3873 case 5:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003874 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003875 return true;
3876 }
3877 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3878 _RandomAccessIterator __j = __first+2;
3879 __sort3<_Compare>(__first, __first+1, __j, __comp);
3880 const unsigned __limit = 8;
3881 unsigned __count = 0;
3882 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3883 {
3884 if (__comp(*__i, *__j))
3885 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003886 value_type __t(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003887 _RandomAccessIterator __k = __j;
3888 __j = __i;
3889 do
3890 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003891 *__j = _VSTD::move(*__k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003892 __j = __k;
3893 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnant0949eed2011-06-30 21:18:19 +00003894 *__j = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003895 if (++__count == __limit)
3896 return ++__i == __last;
3897 }
3898 __j = __i;
3899 }
3900 return true;
3901}
3902
3903template <class _Compare, class _BirdirectionalIterator>
3904void
3905__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3906 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3907{
3908 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3909 if (__first1 != __last1)
3910 {
3911 __destruct_n __d(0);
3912 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3913 value_type* __last2 = __first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003914 ::new(__last2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003915 __d.__incr((value_type*)0);
3916 for (++__last2; ++__first1 != __last1; ++__last2)
3917 {
3918 value_type* __j2 = __last2;
3919 value_type* __i2 = __j2;
3920 if (__comp(*__first1, *--__i2))
3921 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003922 ::new(__j2) value_type(_VSTD::move(*__i2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003923 __d.__incr((value_type*)0);
3924 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00003925 *__j2 = _VSTD::move(*__i2);
3926 *__j2 = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003927 }
3928 else
3929 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003930 ::new(__j2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003931 __d.__incr((value_type*)0);
3932 }
3933 }
3934 __h.release();
3935 }
3936}
3937
3938template <class _Compare, class _RandomAccessIterator>
3939void
3940__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3941{
3942 // _Compare is known to be a reference type
3943 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3944 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant1468b662010-11-19 22:17:28 +00003945 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3946 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003947 while (true)
3948 {
3949 __restart:
3950 difference_type __len = __last - __first;
3951 switch (__len)
3952 {
3953 case 0:
3954 case 1:
3955 return;
3956 case 2:
3957 if (__comp(*--__last, *__first))
3958 swap(*__first, *__last);
3959 return;
3960 case 3:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003961 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003962 return;
3963 case 4:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003964 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003965 return;
3966 case 5:
Howard Hinnant0949eed2011-06-30 21:18:19 +00003967 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003968 return;
3969 }
3970 if (__len <= __limit)
3971 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00003972 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003973 return;
3974 }
3975 // __len > 5
3976 _RandomAccessIterator __m = __first;
3977 _RandomAccessIterator __lm1 = __last;
3978 --__lm1;
3979 unsigned __n_swaps;
3980 {
3981 difference_type __delta;
3982 if (__len >= 1000)
3983 {
3984 __delta = __len/2;
3985 __m += __delta;
3986 __delta /= 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003987 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003988 }
3989 else
3990 {
3991 __delta = __len/2;
3992 __m += __delta;
Howard Hinnant0949eed2011-06-30 21:18:19 +00003993 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003994 }
3995 }
3996 // *__m is median
3997 // partition [__first, __m) < *__m and *__m <= [__m, __last)
3998 // (this inhibits tossing elements equivalent to __m around unnecessarily)
3999 _RandomAccessIterator __i = __first;
4000 _RandomAccessIterator __j = __lm1;
4001 // j points beyond range to be tested, *__m is known to be <= *__lm1
4002 // The search going up is known to be guarded but the search coming down isn't.
4003 // Prime the downward search with a guard.
4004 if (!__comp(*__i, *__m)) // if *__first == *__m
4005 {
4006 // *__first == *__m, *__first doesn't go in first part
4007 // manually guard downward moving __j against __i
4008 while (true)
4009 {
4010 if (__i == --__j)
4011 {
4012 // *__first == *__m, *__m <= all other elements
4013 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
4014 ++__i; // __first + 1
4015 __j = __last;
4016 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
4017 {
4018 while (true)
4019 {
4020 if (__i == __j)
4021 return; // [__first, __last) all equivalent elements
4022 if (__comp(*__first, *__i))
4023 {
4024 swap(*__i, *__j);
4025 ++__n_swaps;
4026 ++__i;
4027 break;
4028 }
4029 ++__i;
4030 }
4031 }
4032 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
4033 if (__i == __j)
4034 return;
4035 while (true)
4036 {
4037 while (!__comp(*__first, *__i))
4038 ++__i;
4039 while (__comp(*__first, *--__j))
4040 ;
4041 if (__i >= __j)
4042 break;
4043 swap(*__i, *__j);
4044 ++__n_swaps;
4045 ++__i;
4046 }
4047 // [__first, __i) == *__first and *__first < [__i, __last)
4048 // The first part is sorted, sort the secod part
Howard Hinnant0949eed2011-06-30 21:18:19 +00004049 // _VSTD::__sort<_Compare>(__i, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004050 __first = __i;
4051 goto __restart;
4052 }
4053 if (__comp(*__j, *__m))
4054 {
4055 swap(*__i, *__j);
4056 ++__n_swaps;
4057 break; // found guard for downward moving __j, now use unguarded partition
4058 }
4059 }
4060 }
4061 // It is known that *__i < *__m
4062 ++__i;
4063 // j points beyond range to be tested, *__m is known to be <= *__lm1
4064 // if not yet partitioned...
4065 if (__i < __j)
4066 {
4067 // known that *(__i - 1) < *__m
4068 // known that __i <= __m
4069 while (true)
4070 {
4071 // __m still guards upward moving __i
4072 while (__comp(*__i, *__m))
4073 ++__i;
4074 // It is now known that a guard exists for downward moving __j
4075 while (!__comp(*--__j, *__m))
4076 ;
4077 if (__i > __j)
4078 break;
4079 swap(*__i, *__j);
4080 ++__n_swaps;
4081 // It is known that __m != __j
4082 // If __m just moved, follow it
4083 if (__m == __i)
4084 __m = __j;
4085 ++__i;
4086 }
4087 }
4088 // [__first, __i) < *__m and *__m <= [__i, __last)
4089 if (__i != __m && __comp(*__m, *__i))
4090 {
4091 swap(*__i, *__m);
4092 ++__n_swaps;
4093 }
4094 // [__first, __i) < *__i and *__i <= [__i+1, __last)
4095 // If we were given a perfect partition, see if insertion sort is quick...
4096 if (__n_swaps == 0)
4097 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004098 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
4099 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004100 {
4101 if (__fs)
4102 return;
4103 __last = __i;
4104 continue;
4105 }
4106 else
4107 {
4108 if (__fs)
4109 {
4110 __first = ++__i;
4111 continue;
4112 }
4113 }
4114 }
4115 // sort smaller range with recursive call and larger with tail recursion elimination
4116 if (__i - __first < __last - __i)
4117 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004118 _VSTD::__sort<_Compare>(__first, __i, __comp);
4119 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004120 __first = ++__i;
4121 }
4122 else
4123 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004124 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4125 // _VSTD::__sort<_Compare>(__first, __i, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004126 __last = __i;
4127 }
4128 }
4129}
4130
4131// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
4132template <class _RandomAccessIterator, class _Compare>
4133inline _LIBCPP_INLINE_VISIBILITY
4134void
4135sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4136{
Howard Hinnant5e571422013-08-23 20:10:18 +00004137#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004138 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4139 __debug_less<_Compare> __c(__comp);
4140 __sort<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004141#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004142 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4143 __sort<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004144#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004145}
4146
4147template <class _RandomAccessIterator>
4148inline _LIBCPP_INLINE_VISIBILITY
4149void
4150sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4151{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004152 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004153}
4154
4155template <class _Tp>
4156inline _LIBCPP_INLINE_VISIBILITY
4157void
4158sort(_Tp** __first, _Tp** __last)
4159{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004160 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004161}
4162
4163template <class _Tp>
4164inline _LIBCPP_INLINE_VISIBILITY
4165void
4166sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4167{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004168 _VSTD::sort(__first.base(), __last.base());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004169}
4170
Howard Hinnant7a563db2011-09-14 18:33:51 +00004171template <class _Tp, class _Compare>
4172inline _LIBCPP_INLINE_VISIBILITY
4173void
4174sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4175{
4176 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4177 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4178}
4179
Howard Hinnante9df0a52013-08-01 18:17:34 +00004180#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +00004181#pragma warning( push )
4182#pragma warning( disable: 4231)
Howard Hinnante9df0a52013-08-01 18:17:34 +00004183#endif // _LIBCPP_MSVC
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004184_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4185_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4186_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4187_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4188_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4189_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4190_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4191_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4192_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4193_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4194_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4195_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>&))
4196_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4197_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4198_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 +00004199
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004200_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4201_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4202_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4203_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4204_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4205_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4206_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4207_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4208_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4209_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4210_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4211_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>&))
4212_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4213_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4214_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 +00004215
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004216_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 +00004217#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +00004218#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34 +00004219#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004220
4221// lower_bound
4222
4223template <class _Compare, class _ForwardIterator, class _Tp>
4224_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004225__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004226{
4227 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004228 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004229 while (__len != 0)
4230 {
4231 difference_type __l2 = __len / 2;
4232 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004233 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004234 if (__comp(*__m, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004235 {
4236 __first = ++__m;
4237 __len -= __l2 + 1;
4238 }
4239 else
4240 __len = __l2;
4241 }
4242 return __first;
4243}
4244
4245template <class _ForwardIterator, class _Tp, class _Compare>
4246inline _LIBCPP_INLINE_VISIBILITY
4247_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004248lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004249{
Howard Hinnant5e571422013-08-23 20:10:18 +00004250#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004251 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4252 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004253 return __lower_bound<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004254#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004255 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004256 return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004257#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004258}
4259
4260template <class _ForwardIterator, class _Tp>
4261inline _LIBCPP_INLINE_VISIBILITY
4262_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004263lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004264{
Howard Hinnant78b68282011-10-22 20:59:45 +00004265 return _VSTD::lower_bound(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004266 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4267}
4268
4269// upper_bound
4270
4271template <class _Compare, class _ForwardIterator, class _Tp>
4272_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004273__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004274{
4275 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004276 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004277 while (__len != 0)
4278 {
4279 difference_type __l2 = __len / 2;
4280 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004281 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004282 if (__comp(__value_, *__m))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004283 __len = __l2;
4284 else
4285 {
4286 __first = ++__m;
4287 __len -= __l2 + 1;
4288 }
4289 }
4290 return __first;
4291}
4292
4293template <class _ForwardIterator, class _Tp, class _Compare>
4294inline _LIBCPP_INLINE_VISIBILITY
4295_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004296upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004297{
Howard Hinnant5e571422013-08-23 20:10:18 +00004298#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004299 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4300 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004301 return __upper_bound<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004302#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004303 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004304 return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004305#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004306}
4307
4308template <class _ForwardIterator, class _Tp>
4309inline _LIBCPP_INLINE_VISIBILITY
4310_ForwardIterator
Howard Hinnant78b68282011-10-22 20:59:45 +00004311upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004312{
Howard Hinnant78b68282011-10-22 20:59:45 +00004313 return _VSTD::upper_bound(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004314 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4315}
4316
4317// equal_range
4318
4319template <class _Compare, class _ForwardIterator, class _Tp>
4320pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004321__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004322{
4323 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004324 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004325 while (__len != 0)
4326 {
4327 difference_type __l2 = __len / 2;
4328 _ForwardIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004329 _VSTD::advance(__m, __l2);
Howard Hinnant78b68282011-10-22 20:59:45 +00004330 if (__comp(*__m, __value_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004331 {
4332 __first = ++__m;
4333 __len -= __l2 + 1;
4334 }
Howard Hinnant78b68282011-10-22 20:59:45 +00004335 else if (__comp(__value_, *__m))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004336 {
4337 __last = __m;
4338 __len = __l2;
4339 }
4340 else
4341 {
4342 _ForwardIterator __mp1 = __m;
4343 return pair<_ForwardIterator, _ForwardIterator>
4344 (
Howard Hinnant78b68282011-10-22 20:59:45 +00004345 __lower_bound<_Compare>(__first, __m, __value_, __comp),
4346 __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004347 );
4348 }
4349 }
4350 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4351}
4352
4353template <class _ForwardIterator, class _Tp, class _Compare>
4354inline _LIBCPP_INLINE_VISIBILITY
4355pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004356equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004357{
Howard Hinnant5e571422013-08-23 20:10:18 +00004358#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004359 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4360 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004361 return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004362#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004363 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004364 return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004365#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004366}
4367
4368template <class _ForwardIterator, class _Tp>
4369inline _LIBCPP_INLINE_VISIBILITY
4370pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant78b68282011-10-22 20:59:45 +00004371equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004372{
Howard Hinnant78b68282011-10-22 20:59:45 +00004373 return _VSTD::equal_range(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004374 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4375}
4376
4377// binary_search
4378
4379template <class _Compare, class _ForwardIterator, class _Tp>
4380inline _LIBCPP_INLINE_VISIBILITY
4381bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004382__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004383{
Howard Hinnant78b68282011-10-22 20:59:45 +00004384 __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4385 return __first != __last && !__comp(__value_, *__first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004386}
4387
4388template <class _ForwardIterator, class _Tp, class _Compare>
4389inline _LIBCPP_INLINE_VISIBILITY
4390bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004391binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004392{
Howard Hinnant5e571422013-08-23 20:10:18 +00004393#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004394 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4395 __debug_less<_Compare> __c(__comp);
Howard Hinnant78b68282011-10-22 20:59:45 +00004396 return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004397#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004398 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant78b68282011-10-22 20:59:45 +00004399 return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004400#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004401}
4402
4403template <class _ForwardIterator, class _Tp>
4404inline _LIBCPP_INLINE_VISIBILITY
4405bool
Howard Hinnant78b68282011-10-22 20:59:45 +00004406binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004407{
Howard Hinnant78b68282011-10-22 20:59:45 +00004408 return _VSTD::binary_search(__first, __last, __value_,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004409 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4410}
4411
4412// merge
4413
4414template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4415_OutputIterator
4416__merge(_InputIterator1 __first1, _InputIterator1 __last1,
4417 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4418{
4419 for (; __first1 != __last1; ++__result)
4420 {
4421 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004422 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004423 if (__comp(*__first2, *__first1))
4424 {
4425 *__result = *__first2;
4426 ++__first2;
4427 }
4428 else
4429 {
4430 *__result = *__first1;
4431 ++__first1;
4432 }
4433 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00004434 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004435}
4436
4437template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4438inline _LIBCPP_INLINE_VISIBILITY
4439_OutputIterator
4440merge(_InputIterator1 __first1, _InputIterator1 __last1,
4441 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4442{
Howard Hinnant5e571422013-08-23 20:10:18 +00004443#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004444 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4445 __debug_less<_Compare> __c(__comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004446 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00004447#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004448 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004449 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00004450#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004451}
4452
4453template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4454inline _LIBCPP_INLINE_VISIBILITY
4455_OutputIterator
4456merge(_InputIterator1 __first1, _InputIterator1 __last1,
4457 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4458{
4459 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4460 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4461 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4462}
4463
4464// inplace_merge
4465
Marshall Clowa3795762015-07-29 16:25:45 +00004466template <class _Compare, class _InputIterator1, class _InputIterator2,
4467 class _OutputIterator>
4468void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4469 _InputIterator2 __first2, _InputIterator2 __last2,
4470 _OutputIterator __result, _Compare __comp)
4471{
4472 for (; __first1 != __last1; ++__result)
4473 {
4474 if (__first2 == __last2)
4475 {
4476 _VSTD::move(__first1, __last1, __result);
4477 return;
4478 }
4479
4480 if (__comp(*__first2, *__first1))
4481 {
4482 *__result = _VSTD::move(*__first2);
4483 ++__first2;
4484 }
4485 else
4486 {
4487 *__result = _VSTD::move(*__first1);
4488 ++__first1;
4489 }
4490 }
4491 // __first2 through __last2 are already in the right spot.
4492}
4493
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004494template <class _Compare, class _BidirectionalIterator>
4495void
4496__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4497 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4498 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4499 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4500{
4501 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004502 __destruct_n __d(0);
4503 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4504 if (__len1 <= __len2)
4505 {
4506 value_type* __p = __buff;
Eric Fiselierb9919752014-10-27 19:28:20 +00004507 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004508 ::new(__p) value_type(_VSTD::move(*__i));
Marshall Clowa3795762015-07-29 16:25:45 +00004509 __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004510 }
4511 else
4512 {
4513 value_type* __p = __buff;
Eric Fiselierb9919752014-10-27 19:28:20 +00004514 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004515 ::new(__p) value_type(_VSTD::move(*__i));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004516 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4517 typedef reverse_iterator<value_type*> _Rv;
Aditya Kumarfdb4f172016-08-25 11:52:38 +00004518 __half_inplace_merge(_Rv(__p), _Rv(__buff),
Marshall Clowa3795762015-07-29 16:25:45 +00004519 _RBi(__middle), _RBi(__first),
4520 _RBi(__last), __negate<_Compare>(__comp));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004521 }
4522}
4523
4524template <class _Compare, class _BidirectionalIterator>
4525void
4526__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4527 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4528 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4529 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4530{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004531 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4532 while (true)
4533 {
4534 // if __middle == __last, we're done
4535 if (__len2 == 0)
4536 return;
Marshall Clowe809f4c2015-02-02 16:44:11 +00004537 if (__len1 <= __buff_size || __len2 <= __buff_size)
4538 return __buffered_inplace_merge<_Compare>
4539 (__first, __middle, __last, __comp, __len1, __len2, __buff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004540 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
Eric Fiselierb9919752014-10-27 19:28:20 +00004541 for (; true; ++__first, (void) --__len1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004542 {
4543 if (__len1 == 0)
4544 return;
4545 if (__comp(*__middle, *__first))
4546 break;
4547 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004548 // __first < __middle < __last
4549 // *__first > *__middle
4550 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4551 // all elements in:
4552 // [__first, __m1) <= [__middle, __m2)
4553 // [__middle, __m2) < [__m1, __middle)
4554 // [__m1, __middle) <= [__m2, __last)
4555 // and __m1 or __m2 is in the middle of its range
4556 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4557 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4558 difference_type __len11; // distance(__first, __m1)
4559 difference_type __len21; // distance(__middle, __m2)
4560 // binary search smaller range
4561 if (__len1 < __len2)
4562 { // __len >= 1, __len2 >= 2
4563 __len21 = __len2 / 2;
4564 __m2 = __middle;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004565 _VSTD::advance(__m2, __len21);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004566 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004567 __len11 = _VSTD::distance(__first, __m1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004568 }
4569 else
4570 {
4571 if (__len1 == 1)
4572 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4573 // It is known *__first > *__middle
4574 swap(*__first, *__middle);
4575 return;
4576 }
4577 // __len1 >= 2, __len2 >= 1
4578 __len11 = __len1 / 2;
4579 __m1 = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004580 _VSTD::advance(__m1, __len11);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004581 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004582 __len21 = _VSTD::distance(__middle, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004583 }
4584 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4585 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4586 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4587 // swap middle two partitions
Howard Hinnant0949eed2011-06-30 21:18:19 +00004588 __middle = _VSTD::rotate(__m1, __middle, __m2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004589 // __len12 and __len21 now have swapped meanings
4590 // merge smaller range with recurisve call and larger with tail recursion elimination
4591 if (__len11 + __len21 < __len12 + __len22)
4592 {
4593 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4594// __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4595 __first = __middle;
4596 __middle = __m2;
4597 __len1 = __len12;
4598 __len2 = __len22;
4599 }
4600 else
4601 {
4602 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4603// __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4604 __last = __middle;
4605 __middle = __m1;
4606 __len1 = __len11;
4607 __len2 = __len21;
4608 }
4609 }
4610}
4611
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004612template <class _BidirectionalIterator, class _Compare>
4613inline _LIBCPP_INLINE_VISIBILITY
4614void
4615inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4616 _Compare __comp)
4617{
4618 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4619 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004620 difference_type __len1 = _VSTD::distance(__first, __middle);
4621 difference_type __len2 = _VSTD::distance(__middle, __last);
4622 difference_type __buf_size = _VSTD::min(__len1, __len2);
Marshall Clow4c2684c2015-02-02 17:35:53 +00004623 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4624 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
4625
Howard Hinnant5e571422013-08-23 20:10:18 +00004626#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004627 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4628 __debug_less<_Compare> __c(__comp);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004629 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004630 __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004631#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004632 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004633 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004634 __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004635#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004636}
4637
4638template <class _BidirectionalIterator>
4639inline _LIBCPP_INLINE_VISIBILITY
4640void
4641inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4642{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004643 _VSTD::inplace_merge(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004644 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4645}
4646
4647// stable_sort
4648
4649template <class _Compare, class _InputIterator1, class _InputIterator2>
4650void
4651__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4652 _InputIterator2 __first2, _InputIterator2 __last2,
4653 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4654{
4655 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4656 __destruct_n __d(0);
4657 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4658 for (; true; ++__result)
4659 {
4660 if (__first1 == __last1)
4661 {
4662 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
Howard Hinnant0949eed2011-06-30 21:18:19 +00004663 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004664 __h.release();
4665 return;
4666 }
4667 if (__first2 == __last2)
4668 {
4669 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
Howard Hinnant0949eed2011-06-30 21:18:19 +00004670 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004671 __h.release();
4672 return;
4673 }
4674 if (__comp(*__first2, *__first1))
4675 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004676 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004677 __d.__incr((value_type*)0);
4678 ++__first2;
4679 }
4680 else
4681 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004682 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004683 __d.__incr((value_type*)0);
4684 ++__first1;
4685 }
4686 }
4687}
4688
4689template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4690void
4691__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4692 _InputIterator2 __first2, _InputIterator2 __last2,
4693 _OutputIterator __result, _Compare __comp)
4694{
4695 for (; __first1 != __last1; ++__result)
4696 {
4697 if (__first2 == __last2)
4698 {
4699 for (; __first1 != __last1; ++__first1, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004700 *__result = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004701 return;
4702 }
4703 if (__comp(*__first2, *__first1))
4704 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004705 *__result = _VSTD::move(*__first2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004706 ++__first2;
4707 }
4708 else
4709 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004710 *__result = _VSTD::move(*__first1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004711 ++__first1;
4712 }
4713 }
4714 for (; __first2 != __last2; ++__first2, ++__result)
Howard Hinnant0949eed2011-06-30 21:18:19 +00004715 *__result = _VSTD::move(*__first2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004716}
4717
4718template <class _Compare, class _RandomAccessIterator>
4719void
4720__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4721 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4722 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4723
4724template <class _Compare, class _RandomAccessIterator>
4725void
4726__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4727 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4728 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4729{
4730 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4731 switch (__len)
4732 {
4733 case 0:
4734 return;
4735 case 1:
Howard Hinnant0949eed2011-06-30 21:18:19 +00004736 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004737 return;
4738 case 2:
4739 __destruct_n __d(0);
4740 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
4741 if (__comp(*--__last1, *__first1))
4742 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004743 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004744 __d.__incr((value_type*)0);
4745 ++__first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004746 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004747 }
4748 else
4749 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004750 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004751 __d.__incr((value_type*)0);
4752 ++__first2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00004753 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004754 }
4755 __h2.release();
4756 return;
4757 }
4758 if (__len <= 8)
4759 {
4760 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4761 return;
4762 }
4763 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4764 _RandomAccessIterator __m = __first1 + __l2;
4765 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4766 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4767 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4768}
4769
4770template <class _Tp>
4771struct __stable_sort_switch
4772{
Howard Hinnant1468b662010-11-19 22:17:28 +00004773 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004774};
4775
4776template <class _Compare, class _RandomAccessIterator>
4777void
4778__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4779 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4780 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4781{
4782 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4783 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4784 switch (__len)
4785 {
4786 case 0:
4787 case 1:
4788 return;
4789 case 2:
4790 if (__comp(*--__last, *__first))
4791 swap(*__first, *__last);
4792 return;
4793 }
4794 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4795 {
4796 __insertion_sort<_Compare>(__first, __last, __comp);
4797 return;
4798 }
4799 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4800 _RandomAccessIterator __m = __first + __l2;
4801 if (__len <= __buff_size)
4802 {
4803 __destruct_n __d(0);
4804 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4805 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4806 __d.__set(__l2, (value_type*)0);
4807 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4808 __d.__set(__len, (value_type*)0);
4809 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4810// __merge<_Compare>(move_iterator<value_type*>(__buff),
4811// move_iterator<value_type*>(__buff + __l2),
4812// move_iterator<_RandomAccessIterator>(__buff + __l2),
4813// move_iterator<_RandomAccessIterator>(__buff + __len),
4814// __first, __comp);
4815 return;
4816 }
4817 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4818 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4819 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4820}
4821
4822template <class _RandomAccessIterator, class _Compare>
4823inline _LIBCPP_INLINE_VISIBILITY
4824void
4825stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4826{
4827 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4828 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4829 difference_type __len = __last - __first;
4830 pair<value_type*, ptrdiff_t> __buf(0, 0);
4831 unique_ptr<value_type, __return_temporary_buffer> __h;
4832 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4833 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004834 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004835 __h.reset(__buf.first);
4836 }
Howard Hinnant5e571422013-08-23 20:10:18 +00004837#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004838 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4839 __debug_less<_Compare> __c(__comp);
4840 __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004841#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004842 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4843 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
Howard Hinnant5e571422013-08-23 20:10:18 +00004844#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004845}
4846
4847template <class _RandomAccessIterator>
4848inline _LIBCPP_INLINE_VISIBILITY
4849void
4850stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4851{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004852 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004853}
4854
4855// is_heap_until
4856
4857template <class _RandomAccessIterator, class _Compare>
4858_RandomAccessIterator
4859is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4860{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004861 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004862 difference_type __len = __last - __first;
4863 difference_type __p = 0;
4864 difference_type __c = 1;
4865 _RandomAccessIterator __pp = __first;
4866 while (__c < __len)
4867 {
4868 _RandomAccessIterator __cp = __first + __c;
4869 if (__comp(*__pp, *__cp))
4870 return __cp;
4871 ++__c;
4872 ++__cp;
4873 if (__c == __len)
4874 return __last;
4875 if (__comp(*__pp, *__cp))
4876 return __cp;
4877 ++__p;
4878 ++__pp;
4879 __c = 2 * __p + 1;
4880 }
4881 return __last;
4882}
4883
Howard Hinnant324bb032010-08-22 00:02:43 +00004884template<class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004885inline _LIBCPP_INLINE_VISIBILITY
4886_RandomAccessIterator
4887is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4888{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004889 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004890}
4891
4892// is_heap
4893
4894template <class _RandomAccessIterator, class _Compare>
4895inline _LIBCPP_INLINE_VISIBILITY
4896bool
4897is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4898{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004899 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004900}
4901
Howard Hinnant324bb032010-08-22 00:02:43 +00004902template<class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004903inline _LIBCPP_INLINE_VISIBILITY
4904bool
4905is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4906{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004907 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004908}
4909
4910// push_heap
4911
4912template <class _Compare, class _RandomAccessIterator>
4913void
David Majnemercb8757a2014-07-22 06:07:09 +00004914__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4915 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004916{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004917 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4918 if (__len > 1)
4919 {
4920 __len = (__len - 2) / 2;
4921 _RandomAccessIterator __ptr = __first + __len;
4922 if (__comp(*__ptr, *--__last))
4923 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004924 value_type __t(_VSTD::move(*__last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004925 do
4926 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00004927 *__last = _VSTD::move(*__ptr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004928 __last = __ptr;
4929 if (__len == 0)
4930 break;
4931 __len = (__len - 1) / 2;
4932 __ptr = __first + __len;
4933 } while (__comp(*__ptr, __t));
Howard Hinnant0949eed2011-06-30 21:18:19 +00004934 *__last = _VSTD::move(__t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004935 }
4936 }
4937}
4938
4939template <class _RandomAccessIterator, class _Compare>
4940inline _LIBCPP_INLINE_VISIBILITY
4941void
4942push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4943{
Howard Hinnant5e571422013-08-23 20:10:18 +00004944#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004945 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4946 __debug_less<_Compare> __c(__comp);
David Majnemercb8757a2014-07-22 06:07:09 +00004947 __sift_up<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004948#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004949 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
David Majnemercb8757a2014-07-22 06:07:09 +00004950 __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00004951#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004952}
4953
4954template <class _RandomAccessIterator>
4955inline _LIBCPP_INLINE_VISIBILITY
4956void
4957push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4958{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004959 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004960}
4961
4962// pop_heap
4963
4964template <class _Compare, class _RandomAccessIterator>
David Majnemercb8757a2014-07-22 06:07:09 +00004965void
4966__sift_down(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4967 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4968 _RandomAccessIterator __start)
4969{
4970 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4971 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4972 // left-child of __start is at 2 * __start + 1
4973 // right-child of __start is at 2 * __start + 2
4974 difference_type __child = __start - __first;
4975
4976 if (__len < 2 || (__len - 2) / 2 < __child)
4977 return;
4978
4979 __child = 2 * __child + 1;
4980 _RandomAccessIterator __child_i = __first + __child;
4981
4982 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4983 // right-child exists and is greater than left-child
4984 ++__child_i;
4985 ++__child;
4986 }
4987
4988 // check if we are in heap-order
4989 if (__comp(*__child_i, *__start))
4990 // we are, __start is larger than it's largest child
4991 return;
4992
4993 value_type __top(_VSTD::move(*__start));
4994 do
4995 {
4996 // we are not in heap-order, swap the parent with it's largest child
4997 *__start = _VSTD::move(*__child_i);
4998 __start = __child_i;
4999
5000 if ((__len - 2) / 2 < __child)
5001 break;
5002
5003 // recompute the child based off of the updated parent
5004 __child = 2 * __child + 1;
5005 __child_i = __first + __child;
5006
5007 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
5008 // right-child exists and is greater than left-child
5009 ++__child_i;
5010 ++__child;
5011 }
5012
5013 // check if we are in heap-order
5014 } while (!__comp(*__child_i, __top));
5015 *__start = _VSTD::move(__top);
5016}
5017
5018template <class _Compare, class _RandomAccessIterator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005019inline _LIBCPP_INLINE_VISIBILITY
5020void
5021__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
5022 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
5023{
5024 if (__len > 1)
5025 {
5026 swap(*__first, *--__last);
David Majnemercb8757a2014-07-22 06:07:09 +00005027 __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005028 }
5029}
5030
5031template <class _RandomAccessIterator, class _Compare>
5032inline _LIBCPP_INLINE_VISIBILITY
5033void
5034pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5035{
Howard Hinnant5e571422013-08-23 20:10:18 +00005036#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005037 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5038 __debug_less<_Compare> __c(__comp);
5039 __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00005040#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005041 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5042 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant5e571422013-08-23 20:10:18 +00005043#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005044}
5045
5046template <class _RandomAccessIterator>
5047inline _LIBCPP_INLINE_VISIBILITY
5048void
5049pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5050{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005051 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005052}
5053
5054// make_heap
5055
5056template <class _Compare, class _RandomAccessIterator>
5057void
5058__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5059{
5060 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5061 difference_type __n = __last - __first;
5062 if (__n > 1)
5063 {
David Majnemercb8757a2014-07-22 06:07:09 +00005064 // start from the first parent, there is no need to consider children
5065 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
5066 {
5067 __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
5068 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005069 }
5070}
5071
5072template <class _RandomAccessIterator, class _Compare>
5073inline _LIBCPP_INLINE_VISIBILITY
5074void
5075make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5076{
Howard Hinnant5e571422013-08-23 20:10:18 +00005077#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005078 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5079 __debug_less<_Compare> __c(__comp);
5080 __make_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005081#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005082 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5083 __make_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005084#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005085}
5086
5087template <class _RandomAccessIterator>
5088inline _LIBCPP_INLINE_VISIBILITY
5089void
5090make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5091{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005092 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005093}
5094
5095// sort_heap
5096
5097template <class _Compare, class _RandomAccessIterator>
5098void
5099__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5100{
5101 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5102 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
5103 __pop_heap<_Compare>(__first, __last, __comp, __n);
5104}
5105
5106template <class _RandomAccessIterator, class _Compare>
5107inline _LIBCPP_INLINE_VISIBILITY
5108void
5109sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5110{
Howard Hinnant5e571422013-08-23 20:10:18 +00005111#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005112 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5113 __debug_less<_Compare> __c(__comp);
5114 __sort_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005115#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005116 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5117 __sort_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005118#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005119}
5120
5121template <class _RandomAccessIterator>
5122inline _LIBCPP_INLINE_VISIBILITY
5123void
5124sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5125{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005126 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005127}
5128
5129// partial_sort
5130
5131template <class _Compare, class _RandomAccessIterator>
5132void
5133__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5134 _Compare __comp)
5135{
5136 __make_heap<_Compare>(__first, __middle, __comp);
5137 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5138 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5139 {
5140 if (__comp(*__i, *__first))
5141 {
5142 swap(*__i, *__first);
David Majnemercb8757a2014-07-22 06:07:09 +00005143 __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005144 }
5145 }
5146 __sort_heap<_Compare>(__first, __middle, __comp);
5147}
5148
5149template <class _RandomAccessIterator, class _Compare>
5150inline _LIBCPP_INLINE_VISIBILITY
5151void
5152partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5153 _Compare __comp)
5154{
Howard Hinnant5e571422013-08-23 20:10:18 +00005155#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005156 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5157 __debug_less<_Compare> __c(__comp);
5158 __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005159#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005160 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5161 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005162#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005163}
5164
5165template <class _RandomAccessIterator>
5166inline _LIBCPP_INLINE_VISIBILITY
5167void
5168partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5169{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005170 _VSTD::partial_sort(__first, __middle, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005171 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5172}
5173
5174// partial_sort_copy
5175
5176template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5177_RandomAccessIterator
5178__partial_sort_copy(_InputIterator __first, _InputIterator __last,
5179 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5180{
5181 _RandomAccessIterator __r = __result_first;
5182 if (__r != __result_last)
5183 {
Eric Fiselierb9919752014-10-27 19:28:20 +00005184 for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005185 *__r = *__first;
5186 __make_heap<_Compare>(__result_first, __r, __comp);
David Majnemercb8757a2014-07-22 06:07:09 +00005187 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005188 for (; __first != __last; ++__first)
5189 if (__comp(*__first, *__result_first))
5190 {
5191 *__result_first = *__first;
David Majnemercb8757a2014-07-22 06:07:09 +00005192 __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005193 }
5194 __sort_heap<_Compare>(__result_first, __r, __comp);
5195 }
5196 return __r;
5197}
5198
5199template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5200inline _LIBCPP_INLINE_VISIBILITY
5201_RandomAccessIterator
5202partial_sort_copy(_InputIterator __first, _InputIterator __last,
5203 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5204{
Howard Hinnant5e571422013-08-23 20:10:18 +00005205#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005206 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5207 __debug_less<_Compare> __c(__comp);
5208 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005209#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005210 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5211 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005212#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005213}
5214
5215template <class _InputIterator, class _RandomAccessIterator>
5216inline _LIBCPP_INLINE_VISIBILITY
5217_RandomAccessIterator
5218partial_sort_copy(_InputIterator __first, _InputIterator __last,
5219 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5220{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005221 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005222 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5223}
5224
5225// nth_element
5226
5227template <class _Compare, class _RandomAccessIterator>
5228void
5229__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5230{
5231 // _Compare is known to be a reference type
5232 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5233 const difference_type __limit = 7;
5234 while (true)
5235 {
5236 __restart:
Howard Hinnant8292d742011-12-29 17:45:35 +00005237 if (__nth == __last)
5238 return;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005239 difference_type __len = __last - __first;
5240 switch (__len)
5241 {
5242 case 0:
5243 case 1:
5244 return;
5245 case 2:
5246 if (__comp(*--__last, *__first))
5247 swap(*__first, *__last);
5248 return;
5249 case 3:
5250 {
5251 _RandomAccessIterator __m = __first;
Howard Hinnant0949eed2011-06-30 21:18:19 +00005252 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005253 return;
5254 }
5255 }
5256 if (__len <= __limit)
5257 {
5258 __selection_sort<_Compare>(__first, __last, __comp);
5259 return;
5260 }
5261 // __len > __limit >= 3
5262 _RandomAccessIterator __m = __first + __len/2;
5263 _RandomAccessIterator __lm1 = __last;
Howard Hinnant0949eed2011-06-30 21:18:19 +00005264 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005265 // *__m is median
5266 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5267 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5268 _RandomAccessIterator __i = __first;
5269 _RandomAccessIterator __j = __lm1;
5270 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5271 // The search going up is known to be guarded but the search coming down isn't.
5272 // Prime the downward search with a guard.
5273 if (!__comp(*__i, *__m)) // if *__first == *__m
5274 {
5275 // *__first == *__m, *__first doesn't go in first part
5276 // manually guard downward moving __j against __i
5277 while (true)
5278 {
5279 if (__i == --__j)
5280 {
5281 // *__first == *__m, *__m <= all other elements
5282 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5283 ++__i; // __first + 1
5284 __j = __last;
5285 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5286 {
5287 while (true)
5288 {
5289 if (__i == __j)
5290 return; // [__first, __last) all equivalent elements
5291 if (__comp(*__first, *__i))
5292 {
5293 swap(*__i, *__j);
5294 ++__n_swaps;
5295 ++__i;
5296 break;
5297 }
5298 ++__i;
5299 }
5300 }
5301 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5302 if (__i == __j)
5303 return;
5304 while (true)
5305 {
5306 while (!__comp(*__first, *__i))
5307 ++__i;
5308 while (__comp(*__first, *--__j))
5309 ;
5310 if (__i >= __j)
5311 break;
5312 swap(*__i, *__j);
5313 ++__n_swaps;
5314 ++__i;
5315 }
5316 // [__first, __i) == *__first and *__first < [__i, __last)
5317 // The first part is sorted,
5318 if (__nth < __i)
5319 return;
5320 // __nth_element the secod part
5321 // __nth_element<_Compare>(__i, __nth, __last, __comp);
5322 __first = __i;
5323 goto __restart;
5324 }
5325 if (__comp(*__j, *__m))
5326 {
5327 swap(*__i, *__j);
5328 ++__n_swaps;
5329 break; // found guard for downward moving __j, now use unguarded partition
5330 }
5331 }
5332 }
5333 ++__i;
5334 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5335 // if not yet partitioned...
5336 if (__i < __j)
5337 {
5338 // known that *(__i - 1) < *__m
5339 while (true)
5340 {
5341 // __m still guards upward moving __i
5342 while (__comp(*__i, *__m))
5343 ++__i;
5344 // It is now known that a guard exists for downward moving __j
5345 while (!__comp(*--__j, *__m))
5346 ;
5347 if (__i >= __j)
5348 break;
5349 swap(*__i, *__j);
5350 ++__n_swaps;
5351 // It is known that __m != __j
5352 // If __m just moved, follow it
5353 if (__m == __i)
5354 __m = __j;
5355 ++__i;
5356 }
5357 }
5358 // [__first, __i) < *__m and *__m <= [__i, __last)
5359 if (__i != __m && __comp(*__m, *__i))
5360 {
5361 swap(*__i, *__m);
5362 ++__n_swaps;
5363 }
5364 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5365 if (__nth == __i)
5366 return;
5367 if (__n_swaps == 0)
5368 {
5369 // We were given a perfectly partitioned sequence. Coincidence?
5370 if (__nth < __i)
5371 {
5372 // Check for [__first, __i) already sorted
5373 __j = __m = __first;
5374 while (++__j != __i)
5375 {
5376 if (__comp(*__j, *__m))
5377 // not yet sorted, so sort
5378 goto not_sorted;
5379 __m = __j;
5380 }
5381 // [__first, __i) sorted
5382 return;
5383 }
5384 else
5385 {
5386 // Check for [__i, __last) already sorted
5387 __j = __m = __i;
5388 while (++__j != __last)
5389 {
5390 if (__comp(*__j, *__m))
5391 // not yet sorted, so sort
5392 goto not_sorted;
5393 __m = __j;
5394 }
5395 // [__i, __last) sorted
5396 return;
5397 }
5398 }
5399not_sorted:
5400 // __nth_element on range containing __nth
5401 if (__nth < __i)
5402 {
5403 // __nth_element<_Compare>(__first, __nth, __i, __comp);
5404 __last = __i;
5405 }
5406 else
5407 {
5408 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5409 __first = ++__i;
5410 }
5411 }
5412}
5413
5414template <class _RandomAccessIterator, class _Compare>
5415inline _LIBCPP_INLINE_VISIBILITY
5416void
5417nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5418{
Howard Hinnant5e571422013-08-23 20:10:18 +00005419#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005420 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5421 __debug_less<_Compare> __c(__comp);
5422 __nth_element<_Comp_ref>(__first, __nth, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005423#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005424 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5425 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005426#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005427}
5428
5429template <class _RandomAccessIterator>
5430inline _LIBCPP_INLINE_VISIBILITY
5431void
5432nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5433{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005434 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005435}
5436
5437// includes
5438
5439template <class _Compare, class _InputIterator1, class _InputIterator2>
5440bool
5441__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5442 _Compare __comp)
5443{
5444 for (; __first2 != __last2; ++__first1)
5445 {
5446 if (__first1 == __last1 || __comp(*__first2, *__first1))
5447 return false;
5448 if (!__comp(*__first1, *__first2))
5449 ++__first2;
5450 }
5451 return true;
5452}
5453
5454template <class _InputIterator1, class _InputIterator2, class _Compare>
5455inline _LIBCPP_INLINE_VISIBILITY
5456bool
5457includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5458 _Compare __comp)
5459{
Howard Hinnant5e571422013-08-23 20:10:18 +00005460#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005461 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5462 __debug_less<_Compare> __c(__comp);
5463 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005464#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005465 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5466 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005467#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005468}
5469
5470template <class _InputIterator1, class _InputIterator2>
5471inline _LIBCPP_INLINE_VISIBILITY
5472bool
5473includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5474{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005475 return _VSTD::includes(__first1, __last1, __first2, __last2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005476 __less<typename iterator_traits<_InputIterator1>::value_type,
5477 typename iterator_traits<_InputIterator2>::value_type>());
5478}
5479
5480// set_union
5481
5482template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5483_OutputIterator
5484__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5485 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5486{
5487 for (; __first1 != __last1; ++__result)
5488 {
5489 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005490 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005491 if (__comp(*__first2, *__first1))
5492 {
5493 *__result = *__first2;
5494 ++__first2;
5495 }
5496 else
5497 {
5498 *__result = *__first1;
5499 if (!__comp(*__first1, *__first2))
5500 ++__first2;
5501 ++__first1;
5502 }
5503 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00005504 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005505}
5506
5507template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5508inline _LIBCPP_INLINE_VISIBILITY
5509_OutputIterator
5510set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5511 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5512{
Howard Hinnant5e571422013-08-23 20:10:18 +00005513#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005514 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5515 __debug_less<_Compare> __c(__comp);
5516 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005517#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005518 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5519 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005520#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005521}
5522
5523template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5524inline _LIBCPP_INLINE_VISIBILITY
5525_OutputIterator
5526set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5527 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5528{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005529 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005530 __less<typename iterator_traits<_InputIterator1>::value_type,
5531 typename iterator_traits<_InputIterator2>::value_type>());
5532}
5533
5534// set_intersection
5535
5536template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5537_OutputIterator
5538__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5539 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5540{
5541 while (__first1 != __last1 && __first2 != __last2)
5542 {
5543 if (__comp(*__first1, *__first2))
5544 ++__first1;
5545 else
5546 {
5547 if (!__comp(*__first2, *__first1))
5548 {
5549 *__result = *__first1;
5550 ++__result;
5551 ++__first1;
5552 }
5553 ++__first2;
5554 }
5555 }
5556 return __result;
5557}
5558
5559template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5560inline _LIBCPP_INLINE_VISIBILITY
5561_OutputIterator
5562set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5563 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5564{
Howard Hinnant5e571422013-08-23 20:10:18 +00005565#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005566 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5567 __debug_less<_Compare> __c(__comp);
5568 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005569#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005570 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5571 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005572#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005573}
5574
5575template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5576inline _LIBCPP_INLINE_VISIBILITY
5577_OutputIterator
5578set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5579 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5580{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005581 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005582 __less<typename iterator_traits<_InputIterator1>::value_type,
5583 typename iterator_traits<_InputIterator2>::value_type>());
5584}
5585
5586// set_difference
5587
5588template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5589_OutputIterator
5590__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5591 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5592{
5593 while (__first1 != __last1)
5594 {
5595 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005596 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005597 if (__comp(*__first1, *__first2))
5598 {
5599 *__result = *__first1;
5600 ++__result;
5601 ++__first1;
5602 }
5603 else
5604 {
5605 if (!__comp(*__first2, *__first1))
5606 ++__first1;
5607 ++__first2;
5608 }
5609 }
5610 return __result;
5611}
5612
5613template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5614inline _LIBCPP_INLINE_VISIBILITY
5615_OutputIterator
5616set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5617 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5618{
Howard Hinnant5e571422013-08-23 20:10:18 +00005619#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005620 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5621 __debug_less<_Compare> __c(__comp);
5622 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005623#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005624 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5625 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005626#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005627}
5628
5629template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5630inline _LIBCPP_INLINE_VISIBILITY
5631_OutputIterator
5632set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5633 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5634{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005635 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005636 __less<typename iterator_traits<_InputIterator1>::value_type,
5637 typename iterator_traits<_InputIterator2>::value_type>());
5638}
5639
5640// set_symmetric_difference
5641
5642template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5643_OutputIterator
5644__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5645 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5646{
5647 while (__first1 != __last1)
5648 {
5649 if (__first2 == __last2)
Howard Hinnant0949eed2011-06-30 21:18:19 +00005650 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005651 if (__comp(*__first1, *__first2))
5652 {
5653 *__result = *__first1;
5654 ++__result;
5655 ++__first1;
5656 }
5657 else
5658 {
5659 if (__comp(*__first2, *__first1))
5660 {
5661 *__result = *__first2;
5662 ++__result;
5663 }
5664 else
5665 ++__first1;
5666 ++__first2;
5667 }
5668 }
Howard Hinnant0949eed2011-06-30 21:18:19 +00005669 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005670}
5671
5672template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5673inline _LIBCPP_INLINE_VISIBILITY
5674_OutputIterator
5675set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5676 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5677{
Howard Hinnant5e571422013-08-23 20:10:18 +00005678#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005679 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5680 __debug_less<_Compare> __c(__comp);
5681 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005682#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005683 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5684 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005685#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005686}
5687
5688template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5689inline _LIBCPP_INLINE_VISIBILITY
5690_OutputIterator
5691set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5692 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5693{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005694 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005695 __less<typename iterator_traits<_InputIterator1>::value_type,
5696 typename iterator_traits<_InputIterator2>::value_type>());
5697}
5698
5699// lexicographical_compare
5700
5701template <class _Compare, class _InputIterator1, class _InputIterator2>
5702bool
5703__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5704 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5705{
Eric Fiselierb9919752014-10-27 19:28:20 +00005706 for (; __first2 != __last2; ++__first1, (void) ++__first2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005707 {
5708 if (__first1 == __last1 || __comp(*__first1, *__first2))
5709 return true;
5710 if (__comp(*__first2, *__first1))
5711 return false;
5712 }
5713 return false;
5714}
5715
5716template <class _InputIterator1, class _InputIterator2, class _Compare>
5717inline _LIBCPP_INLINE_VISIBILITY
5718bool
5719lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5720 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5721{
Howard Hinnant5e571422013-08-23 20:10:18 +00005722#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005723 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5724 __debug_less<_Compare> __c(__comp);
5725 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005726#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005727 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5728 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005729#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005730}
5731
5732template <class _InputIterator1, class _InputIterator2>
5733inline _LIBCPP_INLINE_VISIBILITY
5734bool
5735lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5736 _InputIterator2 __first2, _InputIterator2 __last2)
5737{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005738 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005739 __less<typename iterator_traits<_InputIterator1>::value_type,
5740 typename iterator_traits<_InputIterator2>::value_type>());
5741}
5742
5743// next_permutation
5744
5745template <class _Compare, class _BidirectionalIterator>
5746bool
5747__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5748{
5749 _BidirectionalIterator __i = __last;
5750 if (__first == __last || __first == --__i)
5751 return false;
5752 while (true)
5753 {
5754 _BidirectionalIterator __ip1 = __i;
5755 if (__comp(*--__i, *__ip1))
5756 {
5757 _BidirectionalIterator __j = __last;
5758 while (!__comp(*__i, *--__j))
5759 ;
5760 swap(*__i, *__j);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005761 _VSTD::reverse(__ip1, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005762 return true;
5763 }
5764 if (__i == __first)
5765 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005766 _VSTD::reverse(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005767 return false;
5768 }
5769 }
5770}
5771
5772template <class _BidirectionalIterator, class _Compare>
5773inline _LIBCPP_INLINE_VISIBILITY
5774bool
5775next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5776{
Howard Hinnant5e571422013-08-23 20:10:18 +00005777#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005778 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5779 __debug_less<_Compare> __c(__comp);
5780 return __next_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005781#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005782 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5783 return __next_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005784#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005785}
5786
5787template <class _BidirectionalIterator>
5788inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant324bb032010-08-22 00:02:43 +00005789bool
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005790next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5791{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005792 return _VSTD::next_permutation(__first, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005793 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5794}
5795
5796// prev_permutation
5797
5798template <class _Compare, class _BidirectionalIterator>
5799bool
5800__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5801{
5802 _BidirectionalIterator __i = __last;
5803 if (__first == __last || __first == --__i)
5804 return false;
5805 while (true)
5806 {
5807 _BidirectionalIterator __ip1 = __i;
5808 if (__comp(*__ip1, *--__i))
5809 {
5810 _BidirectionalIterator __j = __last;
5811 while (!__comp(*--__j, *__i))
5812 ;
5813 swap(*__i, *__j);
Howard Hinnant0949eed2011-06-30 21:18:19 +00005814 _VSTD::reverse(__ip1, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005815 return true;
5816 }
5817 if (__i == __first)
5818 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00005819 _VSTD::reverse(__first, __last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005820 return false;
5821 }
5822 }
5823}
5824
5825template <class _BidirectionalIterator, class _Compare>
5826inline _LIBCPP_INLINE_VISIBILITY
5827bool
5828prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5829{
Howard Hinnant5e571422013-08-23 20:10:18 +00005830#ifdef _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005831 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5832 __debug_less<_Compare> __c(__comp);
5833 return __prev_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant5e571422013-08-23 20:10:18 +00005834#else // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005835 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5836 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant5e571422013-08-23 20:10:18 +00005837#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005838}
5839
5840template <class _BidirectionalIterator>
5841inline _LIBCPP_INLINE_VISIBILITY
5842bool
5843prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5844{
Howard Hinnant0949eed2011-06-30 21:18:19 +00005845 return _VSTD::prev_permutation(__first, __last,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005846 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5847}
5848
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005849_LIBCPP_END_NAMESPACE_STD
5850
5851#endif // _LIBCPP_ALGORITHM