blob: 1359fda53b508a64489d95df15a036754c3bb70b [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- algorithm ---------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_ALGORITHM
12#define _LIBCPP_ALGORITHM
13
14/*
15 algorithm synopsis
16
17#include <initializer_list>
18
19namespace std
20{
21
22template <class InputIterator, class Predicate>
Marshall Clowd607fdb2018-01-15 17:20:36 +000023 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000024 all_of(InputIterator first, InputIterator last, Predicate pred);
25
26template <class InputIterator, class Predicate>
Marshall Clowd607fdb2018-01-15 17:20:36 +000027 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000028 any_of(InputIterator first, InputIterator last, Predicate pred);
29
30template <class InputIterator, class Predicate>
Marshall Clowd607fdb2018-01-15 17:20:36 +000031 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000032 none_of(InputIterator first, InputIterator last, Predicate pred);
33
34template <class InputIterator, class Function>
Marshall Clow5492c8a2018-01-22 20:44:33 +000035 constexpr Function // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000036 for_each(InputIterator first, InputIterator last, Function f);
37
Marshall Clowde0169c2017-05-25 02:29:54 +000038template<class InputIterator, class Size, class Function>
Marshall Clow5492c8a2018-01-22 20:44:33 +000039 constexpr InputIterator // constexpr in C++20
40 for_each_n(InputIterator first, Size n, Function f); // C++17
Marshall Clowde0169c2017-05-25 02:29:54 +000041
Howard Hinnantc51e1022010-05-11 19:42:16 +000042template <class InputIterator, class T>
Marshall Clowee0161e2018-01-15 19:26:05 +000043 constexpr InputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000044 find(InputIterator first, InputIterator last, const T& value);
45
46template <class InputIterator, class Predicate>
Marshall Clowee0161e2018-01-15 19:26:05 +000047 constexpr InputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000048 find_if(InputIterator first, InputIterator last, Predicate pred);
49
50template<class InputIterator, class Predicate>
Marshall Clowee0161e2018-01-15 19:26:05 +000051 InputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000052 find_if_not(InputIterator first, InputIterator last, Predicate pred);
53
54template <class ForwardIterator1, class ForwardIterator2>
Marshall Clowee0161e2018-01-15 19:26:05 +000055 ForwardIterator1 // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000056 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
57 ForwardIterator2 first2, ForwardIterator2 last2);
58
59template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clowee0161e2018-01-15 19:26:05 +000060 ForwardIterator1 // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000061 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
62 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
63
64template <class ForwardIterator1, class ForwardIterator2>
Marshall Clowee0161e2018-01-15 19:26:05 +000065 constexpr ForwardIterator1 // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000066 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
67 ForwardIterator2 first2, ForwardIterator2 last2);
68
69template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clowee0161e2018-01-15 19:26:05 +000070 constexpr ForwardIterator1 // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000071 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
72 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
73
74template <class ForwardIterator>
Marshall Clowee0161e2018-01-15 19:26:05 +000075 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000076 adjacent_find(ForwardIterator first, ForwardIterator last);
77
78template <class ForwardIterator, class BinaryPredicate>
Marshall Clowee0161e2018-01-15 19:26:05 +000079 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000080 adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
81
82template <class InputIterator, class T>
Marshall Clow3c0558f2018-01-15 19:40:34 +000083 constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000084 count(InputIterator first, InputIterator last, const T& value);
85
86template <class InputIterator, class Predicate>
Marshall Clow3c0558f2018-01-15 19:40:34 +000087 constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000088 count_if(InputIterator first, InputIterator last, Predicate pred);
89
90template <class InputIterator1, class InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +000091 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000092 mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
93
Marshall Clow96b42b22013-05-09 21:14:23 +000094template <class InputIterator1, class InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +000095 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
Aditya Kumar3a0179a2016-08-25 11:52:38 +000096 mismatch(InputIterator1 first1, InputIterator1 last1,
Marshall Clow96b42b22013-05-09 21:14:23 +000097 InputIterator2 first2, InputIterator2 last2); // **C++14**
98
Howard Hinnantc51e1022010-05-11 19:42:16 +000099template <class InputIterator1, class InputIterator2, class BinaryPredicate>
Marshall Clow30bf3022018-01-16 02:04:10 +0000100 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000101 mismatch(InputIterator1 first1, InputIterator1 last1,
102 InputIterator2 first2, BinaryPredicate pred);
103
Marshall Clow96b42b22013-05-09 21:14:23 +0000104template <class InputIterator1, class InputIterator2, class BinaryPredicate>
Marshall Clow30bf3022018-01-16 02:04:10 +0000105 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
Marshall Clow96b42b22013-05-09 21:14:23 +0000106 mismatch(InputIterator1 first1, InputIterator1 last1,
107 InputIterator2 first2, InputIterator2 last2,
108 BinaryPredicate pred); // **C++14**
109
Howard Hinnantc51e1022010-05-11 19:42:16 +0000110template <class InputIterator1, class InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +0000111 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000112 equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
113
Marshall Clow96b42b22013-05-09 21:14:23 +0000114template <class InputIterator1, class InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +0000115 constexpr bool // constexpr in C++20
Aditya Kumar3a0179a2016-08-25 11:52:38 +0000116 equal(InputIterator1 first1, InputIterator1 last1,
Marshall Clow96b42b22013-05-09 21:14:23 +0000117 InputIterator2 first2, InputIterator2 last2); // **C++14**
118
Howard Hinnantc51e1022010-05-11 19:42:16 +0000119template <class InputIterator1, class InputIterator2, class BinaryPredicate>
Marshall Clow30bf3022018-01-16 02:04:10 +0000120 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000121 equal(InputIterator1 first1, InputIterator1 last1,
122 InputIterator2 first2, BinaryPredicate pred);
123
Marshall Clow96b42b22013-05-09 21:14:23 +0000124template <class InputIterator1, class InputIterator2, class BinaryPredicate>
Marshall Clow30bf3022018-01-16 02:04:10 +0000125 constexpr bool // constexpr in C++20
Marshall Clow96b42b22013-05-09 21:14:23 +0000126 equal(InputIterator1 first1, InputIterator1 last1,
127 InputIterator2 first2, InputIterator2 last2,
128 BinaryPredicate pred); // **C++14**
129
Howard Hinnantc51e1022010-05-11 19:42:16 +0000130template<class ForwardIterator1, class ForwardIterator2>
Marshall Clow96d050a2018-01-15 16:16:32 +0000131 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
133 ForwardIterator2 first2);
134
Marshall Clow96b42b22013-05-09 21:14:23 +0000135template<class ForwardIterator1, class ForwardIterator2>
Marshall Clow96d050a2018-01-15 16:16:32 +0000136 constexpr bool // constexpr in C++20
Marshall Clow96b42b22013-05-09 21:14:23 +0000137 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
138 ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
139
Howard Hinnantc51e1022010-05-11 19:42:16 +0000140template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clow96d050a2018-01-15 16:16:32 +0000141 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
143 ForwardIterator2 first2, BinaryPredicate pred);
144
Marshall Clow96b42b22013-05-09 21:14:23 +0000145template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clow96d050a2018-01-15 16:16:32 +0000146 constexpr bool // constexpr in C++20
Marshall Clow96b42b22013-05-09 21:14:23 +0000147 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
148 ForwardIterator2 first2, ForwardIterator2 last2,
149 BinaryPredicate pred); // **C++14**
150
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151template <class ForwardIterator1, class ForwardIterator2>
Marshall Clow323fc5b2018-01-16 15:48:27 +0000152 constexpr ForwardIterator1 // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000153 search(ForwardIterator1 first1, ForwardIterator1 last1,
154 ForwardIterator2 first2, ForwardIterator2 last2);
155
156template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clow323fc5b2018-01-16 15:48:27 +0000157 constexpr ForwardIterator1 // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000158 search(ForwardIterator1 first1, ForwardIterator1 last1,
159 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
160
161template <class ForwardIterator, class Size, class T>
Marshall Clow323fc5b2018-01-16 15:48:27 +0000162 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000163 search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
164
165template <class ForwardIterator, class Size, class T, class BinaryPredicate>
Marshall Clow323fc5b2018-01-16 15:48:27 +0000166 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000167 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 Clow31427c62018-01-19 17:45:39 +0000197 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000198 transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
199
200template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
Marshall Clow31427c62018-01-19 17:45:39 +0000201 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000202 transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
203 OutputIterator result, BinaryOperation binary_op);
204
205template <class ForwardIterator, class T>
Marshall Clow01bbbd22018-01-19 18:07:29 +0000206 constexpr void // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207 replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
208
209template <class ForwardIterator, class Predicate, class T>
Marshall Clow01bbbd22018-01-19 18:07:29 +0000210 constexpr void // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000211 replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
212
213template <class InputIterator, class OutputIterator, class T>
Marshall Clow01bbbd22018-01-19 18:07:29 +0000214 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215 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 Clow01bbbd22018-01-19 18:07:29 +0000219 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000220 replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
221
222template <class ForwardIterator, class T>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +0000223 constexpr void // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000224 fill(ForwardIterator first, ForwardIterator last, const T& value);
225
226template <class OutputIterator, class Size, class T>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +0000227 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000228 fill_n(OutputIterator first, Size n, const T& value);
229
230template <class ForwardIterator, class Generator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +0000231 constexpr void // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000232 generate(ForwardIterator first, ForwardIterator last, Generator gen);
233
234template <class OutputIterator, class Size, class Generator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +0000235 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000236 generate_n(OutputIterator first, Size n, Generator gen);
237
238template <class ForwardIterator, class T>
Marshall Clow7c0fbd82018-01-22 21:43:04 +0000239 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000240 remove(ForwardIterator first, ForwardIterator last, const T& value);
241
242template <class ForwardIterator, class Predicate>
Marshall Clow7c0fbd82018-01-22 21:43:04 +0000243 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000244 remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
245
246template <class InputIterator, class OutputIterator, class T>
Marshall Clow7c0fbd82018-01-22 21:43:04 +0000247 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000248 remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
249
250template <class InputIterator, class OutputIterator, class Predicate>
Marshall Clow7c0fbd82018-01-22 21:43:04 +0000251 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000252 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 Clow7c0fbd82018-01-22 21:43:04 +0000275 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000276 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 Clowfac06e52017-03-23 13:43:37 +0000288 random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289
290template <class RandomAccessIterator, class RandomNumberGenerator>
291 void
Marshall Clow83f35392014-03-03 06:14:19 +0000292 random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
Marshall Clowfac06e52017-03-23 13:43:37 +0000293 RandomNumberGenerator& rand); // deprecated in C++14, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000294
Eric Fiselier1208fcd2016-08-28 22:14:37 +0000295template<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 Hinnant578ac0f2010-05-26 17:49:34 +0000301template<class RandomAccessIterator, class UniformRandomNumberGenerator>
302 void shuffle(RandomAccessIterator first, RandomAccessIterator last,
Howard Hinnanta5e71782010-11-18 01:47:02 +0000303 UniformRandomNumberGenerator&& g);
Howard Hinnant578ac0f2010-05-26 17:49:34 +0000304
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305template <class InputIterator, class Predicate>
Marshall Clow96d050a2018-01-15 16:16:32 +0000306 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000307 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 Clow5492c8a2018-01-22 20:44:33 +0000315 constexpr pair<OutputIterator1, OutputIterator2> // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316 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 Clowe00916c2018-01-16 02:34:41 +0000325 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000326 partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
327
328template <class ForwardIterator>
Marshall Clow96d050a2018-01-15 16:16:32 +0000329 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000330 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 Clow3c0558f2018-01-15 19:40:34 +0000337 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000338 is_sorted_until(ForwardIterator first, ForwardIterator last);
339
340template <class ForwardIterator, class Compare>
Marshall Clow3c0558f2018-01-15 19:40:34 +0000341 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000342 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 Clowe00916c2018-01-16 02:34:41 +0000387 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000388 lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
389
390template <class ForwardIterator, class T, class Compare>
Marshall Clowe00916c2018-01-16 02:34:41 +0000391 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000392 lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
393
394template <class ForwardIterator, class T>
Marshall Clowe00916c2018-01-16 02:34:41 +0000395 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396 upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
397
398template <class ForwardIterator, class T, class Compare>
Marshall Clowe00916c2018-01-16 02:34:41 +0000399 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000400 upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
401
402template <class ForwardIterator, class T>
Marshall Clowe00916c2018-01-16 02:34:41 +0000403 constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000404 equal_range(ForwardIterator first, ForwardIterator last, const T& value);
405
406template <class ForwardIterator, class T, class Compare>
Marshall Clowe00916c2018-01-16 02:34:41 +0000407 constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000408 equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
409
410template <class ForwardIterator, class T>
Marshall Clowe00916c2018-01-16 02:34:41 +0000411 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000412 binary_search(ForwardIterator first, ForwardIterator last, const T& value);
413
414template <class ForwardIterator, class T, class Compare>
Marshall Clowc0b7f972018-01-22 23:10:40 +0000415 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416 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 Clowc0b7f972018-01-22 23:10:40 +0000437 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000438 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
439
440template <class InputIterator1, class InputIterator2, class Compare>
Marshall Clowc0b7f972018-01-22 23:10:40 +0000441 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000442 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 Clowc0b7f972018-01-22 23:10:40 +0000455 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000456 set_intersection(InputIterator1 first1, InputIterator1 last1,
457 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
458
459template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
Marshall Clowc0b7f972018-01-22 23:10:40 +0000460 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000461 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 Hinnant3b6579a2010-08-22 00:02:43 +0000516template <class RandomAccessIterator>
Marshall Clow96d050a2018-01-15 16:16:32 +0000517 constexpr bool // constexpr in C++20
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000518 is_heap(RandomAccessIterator first, RandomAccessiterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000519
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000520template <class RandomAccessIterator, class Compare>
Marshall Clow96d050a2018-01-15 16:16:32 +0000521 constexpr bool // constexpr in C++20
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000522 is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000523
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000524template <class RandomAccessIterator>
Marshall Clow96d050a2018-01-15 16:16:32 +0000525 constexpr RandomAccessIterator // constexpr in C++20
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000526 is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000527
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000528template <class RandomAccessIterator, class Compare>
Marshall Clow96d050a2018-01-15 16:16:32 +0000529 constexpr RandomAccessIterator // constexpr in C++20
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000530 is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000531
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000532template <class ForwardIterator>
533 ForwardIterator
Marshall Clow9e173072015-05-10 13:53:31 +0000534 min_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000535
536template <class ForwardIterator, class Compare>
537 ForwardIterator
Marshall Clow9e173072015-05-10 13:53:31 +0000538 min_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000539
Howard Hinnantc51e1022010-05-11 19:42:16 +0000540template <class T>
541 const T&
Marshall Clowe9dca072014-02-19 16:51:35 +0000542 min(const T& a, const T& b); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543
544template <class T, class Compare>
545 const T&
Marshall Clowe9dca072014-02-19 16:51:35 +0000546 min(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000547
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000548template<class T>
549 T
Marshall Clowe9dca072014-02-19 16:51:35 +0000550 min(initializer_list<T> t); // constexpr in C++14
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000551
552template<class T, class Compare>
553 T
Marshall Clowe9dca072014-02-19 16:51:35 +0000554 min(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000555
Marshall Clow3e18d0e2016-03-07 22:43:49 +0000556template<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 Hinnantb120e7a2010-08-21 20:10:01 +0000562template <class ForwardIterator>
563 ForwardIterator
Marshall Clow9e173072015-05-10 13:53:31 +0000564 max_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000565
566template <class ForwardIterator, class Compare>
567 ForwardIterator
Marshall Clow9e173072015-05-10 13:53:31 +0000568 max_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000569
Howard Hinnantc51e1022010-05-11 19:42:16 +0000570template <class T>
571 const T&
Marshall Clowe9dca072014-02-19 16:51:35 +0000572 max(const T& a, const T& b); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000573
574template <class T, class Compare>
575 const T&
Marshall Clowe9dca072014-02-19 16:51:35 +0000576 max(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000577
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000578template<class T>
579 T
Marshall Clowe9dca072014-02-19 16:51:35 +0000580 max(initializer_list<T> t); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000581
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000582template<class T, class Compare>
583 T
Marshall Clowe9dca072014-02-19 16:51:35 +0000584 max(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000585
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000586template<class ForwardIterator>
587 pair<ForwardIterator, ForwardIterator>
Marshall Clow9e173072015-05-10 13:53:31 +0000588 minmax_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000589
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000590template<class ForwardIterator, class Compare>
591 pair<ForwardIterator, ForwardIterator>
Marshall Clow9e173072015-05-10 13:53:31 +0000592 minmax_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000593
594template<class T>
595 pair<const T&, const T&>
Marshall Clowe9dca072014-02-19 16:51:35 +0000596 minmax(const T& a, const T& b); // constexpr in C++14
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000597
598template<class T, class Compare>
599 pair<const T&, const T&>
Marshall Clowe9dca072014-02-19 16:51:35 +0000600 minmax(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000601
602template<class T>
603 pair<T, T>
Marshall Clowe9dca072014-02-19 16:51:35 +0000604 minmax(initializer_list<T> t); // constexpr in C++14
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000605
606template<class T, class Compare>
607 pair<T, T>
Marshall Clowe9dca072014-02-19 16:51:35 +0000608 minmax(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000609
610template <class InputIterator1, class InputIterator2>
Marshall Clow5492c8a2018-01-22 20:44:33 +0000611 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612 lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
613
614template <class InputIterator1, class InputIterator2, class Compare>
Marshall Clow5492c8a2018-01-22 20:44:33 +0000615 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000616 lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
617 InputIterator2 first2, InputIterator2 last2, Compare comp);
618
619template <class BidirectionalIterator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000620 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +0000621 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 Fiselier6bfed252016-04-21 23:38:59 +0000643#include <utility> // needed to provide swap_ranges.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000644#include <memory>
Marshall Clowa40686b2018-01-08 19:18:00 +0000645#include <functional>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000646#include <iterator>
Howard Hinnant60e5cf42012-07-26 17:09:09 +0000647#include <cstddef>
Marshall Clowbf32ec92018-08-17 16:07:48 +0000648#include <bit>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000649#include <version>
Howard Hinnantea382952013-08-14 18:00:20 +0000650
Eric Fiselier14b6de92014-08-10 23:53:08 +0000651#include <__debug>
652
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000653#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000654#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000655#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000656
Eric Fiselierf4433a32017-05-31 22:07:49 +0000657_LIBCPP_PUSH_MACROS
658#include <__undef_macros>
659
660
Howard Hinnantc51e1022010-05-11 19:42:16 +0000661_LIBCPP_BEGIN_NAMESPACE_STD
662
Marshall Clowe9dca072014-02-19 16:51:35 +0000663// I'd like to replace these with _VSTD::equal_to<void>, but can't because:
664// * That only works with C++14 and later, and
665// * We haven't included <functional> here.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000666template <class _T1, class _T2 = _T1>
667struct __equal_to
668{
Marshall Clow1f695172018-07-14 04:15:19 +0000669 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
670 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
671 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
672 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673};
674
675template <class _T1>
676struct __equal_to<_T1, _T1>
677{
Marshall Clowe9dca072014-02-19 16:51:35 +0000678 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
679 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680};
681
682template <class _T1>
683struct __equal_to<const _T1, _T1>
684{
Marshall Clowe9dca072014-02-19 16:51:35 +0000685 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
686 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000687};
688
689template <class _T1>
690struct __equal_to<_T1, const _T1>
691{
Marshall Clowe9dca072014-02-19 16:51:35 +0000692 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
693 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000694};
695
696template <class _T1, class _T2 = _T1>
697struct __less
698{
Aditya Kumar3a0179a2016-08-25 11:52:38 +0000699 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowe9dca072014-02-19 16:51:35 +0000700 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
701
702 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
703 bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
704
705 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
706 bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
707
708 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
709 bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000710};
711
712template <class _T1>
713struct __less<_T1, _T1>
714{
Marshall Clowe9dca072014-02-19 16:51:35 +0000715 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
716 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000717};
718
719template <class _T1>
720struct __less<const _T1, _T1>
721{
Marshall Clowe9dca072014-02-19 16:51:35 +0000722 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
723 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000724};
725
726template <class _T1>
727struct __less<_T1, const _T1>
728{
Marshall Clowe9dca072014-02-19 16:51:35 +0000729 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
730 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731};
732
733template <class _Predicate>
Marshall Clow738d1042017-08-28 23:16:13 +0000734class __invert // invert the sense of a comparison
Howard Hinnantc51e1022010-05-11 19:42:16 +0000735{
736private:
737 _Predicate __p_;
738public:
Marshall Clow738d1042017-08-28 23:16:13 +0000739 _LIBCPP_INLINE_VISIBILITY __invert() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000740
741 _LIBCPP_INLINE_VISIBILITY
Marshall Clow738d1042017-08-28 23:16:13 +0000742 explicit __invert(_Predicate __p) : __p_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000743
744 template <class _T1>
745 _LIBCPP_INLINE_VISIBILITY
746 bool operator()(const _T1& __x) {return !__p_(__x);}
747
748 template <class _T1, class _T2>
749 _LIBCPP_INLINE_VISIBILITY
Marshall Clow738d1042017-08-28 23:16:13 +0000750 bool operator()(const _T1& __x, const _T2& __y) {return __p_(__y, __x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000751};
752
Howard Hinnant6148a9b2013-08-23 20:10:18 +0000753#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000754
755template <class _Compare>
756struct __debug_less
757{
758 _Compare __comp_;
759 __debug_less(_Compare& __c) : __comp_(__c) {}
Eric Fiselier32adec82016-07-19 23:27:18 +0000760
Howard Hinnantc51e1022010-05-11 19:42:16 +0000761 template <class _Tp, class _Up>
762 bool operator()(const _Tp& __x, const _Up& __y)
763 {
764 bool __r = __comp_(__x, __y);
765 if (__r)
Eric Fiselier32adec82016-07-19 23:27:18 +0000766 __do_compare_assert(0, __y, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000767 return __r;
768 }
Eric Fiselier32adec82016-07-19 23:27:18 +0000769
770 template <class _LHS, class _RHS>
771 inline _LIBCPP_INLINE_VISIBILITY
772 decltype((void)_VSTD::declval<_Compare&>()(
773 _VSTD::declval<_LHS const&>(), _VSTD::declval<_RHS const&>()))
774 __do_compare_assert(int, _LHS const& __l, _RHS const& __r) {
775 _LIBCPP_ASSERT(!__comp_(__l, __r),
776 "Comparator does not induce a strict weak ordering");
777 }
778
779 template <class _LHS, class _RHS>
780 inline _LIBCPP_INLINE_VISIBILITY
781 void __do_compare_assert(long, _LHS const&, _RHS const&) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000782};
783
Howard Hinnant6148a9b2013-08-23 20:10:18 +0000784#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000785
786// all_of
787
788template <class _InputIterator, class _Predicate>
Marshall Clowd607fdb2018-01-15 17:20:36 +0000789inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000790bool
791all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
792{
793 for (; __first != __last; ++__first)
794 if (!__pred(*__first))
795 return false;
796 return true;
797}
798
799// any_of
800
801template <class _InputIterator, class _Predicate>
Marshall Clowd607fdb2018-01-15 17:20:36 +0000802inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000803bool
804any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
805{
806 for (; __first != __last; ++__first)
807 if (__pred(*__first))
808 return true;
809 return false;
810}
811
812// none_of
813
814template <class _InputIterator, class _Predicate>
Marshall Clowd607fdb2018-01-15 17:20:36 +0000815inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816bool
817none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
818{
819 for (; __first != __last; ++__first)
820 if (__pred(*__first))
821 return false;
822 return true;
823}
824
825// for_each
826
827template <class _InputIterator, class _Function>
Marshall Clow5492c8a2018-01-22 20:44:33 +0000828inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000829_Function
830for_each(_InputIterator __first, _InputIterator __last, _Function __f)
831{
832 for (; __first != __last; ++__first)
833 __f(*__first);
Marshall Clow78dbe462016-11-14 18:22:19 +0000834 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000835}
836
Marshall Clowc588fd62017-05-25 13:40:57 +0000837#if _LIBCPP_STD_VER > 14
Marshall Clowde0169c2017-05-25 02:29:54 +0000838// for_each_n
839
840template <class _InputIterator, class _Size, class _Function>
Marshall Clow5492c8a2018-01-22 20:44:33 +0000841inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowde0169c2017-05-25 02:29:54 +0000842_InputIterator
843for_each_n(_InputIterator __first, _Size __orig_n, _Function __f)
844{
845 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
846 _IntegralSize __n = __orig_n;
847 while (__n > 0)
848 {
849 __f(*__first);
850 ++__first;
851 --__n;
852 }
853 return __first;
854}
Marshall Clowc588fd62017-05-25 13:40:57 +0000855#endif
Marshall Clowde0169c2017-05-25 02:29:54 +0000856
Howard Hinnantc51e1022010-05-11 19:42:16 +0000857// find
858
859template <class _InputIterator, class _Tp>
Marshall Clow96d050a2018-01-15 16:16:32 +0000860inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000861_InputIterator
Howard Hinnantbf074022011-10-22 20:59:45 +0000862find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000863{
864 for (; __first != __last; ++__first)
Howard Hinnantbf074022011-10-22 20:59:45 +0000865 if (*__first == __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000866 break;
867 return __first;
868}
869
870// find_if
871
872template <class _InputIterator, class _Predicate>
Marshall Clow96d050a2018-01-15 16:16:32 +0000873inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000874_InputIterator
875find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
876{
877 for (; __first != __last; ++__first)
878 if (__pred(*__first))
879 break;
880 return __first;
881}
882
883// find_if_not
884
885template<class _InputIterator, class _Predicate>
Marshall Clowee0161e2018-01-15 19:26:05 +0000886inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000887_InputIterator
888find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
889{
890 for (; __first != __last; ++__first)
891 if (!__pred(*__first))
892 break;
893 return __first;
894}
895
896// find_end
897
898template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clowee0161e2018-01-15 19:26:05 +0000899_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1
Howard Hinnantc51e1022010-05-11 19:42:16 +0000900__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
901 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
902 forward_iterator_tag, forward_iterator_tag)
903{
904 // modeled after search algorithm
905 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
906 if (__first2 == __last2)
907 return __r;
908 while (true)
909 {
910 while (true)
911 {
912 if (__first1 == __last1) // if source exhausted return last correct answer
913 return __r; // (or __last1 if never found)
914 if (__pred(*__first1, *__first2))
915 break;
916 ++__first1;
917 }
918 // *__first1 matches *__first2, now match elements after here
919 _ForwardIterator1 __m1 = __first1;
920 _ForwardIterator2 __m2 = __first2;
921 while (true)
922 {
923 if (++__m2 == __last2)
924 { // Pattern exhaused, record answer and search for another one
925 __r = __first1;
926 ++__first1;
927 break;
928 }
929 if (++__m1 == __last1) // Source exhausted, return last answer
930 return __r;
931 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
932 {
933 ++__first1;
934 break;
935 } // else there is a match, check next elements
936 }
937 }
938}
939
940template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
Marshall Clowee0161e2018-01-15 19:26:05 +0000941_LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator1
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
943 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
944 bidirectional_iterator_tag, bidirectional_iterator_tag)
945{
946 // modeled after search algorithm (in reverse)
947 if (__first2 == __last2)
948 return __last1; // Everything matches an empty sequence
949 _BidirectionalIterator1 __l1 = __last1;
950 _BidirectionalIterator2 __l2 = __last2;
951 --__l2;
952 while (true)
953 {
954 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
955 while (true)
956 {
957 if (__first1 == __l1) // return __last1 if no element matches *__first2
958 return __last1;
959 if (__pred(*--__l1, *__l2))
960 break;
961 }
962 // *__l1 matches *__l2, now match elements before here
963 _BidirectionalIterator1 __m1 = __l1;
964 _BidirectionalIterator2 __m2 = __l2;
965 while (true)
966 {
967 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
968 return __m1;
969 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
970 return __last1;
971 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
972 {
973 break;
974 } // else there is a match, check next elements
975 }
976 }
977}
978
979template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clowedfd25a2014-06-10 18:51:55 +0000980_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
982 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
983 random_access_iterator_tag, random_access_iterator_tag)
984{
985 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
986 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
987 if (__len2 == 0)
988 return __last1;
989 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
990 if (__len1 < __len2)
991 return __last1;
992 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
993 _RandomAccessIterator1 __l1 = __last1;
994 _RandomAccessIterator2 __l2 = __last2;
995 --__l2;
996 while (true)
997 {
998 while (true)
999 {
1000 if (__s == __l1)
1001 return __last1;
1002 if (__pred(*--__l1, *__l2))
1003 break;
1004 }
1005 _RandomAccessIterator1 __m1 = __l1;
1006 _RandomAccessIterator2 __m2 = __l2;
1007 while (true)
1008 {
1009 if (__m2 == __first2)
1010 return __m1;
1011 // no need to check range on __m1 because __s guarantees we have enough source
1012 if (!__pred(*--__m1, *--__m2))
1013 {
1014 break;
1015 }
1016 }
1017 }
1018}
1019
1020template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clowee0161e2018-01-15 19:26:05 +00001021inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022_ForwardIterator1
1023find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1024 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1025{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001026 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027 (__first1, __last1, __first2, __last2, __pred,
1028 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1029 typename iterator_traits<_ForwardIterator2>::iterator_category());
1030}
1031
1032template <class _ForwardIterator1, class _ForwardIterator2>
Marshall Clowee0161e2018-01-15 19:26:05 +00001033inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034_ForwardIterator1
1035find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1036 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1037{
1038 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1039 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001040 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041}
1042
1043// find_first_of
1044
1045template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clowedfd25a2014-06-10 18:51:55 +00001046_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1047__find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001048 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1049{
1050 for (; __first1 != __last1; ++__first1)
1051 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1052 if (__pred(*__first1, *__j))
1053 return __first1;
1054 return __last1;
1055}
1056
Marshall Clowedfd25a2014-06-10 18:51:55 +00001057
1058template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clowee0161e2018-01-15 19:26:05 +00001059inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowedfd25a2014-06-10 18:51:55 +00001060_ForwardIterator1
1061find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1062 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1063{
1064 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1065}
1066
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067template <class _ForwardIterator1, class _ForwardIterator2>
Marshall Clowee0161e2018-01-15 19:26:05 +00001068inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069_ForwardIterator1
1070find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1071 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1072{
1073 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1074 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Marshall Clowedfd25a2014-06-10 18:51:55 +00001075 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001076}
1077
1078// adjacent_find
1079
1080template <class _ForwardIterator, class _BinaryPredicate>
Marshall Clowee0161e2018-01-15 19:26:05 +00001081inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082_ForwardIterator
1083adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1084{
1085 if (__first != __last)
1086 {
1087 _ForwardIterator __i = __first;
1088 while (++__i != __last)
1089 {
1090 if (__pred(*__first, *__i))
1091 return __first;
1092 __first = __i;
1093 }
1094 }
1095 return __last;
1096}
1097
1098template <class _ForwardIterator>
Marshall Clowee0161e2018-01-15 19:26:05 +00001099inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001100_ForwardIterator
1101adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1102{
1103 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001104 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001105}
1106
1107// count
1108
1109template <class _InputIterator, class _Tp>
Marshall Clow3c0558f2018-01-15 19:40:34 +00001110inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111typename iterator_traits<_InputIterator>::difference_type
Howard Hinnantbf074022011-10-22 20:59:45 +00001112count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001113{
1114 typename iterator_traits<_InputIterator>::difference_type __r(0);
1115 for (; __first != __last; ++__first)
Howard Hinnantbf074022011-10-22 20:59:45 +00001116 if (*__first == __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117 ++__r;
1118 return __r;
1119}
1120
1121// count_if
1122
1123template <class _InputIterator, class _Predicate>
Marshall Clow3c0558f2018-01-15 19:40:34 +00001124inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125typename iterator_traits<_InputIterator>::difference_type
1126count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1127{
1128 typename iterator_traits<_InputIterator>::difference_type __r(0);
1129 for (; __first != __last; ++__first)
1130 if (__pred(*__first))
1131 ++__r;
1132 return __r;
1133}
1134
1135// mismatch
1136
1137template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Marshall Clow30bf3022018-01-16 02:04:10 +00001138inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001139pair<_InputIterator1, _InputIterator2>
1140mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1141 _InputIterator2 __first2, _BinaryPredicate __pred)
1142{
Marshall Clow2932e512014-09-16 20:40:05 +00001143 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144 if (!__pred(*__first1, *__first2))
1145 break;
1146 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1147}
1148
1149template <class _InputIterator1, class _InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001150inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001151pair<_InputIterator1, _InputIterator2>
1152mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1153{
1154 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1155 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001156 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157}
1158
Marshall Clow96b42b22013-05-09 21:14:23 +00001159#if _LIBCPP_STD_VER > 11
1160template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Marshall Clow30bf3022018-01-16 02:04:10 +00001161inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001162pair<_InputIterator1, _InputIterator2>
1163mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1164 _InputIterator2 __first2, _InputIterator2 __last2,
1165 _BinaryPredicate __pred)
1166{
Marshall Clow2932e512014-09-16 20:40:05 +00001167 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow96b42b22013-05-09 21:14:23 +00001168 if (!__pred(*__first1, *__first2))
1169 break;
1170 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1171}
1172
1173template <class _InputIterator1, class _InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001174inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001175pair<_InputIterator1, _InputIterator2>
1176mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1177 _InputIterator2 __first2, _InputIterator2 __last2)
1178{
1179 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1180 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1181 return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1182}
1183#endif
1184
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185// equal
1186
1187template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Marshall Clow30bf3022018-01-16 02:04:10 +00001188inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189bool
1190equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1191{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001192 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193 if (!__pred(*__first1, *__first2))
1194 return false;
1195 return true;
1196}
1197
1198template <class _InputIterator1, class _InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001199inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200bool
1201equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1202{
1203 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1204 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001205 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206}
1207
Marshall Clow96b42b22013-05-09 21:14:23 +00001208#if _LIBCPP_STD_VER > 11
1209template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001210inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001211bool
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001212__equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow96b42b22013-05-09 21:14:23 +00001213 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1214 input_iterator_tag, input_iterator_tag )
1215{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001216 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow96b42b22013-05-09 21:14:23 +00001217 if (!__pred(*__first1, *__first2))
1218 return false;
1219 return __first1 == __last1 && __first2 == __last2;
1220}
1221
1222template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001223inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001224bool
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001225__equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1226 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
Marshall Clow96b42b22013-05-09 21:14:23 +00001227 random_access_iterator_tag, random_access_iterator_tag )
1228{
1229 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1230 return false;
1231 return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1232 typename add_lvalue_reference<_BinaryPredicate>::type>
1233 (__first1, __last1, __first2, __pred );
1234}
1235
1236template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Marshall Clow30bf3022018-01-16 02:04:10 +00001237inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001238bool
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001239equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow96b42b22013-05-09 21:14:23 +00001240 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1241{
1242 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001243 (__first1, __last1, __first2, __last2, __pred,
Marshall Clow96b42b22013-05-09 21:14:23 +00001244 typename iterator_traits<_InputIterator1>::iterator_category(),
1245 typename iterator_traits<_InputIterator2>::iterator_category());
1246}
1247
1248template <class _InputIterator1, class _InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001249inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001250bool
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001251equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow96b42b22013-05-09 21:14:23 +00001252 _InputIterator2 __first2, _InputIterator2 __last2)
1253{
1254 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1255 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1256 return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1257 typename iterator_traits<_InputIterator1>::iterator_category(),
1258 typename iterator_traits<_InputIterator2>::iterator_category());
1259}
1260#endif
1261
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262// is_permutation
1263
1264template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow96d050a2018-01-15 16:16:32 +00001265_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00001266is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1267 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1268{
Marshall Clow96d050a2018-01-15 16:16:32 +00001269// shorten sequences as much as possible by lopping of any equal prefix
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001270 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001271 if (!__pred(*__first1, *__first2))
Marshall Clow96d050a2018-01-15 16:16:32 +00001272 break;
1273 if (__first1 == __last1)
1274 return true;
1275
1276// __first1 != __last1 && *__first1 != *__first2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001277 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001278 _D1 __l1 = _VSTD::distance(__first1, __last1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001279 if (__l1 == _D1(1))
1280 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001281 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282 // For each element in [f1, l1) see if there are the same number of
1283 // equal elements in [f2, l2)
1284 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1285 {
Marshall Clow96d050a2018-01-15 16:16:32 +00001286 // Have we already counted the number of *__i in [f1, l1)?
Peter Collingbournea5b451b2018-01-26 21:23:27 +00001287 _ForwardIterator1 __match = __first1;
1288 for (; __match != __i; ++__match)
1289 if (__pred(*__match, *__i))
1290 break;
1291 if (__match == __i) {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001292 // Count number of *__i in [f2, l2)
1293 _D1 __c2 = 0;
1294 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1295 if (__pred(*__i, *__j))
1296 ++__c2;
1297 if (__c2 == 0)
1298 return false;
1299 // Count number of *__i in [__i, l1) (we can start with 1)
1300 _D1 __c1 = 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001301 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302 if (__pred(*__i, *__j))
1303 ++__c1;
1304 if (__c1 != __c2)
1305 return false;
1306 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001307 }
1308 return true;
1309}
1310
1311template<class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow96d050a2018-01-15 16:16:32 +00001312inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001313bool
1314is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1315 _ForwardIterator2 __first2)
1316{
1317 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1318 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001319 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001320}
1321
Marshall Clow96b42b22013-05-09 21:14:23 +00001322#if _LIBCPP_STD_VER > 11
1323template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow96d050a2018-01-15 16:16:32 +00001324_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Marshall Clow96b42b22013-05-09 21:14:23 +00001325__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001326 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
Marshall Clow96b42b22013-05-09 21:14:23 +00001327 _BinaryPredicate __pred,
1328 forward_iterator_tag, forward_iterator_tag )
1329{
Marshall Clow96d050a2018-01-15 16:16:32 +00001330// shorten sequences as much as possible by lopping of any equal prefix
Eric Fiselier14673892014-10-27 20:26:25 +00001331 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow96b42b22013-05-09 21:14:23 +00001332 if (!__pred(*__first1, *__first2))
Marshall Clow96d050a2018-01-15 16:16:32 +00001333 break;
1334 if (__first1 == __last1)
Marshall Clow01bbbd22018-01-19 18:07:29 +00001335 return __first2 == __last2;
Marshall Clow96d050a2018-01-15 16:16:32 +00001336 else if (__first2 == __last2)
Marshall Clow01bbbd22018-01-19 18:07:29 +00001337 return false;
Marshall Clow96d050a2018-01-15 16:16:32 +00001338
Marshall Clow96b42b22013-05-09 21:14:23 +00001339 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1340 _D1 __l1 = _VSTD::distance(__first1, __last1);
1341
1342 typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
Marshall Clow42b967c2013-05-10 00:16:10 +00001343 _D2 __l2 = _VSTD::distance(__first2, __last2);
Marshall Clow96b42b22013-05-09 21:14:23 +00001344 if (__l1 != __l2)
1345 return false;
1346
1347 // For each element in [f1, l1) see if there are the same number of
1348 // equal elements in [f2, l2)
1349 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1350 {
Marshall Clow96d050a2018-01-15 16:16:32 +00001351 // Have we already counted the number of *__i in [f1, l1)?
Peter Collingbournea5b451b2018-01-26 21:23:27 +00001352 _ForwardIterator1 __match = __first1;
1353 for (; __match != __i; ++__match)
1354 if (__pred(*__match, *__i))
1355 break;
1356 if (__match == __i) {
Marshall Clow96b42b22013-05-09 21:14:23 +00001357 // Count number of *__i in [f2, l2)
1358 _D1 __c2 = 0;
1359 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1360 if (__pred(*__i, *__j))
1361 ++__c2;
1362 if (__c2 == 0)
1363 return false;
1364 // Count number of *__i in [__i, l1) (we can start with 1)
1365 _D1 __c1 = 1;
1366 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1367 if (__pred(*__i, *__j))
1368 ++__c1;
1369 if (__c1 != __c2)
1370 return false;
1371 }
Marshall Clow96b42b22013-05-09 21:14:23 +00001372 }
1373 return true;
1374}
1375
1376template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow96d050a2018-01-15 16:16:32 +00001377_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Marshall Clow96b42b22013-05-09 21:14:23 +00001378__is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001379 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
Marshall Clow96b42b22013-05-09 21:14:23 +00001380 _BinaryPredicate __pred,
1381 random_access_iterator_tag, random_access_iterator_tag )
1382{
1383 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1384 return false;
1385 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1386 typename add_lvalue_reference<_BinaryPredicate>::type>
1387 (__first1, __last1, __first2, __pred );
1388}
1389
1390template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow96d050a2018-01-15 16:16:32 +00001391inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001392bool
1393is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1394 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1395 _BinaryPredicate __pred )
1396{
1397 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1398 (__first1, __last1, __first2, __last2, __pred,
1399 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1400 typename iterator_traits<_ForwardIterator2>::iterator_category());
1401}
1402
1403template<class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow96d050a2018-01-15 16:16:32 +00001404inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001405bool
1406is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1407 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1408{
1409 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1410 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1411 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1412 __equal_to<__v1, __v2>(),
1413 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1414 typename iterator_traits<_ForwardIterator2>::iterator_category());
1415}
1416#endif
1417
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418// search
Marshall Clowa40686b2018-01-08 19:18:00 +00001419// __search is in <functional>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001420
1421template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001422inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001423_ForwardIterator1
1424search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1425 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1426{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001427 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428 (__first1, __last1, __first2, __last2, __pred,
Marshall Clowaf8be6c2016-03-08 15:12:52 +00001429 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1430 typename iterator_traits<_ForwardIterator2>::iterator_category())
1431 .first;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432}
1433
1434template <class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001435inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436_ForwardIterator1
1437search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1438 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1439{
Marshall Clowaf8be6c2016-03-08 15:12:52 +00001440 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1441 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001442 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443}
1444
Marshall Clowa40686b2018-01-08 19:18:00 +00001445
1446#if _LIBCPP_STD_VER > 14
1447template <class _ForwardIterator, class _Searcher>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001448_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowa40686b2018-01-08 19:18:00 +00001449_ForwardIterator search(_ForwardIterator __f, _ForwardIterator __l, const _Searcher &__s)
1450{ return __s(__f, __l).first; }
1451#endif
1452
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453// search_n
1454
1455template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001456_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457__search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnantbf074022011-10-22 20:59:45 +00001458 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459{
1460 if (__count <= 0)
1461 return __first;
1462 while (true)
1463 {
Howard Hinnantbf074022011-10-22 20:59:45 +00001464 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantc51e1022010-05-11 19:42:16 +00001465 while (true)
1466 {
Howard Hinnantbf074022011-10-22 20:59:45 +00001467 if (__first == __last) // return __last if no element matches __value_
Howard Hinnantc51e1022010-05-11 19:42:16 +00001468 return __last;
Howard Hinnantbf074022011-10-22 20:59:45 +00001469 if (__pred(*__first, __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001470 break;
1471 ++__first;
1472 }
Howard Hinnantbf074022011-10-22 20:59:45 +00001473 // *__first matches __value_, now match elements after here
Howard Hinnantc51e1022010-05-11 19:42:16 +00001474 _ForwardIterator __m = __first;
1475 _Size __c(0);
1476 while (true)
1477 {
1478 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1479 return __first;
1480 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1481 return __last;
Howard Hinnantbf074022011-10-22 20:59:45 +00001482 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantc51e1022010-05-11 19:42:16 +00001483 {
1484 __first = __m;
1485 ++__first;
1486 break;
1487 } // else there is a match, check next elements
1488 }
1489 }
1490}
1491
1492template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001493_LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001494__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnantbf074022011-10-22 20:59:45 +00001495 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001496{
1497 if (__count <= 0)
1498 return __first;
1499 _Size __len = static_cast<_Size>(__last - __first);
1500 if (__len < __count)
1501 return __last;
1502 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1503 while (true)
1504 {
Howard Hinnantbf074022011-10-22 20:59:45 +00001505 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantc51e1022010-05-11 19:42:16 +00001506 while (true)
1507 {
Howard Hinnantbede4c32013-04-04 15:40:48 +00001508 if (__first >= __s) // return __last if no element matches __value_
Howard Hinnantc51e1022010-05-11 19:42:16 +00001509 return __last;
Howard Hinnantbf074022011-10-22 20:59:45 +00001510 if (__pred(*__first, __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001511 break;
1512 ++__first;
1513 }
Howard Hinnantbf074022011-10-22 20:59:45 +00001514 // *__first matches __value_, now match elements after here
Howard Hinnantc51e1022010-05-11 19:42:16 +00001515 _RandomAccessIterator __m = __first;
1516 _Size __c(0);
1517 while (true)
1518 {
1519 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1520 return __first;
1521 ++__m; // no need to check range on __m because __s guarantees we have enough source
Howard Hinnantbf074022011-10-22 20:59:45 +00001522 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523 {
1524 __first = __m;
1525 ++__first;
1526 break;
1527 } // else there is a match, check next elements
1528 }
1529 }
1530}
1531
1532template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001533inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534_ForwardIterator
1535search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnantbf074022011-10-22 20:59:45 +00001536 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001537{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001538 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001539 (__first, __last, __convert_to_integral(__count), __value_, __pred,
1540 typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541}
1542
1543template <class _ForwardIterator, class _Size, class _Tp>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001544inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00001546search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547{
1548 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001549 return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
1550 __value_, __equal_to<__v, _Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001551}
1552
1553// copy
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554template <class _Iter>
1555inline _LIBCPP_INLINE_VISIBILITY
1556_Iter
1557__unwrap_iter(_Iter __i)
1558{
1559 return __i;
1560}
1561
1562template <class _Tp>
Marshall Clow88880de2017-05-25 14:20:26 +00001563inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564typename enable_if
1565<
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001566 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567 _Tp*
1568>::type
1569__unwrap_iter(move_iterator<_Tp*> __i)
1570{
1571 return __i.base();
1572}
1573
Howard Hinnant8ea98242013-08-23 17:37:05 +00001574#if _LIBCPP_DEBUG_LEVEL < 2
1575
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576template <class _Tp>
Marshall Clowae4f8312018-07-13 16:35:26 +00001577inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578typename enable_if
1579<
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001580 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581 _Tp*
1582>::type
1583__unwrap_iter(__wrap_iter<_Tp*> __i)
1584{
1585 return __i.base();
1586}
1587
Eric Fiselier38badb82016-12-28 05:35:32 +00001588#else
1589
1590template <class _Tp>
Marshall Clowae4f8312018-07-13 16:35:26 +00001591inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Eric Fiselier38badb82016-12-28 05:35:32 +00001592typename enable_if
1593<
1594 is_trivially_copy_assignable<_Tp>::value,
1595 __wrap_iter<_Tp*>
1596>::type
1597__unwrap_iter(__wrap_iter<_Tp*> __i)
1598{
1599 return __i;
1600}
1601
Howard Hinnant8ea98242013-08-23 17:37:05 +00001602#endif // _LIBCPP_DEBUG_LEVEL < 2
1603
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604template <class _InputIterator, class _OutputIterator>
1605inline _LIBCPP_INLINE_VISIBILITY
1606_OutputIterator
1607__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1608{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001609 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001610 *__result = *__first;
1611 return __result;
1612}
1613
1614template <class _Tp, class _Up>
1615inline _LIBCPP_INLINE_VISIBILITY
1616typename enable_if
1617<
1618 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001619 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001620 _Up*
1621>::type
1622__copy(_Tp* __first, _Tp* __last, _Up* __result)
1623{
1624 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clowc3ef9622015-06-02 13:52:16 +00001625 if (__n > 0)
1626 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001627 return __result + __n;
1628}
1629
1630template <class _InputIterator, class _OutputIterator>
1631inline _LIBCPP_INLINE_VISIBILITY
1632_OutputIterator
1633copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1634{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001635 return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001636}
1637
1638// copy_backward
1639
Howard Hinnant7f229bc2013-02-06 21:03:39 +00001640template <class _BidirectionalIterator, class _OutputIterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001641inline _LIBCPP_INLINE_VISIBILITY
1642_OutputIterator
Howard Hinnant7f229bc2013-02-06 21:03:39 +00001643__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001644{
1645 while (__first != __last)
1646 *--__result = *--__last;
1647 return __result;
1648}
1649
1650template <class _Tp, class _Up>
1651inline _LIBCPP_INLINE_VISIBILITY
1652typename enable_if
1653<
1654 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001655 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001656 _Up*
1657>::type
1658__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1659{
1660 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clowc3ef9622015-06-02 13:52:16 +00001661 if (__n > 0)
1662 {
1663 __result -= __n;
1664 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1665 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001666 return __result;
1667}
1668
1669template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1670inline _LIBCPP_INLINE_VISIBILITY
1671_BidirectionalIterator2
1672copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1673 _BidirectionalIterator2 __result)
1674{
Eric Fiselier6003c772016-12-23 23:37:52 +00001675 return _VSTD::__copy_backward(__unwrap_iter(__first),
1676 __unwrap_iter(__last),
1677 __unwrap_iter(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001678}
1679
1680// copy_if
1681
1682template<class _InputIterator, class _OutputIterator, class _Predicate>
1683inline _LIBCPP_INLINE_VISIBILITY
1684_OutputIterator
1685copy_if(_InputIterator __first, _InputIterator __last,
1686 _OutputIterator __result, _Predicate __pred)
1687{
1688 for (; __first != __last; ++__first)
1689 {
1690 if (__pred(*__first))
1691 {
1692 *__result = *__first;
1693 ++__result;
1694 }
1695 }
1696 return __result;
1697}
1698
1699// copy_n
1700
1701template<class _InputIterator, class _Size, class _OutputIterator>
1702inline _LIBCPP_INLINE_VISIBILITY
1703typename enable_if
1704<
1705 __is_input_iterator<_InputIterator>::value &&
1706 !__is_random_access_iterator<_InputIterator>::value,
1707 _OutputIterator
1708>::type
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001709copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710{
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001711 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1712 _IntegralSize __n = __orig_n;
Howard Hinnantcbc5dc02011-02-27 20:55:39 +00001713 if (__n > 0)
1714 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715 *__result = *__first;
Howard Hinnantcbc5dc02011-02-27 20:55:39 +00001716 ++__result;
1717 for (--__n; __n > 0; --__n)
1718 {
1719 ++__first;
1720 *__result = *__first;
1721 ++__result;
1722 }
1723 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001724 return __result;
1725}
1726
1727template<class _InputIterator, class _Size, class _OutputIterator>
1728inline _LIBCPP_INLINE_VISIBILITY
1729typename enable_if
1730<
1731 __is_random_access_iterator<_InputIterator>::value,
1732 _OutputIterator
1733>::type
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001734copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735{
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001736 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1737 _IntegralSize __n = __orig_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001738 return _VSTD::copy(__first, __first + __n, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001739}
1740
1741// move
1742
1743template <class _InputIterator, class _OutputIterator>
1744inline _LIBCPP_INLINE_VISIBILITY
1745_OutputIterator
1746__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1747{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001748 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001749 *__result = _VSTD::move(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001750 return __result;
1751}
1752
1753template <class _Tp, class _Up>
1754inline _LIBCPP_INLINE_VISIBILITY
1755typename enable_if
1756<
1757 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001758 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001759 _Up*
1760>::type
1761__move(_Tp* __first, _Tp* __last, _Up* __result)
1762{
1763 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clowc3ef9622015-06-02 13:52:16 +00001764 if (__n > 0)
1765 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001766 return __result + __n;
1767}
1768
1769template <class _InputIterator, class _OutputIterator>
1770inline _LIBCPP_INLINE_VISIBILITY
1771_OutputIterator
1772move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1773{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001774 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775}
1776
1777// move_backward
1778
1779template <class _InputIterator, class _OutputIterator>
1780inline _LIBCPP_INLINE_VISIBILITY
1781_OutputIterator
1782__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1783{
1784 while (__first != __last)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001785 *--__result = _VSTD::move(*--__last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786 return __result;
1787}
1788
1789template <class _Tp, class _Up>
1790inline _LIBCPP_INLINE_VISIBILITY
1791typename enable_if
1792<
1793 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001794 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001795 _Up*
1796>::type
1797__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1798{
1799 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clowc3ef9622015-06-02 13:52:16 +00001800 if (__n > 0)
1801 {
1802 __result -= __n;
1803 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1804 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001805 return __result;
1806}
1807
1808template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1809inline _LIBCPP_INLINE_VISIBILITY
1810_BidirectionalIterator2
1811move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1812 _BidirectionalIterator2 __result)
1813{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001814 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001815}
1816
1817// iter_swap
1818
Howard Hinnantdbfd4b42011-05-27 15:04:19 +00001819// moved to <type_traits> for better swap / noexcept support
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820
1821// transform
1822
1823template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
Marshall Clow31427c62018-01-19 17:45:39 +00001824inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825_OutputIterator
1826transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1827{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001828 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001829 *__result = __op(*__first);
1830 return __result;
1831}
1832
1833template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
Marshall Clow31427c62018-01-19 17:45:39 +00001834inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001835_OutputIterator
1836transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1837 _OutputIterator __result, _BinaryOperation __binary_op)
1838{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001839 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001840 *__result = __binary_op(*__first1, *__first2);
1841 return __result;
1842}
1843
1844// replace
1845
1846template <class _ForwardIterator, class _Tp>
Marshall Clow01bbbd22018-01-19 18:07:29 +00001847inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001848void
1849replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1850{
1851 for (; __first != __last; ++__first)
1852 if (*__first == __old_value)
1853 *__first = __new_value;
1854}
1855
1856// replace_if
1857
1858template <class _ForwardIterator, class _Predicate, class _Tp>
Marshall Clow01bbbd22018-01-19 18:07:29 +00001859inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001860void
1861replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1862{
1863 for (; __first != __last; ++__first)
1864 if (__pred(*__first))
1865 *__first = __new_value;
1866}
1867
1868// replace_copy
1869
1870template <class _InputIterator, class _OutputIterator, class _Tp>
Marshall Clow01bbbd22018-01-19 18:07:29 +00001871inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872_OutputIterator
1873replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1874 const _Tp& __old_value, const _Tp& __new_value)
1875{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001876 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001877 if (*__first == __old_value)
1878 *__result = __new_value;
1879 else
1880 *__result = *__first;
1881 return __result;
1882}
1883
1884// replace_copy_if
1885
1886template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
Marshall Clow01bbbd22018-01-19 18:07:29 +00001887inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001888_OutputIterator
1889replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1890 _Predicate __pred, const _Tp& __new_value)
1891{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001892 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001893 if (__pred(*__first))
1894 *__result = __new_value;
1895 else
1896 *__result = *__first;
1897 return __result;
1898}
1899
1900// fill_n
1901
1902template <class _OutputIterator, class _Size, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001903inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001904_OutputIterator
Howard Hinnant0ad1c122013-08-01 17:29:28 +00001905__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001906{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001907 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnantbf074022011-10-22 20:59:45 +00001908 *__first = __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001909 return __first;
1910}
1911
Howard Hinnantc51e1022010-05-11 19:42:16 +00001912template <class _OutputIterator, class _Size, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001913inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914_OutputIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00001915fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916{
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001917 return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001918}
1919
1920// fill
1921
1922template <class _ForwardIterator, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001923inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001924void
Howard Hinnantbf074022011-10-22 20:59:45 +00001925__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001926{
1927 for (; __first != __last; ++__first)
Howard Hinnantbf074022011-10-22 20:59:45 +00001928 *__first = __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929}
1930
1931template <class _RandomAccessIterator, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001932inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001933void
Howard Hinnantbf074022011-10-22 20:59:45 +00001934__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001935{
Howard Hinnantbf074022011-10-22 20:59:45 +00001936 _VSTD::fill_n(__first, __last - __first, __value_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001937}
1938
1939template <class _ForwardIterator, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001940inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001941void
Howard Hinnantbf074022011-10-22 20:59:45 +00001942fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001943{
Howard Hinnantbf074022011-10-22 20:59:45 +00001944 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001945}
1946
1947// generate
1948
1949template <class _ForwardIterator, class _Generator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001950inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001951void
1952generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
1953{
1954 for (; __first != __last; ++__first)
1955 *__first = __gen();
1956}
1957
1958// generate_n
1959
1960template <class _OutputIterator, class _Size, class _Generator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001961inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001962_OutputIterator
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001963generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001964{
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001965 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1966 _IntegralSize __n = __orig_n;
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001967 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001968 *__first = __gen();
1969 return __first;
1970}
1971
1972// remove
1973
1974template <class _ForwardIterator, class _Tp>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00001975_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00001976remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001977{
Howard Hinnantbf074022011-10-22 20:59:45 +00001978 __first = _VSTD::find(__first, __last, __value_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001979 if (__first != __last)
1980 {
1981 _ForwardIterator __i = __first;
1982 while (++__i != __last)
1983 {
Howard Hinnantbf074022011-10-22 20:59:45 +00001984 if (!(*__i == __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001985 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001986 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987 ++__first;
1988 }
1989 }
1990 }
1991 return __first;
1992}
1993
1994// remove_if
1995
1996template <class _ForwardIterator, class _Predicate>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00001997_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001998remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
1999{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002000 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002001 (__first, __last, __pred);
2002 if (__first != __last)
2003 {
2004 _ForwardIterator __i = __first;
2005 while (++__i != __last)
2006 {
2007 if (!__pred(*__i))
2008 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002009 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002010 ++__first;
2011 }
2012 }
2013 }
2014 return __first;
2015}
2016
2017// remove_copy
2018
2019template <class _InputIterator, class _OutputIterator, class _Tp>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00002020inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002021_OutputIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00002022remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023{
2024 for (; __first != __last; ++__first)
2025 {
Howard Hinnantbf074022011-10-22 20:59:45 +00002026 if (!(*__first == __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027 {
2028 *__result = *__first;
2029 ++__result;
2030 }
2031 }
2032 return __result;
2033}
2034
2035// remove_copy_if
2036
2037template <class _InputIterator, class _OutputIterator, class _Predicate>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00002038inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002039_OutputIterator
2040remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2041{
2042 for (; __first != __last; ++__first)
2043 {
2044 if (!__pred(*__first))
2045 {
2046 *__result = *__first;
2047 ++__result;
2048 }
2049 }
2050 return __result;
2051}
2052
2053// unique
2054
2055template <class _ForwardIterator, class _BinaryPredicate>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002056_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002057unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2058{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002059 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002060 (__first, __last, __pred);
2061 if (__first != __last)
2062 {
2063 // ... a a ? ...
2064 // f i
2065 _ForwardIterator __i = __first;
2066 for (++__i; ++__i != __last;)
2067 if (!__pred(*__first, *__i))
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002068 *++__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002069 ++__first;
2070 }
2071 return __first;
2072}
2073
2074template <class _ForwardIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002075inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002076_ForwardIterator
2077unique(_ForwardIterator __first, _ForwardIterator __last)
2078{
2079 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002080 return _VSTD::unique(__first, __last, __equal_to<__v>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002081}
2082
2083// unique_copy
2084
2085template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002086_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002087__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2088 input_iterator_tag, output_iterator_tag)
2089{
2090 if (__first != __last)
2091 {
2092 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2093 *__result = __t;
2094 ++__result;
2095 while (++__first != __last)
2096 {
2097 if (!__pred(__t, *__first))
2098 {
2099 __t = *__first;
2100 *__result = __t;
2101 ++__result;
2102 }
2103 }
2104 }
2105 return __result;
2106}
2107
2108template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002109_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002110__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2111 forward_iterator_tag, output_iterator_tag)
2112{
2113 if (__first != __last)
2114 {
2115 _ForwardIterator __i = __first;
2116 *__result = *__i;
2117 ++__result;
2118 while (++__first != __last)
2119 {
2120 if (!__pred(*__i, *__first))
2121 {
2122 *__result = *__first;
2123 ++__result;
2124 __i = __first;
2125 }
2126 }
2127 }
2128 return __result;
2129}
2130
2131template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002132_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2134 input_iterator_tag, forward_iterator_tag)
2135{
2136 if (__first != __last)
2137 {
2138 *__result = *__first;
2139 while (++__first != __last)
2140 if (!__pred(*__result, *__first))
2141 *++__result = *__first;
2142 ++__result;
2143 }
2144 return __result;
2145}
2146
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002148inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149_OutputIterator
2150unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2151{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002152 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002153 (__first, __last, __result, __pred,
2154 typename iterator_traits<_InputIterator>::iterator_category(),
2155 typename iterator_traits<_OutputIterator>::iterator_category());
2156}
2157
2158template <class _InputIterator, class _OutputIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002159inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160_OutputIterator
2161unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2162{
2163 typedef typename iterator_traits<_InputIterator>::value_type __v;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002164 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165}
2166
2167// reverse
2168
2169template <class _BidirectionalIterator>
2170inline _LIBCPP_INLINE_VISIBILITY
2171void
2172__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2173{
2174 while (__first != __last)
2175 {
2176 if (__first == --__last)
2177 break;
Marshall Clow2ad71042015-11-02 21:34:25 +00002178 _VSTD::iter_swap(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179 ++__first;
2180 }
2181}
2182
2183template <class _RandomAccessIterator>
2184inline _LIBCPP_INLINE_VISIBILITY
2185void
2186__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2187{
2188 if (__first != __last)
2189 for (; __first < --__last; ++__first)
Marshall Clow2ad71042015-11-02 21:34:25 +00002190 _VSTD::iter_swap(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191}
2192
2193template <class _BidirectionalIterator>
2194inline _LIBCPP_INLINE_VISIBILITY
2195void
2196reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2197{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002198 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002199}
2200
2201// reverse_copy
2202
2203template <class _BidirectionalIterator, class _OutputIterator>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00002204inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205_OutputIterator
2206reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2207{
2208 for (; __first != __last; ++__result)
2209 *__result = *--__last;
2210 return __result;
2211}
2212
2213// rotate
2214
2215template <class _ForwardIterator>
2216_ForwardIterator
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002217__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002218{
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002219 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2220 value_type __tmp = _VSTD::move(*__first);
2221 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2222 *__lm1 = _VSTD::move(__tmp);
2223 return __lm1;
2224}
2225
2226template <class _BidirectionalIterator>
2227_BidirectionalIterator
2228__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2229{
2230 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2231 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2232 value_type __tmp = _VSTD::move(*__lm1);
2233 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2234 *__first = _VSTD::move(__tmp);
2235 return __fp1;
2236}
2237
2238template <class _ForwardIterator>
2239_ForwardIterator
2240__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2241{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242 _ForwardIterator __i = __middle;
2243 while (true)
2244 {
2245 swap(*__first, *__i);
2246 ++__first;
2247 if (++__i == __last)
2248 break;
2249 if (__first == __middle)
2250 __middle = __i;
2251 }
2252 _ForwardIterator __r = __first;
2253 if (__first != __middle)
2254 {
2255 __i = __middle;
2256 while (true)
2257 {
2258 swap(*__first, *__i);
2259 ++__first;
2260 if (++__i == __last)
2261 {
2262 if (__first == __middle)
2263 break;
2264 __i = __middle;
2265 }
2266 else if (__first == __middle)
2267 __middle = __i;
2268 }
2269 }
2270 return __r;
2271}
2272
2273template<typename _Integral>
2274inline _LIBCPP_INLINE_VISIBILITY
2275_Integral
Marshall Clowb8bfc2c2016-07-26 14:29:45 +00002276__algo_gcd(_Integral __x, _Integral __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002277{
2278 do
2279 {
2280 _Integral __t = __x % __y;
2281 __x = __y;
2282 __y = __t;
2283 } while (__y);
2284 return __x;
2285}
2286
2287template<typename _RandomAccessIterator>
2288_RandomAccessIterator
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002289__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002290{
2291 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2292 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002293
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294 const difference_type __m1 = __middle - __first;
2295 const difference_type __m2 = __last - __middle;
2296 if (__m1 == __m2)
2297 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002298 _VSTD::swap_ranges(__first, __middle, __middle);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002299 return __middle;
2300 }
Marshall Clowb8bfc2c2016-07-26 14:29:45 +00002301 const difference_type __g = _VSTD::__algo_gcd(__m1, __m2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002302 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2303 {
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002304 value_type __t(_VSTD::move(*--__p));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002305 _RandomAccessIterator __p1 = __p;
2306 _RandomAccessIterator __p2 = __p1 + __m1;
2307 do
2308 {
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002309 *__p1 = _VSTD::move(*__p2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002310 __p1 = __p2;
2311 const difference_type __d = __last - __p2;
2312 if (__m1 < __d)
2313 __p2 += __m1;
2314 else
2315 __p2 = __first + (__m1 - __d);
2316 } while (__p2 != __p);
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002317 *__p1 = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002318 }
2319 return __first + __m2;
2320}
2321
2322template <class _ForwardIterator>
2323inline _LIBCPP_INLINE_VISIBILITY
2324_ForwardIterator
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002325__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2326 _VSTD::forward_iterator_tag)
2327{
2328 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2329 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2330 {
2331 if (_VSTD::next(__first) == __middle)
2332 return _VSTD::__rotate_left(__first, __last);
2333 }
2334 return _VSTD::__rotate_forward(__first, __middle, __last);
2335}
2336
2337template <class _BidirectionalIterator>
2338inline _LIBCPP_INLINE_VISIBILITY
2339_BidirectionalIterator
2340__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2341 _VSTD::bidirectional_iterator_tag)
2342{
2343 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2344 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2345 {
2346 if (_VSTD::next(__first) == __middle)
2347 return _VSTD::__rotate_left(__first, __last);
2348 if (_VSTD::next(__middle) == __last)
2349 return _VSTD::__rotate_right(__first, __last);
2350 }
2351 return _VSTD::__rotate_forward(__first, __middle, __last);
2352}
2353
2354template <class _RandomAccessIterator>
2355inline _LIBCPP_INLINE_VISIBILITY
2356_RandomAccessIterator
2357__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2358 _VSTD::random_access_iterator_tag)
2359{
2360 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2361 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2362 {
2363 if (_VSTD::next(__first) == __middle)
2364 return _VSTD::__rotate_left(__first, __last);
2365 if (_VSTD::next(__middle) == __last)
2366 return _VSTD::__rotate_right(__first, __last);
2367 return _VSTD::__rotate_gcd(__first, __middle, __last);
2368 }
2369 return _VSTD::__rotate_forward(__first, __middle, __last);
2370}
2371
2372template <class _ForwardIterator>
2373inline _LIBCPP_INLINE_VISIBILITY
2374_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002375rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2376{
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002377 if (__first == __middle)
2378 return __last;
2379 if (__middle == __last)
2380 return __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002381 return _VSTD::__rotate(__first, __middle, __last,
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002382 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002383}
2384
2385// rotate_copy
2386
2387template <class _ForwardIterator, class _OutputIterator>
2388inline _LIBCPP_INLINE_VISIBILITY
2389_OutputIterator
2390rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2391{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002392 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002393}
2394
Howard Hinnantc51e1022010-05-11 19:42:16 +00002395// min_element
2396
2397template <class _ForwardIterator, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002398inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002399_ForwardIterator
Marshall Clow9e173072015-05-10 13:53:31 +00002400min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002401{
Eric Fiselierf8ecd942018-08-22 17:47:13 +00002402 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2403 "std::min_element requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002404 if (__first != __last)
2405 {
2406 _ForwardIterator __i = __first;
2407 while (++__i != __last)
2408 if (__comp(*__i, *__first))
2409 __first = __i;
2410 }
2411 return __first;
2412}
2413
2414template <class _ForwardIterator>
Marshall Clow9e173072015-05-10 13:53:31 +00002415inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002416_ForwardIterator
2417min_element(_ForwardIterator __first, _ForwardIterator __last)
2418{
Marshall Clow9e173072015-05-10 13:53:31 +00002419 return _VSTD::min_element(__first, __last,
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002420 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2421}
2422
2423// min
2424
2425template <class _Tp, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002426inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002427const _Tp&
2428min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2429{
2430 return __comp(__b, __a) ? __b : __a;
2431}
2432
2433template <class _Tp>
Marshall Clowe9dca072014-02-19 16:51:35 +00002434inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002435const _Tp&
2436min(const _Tp& __a, const _Tp& __b)
2437{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002438 return _VSTD::min(__a, __b, __less<_Tp>());
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002439}
2440
Eric Fiselier93dd1372017-04-18 23:26:47 +00002441#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002442
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002443template<class _Tp, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002444inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002445_Tp
2446min(initializer_list<_Tp> __t, _Compare __comp)
2447{
Marshall Clow9e173072015-05-10 13:53:31 +00002448 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002449}
2450
2451template<class _Tp>
Marshall Clowe9dca072014-02-19 16:51:35 +00002452inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002453_Tp
2454min(initializer_list<_Tp> __t)
2455{
Marshall Clow9e173072015-05-10 13:53:31 +00002456 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002457}
2458
Eric Fiselier93dd1372017-04-18 23:26:47 +00002459#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002460
Howard Hinnantc51e1022010-05-11 19:42:16 +00002461// max_element
2462
2463template <class _ForwardIterator, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002464inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002465_ForwardIterator
Marshall Clow9e173072015-05-10 13:53:31 +00002466max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002467{
Eric Fiselierf8ecd942018-08-22 17:47:13 +00002468 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2469 "std::max_element requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002470 if (__first != __last)
2471 {
2472 _ForwardIterator __i = __first;
2473 while (++__i != __last)
2474 if (__comp(*__first, *__i))
2475 __first = __i;
2476 }
2477 return __first;
2478}
2479
Marshall Clowe9dca072014-02-19 16:51:35 +00002480
Howard Hinnantc51e1022010-05-11 19:42:16 +00002481template <class _ForwardIterator>
Marshall Clow9e173072015-05-10 13:53:31 +00002482inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483_ForwardIterator
2484max_element(_ForwardIterator __first, _ForwardIterator __last)
2485{
Marshall Clow9e173072015-05-10 13:53:31 +00002486 return _VSTD::max_element(__first, __last,
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002487 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2488}
2489
2490// max
2491
2492template <class _Tp, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002493inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002494const _Tp&
2495max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2496{
2497 return __comp(__a, __b) ? __b : __a;
2498}
2499
2500template <class _Tp>
Marshall Clowe9dca072014-02-19 16:51:35 +00002501inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002502const _Tp&
2503max(const _Tp& __a, const _Tp& __b)
2504{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002505 return _VSTD::max(__a, __b, __less<_Tp>());
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002506}
2507
Eric Fiselier93dd1372017-04-18 23:26:47 +00002508#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002509
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002510template<class _Tp, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002511inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002512_Tp
2513max(initializer_list<_Tp> __t, _Compare __comp)
2514{
Marshall Clow9e173072015-05-10 13:53:31 +00002515 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002516}
2517
2518template<class _Tp>
Marshall Clowe9dca072014-02-19 16:51:35 +00002519inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002520_Tp
2521max(initializer_list<_Tp> __t)
2522{
Marshall Clow9e173072015-05-10 13:53:31 +00002523 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524}
2525
Eric Fiselier93dd1372017-04-18 23:26:47 +00002526#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002527
Marshall Clow3e18d0e2016-03-07 22:43:49 +00002528#if _LIBCPP_STD_VER > 14
2529// clamp
2530template<class _Tp, class _Compare>
2531inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2532const _Tp&
2533clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
2534{
2535 _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
2536 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
2537
2538}
2539
2540template<class _Tp>
2541inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2542const _Tp&
2543clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
2544{
2545 return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
2546}
2547#endif
2548
Howard Hinnantc51e1022010-05-11 19:42:16 +00002549// minmax_element
2550
2551template <class _ForwardIterator, class _Compare>
Marshall Clow9e173072015-05-10 13:53:31 +00002552_LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002553std::pair<_ForwardIterator, _ForwardIterator>
2554minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2555{
Eric Fiselierf8ecd942018-08-22 17:47:13 +00002556 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2557 "std::minmax_element requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002558 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2559 if (__first != __last)
2560 {
2561 if (++__first != __last)
2562 {
2563 if (__comp(*__first, *__result.first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002564 __result.first = __first;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002565 else
2566 __result.second = __first;
2567 while (++__first != __last)
2568 {
2569 _ForwardIterator __i = __first;
2570 if (++__first == __last)
2571 {
2572 if (__comp(*__i, *__result.first))
2573 __result.first = __i;
2574 else if (!__comp(*__i, *__result.second))
2575 __result.second = __i;
2576 break;
2577 }
2578 else
2579 {
2580 if (__comp(*__first, *__i))
2581 {
2582 if (__comp(*__first, *__result.first))
2583 __result.first = __first;
2584 if (!__comp(*__i, *__result.second))
2585 __result.second = __i;
2586 }
2587 else
2588 {
2589 if (__comp(*__i, *__result.first))
2590 __result.first = __i;
2591 if (!__comp(*__first, *__result.second))
2592 __result.second = __first;
2593 }
2594 }
2595 }
2596 }
2597 }
2598 return __result;
2599}
2600
2601template <class _ForwardIterator>
Marshall Clow9e173072015-05-10 13:53:31 +00002602inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002603std::pair<_ForwardIterator, _ForwardIterator>
2604minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2605{
Marshall Clowe9dca072014-02-19 16:51:35 +00002606 return _VSTD::minmax_element(__first, __last,
2607 __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002608}
2609
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002610// minmax
2611
2612template<class _Tp, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002613inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002614pair<const _Tp&, const _Tp&>
2615minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2616{
2617 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2618 pair<const _Tp&, const _Tp&>(__a, __b);
2619}
2620
2621template<class _Tp>
Marshall Clowe9dca072014-02-19 16:51:35 +00002622inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002623pair<const _Tp&, const _Tp&>
2624minmax(const _Tp& __a, const _Tp& __b)
2625{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002626 return _VSTD::minmax(__a, __b, __less<_Tp>());
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002627}
2628
Eric Fiselier93dd1372017-04-18 23:26:47 +00002629#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002630
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002631template<class _Tp, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002632inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002633pair<_Tp, _Tp>
2634minmax(initializer_list<_Tp> __t, _Compare __comp)
2635{
Marshall Clowe9dca072014-02-19 16:51:35 +00002636 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2637 _Iter __first = __t.begin();
2638 _Iter __last = __t.end();
Marshall Clow447713a2015-02-11 15:41:34 +00002639 std::pair<_Tp, _Tp> __result(*__first, *__first);
Marshall Clowe9dca072014-02-19 16:51:35 +00002640
2641 ++__first;
2642 if (__t.size() % 2 == 0)
2643 {
2644 if (__comp(*__first, __result.first))
2645 __result.first = *__first;
2646 else
2647 __result.second = *__first;
2648 ++__first;
2649 }
Aditya Kumar3a0179a2016-08-25 11:52:38 +00002650
Marshall Clowe9dca072014-02-19 16:51:35 +00002651 while (__first != __last)
2652 {
2653 _Tp __prev = *__first++;
Marshall Clow447713a2015-02-11 15:41:34 +00002654 if (__comp(*__first, __prev)) {
2655 if ( __comp(*__first, __result.first)) __result.first = *__first;
2656 if (!__comp(__prev, __result.second)) __result.second = __prev;
Marshall Clowe9dca072014-02-19 16:51:35 +00002657 }
2658 else {
Marshall Clow447713a2015-02-11 15:41:34 +00002659 if ( __comp(__prev, __result.first)) __result.first = __prev;
2660 if (!__comp(*__first, __result.second)) __result.second = *__first;
Marshall Clowe9dca072014-02-19 16:51:35 +00002661 }
Aditya Kumar3a0179a2016-08-25 11:52:38 +00002662
Marshall Clowe9dca072014-02-19 16:51:35 +00002663 __first++;
2664 }
2665 return __result;
2666}
2667
2668template<class _Tp>
2669inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2670pair<_Tp, _Tp>
2671minmax(initializer_list<_Tp> __t)
2672{
2673 return _VSTD::minmax(__t, __less<_Tp>());
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002674}
2675
Eric Fiselier93dd1372017-04-18 23:26:47 +00002676#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002677
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678// random_shuffle
2679
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002680// __independent_bits_engine
2681
Howard Hinnantc834c512011-11-29 18:15:50 +00002682template <unsigned long long _Xp, size_t _Rp>
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002683struct __log2_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00002684{
Howard Hinnantc834c512011-11-29 18:15:50 +00002685 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2686 : __log2_imp<_Xp, _Rp - 1>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687};
2688
Howard Hinnantc834c512011-11-29 18:15:50 +00002689template <unsigned long long _Xp>
2690struct __log2_imp<_Xp, 0>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691{
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002692 static const size_t value = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002693};
2694
Howard Hinnantc834c512011-11-29 18:15:50 +00002695template <size_t _Rp>
2696struct __log2_imp<0, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002697{
Howard Hinnantc834c512011-11-29 18:15:50 +00002698 static const size_t value = _Rp + 1;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002699};
2700
Eric Fiselier4638fca2017-05-31 21:20:18 +00002701template <class _UIntType, _UIntType _Xp>
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002702struct __log2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002703{
Howard Hinnantc834c512011-11-29 18:15:50 +00002704 static const size_t value = __log2_imp<_Xp,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002705 sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002706};
2707
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002708template<class _Engine, class _UIntType>
2709class __independent_bits_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710{
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002711public:
2712 // types
2713 typedef _UIntType result_type;
2714
2715private:
2716 typedef typename _Engine::result_type _Engine_result_type;
2717 typedef typename conditional
2718 <
2719 sizeof(_Engine_result_type) <= sizeof(result_type),
2720 result_type,
2721 _Engine_result_type
2722 >::type _Working_result_type;
2723
2724 _Engine& __e_;
2725 size_t __w_;
2726 size_t __w0_;
2727 size_t __n_;
2728 size_t __n0_;
2729 _Working_result_type __y0_;
2730 _Working_result_type __y1_;
2731 _Engine_result_type __mask0_;
2732 _Engine_result_type __mask1_;
2733
Eric Fiselier93dd1372017-04-18 23:26:47 +00002734#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc834c512011-11-29 18:15:50 +00002735 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant5a646852012-04-02 21:00:45 +00002736 + _Working_result_type(1);
2737#else
2738 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2739 + _Working_result_type(1);
2740#endif
2741 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2742 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2743 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002744
2745public:
2746 // constructors and seeding functions
2747 __independent_bits_engine(_Engine& __e, size_t __w);
2748
2749 // generating functions
Howard Hinnantc834c512011-11-29 18:15:50 +00002750 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002751
2752private:
Marshall Clowfe778582017-09-20 19:38:43 +00002753 result_type __eval(false_type);
2754 result_type __eval(true_type);
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002755};
2756
2757template<class _Engine, class _UIntType>
2758__independent_bits_engine<_Engine, _UIntType>
2759 ::__independent_bits_engine(_Engine& __e, size_t __w)
2760 : __e_(__e),
2761 __w_(__w)
2762{
2763 __n_ = __w_ / __m + (__w_ % __m != 0);
2764 __w0_ = __w_ / __n_;
Howard Hinnantc834c512011-11-29 18:15:50 +00002765 if (_Rp == 0)
2766 __y0_ = _Rp;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002767 else if (__w0_ < _WDt)
Howard Hinnantc834c512011-11-29 18:15:50 +00002768 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002769 else
2770 __y0_ = 0;
Howard Hinnantc834c512011-11-29 18:15:50 +00002771 if (_Rp - __y0_ > __y0_ / __n_)
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002772 {
2773 ++__n_;
2774 __w0_ = __w_ / __n_;
2775 if (__w0_ < _WDt)
Howard Hinnantc834c512011-11-29 18:15:50 +00002776 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002777 else
2778 __y0_ = 0;
2779 }
2780 __n0_ = __n_ - __w_ % __n_;
2781 if (__w0_ < _WDt - 1)
Howard Hinnantc834c512011-11-29 18:15:50 +00002782 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002783 else
2784 __y1_ = 0;
2785 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2786 _Engine_result_type(0);
2787 __mask1_ = __w0_ < _EDt - 1 ?
2788 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2789 _Engine_result_type(~0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790}
2791
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002792template<class _Engine, class _UIntType>
2793inline
2794_UIntType
Marshall Clowfe778582017-09-20 19:38:43 +00002795__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002796{
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002797 return static_cast<result_type>(__e_() & __mask0_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002798}
2799
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002800template<class _Engine, class _UIntType>
2801_UIntType
Marshall Clowfe778582017-09-20 19:38:43 +00002802__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002803{
Marshall Clowafc48592017-09-20 17:34:11 +00002804 const size_t _WRt = numeric_limits<result_type>::digits;
Howard Hinnantc834c512011-11-29 18:15:50 +00002805 result_type _Sp = 0;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002806 for (size_t __k = 0; __k < __n0_; ++__k)
2807 {
2808 _Engine_result_type __u;
2809 do
2810 {
2811 __u = __e_() - _Engine::min();
2812 } while (__u >= __y0_);
Marshall Clowafc48592017-09-20 17:34:11 +00002813 if (__w0_ < _WRt)
Howard Hinnantc834c512011-11-29 18:15:50 +00002814 _Sp <<= __w0_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002815 else
Howard Hinnantc834c512011-11-29 18:15:50 +00002816 _Sp = 0;
2817 _Sp += __u & __mask0_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002818 }
2819 for (size_t __k = __n0_; __k < __n_; ++__k)
2820 {
2821 _Engine_result_type __u;
2822 do
2823 {
2824 __u = __e_() - _Engine::min();
2825 } while (__u >= __y1_);
Marshall Clowafc48592017-09-20 17:34:11 +00002826 if (__w0_ < _WRt - 1)
Howard Hinnantc834c512011-11-29 18:15:50 +00002827 _Sp <<= __w0_ + 1;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002828 else
Howard Hinnantc834c512011-11-29 18:15:50 +00002829 _Sp = 0;
2830 _Sp += __u & __mask1_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002831 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002832 return _Sp;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002833}
2834
2835// uniform_int_distribution
2836
2837template<class _IntType = int>
2838class uniform_int_distribution
2839{
2840public:
2841 // types
2842 typedef _IntType result_type;
2843
2844 class param_type
2845 {
2846 result_type __a_;
2847 result_type __b_;
2848 public:
2849 typedef uniform_int_distribution distribution_type;
2850
2851 explicit param_type(result_type __a = 0,
2852 result_type __b = numeric_limits<result_type>::max())
2853 : __a_(__a), __b_(__b) {}
2854
2855 result_type a() const {return __a_;}
2856 result_type b() const {return __b_;}
2857
2858 friend bool operator==(const param_type& __x, const param_type& __y)
2859 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2860 friend bool operator!=(const param_type& __x, const param_type& __y)
2861 {return !(__x == __y);}
2862 };
2863
2864private:
2865 param_type __p_;
2866
2867public:
2868 // constructors and reset functions
2869 explicit uniform_int_distribution(result_type __a = 0,
2870 result_type __b = numeric_limits<result_type>::max())
2871 : __p_(param_type(__a, __b)) {}
2872 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
2873 void reset() {}
2874
2875 // generating functions
2876 template<class _URNG> result_type operator()(_URNG& __g)
2877 {return (*this)(__g, __p_);}
2878 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2879
2880 // property functions
2881 result_type a() const {return __p_.a();}
2882 result_type b() const {return __p_.b();}
2883
2884 param_type param() const {return __p_;}
2885 void param(const param_type& __p) {__p_ = __p;}
2886
2887 result_type min() const {return a();}
2888 result_type max() const {return b();}
2889
2890 friend bool operator==(const uniform_int_distribution& __x,
2891 const uniform_int_distribution& __y)
2892 {return __x.__p_ == __y.__p_;}
2893 friend bool operator!=(const uniform_int_distribution& __x,
2894 const uniform_int_distribution& __y)
2895 {return !(__x == __y);}
2896};
2897
2898template<class _IntType>
2899template<class _URNG>
2900typename uniform_int_distribution<_IntType>::result_type
2901uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
2902{
2903 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
2904 uint32_t, uint64_t>::type _UIntType;
Howard Hinnantc834c512011-11-29 18:15:50 +00002905 const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
2906 if (_Rp == 1)
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002907 return __p.a();
2908 const size_t _Dt = numeric_limits<_UIntType>::digits;
2909 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
Howard Hinnantc834c512011-11-29 18:15:50 +00002910 if (_Rp == 0)
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002911 return static_cast<result_type>(_Eng(__g, _Dt)());
Howard Hinnantc834c512011-11-29 18:15:50 +00002912 size_t __w = _Dt - __clz(_Rp) - 1;
Marshall Clow40aada52015-07-30 18:26:34 +00002913 if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002914 ++__w;
2915 _Eng __e(__g, __w);
2916 _UIntType __u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002917 do
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002918 {
2919 __u = __e();
Howard Hinnantc834c512011-11-29 18:15:50 +00002920 } while (__u >= _Rp);
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002921 return static_cast<result_type>(__u + __p.a());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002922}
2923
Eric Fiselierf5fb27c2017-04-03 23:23:44 +00002924#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \
2925 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002926class _LIBCPP_TYPE_VIS __rs_default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002928_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002929
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002930class _LIBCPP_TYPE_VIS __rs_default
Howard Hinnantc51e1022010-05-11 19:42:16 +00002931{
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002932 static unsigned __c_;
2933
2934 __rs_default();
2935public:
Marshall Clow9903c5b2013-02-07 22:12:02 +00002936 typedef uint_fast32_t result_type;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002937
2938 static const result_type _Min = 0;
2939 static const result_type _Max = 0xFFFFFFFF;
2940
2941 __rs_default(const __rs_default&);
2942 ~__rs_default();
2943
2944 result_type operator()();
2945
Howard Hinnant664183b2012-04-02 00:40:41 +00002946 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
2947 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002948
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002949 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002950};
2951
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002952_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002953
2954template <class _RandomAccessIterator>
2955void
2956random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
2957{
2958 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc834c512011-11-29 18:15:50 +00002959 typedef uniform_int_distribution<ptrdiff_t> _Dp;
2960 typedef typename _Dp::param_type _Pp;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002961 difference_type __d = __last - __first;
2962 if (__d > 1)
2963 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002964 _Dp __uid;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002965 __rs_default __g = __rs_get();
2966 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00002967 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002968 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00002969 if (__i != difference_type(0))
2970 swap(*__first, *(__first + __i));
2971 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002972 }
2973}
2974
2975template <class _RandomAccessIterator, class _RandomNumberGenerator>
2976void
2977random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselier93dd1372017-04-18 23:26:47 +00002978#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002979 _RandomNumberGenerator&& __rand)
2980#else
2981 _RandomNumberGenerator& __rand)
2982#endif
2983{
2984 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2985 difference_type __d = __last - __first;
2986 if (__d > 1)
2987 {
2988 for (--__last; __first < __last; ++__first, --__d)
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00002989 {
2990 difference_type __i = __rand(__d);
Marshall Clow5bdfc232018-09-11 18:33:45 +00002991 if (__i != difference_type(0))
2992 swap(*__first, *(__first + __i));
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00002993 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002994 }
2995}
Marshall Clowfac06e52017-03-23 13:43:37 +00002996#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002997
Eric Fiselier1208fcd2016-08-28 22:14:37 +00002998template <class _PopulationIterator, class _SampleIterator, class _Distance,
2999 class _UniformRandomNumberGenerator>
3000_LIBCPP_INLINE_VISIBILITY
3001_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsonc9637642017-11-14 11:14:25 +00003002 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003003 _Distance __n,
3004 _UniformRandomNumberGenerator & __g,
3005 input_iterator_tag) {
3006
3007 _Distance __k = 0;
3008 for (; __first != __last && __k < __n; ++__first, (void)++__k)
Alexander Richardsonc9637642017-11-14 11:14:25 +00003009 __output_iter[__k] = *__first;
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003010 _Distance __sz = __k;
3011 for (; __first != __last; ++__first, (void)++__k) {
3012 _Distance __r = _VSTD::uniform_int_distribution<_Distance>(0, __k)(__g);
3013 if (__r < __sz)
Alexander Richardsonc9637642017-11-14 11:14:25 +00003014 __output_iter[__r] = *__first;
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003015 }
Alexander Richardsonc9637642017-11-14 11:14:25 +00003016 return __output_iter + _VSTD::min(__n, __k);
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003017}
3018
3019template <class _PopulationIterator, class _SampleIterator, class _Distance,
3020 class _UniformRandomNumberGenerator>
3021_LIBCPP_INLINE_VISIBILITY
3022_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsonc9637642017-11-14 11:14:25 +00003023 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003024 _Distance __n,
3025 _UniformRandomNumberGenerator& __g,
3026 forward_iterator_tag) {
3027 _Distance __unsampled_sz = _VSTD::distance(__first, __last);
3028 for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
3029 _Distance __r =
3030 _VSTD::uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
3031 if (__r < __n) {
Alexander Richardsonc9637642017-11-14 11:14:25 +00003032 *__output_iter++ = *__first;
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003033 --__n;
3034 }
3035 }
Alexander Richardsonc9637642017-11-14 11:14:25 +00003036 return __output_iter;
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003037}
3038
3039template <class _PopulationIterator, class _SampleIterator, class _Distance,
3040 class _UniformRandomNumberGenerator>
3041_LIBCPP_INLINE_VISIBILITY
3042_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsonc9637642017-11-14 11:14:25 +00003043 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003044 _Distance __n, _UniformRandomNumberGenerator& __g) {
3045 typedef typename iterator_traits<_PopulationIterator>::iterator_category
3046 _PopCategory;
3047 typedef typename iterator_traits<_PopulationIterator>::difference_type
3048 _Difference;
3049 static_assert(__is_forward_iterator<_PopulationIterator>::value ||
3050 __is_random_access_iterator<_SampleIterator>::value,
3051 "SampleIterator must meet the requirements of RandomAccessIterator");
3052 typedef typename common_type<_Distance, _Difference>::type _CommonType;
3053 _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
3054 return _VSTD::__sample(
Alexander Richardsonc9637642017-11-14 11:14:25 +00003055 __first, __last, __output_iter, _CommonType(__n),
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003056 __g, _PopCategory());
3057}
3058
3059#if _LIBCPP_STD_VER > 14
3060template <class _PopulationIterator, class _SampleIterator, class _Distance,
3061 class _UniformRandomNumberGenerator>
3062inline _LIBCPP_INLINE_VISIBILITY
3063_SampleIterator sample(_PopulationIterator __first,
Alexander Richardsonc9637642017-11-14 11:14:25 +00003064 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003065 _Distance __n, _UniformRandomNumberGenerator&& __g) {
Alexander Richardsonc9637642017-11-14 11:14:25 +00003066 return _VSTD::__sample(__first, __last, __output_iter, __n, __g);
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003067}
3068#endif // _LIBCPP_STD_VER > 14
3069
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003070template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3071 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselier93dd1372017-04-18 23:26:47 +00003072#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanta5e71782010-11-18 01:47:02 +00003073 _UniformRandomNumberGenerator&& __g)
3074#else
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003075 _UniformRandomNumberGenerator& __g)
Howard Hinnanta5e71782010-11-18 01:47:02 +00003076#endif
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003077{
3078 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc834c512011-11-29 18:15:50 +00003079 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3080 typedef typename _Dp::param_type _Pp;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003081 difference_type __d = __last - __first;
3082 if (__d > 1)
3083 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003084 _Dp __uid;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003085 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003086 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003087 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003088 if (__i != difference_type(0))
3089 swap(*__first, *(__first + __i));
3090 }
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003091 }
3092}
3093
Howard Hinnantc51e1022010-05-11 19:42:16 +00003094template <class _InputIterator, class _Predicate>
Marshall Clow96d050a2018-01-15 16:16:32 +00003095_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00003096is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3097{
3098 for (; __first != __last; ++__first)
3099 if (!__pred(*__first))
3100 break;
Marshall Clow3562ed72015-02-02 18:16:35 +00003101 if ( __first == __last )
3102 return true;
3103 ++__first;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003104 for (; __first != __last; ++__first)
3105 if (__pred(*__first))
3106 return false;
3107 return true;
3108}
3109
3110// partition
3111
3112template <class _Predicate, class _ForwardIterator>
3113_ForwardIterator
3114__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3115{
3116 while (true)
3117 {
3118 if (__first == __last)
3119 return __first;
3120 if (!__pred(*__first))
3121 break;
3122 ++__first;
3123 }
3124 for (_ForwardIterator __p = __first; ++__p != __last;)
3125 {
3126 if (__pred(*__p))
3127 {
3128 swap(*__first, *__p);
3129 ++__first;
3130 }
3131 }
3132 return __first;
3133}
3134
3135template <class _Predicate, class _BidirectionalIterator>
3136_BidirectionalIterator
3137__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3138 bidirectional_iterator_tag)
3139{
3140 while (true)
3141 {
3142 while (true)
3143 {
3144 if (__first == __last)
3145 return __first;
3146 if (!__pred(*__first))
3147 break;
3148 ++__first;
3149 }
3150 do
3151 {
3152 if (__first == --__last)
3153 return __first;
3154 } while (!__pred(*__last));
3155 swap(*__first, *__last);
3156 ++__first;
3157 }
3158}
3159
3160template <class _ForwardIterator, class _Predicate>
3161inline _LIBCPP_INLINE_VISIBILITY
3162_ForwardIterator
3163partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3164{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003165 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3167}
3168
3169// partition_copy
3170
3171template <class _InputIterator, class _OutputIterator1,
3172 class _OutputIterator2, class _Predicate>
Marshall Clow5492c8a2018-01-22 20:44:33 +00003173_LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_OutputIterator1, _OutputIterator2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003174partition_copy(_InputIterator __first, _InputIterator __last,
3175 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3176 _Predicate __pred)
3177{
3178 for (; __first != __last; ++__first)
3179 {
3180 if (__pred(*__first))
3181 {
3182 *__out_true = *__first;
3183 ++__out_true;
3184 }
3185 else
3186 {
3187 *__out_false = *__first;
3188 ++__out_false;
3189 }
3190 }
3191 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3192}
3193
3194// partition_point
3195
3196template<class _ForwardIterator, class _Predicate>
Marshall Clowcb3c8262018-01-15 17:53:34 +00003197_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003198partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3199{
3200 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003201 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003202 while (__len != 0)
3203 {
3204 difference_type __l2 = __len / 2;
3205 _ForwardIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003206 _VSTD::advance(__m, __l2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003207 if (__pred(*__m))
3208 {
3209 __first = ++__m;
3210 __len -= __l2 + 1;
3211 }
3212 else
3213 __len = __l2;
3214 }
3215 return __first;
3216}
3217
3218// stable_partition
3219
3220template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3221_ForwardIterator
3222__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3223 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3224{
3225 // *__first is known to be false
3226 // __len >= 1
3227 if (__len == 1)
3228 return __first;
3229 if (__len == 2)
3230 {
3231 _ForwardIterator __m = __first;
3232 if (__pred(*++__m))
3233 {
3234 swap(*__first, *__m);
3235 return __m;
3236 }
3237 return __first;
3238 }
3239 if (__len <= __p.second)
3240 { // The buffer is big enough to use
3241 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3242 __destruct_n __d(0);
3243 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3244 // Move the falses into the temporary buffer, and the trues to the front of the line
3245 // Update __first to always point to the end of the trues
3246 value_type* __t = __p.first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003247 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003248 __d.__incr((value_type*)0);
3249 ++__t;
3250 _ForwardIterator __i = __first;
3251 while (++__i != __last)
3252 {
3253 if (__pred(*__i))
3254 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003255 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003256 ++__first;
3257 }
3258 else
3259 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003260 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003261 __d.__incr((value_type*)0);
3262 ++__t;
3263 }
3264 }
3265 // All trues now at start of range, all falses in buffer
3266 // Move falses back into range, but don't mess up __first which points to first false
3267 __i = __first;
3268 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003269 *__i = _VSTD::move(*__t2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003270 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3271 return __first;
3272 }
3273 // Else not enough buffer, do in place
3274 // __len >= 3
3275 _ForwardIterator __m = __first;
3276 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003277 _VSTD::advance(__m, __len2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003278 // recurse on [__first, __m), *__first know to be false
3279 // F?????????????????
3280 // f m l
3281 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3282 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3283 // TTTFFFFF??????????
3284 // f ff m l
3285 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3286 _ForwardIterator __m1 = __m;
3287 _ForwardIterator __second_false = __last;
3288 _Distance __len_half = __len - __len2;
3289 while (__pred(*__m1))
3290 {
3291 if (++__m1 == __last)
3292 goto __second_half_done;
3293 --__len_half;
3294 }
3295 // TTTFFFFFTTTF??????
3296 // f ff m m1 l
3297 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3298__second_half_done:
3299 // TTTFFFFFTTTTTFFFFF
3300 // f ff m sf l
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003301 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003302 // TTTTTTTTFFFFFFFFFF
3303 // |
3304}
3305
3306struct __return_temporary_buffer
3307{
3308 template <class _Tp>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003309 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003310};
3311
3312template <class _Predicate, class _ForwardIterator>
3313_ForwardIterator
3314__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3315 forward_iterator_tag)
3316{
3317 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3318 // Either prove all true and return __first or point to first false
3319 while (true)
3320 {
3321 if (__first == __last)
3322 return __first;
3323 if (!__pred(*__first))
3324 break;
3325 ++__first;
3326 }
3327 // We now have a reduced range [__first, __last)
3328 // *__first is known to be false
3329 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3330 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003331 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003332 pair<value_type*, ptrdiff_t> __p(0, 0);
3333 unique_ptr<value_type, __return_temporary_buffer> __h;
3334 if (__len >= __alloc_limit)
3335 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003336 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003337 __h.reset(__p.first);
3338 }
3339 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3340 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3341}
3342
3343template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3344_BidirectionalIterator
3345__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3346 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3347{
3348 // *__first is known to be false
3349 // *__last is known to be true
3350 // __len >= 2
3351 if (__len == 2)
3352 {
3353 swap(*__first, *__last);
3354 return __last;
3355 }
3356 if (__len == 3)
3357 {
3358 _BidirectionalIterator __m = __first;
3359 if (__pred(*++__m))
3360 {
3361 swap(*__first, *__m);
3362 swap(*__m, *__last);
3363 return __last;
3364 }
3365 swap(*__m, *__last);
3366 swap(*__first, *__m);
3367 return __m;
3368 }
3369 if (__len <= __p.second)
3370 { // The buffer is big enough to use
3371 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3372 __destruct_n __d(0);
3373 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3374 // Move the falses into the temporary buffer, and the trues to the front of the line
3375 // Update __first to always point to the end of the trues
3376 value_type* __t = __p.first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003377 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003378 __d.__incr((value_type*)0);
3379 ++__t;
3380 _BidirectionalIterator __i = __first;
3381 while (++__i != __last)
3382 {
3383 if (__pred(*__i))
3384 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003385 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003386 ++__first;
3387 }
3388 else
3389 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003390 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003391 __d.__incr((value_type*)0);
3392 ++__t;
3393 }
3394 }
3395 // move *__last, known to be true
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003396 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003397 __i = ++__first;
3398 // All trues now at start of range, all falses in buffer
3399 // Move falses back into range, but don't mess up __first which points to first false
3400 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003401 *__i = _VSTD::move(*__t2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003402 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3403 return __first;
3404 }
3405 // Else not enough buffer, do in place
3406 // __len >= 4
3407 _BidirectionalIterator __m = __first;
3408 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003409 _VSTD::advance(__m, __len2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003410 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3411 // F????????????????T
3412 // f m l
3413 _BidirectionalIterator __m1 = __m;
3414 _BidirectionalIterator __first_false = __first;
3415 _Distance __len_half = __len2;
3416 while (!__pred(*--__m1))
3417 {
3418 if (__m1 == __first)
3419 goto __first_half_done;
3420 --__len_half;
3421 }
3422 // F???TFFF?????????T
3423 // f m1 m l
3424 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3425 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3426__first_half_done:
3427 // TTTFFFFF?????????T
3428 // f ff m l
3429 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3430 __m1 = __m;
3431 _BidirectionalIterator __second_false = __last;
3432 ++__second_false;
3433 __len_half = __len - __len2;
3434 while (__pred(*__m1))
3435 {
3436 if (++__m1 == __last)
3437 goto __second_half_done;
3438 --__len_half;
3439 }
3440 // TTTFFFFFTTTF?????T
3441 // f ff m m1 l
3442 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3443__second_half_done:
3444 // TTTFFFFFTTTTTFFFFF
3445 // f ff m sf l
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003446 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003447 // TTTTTTTTFFFFFFFFFF
3448 // |
3449}
3450
3451template <class _Predicate, class _BidirectionalIterator>
3452_BidirectionalIterator
3453__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3454 bidirectional_iterator_tag)
3455{
3456 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3457 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3458 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3459 // Either prove all true and return __first or point to first false
3460 while (true)
3461 {
3462 if (__first == __last)
3463 return __first;
3464 if (!__pred(*__first))
3465 break;
3466 ++__first;
3467 }
3468 // __first points to first false, everything prior to __first is already set.
3469 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3470 do
3471 {
3472 if (__first == --__last)
3473 return __first;
3474 } while (!__pred(*__last));
3475 // We now have a reduced range [__first, __last]
3476 // *__first is known to be false
3477 // *__last is known to be true
3478 // __len >= 2
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003479 difference_type __len = _VSTD::distance(__first, __last) + 1;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003480 pair<value_type*, ptrdiff_t> __p(0, 0);
3481 unique_ptr<value_type, __return_temporary_buffer> __h;
3482 if (__len >= __alloc_limit)
3483 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003484 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003485 __h.reset(__p.first);
3486 }
3487 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3488 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3489}
3490
3491template <class _ForwardIterator, class _Predicate>
3492inline _LIBCPP_INLINE_VISIBILITY
3493_ForwardIterator
3494stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3495{
3496 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3497 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3498}
3499
3500// is_sorted_until
3501
3502template <class _ForwardIterator, class _Compare>
Marshall Clow96d050a2018-01-15 16:16:32 +00003503_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003504is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3505{
3506 if (__first != __last)
3507 {
3508 _ForwardIterator __i = __first;
3509 while (++__i != __last)
3510 {
3511 if (__comp(*__i, *__first))
3512 return __i;
3513 __first = __i;
3514 }
3515 }
3516 return __last;
3517}
3518
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003519template<class _ForwardIterator>
Marshall Clow96d050a2018-01-15 16:16:32 +00003520inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003521_ForwardIterator
3522is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3523{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003524 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003525}
3526
3527// is_sorted
3528
3529template <class _ForwardIterator, class _Compare>
Marshall Clow96d050a2018-01-15 16:16:32 +00003530inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003531bool
3532is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3533{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003534 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003535}
3536
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003537template<class _ForwardIterator>
Marshall Clow96d050a2018-01-15 16:16:32 +00003538inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539bool
3540is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3541{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003542 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003543}
3544
3545// sort
3546
3547// stable, 2-3 compares, 0-2 swaps
3548
3549template <class _Compare, class _ForwardIterator>
3550unsigned
3551__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3552{
3553 unsigned __r = 0;
3554 if (!__c(*__y, *__x)) // if x <= y
3555 {
3556 if (!__c(*__z, *__y)) // if y <= z
3557 return __r; // x <= y && y <= z
3558 // x <= y && y > z
3559 swap(*__y, *__z); // x <= z && y < z
3560 __r = 1;
3561 if (__c(*__y, *__x)) // if x > y
3562 {
3563 swap(*__x, *__y); // x < y && y <= z
3564 __r = 2;
3565 }
3566 return __r; // x <= y && y < z
3567 }
3568 if (__c(*__z, *__y)) // x > y, if y > z
3569 {
3570 swap(*__x, *__z); // x < y && y < z
3571 __r = 1;
3572 return __r;
3573 }
3574 swap(*__x, *__y); // x > y && y <= z
3575 __r = 1; // x < y && x <= z
3576 if (__c(*__z, *__y)) // if y > z
3577 {
3578 swap(*__y, *__z); // x <= y && y < z
3579 __r = 2;
3580 }
3581 return __r;
3582} // x <= y && y <= z
3583
3584// stable, 3-6 compares, 0-5 swaps
3585
3586template <class _Compare, class _ForwardIterator>
3587unsigned
3588__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3589 _ForwardIterator __x4, _Compare __c)
3590{
3591 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3592 if (__c(*__x4, *__x3))
3593 {
3594 swap(*__x3, *__x4);
3595 ++__r;
3596 if (__c(*__x3, *__x2))
3597 {
3598 swap(*__x2, *__x3);
3599 ++__r;
3600 if (__c(*__x2, *__x1))
3601 {
3602 swap(*__x1, *__x2);
3603 ++__r;
3604 }
3605 }
3606 }
3607 return __r;
3608}
3609
3610// stable, 4-10 compares, 0-9 swaps
3611
3612template <class _Compare, class _ForwardIterator>
3613unsigned
3614__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3615 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3616{
3617 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3618 if (__c(*__x5, *__x4))
3619 {
3620 swap(*__x4, *__x5);
3621 ++__r;
3622 if (__c(*__x4, *__x3))
3623 {
3624 swap(*__x3, *__x4);
3625 ++__r;
3626 if (__c(*__x3, *__x2))
3627 {
3628 swap(*__x2, *__x3);
3629 ++__r;
3630 if (__c(*__x2, *__x1))
3631 {
3632 swap(*__x1, *__x2);
3633 ++__r;
3634 }
3635 }
3636 }
3637 }
3638 return __r;
3639}
3640
3641// Assumes size > 0
3642template <class _Compare, class _BirdirectionalIterator>
3643void
3644__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3645{
3646 _BirdirectionalIterator __lm1 = __last;
3647 for (--__lm1; __first != __lm1; ++__first)
3648 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003649 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650 typename add_lvalue_reference<_Compare>::type>
3651 (__first, __last, __comp);
3652 if (__i != __first)
3653 swap(*__first, *__i);
3654 }
3655}
3656
3657template <class _Compare, class _BirdirectionalIterator>
3658void
3659__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3660{
3661 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3662 if (__first != __last)
3663 {
3664 _BirdirectionalIterator __i = __first;
3665 for (++__i; __i != __last; ++__i)
3666 {
3667 _BirdirectionalIterator __j = __i;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003668 value_type __t(_VSTD::move(*__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003669 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003670 *__j = _VSTD::move(*__k);
3671 *__j = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003672 }
3673 }
3674}
3675
3676template <class _Compare, class _RandomAccessIterator>
3677void
3678__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3679{
3680 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3681 _RandomAccessIterator __j = __first+2;
3682 __sort3<_Compare>(__first, __first+1, __j, __comp);
3683 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3684 {
3685 if (__comp(*__i, *__j))
3686 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003687 value_type __t(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003688 _RandomAccessIterator __k = __j;
3689 __j = __i;
3690 do
3691 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003692 *__j = _VSTD::move(*__k);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003693 __j = __k;
3694 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003695 *__j = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003696 }
3697 __j = __i;
3698 }
3699}
3700
3701template <class _Compare, class _RandomAccessIterator>
3702bool
3703__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3704{
3705 switch (__last - __first)
3706 {
3707 case 0:
3708 case 1:
3709 return true;
3710 case 2:
3711 if (__comp(*--__last, *__first))
3712 swap(*__first, *__last);
3713 return true;
3714 case 3:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003715 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003716 return true;
3717 case 4:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003718 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003719 return true;
3720 case 5:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003721 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003722 return true;
3723 }
3724 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3725 _RandomAccessIterator __j = __first+2;
3726 __sort3<_Compare>(__first, __first+1, __j, __comp);
3727 const unsigned __limit = 8;
3728 unsigned __count = 0;
3729 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3730 {
3731 if (__comp(*__i, *__j))
3732 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003733 value_type __t(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003734 _RandomAccessIterator __k = __j;
3735 __j = __i;
3736 do
3737 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003738 *__j = _VSTD::move(*__k);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003739 __j = __k;
3740 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003741 *__j = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003742 if (++__count == __limit)
3743 return ++__i == __last;
3744 }
3745 __j = __i;
3746 }
3747 return true;
3748}
3749
3750template <class _Compare, class _BirdirectionalIterator>
3751void
3752__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3753 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3754{
3755 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3756 if (__first1 != __last1)
3757 {
3758 __destruct_n __d(0);
3759 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3760 value_type* __last2 = __first2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003761 ::new(__last2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003762 __d.__incr((value_type*)0);
3763 for (++__last2; ++__first1 != __last1; ++__last2)
3764 {
3765 value_type* __j2 = __last2;
3766 value_type* __i2 = __j2;
3767 if (__comp(*__first1, *--__i2))
3768 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003769 ::new(__j2) value_type(_VSTD::move(*__i2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003770 __d.__incr((value_type*)0);
3771 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003772 *__j2 = _VSTD::move(*__i2);
3773 *__j2 = _VSTD::move(*__first1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003774 }
3775 else
3776 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003777 ::new(__j2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003778 __d.__incr((value_type*)0);
3779 }
3780 }
3781 __h.release();
3782 }
3783}
3784
3785template <class _Compare, class _RandomAccessIterator>
3786void
3787__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3788{
3789 // _Compare is known to be a reference type
3790 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3791 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003792 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3793 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003794 while (true)
3795 {
3796 __restart:
3797 difference_type __len = __last - __first;
3798 switch (__len)
3799 {
3800 case 0:
3801 case 1:
3802 return;
3803 case 2:
3804 if (__comp(*--__last, *__first))
3805 swap(*__first, *__last);
3806 return;
3807 case 3:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003808 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003809 return;
3810 case 4:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003811 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003812 return;
3813 case 5:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003814 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003815 return;
3816 }
3817 if (__len <= __limit)
3818 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003819 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003820 return;
3821 }
3822 // __len > 5
3823 _RandomAccessIterator __m = __first;
3824 _RandomAccessIterator __lm1 = __last;
3825 --__lm1;
3826 unsigned __n_swaps;
3827 {
3828 difference_type __delta;
3829 if (__len >= 1000)
3830 {
3831 __delta = __len/2;
3832 __m += __delta;
3833 __delta /= 2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003834 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003835 }
3836 else
3837 {
3838 __delta = __len/2;
3839 __m += __delta;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003840 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003841 }
3842 }
3843 // *__m is median
3844 // partition [__first, __m) < *__m and *__m <= [__m, __last)
3845 // (this inhibits tossing elements equivalent to __m around unnecessarily)
3846 _RandomAccessIterator __i = __first;
3847 _RandomAccessIterator __j = __lm1;
3848 // j points beyond range to be tested, *__m is known to be <= *__lm1
3849 // The search going up is known to be guarded but the search coming down isn't.
3850 // Prime the downward search with a guard.
3851 if (!__comp(*__i, *__m)) // if *__first == *__m
3852 {
3853 // *__first == *__m, *__first doesn't go in first part
3854 // manually guard downward moving __j against __i
3855 while (true)
3856 {
3857 if (__i == --__j)
3858 {
3859 // *__first == *__m, *__m <= all other elements
3860 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
3861 ++__i; // __first + 1
3862 __j = __last;
3863 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
3864 {
3865 while (true)
3866 {
3867 if (__i == __j)
3868 return; // [__first, __last) all equivalent elements
3869 if (__comp(*__first, *__i))
3870 {
3871 swap(*__i, *__j);
3872 ++__n_swaps;
3873 ++__i;
3874 break;
3875 }
3876 ++__i;
3877 }
3878 }
3879 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
3880 if (__i == __j)
3881 return;
3882 while (true)
3883 {
3884 while (!__comp(*__first, *__i))
3885 ++__i;
3886 while (__comp(*__first, *--__j))
3887 ;
3888 if (__i >= __j)
3889 break;
3890 swap(*__i, *__j);
3891 ++__n_swaps;
3892 ++__i;
3893 }
3894 // [__first, __i) == *__first and *__first < [__i, __last)
3895 // The first part is sorted, sort the secod part
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003896 // _VSTD::__sort<_Compare>(__i, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003897 __first = __i;
3898 goto __restart;
3899 }
3900 if (__comp(*__j, *__m))
3901 {
3902 swap(*__i, *__j);
3903 ++__n_swaps;
3904 break; // found guard for downward moving __j, now use unguarded partition
3905 }
3906 }
3907 }
3908 // It is known that *__i < *__m
3909 ++__i;
3910 // j points beyond range to be tested, *__m is known to be <= *__lm1
3911 // if not yet partitioned...
3912 if (__i < __j)
3913 {
3914 // known that *(__i - 1) < *__m
3915 // known that __i <= __m
3916 while (true)
3917 {
3918 // __m still guards upward moving __i
3919 while (__comp(*__i, *__m))
3920 ++__i;
3921 // It is now known that a guard exists for downward moving __j
3922 while (!__comp(*--__j, *__m))
3923 ;
3924 if (__i > __j)
3925 break;
3926 swap(*__i, *__j);
3927 ++__n_swaps;
3928 // It is known that __m != __j
3929 // If __m just moved, follow it
3930 if (__m == __i)
3931 __m = __j;
3932 ++__i;
3933 }
3934 }
3935 // [__first, __i) < *__m and *__m <= [__i, __last)
3936 if (__i != __m && __comp(*__m, *__i))
3937 {
3938 swap(*__i, *__m);
3939 ++__n_swaps;
3940 }
3941 // [__first, __i) < *__i and *__i <= [__i+1, __last)
3942 // If we were given a perfect partition, see if insertion sort is quick...
3943 if (__n_swaps == 0)
3944 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003945 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
3946 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003947 {
3948 if (__fs)
3949 return;
3950 __last = __i;
3951 continue;
3952 }
3953 else
3954 {
3955 if (__fs)
3956 {
3957 __first = ++__i;
3958 continue;
3959 }
3960 }
3961 }
3962 // sort smaller range with recursive call and larger with tail recursion elimination
3963 if (__i - __first < __last - __i)
3964 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003965 _VSTD::__sort<_Compare>(__first, __i, __comp);
3966 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003967 __first = ++__i;
3968 }
3969 else
3970 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003971 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
3972 // _VSTD::__sort<_Compare>(__first, __i, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003973 __last = __i;
3974 }
3975 }
3976}
3977
3978// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
3979template <class _RandomAccessIterator, class _Compare>
3980inline _LIBCPP_INLINE_VISIBILITY
3981void
3982sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3983{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00003984#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003985 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
3986 __debug_less<_Compare> __c(__comp);
3987 __sort<_Comp_ref>(__first, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00003988#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003989 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
3990 __sort<_Comp_ref>(__first, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00003991#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003992}
3993
3994template <class _RandomAccessIterator>
3995inline _LIBCPP_INLINE_VISIBILITY
3996void
3997sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
3998{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003999 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004000}
4001
4002template <class _Tp>
4003inline _LIBCPP_INLINE_VISIBILITY
4004void
4005sort(_Tp** __first, _Tp** __last)
4006{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004007 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004008}
4009
4010template <class _Tp>
4011inline _LIBCPP_INLINE_VISIBILITY
4012void
4013sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4014{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004015 _VSTD::sort(__first.base(), __last.base());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004016}
4017
Howard Hinnant27e0e772011-09-14 18:33:51 +00004018template <class _Tp, class _Compare>
4019inline _LIBCPP_INLINE_VISIBILITY
4020void
4021sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4022{
4023 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4024 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4025}
4026
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004027_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4028_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4029_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4030_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4031_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4032_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4033_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4034_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4035_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4036_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4037_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4038_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>&))
4039_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4040_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4041_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004042
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004043_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4044_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4045_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4046_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4047_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4048_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4049_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4050_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4051_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4052_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4053_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4054_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>&))
4055_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4056_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4057_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004058
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004059_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 Hinnantc51e1022010-05-11 19:42:16 +00004060
4061// lower_bound
4062
4063template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004064_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004065__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004066{
4067 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004068 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004069 while (__len != 0)
4070 {
4071 difference_type __l2 = __len / 2;
4072 _ForwardIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004073 _VSTD::advance(__m, __l2);
Howard Hinnantbf074022011-10-22 20:59:45 +00004074 if (__comp(*__m, __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004075 {
4076 __first = ++__m;
4077 __len -= __l2 + 1;
4078 }
4079 else
4080 __len = __l2;
4081 }
4082 return __first;
4083}
4084
4085template <class _ForwardIterator, class _Tp, class _Compare>
Marshall Clowe00916c2018-01-16 02:34:41 +00004086inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004087_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004088lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004089{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004090#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004091 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4092 __debug_less<_Compare> __c(__comp);
Howard Hinnantbf074022011-10-22 20:59:45 +00004093 return __lower_bound<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004094#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004095 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantbf074022011-10-22 20:59:45 +00004096 return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004097#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004098}
4099
4100template <class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004101inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004102_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004103lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004104{
Howard Hinnantbf074022011-10-22 20:59:45 +00004105 return _VSTD::lower_bound(__first, __last, __value_,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004106 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4107}
4108
4109// upper_bound
4110
4111template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004112_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004113__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004114{
4115 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004116 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004117 while (__len != 0)
4118 {
4119 difference_type __l2 = __len / 2;
4120 _ForwardIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004121 _VSTD::advance(__m, __l2);
Howard Hinnantbf074022011-10-22 20:59:45 +00004122 if (__comp(__value_, *__m))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004123 __len = __l2;
4124 else
4125 {
4126 __first = ++__m;
4127 __len -= __l2 + 1;
4128 }
4129 }
4130 return __first;
4131}
4132
4133template <class _ForwardIterator, class _Tp, class _Compare>
Marshall Clowe00916c2018-01-16 02:34:41 +00004134inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004135_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004136upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004137{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004138#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004139 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4140 __debug_less<_Compare> __c(__comp);
Howard Hinnantbf074022011-10-22 20:59:45 +00004141 return __upper_bound<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004142#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004143 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantbf074022011-10-22 20:59:45 +00004144 return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004145#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004146}
4147
4148template <class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004149inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004150_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004151upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004152{
Howard Hinnantbf074022011-10-22 20:59:45 +00004153 return _VSTD::upper_bound(__first, __last, __value_,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004154 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4155}
4156
4157// equal_range
4158
4159template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004160_LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_ForwardIterator, _ForwardIterator>
Howard Hinnantbf074022011-10-22 20:59:45 +00004161__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004162{
4163 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004164 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004165 while (__len != 0)
4166 {
4167 difference_type __l2 = __len / 2;
4168 _ForwardIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004169 _VSTD::advance(__m, __l2);
Howard Hinnantbf074022011-10-22 20:59:45 +00004170 if (__comp(*__m, __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004171 {
4172 __first = ++__m;
4173 __len -= __l2 + 1;
4174 }
Howard Hinnantbf074022011-10-22 20:59:45 +00004175 else if (__comp(__value_, *__m))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004176 {
4177 __last = __m;
4178 __len = __l2;
4179 }
4180 else
4181 {
4182 _ForwardIterator __mp1 = __m;
4183 return pair<_ForwardIterator, _ForwardIterator>
4184 (
Howard Hinnantbf074022011-10-22 20:59:45 +00004185 __lower_bound<_Compare>(__first, __m, __value_, __comp),
4186 __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004187 );
4188 }
4189 }
4190 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4191}
4192
4193template <class _ForwardIterator, class _Tp, class _Compare>
Marshall Clowe00916c2018-01-16 02:34:41 +00004194inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004195pair<_ForwardIterator, _ForwardIterator>
Howard Hinnantbf074022011-10-22 20:59:45 +00004196equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004197{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004198#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004199 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4200 __debug_less<_Compare> __c(__comp);
Howard Hinnantbf074022011-10-22 20:59:45 +00004201 return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004202#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004203 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantbf074022011-10-22 20:59:45 +00004204 return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004205#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004206}
4207
4208template <class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004209inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004210pair<_ForwardIterator, _ForwardIterator>
Howard Hinnantbf074022011-10-22 20:59:45 +00004211equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004212{
Howard Hinnantbf074022011-10-22 20:59:45 +00004213 return _VSTD::equal_range(__first, __last, __value_,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004214 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4215}
4216
4217// binary_search
4218
4219template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004220inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004221bool
Howard Hinnantbf074022011-10-22 20:59:45 +00004222__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004223{
Howard Hinnantbf074022011-10-22 20:59:45 +00004224 __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4225 return __first != __last && !__comp(__value_, *__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226}
4227
4228template <class _ForwardIterator, class _Tp, class _Compare>
Marshall Clowe00916c2018-01-16 02:34:41 +00004229inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004230bool
Howard Hinnantbf074022011-10-22 20:59:45 +00004231binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004232{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004233#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004234 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4235 __debug_less<_Compare> __c(__comp);
Howard Hinnantbf074022011-10-22 20:59:45 +00004236 return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004237#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004238 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantbf074022011-10-22 20:59:45 +00004239 return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004240#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004241}
4242
4243template <class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004244inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004245bool
Howard Hinnantbf074022011-10-22 20:59:45 +00004246binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247{
Howard Hinnantbf074022011-10-22 20:59:45 +00004248 return _VSTD::binary_search(__first, __last, __value_,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004249 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4250}
4251
4252// merge
4253
4254template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4255_OutputIterator
4256__merge(_InputIterator1 __first1, _InputIterator1 __last1,
4257 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4258{
4259 for (; __first1 != __last1; ++__result)
4260 {
4261 if (__first2 == __last2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004262 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004263 if (__comp(*__first2, *__first1))
4264 {
4265 *__result = *__first2;
4266 ++__first2;
4267 }
4268 else
4269 {
4270 *__result = *__first1;
4271 ++__first1;
4272 }
4273 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004274 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004275}
4276
4277template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4278inline _LIBCPP_INLINE_VISIBILITY
4279_OutputIterator
4280merge(_InputIterator1 __first1, _InputIterator1 __last1,
4281 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4282{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004283#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004284 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4285 __debug_less<_Compare> __c(__comp);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004286 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004287#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004288 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004289 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004290#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004291}
4292
4293template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4294inline _LIBCPP_INLINE_VISIBILITY
4295_OutputIterator
4296merge(_InputIterator1 __first1, _InputIterator1 __last1,
4297 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4298{
4299 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4300 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4301 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4302}
4303
4304// inplace_merge
4305
Marshall Clow1bc51102015-07-29 16:25:45 +00004306template <class _Compare, class _InputIterator1, class _InputIterator2,
4307 class _OutputIterator>
4308void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4309 _InputIterator2 __first2, _InputIterator2 __last2,
4310 _OutputIterator __result, _Compare __comp)
4311{
4312 for (; __first1 != __last1; ++__result)
4313 {
4314 if (__first2 == __last2)
4315 {
4316 _VSTD::move(__first1, __last1, __result);
4317 return;
4318 }
4319
4320 if (__comp(*__first2, *__first1))
4321 {
4322 *__result = _VSTD::move(*__first2);
4323 ++__first2;
4324 }
4325 else
4326 {
4327 *__result = _VSTD::move(*__first1);
4328 ++__first1;
4329 }
4330 }
4331 // __first2 through __last2 are already in the right spot.
4332}
4333
Howard Hinnantc51e1022010-05-11 19:42:16 +00004334template <class _Compare, class _BidirectionalIterator>
4335void
4336__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4337 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4338 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4339 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4340{
4341 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004342 __destruct_n __d(0);
4343 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4344 if (__len1 <= __len2)
4345 {
4346 value_type* __p = __buff;
Eric Fiseliera09a3b42014-10-27 19:28:20 +00004347 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004348 ::new(__p) value_type(_VSTD::move(*__i));
Marshall Clow1bc51102015-07-29 16:25:45 +00004349 __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004350 }
4351 else
4352 {
4353 value_type* __p = __buff;
Eric Fiseliera09a3b42014-10-27 19:28:20 +00004354 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004355 ::new(__p) value_type(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004356 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4357 typedef reverse_iterator<value_type*> _Rv;
Aditya Kumar3a0179a2016-08-25 11:52:38 +00004358 __half_inplace_merge(_Rv(__p), _Rv(__buff),
Marshall Clow1bc51102015-07-29 16:25:45 +00004359 _RBi(__middle), _RBi(__first),
Marshall Clow738d1042017-08-28 23:16:13 +00004360 _RBi(__last), __invert<_Compare>(__comp));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004361 }
4362}
4363
4364template <class _Compare, class _BidirectionalIterator>
4365void
4366__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4367 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4368 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4369 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4370{
Howard Hinnantc51e1022010-05-11 19:42:16 +00004371 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4372 while (true)
4373 {
4374 // if __middle == __last, we're done
4375 if (__len2 == 0)
4376 return;
Marshall Clow8eff8232015-02-02 16:44:11 +00004377 if (__len1 <= __buff_size || __len2 <= __buff_size)
4378 return __buffered_inplace_merge<_Compare>
4379 (__first, __middle, __last, __comp, __len1, __len2, __buff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004380 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
Eric Fiseliera09a3b42014-10-27 19:28:20 +00004381 for (; true; ++__first, (void) --__len1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004382 {
4383 if (__len1 == 0)
4384 return;
4385 if (__comp(*__middle, *__first))
4386 break;
4387 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004388 // __first < __middle < __last
4389 // *__first > *__middle
4390 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4391 // all elements in:
4392 // [__first, __m1) <= [__middle, __m2)
4393 // [__middle, __m2) < [__m1, __middle)
4394 // [__m1, __middle) <= [__m2, __last)
4395 // and __m1 or __m2 is in the middle of its range
4396 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4397 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4398 difference_type __len11; // distance(__first, __m1)
4399 difference_type __len21; // distance(__middle, __m2)
4400 // binary search smaller range
4401 if (__len1 < __len2)
4402 { // __len >= 1, __len2 >= 2
4403 __len21 = __len2 / 2;
4404 __m2 = __middle;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004405 _VSTD::advance(__m2, __len21);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004406 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004407 __len11 = _VSTD::distance(__first, __m1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004408 }
4409 else
4410 {
4411 if (__len1 == 1)
4412 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4413 // It is known *__first > *__middle
4414 swap(*__first, *__middle);
4415 return;
4416 }
4417 // __len1 >= 2, __len2 >= 1
4418 __len11 = __len1 / 2;
4419 __m1 = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004420 _VSTD::advance(__m1, __len11);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004421 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004422 __len21 = _VSTD::distance(__middle, __m2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004423 }
4424 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4425 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4426 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4427 // swap middle two partitions
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004428 __middle = _VSTD::rotate(__m1, __middle, __m2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004429 // __len12 and __len21 now have swapped meanings
4430 // merge smaller range with recurisve call and larger with tail recursion elimination
4431 if (__len11 + __len21 < __len12 + __len22)
4432 {
4433 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4434// __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4435 __first = __middle;
4436 __middle = __m2;
4437 __len1 = __len12;
4438 __len2 = __len22;
4439 }
4440 else
4441 {
4442 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4443// __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4444 __last = __middle;
4445 __middle = __m1;
4446 __len1 = __len11;
4447 __len2 = __len21;
4448 }
4449 }
4450}
4451
Howard Hinnantc51e1022010-05-11 19:42:16 +00004452template <class _BidirectionalIterator, class _Compare>
4453inline _LIBCPP_INLINE_VISIBILITY
4454void
4455inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4456 _Compare __comp)
4457{
4458 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4459 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004460 difference_type __len1 = _VSTD::distance(__first, __middle);
4461 difference_type __len2 = _VSTD::distance(__middle, __last);
4462 difference_type __buf_size = _VSTD::min(__len1, __len2);
Marshall Clow488f19f2015-02-02 17:35:53 +00004463 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4464 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
4465
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004466#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004467 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4468 __debug_less<_Compare> __c(__comp);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004469 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004470 __buf.first, __buf.second);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004471#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004472 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004473 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004474 __buf.first, __buf.second);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004475#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004476}
4477
4478template <class _BidirectionalIterator>
4479inline _LIBCPP_INLINE_VISIBILITY
4480void
4481inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4482{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004483 _VSTD::inplace_merge(__first, __middle, __last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004484 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4485}
4486
4487// stable_sort
4488
4489template <class _Compare, class _InputIterator1, class _InputIterator2>
4490void
4491__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4492 _InputIterator2 __first2, _InputIterator2 __last2,
4493 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4494{
4495 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4496 __destruct_n __d(0);
4497 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4498 for (; true; ++__result)
4499 {
4500 if (__first1 == __last1)
4501 {
4502 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004503 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004504 __h.release();
4505 return;
4506 }
4507 if (__first2 == __last2)
4508 {
4509 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004510 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004511 __h.release();
4512 return;
4513 }
4514 if (__comp(*__first2, *__first1))
4515 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004516 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004517 __d.__incr((value_type*)0);
4518 ++__first2;
4519 }
4520 else
4521 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004522 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004523 __d.__incr((value_type*)0);
4524 ++__first1;
4525 }
4526 }
4527}
4528
4529template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4530void
4531__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4532 _InputIterator2 __first2, _InputIterator2 __last2,
4533 _OutputIterator __result, _Compare __comp)
4534{
4535 for (; __first1 != __last1; ++__result)
4536 {
4537 if (__first2 == __last2)
4538 {
4539 for (; __first1 != __last1; ++__first1, ++__result)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004540 *__result = _VSTD::move(*__first1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004541 return;
4542 }
4543 if (__comp(*__first2, *__first1))
4544 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004545 *__result = _VSTD::move(*__first2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004546 ++__first2;
4547 }
4548 else
4549 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004550 *__result = _VSTD::move(*__first1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004551 ++__first1;
4552 }
4553 }
4554 for (; __first2 != __last2; ++__first2, ++__result)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004555 *__result = _VSTD::move(*__first2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004556}
4557
4558template <class _Compare, class _RandomAccessIterator>
4559void
4560__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4561 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4562 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4563
4564template <class _Compare, class _RandomAccessIterator>
4565void
4566__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4567 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4568 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4569{
4570 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4571 switch (__len)
4572 {
4573 case 0:
4574 return;
4575 case 1:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004576 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004577 return;
4578 case 2:
Marshall Clow32043ac2018-02-06 18:58:05 +00004579 __destruct_n __d(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004580 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
Marshall Clow32043ac2018-02-06 18:58:05 +00004581 if (__comp(*--__last1, *__first1))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004582 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004583 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004584 __d.__incr((value_type*)0);
4585 ++__first2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004586 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004587 }
4588 else
4589 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004590 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004591 __d.__incr((value_type*)0);
4592 ++__first2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004593 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004594 }
4595 __h2.release();
4596 return;
4597 }
4598 if (__len <= 8)
4599 {
4600 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4601 return;
4602 }
4603 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4604 _RandomAccessIterator __m = __first1 + __l2;
4605 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4606 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4607 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4608}
4609
4610template <class _Tp>
4611struct __stable_sort_switch
4612{
Howard Hinnanta9a897e2010-11-19 22:17:28 +00004613 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004614};
4615
4616template <class _Compare, class _RandomAccessIterator>
4617void
4618__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4619 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4620 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4621{
4622 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4623 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4624 switch (__len)
4625 {
4626 case 0:
4627 case 1:
4628 return;
4629 case 2:
4630 if (__comp(*--__last, *__first))
4631 swap(*__first, *__last);
4632 return;
4633 }
4634 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4635 {
4636 __insertion_sort<_Compare>(__first, __last, __comp);
4637 return;
4638 }
4639 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4640 _RandomAccessIterator __m = __first + __l2;
4641 if (__len <= __buff_size)
4642 {
4643 __destruct_n __d(0);
4644 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4645 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4646 __d.__set(__l2, (value_type*)0);
4647 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4648 __d.__set(__len, (value_type*)0);
4649 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4650// __merge<_Compare>(move_iterator<value_type*>(__buff),
4651// move_iterator<value_type*>(__buff + __l2),
4652// move_iterator<_RandomAccessIterator>(__buff + __l2),
4653// move_iterator<_RandomAccessIterator>(__buff + __len),
4654// __first, __comp);
4655 return;
4656 }
4657 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4658 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4659 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4660}
4661
4662template <class _RandomAccessIterator, class _Compare>
4663inline _LIBCPP_INLINE_VISIBILITY
4664void
4665stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4666{
4667 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4668 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4669 difference_type __len = __last - __first;
4670 pair<value_type*, ptrdiff_t> __buf(0, 0);
4671 unique_ptr<value_type, __return_temporary_buffer> __h;
4672 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4673 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004674 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004675 __h.reset(__buf.first);
4676 }
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004677#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004678 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4679 __debug_less<_Compare> __c(__comp);
4680 __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004681#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004682 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4683 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004684#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004685}
4686
4687template <class _RandomAccessIterator>
4688inline _LIBCPP_INLINE_VISIBILITY
4689void
4690stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4691{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004692 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004693}
4694
4695// is_heap_until
4696
4697template <class _RandomAccessIterator, class _Compare>
Marshall Clow96d050a2018-01-15 16:16:32 +00004698_LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00004699is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4700{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004701 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004702 difference_type __len = __last - __first;
4703 difference_type __p = 0;
4704 difference_type __c = 1;
4705 _RandomAccessIterator __pp = __first;
4706 while (__c < __len)
4707 {
4708 _RandomAccessIterator __cp = __first + __c;
4709 if (__comp(*__pp, *__cp))
4710 return __cp;
4711 ++__c;
4712 ++__cp;
4713 if (__c == __len)
4714 return __last;
4715 if (__comp(*__pp, *__cp))
4716 return __cp;
4717 ++__p;
4718 ++__pp;
4719 __c = 2 * __p + 1;
4720 }
4721 return __last;
4722}
4723
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004724template<class _RandomAccessIterator>
Marshall Clow96d050a2018-01-15 16:16:32 +00004725inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004726_RandomAccessIterator
4727is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4728{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004729 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004730}
4731
4732// is_heap
4733
4734template <class _RandomAccessIterator, class _Compare>
Marshall Clow96d050a2018-01-15 16:16:32 +00004735inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004736bool
4737is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4738{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004739 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004740}
4741
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004742template<class _RandomAccessIterator>
Marshall Clow96d050a2018-01-15 16:16:32 +00004743inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004744bool
4745is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4746{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004747 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004748}
4749
4750// push_heap
4751
4752template <class _Compare, class _RandomAccessIterator>
4753void
David Majnemer4468d562014-07-22 06:07:09 +00004754__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4755 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004756{
Howard Hinnantc51e1022010-05-11 19:42:16 +00004757 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4758 if (__len > 1)
4759 {
4760 __len = (__len - 2) / 2;
4761 _RandomAccessIterator __ptr = __first + __len;
4762 if (__comp(*__ptr, *--__last))
4763 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004764 value_type __t(_VSTD::move(*__last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004765 do
4766 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004767 *__last = _VSTD::move(*__ptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004768 __last = __ptr;
4769 if (__len == 0)
4770 break;
4771 __len = (__len - 1) / 2;
4772 __ptr = __first + __len;
4773 } while (__comp(*__ptr, __t));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004774 *__last = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004775 }
4776 }
4777}
4778
4779template <class _RandomAccessIterator, class _Compare>
4780inline _LIBCPP_INLINE_VISIBILITY
4781void
4782push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4783{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004784#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004785 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4786 __debug_less<_Compare> __c(__comp);
David Majnemer4468d562014-07-22 06:07:09 +00004787 __sift_up<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004788#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004789 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
David Majnemer4468d562014-07-22 06:07:09 +00004790 __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004791#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004792}
4793
4794template <class _RandomAccessIterator>
4795inline _LIBCPP_INLINE_VISIBILITY
4796void
4797push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4798{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004799 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004800}
4801
4802// pop_heap
4803
4804template <class _Compare, class _RandomAccessIterator>
David Majnemer4468d562014-07-22 06:07:09 +00004805void
Eric Fiselier6003c772016-12-23 23:37:52 +00004806__sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
4807 _Compare __comp,
David Majnemer4468d562014-07-22 06:07:09 +00004808 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4809 _RandomAccessIterator __start)
4810{
4811 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4812 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4813 // left-child of __start is at 2 * __start + 1
4814 // right-child of __start is at 2 * __start + 2
4815 difference_type __child = __start - __first;
4816
4817 if (__len < 2 || (__len - 2) / 2 < __child)
4818 return;
4819
4820 __child = 2 * __child + 1;
4821 _RandomAccessIterator __child_i = __first + __child;
4822
4823 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4824 // right-child exists and is greater than left-child
4825 ++__child_i;
4826 ++__child;
4827 }
4828
4829 // check if we are in heap-order
4830 if (__comp(*__child_i, *__start))
4831 // we are, __start is larger than it's largest child
4832 return;
4833
4834 value_type __top(_VSTD::move(*__start));
4835 do
4836 {
4837 // we are not in heap-order, swap the parent with it's largest child
4838 *__start = _VSTD::move(*__child_i);
4839 __start = __child_i;
4840
4841 if ((__len - 2) / 2 < __child)
4842 break;
4843
4844 // recompute the child based off of the updated parent
4845 __child = 2 * __child + 1;
4846 __child_i = __first + __child;
4847
4848 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4849 // right-child exists and is greater than left-child
4850 ++__child_i;
4851 ++__child;
4852 }
4853
4854 // check if we are in heap-order
4855 } while (!__comp(*__child_i, __top));
4856 *__start = _VSTD::move(__top);
4857}
4858
4859template <class _Compare, class _RandomAccessIterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004860inline _LIBCPP_INLINE_VISIBILITY
4861void
4862__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4863 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4864{
4865 if (__len > 1)
4866 {
4867 swap(*__first, *--__last);
David Majnemer4468d562014-07-22 06:07:09 +00004868 __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004869 }
4870}
4871
4872template <class _RandomAccessIterator, class _Compare>
4873inline _LIBCPP_INLINE_VISIBILITY
4874void
4875pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4876{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004877#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004878 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4879 __debug_less<_Compare> __c(__comp);
4880 __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004881#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004882 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4883 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004884#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004885}
4886
4887template <class _RandomAccessIterator>
4888inline _LIBCPP_INLINE_VISIBILITY
4889void
4890pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4891{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004892 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004893}
4894
4895// make_heap
4896
4897template <class _Compare, class _RandomAccessIterator>
4898void
4899__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4900{
4901 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4902 difference_type __n = __last - __first;
4903 if (__n > 1)
4904 {
David Majnemer4468d562014-07-22 06:07:09 +00004905 // start from the first parent, there is no need to consider children
4906 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
4907 {
4908 __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
4909 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004910 }
4911}
4912
4913template <class _RandomAccessIterator, class _Compare>
4914inline _LIBCPP_INLINE_VISIBILITY
4915void
4916make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4917{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004918#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004919 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4920 __debug_less<_Compare> __c(__comp);
4921 __make_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004922#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004923 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4924 __make_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004925#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004926}
4927
4928template <class _RandomAccessIterator>
4929inline _LIBCPP_INLINE_VISIBILITY
4930void
4931make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4932{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004933 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004934}
4935
4936// sort_heap
4937
4938template <class _Compare, class _RandomAccessIterator>
4939void
4940__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4941{
4942 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4943 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
4944 __pop_heap<_Compare>(__first, __last, __comp, __n);
4945}
4946
4947template <class _RandomAccessIterator, class _Compare>
4948inline _LIBCPP_INLINE_VISIBILITY
4949void
4950sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4951{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004952#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004953 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4954 __debug_less<_Compare> __c(__comp);
4955 __sort_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004956#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004957 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4958 __sort_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004959#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004960}
4961
4962template <class _RandomAccessIterator>
4963inline _LIBCPP_INLINE_VISIBILITY
4964void
4965sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4966{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004967 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004968}
4969
4970// partial_sort
4971
4972template <class _Compare, class _RandomAccessIterator>
4973void
4974__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
4975 _Compare __comp)
4976{
4977 __make_heap<_Compare>(__first, __middle, __comp);
4978 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
4979 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
4980 {
4981 if (__comp(*__i, *__first))
4982 {
4983 swap(*__i, *__first);
David Majnemer4468d562014-07-22 06:07:09 +00004984 __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004985 }
4986 }
4987 __sort_heap<_Compare>(__first, __middle, __comp);
4988}
4989
4990template <class _RandomAccessIterator, class _Compare>
4991inline _LIBCPP_INLINE_VISIBILITY
4992void
4993partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
4994 _Compare __comp)
4995{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004996#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004997 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4998 __debug_less<_Compare> __c(__comp);
4999 __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005000#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005001 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5002 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005003#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005004}
5005
5006template <class _RandomAccessIterator>
5007inline _LIBCPP_INLINE_VISIBILITY
5008void
5009partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5010{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005011 _VSTD::partial_sort(__first, __middle, __last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005012 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5013}
5014
5015// partial_sort_copy
5016
5017template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5018_RandomAccessIterator
5019__partial_sort_copy(_InputIterator __first, _InputIterator __last,
5020 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5021{
5022 _RandomAccessIterator __r = __result_first;
5023 if (__r != __result_last)
5024 {
Eric Fiseliera09a3b42014-10-27 19:28:20 +00005025 for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005026 *__r = *__first;
5027 __make_heap<_Compare>(__result_first, __r, __comp);
David Majnemer4468d562014-07-22 06:07:09 +00005028 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005029 for (; __first != __last; ++__first)
5030 if (__comp(*__first, *__result_first))
5031 {
5032 *__result_first = *__first;
David Majnemer4468d562014-07-22 06:07:09 +00005033 __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005034 }
5035 __sort_heap<_Compare>(__result_first, __r, __comp);
5036 }
5037 return __r;
5038}
5039
5040template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5041inline _LIBCPP_INLINE_VISIBILITY
5042_RandomAccessIterator
5043partial_sort_copy(_InputIterator __first, _InputIterator __last,
5044 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5045{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005046#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005047 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5048 __debug_less<_Compare> __c(__comp);
5049 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005050#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005051 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5052 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005053#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005054}
5055
5056template <class _InputIterator, class _RandomAccessIterator>
5057inline _LIBCPP_INLINE_VISIBILITY
5058_RandomAccessIterator
5059partial_sort_copy(_InputIterator __first, _InputIterator __last,
5060 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5061{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005062 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005063 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5064}
5065
5066// nth_element
5067
5068template <class _Compare, class _RandomAccessIterator>
5069void
5070__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5071{
5072 // _Compare is known to be a reference type
5073 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5074 const difference_type __limit = 7;
5075 while (true)
5076 {
5077 __restart:
Howard Hinnant2fa038c2011-12-29 17:45:35 +00005078 if (__nth == __last)
5079 return;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005080 difference_type __len = __last - __first;
5081 switch (__len)
5082 {
5083 case 0:
5084 case 1:
5085 return;
5086 case 2:
5087 if (__comp(*--__last, *__first))
5088 swap(*__first, *__last);
5089 return;
5090 case 3:
5091 {
5092 _RandomAccessIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005093 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005094 return;
5095 }
5096 }
5097 if (__len <= __limit)
5098 {
5099 __selection_sort<_Compare>(__first, __last, __comp);
5100 return;
5101 }
5102 // __len > __limit >= 3
5103 _RandomAccessIterator __m = __first + __len/2;
5104 _RandomAccessIterator __lm1 = __last;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005105 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005106 // *__m is median
5107 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5108 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5109 _RandomAccessIterator __i = __first;
5110 _RandomAccessIterator __j = __lm1;
5111 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5112 // The search going up is known to be guarded but the search coming down isn't.
5113 // Prime the downward search with a guard.
5114 if (!__comp(*__i, *__m)) // if *__first == *__m
5115 {
5116 // *__first == *__m, *__first doesn't go in first part
5117 // manually guard downward moving __j against __i
5118 while (true)
5119 {
5120 if (__i == --__j)
5121 {
5122 // *__first == *__m, *__m <= all other elements
5123 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5124 ++__i; // __first + 1
5125 __j = __last;
5126 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5127 {
5128 while (true)
5129 {
5130 if (__i == __j)
5131 return; // [__first, __last) all equivalent elements
5132 if (__comp(*__first, *__i))
5133 {
5134 swap(*__i, *__j);
5135 ++__n_swaps;
5136 ++__i;
5137 break;
5138 }
5139 ++__i;
5140 }
5141 }
5142 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5143 if (__i == __j)
5144 return;
5145 while (true)
5146 {
5147 while (!__comp(*__first, *__i))
5148 ++__i;
5149 while (__comp(*__first, *--__j))
5150 ;
5151 if (__i >= __j)
5152 break;
5153 swap(*__i, *__j);
5154 ++__n_swaps;
5155 ++__i;
5156 }
5157 // [__first, __i) == *__first and *__first < [__i, __last)
5158 // The first part is sorted,
5159 if (__nth < __i)
5160 return;
5161 // __nth_element the secod part
5162 // __nth_element<_Compare>(__i, __nth, __last, __comp);
5163 __first = __i;
5164 goto __restart;
5165 }
5166 if (__comp(*__j, *__m))
5167 {
5168 swap(*__i, *__j);
5169 ++__n_swaps;
5170 break; // found guard for downward moving __j, now use unguarded partition
5171 }
5172 }
5173 }
5174 ++__i;
5175 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5176 // if not yet partitioned...
5177 if (__i < __j)
5178 {
5179 // known that *(__i - 1) < *__m
5180 while (true)
5181 {
5182 // __m still guards upward moving __i
5183 while (__comp(*__i, *__m))
5184 ++__i;
5185 // It is now known that a guard exists for downward moving __j
5186 while (!__comp(*--__j, *__m))
5187 ;
5188 if (__i >= __j)
5189 break;
5190 swap(*__i, *__j);
5191 ++__n_swaps;
5192 // It is known that __m != __j
5193 // If __m just moved, follow it
5194 if (__m == __i)
5195 __m = __j;
5196 ++__i;
5197 }
5198 }
5199 // [__first, __i) < *__m and *__m <= [__i, __last)
5200 if (__i != __m && __comp(*__m, *__i))
5201 {
5202 swap(*__i, *__m);
5203 ++__n_swaps;
5204 }
5205 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5206 if (__nth == __i)
5207 return;
5208 if (__n_swaps == 0)
5209 {
5210 // We were given a perfectly partitioned sequence. Coincidence?
5211 if (__nth < __i)
5212 {
5213 // Check for [__first, __i) already sorted
5214 __j = __m = __first;
5215 while (++__j != __i)
5216 {
5217 if (__comp(*__j, *__m))
5218 // not yet sorted, so sort
5219 goto not_sorted;
5220 __m = __j;
5221 }
5222 // [__first, __i) sorted
5223 return;
5224 }
5225 else
5226 {
5227 // Check for [__i, __last) already sorted
5228 __j = __m = __i;
5229 while (++__j != __last)
5230 {
5231 if (__comp(*__j, *__m))
5232 // not yet sorted, so sort
5233 goto not_sorted;
5234 __m = __j;
5235 }
5236 // [__i, __last) sorted
5237 return;
5238 }
5239 }
5240not_sorted:
5241 // __nth_element on range containing __nth
5242 if (__nth < __i)
5243 {
5244 // __nth_element<_Compare>(__first, __nth, __i, __comp);
5245 __last = __i;
5246 }
5247 else
5248 {
5249 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5250 __first = ++__i;
5251 }
5252 }
5253}
5254
5255template <class _RandomAccessIterator, class _Compare>
5256inline _LIBCPP_INLINE_VISIBILITY
5257void
5258nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5259{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005260#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005261 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5262 __debug_less<_Compare> __c(__comp);
5263 __nth_element<_Comp_ref>(__first, __nth, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005264#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005265 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5266 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005267#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005268}
5269
5270template <class _RandomAccessIterator>
5271inline _LIBCPP_INLINE_VISIBILITY
5272void
5273nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5274{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005275 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00005276}
5277
5278// includes
5279
5280template <class _Compare, class _InputIterator1, class _InputIterator2>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005281_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00005282__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5283 _Compare __comp)
5284{
5285 for (; __first2 != __last2; ++__first1)
5286 {
5287 if (__first1 == __last1 || __comp(*__first2, *__first1))
5288 return false;
5289 if (!__comp(*__first1, *__first2))
5290 ++__first2;
5291 }
5292 return true;
5293}
5294
5295template <class _InputIterator1, class _InputIterator2, class _Compare>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005296inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005297bool
5298includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5299 _Compare __comp)
5300{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005301#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005302 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5303 __debug_less<_Compare> __c(__comp);
5304 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005305#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005306 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5307 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005308#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005309}
5310
5311template <class _InputIterator1, class _InputIterator2>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005312inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005313bool
5314includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5315{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005316 return _VSTD::includes(__first1, __last1, __first2, __last2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005317 __less<typename iterator_traits<_InputIterator1>::value_type,
5318 typename iterator_traits<_InputIterator2>::value_type>());
5319}
5320
5321// set_union
5322
5323template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5324_OutputIterator
5325__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5326 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5327{
5328 for (; __first1 != __last1; ++__result)
5329 {
5330 if (__first2 == __last2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005331 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005332 if (__comp(*__first2, *__first1))
5333 {
5334 *__result = *__first2;
5335 ++__first2;
5336 }
5337 else
5338 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00005339 if (!__comp(*__first1, *__first2))
5340 ++__first2;
Marshall Clowb4687412017-10-30 15:50:00 +00005341 *__result = *__first1;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005342 ++__first1;
5343 }
5344 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005345 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005346}
5347
5348template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5349inline _LIBCPP_INLINE_VISIBILITY
5350_OutputIterator
5351set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5352 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5353{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005354#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005355 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5356 __debug_less<_Compare> __c(__comp);
5357 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005358#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005359 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5360 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005361#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005362}
5363
5364template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5365inline _LIBCPP_INLINE_VISIBILITY
5366_OutputIterator
5367set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5368 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5369{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005370 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005371 __less<typename iterator_traits<_InputIterator1>::value_type,
5372 typename iterator_traits<_InputIterator2>::value_type>());
5373}
5374
5375// set_intersection
5376
5377template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005378_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00005379__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5380 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5381{
5382 while (__first1 != __last1 && __first2 != __last2)
5383 {
5384 if (__comp(*__first1, *__first2))
5385 ++__first1;
5386 else
5387 {
5388 if (!__comp(*__first2, *__first1))
5389 {
5390 *__result = *__first1;
5391 ++__result;
5392 ++__first1;
5393 }
5394 ++__first2;
5395 }
5396 }
5397 return __result;
5398}
5399
5400template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005401inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005402_OutputIterator
5403set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5404 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5405{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005406#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005407 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5408 __debug_less<_Compare> __c(__comp);
5409 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005410#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005411 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5412 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005413#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005414}
5415
5416template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005417inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005418_OutputIterator
5419set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5420 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5421{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005422 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005423 __less<typename iterator_traits<_InputIterator1>::value_type,
5424 typename iterator_traits<_InputIterator2>::value_type>());
5425}
5426
5427// set_difference
5428
5429template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5430_OutputIterator
5431__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5432 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5433{
5434 while (__first1 != __last1)
5435 {
5436 if (__first2 == __last2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005437 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005438 if (__comp(*__first1, *__first2))
5439 {
5440 *__result = *__first1;
5441 ++__result;
5442 ++__first1;
5443 }
5444 else
5445 {
5446 if (!__comp(*__first2, *__first1))
5447 ++__first1;
5448 ++__first2;
5449 }
5450 }
5451 return __result;
5452}
5453
5454template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5455inline _LIBCPP_INLINE_VISIBILITY
5456_OutputIterator
5457set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5458 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5459{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005460#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005461 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5462 __debug_less<_Compare> __c(__comp);
5463 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005464#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005465 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5466 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005467#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005468}
5469
5470template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5471inline _LIBCPP_INLINE_VISIBILITY
5472_OutputIterator
5473set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5474 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5475{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005476 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005477 __less<typename iterator_traits<_InputIterator1>::value_type,
5478 typename iterator_traits<_InputIterator2>::value_type>());
5479}
5480
5481// set_symmetric_difference
5482
5483template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5484_OutputIterator
5485__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5486 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5487{
5488 while (__first1 != __last1)
5489 {
5490 if (__first2 == __last2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005491 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005492 if (__comp(*__first1, *__first2))
5493 {
5494 *__result = *__first1;
5495 ++__result;
5496 ++__first1;
5497 }
5498 else
5499 {
5500 if (__comp(*__first2, *__first1))
5501 {
5502 *__result = *__first2;
5503 ++__result;
5504 }
5505 else
5506 ++__first1;
5507 ++__first2;
5508 }
5509 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005510 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005511}
5512
5513template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5514inline _LIBCPP_INLINE_VISIBILITY
5515_OutputIterator
5516set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5517 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5518{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005519#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005520 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5521 __debug_less<_Compare> __c(__comp);
5522 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005523#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005524 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5525 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005526#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005527}
5528
5529template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5530inline _LIBCPP_INLINE_VISIBILITY
5531_OutputIterator
5532set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5533 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5534{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005535 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005536 __less<typename iterator_traits<_InputIterator1>::value_type,
5537 typename iterator_traits<_InputIterator2>::value_type>());
5538}
5539
5540// lexicographical_compare
5541
5542template <class _Compare, class _InputIterator1, class _InputIterator2>
Marshall Clow5492c8a2018-01-22 20:44:33 +00005543_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00005544__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5545 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5546{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00005547 for (; __first2 != __last2; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005548 {
5549 if (__first1 == __last1 || __comp(*__first1, *__first2))
5550 return true;
5551 if (__comp(*__first2, *__first1))
5552 return false;
5553 }
5554 return false;
5555}
5556
5557template <class _InputIterator1, class _InputIterator2, class _Compare>
Marshall Clow5492c8a2018-01-22 20:44:33 +00005558inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005559bool
5560lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5561 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5562{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005563#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005564 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5565 __debug_less<_Compare> __c(__comp);
5566 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005567#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005568 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5569 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005570#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005571}
5572
5573template <class _InputIterator1, class _InputIterator2>
Marshall Clow5492c8a2018-01-22 20:44:33 +00005574inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005575bool
5576lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5577 _InputIterator2 __first2, _InputIterator2 __last2)
5578{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005579 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005580 __less<typename iterator_traits<_InputIterator1>::value_type,
5581 typename iterator_traits<_InputIterator2>::value_type>());
5582}
5583
5584// next_permutation
5585
5586template <class _Compare, class _BidirectionalIterator>
5587bool
5588__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5589{
5590 _BidirectionalIterator __i = __last;
5591 if (__first == __last || __first == --__i)
5592 return false;
5593 while (true)
5594 {
5595 _BidirectionalIterator __ip1 = __i;
5596 if (__comp(*--__i, *__ip1))
5597 {
5598 _BidirectionalIterator __j = __last;
5599 while (!__comp(*__i, *--__j))
5600 ;
5601 swap(*__i, *__j);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005602 _VSTD::reverse(__ip1, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005603 return true;
5604 }
5605 if (__i == __first)
5606 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005607 _VSTD::reverse(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005608 return false;
5609 }
5610 }
5611}
5612
5613template <class _BidirectionalIterator, class _Compare>
5614inline _LIBCPP_INLINE_VISIBILITY
5615bool
5616next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5617{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005618#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005619 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5620 __debug_less<_Compare> __c(__comp);
5621 return __next_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005622#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005623 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5624 return __next_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005625#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005626}
5627
5628template <class _BidirectionalIterator>
5629inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005630bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00005631next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5632{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005633 return _VSTD::next_permutation(__first, __last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005634 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5635}
5636
5637// prev_permutation
5638
5639template <class _Compare, class _BidirectionalIterator>
5640bool
5641__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5642{
5643 _BidirectionalIterator __i = __last;
5644 if (__first == __last || __first == --__i)
5645 return false;
5646 while (true)
5647 {
5648 _BidirectionalIterator __ip1 = __i;
5649 if (__comp(*__ip1, *--__i))
5650 {
5651 _BidirectionalIterator __j = __last;
5652 while (!__comp(*--__j, *__i))
5653 ;
5654 swap(*__i, *__j);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005655 _VSTD::reverse(__ip1, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005656 return true;
5657 }
5658 if (__i == __first)
5659 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005660 _VSTD::reverse(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005661 return false;
5662 }
5663 }
5664}
5665
5666template <class _BidirectionalIterator, class _Compare>
5667inline _LIBCPP_INLINE_VISIBILITY
5668bool
5669prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5670{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005671#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005672 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5673 __debug_less<_Compare> __c(__comp);
5674 return __prev_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005675#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005676 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5677 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005678#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005679}
5680
5681template <class _BidirectionalIterator>
5682inline _LIBCPP_INLINE_VISIBILITY
5683bool
5684prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5685{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005686 return _VSTD::prev_permutation(__first, __last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005687 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5688}
5689
Howard Hinnantc51e1022010-05-11 19:42:16 +00005690_LIBCPP_END_NAMESPACE_STD
5691
Eric Fiselierf4433a32017-05-31 22:07:49 +00005692_LIBCPP_POP_MACROS
5693
Howard Hinnantc51e1022010-05-11 19:42:16 +00005694#endif // _LIBCPP_ALGORITHM