blob: f119d252063bf1a55abc2fe304f2015292df8852 [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
Eric Fiselier9ed59852018-10-29 19:25:02 +0000753// Perform division by two quickly for positive integers (llvm.org/PR39129)
754
755template <typename _Integral>
756_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
757typename enable_if
758<
759 is_integral<_Integral>::value,
760 _Integral
761>::type
762__half_positive(_Integral __value)
763{
764 return static_cast<_Integral>(static_cast<typename make_unsigned<_Integral>::type>(__value) / 2);
765}
766
767template <typename _Tp>
768_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
769typename enable_if
770<
771 !is_integral<_Tp>::value,
772 _Tp
773>::type
774__half_positive(_Tp __value)
775{
776 return __value / 2;
777}
778
Howard Hinnant6148a9b2013-08-23 20:10:18 +0000779#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000780
781template <class _Compare>
782struct __debug_less
783{
784 _Compare __comp_;
785 __debug_less(_Compare& __c) : __comp_(__c) {}
Eric Fiselier32adec82016-07-19 23:27:18 +0000786
Howard Hinnantc51e1022010-05-11 19:42:16 +0000787 template <class _Tp, class _Up>
788 bool operator()(const _Tp& __x, const _Up& __y)
789 {
790 bool __r = __comp_(__x, __y);
791 if (__r)
Eric Fiselier32adec82016-07-19 23:27:18 +0000792 __do_compare_assert(0, __y, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000793 return __r;
794 }
Eric Fiselier32adec82016-07-19 23:27:18 +0000795
796 template <class _LHS, class _RHS>
797 inline _LIBCPP_INLINE_VISIBILITY
798 decltype((void)_VSTD::declval<_Compare&>()(
799 _VSTD::declval<_LHS const&>(), _VSTD::declval<_RHS const&>()))
800 __do_compare_assert(int, _LHS const& __l, _RHS const& __r) {
801 _LIBCPP_ASSERT(!__comp_(__l, __r),
802 "Comparator does not induce a strict weak ordering");
803 }
804
805 template <class _LHS, class _RHS>
806 inline _LIBCPP_INLINE_VISIBILITY
807 void __do_compare_assert(long, _LHS const&, _RHS const&) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808};
809
Howard Hinnant6148a9b2013-08-23 20:10:18 +0000810#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000811
812// all_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
817all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
818{
819 for (; __first != __last; ++__first)
820 if (!__pred(*__first))
821 return false;
822 return true;
823}
824
825// any_of
826
827template <class _InputIterator, class _Predicate>
Marshall Clowd607fdb2018-01-15 17:20:36 +0000828inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000829bool
830any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
831{
832 for (; __first != __last; ++__first)
833 if (__pred(*__first))
834 return true;
835 return false;
836}
837
838// none_of
839
840template <class _InputIterator, class _Predicate>
Marshall Clowd607fdb2018-01-15 17:20:36 +0000841inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000842bool
843none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
844{
845 for (; __first != __last; ++__first)
846 if (__pred(*__first))
847 return false;
848 return true;
849}
850
851// for_each
852
853template <class _InputIterator, class _Function>
Marshall Clow5492c8a2018-01-22 20:44:33 +0000854inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000855_Function
856for_each(_InputIterator __first, _InputIterator __last, _Function __f)
857{
858 for (; __first != __last; ++__first)
859 __f(*__first);
Marshall Clow78dbe462016-11-14 18:22:19 +0000860 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000861}
862
Marshall Clowc588fd62017-05-25 13:40:57 +0000863#if _LIBCPP_STD_VER > 14
Marshall Clowde0169c2017-05-25 02:29:54 +0000864// for_each_n
865
866template <class _InputIterator, class _Size, class _Function>
Marshall Clow5492c8a2018-01-22 20:44:33 +0000867inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowde0169c2017-05-25 02:29:54 +0000868_InputIterator
869for_each_n(_InputIterator __first, _Size __orig_n, _Function __f)
870{
871 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
872 _IntegralSize __n = __orig_n;
873 while (__n > 0)
874 {
875 __f(*__first);
876 ++__first;
877 --__n;
878 }
879 return __first;
880}
Marshall Clowc588fd62017-05-25 13:40:57 +0000881#endif
Marshall Clowde0169c2017-05-25 02:29:54 +0000882
Howard Hinnantc51e1022010-05-11 19:42:16 +0000883// find
884
885template <class _InputIterator, class _Tp>
Marshall Clow96d050a2018-01-15 16:16:32 +0000886inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000887_InputIterator
Howard Hinnantbf074022011-10-22 20:59:45 +0000888find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000889{
890 for (; __first != __last; ++__first)
Howard Hinnantbf074022011-10-22 20:59:45 +0000891 if (*__first == __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000892 break;
893 return __first;
894}
895
896// find_if
897
898template <class _InputIterator, class _Predicate>
Marshall Clow96d050a2018-01-15 16:16:32 +0000899inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000900_InputIterator
901find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
902{
903 for (; __first != __last; ++__first)
904 if (__pred(*__first))
905 break;
906 return __first;
907}
908
909// find_if_not
910
911template<class _InputIterator, class _Predicate>
Marshall Clowee0161e2018-01-15 19:26:05 +0000912inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000913_InputIterator
914find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
915{
916 for (; __first != __last; ++__first)
917 if (!__pred(*__first))
918 break;
919 return __first;
920}
921
922// find_end
923
924template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clowee0161e2018-01-15 19:26:05 +0000925_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1
Howard Hinnantc51e1022010-05-11 19:42:16 +0000926__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
927 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
928 forward_iterator_tag, forward_iterator_tag)
929{
930 // modeled after search algorithm
931 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
932 if (__first2 == __last2)
933 return __r;
934 while (true)
935 {
936 while (true)
937 {
938 if (__first1 == __last1) // if source exhausted return last correct answer
939 return __r; // (or __last1 if never found)
940 if (__pred(*__first1, *__first2))
941 break;
942 ++__first1;
943 }
944 // *__first1 matches *__first2, now match elements after here
945 _ForwardIterator1 __m1 = __first1;
946 _ForwardIterator2 __m2 = __first2;
947 while (true)
948 {
949 if (++__m2 == __last2)
950 { // Pattern exhaused, record answer and search for another one
951 __r = __first1;
952 ++__first1;
953 break;
954 }
955 if (++__m1 == __last1) // Source exhausted, return last answer
956 return __r;
957 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
958 {
959 ++__first1;
960 break;
961 } // else there is a match, check next elements
962 }
963 }
964}
965
966template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
Marshall Clowee0161e2018-01-15 19:26:05 +0000967_LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator1
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
969 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
970 bidirectional_iterator_tag, bidirectional_iterator_tag)
971{
972 // modeled after search algorithm (in reverse)
973 if (__first2 == __last2)
974 return __last1; // Everything matches an empty sequence
975 _BidirectionalIterator1 __l1 = __last1;
976 _BidirectionalIterator2 __l2 = __last2;
977 --__l2;
978 while (true)
979 {
980 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
981 while (true)
982 {
983 if (__first1 == __l1) // return __last1 if no element matches *__first2
984 return __last1;
985 if (__pred(*--__l1, *__l2))
986 break;
987 }
988 // *__l1 matches *__l2, now match elements before here
989 _BidirectionalIterator1 __m1 = __l1;
990 _BidirectionalIterator2 __m2 = __l2;
991 while (true)
992 {
993 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
994 return __m1;
995 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
996 return __last1;
997 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
998 {
999 break;
1000 } // else there is a match, check next elements
1001 }
1002 }
1003}
1004
1005template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clowedfd25a2014-06-10 18:51:55 +00001006_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
Howard Hinnantc51e1022010-05-11 19:42:16 +00001007__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1008 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1009 random_access_iterator_tag, random_access_iterator_tag)
1010{
1011 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1012 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
1013 if (__len2 == 0)
1014 return __last1;
1015 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
1016 if (__len1 < __len2)
1017 return __last1;
1018 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
1019 _RandomAccessIterator1 __l1 = __last1;
1020 _RandomAccessIterator2 __l2 = __last2;
1021 --__l2;
1022 while (true)
1023 {
1024 while (true)
1025 {
1026 if (__s == __l1)
1027 return __last1;
1028 if (__pred(*--__l1, *__l2))
1029 break;
1030 }
1031 _RandomAccessIterator1 __m1 = __l1;
1032 _RandomAccessIterator2 __m2 = __l2;
1033 while (true)
1034 {
1035 if (__m2 == __first2)
1036 return __m1;
1037 // no need to check range on __m1 because __s guarantees we have enough source
1038 if (!__pred(*--__m1, *--__m2))
1039 {
1040 break;
1041 }
1042 }
1043 }
1044}
1045
1046template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clowee0161e2018-01-15 19:26:05 +00001047inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001048_ForwardIterator1
1049find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1050 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1051{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001052 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053 (__first1, __last1, __first2, __last2, __pred,
1054 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1055 typename iterator_traits<_ForwardIterator2>::iterator_category());
1056}
1057
1058template <class _ForwardIterator1, class _ForwardIterator2>
Marshall Clowee0161e2018-01-15 19:26:05 +00001059inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001060_ForwardIterator1
1061find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1062 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1063{
1064 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1065 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001066 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067}
1068
1069// find_first_of
1070
1071template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clowedfd25a2014-06-10 18:51:55 +00001072_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1073__find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1075{
1076 for (; __first1 != __last1; ++__first1)
1077 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1078 if (__pred(*__first1, *__j))
1079 return __first1;
1080 return __last1;
1081}
1082
Marshall Clowedfd25a2014-06-10 18:51:55 +00001083
1084template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clowee0161e2018-01-15 19:26:05 +00001085inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowedfd25a2014-06-10 18:51:55 +00001086_ForwardIterator1
1087find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1088 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1089{
1090 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1091}
1092
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093template <class _ForwardIterator1, class _ForwardIterator2>
Marshall Clowee0161e2018-01-15 19:26:05 +00001094inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095_ForwardIterator1
1096find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1097 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1098{
1099 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1100 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Marshall Clowedfd25a2014-06-10 18:51:55 +00001101 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102}
1103
1104// adjacent_find
1105
1106template <class _ForwardIterator, class _BinaryPredicate>
Marshall Clowee0161e2018-01-15 19:26:05 +00001107inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001108_ForwardIterator
1109adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1110{
1111 if (__first != __last)
1112 {
1113 _ForwardIterator __i = __first;
1114 while (++__i != __last)
1115 {
1116 if (__pred(*__first, *__i))
1117 return __first;
1118 __first = __i;
1119 }
1120 }
1121 return __last;
1122}
1123
1124template <class _ForwardIterator>
Marshall Clowee0161e2018-01-15 19:26:05 +00001125inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126_ForwardIterator
1127adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1128{
1129 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001130 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131}
1132
1133// count
1134
1135template <class _InputIterator, class _Tp>
Marshall Clow3c0558f2018-01-15 19:40:34 +00001136inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001137typename iterator_traits<_InputIterator>::difference_type
Howard Hinnantbf074022011-10-22 20:59:45 +00001138count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001139{
1140 typename iterator_traits<_InputIterator>::difference_type __r(0);
1141 for (; __first != __last; ++__first)
Howard Hinnantbf074022011-10-22 20:59:45 +00001142 if (*__first == __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143 ++__r;
1144 return __r;
1145}
1146
1147// count_if
1148
1149template <class _InputIterator, class _Predicate>
Marshall Clow3c0558f2018-01-15 19:40:34 +00001150inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001151typename iterator_traits<_InputIterator>::difference_type
1152count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1153{
1154 typename iterator_traits<_InputIterator>::difference_type __r(0);
1155 for (; __first != __last; ++__first)
1156 if (__pred(*__first))
1157 ++__r;
1158 return __r;
1159}
1160
1161// mismatch
1162
1163template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Marshall Clow30bf3022018-01-16 02:04:10 +00001164inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165pair<_InputIterator1, _InputIterator2>
1166mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1167 _InputIterator2 __first2, _BinaryPredicate __pred)
1168{
Marshall Clow2932e512014-09-16 20:40:05 +00001169 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170 if (!__pred(*__first1, *__first2))
1171 break;
1172 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1173}
1174
1175template <class _InputIterator1, class _InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001176inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177pair<_InputIterator1, _InputIterator2>
1178mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1179{
1180 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1181 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001182 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001183}
1184
Marshall Clow96b42b22013-05-09 21:14:23 +00001185#if _LIBCPP_STD_VER > 11
1186template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Marshall Clow30bf3022018-01-16 02:04:10 +00001187inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001188pair<_InputIterator1, _InputIterator2>
1189mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1190 _InputIterator2 __first2, _InputIterator2 __last2,
1191 _BinaryPredicate __pred)
1192{
Marshall Clow2932e512014-09-16 20:40:05 +00001193 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow96b42b22013-05-09 21:14:23 +00001194 if (!__pred(*__first1, *__first2))
1195 break;
1196 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1197}
1198
1199template <class _InputIterator1, class _InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001200inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001201pair<_InputIterator1, _InputIterator2>
1202mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1203 _InputIterator2 __first2, _InputIterator2 __last2)
1204{
1205 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1206 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1207 return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1208}
1209#endif
1210
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211// equal
1212
1213template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Marshall Clow30bf3022018-01-16 02:04:10 +00001214inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215bool
1216equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1217{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001218 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219 if (!__pred(*__first1, *__first2))
1220 return false;
1221 return true;
1222}
1223
1224template <class _InputIterator1, class _InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001225inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226bool
1227equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1228{
1229 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1230 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001231 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232}
1233
Marshall Clow96b42b22013-05-09 21:14:23 +00001234#if _LIBCPP_STD_VER > 11
1235template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001236inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001237bool
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001238__equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow96b42b22013-05-09 21:14:23 +00001239 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1240 input_iterator_tag, input_iterator_tag )
1241{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001242 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow96b42b22013-05-09 21:14:23 +00001243 if (!__pred(*__first1, *__first2))
1244 return false;
1245 return __first1 == __last1 && __first2 == __last2;
1246}
1247
1248template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
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 +00001251__equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1252 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
Marshall Clow96b42b22013-05-09 21:14:23 +00001253 random_access_iterator_tag, random_access_iterator_tag )
1254{
1255 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1256 return false;
1257 return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1258 typename add_lvalue_reference<_BinaryPredicate>::type>
1259 (__first1, __last1, __first2, __pred );
1260}
1261
1262template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Marshall Clow30bf3022018-01-16 02:04:10 +00001263inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001264bool
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001265equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow96b42b22013-05-09 21:14:23 +00001266 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1267{
1268 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001269 (__first1, __last1, __first2, __last2, __pred,
Marshall Clow96b42b22013-05-09 21:14:23 +00001270 typename iterator_traits<_InputIterator1>::iterator_category(),
1271 typename iterator_traits<_InputIterator2>::iterator_category());
1272}
1273
1274template <class _InputIterator1, class _InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001275inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001276bool
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001277equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow96b42b22013-05-09 21:14:23 +00001278 _InputIterator2 __first2, _InputIterator2 __last2)
1279{
1280 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1281 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1282 return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1283 typename iterator_traits<_InputIterator1>::iterator_category(),
1284 typename iterator_traits<_InputIterator2>::iterator_category());
1285}
1286#endif
1287
Howard Hinnantc51e1022010-05-11 19:42:16 +00001288// is_permutation
1289
1290template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow96d050a2018-01-15 16:16:32 +00001291_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00001292is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1293 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1294{
Marshall Clow96d050a2018-01-15 16:16:32 +00001295// shorten sequences as much as possible by lopping of any equal prefix
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001296 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001297 if (!__pred(*__first1, *__first2))
Marshall Clow96d050a2018-01-15 16:16:32 +00001298 break;
1299 if (__first1 == __last1)
1300 return true;
1301
1302// __first1 != __last1 && *__first1 != *__first2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001303 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001304 _D1 __l1 = _VSTD::distance(__first1, __last1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001305 if (__l1 == _D1(1))
1306 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001307 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001308 // For each element in [f1, l1) see if there are the same number of
1309 // equal elements in [f2, l2)
1310 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1311 {
Marshall Clow96d050a2018-01-15 16:16:32 +00001312 // Have we already counted the number of *__i in [f1, l1)?
Peter Collingbournea5b451b2018-01-26 21:23:27 +00001313 _ForwardIterator1 __match = __first1;
1314 for (; __match != __i; ++__match)
1315 if (__pred(*__match, *__i))
1316 break;
1317 if (__match == __i) {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001318 // Count number of *__i in [f2, l2)
1319 _D1 __c2 = 0;
1320 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1321 if (__pred(*__i, *__j))
1322 ++__c2;
1323 if (__c2 == 0)
1324 return false;
1325 // Count number of *__i in [__i, l1) (we can start with 1)
1326 _D1 __c1 = 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001327 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001328 if (__pred(*__i, *__j))
1329 ++__c1;
1330 if (__c1 != __c2)
1331 return false;
1332 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001333 }
1334 return true;
1335}
1336
1337template<class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow96d050a2018-01-15 16:16:32 +00001338inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001339bool
1340is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1341 _ForwardIterator2 __first2)
1342{
1343 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1344 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001345 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001346}
1347
Marshall Clow96b42b22013-05-09 21:14:23 +00001348#if _LIBCPP_STD_VER > 11
1349template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow96d050a2018-01-15 16:16:32 +00001350_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Marshall Clow96b42b22013-05-09 21:14:23 +00001351__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001352 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
Marshall Clow96b42b22013-05-09 21:14:23 +00001353 _BinaryPredicate __pred,
1354 forward_iterator_tag, forward_iterator_tag )
1355{
Marshall Clow96d050a2018-01-15 16:16:32 +00001356// shorten sequences as much as possible by lopping of any equal prefix
Eric Fiselier14673892014-10-27 20:26:25 +00001357 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow96b42b22013-05-09 21:14:23 +00001358 if (!__pred(*__first1, *__first2))
Marshall Clow96d050a2018-01-15 16:16:32 +00001359 break;
1360 if (__first1 == __last1)
Marshall Clow01bbbd22018-01-19 18:07:29 +00001361 return __first2 == __last2;
Marshall Clow96d050a2018-01-15 16:16:32 +00001362 else if (__first2 == __last2)
Marshall Clow01bbbd22018-01-19 18:07:29 +00001363 return false;
Marshall Clow96d050a2018-01-15 16:16:32 +00001364
Marshall Clow96b42b22013-05-09 21:14:23 +00001365 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1366 _D1 __l1 = _VSTD::distance(__first1, __last1);
1367
1368 typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
Marshall Clow42b967c2013-05-10 00:16:10 +00001369 _D2 __l2 = _VSTD::distance(__first2, __last2);
Marshall Clow96b42b22013-05-09 21:14:23 +00001370 if (__l1 != __l2)
1371 return false;
1372
1373 // For each element in [f1, l1) see if there are the same number of
1374 // equal elements in [f2, l2)
1375 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1376 {
Marshall Clow96d050a2018-01-15 16:16:32 +00001377 // Have we already counted the number of *__i in [f1, l1)?
Peter Collingbournea5b451b2018-01-26 21:23:27 +00001378 _ForwardIterator1 __match = __first1;
1379 for (; __match != __i; ++__match)
1380 if (__pred(*__match, *__i))
1381 break;
1382 if (__match == __i) {
Marshall Clow96b42b22013-05-09 21:14:23 +00001383 // Count number of *__i in [f2, l2)
1384 _D1 __c2 = 0;
1385 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1386 if (__pred(*__i, *__j))
1387 ++__c2;
1388 if (__c2 == 0)
1389 return false;
1390 // Count number of *__i in [__i, l1) (we can start with 1)
1391 _D1 __c1 = 1;
1392 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1393 if (__pred(*__i, *__j))
1394 ++__c1;
1395 if (__c1 != __c2)
1396 return false;
1397 }
Marshall Clow96b42b22013-05-09 21:14:23 +00001398 }
1399 return true;
1400}
1401
1402template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow96d050a2018-01-15 16:16:32 +00001403_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Marshall Clow96b42b22013-05-09 21:14:23 +00001404__is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001405 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
Marshall Clow96b42b22013-05-09 21:14:23 +00001406 _BinaryPredicate __pred,
1407 random_access_iterator_tag, random_access_iterator_tag )
1408{
1409 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1410 return false;
1411 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1412 typename add_lvalue_reference<_BinaryPredicate>::type>
1413 (__first1, __last1, __first2, __pred );
1414}
1415
1416template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow96d050a2018-01-15 16:16:32 +00001417inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001418bool
1419is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1420 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1421 _BinaryPredicate __pred )
1422{
1423 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1424 (__first1, __last1, __first2, __last2, __pred,
1425 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1426 typename iterator_traits<_ForwardIterator2>::iterator_category());
1427}
1428
1429template<class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow96d050a2018-01-15 16:16:32 +00001430inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001431bool
1432is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1433 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1434{
1435 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1436 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1437 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1438 __equal_to<__v1, __v2>(),
1439 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1440 typename iterator_traits<_ForwardIterator2>::iterator_category());
1441}
1442#endif
1443
Howard Hinnantc51e1022010-05-11 19:42:16 +00001444// search
Marshall Clowa40686b2018-01-08 19:18:00 +00001445// __search is in <functional>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001446
1447template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001448inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001449_ForwardIterator1
1450search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1451 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1452{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001453 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454 (__first1, __last1, __first2, __last2, __pred,
Marshall Clowaf8be6c2016-03-08 15:12:52 +00001455 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1456 typename iterator_traits<_ForwardIterator2>::iterator_category())
1457 .first;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001458}
1459
1460template <class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001461inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001462_ForwardIterator1
1463search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1464 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1465{
Marshall Clowaf8be6c2016-03-08 15:12:52 +00001466 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1467 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001468 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469}
1470
Marshall Clowa40686b2018-01-08 19:18:00 +00001471
1472#if _LIBCPP_STD_VER > 14
1473template <class _ForwardIterator, class _Searcher>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001474_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowa40686b2018-01-08 19:18:00 +00001475_ForwardIterator search(_ForwardIterator __f, _ForwardIterator __l, const _Searcher &__s)
1476{ return __s(__f, __l).first; }
1477#endif
1478
Howard Hinnantc51e1022010-05-11 19:42:16 +00001479// search_n
1480
1481template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001482_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001483__search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnantbf074022011-10-22 20:59:45 +00001484 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001485{
1486 if (__count <= 0)
1487 return __first;
1488 while (true)
1489 {
Howard Hinnantbf074022011-10-22 20:59:45 +00001490 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantc51e1022010-05-11 19:42:16 +00001491 while (true)
1492 {
Howard Hinnantbf074022011-10-22 20:59:45 +00001493 if (__first == __last) // return __last if no element matches __value_
Howard Hinnantc51e1022010-05-11 19:42:16 +00001494 return __last;
Howard Hinnantbf074022011-10-22 20:59:45 +00001495 if (__pred(*__first, __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001496 break;
1497 ++__first;
1498 }
Howard Hinnantbf074022011-10-22 20:59:45 +00001499 // *__first matches __value_, now match elements after here
Howard Hinnantc51e1022010-05-11 19:42:16 +00001500 _ForwardIterator __m = __first;
1501 _Size __c(0);
1502 while (true)
1503 {
1504 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1505 return __first;
1506 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1507 return __last;
Howard Hinnantbf074022011-10-22 20:59:45 +00001508 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantc51e1022010-05-11 19:42:16 +00001509 {
1510 __first = __m;
1511 ++__first;
1512 break;
1513 } // else there is a match, check next elements
1514 }
1515 }
1516}
1517
1518template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001519_LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001520__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnantbf074022011-10-22 20:59:45 +00001521 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001522{
1523 if (__count <= 0)
1524 return __first;
1525 _Size __len = static_cast<_Size>(__last - __first);
1526 if (__len < __count)
1527 return __last;
1528 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1529 while (true)
1530 {
Howard Hinnantbf074022011-10-22 20:59:45 +00001531 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532 while (true)
1533 {
Howard Hinnantbede4c32013-04-04 15:40:48 +00001534 if (__first >= __s) // return __last if no element matches __value_
Howard Hinnantc51e1022010-05-11 19:42:16 +00001535 return __last;
Howard Hinnantbf074022011-10-22 20:59:45 +00001536 if (__pred(*__first, __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001537 break;
1538 ++__first;
1539 }
Howard Hinnantbf074022011-10-22 20:59:45 +00001540 // *__first matches __value_, now match elements after here
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541 _RandomAccessIterator __m = __first;
1542 _Size __c(0);
1543 while (true)
1544 {
1545 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1546 return __first;
1547 ++__m; // no need to check range on __m because __s guarantees we have enough source
Howard Hinnantbf074022011-10-22 20:59:45 +00001548 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549 {
1550 __first = __m;
1551 ++__first;
1552 break;
1553 } // else there is a match, check next elements
1554 }
1555 }
1556}
1557
1558template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001559inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560_ForwardIterator
1561search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnantbf074022011-10-22 20:59:45 +00001562 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001564 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001565 (__first, __last, __convert_to_integral(__count), __value_, __pred,
1566 typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567}
1568
1569template <class _ForwardIterator, class _Size, class _Tp>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001570inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001571_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00001572search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573{
1574 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001575 return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
1576 __value_, __equal_to<__v, _Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001577}
1578
1579// copy
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580template <class _Iter>
1581inline _LIBCPP_INLINE_VISIBILITY
1582_Iter
1583__unwrap_iter(_Iter __i)
1584{
1585 return __i;
1586}
1587
1588template <class _Tp>
Marshall Clow88880de2017-05-25 14:20:26 +00001589inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001590typename enable_if
1591<
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001592 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 _Tp*
1594>::type
1595__unwrap_iter(move_iterator<_Tp*> __i)
1596{
1597 return __i.base();
1598}
1599
Howard Hinnant8ea98242013-08-23 17:37:05 +00001600#if _LIBCPP_DEBUG_LEVEL < 2
1601
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602template <class _Tp>
Marshall Clowae4f8312018-07-13 16:35:26 +00001603inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604typename enable_if
1605<
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001606 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607 _Tp*
1608>::type
1609__unwrap_iter(__wrap_iter<_Tp*> __i)
1610{
1611 return __i.base();
1612}
1613
Eric Fiselier38badb82016-12-28 05:35:32 +00001614#else
1615
1616template <class _Tp>
Marshall Clowae4f8312018-07-13 16:35:26 +00001617inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Eric Fiselier38badb82016-12-28 05:35:32 +00001618typename enable_if
1619<
1620 is_trivially_copy_assignable<_Tp>::value,
1621 __wrap_iter<_Tp*>
1622>::type
1623__unwrap_iter(__wrap_iter<_Tp*> __i)
1624{
1625 return __i;
1626}
1627
Howard Hinnant8ea98242013-08-23 17:37:05 +00001628#endif // _LIBCPP_DEBUG_LEVEL < 2
1629
Howard Hinnantc51e1022010-05-11 19:42:16 +00001630template <class _InputIterator, class _OutputIterator>
1631inline _LIBCPP_INLINE_VISIBILITY
1632_OutputIterator
1633__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1634{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001635 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001636 *__result = *__first;
1637 return __result;
1638}
1639
1640template <class _Tp, class _Up>
1641inline _LIBCPP_INLINE_VISIBILITY
1642typename enable_if
1643<
1644 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001645 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001646 _Up*
1647>::type
1648__copy(_Tp* __first, _Tp* __last, _Up* __result)
1649{
1650 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clowc3ef9622015-06-02 13:52:16 +00001651 if (__n > 0)
1652 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001653 return __result + __n;
1654}
1655
1656template <class _InputIterator, class _OutputIterator>
1657inline _LIBCPP_INLINE_VISIBILITY
1658_OutputIterator
1659copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1660{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001661 return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662}
1663
1664// copy_backward
1665
Howard Hinnant7f229bc2013-02-06 21:03:39 +00001666template <class _BidirectionalIterator, class _OutputIterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001667inline _LIBCPP_INLINE_VISIBILITY
1668_OutputIterator
Howard Hinnant7f229bc2013-02-06 21:03:39 +00001669__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001670{
1671 while (__first != __last)
1672 *--__result = *--__last;
1673 return __result;
1674}
1675
1676template <class _Tp, class _Up>
1677inline _LIBCPP_INLINE_VISIBILITY
1678typename enable_if
1679<
1680 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001681 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001682 _Up*
1683>::type
1684__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1685{
1686 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clowc3ef9622015-06-02 13:52:16 +00001687 if (__n > 0)
1688 {
1689 __result -= __n;
1690 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1691 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001692 return __result;
1693}
1694
1695template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1696inline _LIBCPP_INLINE_VISIBILITY
1697_BidirectionalIterator2
1698copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1699 _BidirectionalIterator2 __result)
1700{
Eric Fiselier6003c772016-12-23 23:37:52 +00001701 return _VSTD::__copy_backward(__unwrap_iter(__first),
1702 __unwrap_iter(__last),
1703 __unwrap_iter(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001704}
1705
1706// copy_if
1707
1708template<class _InputIterator, class _OutputIterator, class _Predicate>
1709inline _LIBCPP_INLINE_VISIBILITY
1710_OutputIterator
1711copy_if(_InputIterator __first, _InputIterator __last,
1712 _OutputIterator __result, _Predicate __pred)
1713{
1714 for (; __first != __last; ++__first)
1715 {
1716 if (__pred(*__first))
1717 {
1718 *__result = *__first;
1719 ++__result;
1720 }
1721 }
1722 return __result;
1723}
1724
1725// copy_n
1726
1727template<class _InputIterator, class _Size, class _OutputIterator>
1728inline _LIBCPP_INLINE_VISIBILITY
1729typename enable_if
1730<
1731 __is_input_iterator<_InputIterator>::value &&
1732 !__is_random_access_iterator<_InputIterator>::value,
1733 _OutputIterator
1734>::type
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001735copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736{
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001737 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1738 _IntegralSize __n = __orig_n;
Howard Hinnantcbc5dc02011-02-27 20:55:39 +00001739 if (__n > 0)
1740 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741 *__result = *__first;
Howard Hinnantcbc5dc02011-02-27 20:55:39 +00001742 ++__result;
1743 for (--__n; __n > 0; --__n)
1744 {
1745 ++__first;
1746 *__result = *__first;
1747 ++__result;
1748 }
1749 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001750 return __result;
1751}
1752
1753template<class _InputIterator, class _Size, class _OutputIterator>
1754inline _LIBCPP_INLINE_VISIBILITY
1755typename enable_if
1756<
1757 __is_random_access_iterator<_InputIterator>::value,
1758 _OutputIterator
1759>::type
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001760copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761{
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001762 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1763 _IntegralSize __n = __orig_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001764 return _VSTD::copy(__first, __first + __n, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765}
1766
1767// move
1768
1769template <class _InputIterator, class _OutputIterator>
1770inline _LIBCPP_INLINE_VISIBILITY
1771_OutputIterator
1772__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1773{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001774 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001775 *__result = _VSTD::move(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776 return __result;
1777}
1778
1779template <class _Tp, class _Up>
1780inline _LIBCPP_INLINE_VISIBILITY
1781typename enable_if
1782<
1783 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001784 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001785 _Up*
1786>::type
1787__move(_Tp* __first, _Tp* __last, _Up* __result)
1788{
1789 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clowc3ef9622015-06-02 13:52:16 +00001790 if (__n > 0)
1791 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001792 return __result + __n;
1793}
1794
1795template <class _InputIterator, class _OutputIterator>
1796inline _LIBCPP_INLINE_VISIBILITY
1797_OutputIterator
1798move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1799{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001800 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801}
1802
1803// move_backward
1804
1805template <class _InputIterator, class _OutputIterator>
1806inline _LIBCPP_INLINE_VISIBILITY
1807_OutputIterator
1808__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1809{
1810 while (__first != __last)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001811 *--__result = _VSTD::move(*--__last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812 return __result;
1813}
1814
1815template <class _Tp, class _Up>
1816inline _LIBCPP_INLINE_VISIBILITY
1817typename enable_if
1818<
1819 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001820 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001821 _Up*
1822>::type
1823__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1824{
1825 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clowc3ef9622015-06-02 13:52:16 +00001826 if (__n > 0)
1827 {
1828 __result -= __n;
1829 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1830 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001831 return __result;
1832}
1833
1834template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1835inline _LIBCPP_INLINE_VISIBILITY
1836_BidirectionalIterator2
1837move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1838 _BidirectionalIterator2 __result)
1839{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001840 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001841}
1842
1843// iter_swap
1844
Howard Hinnantdbfd4b42011-05-27 15:04:19 +00001845// moved to <type_traits> for better swap / noexcept support
Howard Hinnantc51e1022010-05-11 19:42:16 +00001846
1847// transform
1848
1849template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
Marshall Clow31427c62018-01-19 17:45:39 +00001850inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851_OutputIterator
1852transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1853{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001854 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001855 *__result = __op(*__first);
1856 return __result;
1857}
1858
1859template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
Marshall Clow31427c62018-01-19 17:45:39 +00001860inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001861_OutputIterator
1862transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1863 _OutputIterator __result, _BinaryOperation __binary_op)
1864{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001865 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001866 *__result = __binary_op(*__first1, *__first2);
1867 return __result;
1868}
1869
1870// replace
1871
1872template <class _ForwardIterator, class _Tp>
Marshall Clow01bbbd22018-01-19 18:07:29 +00001873inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001874void
1875replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1876{
1877 for (; __first != __last; ++__first)
1878 if (*__first == __old_value)
1879 *__first = __new_value;
1880}
1881
1882// replace_if
1883
1884template <class _ForwardIterator, class _Predicate, class _Tp>
Marshall Clow01bbbd22018-01-19 18:07:29 +00001885inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886void
1887replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1888{
1889 for (; __first != __last; ++__first)
1890 if (__pred(*__first))
1891 *__first = __new_value;
1892}
1893
1894// replace_copy
1895
1896template <class _InputIterator, class _OutputIterator, class _Tp>
Marshall Clow01bbbd22018-01-19 18:07:29 +00001897inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001898_OutputIterator
1899replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1900 const _Tp& __old_value, const _Tp& __new_value)
1901{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001902 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001903 if (*__first == __old_value)
1904 *__result = __new_value;
1905 else
1906 *__result = *__first;
1907 return __result;
1908}
1909
1910// replace_copy_if
1911
1912template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
Marshall Clow01bbbd22018-01-19 18:07:29 +00001913inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914_OutputIterator
1915replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1916 _Predicate __pred, const _Tp& __new_value)
1917{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001918 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919 if (__pred(*__first))
1920 *__result = __new_value;
1921 else
1922 *__result = *__first;
1923 return __result;
1924}
1925
1926// fill_n
1927
1928template <class _OutputIterator, class _Size, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001929inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930_OutputIterator
Howard Hinnant0ad1c122013-08-01 17:29:28 +00001931__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001932{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001933 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnantbf074022011-10-22 20:59:45 +00001934 *__first = __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001935 return __first;
1936}
1937
Howard Hinnantc51e1022010-05-11 19:42:16 +00001938template <class _OutputIterator, class _Size, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001939inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940_OutputIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00001941fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001942{
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001943 return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001944}
1945
1946// fill
1947
1948template <class _ForwardIterator, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001949inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001950void
Howard Hinnantbf074022011-10-22 20:59:45 +00001951__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952{
1953 for (; __first != __last; ++__first)
Howard Hinnantbf074022011-10-22 20:59:45 +00001954 *__first = __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955}
1956
1957template <class _RandomAccessIterator, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001958inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001959void
Howard Hinnantbf074022011-10-22 20:59:45 +00001960__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001961{
Howard Hinnantbf074022011-10-22 20:59:45 +00001962 _VSTD::fill_n(__first, __last - __first, __value_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001963}
1964
1965template <class _ForwardIterator, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001966inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001967void
Howard Hinnantbf074022011-10-22 20:59:45 +00001968fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001969{
Howard Hinnantbf074022011-10-22 20:59:45 +00001970 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001971}
1972
1973// generate
1974
1975template <class _ForwardIterator, class _Generator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001976inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001977void
1978generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
1979{
1980 for (; __first != __last; ++__first)
1981 *__first = __gen();
1982}
1983
1984// generate_n
1985
1986template <class _OutputIterator, class _Size, class _Generator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001987inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001988_OutputIterator
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001989generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990{
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001991 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1992 _IntegralSize __n = __orig_n;
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001993 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001994 *__first = __gen();
1995 return __first;
1996}
1997
1998// remove
1999
2000template <class _ForwardIterator, class _Tp>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00002001_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00002002remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002003{
Howard Hinnantbf074022011-10-22 20:59:45 +00002004 __first = _VSTD::find(__first, __last, __value_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002005 if (__first != __last)
2006 {
2007 _ForwardIterator __i = __first;
2008 while (++__i != __last)
2009 {
Howard Hinnantbf074022011-10-22 20:59:45 +00002010 if (!(*__i == __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002011 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002012 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002013 ++__first;
2014 }
2015 }
2016 }
2017 return __first;
2018}
2019
2020// remove_if
2021
2022template <class _ForwardIterator, class _Predicate>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00002023_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002024remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2025{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002026 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027 (__first, __last, __pred);
2028 if (__first != __last)
2029 {
2030 _ForwardIterator __i = __first;
2031 while (++__i != __last)
2032 {
2033 if (!__pred(*__i))
2034 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002035 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002036 ++__first;
2037 }
2038 }
2039 }
2040 return __first;
2041}
2042
2043// remove_copy
2044
2045template <class _InputIterator, class _OutputIterator, class _Tp>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00002046inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002047_OutputIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00002048remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002049{
2050 for (; __first != __last; ++__first)
2051 {
Howard Hinnantbf074022011-10-22 20:59:45 +00002052 if (!(*__first == __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002053 {
2054 *__result = *__first;
2055 ++__result;
2056 }
2057 }
2058 return __result;
2059}
2060
2061// remove_copy_if
2062
2063template <class _InputIterator, class _OutputIterator, class _Predicate>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00002064inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002065_OutputIterator
2066remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2067{
2068 for (; __first != __last; ++__first)
2069 {
2070 if (!__pred(*__first))
2071 {
2072 *__result = *__first;
2073 ++__result;
2074 }
2075 }
2076 return __result;
2077}
2078
2079// unique
2080
2081template <class _ForwardIterator, class _BinaryPredicate>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002082_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002083unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2084{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002085 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002086 (__first, __last, __pred);
2087 if (__first != __last)
2088 {
2089 // ... a a ? ...
2090 // f i
2091 _ForwardIterator __i = __first;
2092 for (++__i; ++__i != __last;)
2093 if (!__pred(*__first, *__i))
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002094 *++__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002095 ++__first;
2096 }
2097 return __first;
2098}
2099
2100template <class _ForwardIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002101inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002102_ForwardIterator
2103unique(_ForwardIterator __first, _ForwardIterator __last)
2104{
2105 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002106 return _VSTD::unique(__first, __last, __equal_to<__v>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002107}
2108
2109// unique_copy
2110
2111template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002112_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2114 input_iterator_tag, output_iterator_tag)
2115{
2116 if (__first != __last)
2117 {
2118 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2119 *__result = __t;
2120 ++__result;
2121 while (++__first != __last)
2122 {
2123 if (!__pred(__t, *__first))
2124 {
2125 __t = *__first;
2126 *__result = __t;
2127 ++__result;
2128 }
2129 }
2130 }
2131 return __result;
2132}
2133
2134template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002135_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2137 forward_iterator_tag, output_iterator_tag)
2138{
2139 if (__first != __last)
2140 {
2141 _ForwardIterator __i = __first;
2142 *__result = *__i;
2143 ++__result;
2144 while (++__first != __last)
2145 {
2146 if (!__pred(*__i, *__first))
2147 {
2148 *__result = *__first;
2149 ++__result;
2150 __i = __first;
2151 }
2152 }
2153 }
2154 return __result;
2155}
2156
2157template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002158_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002159__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2160 input_iterator_tag, forward_iterator_tag)
2161{
2162 if (__first != __last)
2163 {
2164 *__result = *__first;
2165 while (++__first != __last)
2166 if (!__pred(*__result, *__first))
2167 *++__result = *__first;
2168 ++__result;
2169 }
2170 return __result;
2171}
2172
Howard Hinnantc51e1022010-05-11 19:42:16 +00002173template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002174inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002175_OutputIterator
2176unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2177{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002178 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179 (__first, __last, __result, __pred,
2180 typename iterator_traits<_InputIterator>::iterator_category(),
2181 typename iterator_traits<_OutputIterator>::iterator_category());
2182}
2183
2184template <class _InputIterator, class _OutputIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002185inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002186_OutputIterator
2187unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2188{
2189 typedef typename iterator_traits<_InputIterator>::value_type __v;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002190 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191}
2192
2193// reverse
2194
2195template <class _BidirectionalIterator>
2196inline _LIBCPP_INLINE_VISIBILITY
2197void
2198__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2199{
2200 while (__first != __last)
2201 {
2202 if (__first == --__last)
2203 break;
Marshall Clow2ad71042015-11-02 21:34:25 +00002204 _VSTD::iter_swap(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205 ++__first;
2206 }
2207}
2208
2209template <class _RandomAccessIterator>
2210inline _LIBCPP_INLINE_VISIBILITY
2211void
2212__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2213{
2214 if (__first != __last)
2215 for (; __first < --__last; ++__first)
Marshall Clow2ad71042015-11-02 21:34:25 +00002216 _VSTD::iter_swap(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217}
2218
2219template <class _BidirectionalIterator>
2220inline _LIBCPP_INLINE_VISIBILITY
2221void
2222reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2223{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002224 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225}
2226
2227// reverse_copy
2228
2229template <class _BidirectionalIterator, class _OutputIterator>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00002230inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002231_OutputIterator
2232reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2233{
2234 for (; __first != __last; ++__result)
2235 *__result = *--__last;
2236 return __result;
2237}
2238
2239// rotate
2240
2241template <class _ForwardIterator>
2242_ForwardIterator
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002243__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244{
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002245 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2246 value_type __tmp = _VSTD::move(*__first);
2247 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2248 *__lm1 = _VSTD::move(__tmp);
2249 return __lm1;
2250}
2251
2252template <class _BidirectionalIterator>
2253_BidirectionalIterator
2254__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2255{
2256 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2257 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2258 value_type __tmp = _VSTD::move(*__lm1);
2259 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2260 *__first = _VSTD::move(__tmp);
2261 return __fp1;
2262}
2263
2264template <class _ForwardIterator>
2265_ForwardIterator
2266__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2267{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002268 _ForwardIterator __i = __middle;
2269 while (true)
2270 {
2271 swap(*__first, *__i);
2272 ++__first;
2273 if (++__i == __last)
2274 break;
2275 if (__first == __middle)
2276 __middle = __i;
2277 }
2278 _ForwardIterator __r = __first;
2279 if (__first != __middle)
2280 {
2281 __i = __middle;
2282 while (true)
2283 {
2284 swap(*__first, *__i);
2285 ++__first;
2286 if (++__i == __last)
2287 {
2288 if (__first == __middle)
2289 break;
2290 __i = __middle;
2291 }
2292 else if (__first == __middle)
2293 __middle = __i;
2294 }
2295 }
2296 return __r;
2297}
2298
2299template<typename _Integral>
2300inline _LIBCPP_INLINE_VISIBILITY
2301_Integral
Marshall Clowb8bfc2c2016-07-26 14:29:45 +00002302__algo_gcd(_Integral __x, _Integral __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002303{
2304 do
2305 {
2306 _Integral __t = __x % __y;
2307 __x = __y;
2308 __y = __t;
2309 } while (__y);
2310 return __x;
2311}
2312
2313template<typename _RandomAccessIterator>
2314_RandomAccessIterator
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002315__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002316{
2317 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2318 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002319
Howard Hinnantc51e1022010-05-11 19:42:16 +00002320 const difference_type __m1 = __middle - __first;
2321 const difference_type __m2 = __last - __middle;
2322 if (__m1 == __m2)
2323 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002324 _VSTD::swap_ranges(__first, __middle, __middle);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002325 return __middle;
2326 }
Marshall Clowb8bfc2c2016-07-26 14:29:45 +00002327 const difference_type __g = _VSTD::__algo_gcd(__m1, __m2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002328 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2329 {
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002330 value_type __t(_VSTD::move(*--__p));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002331 _RandomAccessIterator __p1 = __p;
2332 _RandomAccessIterator __p2 = __p1 + __m1;
2333 do
2334 {
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002335 *__p1 = _VSTD::move(*__p2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002336 __p1 = __p2;
2337 const difference_type __d = __last - __p2;
2338 if (__m1 < __d)
2339 __p2 += __m1;
2340 else
2341 __p2 = __first + (__m1 - __d);
2342 } while (__p2 != __p);
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002343 *__p1 = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002344 }
2345 return __first + __m2;
2346}
2347
2348template <class _ForwardIterator>
2349inline _LIBCPP_INLINE_VISIBILITY
2350_ForwardIterator
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002351__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2352 _VSTD::forward_iterator_tag)
2353{
2354 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2355 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2356 {
2357 if (_VSTD::next(__first) == __middle)
2358 return _VSTD::__rotate_left(__first, __last);
2359 }
2360 return _VSTD::__rotate_forward(__first, __middle, __last);
2361}
2362
2363template <class _BidirectionalIterator>
2364inline _LIBCPP_INLINE_VISIBILITY
2365_BidirectionalIterator
2366__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2367 _VSTD::bidirectional_iterator_tag)
2368{
2369 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2370 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2371 {
2372 if (_VSTD::next(__first) == __middle)
2373 return _VSTD::__rotate_left(__first, __last);
2374 if (_VSTD::next(__middle) == __last)
2375 return _VSTD::__rotate_right(__first, __last);
2376 }
2377 return _VSTD::__rotate_forward(__first, __middle, __last);
2378}
2379
2380template <class _RandomAccessIterator>
2381inline _LIBCPP_INLINE_VISIBILITY
2382_RandomAccessIterator
2383__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2384 _VSTD::random_access_iterator_tag)
2385{
2386 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2387 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2388 {
2389 if (_VSTD::next(__first) == __middle)
2390 return _VSTD::__rotate_left(__first, __last);
2391 if (_VSTD::next(__middle) == __last)
2392 return _VSTD::__rotate_right(__first, __last);
2393 return _VSTD::__rotate_gcd(__first, __middle, __last);
2394 }
2395 return _VSTD::__rotate_forward(__first, __middle, __last);
2396}
2397
2398template <class _ForwardIterator>
2399inline _LIBCPP_INLINE_VISIBILITY
2400_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002401rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2402{
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002403 if (__first == __middle)
2404 return __last;
2405 if (__middle == __last)
2406 return __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002407 return _VSTD::__rotate(__first, __middle, __last,
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002408 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002409}
2410
2411// rotate_copy
2412
2413template <class _ForwardIterator, class _OutputIterator>
2414inline _LIBCPP_INLINE_VISIBILITY
2415_OutputIterator
2416rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2417{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002418 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002419}
2420
Howard Hinnantc51e1022010-05-11 19:42:16 +00002421// min_element
2422
2423template <class _ForwardIterator, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002424inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002425_ForwardIterator
Marshall Clow9e173072015-05-10 13:53:31 +00002426min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427{
Eric Fiselierf8ecd942018-08-22 17:47:13 +00002428 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2429 "std::min_element requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002430 if (__first != __last)
2431 {
2432 _ForwardIterator __i = __first;
2433 while (++__i != __last)
2434 if (__comp(*__i, *__first))
2435 __first = __i;
2436 }
2437 return __first;
2438}
2439
2440template <class _ForwardIterator>
Marshall Clow9e173072015-05-10 13:53:31 +00002441inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002442_ForwardIterator
2443min_element(_ForwardIterator __first, _ForwardIterator __last)
2444{
Marshall Clow9e173072015-05-10 13:53:31 +00002445 return _VSTD::min_element(__first, __last,
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002446 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2447}
2448
2449// min
2450
2451template <class _Tp, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002452inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002453const _Tp&
2454min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2455{
2456 return __comp(__b, __a) ? __b : __a;
2457}
2458
2459template <class _Tp>
Marshall Clowe9dca072014-02-19 16:51:35 +00002460inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002461const _Tp&
2462min(const _Tp& __a, const _Tp& __b)
2463{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002464 return _VSTD::min(__a, __b, __less<_Tp>());
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002465}
2466
Eric Fiselier93dd1372017-04-18 23:26:47 +00002467#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002468
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002469template<class _Tp, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002470inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002471_Tp
2472min(initializer_list<_Tp> __t, _Compare __comp)
2473{
Marshall Clow9e173072015-05-10 13:53:31 +00002474 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002475}
2476
2477template<class _Tp>
Marshall Clowe9dca072014-02-19 16:51:35 +00002478inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002479_Tp
2480min(initializer_list<_Tp> __t)
2481{
Marshall Clow9e173072015-05-10 13:53:31 +00002482 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483}
2484
Eric Fiselier93dd1372017-04-18 23:26:47 +00002485#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002486
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487// max_element
2488
2489template <class _ForwardIterator, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002490inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002491_ForwardIterator
Marshall Clow9e173072015-05-10 13:53:31 +00002492max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002493{
Eric Fiselierf8ecd942018-08-22 17:47:13 +00002494 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2495 "std::max_element requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002496 if (__first != __last)
2497 {
2498 _ForwardIterator __i = __first;
2499 while (++__i != __last)
2500 if (__comp(*__first, *__i))
2501 __first = __i;
2502 }
2503 return __first;
2504}
2505
Marshall Clowe9dca072014-02-19 16:51:35 +00002506
Howard Hinnantc51e1022010-05-11 19:42:16 +00002507template <class _ForwardIterator>
Marshall Clow9e173072015-05-10 13:53:31 +00002508inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002509_ForwardIterator
2510max_element(_ForwardIterator __first, _ForwardIterator __last)
2511{
Marshall Clow9e173072015-05-10 13:53:31 +00002512 return _VSTD::max_element(__first, __last,
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002513 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2514}
2515
2516// max
2517
2518template <class _Tp, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002519inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002520const _Tp&
2521max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2522{
2523 return __comp(__a, __b) ? __b : __a;
2524}
2525
2526template <class _Tp>
Marshall Clowe9dca072014-02-19 16:51:35 +00002527inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002528const _Tp&
2529max(const _Tp& __a, const _Tp& __b)
2530{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002531 return _VSTD::max(__a, __b, __less<_Tp>());
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002532}
2533
Eric Fiselier93dd1372017-04-18 23:26:47 +00002534#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002535
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002536template<class _Tp, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002537inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002538_Tp
2539max(initializer_list<_Tp> __t, _Compare __comp)
2540{
Marshall Clow9e173072015-05-10 13:53:31 +00002541 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002542}
2543
2544template<class _Tp>
Marshall Clowe9dca072014-02-19 16:51:35 +00002545inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002546_Tp
2547max(initializer_list<_Tp> __t)
2548{
Marshall Clow9e173072015-05-10 13:53:31 +00002549 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002550}
2551
Eric Fiselier93dd1372017-04-18 23:26:47 +00002552#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002553
Marshall Clow3e18d0e2016-03-07 22:43:49 +00002554#if _LIBCPP_STD_VER > 14
2555// clamp
2556template<class _Tp, class _Compare>
2557inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2558const _Tp&
2559clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
2560{
2561 _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
2562 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
2563
2564}
2565
2566template<class _Tp>
2567inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2568const _Tp&
2569clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
2570{
2571 return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
2572}
2573#endif
2574
Howard Hinnantc51e1022010-05-11 19:42:16 +00002575// minmax_element
2576
2577template <class _ForwardIterator, class _Compare>
Marshall Clow9e173072015-05-10 13:53:31 +00002578_LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002579std::pair<_ForwardIterator, _ForwardIterator>
2580minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2581{
Eric Fiselierf8ecd942018-08-22 17:47:13 +00002582 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2583 "std::minmax_element requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002584 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2585 if (__first != __last)
2586 {
2587 if (++__first != __last)
2588 {
2589 if (__comp(*__first, *__result.first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590 __result.first = __first;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002591 else
2592 __result.second = __first;
2593 while (++__first != __last)
2594 {
2595 _ForwardIterator __i = __first;
2596 if (++__first == __last)
2597 {
2598 if (__comp(*__i, *__result.first))
2599 __result.first = __i;
2600 else if (!__comp(*__i, *__result.second))
2601 __result.second = __i;
2602 break;
2603 }
2604 else
2605 {
2606 if (__comp(*__first, *__i))
2607 {
2608 if (__comp(*__first, *__result.first))
2609 __result.first = __first;
2610 if (!__comp(*__i, *__result.second))
2611 __result.second = __i;
2612 }
2613 else
2614 {
2615 if (__comp(*__i, *__result.first))
2616 __result.first = __i;
2617 if (!__comp(*__first, *__result.second))
2618 __result.second = __first;
2619 }
2620 }
2621 }
2622 }
2623 }
2624 return __result;
2625}
2626
2627template <class _ForwardIterator>
Marshall Clow9e173072015-05-10 13:53:31 +00002628inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002629std::pair<_ForwardIterator, _ForwardIterator>
2630minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2631{
Marshall Clowe9dca072014-02-19 16:51:35 +00002632 return _VSTD::minmax_element(__first, __last,
2633 __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002634}
2635
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002636// minmax
2637
2638template<class _Tp, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002639inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002640pair<const _Tp&, const _Tp&>
2641minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2642{
2643 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2644 pair<const _Tp&, const _Tp&>(__a, __b);
2645}
2646
2647template<class _Tp>
Marshall Clowe9dca072014-02-19 16:51:35 +00002648inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002649pair<const _Tp&, const _Tp&>
2650minmax(const _Tp& __a, const _Tp& __b)
2651{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002652 return _VSTD::minmax(__a, __b, __less<_Tp>());
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002653}
2654
Eric Fiselier93dd1372017-04-18 23:26:47 +00002655#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002656
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002657template<class _Tp, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002658inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002659pair<_Tp, _Tp>
2660minmax(initializer_list<_Tp> __t, _Compare __comp)
2661{
Marshall Clowe9dca072014-02-19 16:51:35 +00002662 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2663 _Iter __first = __t.begin();
2664 _Iter __last = __t.end();
Marshall Clow447713a2015-02-11 15:41:34 +00002665 std::pair<_Tp, _Tp> __result(*__first, *__first);
Marshall Clowe9dca072014-02-19 16:51:35 +00002666
2667 ++__first;
2668 if (__t.size() % 2 == 0)
2669 {
2670 if (__comp(*__first, __result.first))
2671 __result.first = *__first;
2672 else
2673 __result.second = *__first;
2674 ++__first;
2675 }
Aditya Kumar3a0179a2016-08-25 11:52:38 +00002676
Marshall Clowe9dca072014-02-19 16:51:35 +00002677 while (__first != __last)
2678 {
2679 _Tp __prev = *__first++;
Marshall Clow447713a2015-02-11 15:41:34 +00002680 if (__comp(*__first, __prev)) {
2681 if ( __comp(*__first, __result.first)) __result.first = *__first;
2682 if (!__comp(__prev, __result.second)) __result.second = __prev;
Marshall Clowe9dca072014-02-19 16:51:35 +00002683 }
2684 else {
Marshall Clow447713a2015-02-11 15:41:34 +00002685 if ( __comp(__prev, __result.first)) __result.first = __prev;
2686 if (!__comp(*__first, __result.second)) __result.second = *__first;
Marshall Clowe9dca072014-02-19 16:51:35 +00002687 }
Aditya Kumar3a0179a2016-08-25 11:52:38 +00002688
Marshall Clowe9dca072014-02-19 16:51:35 +00002689 __first++;
2690 }
2691 return __result;
2692}
2693
2694template<class _Tp>
2695inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2696pair<_Tp, _Tp>
2697minmax(initializer_list<_Tp> __t)
2698{
2699 return _VSTD::minmax(__t, __less<_Tp>());
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002700}
2701
Eric Fiselier93dd1372017-04-18 23:26:47 +00002702#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002703
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704// random_shuffle
2705
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002706// __independent_bits_engine
2707
Howard Hinnantc834c512011-11-29 18:15:50 +00002708template <unsigned long long _Xp, size_t _Rp>
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002709struct __log2_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710{
Howard Hinnantc834c512011-11-29 18:15:50 +00002711 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2712 : __log2_imp<_Xp, _Rp - 1>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002713};
2714
Howard Hinnantc834c512011-11-29 18:15:50 +00002715template <unsigned long long _Xp>
2716struct __log2_imp<_Xp, 0>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002717{
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002718 static const size_t value = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002719};
2720
Howard Hinnantc834c512011-11-29 18:15:50 +00002721template <size_t _Rp>
2722struct __log2_imp<0, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002723{
Howard Hinnantc834c512011-11-29 18:15:50 +00002724 static const size_t value = _Rp + 1;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002725};
2726
Eric Fiselier4638fca2017-05-31 21:20:18 +00002727template <class _UIntType, _UIntType _Xp>
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002728struct __log2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002729{
Howard Hinnantc834c512011-11-29 18:15:50 +00002730 static const size_t value = __log2_imp<_Xp,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002731 sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002732};
2733
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002734template<class _Engine, class _UIntType>
2735class __independent_bits_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00002736{
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002737public:
2738 // types
2739 typedef _UIntType result_type;
2740
2741private:
2742 typedef typename _Engine::result_type _Engine_result_type;
2743 typedef typename conditional
2744 <
2745 sizeof(_Engine_result_type) <= sizeof(result_type),
2746 result_type,
2747 _Engine_result_type
2748 >::type _Working_result_type;
2749
2750 _Engine& __e_;
2751 size_t __w_;
2752 size_t __w0_;
2753 size_t __n_;
2754 size_t __n0_;
2755 _Working_result_type __y0_;
2756 _Working_result_type __y1_;
2757 _Engine_result_type __mask0_;
2758 _Engine_result_type __mask1_;
2759
Eric Fiselier93dd1372017-04-18 23:26:47 +00002760#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc834c512011-11-29 18:15:50 +00002761 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant5a646852012-04-02 21:00:45 +00002762 + _Working_result_type(1);
2763#else
2764 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2765 + _Working_result_type(1);
2766#endif
2767 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2768 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2769 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002770
2771public:
2772 // constructors and seeding functions
2773 __independent_bits_engine(_Engine& __e, size_t __w);
2774
2775 // generating functions
Howard Hinnantc834c512011-11-29 18:15:50 +00002776 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002777
2778private:
Marshall Clowfe778582017-09-20 19:38:43 +00002779 result_type __eval(false_type);
2780 result_type __eval(true_type);
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002781};
2782
2783template<class _Engine, class _UIntType>
2784__independent_bits_engine<_Engine, _UIntType>
2785 ::__independent_bits_engine(_Engine& __e, size_t __w)
2786 : __e_(__e),
2787 __w_(__w)
2788{
2789 __n_ = __w_ / __m + (__w_ % __m != 0);
2790 __w0_ = __w_ / __n_;
Howard Hinnantc834c512011-11-29 18:15:50 +00002791 if (_Rp == 0)
2792 __y0_ = _Rp;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002793 else if (__w0_ < _WDt)
Howard Hinnantc834c512011-11-29 18:15:50 +00002794 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002795 else
2796 __y0_ = 0;
Howard Hinnantc834c512011-11-29 18:15:50 +00002797 if (_Rp - __y0_ > __y0_ / __n_)
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002798 {
2799 ++__n_;
2800 __w0_ = __w_ / __n_;
2801 if (__w0_ < _WDt)
Howard Hinnantc834c512011-11-29 18:15:50 +00002802 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002803 else
2804 __y0_ = 0;
2805 }
2806 __n0_ = __n_ - __w_ % __n_;
2807 if (__w0_ < _WDt - 1)
Howard Hinnantc834c512011-11-29 18:15:50 +00002808 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002809 else
2810 __y1_ = 0;
2811 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2812 _Engine_result_type(0);
2813 __mask1_ = __w0_ < _EDt - 1 ?
2814 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2815 _Engine_result_type(~0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816}
2817
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002818template<class _Engine, class _UIntType>
2819inline
2820_UIntType
Marshall Clowfe778582017-09-20 19:38:43 +00002821__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822{
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002823 return static_cast<result_type>(__e_() & __mask0_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824}
2825
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002826template<class _Engine, class _UIntType>
2827_UIntType
Marshall Clowfe778582017-09-20 19:38:43 +00002828__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002829{
Marshall Clowafc48592017-09-20 17:34:11 +00002830 const size_t _WRt = numeric_limits<result_type>::digits;
Howard Hinnantc834c512011-11-29 18:15:50 +00002831 result_type _Sp = 0;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002832 for (size_t __k = 0; __k < __n0_; ++__k)
2833 {
2834 _Engine_result_type __u;
2835 do
2836 {
2837 __u = __e_() - _Engine::min();
2838 } while (__u >= __y0_);
Marshall Clowafc48592017-09-20 17:34:11 +00002839 if (__w0_ < _WRt)
Howard Hinnantc834c512011-11-29 18:15:50 +00002840 _Sp <<= __w0_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002841 else
Howard Hinnantc834c512011-11-29 18:15:50 +00002842 _Sp = 0;
2843 _Sp += __u & __mask0_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002844 }
2845 for (size_t __k = __n0_; __k < __n_; ++__k)
2846 {
2847 _Engine_result_type __u;
2848 do
2849 {
2850 __u = __e_() - _Engine::min();
2851 } while (__u >= __y1_);
Marshall Clowafc48592017-09-20 17:34:11 +00002852 if (__w0_ < _WRt - 1)
Howard Hinnantc834c512011-11-29 18:15:50 +00002853 _Sp <<= __w0_ + 1;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002854 else
Howard Hinnantc834c512011-11-29 18:15:50 +00002855 _Sp = 0;
2856 _Sp += __u & __mask1_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002857 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002858 return _Sp;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002859}
2860
2861// uniform_int_distribution
2862
2863template<class _IntType = int>
2864class uniform_int_distribution
2865{
2866public:
2867 // types
2868 typedef _IntType result_type;
2869
2870 class param_type
2871 {
2872 result_type __a_;
2873 result_type __b_;
2874 public:
2875 typedef uniform_int_distribution distribution_type;
2876
2877 explicit param_type(result_type __a = 0,
2878 result_type __b = numeric_limits<result_type>::max())
2879 : __a_(__a), __b_(__b) {}
2880
2881 result_type a() const {return __a_;}
2882 result_type b() const {return __b_;}
2883
2884 friend bool operator==(const param_type& __x, const param_type& __y)
2885 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2886 friend bool operator!=(const param_type& __x, const param_type& __y)
2887 {return !(__x == __y);}
2888 };
2889
2890private:
2891 param_type __p_;
2892
2893public:
2894 // constructors and reset functions
2895 explicit uniform_int_distribution(result_type __a = 0,
2896 result_type __b = numeric_limits<result_type>::max())
2897 : __p_(param_type(__a, __b)) {}
2898 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
2899 void reset() {}
2900
2901 // generating functions
2902 template<class _URNG> result_type operator()(_URNG& __g)
2903 {return (*this)(__g, __p_);}
2904 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2905
2906 // property functions
2907 result_type a() const {return __p_.a();}
2908 result_type b() const {return __p_.b();}
2909
2910 param_type param() const {return __p_;}
2911 void param(const param_type& __p) {__p_ = __p;}
2912
2913 result_type min() const {return a();}
2914 result_type max() const {return b();}
2915
2916 friend bool operator==(const uniform_int_distribution& __x,
2917 const uniform_int_distribution& __y)
2918 {return __x.__p_ == __y.__p_;}
2919 friend bool operator!=(const uniform_int_distribution& __x,
2920 const uniform_int_distribution& __y)
2921 {return !(__x == __y);}
2922};
2923
2924template<class _IntType>
2925template<class _URNG>
2926typename uniform_int_distribution<_IntType>::result_type
2927uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
Marshall Clowf79f4402018-10-08 20:20:34 +00002928_LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002929{
2930 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
2931 uint32_t, uint64_t>::type _UIntType;
Marshall Clowf79f4402018-10-08 20:20:34 +00002932 const _UIntType _Rp = _UIntType(__p.b()) - _UIntType(__p.a()) + _UIntType(1);
Howard Hinnantc834c512011-11-29 18:15:50 +00002933 if (_Rp == 1)
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002934 return __p.a();
2935 const size_t _Dt = numeric_limits<_UIntType>::digits;
2936 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
Howard Hinnantc834c512011-11-29 18:15:50 +00002937 if (_Rp == 0)
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002938 return static_cast<result_type>(_Eng(__g, _Dt)());
Howard Hinnantc834c512011-11-29 18:15:50 +00002939 size_t __w = _Dt - __clz(_Rp) - 1;
Marshall Clow40aada52015-07-30 18:26:34 +00002940 if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002941 ++__w;
2942 _Eng __e(__g, __w);
2943 _UIntType __u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002944 do
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002945 {
2946 __u = __e();
Howard Hinnantc834c512011-11-29 18:15:50 +00002947 } while (__u >= _Rp);
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002948 return static_cast<result_type>(__u + __p.a());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002949}
2950
Eric Fiselierf5fb27c2017-04-03 23:23:44 +00002951#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \
2952 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002953class _LIBCPP_TYPE_VIS __rs_default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002954
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002955_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002956
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002957class _LIBCPP_TYPE_VIS __rs_default
Howard Hinnantc51e1022010-05-11 19:42:16 +00002958{
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002959 static unsigned __c_;
2960
2961 __rs_default();
2962public:
Marshall Clow9903c5b2013-02-07 22:12:02 +00002963 typedef uint_fast32_t result_type;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002964
2965 static const result_type _Min = 0;
2966 static const result_type _Max = 0xFFFFFFFF;
2967
2968 __rs_default(const __rs_default&);
2969 ~__rs_default();
2970
2971 result_type operator()();
2972
Howard Hinnant664183b2012-04-02 00:40:41 +00002973 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
2974 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002975
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002976 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002977};
2978
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002979_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002980
2981template <class _RandomAccessIterator>
Louis Dionne481a2662018-09-23 18:35:00 +00002982_LIBCPP_DEPRECATED_IN_CXX14 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00002983random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
2984{
2985 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc834c512011-11-29 18:15:50 +00002986 typedef uniform_int_distribution<ptrdiff_t> _Dp;
2987 typedef typename _Dp::param_type _Pp;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002988 difference_type __d = __last - __first;
2989 if (__d > 1)
2990 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002991 _Dp __uid;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002992 __rs_default __g = __rs_get();
2993 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00002994 {
Howard Hinnantc834c512011-11-29 18:15:50 +00002995 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00002996 if (__i != difference_type(0))
2997 swap(*__first, *(__first + __i));
2998 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002999 }
3000}
3001
3002template <class _RandomAccessIterator, class _RandomNumberGenerator>
Louis Dionne481a2662018-09-23 18:35:00 +00003003_LIBCPP_DEPRECATED_IN_CXX14 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00003004random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselier93dd1372017-04-18 23:26:47 +00003005#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003006 _RandomNumberGenerator&& __rand)
3007#else
3008 _RandomNumberGenerator& __rand)
3009#endif
3010{
3011 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3012 difference_type __d = __last - __first;
3013 if (__d > 1)
3014 {
3015 for (--__last; __first < __last; ++__first, --__d)
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003016 {
3017 difference_type __i = __rand(__d);
Marshall Clow5bdfc232018-09-11 18:33:45 +00003018 if (__i != difference_type(0))
Marshall Clowf79f4402018-10-08 20:20:34 +00003019 swap(*__first, *(__first + __i));
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003020 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003021 }
3022}
Marshall Clowfac06e52017-03-23 13:43:37 +00003023#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003024
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003025template <class _PopulationIterator, class _SampleIterator, class _Distance,
3026 class _UniformRandomNumberGenerator>
3027_LIBCPP_INLINE_VISIBILITY
3028_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsonc9637642017-11-14 11:14:25 +00003029 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003030 _Distance __n,
3031 _UniformRandomNumberGenerator & __g,
3032 input_iterator_tag) {
3033
3034 _Distance __k = 0;
3035 for (; __first != __last && __k < __n; ++__first, (void)++__k)
Alexander Richardsonc9637642017-11-14 11:14:25 +00003036 __output_iter[__k] = *__first;
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003037 _Distance __sz = __k;
3038 for (; __first != __last; ++__first, (void)++__k) {
3039 _Distance __r = _VSTD::uniform_int_distribution<_Distance>(0, __k)(__g);
3040 if (__r < __sz)
Alexander Richardsonc9637642017-11-14 11:14:25 +00003041 __output_iter[__r] = *__first;
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003042 }
Alexander Richardsonc9637642017-11-14 11:14:25 +00003043 return __output_iter + _VSTD::min(__n, __k);
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003044}
3045
3046template <class _PopulationIterator, class _SampleIterator, class _Distance,
3047 class _UniformRandomNumberGenerator>
3048_LIBCPP_INLINE_VISIBILITY
3049_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsonc9637642017-11-14 11:14:25 +00003050 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003051 _Distance __n,
3052 _UniformRandomNumberGenerator& __g,
3053 forward_iterator_tag) {
3054 _Distance __unsampled_sz = _VSTD::distance(__first, __last);
3055 for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
3056 _Distance __r =
3057 _VSTD::uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
3058 if (__r < __n) {
Alexander Richardsonc9637642017-11-14 11:14:25 +00003059 *__output_iter++ = *__first;
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003060 --__n;
3061 }
3062 }
Alexander Richardsonc9637642017-11-14 11:14:25 +00003063 return __output_iter;
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003064}
3065
3066template <class _PopulationIterator, class _SampleIterator, class _Distance,
3067 class _UniformRandomNumberGenerator>
3068_LIBCPP_INLINE_VISIBILITY
3069_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsonc9637642017-11-14 11:14:25 +00003070 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003071 _Distance __n, _UniformRandomNumberGenerator& __g) {
3072 typedef typename iterator_traits<_PopulationIterator>::iterator_category
3073 _PopCategory;
3074 typedef typename iterator_traits<_PopulationIterator>::difference_type
3075 _Difference;
3076 static_assert(__is_forward_iterator<_PopulationIterator>::value ||
3077 __is_random_access_iterator<_SampleIterator>::value,
3078 "SampleIterator must meet the requirements of RandomAccessIterator");
3079 typedef typename common_type<_Distance, _Difference>::type _CommonType;
3080 _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
3081 return _VSTD::__sample(
Alexander Richardsonc9637642017-11-14 11:14:25 +00003082 __first, __last, __output_iter, _CommonType(__n),
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003083 __g, _PopCategory());
3084}
3085
3086#if _LIBCPP_STD_VER > 14
3087template <class _PopulationIterator, class _SampleIterator, class _Distance,
3088 class _UniformRandomNumberGenerator>
3089inline _LIBCPP_INLINE_VISIBILITY
3090_SampleIterator sample(_PopulationIterator __first,
Alexander Richardsonc9637642017-11-14 11:14:25 +00003091 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003092 _Distance __n, _UniformRandomNumberGenerator&& __g) {
Alexander Richardsonc9637642017-11-14 11:14:25 +00003093 return _VSTD::__sample(__first, __last, __output_iter, __n, __g);
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003094}
3095#endif // _LIBCPP_STD_VER > 14
3096
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003097template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3098 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselier93dd1372017-04-18 23:26:47 +00003099#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanta5e71782010-11-18 01:47:02 +00003100 _UniformRandomNumberGenerator&& __g)
3101#else
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003102 _UniformRandomNumberGenerator& __g)
Howard Hinnanta5e71782010-11-18 01:47:02 +00003103#endif
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003104{
3105 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc834c512011-11-29 18:15:50 +00003106 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3107 typedef typename _Dp::param_type _Pp;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003108 difference_type __d = __last - __first;
3109 if (__d > 1)
3110 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003111 _Dp __uid;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003112 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003113 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003114 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003115 if (__i != difference_type(0))
3116 swap(*__first, *(__first + __i));
3117 }
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003118 }
3119}
3120
Howard Hinnantc51e1022010-05-11 19:42:16 +00003121template <class _InputIterator, class _Predicate>
Marshall Clow96d050a2018-01-15 16:16:32 +00003122_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00003123is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3124{
3125 for (; __first != __last; ++__first)
3126 if (!__pred(*__first))
3127 break;
Marshall Clow3562ed72015-02-02 18:16:35 +00003128 if ( __first == __last )
3129 return true;
3130 ++__first;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003131 for (; __first != __last; ++__first)
3132 if (__pred(*__first))
3133 return false;
3134 return true;
3135}
3136
3137// partition
3138
3139template <class _Predicate, class _ForwardIterator>
3140_ForwardIterator
3141__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3142{
3143 while (true)
3144 {
3145 if (__first == __last)
3146 return __first;
3147 if (!__pred(*__first))
3148 break;
3149 ++__first;
3150 }
3151 for (_ForwardIterator __p = __first; ++__p != __last;)
3152 {
3153 if (__pred(*__p))
3154 {
3155 swap(*__first, *__p);
3156 ++__first;
3157 }
3158 }
3159 return __first;
3160}
3161
3162template <class _Predicate, class _BidirectionalIterator>
3163_BidirectionalIterator
3164__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3165 bidirectional_iterator_tag)
3166{
3167 while (true)
3168 {
3169 while (true)
3170 {
3171 if (__first == __last)
3172 return __first;
3173 if (!__pred(*__first))
3174 break;
3175 ++__first;
3176 }
3177 do
3178 {
3179 if (__first == --__last)
3180 return __first;
3181 } while (!__pred(*__last));
3182 swap(*__first, *__last);
3183 ++__first;
3184 }
3185}
3186
3187template <class _ForwardIterator, class _Predicate>
3188inline _LIBCPP_INLINE_VISIBILITY
3189_ForwardIterator
3190partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3191{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003192 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003193 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3194}
3195
3196// partition_copy
3197
3198template <class _InputIterator, class _OutputIterator1,
3199 class _OutputIterator2, class _Predicate>
Marshall Clow5492c8a2018-01-22 20:44:33 +00003200_LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_OutputIterator1, _OutputIterator2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003201partition_copy(_InputIterator __first, _InputIterator __last,
3202 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3203 _Predicate __pred)
3204{
3205 for (; __first != __last; ++__first)
3206 {
3207 if (__pred(*__first))
3208 {
3209 *__out_true = *__first;
3210 ++__out_true;
3211 }
3212 else
3213 {
3214 *__out_false = *__first;
3215 ++__out_false;
3216 }
3217 }
3218 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3219}
3220
3221// partition_point
3222
3223template<class _ForwardIterator, class _Predicate>
Marshall Clowcb3c8262018-01-15 17:53:34 +00003224_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003225partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3226{
3227 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003228 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003229 while (__len != 0)
3230 {
Eric Fiselier9ed59852018-10-29 19:25:02 +00003231 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003232 _ForwardIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003233 _VSTD::advance(__m, __l2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003234 if (__pred(*__m))
3235 {
3236 __first = ++__m;
3237 __len -= __l2 + 1;
3238 }
3239 else
3240 __len = __l2;
3241 }
3242 return __first;
3243}
3244
3245// stable_partition
3246
3247template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3248_ForwardIterator
3249__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3250 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3251{
3252 // *__first is known to be false
3253 // __len >= 1
3254 if (__len == 1)
3255 return __first;
3256 if (__len == 2)
3257 {
3258 _ForwardIterator __m = __first;
3259 if (__pred(*++__m))
3260 {
3261 swap(*__first, *__m);
3262 return __m;
3263 }
3264 return __first;
3265 }
3266 if (__len <= __p.second)
3267 { // The buffer is big enough to use
3268 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3269 __destruct_n __d(0);
3270 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3271 // Move the falses into the temporary buffer, and the trues to the front of the line
3272 // Update __first to always point to the end of the trues
3273 value_type* __t = __p.first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003274 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003275 __d.__incr((value_type*)0);
3276 ++__t;
3277 _ForwardIterator __i = __first;
3278 while (++__i != __last)
3279 {
3280 if (__pred(*__i))
3281 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003282 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003283 ++__first;
3284 }
3285 else
3286 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003287 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003288 __d.__incr((value_type*)0);
3289 ++__t;
3290 }
3291 }
3292 // All trues now at start of range, all falses in buffer
3293 // Move falses back into range, but don't mess up __first which points to first false
3294 __i = __first;
3295 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003296 *__i = _VSTD::move(*__t2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003297 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3298 return __first;
3299 }
3300 // Else not enough buffer, do in place
3301 // __len >= 3
3302 _ForwardIterator __m = __first;
3303 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003304 _VSTD::advance(__m, __len2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003305 // recurse on [__first, __m), *__first know to be false
3306 // F?????????????????
3307 // f m l
3308 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3309 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3310 // TTTFFFFF??????????
3311 // f ff m l
3312 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3313 _ForwardIterator __m1 = __m;
3314 _ForwardIterator __second_false = __last;
3315 _Distance __len_half = __len - __len2;
3316 while (__pred(*__m1))
3317 {
3318 if (++__m1 == __last)
3319 goto __second_half_done;
3320 --__len_half;
3321 }
3322 // TTTFFFFFTTTF??????
3323 // f ff m m1 l
3324 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3325__second_half_done:
3326 // TTTFFFFFTTTTTFFFFF
3327 // f ff m sf l
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003328 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003329 // TTTTTTTTFFFFFFFFFF
3330 // |
3331}
3332
3333struct __return_temporary_buffer
3334{
3335 template <class _Tp>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003336 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003337};
3338
3339template <class _Predicate, class _ForwardIterator>
3340_ForwardIterator
3341__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3342 forward_iterator_tag)
3343{
3344 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3345 // Either prove all true and return __first or point to first false
3346 while (true)
3347 {
3348 if (__first == __last)
3349 return __first;
3350 if (!__pred(*__first))
3351 break;
3352 ++__first;
3353 }
3354 // We now have a reduced range [__first, __last)
3355 // *__first is known to be false
3356 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3357 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003358 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003359 pair<value_type*, ptrdiff_t> __p(0, 0);
3360 unique_ptr<value_type, __return_temporary_buffer> __h;
3361 if (__len >= __alloc_limit)
3362 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003363 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003364 __h.reset(__p.first);
3365 }
3366 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3367 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3368}
3369
3370template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3371_BidirectionalIterator
3372__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3373 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3374{
3375 // *__first is known to be false
3376 // *__last is known to be true
3377 // __len >= 2
3378 if (__len == 2)
3379 {
3380 swap(*__first, *__last);
3381 return __last;
3382 }
3383 if (__len == 3)
3384 {
3385 _BidirectionalIterator __m = __first;
3386 if (__pred(*++__m))
3387 {
3388 swap(*__first, *__m);
3389 swap(*__m, *__last);
3390 return __last;
3391 }
3392 swap(*__m, *__last);
3393 swap(*__first, *__m);
3394 return __m;
3395 }
3396 if (__len <= __p.second)
3397 { // The buffer is big enough to use
3398 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3399 __destruct_n __d(0);
3400 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3401 // Move the falses into the temporary buffer, and the trues to the front of the line
3402 // Update __first to always point to the end of the trues
3403 value_type* __t = __p.first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003404 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003405 __d.__incr((value_type*)0);
3406 ++__t;
3407 _BidirectionalIterator __i = __first;
3408 while (++__i != __last)
3409 {
3410 if (__pred(*__i))
3411 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003412 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003413 ++__first;
3414 }
3415 else
3416 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003417 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003418 __d.__incr((value_type*)0);
3419 ++__t;
3420 }
3421 }
3422 // move *__last, known to be true
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003423 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003424 __i = ++__first;
3425 // All trues now at start of range, all falses in buffer
3426 // Move falses back into range, but don't mess up __first which points to first false
3427 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003428 *__i = _VSTD::move(*__t2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003429 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3430 return __first;
3431 }
3432 // Else not enough buffer, do in place
3433 // __len >= 4
3434 _BidirectionalIterator __m = __first;
3435 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003436 _VSTD::advance(__m, __len2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003437 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3438 // F????????????????T
3439 // f m l
3440 _BidirectionalIterator __m1 = __m;
3441 _BidirectionalIterator __first_false = __first;
3442 _Distance __len_half = __len2;
3443 while (!__pred(*--__m1))
3444 {
3445 if (__m1 == __first)
3446 goto __first_half_done;
3447 --__len_half;
3448 }
3449 // F???TFFF?????????T
3450 // f m1 m l
3451 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3452 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3453__first_half_done:
3454 // TTTFFFFF?????????T
3455 // f ff m l
3456 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3457 __m1 = __m;
3458 _BidirectionalIterator __second_false = __last;
3459 ++__second_false;
3460 __len_half = __len - __len2;
3461 while (__pred(*__m1))
3462 {
3463 if (++__m1 == __last)
3464 goto __second_half_done;
3465 --__len_half;
3466 }
3467 // TTTFFFFFTTTF?????T
3468 // f ff m m1 l
3469 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3470__second_half_done:
3471 // TTTFFFFFTTTTTFFFFF
3472 // f ff m sf l
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003473 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003474 // TTTTTTTTFFFFFFFFFF
3475 // |
3476}
3477
3478template <class _Predicate, class _BidirectionalIterator>
3479_BidirectionalIterator
3480__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3481 bidirectional_iterator_tag)
3482{
3483 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3484 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3485 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3486 // Either prove all true and return __first or point to first false
3487 while (true)
3488 {
3489 if (__first == __last)
3490 return __first;
3491 if (!__pred(*__first))
3492 break;
3493 ++__first;
3494 }
3495 // __first points to first false, everything prior to __first is already set.
3496 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3497 do
3498 {
3499 if (__first == --__last)
3500 return __first;
3501 } while (!__pred(*__last));
3502 // We now have a reduced range [__first, __last]
3503 // *__first is known to be false
3504 // *__last is known to be true
3505 // __len >= 2
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003506 difference_type __len = _VSTD::distance(__first, __last) + 1;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003507 pair<value_type*, ptrdiff_t> __p(0, 0);
3508 unique_ptr<value_type, __return_temporary_buffer> __h;
3509 if (__len >= __alloc_limit)
3510 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003511 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003512 __h.reset(__p.first);
3513 }
3514 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3515 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3516}
3517
3518template <class _ForwardIterator, class _Predicate>
3519inline _LIBCPP_INLINE_VISIBILITY
3520_ForwardIterator
3521stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3522{
3523 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3524 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3525}
3526
3527// is_sorted_until
3528
3529template <class _ForwardIterator, class _Compare>
Marshall Clow96d050a2018-01-15 16:16:32 +00003530_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003531is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3532{
3533 if (__first != __last)
3534 {
3535 _ForwardIterator __i = __first;
3536 while (++__i != __last)
3537 {
3538 if (__comp(*__i, *__first))
3539 return __i;
3540 __first = __i;
3541 }
3542 }
3543 return __last;
3544}
3545
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003546template<class _ForwardIterator>
Marshall Clow96d050a2018-01-15 16:16:32 +00003547inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548_ForwardIterator
3549is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3550{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003551 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552}
3553
3554// is_sorted
3555
3556template <class _ForwardIterator, class _Compare>
Marshall Clow96d050a2018-01-15 16:16:32 +00003557inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003558bool
3559is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3560{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003561 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003562}
3563
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003564template<class _ForwardIterator>
Marshall Clow96d050a2018-01-15 16:16:32 +00003565inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003566bool
3567is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3568{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003569 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003570}
3571
3572// sort
3573
3574// stable, 2-3 compares, 0-2 swaps
3575
3576template <class _Compare, class _ForwardIterator>
3577unsigned
3578__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3579{
3580 unsigned __r = 0;
3581 if (!__c(*__y, *__x)) // if x <= y
3582 {
3583 if (!__c(*__z, *__y)) // if y <= z
3584 return __r; // x <= y && y <= z
3585 // x <= y && y > z
3586 swap(*__y, *__z); // x <= z && y < z
3587 __r = 1;
3588 if (__c(*__y, *__x)) // if x > y
3589 {
3590 swap(*__x, *__y); // x < y && y <= z
3591 __r = 2;
3592 }
3593 return __r; // x <= y && y < z
3594 }
3595 if (__c(*__z, *__y)) // x > y, if y > z
3596 {
3597 swap(*__x, *__z); // x < y && y < z
3598 __r = 1;
3599 return __r;
3600 }
3601 swap(*__x, *__y); // x > y && y <= z
3602 __r = 1; // x < y && x <= z
3603 if (__c(*__z, *__y)) // if y > z
3604 {
3605 swap(*__y, *__z); // x <= y && y < z
3606 __r = 2;
3607 }
3608 return __r;
3609} // x <= y && y <= z
3610
3611// stable, 3-6 compares, 0-5 swaps
3612
3613template <class _Compare, class _ForwardIterator>
3614unsigned
3615__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3616 _ForwardIterator __x4, _Compare __c)
3617{
3618 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3619 if (__c(*__x4, *__x3))
3620 {
3621 swap(*__x3, *__x4);
3622 ++__r;
3623 if (__c(*__x3, *__x2))
3624 {
3625 swap(*__x2, *__x3);
3626 ++__r;
3627 if (__c(*__x2, *__x1))
3628 {
3629 swap(*__x1, *__x2);
3630 ++__r;
3631 }
3632 }
3633 }
3634 return __r;
3635}
3636
3637// stable, 4-10 compares, 0-9 swaps
3638
3639template <class _Compare, class _ForwardIterator>
3640unsigned
3641__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3642 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3643{
3644 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3645 if (__c(*__x5, *__x4))
3646 {
3647 swap(*__x4, *__x5);
3648 ++__r;
3649 if (__c(*__x4, *__x3))
3650 {
3651 swap(*__x3, *__x4);
3652 ++__r;
3653 if (__c(*__x3, *__x2))
3654 {
3655 swap(*__x2, *__x3);
3656 ++__r;
3657 if (__c(*__x2, *__x1))
3658 {
3659 swap(*__x1, *__x2);
3660 ++__r;
3661 }
3662 }
3663 }
3664 }
3665 return __r;
3666}
3667
3668// Assumes size > 0
3669template <class _Compare, class _BirdirectionalIterator>
3670void
3671__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3672{
3673 _BirdirectionalIterator __lm1 = __last;
3674 for (--__lm1; __first != __lm1; ++__first)
3675 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003676 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003677 typename add_lvalue_reference<_Compare>::type>
3678 (__first, __last, __comp);
3679 if (__i != __first)
3680 swap(*__first, *__i);
3681 }
3682}
3683
3684template <class _Compare, class _BirdirectionalIterator>
3685void
3686__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3687{
3688 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3689 if (__first != __last)
3690 {
3691 _BirdirectionalIterator __i = __first;
3692 for (++__i; __i != __last; ++__i)
3693 {
3694 _BirdirectionalIterator __j = __i;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003695 value_type __t(_VSTD::move(*__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003696 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003697 *__j = _VSTD::move(*__k);
3698 *__j = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003699 }
3700 }
3701}
3702
3703template <class _Compare, class _RandomAccessIterator>
3704void
3705__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3706{
3707 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3708 _RandomAccessIterator __j = __first+2;
3709 __sort3<_Compare>(__first, __first+1, __j, __comp);
3710 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3711 {
3712 if (__comp(*__i, *__j))
3713 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003714 value_type __t(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003715 _RandomAccessIterator __k = __j;
3716 __j = __i;
3717 do
3718 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003719 *__j = _VSTD::move(*__k);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003720 __j = __k;
3721 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003722 *__j = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003723 }
3724 __j = __i;
3725 }
3726}
3727
3728template <class _Compare, class _RandomAccessIterator>
3729bool
3730__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3731{
3732 switch (__last - __first)
3733 {
3734 case 0:
3735 case 1:
3736 return true;
3737 case 2:
3738 if (__comp(*--__last, *__first))
3739 swap(*__first, *__last);
3740 return true;
3741 case 3:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003742 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003743 return true;
3744 case 4:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003745 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003746 return true;
3747 case 5:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003748 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003749 return true;
3750 }
3751 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3752 _RandomAccessIterator __j = __first+2;
3753 __sort3<_Compare>(__first, __first+1, __j, __comp);
3754 const unsigned __limit = 8;
3755 unsigned __count = 0;
3756 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3757 {
3758 if (__comp(*__i, *__j))
3759 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003760 value_type __t(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003761 _RandomAccessIterator __k = __j;
3762 __j = __i;
3763 do
3764 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003765 *__j = _VSTD::move(*__k);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003766 __j = __k;
3767 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003768 *__j = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003769 if (++__count == __limit)
3770 return ++__i == __last;
3771 }
3772 __j = __i;
3773 }
3774 return true;
3775}
3776
3777template <class _Compare, class _BirdirectionalIterator>
3778void
3779__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3780 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3781{
3782 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3783 if (__first1 != __last1)
3784 {
3785 __destruct_n __d(0);
3786 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3787 value_type* __last2 = __first2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003788 ::new(__last2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003789 __d.__incr((value_type*)0);
3790 for (++__last2; ++__first1 != __last1; ++__last2)
3791 {
3792 value_type* __j2 = __last2;
3793 value_type* __i2 = __j2;
3794 if (__comp(*__first1, *--__i2))
3795 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003796 ::new(__j2) value_type(_VSTD::move(*__i2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003797 __d.__incr((value_type*)0);
3798 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003799 *__j2 = _VSTD::move(*__i2);
3800 *__j2 = _VSTD::move(*__first1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003801 }
3802 else
3803 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003804 ::new(__j2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003805 __d.__incr((value_type*)0);
3806 }
3807 }
3808 __h.release();
3809 }
3810}
3811
3812template <class _Compare, class _RandomAccessIterator>
3813void
3814__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3815{
3816 // _Compare is known to be a reference type
3817 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3818 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003819 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3820 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821 while (true)
3822 {
3823 __restart:
3824 difference_type __len = __last - __first;
3825 switch (__len)
3826 {
3827 case 0:
3828 case 1:
3829 return;
3830 case 2:
3831 if (__comp(*--__last, *__first))
3832 swap(*__first, *__last);
3833 return;
3834 case 3:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003835 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003836 return;
3837 case 4:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003838 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003839 return;
3840 case 5:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003841 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003842 return;
3843 }
3844 if (__len <= __limit)
3845 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003846 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003847 return;
3848 }
3849 // __len > 5
3850 _RandomAccessIterator __m = __first;
3851 _RandomAccessIterator __lm1 = __last;
3852 --__lm1;
3853 unsigned __n_swaps;
3854 {
3855 difference_type __delta;
3856 if (__len >= 1000)
3857 {
3858 __delta = __len/2;
3859 __m += __delta;
3860 __delta /= 2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003861 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003862 }
3863 else
3864 {
3865 __delta = __len/2;
3866 __m += __delta;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003867 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003868 }
3869 }
3870 // *__m is median
3871 // partition [__first, __m) < *__m and *__m <= [__m, __last)
3872 // (this inhibits tossing elements equivalent to __m around unnecessarily)
3873 _RandomAccessIterator __i = __first;
3874 _RandomAccessIterator __j = __lm1;
3875 // j points beyond range to be tested, *__m is known to be <= *__lm1
3876 // The search going up is known to be guarded but the search coming down isn't.
3877 // Prime the downward search with a guard.
3878 if (!__comp(*__i, *__m)) // if *__first == *__m
3879 {
3880 // *__first == *__m, *__first doesn't go in first part
3881 // manually guard downward moving __j against __i
3882 while (true)
3883 {
3884 if (__i == --__j)
3885 {
3886 // *__first == *__m, *__m <= all other elements
3887 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
3888 ++__i; // __first + 1
3889 __j = __last;
3890 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
3891 {
3892 while (true)
3893 {
3894 if (__i == __j)
3895 return; // [__first, __last) all equivalent elements
3896 if (__comp(*__first, *__i))
3897 {
3898 swap(*__i, *__j);
3899 ++__n_swaps;
3900 ++__i;
3901 break;
3902 }
3903 ++__i;
3904 }
3905 }
3906 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
3907 if (__i == __j)
3908 return;
3909 while (true)
3910 {
3911 while (!__comp(*__first, *__i))
3912 ++__i;
3913 while (__comp(*__first, *--__j))
3914 ;
3915 if (__i >= __j)
3916 break;
3917 swap(*__i, *__j);
3918 ++__n_swaps;
3919 ++__i;
3920 }
3921 // [__first, __i) == *__first and *__first < [__i, __last)
3922 // The first part is sorted, sort the secod part
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003923 // _VSTD::__sort<_Compare>(__i, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003924 __first = __i;
3925 goto __restart;
3926 }
3927 if (__comp(*__j, *__m))
3928 {
3929 swap(*__i, *__j);
3930 ++__n_swaps;
3931 break; // found guard for downward moving __j, now use unguarded partition
3932 }
3933 }
3934 }
3935 // It is known that *__i < *__m
3936 ++__i;
3937 // j points beyond range to be tested, *__m is known to be <= *__lm1
3938 // if not yet partitioned...
3939 if (__i < __j)
3940 {
3941 // known that *(__i - 1) < *__m
3942 // known that __i <= __m
3943 while (true)
3944 {
3945 // __m still guards upward moving __i
3946 while (__comp(*__i, *__m))
3947 ++__i;
3948 // It is now known that a guard exists for downward moving __j
3949 while (!__comp(*--__j, *__m))
3950 ;
3951 if (__i > __j)
3952 break;
3953 swap(*__i, *__j);
3954 ++__n_swaps;
3955 // It is known that __m != __j
3956 // If __m just moved, follow it
3957 if (__m == __i)
3958 __m = __j;
3959 ++__i;
3960 }
3961 }
3962 // [__first, __i) < *__m and *__m <= [__i, __last)
3963 if (__i != __m && __comp(*__m, *__i))
3964 {
3965 swap(*__i, *__m);
3966 ++__n_swaps;
3967 }
3968 // [__first, __i) < *__i and *__i <= [__i+1, __last)
3969 // If we were given a perfect partition, see if insertion sort is quick...
3970 if (__n_swaps == 0)
3971 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003972 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
3973 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003974 {
3975 if (__fs)
3976 return;
3977 __last = __i;
3978 continue;
3979 }
3980 else
3981 {
3982 if (__fs)
3983 {
3984 __first = ++__i;
3985 continue;
3986 }
3987 }
3988 }
3989 // sort smaller range with recursive call and larger with tail recursion elimination
3990 if (__i - __first < __last - __i)
3991 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003992 _VSTD::__sort<_Compare>(__first, __i, __comp);
3993 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003994 __first = ++__i;
3995 }
3996 else
3997 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003998 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
3999 // _VSTD::__sort<_Compare>(__first, __i, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004000 __last = __i;
4001 }
4002 }
4003}
4004
4005// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
4006template <class _RandomAccessIterator, class _Compare>
4007inline _LIBCPP_INLINE_VISIBILITY
4008void
4009sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4010{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004011#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004012 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4013 __debug_less<_Compare> __c(__comp);
4014 __sort<_Comp_ref>(__first, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004015#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004016 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4017 __sort<_Comp_ref>(__first, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004018#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004019}
4020
4021template <class _RandomAccessIterator>
4022inline _LIBCPP_INLINE_VISIBILITY
4023void
4024sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4025{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004026 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004027}
4028
4029template <class _Tp>
4030inline _LIBCPP_INLINE_VISIBILITY
4031void
4032sort(_Tp** __first, _Tp** __last)
4033{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004034 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004035}
4036
4037template <class _Tp>
4038inline _LIBCPP_INLINE_VISIBILITY
4039void
4040sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4041{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004042 _VSTD::sort(__first.base(), __last.base());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004043}
4044
Howard Hinnant27e0e772011-09-14 18:33:51 +00004045template <class _Tp, class _Compare>
4046inline _LIBCPP_INLINE_VISIBILITY
4047void
4048sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4049{
4050 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4051 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4052}
4053
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004054_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4055_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4056_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4057_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4058_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4059_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4060_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4061_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4062_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4063_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4064_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4065_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>&))
4066_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4067_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4068_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 +00004069
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004070_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4071_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4072_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4073_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4074_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4075_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4076_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4077_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4078_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4079_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4080_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4081_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>&))
4082_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4083_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4084_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 +00004085
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004086_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 +00004087
4088// lower_bound
4089
4090template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004091_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004092__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093{
4094 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004095 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004096 while (__len != 0)
4097 {
Eric Fiselier9ed59852018-10-29 19:25:02 +00004098 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004099 _ForwardIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004100 _VSTD::advance(__m, __l2);
Howard Hinnantbf074022011-10-22 20:59:45 +00004101 if (__comp(*__m, __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004102 {
4103 __first = ++__m;
4104 __len -= __l2 + 1;
4105 }
4106 else
4107 __len = __l2;
4108 }
4109 return __first;
4110}
4111
4112template <class _ForwardIterator, class _Tp, class _Compare>
Marshall Clowe00916c2018-01-16 02:34:41 +00004113inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004114_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004115lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004116{
Howard Hinnantc51e1022010-05-11 19:42:16 +00004117 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantbf074022011-10-22 20:59:45 +00004118 return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004119}
4120
4121template <class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004122inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004123_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004124lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004125{
Howard Hinnantbf074022011-10-22 20:59:45 +00004126 return _VSTD::lower_bound(__first, __last, __value_,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004127 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4128}
4129
4130// upper_bound
4131
4132template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004133_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004134__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004135{
4136 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004137 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004138 while (__len != 0)
4139 {
Eric Fiselier9ed59852018-10-29 19:25:02 +00004140 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004141 _ForwardIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004142 _VSTD::advance(__m, __l2);
Howard Hinnantbf074022011-10-22 20:59:45 +00004143 if (__comp(__value_, *__m))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004144 __len = __l2;
4145 else
4146 {
4147 __first = ++__m;
4148 __len -= __l2 + 1;
4149 }
4150 }
4151 return __first;
4152}
4153
4154template <class _ForwardIterator, class _Tp, class _Compare>
Marshall Clowe00916c2018-01-16 02:34:41 +00004155inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004156_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004157upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004158{
Howard Hinnantc51e1022010-05-11 19:42:16 +00004159 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantbf074022011-10-22 20:59:45 +00004160 return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004161}
4162
4163template <class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004164inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004165_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004166upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004167{
Howard Hinnantbf074022011-10-22 20:59:45 +00004168 return _VSTD::upper_bound(__first, __last, __value_,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4170}
4171
4172// equal_range
4173
4174template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004175_LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_ForwardIterator, _ForwardIterator>
Howard Hinnantbf074022011-10-22 20:59:45 +00004176__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004177{
4178 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004179 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004180 while (__len != 0)
4181 {
Eric Fiselier9ed59852018-10-29 19:25:02 +00004182 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004183 _ForwardIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004184 _VSTD::advance(__m, __l2);
Howard Hinnantbf074022011-10-22 20:59:45 +00004185 if (__comp(*__m, __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004186 {
4187 __first = ++__m;
4188 __len -= __l2 + 1;
4189 }
Howard Hinnantbf074022011-10-22 20:59:45 +00004190 else if (__comp(__value_, *__m))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004191 {
4192 __last = __m;
4193 __len = __l2;
4194 }
4195 else
4196 {
4197 _ForwardIterator __mp1 = __m;
4198 return pair<_ForwardIterator, _ForwardIterator>
4199 (
Howard Hinnantbf074022011-10-22 20:59:45 +00004200 __lower_bound<_Compare>(__first, __m, __value_, __comp),
4201 __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004202 );
4203 }
4204 }
4205 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4206}
4207
4208template <class _ForwardIterator, class _Tp, class _Compare>
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_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004212{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004213#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004214 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4215 __debug_less<_Compare> __c(__comp);
Howard Hinnantbf074022011-10-22 20:59:45 +00004216 return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004217#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004218 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantbf074022011-10-22 20:59:45 +00004219 return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004220#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004221}
4222
4223template <class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004224inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004225pair<_ForwardIterator, _ForwardIterator>
Howard Hinnantbf074022011-10-22 20:59:45 +00004226equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004227{
Howard Hinnantbf074022011-10-22 20:59:45 +00004228 return _VSTD::equal_range(__first, __last, __value_,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004229 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4230}
4231
4232// binary_search
4233
4234template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004235inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004236bool
Howard Hinnantbf074022011-10-22 20:59:45 +00004237__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004238{
Howard Hinnantbf074022011-10-22 20:59:45 +00004239 __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4240 return __first != __last && !__comp(__value_, *__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004241}
4242
4243template <class _ForwardIterator, class _Tp, class _Compare>
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_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004248#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004249 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4250 __debug_less<_Compare> __c(__comp);
Howard Hinnantbf074022011-10-22 20:59:45 +00004251 return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004252#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004253 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantbf074022011-10-22 20:59:45 +00004254 return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004255#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004256}
4257
4258template <class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004259inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004260bool
Howard Hinnantbf074022011-10-22 20:59:45 +00004261binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004262{
Howard Hinnantbf074022011-10-22 20:59:45 +00004263 return _VSTD::binary_search(__first, __last, __value_,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004264 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4265}
4266
4267// merge
4268
4269template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4270_OutputIterator
4271__merge(_InputIterator1 __first1, _InputIterator1 __last1,
4272 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4273{
4274 for (; __first1 != __last1; ++__result)
4275 {
4276 if (__first2 == __last2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004277 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278 if (__comp(*__first2, *__first1))
4279 {
4280 *__result = *__first2;
4281 ++__first2;
4282 }
4283 else
4284 {
4285 *__result = *__first1;
4286 ++__first1;
4287 }
4288 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004289 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004290}
4291
4292template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4293inline _LIBCPP_INLINE_VISIBILITY
4294_OutputIterator
4295merge(_InputIterator1 __first1, _InputIterator1 __last1,
4296 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4297{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004298#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004299 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4300 __debug_less<_Compare> __c(__comp);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004301 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004302#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004303 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004304 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004305#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004306}
4307
4308template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4309inline _LIBCPP_INLINE_VISIBILITY
4310_OutputIterator
4311merge(_InputIterator1 __first1, _InputIterator1 __last1,
4312 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4313{
4314 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4315 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4316 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4317}
4318
4319// inplace_merge
4320
Marshall Clow1bc51102015-07-29 16:25:45 +00004321template <class _Compare, class _InputIterator1, class _InputIterator2,
4322 class _OutputIterator>
4323void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4324 _InputIterator2 __first2, _InputIterator2 __last2,
4325 _OutputIterator __result, _Compare __comp)
4326{
4327 for (; __first1 != __last1; ++__result)
4328 {
4329 if (__first2 == __last2)
4330 {
4331 _VSTD::move(__first1, __last1, __result);
4332 return;
4333 }
4334
4335 if (__comp(*__first2, *__first1))
4336 {
4337 *__result = _VSTD::move(*__first2);
4338 ++__first2;
4339 }
4340 else
4341 {
4342 *__result = _VSTD::move(*__first1);
4343 ++__first1;
4344 }
4345 }
4346 // __first2 through __last2 are already in the right spot.
4347}
4348
Howard Hinnantc51e1022010-05-11 19:42:16 +00004349template <class _Compare, class _BidirectionalIterator>
4350void
4351__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4352 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4353 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4354 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4355{
4356 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004357 __destruct_n __d(0);
4358 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4359 if (__len1 <= __len2)
4360 {
4361 value_type* __p = __buff;
Eric Fiseliera09a3b42014-10-27 19:28:20 +00004362 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004363 ::new(__p) value_type(_VSTD::move(*__i));
Marshall Clow1bc51102015-07-29 16:25:45 +00004364 __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004365 }
4366 else
4367 {
4368 value_type* __p = __buff;
Eric Fiseliera09a3b42014-10-27 19:28:20 +00004369 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004370 ::new(__p) value_type(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004371 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4372 typedef reverse_iterator<value_type*> _Rv;
Aditya Kumar3a0179a2016-08-25 11:52:38 +00004373 __half_inplace_merge(_Rv(__p), _Rv(__buff),
Marshall Clow1bc51102015-07-29 16:25:45 +00004374 _RBi(__middle), _RBi(__first),
Marshall Clow738d1042017-08-28 23:16:13 +00004375 _RBi(__last), __invert<_Compare>(__comp));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004376 }
4377}
4378
4379template <class _Compare, class _BidirectionalIterator>
4380void
4381__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4382 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4383 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4384 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4385{
Howard Hinnantc51e1022010-05-11 19:42:16 +00004386 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4387 while (true)
4388 {
4389 // if __middle == __last, we're done
4390 if (__len2 == 0)
4391 return;
Marshall Clow8eff8232015-02-02 16:44:11 +00004392 if (__len1 <= __buff_size || __len2 <= __buff_size)
4393 return __buffered_inplace_merge<_Compare>
4394 (__first, __middle, __last, __comp, __len1, __len2, __buff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004395 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
Eric Fiseliera09a3b42014-10-27 19:28:20 +00004396 for (; true; ++__first, (void) --__len1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004397 {
4398 if (__len1 == 0)
4399 return;
4400 if (__comp(*__middle, *__first))
4401 break;
4402 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004403 // __first < __middle < __last
4404 // *__first > *__middle
4405 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4406 // all elements in:
4407 // [__first, __m1) <= [__middle, __m2)
4408 // [__middle, __m2) < [__m1, __middle)
4409 // [__m1, __middle) <= [__m2, __last)
4410 // and __m1 or __m2 is in the middle of its range
4411 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4412 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4413 difference_type __len11; // distance(__first, __m1)
4414 difference_type __len21; // distance(__middle, __m2)
4415 // binary search smaller range
4416 if (__len1 < __len2)
4417 { // __len >= 1, __len2 >= 2
4418 __len21 = __len2 / 2;
4419 __m2 = __middle;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004420 _VSTD::advance(__m2, __len21);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004421 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004422 __len11 = _VSTD::distance(__first, __m1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004423 }
4424 else
4425 {
4426 if (__len1 == 1)
4427 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4428 // It is known *__first > *__middle
4429 swap(*__first, *__middle);
4430 return;
4431 }
4432 // __len1 >= 2, __len2 >= 1
4433 __len11 = __len1 / 2;
4434 __m1 = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004435 _VSTD::advance(__m1, __len11);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004436 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004437 __len21 = _VSTD::distance(__middle, __m2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004438 }
4439 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4440 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4441 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4442 // swap middle two partitions
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004443 __middle = _VSTD::rotate(__m1, __middle, __m2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004444 // __len12 and __len21 now have swapped meanings
4445 // merge smaller range with recurisve call and larger with tail recursion elimination
4446 if (__len11 + __len21 < __len12 + __len22)
4447 {
4448 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4449// __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4450 __first = __middle;
4451 __middle = __m2;
4452 __len1 = __len12;
4453 __len2 = __len22;
4454 }
4455 else
4456 {
4457 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4458// __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4459 __last = __middle;
4460 __middle = __m1;
4461 __len1 = __len11;
4462 __len2 = __len21;
4463 }
4464 }
4465}
4466
Howard Hinnantc51e1022010-05-11 19:42:16 +00004467template <class _BidirectionalIterator, class _Compare>
4468inline _LIBCPP_INLINE_VISIBILITY
4469void
4470inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4471 _Compare __comp)
4472{
4473 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4474 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004475 difference_type __len1 = _VSTD::distance(__first, __middle);
4476 difference_type __len2 = _VSTD::distance(__middle, __last);
4477 difference_type __buf_size = _VSTD::min(__len1, __len2);
Marshall Clow488f19f2015-02-02 17:35:53 +00004478 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4479 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
4480
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004481#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004482 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4483 __debug_less<_Compare> __c(__comp);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004484 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004485 __buf.first, __buf.second);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004486#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004487 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004488 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004489 __buf.first, __buf.second);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004490#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004491}
4492
4493template <class _BidirectionalIterator>
4494inline _LIBCPP_INLINE_VISIBILITY
4495void
4496inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4497{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004498 _VSTD::inplace_merge(__first, __middle, __last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004499 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4500}
4501
4502// stable_sort
4503
4504template <class _Compare, class _InputIterator1, class _InputIterator2>
4505void
4506__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4507 _InputIterator2 __first2, _InputIterator2 __last2,
4508 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4509{
4510 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4511 __destruct_n __d(0);
4512 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4513 for (; true; ++__result)
4514 {
4515 if (__first1 == __last1)
4516 {
4517 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004518 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004519 __h.release();
4520 return;
4521 }
4522 if (__first2 == __last2)
4523 {
4524 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004525 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004526 __h.release();
4527 return;
4528 }
4529 if (__comp(*__first2, *__first1))
4530 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004531 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004532 __d.__incr((value_type*)0);
4533 ++__first2;
4534 }
4535 else
4536 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004537 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004538 __d.__incr((value_type*)0);
4539 ++__first1;
4540 }
4541 }
4542}
4543
4544template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4545void
4546__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4547 _InputIterator2 __first2, _InputIterator2 __last2,
4548 _OutputIterator __result, _Compare __comp)
4549{
4550 for (; __first1 != __last1; ++__result)
4551 {
4552 if (__first2 == __last2)
4553 {
4554 for (; __first1 != __last1; ++__first1, ++__result)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004555 *__result = _VSTD::move(*__first1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004556 return;
4557 }
4558 if (__comp(*__first2, *__first1))
4559 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004560 *__result = _VSTD::move(*__first2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004561 ++__first2;
4562 }
4563 else
4564 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004565 *__result = _VSTD::move(*__first1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004566 ++__first1;
4567 }
4568 }
4569 for (; __first2 != __last2; ++__first2, ++__result)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004570 *__result = _VSTD::move(*__first2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004571}
4572
4573template <class _Compare, class _RandomAccessIterator>
4574void
4575__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4576 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4577 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4578
4579template <class _Compare, class _RandomAccessIterator>
4580void
4581__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4582 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4583 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4584{
4585 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4586 switch (__len)
4587 {
4588 case 0:
4589 return;
4590 case 1:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004591 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004592 return;
4593 case 2:
Marshall Clow32043ac2018-02-06 18:58:05 +00004594 __destruct_n __d(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004595 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
Marshall Clow32043ac2018-02-06 18:58:05 +00004596 if (__comp(*--__last1, *__first1))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004597 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004598 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004599 __d.__incr((value_type*)0);
4600 ++__first2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004601 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004602 }
4603 else
4604 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004605 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004606 __d.__incr((value_type*)0);
4607 ++__first2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004608 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004609 }
4610 __h2.release();
4611 return;
4612 }
4613 if (__len <= 8)
4614 {
4615 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4616 return;
4617 }
4618 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4619 _RandomAccessIterator __m = __first1 + __l2;
4620 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4621 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4622 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4623}
4624
4625template <class _Tp>
4626struct __stable_sort_switch
4627{
Howard Hinnanta9a897e2010-11-19 22:17:28 +00004628 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004629};
4630
4631template <class _Compare, class _RandomAccessIterator>
4632void
4633__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4634 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4635 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4636{
4637 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4638 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4639 switch (__len)
4640 {
4641 case 0:
4642 case 1:
4643 return;
4644 case 2:
4645 if (__comp(*--__last, *__first))
4646 swap(*__first, *__last);
4647 return;
4648 }
4649 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4650 {
4651 __insertion_sort<_Compare>(__first, __last, __comp);
4652 return;
4653 }
4654 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4655 _RandomAccessIterator __m = __first + __l2;
4656 if (__len <= __buff_size)
4657 {
4658 __destruct_n __d(0);
4659 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4660 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4661 __d.__set(__l2, (value_type*)0);
4662 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4663 __d.__set(__len, (value_type*)0);
4664 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4665// __merge<_Compare>(move_iterator<value_type*>(__buff),
4666// move_iterator<value_type*>(__buff + __l2),
4667// move_iterator<_RandomAccessIterator>(__buff + __l2),
4668// move_iterator<_RandomAccessIterator>(__buff + __len),
4669// __first, __comp);
4670 return;
4671 }
4672 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4673 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4674 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4675}
4676
4677template <class _RandomAccessIterator, class _Compare>
4678inline _LIBCPP_INLINE_VISIBILITY
4679void
4680stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4681{
4682 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4683 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4684 difference_type __len = __last - __first;
4685 pair<value_type*, ptrdiff_t> __buf(0, 0);
4686 unique_ptr<value_type, __return_temporary_buffer> __h;
4687 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4688 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004689 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004690 __h.reset(__buf.first);
4691 }
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004692#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004693 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4694 __debug_less<_Compare> __c(__comp);
4695 __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004696#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004697 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4698 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004699#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004700}
4701
4702template <class _RandomAccessIterator>
4703inline _LIBCPP_INLINE_VISIBILITY
4704void
4705stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4706{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004707 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004708}
4709
4710// is_heap_until
4711
4712template <class _RandomAccessIterator, class _Compare>
Marshall Clow96d050a2018-01-15 16:16:32 +00004713_LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00004714is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4715{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004716 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004717 difference_type __len = __last - __first;
4718 difference_type __p = 0;
4719 difference_type __c = 1;
4720 _RandomAccessIterator __pp = __first;
4721 while (__c < __len)
4722 {
4723 _RandomAccessIterator __cp = __first + __c;
4724 if (__comp(*__pp, *__cp))
4725 return __cp;
4726 ++__c;
4727 ++__cp;
4728 if (__c == __len)
4729 return __last;
4730 if (__comp(*__pp, *__cp))
4731 return __cp;
4732 ++__p;
4733 ++__pp;
4734 __c = 2 * __p + 1;
4735 }
4736 return __last;
4737}
4738
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004739template<class _RandomAccessIterator>
Marshall Clow96d050a2018-01-15 16:16:32 +00004740inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004741_RandomAccessIterator
4742is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4743{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004744 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004745}
4746
4747// is_heap
4748
4749template <class _RandomAccessIterator, class _Compare>
Marshall Clow96d050a2018-01-15 16:16:32 +00004750inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004751bool
4752is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4753{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004754 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004755}
4756
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004757template<class _RandomAccessIterator>
Marshall Clow96d050a2018-01-15 16:16:32 +00004758inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004759bool
4760is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4761{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004762 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004763}
4764
4765// push_heap
4766
4767template <class _Compare, class _RandomAccessIterator>
4768void
David Majnemer4468d562014-07-22 06:07:09 +00004769__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4770 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004771{
Howard Hinnantc51e1022010-05-11 19:42:16 +00004772 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4773 if (__len > 1)
4774 {
4775 __len = (__len - 2) / 2;
4776 _RandomAccessIterator __ptr = __first + __len;
4777 if (__comp(*__ptr, *--__last))
4778 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004779 value_type __t(_VSTD::move(*__last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004780 do
4781 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004782 *__last = _VSTD::move(*__ptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004783 __last = __ptr;
4784 if (__len == 0)
4785 break;
4786 __len = (__len - 1) / 2;
4787 __ptr = __first + __len;
4788 } while (__comp(*__ptr, __t));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004789 *__last = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004790 }
4791 }
4792}
4793
4794template <class _RandomAccessIterator, class _Compare>
4795inline _LIBCPP_INLINE_VISIBILITY
4796void
4797push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4798{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004799#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004800 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4801 __debug_less<_Compare> __c(__comp);
David Majnemer4468d562014-07-22 06:07:09 +00004802 __sift_up<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004803#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004804 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
David Majnemer4468d562014-07-22 06:07:09 +00004805 __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004806#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004807}
4808
4809template <class _RandomAccessIterator>
4810inline _LIBCPP_INLINE_VISIBILITY
4811void
4812push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4813{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004814 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004815}
4816
4817// pop_heap
4818
4819template <class _Compare, class _RandomAccessIterator>
David Majnemer4468d562014-07-22 06:07:09 +00004820void
Eric Fiselier6003c772016-12-23 23:37:52 +00004821__sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
4822 _Compare __comp,
David Majnemer4468d562014-07-22 06:07:09 +00004823 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4824 _RandomAccessIterator __start)
4825{
4826 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4827 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4828 // left-child of __start is at 2 * __start + 1
4829 // right-child of __start is at 2 * __start + 2
4830 difference_type __child = __start - __first;
4831
4832 if (__len < 2 || (__len - 2) / 2 < __child)
4833 return;
4834
4835 __child = 2 * __child + 1;
4836 _RandomAccessIterator __child_i = __first + __child;
4837
4838 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4839 // right-child exists and is greater than left-child
4840 ++__child_i;
4841 ++__child;
4842 }
4843
4844 // check if we are in heap-order
4845 if (__comp(*__child_i, *__start))
4846 // we are, __start is larger than it's largest child
4847 return;
4848
4849 value_type __top(_VSTD::move(*__start));
4850 do
4851 {
4852 // we are not in heap-order, swap the parent with it's largest child
4853 *__start = _VSTD::move(*__child_i);
4854 __start = __child_i;
4855
4856 if ((__len - 2) / 2 < __child)
4857 break;
4858
4859 // recompute the child based off of the updated parent
4860 __child = 2 * __child + 1;
4861 __child_i = __first + __child;
4862
4863 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4864 // right-child exists and is greater than left-child
4865 ++__child_i;
4866 ++__child;
4867 }
4868
4869 // check if we are in heap-order
4870 } while (!__comp(*__child_i, __top));
4871 *__start = _VSTD::move(__top);
4872}
4873
4874template <class _Compare, class _RandomAccessIterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004875inline _LIBCPP_INLINE_VISIBILITY
4876void
4877__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4878 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4879{
4880 if (__len > 1)
4881 {
4882 swap(*__first, *--__last);
David Majnemer4468d562014-07-22 06:07:09 +00004883 __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004884 }
4885}
4886
4887template <class _RandomAccessIterator, class _Compare>
4888inline _LIBCPP_INLINE_VISIBILITY
4889void
4890pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4891{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004892#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004893 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4894 __debug_less<_Compare> __c(__comp);
4895 __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004896#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004897 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4898 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004899#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004900}
4901
4902template <class _RandomAccessIterator>
4903inline _LIBCPP_INLINE_VISIBILITY
4904void
4905pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4906{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004907 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004908}
4909
4910// make_heap
4911
4912template <class _Compare, class _RandomAccessIterator>
4913void
4914__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4915{
4916 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4917 difference_type __n = __last - __first;
4918 if (__n > 1)
4919 {
David Majnemer4468d562014-07-22 06:07:09 +00004920 // start from the first parent, there is no need to consider children
4921 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
4922 {
4923 __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
4924 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004925 }
4926}
4927
4928template <class _RandomAccessIterator, class _Compare>
4929inline _LIBCPP_INLINE_VISIBILITY
4930void
4931make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4932{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004933#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004934 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4935 __debug_less<_Compare> __c(__comp);
4936 __make_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004937#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004938 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4939 __make_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004940#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004941}
4942
4943template <class _RandomAccessIterator>
4944inline _LIBCPP_INLINE_VISIBILITY
4945void
4946make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4947{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004948 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004949}
4950
4951// sort_heap
4952
4953template <class _Compare, class _RandomAccessIterator>
4954void
4955__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4956{
4957 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4958 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
4959 __pop_heap<_Compare>(__first, __last, __comp, __n);
4960}
4961
4962template <class _RandomAccessIterator, class _Compare>
4963inline _LIBCPP_INLINE_VISIBILITY
4964void
4965sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4966{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004967#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004968 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4969 __debug_less<_Compare> __c(__comp);
4970 __sort_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004971#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004972 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4973 __sort_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004974#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004975}
4976
4977template <class _RandomAccessIterator>
4978inline _LIBCPP_INLINE_VISIBILITY
4979void
4980sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4981{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004982 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004983}
4984
4985// partial_sort
4986
4987template <class _Compare, class _RandomAccessIterator>
4988void
4989__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
4990 _Compare __comp)
4991{
4992 __make_heap<_Compare>(__first, __middle, __comp);
4993 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
4994 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
4995 {
4996 if (__comp(*__i, *__first))
4997 {
4998 swap(*__i, *__first);
David Majnemer4468d562014-07-22 06:07:09 +00004999 __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005000 }
5001 }
5002 __sort_heap<_Compare>(__first, __middle, __comp);
5003}
5004
5005template <class _RandomAccessIterator, class _Compare>
5006inline _LIBCPP_INLINE_VISIBILITY
5007void
5008partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5009 _Compare __comp)
5010{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005011#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005012 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5013 __debug_less<_Compare> __c(__comp);
5014 __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005015#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005016 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5017 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005018#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005019}
5020
5021template <class _RandomAccessIterator>
5022inline _LIBCPP_INLINE_VISIBILITY
5023void
5024partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5025{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005026 _VSTD::partial_sort(__first, __middle, __last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005027 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5028}
5029
5030// partial_sort_copy
5031
5032template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5033_RandomAccessIterator
5034__partial_sort_copy(_InputIterator __first, _InputIterator __last,
5035 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5036{
5037 _RandomAccessIterator __r = __result_first;
5038 if (__r != __result_last)
5039 {
Eric Fiseliera09a3b42014-10-27 19:28:20 +00005040 for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005041 *__r = *__first;
5042 __make_heap<_Compare>(__result_first, __r, __comp);
David Majnemer4468d562014-07-22 06:07:09 +00005043 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005044 for (; __first != __last; ++__first)
5045 if (__comp(*__first, *__result_first))
5046 {
5047 *__result_first = *__first;
David Majnemer4468d562014-07-22 06:07:09 +00005048 __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005049 }
5050 __sort_heap<_Compare>(__result_first, __r, __comp);
5051 }
5052 return __r;
5053}
5054
5055template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5056inline _LIBCPP_INLINE_VISIBILITY
5057_RandomAccessIterator
5058partial_sort_copy(_InputIterator __first, _InputIterator __last,
5059 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5060{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005061#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005062 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5063 __debug_less<_Compare> __c(__comp);
5064 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005065#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005066 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5067 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005068#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005069}
5070
5071template <class _InputIterator, class _RandomAccessIterator>
5072inline _LIBCPP_INLINE_VISIBILITY
5073_RandomAccessIterator
5074partial_sort_copy(_InputIterator __first, _InputIterator __last,
5075 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5076{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005077 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005078 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5079}
5080
5081// nth_element
5082
5083template <class _Compare, class _RandomAccessIterator>
5084void
5085__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5086{
5087 // _Compare is known to be a reference type
5088 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5089 const difference_type __limit = 7;
5090 while (true)
5091 {
5092 __restart:
Howard Hinnant2fa038c2011-12-29 17:45:35 +00005093 if (__nth == __last)
5094 return;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005095 difference_type __len = __last - __first;
5096 switch (__len)
5097 {
5098 case 0:
5099 case 1:
5100 return;
5101 case 2:
5102 if (__comp(*--__last, *__first))
5103 swap(*__first, *__last);
5104 return;
5105 case 3:
5106 {
5107 _RandomAccessIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005108 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005109 return;
5110 }
5111 }
5112 if (__len <= __limit)
5113 {
5114 __selection_sort<_Compare>(__first, __last, __comp);
5115 return;
5116 }
5117 // __len > __limit >= 3
5118 _RandomAccessIterator __m = __first + __len/2;
5119 _RandomAccessIterator __lm1 = __last;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005120 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005121 // *__m is median
5122 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5123 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5124 _RandomAccessIterator __i = __first;
5125 _RandomAccessIterator __j = __lm1;
5126 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5127 // The search going up is known to be guarded but the search coming down isn't.
5128 // Prime the downward search with a guard.
5129 if (!__comp(*__i, *__m)) // if *__first == *__m
5130 {
5131 // *__first == *__m, *__first doesn't go in first part
5132 // manually guard downward moving __j against __i
5133 while (true)
5134 {
5135 if (__i == --__j)
5136 {
5137 // *__first == *__m, *__m <= all other elements
5138 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5139 ++__i; // __first + 1
5140 __j = __last;
5141 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5142 {
5143 while (true)
5144 {
5145 if (__i == __j)
5146 return; // [__first, __last) all equivalent elements
5147 if (__comp(*__first, *__i))
5148 {
5149 swap(*__i, *__j);
5150 ++__n_swaps;
5151 ++__i;
5152 break;
5153 }
5154 ++__i;
5155 }
5156 }
5157 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5158 if (__i == __j)
5159 return;
5160 while (true)
5161 {
5162 while (!__comp(*__first, *__i))
5163 ++__i;
5164 while (__comp(*__first, *--__j))
5165 ;
5166 if (__i >= __j)
5167 break;
5168 swap(*__i, *__j);
5169 ++__n_swaps;
5170 ++__i;
5171 }
5172 // [__first, __i) == *__first and *__first < [__i, __last)
5173 // The first part is sorted,
5174 if (__nth < __i)
5175 return;
5176 // __nth_element the secod part
5177 // __nth_element<_Compare>(__i, __nth, __last, __comp);
5178 __first = __i;
5179 goto __restart;
5180 }
5181 if (__comp(*__j, *__m))
5182 {
5183 swap(*__i, *__j);
5184 ++__n_swaps;
5185 break; // found guard for downward moving __j, now use unguarded partition
5186 }
5187 }
5188 }
5189 ++__i;
5190 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5191 // if not yet partitioned...
5192 if (__i < __j)
5193 {
5194 // known that *(__i - 1) < *__m
5195 while (true)
5196 {
5197 // __m still guards upward moving __i
5198 while (__comp(*__i, *__m))
5199 ++__i;
5200 // It is now known that a guard exists for downward moving __j
5201 while (!__comp(*--__j, *__m))
5202 ;
5203 if (__i >= __j)
5204 break;
5205 swap(*__i, *__j);
5206 ++__n_swaps;
5207 // It is known that __m != __j
5208 // If __m just moved, follow it
5209 if (__m == __i)
5210 __m = __j;
5211 ++__i;
5212 }
5213 }
5214 // [__first, __i) < *__m and *__m <= [__i, __last)
5215 if (__i != __m && __comp(*__m, *__i))
5216 {
5217 swap(*__i, *__m);
5218 ++__n_swaps;
5219 }
5220 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5221 if (__nth == __i)
5222 return;
5223 if (__n_swaps == 0)
5224 {
5225 // We were given a perfectly partitioned sequence. Coincidence?
5226 if (__nth < __i)
5227 {
5228 // Check for [__first, __i) already sorted
5229 __j = __m = __first;
5230 while (++__j != __i)
5231 {
5232 if (__comp(*__j, *__m))
5233 // not yet sorted, so sort
5234 goto not_sorted;
5235 __m = __j;
5236 }
5237 // [__first, __i) sorted
5238 return;
5239 }
5240 else
5241 {
5242 // Check for [__i, __last) already sorted
5243 __j = __m = __i;
5244 while (++__j != __last)
5245 {
5246 if (__comp(*__j, *__m))
5247 // not yet sorted, so sort
5248 goto not_sorted;
5249 __m = __j;
5250 }
5251 // [__i, __last) sorted
5252 return;
5253 }
5254 }
5255not_sorted:
5256 // __nth_element on range containing __nth
5257 if (__nth < __i)
5258 {
5259 // __nth_element<_Compare>(__first, __nth, __i, __comp);
5260 __last = __i;
5261 }
5262 else
5263 {
5264 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5265 __first = ++__i;
5266 }
5267 }
5268}
5269
5270template <class _RandomAccessIterator, class _Compare>
5271inline _LIBCPP_INLINE_VISIBILITY
5272void
5273nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5274{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005275#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005276 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5277 __debug_less<_Compare> __c(__comp);
5278 __nth_element<_Comp_ref>(__first, __nth, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005279#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005280 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5281 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005282#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005283}
5284
5285template <class _RandomAccessIterator>
5286inline _LIBCPP_INLINE_VISIBILITY
5287void
5288nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5289{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005290 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00005291}
5292
5293// includes
5294
5295template <class _Compare, class _InputIterator1, class _InputIterator2>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005296_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00005297__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5298 _Compare __comp)
5299{
5300 for (; __first2 != __last2; ++__first1)
5301 {
5302 if (__first1 == __last1 || __comp(*__first2, *__first1))
5303 return false;
5304 if (!__comp(*__first1, *__first2))
5305 ++__first2;
5306 }
5307 return true;
5308}
5309
5310template <class _InputIterator1, class _InputIterator2, class _Compare>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005311inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005312bool
5313includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5314 _Compare __comp)
5315{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005316#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005317 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5318 __debug_less<_Compare> __c(__comp);
5319 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005320#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005321 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5322 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005323#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005324}
5325
5326template <class _InputIterator1, class _InputIterator2>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005327inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005328bool
5329includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5330{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005331 return _VSTD::includes(__first1, __last1, __first2, __last2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005332 __less<typename iterator_traits<_InputIterator1>::value_type,
5333 typename iterator_traits<_InputIterator2>::value_type>());
5334}
5335
5336// set_union
5337
5338template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5339_OutputIterator
5340__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5341 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5342{
5343 for (; __first1 != __last1; ++__result)
5344 {
5345 if (__first2 == __last2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005346 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005347 if (__comp(*__first2, *__first1))
5348 {
5349 *__result = *__first2;
5350 ++__first2;
5351 }
5352 else
5353 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00005354 if (!__comp(*__first1, *__first2))
5355 ++__first2;
Marshall Clowb4687412017-10-30 15:50:00 +00005356 *__result = *__first1;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005357 ++__first1;
5358 }
5359 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005360 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005361}
5362
5363template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5364inline _LIBCPP_INLINE_VISIBILITY
5365_OutputIterator
5366set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5367 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5368{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005369#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005370 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5371 __debug_less<_Compare> __c(__comp);
5372 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005373#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005374 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5375 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005376#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005377}
5378
5379template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5380inline _LIBCPP_INLINE_VISIBILITY
5381_OutputIterator
5382set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5383 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5384{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005385 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005386 __less<typename iterator_traits<_InputIterator1>::value_type,
5387 typename iterator_traits<_InputIterator2>::value_type>());
5388}
5389
5390// set_intersection
5391
5392template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005393_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00005394__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5395 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5396{
5397 while (__first1 != __last1 && __first2 != __last2)
5398 {
5399 if (__comp(*__first1, *__first2))
5400 ++__first1;
5401 else
5402 {
5403 if (!__comp(*__first2, *__first1))
5404 {
5405 *__result = *__first1;
5406 ++__result;
5407 ++__first1;
5408 }
5409 ++__first2;
5410 }
5411 }
5412 return __result;
5413}
5414
5415template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005416inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005417_OutputIterator
5418set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5419 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5420{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005421#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005422 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5423 __debug_less<_Compare> __c(__comp);
5424 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005425#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005426 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5427 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005428#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005429}
5430
5431template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005432inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005433_OutputIterator
5434set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5435 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5436{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005437 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005438 __less<typename iterator_traits<_InputIterator1>::value_type,
5439 typename iterator_traits<_InputIterator2>::value_type>());
5440}
5441
5442// set_difference
5443
5444template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5445_OutputIterator
5446__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5447 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5448{
5449 while (__first1 != __last1)
5450 {
5451 if (__first2 == __last2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005452 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005453 if (__comp(*__first1, *__first2))
5454 {
5455 *__result = *__first1;
5456 ++__result;
5457 ++__first1;
5458 }
5459 else
5460 {
5461 if (!__comp(*__first2, *__first1))
5462 ++__first1;
5463 ++__first2;
5464 }
5465 }
5466 return __result;
5467}
5468
5469template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5470inline _LIBCPP_INLINE_VISIBILITY
5471_OutputIterator
5472set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5473 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5474{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005475#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005476 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5477 __debug_less<_Compare> __c(__comp);
5478 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005479#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005480 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5481 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005482#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005483}
5484
5485template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5486inline _LIBCPP_INLINE_VISIBILITY
5487_OutputIterator
5488set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5489 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5490{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005491 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005492 __less<typename iterator_traits<_InputIterator1>::value_type,
5493 typename iterator_traits<_InputIterator2>::value_type>());
5494}
5495
5496// set_symmetric_difference
5497
5498template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5499_OutputIterator
5500__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5501 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5502{
5503 while (__first1 != __last1)
5504 {
5505 if (__first2 == __last2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005506 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005507 if (__comp(*__first1, *__first2))
5508 {
5509 *__result = *__first1;
5510 ++__result;
5511 ++__first1;
5512 }
5513 else
5514 {
5515 if (__comp(*__first2, *__first1))
5516 {
5517 *__result = *__first2;
5518 ++__result;
5519 }
5520 else
5521 ++__first1;
5522 ++__first2;
5523 }
5524 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005525 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005526}
5527
5528template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5529inline _LIBCPP_INLINE_VISIBILITY
5530_OutputIterator
5531set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5532 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5533{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005534#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005535 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5536 __debug_less<_Compare> __c(__comp);
5537 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005538#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005539 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5540 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005541#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005542}
5543
5544template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5545inline _LIBCPP_INLINE_VISIBILITY
5546_OutputIterator
5547set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5548 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5549{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005550 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005551 __less<typename iterator_traits<_InputIterator1>::value_type,
5552 typename iterator_traits<_InputIterator2>::value_type>());
5553}
5554
5555// lexicographical_compare
5556
5557template <class _Compare, class _InputIterator1, class _InputIterator2>
Marshall Clow5492c8a2018-01-22 20:44:33 +00005558_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00005559__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5560 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5561{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00005562 for (; __first2 != __last2; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005563 {
5564 if (__first1 == __last1 || __comp(*__first1, *__first2))
5565 return true;
5566 if (__comp(*__first2, *__first1))
5567 return false;
5568 }
5569 return false;
5570}
5571
5572template <class _InputIterator1, class _InputIterator2, class _Compare>
Marshall Clow5492c8a2018-01-22 20:44:33 +00005573inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005574bool
5575lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5576 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5577{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005578#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005579 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5580 __debug_less<_Compare> __c(__comp);
5581 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005582#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005583 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5584 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005585#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005586}
5587
5588template <class _InputIterator1, class _InputIterator2>
Marshall Clow5492c8a2018-01-22 20:44:33 +00005589inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005590bool
5591lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5592 _InputIterator2 __first2, _InputIterator2 __last2)
5593{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005594 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005595 __less<typename iterator_traits<_InputIterator1>::value_type,
5596 typename iterator_traits<_InputIterator2>::value_type>());
5597}
5598
5599// next_permutation
5600
5601template <class _Compare, class _BidirectionalIterator>
5602bool
5603__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5604{
5605 _BidirectionalIterator __i = __last;
5606 if (__first == __last || __first == --__i)
5607 return false;
5608 while (true)
5609 {
5610 _BidirectionalIterator __ip1 = __i;
5611 if (__comp(*--__i, *__ip1))
5612 {
5613 _BidirectionalIterator __j = __last;
5614 while (!__comp(*__i, *--__j))
5615 ;
5616 swap(*__i, *__j);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005617 _VSTD::reverse(__ip1, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005618 return true;
5619 }
5620 if (__i == __first)
5621 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005622 _VSTD::reverse(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005623 return false;
5624 }
5625 }
5626}
5627
5628template <class _BidirectionalIterator, class _Compare>
5629inline _LIBCPP_INLINE_VISIBILITY
5630bool
5631next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5632{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005633#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005634 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5635 __debug_less<_Compare> __c(__comp);
5636 return __next_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005637#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005638 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5639 return __next_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005640#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005641}
5642
5643template <class _BidirectionalIterator>
5644inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005645bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00005646next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5647{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005648 return _VSTD::next_permutation(__first, __last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005649 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5650}
5651
5652// prev_permutation
5653
5654template <class _Compare, class _BidirectionalIterator>
5655bool
5656__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5657{
5658 _BidirectionalIterator __i = __last;
5659 if (__first == __last || __first == --__i)
5660 return false;
5661 while (true)
5662 {
5663 _BidirectionalIterator __ip1 = __i;
5664 if (__comp(*__ip1, *--__i))
5665 {
5666 _BidirectionalIterator __j = __last;
5667 while (!__comp(*--__j, *__i))
5668 ;
5669 swap(*__i, *__j);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005670 _VSTD::reverse(__ip1, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005671 return true;
5672 }
5673 if (__i == __first)
5674 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005675 _VSTD::reverse(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005676 return false;
5677 }
5678 }
5679}
5680
5681template <class _BidirectionalIterator, class _Compare>
5682inline _LIBCPP_INLINE_VISIBILITY
5683bool
5684prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5685{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005686#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005687 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5688 __debug_less<_Compare> __c(__comp);
5689 return __prev_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005690#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005691 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5692 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005693#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005694}
5695
5696template <class _BidirectionalIterator>
5697inline _LIBCPP_INLINE_VISIBILITY
5698bool
5699prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5700{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005701 return _VSTD::prev_permutation(__first, __last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005702 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5703}
5704
Howard Hinnantc51e1022010-05-11 19:42:16 +00005705_LIBCPP_END_NAMESPACE_STD
5706
Eric Fiselierf4433a32017-05-31 22:07:49 +00005707_LIBCPP_POP_MACROS
5708
Howard Hinnantc51e1022010-05-11 19:42:16 +00005709#endif // _LIBCPP_ALGORITHM