blob: d102899f2dfceaa03a6c65a451c5887c51f8e2c7 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:161// -*- C++ -*-
2//===-------------------------- algorithm ---------------------------------===//
3//
Howard Hinnant5b08a8a2010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:165//
Howard Hinnant412dbeb2010-11-16 22:09:026// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:168//
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>
Marshall Clow706ffef2018-01-15 17:20:3623 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1624 all_of(InputIterator first, InputIterator last, Predicate pred);
25
26template <class InputIterator, class Predicate>
Marshall Clow706ffef2018-01-15 17:20:3627 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1628 any_of(InputIterator first, InputIterator last, Predicate pred);
29
30template <class InputIterator, class Predicate>
Marshall Clow706ffef2018-01-15 17:20:3631 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1632 none_of(InputIterator first, InputIterator last, Predicate pred);
33
34template <class InputIterator, class Function>
Marshall Clow1b9a4ff2018-01-22 20:44:3335 constexpr Function // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1636 for_each(InputIterator first, InputIterator last, Function f);
37
Marshall Clowd5c65ff2017-05-25 02:29:5438template<class InputIterator, class Size, class Function>
Marshall Clow1b9a4ff2018-01-22 20:44:3339 constexpr InputIterator // constexpr in C++20
40 for_each_n(InputIterator first, Size n, Function f); // C++17
Marshall Clowd5c65ff2017-05-25 02:29:5441
Howard Hinnant3e519522010-05-11 19:42:1642template <class InputIterator, class T>
Marshall Clow86944282018-01-15 19:26:0543 constexpr InputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1644 find(InputIterator first, InputIterator last, const T& value);
45
46template <class InputIterator, class Predicate>
Marshall Clow86944282018-01-15 19:26:0547 constexpr InputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1648 find_if(InputIterator first, InputIterator last, Predicate pred);
49
50template<class InputIterator, class Predicate>
Marshall Clow86944282018-01-15 19:26:0551 InputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1652 find_if_not(InputIterator first, InputIterator last, Predicate pred);
53
54template <class ForwardIterator1, class ForwardIterator2>
Marshall Clow86944282018-01-15 19:26:0555 ForwardIterator1 // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1656 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
57 ForwardIterator2 first2, ForwardIterator2 last2);
58
59template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clow86944282018-01-15 19:26:0560 ForwardIterator1 // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1661 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
62 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
63
64template <class ForwardIterator1, class ForwardIterator2>
Marshall Clow86944282018-01-15 19:26:0565 constexpr ForwardIterator1 // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1666 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
67 ForwardIterator2 first2, ForwardIterator2 last2);
68
69template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clow86944282018-01-15 19:26:0570 constexpr ForwardIterator1 // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1671 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
72 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
73
74template <class ForwardIterator>
Marshall Clow86944282018-01-15 19:26:0575 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1676 adjacent_find(ForwardIterator first, ForwardIterator last);
77
78template <class ForwardIterator, class BinaryPredicate>
Marshall Clow86944282018-01-15 19:26:0579 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1680 adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
81
82template <class InputIterator, class T>
Marshall Clow056f15e2018-01-15 19:40:3483 constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1684 count(InputIterator first, InputIterator last, const T& value);
85
86template <class InputIterator, class Predicate>
Marshall Clow056f15e2018-01-15 19:40:3487 constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1688 count_if(InputIterator first, InputIterator last, Predicate pred);
89
90template <class InputIterator1, class InputIterator2>
Marshall Clow6538e28d2018-01-16 02:04:1091 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1692 mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
93
Marshall Clow0b0bbd22013-05-09 21:14:2394template <class InputIterator1, class InputIterator2>
Marshall Clow6538e28d2018-01-16 02:04:1095 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
Aditya Kumar331fb802016-08-25 11:52:3896 mismatch(InputIterator1 first1, InputIterator1 last1,
Marshall Clow0b0bbd22013-05-09 21:14:2397 InputIterator2 first2, InputIterator2 last2); // **C++14**
98
Howard Hinnant3e519522010-05-11 19:42:1699template <class InputIterator1, class InputIterator2, class BinaryPredicate>
Marshall Clow6538e28d2018-01-16 02:04:10100 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16101 mismatch(InputIterator1 first1, InputIterator1 last1,
102 InputIterator2 first2, BinaryPredicate pred);
103
Marshall Clow0b0bbd22013-05-09 21:14:23104template <class InputIterator1, class InputIterator2, class BinaryPredicate>
Marshall Clow6538e28d2018-01-16 02:04:10105 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
Marshall Clow0b0bbd22013-05-09 21:14:23106 mismatch(InputIterator1 first1, InputIterator1 last1,
107 InputIterator2 first2, InputIterator2 last2,
108 BinaryPredicate pred); // **C++14**
109
Howard Hinnant3e519522010-05-11 19:42:16110template <class InputIterator1, class InputIterator2>
Marshall Clow6538e28d2018-01-16 02:04:10111 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16112 equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
113
Marshall Clow0b0bbd22013-05-09 21:14:23114template <class InputIterator1, class InputIterator2>
Marshall Clow6538e28d2018-01-16 02:04:10115 constexpr bool // constexpr in C++20
Aditya Kumar331fb802016-08-25 11:52:38116 equal(InputIterator1 first1, InputIterator1 last1,
Marshall Clow0b0bbd22013-05-09 21:14:23117 InputIterator2 first2, InputIterator2 last2); // **C++14**
118
Howard Hinnant3e519522010-05-11 19:42:16119template <class InputIterator1, class InputIterator2, class BinaryPredicate>
Marshall Clow6538e28d2018-01-16 02:04:10120 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16121 equal(InputIterator1 first1, InputIterator1 last1,
122 InputIterator2 first2, BinaryPredicate pred);
123
Marshall Clow0b0bbd22013-05-09 21:14:23124template <class InputIterator1, class InputIterator2, class BinaryPredicate>
Marshall Clow6538e28d2018-01-16 02:04:10125 constexpr bool // constexpr in C++20
Marshall Clow0b0bbd22013-05-09 21:14:23126 equal(InputIterator1 first1, InputIterator1 last1,
127 InputIterator2 first2, InputIterator2 last2,
128 BinaryPredicate pred); // **C++14**
129
Howard Hinnant3e519522010-05-11 19:42:16130template<class ForwardIterator1, class ForwardIterator2>
Marshall Clow49c76432018-01-15 16:16:32131 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16132 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
133 ForwardIterator2 first2);
134
Marshall Clow0b0bbd22013-05-09 21:14:23135template<class ForwardIterator1, class ForwardIterator2>
Marshall Clow49c76432018-01-15 16:16:32136 constexpr bool // constexpr in C++20
Marshall Clow0b0bbd22013-05-09 21:14:23137 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
138 ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
139
Howard Hinnant3e519522010-05-11 19:42:16140template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clow49c76432018-01-15 16:16:32141 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16142 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
143 ForwardIterator2 first2, BinaryPredicate pred);
144
Marshall Clow0b0bbd22013-05-09 21:14:23145template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clow49c76432018-01-15 16:16:32146 constexpr bool // constexpr in C++20
Marshall Clow0b0bbd22013-05-09 21:14:23147 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
148 ForwardIterator2 first2, ForwardIterator2 last2,
149 BinaryPredicate pred); // **C++14**
150
Howard Hinnant3e519522010-05-11 19:42:16151template <class ForwardIterator1, class ForwardIterator2>
Marshall Clow12f0a772018-01-16 15:48:27152 constexpr ForwardIterator1 // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16153 search(ForwardIterator1 first1, ForwardIterator1 last1,
154 ForwardIterator2 first2, ForwardIterator2 last2);
155
156template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clow12f0a772018-01-16 15:48:27157 constexpr ForwardIterator1 // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16158 search(ForwardIterator1 first1, ForwardIterator1 last1,
159 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
160
161template <class ForwardIterator, class Size, class T>
Marshall Clow12f0a772018-01-16 15:48:27162 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16163 search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
164
165template <class ForwardIterator, class Size, class T, class BinaryPredicate>
Marshall Clow12f0a772018-01-16 15:48:27166 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16167 search_n(ForwardIterator first, ForwardIterator last,
168 Size count, const T& value, BinaryPredicate pred);
169
170template <class InputIterator, class OutputIterator>
171 OutputIterator
172 copy(InputIterator first, InputIterator last, OutputIterator result);
173
174template<class InputIterator, class OutputIterator, class Predicate>
175 OutputIterator
176 copy_if(InputIterator first, InputIterator last,
177 OutputIterator result, Predicate pred);
178
179template<class InputIterator, class Size, class OutputIterator>
180 OutputIterator
181 copy_n(InputIterator first, Size n, OutputIterator result);
182
183template <class BidirectionalIterator1, class BidirectionalIterator2>
184 BidirectionalIterator2
185 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
186 BidirectionalIterator2 result);
187
188template <class ForwardIterator1, class ForwardIterator2>
189 ForwardIterator2
190 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
191
192template <class ForwardIterator1, class ForwardIterator2>
193 void
194 iter_swap(ForwardIterator1 a, ForwardIterator2 b);
195
196template <class InputIterator, class OutputIterator, class UnaryOperation>
Marshall Clow99894b62018-01-19 17:45:39197 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16198 transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
199
200template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
Marshall Clow99894b62018-01-19 17:45:39201 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16202 transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
203 OutputIterator result, BinaryOperation binary_op);
204
205template <class ForwardIterator, class T>
Marshall Clow12c74232018-01-19 18:07:29206 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16207 replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
208
209template <class ForwardIterator, class Predicate, class T>
Marshall Clow12c74232018-01-19 18:07:29210 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16211 replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
212
213template <class InputIterator, class OutputIterator, class T>
Marshall Clow12c74232018-01-19 18:07:29214 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16215 replace_copy(InputIterator first, InputIterator last, OutputIterator result,
216 const T& old_value, const T& new_value);
217
218template <class InputIterator, class OutputIterator, class Predicate, class T>
Marshall Clow12c74232018-01-19 18:07:29219 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16220 replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
221
222template <class ForwardIterator, class T>
Marshall Clow4bfb9312018-01-20 20:14:32223 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16224 fill(ForwardIterator first, ForwardIterator last, const T& value);
225
226template <class OutputIterator, class Size, class T>
Marshall Clow4bfb9312018-01-20 20:14:32227 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16228 fill_n(OutputIterator first, Size n, const T& value);
229
230template <class ForwardIterator, class Generator>
Marshall Clow4bfb9312018-01-20 20:14:32231 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16232 generate(ForwardIterator first, ForwardIterator last, Generator gen);
233
234template <class OutputIterator, class Size, class Generator>
Marshall Clow4bfb9312018-01-20 20:14:32235 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16236 generate_n(OutputIterator first, Size n, Generator gen);
237
238template <class ForwardIterator, class T>
Marshall Clowe8ea8292018-01-22 21:43:04239 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16240 remove(ForwardIterator first, ForwardIterator last, const T& value);
241
242template <class ForwardIterator, class Predicate>
Marshall Clowe8ea8292018-01-22 21:43:04243 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16244 remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
245
246template <class InputIterator, class OutputIterator, class T>
Marshall Clowe8ea8292018-01-22 21:43:04247 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16248 remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
249
250template <class InputIterator, class OutputIterator, class Predicate>
Marshall Clowe8ea8292018-01-22 21:43:04251 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16252 remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
253
254template <class ForwardIterator>
255 ForwardIterator
256 unique(ForwardIterator first, ForwardIterator last);
257
258template <class ForwardIterator, class BinaryPredicate>
259 ForwardIterator
260 unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
261
262template <class InputIterator, class OutputIterator>
263 OutputIterator
264 unique_copy(InputIterator first, InputIterator last, OutputIterator result);
265
266template <class InputIterator, class OutputIterator, class BinaryPredicate>
267 OutputIterator
268 unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
269
270template <class BidirectionalIterator>
271 void
272 reverse(BidirectionalIterator first, BidirectionalIterator last);
273
274template <class BidirectionalIterator, class OutputIterator>
Marshall Clowe8ea8292018-01-22 21:43:04275 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16276 reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
277
278template <class ForwardIterator>
279 ForwardIterator
280 rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
281
282template <class ForwardIterator, class OutputIterator>
283 OutputIterator
284 rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
285
286template <class RandomAccessIterator>
287 void
Marshall Clow0f37a412017-03-23 13:43:37288 random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14, removed in C++17
Howard Hinnant3e519522010-05-11 19:42:16289
290template <class RandomAccessIterator, class RandomNumberGenerator>
291 void
Marshall Clow06965c12014-03-03 06:14:19292 random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
Marshall Clow0f37a412017-03-23 13:43:37293 RandomNumberGenerator& rand); // deprecated in C++14, removed in C++17
Howard Hinnant3e519522010-05-11 19:42:16294
Eric Fiseliere7154702016-08-28 22:14:37295template<class PopulationIterator, class SampleIterator,
296 class Distance, class UniformRandomBitGenerator>
297 SampleIterator sample(PopulationIterator first, PopulationIterator last,
298 SampleIterator out, Distance n,
299 UniformRandomBitGenerator&& g); // C++17
300
Howard Hinnantf9d540b2010-05-26 17:49:34301template<class RandomAccessIterator, class UniformRandomNumberGenerator>
302 void shuffle(RandomAccessIterator first, RandomAccessIterator last,
Howard Hinnantfb340102010-11-18 01:47:02303 UniformRandomNumberGenerator&& g);
Howard Hinnantf9d540b2010-05-26 17:49:34304
Howard Hinnant3e519522010-05-11 19:42:16305template <class InputIterator, class Predicate>
Marshall Clow49c76432018-01-15 16:16:32306 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16307 is_partitioned(InputIterator first, InputIterator last, Predicate pred);
308
309template <class ForwardIterator, class Predicate>
310 ForwardIterator
311 partition(ForwardIterator first, ForwardIterator last, Predicate pred);
312
313template <class InputIterator, class OutputIterator1,
314 class OutputIterator2, class Predicate>
Marshall Clow1b9a4ff2018-01-22 20:44:33315 constexpr pair<OutputIterator1, OutputIterator2> // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16316 partition_copy(InputIterator first, InputIterator last,
317 OutputIterator1 out_true, OutputIterator2 out_false,
318 Predicate pred);
319
320template <class ForwardIterator, class Predicate>
321 ForwardIterator
322 stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
323
324template<class ForwardIterator, class Predicate>
Marshall Clowd57c03d2018-01-16 02:34:41325 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16326 partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
327
328template <class ForwardIterator>
Marshall Clow49c76432018-01-15 16:16:32329 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16330 is_sorted(ForwardIterator first, ForwardIterator last);
331
332template <class ForwardIterator, class Compare>
333 bool
334 is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
335
336template<class ForwardIterator>
Marshall Clow056f15e2018-01-15 19:40:34337 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16338 is_sorted_until(ForwardIterator first, ForwardIterator last);
339
340template <class ForwardIterator, class Compare>
Marshall Clow056f15e2018-01-15 19:40:34341 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16342 is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
343
344template <class RandomAccessIterator>
345 void
346 sort(RandomAccessIterator first, RandomAccessIterator last);
347
348template <class RandomAccessIterator, class Compare>
349 void
350 sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
351
352template <class RandomAccessIterator>
353 void
354 stable_sort(RandomAccessIterator first, RandomAccessIterator last);
355
356template <class RandomAccessIterator, class Compare>
357 void
358 stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
359
360template <class RandomAccessIterator>
361 void
362 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
363
364template <class RandomAccessIterator, class Compare>
365 void
366 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
367
368template <class InputIterator, class RandomAccessIterator>
369 RandomAccessIterator
370 partial_sort_copy(InputIterator first, InputIterator last,
371 RandomAccessIterator result_first, RandomAccessIterator result_last);
372
373template <class InputIterator, class RandomAccessIterator, class Compare>
374 RandomAccessIterator
375 partial_sort_copy(InputIterator first, InputIterator last,
376 RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
377
378template <class RandomAccessIterator>
379 void
380 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
381
382template <class RandomAccessIterator, class Compare>
383 void
384 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
385
386template <class ForwardIterator, class T>
Marshall Clowd57c03d2018-01-16 02:34:41387 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16388 lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
389
390template <class ForwardIterator, class T, class Compare>
Marshall Clowd57c03d2018-01-16 02:34:41391 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16392 lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
393
394template <class ForwardIterator, class T>
Marshall Clowd57c03d2018-01-16 02:34:41395 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16396 upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
397
398template <class ForwardIterator, class T, class Compare>
Marshall Clowd57c03d2018-01-16 02:34:41399 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16400 upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
401
402template <class ForwardIterator, class T>
Marshall Clowd57c03d2018-01-16 02:34:41403 constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16404 equal_range(ForwardIterator first, ForwardIterator last, const T& value);
405
406template <class ForwardIterator, class T, class Compare>
Marshall Clowd57c03d2018-01-16 02:34:41407 constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16408 equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
409
410template <class ForwardIterator, class T>
Marshall Clowd57c03d2018-01-16 02:34:41411 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16412 binary_search(ForwardIterator first, ForwardIterator last, const T& value);
413
414template <class ForwardIterator, class T, class Compare>
Marshall Clow8da1a482018-01-22 23:10:40415 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16416 binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
417
418template <class InputIterator1, class InputIterator2, class OutputIterator>
419 OutputIterator
420 merge(InputIterator1 first1, InputIterator1 last1,
421 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
422
423template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
424 OutputIterator
425 merge(InputIterator1 first1, InputIterator1 last1,
426 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
427
428template <class BidirectionalIterator>
429 void
430 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
431
432template <class BidirectionalIterator, class Compare>
433 void
434 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
435
436template <class InputIterator1, class InputIterator2>
Marshall Clow8da1a482018-01-22 23:10:40437 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16438 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
439
440template <class InputIterator1, class InputIterator2, class Compare>
Marshall Clow8da1a482018-01-22 23:10:40441 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16442 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
443
444template <class InputIterator1, class InputIterator2, class OutputIterator>
445 OutputIterator
446 set_union(InputIterator1 first1, InputIterator1 last1,
447 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
448
449template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
450 OutputIterator
451 set_union(InputIterator1 first1, InputIterator1 last1,
452 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
453
454template <class InputIterator1, class InputIterator2, class OutputIterator>
Marshall Clow8da1a482018-01-22 23:10:40455 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16456 set_intersection(InputIterator1 first1, InputIterator1 last1,
457 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
458
459template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
Marshall Clow8da1a482018-01-22 23:10:40460 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16461 set_intersection(InputIterator1 first1, InputIterator1 last1,
462 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
463
464template <class InputIterator1, class InputIterator2, class OutputIterator>
465 OutputIterator
466 set_difference(InputIterator1 first1, InputIterator1 last1,
467 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
468
469template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
470 OutputIterator
471 set_difference(InputIterator1 first1, InputIterator1 last1,
472 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
473
474template <class InputIterator1, class InputIterator2, class OutputIterator>
475 OutputIterator
476 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
477 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
478
479template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
480 OutputIterator
481 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
482 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
483
484template <class RandomAccessIterator>
485 void
486 push_heap(RandomAccessIterator first, RandomAccessIterator last);
487
488template <class RandomAccessIterator, class Compare>
489 void
490 push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
491
492template <class RandomAccessIterator>
493 void
494 pop_heap(RandomAccessIterator first, RandomAccessIterator last);
495
496template <class RandomAccessIterator, class Compare>
497 void
498 pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
499
500template <class RandomAccessIterator>
501 void
502 make_heap(RandomAccessIterator first, RandomAccessIterator last);
503
504template <class RandomAccessIterator, class Compare>
505 void
506 make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
507
508template <class RandomAccessIterator>
509 void
510 sort_heap(RandomAccessIterator first, RandomAccessIterator last);
511
512template <class RandomAccessIterator, class Compare>
513 void
514 sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
515
Howard Hinnantb3371f62010-08-22 00:02:43516template <class RandomAccessIterator>
Marshall Clow49c76432018-01-15 16:16:32517 constexpr bool // constexpr in C++20
Howard Hinnantb3371f62010-08-22 00:02:43518 is_heap(RandomAccessIterator first, RandomAccessiterator last);
Howard Hinnant3e519522010-05-11 19:42:16519
Howard Hinnantb3371f62010-08-22 00:02:43520template <class RandomAccessIterator, class Compare>
Marshall Clow49c76432018-01-15 16:16:32521 constexpr bool // constexpr in C++20
Howard Hinnantb3371f62010-08-22 00:02:43522 is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
Howard Hinnant3e519522010-05-11 19:42:16523
Howard Hinnantb3371f62010-08-22 00:02:43524template <class RandomAccessIterator>
Marshall Clow49c76432018-01-15 16:16:32525 constexpr RandomAccessIterator // constexpr in C++20
Howard Hinnantb3371f62010-08-22 00:02:43526 is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
Howard Hinnant3e519522010-05-11 19:42:16527
Howard Hinnantb3371f62010-08-22 00:02:43528template <class RandomAccessIterator, class Compare>
Marshall Clow49c76432018-01-15 16:16:32529 constexpr RandomAccessIterator // constexpr in C++20
Howard Hinnantb3371f62010-08-22 00:02:43530 is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
Howard Hinnant3e519522010-05-11 19:42:16531
Howard Hinnant4eb27b72010-08-21 20:10:01532template <class ForwardIterator>
533 ForwardIterator
Marshall Clow0b0671a2015-05-10 13:53:31534 min_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnant4eb27b72010-08-21 20:10:01535
536template <class ForwardIterator, class Compare>
537 ForwardIterator
Marshall Clow0b0671a2015-05-10 13:53:31538 min_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnant4eb27b72010-08-21 20:10:01539
Howard Hinnant3e519522010-05-11 19:42:16540template <class T>
541 const T&
Marshall Clow9d67c6d2014-02-19 16:51:35542 min(const T& a, const T& b); // constexpr in C++14
Howard Hinnant3e519522010-05-11 19:42:16543
544template <class T, class Compare>
545 const T&
Marshall Clow9d67c6d2014-02-19 16:51:35546 min(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnant3e519522010-05-11 19:42:16547
Howard Hinnant4eb27b72010-08-21 20:10:01548template<class T>
549 T
Marshall Clow9d67c6d2014-02-19 16:51:35550 min(initializer_list<T> t); // constexpr in C++14
Howard Hinnant4eb27b72010-08-21 20:10:01551
552template<class T, class Compare>
553 T
Marshall Clow9d67c6d2014-02-19 16:51:35554 min(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnant4eb27b72010-08-21 20:10:01555
Marshall Clow146c14a2016-03-07 22:43:49556template<class T>
557 constexpr const T& clamp( const T& v, const T& lo, const T& hi ); // C++17
558
559template<class T, class Compare>
560 constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp ); // C++17
561
Howard Hinnant4eb27b72010-08-21 20:10:01562template <class ForwardIterator>
563 ForwardIterator
Marshall Clow0b0671a2015-05-10 13:53:31564 max_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnant4eb27b72010-08-21 20:10:01565
566template <class ForwardIterator, class Compare>
567 ForwardIterator
Marshall Clow0b0671a2015-05-10 13:53:31568 max_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnant4eb27b72010-08-21 20:10:01569
Howard Hinnant3e519522010-05-11 19:42:16570template <class T>
571 const T&
Marshall Clow9d67c6d2014-02-19 16:51:35572 max(const T& a, const T& b); // constexpr in C++14
Howard Hinnant3e519522010-05-11 19:42:16573
574template <class T, class Compare>
575 const T&
Marshall Clow9d67c6d2014-02-19 16:51:35576 max(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnant3e519522010-05-11 19:42:16577
Howard Hinnant4eb27b72010-08-21 20:10:01578template<class T>
579 T
Marshall Clow9d67c6d2014-02-19 16:51:35580 max(initializer_list<T> t); // constexpr in C++14
Howard Hinnant3e519522010-05-11 19:42:16581
Howard Hinnant4eb27b72010-08-21 20:10:01582template<class T, class Compare>
583 T
Marshall Clow9d67c6d2014-02-19 16:51:35584 max(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnant3e519522010-05-11 19:42:16585
Howard Hinnant4eb27b72010-08-21 20:10:01586template<class ForwardIterator>
587 pair<ForwardIterator, ForwardIterator>
Marshall Clow0b0671a2015-05-10 13:53:31588 minmax_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnant3e519522010-05-11 19:42:16589
Howard Hinnant4eb27b72010-08-21 20:10:01590template<class ForwardIterator, class Compare>
591 pair<ForwardIterator, ForwardIterator>
Marshall Clow0b0671a2015-05-10 13:53:31592 minmax_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnant4eb27b72010-08-21 20:10:01593
594template<class T>
595 pair<const T&, const T&>
Marshall Clow9d67c6d2014-02-19 16:51:35596 minmax(const T& a, const T& b); // constexpr in C++14
Howard Hinnant4eb27b72010-08-21 20:10:01597
598template<class T, class Compare>
599 pair<const T&, const T&>
Marshall Clow9d67c6d2014-02-19 16:51:35600 minmax(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnant4eb27b72010-08-21 20:10:01601
602template<class T>
603 pair<T, T>
Marshall Clow9d67c6d2014-02-19 16:51:35604 minmax(initializer_list<T> t); // constexpr in C++14
Howard Hinnant4eb27b72010-08-21 20:10:01605
606template<class T, class Compare>
607 pair<T, T>
Marshall Clow9d67c6d2014-02-19 16:51:35608 minmax(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnant3e519522010-05-11 19:42:16609
610template <class InputIterator1, class InputIterator2>
Marshall Clow1b9a4ff2018-01-22 20:44:33611 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16612 lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
613
614template <class InputIterator1, class InputIterator2, class Compare>
Marshall Clow1b9a4ff2018-01-22 20:44:33615 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16616 lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
617 InputIterator2 first2, InputIterator2 last2, Compare comp);
618
619template <class BidirectionalIterator>
Howard Hinnantb3371f62010-08-22 00:02:43620 bool
Howard Hinnant3e519522010-05-11 19:42:16621 next_permutation(BidirectionalIterator first, BidirectionalIterator last);
622
623template <class BidirectionalIterator, class Compare>
624 bool
625 next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
626
627template <class BidirectionalIterator>
628 bool
629 prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
630
631template <class BidirectionalIterator, class Compare>
632 bool
633 prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
634
635} // std
636
637*/
638
639#include <__config>
640#include <initializer_list>
641#include <type_traits>
642#include <cstring>
Eric Fiselierf07dd8d2016-04-21 23:38:59643#include <utility> // needed to provide swap_ranges.
Howard Hinnant3e519522010-05-11 19:42:16644#include <memory>
Marshall Clowd835e592018-01-08 19:18:00645#include <functional>
Howard Hinnant3e519522010-05-11 19:42:16646#include <iterator>
Howard Hinnanta1d07d52012-07-26 17:09:09647#include <cstddef>
Marshall Clowe02ee4f2018-08-17 16:07:48648#include <bit>
Marshall Clowf56972e2018-09-12 19:41:40649#include <version>
Howard Hinnant5d1a7012013-08-14 18:00:20650
Eric Fiselierc1bd9192014-08-10 23:53:08651#include <__debug>
652
Howard Hinnant073458b2011-10-17 20:05:10653#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16654#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10655#endif
Howard Hinnant3e519522010-05-11 19:42:16656
Eric Fiseliera016efb2017-05-31 22:07:49657_LIBCPP_PUSH_MACROS
658#include <__undef_macros>
659
660
Howard Hinnant3e519522010-05-11 19:42:16661_LIBCPP_BEGIN_NAMESPACE_STD
662
Marshall Clow9d67c6d2014-02-19 16:51:35663// I'd like to replace these with _VSTD::equal_to<void>, but can't because:
664// * That only works with C++14 and later, and
665// * We haven't included <functional> here.
Howard Hinnant3e519522010-05-11 19:42:16666template <class _T1, class _T2 = _T1>
667struct __equal_to
668{
Marshall Clowd8098f92018-07-14 04:15:19669 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
670 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
671 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
672 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
Howard Hinnant3e519522010-05-11 19:42:16673};
674
675template <class _T1>
676struct __equal_to<_T1, _T1>
677{
Marshall Clow9d67c6d2014-02-19 16:51:35678 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
679 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnant3e519522010-05-11 19:42:16680};
681
682template <class _T1>
683struct __equal_to<const _T1, _T1>
684{
Marshall Clow9d67c6d2014-02-19 16:51:35685 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
686 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnant3e519522010-05-11 19:42:16687};
688
689template <class _T1>
690struct __equal_to<_T1, const _T1>
691{
Marshall Clow9d67c6d2014-02-19 16:51:35692 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
693 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnant3e519522010-05-11 19:42:16694};
695
696template <class _T1, class _T2 = _T1>
697struct __less
698{
Aditya Kumar331fb802016-08-25 11:52:38699 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow9d67c6d2014-02-19 16:51:35700 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
701
702 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
703 bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
704
705 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
706 bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
707
708 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
709 bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
Howard Hinnant3e519522010-05-11 19:42:16710};
711
712template <class _T1>
713struct __less<_T1, _T1>
714{
Marshall Clow9d67c6d2014-02-19 16:51:35715 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
716 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnant3e519522010-05-11 19:42:16717};
718
719template <class _T1>
720struct __less<const _T1, _T1>
721{
Marshall Clow9d67c6d2014-02-19 16:51:35722 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
723 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnant3e519522010-05-11 19:42:16724};
725
726template <class _T1>
727struct __less<_T1, const _T1>
728{
Marshall Clow9d67c6d2014-02-19 16:51:35729 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
730 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnant3e519522010-05-11 19:42:16731};
732
733template <class _Predicate>
Marshall Clowa763b362017-08-28 23:16:13734class __invert // invert the sense of a comparison
Howard Hinnant3e519522010-05-11 19:42:16735{
736private:
737 _Predicate __p_;
738public:
Marshall Clowa763b362017-08-28 23:16:13739 _LIBCPP_INLINE_VISIBILITY __invert() {}
Howard Hinnant3e519522010-05-11 19:42:16740
741 _LIBCPP_INLINE_VISIBILITY
Marshall Clowa763b362017-08-28 23:16:13742 explicit __invert(_Predicate __p) : __p_(__p) {}
Howard Hinnant3e519522010-05-11 19:42:16743
744 template <class _T1>
745 _LIBCPP_INLINE_VISIBILITY
746 bool operator()(const _T1& __x) {return !__p_(__x);}
747
748 template <class _T1, class _T2>
749 _LIBCPP_INLINE_VISIBILITY
Marshall Clowa763b362017-08-28 23:16:13750 bool operator()(const _T1& __x, const _T2& __y) {return __p_(__y, __x);}
Howard Hinnant3e519522010-05-11 19:42:16751};
752
Louis Dionne04695a72018-12-17 16:04:39753// Perform division by two quickly for positive integers (llvm.org/PR39129)
754
755template <typename _Integral>
756_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
757typename enable_if
758<
759 is_integral<_Integral>::value,
760 _Integral
761>::type
762__half_positive(_Integral __value)
763{
764 return static_cast<_Integral>(static_cast<typename make_unsigned<_Integral>::type>(__value) / 2);
765}
766
767template <typename _Tp>
768_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
769typename enable_if
770<
771 !is_integral<_Tp>::value,
772 _Tp
773>::type
774__half_positive(_Tp __value)
775{
776 return __value / 2;
777}
778
Howard Hinnant145afa12013-08-23 20:10:18779#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:16780
781template <class _Compare>
782struct __debug_less
783{
784 _Compare __comp_;
785 __debug_less(_Compare& __c) : __comp_(__c) {}
Eric Fiselier331d2152016-07-19 23:27:18786
Howard Hinnant3e519522010-05-11 19:42:16787 template <class _Tp, class _Up>
788 bool operator()(const _Tp& __x, const _Up& __y)
789 {
790 bool __r = __comp_(__x, __y);
791 if (__r)
Eric Fiselier331d2152016-07-19 23:27:18792 __do_compare_assert(0, __y, __x);
Howard Hinnant3e519522010-05-11 19:42:16793 return __r;
794 }
Eric Fiselier331d2152016-07-19 23:27:18795
796 template <class _LHS, class _RHS>
797 inline _LIBCPP_INLINE_VISIBILITY
798 decltype((void)_VSTD::declval<_Compare&>()(
799 _VSTD::declval<_LHS const&>(), _VSTD::declval<_RHS const&>()))
800 __do_compare_assert(int, _LHS const& __l, _RHS const& __r) {
801 _LIBCPP_ASSERT(!__comp_(__l, __r),
802 "Comparator does not induce a strict weak ordering");
803 }
804
805 template <class _LHS, class _RHS>
806 inline _LIBCPP_INLINE_VISIBILITY
807 void __do_compare_assert(long, _LHS const&, _RHS const&) {}
Howard Hinnant3e519522010-05-11 19:42:16808};
809
Howard Hinnant145afa12013-08-23 20:10:18810#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:16811
812// all_of
813
814template <class _InputIterator, class _Predicate>
Marshall Clow706ffef2018-01-15 17:20:36815inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16816bool
817all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
818{
819 for (; __first != __last; ++__first)
820 if (!__pred(*__first))
821 return false;
822 return true;
823}
824
825// any_of
826
827template <class _InputIterator, class _Predicate>
Marshall Clow706ffef2018-01-15 17:20:36828inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16829bool
830any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
831{
832 for (; __first != __last; ++__first)
833 if (__pred(*__first))
834 return true;
835 return false;
836}
837
838// none_of
839
840template <class _InputIterator, class _Predicate>
Marshall Clow706ffef2018-01-15 17:20:36841inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16842bool
843none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
844{
845 for (; __first != __last; ++__first)
846 if (__pred(*__first))
847 return false;
848 return true;
849}
850
851// for_each
852
853template <class _InputIterator, class _Function>
Marshall Clow1b9a4ff2018-01-22 20:44:33854inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16855_Function
856for_each(_InputIterator __first, _InputIterator __last, _Function __f)
857{
858 for (; __first != __last; ++__first)
859 __f(*__first);
Marshall Clow1c7fe122016-11-14 18:22:19860 return __f;
Howard Hinnant3e519522010-05-11 19:42:16861}
862
Marshall Clow1d029962017-05-25 13:40:57863#if _LIBCPP_STD_VER > 14
Marshall Clowd5c65ff2017-05-25 02:29:54864// for_each_n
865
866template <class _InputIterator, class _Size, class _Function>
Marshall Clow1b9a4ff2018-01-22 20:44:33867inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowd5c65ff2017-05-25 02:29:54868_InputIterator
869for_each_n(_InputIterator __first, _Size __orig_n, _Function __f)
870{
871 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
872 _IntegralSize __n = __orig_n;
873 while (__n > 0)
874 {
875 __f(*__first);
876 ++__first;
877 --__n;
878 }
879 return __first;
880}
Marshall Clow1d029962017-05-25 13:40:57881#endif
Marshall Clowd5c65ff2017-05-25 02:29:54882
Howard Hinnant3e519522010-05-11 19:42:16883// find
884
885template <class _InputIterator, class _Tp>
Marshall Clow49c76432018-01-15 16:16:32886inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16887_InputIterator
Howard Hinnante4383372011-10-22 20:59:45888find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:16889{
890 for (; __first != __last; ++__first)
Howard Hinnante4383372011-10-22 20:59:45891 if (*__first == __value_)
Howard Hinnant3e519522010-05-11 19:42:16892 break;
893 return __first;
894}
895
896// find_if
897
898template <class _InputIterator, class _Predicate>
Marshall Clow49c76432018-01-15 16:16:32899inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16900_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>
Marshall Clow86944282018-01-15 19:26:05912inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16913_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>
Marshall Clow86944282018-01-15 19:26:05925_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1
Howard Hinnant3e519522010-05-11 19:42:16926__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>
Marshall Clow86944282018-01-15 19:26:05967_LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator1
Howard Hinnant3e519522010-05-11 19:42:16968__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 Clow9b0af342014-06-10 18:51:551006_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
Howard Hinnant3e519522010-05-11 19:42:161007__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>
Marshall Clow86944282018-01-15 19:26:051047inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161048_ForwardIterator1
1049find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1050 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1051{
Howard Hinnantce48a112011-06-30 21:18:191052 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnant3e519522010-05-11 19:42:161053 (__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>
Marshall Clow86944282018-01-15 19:26:051059inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161060_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 Hinnantce48a112011-06-30 21:18:191066 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnant3e519522010-05-11 19:42:161067}
1068
1069// find_first_of
1070
1071template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow9b0af342014-06-10 18:51:551072_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1073__find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Howard Hinnant3e519522010-05-11 19:42:161074 _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 Clow9b0af342014-06-10 18:51:551083
1084template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow86944282018-01-15 19:26:051085inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow9b0af342014-06-10 18:51:551086_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 Hinnant3e519522010-05-11 19:42:161093template <class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow86944282018-01-15 19:26:051094inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161095_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 Clow9b0af342014-06-10 18:51:551101 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnant3e519522010-05-11 19:42:161102}
1103
1104// adjacent_find
1105
1106template <class _ForwardIterator, class _BinaryPredicate>
Marshall Clow86944282018-01-15 19:26:051107inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161108_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>
Marshall Clow86944282018-01-15 19:26:051125inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161126_ForwardIterator
1127adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1128{
1129 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnantce48a112011-06-30 21:18:191130 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
Howard Hinnant3e519522010-05-11 19:42:161131}
1132
1133// count
1134
1135template <class _InputIterator, class _Tp>
Marshall Clow056f15e2018-01-15 19:40:341136inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161137typename iterator_traits<_InputIterator>::difference_type
Howard Hinnante4383372011-10-22 20:59:451138count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:161139{
1140 typename iterator_traits<_InputIterator>::difference_type __r(0);
1141 for (; __first != __last; ++__first)
Howard Hinnante4383372011-10-22 20:59:451142 if (*__first == __value_)
Howard Hinnant3e519522010-05-11 19:42:161143 ++__r;
1144 return __r;
1145}
1146
1147// count_if
1148
1149template <class _InputIterator, class _Predicate>
Marshall Clow056f15e2018-01-15 19:40:341150inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161151typename 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>
Marshall Clow6538e28d2018-01-16 02:04:101164inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161165pair<_InputIterator1, _InputIterator2>
1166mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1167 _InputIterator2 __first2, _BinaryPredicate __pred)
1168{
Marshall Clowbd7c7b52014-09-16 20:40:051169 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnant3e519522010-05-11 19:42:161170 if (!__pred(*__first1, *__first2))
1171 break;
1172 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1173}
1174
1175template <class _InputIterator1, class _InputIterator2>
Marshall Clow6538e28d2018-01-16 02:04:101176inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161177pair<_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 Hinnantce48a112011-06-30 21:18:191182 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnant3e519522010-05-11 19:42:161183}
1184
Marshall Clow0b0bbd22013-05-09 21:14:231185#if _LIBCPP_STD_VER > 11
1186template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Marshall Clow6538e28d2018-01-16 02:04:101187inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231188pair<_InputIterator1, _InputIterator2>
1189mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1190 _InputIterator2 __first2, _InputIterator2 __last2,
1191 _BinaryPredicate __pred)
1192{
Marshall Clowbd7c7b52014-09-16 20:40:051193 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow0b0bbd22013-05-09 21:14:231194 if (!__pred(*__first1, *__first2))
1195 break;
1196 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1197}
1198
1199template <class _InputIterator1, class _InputIterator2>
Marshall Clow6538e28d2018-01-16 02:04:101200inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231201pair<_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 Hinnant3e519522010-05-11 19:42:161211// equal
1212
1213template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Marshall Clow6538e28d2018-01-16 02:04:101214inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161215bool
1216equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1217{
Eric Fiselier910285b2014-10-27 19:28:201218 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnant3e519522010-05-11 19:42:161219 if (!__pred(*__first1, *__first2))
1220 return false;
1221 return true;
1222}
1223
1224template <class _InputIterator1, class _InputIterator2>
Marshall Clow6538e28d2018-01-16 02:04:101225inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161226bool
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 Hinnantce48a112011-06-30 21:18:191231 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnant3e519522010-05-11 19:42:161232}
1233
Marshall Clow0b0bbd22013-05-09 21:14:231234#if _LIBCPP_STD_VER > 11
1235template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
Marshall Clow6538e28d2018-01-16 02:04:101236inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231237bool
Aditya Kumar331fb802016-08-25 11:52:381238__equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow0b0bbd22013-05-09 21:14:231239 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1240 input_iterator_tag, input_iterator_tag )
1241{
Eric Fiselier910285b2014-10-27 19:28:201242 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow0b0bbd22013-05-09 21:14:231243 if (!__pred(*__first1, *__first2))
1244 return false;
1245 return __first1 == __last1 && __first2 == __last2;
1246}
1247
1248template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow6538e28d2018-01-16 02:04:101249inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231250bool
Aditya Kumar331fb802016-08-25 11:52:381251__equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1252 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
Marshall Clow0b0bbd22013-05-09 21:14:231253 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>
Marshall Clow6538e28d2018-01-16 02:04:101263inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231264bool
Aditya Kumar331fb802016-08-25 11:52:381265equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow0b0bbd22013-05-09 21:14:231266 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1267{
1268 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
Aditya Kumar331fb802016-08-25 11:52:381269 (__first1, __last1, __first2, __last2, __pred,
Marshall Clow0b0bbd22013-05-09 21:14:231270 typename iterator_traits<_InputIterator1>::iterator_category(),
1271 typename iterator_traits<_InputIterator2>::iterator_category());
1272}
1273
1274template <class _InputIterator1, class _InputIterator2>
Marshall Clow6538e28d2018-01-16 02:04:101275inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231276bool
Aditya Kumar331fb802016-08-25 11:52:381277equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow0b0bbd22013-05-09 21:14:231278 _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 Hinnant3e519522010-05-11 19:42:161288// is_permutation
1289
1290template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow49c76432018-01-15 16:16:321291_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnant3e519522010-05-11 19:42:161292is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1293 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1294{
Marshall Clow49c76432018-01-15 16:16:321295// shorten sequences as much as possible by lopping of any equal prefix
Eric Fiselier910285b2014-10-27 19:28:201296 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnant3e519522010-05-11 19:42:161297 if (!__pred(*__first1, *__first2))
Marshall Clow49c76432018-01-15 16:16:321298 break;
1299 if (__first1 == __last1)
1300 return true;
1301
1302// __first1 != __last1 && *__first1 != *__first2
Howard Hinnant3e519522010-05-11 19:42:161303 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
Howard Hinnantce48a112011-06-30 21:18:191304 _D1 __l1 = _VSTD::distance(__first1, __last1);
Howard Hinnant3e519522010-05-11 19:42:161305 if (__l1 == _D1(1))
1306 return false;
Howard Hinnantce48a112011-06-30 21:18:191307 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
Howard Hinnant3e519522010-05-11 19:42:161308 // For each element in [f1, l1) see if there are the same number of
1309 // equal elements in [f2, l2)
1310 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1311 {
Marshall Clow49c76432018-01-15 16:16:321312 // Have we already counted the number of *__i in [f1, l1)?
Peter Collingbourne939b1622018-01-26 21:23:271313 _ForwardIterator1 __match = __first1;
1314 for (; __match != __i; ++__match)
1315 if (__pred(*__match, *__i))
1316 break;
1317 if (__match == __i) {
Howard Hinnant3e519522010-05-11 19:42:161318 // Count number of *__i in [f2, l2)
1319 _D1 __c2 = 0;
1320 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1321 if (__pred(*__i, *__j))
1322 ++__c2;
1323 if (__c2 == 0)
1324 return false;
1325 // Count number of *__i in [__i, l1) (we can start with 1)
1326 _D1 __c1 = 1;
Howard Hinnantce48a112011-06-30 21:18:191327 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
Howard Hinnant3e519522010-05-11 19:42:161328 if (__pred(*__i, *__j))
1329 ++__c1;
1330 if (__c1 != __c2)
1331 return false;
1332 }
Howard Hinnant3e519522010-05-11 19:42:161333 }
1334 return true;
1335}
1336
1337template<class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow49c76432018-01-15 16:16:321338inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161339bool
1340is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1341 _ForwardIterator2 __first2)
1342{
1343 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1344 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantce48a112011-06-30 21:18:191345 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnant3e519522010-05-11 19:42:161346}
1347
Marshall Clow0b0bbd22013-05-09 21:14:231348#if _LIBCPP_STD_VER > 11
1349template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow49c76432018-01-15 16:16:321350_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Marshall Clow0b0bbd22013-05-09 21:14:231351__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Aditya Kumar331fb802016-08-25 11:52:381352 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
Marshall Clow0b0bbd22013-05-09 21:14:231353 _BinaryPredicate __pred,
1354 forward_iterator_tag, forward_iterator_tag )
1355{
Marshall Clow49c76432018-01-15 16:16:321356// shorten sequences as much as possible by lopping of any equal prefix
Eric Fiselier847ee132014-10-27 20:26:251357 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow0b0bbd22013-05-09 21:14:231358 if (!__pred(*__first1, *__first2))
Marshall Clow49c76432018-01-15 16:16:321359 break;
1360 if (__first1 == __last1)
Marshall Clow12c74232018-01-19 18:07:291361 return __first2 == __last2;
Marshall Clow49c76432018-01-15 16:16:321362 else if (__first2 == __last2)
Marshall Clow12c74232018-01-19 18:07:291363 return false;
Marshall Clow49c76432018-01-15 16:16:321364
Marshall Clow0b0bbd22013-05-09 21:14:231365 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1366 _D1 __l1 = _VSTD::distance(__first1, __last1);
1367
1368 typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
Marshall Clowfce85ba2013-05-10 00:16:101369 _D2 __l2 = _VSTD::distance(__first2, __last2);
Marshall Clow0b0bbd22013-05-09 21:14:231370 if (__l1 != __l2)
1371 return false;
1372
1373 // For each element in [f1, l1) see if there are the same number of
1374 // equal elements in [f2, l2)
1375 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1376 {
Marshall Clow49c76432018-01-15 16:16:321377 // Have we already counted the number of *__i in [f1, l1)?
Peter Collingbourne939b1622018-01-26 21:23:271378 _ForwardIterator1 __match = __first1;
1379 for (; __match != __i; ++__match)
1380 if (__pred(*__match, *__i))
1381 break;
1382 if (__match == __i) {
Marshall Clow0b0bbd22013-05-09 21:14:231383 // Count number of *__i in [f2, l2)
1384 _D1 __c2 = 0;
1385 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1386 if (__pred(*__i, *__j))
1387 ++__c2;
1388 if (__c2 == 0)
1389 return false;
1390 // Count number of *__i in [__i, l1) (we can start with 1)
1391 _D1 __c1 = 1;
1392 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1393 if (__pred(*__i, *__j))
1394 ++__c1;
1395 if (__c1 != __c2)
1396 return false;
1397 }
Marshall Clow0b0bbd22013-05-09 21:14:231398 }
1399 return true;
1400}
1401
1402template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow49c76432018-01-15 16:16:321403_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Marshall Clow0b0bbd22013-05-09 21:14:231404__is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
Aditya Kumar331fb802016-08-25 11:52:381405 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
Marshall Clow0b0bbd22013-05-09 21:14:231406 _BinaryPredicate __pred,
1407 random_access_iterator_tag, random_access_iterator_tag )
1408{
1409 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1410 return false;
1411 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1412 typename add_lvalue_reference<_BinaryPredicate>::type>
1413 (__first1, __last1, __first2, __pred );
1414}
1415
1416template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow49c76432018-01-15 16:16:321417inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231418bool
1419is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1420 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1421 _BinaryPredicate __pred )
1422{
1423 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1424 (__first1, __last1, __first2, __last2, __pred,
1425 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1426 typename iterator_traits<_ForwardIterator2>::iterator_category());
1427}
1428
1429template<class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow49c76432018-01-15 16:16:321430inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231431bool
1432is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1433 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1434{
1435 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1436 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1437 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1438 __equal_to<__v1, __v2>(),
1439 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1440 typename iterator_traits<_ForwardIterator2>::iterator_category());
1441}
1442#endif
1443
Howard Hinnant3e519522010-05-11 19:42:161444// search
Marshall Clowd835e592018-01-08 19:18:001445// __search is in <functional>
Howard Hinnant3e519522010-05-11 19:42:161446
1447template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow12f0a772018-01-16 15:48:271448inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161449_ForwardIterator1
1450search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1451 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1452{
Howard Hinnantce48a112011-06-30 21:18:191453 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnant3e519522010-05-11 19:42:161454 (__first1, __last1, __first2, __last2, __pred,
Marshall Clow28cc4dd2016-03-08 15:12:521455 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1456 typename iterator_traits<_ForwardIterator2>::iterator_category())
1457 .first;
Howard Hinnant3e519522010-05-11 19:42:161458}
1459
1460template <class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow12f0a772018-01-16 15:48:271461inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161462_ForwardIterator1
1463search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1464 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1465{
Marshall Clow28cc4dd2016-03-08 15:12:521466 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1467 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantce48a112011-06-30 21:18:191468 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnant3e519522010-05-11 19:42:161469}
1470
Marshall Clowd835e592018-01-08 19:18:001471
1472#if _LIBCPP_STD_VER > 14
1473template <class _ForwardIterator, class _Searcher>
Marshall Clow12f0a772018-01-16 15:48:271474_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowd835e592018-01-08 19:18:001475_ForwardIterator search(_ForwardIterator __f, _ForwardIterator __l, const _Searcher &__s)
1476{ return __s(__f, __l).first; }
1477#endif
1478
Howard Hinnant3e519522010-05-11 19:42:161479// search_n
1480
1481template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
Marshall Clow12f0a772018-01-16 15:48:271482_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:161483__search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnante4383372011-10-22 20:59:451484 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
Howard Hinnant3e519522010-05-11 19:42:161485{
1486 if (__count <= 0)
1487 return __first;
1488 while (true)
1489 {
Howard Hinnante4383372011-10-22 20:59:451490 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnant3e519522010-05-11 19:42:161491 while (true)
1492 {
Howard Hinnante4383372011-10-22 20:59:451493 if (__first == __last) // return __last if no element matches __value_
Howard Hinnant3e519522010-05-11 19:42:161494 return __last;
Howard Hinnante4383372011-10-22 20:59:451495 if (__pred(*__first, __value_))
Howard Hinnant3e519522010-05-11 19:42:161496 break;
1497 ++__first;
1498 }
Howard Hinnante4383372011-10-22 20:59:451499 // *__first matches __value_, now match elements after here
Howard Hinnant3e519522010-05-11 19:42:161500 _ForwardIterator __m = __first;
1501 _Size __c(0);
1502 while (true)
1503 {
1504 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1505 return __first;
1506 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1507 return __last;
Howard Hinnante4383372011-10-22 20:59:451508 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnant3e519522010-05-11 19:42:161509 {
1510 __first = __m;
1511 ++__first;
1512 break;
1513 } // else there is a match, check next elements
1514 }
1515 }
1516}
1517
1518template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
Marshall Clow12f0a772018-01-16 15:48:271519_LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
Howard Hinnant3e519522010-05-11 19:42:161520__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnante4383372011-10-22 20:59:451521 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
Howard Hinnant3e519522010-05-11 19:42:161522{
1523 if (__count <= 0)
1524 return __first;
1525 _Size __len = static_cast<_Size>(__last - __first);
1526 if (__len < __count)
1527 return __last;
1528 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1529 while (true)
1530 {
Howard Hinnante4383372011-10-22 20:59:451531 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnant3e519522010-05-11 19:42:161532 while (true)
1533 {
Howard Hinnantb13fcad2013-04-04 15:40:481534 if (__first >= __s) // return __last if no element matches __value_
Howard Hinnant3e519522010-05-11 19:42:161535 return __last;
Howard Hinnante4383372011-10-22 20:59:451536 if (__pred(*__first, __value_))
Howard Hinnant3e519522010-05-11 19:42:161537 break;
1538 ++__first;
1539 }
Howard Hinnante4383372011-10-22 20:59:451540 // *__first matches __value_, now match elements after here
Howard Hinnant3e519522010-05-11 19:42:161541 _RandomAccessIterator __m = __first;
1542 _Size __c(0);
1543 while (true)
1544 {
1545 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1546 return __first;
1547 ++__m; // no need to check range on __m because __s guarantees we have enough source
Howard Hinnante4383372011-10-22 20:59:451548 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnant3e519522010-05-11 19:42:161549 {
1550 __first = __m;
1551 ++__first;
1552 break;
1553 } // else there is a match, check next elements
1554 }
1555 }
1556}
1557
1558template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
Marshall Clow12f0a772018-01-16 15:48:271559inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161560_ForwardIterator
1561search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnante4383372011-10-22 20:59:451562 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
Howard Hinnant3e519522010-05-11 19:42:161563{
Howard Hinnantce48a112011-06-30 21:18:191564 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
Eric Fiselier51544022015-02-10 16:46:421565 (__first, __last, __convert_to_integral(__count), __value_, __pred,
1566 typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnant3e519522010-05-11 19:42:161567}
1568
1569template <class _ForwardIterator, class _Size, class _Tp>
Marshall Clow12f0a772018-01-16 15:48:271570inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161571_ForwardIterator
Howard Hinnante4383372011-10-22 20:59:451572search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:161573{
1574 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Eric Fiselier51544022015-02-10 16:46:421575 return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
1576 __value_, __equal_to<__v, _Tp>());
Howard Hinnant3e519522010-05-11 19:42:161577}
1578
1579// copy
Howard Hinnant3e519522010-05-11 19:42:161580template <class _Iter>
1581inline _LIBCPP_INLINE_VISIBILITY
1582_Iter
1583__unwrap_iter(_Iter __i)
1584{
1585 return __i;
1586}
1587
1588template <class _Tp>
Marshall Clow5b7c98e2017-05-25 14:20:261589inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161590typename enable_if
1591<
Howard Hinnantca740482010-11-19 22:17:281592 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnant3e519522010-05-11 19:42:161593 _Tp*
1594>::type
1595__unwrap_iter(move_iterator<_Tp*> __i)
1596{
1597 return __i.base();
1598}
1599
Howard Hinnantfc88dbd2013-08-23 17:37:051600#if _LIBCPP_DEBUG_LEVEL < 2
1601
Howard Hinnant3e519522010-05-11 19:42:161602template <class _Tp>
Marshall Clow9cad5022018-07-13 16:35:261603inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant3e519522010-05-11 19:42:161604typename enable_if
1605<
Howard Hinnantca740482010-11-19 22:17:281606 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnant3e519522010-05-11 19:42:161607 _Tp*
1608>::type
1609__unwrap_iter(__wrap_iter<_Tp*> __i)
1610{
1611 return __i.base();
1612}
1613
Eric Fiselier14bd0bf2016-12-28 05:35:321614#else
1615
1616template <class _Tp>
Marshall Clow9cad5022018-07-13 16:35:261617inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Eric Fiselier14bd0bf2016-12-28 05:35:321618typename enable_if
1619<
1620 is_trivially_copy_assignable<_Tp>::value,
1621 __wrap_iter<_Tp*>
1622>::type
1623__unwrap_iter(__wrap_iter<_Tp*> __i)
1624{
1625 return __i;
1626}
1627
Howard Hinnantfc88dbd2013-08-23 17:37:051628#endif // _LIBCPP_DEBUG_LEVEL < 2
1629
Howard Hinnant3e519522010-05-11 19:42:161630template <class _InputIterator, class _OutputIterator>
1631inline _LIBCPP_INLINE_VISIBILITY
1632_OutputIterator
1633__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1634{
Eric Fiselier910285b2014-10-27 19:28:201635 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant3e519522010-05-11 19:42:161636 *__result = *__first;
1637 return __result;
1638}
1639
1640template <class _Tp, class _Up>
1641inline _LIBCPP_INLINE_VISIBILITY
1642typename enable_if
1643<
1644 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnantca740482010-11-19 22:17:281645 is_trivially_copy_assignable<_Up>::value,
Howard Hinnant3e519522010-05-11 19:42:161646 _Up*
1647>::type
1648__copy(_Tp* __first, _Tp* __last, _Up* __result)
1649{
1650 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow5b312052015-06-02 13:52:161651 if (__n > 0)
1652 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnant3e519522010-05-11 19:42:161653 return __result + __n;
1654}
1655
1656template <class _InputIterator, class _OutputIterator>
1657inline _LIBCPP_INLINE_VISIBILITY
1658_OutputIterator
1659copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1660{
Howard Hinnantce48a112011-06-30 21:18:191661 return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnant3e519522010-05-11 19:42:161662}
1663
1664// copy_backward
1665
Howard Hinnantd3d43562013-02-06 21:03:391666template <class _BidirectionalIterator, class _OutputIterator>
Howard Hinnant3e519522010-05-11 19:42:161667inline _LIBCPP_INLINE_VISIBILITY
1668_OutputIterator
Howard Hinnantd3d43562013-02-06 21:03:391669__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
Howard Hinnant3e519522010-05-11 19:42:161670{
1671 while (__first != __last)
1672 *--__result = *--__last;
1673 return __result;
1674}
1675
1676template <class _Tp, class _Up>
1677inline _LIBCPP_INLINE_VISIBILITY
1678typename enable_if
1679<
1680 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnantca740482010-11-19 22:17:281681 is_trivially_copy_assignable<_Up>::value,
Howard Hinnant3e519522010-05-11 19:42:161682 _Up*
1683>::type
1684__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1685{
1686 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow5b312052015-06-02 13:52:161687 if (__n > 0)
1688 {
1689 __result -= __n;
1690 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1691 }
Howard Hinnant3e519522010-05-11 19:42:161692 return __result;
1693}
1694
1695template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1696inline _LIBCPP_INLINE_VISIBILITY
1697_BidirectionalIterator2
1698copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1699 _BidirectionalIterator2 __result)
1700{
Eric Fiselierfd838222016-12-23 23:37:521701 return _VSTD::__copy_backward(__unwrap_iter(__first),
1702 __unwrap_iter(__last),
1703 __unwrap_iter(__result));
Howard Hinnant3e519522010-05-11 19:42:161704}
1705
1706// copy_if
1707
1708template<class _InputIterator, class _OutputIterator, class _Predicate>
1709inline _LIBCPP_INLINE_VISIBILITY
1710_OutputIterator
1711copy_if(_InputIterator __first, _InputIterator __last,
1712 _OutputIterator __result, _Predicate __pred)
1713{
1714 for (; __first != __last; ++__first)
1715 {
1716 if (__pred(*__first))
1717 {
1718 *__result = *__first;
1719 ++__result;
1720 }
1721 }
1722 return __result;
1723}
1724
1725// copy_n
1726
1727template<class _InputIterator, class _Size, class _OutputIterator>
1728inline _LIBCPP_INLINE_VISIBILITY
1729typename enable_if
1730<
1731 __is_input_iterator<_InputIterator>::value &&
1732 !__is_random_access_iterator<_InputIterator>::value,
1733 _OutputIterator
1734>::type
Eric Fiselier51544022015-02-10 16:46:421735copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnant3e519522010-05-11 19:42:161736{
Eric Fiselier51544022015-02-10 16:46:421737 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1738 _IntegralSize __n = __orig_n;
Howard Hinnant99847d22011-02-27 20:55:391739 if (__n > 0)
1740 {
Howard Hinnant3e519522010-05-11 19:42:161741 *__result = *__first;
Howard Hinnant99847d22011-02-27 20:55:391742 ++__result;
1743 for (--__n; __n > 0; --__n)
1744 {
1745 ++__first;
1746 *__result = *__first;
1747 ++__result;
1748 }
1749 }
Howard Hinnant3e519522010-05-11 19:42:161750 return __result;
1751}
1752
1753template<class _InputIterator, class _Size, class _OutputIterator>
1754inline _LIBCPP_INLINE_VISIBILITY
1755typename enable_if
1756<
1757 __is_random_access_iterator<_InputIterator>::value,
1758 _OutputIterator
1759>::type
Eric Fiselier51544022015-02-10 16:46:421760copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnant3e519522010-05-11 19:42:161761{
Eric Fiselier51544022015-02-10 16:46:421762 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1763 _IntegralSize __n = __orig_n;
Howard Hinnantce48a112011-06-30 21:18:191764 return _VSTD::copy(__first, __first + __n, __result);
Howard Hinnant3e519522010-05-11 19:42:161765}
1766
1767// move
1768
1769template <class _InputIterator, class _OutputIterator>
1770inline _LIBCPP_INLINE_VISIBILITY
1771_OutputIterator
1772__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1773{
Eric Fiselier910285b2014-10-27 19:28:201774 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantce48a112011-06-30 21:18:191775 *__result = _VSTD::move(*__first);
Howard Hinnant3e519522010-05-11 19:42:161776 return __result;
1777}
1778
1779template <class _Tp, class _Up>
1780inline _LIBCPP_INLINE_VISIBILITY
1781typename enable_if
1782<
1783 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnantca740482010-11-19 22:17:281784 is_trivially_copy_assignable<_Up>::value,
Howard Hinnant3e519522010-05-11 19:42:161785 _Up*
1786>::type
1787__move(_Tp* __first, _Tp* __last, _Up* __result)
1788{
1789 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow5b312052015-06-02 13:52:161790 if (__n > 0)
1791 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnant3e519522010-05-11 19:42:161792 return __result + __n;
1793}
1794
1795template <class _InputIterator, class _OutputIterator>
1796inline _LIBCPP_INLINE_VISIBILITY
1797_OutputIterator
1798move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1799{
Howard Hinnantce48a112011-06-30 21:18:191800 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnant3e519522010-05-11 19:42:161801}
1802
1803// move_backward
1804
1805template <class _InputIterator, class _OutputIterator>
1806inline _LIBCPP_INLINE_VISIBILITY
1807_OutputIterator
1808__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1809{
1810 while (__first != __last)
Howard Hinnantce48a112011-06-30 21:18:191811 *--__result = _VSTD::move(*--__last);
Howard Hinnant3e519522010-05-11 19:42:161812 return __result;
1813}
1814
1815template <class _Tp, class _Up>
1816inline _LIBCPP_INLINE_VISIBILITY
1817typename enable_if
1818<
1819 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnantca740482010-11-19 22:17:281820 is_trivially_copy_assignable<_Up>::value,
Howard Hinnant3e519522010-05-11 19:42:161821 _Up*
1822>::type
1823__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1824{
1825 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow5b312052015-06-02 13:52:161826 if (__n > 0)
1827 {
1828 __result -= __n;
1829 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1830 }
Howard Hinnant3e519522010-05-11 19:42:161831 return __result;
1832}
1833
1834template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1835inline _LIBCPP_INLINE_VISIBILITY
1836_BidirectionalIterator2
1837move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1838 _BidirectionalIterator2 __result)
1839{
Howard Hinnantce48a112011-06-30 21:18:191840 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnant3e519522010-05-11 19:42:161841}
1842
1843// iter_swap
1844
Howard Hinnanta676f7d2011-05-27 15:04:191845// moved to <type_traits> for better swap / noexcept support
Howard Hinnant3e519522010-05-11 19:42:161846
1847// transform
1848
1849template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
Marshall Clow99894b62018-01-19 17:45:391850inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161851_OutputIterator
1852transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1853{
Eric Fiselier910285b2014-10-27 19:28:201854 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant3e519522010-05-11 19:42:161855 *__result = __op(*__first);
1856 return __result;
1857}
1858
1859template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
Marshall Clow99894b62018-01-19 17:45:391860inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161861_OutputIterator
1862transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1863 _OutputIterator __result, _BinaryOperation __binary_op)
1864{
Eric Fiselier910285b2014-10-27 19:28:201865 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
Howard Hinnant3e519522010-05-11 19:42:161866 *__result = __binary_op(*__first1, *__first2);
1867 return __result;
1868}
1869
1870// replace
1871
1872template <class _ForwardIterator, class _Tp>
Marshall Clow12c74232018-01-19 18:07:291873inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161874void
1875replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1876{
1877 for (; __first != __last; ++__first)
1878 if (*__first == __old_value)
1879 *__first = __new_value;
1880}
1881
1882// replace_if
1883
1884template <class _ForwardIterator, class _Predicate, class _Tp>
Marshall Clow12c74232018-01-19 18:07:291885inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161886void
1887replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1888{
1889 for (; __first != __last; ++__first)
1890 if (__pred(*__first))
1891 *__first = __new_value;
1892}
1893
1894// replace_copy
1895
1896template <class _InputIterator, class _OutputIterator, class _Tp>
Marshall Clow12c74232018-01-19 18:07:291897inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161898_OutputIterator
1899replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1900 const _Tp& __old_value, const _Tp& __new_value)
1901{
Eric Fiselier910285b2014-10-27 19:28:201902 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant3e519522010-05-11 19:42:161903 if (*__first == __old_value)
1904 *__result = __new_value;
1905 else
1906 *__result = *__first;
1907 return __result;
1908}
1909
1910// replace_copy_if
1911
1912template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
Marshall Clow12c74232018-01-19 18:07:291913inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161914_OutputIterator
1915replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1916 _Predicate __pred, const _Tp& __new_value)
1917{
Eric Fiselier910285b2014-10-27 19:28:201918 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant3e519522010-05-11 19:42:161919 if (__pred(*__first))
1920 *__result = __new_value;
1921 else
1922 *__result = *__first;
1923 return __result;
1924}
1925
1926// fill_n
1927
1928template <class _OutputIterator, class _Size, class _Tp>
Marshall Clow4bfb9312018-01-20 20:14:321929inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161930_OutputIterator
Howard Hinnant0f242be2013-08-01 17:29:281931__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:161932{
Eric Fiselier910285b2014-10-27 19:28:201933 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnante4383372011-10-22 20:59:451934 *__first = __value_;
Howard Hinnant3e519522010-05-11 19:42:161935 return __first;
1936}
1937
Howard Hinnant3e519522010-05-11 19:42:161938template <class _OutputIterator, class _Size, class _Tp>
Marshall Clow4bfb9312018-01-20 20:14:321939inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161940_OutputIterator
Howard Hinnante4383372011-10-22 20:59:451941fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:161942{
Eric Fiselier51544022015-02-10 16:46:421943 return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
Howard Hinnant3e519522010-05-11 19:42:161944}
1945
1946// fill
1947
1948template <class _ForwardIterator, class _Tp>
Marshall Clow4bfb9312018-01-20 20:14:321949inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161950void
Howard Hinnante4383372011-10-22 20:59:451951__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
Howard Hinnant3e519522010-05-11 19:42:161952{
1953 for (; __first != __last; ++__first)
Howard Hinnante4383372011-10-22 20:59:451954 *__first = __value_;
Howard Hinnant3e519522010-05-11 19:42:161955}
1956
1957template <class _RandomAccessIterator, class _Tp>
Marshall Clow4bfb9312018-01-20 20:14:321958inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161959void
Howard Hinnante4383372011-10-22 20:59:451960__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
Howard Hinnant3e519522010-05-11 19:42:161961{
Howard Hinnante4383372011-10-22 20:59:451962 _VSTD::fill_n(__first, __last - __first, __value_);
Howard Hinnant3e519522010-05-11 19:42:161963}
1964
1965template <class _ForwardIterator, class _Tp>
Marshall Clow4bfb9312018-01-20 20:14:321966inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161967void
Howard Hinnante4383372011-10-22 20:59:451968fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:161969{
Howard Hinnante4383372011-10-22 20:59:451970 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnant3e519522010-05-11 19:42:161971}
1972
1973// generate
1974
1975template <class _ForwardIterator, class _Generator>
Marshall Clow4bfb9312018-01-20 20:14:321976inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161977void
1978generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
1979{
1980 for (; __first != __last; ++__first)
1981 *__first = __gen();
1982}
1983
1984// generate_n
1985
1986template <class _OutputIterator, class _Size, class _Generator>
Marshall Clow4bfb9312018-01-20 20:14:321987inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161988_OutputIterator
Eric Fiselier51544022015-02-10 16:46:421989generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
Howard Hinnant3e519522010-05-11 19:42:161990{
Eric Fiselier51544022015-02-10 16:46:421991 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1992 _IntegralSize __n = __orig_n;
Eric Fiselier910285b2014-10-27 19:28:201993 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnant3e519522010-05-11 19:42:161994 *__first = __gen();
1995 return __first;
1996}
1997
1998// remove
1999
2000template <class _ForwardIterator, class _Tp>
Marshall Clowe8ea8292018-01-22 21:43:042001_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnante4383372011-10-22 20:59:452002remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:162003{
Howard Hinnante4383372011-10-22 20:59:452004 __first = _VSTD::find(__first, __last, __value_);
Howard Hinnant3e519522010-05-11 19:42:162005 if (__first != __last)
2006 {
2007 _ForwardIterator __i = __first;
2008 while (++__i != __last)
2009 {
Howard Hinnante4383372011-10-22 20:59:452010 if (!(*__i == __value_))
Howard Hinnant3e519522010-05-11 19:42:162011 {
Howard Hinnantce48a112011-06-30 21:18:192012 *__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:162013 ++__first;
2014 }
2015 }
2016 }
2017 return __first;
2018}
2019
2020// remove_if
2021
2022template <class _ForwardIterator, class _Predicate>
Marshall Clowe8ea8292018-01-22 21:43:042023_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:162024remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2025{
Howard Hinnantce48a112011-06-30 21:18:192026 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
Howard Hinnant3e519522010-05-11 19:42:162027 (__first, __last, __pred);
2028 if (__first != __last)
2029 {
2030 _ForwardIterator __i = __first;
2031 while (++__i != __last)
2032 {
2033 if (!__pred(*__i))
2034 {
Howard Hinnantce48a112011-06-30 21:18:192035 *__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:162036 ++__first;
2037 }
2038 }
2039 }
2040 return __first;
2041}
2042
2043// remove_copy
2044
2045template <class _InputIterator, class _OutputIterator, class _Tp>
Marshall Clowe8ea8292018-01-22 21:43:042046inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162047_OutputIterator
Howard Hinnante4383372011-10-22 20:59:452048remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:162049{
2050 for (; __first != __last; ++__first)
2051 {
Howard Hinnante4383372011-10-22 20:59:452052 if (!(*__first == __value_))
Howard Hinnant3e519522010-05-11 19:42:162053 {
2054 *__result = *__first;
2055 ++__result;
2056 }
2057 }
2058 return __result;
2059}
2060
2061// remove_copy_if
2062
2063template <class _InputIterator, class _OutputIterator, class _Predicate>
Marshall Clowe8ea8292018-01-22 21:43:042064inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162065_OutputIterator
2066remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2067{
2068 for (; __first != __last; ++__first)
2069 {
2070 if (!__pred(*__first))
2071 {
2072 *__result = *__first;
2073 ++__result;
2074 }
2075 }
2076 return __result;
2077}
2078
2079// unique
2080
2081template <class _ForwardIterator, class _BinaryPredicate>
Marshall Clow4bfb9312018-01-20 20:14:322082_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:162083unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2084{
Howard Hinnantce48a112011-06-30 21:18:192085 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnant3e519522010-05-11 19:42:162086 (__first, __last, __pred);
2087 if (__first != __last)
2088 {
2089 // ... a a ? ...
2090 // f i
2091 _ForwardIterator __i = __first;
2092 for (++__i; ++__i != __last;)
2093 if (!__pred(*__first, *__i))
Howard Hinnantce48a112011-06-30 21:18:192094 *++__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:162095 ++__first;
2096 }
2097 return __first;
2098}
2099
2100template <class _ForwardIterator>
Marshall Clow4bfb9312018-01-20 20:14:322101inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162102_ForwardIterator
2103unique(_ForwardIterator __first, _ForwardIterator __last)
2104{
2105 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnantce48a112011-06-30 21:18:192106 return _VSTD::unique(__first, __last, __equal_to<__v>());
Howard Hinnant3e519522010-05-11 19:42:162107}
2108
2109// unique_copy
2110
2111template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
Marshall Clow4bfb9312018-01-20 20:14:322112_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnant3e519522010-05-11 19:42:162113__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2114 input_iterator_tag, output_iterator_tag)
2115{
2116 if (__first != __last)
2117 {
2118 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2119 *__result = __t;
2120 ++__result;
2121 while (++__first != __last)
2122 {
2123 if (!__pred(__t, *__first))
2124 {
2125 __t = *__first;
2126 *__result = __t;
2127 ++__result;
2128 }
2129 }
2130 }
2131 return __result;
2132}
2133
2134template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
Marshall Clow4bfb9312018-01-20 20:14:322135_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnant3e519522010-05-11 19:42:162136__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2137 forward_iterator_tag, output_iterator_tag)
2138{
2139 if (__first != __last)
2140 {
2141 _ForwardIterator __i = __first;
2142 *__result = *__i;
2143 ++__result;
2144 while (++__first != __last)
2145 {
2146 if (!__pred(*__i, *__first))
2147 {
2148 *__result = *__first;
2149 ++__result;
2150 __i = __first;
2151 }
2152 }
2153 }
2154 return __result;
2155}
2156
2157template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
Marshall Clow4bfb9312018-01-20 20:14:322158_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:162159__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2160 input_iterator_tag, forward_iterator_tag)
2161{
2162 if (__first != __last)
2163 {
2164 *__result = *__first;
2165 while (++__first != __last)
2166 if (!__pred(*__result, *__first))
2167 *++__result = *__first;
2168 ++__result;
2169 }
2170 return __result;
2171}
2172
Howard Hinnant3e519522010-05-11 19:42:162173template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
Marshall Clow4bfb9312018-01-20 20:14:322174inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162175_OutputIterator
2176unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2177{
Howard Hinnantce48a112011-06-30 21:18:192178 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnant3e519522010-05-11 19:42:162179 (__first, __last, __result, __pred,
2180 typename iterator_traits<_InputIterator>::iterator_category(),
2181 typename iterator_traits<_OutputIterator>::iterator_category());
2182}
2183
2184template <class _InputIterator, class _OutputIterator>
Marshall Clow4bfb9312018-01-20 20:14:322185inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162186_OutputIterator
2187unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2188{
2189 typedef typename iterator_traits<_InputIterator>::value_type __v;
Howard Hinnantce48a112011-06-30 21:18:192190 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
Howard Hinnant3e519522010-05-11 19:42:162191}
2192
2193// reverse
2194
2195template <class _BidirectionalIterator>
2196inline _LIBCPP_INLINE_VISIBILITY
2197void
2198__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2199{
2200 while (__first != __last)
2201 {
2202 if (__first == --__last)
2203 break;
Marshall Clowdef501d2015-11-02 21:34:252204 _VSTD::iter_swap(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:162205 ++__first;
2206 }
2207}
2208
2209template <class _RandomAccessIterator>
2210inline _LIBCPP_INLINE_VISIBILITY
2211void
2212__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2213{
2214 if (__first != __last)
2215 for (; __first < --__last; ++__first)
Marshall Clowdef501d2015-11-02 21:34:252216 _VSTD::iter_swap(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:162217}
2218
2219template <class _BidirectionalIterator>
2220inline _LIBCPP_INLINE_VISIBILITY
2221void
2222reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2223{
Howard Hinnantce48a112011-06-30 21:18:192224 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
Howard Hinnant3e519522010-05-11 19:42:162225}
2226
2227// reverse_copy
2228
2229template <class _BidirectionalIterator, class _OutputIterator>
Marshall Clowe8ea8292018-01-22 21:43:042230inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162231_OutputIterator
2232reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2233{
2234 for (; __first != __last; ++__result)
2235 *__result = *--__last;
2236 return __result;
2237}
2238
2239// rotate
2240
2241template <class _ForwardIterator>
2242_ForwardIterator
Howard Hinnantaca09de2012-08-03 18:01:202243__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
Howard Hinnant3e519522010-05-11 19:42:162244{
Howard Hinnantaca09de2012-08-03 18:01:202245 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2246 value_type __tmp = _VSTD::move(*__first);
2247 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2248 *__lm1 = _VSTD::move(__tmp);
2249 return __lm1;
2250}
2251
2252template <class _BidirectionalIterator>
2253_BidirectionalIterator
2254__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2255{
2256 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2257 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2258 value_type __tmp = _VSTD::move(*__lm1);
2259 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2260 *__first = _VSTD::move(__tmp);
2261 return __fp1;
2262}
2263
2264template <class _ForwardIterator>
2265_ForwardIterator
2266__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2267{
Howard Hinnant3e519522010-05-11 19:42:162268 _ForwardIterator __i = __middle;
2269 while (true)
2270 {
2271 swap(*__first, *__i);
2272 ++__first;
2273 if (++__i == __last)
2274 break;
2275 if (__first == __middle)
2276 __middle = __i;
2277 }
2278 _ForwardIterator __r = __first;
2279 if (__first != __middle)
2280 {
2281 __i = __middle;
2282 while (true)
2283 {
2284 swap(*__first, *__i);
2285 ++__first;
2286 if (++__i == __last)
2287 {
2288 if (__first == __middle)
2289 break;
2290 __i = __middle;
2291 }
2292 else if (__first == __middle)
2293 __middle = __i;
2294 }
2295 }
2296 return __r;
2297}
2298
2299template<typename _Integral>
2300inline _LIBCPP_INLINE_VISIBILITY
2301_Integral
Marshall Clow19b40352016-07-26 14:29:452302__algo_gcd(_Integral __x, _Integral __y)
Howard Hinnant3e519522010-05-11 19:42:162303{
2304 do
2305 {
2306 _Integral __t = __x % __y;
2307 __x = __y;
2308 __y = __t;
2309 } while (__y);
2310 return __x;
2311}
2312
2313template<typename _RandomAccessIterator>
2314_RandomAccessIterator
Howard Hinnantaca09de2012-08-03 18:01:202315__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
Howard Hinnant3e519522010-05-11 19:42:162316{
2317 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2318 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnantb3371f62010-08-22 00:02:432319
Howard Hinnant3e519522010-05-11 19:42:162320 const difference_type __m1 = __middle - __first;
2321 const difference_type __m2 = __last - __middle;
2322 if (__m1 == __m2)
2323 {
Howard Hinnantce48a112011-06-30 21:18:192324 _VSTD::swap_ranges(__first, __middle, __middle);
Howard Hinnant3e519522010-05-11 19:42:162325 return __middle;
2326 }
Marshall Clow19b40352016-07-26 14:29:452327 const difference_type __g = _VSTD::__algo_gcd(__m1, __m2);
Howard Hinnant3e519522010-05-11 19:42:162328 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2329 {
Howard Hinnantaca09de2012-08-03 18:01:202330 value_type __t(_VSTD::move(*--__p));
Howard Hinnant3e519522010-05-11 19:42:162331 _RandomAccessIterator __p1 = __p;
2332 _RandomAccessIterator __p2 = __p1 + __m1;
2333 do
2334 {
Howard Hinnantaca09de2012-08-03 18:01:202335 *__p1 = _VSTD::move(*__p2);
Howard Hinnant3e519522010-05-11 19:42:162336 __p1 = __p2;
2337 const difference_type __d = __last - __p2;
2338 if (__m1 < __d)
2339 __p2 += __m1;
2340 else
2341 __p2 = __first + (__m1 - __d);
2342 } while (__p2 != __p);
Howard Hinnantaca09de2012-08-03 18:01:202343 *__p1 = _VSTD::move(__t);
Howard Hinnant3e519522010-05-11 19:42:162344 }
2345 return __first + __m2;
2346}
2347
2348template <class _ForwardIterator>
2349inline _LIBCPP_INLINE_VISIBILITY
2350_ForwardIterator
Howard Hinnantaca09de2012-08-03 18:01:202351__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2352 _VSTD::forward_iterator_tag)
2353{
2354 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2355 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2356 {
2357 if (_VSTD::next(__first) == __middle)
2358 return _VSTD::__rotate_left(__first, __last);
2359 }
2360 return _VSTD::__rotate_forward(__first, __middle, __last);
2361}
2362
2363template <class _BidirectionalIterator>
2364inline _LIBCPP_INLINE_VISIBILITY
2365_BidirectionalIterator
2366__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2367 _VSTD::bidirectional_iterator_tag)
2368{
2369 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2370 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2371 {
2372 if (_VSTD::next(__first) == __middle)
2373 return _VSTD::__rotate_left(__first, __last);
2374 if (_VSTD::next(__middle) == __last)
2375 return _VSTD::__rotate_right(__first, __last);
2376 }
2377 return _VSTD::__rotate_forward(__first, __middle, __last);
2378}
2379
2380template <class _RandomAccessIterator>
2381inline _LIBCPP_INLINE_VISIBILITY
2382_RandomAccessIterator
2383__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2384 _VSTD::random_access_iterator_tag)
2385{
2386 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2387 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2388 {
2389 if (_VSTD::next(__first) == __middle)
2390 return _VSTD::__rotate_left(__first, __last);
2391 if (_VSTD::next(__middle) == __last)
2392 return _VSTD::__rotate_right(__first, __last);
2393 return _VSTD::__rotate_gcd(__first, __middle, __last);
2394 }
2395 return _VSTD::__rotate_forward(__first, __middle, __last);
2396}
2397
2398template <class _ForwardIterator>
2399inline _LIBCPP_INLINE_VISIBILITY
2400_ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:162401rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2402{
Howard Hinnantaca09de2012-08-03 18:01:202403 if (__first == __middle)
2404 return __last;
2405 if (__middle == __last)
2406 return __first;
Howard Hinnantce48a112011-06-30 21:18:192407 return _VSTD::__rotate(__first, __middle, __last,
Howard Hinnantaca09de2012-08-03 18:01:202408 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnant3e519522010-05-11 19:42:162409}
2410
2411// rotate_copy
2412
2413template <class _ForwardIterator, class _OutputIterator>
2414inline _LIBCPP_INLINE_VISIBILITY
2415_OutputIterator
2416rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2417{
Howard Hinnantce48a112011-06-30 21:18:192418 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
Howard Hinnant3e519522010-05-11 19:42:162419}
2420
Howard Hinnant3e519522010-05-11 19:42:162421// min_element
2422
2423template <class _ForwardIterator, class _Compare>
Marshall Clow9d67c6d2014-02-19 16:51:352424inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant3e519522010-05-11 19:42:162425_ForwardIterator
Marshall Clow0b0671a2015-05-10 13:53:312426min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:162427{
Eric Fiseliera60d7fa2018-08-22 17:47:132428 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2429 "std::min_element requires a ForwardIterator");
Howard Hinnant3e519522010-05-11 19:42:162430 if (__first != __last)
2431 {
2432 _ForwardIterator __i = __first;
2433 while (++__i != __last)
2434 if (__comp(*__i, *__first))
2435 __first = __i;
2436 }
2437 return __first;
2438}
2439
2440template <class _ForwardIterator>
Marshall Clow0b0671a2015-05-10 13:53:312441inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant3e519522010-05-11 19:42:162442_ForwardIterator
2443min_element(_ForwardIterator __first, _ForwardIterator __last)
2444{
Marshall Clow0b0671a2015-05-10 13:53:312445 return _VSTD::min_element(__first, __last,
Howard Hinnant4eb27b72010-08-21 20:10:012446 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2447}
2448
2449// min
2450
2451template <class _Tp, class _Compare>
Marshall Clow9d67c6d2014-02-19 16:51:352452inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012453const _Tp&
2454min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2455{
2456 return __comp(__b, __a) ? __b : __a;
2457}
2458
2459template <class _Tp>
Marshall Clow9d67c6d2014-02-19 16:51:352460inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012461const _Tp&
2462min(const _Tp& __a, const _Tp& __b)
2463{
Howard Hinnantce48a112011-06-30 21:18:192464 return _VSTD::min(__a, __b, __less<_Tp>());
Howard Hinnant4eb27b72010-08-21 20:10:012465}
2466
Eric Fiselierddda4562017-04-18 23:26:472467#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022468
Howard Hinnant4eb27b72010-08-21 20:10:012469template<class _Tp, class _Compare>
Marshall Clow9d67c6d2014-02-19 16:51:352470inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012471_Tp
2472min(initializer_list<_Tp> __t, _Compare __comp)
2473{
Marshall Clow0b0671a2015-05-10 13:53:312474 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
Howard Hinnant4eb27b72010-08-21 20:10:012475}
2476
2477template<class _Tp>
Marshall Clow9d67c6d2014-02-19 16:51:352478inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012479_Tp
2480min(initializer_list<_Tp> __t)
2481{
Marshall Clow0b0671a2015-05-10 13:53:312482 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnant3e519522010-05-11 19:42:162483}
2484
Eric Fiselierddda4562017-04-18 23:26:472485#endif // _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022486
Howard Hinnant3e519522010-05-11 19:42:162487// max_element
2488
2489template <class _ForwardIterator, class _Compare>
Marshall Clow9d67c6d2014-02-19 16:51:352490inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant3e519522010-05-11 19:42:162491_ForwardIterator
Marshall Clow0b0671a2015-05-10 13:53:312492max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:162493{
Eric Fiseliera60d7fa2018-08-22 17:47:132494 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2495 "std::max_element requires a ForwardIterator");
Howard Hinnant3e519522010-05-11 19:42:162496 if (__first != __last)
2497 {
2498 _ForwardIterator __i = __first;
2499 while (++__i != __last)
2500 if (__comp(*__first, *__i))
2501 __first = __i;
2502 }
2503 return __first;
2504}
2505
Marshall Clow9d67c6d2014-02-19 16:51:352506
Howard Hinnant3e519522010-05-11 19:42:162507template <class _ForwardIterator>
Marshall Clow0b0671a2015-05-10 13:53:312508inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant3e519522010-05-11 19:42:162509_ForwardIterator
2510max_element(_ForwardIterator __first, _ForwardIterator __last)
2511{
Marshall Clow0b0671a2015-05-10 13:53:312512 return _VSTD::max_element(__first, __last,
Howard Hinnant4eb27b72010-08-21 20:10:012513 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2514}
2515
2516// max
2517
2518template <class _Tp, class _Compare>
Marshall Clow9d67c6d2014-02-19 16:51:352519inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012520const _Tp&
2521max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2522{
2523 return __comp(__a, __b) ? __b : __a;
2524}
2525
2526template <class _Tp>
Marshall Clow9d67c6d2014-02-19 16:51:352527inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012528const _Tp&
2529max(const _Tp& __a, const _Tp& __b)
2530{
Howard Hinnantce48a112011-06-30 21:18:192531 return _VSTD::max(__a, __b, __less<_Tp>());
Howard Hinnant4eb27b72010-08-21 20:10:012532}
2533
Eric Fiselierddda4562017-04-18 23:26:472534#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022535
Howard Hinnant4eb27b72010-08-21 20:10:012536template<class _Tp, class _Compare>
Marshall Clow9d67c6d2014-02-19 16:51:352537inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012538_Tp
2539max(initializer_list<_Tp> __t, _Compare __comp)
2540{
Marshall Clow0b0671a2015-05-10 13:53:312541 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
Howard Hinnant4eb27b72010-08-21 20:10:012542}
2543
2544template<class _Tp>
Marshall Clow9d67c6d2014-02-19 16:51:352545inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012546_Tp
2547max(initializer_list<_Tp> __t)
2548{
Marshall Clow0b0671a2015-05-10 13:53:312549 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnant3e519522010-05-11 19:42:162550}
2551
Eric Fiselierddda4562017-04-18 23:26:472552#endif // _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022553
Marshall Clow146c14a2016-03-07 22:43:492554#if _LIBCPP_STD_VER > 14
2555// clamp
2556template<class _Tp, class _Compare>
2557inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2558const _Tp&
2559clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
2560{
2561 _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
2562 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
2563
2564}
2565
2566template<class _Tp>
2567inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2568const _Tp&
2569clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
2570{
2571 return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
2572}
2573#endif
2574
Howard Hinnant3e519522010-05-11 19:42:162575// minmax_element
2576
2577template <class _ForwardIterator, class _Compare>
Marshall Clow0b0671a2015-05-10 13:53:312578_LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant3e519522010-05-11 19:42:162579std::pair<_ForwardIterator, _ForwardIterator>
2580minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2581{
Eric Fiseliera60d7fa2018-08-22 17:47:132582 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2583 "std::minmax_element requires a ForwardIterator");
Howard Hinnant3e519522010-05-11 19:42:162584 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2585 if (__first != __last)
2586 {
2587 if (++__first != __last)
2588 {
2589 if (__comp(*__first, *__result.first))
Howard Hinnant3e519522010-05-11 19:42:162590 __result.first = __first;
Howard Hinnant3e519522010-05-11 19:42:162591 else
2592 __result.second = __first;
2593 while (++__first != __last)
2594 {
2595 _ForwardIterator __i = __first;
2596 if (++__first == __last)
2597 {
2598 if (__comp(*__i, *__result.first))
2599 __result.first = __i;
2600 else if (!__comp(*__i, *__result.second))
2601 __result.second = __i;
2602 break;
2603 }
2604 else
2605 {
2606 if (__comp(*__first, *__i))
2607 {
2608 if (__comp(*__first, *__result.first))
2609 __result.first = __first;
2610 if (!__comp(*__i, *__result.second))
2611 __result.second = __i;
2612 }
2613 else
2614 {
2615 if (__comp(*__i, *__result.first))
2616 __result.first = __i;
2617 if (!__comp(*__first, *__result.second))
2618 __result.second = __first;
2619 }
2620 }
2621 }
2622 }
2623 }
2624 return __result;
2625}
2626
2627template <class _ForwardIterator>
Marshall Clow0b0671a2015-05-10 13:53:312628inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant3e519522010-05-11 19:42:162629std::pair<_ForwardIterator, _ForwardIterator>
2630minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2631{
Marshall Clow9d67c6d2014-02-19 16:51:352632 return _VSTD::minmax_element(__first, __last,
2633 __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:162634}
2635
Howard Hinnant4eb27b72010-08-21 20:10:012636// minmax
2637
2638template<class _Tp, class _Compare>
Marshall Clow9d67c6d2014-02-19 16:51:352639inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012640pair<const _Tp&, const _Tp&>
2641minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2642{
2643 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2644 pair<const _Tp&, const _Tp&>(__a, __b);
2645}
2646
2647template<class _Tp>
Marshall Clow9d67c6d2014-02-19 16:51:352648inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012649pair<const _Tp&, const _Tp&>
2650minmax(const _Tp& __a, const _Tp& __b)
2651{
Howard Hinnantce48a112011-06-30 21:18:192652 return _VSTD::minmax(__a, __b, __less<_Tp>());
Howard Hinnant4eb27b72010-08-21 20:10:012653}
2654
Eric Fiselierddda4562017-04-18 23:26:472655#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022656
Howard Hinnant4eb27b72010-08-21 20:10:012657template<class _Tp, class _Compare>
Marshall Clow9d67c6d2014-02-19 16:51:352658inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012659pair<_Tp, _Tp>
2660minmax(initializer_list<_Tp> __t, _Compare __comp)
2661{
Marshall Clow9d67c6d2014-02-19 16:51:352662 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2663 _Iter __first = __t.begin();
2664 _Iter __last = __t.end();
Marshall Clow002144f2015-02-11 15:41:342665 std::pair<_Tp, _Tp> __result(*__first, *__first);
Marshall Clow9d67c6d2014-02-19 16:51:352666
2667 ++__first;
2668 if (__t.size() % 2 == 0)
2669 {
2670 if (__comp(*__first, __result.first))
2671 __result.first = *__first;
2672 else
2673 __result.second = *__first;
2674 ++__first;
2675 }
Aditya Kumar331fb802016-08-25 11:52:382676
Marshall Clow9d67c6d2014-02-19 16:51:352677 while (__first != __last)
2678 {
2679 _Tp __prev = *__first++;
Marshall Clow002144f2015-02-11 15:41:342680 if (__comp(*__first, __prev)) {
2681 if ( __comp(*__first, __result.first)) __result.first = *__first;
2682 if (!__comp(__prev, __result.second)) __result.second = __prev;
Marshall Clow9d67c6d2014-02-19 16:51:352683 }
2684 else {
Marshall Clow002144f2015-02-11 15:41:342685 if ( __comp(__prev, __result.first)) __result.first = __prev;
2686 if (!__comp(*__first, __result.second)) __result.second = *__first;
Marshall Clow9d67c6d2014-02-19 16:51:352687 }
Aditya Kumar331fb802016-08-25 11:52:382688
Marshall Clow9d67c6d2014-02-19 16:51:352689 __first++;
2690 }
2691 return __result;
2692}
2693
2694template<class _Tp>
2695inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2696pair<_Tp, _Tp>
2697minmax(initializer_list<_Tp> __t)
2698{
2699 return _VSTD::minmax(__t, __less<_Tp>());
Howard Hinnant4eb27b72010-08-21 20:10:012700}
2701
Eric Fiselierddda4562017-04-18 23:26:472702#endif // _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022703
Howard Hinnant3e519522010-05-11 19:42:162704// random_shuffle
2705
Howard Hinnantf9d540b2010-05-26 17:49:342706// __independent_bits_engine
2707
Howard Hinnantc003db12011-11-29 18:15:502708template <unsigned long long _Xp, size_t _Rp>
Howard Hinnantf9d540b2010-05-26 17:49:342709struct __log2_imp
Howard Hinnant3e519522010-05-11 19:42:162710{
Howard Hinnantc003db12011-11-29 18:15:502711 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2712 : __log2_imp<_Xp, _Rp - 1>::value;
Howard Hinnant3e519522010-05-11 19:42:162713};
2714
Howard Hinnantc003db12011-11-29 18:15:502715template <unsigned long long _Xp>
2716struct __log2_imp<_Xp, 0>
Howard Hinnant3e519522010-05-11 19:42:162717{
Howard Hinnantf9d540b2010-05-26 17:49:342718 static const size_t value = 0;
Howard Hinnant3e519522010-05-11 19:42:162719};
2720
Howard Hinnantc003db12011-11-29 18:15:502721template <size_t _Rp>
2722struct __log2_imp<0, _Rp>
Howard Hinnant3e519522010-05-11 19:42:162723{
Howard Hinnantc003db12011-11-29 18:15:502724 static const size_t value = _Rp + 1;
Howard Hinnant3e519522010-05-11 19:42:162725};
2726
Eric Fiselier89918ca2017-05-31 21:20:182727template <class _UIntType, _UIntType _Xp>
Howard Hinnantf9d540b2010-05-26 17:49:342728struct __log2
Howard Hinnant3e519522010-05-11 19:42:162729{
Howard Hinnantc003db12011-11-29 18:15:502730 static const size_t value = __log2_imp<_Xp,
Eric Fiselier89918ca2017-05-31 21:20:182731 sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
Howard Hinnant3e519522010-05-11 19:42:162732};
2733
Howard Hinnantf9d540b2010-05-26 17:49:342734template<class _Engine, class _UIntType>
2735class __independent_bits_engine
Howard Hinnant3e519522010-05-11 19:42:162736{
Howard Hinnantf9d540b2010-05-26 17:49:342737public:
2738 // types
2739 typedef _UIntType result_type;
2740
2741private:
2742 typedef typename _Engine::result_type _Engine_result_type;
2743 typedef typename conditional
2744 <
2745 sizeof(_Engine_result_type) <= sizeof(result_type),
2746 result_type,
2747 _Engine_result_type
2748 >::type _Working_result_type;
2749
2750 _Engine& __e_;
2751 size_t __w_;
2752 size_t __w0_;
2753 size_t __n_;
2754 size_t __n0_;
2755 _Working_result_type __y0_;
2756 _Working_result_type __y1_;
2757 _Engine_result_type __mask0_;
2758 _Engine_result_type __mask1_;
2759
Eric Fiselierddda4562017-04-18 23:26:472760#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc003db12011-11-29 18:15:502761 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnante386b7b2012-04-02 21:00:452762 + _Working_result_type(1);
2763#else
2764 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2765 + _Working_result_type(1);
2766#endif
2767 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2768 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2769 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
Howard Hinnantf9d540b2010-05-26 17:49:342770
2771public:
2772 // constructors and seeding functions
2773 __independent_bits_engine(_Engine& __e, size_t __w);
2774
2775 // generating functions
Howard Hinnantc003db12011-11-29 18:15:502776 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantf9d540b2010-05-26 17:49:342777
2778private:
Marshall Clow08bba642017-09-20 19:38:432779 result_type __eval(false_type);
2780 result_type __eval(true_type);
Howard Hinnantf9d540b2010-05-26 17:49:342781};
2782
2783template<class _Engine, class _UIntType>
2784__independent_bits_engine<_Engine, _UIntType>
2785 ::__independent_bits_engine(_Engine& __e, size_t __w)
2786 : __e_(__e),
2787 __w_(__w)
2788{
2789 __n_ = __w_ / __m + (__w_ % __m != 0);
2790 __w0_ = __w_ / __n_;
Howard Hinnantc003db12011-11-29 18:15:502791 if (_Rp == 0)
2792 __y0_ = _Rp;
Howard Hinnantf9d540b2010-05-26 17:49:342793 else if (__w0_ < _WDt)
Howard Hinnantc003db12011-11-29 18:15:502794 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantf9d540b2010-05-26 17:49:342795 else
2796 __y0_ = 0;
Howard Hinnantc003db12011-11-29 18:15:502797 if (_Rp - __y0_ > __y0_ / __n_)
Howard Hinnantf9d540b2010-05-26 17:49:342798 {
2799 ++__n_;
2800 __w0_ = __w_ / __n_;
2801 if (__w0_ < _WDt)
Howard Hinnantc003db12011-11-29 18:15:502802 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantf9d540b2010-05-26 17:49:342803 else
2804 __y0_ = 0;
2805 }
2806 __n0_ = __n_ - __w_ % __n_;
2807 if (__w0_ < _WDt - 1)
Howard Hinnantc003db12011-11-29 18:15:502808 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
Howard Hinnantf9d540b2010-05-26 17:49:342809 else
2810 __y1_ = 0;
2811 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2812 _Engine_result_type(0);
2813 __mask1_ = __w0_ < _EDt - 1 ?
2814 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2815 _Engine_result_type(~0);
Howard Hinnant3e519522010-05-11 19:42:162816}
2817
Howard Hinnantf9d540b2010-05-26 17:49:342818template<class _Engine, class _UIntType>
2819inline
2820_UIntType
Marshall Clow08bba642017-09-20 19:38:432821__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
Howard Hinnant3e519522010-05-11 19:42:162822{
Howard Hinnantf9d540b2010-05-26 17:49:342823 return static_cast<result_type>(__e_() & __mask0_);
Howard Hinnant3e519522010-05-11 19:42:162824}
2825
Howard Hinnantf9d540b2010-05-26 17:49:342826template<class _Engine, class _UIntType>
2827_UIntType
Marshall Clow08bba642017-09-20 19:38:432828__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
Howard Hinnant3e519522010-05-11 19:42:162829{
Marshall Clow5beb2c32017-09-20 17:34:112830 const size_t _WRt = numeric_limits<result_type>::digits;
Howard Hinnantc003db12011-11-29 18:15:502831 result_type _Sp = 0;
Howard Hinnantf9d540b2010-05-26 17:49:342832 for (size_t __k = 0; __k < __n0_; ++__k)
2833 {
2834 _Engine_result_type __u;
2835 do
2836 {
2837 __u = __e_() - _Engine::min();
2838 } while (__u >= __y0_);
Marshall Clow5beb2c32017-09-20 17:34:112839 if (__w0_ < _WRt)
Howard Hinnantc003db12011-11-29 18:15:502840 _Sp <<= __w0_;
Howard Hinnantf9d540b2010-05-26 17:49:342841 else
Howard Hinnantc003db12011-11-29 18:15:502842 _Sp = 0;
2843 _Sp += __u & __mask0_;
Howard Hinnantf9d540b2010-05-26 17:49:342844 }
2845 for (size_t __k = __n0_; __k < __n_; ++__k)
2846 {
2847 _Engine_result_type __u;
2848 do
2849 {
2850 __u = __e_() - _Engine::min();
2851 } while (__u >= __y1_);
Marshall Clow5beb2c32017-09-20 17:34:112852 if (__w0_ < _WRt - 1)
Howard Hinnantc003db12011-11-29 18:15:502853 _Sp <<= __w0_ + 1;
Howard Hinnantf9d540b2010-05-26 17:49:342854 else
Howard Hinnantc003db12011-11-29 18:15:502855 _Sp = 0;
2856 _Sp += __u & __mask1_;
Howard Hinnantf9d540b2010-05-26 17:49:342857 }
Howard Hinnantc003db12011-11-29 18:15:502858 return _Sp;
Howard Hinnantf9d540b2010-05-26 17:49:342859}
2860
2861// uniform_int_distribution
2862
2863template<class _IntType = int>
2864class uniform_int_distribution
2865{
2866public:
2867 // types
2868 typedef _IntType result_type;
2869
2870 class param_type
2871 {
2872 result_type __a_;
2873 result_type __b_;
2874 public:
2875 typedef uniform_int_distribution distribution_type;
2876
2877 explicit param_type(result_type __a = 0,
2878 result_type __b = numeric_limits<result_type>::max())
2879 : __a_(__a), __b_(__b) {}
2880
2881 result_type a() const {return __a_;}
2882 result_type b() const {return __b_;}
2883
2884 friend bool operator==(const param_type& __x, const param_type& __y)
2885 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2886 friend bool operator!=(const param_type& __x, const param_type& __y)
2887 {return !(__x == __y);}
2888 };
2889
2890private:
2891 param_type __p_;
2892
2893public:
2894 // constructors and reset functions
2895 explicit uniform_int_distribution(result_type __a = 0,
2896 result_type __b = numeric_limits<result_type>::max())
2897 : __p_(param_type(__a, __b)) {}
2898 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
2899 void reset() {}
2900
2901 // generating functions
2902 template<class _URNG> result_type operator()(_URNG& __g)
2903 {return (*this)(__g, __p_);}
2904 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2905
2906 // property functions
2907 result_type a() const {return __p_.a();}
2908 result_type b() const {return __p_.b();}
2909
2910 param_type param() const {return __p_;}
2911 void param(const param_type& __p) {__p_ = __p;}
2912
2913 result_type min() const {return a();}
2914 result_type max() const {return b();}
2915
2916 friend bool operator==(const uniform_int_distribution& __x,
2917 const uniform_int_distribution& __y)
2918 {return __x.__p_ == __y.__p_;}
2919 friend bool operator!=(const uniform_int_distribution& __x,
2920 const uniform_int_distribution& __y)
2921 {return !(__x == __y);}
2922};
2923
2924template<class _IntType>
2925template<class _URNG>
2926typename uniform_int_distribution<_IntType>::result_type
2927uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
Marshall Clow2dd53352018-10-08 20:20:342928_LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantf9d540b2010-05-26 17:49:342929{
2930 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
2931 uint32_t, uint64_t>::type _UIntType;
Marshall Clow2dd53352018-10-08 20:20:342932 const _UIntType _Rp = _UIntType(__p.b()) - _UIntType(__p.a()) + _UIntType(1);
Howard Hinnantc003db12011-11-29 18:15:502933 if (_Rp == 1)
Howard Hinnantf9d540b2010-05-26 17:49:342934 return __p.a();
2935 const size_t _Dt = numeric_limits<_UIntType>::digits;
2936 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
Howard Hinnantc003db12011-11-29 18:15:502937 if (_Rp == 0)
Howard Hinnantf9d540b2010-05-26 17:49:342938 return static_cast<result_type>(_Eng(__g, _Dt)());
Howard Hinnantc003db12011-11-29 18:15:502939 size_t __w = _Dt - __clz(_Rp) - 1;
Marshall Clowa6438ca2015-07-30 18:26:342940 if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
Howard Hinnantf9d540b2010-05-26 17:49:342941 ++__w;
2942 _Eng __e(__g, __w);
2943 _UIntType __u;
Howard Hinnant3e519522010-05-11 19:42:162944 do
Howard Hinnantf9d540b2010-05-26 17:49:342945 {
2946 __u = __e();
Howard Hinnantc003db12011-11-29 18:15:502947 } while (__u >= _Rp);
Howard Hinnantf9d540b2010-05-26 17:49:342948 return static_cast<result_type>(__u + __p.a());
Howard Hinnant3e519522010-05-11 19:42:162949}
2950
Eric Fiselier66f1ec42017-04-03 23:23:442951#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \
2952 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnantf0544c22013-08-12 18:38:342953class _LIBCPP_TYPE_VIS __rs_default;
Howard Hinnant3e519522010-05-11 19:42:162954
Howard Hinnantf0544c22013-08-12 18:38:342955_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantf9d540b2010-05-26 17:49:342956
Howard Hinnantf0544c22013-08-12 18:38:342957class _LIBCPP_TYPE_VIS __rs_default
Howard Hinnant3e519522010-05-11 19:42:162958{
Howard Hinnantf9d540b2010-05-26 17:49:342959 static unsigned __c_;
2960
2961 __rs_default();
2962public:
Marshall Clowb6e5f852013-02-07 22:12:022963 typedef uint_fast32_t result_type;
Howard Hinnantf9d540b2010-05-26 17:49:342964
2965 static const result_type _Min = 0;
2966 static const result_type _Max = 0xFFFFFFFF;
2967
2968 __rs_default(const __rs_default&);
2969 ~__rs_default();
2970
2971 result_type operator()();
2972
Howard Hinnant788c9972012-04-02 00:40:412973 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
2974 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
Howard Hinnantf9d540b2010-05-26 17:49:342975
Howard Hinnantf0544c22013-08-12 18:38:342976 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnant3e519522010-05-11 19:42:162977};
2978
Howard Hinnantf0544c22013-08-12 18:38:342979_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnant3e519522010-05-11 19:42:162980
2981template <class _RandomAccessIterator>
Louis Dionneea5cd3b2018-09-23 18:35:002982_LIBCPP_DEPRECATED_IN_CXX14 void
Howard Hinnant3e519522010-05-11 19:42:162983random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
2984{
2985 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc003db12011-11-29 18:15:502986 typedef uniform_int_distribution<ptrdiff_t> _Dp;
2987 typedef typename _Dp::param_type _Pp;
Howard Hinnant3e519522010-05-11 19:42:162988 difference_type __d = __last - __first;
2989 if (__d > 1)
2990 {
Howard Hinnantc003db12011-11-29 18:15:502991 _Dp __uid;
Howard Hinnantf9d540b2010-05-26 17:49:342992 __rs_default __g = __rs_get();
2993 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant007b26b2010-10-22 15:26:392994 {
Howard Hinnantc003db12011-11-29 18:15:502995 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant007b26b2010-10-22 15:26:392996 if (__i != difference_type(0))
2997 swap(*__first, *(__first + __i));
2998 }
Howard Hinnant3e519522010-05-11 19:42:162999 }
3000}
3001
3002template <class _RandomAccessIterator, class _RandomNumberGenerator>
Louis Dionneea5cd3b2018-09-23 18:35:003003_LIBCPP_DEPRECATED_IN_CXX14 void
Howard Hinnant3e519522010-05-11 19:42:163004random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselierddda4562017-04-18 23:26:473005#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:163006 _RandomNumberGenerator&& __rand)
3007#else
3008 _RandomNumberGenerator& __rand)
3009#endif
3010{
3011 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3012 difference_type __d = __last - __first;
3013 if (__d > 1)
3014 {
3015 for (--__last; __first < __last; ++__first, --__d)
Howard Hinnant007b26b2010-10-22 15:26:393016 {
3017 difference_type __i = __rand(__d);
Marshall Clowe9cc5452018-09-11 18:33:453018 if (__i != difference_type(0))
Marshall Clow2dd53352018-10-08 20:20:343019 swap(*__first, *(__first + __i));
Howard Hinnant007b26b2010-10-22 15:26:393020 }
Howard Hinnant3e519522010-05-11 19:42:163021 }
3022}
Marshall Clow0f37a412017-03-23 13:43:373023#endif
Howard Hinnant3e519522010-05-11 19:42:163024
Eric Fiseliere7154702016-08-28 22:14:373025template <class _PopulationIterator, class _SampleIterator, class _Distance,
3026 class _UniformRandomNumberGenerator>
3027_LIBCPP_INLINE_VISIBILITY
3028_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardson42bfedd2017-11-14 11:14:253029 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiseliere7154702016-08-28 22:14:373030 _Distance __n,
3031 _UniformRandomNumberGenerator & __g,
3032 input_iterator_tag) {
3033
3034 _Distance __k = 0;
3035 for (; __first != __last && __k < __n; ++__first, (void)++__k)
Alexander Richardson42bfedd2017-11-14 11:14:253036 __output_iter[__k] = *__first;
Eric Fiseliere7154702016-08-28 22:14:373037 _Distance __sz = __k;
3038 for (; __first != __last; ++__first, (void)++__k) {
3039 _Distance __r = _VSTD::uniform_int_distribution<_Distance>(0, __k)(__g);
3040 if (__r < __sz)
Alexander Richardson42bfedd2017-11-14 11:14:253041 __output_iter[__r] = *__first;
Eric Fiseliere7154702016-08-28 22:14:373042 }
Alexander Richardson42bfedd2017-11-14 11:14:253043 return __output_iter + _VSTD::min(__n, __k);
Eric Fiseliere7154702016-08-28 22:14:373044}
3045
3046template <class _PopulationIterator, class _SampleIterator, class _Distance,
3047 class _UniformRandomNumberGenerator>
3048_LIBCPP_INLINE_VISIBILITY
3049_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardson42bfedd2017-11-14 11:14:253050 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiseliere7154702016-08-28 22:14:373051 _Distance __n,
3052 _UniformRandomNumberGenerator& __g,
3053 forward_iterator_tag) {
3054 _Distance __unsampled_sz = _VSTD::distance(__first, __last);
3055 for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
3056 _Distance __r =
3057 _VSTD::uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
3058 if (__r < __n) {
Alexander Richardson42bfedd2017-11-14 11:14:253059 *__output_iter++ = *__first;
Eric Fiseliere7154702016-08-28 22:14:373060 --__n;
3061 }
3062 }
Alexander Richardson42bfedd2017-11-14 11:14:253063 return __output_iter;
Eric Fiseliere7154702016-08-28 22:14:373064}
3065
3066template <class _PopulationIterator, class _SampleIterator, class _Distance,
3067 class _UniformRandomNumberGenerator>
3068_LIBCPP_INLINE_VISIBILITY
3069_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardson42bfedd2017-11-14 11:14:253070 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiseliere7154702016-08-28 22:14:373071 _Distance __n, _UniformRandomNumberGenerator& __g) {
3072 typedef typename iterator_traits<_PopulationIterator>::iterator_category
3073 _PopCategory;
3074 typedef typename iterator_traits<_PopulationIterator>::difference_type
3075 _Difference;
3076 static_assert(__is_forward_iterator<_PopulationIterator>::value ||
3077 __is_random_access_iterator<_SampleIterator>::value,
3078 "SampleIterator must meet the requirements of RandomAccessIterator");
3079 typedef typename common_type<_Distance, _Difference>::type _CommonType;
3080 _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
3081 return _VSTD::__sample(
Alexander Richardson42bfedd2017-11-14 11:14:253082 __first, __last, __output_iter, _CommonType(__n),
Eric Fiseliere7154702016-08-28 22:14:373083 __g, _PopCategory());
3084}
3085
3086#if _LIBCPP_STD_VER > 14
3087template <class _PopulationIterator, class _SampleIterator, class _Distance,
3088 class _UniformRandomNumberGenerator>
3089inline _LIBCPP_INLINE_VISIBILITY
3090_SampleIterator sample(_PopulationIterator __first,
Alexander Richardson42bfedd2017-11-14 11:14:253091 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiseliere7154702016-08-28 22:14:373092 _Distance __n, _UniformRandomNumberGenerator&& __g) {
Alexander Richardson42bfedd2017-11-14 11:14:253093 return _VSTD::__sample(__first, __last, __output_iter, __n, __g);
Eric Fiseliere7154702016-08-28 22:14:373094}
3095#endif // _LIBCPP_STD_VER > 14
3096
Howard Hinnantf9d540b2010-05-26 17:49:343097template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3098 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselierddda4562017-04-18 23:26:473099#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantfb340102010-11-18 01:47:023100 _UniformRandomNumberGenerator&& __g)
3101#else
Howard Hinnantf9d540b2010-05-26 17:49:343102 _UniformRandomNumberGenerator& __g)
Howard Hinnantfb340102010-11-18 01:47:023103#endif
Howard Hinnantf9d540b2010-05-26 17:49:343104{
3105 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc003db12011-11-29 18:15:503106 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3107 typedef typename _Dp::param_type _Pp;
Howard Hinnantf9d540b2010-05-26 17:49:343108 difference_type __d = __last - __first;
3109 if (__d > 1)
3110 {
Howard Hinnantc003db12011-11-29 18:15:503111 _Dp __uid;
Howard Hinnantf9d540b2010-05-26 17:49:343112 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant007b26b2010-10-22 15:26:393113 {
Howard Hinnantc003db12011-11-29 18:15:503114 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant007b26b2010-10-22 15:26:393115 if (__i != difference_type(0))
3116 swap(*__first, *(__first + __i));
3117 }
Howard Hinnantf9d540b2010-05-26 17:49:343118 }
3119}
3120
Howard Hinnant3e519522010-05-11 19:42:163121template <class _InputIterator, class _Predicate>
Marshall Clow49c76432018-01-15 16:16:323122_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnant3e519522010-05-11 19:42:163123is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3124{
3125 for (; __first != __last; ++__first)
3126 if (!__pred(*__first))
3127 break;
Marshall Clowb9595b72015-02-02 18:16:353128 if ( __first == __last )
3129 return true;
3130 ++__first;
Howard Hinnant3e519522010-05-11 19:42:163131 for (; __first != __last; ++__first)
3132 if (__pred(*__first))
3133 return false;
3134 return true;
3135}
3136
3137// partition
3138
3139template <class _Predicate, class _ForwardIterator>
3140_ForwardIterator
3141__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3142{
3143 while (true)
3144 {
3145 if (__first == __last)
3146 return __first;
3147 if (!__pred(*__first))
3148 break;
3149 ++__first;
3150 }
3151 for (_ForwardIterator __p = __first; ++__p != __last;)
3152 {
3153 if (__pred(*__p))
3154 {
3155 swap(*__first, *__p);
3156 ++__first;
3157 }
3158 }
3159 return __first;
3160}
3161
3162template <class _Predicate, class _BidirectionalIterator>
3163_BidirectionalIterator
3164__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3165 bidirectional_iterator_tag)
3166{
3167 while (true)
3168 {
3169 while (true)
3170 {
3171 if (__first == __last)
3172 return __first;
3173 if (!__pred(*__first))
3174 break;
3175 ++__first;
3176 }
3177 do
3178 {
3179 if (__first == --__last)
3180 return __first;
3181 } while (!__pred(*__last));
3182 swap(*__first, *__last);
3183 ++__first;
3184 }
3185}
3186
3187template <class _ForwardIterator, class _Predicate>
3188inline _LIBCPP_INLINE_VISIBILITY
3189_ForwardIterator
3190partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3191{
Howard Hinnantce48a112011-06-30 21:18:193192 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnant3e519522010-05-11 19:42:163193 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3194}
3195
3196// partition_copy
3197
3198template <class _InputIterator, class _OutputIterator1,
3199 class _OutputIterator2, class _Predicate>
Marshall Clow1b9a4ff2018-01-22 20:44:333200_LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_OutputIterator1, _OutputIterator2>
Howard Hinnant3e519522010-05-11 19:42:163201partition_copy(_InputIterator __first, _InputIterator __last,
3202 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3203 _Predicate __pred)
3204{
3205 for (; __first != __last; ++__first)
3206 {
3207 if (__pred(*__first))
3208 {
3209 *__out_true = *__first;
3210 ++__out_true;
3211 }
3212 else
3213 {
3214 *__out_false = *__first;
3215 ++__out_false;
3216 }
3217 }
3218 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3219}
3220
3221// partition_point
3222
3223template<class _ForwardIterator, class _Predicate>
Marshall Clow674f9122018-01-15 17:53:343224_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:163225partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3226{
3227 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantce48a112011-06-30 21:18:193228 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:163229 while (__len != 0)
3230 {
Louis Dionne04695a72018-12-17 16:04:393231 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnant3e519522010-05-11 19:42:163232 _ForwardIterator __m = __first;
Howard Hinnantce48a112011-06-30 21:18:193233 _VSTD::advance(__m, __l2);
Howard Hinnant3e519522010-05-11 19:42:163234 if (__pred(*__m))
3235 {
3236 __first = ++__m;
3237 __len -= __l2 + 1;
3238 }
3239 else
3240 __len = __l2;
3241 }
3242 return __first;
3243}
3244
3245// stable_partition
3246
3247template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3248_ForwardIterator
3249__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3250 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3251{
3252 // *__first is known to be false
3253 // __len >= 1
3254 if (__len == 1)
3255 return __first;
3256 if (__len == 2)
3257 {
3258 _ForwardIterator __m = __first;
3259 if (__pred(*++__m))
3260 {
3261 swap(*__first, *__m);
3262 return __m;
3263 }
3264 return __first;
3265 }
3266 if (__len <= __p.second)
3267 { // The buffer is big enough to use
3268 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3269 __destruct_n __d(0);
3270 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3271 // Move the falses into the temporary buffer, and the trues to the front of the line
3272 // Update __first to always point to the end of the trues
3273 value_type* __t = __p.first;
Howard Hinnantce48a112011-06-30 21:18:193274 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnant3e519522010-05-11 19:42:163275 __d.__incr((value_type*)0);
3276 ++__t;
3277 _ForwardIterator __i = __first;
3278 while (++__i != __last)
3279 {
3280 if (__pred(*__i))
3281 {
Howard Hinnantce48a112011-06-30 21:18:193282 *__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:163283 ++__first;
3284 }
3285 else
3286 {
Howard Hinnantce48a112011-06-30 21:18:193287 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnant3e519522010-05-11 19:42:163288 __d.__incr((value_type*)0);
3289 ++__t;
3290 }
3291 }
3292 // All trues now at start of range, all falses in buffer
3293 // Move falses back into range, but don't mess up __first which points to first false
3294 __i = __first;
3295 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnantce48a112011-06-30 21:18:193296 *__i = _VSTD::move(*__t2);
Howard Hinnant3e519522010-05-11 19:42:163297 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3298 return __first;
3299 }
3300 // Else not enough buffer, do in place
3301 // __len >= 3
3302 _ForwardIterator __m = __first;
3303 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnantce48a112011-06-30 21:18:193304 _VSTD::advance(__m, __len2);
Howard Hinnant3e519522010-05-11 19:42:163305 // recurse on [__first, __m), *__first know to be false
3306 // F?????????????????
3307 // f m l
3308 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3309 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3310 // TTTFFFFF??????????
3311 // f ff m l
3312 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3313 _ForwardIterator __m1 = __m;
3314 _ForwardIterator __second_false = __last;
3315 _Distance __len_half = __len - __len2;
3316 while (__pred(*__m1))
3317 {
3318 if (++__m1 == __last)
3319 goto __second_half_done;
3320 --__len_half;
3321 }
3322 // TTTFFFFFTTTF??????
3323 // f ff m m1 l
3324 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3325__second_half_done:
3326 // TTTFFFFFTTTTTFFFFF
3327 // f ff m sf l
Howard Hinnantce48a112011-06-30 21:18:193328 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnant3e519522010-05-11 19:42:163329 // TTTTTTTTFFFFFFFFFF
3330 // |
3331}
3332
3333struct __return_temporary_buffer
3334{
3335 template <class _Tp>
Howard Hinnantce48a112011-06-30 21:18:193336 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
Howard Hinnant3e519522010-05-11 19:42:163337};
3338
3339template <class _Predicate, class _ForwardIterator>
3340_ForwardIterator
3341__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3342 forward_iterator_tag)
3343{
3344 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3345 // Either prove all true and return __first or point to first false
3346 while (true)
3347 {
3348 if (__first == __last)
3349 return __first;
3350 if (!__pred(*__first))
3351 break;
3352 ++__first;
3353 }
3354 // We now have a reduced range [__first, __last)
3355 // *__first is known to be false
3356 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3357 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnantce48a112011-06-30 21:18:193358 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:163359 pair<value_type*, ptrdiff_t> __p(0, 0);
3360 unique_ptr<value_type, __return_temporary_buffer> __h;
3361 if (__len >= __alloc_limit)
3362 {
Howard Hinnantce48a112011-06-30 21:18:193363 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnant3e519522010-05-11 19:42:163364 __h.reset(__p.first);
3365 }
3366 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3367 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3368}
3369
3370template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3371_BidirectionalIterator
3372__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3373 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3374{
3375 // *__first is known to be false
3376 // *__last is known to be true
3377 // __len >= 2
3378 if (__len == 2)
3379 {
3380 swap(*__first, *__last);
3381 return __last;
3382 }
3383 if (__len == 3)
3384 {
3385 _BidirectionalIterator __m = __first;
3386 if (__pred(*++__m))
3387 {
3388 swap(*__first, *__m);
3389 swap(*__m, *__last);
3390 return __last;
3391 }
3392 swap(*__m, *__last);
3393 swap(*__first, *__m);
3394 return __m;
3395 }
3396 if (__len <= __p.second)
3397 { // The buffer is big enough to use
3398 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3399 __destruct_n __d(0);
3400 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3401 // Move the falses into the temporary buffer, and the trues to the front of the line
3402 // Update __first to always point to the end of the trues
3403 value_type* __t = __p.first;
Howard Hinnantce48a112011-06-30 21:18:193404 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnant3e519522010-05-11 19:42:163405 __d.__incr((value_type*)0);
3406 ++__t;
3407 _BidirectionalIterator __i = __first;
3408 while (++__i != __last)
3409 {
3410 if (__pred(*__i))
3411 {
Howard Hinnantce48a112011-06-30 21:18:193412 *__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:163413 ++__first;
3414 }
3415 else
3416 {
Howard Hinnantce48a112011-06-30 21:18:193417 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnant3e519522010-05-11 19:42:163418 __d.__incr((value_type*)0);
3419 ++__t;
3420 }
3421 }
3422 // move *__last, known to be true
Howard Hinnantce48a112011-06-30 21:18:193423 *__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:163424 __i = ++__first;
3425 // All trues now at start of range, all falses in buffer
3426 // Move falses back into range, but don't mess up __first which points to first false
3427 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnantce48a112011-06-30 21:18:193428 *__i = _VSTD::move(*__t2);
Howard Hinnant3e519522010-05-11 19:42:163429 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3430 return __first;
3431 }
3432 // Else not enough buffer, do in place
3433 // __len >= 4
3434 _BidirectionalIterator __m = __first;
3435 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnantce48a112011-06-30 21:18:193436 _VSTD::advance(__m, __len2);
Howard Hinnant3e519522010-05-11 19:42:163437 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3438 // F????????????????T
3439 // f m l
3440 _BidirectionalIterator __m1 = __m;
3441 _BidirectionalIterator __first_false = __first;
3442 _Distance __len_half = __len2;
3443 while (!__pred(*--__m1))
3444 {
3445 if (__m1 == __first)
3446 goto __first_half_done;
3447 --__len_half;
3448 }
3449 // F???TFFF?????????T
3450 // f m1 m l
3451 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3452 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3453__first_half_done:
3454 // TTTFFFFF?????????T
3455 // f ff m l
3456 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3457 __m1 = __m;
3458 _BidirectionalIterator __second_false = __last;
3459 ++__second_false;
3460 __len_half = __len - __len2;
3461 while (__pred(*__m1))
3462 {
3463 if (++__m1 == __last)
3464 goto __second_half_done;
3465 --__len_half;
3466 }
3467 // TTTFFFFFTTTF?????T
3468 // f ff m m1 l
3469 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3470__second_half_done:
3471 // TTTFFFFFTTTTTFFFFF
3472 // f ff m sf l
Howard Hinnantce48a112011-06-30 21:18:193473 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnant3e519522010-05-11 19:42:163474 // TTTTTTTTFFFFFFFFFF
3475 // |
3476}
3477
3478template <class _Predicate, class _BidirectionalIterator>
3479_BidirectionalIterator
3480__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3481 bidirectional_iterator_tag)
3482{
3483 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3484 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3485 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3486 // Either prove all true and return __first or point to first false
3487 while (true)
3488 {
3489 if (__first == __last)
3490 return __first;
3491 if (!__pred(*__first))
3492 break;
3493 ++__first;
3494 }
3495 // __first points to first false, everything prior to __first is already set.
3496 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3497 do
3498 {
3499 if (__first == --__last)
3500 return __first;
3501 } while (!__pred(*__last));
3502 // We now have a reduced range [__first, __last]
3503 // *__first is known to be false
3504 // *__last is known to be true
3505 // __len >= 2
Howard Hinnantce48a112011-06-30 21:18:193506 difference_type __len = _VSTD::distance(__first, __last) + 1;
Howard Hinnant3e519522010-05-11 19:42:163507 pair<value_type*, ptrdiff_t> __p(0, 0);
3508 unique_ptr<value_type, __return_temporary_buffer> __h;
3509 if (__len >= __alloc_limit)
3510 {
Howard Hinnantce48a112011-06-30 21:18:193511 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnant3e519522010-05-11 19:42:163512 __h.reset(__p.first);
3513 }
3514 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3515 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3516}
3517
3518template <class _ForwardIterator, class _Predicate>
3519inline _LIBCPP_INLINE_VISIBILITY
3520_ForwardIterator
3521stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3522{
3523 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3524 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3525}
3526
3527// is_sorted_until
3528
3529template <class _ForwardIterator, class _Compare>
Marshall Clow49c76432018-01-15 16:16:323530_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:163531is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3532{
3533 if (__first != __last)
3534 {
3535 _ForwardIterator __i = __first;
3536 while (++__i != __last)
3537 {
3538 if (__comp(*__i, *__first))
3539 return __i;
3540 __first = __i;
3541 }
3542 }
3543 return __last;
3544}
3545
Howard Hinnantb3371f62010-08-22 00:02:433546template<class _ForwardIterator>
Marshall Clow49c76432018-01-15 16:16:323547inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:163548_ForwardIterator
3549is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3550{
Howard Hinnantce48a112011-06-30 21:18:193551 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:163552}
3553
3554// is_sorted
3555
3556template <class _ForwardIterator, class _Compare>
Marshall Clow49c76432018-01-15 16:16:323557inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:163558bool
3559is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3560{
Howard Hinnantce48a112011-06-30 21:18:193561 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
Howard Hinnant3e519522010-05-11 19:42:163562}
3563
Howard Hinnantb3371f62010-08-22 00:02:433564template<class _ForwardIterator>
Marshall Clow49c76432018-01-15 16:16:323565inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:163566bool
3567is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3568{
Howard Hinnantce48a112011-06-30 21:18:193569 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:163570}
3571
3572// sort
3573
3574// stable, 2-3 compares, 0-2 swaps
3575
3576template <class _Compare, class _ForwardIterator>
3577unsigned
3578__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3579{
3580 unsigned __r = 0;
3581 if (!__c(*__y, *__x)) // if x <= y
3582 {
3583 if (!__c(*__z, *__y)) // if y <= z
3584 return __r; // x <= y && y <= z
3585 // x <= y && y > z
3586 swap(*__y, *__z); // x <= z && y < z
3587 __r = 1;
3588 if (__c(*__y, *__x)) // if x > y
3589 {
3590 swap(*__x, *__y); // x < y && y <= z
3591 __r = 2;
3592 }
3593 return __r; // x <= y && y < z
3594 }
3595 if (__c(*__z, *__y)) // x > y, if y > z
3596 {
3597 swap(*__x, *__z); // x < y && y < z
3598 __r = 1;
3599 return __r;
3600 }
3601 swap(*__x, *__y); // x > y && y <= z
3602 __r = 1; // x < y && x <= z
3603 if (__c(*__z, *__y)) // if y > z
3604 {
3605 swap(*__y, *__z); // x <= y && y < z
3606 __r = 2;
3607 }
3608 return __r;
3609} // x <= y && y <= z
3610
3611// stable, 3-6 compares, 0-5 swaps
3612
3613template <class _Compare, class _ForwardIterator>
3614unsigned
3615__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3616 _ForwardIterator __x4, _Compare __c)
3617{
3618 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3619 if (__c(*__x4, *__x3))
3620 {
3621 swap(*__x3, *__x4);
3622 ++__r;
3623 if (__c(*__x3, *__x2))
3624 {
3625 swap(*__x2, *__x3);
3626 ++__r;
3627 if (__c(*__x2, *__x1))
3628 {
3629 swap(*__x1, *__x2);
3630 ++__r;
3631 }
3632 }
3633 }
3634 return __r;
3635}
3636
3637// stable, 4-10 compares, 0-9 swaps
3638
3639template <class _Compare, class _ForwardIterator>
Louis Dionne835140a2018-11-21 16:24:463640_LIBCPP_HIDDEN
Howard Hinnant3e519522010-05-11 19:42:163641unsigned
3642__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3643 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3644{
3645 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3646 if (__c(*__x5, *__x4))
3647 {
3648 swap(*__x4, *__x5);
3649 ++__r;
3650 if (__c(*__x4, *__x3))
3651 {
3652 swap(*__x3, *__x4);
3653 ++__r;
3654 if (__c(*__x3, *__x2))
3655 {
3656 swap(*__x2, *__x3);
3657 ++__r;
3658 if (__c(*__x2, *__x1))
3659 {
3660 swap(*__x1, *__x2);
3661 ++__r;
3662 }
3663 }
3664 }
3665 }
3666 return __r;
3667}
3668
3669// Assumes size > 0
3670template <class _Compare, class _BirdirectionalIterator>
3671void
3672__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3673{
3674 _BirdirectionalIterator __lm1 = __last;
3675 for (--__lm1; __first != __lm1; ++__first)
3676 {
Howard Hinnantce48a112011-06-30 21:18:193677 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
Howard Hinnant3e519522010-05-11 19:42:163678 typename add_lvalue_reference<_Compare>::type>
3679 (__first, __last, __comp);
3680 if (__i != __first)
3681 swap(*__first, *__i);
3682 }
3683}
3684
3685template <class _Compare, class _BirdirectionalIterator>
3686void
3687__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3688{
3689 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3690 if (__first != __last)
3691 {
3692 _BirdirectionalIterator __i = __first;
3693 for (++__i; __i != __last; ++__i)
3694 {
3695 _BirdirectionalIterator __j = __i;
Howard Hinnantce48a112011-06-30 21:18:193696 value_type __t(_VSTD::move(*__j));
Howard Hinnant3e519522010-05-11 19:42:163697 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
Howard Hinnantce48a112011-06-30 21:18:193698 *__j = _VSTD::move(*__k);
3699 *__j = _VSTD::move(__t);
Howard Hinnant3e519522010-05-11 19:42:163700 }
3701 }
3702}
3703
3704template <class _Compare, class _RandomAccessIterator>
3705void
3706__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3707{
3708 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3709 _RandomAccessIterator __j = __first+2;
3710 __sort3<_Compare>(__first, __first+1, __j, __comp);
3711 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3712 {
3713 if (__comp(*__i, *__j))
3714 {
Howard Hinnantce48a112011-06-30 21:18:193715 value_type __t(_VSTD::move(*__i));
Howard Hinnant3e519522010-05-11 19:42:163716 _RandomAccessIterator __k = __j;
3717 __j = __i;
3718 do
3719 {
Howard Hinnantce48a112011-06-30 21:18:193720 *__j = _VSTD::move(*__k);
Howard Hinnant3e519522010-05-11 19:42:163721 __j = __k;
3722 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnantce48a112011-06-30 21:18:193723 *__j = _VSTD::move(__t);
Howard Hinnant3e519522010-05-11 19:42:163724 }
3725 __j = __i;
3726 }
3727}
3728
3729template <class _Compare, class _RandomAccessIterator>
3730bool
3731__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3732{
3733 switch (__last - __first)
3734 {
3735 case 0:
3736 case 1:
3737 return true;
3738 case 2:
3739 if (__comp(*--__last, *__first))
3740 swap(*__first, *__last);
3741 return true;
3742 case 3:
Howard Hinnantce48a112011-06-30 21:18:193743 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163744 return true;
3745 case 4:
Howard Hinnantce48a112011-06-30 21:18:193746 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163747 return true;
3748 case 5:
Howard Hinnantce48a112011-06-30 21:18:193749 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163750 return true;
3751 }
3752 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3753 _RandomAccessIterator __j = __first+2;
3754 __sort3<_Compare>(__first, __first+1, __j, __comp);
3755 const unsigned __limit = 8;
3756 unsigned __count = 0;
3757 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3758 {
3759 if (__comp(*__i, *__j))
3760 {
Howard Hinnantce48a112011-06-30 21:18:193761 value_type __t(_VSTD::move(*__i));
Howard Hinnant3e519522010-05-11 19:42:163762 _RandomAccessIterator __k = __j;
3763 __j = __i;
3764 do
3765 {
Howard Hinnantce48a112011-06-30 21:18:193766 *__j = _VSTD::move(*__k);
Howard Hinnant3e519522010-05-11 19:42:163767 __j = __k;
3768 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnantce48a112011-06-30 21:18:193769 *__j = _VSTD::move(__t);
Howard Hinnant3e519522010-05-11 19:42:163770 if (++__count == __limit)
3771 return ++__i == __last;
3772 }
3773 __j = __i;
3774 }
3775 return true;
3776}
3777
3778template <class _Compare, class _BirdirectionalIterator>
3779void
3780__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3781 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3782{
3783 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3784 if (__first1 != __last1)
3785 {
3786 __destruct_n __d(0);
3787 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3788 value_type* __last2 = __first2;
Howard Hinnantce48a112011-06-30 21:18:193789 ::new(__last2) value_type(_VSTD::move(*__first1));
Howard Hinnant3e519522010-05-11 19:42:163790 __d.__incr((value_type*)0);
3791 for (++__last2; ++__first1 != __last1; ++__last2)
3792 {
3793 value_type* __j2 = __last2;
3794 value_type* __i2 = __j2;
3795 if (__comp(*__first1, *--__i2))
3796 {
Howard Hinnantce48a112011-06-30 21:18:193797 ::new(__j2) value_type(_VSTD::move(*__i2));
Howard Hinnant3e519522010-05-11 19:42:163798 __d.__incr((value_type*)0);
3799 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
Howard Hinnantce48a112011-06-30 21:18:193800 *__j2 = _VSTD::move(*__i2);
3801 *__j2 = _VSTD::move(*__first1);
Howard Hinnant3e519522010-05-11 19:42:163802 }
3803 else
3804 {
Howard Hinnantce48a112011-06-30 21:18:193805 ::new(__j2) value_type(_VSTD::move(*__first1));
Howard Hinnant3e519522010-05-11 19:42:163806 __d.__incr((value_type*)0);
3807 }
3808 }
3809 __h.release();
3810 }
3811}
3812
3813template <class _Compare, class _RandomAccessIterator>
3814void
3815__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3816{
3817 // _Compare is known to be a reference type
3818 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3819 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnantca740482010-11-19 22:17:283820 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3821 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
Howard Hinnant3e519522010-05-11 19:42:163822 while (true)
3823 {
3824 __restart:
3825 difference_type __len = __last - __first;
3826 switch (__len)
3827 {
3828 case 0:
3829 case 1:
3830 return;
3831 case 2:
3832 if (__comp(*--__last, *__first))
3833 swap(*__first, *__last);
3834 return;
3835 case 3:
Howard Hinnantce48a112011-06-30 21:18:193836 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163837 return;
3838 case 4:
Howard Hinnantce48a112011-06-30 21:18:193839 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163840 return;
3841 case 5:
Howard Hinnantce48a112011-06-30 21:18:193842 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163843 return;
3844 }
3845 if (__len <= __limit)
3846 {
Howard Hinnantce48a112011-06-30 21:18:193847 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163848 return;
3849 }
3850 // __len > 5
3851 _RandomAccessIterator __m = __first;
3852 _RandomAccessIterator __lm1 = __last;
3853 --__lm1;
3854 unsigned __n_swaps;
3855 {
3856 difference_type __delta;
3857 if (__len >= 1000)
3858 {
3859 __delta = __len/2;
3860 __m += __delta;
3861 __delta /= 2;
Howard Hinnantce48a112011-06-30 21:18:193862 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
Howard Hinnant3e519522010-05-11 19:42:163863 }
3864 else
3865 {
3866 __delta = __len/2;
3867 __m += __delta;
Howard Hinnantce48a112011-06-30 21:18:193868 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
Howard Hinnant3e519522010-05-11 19:42:163869 }
3870 }
3871 // *__m is median
3872 // partition [__first, __m) < *__m and *__m <= [__m, __last)
3873 // (this inhibits tossing elements equivalent to __m around unnecessarily)
3874 _RandomAccessIterator __i = __first;
3875 _RandomAccessIterator __j = __lm1;
3876 // j points beyond range to be tested, *__m is known to be <= *__lm1
3877 // The search going up is known to be guarded but the search coming down isn't.
3878 // Prime the downward search with a guard.
3879 if (!__comp(*__i, *__m)) // if *__first == *__m
3880 {
3881 // *__first == *__m, *__first doesn't go in first part
3882 // manually guard downward moving __j against __i
3883 while (true)
3884 {
3885 if (__i == --__j)
3886 {
3887 // *__first == *__m, *__m <= all other elements
3888 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
3889 ++__i; // __first + 1
3890 __j = __last;
3891 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
3892 {
3893 while (true)
3894 {
3895 if (__i == __j)
3896 return; // [__first, __last) all equivalent elements
3897 if (__comp(*__first, *__i))
3898 {
3899 swap(*__i, *__j);
3900 ++__n_swaps;
3901 ++__i;
3902 break;
3903 }
3904 ++__i;
3905 }
3906 }
3907 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
3908 if (__i == __j)
3909 return;
3910 while (true)
3911 {
3912 while (!__comp(*__first, *__i))
3913 ++__i;
3914 while (__comp(*__first, *--__j))
3915 ;
3916 if (__i >= __j)
3917 break;
3918 swap(*__i, *__j);
3919 ++__n_swaps;
3920 ++__i;
3921 }
3922 // [__first, __i) == *__first and *__first < [__i, __last)
3923 // The first part is sorted, sort the secod part
Howard Hinnantce48a112011-06-30 21:18:193924 // _VSTD::__sort<_Compare>(__i, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163925 __first = __i;
3926 goto __restart;
3927 }
3928 if (__comp(*__j, *__m))
3929 {
3930 swap(*__i, *__j);
3931 ++__n_swaps;
3932 break; // found guard for downward moving __j, now use unguarded partition
3933 }
3934 }
3935 }
3936 // It is known that *__i < *__m
3937 ++__i;
3938 // j points beyond range to be tested, *__m is known to be <= *__lm1
3939 // if not yet partitioned...
3940 if (__i < __j)
3941 {
3942 // known that *(__i - 1) < *__m
3943 // known that __i <= __m
3944 while (true)
3945 {
3946 // __m still guards upward moving __i
3947 while (__comp(*__i, *__m))
3948 ++__i;
3949 // It is now known that a guard exists for downward moving __j
3950 while (!__comp(*--__j, *__m))
3951 ;
3952 if (__i > __j)
3953 break;
3954 swap(*__i, *__j);
3955 ++__n_swaps;
3956 // It is known that __m != __j
3957 // If __m just moved, follow it
3958 if (__m == __i)
3959 __m = __j;
3960 ++__i;
3961 }
3962 }
3963 // [__first, __i) < *__m and *__m <= [__i, __last)
3964 if (__i != __m && __comp(*__m, *__i))
3965 {
3966 swap(*__i, *__m);
3967 ++__n_swaps;
3968 }
3969 // [__first, __i) < *__i and *__i <= [__i+1, __last)
3970 // If we were given a perfect partition, see if insertion sort is quick...
3971 if (__n_swaps == 0)
3972 {
Howard Hinnantce48a112011-06-30 21:18:193973 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
3974 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
Howard Hinnant3e519522010-05-11 19:42:163975 {
3976 if (__fs)
3977 return;
3978 __last = __i;
3979 continue;
3980 }
3981 else
3982 {
3983 if (__fs)
3984 {
3985 __first = ++__i;
3986 continue;
3987 }
3988 }
3989 }
3990 // sort smaller range with recursive call and larger with tail recursion elimination
3991 if (__i - __first < __last - __i)
3992 {
Howard Hinnantce48a112011-06-30 21:18:193993 _VSTD::__sort<_Compare>(__first, __i, __comp);
3994 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163995 __first = ++__i;
3996 }
3997 else
3998 {
Howard Hinnantce48a112011-06-30 21:18:193999 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4000 // _VSTD::__sort<_Compare>(__first, __i, __comp);
Howard Hinnant3e519522010-05-11 19:42:164001 __last = __i;
4002 }
4003 }
4004}
4005
4006// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
4007template <class _RandomAccessIterator, class _Compare>
4008inline _LIBCPP_INLINE_VISIBILITY
4009void
4010sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4011{
Howard Hinnant145afa12013-08-23 20:10:184012#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164013 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4014 __debug_less<_Compare> __c(__comp);
4015 __sort<_Comp_ref>(__first, __last, __c);
Howard Hinnant145afa12013-08-23 20:10:184016#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164017 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4018 __sort<_Comp_ref>(__first, __last, __comp);
Howard Hinnant145afa12013-08-23 20:10:184019#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164020}
4021
4022template <class _RandomAccessIterator>
4023inline _LIBCPP_INLINE_VISIBILITY
4024void
4025sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4026{
Howard Hinnantce48a112011-06-30 21:18:194027 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164028}
4029
4030template <class _Tp>
4031inline _LIBCPP_INLINE_VISIBILITY
4032void
4033sort(_Tp** __first, _Tp** __last)
4034{
Howard Hinnantce48a112011-06-30 21:18:194035 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
Howard Hinnant3e519522010-05-11 19:42:164036}
4037
4038template <class _Tp>
4039inline _LIBCPP_INLINE_VISIBILITY
4040void
4041sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4042{
Howard Hinnantce48a112011-06-30 21:18:194043 _VSTD::sort(__first.base(), __last.base());
Howard Hinnant3e519522010-05-11 19:42:164044}
4045
Howard Hinnantf554add2011-09-14 18:33:514046template <class _Tp, class _Compare>
4047inline _LIBCPP_INLINE_VISIBILITY
4048void
4049sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4050{
4051 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4052 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4053}
4054
Howard Hinnantf0544c22013-08-12 18:38:344055_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4056_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4057_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4058_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4059_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4060_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4061_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4062_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4063_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4064_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4065_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4066_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>&))
4067_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4068_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4069_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
Howard Hinnant3e519522010-05-11 19:42:164070
Howard Hinnantf0544c22013-08-12 18:38:344071_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4072_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4073_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4074_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4075_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4076_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4077_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4078_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4079_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4080_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4081_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4082_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>&))
4083_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4084_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4085_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
Howard Hinnant3e519522010-05-11 19:42:164086
Howard Hinnantf0544c22013-08-12 18:38:344087_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 Hinnant3e519522010-05-11 19:42:164088
4089// lower_bound
4090
4091template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414092_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454093__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164094{
4095 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantce48a112011-06-30 21:18:194096 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:164097 while (__len != 0)
4098 {
Louis Dionne04695a72018-12-17 16:04:394099 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnant3e519522010-05-11 19:42:164100 _ForwardIterator __m = __first;
Howard Hinnantce48a112011-06-30 21:18:194101 _VSTD::advance(__m, __l2);
Howard Hinnante4383372011-10-22 20:59:454102 if (__comp(*__m, __value_))
Howard Hinnant3e519522010-05-11 19:42:164103 {
4104 __first = ++__m;
4105 __len -= __l2 + 1;
4106 }
4107 else
4108 __len = __l2;
4109 }
4110 return __first;
4111}
4112
4113template <class _ForwardIterator, class _Tp, class _Compare>
Marshall Clowd57c03d2018-01-16 02:34:414114inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164115_ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454116lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164117{
Howard Hinnant3e519522010-05-11 19:42:164118 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnante4383372011-10-22 20:59:454119 return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant3e519522010-05-11 19:42:164120}
4121
4122template <class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414123inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164124_ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454125lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:164126{
Howard Hinnante4383372011-10-22 20:59:454127 return _VSTD::lower_bound(__first, __last, __value_,
Howard Hinnant3e519522010-05-11 19:42:164128 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4129}
4130
4131// upper_bound
4132
4133template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414134_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454135__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164136{
4137 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantce48a112011-06-30 21:18:194138 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:164139 while (__len != 0)
4140 {
Louis Dionne04695a72018-12-17 16:04:394141 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnant3e519522010-05-11 19:42:164142 _ForwardIterator __m = __first;
Howard Hinnantce48a112011-06-30 21:18:194143 _VSTD::advance(__m, __l2);
Howard Hinnante4383372011-10-22 20:59:454144 if (__comp(__value_, *__m))
Howard Hinnant3e519522010-05-11 19:42:164145 __len = __l2;
4146 else
4147 {
4148 __first = ++__m;
4149 __len -= __l2 + 1;
4150 }
4151 }
4152 return __first;
4153}
4154
4155template <class _ForwardIterator, class _Tp, class _Compare>
Marshall Clowd57c03d2018-01-16 02:34:414156inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164157_ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454158upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164159{
Howard Hinnant3e519522010-05-11 19:42:164160 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnante4383372011-10-22 20:59:454161 return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant3e519522010-05-11 19:42:164162}
4163
4164template <class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414165inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164166_ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454167upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:164168{
Howard Hinnante4383372011-10-22 20:59:454169 return _VSTD::upper_bound(__first, __last, __value_,
Howard Hinnant3e519522010-05-11 19:42:164170 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4171}
4172
4173// equal_range
4174
4175template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414176_LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_ForwardIterator, _ForwardIterator>
Howard Hinnante4383372011-10-22 20:59:454177__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164178{
4179 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantce48a112011-06-30 21:18:194180 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:164181 while (__len != 0)
4182 {
Louis Dionne04695a72018-12-17 16:04:394183 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnant3e519522010-05-11 19:42:164184 _ForwardIterator __m = __first;
Howard Hinnantce48a112011-06-30 21:18:194185 _VSTD::advance(__m, __l2);
Howard Hinnante4383372011-10-22 20:59:454186 if (__comp(*__m, __value_))
Howard Hinnant3e519522010-05-11 19:42:164187 {
4188 __first = ++__m;
4189 __len -= __l2 + 1;
4190 }
Howard Hinnante4383372011-10-22 20:59:454191 else if (__comp(__value_, *__m))
Howard Hinnant3e519522010-05-11 19:42:164192 {
4193 __last = __m;
4194 __len = __l2;
4195 }
4196 else
4197 {
4198 _ForwardIterator __mp1 = __m;
4199 return pair<_ForwardIterator, _ForwardIterator>
4200 (
Howard Hinnante4383372011-10-22 20:59:454201 __lower_bound<_Compare>(__first, __m, __value_, __comp),
4202 __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
Howard Hinnant3e519522010-05-11 19:42:164203 );
4204 }
4205 }
4206 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4207}
4208
4209template <class _ForwardIterator, class _Tp, class _Compare>
Marshall Clowd57c03d2018-01-16 02:34:414210inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164211pair<_ForwardIterator, _ForwardIterator>
Howard Hinnante4383372011-10-22 20:59:454212equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164213{
Howard Hinnant145afa12013-08-23 20:10:184214#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164215 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4216 __debug_less<_Compare> __c(__comp);
Howard Hinnante4383372011-10-22 20:59:454217 return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant145afa12013-08-23 20:10:184218#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164219 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnante4383372011-10-22 20:59:454220 return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant145afa12013-08-23 20:10:184221#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164222}
4223
4224template <class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414225inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164226pair<_ForwardIterator, _ForwardIterator>
Howard Hinnante4383372011-10-22 20:59:454227equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:164228{
Howard Hinnante4383372011-10-22 20:59:454229 return _VSTD::equal_range(__first, __last, __value_,
Howard Hinnant3e519522010-05-11 19:42:164230 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4231}
4232
4233// binary_search
4234
4235template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414236inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164237bool
Howard Hinnante4383372011-10-22 20:59:454238__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164239{
Howard Hinnante4383372011-10-22 20:59:454240 __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4241 return __first != __last && !__comp(__value_, *__first);
Howard Hinnant3e519522010-05-11 19:42:164242}
4243
4244template <class _ForwardIterator, class _Tp, class _Compare>
Marshall Clowd57c03d2018-01-16 02:34:414245inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164246bool
Howard Hinnante4383372011-10-22 20:59:454247binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164248{
Howard Hinnant145afa12013-08-23 20:10:184249#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164250 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4251 __debug_less<_Compare> __c(__comp);
Howard Hinnante4383372011-10-22 20:59:454252 return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant145afa12013-08-23 20:10:184253#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164254 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnante4383372011-10-22 20:59:454255 return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant145afa12013-08-23 20:10:184256#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164257}
4258
4259template <class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414260inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164261bool
Howard Hinnante4383372011-10-22 20:59:454262binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:164263{
Howard Hinnante4383372011-10-22 20:59:454264 return _VSTD::binary_search(__first, __last, __value_,
Howard Hinnant3e519522010-05-11 19:42:164265 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4266}
4267
4268// merge
4269
4270template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4271_OutputIterator
4272__merge(_InputIterator1 __first1, _InputIterator1 __last1,
4273 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4274{
4275 for (; __first1 != __last1; ++__result)
4276 {
4277 if (__first2 == __last2)
Howard Hinnantce48a112011-06-30 21:18:194278 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnant3e519522010-05-11 19:42:164279 if (__comp(*__first2, *__first1))
4280 {
4281 *__result = *__first2;
4282 ++__first2;
4283 }
4284 else
4285 {
4286 *__result = *__first1;
4287 ++__first1;
4288 }
4289 }
Howard Hinnantce48a112011-06-30 21:18:194290 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnant3e519522010-05-11 19:42:164291}
4292
4293template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4294inline _LIBCPP_INLINE_VISIBILITY
4295_OutputIterator
4296merge(_InputIterator1 __first1, _InputIterator1 __last1,
4297 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4298{
Howard Hinnant145afa12013-08-23 20:10:184299#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164300 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4301 __debug_less<_Compare> __c(__comp);
Howard Hinnantce48a112011-06-30 21:18:194302 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant145afa12013-08-23 20:10:184303#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164304 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantce48a112011-06-30 21:18:194305 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant145afa12013-08-23 20:10:184306#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164307}
4308
4309template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4310inline _LIBCPP_INLINE_VISIBILITY
4311_OutputIterator
4312merge(_InputIterator1 __first1, _InputIterator1 __last1,
4313 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4314{
4315 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4316 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4317 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4318}
4319
4320// inplace_merge
4321
Marshall Clowadfdae12015-07-29 16:25:454322template <class _Compare, class _InputIterator1, class _InputIterator2,
4323 class _OutputIterator>
4324void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4325 _InputIterator2 __first2, _InputIterator2 __last2,
4326 _OutputIterator __result, _Compare __comp)
4327{
4328 for (; __first1 != __last1; ++__result)
4329 {
4330 if (__first2 == __last2)
4331 {
4332 _VSTD::move(__first1, __last1, __result);
4333 return;
4334 }
4335
4336 if (__comp(*__first2, *__first1))
4337 {
4338 *__result = _VSTD::move(*__first2);
4339 ++__first2;
4340 }
4341 else
4342 {
4343 *__result = _VSTD::move(*__first1);
4344 ++__first1;
4345 }
4346 }
4347 // __first2 through __last2 are already in the right spot.
4348}
4349
Howard Hinnant3e519522010-05-11 19:42:164350template <class _Compare, class _BidirectionalIterator>
4351void
4352__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4353 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4354 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4355 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4356{
4357 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
Howard Hinnant3e519522010-05-11 19:42:164358 __destruct_n __d(0);
4359 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4360 if (__len1 <= __len2)
4361 {
4362 value_type* __p = __buff;
Eric Fiselier910285b2014-10-27 19:28:204363 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnantce48a112011-06-30 21:18:194364 ::new(__p) value_type(_VSTD::move(*__i));
Marshall Clowadfdae12015-07-29 16:25:454365 __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
Howard Hinnant3e519522010-05-11 19:42:164366 }
4367 else
4368 {
4369 value_type* __p = __buff;
Eric Fiselier910285b2014-10-27 19:28:204370 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnantce48a112011-06-30 21:18:194371 ::new(__p) value_type(_VSTD::move(*__i));
Howard Hinnant3e519522010-05-11 19:42:164372 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4373 typedef reverse_iterator<value_type*> _Rv;
Aditya Kumar331fb802016-08-25 11:52:384374 __half_inplace_merge(_Rv(__p), _Rv(__buff),
Marshall Clowadfdae12015-07-29 16:25:454375 _RBi(__middle), _RBi(__first),
Marshall Clowa763b362017-08-28 23:16:134376 _RBi(__last), __invert<_Compare>(__comp));
Howard Hinnant3e519522010-05-11 19:42:164377 }
4378}
4379
4380template <class _Compare, class _BidirectionalIterator>
4381void
4382__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4383 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4384 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4385 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4386{
Howard Hinnant3e519522010-05-11 19:42:164387 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4388 while (true)
4389 {
4390 // if __middle == __last, we're done
4391 if (__len2 == 0)
4392 return;
Marshall Clow526e0922015-02-02 16:44:114393 if (__len1 <= __buff_size || __len2 <= __buff_size)
4394 return __buffered_inplace_merge<_Compare>
4395 (__first, __middle, __last, __comp, __len1, __len2, __buff);
Howard Hinnant3e519522010-05-11 19:42:164396 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
Eric Fiselier910285b2014-10-27 19:28:204397 for (; true; ++__first, (void) --__len1)
Howard Hinnant3e519522010-05-11 19:42:164398 {
4399 if (__len1 == 0)
4400 return;
4401 if (__comp(*__middle, *__first))
4402 break;
4403 }
Howard Hinnant3e519522010-05-11 19:42:164404 // __first < __middle < __last
4405 // *__first > *__middle
4406 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4407 // all elements in:
4408 // [__first, __m1) <= [__middle, __m2)
4409 // [__middle, __m2) < [__m1, __middle)
4410 // [__m1, __middle) <= [__m2, __last)
4411 // and __m1 or __m2 is in the middle of its range
4412 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4413 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4414 difference_type __len11; // distance(__first, __m1)
4415 difference_type __len21; // distance(__middle, __m2)
4416 // binary search smaller range
4417 if (__len1 < __len2)
4418 { // __len >= 1, __len2 >= 2
4419 __len21 = __len2 / 2;
4420 __m2 = __middle;
Howard Hinnantce48a112011-06-30 21:18:194421 _VSTD::advance(__m2, __len21);
Howard Hinnant3e519522010-05-11 19:42:164422 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
Howard Hinnantce48a112011-06-30 21:18:194423 __len11 = _VSTD::distance(__first, __m1);
Howard Hinnant3e519522010-05-11 19:42:164424 }
4425 else
4426 {
4427 if (__len1 == 1)
4428 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4429 // It is known *__first > *__middle
4430 swap(*__first, *__middle);
4431 return;
4432 }
4433 // __len1 >= 2, __len2 >= 1
4434 __len11 = __len1 / 2;
4435 __m1 = __first;
Howard Hinnantce48a112011-06-30 21:18:194436 _VSTD::advance(__m1, __len11);
Howard Hinnant3e519522010-05-11 19:42:164437 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
Howard Hinnantce48a112011-06-30 21:18:194438 __len21 = _VSTD::distance(__middle, __m2);
Howard Hinnant3e519522010-05-11 19:42:164439 }
4440 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4441 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4442 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4443 // swap middle two partitions
Howard Hinnantce48a112011-06-30 21:18:194444 __middle = _VSTD::rotate(__m1, __middle, __m2);
Howard Hinnant3e519522010-05-11 19:42:164445 // __len12 and __len21 now have swapped meanings
4446 // merge smaller range with recurisve call and larger with tail recursion elimination
4447 if (__len11 + __len21 < __len12 + __len22)
4448 {
4449 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4450// __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4451 __first = __middle;
4452 __middle = __m2;
4453 __len1 = __len12;
4454 __len2 = __len22;
4455 }
4456 else
4457 {
4458 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4459// __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4460 __last = __middle;
4461 __middle = __m1;
4462 __len1 = __len11;
4463 __len2 = __len21;
4464 }
4465 }
4466}
4467
Howard Hinnant3e519522010-05-11 19:42:164468template <class _BidirectionalIterator, class _Compare>
4469inline _LIBCPP_INLINE_VISIBILITY
4470void
4471inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4472 _Compare __comp)
4473{
4474 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4475 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnantce48a112011-06-30 21:18:194476 difference_type __len1 = _VSTD::distance(__first, __middle);
4477 difference_type __len2 = _VSTD::distance(__middle, __last);
4478 difference_type __buf_size = _VSTD::min(__len1, __len2);
Marshall Clow0b48cf92015-02-02 17:35:534479 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4480 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
4481
Howard Hinnant145afa12013-08-23 20:10:184482#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164483 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4484 __debug_less<_Compare> __c(__comp);
Howard Hinnantce48a112011-06-30 21:18:194485 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
Howard Hinnant3e519522010-05-11 19:42:164486 __buf.first, __buf.second);
Howard Hinnant145afa12013-08-23 20:10:184487#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164488 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantce48a112011-06-30 21:18:194489 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
Howard Hinnant3e519522010-05-11 19:42:164490 __buf.first, __buf.second);
Howard Hinnant145afa12013-08-23 20:10:184491#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164492}
4493
4494template <class _BidirectionalIterator>
4495inline _LIBCPP_INLINE_VISIBILITY
4496void
4497inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4498{
Howard Hinnantce48a112011-06-30 21:18:194499 _VSTD::inplace_merge(__first, __middle, __last,
Howard Hinnant3e519522010-05-11 19:42:164500 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4501}
4502
4503// stable_sort
4504
4505template <class _Compare, class _InputIterator1, class _InputIterator2>
4506void
4507__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4508 _InputIterator2 __first2, _InputIterator2 __last2,
4509 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4510{
4511 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4512 __destruct_n __d(0);
4513 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4514 for (; true; ++__result)
4515 {
4516 if (__first1 == __last1)
4517 {
4518 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
Howard Hinnantce48a112011-06-30 21:18:194519 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnant3e519522010-05-11 19:42:164520 __h.release();
4521 return;
4522 }
4523 if (__first2 == __last2)
4524 {
4525 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
Howard Hinnantce48a112011-06-30 21:18:194526 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnant3e519522010-05-11 19:42:164527 __h.release();
4528 return;
4529 }
4530 if (__comp(*__first2, *__first1))
4531 {
Howard Hinnantce48a112011-06-30 21:18:194532 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnant3e519522010-05-11 19:42:164533 __d.__incr((value_type*)0);
4534 ++__first2;
4535 }
4536 else
4537 {
Howard Hinnantce48a112011-06-30 21:18:194538 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnant3e519522010-05-11 19:42:164539 __d.__incr((value_type*)0);
4540 ++__first1;
4541 }
4542 }
4543}
4544
4545template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4546void
4547__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4548 _InputIterator2 __first2, _InputIterator2 __last2,
4549 _OutputIterator __result, _Compare __comp)
4550{
4551 for (; __first1 != __last1; ++__result)
4552 {
4553 if (__first2 == __last2)
4554 {
4555 for (; __first1 != __last1; ++__first1, ++__result)
Howard Hinnantce48a112011-06-30 21:18:194556 *__result = _VSTD::move(*__first1);
Howard Hinnant3e519522010-05-11 19:42:164557 return;
4558 }
4559 if (__comp(*__first2, *__first1))
4560 {
Howard Hinnantce48a112011-06-30 21:18:194561 *__result = _VSTD::move(*__first2);
Howard Hinnant3e519522010-05-11 19:42:164562 ++__first2;
4563 }
4564 else
4565 {
Howard Hinnantce48a112011-06-30 21:18:194566 *__result = _VSTD::move(*__first1);
Howard Hinnant3e519522010-05-11 19:42:164567 ++__first1;
4568 }
4569 }
4570 for (; __first2 != __last2; ++__first2, ++__result)
Howard Hinnantce48a112011-06-30 21:18:194571 *__result = _VSTD::move(*__first2);
Howard Hinnant3e519522010-05-11 19:42:164572}
4573
4574template <class _Compare, class _RandomAccessIterator>
4575void
4576__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4577 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4578 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4579
4580template <class _Compare, class _RandomAccessIterator>
4581void
4582__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4583 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4584 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4585{
4586 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4587 switch (__len)
4588 {
4589 case 0:
4590 return;
4591 case 1:
Howard Hinnantce48a112011-06-30 21:18:194592 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnant3e519522010-05-11 19:42:164593 return;
4594 case 2:
Marshall Clowf951fc32018-02-06 18:58:054595 __destruct_n __d(0);
Howard Hinnant3e519522010-05-11 19:42:164596 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
Marshall Clowf951fc32018-02-06 18:58:054597 if (__comp(*--__last1, *__first1))
Howard Hinnant3e519522010-05-11 19:42:164598 {
Howard Hinnantce48a112011-06-30 21:18:194599 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnant3e519522010-05-11 19:42:164600 __d.__incr((value_type*)0);
4601 ++__first2;
Howard Hinnantce48a112011-06-30 21:18:194602 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnant3e519522010-05-11 19:42:164603 }
4604 else
4605 {
Howard Hinnantce48a112011-06-30 21:18:194606 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnant3e519522010-05-11 19:42:164607 __d.__incr((value_type*)0);
4608 ++__first2;
Howard Hinnantce48a112011-06-30 21:18:194609 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnant3e519522010-05-11 19:42:164610 }
4611 __h2.release();
4612 return;
4613 }
4614 if (__len <= 8)
4615 {
4616 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4617 return;
4618 }
4619 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4620 _RandomAccessIterator __m = __first1 + __l2;
4621 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4622 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4623 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4624}
4625
4626template <class _Tp>
4627struct __stable_sort_switch
4628{
Howard Hinnantca740482010-11-19 22:17:284629 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
Howard Hinnant3e519522010-05-11 19:42:164630};
4631
4632template <class _Compare, class _RandomAccessIterator>
4633void
4634__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4635 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4636 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4637{
4638 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4639 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4640 switch (__len)
4641 {
4642 case 0:
4643 case 1:
4644 return;
4645 case 2:
4646 if (__comp(*--__last, *__first))
4647 swap(*__first, *__last);
4648 return;
4649 }
4650 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4651 {
4652 __insertion_sort<_Compare>(__first, __last, __comp);
4653 return;
4654 }
4655 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4656 _RandomAccessIterator __m = __first + __l2;
4657 if (__len <= __buff_size)
4658 {
4659 __destruct_n __d(0);
4660 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4661 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4662 __d.__set(__l2, (value_type*)0);
4663 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4664 __d.__set(__len, (value_type*)0);
4665 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4666// __merge<_Compare>(move_iterator<value_type*>(__buff),
4667// move_iterator<value_type*>(__buff + __l2),
4668// move_iterator<_RandomAccessIterator>(__buff + __l2),
4669// move_iterator<_RandomAccessIterator>(__buff + __len),
4670// __first, __comp);
4671 return;
4672 }
4673 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4674 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4675 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4676}
4677
4678template <class _RandomAccessIterator, class _Compare>
4679inline _LIBCPP_INLINE_VISIBILITY
4680void
4681stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4682{
4683 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4684 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4685 difference_type __len = __last - __first;
4686 pair<value_type*, ptrdiff_t> __buf(0, 0);
4687 unique_ptr<value_type, __return_temporary_buffer> __h;
4688 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4689 {
Howard Hinnantce48a112011-06-30 21:18:194690 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnant3e519522010-05-11 19:42:164691 __h.reset(__buf.first);
4692 }
Howard Hinnant145afa12013-08-23 20:10:184693#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164694 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4695 __debug_less<_Compare> __c(__comp);
4696 __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
Howard Hinnant145afa12013-08-23 20:10:184697#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164698 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4699 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
Howard Hinnant145afa12013-08-23 20:10:184700#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164701}
4702
4703template <class _RandomAccessIterator>
4704inline _LIBCPP_INLINE_VISIBILITY
4705void
4706stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4707{
Howard Hinnantce48a112011-06-30 21:18:194708 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164709}
4710
4711// is_heap_until
4712
4713template <class _RandomAccessIterator, class _Compare>
Marshall Clow49c76432018-01-15 16:16:324714_LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
Howard Hinnant3e519522010-05-11 19:42:164715is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4716{
Howard Hinnantce48a112011-06-30 21:18:194717 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnant3e519522010-05-11 19:42:164718 difference_type __len = __last - __first;
4719 difference_type __p = 0;
4720 difference_type __c = 1;
4721 _RandomAccessIterator __pp = __first;
4722 while (__c < __len)
4723 {
4724 _RandomAccessIterator __cp = __first + __c;
4725 if (__comp(*__pp, *__cp))
4726 return __cp;
4727 ++__c;
4728 ++__cp;
4729 if (__c == __len)
4730 return __last;
4731 if (__comp(*__pp, *__cp))
4732 return __cp;
4733 ++__p;
4734 ++__pp;
4735 __c = 2 * __p + 1;
4736 }
4737 return __last;
4738}
4739
Howard Hinnantb3371f62010-08-22 00:02:434740template<class _RandomAccessIterator>
Marshall Clow49c76432018-01-15 16:16:324741inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164742_RandomAccessIterator
4743is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4744{
Howard Hinnantce48a112011-06-30 21:18:194745 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164746}
4747
4748// is_heap
4749
4750template <class _RandomAccessIterator, class _Compare>
Marshall Clow49c76432018-01-15 16:16:324751inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164752bool
4753is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4754{
Howard Hinnantce48a112011-06-30 21:18:194755 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
Howard Hinnant3e519522010-05-11 19:42:164756}
4757
Howard Hinnantb3371f62010-08-22 00:02:434758template<class _RandomAccessIterator>
Marshall Clow49c76432018-01-15 16:16:324759inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164760bool
4761is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4762{
Howard Hinnantce48a112011-06-30 21:18:194763 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164764}
4765
4766// push_heap
4767
4768template <class _Compare, class _RandomAccessIterator>
4769void
David Majnemer8b512602014-07-22 06:07:094770__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4771 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
Howard Hinnant3e519522010-05-11 19:42:164772{
Howard Hinnant3e519522010-05-11 19:42:164773 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4774 if (__len > 1)
4775 {
4776 __len = (__len - 2) / 2;
4777 _RandomAccessIterator __ptr = __first + __len;
4778 if (__comp(*__ptr, *--__last))
4779 {
Howard Hinnantce48a112011-06-30 21:18:194780 value_type __t(_VSTD::move(*__last));
Howard Hinnant3e519522010-05-11 19:42:164781 do
4782 {
Howard Hinnantce48a112011-06-30 21:18:194783 *__last = _VSTD::move(*__ptr);
Howard Hinnant3e519522010-05-11 19:42:164784 __last = __ptr;
4785 if (__len == 0)
4786 break;
4787 __len = (__len - 1) / 2;
4788 __ptr = __first + __len;
4789 } while (__comp(*__ptr, __t));
Howard Hinnantce48a112011-06-30 21:18:194790 *__last = _VSTD::move(__t);
Howard Hinnant3e519522010-05-11 19:42:164791 }
4792 }
4793}
4794
4795template <class _RandomAccessIterator, class _Compare>
4796inline _LIBCPP_INLINE_VISIBILITY
4797void
4798push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4799{
Howard Hinnant145afa12013-08-23 20:10:184800#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164801 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4802 __debug_less<_Compare> __c(__comp);
David Majnemer8b512602014-07-22 06:07:094803 __sift_up<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant145afa12013-08-23 20:10:184804#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164805 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
David Majnemer8b512602014-07-22 06:07:094806 __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant145afa12013-08-23 20:10:184807#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164808}
4809
4810template <class _RandomAccessIterator>
4811inline _LIBCPP_INLINE_VISIBILITY
4812void
4813push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4814{
Howard Hinnantce48a112011-06-30 21:18:194815 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164816}
4817
4818// pop_heap
4819
4820template <class _Compare, class _RandomAccessIterator>
David Majnemer8b512602014-07-22 06:07:094821void
Eric Fiselierfd838222016-12-23 23:37:524822__sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
4823 _Compare __comp,
David Majnemer8b512602014-07-22 06:07:094824 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4825 _RandomAccessIterator __start)
4826{
4827 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4828 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4829 // left-child of __start is at 2 * __start + 1
4830 // right-child of __start is at 2 * __start + 2
4831 difference_type __child = __start - __first;
4832
4833 if (__len < 2 || (__len - 2) / 2 < __child)
4834 return;
4835
4836 __child = 2 * __child + 1;
4837 _RandomAccessIterator __child_i = __first + __child;
4838
4839 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4840 // right-child exists and is greater than left-child
4841 ++__child_i;
4842 ++__child;
4843 }
4844
4845 // check if we are in heap-order
4846 if (__comp(*__child_i, *__start))
4847 // we are, __start is larger than it's largest child
4848 return;
4849
4850 value_type __top(_VSTD::move(*__start));
4851 do
4852 {
4853 // we are not in heap-order, swap the parent with it's largest child
4854 *__start = _VSTD::move(*__child_i);
4855 __start = __child_i;
4856
4857 if ((__len - 2) / 2 < __child)
4858 break;
4859
4860 // recompute the child based off of the updated parent
4861 __child = 2 * __child + 1;
4862 __child_i = __first + __child;
4863
4864 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4865 // right-child exists and is greater than left-child
4866 ++__child_i;
4867 ++__child;
4868 }
4869
4870 // check if we are in heap-order
4871 } while (!__comp(*__child_i, __top));
4872 *__start = _VSTD::move(__top);
4873}
4874
4875template <class _Compare, class _RandomAccessIterator>
Howard Hinnant3e519522010-05-11 19:42:164876inline _LIBCPP_INLINE_VISIBILITY
4877void
4878__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4879 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4880{
4881 if (__len > 1)
4882 {
4883 swap(*__first, *--__last);
David Majnemer8b512602014-07-22 06:07:094884 __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
Howard Hinnant3e519522010-05-11 19:42:164885 }
4886}
4887
4888template <class _RandomAccessIterator, class _Compare>
4889inline _LIBCPP_INLINE_VISIBILITY
4890void
4891pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4892{
Howard Hinnant145afa12013-08-23 20:10:184893#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164894 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4895 __debug_less<_Compare> __c(__comp);
4896 __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant145afa12013-08-23 20:10:184897#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164898 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4899 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant145afa12013-08-23 20:10:184900#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164901}
4902
4903template <class _RandomAccessIterator>
4904inline _LIBCPP_INLINE_VISIBILITY
4905void
4906pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4907{
Howard Hinnantce48a112011-06-30 21:18:194908 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164909}
4910
4911// make_heap
4912
4913template <class _Compare, class _RandomAccessIterator>
4914void
4915__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4916{
4917 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4918 difference_type __n = __last - __first;
4919 if (__n > 1)
4920 {
David Majnemer8b512602014-07-22 06:07:094921 // start from the first parent, there is no need to consider children
4922 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
4923 {
4924 __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
4925 }
Howard Hinnant3e519522010-05-11 19:42:164926 }
4927}
4928
4929template <class _RandomAccessIterator, class _Compare>
4930inline _LIBCPP_INLINE_VISIBILITY
4931void
4932make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4933{
Howard Hinnant145afa12013-08-23 20:10:184934#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164935 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4936 __debug_less<_Compare> __c(__comp);
4937 __make_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant145afa12013-08-23 20:10:184938#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164939 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4940 __make_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant145afa12013-08-23 20:10:184941#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164942}
4943
4944template <class _RandomAccessIterator>
4945inline _LIBCPP_INLINE_VISIBILITY
4946void
4947make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4948{
Howard Hinnantce48a112011-06-30 21:18:194949 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164950}
4951
4952// sort_heap
4953
4954template <class _Compare, class _RandomAccessIterator>
4955void
4956__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4957{
4958 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4959 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
4960 __pop_heap<_Compare>(__first, __last, __comp, __n);
4961}
4962
4963template <class _RandomAccessIterator, class _Compare>
4964inline _LIBCPP_INLINE_VISIBILITY
4965void
4966sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4967{
Howard Hinnant145afa12013-08-23 20:10:184968#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164969 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4970 __debug_less<_Compare> __c(__comp);
4971 __sort_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant145afa12013-08-23 20:10:184972#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164973 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4974 __sort_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant145afa12013-08-23 20:10:184975#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164976}
4977
4978template <class _RandomAccessIterator>
4979inline _LIBCPP_INLINE_VISIBILITY
4980void
4981sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4982{
Howard Hinnantce48a112011-06-30 21:18:194983 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164984}
4985
4986// partial_sort
4987
4988template <class _Compare, class _RandomAccessIterator>
4989void
4990__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
4991 _Compare __comp)
4992{
4993 __make_heap<_Compare>(__first, __middle, __comp);
4994 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
4995 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
4996 {
4997 if (__comp(*__i, *__first))
4998 {
4999 swap(*__i, *__first);
David Majnemer8b512602014-07-22 06:07:095000 __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
Howard Hinnant3e519522010-05-11 19:42:165001 }
5002 }
5003 __sort_heap<_Compare>(__first, __middle, __comp);
5004}
5005
5006template <class _RandomAccessIterator, class _Compare>
5007inline _LIBCPP_INLINE_VISIBILITY
5008void
5009partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5010 _Compare __comp)
5011{
Howard Hinnant145afa12013-08-23 20:10:185012#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165013 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5014 __debug_less<_Compare> __c(__comp);
5015 __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
Howard Hinnant145afa12013-08-23 20:10:185016#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165017 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5018 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
Howard Hinnant145afa12013-08-23 20:10:185019#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165020}
5021
5022template <class _RandomAccessIterator>
5023inline _LIBCPP_INLINE_VISIBILITY
5024void
5025partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5026{
Howard Hinnantce48a112011-06-30 21:18:195027 _VSTD::partial_sort(__first, __middle, __last,
Howard Hinnant3e519522010-05-11 19:42:165028 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5029}
5030
5031// partial_sort_copy
5032
5033template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5034_RandomAccessIterator
5035__partial_sort_copy(_InputIterator __first, _InputIterator __last,
5036 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5037{
5038 _RandomAccessIterator __r = __result_first;
5039 if (__r != __result_last)
5040 {
Eric Fiselier910285b2014-10-27 19:28:205041 for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
Howard Hinnant3e519522010-05-11 19:42:165042 *__r = *__first;
5043 __make_heap<_Compare>(__result_first, __r, __comp);
David Majnemer8b512602014-07-22 06:07:095044 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
Howard Hinnant3e519522010-05-11 19:42:165045 for (; __first != __last; ++__first)
5046 if (__comp(*__first, *__result_first))
5047 {
5048 *__result_first = *__first;
David Majnemer8b512602014-07-22 06:07:095049 __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
Howard Hinnant3e519522010-05-11 19:42:165050 }
5051 __sort_heap<_Compare>(__result_first, __r, __comp);
5052 }
5053 return __r;
5054}
5055
5056template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5057inline _LIBCPP_INLINE_VISIBILITY
5058_RandomAccessIterator
5059partial_sort_copy(_InputIterator __first, _InputIterator __last,
5060 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5061{
Howard Hinnant145afa12013-08-23 20:10:185062#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165063 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5064 __debug_less<_Compare> __c(__comp);
5065 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
Howard Hinnant145afa12013-08-23 20:10:185066#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165067 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5068 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
Howard Hinnant145afa12013-08-23 20:10:185069#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165070}
5071
5072template <class _InputIterator, class _RandomAccessIterator>
5073inline _LIBCPP_INLINE_VISIBILITY
5074_RandomAccessIterator
5075partial_sort_copy(_InputIterator __first, _InputIterator __last,
5076 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5077{
Howard Hinnantce48a112011-06-30 21:18:195078 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
Howard Hinnant3e519522010-05-11 19:42:165079 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5080}
5081
5082// nth_element
5083
5084template <class _Compare, class _RandomAccessIterator>
5085void
5086__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5087{
5088 // _Compare is known to be a reference type
5089 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5090 const difference_type __limit = 7;
5091 while (true)
5092 {
5093 __restart:
Howard Hinnantb34b48192011-12-29 17:45:355094 if (__nth == __last)
5095 return;
Howard Hinnant3e519522010-05-11 19:42:165096 difference_type __len = __last - __first;
5097 switch (__len)
5098 {
5099 case 0:
5100 case 1:
5101 return;
5102 case 2:
5103 if (__comp(*--__last, *__first))
5104 swap(*__first, *__last);
5105 return;
5106 case 3:
5107 {
5108 _RandomAccessIterator __m = __first;
Howard Hinnantce48a112011-06-30 21:18:195109 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165110 return;
5111 }
5112 }
5113 if (__len <= __limit)
5114 {
5115 __selection_sort<_Compare>(__first, __last, __comp);
5116 return;
5117 }
5118 // __len > __limit >= 3
5119 _RandomAccessIterator __m = __first + __len/2;
5120 _RandomAccessIterator __lm1 = __last;
Howard Hinnantce48a112011-06-30 21:18:195121 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
Howard Hinnant3e519522010-05-11 19:42:165122 // *__m is median
5123 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5124 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5125 _RandomAccessIterator __i = __first;
5126 _RandomAccessIterator __j = __lm1;
5127 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5128 // The search going up is known to be guarded but the search coming down isn't.
5129 // Prime the downward search with a guard.
5130 if (!__comp(*__i, *__m)) // if *__first == *__m
5131 {
5132 // *__first == *__m, *__first doesn't go in first part
5133 // manually guard downward moving __j against __i
5134 while (true)
5135 {
5136 if (__i == --__j)
5137 {
5138 // *__first == *__m, *__m <= all other elements
5139 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5140 ++__i; // __first + 1
5141 __j = __last;
5142 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5143 {
5144 while (true)
5145 {
5146 if (__i == __j)
5147 return; // [__first, __last) all equivalent elements
5148 if (__comp(*__first, *__i))
5149 {
5150 swap(*__i, *__j);
5151 ++__n_swaps;
5152 ++__i;
5153 break;
5154 }
5155 ++__i;
5156 }
5157 }
5158 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5159 if (__i == __j)
5160 return;
5161 while (true)
5162 {
5163 while (!__comp(*__first, *__i))
5164 ++__i;
5165 while (__comp(*__first, *--__j))
5166 ;
5167 if (__i >= __j)
5168 break;
5169 swap(*__i, *__j);
5170 ++__n_swaps;
5171 ++__i;
5172 }
5173 // [__first, __i) == *__first and *__first < [__i, __last)
5174 // The first part is sorted,
5175 if (__nth < __i)
5176 return;
5177 // __nth_element the secod part
5178 // __nth_element<_Compare>(__i, __nth, __last, __comp);
5179 __first = __i;
5180 goto __restart;
5181 }
5182 if (__comp(*__j, *__m))
5183 {
5184 swap(*__i, *__j);
5185 ++__n_swaps;
5186 break; // found guard for downward moving __j, now use unguarded partition
5187 }
5188 }
5189 }
5190 ++__i;
5191 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5192 // if not yet partitioned...
5193 if (__i < __j)
5194 {
5195 // known that *(__i - 1) < *__m
5196 while (true)
5197 {
5198 // __m still guards upward moving __i
5199 while (__comp(*__i, *__m))
5200 ++__i;
5201 // It is now known that a guard exists for downward moving __j
5202 while (!__comp(*--__j, *__m))
5203 ;
5204 if (__i >= __j)
5205 break;
5206 swap(*__i, *__j);
5207 ++__n_swaps;
5208 // It is known that __m != __j
5209 // If __m just moved, follow it
5210 if (__m == __i)
5211 __m = __j;
5212 ++__i;
5213 }
5214 }
5215 // [__first, __i) < *__m and *__m <= [__i, __last)
5216 if (__i != __m && __comp(*__m, *__i))
5217 {
5218 swap(*__i, *__m);
5219 ++__n_swaps;
5220 }
5221 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5222 if (__nth == __i)
5223 return;
5224 if (__n_swaps == 0)
5225 {
5226 // We were given a perfectly partitioned sequence. Coincidence?
5227 if (__nth < __i)
5228 {
5229 // Check for [__first, __i) already sorted
5230 __j = __m = __first;
5231 while (++__j != __i)
5232 {
5233 if (__comp(*__j, *__m))
5234 // not yet sorted, so sort
5235 goto not_sorted;
5236 __m = __j;
5237 }
5238 // [__first, __i) sorted
5239 return;
5240 }
5241 else
5242 {
5243 // Check for [__i, __last) already sorted
5244 __j = __m = __i;
5245 while (++__j != __last)
5246 {
5247 if (__comp(*__j, *__m))
5248 // not yet sorted, so sort
5249 goto not_sorted;
5250 __m = __j;
5251 }
5252 // [__i, __last) sorted
5253 return;
5254 }
5255 }
5256not_sorted:
5257 // __nth_element on range containing __nth
5258 if (__nth < __i)
5259 {
5260 // __nth_element<_Compare>(__first, __nth, __i, __comp);
5261 __last = __i;
5262 }
5263 else
5264 {
5265 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5266 __first = ++__i;
5267 }
5268 }
5269}
5270
5271template <class _RandomAccessIterator, class _Compare>
5272inline _LIBCPP_INLINE_VISIBILITY
5273void
5274nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5275{
Howard Hinnant145afa12013-08-23 20:10:185276#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165277 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5278 __debug_less<_Compare> __c(__comp);
5279 __nth_element<_Comp_ref>(__first, __nth, __last, __c);
Howard Hinnant145afa12013-08-23 20:10:185280#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165281 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5282 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
Howard Hinnant145afa12013-08-23 20:10:185283#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165284}
5285
5286template <class _RandomAccessIterator>
5287inline _LIBCPP_INLINE_VISIBILITY
5288void
5289nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5290{
Howard Hinnantce48a112011-06-30 21:18:195291 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:165292}
5293
5294// includes
5295
5296template <class _Compare, class _InputIterator1, class _InputIterator2>
Marshall Clow8da1a482018-01-22 23:10:405297_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnant3e519522010-05-11 19:42:165298__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5299 _Compare __comp)
5300{
5301 for (; __first2 != __last2; ++__first1)
5302 {
5303 if (__first1 == __last1 || __comp(*__first2, *__first1))
5304 return false;
5305 if (!__comp(*__first1, *__first2))
5306 ++__first2;
5307 }
5308 return true;
5309}
5310
5311template <class _InputIterator1, class _InputIterator2, class _Compare>
Marshall Clow8da1a482018-01-22 23:10:405312inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165313bool
5314includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5315 _Compare __comp)
5316{
Howard Hinnant145afa12013-08-23 20:10:185317#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165318 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5319 __debug_less<_Compare> __c(__comp);
5320 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant145afa12013-08-23 20:10:185321#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165322 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5323 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant145afa12013-08-23 20:10:185324#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165325}
5326
5327template <class _InputIterator1, class _InputIterator2>
Marshall Clow8da1a482018-01-22 23:10:405328inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165329bool
5330includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5331{
Howard Hinnantce48a112011-06-30 21:18:195332 return _VSTD::includes(__first1, __last1, __first2, __last2,
Howard Hinnant3e519522010-05-11 19:42:165333 __less<typename iterator_traits<_InputIterator1>::value_type,
5334 typename iterator_traits<_InputIterator2>::value_type>());
5335}
5336
5337// set_union
5338
5339template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5340_OutputIterator
5341__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5342 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5343{
5344 for (; __first1 != __last1; ++__result)
5345 {
5346 if (__first2 == __last2)
Howard Hinnantce48a112011-06-30 21:18:195347 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnant3e519522010-05-11 19:42:165348 if (__comp(*__first2, *__first1))
5349 {
5350 *__result = *__first2;
5351 ++__first2;
5352 }
5353 else
5354 {
Howard Hinnant3e519522010-05-11 19:42:165355 if (!__comp(*__first1, *__first2))
5356 ++__first2;
Marshall Clow05da5b02017-10-30 15:50:005357 *__result = *__first1;
Howard Hinnant3e519522010-05-11 19:42:165358 ++__first1;
5359 }
5360 }
Howard Hinnantce48a112011-06-30 21:18:195361 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnant3e519522010-05-11 19:42:165362}
5363
5364template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5365inline _LIBCPP_INLINE_VISIBILITY
5366_OutputIterator
5367set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5368 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5369{
Howard Hinnant145afa12013-08-23 20:10:185370#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165371 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5372 __debug_less<_Compare> __c(__comp);
5373 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant145afa12013-08-23 20:10:185374#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165375 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5376 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant145afa12013-08-23 20:10:185377#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165378}
5379
5380template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5381inline _LIBCPP_INLINE_VISIBILITY
5382_OutputIterator
5383set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5384 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5385{
Howard Hinnantce48a112011-06-30 21:18:195386 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
Howard Hinnant3e519522010-05-11 19:42:165387 __less<typename iterator_traits<_InputIterator1>::value_type,
5388 typename iterator_traits<_InputIterator2>::value_type>());
5389}
5390
5391// set_intersection
5392
5393template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
Marshall Clow8da1a482018-01-22 23:10:405394_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnant3e519522010-05-11 19:42:165395__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5396 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5397{
5398 while (__first1 != __last1 && __first2 != __last2)
5399 {
5400 if (__comp(*__first1, *__first2))
5401 ++__first1;
5402 else
5403 {
5404 if (!__comp(*__first2, *__first1))
5405 {
5406 *__result = *__first1;
5407 ++__result;
5408 ++__first1;
5409 }
5410 ++__first2;
5411 }
5412 }
5413 return __result;
5414}
5415
5416template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
Marshall Clow8da1a482018-01-22 23:10:405417inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165418_OutputIterator
5419set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5420 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5421{
Howard Hinnant145afa12013-08-23 20:10:185422#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165423 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5424 __debug_less<_Compare> __c(__comp);
5425 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant145afa12013-08-23 20:10:185426#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165427 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5428 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant145afa12013-08-23 20:10:185429#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165430}
5431
5432template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
Marshall Clow8da1a482018-01-22 23:10:405433inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165434_OutputIterator
5435set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5436 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5437{
Howard Hinnantce48a112011-06-30 21:18:195438 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
Howard Hinnant3e519522010-05-11 19:42:165439 __less<typename iterator_traits<_InputIterator1>::value_type,
5440 typename iterator_traits<_InputIterator2>::value_type>());
5441}
5442
5443// set_difference
5444
5445template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5446_OutputIterator
5447__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5448 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5449{
5450 while (__first1 != __last1)
5451 {
5452 if (__first2 == __last2)
Howard Hinnantce48a112011-06-30 21:18:195453 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnant3e519522010-05-11 19:42:165454 if (__comp(*__first1, *__first2))
5455 {
5456 *__result = *__first1;
5457 ++__result;
5458 ++__first1;
5459 }
5460 else
5461 {
5462 if (!__comp(*__first2, *__first1))
5463 ++__first1;
5464 ++__first2;
5465 }
5466 }
5467 return __result;
5468}
5469
5470template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5471inline _LIBCPP_INLINE_VISIBILITY
5472_OutputIterator
5473set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5474 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5475{
Howard Hinnant145afa12013-08-23 20:10:185476#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165477 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5478 __debug_less<_Compare> __c(__comp);
5479 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant145afa12013-08-23 20:10:185480#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165481 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5482 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant145afa12013-08-23 20:10:185483#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165484}
5485
5486template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5487inline _LIBCPP_INLINE_VISIBILITY
5488_OutputIterator
5489set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5490 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5491{
Howard Hinnantce48a112011-06-30 21:18:195492 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnant3e519522010-05-11 19:42:165493 __less<typename iterator_traits<_InputIterator1>::value_type,
5494 typename iterator_traits<_InputIterator2>::value_type>());
5495}
5496
5497// set_symmetric_difference
5498
5499template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5500_OutputIterator
5501__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5502 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5503{
5504 while (__first1 != __last1)
5505 {
5506 if (__first2 == __last2)
Howard Hinnantce48a112011-06-30 21:18:195507 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnant3e519522010-05-11 19:42:165508 if (__comp(*__first1, *__first2))
5509 {
5510 *__result = *__first1;
5511 ++__result;
5512 ++__first1;
5513 }
5514 else
5515 {
5516 if (__comp(*__first2, *__first1))
5517 {
5518 *__result = *__first2;
5519 ++__result;
5520 }
5521 else
5522 ++__first1;
5523 ++__first2;
5524 }
5525 }
Howard Hinnantce48a112011-06-30 21:18:195526 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnant3e519522010-05-11 19:42:165527}
5528
5529template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5530inline _LIBCPP_INLINE_VISIBILITY
5531_OutputIterator
5532set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5533 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5534{
Howard Hinnant145afa12013-08-23 20:10:185535#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165536 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5537 __debug_less<_Compare> __c(__comp);
5538 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant145afa12013-08-23 20:10:185539#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165540 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5541 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant145afa12013-08-23 20:10:185542#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165543}
5544
5545template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5546inline _LIBCPP_INLINE_VISIBILITY
5547_OutputIterator
5548set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5549 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5550{
Howard Hinnantce48a112011-06-30 21:18:195551 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnant3e519522010-05-11 19:42:165552 __less<typename iterator_traits<_InputIterator1>::value_type,
5553 typename iterator_traits<_InputIterator2>::value_type>());
5554}
5555
5556// lexicographical_compare
5557
5558template <class _Compare, class _InputIterator1, class _InputIterator2>
Marshall Clow1b9a4ff2018-01-22 20:44:335559_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnant3e519522010-05-11 19:42:165560__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5561 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5562{
Eric Fiselier910285b2014-10-27 19:28:205563 for (; __first2 != __last2; ++__first1, (void) ++__first2)
Howard Hinnant3e519522010-05-11 19:42:165564 {
5565 if (__first1 == __last1 || __comp(*__first1, *__first2))
5566 return true;
5567 if (__comp(*__first2, *__first1))
5568 return false;
5569 }
5570 return false;
5571}
5572
5573template <class _InputIterator1, class _InputIterator2, class _Compare>
Marshall Clow1b9a4ff2018-01-22 20:44:335574inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165575bool
5576lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5577 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5578{
Howard Hinnant145afa12013-08-23 20:10:185579#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165580 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5581 __debug_less<_Compare> __c(__comp);
5582 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant145afa12013-08-23 20:10:185583#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165584 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5585 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant145afa12013-08-23 20:10:185586#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165587}
5588
5589template <class _InputIterator1, class _InputIterator2>
Marshall Clow1b9a4ff2018-01-22 20:44:335590inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165591bool
5592lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5593 _InputIterator2 __first2, _InputIterator2 __last2)
5594{
Howard Hinnantce48a112011-06-30 21:18:195595 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
Howard Hinnant3e519522010-05-11 19:42:165596 __less<typename iterator_traits<_InputIterator1>::value_type,
5597 typename iterator_traits<_InputIterator2>::value_type>());
5598}
5599
5600// next_permutation
5601
5602template <class _Compare, class _BidirectionalIterator>
5603bool
5604__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5605{
5606 _BidirectionalIterator __i = __last;
5607 if (__first == __last || __first == --__i)
5608 return false;
5609 while (true)
5610 {
5611 _BidirectionalIterator __ip1 = __i;
5612 if (__comp(*--__i, *__ip1))
5613 {
5614 _BidirectionalIterator __j = __last;
5615 while (!__comp(*__i, *--__j))
5616 ;
5617 swap(*__i, *__j);
Howard Hinnantce48a112011-06-30 21:18:195618 _VSTD::reverse(__ip1, __last);
Howard Hinnant3e519522010-05-11 19:42:165619 return true;
5620 }
5621 if (__i == __first)
5622 {
Howard Hinnantce48a112011-06-30 21:18:195623 _VSTD::reverse(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:165624 return false;
5625 }
5626 }
5627}
5628
5629template <class _BidirectionalIterator, class _Compare>
5630inline _LIBCPP_INLINE_VISIBILITY
5631bool
5632next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5633{
Howard Hinnant145afa12013-08-23 20:10:185634#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165635 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5636 __debug_less<_Compare> __c(__comp);
5637 return __next_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant145afa12013-08-23 20:10:185638#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165639 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5640 return __next_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant145afa12013-08-23 20:10:185641#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165642}
5643
5644template <class _BidirectionalIterator>
5645inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb3371f62010-08-22 00:02:435646bool
Howard Hinnant3e519522010-05-11 19:42:165647next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5648{
Howard Hinnantce48a112011-06-30 21:18:195649 return _VSTD::next_permutation(__first, __last,
Howard Hinnant3e519522010-05-11 19:42:165650 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5651}
5652
5653// prev_permutation
5654
5655template <class _Compare, class _BidirectionalIterator>
5656bool
5657__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5658{
5659 _BidirectionalIterator __i = __last;
5660 if (__first == __last || __first == --__i)
5661 return false;
5662 while (true)
5663 {
5664 _BidirectionalIterator __ip1 = __i;
5665 if (__comp(*__ip1, *--__i))
5666 {
5667 _BidirectionalIterator __j = __last;
5668 while (!__comp(*--__j, *__i))
5669 ;
5670 swap(*__i, *__j);
Howard Hinnantce48a112011-06-30 21:18:195671 _VSTD::reverse(__ip1, __last);
Howard Hinnant3e519522010-05-11 19:42:165672 return true;
5673 }
5674 if (__i == __first)
5675 {
Howard Hinnantce48a112011-06-30 21:18:195676 _VSTD::reverse(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:165677 return false;
5678 }
5679 }
5680}
5681
5682template <class _BidirectionalIterator, class _Compare>
5683inline _LIBCPP_INLINE_VISIBILITY
5684bool
5685prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5686{
Howard Hinnant145afa12013-08-23 20:10:185687#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165688 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5689 __debug_less<_Compare> __c(__comp);
5690 return __prev_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant145afa12013-08-23 20:10:185691#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165692 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5693 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant145afa12013-08-23 20:10:185694#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165695}
5696
5697template <class _BidirectionalIterator>
5698inline _LIBCPP_INLINE_VISIBILITY
5699bool
5700prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5701{
Howard Hinnantce48a112011-06-30 21:18:195702 return _VSTD::prev_permutation(__first, __last,
Howard Hinnant3e519522010-05-11 19:42:165703 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5704}
5705
Howard Hinnant3e519522010-05-11 19:42:165706_LIBCPP_END_NAMESPACE_STD
5707
Eric Fiseliera016efb2017-05-31 22:07:495708_LIBCPP_POP_MACROS
5709
Howard Hinnant3e519522010-05-11 19:42:165710#endif // _LIBCPP_ALGORITHM