blob: 55e1f9fc113ad9a766c0cb1071933d6b8ff12a4b [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>
Howard Hinnant5d1a7012013-08-14 18:00:20649
Eric Fiselierc1bd9192014-08-10 23:53:08650#include <__debug>
651
Howard Hinnant073458b2011-10-17 20:05:10652#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16653#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10654#endif
Howard Hinnant3e519522010-05-11 19:42:16655
Eric Fiseliera016efb2017-05-31 22:07:49656_LIBCPP_PUSH_MACROS
657#include <__undef_macros>
658
659
Howard Hinnant3e519522010-05-11 19:42:16660_LIBCPP_BEGIN_NAMESPACE_STD
661
Marshall Clow9d67c6d2014-02-19 16:51:35662// I'd like to replace these with _VSTD::equal_to<void>, but can't because:
663// * That only works with C++14 and later, and
664// * We haven't included <functional> here.
Howard Hinnant3e519522010-05-11 19:42:16665template <class _T1, class _T2 = _T1>
666struct __equal_to
667{
Marshall Clowd8098f92018-07-14 04:15:19668 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
669 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
670 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
671 _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:16672};
673
674template <class _T1>
675struct __equal_to<_T1, _T1>
676{
Marshall Clow9d67c6d2014-02-19 16:51:35677 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
678 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnant3e519522010-05-11 19:42:16679};
680
681template <class _T1>
682struct __equal_to<const _T1, _T1>
683{
Marshall Clow9d67c6d2014-02-19 16:51:35684 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
685 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnant3e519522010-05-11 19:42:16686};
687
688template <class _T1>
689struct __equal_to<_T1, const _T1>
690{
Marshall Clow9d67c6d2014-02-19 16:51:35691 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
692 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnant3e519522010-05-11 19:42:16693};
694
695template <class _T1, class _T2 = _T1>
696struct __less
697{
Aditya Kumar331fb802016-08-25 11:52:38698 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow9d67c6d2014-02-19 16:51:35699 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
700
701 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
702 bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
703
704 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
705 bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
706
707 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
708 bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
Howard Hinnant3e519522010-05-11 19:42:16709};
710
711template <class _T1>
712struct __less<_T1, _T1>
713{
Marshall Clow9d67c6d2014-02-19 16:51:35714 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
715 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnant3e519522010-05-11 19:42:16716};
717
718template <class _T1>
719struct __less<const _T1, _T1>
720{
Marshall Clow9d67c6d2014-02-19 16:51:35721 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
722 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnant3e519522010-05-11 19:42:16723};
724
725template <class _T1>
726struct __less<_T1, const _T1>
727{
Marshall Clow9d67c6d2014-02-19 16:51:35728 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
729 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnant3e519522010-05-11 19:42:16730};
731
732template <class _Predicate>
Marshall Clowa763b362017-08-28 23:16:13733class __invert // invert the sense of a comparison
Howard Hinnant3e519522010-05-11 19:42:16734{
735private:
736 _Predicate __p_;
737public:
Marshall Clowa763b362017-08-28 23:16:13738 _LIBCPP_INLINE_VISIBILITY __invert() {}
Howard Hinnant3e519522010-05-11 19:42:16739
740 _LIBCPP_INLINE_VISIBILITY
Marshall Clowa763b362017-08-28 23:16:13741 explicit __invert(_Predicate __p) : __p_(__p) {}
Howard Hinnant3e519522010-05-11 19:42:16742
743 template <class _T1>
744 _LIBCPP_INLINE_VISIBILITY
745 bool operator()(const _T1& __x) {return !__p_(__x);}
746
747 template <class _T1, class _T2>
748 _LIBCPP_INLINE_VISIBILITY
Marshall Clowa763b362017-08-28 23:16:13749 bool operator()(const _T1& __x, const _T2& __y) {return __p_(__y, __x);}
Howard Hinnant3e519522010-05-11 19:42:16750};
751
Howard Hinnant145afa12013-08-23 20:10:18752#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:16753
754template <class _Compare>
755struct __debug_less
756{
757 _Compare __comp_;
758 __debug_less(_Compare& __c) : __comp_(__c) {}
Eric Fiselier331d2152016-07-19 23:27:18759
Howard Hinnant3e519522010-05-11 19:42:16760 template <class _Tp, class _Up>
761 bool operator()(const _Tp& __x, const _Up& __y)
762 {
763 bool __r = __comp_(__x, __y);
764 if (__r)
Eric Fiselier331d2152016-07-19 23:27:18765 __do_compare_assert(0, __y, __x);
Howard Hinnant3e519522010-05-11 19:42:16766 return __r;
767 }
Eric Fiselier331d2152016-07-19 23:27:18768
769 template <class _LHS, class _RHS>
770 inline _LIBCPP_INLINE_VISIBILITY
771 decltype((void)_VSTD::declval<_Compare&>()(
772 _VSTD::declval<_LHS const&>(), _VSTD::declval<_RHS const&>()))
773 __do_compare_assert(int, _LHS const& __l, _RHS const& __r) {
774 _LIBCPP_ASSERT(!__comp_(__l, __r),
775 "Comparator does not induce a strict weak ordering");
776 }
777
778 template <class _LHS, class _RHS>
779 inline _LIBCPP_INLINE_VISIBILITY
780 void __do_compare_assert(long, _LHS const&, _RHS const&) {}
Howard Hinnant3e519522010-05-11 19:42:16781};
782
Howard Hinnant145afa12013-08-23 20:10:18783#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:16784
785// all_of
786
787template <class _InputIterator, class _Predicate>
Marshall Clow706ffef2018-01-15 17:20:36788inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16789bool
790all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
791{
792 for (; __first != __last; ++__first)
793 if (!__pred(*__first))
794 return false;
795 return true;
796}
797
798// any_of
799
800template <class _InputIterator, class _Predicate>
Marshall Clow706ffef2018-01-15 17:20:36801inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16802bool
803any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
804{
805 for (; __first != __last; ++__first)
806 if (__pred(*__first))
807 return true;
808 return false;
809}
810
811// none_of
812
813template <class _InputIterator, class _Predicate>
Marshall Clow706ffef2018-01-15 17:20:36814inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16815bool
816none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
817{
818 for (; __first != __last; ++__first)
819 if (__pred(*__first))
820 return false;
821 return true;
822}
823
824// for_each
825
826template <class _InputIterator, class _Function>
Marshall Clow1b9a4ff2018-01-22 20:44:33827inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16828_Function
829for_each(_InputIterator __first, _InputIterator __last, _Function __f)
830{
831 for (; __first != __last; ++__first)
832 __f(*__first);
Marshall Clow1c7fe122016-11-14 18:22:19833 return __f;
Howard Hinnant3e519522010-05-11 19:42:16834}
835
Marshall Clow1d029962017-05-25 13:40:57836#if _LIBCPP_STD_VER > 14
Marshall Clowd5c65ff2017-05-25 02:29:54837// for_each_n
838
839template <class _InputIterator, class _Size, class _Function>
Marshall Clow1b9a4ff2018-01-22 20:44:33840inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowd5c65ff2017-05-25 02:29:54841_InputIterator
842for_each_n(_InputIterator __first, _Size __orig_n, _Function __f)
843{
844 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
845 _IntegralSize __n = __orig_n;
846 while (__n > 0)
847 {
848 __f(*__first);
849 ++__first;
850 --__n;
851 }
852 return __first;
853}
Marshall Clow1d029962017-05-25 13:40:57854#endif
Marshall Clowd5c65ff2017-05-25 02:29:54855
Howard Hinnant3e519522010-05-11 19:42:16856// find
857
858template <class _InputIterator, class _Tp>
Marshall Clow49c76432018-01-15 16:16:32859inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16860_InputIterator
Howard Hinnante4383372011-10-22 20:59:45861find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:16862{
863 for (; __first != __last; ++__first)
Howard Hinnante4383372011-10-22 20:59:45864 if (*__first == __value_)
Howard Hinnant3e519522010-05-11 19:42:16865 break;
866 return __first;
867}
868
869// find_if
870
871template <class _InputIterator, class _Predicate>
Marshall Clow49c76432018-01-15 16:16:32872inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16873_InputIterator
874find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
875{
876 for (; __first != __last; ++__first)
877 if (__pred(*__first))
878 break;
879 return __first;
880}
881
882// find_if_not
883
884template<class _InputIterator, class _Predicate>
Marshall Clow86944282018-01-15 19:26:05885inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16886_InputIterator
887find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
888{
889 for (; __first != __last; ++__first)
890 if (!__pred(*__first))
891 break;
892 return __first;
893}
894
895// find_end
896
897template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow86944282018-01-15 19:26:05898_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1
Howard Hinnant3e519522010-05-11 19:42:16899__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
900 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
901 forward_iterator_tag, forward_iterator_tag)
902{
903 // modeled after search algorithm
904 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
905 if (__first2 == __last2)
906 return __r;
907 while (true)
908 {
909 while (true)
910 {
911 if (__first1 == __last1) // if source exhausted return last correct answer
912 return __r; // (or __last1 if never found)
913 if (__pred(*__first1, *__first2))
914 break;
915 ++__first1;
916 }
917 // *__first1 matches *__first2, now match elements after here
918 _ForwardIterator1 __m1 = __first1;
919 _ForwardIterator2 __m2 = __first2;
920 while (true)
921 {
922 if (++__m2 == __last2)
923 { // Pattern exhaused, record answer and search for another one
924 __r = __first1;
925 ++__first1;
926 break;
927 }
928 if (++__m1 == __last1) // Source exhausted, return last answer
929 return __r;
930 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
931 {
932 ++__first1;
933 break;
934 } // else there is a match, check next elements
935 }
936 }
937}
938
939template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
Marshall Clow86944282018-01-15 19:26:05940_LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator1
Howard Hinnant3e519522010-05-11 19:42:16941__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
942 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
943 bidirectional_iterator_tag, bidirectional_iterator_tag)
944{
945 // modeled after search algorithm (in reverse)
946 if (__first2 == __last2)
947 return __last1; // Everything matches an empty sequence
948 _BidirectionalIterator1 __l1 = __last1;
949 _BidirectionalIterator2 __l2 = __last2;
950 --__l2;
951 while (true)
952 {
953 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
954 while (true)
955 {
956 if (__first1 == __l1) // return __last1 if no element matches *__first2
957 return __last1;
958 if (__pred(*--__l1, *__l2))
959 break;
960 }
961 // *__l1 matches *__l2, now match elements before here
962 _BidirectionalIterator1 __m1 = __l1;
963 _BidirectionalIterator2 __m2 = __l2;
964 while (true)
965 {
966 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
967 return __m1;
968 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
969 return __last1;
970 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
971 {
972 break;
973 } // else there is a match, check next elements
974 }
975 }
976}
977
978template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow9b0af342014-06-10 18:51:55979_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
Howard Hinnant3e519522010-05-11 19:42:16980__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
981 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
982 random_access_iterator_tag, random_access_iterator_tag)
983{
984 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
985 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
986 if (__len2 == 0)
987 return __last1;
988 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
989 if (__len1 < __len2)
990 return __last1;
991 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
992 _RandomAccessIterator1 __l1 = __last1;
993 _RandomAccessIterator2 __l2 = __last2;
994 --__l2;
995 while (true)
996 {
997 while (true)
998 {
999 if (__s == __l1)
1000 return __last1;
1001 if (__pred(*--__l1, *__l2))
1002 break;
1003 }
1004 _RandomAccessIterator1 __m1 = __l1;
1005 _RandomAccessIterator2 __m2 = __l2;
1006 while (true)
1007 {
1008 if (__m2 == __first2)
1009 return __m1;
1010 // no need to check range on __m1 because __s guarantees we have enough source
1011 if (!__pred(*--__m1, *--__m2))
1012 {
1013 break;
1014 }
1015 }
1016 }
1017}
1018
1019template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow86944282018-01-15 19:26:051020inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161021_ForwardIterator1
1022find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1023 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1024{
Howard Hinnantce48a112011-06-30 21:18:191025 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnant3e519522010-05-11 19:42:161026 (__first1, __last1, __first2, __last2, __pred,
1027 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1028 typename iterator_traits<_ForwardIterator2>::iterator_category());
1029}
1030
1031template <class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow86944282018-01-15 19:26:051032inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161033_ForwardIterator1
1034find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1035 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1036{
1037 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1038 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantce48a112011-06-30 21:18:191039 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnant3e519522010-05-11 19:42:161040}
1041
1042// find_first_of
1043
1044template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow9b0af342014-06-10 18:51:551045_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1046__find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Howard Hinnant3e519522010-05-11 19:42:161047 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1048{
1049 for (; __first1 != __last1; ++__first1)
1050 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1051 if (__pred(*__first1, *__j))
1052 return __first1;
1053 return __last1;
1054}
1055
Marshall Clow9b0af342014-06-10 18:51:551056
1057template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow86944282018-01-15 19:26:051058inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow9b0af342014-06-10 18:51:551059_ForwardIterator1
1060find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1061 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1062{
1063 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1064}
1065
Howard Hinnant3e519522010-05-11 19:42:161066template <class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow86944282018-01-15 19:26:051067inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161068_ForwardIterator1
1069find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1070 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1071{
1072 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1073 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Marshall Clow9b0af342014-06-10 18:51:551074 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnant3e519522010-05-11 19:42:161075}
1076
1077// adjacent_find
1078
1079template <class _ForwardIterator, class _BinaryPredicate>
Marshall Clow86944282018-01-15 19:26:051080inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161081_ForwardIterator
1082adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1083{
1084 if (__first != __last)
1085 {
1086 _ForwardIterator __i = __first;
1087 while (++__i != __last)
1088 {
1089 if (__pred(*__first, *__i))
1090 return __first;
1091 __first = __i;
1092 }
1093 }
1094 return __last;
1095}
1096
1097template <class _ForwardIterator>
Marshall Clow86944282018-01-15 19:26:051098inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161099_ForwardIterator
1100adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1101{
1102 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnantce48a112011-06-30 21:18:191103 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
Howard Hinnant3e519522010-05-11 19:42:161104}
1105
1106// count
1107
1108template <class _InputIterator, class _Tp>
Marshall Clow056f15e2018-01-15 19:40:341109inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161110typename iterator_traits<_InputIterator>::difference_type
Howard Hinnante4383372011-10-22 20:59:451111count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:161112{
1113 typename iterator_traits<_InputIterator>::difference_type __r(0);
1114 for (; __first != __last; ++__first)
Howard Hinnante4383372011-10-22 20:59:451115 if (*__first == __value_)
Howard Hinnant3e519522010-05-11 19:42:161116 ++__r;
1117 return __r;
1118}
1119
1120// count_if
1121
1122template <class _InputIterator, class _Predicate>
Marshall Clow056f15e2018-01-15 19:40:341123inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161124typename iterator_traits<_InputIterator>::difference_type
1125count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1126{
1127 typename iterator_traits<_InputIterator>::difference_type __r(0);
1128 for (; __first != __last; ++__first)
1129 if (__pred(*__first))
1130 ++__r;
1131 return __r;
1132}
1133
1134// mismatch
1135
1136template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Marshall Clow6538e28d2018-01-16 02:04:101137inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161138pair<_InputIterator1, _InputIterator2>
1139mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1140 _InputIterator2 __first2, _BinaryPredicate __pred)
1141{
Marshall Clowbd7c7b52014-09-16 20:40:051142 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnant3e519522010-05-11 19:42:161143 if (!__pred(*__first1, *__first2))
1144 break;
1145 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1146}
1147
1148template <class _InputIterator1, class _InputIterator2>
Marshall Clow6538e28d2018-01-16 02:04:101149inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161150pair<_InputIterator1, _InputIterator2>
1151mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1152{
1153 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1154 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnantce48a112011-06-30 21:18:191155 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnant3e519522010-05-11 19:42:161156}
1157
Marshall Clow0b0bbd22013-05-09 21:14:231158#if _LIBCPP_STD_VER > 11
1159template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Marshall Clow6538e28d2018-01-16 02:04:101160inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231161pair<_InputIterator1, _InputIterator2>
1162mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1163 _InputIterator2 __first2, _InputIterator2 __last2,
1164 _BinaryPredicate __pred)
1165{
Marshall Clowbd7c7b52014-09-16 20:40:051166 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow0b0bbd22013-05-09 21:14:231167 if (!__pred(*__first1, *__first2))
1168 break;
1169 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1170}
1171
1172template <class _InputIterator1, class _InputIterator2>
Marshall Clow6538e28d2018-01-16 02:04:101173inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231174pair<_InputIterator1, _InputIterator2>
1175mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1176 _InputIterator2 __first2, _InputIterator2 __last2)
1177{
1178 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1179 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1180 return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1181}
1182#endif
1183
Howard Hinnant3e519522010-05-11 19:42:161184// equal
1185
1186template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Marshall Clow6538e28d2018-01-16 02:04:101187inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161188bool
1189equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1190{
Eric Fiselier910285b2014-10-27 19:28:201191 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnant3e519522010-05-11 19:42:161192 if (!__pred(*__first1, *__first2))
1193 return false;
1194 return true;
1195}
1196
1197template <class _InputIterator1, class _InputIterator2>
Marshall Clow6538e28d2018-01-16 02:04:101198inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161199bool
1200equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1201{
1202 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1203 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnantce48a112011-06-30 21:18:191204 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnant3e519522010-05-11 19:42:161205}
1206
Marshall Clow0b0bbd22013-05-09 21:14:231207#if _LIBCPP_STD_VER > 11
1208template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
Marshall Clow6538e28d2018-01-16 02:04:101209inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231210bool
Aditya Kumar331fb802016-08-25 11:52:381211__equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow0b0bbd22013-05-09 21:14:231212 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1213 input_iterator_tag, input_iterator_tag )
1214{
Eric Fiselier910285b2014-10-27 19:28:201215 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow0b0bbd22013-05-09 21:14:231216 if (!__pred(*__first1, *__first2))
1217 return false;
1218 return __first1 == __last1 && __first2 == __last2;
1219}
1220
1221template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow6538e28d2018-01-16 02:04:101222inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231223bool
Aditya Kumar331fb802016-08-25 11:52:381224__equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1225 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
Marshall Clow0b0bbd22013-05-09 21:14:231226 random_access_iterator_tag, random_access_iterator_tag )
1227{
1228 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1229 return false;
1230 return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1231 typename add_lvalue_reference<_BinaryPredicate>::type>
1232 (__first1, __last1, __first2, __pred );
1233}
1234
1235template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
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:381238equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow0b0bbd22013-05-09 21:14:231239 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1240{
1241 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
Aditya Kumar331fb802016-08-25 11:52:381242 (__first1, __last1, __first2, __last2, __pred,
Marshall Clow0b0bbd22013-05-09 21:14:231243 typename iterator_traits<_InputIterator1>::iterator_category(),
1244 typename iterator_traits<_InputIterator2>::iterator_category());
1245}
1246
1247template <class _InputIterator1, class _InputIterator2>
Marshall Clow6538e28d2018-01-16 02:04:101248inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231249bool
Aditya Kumar331fb802016-08-25 11:52:381250equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow0b0bbd22013-05-09 21:14:231251 _InputIterator2 __first2, _InputIterator2 __last2)
1252{
1253 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1254 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1255 return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1256 typename iterator_traits<_InputIterator1>::iterator_category(),
1257 typename iterator_traits<_InputIterator2>::iterator_category());
1258}
1259#endif
1260
Howard Hinnant3e519522010-05-11 19:42:161261// is_permutation
1262
1263template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow49c76432018-01-15 16:16:321264_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnant3e519522010-05-11 19:42:161265is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1266 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1267{
Marshall Clow49c76432018-01-15 16:16:321268// shorten sequences as much as possible by lopping of any equal prefix
Eric Fiselier910285b2014-10-27 19:28:201269 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnant3e519522010-05-11 19:42:161270 if (!__pred(*__first1, *__first2))
Marshall Clow49c76432018-01-15 16:16:321271 break;
1272 if (__first1 == __last1)
1273 return true;
1274
1275// __first1 != __last1 && *__first1 != *__first2
Howard Hinnant3e519522010-05-11 19:42:161276 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
Howard Hinnantce48a112011-06-30 21:18:191277 _D1 __l1 = _VSTD::distance(__first1, __last1);
Howard Hinnant3e519522010-05-11 19:42:161278 if (__l1 == _D1(1))
1279 return false;
Howard Hinnantce48a112011-06-30 21:18:191280 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
Howard Hinnant3e519522010-05-11 19:42:161281 // For each element in [f1, l1) see if there are the same number of
1282 // equal elements in [f2, l2)
1283 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1284 {
Marshall Clow49c76432018-01-15 16:16:321285 // Have we already counted the number of *__i in [f1, l1)?
Peter Collingbourne939b1622018-01-26 21:23:271286 _ForwardIterator1 __match = __first1;
1287 for (; __match != __i; ++__match)
1288 if (__pred(*__match, *__i))
1289 break;
1290 if (__match == __i) {
Howard Hinnant3e519522010-05-11 19:42:161291 // Count number of *__i in [f2, l2)
1292 _D1 __c2 = 0;
1293 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1294 if (__pred(*__i, *__j))
1295 ++__c2;
1296 if (__c2 == 0)
1297 return false;
1298 // Count number of *__i in [__i, l1) (we can start with 1)
1299 _D1 __c1 = 1;
Howard Hinnantce48a112011-06-30 21:18:191300 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
Howard Hinnant3e519522010-05-11 19:42:161301 if (__pred(*__i, *__j))
1302 ++__c1;
1303 if (__c1 != __c2)
1304 return false;
1305 }
Howard Hinnant3e519522010-05-11 19:42:161306 }
1307 return true;
1308}
1309
1310template<class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow49c76432018-01-15 16:16:321311inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161312bool
1313is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1314 _ForwardIterator2 __first2)
1315{
1316 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1317 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantce48a112011-06-30 21:18:191318 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnant3e519522010-05-11 19:42:161319}
1320
Marshall Clow0b0bbd22013-05-09 21:14:231321#if _LIBCPP_STD_VER > 11
1322template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow49c76432018-01-15 16:16:321323_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Marshall Clow0b0bbd22013-05-09 21:14:231324__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Aditya Kumar331fb802016-08-25 11:52:381325 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
Marshall Clow0b0bbd22013-05-09 21:14:231326 _BinaryPredicate __pred,
1327 forward_iterator_tag, forward_iterator_tag )
1328{
Marshall Clow49c76432018-01-15 16:16:321329// shorten sequences as much as possible by lopping of any equal prefix
Eric Fiselier847ee132014-10-27 20:26:251330 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow0b0bbd22013-05-09 21:14:231331 if (!__pred(*__first1, *__first2))
Marshall Clow49c76432018-01-15 16:16:321332 break;
1333 if (__first1 == __last1)
Marshall Clow12c74232018-01-19 18:07:291334 return __first2 == __last2;
Marshall Clow49c76432018-01-15 16:16:321335 else if (__first2 == __last2)
Marshall Clow12c74232018-01-19 18:07:291336 return false;
Marshall Clow49c76432018-01-15 16:16:321337
Marshall Clow0b0bbd22013-05-09 21:14:231338 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1339 _D1 __l1 = _VSTD::distance(__first1, __last1);
1340
1341 typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
Marshall Clowfce85ba2013-05-10 00:16:101342 _D2 __l2 = _VSTD::distance(__first2, __last2);
Marshall Clow0b0bbd22013-05-09 21:14:231343 if (__l1 != __l2)
1344 return false;
1345
1346 // For each element in [f1, l1) see if there are the same number of
1347 // equal elements in [f2, l2)
1348 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1349 {
Marshall Clow49c76432018-01-15 16:16:321350 // Have we already counted the number of *__i in [f1, l1)?
Peter Collingbourne939b1622018-01-26 21:23:271351 _ForwardIterator1 __match = __first1;
1352 for (; __match != __i; ++__match)
1353 if (__pred(*__match, *__i))
1354 break;
1355 if (__match == __i) {
Marshall Clow0b0bbd22013-05-09 21:14:231356 // Count number of *__i in [f2, l2)
1357 _D1 __c2 = 0;
1358 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1359 if (__pred(*__i, *__j))
1360 ++__c2;
1361 if (__c2 == 0)
1362 return false;
1363 // Count number of *__i in [__i, l1) (we can start with 1)
1364 _D1 __c1 = 1;
1365 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1366 if (__pred(*__i, *__j))
1367 ++__c1;
1368 if (__c1 != __c2)
1369 return false;
1370 }
Marshall Clow0b0bbd22013-05-09 21:14:231371 }
1372 return true;
1373}
1374
1375template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow49c76432018-01-15 16:16:321376_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Marshall Clow0b0bbd22013-05-09 21:14:231377__is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
Aditya Kumar331fb802016-08-25 11:52:381378 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
Marshall Clow0b0bbd22013-05-09 21:14:231379 _BinaryPredicate __pred,
1380 random_access_iterator_tag, random_access_iterator_tag )
1381{
1382 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1383 return false;
1384 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1385 typename add_lvalue_reference<_BinaryPredicate>::type>
1386 (__first1, __last1, __first2, __pred );
1387}
1388
1389template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow49c76432018-01-15 16:16:321390inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231391bool
1392is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1393 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1394 _BinaryPredicate __pred )
1395{
1396 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1397 (__first1, __last1, __first2, __last2, __pred,
1398 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1399 typename iterator_traits<_ForwardIterator2>::iterator_category());
1400}
1401
1402template<class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow49c76432018-01-15 16:16:321403inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231404bool
1405is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1406 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1407{
1408 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1409 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1410 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1411 __equal_to<__v1, __v2>(),
1412 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1413 typename iterator_traits<_ForwardIterator2>::iterator_category());
1414}
1415#endif
1416
Howard Hinnant3e519522010-05-11 19:42:161417// search
Marshall Clowd835e592018-01-08 19:18:001418// __search is in <functional>
Howard Hinnant3e519522010-05-11 19:42:161419
1420template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow12f0a772018-01-16 15:48:271421inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161422_ForwardIterator1
1423search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1424 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1425{
Howard Hinnantce48a112011-06-30 21:18:191426 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnant3e519522010-05-11 19:42:161427 (__first1, __last1, __first2, __last2, __pred,
Marshall Clow28cc4dd2016-03-08 15:12:521428 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1429 typename iterator_traits<_ForwardIterator2>::iterator_category())
1430 .first;
Howard Hinnant3e519522010-05-11 19:42:161431}
1432
1433template <class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow12f0a772018-01-16 15:48:271434inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161435_ForwardIterator1
1436search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1437 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1438{
Marshall Clow28cc4dd2016-03-08 15:12:521439 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1440 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantce48a112011-06-30 21:18:191441 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnant3e519522010-05-11 19:42:161442}
1443
Marshall Clowd835e592018-01-08 19:18:001444
1445#if _LIBCPP_STD_VER > 14
1446template <class _ForwardIterator, class _Searcher>
Marshall Clow12f0a772018-01-16 15:48:271447_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowd835e592018-01-08 19:18:001448_ForwardIterator search(_ForwardIterator __f, _ForwardIterator __l, const _Searcher &__s)
1449{ return __s(__f, __l).first; }
1450#endif
1451
Howard Hinnant3e519522010-05-11 19:42:161452// search_n
1453
1454template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
Marshall Clow12f0a772018-01-16 15:48:271455_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:161456__search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnante4383372011-10-22 20:59:451457 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
Howard Hinnant3e519522010-05-11 19:42:161458{
1459 if (__count <= 0)
1460 return __first;
1461 while (true)
1462 {
Howard Hinnante4383372011-10-22 20:59:451463 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnant3e519522010-05-11 19:42:161464 while (true)
1465 {
Howard Hinnante4383372011-10-22 20:59:451466 if (__first == __last) // return __last if no element matches __value_
Howard Hinnant3e519522010-05-11 19:42:161467 return __last;
Howard Hinnante4383372011-10-22 20:59:451468 if (__pred(*__first, __value_))
Howard Hinnant3e519522010-05-11 19:42:161469 break;
1470 ++__first;
1471 }
Howard Hinnante4383372011-10-22 20:59:451472 // *__first matches __value_, now match elements after here
Howard Hinnant3e519522010-05-11 19:42:161473 _ForwardIterator __m = __first;
1474 _Size __c(0);
1475 while (true)
1476 {
1477 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1478 return __first;
1479 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1480 return __last;
Howard Hinnante4383372011-10-22 20:59:451481 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnant3e519522010-05-11 19:42:161482 {
1483 __first = __m;
1484 ++__first;
1485 break;
1486 } // else there is a match, check next elements
1487 }
1488 }
1489}
1490
1491template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
Marshall Clow12f0a772018-01-16 15:48:271492_LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
Howard Hinnant3e519522010-05-11 19:42:161493__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnante4383372011-10-22 20:59:451494 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
Howard Hinnant3e519522010-05-11 19:42:161495{
1496 if (__count <= 0)
1497 return __first;
1498 _Size __len = static_cast<_Size>(__last - __first);
1499 if (__len < __count)
1500 return __last;
1501 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1502 while (true)
1503 {
Howard Hinnante4383372011-10-22 20:59:451504 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnant3e519522010-05-11 19:42:161505 while (true)
1506 {
Howard Hinnantb13fcad2013-04-04 15:40:481507 if (__first >= __s) // return __last if no element matches __value_
Howard Hinnant3e519522010-05-11 19:42:161508 return __last;
Howard Hinnante4383372011-10-22 20:59:451509 if (__pred(*__first, __value_))
Howard Hinnant3e519522010-05-11 19:42:161510 break;
1511 ++__first;
1512 }
Howard Hinnante4383372011-10-22 20:59:451513 // *__first matches __value_, now match elements after here
Howard Hinnant3e519522010-05-11 19:42:161514 _RandomAccessIterator __m = __first;
1515 _Size __c(0);
1516 while (true)
1517 {
1518 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1519 return __first;
1520 ++__m; // no need to check range on __m because __s guarantees we have enough source
Howard Hinnante4383372011-10-22 20:59:451521 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnant3e519522010-05-11 19:42:161522 {
1523 __first = __m;
1524 ++__first;
1525 break;
1526 } // else there is a match, check next elements
1527 }
1528 }
1529}
1530
1531template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
Marshall Clow12f0a772018-01-16 15:48:271532inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161533_ForwardIterator
1534search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnante4383372011-10-22 20:59:451535 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
Howard Hinnant3e519522010-05-11 19:42:161536{
Howard Hinnantce48a112011-06-30 21:18:191537 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
Eric Fiselier51544022015-02-10 16:46:421538 (__first, __last, __convert_to_integral(__count), __value_, __pred,
1539 typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnant3e519522010-05-11 19:42:161540}
1541
1542template <class _ForwardIterator, class _Size, class _Tp>
Marshall Clow12f0a772018-01-16 15:48:271543inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161544_ForwardIterator
Howard Hinnante4383372011-10-22 20:59:451545search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:161546{
1547 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Eric Fiselier51544022015-02-10 16:46:421548 return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
1549 __value_, __equal_to<__v, _Tp>());
Howard Hinnant3e519522010-05-11 19:42:161550}
1551
1552// copy
Howard Hinnant3e519522010-05-11 19:42:161553template <class _Iter>
1554inline _LIBCPP_INLINE_VISIBILITY
1555_Iter
1556__unwrap_iter(_Iter __i)
1557{
1558 return __i;
1559}
1560
1561template <class _Tp>
Marshall Clow5b7c98e2017-05-25 14:20:261562inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161563typename enable_if
1564<
Howard Hinnantca740482010-11-19 22:17:281565 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnant3e519522010-05-11 19:42:161566 _Tp*
1567>::type
1568__unwrap_iter(move_iterator<_Tp*> __i)
1569{
1570 return __i.base();
1571}
1572
Howard Hinnantfc88dbd2013-08-23 17:37:051573#if _LIBCPP_DEBUG_LEVEL < 2
1574
Howard Hinnant3e519522010-05-11 19:42:161575template <class _Tp>
Marshall Clow9cad5022018-07-13 16:35:261576inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant3e519522010-05-11 19:42:161577typename enable_if
1578<
Howard Hinnantca740482010-11-19 22:17:281579 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnant3e519522010-05-11 19:42:161580 _Tp*
1581>::type
1582__unwrap_iter(__wrap_iter<_Tp*> __i)
1583{
1584 return __i.base();
1585}
1586
Eric Fiselier14bd0bf2016-12-28 05:35:321587#else
1588
1589template <class _Tp>
Marshall Clow9cad5022018-07-13 16:35:261590inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Eric Fiselier14bd0bf2016-12-28 05:35:321591typename enable_if
1592<
1593 is_trivially_copy_assignable<_Tp>::value,
1594 __wrap_iter<_Tp*>
1595>::type
1596__unwrap_iter(__wrap_iter<_Tp*> __i)
1597{
1598 return __i;
1599}
1600
Howard Hinnantfc88dbd2013-08-23 17:37:051601#endif // _LIBCPP_DEBUG_LEVEL < 2
1602
Howard Hinnant3e519522010-05-11 19:42:161603template <class _InputIterator, class _OutputIterator>
1604inline _LIBCPP_INLINE_VISIBILITY
1605_OutputIterator
1606__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1607{
Eric Fiselier910285b2014-10-27 19:28:201608 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant3e519522010-05-11 19:42:161609 *__result = *__first;
1610 return __result;
1611}
1612
1613template <class _Tp, class _Up>
1614inline _LIBCPP_INLINE_VISIBILITY
1615typename enable_if
1616<
1617 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnantca740482010-11-19 22:17:281618 is_trivially_copy_assignable<_Up>::value,
Howard Hinnant3e519522010-05-11 19:42:161619 _Up*
1620>::type
1621__copy(_Tp* __first, _Tp* __last, _Up* __result)
1622{
1623 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow5b312052015-06-02 13:52:161624 if (__n > 0)
1625 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnant3e519522010-05-11 19:42:161626 return __result + __n;
1627}
1628
1629template <class _InputIterator, class _OutputIterator>
1630inline _LIBCPP_INLINE_VISIBILITY
1631_OutputIterator
1632copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1633{
Howard Hinnantce48a112011-06-30 21:18:191634 return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnant3e519522010-05-11 19:42:161635}
1636
1637// copy_backward
1638
Howard Hinnantd3d43562013-02-06 21:03:391639template <class _BidirectionalIterator, class _OutputIterator>
Howard Hinnant3e519522010-05-11 19:42:161640inline _LIBCPP_INLINE_VISIBILITY
1641_OutputIterator
Howard Hinnantd3d43562013-02-06 21:03:391642__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
Howard Hinnant3e519522010-05-11 19:42:161643{
1644 while (__first != __last)
1645 *--__result = *--__last;
1646 return __result;
1647}
1648
1649template <class _Tp, class _Up>
1650inline _LIBCPP_INLINE_VISIBILITY
1651typename enable_if
1652<
1653 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnantca740482010-11-19 22:17:281654 is_trivially_copy_assignable<_Up>::value,
Howard Hinnant3e519522010-05-11 19:42:161655 _Up*
1656>::type
1657__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1658{
1659 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow5b312052015-06-02 13:52:161660 if (__n > 0)
1661 {
1662 __result -= __n;
1663 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1664 }
Howard Hinnant3e519522010-05-11 19:42:161665 return __result;
1666}
1667
1668template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1669inline _LIBCPP_INLINE_VISIBILITY
1670_BidirectionalIterator2
1671copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1672 _BidirectionalIterator2 __result)
1673{
Eric Fiselierfd838222016-12-23 23:37:521674 return _VSTD::__copy_backward(__unwrap_iter(__first),
1675 __unwrap_iter(__last),
1676 __unwrap_iter(__result));
Howard Hinnant3e519522010-05-11 19:42:161677}
1678
1679// copy_if
1680
1681template<class _InputIterator, class _OutputIterator, class _Predicate>
1682inline _LIBCPP_INLINE_VISIBILITY
1683_OutputIterator
1684copy_if(_InputIterator __first, _InputIterator __last,
1685 _OutputIterator __result, _Predicate __pred)
1686{
1687 for (; __first != __last; ++__first)
1688 {
1689 if (__pred(*__first))
1690 {
1691 *__result = *__first;
1692 ++__result;
1693 }
1694 }
1695 return __result;
1696}
1697
1698// copy_n
1699
1700template<class _InputIterator, class _Size, class _OutputIterator>
1701inline _LIBCPP_INLINE_VISIBILITY
1702typename enable_if
1703<
1704 __is_input_iterator<_InputIterator>::value &&
1705 !__is_random_access_iterator<_InputIterator>::value,
1706 _OutputIterator
1707>::type
Eric Fiselier51544022015-02-10 16:46:421708copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnant3e519522010-05-11 19:42:161709{
Eric Fiselier51544022015-02-10 16:46:421710 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1711 _IntegralSize __n = __orig_n;
Howard Hinnant99847d22011-02-27 20:55:391712 if (__n > 0)
1713 {
Howard Hinnant3e519522010-05-11 19:42:161714 *__result = *__first;
Howard Hinnant99847d22011-02-27 20:55:391715 ++__result;
1716 for (--__n; __n > 0; --__n)
1717 {
1718 ++__first;
1719 *__result = *__first;
1720 ++__result;
1721 }
1722 }
Howard Hinnant3e519522010-05-11 19:42:161723 return __result;
1724}
1725
1726template<class _InputIterator, class _Size, class _OutputIterator>
1727inline _LIBCPP_INLINE_VISIBILITY
1728typename enable_if
1729<
1730 __is_random_access_iterator<_InputIterator>::value,
1731 _OutputIterator
1732>::type
Eric Fiselier51544022015-02-10 16:46:421733copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnant3e519522010-05-11 19:42:161734{
Eric Fiselier51544022015-02-10 16:46:421735 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1736 _IntegralSize __n = __orig_n;
Howard Hinnantce48a112011-06-30 21:18:191737 return _VSTD::copy(__first, __first + __n, __result);
Howard Hinnant3e519522010-05-11 19:42:161738}
1739
1740// move
1741
1742template <class _InputIterator, class _OutputIterator>
1743inline _LIBCPP_INLINE_VISIBILITY
1744_OutputIterator
1745__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1746{
Eric Fiselier910285b2014-10-27 19:28:201747 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantce48a112011-06-30 21:18:191748 *__result = _VSTD::move(*__first);
Howard Hinnant3e519522010-05-11 19:42:161749 return __result;
1750}
1751
1752template <class _Tp, class _Up>
1753inline _LIBCPP_INLINE_VISIBILITY
1754typename enable_if
1755<
1756 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnantca740482010-11-19 22:17:281757 is_trivially_copy_assignable<_Up>::value,
Howard Hinnant3e519522010-05-11 19:42:161758 _Up*
1759>::type
1760__move(_Tp* __first, _Tp* __last, _Up* __result)
1761{
1762 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow5b312052015-06-02 13:52:161763 if (__n > 0)
1764 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnant3e519522010-05-11 19:42:161765 return __result + __n;
1766}
1767
1768template <class _InputIterator, class _OutputIterator>
1769inline _LIBCPP_INLINE_VISIBILITY
1770_OutputIterator
1771move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1772{
Howard Hinnantce48a112011-06-30 21:18:191773 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnant3e519522010-05-11 19:42:161774}
1775
1776// move_backward
1777
1778template <class _InputIterator, class _OutputIterator>
1779inline _LIBCPP_INLINE_VISIBILITY
1780_OutputIterator
1781__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1782{
1783 while (__first != __last)
Howard Hinnantce48a112011-06-30 21:18:191784 *--__result = _VSTD::move(*--__last);
Howard Hinnant3e519522010-05-11 19:42:161785 return __result;
1786}
1787
1788template <class _Tp, class _Up>
1789inline _LIBCPP_INLINE_VISIBILITY
1790typename enable_if
1791<
1792 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnantca740482010-11-19 22:17:281793 is_trivially_copy_assignable<_Up>::value,
Howard Hinnant3e519522010-05-11 19:42:161794 _Up*
1795>::type
1796__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1797{
1798 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow5b312052015-06-02 13:52:161799 if (__n > 0)
1800 {
1801 __result -= __n;
1802 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1803 }
Howard Hinnant3e519522010-05-11 19:42:161804 return __result;
1805}
1806
1807template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1808inline _LIBCPP_INLINE_VISIBILITY
1809_BidirectionalIterator2
1810move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1811 _BidirectionalIterator2 __result)
1812{
Howard Hinnantce48a112011-06-30 21:18:191813 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnant3e519522010-05-11 19:42:161814}
1815
1816// iter_swap
1817
Howard Hinnanta676f7d2011-05-27 15:04:191818// moved to <type_traits> for better swap / noexcept support
Howard Hinnant3e519522010-05-11 19:42:161819
1820// transform
1821
1822template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
Marshall Clow99894b62018-01-19 17:45:391823inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161824_OutputIterator
1825transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1826{
Eric Fiselier910285b2014-10-27 19:28:201827 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant3e519522010-05-11 19:42:161828 *__result = __op(*__first);
1829 return __result;
1830}
1831
1832template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
Marshall Clow99894b62018-01-19 17:45:391833inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161834_OutputIterator
1835transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1836 _OutputIterator __result, _BinaryOperation __binary_op)
1837{
Eric Fiselier910285b2014-10-27 19:28:201838 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
Howard Hinnant3e519522010-05-11 19:42:161839 *__result = __binary_op(*__first1, *__first2);
1840 return __result;
1841}
1842
1843// replace
1844
1845template <class _ForwardIterator, class _Tp>
Marshall Clow12c74232018-01-19 18:07:291846inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161847void
1848replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1849{
1850 for (; __first != __last; ++__first)
1851 if (*__first == __old_value)
1852 *__first = __new_value;
1853}
1854
1855// replace_if
1856
1857template <class _ForwardIterator, class _Predicate, class _Tp>
Marshall Clow12c74232018-01-19 18:07:291858inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161859void
1860replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1861{
1862 for (; __first != __last; ++__first)
1863 if (__pred(*__first))
1864 *__first = __new_value;
1865}
1866
1867// replace_copy
1868
1869template <class _InputIterator, class _OutputIterator, class _Tp>
Marshall Clow12c74232018-01-19 18:07:291870inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161871_OutputIterator
1872replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1873 const _Tp& __old_value, const _Tp& __new_value)
1874{
Eric Fiselier910285b2014-10-27 19:28:201875 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant3e519522010-05-11 19:42:161876 if (*__first == __old_value)
1877 *__result = __new_value;
1878 else
1879 *__result = *__first;
1880 return __result;
1881}
1882
1883// replace_copy_if
1884
1885template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
Marshall Clow12c74232018-01-19 18:07:291886inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161887_OutputIterator
1888replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1889 _Predicate __pred, const _Tp& __new_value)
1890{
Eric Fiselier910285b2014-10-27 19:28:201891 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant3e519522010-05-11 19:42:161892 if (__pred(*__first))
1893 *__result = __new_value;
1894 else
1895 *__result = *__first;
1896 return __result;
1897}
1898
1899// fill_n
1900
1901template <class _OutputIterator, class _Size, class _Tp>
Marshall Clow4bfb9312018-01-20 20:14:321902inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161903_OutputIterator
Howard Hinnant0f242be2013-08-01 17:29:281904__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:161905{
Eric Fiselier910285b2014-10-27 19:28:201906 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnante4383372011-10-22 20:59:451907 *__first = __value_;
Howard Hinnant3e519522010-05-11 19:42:161908 return __first;
1909}
1910
Howard Hinnant3e519522010-05-11 19:42:161911template <class _OutputIterator, class _Size, class _Tp>
Marshall Clow4bfb9312018-01-20 20:14:321912inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161913_OutputIterator
Howard Hinnante4383372011-10-22 20:59:451914fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:161915{
Eric Fiselier51544022015-02-10 16:46:421916 return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
Howard Hinnant3e519522010-05-11 19:42:161917}
1918
1919// fill
1920
1921template <class _ForwardIterator, class _Tp>
Marshall Clow4bfb9312018-01-20 20:14:321922inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161923void
Howard Hinnante4383372011-10-22 20:59:451924__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
Howard Hinnant3e519522010-05-11 19:42:161925{
1926 for (; __first != __last; ++__first)
Howard Hinnante4383372011-10-22 20:59:451927 *__first = __value_;
Howard Hinnant3e519522010-05-11 19:42:161928}
1929
1930template <class _RandomAccessIterator, class _Tp>
Marshall Clow4bfb9312018-01-20 20:14:321931inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161932void
Howard Hinnante4383372011-10-22 20:59:451933__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
Howard Hinnant3e519522010-05-11 19:42:161934{
Howard Hinnante4383372011-10-22 20:59:451935 _VSTD::fill_n(__first, __last - __first, __value_);
Howard Hinnant3e519522010-05-11 19:42:161936}
1937
1938template <class _ForwardIterator, class _Tp>
Marshall Clow4bfb9312018-01-20 20:14:321939inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161940void
Howard Hinnante4383372011-10-22 20:59:451941fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:161942{
Howard Hinnante4383372011-10-22 20:59:451943 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnant3e519522010-05-11 19:42:161944}
1945
1946// generate
1947
1948template <class _ForwardIterator, class _Generator>
Marshall Clow4bfb9312018-01-20 20:14:321949inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161950void
1951generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
1952{
1953 for (; __first != __last; ++__first)
1954 *__first = __gen();
1955}
1956
1957// generate_n
1958
1959template <class _OutputIterator, class _Size, class _Generator>
Marshall Clow4bfb9312018-01-20 20:14:321960inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161961_OutputIterator
Eric Fiselier51544022015-02-10 16:46:421962generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
Howard Hinnant3e519522010-05-11 19:42:161963{
Eric Fiselier51544022015-02-10 16:46:421964 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1965 _IntegralSize __n = __orig_n;
Eric Fiselier910285b2014-10-27 19:28:201966 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnant3e519522010-05-11 19:42:161967 *__first = __gen();
1968 return __first;
1969}
1970
1971// remove
1972
1973template <class _ForwardIterator, class _Tp>
Marshall Clowe8ea8292018-01-22 21:43:041974_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnante4383372011-10-22 20:59:451975remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:161976{
Howard Hinnante4383372011-10-22 20:59:451977 __first = _VSTD::find(__first, __last, __value_);
Howard Hinnant3e519522010-05-11 19:42:161978 if (__first != __last)
1979 {
1980 _ForwardIterator __i = __first;
1981 while (++__i != __last)
1982 {
Howard Hinnante4383372011-10-22 20:59:451983 if (!(*__i == __value_))
Howard Hinnant3e519522010-05-11 19:42:161984 {
Howard Hinnantce48a112011-06-30 21:18:191985 *__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:161986 ++__first;
1987 }
1988 }
1989 }
1990 return __first;
1991}
1992
1993// remove_if
1994
1995template <class _ForwardIterator, class _Predicate>
Marshall Clowe8ea8292018-01-22 21:43:041996_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:161997remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
1998{
Howard Hinnantce48a112011-06-30 21:18:191999 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
Howard Hinnant3e519522010-05-11 19:42:162000 (__first, __last, __pred);
2001 if (__first != __last)
2002 {
2003 _ForwardIterator __i = __first;
2004 while (++__i != __last)
2005 {
2006 if (!__pred(*__i))
2007 {
Howard Hinnantce48a112011-06-30 21:18:192008 *__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:162009 ++__first;
2010 }
2011 }
2012 }
2013 return __first;
2014}
2015
2016// remove_copy
2017
2018template <class _InputIterator, class _OutputIterator, class _Tp>
Marshall Clowe8ea8292018-01-22 21:43:042019inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162020_OutputIterator
Howard Hinnante4383372011-10-22 20:59:452021remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:162022{
2023 for (; __first != __last; ++__first)
2024 {
Howard Hinnante4383372011-10-22 20:59:452025 if (!(*__first == __value_))
Howard Hinnant3e519522010-05-11 19:42:162026 {
2027 *__result = *__first;
2028 ++__result;
2029 }
2030 }
2031 return __result;
2032}
2033
2034// remove_copy_if
2035
2036template <class _InputIterator, class _OutputIterator, class _Predicate>
Marshall Clowe8ea8292018-01-22 21:43:042037inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162038_OutputIterator
2039remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2040{
2041 for (; __first != __last; ++__first)
2042 {
2043 if (!__pred(*__first))
2044 {
2045 *__result = *__first;
2046 ++__result;
2047 }
2048 }
2049 return __result;
2050}
2051
2052// unique
2053
2054template <class _ForwardIterator, class _BinaryPredicate>
Marshall Clow4bfb9312018-01-20 20:14:322055_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:162056unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2057{
Howard Hinnantce48a112011-06-30 21:18:192058 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnant3e519522010-05-11 19:42:162059 (__first, __last, __pred);
2060 if (__first != __last)
2061 {
2062 // ... a a ? ...
2063 // f i
2064 _ForwardIterator __i = __first;
2065 for (++__i; ++__i != __last;)
2066 if (!__pred(*__first, *__i))
Howard Hinnantce48a112011-06-30 21:18:192067 *++__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:162068 ++__first;
2069 }
2070 return __first;
2071}
2072
2073template <class _ForwardIterator>
Marshall Clow4bfb9312018-01-20 20:14:322074inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162075_ForwardIterator
2076unique(_ForwardIterator __first, _ForwardIterator __last)
2077{
2078 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnantce48a112011-06-30 21:18:192079 return _VSTD::unique(__first, __last, __equal_to<__v>());
Howard Hinnant3e519522010-05-11 19:42:162080}
2081
2082// unique_copy
2083
2084template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
Marshall Clow4bfb9312018-01-20 20:14:322085_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnant3e519522010-05-11 19:42:162086__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2087 input_iterator_tag, output_iterator_tag)
2088{
2089 if (__first != __last)
2090 {
2091 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2092 *__result = __t;
2093 ++__result;
2094 while (++__first != __last)
2095 {
2096 if (!__pred(__t, *__first))
2097 {
2098 __t = *__first;
2099 *__result = __t;
2100 ++__result;
2101 }
2102 }
2103 }
2104 return __result;
2105}
2106
2107template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
Marshall Clow4bfb9312018-01-20 20:14:322108_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnant3e519522010-05-11 19:42:162109__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2110 forward_iterator_tag, output_iterator_tag)
2111{
2112 if (__first != __last)
2113 {
2114 _ForwardIterator __i = __first;
2115 *__result = *__i;
2116 ++__result;
2117 while (++__first != __last)
2118 {
2119 if (!__pred(*__i, *__first))
2120 {
2121 *__result = *__first;
2122 ++__result;
2123 __i = __first;
2124 }
2125 }
2126 }
2127 return __result;
2128}
2129
2130template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
Marshall Clow4bfb9312018-01-20 20:14:322131_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:162132__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2133 input_iterator_tag, forward_iterator_tag)
2134{
2135 if (__first != __last)
2136 {
2137 *__result = *__first;
2138 while (++__first != __last)
2139 if (!__pred(*__result, *__first))
2140 *++__result = *__first;
2141 ++__result;
2142 }
2143 return __result;
2144}
2145
Howard Hinnant3e519522010-05-11 19:42:162146template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
Marshall Clow4bfb9312018-01-20 20:14:322147inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162148_OutputIterator
2149unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2150{
Howard Hinnantce48a112011-06-30 21:18:192151 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnant3e519522010-05-11 19:42:162152 (__first, __last, __result, __pred,
2153 typename iterator_traits<_InputIterator>::iterator_category(),
2154 typename iterator_traits<_OutputIterator>::iterator_category());
2155}
2156
2157template <class _InputIterator, class _OutputIterator>
Marshall Clow4bfb9312018-01-20 20:14:322158inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162159_OutputIterator
2160unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2161{
2162 typedef typename iterator_traits<_InputIterator>::value_type __v;
Howard Hinnantce48a112011-06-30 21:18:192163 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
Howard Hinnant3e519522010-05-11 19:42:162164}
2165
2166// reverse
2167
2168template <class _BidirectionalIterator>
2169inline _LIBCPP_INLINE_VISIBILITY
2170void
2171__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2172{
2173 while (__first != __last)
2174 {
2175 if (__first == --__last)
2176 break;
Marshall Clowdef501d2015-11-02 21:34:252177 _VSTD::iter_swap(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:162178 ++__first;
2179 }
2180}
2181
2182template <class _RandomAccessIterator>
2183inline _LIBCPP_INLINE_VISIBILITY
2184void
2185__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2186{
2187 if (__first != __last)
2188 for (; __first < --__last; ++__first)
Marshall Clowdef501d2015-11-02 21:34:252189 _VSTD::iter_swap(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:162190}
2191
2192template <class _BidirectionalIterator>
2193inline _LIBCPP_INLINE_VISIBILITY
2194void
2195reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2196{
Howard Hinnantce48a112011-06-30 21:18:192197 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
Howard Hinnant3e519522010-05-11 19:42:162198}
2199
2200// reverse_copy
2201
2202template <class _BidirectionalIterator, class _OutputIterator>
Marshall Clowe8ea8292018-01-22 21:43:042203inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162204_OutputIterator
2205reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2206{
2207 for (; __first != __last; ++__result)
2208 *__result = *--__last;
2209 return __result;
2210}
2211
2212// rotate
2213
2214template <class _ForwardIterator>
2215_ForwardIterator
Howard Hinnantaca09de2012-08-03 18:01:202216__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
Howard Hinnant3e519522010-05-11 19:42:162217{
Howard Hinnantaca09de2012-08-03 18:01:202218 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2219 value_type __tmp = _VSTD::move(*__first);
2220 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2221 *__lm1 = _VSTD::move(__tmp);
2222 return __lm1;
2223}
2224
2225template <class _BidirectionalIterator>
2226_BidirectionalIterator
2227__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2228{
2229 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2230 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2231 value_type __tmp = _VSTD::move(*__lm1);
2232 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2233 *__first = _VSTD::move(__tmp);
2234 return __fp1;
2235}
2236
2237template <class _ForwardIterator>
2238_ForwardIterator
2239__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2240{
Howard Hinnant3e519522010-05-11 19:42:162241 _ForwardIterator __i = __middle;
2242 while (true)
2243 {
2244 swap(*__first, *__i);
2245 ++__first;
2246 if (++__i == __last)
2247 break;
2248 if (__first == __middle)
2249 __middle = __i;
2250 }
2251 _ForwardIterator __r = __first;
2252 if (__first != __middle)
2253 {
2254 __i = __middle;
2255 while (true)
2256 {
2257 swap(*__first, *__i);
2258 ++__first;
2259 if (++__i == __last)
2260 {
2261 if (__first == __middle)
2262 break;
2263 __i = __middle;
2264 }
2265 else if (__first == __middle)
2266 __middle = __i;
2267 }
2268 }
2269 return __r;
2270}
2271
2272template<typename _Integral>
2273inline _LIBCPP_INLINE_VISIBILITY
2274_Integral
Marshall Clow19b40352016-07-26 14:29:452275__algo_gcd(_Integral __x, _Integral __y)
Howard Hinnant3e519522010-05-11 19:42:162276{
2277 do
2278 {
2279 _Integral __t = __x % __y;
2280 __x = __y;
2281 __y = __t;
2282 } while (__y);
2283 return __x;
2284}
2285
2286template<typename _RandomAccessIterator>
2287_RandomAccessIterator
Howard Hinnantaca09de2012-08-03 18:01:202288__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
Howard Hinnant3e519522010-05-11 19:42:162289{
2290 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2291 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnantb3371f62010-08-22 00:02:432292
Howard Hinnant3e519522010-05-11 19:42:162293 const difference_type __m1 = __middle - __first;
2294 const difference_type __m2 = __last - __middle;
2295 if (__m1 == __m2)
2296 {
Howard Hinnantce48a112011-06-30 21:18:192297 _VSTD::swap_ranges(__first, __middle, __middle);
Howard Hinnant3e519522010-05-11 19:42:162298 return __middle;
2299 }
Marshall Clow19b40352016-07-26 14:29:452300 const difference_type __g = _VSTD::__algo_gcd(__m1, __m2);
Howard Hinnant3e519522010-05-11 19:42:162301 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2302 {
Howard Hinnantaca09de2012-08-03 18:01:202303 value_type __t(_VSTD::move(*--__p));
Howard Hinnant3e519522010-05-11 19:42:162304 _RandomAccessIterator __p1 = __p;
2305 _RandomAccessIterator __p2 = __p1 + __m1;
2306 do
2307 {
Howard Hinnantaca09de2012-08-03 18:01:202308 *__p1 = _VSTD::move(*__p2);
Howard Hinnant3e519522010-05-11 19:42:162309 __p1 = __p2;
2310 const difference_type __d = __last - __p2;
2311 if (__m1 < __d)
2312 __p2 += __m1;
2313 else
2314 __p2 = __first + (__m1 - __d);
2315 } while (__p2 != __p);
Howard Hinnantaca09de2012-08-03 18:01:202316 *__p1 = _VSTD::move(__t);
Howard Hinnant3e519522010-05-11 19:42:162317 }
2318 return __first + __m2;
2319}
2320
2321template <class _ForwardIterator>
2322inline _LIBCPP_INLINE_VISIBILITY
2323_ForwardIterator
Howard Hinnantaca09de2012-08-03 18:01:202324__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2325 _VSTD::forward_iterator_tag)
2326{
2327 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2328 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2329 {
2330 if (_VSTD::next(__first) == __middle)
2331 return _VSTD::__rotate_left(__first, __last);
2332 }
2333 return _VSTD::__rotate_forward(__first, __middle, __last);
2334}
2335
2336template <class _BidirectionalIterator>
2337inline _LIBCPP_INLINE_VISIBILITY
2338_BidirectionalIterator
2339__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2340 _VSTD::bidirectional_iterator_tag)
2341{
2342 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2343 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2344 {
2345 if (_VSTD::next(__first) == __middle)
2346 return _VSTD::__rotate_left(__first, __last);
2347 if (_VSTD::next(__middle) == __last)
2348 return _VSTD::__rotate_right(__first, __last);
2349 }
2350 return _VSTD::__rotate_forward(__first, __middle, __last);
2351}
2352
2353template <class _RandomAccessIterator>
2354inline _LIBCPP_INLINE_VISIBILITY
2355_RandomAccessIterator
2356__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2357 _VSTD::random_access_iterator_tag)
2358{
2359 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2360 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2361 {
2362 if (_VSTD::next(__first) == __middle)
2363 return _VSTD::__rotate_left(__first, __last);
2364 if (_VSTD::next(__middle) == __last)
2365 return _VSTD::__rotate_right(__first, __last);
2366 return _VSTD::__rotate_gcd(__first, __middle, __last);
2367 }
2368 return _VSTD::__rotate_forward(__first, __middle, __last);
2369}
2370
2371template <class _ForwardIterator>
2372inline _LIBCPP_INLINE_VISIBILITY
2373_ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:162374rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2375{
Howard Hinnantaca09de2012-08-03 18:01:202376 if (__first == __middle)
2377 return __last;
2378 if (__middle == __last)
2379 return __first;
Howard Hinnantce48a112011-06-30 21:18:192380 return _VSTD::__rotate(__first, __middle, __last,
Howard Hinnantaca09de2012-08-03 18:01:202381 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnant3e519522010-05-11 19:42:162382}
2383
2384// rotate_copy
2385
2386template <class _ForwardIterator, class _OutputIterator>
2387inline _LIBCPP_INLINE_VISIBILITY
2388_OutputIterator
2389rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2390{
Howard Hinnantce48a112011-06-30 21:18:192391 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
Howard Hinnant3e519522010-05-11 19:42:162392}
2393
Howard Hinnant3e519522010-05-11 19:42:162394// min_element
2395
2396template <class _ForwardIterator, class _Compare>
Marshall Clow9d67c6d2014-02-19 16:51:352397inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant3e519522010-05-11 19:42:162398_ForwardIterator
Marshall Clow0b0671a2015-05-10 13:53:312399min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:162400{
Eric Fiseliera60d7fa2018-08-22 17:47:132401 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2402 "std::min_element requires a ForwardIterator");
Howard Hinnant3e519522010-05-11 19:42:162403 if (__first != __last)
2404 {
2405 _ForwardIterator __i = __first;
2406 while (++__i != __last)
2407 if (__comp(*__i, *__first))
2408 __first = __i;
2409 }
2410 return __first;
2411}
2412
2413template <class _ForwardIterator>
Marshall Clow0b0671a2015-05-10 13:53:312414inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant3e519522010-05-11 19:42:162415_ForwardIterator
2416min_element(_ForwardIterator __first, _ForwardIterator __last)
2417{
Marshall Clow0b0671a2015-05-10 13:53:312418 return _VSTD::min_element(__first, __last,
Howard Hinnant4eb27b72010-08-21 20:10:012419 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2420}
2421
2422// min
2423
2424template <class _Tp, class _Compare>
Marshall Clow9d67c6d2014-02-19 16:51:352425inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012426const _Tp&
2427min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2428{
2429 return __comp(__b, __a) ? __b : __a;
2430}
2431
2432template <class _Tp>
Marshall Clow9d67c6d2014-02-19 16:51:352433inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012434const _Tp&
2435min(const _Tp& __a, const _Tp& __b)
2436{
Howard Hinnantce48a112011-06-30 21:18:192437 return _VSTD::min(__a, __b, __less<_Tp>());
Howard Hinnant4eb27b72010-08-21 20:10:012438}
2439
Eric Fiselierddda4562017-04-18 23:26:472440#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022441
Howard Hinnant4eb27b72010-08-21 20:10:012442template<class _Tp, class _Compare>
Marshall Clow9d67c6d2014-02-19 16:51:352443inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012444_Tp
2445min(initializer_list<_Tp> __t, _Compare __comp)
2446{
Marshall Clow0b0671a2015-05-10 13:53:312447 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
Howard Hinnant4eb27b72010-08-21 20:10:012448}
2449
2450template<class _Tp>
Marshall Clow9d67c6d2014-02-19 16:51:352451inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012452_Tp
2453min(initializer_list<_Tp> __t)
2454{
Marshall Clow0b0671a2015-05-10 13:53:312455 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnant3e519522010-05-11 19:42:162456}
2457
Eric Fiselierddda4562017-04-18 23:26:472458#endif // _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022459
Howard Hinnant3e519522010-05-11 19:42:162460// max_element
2461
2462template <class _ForwardIterator, class _Compare>
Marshall Clow9d67c6d2014-02-19 16:51:352463inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant3e519522010-05-11 19:42:162464_ForwardIterator
Marshall Clow0b0671a2015-05-10 13:53:312465max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:162466{
Eric Fiseliera60d7fa2018-08-22 17:47:132467 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2468 "std::max_element requires a ForwardIterator");
Howard Hinnant3e519522010-05-11 19:42:162469 if (__first != __last)
2470 {
2471 _ForwardIterator __i = __first;
2472 while (++__i != __last)
2473 if (__comp(*__first, *__i))
2474 __first = __i;
2475 }
2476 return __first;
2477}
2478
Marshall Clow9d67c6d2014-02-19 16:51:352479
Howard Hinnant3e519522010-05-11 19:42:162480template <class _ForwardIterator>
Marshall Clow0b0671a2015-05-10 13:53:312481inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant3e519522010-05-11 19:42:162482_ForwardIterator
2483max_element(_ForwardIterator __first, _ForwardIterator __last)
2484{
Marshall Clow0b0671a2015-05-10 13:53:312485 return _VSTD::max_element(__first, __last,
Howard Hinnant4eb27b72010-08-21 20:10:012486 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2487}
2488
2489// max
2490
2491template <class _Tp, class _Compare>
Marshall Clow9d67c6d2014-02-19 16:51:352492inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012493const _Tp&
2494max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2495{
2496 return __comp(__a, __b) ? __b : __a;
2497}
2498
2499template <class _Tp>
Marshall Clow9d67c6d2014-02-19 16:51:352500inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012501const _Tp&
2502max(const _Tp& __a, const _Tp& __b)
2503{
Howard Hinnantce48a112011-06-30 21:18:192504 return _VSTD::max(__a, __b, __less<_Tp>());
Howard Hinnant4eb27b72010-08-21 20:10:012505}
2506
Eric Fiselierddda4562017-04-18 23:26:472507#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022508
Howard Hinnant4eb27b72010-08-21 20:10:012509template<class _Tp, class _Compare>
Marshall Clow9d67c6d2014-02-19 16:51:352510inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012511_Tp
2512max(initializer_list<_Tp> __t, _Compare __comp)
2513{
Marshall Clow0b0671a2015-05-10 13:53:312514 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
Howard Hinnant4eb27b72010-08-21 20:10:012515}
2516
2517template<class _Tp>
Marshall Clow9d67c6d2014-02-19 16:51:352518inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012519_Tp
2520max(initializer_list<_Tp> __t)
2521{
Marshall Clow0b0671a2015-05-10 13:53:312522 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnant3e519522010-05-11 19:42:162523}
2524
Eric Fiselierddda4562017-04-18 23:26:472525#endif // _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022526
Marshall Clow146c14a2016-03-07 22:43:492527#if _LIBCPP_STD_VER > 14
2528// clamp
2529template<class _Tp, class _Compare>
2530inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2531const _Tp&
2532clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
2533{
2534 _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
2535 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
2536
2537}
2538
2539template<class _Tp>
2540inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2541const _Tp&
2542clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
2543{
2544 return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
2545}
2546#endif
2547
Howard Hinnant3e519522010-05-11 19:42:162548// minmax_element
2549
2550template <class _ForwardIterator, class _Compare>
Marshall Clow0b0671a2015-05-10 13:53:312551_LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant3e519522010-05-11 19:42:162552std::pair<_ForwardIterator, _ForwardIterator>
2553minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2554{
Eric Fiseliera60d7fa2018-08-22 17:47:132555 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2556 "std::minmax_element requires a ForwardIterator");
Howard Hinnant3e519522010-05-11 19:42:162557 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2558 if (__first != __last)
2559 {
2560 if (++__first != __last)
2561 {
2562 if (__comp(*__first, *__result.first))
Howard Hinnant3e519522010-05-11 19:42:162563 __result.first = __first;
Howard Hinnant3e519522010-05-11 19:42:162564 else
2565 __result.second = __first;
2566 while (++__first != __last)
2567 {
2568 _ForwardIterator __i = __first;
2569 if (++__first == __last)
2570 {
2571 if (__comp(*__i, *__result.first))
2572 __result.first = __i;
2573 else if (!__comp(*__i, *__result.second))
2574 __result.second = __i;
2575 break;
2576 }
2577 else
2578 {
2579 if (__comp(*__first, *__i))
2580 {
2581 if (__comp(*__first, *__result.first))
2582 __result.first = __first;
2583 if (!__comp(*__i, *__result.second))
2584 __result.second = __i;
2585 }
2586 else
2587 {
2588 if (__comp(*__i, *__result.first))
2589 __result.first = __i;
2590 if (!__comp(*__first, *__result.second))
2591 __result.second = __first;
2592 }
2593 }
2594 }
2595 }
2596 }
2597 return __result;
2598}
2599
2600template <class _ForwardIterator>
Marshall Clow0b0671a2015-05-10 13:53:312601inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant3e519522010-05-11 19:42:162602std::pair<_ForwardIterator, _ForwardIterator>
2603minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2604{
Marshall Clow9d67c6d2014-02-19 16:51:352605 return _VSTD::minmax_element(__first, __last,
2606 __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:162607}
2608
Howard Hinnant4eb27b72010-08-21 20:10:012609// minmax
2610
2611template<class _Tp, class _Compare>
Marshall Clow9d67c6d2014-02-19 16:51:352612inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012613pair<const _Tp&, const _Tp&>
2614minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2615{
2616 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2617 pair<const _Tp&, const _Tp&>(__a, __b);
2618}
2619
2620template<class _Tp>
Marshall Clow9d67c6d2014-02-19 16:51:352621inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012622pair<const _Tp&, const _Tp&>
2623minmax(const _Tp& __a, const _Tp& __b)
2624{
Howard Hinnantce48a112011-06-30 21:18:192625 return _VSTD::minmax(__a, __b, __less<_Tp>());
Howard Hinnant4eb27b72010-08-21 20:10:012626}
2627
Eric Fiselierddda4562017-04-18 23:26:472628#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022629
Howard Hinnant4eb27b72010-08-21 20:10:012630template<class _Tp, class _Compare>
Marshall Clow9d67c6d2014-02-19 16:51:352631inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012632pair<_Tp, _Tp>
2633minmax(initializer_list<_Tp> __t, _Compare __comp)
2634{
Marshall Clow9d67c6d2014-02-19 16:51:352635 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2636 _Iter __first = __t.begin();
2637 _Iter __last = __t.end();
Marshall Clow002144f2015-02-11 15:41:342638 std::pair<_Tp, _Tp> __result(*__first, *__first);
Marshall Clow9d67c6d2014-02-19 16:51:352639
2640 ++__first;
2641 if (__t.size() % 2 == 0)
2642 {
2643 if (__comp(*__first, __result.first))
2644 __result.first = *__first;
2645 else
2646 __result.second = *__first;
2647 ++__first;
2648 }
Aditya Kumar331fb802016-08-25 11:52:382649
Marshall Clow9d67c6d2014-02-19 16:51:352650 while (__first != __last)
2651 {
2652 _Tp __prev = *__first++;
Marshall Clow002144f2015-02-11 15:41:342653 if (__comp(*__first, __prev)) {
2654 if ( __comp(*__first, __result.first)) __result.first = *__first;
2655 if (!__comp(__prev, __result.second)) __result.second = __prev;
Marshall Clow9d67c6d2014-02-19 16:51:352656 }
2657 else {
Marshall Clow002144f2015-02-11 15:41:342658 if ( __comp(__prev, __result.first)) __result.first = __prev;
2659 if (!__comp(*__first, __result.second)) __result.second = *__first;
Marshall Clow9d67c6d2014-02-19 16:51:352660 }
Aditya Kumar331fb802016-08-25 11:52:382661
Marshall Clow9d67c6d2014-02-19 16:51:352662 __first++;
2663 }
2664 return __result;
2665}
2666
2667template<class _Tp>
2668inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2669pair<_Tp, _Tp>
2670minmax(initializer_list<_Tp> __t)
2671{
2672 return _VSTD::minmax(__t, __less<_Tp>());
Howard Hinnant4eb27b72010-08-21 20:10:012673}
2674
Eric Fiselierddda4562017-04-18 23:26:472675#endif // _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022676
Howard Hinnant3e519522010-05-11 19:42:162677// random_shuffle
2678
Howard Hinnantf9d540b2010-05-26 17:49:342679// __independent_bits_engine
2680
Howard Hinnantc003db12011-11-29 18:15:502681template <unsigned long long _Xp, size_t _Rp>
Howard Hinnantf9d540b2010-05-26 17:49:342682struct __log2_imp
Howard Hinnant3e519522010-05-11 19:42:162683{
Howard Hinnantc003db12011-11-29 18:15:502684 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2685 : __log2_imp<_Xp, _Rp - 1>::value;
Howard Hinnant3e519522010-05-11 19:42:162686};
2687
Howard Hinnantc003db12011-11-29 18:15:502688template <unsigned long long _Xp>
2689struct __log2_imp<_Xp, 0>
Howard Hinnant3e519522010-05-11 19:42:162690{
Howard Hinnantf9d540b2010-05-26 17:49:342691 static const size_t value = 0;
Howard Hinnant3e519522010-05-11 19:42:162692};
2693
Howard Hinnantc003db12011-11-29 18:15:502694template <size_t _Rp>
2695struct __log2_imp<0, _Rp>
Howard Hinnant3e519522010-05-11 19:42:162696{
Howard Hinnantc003db12011-11-29 18:15:502697 static const size_t value = _Rp + 1;
Howard Hinnant3e519522010-05-11 19:42:162698};
2699
Eric Fiselier89918ca2017-05-31 21:20:182700template <class _UIntType, _UIntType _Xp>
Howard Hinnantf9d540b2010-05-26 17:49:342701struct __log2
Howard Hinnant3e519522010-05-11 19:42:162702{
Howard Hinnantc003db12011-11-29 18:15:502703 static const size_t value = __log2_imp<_Xp,
Eric Fiselier89918ca2017-05-31 21:20:182704 sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
Howard Hinnant3e519522010-05-11 19:42:162705};
2706
Howard Hinnantf9d540b2010-05-26 17:49:342707template<class _Engine, class _UIntType>
2708class __independent_bits_engine
Howard Hinnant3e519522010-05-11 19:42:162709{
Howard Hinnantf9d540b2010-05-26 17:49:342710public:
2711 // types
2712 typedef _UIntType result_type;
2713
2714private:
2715 typedef typename _Engine::result_type _Engine_result_type;
2716 typedef typename conditional
2717 <
2718 sizeof(_Engine_result_type) <= sizeof(result_type),
2719 result_type,
2720 _Engine_result_type
2721 >::type _Working_result_type;
2722
2723 _Engine& __e_;
2724 size_t __w_;
2725 size_t __w0_;
2726 size_t __n_;
2727 size_t __n0_;
2728 _Working_result_type __y0_;
2729 _Working_result_type __y1_;
2730 _Engine_result_type __mask0_;
2731 _Engine_result_type __mask1_;
2732
Eric Fiselierddda4562017-04-18 23:26:472733#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc003db12011-11-29 18:15:502734 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnante386b7b2012-04-02 21:00:452735 + _Working_result_type(1);
2736#else
2737 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2738 + _Working_result_type(1);
2739#endif
2740 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2741 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2742 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
Howard Hinnantf9d540b2010-05-26 17:49:342743
2744public:
2745 // constructors and seeding functions
2746 __independent_bits_engine(_Engine& __e, size_t __w);
2747
2748 // generating functions
Howard Hinnantc003db12011-11-29 18:15:502749 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantf9d540b2010-05-26 17:49:342750
2751private:
Marshall Clow08bba642017-09-20 19:38:432752 result_type __eval(false_type);
2753 result_type __eval(true_type);
Howard Hinnantf9d540b2010-05-26 17:49:342754};
2755
2756template<class _Engine, class _UIntType>
2757__independent_bits_engine<_Engine, _UIntType>
2758 ::__independent_bits_engine(_Engine& __e, size_t __w)
2759 : __e_(__e),
2760 __w_(__w)
2761{
2762 __n_ = __w_ / __m + (__w_ % __m != 0);
2763 __w0_ = __w_ / __n_;
Howard Hinnantc003db12011-11-29 18:15:502764 if (_Rp == 0)
2765 __y0_ = _Rp;
Howard Hinnantf9d540b2010-05-26 17:49:342766 else if (__w0_ < _WDt)
Howard Hinnantc003db12011-11-29 18:15:502767 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantf9d540b2010-05-26 17:49:342768 else
2769 __y0_ = 0;
Howard Hinnantc003db12011-11-29 18:15:502770 if (_Rp - __y0_ > __y0_ / __n_)
Howard Hinnantf9d540b2010-05-26 17:49:342771 {
2772 ++__n_;
2773 __w0_ = __w_ / __n_;
2774 if (__w0_ < _WDt)
Howard Hinnantc003db12011-11-29 18:15:502775 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantf9d540b2010-05-26 17:49:342776 else
2777 __y0_ = 0;
2778 }
2779 __n0_ = __n_ - __w_ % __n_;
2780 if (__w0_ < _WDt - 1)
Howard Hinnantc003db12011-11-29 18:15:502781 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
Howard Hinnantf9d540b2010-05-26 17:49:342782 else
2783 __y1_ = 0;
2784 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2785 _Engine_result_type(0);
2786 __mask1_ = __w0_ < _EDt - 1 ?
2787 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2788 _Engine_result_type(~0);
Howard Hinnant3e519522010-05-11 19:42:162789}
2790
Howard Hinnantf9d540b2010-05-26 17:49:342791template<class _Engine, class _UIntType>
2792inline
2793_UIntType
Marshall Clow08bba642017-09-20 19:38:432794__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
Howard Hinnant3e519522010-05-11 19:42:162795{
Howard Hinnantf9d540b2010-05-26 17:49:342796 return static_cast<result_type>(__e_() & __mask0_);
Howard Hinnant3e519522010-05-11 19:42:162797}
2798
Howard Hinnantf9d540b2010-05-26 17:49:342799template<class _Engine, class _UIntType>
2800_UIntType
Marshall Clow08bba642017-09-20 19:38:432801__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
Howard Hinnant3e519522010-05-11 19:42:162802{
Marshall Clow5beb2c32017-09-20 17:34:112803 const size_t _WRt = numeric_limits<result_type>::digits;
Howard Hinnantc003db12011-11-29 18:15:502804 result_type _Sp = 0;
Howard Hinnantf9d540b2010-05-26 17:49:342805 for (size_t __k = 0; __k < __n0_; ++__k)
2806 {
2807 _Engine_result_type __u;
2808 do
2809 {
2810 __u = __e_() - _Engine::min();
2811 } while (__u >= __y0_);
Marshall Clow5beb2c32017-09-20 17:34:112812 if (__w0_ < _WRt)
Howard Hinnantc003db12011-11-29 18:15:502813 _Sp <<= __w0_;
Howard Hinnantf9d540b2010-05-26 17:49:342814 else
Howard Hinnantc003db12011-11-29 18:15:502815 _Sp = 0;
2816 _Sp += __u & __mask0_;
Howard Hinnantf9d540b2010-05-26 17:49:342817 }
2818 for (size_t __k = __n0_; __k < __n_; ++__k)
2819 {
2820 _Engine_result_type __u;
2821 do
2822 {
2823 __u = __e_() - _Engine::min();
2824 } while (__u >= __y1_);
Marshall Clow5beb2c32017-09-20 17:34:112825 if (__w0_ < _WRt - 1)
Howard Hinnantc003db12011-11-29 18:15:502826 _Sp <<= __w0_ + 1;
Howard Hinnantf9d540b2010-05-26 17:49:342827 else
Howard Hinnantc003db12011-11-29 18:15:502828 _Sp = 0;
2829 _Sp += __u & __mask1_;
Howard Hinnantf9d540b2010-05-26 17:49:342830 }
Howard Hinnantc003db12011-11-29 18:15:502831 return _Sp;
Howard Hinnantf9d540b2010-05-26 17:49:342832}
2833
2834// uniform_int_distribution
2835
2836template<class _IntType = int>
2837class uniform_int_distribution
2838{
2839public:
2840 // types
2841 typedef _IntType result_type;
2842
2843 class param_type
2844 {
2845 result_type __a_;
2846 result_type __b_;
2847 public:
2848 typedef uniform_int_distribution distribution_type;
2849
2850 explicit param_type(result_type __a = 0,
2851 result_type __b = numeric_limits<result_type>::max())
2852 : __a_(__a), __b_(__b) {}
2853
2854 result_type a() const {return __a_;}
2855 result_type b() const {return __b_;}
2856
2857 friend bool operator==(const param_type& __x, const param_type& __y)
2858 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2859 friend bool operator!=(const param_type& __x, const param_type& __y)
2860 {return !(__x == __y);}
2861 };
2862
2863private:
2864 param_type __p_;
2865
2866public:
2867 // constructors and reset functions
2868 explicit uniform_int_distribution(result_type __a = 0,
2869 result_type __b = numeric_limits<result_type>::max())
2870 : __p_(param_type(__a, __b)) {}
2871 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
2872 void reset() {}
2873
2874 // generating functions
2875 template<class _URNG> result_type operator()(_URNG& __g)
2876 {return (*this)(__g, __p_);}
2877 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2878
2879 // property functions
2880 result_type a() const {return __p_.a();}
2881 result_type b() const {return __p_.b();}
2882
2883 param_type param() const {return __p_;}
2884 void param(const param_type& __p) {__p_ = __p;}
2885
2886 result_type min() const {return a();}
2887 result_type max() const {return b();}
2888
2889 friend bool operator==(const uniform_int_distribution& __x,
2890 const uniform_int_distribution& __y)
2891 {return __x.__p_ == __y.__p_;}
2892 friend bool operator!=(const uniform_int_distribution& __x,
2893 const uniform_int_distribution& __y)
2894 {return !(__x == __y);}
2895};
2896
2897template<class _IntType>
2898template<class _URNG>
2899typename uniform_int_distribution<_IntType>::result_type
2900uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
2901{
2902 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
2903 uint32_t, uint64_t>::type _UIntType;
Howard Hinnantc003db12011-11-29 18:15:502904 const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
2905 if (_Rp == 1)
Howard Hinnantf9d540b2010-05-26 17:49:342906 return __p.a();
2907 const size_t _Dt = numeric_limits<_UIntType>::digits;
2908 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
Howard Hinnantc003db12011-11-29 18:15:502909 if (_Rp == 0)
Howard Hinnantf9d540b2010-05-26 17:49:342910 return static_cast<result_type>(_Eng(__g, _Dt)());
Howard Hinnantc003db12011-11-29 18:15:502911 size_t __w = _Dt - __clz(_Rp) - 1;
Marshall Clowa6438ca2015-07-30 18:26:342912 if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
Howard Hinnantf9d540b2010-05-26 17:49:342913 ++__w;
2914 _Eng __e(__g, __w);
2915 _UIntType __u;
Howard Hinnant3e519522010-05-11 19:42:162916 do
Howard Hinnantf9d540b2010-05-26 17:49:342917 {
2918 __u = __e();
Howard Hinnantc003db12011-11-29 18:15:502919 } while (__u >= _Rp);
Howard Hinnantf9d540b2010-05-26 17:49:342920 return static_cast<result_type>(__u + __p.a());
Howard Hinnant3e519522010-05-11 19:42:162921}
2922
Eric Fiselier66f1ec42017-04-03 23:23:442923#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \
2924 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnantf0544c22013-08-12 18:38:342925class _LIBCPP_TYPE_VIS __rs_default;
Howard Hinnant3e519522010-05-11 19:42:162926
Howard Hinnantf0544c22013-08-12 18:38:342927_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantf9d540b2010-05-26 17:49:342928
Howard Hinnantf0544c22013-08-12 18:38:342929class _LIBCPP_TYPE_VIS __rs_default
Howard Hinnant3e519522010-05-11 19:42:162930{
Howard Hinnantf9d540b2010-05-26 17:49:342931 static unsigned __c_;
2932
2933 __rs_default();
2934public:
Marshall Clowb6e5f852013-02-07 22:12:022935 typedef uint_fast32_t result_type;
Howard Hinnantf9d540b2010-05-26 17:49:342936
2937 static const result_type _Min = 0;
2938 static const result_type _Max = 0xFFFFFFFF;
2939
2940 __rs_default(const __rs_default&);
2941 ~__rs_default();
2942
2943 result_type operator()();
2944
Howard Hinnant788c9972012-04-02 00:40:412945 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
2946 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
Howard Hinnantf9d540b2010-05-26 17:49:342947
Howard Hinnantf0544c22013-08-12 18:38:342948 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnant3e519522010-05-11 19:42:162949};
2950
Howard Hinnantf0544c22013-08-12 18:38:342951_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnant3e519522010-05-11 19:42:162952
2953template <class _RandomAccessIterator>
2954void
2955random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
2956{
2957 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc003db12011-11-29 18:15:502958 typedef uniform_int_distribution<ptrdiff_t> _Dp;
2959 typedef typename _Dp::param_type _Pp;
Howard Hinnant3e519522010-05-11 19:42:162960 difference_type __d = __last - __first;
2961 if (__d > 1)
2962 {
Howard Hinnantc003db12011-11-29 18:15:502963 _Dp __uid;
Howard Hinnantf9d540b2010-05-26 17:49:342964 __rs_default __g = __rs_get();
2965 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant007b26b2010-10-22 15:26:392966 {
Howard Hinnantc003db12011-11-29 18:15:502967 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant007b26b2010-10-22 15:26:392968 if (__i != difference_type(0))
2969 swap(*__first, *(__first + __i));
2970 }
Howard Hinnant3e519522010-05-11 19:42:162971 }
2972}
2973
2974template <class _RandomAccessIterator, class _RandomNumberGenerator>
2975void
2976random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselierddda4562017-04-18 23:26:472977#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:162978 _RandomNumberGenerator&& __rand)
2979#else
2980 _RandomNumberGenerator& __rand)
2981#endif
2982{
2983 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2984 difference_type __d = __last - __first;
2985 if (__d > 1)
2986 {
2987 for (--__last; __first < __last; ++__first, --__d)
Howard Hinnant007b26b2010-10-22 15:26:392988 {
2989 difference_type __i = __rand(__d);
Marshall Clowe9cc5452018-09-11 18:33:452990 if (__i != difference_type(0))
2991 swap(*__first, *(__first + __i));
Howard Hinnant007b26b2010-10-22 15:26:392992 }
Howard Hinnant3e519522010-05-11 19:42:162993 }
2994}
Marshall Clow0f37a412017-03-23 13:43:372995#endif
Howard Hinnant3e519522010-05-11 19:42:162996
Eric Fiseliere7154702016-08-28 22:14:372997template <class _PopulationIterator, class _SampleIterator, class _Distance,
2998 class _UniformRandomNumberGenerator>
2999_LIBCPP_INLINE_VISIBILITY
3000_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardson42bfedd2017-11-14 11:14:253001 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiseliere7154702016-08-28 22:14:373002 _Distance __n,
3003 _UniformRandomNumberGenerator & __g,
3004 input_iterator_tag) {
3005
3006 _Distance __k = 0;
3007 for (; __first != __last && __k < __n; ++__first, (void)++__k)
Alexander Richardson42bfedd2017-11-14 11:14:253008 __output_iter[__k] = *__first;
Eric Fiseliere7154702016-08-28 22:14:373009 _Distance __sz = __k;
3010 for (; __first != __last; ++__first, (void)++__k) {
3011 _Distance __r = _VSTD::uniform_int_distribution<_Distance>(0, __k)(__g);
3012 if (__r < __sz)
Alexander Richardson42bfedd2017-11-14 11:14:253013 __output_iter[__r] = *__first;
Eric Fiseliere7154702016-08-28 22:14:373014 }
Alexander Richardson42bfedd2017-11-14 11:14:253015 return __output_iter + _VSTD::min(__n, __k);
Eric Fiseliere7154702016-08-28 22:14:373016}
3017
3018template <class _PopulationIterator, class _SampleIterator, class _Distance,
3019 class _UniformRandomNumberGenerator>
3020_LIBCPP_INLINE_VISIBILITY
3021_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardson42bfedd2017-11-14 11:14:253022 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiseliere7154702016-08-28 22:14:373023 _Distance __n,
3024 _UniformRandomNumberGenerator& __g,
3025 forward_iterator_tag) {
3026 _Distance __unsampled_sz = _VSTD::distance(__first, __last);
3027 for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
3028 _Distance __r =
3029 _VSTD::uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
3030 if (__r < __n) {
Alexander Richardson42bfedd2017-11-14 11:14:253031 *__output_iter++ = *__first;
Eric Fiseliere7154702016-08-28 22:14:373032 --__n;
3033 }
3034 }
Alexander Richardson42bfedd2017-11-14 11:14:253035 return __output_iter;
Eric Fiseliere7154702016-08-28 22:14:373036}
3037
3038template <class _PopulationIterator, class _SampleIterator, class _Distance,
3039 class _UniformRandomNumberGenerator>
3040_LIBCPP_INLINE_VISIBILITY
3041_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardson42bfedd2017-11-14 11:14:253042 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiseliere7154702016-08-28 22:14:373043 _Distance __n, _UniformRandomNumberGenerator& __g) {
3044 typedef typename iterator_traits<_PopulationIterator>::iterator_category
3045 _PopCategory;
3046 typedef typename iterator_traits<_PopulationIterator>::difference_type
3047 _Difference;
3048 static_assert(__is_forward_iterator<_PopulationIterator>::value ||
3049 __is_random_access_iterator<_SampleIterator>::value,
3050 "SampleIterator must meet the requirements of RandomAccessIterator");
3051 typedef typename common_type<_Distance, _Difference>::type _CommonType;
3052 _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
3053 return _VSTD::__sample(
Alexander Richardson42bfedd2017-11-14 11:14:253054 __first, __last, __output_iter, _CommonType(__n),
Eric Fiseliere7154702016-08-28 22:14:373055 __g, _PopCategory());
3056}
3057
3058#if _LIBCPP_STD_VER > 14
3059template <class _PopulationIterator, class _SampleIterator, class _Distance,
3060 class _UniformRandomNumberGenerator>
3061inline _LIBCPP_INLINE_VISIBILITY
3062_SampleIterator sample(_PopulationIterator __first,
Alexander Richardson42bfedd2017-11-14 11:14:253063 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiseliere7154702016-08-28 22:14:373064 _Distance __n, _UniformRandomNumberGenerator&& __g) {
Alexander Richardson42bfedd2017-11-14 11:14:253065 return _VSTD::__sample(__first, __last, __output_iter, __n, __g);
Eric Fiseliere7154702016-08-28 22:14:373066}
3067#endif // _LIBCPP_STD_VER > 14
3068
Howard Hinnantf9d540b2010-05-26 17:49:343069template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3070 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselierddda4562017-04-18 23:26:473071#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantfb340102010-11-18 01:47:023072 _UniformRandomNumberGenerator&& __g)
3073#else
Howard Hinnantf9d540b2010-05-26 17:49:343074 _UniformRandomNumberGenerator& __g)
Howard Hinnantfb340102010-11-18 01:47:023075#endif
Howard Hinnantf9d540b2010-05-26 17:49:343076{
3077 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc003db12011-11-29 18:15:503078 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3079 typedef typename _Dp::param_type _Pp;
Howard Hinnantf9d540b2010-05-26 17:49:343080 difference_type __d = __last - __first;
3081 if (__d > 1)
3082 {
Howard Hinnantc003db12011-11-29 18:15:503083 _Dp __uid;
Howard Hinnantf9d540b2010-05-26 17:49:343084 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnant007b26b2010-10-22 15:26:393085 {
Howard Hinnantc003db12011-11-29 18:15:503086 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant007b26b2010-10-22 15:26:393087 if (__i != difference_type(0))
3088 swap(*__first, *(__first + __i));
3089 }
Howard Hinnantf9d540b2010-05-26 17:49:343090 }
3091}
3092
Howard Hinnant3e519522010-05-11 19:42:163093template <class _InputIterator, class _Predicate>
Marshall Clow49c76432018-01-15 16:16:323094_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnant3e519522010-05-11 19:42:163095is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3096{
3097 for (; __first != __last; ++__first)
3098 if (!__pred(*__first))
3099 break;
Marshall Clowb9595b72015-02-02 18:16:353100 if ( __first == __last )
3101 return true;
3102 ++__first;
Howard Hinnant3e519522010-05-11 19:42:163103 for (; __first != __last; ++__first)
3104 if (__pred(*__first))
3105 return false;
3106 return true;
3107}
3108
3109// partition
3110
3111template <class _Predicate, class _ForwardIterator>
3112_ForwardIterator
3113__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3114{
3115 while (true)
3116 {
3117 if (__first == __last)
3118 return __first;
3119 if (!__pred(*__first))
3120 break;
3121 ++__first;
3122 }
3123 for (_ForwardIterator __p = __first; ++__p != __last;)
3124 {
3125 if (__pred(*__p))
3126 {
3127 swap(*__first, *__p);
3128 ++__first;
3129 }
3130 }
3131 return __first;
3132}
3133
3134template <class _Predicate, class _BidirectionalIterator>
3135_BidirectionalIterator
3136__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3137 bidirectional_iterator_tag)
3138{
3139 while (true)
3140 {
3141 while (true)
3142 {
3143 if (__first == __last)
3144 return __first;
3145 if (!__pred(*__first))
3146 break;
3147 ++__first;
3148 }
3149 do
3150 {
3151 if (__first == --__last)
3152 return __first;
3153 } while (!__pred(*__last));
3154 swap(*__first, *__last);
3155 ++__first;
3156 }
3157}
3158
3159template <class _ForwardIterator, class _Predicate>
3160inline _LIBCPP_INLINE_VISIBILITY
3161_ForwardIterator
3162partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3163{
Howard Hinnantce48a112011-06-30 21:18:193164 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnant3e519522010-05-11 19:42:163165 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3166}
3167
3168// partition_copy
3169
3170template <class _InputIterator, class _OutputIterator1,
3171 class _OutputIterator2, class _Predicate>
Marshall Clow1b9a4ff2018-01-22 20:44:333172_LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_OutputIterator1, _OutputIterator2>
Howard Hinnant3e519522010-05-11 19:42:163173partition_copy(_InputIterator __first, _InputIterator __last,
3174 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3175 _Predicate __pred)
3176{
3177 for (; __first != __last; ++__first)
3178 {
3179 if (__pred(*__first))
3180 {
3181 *__out_true = *__first;
3182 ++__out_true;
3183 }
3184 else
3185 {
3186 *__out_false = *__first;
3187 ++__out_false;
3188 }
3189 }
3190 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3191}
3192
3193// partition_point
3194
3195template<class _ForwardIterator, class _Predicate>
Marshall Clow674f9122018-01-15 17:53:343196_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:163197partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3198{
3199 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantce48a112011-06-30 21:18:193200 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:163201 while (__len != 0)
3202 {
3203 difference_type __l2 = __len / 2;
3204 _ForwardIterator __m = __first;
Howard Hinnantce48a112011-06-30 21:18:193205 _VSTD::advance(__m, __l2);
Howard Hinnant3e519522010-05-11 19:42:163206 if (__pred(*__m))
3207 {
3208 __first = ++__m;
3209 __len -= __l2 + 1;
3210 }
3211 else
3212 __len = __l2;
3213 }
3214 return __first;
3215}
3216
3217// stable_partition
3218
3219template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3220_ForwardIterator
3221__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3222 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3223{
3224 // *__first is known to be false
3225 // __len >= 1
3226 if (__len == 1)
3227 return __first;
3228 if (__len == 2)
3229 {
3230 _ForwardIterator __m = __first;
3231 if (__pred(*++__m))
3232 {
3233 swap(*__first, *__m);
3234 return __m;
3235 }
3236 return __first;
3237 }
3238 if (__len <= __p.second)
3239 { // The buffer is big enough to use
3240 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3241 __destruct_n __d(0);
3242 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3243 // Move the falses into the temporary buffer, and the trues to the front of the line
3244 // Update __first to always point to the end of the trues
3245 value_type* __t = __p.first;
Howard Hinnantce48a112011-06-30 21:18:193246 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnant3e519522010-05-11 19:42:163247 __d.__incr((value_type*)0);
3248 ++__t;
3249 _ForwardIterator __i = __first;
3250 while (++__i != __last)
3251 {
3252 if (__pred(*__i))
3253 {
Howard Hinnantce48a112011-06-30 21:18:193254 *__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:163255 ++__first;
3256 }
3257 else
3258 {
Howard Hinnantce48a112011-06-30 21:18:193259 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnant3e519522010-05-11 19:42:163260 __d.__incr((value_type*)0);
3261 ++__t;
3262 }
3263 }
3264 // All trues now at start of range, all falses in buffer
3265 // Move falses back into range, but don't mess up __first which points to first false
3266 __i = __first;
3267 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnantce48a112011-06-30 21:18:193268 *__i = _VSTD::move(*__t2);
Howard Hinnant3e519522010-05-11 19:42:163269 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3270 return __first;
3271 }
3272 // Else not enough buffer, do in place
3273 // __len >= 3
3274 _ForwardIterator __m = __first;
3275 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnantce48a112011-06-30 21:18:193276 _VSTD::advance(__m, __len2);
Howard Hinnant3e519522010-05-11 19:42:163277 // recurse on [__first, __m), *__first know to be false
3278 // F?????????????????
3279 // f m l
3280 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3281 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3282 // TTTFFFFF??????????
3283 // f ff m l
3284 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3285 _ForwardIterator __m1 = __m;
3286 _ForwardIterator __second_false = __last;
3287 _Distance __len_half = __len - __len2;
3288 while (__pred(*__m1))
3289 {
3290 if (++__m1 == __last)
3291 goto __second_half_done;
3292 --__len_half;
3293 }
3294 // TTTFFFFFTTTF??????
3295 // f ff m m1 l
3296 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3297__second_half_done:
3298 // TTTFFFFFTTTTTFFFFF
3299 // f ff m sf l
Howard Hinnantce48a112011-06-30 21:18:193300 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnant3e519522010-05-11 19:42:163301 // TTTTTTTTFFFFFFFFFF
3302 // |
3303}
3304
3305struct __return_temporary_buffer
3306{
3307 template <class _Tp>
Howard Hinnantce48a112011-06-30 21:18:193308 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
Howard Hinnant3e519522010-05-11 19:42:163309};
3310
3311template <class _Predicate, class _ForwardIterator>
3312_ForwardIterator
3313__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3314 forward_iterator_tag)
3315{
3316 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3317 // Either prove all true and return __first or point to first false
3318 while (true)
3319 {
3320 if (__first == __last)
3321 return __first;
3322 if (!__pred(*__first))
3323 break;
3324 ++__first;
3325 }
3326 // We now have a reduced range [__first, __last)
3327 // *__first is known to be false
3328 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3329 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnantce48a112011-06-30 21:18:193330 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:163331 pair<value_type*, ptrdiff_t> __p(0, 0);
3332 unique_ptr<value_type, __return_temporary_buffer> __h;
3333 if (__len >= __alloc_limit)
3334 {
Howard Hinnantce48a112011-06-30 21:18:193335 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnant3e519522010-05-11 19:42:163336 __h.reset(__p.first);
3337 }
3338 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3339 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3340}
3341
3342template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3343_BidirectionalIterator
3344__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3345 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3346{
3347 // *__first is known to be false
3348 // *__last is known to be true
3349 // __len >= 2
3350 if (__len == 2)
3351 {
3352 swap(*__first, *__last);
3353 return __last;
3354 }
3355 if (__len == 3)
3356 {
3357 _BidirectionalIterator __m = __first;
3358 if (__pred(*++__m))
3359 {
3360 swap(*__first, *__m);
3361 swap(*__m, *__last);
3362 return __last;
3363 }
3364 swap(*__m, *__last);
3365 swap(*__first, *__m);
3366 return __m;
3367 }
3368 if (__len <= __p.second)
3369 { // The buffer is big enough to use
3370 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3371 __destruct_n __d(0);
3372 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3373 // Move the falses into the temporary buffer, and the trues to the front of the line
3374 // Update __first to always point to the end of the trues
3375 value_type* __t = __p.first;
Howard Hinnantce48a112011-06-30 21:18:193376 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnant3e519522010-05-11 19:42:163377 __d.__incr((value_type*)0);
3378 ++__t;
3379 _BidirectionalIterator __i = __first;
3380 while (++__i != __last)
3381 {
3382 if (__pred(*__i))
3383 {
Howard Hinnantce48a112011-06-30 21:18:193384 *__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:163385 ++__first;
3386 }
3387 else
3388 {
Howard Hinnantce48a112011-06-30 21:18:193389 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnant3e519522010-05-11 19:42:163390 __d.__incr((value_type*)0);
3391 ++__t;
3392 }
3393 }
3394 // move *__last, known to be true
Howard Hinnantce48a112011-06-30 21:18:193395 *__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:163396 __i = ++__first;
3397 // All trues now at start of range, all falses in buffer
3398 // Move falses back into range, but don't mess up __first which points to first false
3399 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnantce48a112011-06-30 21:18:193400 *__i = _VSTD::move(*__t2);
Howard Hinnant3e519522010-05-11 19:42:163401 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3402 return __first;
3403 }
3404 // Else not enough buffer, do in place
3405 // __len >= 4
3406 _BidirectionalIterator __m = __first;
3407 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnantce48a112011-06-30 21:18:193408 _VSTD::advance(__m, __len2);
Howard Hinnant3e519522010-05-11 19:42:163409 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3410 // F????????????????T
3411 // f m l
3412 _BidirectionalIterator __m1 = __m;
3413 _BidirectionalIterator __first_false = __first;
3414 _Distance __len_half = __len2;
3415 while (!__pred(*--__m1))
3416 {
3417 if (__m1 == __first)
3418 goto __first_half_done;
3419 --__len_half;
3420 }
3421 // F???TFFF?????????T
3422 // f m1 m l
3423 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3424 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3425__first_half_done:
3426 // TTTFFFFF?????????T
3427 // f ff m l
3428 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3429 __m1 = __m;
3430 _BidirectionalIterator __second_false = __last;
3431 ++__second_false;
3432 __len_half = __len - __len2;
3433 while (__pred(*__m1))
3434 {
3435 if (++__m1 == __last)
3436 goto __second_half_done;
3437 --__len_half;
3438 }
3439 // TTTFFFFFTTTF?????T
3440 // f ff m m1 l
3441 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3442__second_half_done:
3443 // TTTFFFFFTTTTTFFFFF
3444 // f ff m sf l
Howard Hinnantce48a112011-06-30 21:18:193445 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnant3e519522010-05-11 19:42:163446 // TTTTTTTTFFFFFFFFFF
3447 // |
3448}
3449
3450template <class _Predicate, class _BidirectionalIterator>
3451_BidirectionalIterator
3452__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3453 bidirectional_iterator_tag)
3454{
3455 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3456 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3457 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3458 // Either prove all true and return __first or point to first false
3459 while (true)
3460 {
3461 if (__first == __last)
3462 return __first;
3463 if (!__pred(*__first))
3464 break;
3465 ++__first;
3466 }
3467 // __first points to first false, everything prior to __first is already set.
3468 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3469 do
3470 {
3471 if (__first == --__last)
3472 return __first;
3473 } while (!__pred(*__last));
3474 // We now have a reduced range [__first, __last]
3475 // *__first is known to be false
3476 // *__last is known to be true
3477 // __len >= 2
Howard Hinnantce48a112011-06-30 21:18:193478 difference_type __len = _VSTD::distance(__first, __last) + 1;
Howard Hinnant3e519522010-05-11 19:42:163479 pair<value_type*, ptrdiff_t> __p(0, 0);
3480 unique_ptr<value_type, __return_temporary_buffer> __h;
3481 if (__len >= __alloc_limit)
3482 {
Howard Hinnantce48a112011-06-30 21:18:193483 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnant3e519522010-05-11 19:42:163484 __h.reset(__p.first);
3485 }
3486 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3487 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3488}
3489
3490template <class _ForwardIterator, class _Predicate>
3491inline _LIBCPP_INLINE_VISIBILITY
3492_ForwardIterator
3493stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3494{
3495 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3496 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3497}
3498
3499// is_sorted_until
3500
3501template <class _ForwardIterator, class _Compare>
Marshall Clow49c76432018-01-15 16:16:323502_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:163503is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3504{
3505 if (__first != __last)
3506 {
3507 _ForwardIterator __i = __first;
3508 while (++__i != __last)
3509 {
3510 if (__comp(*__i, *__first))
3511 return __i;
3512 __first = __i;
3513 }
3514 }
3515 return __last;
3516}
3517
Howard Hinnantb3371f62010-08-22 00:02:433518template<class _ForwardIterator>
Marshall Clow49c76432018-01-15 16:16:323519inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:163520_ForwardIterator
3521is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3522{
Howard Hinnantce48a112011-06-30 21:18:193523 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:163524}
3525
3526// is_sorted
3527
3528template <class _ForwardIterator, class _Compare>
Marshall Clow49c76432018-01-15 16:16:323529inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:163530bool
3531is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3532{
Howard Hinnantce48a112011-06-30 21:18:193533 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
Howard Hinnant3e519522010-05-11 19:42:163534}
3535
Howard Hinnantb3371f62010-08-22 00:02:433536template<class _ForwardIterator>
Marshall Clow49c76432018-01-15 16:16:323537inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:163538bool
3539is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3540{
Howard Hinnantce48a112011-06-30 21:18:193541 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:163542}
3543
3544// sort
3545
3546// stable, 2-3 compares, 0-2 swaps
3547
3548template <class _Compare, class _ForwardIterator>
3549unsigned
3550__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3551{
3552 unsigned __r = 0;
3553 if (!__c(*__y, *__x)) // if x <= y
3554 {
3555 if (!__c(*__z, *__y)) // if y <= z
3556 return __r; // x <= y && y <= z
3557 // x <= y && y > z
3558 swap(*__y, *__z); // x <= z && y < z
3559 __r = 1;
3560 if (__c(*__y, *__x)) // if x > y
3561 {
3562 swap(*__x, *__y); // x < y && y <= z
3563 __r = 2;
3564 }
3565 return __r; // x <= y && y < z
3566 }
3567 if (__c(*__z, *__y)) // x > y, if y > z
3568 {
3569 swap(*__x, *__z); // x < y && y < z
3570 __r = 1;
3571 return __r;
3572 }
3573 swap(*__x, *__y); // x > y && y <= z
3574 __r = 1; // x < y && x <= z
3575 if (__c(*__z, *__y)) // if y > z
3576 {
3577 swap(*__y, *__z); // x <= y && y < z
3578 __r = 2;
3579 }
3580 return __r;
3581} // x <= y && y <= z
3582
3583// stable, 3-6 compares, 0-5 swaps
3584
3585template <class _Compare, class _ForwardIterator>
3586unsigned
3587__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3588 _ForwardIterator __x4, _Compare __c)
3589{
3590 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3591 if (__c(*__x4, *__x3))
3592 {
3593 swap(*__x3, *__x4);
3594 ++__r;
3595 if (__c(*__x3, *__x2))
3596 {
3597 swap(*__x2, *__x3);
3598 ++__r;
3599 if (__c(*__x2, *__x1))
3600 {
3601 swap(*__x1, *__x2);
3602 ++__r;
3603 }
3604 }
3605 }
3606 return __r;
3607}
3608
3609// stable, 4-10 compares, 0-9 swaps
3610
3611template <class _Compare, class _ForwardIterator>
3612unsigned
3613__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3614 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3615{
3616 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3617 if (__c(*__x5, *__x4))
3618 {
3619 swap(*__x4, *__x5);
3620 ++__r;
3621 if (__c(*__x4, *__x3))
3622 {
3623 swap(*__x3, *__x4);
3624 ++__r;
3625 if (__c(*__x3, *__x2))
3626 {
3627 swap(*__x2, *__x3);
3628 ++__r;
3629 if (__c(*__x2, *__x1))
3630 {
3631 swap(*__x1, *__x2);
3632 ++__r;
3633 }
3634 }
3635 }
3636 }
3637 return __r;
3638}
3639
3640// Assumes size > 0
3641template <class _Compare, class _BirdirectionalIterator>
3642void
3643__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3644{
3645 _BirdirectionalIterator __lm1 = __last;
3646 for (--__lm1; __first != __lm1; ++__first)
3647 {
Howard Hinnantce48a112011-06-30 21:18:193648 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
Howard Hinnant3e519522010-05-11 19:42:163649 typename add_lvalue_reference<_Compare>::type>
3650 (__first, __last, __comp);
3651 if (__i != __first)
3652 swap(*__first, *__i);
3653 }
3654}
3655
3656template <class _Compare, class _BirdirectionalIterator>
3657void
3658__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3659{
3660 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3661 if (__first != __last)
3662 {
3663 _BirdirectionalIterator __i = __first;
3664 for (++__i; __i != __last; ++__i)
3665 {
3666 _BirdirectionalIterator __j = __i;
Howard Hinnantce48a112011-06-30 21:18:193667 value_type __t(_VSTD::move(*__j));
Howard Hinnant3e519522010-05-11 19:42:163668 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
Howard Hinnantce48a112011-06-30 21:18:193669 *__j = _VSTD::move(*__k);
3670 *__j = _VSTD::move(__t);
Howard Hinnant3e519522010-05-11 19:42:163671 }
3672 }
3673}
3674
3675template <class _Compare, class _RandomAccessIterator>
3676void
3677__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3678{
3679 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3680 _RandomAccessIterator __j = __first+2;
3681 __sort3<_Compare>(__first, __first+1, __j, __comp);
3682 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3683 {
3684 if (__comp(*__i, *__j))
3685 {
Howard Hinnantce48a112011-06-30 21:18:193686 value_type __t(_VSTD::move(*__i));
Howard Hinnant3e519522010-05-11 19:42:163687 _RandomAccessIterator __k = __j;
3688 __j = __i;
3689 do
3690 {
Howard Hinnantce48a112011-06-30 21:18:193691 *__j = _VSTD::move(*__k);
Howard Hinnant3e519522010-05-11 19:42:163692 __j = __k;
3693 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnantce48a112011-06-30 21:18:193694 *__j = _VSTD::move(__t);
Howard Hinnant3e519522010-05-11 19:42:163695 }
3696 __j = __i;
3697 }
3698}
3699
3700template <class _Compare, class _RandomAccessIterator>
3701bool
3702__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3703{
3704 switch (__last - __first)
3705 {
3706 case 0:
3707 case 1:
3708 return true;
3709 case 2:
3710 if (__comp(*--__last, *__first))
3711 swap(*__first, *__last);
3712 return true;
3713 case 3:
Howard Hinnantce48a112011-06-30 21:18:193714 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163715 return true;
3716 case 4:
Howard Hinnantce48a112011-06-30 21:18:193717 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163718 return true;
3719 case 5:
Howard Hinnantce48a112011-06-30 21:18:193720 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163721 return true;
3722 }
3723 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3724 _RandomAccessIterator __j = __first+2;
3725 __sort3<_Compare>(__first, __first+1, __j, __comp);
3726 const unsigned __limit = 8;
3727 unsigned __count = 0;
3728 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3729 {
3730 if (__comp(*__i, *__j))
3731 {
Howard Hinnantce48a112011-06-30 21:18:193732 value_type __t(_VSTD::move(*__i));
Howard Hinnant3e519522010-05-11 19:42:163733 _RandomAccessIterator __k = __j;
3734 __j = __i;
3735 do
3736 {
Howard Hinnantce48a112011-06-30 21:18:193737 *__j = _VSTD::move(*__k);
Howard Hinnant3e519522010-05-11 19:42:163738 __j = __k;
3739 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnantce48a112011-06-30 21:18:193740 *__j = _VSTD::move(__t);
Howard Hinnant3e519522010-05-11 19:42:163741 if (++__count == __limit)
3742 return ++__i == __last;
3743 }
3744 __j = __i;
3745 }
3746 return true;
3747}
3748
3749template <class _Compare, class _BirdirectionalIterator>
3750void
3751__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3752 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3753{
3754 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3755 if (__first1 != __last1)
3756 {
3757 __destruct_n __d(0);
3758 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3759 value_type* __last2 = __first2;
Howard Hinnantce48a112011-06-30 21:18:193760 ::new(__last2) value_type(_VSTD::move(*__first1));
Howard Hinnant3e519522010-05-11 19:42:163761 __d.__incr((value_type*)0);
3762 for (++__last2; ++__first1 != __last1; ++__last2)
3763 {
3764 value_type* __j2 = __last2;
3765 value_type* __i2 = __j2;
3766 if (__comp(*__first1, *--__i2))
3767 {
Howard Hinnantce48a112011-06-30 21:18:193768 ::new(__j2) value_type(_VSTD::move(*__i2));
Howard Hinnant3e519522010-05-11 19:42:163769 __d.__incr((value_type*)0);
3770 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
Howard Hinnantce48a112011-06-30 21:18:193771 *__j2 = _VSTD::move(*__i2);
3772 *__j2 = _VSTD::move(*__first1);
Howard Hinnant3e519522010-05-11 19:42:163773 }
3774 else
3775 {
Howard Hinnantce48a112011-06-30 21:18:193776 ::new(__j2) value_type(_VSTD::move(*__first1));
Howard Hinnant3e519522010-05-11 19:42:163777 __d.__incr((value_type*)0);
3778 }
3779 }
3780 __h.release();
3781 }
3782}
3783
3784template <class _Compare, class _RandomAccessIterator>
3785void
3786__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3787{
3788 // _Compare is known to be a reference type
3789 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3790 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnantca740482010-11-19 22:17:283791 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3792 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
Howard Hinnant3e519522010-05-11 19:42:163793 while (true)
3794 {
3795 __restart:
3796 difference_type __len = __last - __first;
3797 switch (__len)
3798 {
3799 case 0:
3800 case 1:
3801 return;
3802 case 2:
3803 if (__comp(*--__last, *__first))
3804 swap(*__first, *__last);
3805 return;
3806 case 3:
Howard Hinnantce48a112011-06-30 21:18:193807 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163808 return;
3809 case 4:
Howard Hinnantce48a112011-06-30 21:18:193810 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163811 return;
3812 case 5:
Howard Hinnantce48a112011-06-30 21:18:193813 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163814 return;
3815 }
3816 if (__len <= __limit)
3817 {
Howard Hinnantce48a112011-06-30 21:18:193818 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163819 return;
3820 }
3821 // __len > 5
3822 _RandomAccessIterator __m = __first;
3823 _RandomAccessIterator __lm1 = __last;
3824 --__lm1;
3825 unsigned __n_swaps;
3826 {
3827 difference_type __delta;
3828 if (__len >= 1000)
3829 {
3830 __delta = __len/2;
3831 __m += __delta;
3832 __delta /= 2;
Howard Hinnantce48a112011-06-30 21:18:193833 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
Howard Hinnant3e519522010-05-11 19:42:163834 }
3835 else
3836 {
3837 __delta = __len/2;
3838 __m += __delta;
Howard Hinnantce48a112011-06-30 21:18:193839 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
Howard Hinnant3e519522010-05-11 19:42:163840 }
3841 }
3842 // *__m is median
3843 // partition [__first, __m) < *__m and *__m <= [__m, __last)
3844 // (this inhibits tossing elements equivalent to __m around unnecessarily)
3845 _RandomAccessIterator __i = __first;
3846 _RandomAccessIterator __j = __lm1;
3847 // j points beyond range to be tested, *__m is known to be <= *__lm1
3848 // The search going up is known to be guarded but the search coming down isn't.
3849 // Prime the downward search with a guard.
3850 if (!__comp(*__i, *__m)) // if *__first == *__m
3851 {
3852 // *__first == *__m, *__first doesn't go in first part
3853 // manually guard downward moving __j against __i
3854 while (true)
3855 {
3856 if (__i == --__j)
3857 {
3858 // *__first == *__m, *__m <= all other elements
3859 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
3860 ++__i; // __first + 1
3861 __j = __last;
3862 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
3863 {
3864 while (true)
3865 {
3866 if (__i == __j)
3867 return; // [__first, __last) all equivalent elements
3868 if (__comp(*__first, *__i))
3869 {
3870 swap(*__i, *__j);
3871 ++__n_swaps;
3872 ++__i;
3873 break;
3874 }
3875 ++__i;
3876 }
3877 }
3878 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
3879 if (__i == __j)
3880 return;
3881 while (true)
3882 {
3883 while (!__comp(*__first, *__i))
3884 ++__i;
3885 while (__comp(*__first, *--__j))
3886 ;
3887 if (__i >= __j)
3888 break;
3889 swap(*__i, *__j);
3890 ++__n_swaps;
3891 ++__i;
3892 }
3893 // [__first, __i) == *__first and *__first < [__i, __last)
3894 // The first part is sorted, sort the secod part
Howard Hinnantce48a112011-06-30 21:18:193895 // _VSTD::__sort<_Compare>(__i, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163896 __first = __i;
3897 goto __restart;
3898 }
3899 if (__comp(*__j, *__m))
3900 {
3901 swap(*__i, *__j);
3902 ++__n_swaps;
3903 break; // found guard for downward moving __j, now use unguarded partition
3904 }
3905 }
3906 }
3907 // It is known that *__i < *__m
3908 ++__i;
3909 // j points beyond range to be tested, *__m is known to be <= *__lm1
3910 // if not yet partitioned...
3911 if (__i < __j)
3912 {
3913 // known that *(__i - 1) < *__m
3914 // known that __i <= __m
3915 while (true)
3916 {
3917 // __m still guards upward moving __i
3918 while (__comp(*__i, *__m))
3919 ++__i;
3920 // It is now known that a guard exists for downward moving __j
3921 while (!__comp(*--__j, *__m))
3922 ;
3923 if (__i > __j)
3924 break;
3925 swap(*__i, *__j);
3926 ++__n_swaps;
3927 // It is known that __m != __j
3928 // If __m just moved, follow it
3929 if (__m == __i)
3930 __m = __j;
3931 ++__i;
3932 }
3933 }
3934 // [__first, __i) < *__m and *__m <= [__i, __last)
3935 if (__i != __m && __comp(*__m, *__i))
3936 {
3937 swap(*__i, *__m);
3938 ++__n_swaps;
3939 }
3940 // [__first, __i) < *__i and *__i <= [__i+1, __last)
3941 // If we were given a perfect partition, see if insertion sort is quick...
3942 if (__n_swaps == 0)
3943 {
Howard Hinnantce48a112011-06-30 21:18:193944 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
3945 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
Howard Hinnant3e519522010-05-11 19:42:163946 {
3947 if (__fs)
3948 return;
3949 __last = __i;
3950 continue;
3951 }
3952 else
3953 {
3954 if (__fs)
3955 {
3956 __first = ++__i;
3957 continue;
3958 }
3959 }
3960 }
3961 // sort smaller range with recursive call and larger with tail recursion elimination
3962 if (__i - __first < __last - __i)
3963 {
Howard Hinnantce48a112011-06-30 21:18:193964 _VSTD::__sort<_Compare>(__first, __i, __comp);
3965 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163966 __first = ++__i;
3967 }
3968 else
3969 {
Howard Hinnantce48a112011-06-30 21:18:193970 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
3971 // _VSTD::__sort<_Compare>(__first, __i, __comp);
Howard Hinnant3e519522010-05-11 19:42:163972 __last = __i;
3973 }
3974 }
3975}
3976
3977// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
3978template <class _RandomAccessIterator, class _Compare>
3979inline _LIBCPP_INLINE_VISIBILITY
3980void
3981sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3982{
Howard Hinnant145afa12013-08-23 20:10:183983#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:163984 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3985 __debug_less<_Compare> __c(__comp);
3986 __sort<_Comp_ref>(__first, __last, __c);
Howard Hinnant145afa12013-08-23 20:10:183987#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:163988 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3989 __sort<_Comp_ref>(__first, __last, __comp);
Howard Hinnant145afa12013-08-23 20:10:183990#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:163991}
3992
3993template <class _RandomAccessIterator>
3994inline _LIBCPP_INLINE_VISIBILITY
3995void
3996sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
3997{
Howard Hinnantce48a112011-06-30 21:18:193998 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:163999}
4000
4001template <class _Tp>
4002inline _LIBCPP_INLINE_VISIBILITY
4003void
4004sort(_Tp** __first, _Tp** __last)
4005{
Howard Hinnantce48a112011-06-30 21:18:194006 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
Howard Hinnant3e519522010-05-11 19:42:164007}
4008
4009template <class _Tp>
4010inline _LIBCPP_INLINE_VISIBILITY
4011void
4012sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4013{
Howard Hinnantce48a112011-06-30 21:18:194014 _VSTD::sort(__first.base(), __last.base());
Howard Hinnant3e519522010-05-11 19:42:164015}
4016
Howard Hinnantf554add2011-09-14 18:33:514017template <class _Tp, class _Compare>
4018inline _LIBCPP_INLINE_VISIBILITY
4019void
4020sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4021{
4022 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4023 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4024}
4025
Howard Hinnantf0544c22013-08-12 18:38:344026_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4027_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4028_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4029_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4030_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4031_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4032_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4033_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4034_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4035_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4036_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4037_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>&))
4038_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4039_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4040_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:164041
Howard Hinnantf0544c22013-08-12 18:38:344042_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4043_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4044_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4045_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4046_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4047_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4048_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4049_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4050_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4051_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4052_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4053_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>&))
4054_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4055_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4056_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:164057
Howard Hinnantf0544c22013-08-12 18:38:344058_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:164059
4060// lower_bound
4061
4062template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414063_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454064__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164065{
4066 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantce48a112011-06-30 21:18:194067 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:164068 while (__len != 0)
4069 {
4070 difference_type __l2 = __len / 2;
4071 _ForwardIterator __m = __first;
Howard Hinnantce48a112011-06-30 21:18:194072 _VSTD::advance(__m, __l2);
Howard Hinnante4383372011-10-22 20:59:454073 if (__comp(*__m, __value_))
Howard Hinnant3e519522010-05-11 19:42:164074 {
4075 __first = ++__m;
4076 __len -= __l2 + 1;
4077 }
4078 else
4079 __len = __l2;
4080 }
4081 return __first;
4082}
4083
4084template <class _ForwardIterator, class _Tp, class _Compare>
Marshall Clowd57c03d2018-01-16 02:34:414085inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164086_ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454087lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164088{
Howard Hinnant145afa12013-08-23 20:10:184089#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164090 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4091 __debug_less<_Compare> __c(__comp);
Howard Hinnante4383372011-10-22 20:59:454092 return __lower_bound<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant145afa12013-08-23 20:10:184093#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164094 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnante4383372011-10-22 20:59:454095 return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant145afa12013-08-23 20:10:184096#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164097}
4098
4099template <class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414100inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164101_ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454102lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:164103{
Howard Hinnante4383372011-10-22 20:59:454104 return _VSTD::lower_bound(__first, __last, __value_,
Howard Hinnant3e519522010-05-11 19:42:164105 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4106}
4107
4108// upper_bound
4109
4110template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414111_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454112__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164113{
4114 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantce48a112011-06-30 21:18:194115 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:164116 while (__len != 0)
4117 {
4118 difference_type __l2 = __len / 2;
4119 _ForwardIterator __m = __first;
Howard Hinnantce48a112011-06-30 21:18:194120 _VSTD::advance(__m, __l2);
Howard Hinnante4383372011-10-22 20:59:454121 if (__comp(__value_, *__m))
Howard Hinnant3e519522010-05-11 19:42:164122 __len = __l2;
4123 else
4124 {
4125 __first = ++__m;
4126 __len -= __l2 + 1;
4127 }
4128 }
4129 return __first;
4130}
4131
4132template <class _ForwardIterator, class _Tp, class _Compare>
Marshall Clowd57c03d2018-01-16 02:34:414133inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164134_ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454135upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164136{
Howard Hinnant145afa12013-08-23 20:10:184137#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164138 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4139 __debug_less<_Compare> __c(__comp);
Howard Hinnante4383372011-10-22 20:59:454140 return __upper_bound<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant145afa12013-08-23 20:10:184141#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164142 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnante4383372011-10-22 20:59:454143 return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant145afa12013-08-23 20:10:184144#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164145}
4146
4147template <class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414148inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164149_ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454150upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:164151{
Howard Hinnante4383372011-10-22 20:59:454152 return _VSTD::upper_bound(__first, __last, __value_,
Howard Hinnant3e519522010-05-11 19:42:164153 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4154}
4155
4156// equal_range
4157
4158template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414159_LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_ForwardIterator, _ForwardIterator>
Howard Hinnante4383372011-10-22 20:59:454160__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164161{
4162 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantce48a112011-06-30 21:18:194163 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:164164 while (__len != 0)
4165 {
4166 difference_type __l2 = __len / 2;
4167 _ForwardIterator __m = __first;
Howard Hinnantce48a112011-06-30 21:18:194168 _VSTD::advance(__m, __l2);
Howard Hinnante4383372011-10-22 20:59:454169 if (__comp(*__m, __value_))
Howard Hinnant3e519522010-05-11 19:42:164170 {
4171 __first = ++__m;
4172 __len -= __l2 + 1;
4173 }
Howard Hinnante4383372011-10-22 20:59:454174 else if (__comp(__value_, *__m))
Howard Hinnant3e519522010-05-11 19:42:164175 {
4176 __last = __m;
4177 __len = __l2;
4178 }
4179 else
4180 {
4181 _ForwardIterator __mp1 = __m;
4182 return pair<_ForwardIterator, _ForwardIterator>
4183 (
Howard Hinnante4383372011-10-22 20:59:454184 __lower_bound<_Compare>(__first, __m, __value_, __comp),
4185 __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
Howard Hinnant3e519522010-05-11 19:42:164186 );
4187 }
4188 }
4189 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4190}
4191
4192template <class _ForwardIterator, class _Tp, class _Compare>
Marshall Clowd57c03d2018-01-16 02:34:414193inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164194pair<_ForwardIterator, _ForwardIterator>
Howard Hinnante4383372011-10-22 20:59:454195equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164196{
Howard Hinnant145afa12013-08-23 20:10:184197#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164198 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4199 __debug_less<_Compare> __c(__comp);
Howard Hinnante4383372011-10-22 20:59:454200 return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant145afa12013-08-23 20:10:184201#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164202 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnante4383372011-10-22 20:59:454203 return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant145afa12013-08-23 20:10:184204#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164205}
4206
4207template <class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414208inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164209pair<_ForwardIterator, _ForwardIterator>
Howard Hinnante4383372011-10-22 20:59:454210equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:164211{
Howard Hinnante4383372011-10-22 20:59:454212 return _VSTD::equal_range(__first, __last, __value_,
Howard Hinnant3e519522010-05-11 19:42:164213 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4214}
4215
4216// binary_search
4217
4218template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414219inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164220bool
Howard Hinnante4383372011-10-22 20:59:454221__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164222{
Howard Hinnante4383372011-10-22 20:59:454223 __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4224 return __first != __last && !__comp(__value_, *__first);
Howard Hinnant3e519522010-05-11 19:42:164225}
4226
4227template <class _ForwardIterator, class _Tp, class _Compare>
Marshall Clowd57c03d2018-01-16 02:34:414228inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164229bool
Howard Hinnante4383372011-10-22 20:59:454230binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164231{
Howard Hinnant145afa12013-08-23 20:10:184232#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164233 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4234 __debug_less<_Compare> __c(__comp);
Howard Hinnante4383372011-10-22 20:59:454235 return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant145afa12013-08-23 20:10:184236#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164237 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnante4383372011-10-22 20:59:454238 return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant145afa12013-08-23 20:10:184239#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164240}
4241
4242template <class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414243inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164244bool
Howard Hinnante4383372011-10-22 20:59:454245binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:164246{
Howard Hinnante4383372011-10-22 20:59:454247 return _VSTD::binary_search(__first, __last, __value_,
Howard Hinnant3e519522010-05-11 19:42:164248 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4249}
4250
4251// merge
4252
4253template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4254_OutputIterator
4255__merge(_InputIterator1 __first1, _InputIterator1 __last1,
4256 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4257{
4258 for (; __first1 != __last1; ++__result)
4259 {
4260 if (__first2 == __last2)
Howard Hinnantce48a112011-06-30 21:18:194261 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnant3e519522010-05-11 19:42:164262 if (__comp(*__first2, *__first1))
4263 {
4264 *__result = *__first2;
4265 ++__first2;
4266 }
4267 else
4268 {
4269 *__result = *__first1;
4270 ++__first1;
4271 }
4272 }
Howard Hinnantce48a112011-06-30 21:18:194273 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnant3e519522010-05-11 19:42:164274}
4275
4276template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4277inline _LIBCPP_INLINE_VISIBILITY
4278_OutputIterator
4279merge(_InputIterator1 __first1, _InputIterator1 __last1,
4280 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4281{
Howard Hinnant145afa12013-08-23 20:10:184282#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164283 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4284 __debug_less<_Compare> __c(__comp);
Howard Hinnantce48a112011-06-30 21:18:194285 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant145afa12013-08-23 20:10:184286#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164287 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantce48a112011-06-30 21:18:194288 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant145afa12013-08-23 20:10:184289#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164290}
4291
4292template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4293inline _LIBCPP_INLINE_VISIBILITY
4294_OutputIterator
4295merge(_InputIterator1 __first1, _InputIterator1 __last1,
4296 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4297{
4298 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4299 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4300 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4301}
4302
4303// inplace_merge
4304
Marshall Clowadfdae12015-07-29 16:25:454305template <class _Compare, class _InputIterator1, class _InputIterator2,
4306 class _OutputIterator>
4307void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4308 _InputIterator2 __first2, _InputIterator2 __last2,
4309 _OutputIterator __result, _Compare __comp)
4310{
4311 for (; __first1 != __last1; ++__result)
4312 {
4313 if (__first2 == __last2)
4314 {
4315 _VSTD::move(__first1, __last1, __result);
4316 return;
4317 }
4318
4319 if (__comp(*__first2, *__first1))
4320 {
4321 *__result = _VSTD::move(*__first2);
4322 ++__first2;
4323 }
4324 else
4325 {
4326 *__result = _VSTD::move(*__first1);
4327 ++__first1;
4328 }
4329 }
4330 // __first2 through __last2 are already in the right spot.
4331}
4332
Howard Hinnant3e519522010-05-11 19:42:164333template <class _Compare, class _BidirectionalIterator>
4334void
4335__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4336 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4337 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4338 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4339{
4340 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
Howard Hinnant3e519522010-05-11 19:42:164341 __destruct_n __d(0);
4342 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4343 if (__len1 <= __len2)
4344 {
4345 value_type* __p = __buff;
Eric Fiselier910285b2014-10-27 19:28:204346 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnantce48a112011-06-30 21:18:194347 ::new(__p) value_type(_VSTD::move(*__i));
Marshall Clowadfdae12015-07-29 16:25:454348 __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
Howard Hinnant3e519522010-05-11 19:42:164349 }
4350 else
4351 {
4352 value_type* __p = __buff;
Eric Fiselier910285b2014-10-27 19:28:204353 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnantce48a112011-06-30 21:18:194354 ::new(__p) value_type(_VSTD::move(*__i));
Howard Hinnant3e519522010-05-11 19:42:164355 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4356 typedef reverse_iterator<value_type*> _Rv;
Aditya Kumar331fb802016-08-25 11:52:384357 __half_inplace_merge(_Rv(__p), _Rv(__buff),
Marshall Clowadfdae12015-07-29 16:25:454358 _RBi(__middle), _RBi(__first),
Marshall Clowa763b362017-08-28 23:16:134359 _RBi(__last), __invert<_Compare>(__comp));
Howard Hinnant3e519522010-05-11 19:42:164360 }
4361}
4362
4363template <class _Compare, class _BidirectionalIterator>
4364void
4365__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4366 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4367 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4368 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4369{
Howard Hinnant3e519522010-05-11 19:42:164370 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4371 while (true)
4372 {
4373 // if __middle == __last, we're done
4374 if (__len2 == 0)
4375 return;
Marshall Clow526e0922015-02-02 16:44:114376 if (__len1 <= __buff_size || __len2 <= __buff_size)
4377 return __buffered_inplace_merge<_Compare>
4378 (__first, __middle, __last, __comp, __len1, __len2, __buff);
Howard Hinnant3e519522010-05-11 19:42:164379 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
Eric Fiselier910285b2014-10-27 19:28:204380 for (; true; ++__first, (void) --__len1)
Howard Hinnant3e519522010-05-11 19:42:164381 {
4382 if (__len1 == 0)
4383 return;
4384 if (__comp(*__middle, *__first))
4385 break;
4386 }
Howard Hinnant3e519522010-05-11 19:42:164387 // __first < __middle < __last
4388 // *__first > *__middle
4389 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4390 // all elements in:
4391 // [__first, __m1) <= [__middle, __m2)
4392 // [__middle, __m2) < [__m1, __middle)
4393 // [__m1, __middle) <= [__m2, __last)
4394 // and __m1 or __m2 is in the middle of its range
4395 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4396 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4397 difference_type __len11; // distance(__first, __m1)
4398 difference_type __len21; // distance(__middle, __m2)
4399 // binary search smaller range
4400 if (__len1 < __len2)
4401 { // __len >= 1, __len2 >= 2
4402 __len21 = __len2 / 2;
4403 __m2 = __middle;
Howard Hinnantce48a112011-06-30 21:18:194404 _VSTD::advance(__m2, __len21);
Howard Hinnant3e519522010-05-11 19:42:164405 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
Howard Hinnantce48a112011-06-30 21:18:194406 __len11 = _VSTD::distance(__first, __m1);
Howard Hinnant3e519522010-05-11 19:42:164407 }
4408 else
4409 {
4410 if (__len1 == 1)
4411 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4412 // It is known *__first > *__middle
4413 swap(*__first, *__middle);
4414 return;
4415 }
4416 // __len1 >= 2, __len2 >= 1
4417 __len11 = __len1 / 2;
4418 __m1 = __first;
Howard Hinnantce48a112011-06-30 21:18:194419 _VSTD::advance(__m1, __len11);
Howard Hinnant3e519522010-05-11 19:42:164420 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
Howard Hinnantce48a112011-06-30 21:18:194421 __len21 = _VSTD::distance(__middle, __m2);
Howard Hinnant3e519522010-05-11 19:42:164422 }
4423 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4424 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4425 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4426 // swap middle two partitions
Howard Hinnantce48a112011-06-30 21:18:194427 __middle = _VSTD::rotate(__m1, __middle, __m2);
Howard Hinnant3e519522010-05-11 19:42:164428 // __len12 and __len21 now have swapped meanings
4429 // merge smaller range with recurisve call and larger with tail recursion elimination
4430 if (__len11 + __len21 < __len12 + __len22)
4431 {
4432 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4433// __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4434 __first = __middle;
4435 __middle = __m2;
4436 __len1 = __len12;
4437 __len2 = __len22;
4438 }
4439 else
4440 {
4441 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4442// __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4443 __last = __middle;
4444 __middle = __m1;
4445 __len1 = __len11;
4446 __len2 = __len21;
4447 }
4448 }
4449}
4450
Howard Hinnant3e519522010-05-11 19:42:164451template <class _BidirectionalIterator, class _Compare>
4452inline _LIBCPP_INLINE_VISIBILITY
4453void
4454inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4455 _Compare __comp)
4456{
4457 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4458 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnantce48a112011-06-30 21:18:194459 difference_type __len1 = _VSTD::distance(__first, __middle);
4460 difference_type __len2 = _VSTD::distance(__middle, __last);
4461 difference_type __buf_size = _VSTD::min(__len1, __len2);
Marshall Clow0b48cf92015-02-02 17:35:534462 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4463 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
4464
Howard Hinnant145afa12013-08-23 20:10:184465#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164466 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4467 __debug_less<_Compare> __c(__comp);
Howard Hinnantce48a112011-06-30 21:18:194468 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
Howard Hinnant3e519522010-05-11 19:42:164469 __buf.first, __buf.second);
Howard Hinnant145afa12013-08-23 20:10:184470#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164471 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantce48a112011-06-30 21:18:194472 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
Howard Hinnant3e519522010-05-11 19:42:164473 __buf.first, __buf.second);
Howard Hinnant145afa12013-08-23 20:10:184474#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164475}
4476
4477template <class _BidirectionalIterator>
4478inline _LIBCPP_INLINE_VISIBILITY
4479void
4480inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4481{
Howard Hinnantce48a112011-06-30 21:18:194482 _VSTD::inplace_merge(__first, __middle, __last,
Howard Hinnant3e519522010-05-11 19:42:164483 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4484}
4485
4486// stable_sort
4487
4488template <class _Compare, class _InputIterator1, class _InputIterator2>
4489void
4490__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4491 _InputIterator2 __first2, _InputIterator2 __last2,
4492 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4493{
4494 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4495 __destruct_n __d(0);
4496 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4497 for (; true; ++__result)
4498 {
4499 if (__first1 == __last1)
4500 {
4501 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
Howard Hinnantce48a112011-06-30 21:18:194502 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnant3e519522010-05-11 19:42:164503 __h.release();
4504 return;
4505 }
4506 if (__first2 == __last2)
4507 {
4508 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
Howard Hinnantce48a112011-06-30 21:18:194509 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnant3e519522010-05-11 19:42:164510 __h.release();
4511 return;
4512 }
4513 if (__comp(*__first2, *__first1))
4514 {
Howard Hinnantce48a112011-06-30 21:18:194515 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnant3e519522010-05-11 19:42:164516 __d.__incr((value_type*)0);
4517 ++__first2;
4518 }
4519 else
4520 {
Howard Hinnantce48a112011-06-30 21:18:194521 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnant3e519522010-05-11 19:42:164522 __d.__incr((value_type*)0);
4523 ++__first1;
4524 }
4525 }
4526}
4527
4528template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4529void
4530__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4531 _InputIterator2 __first2, _InputIterator2 __last2,
4532 _OutputIterator __result, _Compare __comp)
4533{
4534 for (; __first1 != __last1; ++__result)
4535 {
4536 if (__first2 == __last2)
4537 {
4538 for (; __first1 != __last1; ++__first1, ++__result)
Howard Hinnantce48a112011-06-30 21:18:194539 *__result = _VSTD::move(*__first1);
Howard Hinnant3e519522010-05-11 19:42:164540 return;
4541 }
4542 if (__comp(*__first2, *__first1))
4543 {
Howard Hinnantce48a112011-06-30 21:18:194544 *__result = _VSTD::move(*__first2);
Howard Hinnant3e519522010-05-11 19:42:164545 ++__first2;
4546 }
4547 else
4548 {
Howard Hinnantce48a112011-06-30 21:18:194549 *__result = _VSTD::move(*__first1);
Howard Hinnant3e519522010-05-11 19:42:164550 ++__first1;
4551 }
4552 }
4553 for (; __first2 != __last2; ++__first2, ++__result)
Howard Hinnantce48a112011-06-30 21:18:194554 *__result = _VSTD::move(*__first2);
Howard Hinnant3e519522010-05-11 19:42:164555}
4556
4557template <class _Compare, class _RandomAccessIterator>
4558void
4559__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4560 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4561 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4562
4563template <class _Compare, class _RandomAccessIterator>
4564void
4565__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4566 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4567 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4568{
4569 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4570 switch (__len)
4571 {
4572 case 0:
4573 return;
4574 case 1:
Howard Hinnantce48a112011-06-30 21:18:194575 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnant3e519522010-05-11 19:42:164576 return;
4577 case 2:
Marshall Clowf951fc32018-02-06 18:58:054578 __destruct_n __d(0);
Howard Hinnant3e519522010-05-11 19:42:164579 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
Marshall Clowf951fc32018-02-06 18:58:054580 if (__comp(*--__last1, *__first1))
Howard Hinnant3e519522010-05-11 19:42:164581 {
Howard Hinnantce48a112011-06-30 21:18:194582 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnant3e519522010-05-11 19:42:164583 __d.__incr((value_type*)0);
4584 ++__first2;
Howard Hinnantce48a112011-06-30 21:18:194585 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnant3e519522010-05-11 19:42:164586 }
4587 else
4588 {
Howard Hinnantce48a112011-06-30 21:18:194589 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnant3e519522010-05-11 19:42:164590 __d.__incr((value_type*)0);
4591 ++__first2;
Howard Hinnantce48a112011-06-30 21:18:194592 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnant3e519522010-05-11 19:42:164593 }
4594 __h2.release();
4595 return;
4596 }
4597 if (__len <= 8)
4598 {
4599 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4600 return;
4601 }
4602 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4603 _RandomAccessIterator __m = __first1 + __l2;
4604 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4605 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4606 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4607}
4608
4609template <class _Tp>
4610struct __stable_sort_switch
4611{
Howard Hinnantca740482010-11-19 22:17:284612 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
Howard Hinnant3e519522010-05-11 19:42:164613};
4614
4615template <class _Compare, class _RandomAccessIterator>
4616void
4617__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4618 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4619 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4620{
4621 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4622 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4623 switch (__len)
4624 {
4625 case 0:
4626 case 1:
4627 return;
4628 case 2:
4629 if (__comp(*--__last, *__first))
4630 swap(*__first, *__last);
4631 return;
4632 }
4633 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4634 {
4635 __insertion_sort<_Compare>(__first, __last, __comp);
4636 return;
4637 }
4638 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4639 _RandomAccessIterator __m = __first + __l2;
4640 if (__len <= __buff_size)
4641 {
4642 __destruct_n __d(0);
4643 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4644 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4645 __d.__set(__l2, (value_type*)0);
4646 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4647 __d.__set(__len, (value_type*)0);
4648 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4649// __merge<_Compare>(move_iterator<value_type*>(__buff),
4650// move_iterator<value_type*>(__buff + __l2),
4651// move_iterator<_RandomAccessIterator>(__buff + __l2),
4652// move_iterator<_RandomAccessIterator>(__buff + __len),
4653// __first, __comp);
4654 return;
4655 }
4656 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4657 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4658 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4659}
4660
4661template <class _RandomAccessIterator, class _Compare>
4662inline _LIBCPP_INLINE_VISIBILITY
4663void
4664stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4665{
4666 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4667 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4668 difference_type __len = __last - __first;
4669 pair<value_type*, ptrdiff_t> __buf(0, 0);
4670 unique_ptr<value_type, __return_temporary_buffer> __h;
4671 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4672 {
Howard Hinnantce48a112011-06-30 21:18:194673 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnant3e519522010-05-11 19:42:164674 __h.reset(__buf.first);
4675 }
Howard Hinnant145afa12013-08-23 20:10:184676#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164677 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4678 __debug_less<_Compare> __c(__comp);
4679 __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
Howard Hinnant145afa12013-08-23 20:10:184680#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164681 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4682 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
Howard Hinnant145afa12013-08-23 20:10:184683#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164684}
4685
4686template <class _RandomAccessIterator>
4687inline _LIBCPP_INLINE_VISIBILITY
4688void
4689stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4690{
Howard Hinnantce48a112011-06-30 21:18:194691 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164692}
4693
4694// is_heap_until
4695
4696template <class _RandomAccessIterator, class _Compare>
Marshall Clow49c76432018-01-15 16:16:324697_LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
Howard Hinnant3e519522010-05-11 19:42:164698is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4699{
Howard Hinnantce48a112011-06-30 21:18:194700 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnant3e519522010-05-11 19:42:164701 difference_type __len = __last - __first;
4702 difference_type __p = 0;
4703 difference_type __c = 1;
4704 _RandomAccessIterator __pp = __first;
4705 while (__c < __len)
4706 {
4707 _RandomAccessIterator __cp = __first + __c;
4708 if (__comp(*__pp, *__cp))
4709 return __cp;
4710 ++__c;
4711 ++__cp;
4712 if (__c == __len)
4713 return __last;
4714 if (__comp(*__pp, *__cp))
4715 return __cp;
4716 ++__p;
4717 ++__pp;
4718 __c = 2 * __p + 1;
4719 }
4720 return __last;
4721}
4722
Howard Hinnantb3371f62010-08-22 00:02:434723template<class _RandomAccessIterator>
Marshall Clow49c76432018-01-15 16:16:324724inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164725_RandomAccessIterator
4726is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4727{
Howard Hinnantce48a112011-06-30 21:18:194728 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164729}
4730
4731// is_heap
4732
4733template <class _RandomAccessIterator, class _Compare>
Marshall Clow49c76432018-01-15 16:16:324734inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164735bool
4736is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4737{
Howard Hinnantce48a112011-06-30 21:18:194738 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
Howard Hinnant3e519522010-05-11 19:42:164739}
4740
Howard Hinnantb3371f62010-08-22 00:02:434741template<class _RandomAccessIterator>
Marshall Clow49c76432018-01-15 16:16:324742inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164743bool
4744is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4745{
Howard Hinnantce48a112011-06-30 21:18:194746 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164747}
4748
4749// push_heap
4750
4751template <class _Compare, class _RandomAccessIterator>
4752void
David Majnemer8b512602014-07-22 06:07:094753__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4754 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
Howard Hinnant3e519522010-05-11 19:42:164755{
Howard Hinnant3e519522010-05-11 19:42:164756 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4757 if (__len > 1)
4758 {
4759 __len = (__len - 2) / 2;
4760 _RandomAccessIterator __ptr = __first + __len;
4761 if (__comp(*__ptr, *--__last))
4762 {
Howard Hinnantce48a112011-06-30 21:18:194763 value_type __t(_VSTD::move(*__last));
Howard Hinnant3e519522010-05-11 19:42:164764 do
4765 {
Howard Hinnantce48a112011-06-30 21:18:194766 *__last = _VSTD::move(*__ptr);
Howard Hinnant3e519522010-05-11 19:42:164767 __last = __ptr;
4768 if (__len == 0)
4769 break;
4770 __len = (__len - 1) / 2;
4771 __ptr = __first + __len;
4772 } while (__comp(*__ptr, __t));
Howard Hinnantce48a112011-06-30 21:18:194773 *__last = _VSTD::move(__t);
Howard Hinnant3e519522010-05-11 19:42:164774 }
4775 }
4776}
4777
4778template <class _RandomAccessIterator, class _Compare>
4779inline _LIBCPP_INLINE_VISIBILITY
4780void
4781push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4782{
Howard Hinnant145afa12013-08-23 20:10:184783#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164784 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4785 __debug_less<_Compare> __c(__comp);
David Majnemer8b512602014-07-22 06:07:094786 __sift_up<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant145afa12013-08-23 20:10:184787#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164788 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
David Majnemer8b512602014-07-22 06:07:094789 __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant145afa12013-08-23 20:10:184790#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164791}
4792
4793template <class _RandomAccessIterator>
4794inline _LIBCPP_INLINE_VISIBILITY
4795void
4796push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4797{
Howard Hinnantce48a112011-06-30 21:18:194798 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164799}
4800
4801// pop_heap
4802
4803template <class _Compare, class _RandomAccessIterator>
David Majnemer8b512602014-07-22 06:07:094804void
Eric Fiselierfd838222016-12-23 23:37:524805__sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
4806 _Compare __comp,
David Majnemer8b512602014-07-22 06:07:094807 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4808 _RandomAccessIterator __start)
4809{
4810 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4811 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4812 // left-child of __start is at 2 * __start + 1
4813 // right-child of __start is at 2 * __start + 2
4814 difference_type __child = __start - __first;
4815
4816 if (__len < 2 || (__len - 2) / 2 < __child)
4817 return;
4818
4819 __child = 2 * __child + 1;
4820 _RandomAccessIterator __child_i = __first + __child;
4821
4822 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4823 // right-child exists and is greater than left-child
4824 ++__child_i;
4825 ++__child;
4826 }
4827
4828 // check if we are in heap-order
4829 if (__comp(*__child_i, *__start))
4830 // we are, __start is larger than it's largest child
4831 return;
4832
4833 value_type __top(_VSTD::move(*__start));
4834 do
4835 {
4836 // we are not in heap-order, swap the parent with it's largest child
4837 *__start = _VSTD::move(*__child_i);
4838 __start = __child_i;
4839
4840 if ((__len - 2) / 2 < __child)
4841 break;
4842
4843 // recompute the child based off of the updated parent
4844 __child = 2 * __child + 1;
4845 __child_i = __first + __child;
4846
4847 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4848 // right-child exists and is greater than left-child
4849 ++__child_i;
4850 ++__child;
4851 }
4852
4853 // check if we are in heap-order
4854 } while (!__comp(*__child_i, __top));
4855 *__start = _VSTD::move(__top);
4856}
4857
4858template <class _Compare, class _RandomAccessIterator>
Howard Hinnant3e519522010-05-11 19:42:164859inline _LIBCPP_INLINE_VISIBILITY
4860void
4861__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4862 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4863{
4864 if (__len > 1)
4865 {
4866 swap(*__first, *--__last);
David Majnemer8b512602014-07-22 06:07:094867 __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
Howard Hinnant3e519522010-05-11 19:42:164868 }
4869}
4870
4871template <class _RandomAccessIterator, class _Compare>
4872inline _LIBCPP_INLINE_VISIBILITY
4873void
4874pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4875{
Howard Hinnant145afa12013-08-23 20:10:184876#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164877 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4878 __debug_less<_Compare> __c(__comp);
4879 __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant145afa12013-08-23 20:10:184880#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164881 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4882 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant145afa12013-08-23 20:10:184883#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164884}
4885
4886template <class _RandomAccessIterator>
4887inline _LIBCPP_INLINE_VISIBILITY
4888void
4889pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4890{
Howard Hinnantce48a112011-06-30 21:18:194891 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164892}
4893
4894// make_heap
4895
4896template <class _Compare, class _RandomAccessIterator>
4897void
4898__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4899{
4900 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4901 difference_type __n = __last - __first;
4902 if (__n > 1)
4903 {
David Majnemer8b512602014-07-22 06:07:094904 // start from the first parent, there is no need to consider children
4905 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
4906 {
4907 __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
4908 }
Howard Hinnant3e519522010-05-11 19:42:164909 }
4910}
4911
4912template <class _RandomAccessIterator, class _Compare>
4913inline _LIBCPP_INLINE_VISIBILITY
4914void
4915make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4916{
Howard Hinnant145afa12013-08-23 20:10:184917#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164918 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4919 __debug_less<_Compare> __c(__comp);
4920 __make_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant145afa12013-08-23 20:10:184921#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164922 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4923 __make_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant145afa12013-08-23 20:10:184924#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164925}
4926
4927template <class _RandomAccessIterator>
4928inline _LIBCPP_INLINE_VISIBILITY
4929void
4930make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4931{
Howard Hinnantce48a112011-06-30 21:18:194932 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164933}
4934
4935// sort_heap
4936
4937template <class _Compare, class _RandomAccessIterator>
4938void
4939__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4940{
4941 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4942 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
4943 __pop_heap<_Compare>(__first, __last, __comp, __n);
4944}
4945
4946template <class _RandomAccessIterator, class _Compare>
4947inline _LIBCPP_INLINE_VISIBILITY
4948void
4949sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4950{
Howard Hinnant145afa12013-08-23 20:10:184951#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164952 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4953 __debug_less<_Compare> __c(__comp);
4954 __sort_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant145afa12013-08-23 20:10:184955#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164956 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4957 __sort_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant145afa12013-08-23 20:10:184958#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164959}
4960
4961template <class _RandomAccessIterator>
4962inline _LIBCPP_INLINE_VISIBILITY
4963void
4964sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4965{
Howard Hinnantce48a112011-06-30 21:18:194966 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164967}
4968
4969// partial_sort
4970
4971template <class _Compare, class _RandomAccessIterator>
4972void
4973__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
4974 _Compare __comp)
4975{
4976 __make_heap<_Compare>(__first, __middle, __comp);
4977 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
4978 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
4979 {
4980 if (__comp(*__i, *__first))
4981 {
4982 swap(*__i, *__first);
David Majnemer8b512602014-07-22 06:07:094983 __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
Howard Hinnant3e519522010-05-11 19:42:164984 }
4985 }
4986 __sort_heap<_Compare>(__first, __middle, __comp);
4987}
4988
4989template <class _RandomAccessIterator, class _Compare>
4990inline _LIBCPP_INLINE_VISIBILITY
4991void
4992partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
4993 _Compare __comp)
4994{
Howard Hinnant145afa12013-08-23 20:10:184995#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:164996 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4997 __debug_less<_Compare> __c(__comp);
4998 __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
Howard Hinnant145afa12013-08-23 20:10:184999#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165000 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5001 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
Howard Hinnant145afa12013-08-23 20:10:185002#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165003}
5004
5005template <class _RandomAccessIterator>
5006inline _LIBCPP_INLINE_VISIBILITY
5007void
5008partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5009{
Howard Hinnantce48a112011-06-30 21:18:195010 _VSTD::partial_sort(__first, __middle, __last,
Howard Hinnant3e519522010-05-11 19:42:165011 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5012}
5013
5014// partial_sort_copy
5015
5016template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5017_RandomAccessIterator
5018__partial_sort_copy(_InputIterator __first, _InputIterator __last,
5019 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5020{
5021 _RandomAccessIterator __r = __result_first;
5022 if (__r != __result_last)
5023 {
Eric Fiselier910285b2014-10-27 19:28:205024 for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
Howard Hinnant3e519522010-05-11 19:42:165025 *__r = *__first;
5026 __make_heap<_Compare>(__result_first, __r, __comp);
David Majnemer8b512602014-07-22 06:07:095027 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
Howard Hinnant3e519522010-05-11 19:42:165028 for (; __first != __last; ++__first)
5029 if (__comp(*__first, *__result_first))
5030 {
5031 *__result_first = *__first;
David Majnemer8b512602014-07-22 06:07:095032 __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
Howard Hinnant3e519522010-05-11 19:42:165033 }
5034 __sort_heap<_Compare>(__result_first, __r, __comp);
5035 }
5036 return __r;
5037}
5038
5039template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5040inline _LIBCPP_INLINE_VISIBILITY
5041_RandomAccessIterator
5042partial_sort_copy(_InputIterator __first, _InputIterator __last,
5043 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5044{
Howard Hinnant145afa12013-08-23 20:10:185045#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165046 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5047 __debug_less<_Compare> __c(__comp);
5048 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
Howard Hinnant145afa12013-08-23 20:10:185049#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165050 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5051 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
Howard Hinnant145afa12013-08-23 20:10:185052#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165053}
5054
5055template <class _InputIterator, class _RandomAccessIterator>
5056inline _LIBCPP_INLINE_VISIBILITY
5057_RandomAccessIterator
5058partial_sort_copy(_InputIterator __first, _InputIterator __last,
5059 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5060{
Howard Hinnantce48a112011-06-30 21:18:195061 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
Howard Hinnant3e519522010-05-11 19:42:165062 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5063}
5064
5065// nth_element
5066
5067template <class _Compare, class _RandomAccessIterator>
5068void
5069__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5070{
5071 // _Compare is known to be a reference type
5072 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5073 const difference_type __limit = 7;
5074 while (true)
5075 {
5076 __restart:
Howard Hinnantb34b48192011-12-29 17:45:355077 if (__nth == __last)
5078 return;
Howard Hinnant3e519522010-05-11 19:42:165079 difference_type __len = __last - __first;
5080 switch (__len)
5081 {
5082 case 0:
5083 case 1:
5084 return;
5085 case 2:
5086 if (__comp(*--__last, *__first))
5087 swap(*__first, *__last);
5088 return;
5089 case 3:
5090 {
5091 _RandomAccessIterator __m = __first;
Howard Hinnantce48a112011-06-30 21:18:195092 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165093 return;
5094 }
5095 }
5096 if (__len <= __limit)
5097 {
5098 __selection_sort<_Compare>(__first, __last, __comp);
5099 return;
5100 }
5101 // __len > __limit >= 3
5102 _RandomAccessIterator __m = __first + __len/2;
5103 _RandomAccessIterator __lm1 = __last;
Howard Hinnantce48a112011-06-30 21:18:195104 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
Howard Hinnant3e519522010-05-11 19:42:165105 // *__m is median
5106 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5107 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5108 _RandomAccessIterator __i = __first;
5109 _RandomAccessIterator __j = __lm1;
5110 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5111 // The search going up is known to be guarded but the search coming down isn't.
5112 // Prime the downward search with a guard.
5113 if (!__comp(*__i, *__m)) // if *__first == *__m
5114 {
5115 // *__first == *__m, *__first doesn't go in first part
5116 // manually guard downward moving __j against __i
5117 while (true)
5118 {
5119 if (__i == --__j)
5120 {
5121 // *__first == *__m, *__m <= all other elements
5122 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5123 ++__i; // __first + 1
5124 __j = __last;
5125 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5126 {
5127 while (true)
5128 {
5129 if (__i == __j)
5130 return; // [__first, __last) all equivalent elements
5131 if (__comp(*__first, *__i))
5132 {
5133 swap(*__i, *__j);
5134 ++__n_swaps;
5135 ++__i;
5136 break;
5137 }
5138 ++__i;
5139 }
5140 }
5141 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5142 if (__i == __j)
5143 return;
5144 while (true)
5145 {
5146 while (!__comp(*__first, *__i))
5147 ++__i;
5148 while (__comp(*__first, *--__j))
5149 ;
5150 if (__i >= __j)
5151 break;
5152 swap(*__i, *__j);
5153 ++__n_swaps;
5154 ++__i;
5155 }
5156 // [__first, __i) == *__first and *__first < [__i, __last)
5157 // The first part is sorted,
5158 if (__nth < __i)
5159 return;
5160 // __nth_element the secod part
5161 // __nth_element<_Compare>(__i, __nth, __last, __comp);
5162 __first = __i;
5163 goto __restart;
5164 }
5165 if (__comp(*__j, *__m))
5166 {
5167 swap(*__i, *__j);
5168 ++__n_swaps;
5169 break; // found guard for downward moving __j, now use unguarded partition
5170 }
5171 }
5172 }
5173 ++__i;
5174 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5175 // if not yet partitioned...
5176 if (__i < __j)
5177 {
5178 // known that *(__i - 1) < *__m
5179 while (true)
5180 {
5181 // __m still guards upward moving __i
5182 while (__comp(*__i, *__m))
5183 ++__i;
5184 // It is now known that a guard exists for downward moving __j
5185 while (!__comp(*--__j, *__m))
5186 ;
5187 if (__i >= __j)
5188 break;
5189 swap(*__i, *__j);
5190 ++__n_swaps;
5191 // It is known that __m != __j
5192 // If __m just moved, follow it
5193 if (__m == __i)
5194 __m = __j;
5195 ++__i;
5196 }
5197 }
5198 // [__first, __i) < *__m and *__m <= [__i, __last)
5199 if (__i != __m && __comp(*__m, *__i))
5200 {
5201 swap(*__i, *__m);
5202 ++__n_swaps;
5203 }
5204 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5205 if (__nth == __i)
5206 return;
5207 if (__n_swaps == 0)
5208 {
5209 // We were given a perfectly partitioned sequence. Coincidence?
5210 if (__nth < __i)
5211 {
5212 // Check for [__first, __i) already sorted
5213 __j = __m = __first;
5214 while (++__j != __i)
5215 {
5216 if (__comp(*__j, *__m))
5217 // not yet sorted, so sort
5218 goto not_sorted;
5219 __m = __j;
5220 }
5221 // [__first, __i) sorted
5222 return;
5223 }
5224 else
5225 {
5226 // Check for [__i, __last) already sorted
5227 __j = __m = __i;
5228 while (++__j != __last)
5229 {
5230 if (__comp(*__j, *__m))
5231 // not yet sorted, so sort
5232 goto not_sorted;
5233 __m = __j;
5234 }
5235 // [__i, __last) sorted
5236 return;
5237 }
5238 }
5239not_sorted:
5240 // __nth_element on range containing __nth
5241 if (__nth < __i)
5242 {
5243 // __nth_element<_Compare>(__first, __nth, __i, __comp);
5244 __last = __i;
5245 }
5246 else
5247 {
5248 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5249 __first = ++__i;
5250 }
5251 }
5252}
5253
5254template <class _RandomAccessIterator, class _Compare>
5255inline _LIBCPP_INLINE_VISIBILITY
5256void
5257nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5258{
Howard Hinnant145afa12013-08-23 20:10:185259#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165260 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5261 __debug_less<_Compare> __c(__comp);
5262 __nth_element<_Comp_ref>(__first, __nth, __last, __c);
Howard Hinnant145afa12013-08-23 20:10:185263#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165264 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5265 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
Howard Hinnant145afa12013-08-23 20:10:185266#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165267}
5268
5269template <class _RandomAccessIterator>
5270inline _LIBCPP_INLINE_VISIBILITY
5271void
5272nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5273{
Howard Hinnantce48a112011-06-30 21:18:195274 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:165275}
5276
5277// includes
5278
5279template <class _Compare, class _InputIterator1, class _InputIterator2>
Marshall Clow8da1a482018-01-22 23:10:405280_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnant3e519522010-05-11 19:42:165281__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5282 _Compare __comp)
5283{
5284 for (; __first2 != __last2; ++__first1)
5285 {
5286 if (__first1 == __last1 || __comp(*__first2, *__first1))
5287 return false;
5288 if (!__comp(*__first1, *__first2))
5289 ++__first2;
5290 }
5291 return true;
5292}
5293
5294template <class _InputIterator1, class _InputIterator2, class _Compare>
Marshall Clow8da1a482018-01-22 23:10:405295inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165296bool
5297includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5298 _Compare __comp)
5299{
Howard Hinnant145afa12013-08-23 20:10:185300#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165301 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5302 __debug_less<_Compare> __c(__comp);
5303 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant145afa12013-08-23 20:10:185304#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165305 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5306 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant145afa12013-08-23 20:10:185307#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165308}
5309
5310template <class _InputIterator1, class _InputIterator2>
Marshall Clow8da1a482018-01-22 23:10:405311inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165312bool
5313includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5314{
Howard Hinnantce48a112011-06-30 21:18:195315 return _VSTD::includes(__first1, __last1, __first2, __last2,
Howard Hinnant3e519522010-05-11 19:42:165316 __less<typename iterator_traits<_InputIterator1>::value_type,
5317 typename iterator_traits<_InputIterator2>::value_type>());
5318}
5319
5320// set_union
5321
5322template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5323_OutputIterator
5324__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5325 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5326{
5327 for (; __first1 != __last1; ++__result)
5328 {
5329 if (__first2 == __last2)
Howard Hinnantce48a112011-06-30 21:18:195330 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnant3e519522010-05-11 19:42:165331 if (__comp(*__first2, *__first1))
5332 {
5333 *__result = *__first2;
5334 ++__first2;
5335 }
5336 else
5337 {
Howard Hinnant3e519522010-05-11 19:42:165338 if (!__comp(*__first1, *__first2))
5339 ++__first2;
Marshall Clow05da5b02017-10-30 15:50:005340 *__result = *__first1;
Howard Hinnant3e519522010-05-11 19:42:165341 ++__first1;
5342 }
5343 }
Howard Hinnantce48a112011-06-30 21:18:195344 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnant3e519522010-05-11 19:42:165345}
5346
5347template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5348inline _LIBCPP_INLINE_VISIBILITY
5349_OutputIterator
5350set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5351 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5352{
Howard Hinnant145afa12013-08-23 20:10:185353#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165354 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5355 __debug_less<_Compare> __c(__comp);
5356 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant145afa12013-08-23 20:10:185357#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165358 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5359 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant145afa12013-08-23 20:10:185360#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165361}
5362
5363template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5364inline _LIBCPP_INLINE_VISIBILITY
5365_OutputIterator
5366set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5367 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5368{
Howard Hinnantce48a112011-06-30 21:18:195369 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
Howard Hinnant3e519522010-05-11 19:42:165370 __less<typename iterator_traits<_InputIterator1>::value_type,
5371 typename iterator_traits<_InputIterator2>::value_type>());
5372}
5373
5374// set_intersection
5375
5376template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
Marshall Clow8da1a482018-01-22 23:10:405377_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnant3e519522010-05-11 19:42:165378__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5379 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5380{
5381 while (__first1 != __last1 && __first2 != __last2)
5382 {
5383 if (__comp(*__first1, *__first2))
5384 ++__first1;
5385 else
5386 {
5387 if (!__comp(*__first2, *__first1))
5388 {
5389 *__result = *__first1;
5390 ++__result;
5391 ++__first1;
5392 }
5393 ++__first2;
5394 }
5395 }
5396 return __result;
5397}
5398
5399template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
Marshall Clow8da1a482018-01-22 23:10:405400inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165401_OutputIterator
5402set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5403 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5404{
Howard Hinnant145afa12013-08-23 20:10:185405#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165406 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5407 __debug_less<_Compare> __c(__comp);
5408 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant145afa12013-08-23 20:10:185409#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165410 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5411 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant145afa12013-08-23 20:10:185412#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165413}
5414
5415template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
Marshall Clow8da1a482018-01-22 23:10:405416inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165417_OutputIterator
5418set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5419 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5420{
Howard Hinnantce48a112011-06-30 21:18:195421 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
Howard Hinnant3e519522010-05-11 19:42:165422 __less<typename iterator_traits<_InputIterator1>::value_type,
5423 typename iterator_traits<_InputIterator2>::value_type>());
5424}
5425
5426// set_difference
5427
5428template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5429_OutputIterator
5430__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5431 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5432{
5433 while (__first1 != __last1)
5434 {
5435 if (__first2 == __last2)
Howard Hinnantce48a112011-06-30 21:18:195436 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnant3e519522010-05-11 19:42:165437 if (__comp(*__first1, *__first2))
5438 {
5439 *__result = *__first1;
5440 ++__result;
5441 ++__first1;
5442 }
5443 else
5444 {
5445 if (!__comp(*__first2, *__first1))
5446 ++__first1;
5447 ++__first2;
5448 }
5449 }
5450 return __result;
5451}
5452
5453template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5454inline _LIBCPP_INLINE_VISIBILITY
5455_OutputIterator
5456set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5457 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5458{
Howard Hinnant145afa12013-08-23 20:10:185459#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165460 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5461 __debug_less<_Compare> __c(__comp);
5462 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant145afa12013-08-23 20:10:185463#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165464 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5465 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant145afa12013-08-23 20:10:185466#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165467}
5468
5469template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5470inline _LIBCPP_INLINE_VISIBILITY
5471_OutputIterator
5472set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5473 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5474{
Howard Hinnantce48a112011-06-30 21:18:195475 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnant3e519522010-05-11 19:42:165476 __less<typename iterator_traits<_InputIterator1>::value_type,
5477 typename iterator_traits<_InputIterator2>::value_type>());
5478}
5479
5480// set_symmetric_difference
5481
5482template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5483_OutputIterator
5484__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5485 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5486{
5487 while (__first1 != __last1)
5488 {
5489 if (__first2 == __last2)
Howard Hinnantce48a112011-06-30 21:18:195490 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnant3e519522010-05-11 19:42:165491 if (__comp(*__first1, *__first2))
5492 {
5493 *__result = *__first1;
5494 ++__result;
5495 ++__first1;
5496 }
5497 else
5498 {
5499 if (__comp(*__first2, *__first1))
5500 {
5501 *__result = *__first2;
5502 ++__result;
5503 }
5504 else
5505 ++__first1;
5506 ++__first2;
5507 }
5508 }
Howard Hinnantce48a112011-06-30 21:18:195509 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnant3e519522010-05-11 19:42:165510}
5511
5512template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5513inline _LIBCPP_INLINE_VISIBILITY
5514_OutputIterator
5515set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5516 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5517{
Howard Hinnant145afa12013-08-23 20:10:185518#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165519 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5520 __debug_less<_Compare> __c(__comp);
5521 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant145afa12013-08-23 20:10:185522#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165523 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5524 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant145afa12013-08-23 20:10:185525#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165526}
5527
5528template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5529inline _LIBCPP_INLINE_VISIBILITY
5530_OutputIterator
5531set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5532 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5533{
Howard Hinnantce48a112011-06-30 21:18:195534 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnant3e519522010-05-11 19:42:165535 __less<typename iterator_traits<_InputIterator1>::value_type,
5536 typename iterator_traits<_InputIterator2>::value_type>());
5537}
5538
5539// lexicographical_compare
5540
5541template <class _Compare, class _InputIterator1, class _InputIterator2>
Marshall Clow1b9a4ff2018-01-22 20:44:335542_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnant3e519522010-05-11 19:42:165543__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5544 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5545{
Eric Fiselier910285b2014-10-27 19:28:205546 for (; __first2 != __last2; ++__first1, (void) ++__first2)
Howard Hinnant3e519522010-05-11 19:42:165547 {
5548 if (__first1 == __last1 || __comp(*__first1, *__first2))
5549 return true;
5550 if (__comp(*__first2, *__first1))
5551 return false;
5552 }
5553 return false;
5554}
5555
5556template <class _InputIterator1, class _InputIterator2, class _Compare>
Marshall Clow1b9a4ff2018-01-22 20:44:335557inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165558bool
5559lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5560 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5561{
Howard Hinnant145afa12013-08-23 20:10:185562#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165563 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5564 __debug_less<_Compare> __c(__comp);
5565 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant145afa12013-08-23 20:10:185566#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165567 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5568 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant145afa12013-08-23 20:10:185569#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165570}
5571
5572template <class _InputIterator1, class _InputIterator2>
Marshall Clow1b9a4ff2018-01-22 20:44:335573inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165574bool
5575lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5576 _InputIterator2 __first2, _InputIterator2 __last2)
5577{
Howard Hinnantce48a112011-06-30 21:18:195578 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
Howard Hinnant3e519522010-05-11 19:42:165579 __less<typename iterator_traits<_InputIterator1>::value_type,
5580 typename iterator_traits<_InputIterator2>::value_type>());
5581}
5582
5583// next_permutation
5584
5585template <class _Compare, class _BidirectionalIterator>
5586bool
5587__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5588{
5589 _BidirectionalIterator __i = __last;
5590 if (__first == __last || __first == --__i)
5591 return false;
5592 while (true)
5593 {
5594 _BidirectionalIterator __ip1 = __i;
5595 if (__comp(*--__i, *__ip1))
5596 {
5597 _BidirectionalIterator __j = __last;
5598 while (!__comp(*__i, *--__j))
5599 ;
5600 swap(*__i, *__j);
Howard Hinnantce48a112011-06-30 21:18:195601 _VSTD::reverse(__ip1, __last);
Howard Hinnant3e519522010-05-11 19:42:165602 return true;
5603 }
5604 if (__i == __first)
5605 {
Howard Hinnantce48a112011-06-30 21:18:195606 _VSTD::reverse(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:165607 return false;
5608 }
5609 }
5610}
5611
5612template <class _BidirectionalIterator, class _Compare>
5613inline _LIBCPP_INLINE_VISIBILITY
5614bool
5615next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5616{
Howard Hinnant145afa12013-08-23 20:10:185617#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165618 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5619 __debug_less<_Compare> __c(__comp);
5620 return __next_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant145afa12013-08-23 20:10:185621#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165622 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5623 return __next_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant145afa12013-08-23 20:10:185624#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165625}
5626
5627template <class _BidirectionalIterator>
5628inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb3371f62010-08-22 00:02:435629bool
Howard Hinnant3e519522010-05-11 19:42:165630next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5631{
Howard Hinnantce48a112011-06-30 21:18:195632 return _VSTD::next_permutation(__first, __last,
Howard Hinnant3e519522010-05-11 19:42:165633 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5634}
5635
5636// prev_permutation
5637
5638template <class _Compare, class _BidirectionalIterator>
5639bool
5640__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5641{
5642 _BidirectionalIterator __i = __last;
5643 if (__first == __last || __first == --__i)
5644 return false;
5645 while (true)
5646 {
5647 _BidirectionalIterator __ip1 = __i;
5648 if (__comp(*__ip1, *--__i))
5649 {
5650 _BidirectionalIterator __j = __last;
5651 while (!__comp(*--__j, *__i))
5652 ;
5653 swap(*__i, *__j);
Howard Hinnantce48a112011-06-30 21:18:195654 _VSTD::reverse(__ip1, __last);
Howard Hinnant3e519522010-05-11 19:42:165655 return true;
5656 }
5657 if (__i == __first)
5658 {
Howard Hinnantce48a112011-06-30 21:18:195659 _VSTD::reverse(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:165660 return false;
5661 }
5662 }
5663}
5664
5665template <class _BidirectionalIterator, class _Compare>
5666inline _LIBCPP_INLINE_VISIBILITY
5667bool
5668prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5669{
Howard Hinnant145afa12013-08-23 20:10:185670#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165671 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5672 __debug_less<_Compare> __c(__comp);
5673 return __prev_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant145afa12013-08-23 20:10:185674#else // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165675 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5676 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant145afa12013-08-23 20:10:185677#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:165678}
5679
5680template <class _BidirectionalIterator>
5681inline _LIBCPP_INLINE_VISIBILITY
5682bool
5683prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5684{
Howard Hinnantce48a112011-06-30 21:18:195685 return _VSTD::prev_permutation(__first, __last,
Howard Hinnant3e519522010-05-11 19:42:165686 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5687}
5688
Howard Hinnant3e519522010-05-11 19:42:165689_LIBCPP_END_NAMESPACE_STD
5690
Eric Fiseliera016efb2017-05-31 22:07:495691_LIBCPP_POP_MACROS
5692
Howard Hinnant3e519522010-05-11 19:42:165693#endif // _LIBCPP_ALGORITHM