blob: 7da753a490d573ad651471d8c06586e0a70c9f46 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- algorithm ---------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_ALGORITHM
11#define _LIBCPP_ALGORITHM
12
13/*
14 algorithm synopsis
15
16#include <initializer_list>
17
18namespace std
19{
20
21template <class InputIterator, class Predicate>
Marshall Clowd607fdb2018-01-15 17:20:36 +000022 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000023 all_of(InputIterator first, InputIterator last, Predicate pred);
24
25template <class InputIterator, class Predicate>
Marshall Clowd607fdb2018-01-15 17:20:36 +000026 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000027 any_of(InputIterator first, InputIterator last, Predicate pred);
28
29template <class InputIterator, class Predicate>
Marshall Clowd607fdb2018-01-15 17:20:36 +000030 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000031 none_of(InputIterator first, InputIterator last, Predicate pred);
32
33template <class InputIterator, class Function>
Marshall Clow5492c8a2018-01-22 20:44:33 +000034 constexpr Function // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000035 for_each(InputIterator first, InputIterator last, Function f);
36
Marshall Clowde0169c2017-05-25 02:29:54 +000037template<class InputIterator, class Size, class Function>
Marshall Clow5492c8a2018-01-22 20:44:33 +000038 constexpr InputIterator // constexpr in C++20
39 for_each_n(InputIterator first, Size n, Function f); // C++17
Marshall Clowde0169c2017-05-25 02:29:54 +000040
Howard Hinnantc51e1022010-05-11 19:42:16 +000041template <class InputIterator, class T>
Marshall Clowee0161e2018-01-15 19:26:05 +000042 constexpr InputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000043 find(InputIterator first, InputIterator last, const T& value);
44
45template <class InputIterator, class Predicate>
Marshall Clowee0161e2018-01-15 19:26:05 +000046 constexpr InputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000047 find_if(InputIterator first, InputIterator last, Predicate pred);
48
49template<class InputIterator, class Predicate>
Marshall Clowee0161e2018-01-15 19:26:05 +000050 InputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000051 find_if_not(InputIterator first, InputIterator last, Predicate pred);
52
53template <class ForwardIterator1, class ForwardIterator2>
Marshall Clowee0161e2018-01-15 19:26:05 +000054 ForwardIterator1 // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000055 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
56 ForwardIterator2 first2, ForwardIterator2 last2);
57
58template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clowee0161e2018-01-15 19:26:05 +000059 ForwardIterator1 // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000060 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
61 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
62
63template <class ForwardIterator1, class ForwardIterator2>
Marshall Clowee0161e2018-01-15 19:26:05 +000064 constexpr ForwardIterator1 // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000065 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
66 ForwardIterator2 first2, ForwardIterator2 last2);
67
68template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clowee0161e2018-01-15 19:26:05 +000069 constexpr ForwardIterator1 // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000070 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
71 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
72
73template <class ForwardIterator>
Marshall Clowee0161e2018-01-15 19:26:05 +000074 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000075 adjacent_find(ForwardIterator first, ForwardIterator last);
76
77template <class ForwardIterator, class BinaryPredicate>
Marshall Clowee0161e2018-01-15 19:26:05 +000078 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000079 adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
80
81template <class InputIterator, class T>
Marshall Clow3c0558f2018-01-15 19:40:34 +000082 constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000083 count(InputIterator first, InputIterator last, const T& value);
84
85template <class InputIterator, class Predicate>
Marshall Clow3c0558f2018-01-15 19:40:34 +000086 constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000087 count_if(InputIterator first, InputIterator last, Predicate pred);
88
89template <class InputIterator1, class InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +000090 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000091 mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
92
Marshall Clow96b42b22013-05-09 21:14:23 +000093template <class InputIterator1, class InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +000094 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
Aditya Kumar3a0179a2016-08-25 11:52:38 +000095 mismatch(InputIterator1 first1, InputIterator1 last1,
Marshall Clow96b42b22013-05-09 21:14:23 +000096 InputIterator2 first2, InputIterator2 last2); // **C++14**
97
Howard Hinnantc51e1022010-05-11 19:42:16 +000098template <class InputIterator1, class InputIterator2, class BinaryPredicate>
Marshall Clow30bf3022018-01-16 02:04:10 +000099 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100 mismatch(InputIterator1 first1, InputIterator1 last1,
101 InputIterator2 first2, BinaryPredicate pred);
102
Marshall Clow96b42b22013-05-09 21:14:23 +0000103template <class InputIterator1, class InputIterator2, class BinaryPredicate>
Marshall Clow30bf3022018-01-16 02:04:10 +0000104 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
Marshall Clow96b42b22013-05-09 21:14:23 +0000105 mismatch(InputIterator1 first1, InputIterator1 last1,
106 InputIterator2 first2, InputIterator2 last2,
107 BinaryPredicate pred); // **C++14**
108
Howard Hinnantc51e1022010-05-11 19:42:16 +0000109template <class InputIterator1, class InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +0000110 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000111 equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
112
Marshall Clow96b42b22013-05-09 21:14:23 +0000113template <class InputIterator1, class InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +0000114 constexpr bool // constexpr in C++20
Aditya Kumar3a0179a2016-08-25 11:52:38 +0000115 equal(InputIterator1 first1, InputIterator1 last1,
Marshall Clow96b42b22013-05-09 21:14:23 +0000116 InputIterator2 first2, InputIterator2 last2); // **C++14**
117
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118template <class InputIterator1, class InputIterator2, class BinaryPredicate>
Marshall Clow30bf3022018-01-16 02:04:10 +0000119 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000120 equal(InputIterator1 first1, InputIterator1 last1,
121 InputIterator2 first2, BinaryPredicate pred);
122
Marshall Clow96b42b22013-05-09 21:14:23 +0000123template <class InputIterator1, class InputIterator2, class BinaryPredicate>
Marshall Clow30bf3022018-01-16 02:04:10 +0000124 constexpr bool // constexpr in C++20
Marshall Clow96b42b22013-05-09 21:14:23 +0000125 equal(InputIterator1 first1, InputIterator1 last1,
126 InputIterator2 first2, InputIterator2 last2,
127 BinaryPredicate pred); // **C++14**
128
Howard Hinnantc51e1022010-05-11 19:42:16 +0000129template<class ForwardIterator1, class ForwardIterator2>
Marshall Clow96d050a2018-01-15 16:16:32 +0000130 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000131 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
132 ForwardIterator2 first2);
133
Marshall Clow96b42b22013-05-09 21:14:23 +0000134template<class ForwardIterator1, class ForwardIterator2>
Marshall Clow96d050a2018-01-15 16:16:32 +0000135 constexpr bool // constexpr in C++20
Marshall Clow96b42b22013-05-09 21:14:23 +0000136 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
137 ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
138
Howard Hinnantc51e1022010-05-11 19:42:16 +0000139template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clow96d050a2018-01-15 16:16:32 +0000140 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000141 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
142 ForwardIterator2 first2, BinaryPredicate pred);
143
Marshall Clow96b42b22013-05-09 21:14:23 +0000144template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clow96d050a2018-01-15 16:16:32 +0000145 constexpr bool // constexpr in C++20
Marshall Clow96b42b22013-05-09 21:14:23 +0000146 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
147 ForwardIterator2 first2, ForwardIterator2 last2,
148 BinaryPredicate pred); // **C++14**
149
Howard Hinnantc51e1022010-05-11 19:42:16 +0000150template <class ForwardIterator1, class ForwardIterator2>
Marshall Clow323fc5b2018-01-16 15:48:27 +0000151 constexpr ForwardIterator1 // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152 search(ForwardIterator1 first1, ForwardIterator1 last1,
153 ForwardIterator2 first2, ForwardIterator2 last2);
154
155template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clow323fc5b2018-01-16 15:48:27 +0000156 constexpr ForwardIterator1 // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157 search(ForwardIterator1 first1, ForwardIterator1 last1,
158 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
159
160template <class ForwardIterator, class Size, class T>
Marshall Clow323fc5b2018-01-16 15:48:27 +0000161 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000162 search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
163
164template <class ForwardIterator, class Size, class T, class BinaryPredicate>
Marshall Clow323fc5b2018-01-16 15:48:27 +0000165 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 search_n(ForwardIterator first, ForwardIterator last,
167 Size count, const T& value, BinaryPredicate pred);
168
169template <class InputIterator, class OutputIterator>
170 OutputIterator
171 copy(InputIterator first, InputIterator last, OutputIterator result);
172
173template<class InputIterator, class OutputIterator, class Predicate>
174 OutputIterator
175 copy_if(InputIterator first, InputIterator last,
176 OutputIterator result, Predicate pred);
177
178template<class InputIterator, class Size, class OutputIterator>
179 OutputIterator
180 copy_n(InputIterator first, Size n, OutputIterator result);
181
182template <class BidirectionalIterator1, class BidirectionalIterator2>
183 BidirectionalIterator2
184 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
185 BidirectionalIterator2 result);
186
187template <class ForwardIterator1, class ForwardIterator2>
188 ForwardIterator2
189 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
190
191template <class ForwardIterator1, class ForwardIterator2>
192 void
193 iter_swap(ForwardIterator1 a, ForwardIterator2 b);
194
195template <class InputIterator, class OutputIterator, class UnaryOperation>
Marshall Clow31427c62018-01-19 17:45:39 +0000196 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000197 transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
198
199template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
Marshall Clow31427c62018-01-19 17:45:39 +0000200 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000201 transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
202 OutputIterator result, BinaryOperation binary_op);
203
204template <class ForwardIterator, class T>
Marshall Clow01bbbd22018-01-19 18:07:29 +0000205 constexpr void // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000206 replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
207
208template <class ForwardIterator, class Predicate, class T>
Marshall Clow01bbbd22018-01-19 18:07:29 +0000209 constexpr void // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000210 replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
211
212template <class InputIterator, class OutputIterator, class T>
Marshall Clow01bbbd22018-01-19 18:07:29 +0000213 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000214 replace_copy(InputIterator first, InputIterator last, OutputIterator result,
215 const T& old_value, const T& new_value);
216
217template <class InputIterator, class OutputIterator, class Predicate, class T>
Marshall Clow01bbbd22018-01-19 18:07:29 +0000218 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000219 replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
220
221template <class ForwardIterator, class T>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +0000222 constexpr void // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223 fill(ForwardIterator first, ForwardIterator last, const T& value);
224
225template <class OutputIterator, class Size, class T>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +0000226 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000227 fill_n(OutputIterator first, Size n, const T& value);
228
229template <class ForwardIterator, class Generator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +0000230 constexpr void // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 generate(ForwardIterator first, ForwardIterator last, Generator gen);
232
233template <class OutputIterator, class Size, class Generator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +0000234 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000235 generate_n(OutputIterator first, Size n, Generator gen);
236
237template <class ForwardIterator, class T>
Marshall Clow7c0fbd82018-01-22 21:43:04 +0000238 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000239 remove(ForwardIterator first, ForwardIterator last, const T& value);
240
241template <class ForwardIterator, class Predicate>
Marshall Clow7c0fbd82018-01-22 21:43:04 +0000242 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000243 remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
244
245template <class InputIterator, class OutputIterator, class T>
Marshall Clow7c0fbd82018-01-22 21:43:04 +0000246 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247 remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
248
249template <class InputIterator, class OutputIterator, class Predicate>
Marshall Clow7c0fbd82018-01-22 21:43:04 +0000250 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000251 remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
252
253template <class ForwardIterator>
254 ForwardIterator
255 unique(ForwardIterator first, ForwardIterator last);
256
257template <class ForwardIterator, class BinaryPredicate>
258 ForwardIterator
259 unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
260
261template <class InputIterator, class OutputIterator>
262 OutputIterator
263 unique_copy(InputIterator first, InputIterator last, OutputIterator result);
264
265template <class InputIterator, class OutputIterator, class BinaryPredicate>
266 OutputIterator
267 unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
268
269template <class BidirectionalIterator>
270 void
271 reverse(BidirectionalIterator first, BidirectionalIterator last);
272
273template <class BidirectionalIterator, class OutputIterator>
Marshall Clow7c0fbd82018-01-22 21:43:04 +0000274 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000275 reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
276
277template <class ForwardIterator>
278 ForwardIterator
279 rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
280
281template <class ForwardIterator, class OutputIterator>
282 OutputIterator
283 rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
284
285template <class RandomAccessIterator>
286 void
Marshall Clowfac06e52017-03-23 13:43:37 +0000287 random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000288
289template <class RandomAccessIterator, class RandomNumberGenerator>
290 void
Marshall Clow83f35392014-03-03 06:14:19 +0000291 random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
Marshall Clowfac06e52017-03-23 13:43:37 +0000292 RandomNumberGenerator& rand); // deprecated in C++14, removed in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000293
Eric Fiselier1208fcd2016-08-28 22:14:37 +0000294template<class PopulationIterator, class SampleIterator,
295 class Distance, class UniformRandomBitGenerator>
296 SampleIterator sample(PopulationIterator first, PopulationIterator last,
297 SampleIterator out, Distance n,
298 UniformRandomBitGenerator&& g); // C++17
299
Howard Hinnant578ac0f2010-05-26 17:49:34 +0000300template<class RandomAccessIterator, class UniformRandomNumberGenerator>
301 void shuffle(RandomAccessIterator first, RandomAccessIterator last,
Howard Hinnanta5e71782010-11-18 01:47:02 +0000302 UniformRandomNumberGenerator&& g);
Howard Hinnant578ac0f2010-05-26 17:49:34 +0000303
Howard Hinnantc51e1022010-05-11 19:42:16 +0000304template <class InputIterator, class Predicate>
Marshall Clow96d050a2018-01-15 16:16:32 +0000305 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000306 is_partitioned(InputIterator first, InputIterator last, Predicate pred);
307
308template <class ForwardIterator, class Predicate>
309 ForwardIterator
310 partition(ForwardIterator first, ForwardIterator last, Predicate pred);
311
312template <class InputIterator, class OutputIterator1,
313 class OutputIterator2, class Predicate>
Marshall Clow5492c8a2018-01-22 20:44:33 +0000314 constexpr pair<OutputIterator1, OutputIterator2> // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000315 partition_copy(InputIterator first, InputIterator last,
316 OutputIterator1 out_true, OutputIterator2 out_false,
317 Predicate pred);
318
319template <class ForwardIterator, class Predicate>
320 ForwardIterator
321 stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
322
323template<class ForwardIterator, class Predicate>
Marshall Clowe00916c2018-01-16 02:34:41 +0000324 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000325 partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
326
327template <class ForwardIterator>
Marshall Clow96d050a2018-01-15 16:16:32 +0000328 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000329 is_sorted(ForwardIterator first, ForwardIterator last);
330
331template <class ForwardIterator, class Compare>
332 bool
333 is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
334
335template<class ForwardIterator>
Marshall Clow3c0558f2018-01-15 19:40:34 +0000336 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000337 is_sorted_until(ForwardIterator first, ForwardIterator last);
338
339template <class ForwardIterator, class Compare>
Marshall Clow3c0558f2018-01-15 19:40:34 +0000340 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000341 is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
342
343template <class RandomAccessIterator>
344 void
345 sort(RandomAccessIterator first, RandomAccessIterator last);
346
347template <class RandomAccessIterator, class Compare>
348 void
349 sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
350
351template <class RandomAccessIterator>
352 void
353 stable_sort(RandomAccessIterator first, RandomAccessIterator last);
354
355template <class RandomAccessIterator, class Compare>
356 void
357 stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
358
359template <class RandomAccessIterator>
360 void
361 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
362
363template <class RandomAccessIterator, class Compare>
364 void
365 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
366
367template <class InputIterator, class RandomAccessIterator>
368 RandomAccessIterator
369 partial_sort_copy(InputIterator first, InputIterator last,
370 RandomAccessIterator result_first, RandomAccessIterator result_last);
371
372template <class InputIterator, class RandomAccessIterator, class Compare>
373 RandomAccessIterator
374 partial_sort_copy(InputIterator first, InputIterator last,
375 RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
376
377template <class RandomAccessIterator>
378 void
379 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
380
381template <class RandomAccessIterator, class Compare>
382 void
383 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
384
385template <class ForwardIterator, class T>
Marshall Clowe00916c2018-01-16 02:34:41 +0000386 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000387 lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
388
389template <class ForwardIterator, class T, class Compare>
Marshall Clowe00916c2018-01-16 02:34:41 +0000390 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391 lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
392
393template <class ForwardIterator, class T>
Marshall Clowe00916c2018-01-16 02:34:41 +0000394 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000395 upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
396
397template <class ForwardIterator, class T, class Compare>
Marshall Clowe00916c2018-01-16 02:34:41 +0000398 constexpr ForwardIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399 upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
400
401template <class ForwardIterator, class T>
Marshall Clowe00916c2018-01-16 02:34:41 +0000402 constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403 equal_range(ForwardIterator first, ForwardIterator last, const T& value);
404
405template <class ForwardIterator, class T, class Compare>
Marshall Clowe00916c2018-01-16 02:34:41 +0000406 constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407 equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
408
409template <class ForwardIterator, class T>
Marshall Clowe00916c2018-01-16 02:34:41 +0000410 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411 binary_search(ForwardIterator first, ForwardIterator last, const T& value);
412
413template <class ForwardIterator, class T, class Compare>
Marshall Clowc0b7f972018-01-22 23:10:40 +0000414 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415 binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
416
417template <class InputIterator1, class InputIterator2, class OutputIterator>
418 OutputIterator
419 merge(InputIterator1 first1, InputIterator1 last1,
420 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
421
422template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
423 OutputIterator
424 merge(InputIterator1 first1, InputIterator1 last1,
425 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
426
427template <class BidirectionalIterator>
428 void
429 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
430
431template <class BidirectionalIterator, class Compare>
432 void
433 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
434
435template <class InputIterator1, class InputIterator2>
Marshall Clowc0b7f972018-01-22 23:10:40 +0000436 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000437 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
438
439template <class InputIterator1, class InputIterator2, class Compare>
Marshall Clowc0b7f972018-01-22 23:10:40 +0000440 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000441 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
442
443template <class InputIterator1, class InputIterator2, class OutputIterator>
444 OutputIterator
445 set_union(InputIterator1 first1, InputIterator1 last1,
446 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
447
448template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
449 OutputIterator
450 set_union(InputIterator1 first1, InputIterator1 last1,
451 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
452
453template <class InputIterator1, class InputIterator2, class OutputIterator>
Marshall Clowc0b7f972018-01-22 23:10:40 +0000454 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000455 set_intersection(InputIterator1 first1, InputIterator1 last1,
456 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
457
458template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
Marshall Clowc0b7f972018-01-22 23:10:40 +0000459 constexpr OutputIterator // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000460 set_intersection(InputIterator1 first1, InputIterator1 last1,
461 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
462
463template <class InputIterator1, class InputIterator2, class OutputIterator>
464 OutputIterator
465 set_difference(InputIterator1 first1, InputIterator1 last1,
466 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
467
468template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
469 OutputIterator
470 set_difference(InputIterator1 first1, InputIterator1 last1,
471 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
472
473template <class InputIterator1, class InputIterator2, class OutputIterator>
474 OutputIterator
475 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
476 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
477
478template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
479 OutputIterator
480 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
481 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
482
483template <class RandomAccessIterator>
484 void
485 push_heap(RandomAccessIterator first, RandomAccessIterator last);
486
487template <class RandomAccessIterator, class Compare>
488 void
489 push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
490
491template <class RandomAccessIterator>
492 void
493 pop_heap(RandomAccessIterator first, RandomAccessIterator last);
494
495template <class RandomAccessIterator, class Compare>
496 void
497 pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
498
499template <class RandomAccessIterator>
500 void
501 make_heap(RandomAccessIterator first, RandomAccessIterator last);
502
503template <class RandomAccessIterator, class Compare>
504 void
505 make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
506
507template <class RandomAccessIterator>
508 void
509 sort_heap(RandomAccessIterator first, RandomAccessIterator last);
510
511template <class RandomAccessIterator, class Compare>
512 void
513 sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
514
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000515template <class RandomAccessIterator>
Marshall Clow96d050a2018-01-15 16:16:32 +0000516 constexpr bool // constexpr in C++20
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000517 is_heap(RandomAccessIterator first, RandomAccessiterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000518
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000519template <class RandomAccessIterator, class Compare>
Marshall Clow96d050a2018-01-15 16:16:32 +0000520 constexpr bool // constexpr in C++20
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000521 is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000522
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000523template <class RandomAccessIterator>
Marshall Clow96d050a2018-01-15 16:16:32 +0000524 constexpr RandomAccessIterator // constexpr in C++20
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000525 is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000526
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000527template <class RandomAccessIterator, class Compare>
Marshall Clow96d050a2018-01-15 16:16:32 +0000528 constexpr RandomAccessIterator // constexpr in C++20
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000529 is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000530
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000531template <class ForwardIterator>
532 ForwardIterator
Marshall Clow9e173072015-05-10 13:53:31 +0000533 min_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000534
535template <class ForwardIterator, class Compare>
536 ForwardIterator
Marshall Clow9e173072015-05-10 13:53:31 +0000537 min_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000538
Howard Hinnantc51e1022010-05-11 19:42:16 +0000539template <class T>
540 const T&
Marshall Clowe9dca072014-02-19 16:51:35 +0000541 min(const T& a, const T& b); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000542
543template <class T, class Compare>
544 const T&
Marshall Clowe9dca072014-02-19 16:51:35 +0000545 min(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000546
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000547template<class T>
548 T
Marshall Clowe9dca072014-02-19 16:51:35 +0000549 min(initializer_list<T> t); // constexpr in C++14
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000550
551template<class T, class Compare>
552 T
Marshall Clowe9dca072014-02-19 16:51:35 +0000553 min(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000554
Marshall Clow3e18d0e2016-03-07 22:43:49 +0000555template<class T>
556 constexpr const T& clamp( const T& v, const T& lo, const T& hi ); // C++17
557
558template<class T, class Compare>
559 constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp ); // C++17
560
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000561template <class ForwardIterator>
562 ForwardIterator
Marshall Clow9e173072015-05-10 13:53:31 +0000563 max_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000564
565template <class ForwardIterator, class Compare>
566 ForwardIterator
Marshall Clow9e173072015-05-10 13:53:31 +0000567 max_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000568
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569template <class T>
570 const T&
Marshall Clowe9dca072014-02-19 16:51:35 +0000571 max(const T& a, const T& b); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000572
573template <class T, class Compare>
574 const T&
Marshall Clowe9dca072014-02-19 16:51:35 +0000575 max(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000576
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000577template<class T>
578 T
Marshall Clowe9dca072014-02-19 16:51:35 +0000579 max(initializer_list<T> t); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000580
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000581template<class T, class Compare>
582 T
Marshall Clowe9dca072014-02-19 16:51:35 +0000583 max(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000585template<class ForwardIterator>
586 pair<ForwardIterator, ForwardIterator>
Marshall Clow9e173072015-05-10 13:53:31 +0000587 minmax_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000588
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000589template<class ForwardIterator, class Compare>
590 pair<ForwardIterator, ForwardIterator>
Marshall Clow9e173072015-05-10 13:53:31 +0000591 minmax_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000592
593template<class T>
594 pair<const T&, const T&>
Marshall Clowe9dca072014-02-19 16:51:35 +0000595 minmax(const T& a, const T& b); // constexpr in C++14
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000596
597template<class T, class Compare>
598 pair<const T&, const T&>
Marshall Clowe9dca072014-02-19 16:51:35 +0000599 minmax(const T& a, const T& b, Compare comp); // constexpr in C++14
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000600
601template<class T>
602 pair<T, T>
Marshall Clowe9dca072014-02-19 16:51:35 +0000603 minmax(initializer_list<T> t); // constexpr in C++14
Howard Hinnantb120e7a2010-08-21 20:10:01 +0000604
605template<class T, class Compare>
606 pair<T, T>
Marshall Clowe9dca072014-02-19 16:51:35 +0000607 minmax(initializer_list<T> t, Compare comp); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000608
609template <class InputIterator1, class InputIterator2>
Marshall Clow5492c8a2018-01-22 20:44:33 +0000610 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000611 lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
612
613template <class InputIterator1, class InputIterator2, class Compare>
Marshall Clow5492c8a2018-01-22 20:44:33 +0000614 constexpr bool // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000615 lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
616 InputIterator2 first2, InputIterator2 last2, Compare comp);
617
618template <class BidirectionalIterator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000619 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +0000620 next_permutation(BidirectionalIterator first, BidirectionalIterator last);
621
622template <class BidirectionalIterator, class Compare>
623 bool
624 next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
625
626template <class BidirectionalIterator>
627 bool
628 prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
629
630template <class BidirectionalIterator, class Compare>
631 bool
632 prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
633
634} // std
635
636*/
637
638#include <__config>
639#include <initializer_list>
640#include <type_traits>
641#include <cstring>
Eric Fiselier6bfed252016-04-21 23:38:59 +0000642#include <utility> // needed to provide swap_ranges.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000643#include <memory>
Marshall Clowa40686b2018-01-08 19:18:00 +0000644#include <functional>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000645#include <iterator>
Howard Hinnant60e5cf42012-07-26 17:09:09 +0000646#include <cstddef>
Marshall Clowbf32ec92018-08-17 16:07:48 +0000647#include <bit>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000648#include <version>
Howard Hinnantea382952013-08-14 18:00:20 +0000649
Eric Fiselier14b6de92014-08-10 23:53:08 +0000650#include <__debug>
651
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000652#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000653#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000654#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000655
Eric Fiselierf4433a32017-05-31 22:07:49 +0000656_LIBCPP_PUSH_MACROS
657#include <__undef_macros>
658
659
Howard Hinnantc51e1022010-05-11 19:42:16 +0000660_LIBCPP_BEGIN_NAMESPACE_STD
661
Marshall Clowe9dca072014-02-19 16:51:35 +0000662// I'd like to replace these with _VSTD::equal_to<void>, but can't because:
663// * That only works with C++14 and later, and
664// * We haven't included <functional> here.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000665template <class _T1, class _T2 = _T1>
666struct __equal_to
667{
Marshall Clow1f695172018-07-14 04:15:19 +0000668 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
669 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
670 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
671 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672};
673
674template <class _T1>
675struct __equal_to<_T1, _T1>
676{
Marshall Clowe9dca072014-02-19 16:51:35 +0000677 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
678 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000679};
680
681template <class _T1>
682struct __equal_to<const _T1, _T1>
683{
Marshall Clowe9dca072014-02-19 16:51:35 +0000684 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
685 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000686};
687
688template <class _T1>
689struct __equal_to<_T1, const _T1>
690{
Marshall Clowe9dca072014-02-19 16:51:35 +0000691 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
692 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000693};
694
695template <class _T1, class _T2 = _T1>
696struct __less
697{
Aditya Kumar3a0179a2016-08-25 11:52:38 +0000698 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowe9dca072014-02-19 16:51:35 +0000699 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
700
701 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
702 bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
703
704 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
705 bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
706
707 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
708 bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000709};
710
711template <class _T1>
712struct __less<_T1, _T1>
713{
Marshall Clowe9dca072014-02-19 16:51:35 +0000714 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
715 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000716};
717
718template <class _T1>
719struct __less<const _T1, _T1>
720{
Marshall Clowe9dca072014-02-19 16:51:35 +0000721 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
722 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000723};
724
725template <class _T1>
726struct __less<_T1, const _T1>
727{
Marshall Clowe9dca072014-02-19 16:51:35 +0000728 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
729 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000730};
731
732template <class _Predicate>
Marshall Clow738d1042017-08-28 23:16:13 +0000733class __invert // invert the sense of a comparison
Howard Hinnantc51e1022010-05-11 19:42:16 +0000734{
735private:
736 _Predicate __p_;
737public:
Marshall Clow738d1042017-08-28 23:16:13 +0000738 _LIBCPP_INLINE_VISIBILITY __invert() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000739
740 _LIBCPP_INLINE_VISIBILITY
Marshall Clow738d1042017-08-28 23:16:13 +0000741 explicit __invert(_Predicate __p) : __p_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000742
743 template <class _T1>
744 _LIBCPP_INLINE_VISIBILITY
745 bool operator()(const _T1& __x) {return !__p_(__x);}
746
747 template <class _T1, class _T2>
748 _LIBCPP_INLINE_VISIBILITY
Marshall Clow738d1042017-08-28 23:16:13 +0000749 bool operator()(const _T1& __x, const _T2& __y) {return __p_(__y, __x);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750};
751
Louis Dionnedda14512018-12-17 16:04:39 +0000752// Perform division by two quickly for positive integers (llvm.org/PR39129)
753
754template <typename _Integral>
755_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
756typename enable_if
757<
758 is_integral<_Integral>::value,
759 _Integral
760>::type
761__half_positive(_Integral __value)
762{
763 return static_cast<_Integral>(static_cast<typename make_unsigned<_Integral>::type>(__value) / 2);
764}
765
766template <typename _Tp>
767_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
768typename enable_if
769<
770 !is_integral<_Tp>::value,
771 _Tp
772>::type
773__half_positive(_Tp __value)
774{
775 return __value / 2;
776}
777
Howard Hinnant6148a9b2013-08-23 20:10:18 +0000778#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000779
780template <class _Compare>
781struct __debug_less
782{
783 _Compare __comp_;
784 __debug_less(_Compare& __c) : __comp_(__c) {}
Eric Fiselier32adec82016-07-19 23:27:18 +0000785
Howard Hinnantc51e1022010-05-11 19:42:16 +0000786 template <class _Tp, class _Up>
787 bool operator()(const _Tp& __x, const _Up& __y)
788 {
789 bool __r = __comp_(__x, __y);
790 if (__r)
Eric Fiselier32adec82016-07-19 23:27:18 +0000791 __do_compare_assert(0, __y, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000792 return __r;
793 }
Eric Fiselier32adec82016-07-19 23:27:18 +0000794
795 template <class _LHS, class _RHS>
796 inline _LIBCPP_INLINE_VISIBILITY
797 decltype((void)_VSTD::declval<_Compare&>()(
798 _VSTD::declval<_LHS const&>(), _VSTD::declval<_RHS const&>()))
799 __do_compare_assert(int, _LHS const& __l, _RHS const& __r) {
800 _LIBCPP_ASSERT(!__comp_(__l, __r),
801 "Comparator does not induce a strict weak ordering");
802 }
803
804 template <class _LHS, class _RHS>
805 inline _LIBCPP_INLINE_VISIBILITY
806 void __do_compare_assert(long, _LHS const&, _RHS const&) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000807};
808
Howard Hinnant6148a9b2013-08-23 20:10:18 +0000809#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810
811// all_of
812
813template <class _InputIterator, class _Predicate>
Marshall Clowd607fdb2018-01-15 17:20:36 +0000814inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000815bool
816all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
817{
818 for (; __first != __last; ++__first)
819 if (!__pred(*__first))
820 return false;
821 return true;
822}
823
824// any_of
825
826template <class _InputIterator, class _Predicate>
Marshall Clowd607fdb2018-01-15 17:20:36 +0000827inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000828bool
829any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
830{
831 for (; __first != __last; ++__first)
832 if (__pred(*__first))
833 return true;
834 return false;
835}
836
837// none_of
838
839template <class _InputIterator, class _Predicate>
Marshall Clowd607fdb2018-01-15 17:20:36 +0000840inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000841bool
842none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
843{
844 for (; __first != __last; ++__first)
845 if (__pred(*__first))
846 return false;
847 return true;
848}
849
850// for_each
851
852template <class _InputIterator, class _Function>
Marshall Clow5492c8a2018-01-22 20:44:33 +0000853inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000854_Function
855for_each(_InputIterator __first, _InputIterator __last, _Function __f)
856{
857 for (; __first != __last; ++__first)
858 __f(*__first);
Marshall Clow78dbe462016-11-14 18:22:19 +0000859 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000860}
861
Marshall Clowc588fd62017-05-25 13:40:57 +0000862#if _LIBCPP_STD_VER > 14
Marshall Clowde0169c2017-05-25 02:29:54 +0000863// for_each_n
864
865template <class _InputIterator, class _Size, class _Function>
Marshall Clow5492c8a2018-01-22 20:44:33 +0000866inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowde0169c2017-05-25 02:29:54 +0000867_InputIterator
868for_each_n(_InputIterator __first, _Size __orig_n, _Function __f)
869{
870 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
871 _IntegralSize __n = __orig_n;
872 while (__n > 0)
873 {
874 __f(*__first);
875 ++__first;
876 --__n;
877 }
878 return __first;
879}
Marshall Clowc588fd62017-05-25 13:40:57 +0000880#endif
Marshall Clowde0169c2017-05-25 02:29:54 +0000881
Howard Hinnantc51e1022010-05-11 19:42:16 +0000882// find
883
884template <class _InputIterator, class _Tp>
Marshall Clow96d050a2018-01-15 16:16:32 +0000885inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000886_InputIterator
Howard Hinnantbf074022011-10-22 20:59:45 +0000887find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000888{
889 for (; __first != __last; ++__first)
Howard Hinnantbf074022011-10-22 20:59:45 +0000890 if (*__first == __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000891 break;
892 return __first;
893}
894
895// find_if
896
897template <class _InputIterator, class _Predicate>
Marshall Clow96d050a2018-01-15 16:16:32 +0000898inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000899_InputIterator
900find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
901{
902 for (; __first != __last; ++__first)
903 if (__pred(*__first))
904 break;
905 return __first;
906}
907
908// find_if_not
909
910template<class _InputIterator, class _Predicate>
Marshall Clowee0161e2018-01-15 19:26:05 +0000911inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000912_InputIterator
913find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
914{
915 for (; __first != __last; ++__first)
916 if (!__pred(*__first))
917 break;
918 return __first;
919}
920
921// find_end
922
923template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clowee0161e2018-01-15 19:26:05 +0000924_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1
Howard Hinnantc51e1022010-05-11 19:42:16 +0000925__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
926 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
927 forward_iterator_tag, forward_iterator_tag)
928{
929 // modeled after search algorithm
930 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
931 if (__first2 == __last2)
932 return __r;
933 while (true)
934 {
935 while (true)
936 {
937 if (__first1 == __last1) // if source exhausted return last correct answer
938 return __r; // (or __last1 if never found)
939 if (__pred(*__first1, *__first2))
940 break;
941 ++__first1;
942 }
943 // *__first1 matches *__first2, now match elements after here
944 _ForwardIterator1 __m1 = __first1;
945 _ForwardIterator2 __m2 = __first2;
946 while (true)
947 {
948 if (++__m2 == __last2)
949 { // Pattern exhaused, record answer and search for another one
950 __r = __first1;
951 ++__first1;
952 break;
953 }
954 if (++__m1 == __last1) // Source exhausted, return last answer
955 return __r;
956 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
957 {
958 ++__first1;
959 break;
960 } // else there is a match, check next elements
961 }
962 }
963}
964
965template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
Marshall Clowee0161e2018-01-15 19:26:05 +0000966_LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator1
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
968 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
969 bidirectional_iterator_tag, bidirectional_iterator_tag)
970{
971 // modeled after search algorithm (in reverse)
972 if (__first2 == __last2)
973 return __last1; // Everything matches an empty sequence
974 _BidirectionalIterator1 __l1 = __last1;
975 _BidirectionalIterator2 __l2 = __last2;
976 --__l2;
977 while (true)
978 {
979 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
980 while (true)
981 {
982 if (__first1 == __l1) // return __last1 if no element matches *__first2
983 return __last1;
984 if (__pred(*--__l1, *__l2))
985 break;
986 }
987 // *__l1 matches *__l2, now match elements before here
988 _BidirectionalIterator1 __m1 = __l1;
989 _BidirectionalIterator2 __m2 = __l2;
990 while (true)
991 {
992 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
993 return __m1;
994 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
995 return __last1;
996 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
997 {
998 break;
999 } // else there is a match, check next elements
1000 }
1001 }
1002}
1003
1004template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clowedfd25a2014-06-10 18:51:55 +00001005_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1007 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1008 random_access_iterator_tag, random_access_iterator_tag)
1009{
1010 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1011 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
1012 if (__len2 == 0)
1013 return __last1;
1014 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
1015 if (__len1 < __len2)
1016 return __last1;
1017 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
1018 _RandomAccessIterator1 __l1 = __last1;
1019 _RandomAccessIterator2 __l2 = __last2;
1020 --__l2;
1021 while (true)
1022 {
1023 while (true)
1024 {
1025 if (__s == __l1)
1026 return __last1;
1027 if (__pred(*--__l1, *__l2))
1028 break;
1029 }
1030 _RandomAccessIterator1 __m1 = __l1;
1031 _RandomAccessIterator2 __m2 = __l2;
1032 while (true)
1033 {
1034 if (__m2 == __first2)
1035 return __m1;
1036 // no need to check range on __m1 because __s guarantees we have enough source
1037 if (!__pred(*--__m1, *--__m2))
1038 {
1039 break;
1040 }
1041 }
1042 }
1043}
1044
1045template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clowee0161e2018-01-15 19:26:05 +00001046inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001047_ForwardIterator1
1048find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1049 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1050{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001051 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052 (__first1, __last1, __first2, __last2, __pred,
1053 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1054 typename iterator_traits<_ForwardIterator2>::iterator_category());
1055}
1056
1057template <class _ForwardIterator1, class _ForwardIterator2>
Marshall Clowee0161e2018-01-15 19:26:05 +00001058inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001059_ForwardIterator1
1060find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1061 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1062{
1063 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1064 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001065 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066}
1067
1068// find_first_of
1069
1070template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clowedfd25a2014-06-10 18:51:55 +00001071_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1072__find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1074{
1075 for (; __first1 != __last1; ++__first1)
1076 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1077 if (__pred(*__first1, *__j))
1078 return __first1;
1079 return __last1;
1080}
1081
Marshall Clowedfd25a2014-06-10 18:51:55 +00001082
1083template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clowee0161e2018-01-15 19:26:05 +00001084inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowedfd25a2014-06-10 18:51:55 +00001085_ForwardIterator1
1086find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1087 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1088{
1089 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1090}
1091
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092template <class _ForwardIterator1, class _ForwardIterator2>
Marshall Clowee0161e2018-01-15 19:26:05 +00001093inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094_ForwardIterator1
1095find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1096 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1097{
1098 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1099 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Marshall Clowedfd25a2014-06-10 18:51:55 +00001100 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101}
1102
1103// adjacent_find
1104
1105template <class _ForwardIterator, class _BinaryPredicate>
Marshall Clowee0161e2018-01-15 19:26:05 +00001106inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001107_ForwardIterator
1108adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1109{
1110 if (__first != __last)
1111 {
1112 _ForwardIterator __i = __first;
1113 while (++__i != __last)
1114 {
1115 if (__pred(*__first, *__i))
1116 return __first;
1117 __first = __i;
1118 }
1119 }
1120 return __last;
1121}
1122
1123template <class _ForwardIterator>
Marshall Clowee0161e2018-01-15 19:26:05 +00001124inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125_ForwardIterator
1126adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1127{
1128 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001129 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130}
1131
1132// count
1133
1134template <class _InputIterator, class _Tp>
Marshall Clow3c0558f2018-01-15 19:40:34 +00001135inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136typename iterator_traits<_InputIterator>::difference_type
Howard Hinnantbf074022011-10-22 20:59:45 +00001137count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001138{
1139 typename iterator_traits<_InputIterator>::difference_type __r(0);
1140 for (; __first != __last; ++__first)
Howard Hinnantbf074022011-10-22 20:59:45 +00001141 if (*__first == __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142 ++__r;
1143 return __r;
1144}
1145
1146// count_if
1147
1148template <class _InputIterator, class _Predicate>
Marshall Clow3c0558f2018-01-15 19:40:34 +00001149inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150typename iterator_traits<_InputIterator>::difference_type
1151count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1152{
1153 typename iterator_traits<_InputIterator>::difference_type __r(0);
1154 for (; __first != __last; ++__first)
1155 if (__pred(*__first))
1156 ++__r;
1157 return __r;
1158}
1159
1160// mismatch
1161
1162template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Marshall Clow30bf3022018-01-16 02:04:10 +00001163inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164pair<_InputIterator1, _InputIterator2>
1165mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1166 _InputIterator2 __first2, _BinaryPredicate __pred)
1167{
Marshall Clow2932e512014-09-16 20:40:05 +00001168 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 if (!__pred(*__first1, *__first2))
1170 break;
1171 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1172}
1173
1174template <class _InputIterator1, class _InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001175inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176pair<_InputIterator1, _InputIterator2>
1177mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1178{
1179 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1180 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001181 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182}
1183
Marshall Clow96b42b22013-05-09 21:14:23 +00001184#if _LIBCPP_STD_VER > 11
1185template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Marshall Clow30bf3022018-01-16 02:04:10 +00001186inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001187pair<_InputIterator1, _InputIterator2>
1188mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1189 _InputIterator2 __first2, _InputIterator2 __last2,
1190 _BinaryPredicate __pred)
1191{
Marshall Clow2932e512014-09-16 20:40:05 +00001192 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow96b42b22013-05-09 21:14:23 +00001193 if (!__pred(*__first1, *__first2))
1194 break;
1195 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1196}
1197
1198template <class _InputIterator1, class _InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001199inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001200pair<_InputIterator1, _InputIterator2>
1201mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1202 _InputIterator2 __first2, _InputIterator2 __last2)
1203{
1204 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1205 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1206 return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1207}
1208#endif
1209
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210// equal
1211
1212template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Marshall Clow30bf3022018-01-16 02:04:10 +00001213inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214bool
1215equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1216{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001217 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001218 if (!__pred(*__first1, *__first2))
1219 return false;
1220 return true;
1221}
1222
1223template <class _InputIterator1, class _InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001224inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225bool
1226equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1227{
1228 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1229 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001230 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231}
1232
Marshall Clow96b42b22013-05-09 21:14:23 +00001233#if _LIBCPP_STD_VER > 11
1234template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001235inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001236bool
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001237__equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow96b42b22013-05-09 21:14:23 +00001238 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1239 input_iterator_tag, input_iterator_tag )
1240{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001241 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow96b42b22013-05-09 21:14:23 +00001242 if (!__pred(*__first1, *__first2))
1243 return false;
1244 return __first1 == __last1 && __first2 == __last2;
1245}
1246
1247template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001248inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001249bool
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001250__equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1251 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
Marshall Clow96b42b22013-05-09 21:14:23 +00001252 random_access_iterator_tag, random_access_iterator_tag )
1253{
1254 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1255 return false;
1256 return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1257 typename add_lvalue_reference<_BinaryPredicate>::type>
1258 (__first1, __last1, __first2, __pred );
1259}
1260
1261template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Marshall Clow30bf3022018-01-16 02:04:10 +00001262inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001263bool
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001264equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow96b42b22013-05-09 21:14:23 +00001265 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1266{
1267 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001268 (__first1, __last1, __first2, __last2, __pred,
Marshall Clow96b42b22013-05-09 21:14:23 +00001269 typename iterator_traits<_InputIterator1>::iterator_category(),
1270 typename iterator_traits<_InputIterator2>::iterator_category());
1271}
1272
1273template <class _InputIterator1, class _InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001274inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001275bool
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001276equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow96b42b22013-05-09 21:14:23 +00001277 _InputIterator2 __first2, _InputIterator2 __last2)
1278{
1279 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1280 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1281 return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1282 typename iterator_traits<_InputIterator1>::iterator_category(),
1283 typename iterator_traits<_InputIterator2>::iterator_category());
1284}
1285#endif
1286
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287// is_permutation
1288
1289template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow96d050a2018-01-15 16:16:32 +00001290_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00001291is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1292 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1293{
Marshall Clow96d050a2018-01-15 16:16:32 +00001294// shorten sequences as much as possible by lopping of any equal prefix
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001295 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001296 if (!__pred(*__first1, *__first2))
Marshall Clow96d050a2018-01-15 16:16:32 +00001297 break;
1298 if (__first1 == __last1)
1299 return true;
1300
1301// __first1 != __last1 && *__first1 != *__first2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001303 _D1 __l1 = _VSTD::distance(__first1, __last1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304 if (__l1 == _D1(1))
1305 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001306 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001307 // For each element in [f1, l1) see if there are the same number of
1308 // equal elements in [f2, l2)
1309 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1310 {
Marshall Clow96d050a2018-01-15 16:16:32 +00001311 // Have we already counted the number of *__i in [f1, l1)?
Peter Collingbournea5b451b2018-01-26 21:23:27 +00001312 _ForwardIterator1 __match = __first1;
1313 for (; __match != __i; ++__match)
1314 if (__pred(*__match, *__i))
1315 break;
1316 if (__match == __i) {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001317 // Count number of *__i in [f2, l2)
1318 _D1 __c2 = 0;
1319 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1320 if (__pred(*__i, *__j))
1321 ++__c2;
1322 if (__c2 == 0)
1323 return false;
1324 // Count number of *__i in [__i, l1) (we can start with 1)
1325 _D1 __c1 = 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001326 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001327 if (__pred(*__i, *__j))
1328 ++__c1;
1329 if (__c1 != __c2)
1330 return false;
1331 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001332 }
1333 return true;
1334}
1335
1336template<class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow96d050a2018-01-15 16:16:32 +00001337inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338bool
1339is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1340 _ForwardIterator2 __first2)
1341{
1342 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1343 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001344 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345}
1346
Marshall Clow96b42b22013-05-09 21:14:23 +00001347#if _LIBCPP_STD_VER > 11
1348template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow96d050a2018-01-15 16:16:32 +00001349_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Marshall Clow96b42b22013-05-09 21:14:23 +00001350__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001351 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
Marshall Clow96b42b22013-05-09 21:14:23 +00001352 _BinaryPredicate __pred,
1353 forward_iterator_tag, forward_iterator_tag )
1354{
Marshall Clow96d050a2018-01-15 16:16:32 +00001355// shorten sequences as much as possible by lopping of any equal prefix
Eric Fiselier14673892014-10-27 20:26:25 +00001356 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow96b42b22013-05-09 21:14:23 +00001357 if (!__pred(*__first1, *__first2))
Marshall Clow96d050a2018-01-15 16:16:32 +00001358 break;
1359 if (__first1 == __last1)
Marshall Clow01bbbd22018-01-19 18:07:29 +00001360 return __first2 == __last2;
Marshall Clow96d050a2018-01-15 16:16:32 +00001361 else if (__first2 == __last2)
Marshall Clow01bbbd22018-01-19 18:07:29 +00001362 return false;
Marshall Clow96d050a2018-01-15 16:16:32 +00001363
Marshall Clow96b42b22013-05-09 21:14:23 +00001364 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1365 _D1 __l1 = _VSTD::distance(__first1, __last1);
1366
1367 typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
Marshall Clow42b967c2013-05-10 00:16:10 +00001368 _D2 __l2 = _VSTD::distance(__first2, __last2);
Marshall Clow96b42b22013-05-09 21:14:23 +00001369 if (__l1 != __l2)
1370 return false;
1371
1372 // For each element in [f1, l1) see if there are the same number of
1373 // equal elements in [f2, l2)
1374 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1375 {
Marshall Clow96d050a2018-01-15 16:16:32 +00001376 // Have we already counted the number of *__i in [f1, l1)?
Peter Collingbournea5b451b2018-01-26 21:23:27 +00001377 _ForwardIterator1 __match = __first1;
1378 for (; __match != __i; ++__match)
1379 if (__pred(*__match, *__i))
1380 break;
1381 if (__match == __i) {
Marshall Clow96b42b22013-05-09 21:14:23 +00001382 // Count number of *__i in [f2, l2)
1383 _D1 __c2 = 0;
1384 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1385 if (__pred(*__i, *__j))
1386 ++__c2;
1387 if (__c2 == 0)
1388 return false;
1389 // Count number of *__i in [__i, l1) (we can start with 1)
1390 _D1 __c1 = 1;
1391 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1392 if (__pred(*__i, *__j))
1393 ++__c1;
1394 if (__c1 != __c2)
1395 return false;
1396 }
Marshall Clow96b42b22013-05-09 21:14:23 +00001397 }
1398 return true;
1399}
1400
1401template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow96d050a2018-01-15 16:16:32 +00001402_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Marshall Clow96b42b22013-05-09 21:14:23 +00001403__is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001404 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
Marshall Clow96b42b22013-05-09 21:14:23 +00001405 _BinaryPredicate __pred,
1406 random_access_iterator_tag, random_access_iterator_tag )
1407{
1408 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1409 return false;
1410 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1411 typename add_lvalue_reference<_BinaryPredicate>::type>
1412 (__first1, __last1, __first2, __pred );
1413}
1414
1415template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow96d050a2018-01-15 16:16:32 +00001416inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001417bool
1418is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1419 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1420 _BinaryPredicate __pred )
1421{
1422 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1423 (__first1, __last1, __first2, __last2, __pred,
1424 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1425 typename iterator_traits<_ForwardIterator2>::iterator_category());
1426}
1427
1428template<class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow96d050a2018-01-15 16:16:32 +00001429inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001430bool
1431is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1432 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1433{
1434 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1435 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1436 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1437 __equal_to<__v1, __v2>(),
1438 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1439 typename iterator_traits<_ForwardIterator2>::iterator_category());
1440}
1441#endif
1442
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443// search
Marshall Clowa40686b2018-01-08 19:18:00 +00001444// __search is in <functional>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445
1446template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001447inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448_ForwardIterator1
1449search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1450 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1451{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001452 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453 (__first1, __last1, __first2, __last2, __pred,
Marshall Clowaf8be6c2016-03-08 15:12:52 +00001454 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1455 typename iterator_traits<_ForwardIterator2>::iterator_category())
1456 .first;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457}
1458
1459template <class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001460inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461_ForwardIterator1
1462search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1463 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1464{
Marshall Clowaf8be6c2016-03-08 15:12:52 +00001465 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1466 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001467 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001468}
1469
Marshall Clowa40686b2018-01-08 19:18:00 +00001470
1471#if _LIBCPP_STD_VER > 14
1472template <class _ForwardIterator, class _Searcher>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001473_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowa40686b2018-01-08 19:18:00 +00001474_ForwardIterator search(_ForwardIterator __f, _ForwardIterator __l, const _Searcher &__s)
1475{ return __s(__f, __l).first; }
1476#endif
1477
Howard Hinnantc51e1022010-05-11 19:42:16 +00001478// search_n
1479
1480template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001481_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001482__search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnantbf074022011-10-22 20:59:45 +00001483 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001484{
1485 if (__count <= 0)
1486 return __first;
1487 while (true)
1488 {
Howard Hinnantbf074022011-10-22 20:59:45 +00001489 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490 while (true)
1491 {
Howard Hinnantbf074022011-10-22 20:59:45 +00001492 if (__first == __last) // return __last if no element matches __value_
Howard Hinnantc51e1022010-05-11 19:42:16 +00001493 return __last;
Howard Hinnantbf074022011-10-22 20:59:45 +00001494 if (__pred(*__first, __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001495 break;
1496 ++__first;
1497 }
Howard Hinnantbf074022011-10-22 20:59:45 +00001498 // *__first matches __value_, now match elements after here
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499 _ForwardIterator __m = __first;
1500 _Size __c(0);
1501 while (true)
1502 {
1503 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1504 return __first;
1505 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1506 return __last;
Howard Hinnantbf074022011-10-22 20:59:45 +00001507 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantc51e1022010-05-11 19:42:16 +00001508 {
1509 __first = __m;
1510 ++__first;
1511 break;
1512 } // else there is a match, check next elements
1513 }
1514 }
1515}
1516
1517template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001518_LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001519__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnantbf074022011-10-22 20:59:45 +00001520 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001521{
1522 if (__count <= 0)
1523 return __first;
1524 _Size __len = static_cast<_Size>(__last - __first);
1525 if (__len < __count)
1526 return __last;
1527 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1528 while (true)
1529 {
Howard Hinnantbf074022011-10-22 20:59:45 +00001530 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531 while (true)
1532 {
Howard Hinnantbede4c32013-04-04 15:40:48 +00001533 if (__first >= __s) // return __last if no element matches __value_
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534 return __last;
Howard Hinnantbf074022011-10-22 20:59:45 +00001535 if (__pred(*__first, __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536 break;
1537 ++__first;
1538 }
Howard Hinnantbf074022011-10-22 20:59:45 +00001539 // *__first matches __value_, now match elements after here
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540 _RandomAccessIterator __m = __first;
1541 _Size __c(0);
1542 while (true)
1543 {
1544 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1545 return __first;
1546 ++__m; // no need to check range on __m because __s guarantees we have enough source
Howard Hinnantbf074022011-10-22 20:59:45 +00001547 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548 {
1549 __first = __m;
1550 ++__first;
1551 break;
1552 } // else there is a match, check next elements
1553 }
1554 }
1555}
1556
1557template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001558inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559_ForwardIterator
1560search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnantbf074022011-10-22 20:59:45 +00001561 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001563 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001564 (__first, __last, __convert_to_integral(__count), __value_, __pred,
1565 typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001566}
1567
1568template <class _ForwardIterator, class _Size, class _Tp>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001569inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00001571search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001572{
1573 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001574 return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
1575 __value_, __equal_to<__v, _Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576}
1577
1578// copy
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579template <class _Iter>
1580inline _LIBCPP_INLINE_VISIBILITY
1581_Iter
1582__unwrap_iter(_Iter __i)
1583{
1584 return __i;
1585}
1586
1587template <class _Tp>
Marshall Clow88880de2017-05-25 14:20:26 +00001588inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589typename enable_if
1590<
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001591 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592 _Tp*
1593>::type
1594__unwrap_iter(move_iterator<_Tp*> __i)
1595{
1596 return __i.base();
1597}
1598
Howard Hinnant8ea98242013-08-23 17:37:05 +00001599#if _LIBCPP_DEBUG_LEVEL < 2
1600
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601template <class _Tp>
Marshall Clowae4f8312018-07-13 16:35:26 +00001602inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001603typename enable_if
1604<
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001605 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001606 _Tp*
1607>::type
1608__unwrap_iter(__wrap_iter<_Tp*> __i)
1609{
1610 return __i.base();
1611}
1612
Marshall Clowbae96ac2019-02-06 16:10:25 +00001613template <class _Tp>
1614inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1615typename enable_if
1616<
1617 is_trivially_copy_assignable<_Tp>::value,
1618 const _Tp*
1619>::type
1620__unwrap_iter(__wrap_iter<const _Tp*> __i)
1621{
1622 return __i.base();
1623}
1624
Eric Fiselier38badb82016-12-28 05:35:32 +00001625#else
1626
1627template <class _Tp>
Marshall Clowae4f8312018-07-13 16:35:26 +00001628inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Eric Fiselier38badb82016-12-28 05:35:32 +00001629typename enable_if
1630<
1631 is_trivially_copy_assignable<_Tp>::value,
1632 __wrap_iter<_Tp*>
1633>::type
1634__unwrap_iter(__wrap_iter<_Tp*> __i)
1635{
1636 return __i;
1637}
1638
Howard Hinnant8ea98242013-08-23 17:37:05 +00001639#endif // _LIBCPP_DEBUG_LEVEL < 2
1640
Howard Hinnantc51e1022010-05-11 19:42:16 +00001641template <class _InputIterator, class _OutputIterator>
1642inline _LIBCPP_INLINE_VISIBILITY
1643_OutputIterator
1644__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1645{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001646 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001647 *__result = *__first;
1648 return __result;
1649}
1650
1651template <class _Tp, class _Up>
1652inline _LIBCPP_INLINE_VISIBILITY
1653typename enable_if
1654<
1655 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001656 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001657 _Up*
1658>::type
1659__copy(_Tp* __first, _Tp* __last, _Up* __result)
1660{
1661 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clowc3ef9622015-06-02 13:52:16 +00001662 if (__n > 0)
1663 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001664 return __result + __n;
1665}
1666
1667template <class _InputIterator, class _OutputIterator>
1668inline _LIBCPP_INLINE_VISIBILITY
1669_OutputIterator
1670copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1671{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001672 return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001673}
1674
1675// copy_backward
1676
Howard Hinnant7f229bc2013-02-06 21:03:39 +00001677template <class _BidirectionalIterator, class _OutputIterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001678inline _LIBCPP_INLINE_VISIBILITY
1679_OutputIterator
Howard Hinnant7f229bc2013-02-06 21:03:39 +00001680__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001681{
1682 while (__first != __last)
1683 *--__result = *--__last;
1684 return __result;
1685}
1686
1687template <class _Tp, class _Up>
1688inline _LIBCPP_INLINE_VISIBILITY
1689typename enable_if
1690<
1691 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001692 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001693 _Up*
1694>::type
1695__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1696{
1697 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clowc3ef9622015-06-02 13:52:16 +00001698 if (__n > 0)
1699 {
1700 __result -= __n;
1701 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1702 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703 return __result;
1704}
1705
1706template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1707inline _LIBCPP_INLINE_VISIBILITY
1708_BidirectionalIterator2
1709copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1710 _BidirectionalIterator2 __result)
1711{
Eric Fiselier6003c772016-12-23 23:37:52 +00001712 return _VSTD::__copy_backward(__unwrap_iter(__first),
1713 __unwrap_iter(__last),
1714 __unwrap_iter(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715}
1716
1717// copy_if
1718
1719template<class _InputIterator, class _OutputIterator, class _Predicate>
1720inline _LIBCPP_INLINE_VISIBILITY
1721_OutputIterator
1722copy_if(_InputIterator __first, _InputIterator __last,
1723 _OutputIterator __result, _Predicate __pred)
1724{
1725 for (; __first != __last; ++__first)
1726 {
1727 if (__pred(*__first))
1728 {
1729 *__result = *__first;
1730 ++__result;
1731 }
1732 }
1733 return __result;
1734}
1735
1736// copy_n
1737
1738template<class _InputIterator, class _Size, class _OutputIterator>
1739inline _LIBCPP_INLINE_VISIBILITY
1740typename enable_if
1741<
1742 __is_input_iterator<_InputIterator>::value &&
1743 !__is_random_access_iterator<_InputIterator>::value,
1744 _OutputIterator
1745>::type
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001746copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001747{
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001748 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1749 _IntegralSize __n = __orig_n;
Howard Hinnantcbc5dc02011-02-27 20:55:39 +00001750 if (__n > 0)
1751 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752 *__result = *__first;
Howard Hinnantcbc5dc02011-02-27 20:55:39 +00001753 ++__result;
1754 for (--__n; __n > 0; --__n)
1755 {
1756 ++__first;
1757 *__result = *__first;
1758 ++__result;
1759 }
1760 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761 return __result;
1762}
1763
1764template<class _InputIterator, class _Size, class _OutputIterator>
1765inline _LIBCPP_INLINE_VISIBILITY
1766typename enable_if
1767<
1768 __is_random_access_iterator<_InputIterator>::value,
1769 _OutputIterator
1770>::type
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001771copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001772{
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001773 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1774 _IntegralSize __n = __orig_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001775 return _VSTD::copy(__first, __first + __n, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776}
1777
1778// move
1779
1780template <class _InputIterator, class _OutputIterator>
1781inline _LIBCPP_INLINE_VISIBILITY
1782_OutputIterator
1783__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1784{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001785 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001786 *__result = _VSTD::move(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001787 return __result;
1788}
1789
1790template <class _Tp, class _Up>
1791inline _LIBCPP_INLINE_VISIBILITY
1792typename enable_if
1793<
1794 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001795 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796 _Up*
1797>::type
1798__move(_Tp* __first, _Tp* __last, _Up* __result)
1799{
1800 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clowc3ef9622015-06-02 13:52:16 +00001801 if (__n > 0)
1802 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001803 return __result + __n;
1804}
1805
1806template <class _InputIterator, class _OutputIterator>
1807inline _LIBCPP_INLINE_VISIBILITY
1808_OutputIterator
1809move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1810{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001811 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812}
1813
1814// move_backward
1815
1816template <class _InputIterator, class _OutputIterator>
1817inline _LIBCPP_INLINE_VISIBILITY
1818_OutputIterator
1819__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1820{
1821 while (__first != __last)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001822 *--__result = _VSTD::move(*--__last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001823 return __result;
1824}
1825
1826template <class _Tp, class _Up>
1827inline _LIBCPP_INLINE_VISIBILITY
1828typename enable_if
1829<
1830 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001831 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001832 _Up*
1833>::type
1834__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1835{
1836 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clowc3ef9622015-06-02 13:52:16 +00001837 if (__n > 0)
1838 {
1839 __result -= __n;
1840 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1841 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001842 return __result;
1843}
1844
1845template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1846inline _LIBCPP_INLINE_VISIBILITY
1847_BidirectionalIterator2
1848move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1849 _BidirectionalIterator2 __result)
1850{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001851 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001852}
1853
1854// iter_swap
1855
Howard Hinnantdbfd4b42011-05-27 15:04:19 +00001856// moved to <type_traits> for better swap / noexcept support
Howard Hinnantc51e1022010-05-11 19:42:16 +00001857
1858// transform
1859
1860template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
Marshall Clow31427c62018-01-19 17:45:39 +00001861inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001862_OutputIterator
1863transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1864{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001865 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001866 *__result = __op(*__first);
1867 return __result;
1868}
1869
1870template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
Marshall Clow31427c62018-01-19 17:45:39 +00001871inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872_OutputIterator
1873transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1874 _OutputIterator __result, _BinaryOperation __binary_op)
1875{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001876 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001877 *__result = __binary_op(*__first1, *__first2);
1878 return __result;
1879}
1880
1881// replace
1882
1883template <class _ForwardIterator, class _Tp>
Marshall Clow01bbbd22018-01-19 18:07:29 +00001884inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001885void
1886replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1887{
1888 for (; __first != __last; ++__first)
1889 if (*__first == __old_value)
1890 *__first = __new_value;
1891}
1892
1893// replace_if
1894
1895template <class _ForwardIterator, class _Predicate, class _Tp>
Marshall Clow01bbbd22018-01-19 18:07:29 +00001896inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001897void
1898replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1899{
1900 for (; __first != __last; ++__first)
1901 if (__pred(*__first))
1902 *__first = __new_value;
1903}
1904
1905// replace_copy
1906
1907template <class _InputIterator, class _OutputIterator, class _Tp>
Marshall Clow01bbbd22018-01-19 18:07:29 +00001908inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001909_OutputIterator
1910replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1911 const _Tp& __old_value, const _Tp& __new_value)
1912{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001913 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914 if (*__first == __old_value)
1915 *__result = __new_value;
1916 else
1917 *__result = *__first;
1918 return __result;
1919}
1920
1921// replace_copy_if
1922
1923template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
Marshall Clow01bbbd22018-01-19 18:07:29 +00001924inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001925_OutputIterator
1926replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1927 _Predicate __pred, const _Tp& __new_value)
1928{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001929 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930 if (__pred(*__first))
1931 *__result = __new_value;
1932 else
1933 *__result = *__first;
1934 return __result;
1935}
1936
1937// fill_n
1938
1939template <class _OutputIterator, class _Size, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001940inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001941_OutputIterator
Howard Hinnant0ad1c122013-08-01 17:29:28 +00001942__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001943{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001944 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnantbf074022011-10-22 20:59:45 +00001945 *__first = __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001946 return __first;
1947}
1948
Howard Hinnantc51e1022010-05-11 19:42:16 +00001949template <class _OutputIterator, class _Size, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001950inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001951_OutputIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00001952fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001953{
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001954 return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955}
1956
1957// fill
1958
1959template <class _ForwardIterator, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001960inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001961void
Howard Hinnantbf074022011-10-22 20:59:45 +00001962__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001963{
1964 for (; __first != __last; ++__first)
Howard Hinnantbf074022011-10-22 20:59:45 +00001965 *__first = __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001966}
1967
1968template <class _RandomAccessIterator, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001969inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001970void
Howard Hinnantbf074022011-10-22 20:59:45 +00001971__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001972{
Howard Hinnantbf074022011-10-22 20:59:45 +00001973 _VSTD::fill_n(__first, __last - __first, __value_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001974}
1975
1976template <class _ForwardIterator, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001977inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001978void
Howard Hinnantbf074022011-10-22 20:59:45 +00001979fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001980{
Howard Hinnantbf074022011-10-22 20:59:45 +00001981 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001982}
1983
1984// generate
1985
1986template <class _ForwardIterator, class _Generator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001987inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001988void
1989generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
1990{
1991 for (; __first != __last; ++__first)
1992 *__first = __gen();
1993}
1994
1995// generate_n
1996
1997template <class _OutputIterator, class _Size, class _Generator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001998inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999_OutputIterator
Eric Fiselier97ec07d2015-02-10 16:46:42 +00002000generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002001{
Eric Fiselier97ec07d2015-02-10 16:46:42 +00002002 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
2003 _IntegralSize __n = __orig_n;
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002004 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002005 *__first = __gen();
2006 return __first;
2007}
2008
2009// remove
2010
2011template <class _ForwardIterator, class _Tp>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00002012_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00002013remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002014{
Howard Hinnantbf074022011-10-22 20:59:45 +00002015 __first = _VSTD::find(__first, __last, __value_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002016 if (__first != __last)
2017 {
2018 _ForwardIterator __i = __first;
2019 while (++__i != __last)
2020 {
Howard Hinnantbf074022011-10-22 20:59:45 +00002021 if (!(*__i == __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002022 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002023 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002024 ++__first;
2025 }
2026 }
2027 }
2028 return __first;
2029}
2030
2031// remove_if
2032
2033template <class _ForwardIterator, class _Predicate>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00002034_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002035remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2036{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002037 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002038 (__first, __last, __pred);
2039 if (__first != __last)
2040 {
2041 _ForwardIterator __i = __first;
2042 while (++__i != __last)
2043 {
2044 if (!__pred(*__i))
2045 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002046 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002047 ++__first;
2048 }
2049 }
2050 }
2051 return __first;
2052}
2053
2054// remove_copy
2055
2056template <class _InputIterator, class _OutputIterator, class _Tp>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00002057inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002058_OutputIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00002059remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002060{
2061 for (; __first != __last; ++__first)
2062 {
Howard Hinnantbf074022011-10-22 20:59:45 +00002063 if (!(*__first == __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002064 {
2065 *__result = *__first;
2066 ++__result;
2067 }
2068 }
2069 return __result;
2070}
2071
2072// remove_copy_if
2073
2074template <class _InputIterator, class _OutputIterator, class _Predicate>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00002075inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002076_OutputIterator
2077remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2078{
2079 for (; __first != __last; ++__first)
2080 {
2081 if (!__pred(*__first))
2082 {
2083 *__result = *__first;
2084 ++__result;
2085 }
2086 }
2087 return __result;
2088}
2089
2090// unique
2091
2092template <class _ForwardIterator, class _BinaryPredicate>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002093_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002094unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2095{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002096 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002097 (__first, __last, __pred);
2098 if (__first != __last)
2099 {
2100 // ... a a ? ...
2101 // f i
2102 _ForwardIterator __i = __first;
2103 for (++__i; ++__i != __last;)
2104 if (!__pred(*__first, *__i))
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002105 *++__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106 ++__first;
2107 }
2108 return __first;
2109}
2110
2111template <class _ForwardIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002112inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113_ForwardIterator
2114unique(_ForwardIterator __first, _ForwardIterator __last)
2115{
2116 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002117 return _VSTD::unique(__first, __last, __equal_to<__v>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002118}
2119
2120// unique_copy
2121
2122template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002123_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2125 input_iterator_tag, output_iterator_tag)
2126{
2127 if (__first != __last)
2128 {
2129 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2130 *__result = __t;
2131 ++__result;
2132 while (++__first != __last)
2133 {
2134 if (!__pred(__t, *__first))
2135 {
2136 __t = *__first;
2137 *__result = __t;
2138 ++__result;
2139 }
2140 }
2141 }
2142 return __result;
2143}
2144
2145template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002146_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2148 forward_iterator_tag, output_iterator_tag)
2149{
2150 if (__first != __last)
2151 {
2152 _ForwardIterator __i = __first;
2153 *__result = *__i;
2154 ++__result;
2155 while (++__first != __last)
2156 {
2157 if (!__pred(*__i, *__first))
2158 {
2159 *__result = *__first;
2160 ++__result;
2161 __i = __first;
2162 }
2163 }
2164 }
2165 return __result;
2166}
2167
2168template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002169_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2171 input_iterator_tag, forward_iterator_tag)
2172{
2173 if (__first != __last)
2174 {
2175 *__result = *__first;
2176 while (++__first != __last)
2177 if (!__pred(*__result, *__first))
2178 *++__result = *__first;
2179 ++__result;
2180 }
2181 return __result;
2182}
2183
Howard Hinnantc51e1022010-05-11 19:42:16 +00002184template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
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, _BinaryPredicate __pred)
2188{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002189 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190 (__first, __last, __result, __pred,
2191 typename iterator_traits<_InputIterator>::iterator_category(),
2192 typename iterator_traits<_OutputIterator>::iterator_category());
2193}
2194
2195template <class _InputIterator, class _OutputIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002196inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197_OutputIterator
2198unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2199{
2200 typedef typename iterator_traits<_InputIterator>::value_type __v;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002201 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002202}
2203
2204// reverse
2205
2206template <class _BidirectionalIterator>
2207inline _LIBCPP_INLINE_VISIBILITY
2208void
2209__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2210{
2211 while (__first != __last)
2212 {
2213 if (__first == --__last)
2214 break;
Marshall Clow2ad71042015-11-02 21:34:25 +00002215 _VSTD::iter_swap(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216 ++__first;
2217 }
2218}
2219
2220template <class _RandomAccessIterator>
2221inline _LIBCPP_INLINE_VISIBILITY
2222void
2223__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2224{
2225 if (__first != __last)
2226 for (; __first < --__last; ++__first)
Marshall Clow2ad71042015-11-02 21:34:25 +00002227 _VSTD::iter_swap(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228}
2229
2230template <class _BidirectionalIterator>
2231inline _LIBCPP_INLINE_VISIBILITY
2232void
2233reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2234{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002235 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236}
2237
2238// reverse_copy
2239
2240template <class _BidirectionalIterator, class _OutputIterator>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00002241inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242_OutputIterator
2243reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2244{
2245 for (; __first != __last; ++__result)
2246 *__result = *--__last;
2247 return __result;
2248}
2249
2250// rotate
2251
2252template <class _ForwardIterator>
2253_ForwardIterator
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002254__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255{
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002256 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2257 value_type __tmp = _VSTD::move(*__first);
2258 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2259 *__lm1 = _VSTD::move(__tmp);
2260 return __lm1;
2261}
2262
2263template <class _BidirectionalIterator>
2264_BidirectionalIterator
2265__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2266{
2267 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2268 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2269 value_type __tmp = _VSTD::move(*__lm1);
2270 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2271 *__first = _VSTD::move(__tmp);
2272 return __fp1;
2273}
2274
2275template <class _ForwardIterator>
2276_ForwardIterator
2277__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2278{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002279 _ForwardIterator __i = __middle;
2280 while (true)
2281 {
2282 swap(*__first, *__i);
2283 ++__first;
2284 if (++__i == __last)
2285 break;
2286 if (__first == __middle)
2287 __middle = __i;
2288 }
2289 _ForwardIterator __r = __first;
2290 if (__first != __middle)
2291 {
2292 __i = __middle;
2293 while (true)
2294 {
2295 swap(*__first, *__i);
2296 ++__first;
2297 if (++__i == __last)
2298 {
2299 if (__first == __middle)
2300 break;
2301 __i = __middle;
2302 }
2303 else if (__first == __middle)
2304 __middle = __i;
2305 }
2306 }
2307 return __r;
2308}
2309
2310template<typename _Integral>
2311inline _LIBCPP_INLINE_VISIBILITY
2312_Integral
Marshall Clowb8bfc2c2016-07-26 14:29:45 +00002313__algo_gcd(_Integral __x, _Integral __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002314{
2315 do
2316 {
2317 _Integral __t = __x % __y;
2318 __x = __y;
2319 __y = __t;
2320 } while (__y);
2321 return __x;
2322}
2323
2324template<typename _RandomAccessIterator>
2325_RandomAccessIterator
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002326__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002327{
2328 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2329 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002330
Howard Hinnantc51e1022010-05-11 19:42:16 +00002331 const difference_type __m1 = __middle - __first;
2332 const difference_type __m2 = __last - __middle;
2333 if (__m1 == __m2)
2334 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002335 _VSTD::swap_ranges(__first, __middle, __middle);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002336 return __middle;
2337 }
Marshall Clowb8bfc2c2016-07-26 14:29:45 +00002338 const difference_type __g = _VSTD::__algo_gcd(__m1, __m2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002339 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2340 {
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002341 value_type __t(_VSTD::move(*--__p));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002342 _RandomAccessIterator __p1 = __p;
2343 _RandomAccessIterator __p2 = __p1 + __m1;
2344 do
2345 {
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002346 *__p1 = _VSTD::move(*__p2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002347 __p1 = __p2;
2348 const difference_type __d = __last - __p2;
2349 if (__m1 < __d)
2350 __p2 += __m1;
2351 else
2352 __p2 = __first + (__m1 - __d);
2353 } while (__p2 != __p);
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002354 *__p1 = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002355 }
2356 return __first + __m2;
2357}
2358
2359template <class _ForwardIterator>
2360inline _LIBCPP_INLINE_VISIBILITY
2361_ForwardIterator
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002362__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2363 _VSTD::forward_iterator_tag)
2364{
2365 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2366 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2367 {
2368 if (_VSTD::next(__first) == __middle)
2369 return _VSTD::__rotate_left(__first, __last);
2370 }
2371 return _VSTD::__rotate_forward(__first, __middle, __last);
2372}
2373
2374template <class _BidirectionalIterator>
2375inline _LIBCPP_INLINE_VISIBILITY
2376_BidirectionalIterator
2377__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2378 _VSTD::bidirectional_iterator_tag)
2379{
2380 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2381 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2382 {
2383 if (_VSTD::next(__first) == __middle)
2384 return _VSTD::__rotate_left(__first, __last);
2385 if (_VSTD::next(__middle) == __last)
2386 return _VSTD::__rotate_right(__first, __last);
2387 }
2388 return _VSTD::__rotate_forward(__first, __middle, __last);
2389}
2390
2391template <class _RandomAccessIterator>
2392inline _LIBCPP_INLINE_VISIBILITY
2393_RandomAccessIterator
2394__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2395 _VSTD::random_access_iterator_tag)
2396{
2397 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2398 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2399 {
2400 if (_VSTD::next(__first) == __middle)
2401 return _VSTD::__rotate_left(__first, __last);
2402 if (_VSTD::next(__middle) == __last)
2403 return _VSTD::__rotate_right(__first, __last);
2404 return _VSTD::__rotate_gcd(__first, __middle, __last);
2405 }
2406 return _VSTD::__rotate_forward(__first, __middle, __last);
2407}
2408
2409template <class _ForwardIterator>
2410inline _LIBCPP_INLINE_VISIBILITY
2411_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002412rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2413{
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002414 if (__first == __middle)
2415 return __last;
2416 if (__middle == __last)
2417 return __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002418 return _VSTD::__rotate(__first, __middle, __last,
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002419 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002420}
2421
2422// rotate_copy
2423
2424template <class _ForwardIterator, class _OutputIterator>
2425inline _LIBCPP_INLINE_VISIBILITY
2426_OutputIterator
2427rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2428{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002429 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002430}
2431
Howard Hinnantc51e1022010-05-11 19:42:16 +00002432// min_element
2433
2434template <class _ForwardIterator, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002435inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002436_ForwardIterator
Marshall Clow9e173072015-05-10 13:53:31 +00002437min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002438{
Eric Fiselierf8ecd942018-08-22 17:47:13 +00002439 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2440 "std::min_element requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002441 if (__first != __last)
2442 {
2443 _ForwardIterator __i = __first;
2444 while (++__i != __last)
2445 if (__comp(*__i, *__first))
2446 __first = __i;
2447 }
2448 return __first;
2449}
2450
2451template <class _ForwardIterator>
Marshall Clow9e173072015-05-10 13:53:31 +00002452inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002453_ForwardIterator
2454min_element(_ForwardIterator __first, _ForwardIterator __last)
2455{
Marshall Clow9e173072015-05-10 13:53:31 +00002456 return _VSTD::min_element(__first, __last,
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002457 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2458}
2459
2460// min
2461
2462template <class _Tp, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002463inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002464const _Tp&
2465min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2466{
2467 return __comp(__b, __a) ? __b : __a;
2468}
2469
2470template <class _Tp>
Marshall Clowe9dca072014-02-19 16:51:35 +00002471inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002472const _Tp&
2473min(const _Tp& __a, const _Tp& __b)
2474{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002475 return _VSTD::min(__a, __b, __less<_Tp>());
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002476}
2477
Eric Fiselier93dd1372017-04-18 23:26:47 +00002478#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002479
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002480template<class _Tp, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002481inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002482_Tp
2483min(initializer_list<_Tp> __t, _Compare __comp)
2484{
Marshall Clow9e173072015-05-10 13:53:31 +00002485 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002486}
2487
2488template<class _Tp>
Marshall Clowe9dca072014-02-19 16:51:35 +00002489inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002490_Tp
2491min(initializer_list<_Tp> __t)
2492{
Marshall Clow9e173072015-05-10 13:53:31 +00002493 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002494}
2495
Eric Fiselier93dd1372017-04-18 23:26:47 +00002496#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002497
Howard Hinnantc51e1022010-05-11 19:42:16 +00002498// max_element
2499
2500template <class _ForwardIterator, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002501inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002502_ForwardIterator
Marshall Clow9e173072015-05-10 13:53:31 +00002503max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002504{
Eric Fiselierf8ecd942018-08-22 17:47:13 +00002505 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2506 "std::max_element requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002507 if (__first != __last)
2508 {
2509 _ForwardIterator __i = __first;
2510 while (++__i != __last)
2511 if (__comp(*__first, *__i))
2512 __first = __i;
2513 }
2514 return __first;
2515}
2516
Marshall Clowe9dca072014-02-19 16:51:35 +00002517
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518template <class _ForwardIterator>
Marshall Clow9e173072015-05-10 13:53:31 +00002519inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002520_ForwardIterator
2521max_element(_ForwardIterator __first, _ForwardIterator __last)
2522{
Marshall Clow9e173072015-05-10 13:53:31 +00002523 return _VSTD::max_element(__first, __last,
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002524 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2525}
2526
2527// max
2528
2529template <class _Tp, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002530inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002531const _Tp&
2532max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2533{
2534 return __comp(__a, __b) ? __b : __a;
2535}
2536
2537template <class _Tp>
Marshall Clowe9dca072014-02-19 16:51:35 +00002538inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002539const _Tp&
2540max(const _Tp& __a, const _Tp& __b)
2541{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002542 return _VSTD::max(__a, __b, __less<_Tp>());
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002543}
2544
Eric Fiselier93dd1372017-04-18 23:26:47 +00002545#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002546
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002547template<class _Tp, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002548inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002549_Tp
2550max(initializer_list<_Tp> __t, _Compare __comp)
2551{
Marshall Clow9e173072015-05-10 13:53:31 +00002552 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002553}
2554
2555template<class _Tp>
Marshall Clowe9dca072014-02-19 16:51:35 +00002556inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002557_Tp
2558max(initializer_list<_Tp> __t)
2559{
Marshall Clow9e173072015-05-10 13:53:31 +00002560 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561}
2562
Eric Fiselier93dd1372017-04-18 23:26:47 +00002563#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002564
Marshall Clow3e18d0e2016-03-07 22:43:49 +00002565#if _LIBCPP_STD_VER > 14
2566// clamp
2567template<class _Tp, class _Compare>
2568inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2569const _Tp&
2570clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
2571{
2572 _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
2573 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
2574
2575}
2576
2577template<class _Tp>
2578inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2579const _Tp&
2580clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
2581{
2582 return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
2583}
2584#endif
2585
Howard Hinnantc51e1022010-05-11 19:42:16 +00002586// minmax_element
2587
2588template <class _ForwardIterator, class _Compare>
Marshall Clow9e173072015-05-10 13:53:31 +00002589_LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590std::pair<_ForwardIterator, _ForwardIterator>
2591minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2592{
Eric Fiselierf8ecd942018-08-22 17:47:13 +00002593 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2594 "std::minmax_element requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002595 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2596 if (__first != __last)
2597 {
2598 if (++__first != __last)
2599 {
2600 if (__comp(*__first, *__result.first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002601 __result.first = __first;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002602 else
2603 __result.second = __first;
2604 while (++__first != __last)
2605 {
2606 _ForwardIterator __i = __first;
2607 if (++__first == __last)
2608 {
2609 if (__comp(*__i, *__result.first))
2610 __result.first = __i;
2611 else if (!__comp(*__i, *__result.second))
2612 __result.second = __i;
2613 break;
2614 }
2615 else
2616 {
2617 if (__comp(*__first, *__i))
2618 {
2619 if (__comp(*__first, *__result.first))
2620 __result.first = __first;
2621 if (!__comp(*__i, *__result.second))
2622 __result.second = __i;
2623 }
2624 else
2625 {
2626 if (__comp(*__i, *__result.first))
2627 __result.first = __i;
2628 if (!__comp(*__first, *__result.second))
2629 __result.second = __first;
2630 }
2631 }
2632 }
2633 }
2634 }
2635 return __result;
2636}
2637
2638template <class _ForwardIterator>
Marshall Clow9e173072015-05-10 13:53:31 +00002639inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002640std::pair<_ForwardIterator, _ForwardIterator>
2641minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2642{
Marshall Clowe9dca072014-02-19 16:51:35 +00002643 return _VSTD::minmax_element(__first, __last,
2644 __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002645}
2646
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002647// minmax
2648
2649template<class _Tp, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002650inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002651pair<const _Tp&, const _Tp&>
2652minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2653{
2654 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2655 pair<const _Tp&, const _Tp&>(__a, __b);
2656}
2657
2658template<class _Tp>
Marshall Clowe9dca072014-02-19 16:51:35 +00002659inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002660pair<const _Tp&, const _Tp&>
2661minmax(const _Tp& __a, const _Tp& __b)
2662{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002663 return _VSTD::minmax(__a, __b, __less<_Tp>());
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002664}
2665
Eric Fiselier93dd1372017-04-18 23:26:47 +00002666#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002667
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002668template<class _Tp, class _Compare>
Marshall Clowe9dca072014-02-19 16:51:35 +00002669inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002670pair<_Tp, _Tp>
2671minmax(initializer_list<_Tp> __t, _Compare __comp)
2672{
Marshall Clowe9dca072014-02-19 16:51:35 +00002673 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2674 _Iter __first = __t.begin();
2675 _Iter __last = __t.end();
Marshall Clow447713a2015-02-11 15:41:34 +00002676 std::pair<_Tp, _Tp> __result(*__first, *__first);
Marshall Clowe9dca072014-02-19 16:51:35 +00002677
2678 ++__first;
2679 if (__t.size() % 2 == 0)
2680 {
2681 if (__comp(*__first, __result.first))
2682 __result.first = *__first;
2683 else
2684 __result.second = *__first;
2685 ++__first;
2686 }
Aditya Kumar3a0179a2016-08-25 11:52:38 +00002687
Marshall Clowe9dca072014-02-19 16:51:35 +00002688 while (__first != __last)
2689 {
2690 _Tp __prev = *__first++;
Marshall Clow447713a2015-02-11 15:41:34 +00002691 if (__comp(*__first, __prev)) {
2692 if ( __comp(*__first, __result.first)) __result.first = *__first;
2693 if (!__comp(__prev, __result.second)) __result.second = __prev;
Marshall Clowe9dca072014-02-19 16:51:35 +00002694 }
2695 else {
Marshall Clow447713a2015-02-11 15:41:34 +00002696 if ( __comp(__prev, __result.first)) __result.first = __prev;
2697 if (!__comp(*__first, __result.second)) __result.second = *__first;
Marshall Clowe9dca072014-02-19 16:51:35 +00002698 }
Aditya Kumar3a0179a2016-08-25 11:52:38 +00002699
Marshall Clowe9dca072014-02-19 16:51:35 +00002700 __first++;
2701 }
2702 return __result;
2703}
2704
2705template<class _Tp>
2706inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2707pair<_Tp, _Tp>
2708minmax(initializer_list<_Tp> __t)
2709{
2710 return _VSTD::minmax(__t, __less<_Tp>());
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002711}
2712
Eric Fiselier93dd1372017-04-18 23:26:47 +00002713#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002714
Howard Hinnantc51e1022010-05-11 19:42:16 +00002715// random_shuffle
2716
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002717// __independent_bits_engine
2718
Howard Hinnantc834c512011-11-29 18:15:50 +00002719template <unsigned long long _Xp, size_t _Rp>
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002720struct __log2_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00002721{
Howard Hinnantc834c512011-11-29 18:15:50 +00002722 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2723 : __log2_imp<_Xp, _Rp - 1>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002724};
2725
Howard Hinnantc834c512011-11-29 18:15:50 +00002726template <unsigned long long _Xp>
2727struct __log2_imp<_Xp, 0>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002728{
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002729 static const size_t value = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002730};
2731
Howard Hinnantc834c512011-11-29 18:15:50 +00002732template <size_t _Rp>
2733struct __log2_imp<0, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002734{
Howard Hinnantc834c512011-11-29 18:15:50 +00002735 static const size_t value = _Rp + 1;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002736};
2737
Eric Fiselier4638fca2017-05-31 21:20:18 +00002738template <class _UIntType, _UIntType _Xp>
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002739struct __log2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002740{
Howard Hinnantc834c512011-11-29 18:15:50 +00002741 static const size_t value = __log2_imp<_Xp,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002742 sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002743};
2744
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002745template<class _Engine, class _UIntType>
2746class __independent_bits_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00002747{
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002748public:
2749 // types
2750 typedef _UIntType result_type;
2751
2752private:
2753 typedef typename _Engine::result_type _Engine_result_type;
2754 typedef typename conditional
2755 <
2756 sizeof(_Engine_result_type) <= sizeof(result_type),
2757 result_type,
2758 _Engine_result_type
2759 >::type _Working_result_type;
2760
2761 _Engine& __e_;
2762 size_t __w_;
2763 size_t __w0_;
2764 size_t __n_;
2765 size_t __n0_;
2766 _Working_result_type __y0_;
2767 _Working_result_type __y1_;
2768 _Engine_result_type __mask0_;
2769 _Engine_result_type __mask1_;
2770
Eric Fiselier93dd1372017-04-18 23:26:47 +00002771#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc834c512011-11-29 18:15:50 +00002772 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant5a646852012-04-02 21:00:45 +00002773 + _Working_result_type(1);
2774#else
2775 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2776 + _Working_result_type(1);
2777#endif
2778 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2779 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2780 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002781
2782public:
2783 // constructors and seeding functions
2784 __independent_bits_engine(_Engine& __e, size_t __w);
2785
2786 // generating functions
Howard Hinnantc834c512011-11-29 18:15:50 +00002787 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002788
2789private:
Marshall Clowfe778582017-09-20 19:38:43 +00002790 result_type __eval(false_type);
2791 result_type __eval(true_type);
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002792};
2793
2794template<class _Engine, class _UIntType>
2795__independent_bits_engine<_Engine, _UIntType>
2796 ::__independent_bits_engine(_Engine& __e, size_t __w)
2797 : __e_(__e),
2798 __w_(__w)
2799{
2800 __n_ = __w_ / __m + (__w_ % __m != 0);
2801 __w0_ = __w_ / __n_;
Howard Hinnantc834c512011-11-29 18:15:50 +00002802 if (_Rp == 0)
2803 __y0_ = _Rp;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002804 else if (__w0_ < _WDt)
Howard Hinnantc834c512011-11-29 18:15:50 +00002805 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002806 else
2807 __y0_ = 0;
Howard Hinnantc834c512011-11-29 18:15:50 +00002808 if (_Rp - __y0_ > __y0_ / __n_)
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002809 {
2810 ++__n_;
2811 __w0_ = __w_ / __n_;
2812 if (__w0_ < _WDt)
Howard Hinnantc834c512011-11-29 18:15:50 +00002813 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002814 else
2815 __y0_ = 0;
2816 }
2817 __n0_ = __n_ - __w_ % __n_;
2818 if (__w0_ < _WDt - 1)
Howard Hinnantc834c512011-11-29 18:15:50 +00002819 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002820 else
2821 __y1_ = 0;
2822 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2823 _Engine_result_type(0);
2824 __mask1_ = __w0_ < _EDt - 1 ?
2825 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2826 _Engine_result_type(~0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002827}
2828
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002829template<class _Engine, class _UIntType>
2830inline
2831_UIntType
Marshall Clowfe778582017-09-20 19:38:43 +00002832__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002833{
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002834 return static_cast<result_type>(__e_() & __mask0_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002835}
2836
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002837template<class _Engine, class _UIntType>
2838_UIntType
Marshall Clowfe778582017-09-20 19:38:43 +00002839__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002840{
Marshall Clowafc48592017-09-20 17:34:11 +00002841 const size_t _WRt = numeric_limits<result_type>::digits;
Howard Hinnantc834c512011-11-29 18:15:50 +00002842 result_type _Sp = 0;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002843 for (size_t __k = 0; __k < __n0_; ++__k)
2844 {
2845 _Engine_result_type __u;
2846 do
2847 {
2848 __u = __e_() - _Engine::min();
2849 } while (__u >= __y0_);
Marshall Clowafc48592017-09-20 17:34:11 +00002850 if (__w0_ < _WRt)
Howard Hinnantc834c512011-11-29 18:15:50 +00002851 _Sp <<= __w0_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002852 else
Howard Hinnantc834c512011-11-29 18:15:50 +00002853 _Sp = 0;
2854 _Sp += __u & __mask0_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002855 }
2856 for (size_t __k = __n0_; __k < __n_; ++__k)
2857 {
2858 _Engine_result_type __u;
2859 do
2860 {
2861 __u = __e_() - _Engine::min();
2862 } while (__u >= __y1_);
Marshall Clowafc48592017-09-20 17:34:11 +00002863 if (__w0_ < _WRt - 1)
Howard Hinnantc834c512011-11-29 18:15:50 +00002864 _Sp <<= __w0_ + 1;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002865 else
Howard Hinnantc834c512011-11-29 18:15:50 +00002866 _Sp = 0;
2867 _Sp += __u & __mask1_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002868 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002869 return _Sp;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002870}
2871
2872// uniform_int_distribution
2873
2874template<class _IntType = int>
2875class uniform_int_distribution
2876{
2877public:
2878 // types
2879 typedef _IntType result_type;
2880
2881 class param_type
2882 {
2883 result_type __a_;
2884 result_type __b_;
2885 public:
2886 typedef uniform_int_distribution distribution_type;
2887
2888 explicit param_type(result_type __a = 0,
2889 result_type __b = numeric_limits<result_type>::max())
2890 : __a_(__a), __b_(__b) {}
2891
2892 result_type a() const {return __a_;}
2893 result_type b() const {return __b_;}
2894
2895 friend bool operator==(const param_type& __x, const param_type& __y)
2896 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2897 friend bool operator!=(const param_type& __x, const param_type& __y)
2898 {return !(__x == __y);}
2899 };
2900
2901private:
2902 param_type __p_;
2903
2904public:
2905 // constructors and reset functions
2906 explicit uniform_int_distribution(result_type __a = 0,
2907 result_type __b = numeric_limits<result_type>::max())
2908 : __p_(param_type(__a, __b)) {}
2909 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
2910 void reset() {}
2911
2912 // generating functions
2913 template<class _URNG> result_type operator()(_URNG& __g)
2914 {return (*this)(__g, __p_);}
2915 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2916
2917 // property functions
2918 result_type a() const {return __p_.a();}
2919 result_type b() const {return __p_.b();}
2920
2921 param_type param() const {return __p_;}
2922 void param(const param_type& __p) {__p_ = __p;}
2923
2924 result_type min() const {return a();}
2925 result_type max() const {return b();}
2926
2927 friend bool operator==(const uniform_int_distribution& __x,
2928 const uniform_int_distribution& __y)
2929 {return __x.__p_ == __y.__p_;}
2930 friend bool operator!=(const uniform_int_distribution& __x,
2931 const uniform_int_distribution& __y)
2932 {return !(__x == __y);}
2933};
2934
2935template<class _IntType>
2936template<class _URNG>
2937typename uniform_int_distribution<_IntType>::result_type
2938uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
Marshall Clowf79f4402018-10-08 20:20:34 +00002939_LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002940{
2941 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
2942 uint32_t, uint64_t>::type _UIntType;
Marshall Clowf79f4402018-10-08 20:20:34 +00002943 const _UIntType _Rp = _UIntType(__p.b()) - _UIntType(__p.a()) + _UIntType(1);
Howard Hinnantc834c512011-11-29 18:15:50 +00002944 if (_Rp == 1)
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002945 return __p.a();
2946 const size_t _Dt = numeric_limits<_UIntType>::digits;
2947 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
Howard Hinnantc834c512011-11-29 18:15:50 +00002948 if (_Rp == 0)
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002949 return static_cast<result_type>(_Eng(__g, _Dt)());
Howard Hinnantc834c512011-11-29 18:15:50 +00002950 size_t __w = _Dt - __clz(_Rp) - 1;
Marshall Clow40aada52015-07-30 18:26:34 +00002951 if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002952 ++__w;
2953 _Eng __e(__g, __w);
2954 _UIntType __u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002955 do
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002956 {
2957 __u = __e();
Howard Hinnantc834c512011-11-29 18:15:50 +00002958 } while (__u >= _Rp);
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002959 return static_cast<result_type>(__u + __p.a());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002960}
2961
Eric Fiselierf5fb27c2017-04-03 23:23:44 +00002962#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \
2963 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002964class _LIBCPP_TYPE_VIS __rs_default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002965
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002966_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002967
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002968class _LIBCPP_TYPE_VIS __rs_default
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969{
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002970 static unsigned __c_;
2971
2972 __rs_default();
2973public:
Marshall Clow9903c5b2013-02-07 22:12:02 +00002974 typedef uint_fast32_t result_type;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002975
2976 static const result_type _Min = 0;
2977 static const result_type _Max = 0xFFFFFFFF;
2978
2979 __rs_default(const __rs_default&);
2980 ~__rs_default();
2981
2982 result_type operator()();
2983
Howard Hinnant664183b2012-04-02 00:40:41 +00002984 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
2985 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002986
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002987 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002988};
2989
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002990_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002991
2992template <class _RandomAccessIterator>
Louis Dionne481a2662018-09-23 18:35:00 +00002993_LIBCPP_DEPRECATED_IN_CXX14 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00002994random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
2995{
2996 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc834c512011-11-29 18:15:50 +00002997 typedef uniform_int_distribution<ptrdiff_t> _Dp;
2998 typedef typename _Dp::param_type _Pp;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002999 difference_type __d = __last - __first;
3000 if (__d > 1)
3001 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003002 _Dp __uid;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003003 __rs_default __g = __rs_get();
Marshall Clowa224ace2019-01-24 19:20:19 +00003004 for (--__last, (void) --__d; __first < __last; ++__first, (void) --__d)
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003005 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003006 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003007 if (__i != difference_type(0))
3008 swap(*__first, *(__first + __i));
3009 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003010 }
3011}
3012
3013template <class _RandomAccessIterator, class _RandomNumberGenerator>
Louis Dionne481a2662018-09-23 18:35:00 +00003014_LIBCPP_DEPRECATED_IN_CXX14 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00003015random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselier93dd1372017-04-18 23:26:47 +00003016#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003017 _RandomNumberGenerator&& __rand)
3018#else
3019 _RandomNumberGenerator& __rand)
3020#endif
3021{
3022 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3023 difference_type __d = __last - __first;
3024 if (__d > 1)
3025 {
Marshall Clowa224ace2019-01-24 19:20:19 +00003026 for (--__last; __first < __last; ++__first, (void) --__d)
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003027 {
3028 difference_type __i = __rand(__d);
Marshall Clow5bdfc232018-09-11 18:33:45 +00003029 if (__i != difference_type(0))
Marshall Clowf79f4402018-10-08 20:20:34 +00003030 swap(*__first, *(__first + __i));
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003031 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003032 }
3033}
Marshall Clowfac06e52017-03-23 13:43:37 +00003034#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003035
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003036template <class _PopulationIterator, class _SampleIterator, class _Distance,
3037 class _UniformRandomNumberGenerator>
3038_LIBCPP_INLINE_VISIBILITY
3039_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsonc9637642017-11-14 11:14:25 +00003040 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003041 _Distance __n,
3042 _UniformRandomNumberGenerator & __g,
3043 input_iterator_tag) {
3044
3045 _Distance __k = 0;
3046 for (; __first != __last && __k < __n; ++__first, (void)++__k)
Alexander Richardsonc9637642017-11-14 11:14:25 +00003047 __output_iter[__k] = *__first;
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003048 _Distance __sz = __k;
3049 for (; __first != __last; ++__first, (void)++__k) {
3050 _Distance __r = _VSTD::uniform_int_distribution<_Distance>(0, __k)(__g);
3051 if (__r < __sz)
Alexander Richardsonc9637642017-11-14 11:14:25 +00003052 __output_iter[__r] = *__first;
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003053 }
Alexander Richardsonc9637642017-11-14 11:14:25 +00003054 return __output_iter + _VSTD::min(__n, __k);
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003055}
3056
3057template <class _PopulationIterator, class _SampleIterator, class _Distance,
3058 class _UniformRandomNumberGenerator>
3059_LIBCPP_INLINE_VISIBILITY
3060_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsonc9637642017-11-14 11:14:25 +00003061 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003062 _Distance __n,
3063 _UniformRandomNumberGenerator& __g,
3064 forward_iterator_tag) {
3065 _Distance __unsampled_sz = _VSTD::distance(__first, __last);
3066 for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
3067 _Distance __r =
3068 _VSTD::uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
3069 if (__r < __n) {
Alexander Richardsonc9637642017-11-14 11:14:25 +00003070 *__output_iter++ = *__first;
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003071 --__n;
3072 }
3073 }
Alexander Richardsonc9637642017-11-14 11:14:25 +00003074 return __output_iter;
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003075}
3076
3077template <class _PopulationIterator, class _SampleIterator, class _Distance,
3078 class _UniformRandomNumberGenerator>
3079_LIBCPP_INLINE_VISIBILITY
3080_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsonc9637642017-11-14 11:14:25 +00003081 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003082 _Distance __n, _UniformRandomNumberGenerator& __g) {
3083 typedef typename iterator_traits<_PopulationIterator>::iterator_category
3084 _PopCategory;
3085 typedef typename iterator_traits<_PopulationIterator>::difference_type
3086 _Difference;
3087 static_assert(__is_forward_iterator<_PopulationIterator>::value ||
3088 __is_random_access_iterator<_SampleIterator>::value,
3089 "SampleIterator must meet the requirements of RandomAccessIterator");
3090 typedef typename common_type<_Distance, _Difference>::type _CommonType;
3091 _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
3092 return _VSTD::__sample(
Alexander Richardsonc9637642017-11-14 11:14:25 +00003093 __first, __last, __output_iter, _CommonType(__n),
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003094 __g, _PopCategory());
3095}
3096
3097#if _LIBCPP_STD_VER > 14
3098template <class _PopulationIterator, class _SampleIterator, class _Distance,
3099 class _UniformRandomNumberGenerator>
3100inline _LIBCPP_INLINE_VISIBILITY
3101_SampleIterator sample(_PopulationIterator __first,
Alexander Richardsonc9637642017-11-14 11:14:25 +00003102 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003103 _Distance __n, _UniformRandomNumberGenerator&& __g) {
Alexander Richardsonc9637642017-11-14 11:14:25 +00003104 return _VSTD::__sample(__first, __last, __output_iter, __n, __g);
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003105}
3106#endif // _LIBCPP_STD_VER > 14
3107
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003108template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3109 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselier93dd1372017-04-18 23:26:47 +00003110#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanta5e71782010-11-18 01:47:02 +00003111 _UniformRandomNumberGenerator&& __g)
3112#else
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003113 _UniformRandomNumberGenerator& __g)
Howard Hinnanta5e71782010-11-18 01:47:02 +00003114#endif
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003115{
3116 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc834c512011-11-29 18:15:50 +00003117 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3118 typedef typename _Dp::param_type _Pp;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003119 difference_type __d = __last - __first;
3120 if (__d > 1)
3121 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003122 _Dp __uid;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003123 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003124 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003125 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003126 if (__i != difference_type(0))
3127 swap(*__first, *(__first + __i));
3128 }
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003129 }
3130}
3131
Howard Hinnantc51e1022010-05-11 19:42:16 +00003132template <class _InputIterator, class _Predicate>
Marshall Clow96d050a2018-01-15 16:16:32 +00003133_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00003134is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3135{
3136 for (; __first != __last; ++__first)
3137 if (!__pred(*__first))
3138 break;
Marshall Clow3562ed72015-02-02 18:16:35 +00003139 if ( __first == __last )
3140 return true;
3141 ++__first;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003142 for (; __first != __last; ++__first)
3143 if (__pred(*__first))
3144 return false;
3145 return true;
3146}
3147
3148// partition
3149
3150template <class _Predicate, class _ForwardIterator>
3151_ForwardIterator
3152__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3153{
3154 while (true)
3155 {
3156 if (__first == __last)
3157 return __first;
3158 if (!__pred(*__first))
3159 break;
3160 ++__first;
3161 }
3162 for (_ForwardIterator __p = __first; ++__p != __last;)
3163 {
3164 if (__pred(*__p))
3165 {
3166 swap(*__first, *__p);
3167 ++__first;
3168 }
3169 }
3170 return __first;
3171}
3172
3173template <class _Predicate, class _BidirectionalIterator>
3174_BidirectionalIterator
3175__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3176 bidirectional_iterator_tag)
3177{
3178 while (true)
3179 {
3180 while (true)
3181 {
3182 if (__first == __last)
3183 return __first;
3184 if (!__pred(*__first))
3185 break;
3186 ++__first;
3187 }
3188 do
3189 {
3190 if (__first == --__last)
3191 return __first;
3192 } while (!__pred(*__last));
3193 swap(*__first, *__last);
3194 ++__first;
3195 }
3196}
3197
3198template <class _ForwardIterator, class _Predicate>
3199inline _LIBCPP_INLINE_VISIBILITY
3200_ForwardIterator
3201partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3202{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003203 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003204 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3205}
3206
3207// partition_copy
3208
3209template <class _InputIterator, class _OutputIterator1,
3210 class _OutputIterator2, class _Predicate>
Marshall Clow5492c8a2018-01-22 20:44:33 +00003211_LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_OutputIterator1, _OutputIterator2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003212partition_copy(_InputIterator __first, _InputIterator __last,
3213 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3214 _Predicate __pred)
3215{
3216 for (; __first != __last; ++__first)
3217 {
3218 if (__pred(*__first))
3219 {
3220 *__out_true = *__first;
3221 ++__out_true;
3222 }
3223 else
3224 {
3225 *__out_false = *__first;
3226 ++__out_false;
3227 }
3228 }
3229 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3230}
3231
3232// partition_point
3233
3234template<class _ForwardIterator, class _Predicate>
Marshall Clowcb3c8262018-01-15 17:53:34 +00003235_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003236partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3237{
3238 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003239 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003240 while (__len != 0)
3241 {
Louis Dionnedda14512018-12-17 16:04:39 +00003242 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003243 _ForwardIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003244 _VSTD::advance(__m, __l2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003245 if (__pred(*__m))
3246 {
3247 __first = ++__m;
3248 __len -= __l2 + 1;
3249 }
3250 else
3251 __len = __l2;
3252 }
3253 return __first;
3254}
3255
3256// stable_partition
3257
3258template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3259_ForwardIterator
3260__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3261 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3262{
3263 // *__first is known to be false
3264 // __len >= 1
3265 if (__len == 1)
3266 return __first;
3267 if (__len == 2)
3268 {
3269 _ForwardIterator __m = __first;
3270 if (__pred(*++__m))
3271 {
3272 swap(*__first, *__m);
3273 return __m;
3274 }
3275 return __first;
3276 }
3277 if (__len <= __p.second)
3278 { // The buffer is big enough to use
3279 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3280 __destruct_n __d(0);
3281 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3282 // Move the falses into the temporary buffer, and the trues to the front of the line
3283 // Update __first to always point to the end of the trues
3284 value_type* __t = __p.first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003285 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003286 __d.__incr((value_type*)0);
3287 ++__t;
3288 _ForwardIterator __i = __first;
3289 while (++__i != __last)
3290 {
3291 if (__pred(*__i))
3292 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003293 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003294 ++__first;
3295 }
3296 else
3297 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003298 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003299 __d.__incr((value_type*)0);
3300 ++__t;
3301 }
3302 }
3303 // All trues now at start of range, all falses in buffer
3304 // Move falses back into range, but don't mess up __first which points to first false
3305 __i = __first;
3306 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003307 *__i = _VSTD::move(*__t2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003308 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3309 return __first;
3310 }
3311 // Else not enough buffer, do in place
3312 // __len >= 3
3313 _ForwardIterator __m = __first;
3314 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003315 _VSTD::advance(__m, __len2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003316 // recurse on [__first, __m), *__first know to be false
3317 // F?????????????????
3318 // f m l
3319 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3320 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3321 // TTTFFFFF??????????
3322 // f ff m l
3323 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3324 _ForwardIterator __m1 = __m;
3325 _ForwardIterator __second_false = __last;
3326 _Distance __len_half = __len - __len2;
3327 while (__pred(*__m1))
3328 {
3329 if (++__m1 == __last)
3330 goto __second_half_done;
3331 --__len_half;
3332 }
3333 // TTTFFFFFTTTF??????
3334 // f ff m m1 l
3335 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3336__second_half_done:
3337 // TTTFFFFFTTTTTFFFFF
3338 // f ff m sf l
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003339 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003340 // TTTTTTTTFFFFFFFFFF
3341 // |
3342}
3343
3344struct __return_temporary_buffer
3345{
3346 template <class _Tp>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003347 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003348};
3349
3350template <class _Predicate, class _ForwardIterator>
3351_ForwardIterator
3352__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3353 forward_iterator_tag)
3354{
3355 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3356 // Either prove all true and return __first or point to first false
3357 while (true)
3358 {
3359 if (__first == __last)
3360 return __first;
3361 if (!__pred(*__first))
3362 break;
3363 ++__first;
3364 }
3365 // We now have a reduced range [__first, __last)
3366 // *__first is known to be false
3367 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3368 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003369 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003370 pair<value_type*, ptrdiff_t> __p(0, 0);
3371 unique_ptr<value_type, __return_temporary_buffer> __h;
3372 if (__len >= __alloc_limit)
3373 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003374 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003375 __h.reset(__p.first);
3376 }
3377 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3378 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3379}
3380
3381template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3382_BidirectionalIterator
3383__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3384 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3385{
3386 // *__first is known to be false
3387 // *__last is known to be true
3388 // __len >= 2
3389 if (__len == 2)
3390 {
3391 swap(*__first, *__last);
3392 return __last;
3393 }
3394 if (__len == 3)
3395 {
3396 _BidirectionalIterator __m = __first;
3397 if (__pred(*++__m))
3398 {
3399 swap(*__first, *__m);
3400 swap(*__m, *__last);
3401 return __last;
3402 }
3403 swap(*__m, *__last);
3404 swap(*__first, *__m);
3405 return __m;
3406 }
3407 if (__len <= __p.second)
3408 { // The buffer is big enough to use
3409 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3410 __destruct_n __d(0);
3411 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3412 // Move the falses into the temporary buffer, and the trues to the front of the line
3413 // Update __first to always point to the end of the trues
3414 value_type* __t = __p.first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003415 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003416 __d.__incr((value_type*)0);
3417 ++__t;
3418 _BidirectionalIterator __i = __first;
3419 while (++__i != __last)
3420 {
3421 if (__pred(*__i))
3422 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003423 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003424 ++__first;
3425 }
3426 else
3427 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003428 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003429 __d.__incr((value_type*)0);
3430 ++__t;
3431 }
3432 }
3433 // move *__last, known to be true
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003434 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003435 __i = ++__first;
3436 // All trues now at start of range, all falses in buffer
3437 // Move falses back into range, but don't mess up __first which points to first false
3438 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003439 *__i = _VSTD::move(*__t2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003440 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3441 return __first;
3442 }
3443 // Else not enough buffer, do in place
3444 // __len >= 4
3445 _BidirectionalIterator __m = __first;
3446 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003447 _VSTD::advance(__m, __len2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003448 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3449 // F????????????????T
3450 // f m l
3451 _BidirectionalIterator __m1 = __m;
3452 _BidirectionalIterator __first_false = __first;
3453 _Distance __len_half = __len2;
3454 while (!__pred(*--__m1))
3455 {
3456 if (__m1 == __first)
3457 goto __first_half_done;
3458 --__len_half;
3459 }
3460 // F???TFFF?????????T
3461 // f m1 m l
3462 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3463 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3464__first_half_done:
3465 // TTTFFFFF?????????T
3466 // f ff m l
3467 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3468 __m1 = __m;
3469 _BidirectionalIterator __second_false = __last;
3470 ++__second_false;
3471 __len_half = __len - __len2;
3472 while (__pred(*__m1))
3473 {
3474 if (++__m1 == __last)
3475 goto __second_half_done;
3476 --__len_half;
3477 }
3478 // TTTFFFFFTTTF?????T
3479 // f ff m m1 l
3480 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3481__second_half_done:
3482 // TTTFFFFFTTTTTFFFFF
3483 // f ff m sf l
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003484 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003485 // TTTTTTTTFFFFFFFFFF
3486 // |
3487}
3488
3489template <class _Predicate, class _BidirectionalIterator>
3490_BidirectionalIterator
3491__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3492 bidirectional_iterator_tag)
3493{
3494 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3495 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3496 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3497 // Either prove all true and return __first or point to first false
3498 while (true)
3499 {
3500 if (__first == __last)
3501 return __first;
3502 if (!__pred(*__first))
3503 break;
3504 ++__first;
3505 }
3506 // __first points to first false, everything prior to __first is already set.
3507 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3508 do
3509 {
3510 if (__first == --__last)
3511 return __first;
3512 } while (!__pred(*__last));
3513 // We now have a reduced range [__first, __last]
3514 // *__first is known to be false
3515 // *__last is known to be true
3516 // __len >= 2
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003517 difference_type __len = _VSTD::distance(__first, __last) + 1;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003518 pair<value_type*, ptrdiff_t> __p(0, 0);
3519 unique_ptr<value_type, __return_temporary_buffer> __h;
3520 if (__len >= __alloc_limit)
3521 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003522 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003523 __h.reset(__p.first);
3524 }
3525 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3526 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3527}
3528
3529template <class _ForwardIterator, class _Predicate>
3530inline _LIBCPP_INLINE_VISIBILITY
3531_ForwardIterator
3532stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3533{
3534 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3535 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3536}
3537
3538// is_sorted_until
3539
3540template <class _ForwardIterator, class _Compare>
Marshall Clow96d050a2018-01-15 16:16:32 +00003541_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003542is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3543{
3544 if (__first != __last)
3545 {
3546 _ForwardIterator __i = __first;
3547 while (++__i != __last)
3548 {
3549 if (__comp(*__i, *__first))
3550 return __i;
3551 __first = __i;
3552 }
3553 }
3554 return __last;
3555}
3556
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003557template<class _ForwardIterator>
Marshall Clow96d050a2018-01-15 16:16:32 +00003558inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559_ForwardIterator
3560is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3561{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003562 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003563}
3564
3565// is_sorted
3566
3567template <class _ForwardIterator, class _Compare>
Marshall Clow96d050a2018-01-15 16:16:32 +00003568inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003569bool
3570is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3571{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003572 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003573}
3574
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003575template<class _ForwardIterator>
Marshall Clow96d050a2018-01-15 16:16:32 +00003576inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003577bool
3578is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3579{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003580 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003581}
3582
3583// sort
3584
3585// stable, 2-3 compares, 0-2 swaps
3586
3587template <class _Compare, class _ForwardIterator>
3588unsigned
3589__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3590{
3591 unsigned __r = 0;
3592 if (!__c(*__y, *__x)) // if x <= y
3593 {
3594 if (!__c(*__z, *__y)) // if y <= z
3595 return __r; // x <= y && y <= z
3596 // x <= y && y > z
3597 swap(*__y, *__z); // x <= z && y < z
3598 __r = 1;
3599 if (__c(*__y, *__x)) // if x > y
3600 {
3601 swap(*__x, *__y); // x < y && y <= z
3602 __r = 2;
3603 }
3604 return __r; // x <= y && y < z
3605 }
3606 if (__c(*__z, *__y)) // x > y, if y > z
3607 {
3608 swap(*__x, *__z); // x < y && y < z
3609 __r = 1;
3610 return __r;
3611 }
3612 swap(*__x, *__y); // x > y && y <= z
3613 __r = 1; // x < y && x <= z
3614 if (__c(*__z, *__y)) // if y > z
3615 {
3616 swap(*__y, *__z); // x <= y && y < z
3617 __r = 2;
3618 }
3619 return __r;
3620} // x <= y && y <= z
3621
3622// stable, 3-6 compares, 0-5 swaps
3623
3624template <class _Compare, class _ForwardIterator>
3625unsigned
3626__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3627 _ForwardIterator __x4, _Compare __c)
3628{
3629 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3630 if (__c(*__x4, *__x3))
3631 {
3632 swap(*__x3, *__x4);
3633 ++__r;
3634 if (__c(*__x3, *__x2))
3635 {
3636 swap(*__x2, *__x3);
3637 ++__r;
3638 if (__c(*__x2, *__x1))
3639 {
3640 swap(*__x1, *__x2);
3641 ++__r;
3642 }
3643 }
3644 }
3645 return __r;
3646}
3647
3648// stable, 4-10 compares, 0-9 swaps
3649
3650template <class _Compare, class _ForwardIterator>
Louis Dionne9e70e362018-11-21 16:24:46 +00003651_LIBCPP_HIDDEN
Howard Hinnantc51e1022010-05-11 19:42:16 +00003652unsigned
3653__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3654 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3655{
3656 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3657 if (__c(*__x5, *__x4))
3658 {
3659 swap(*__x4, *__x5);
3660 ++__r;
3661 if (__c(*__x4, *__x3))
3662 {
3663 swap(*__x3, *__x4);
3664 ++__r;
3665 if (__c(*__x3, *__x2))
3666 {
3667 swap(*__x2, *__x3);
3668 ++__r;
3669 if (__c(*__x2, *__x1))
3670 {
3671 swap(*__x1, *__x2);
3672 ++__r;
3673 }
3674 }
3675 }
3676 }
3677 return __r;
3678}
3679
3680// Assumes size > 0
3681template <class _Compare, class _BirdirectionalIterator>
3682void
3683__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3684{
3685 _BirdirectionalIterator __lm1 = __last;
3686 for (--__lm1; __first != __lm1; ++__first)
3687 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003688 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003689 typename add_lvalue_reference<_Compare>::type>
3690 (__first, __last, __comp);
3691 if (__i != __first)
3692 swap(*__first, *__i);
3693 }
3694}
3695
3696template <class _Compare, class _BirdirectionalIterator>
3697void
3698__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3699{
3700 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3701 if (__first != __last)
3702 {
3703 _BirdirectionalIterator __i = __first;
3704 for (++__i; __i != __last; ++__i)
3705 {
3706 _BirdirectionalIterator __j = __i;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003707 value_type __t(_VSTD::move(*__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003709 *__j = _VSTD::move(*__k);
3710 *__j = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003711 }
3712 }
3713}
3714
3715template <class _Compare, class _RandomAccessIterator>
3716void
3717__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3718{
3719 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3720 _RandomAccessIterator __j = __first+2;
3721 __sort3<_Compare>(__first, __first+1, __j, __comp);
3722 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3723 {
3724 if (__comp(*__i, *__j))
3725 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003726 value_type __t(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003727 _RandomAccessIterator __k = __j;
3728 __j = __i;
3729 do
3730 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003731 *__j = _VSTD::move(*__k);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003732 __j = __k;
3733 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003734 *__j = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003735 }
3736 __j = __i;
3737 }
3738}
3739
3740template <class _Compare, class _RandomAccessIterator>
3741bool
3742__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3743{
3744 switch (__last - __first)
3745 {
3746 case 0:
3747 case 1:
3748 return true;
3749 case 2:
3750 if (__comp(*--__last, *__first))
3751 swap(*__first, *__last);
3752 return true;
3753 case 3:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003754 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003755 return true;
3756 case 4:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003757 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003758 return true;
3759 case 5:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003760 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003761 return true;
3762 }
3763 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3764 _RandomAccessIterator __j = __first+2;
3765 __sort3<_Compare>(__first, __first+1, __j, __comp);
3766 const unsigned __limit = 8;
3767 unsigned __count = 0;
3768 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3769 {
3770 if (__comp(*__i, *__j))
3771 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003772 value_type __t(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003773 _RandomAccessIterator __k = __j;
3774 __j = __i;
3775 do
3776 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003777 *__j = _VSTD::move(*__k);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003778 __j = __k;
3779 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003780 *__j = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003781 if (++__count == __limit)
3782 return ++__i == __last;
3783 }
3784 __j = __i;
3785 }
3786 return true;
3787}
3788
3789template <class _Compare, class _BirdirectionalIterator>
3790void
3791__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3792 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3793{
3794 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3795 if (__first1 != __last1)
3796 {
3797 __destruct_n __d(0);
3798 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3799 value_type* __last2 = __first2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003800 ::new(__last2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003801 __d.__incr((value_type*)0);
3802 for (++__last2; ++__first1 != __last1; ++__last2)
3803 {
3804 value_type* __j2 = __last2;
3805 value_type* __i2 = __j2;
3806 if (__comp(*__first1, *--__i2))
3807 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003808 ::new(__j2) value_type(_VSTD::move(*__i2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003809 __d.__incr((value_type*)0);
3810 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003811 *__j2 = _VSTD::move(*__i2);
3812 *__j2 = _VSTD::move(*__first1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003813 }
3814 else
3815 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003816 ::new(__j2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003817 __d.__incr((value_type*)0);
3818 }
3819 }
3820 __h.release();
3821 }
3822}
3823
3824template <class _Compare, class _RandomAccessIterator>
3825void
3826__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3827{
3828 // _Compare is known to be a reference type
3829 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3830 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003831 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3832 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003833 while (true)
3834 {
3835 __restart:
3836 difference_type __len = __last - __first;
3837 switch (__len)
3838 {
3839 case 0:
3840 case 1:
3841 return;
3842 case 2:
3843 if (__comp(*--__last, *__first))
3844 swap(*__first, *__last);
3845 return;
3846 case 3:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003847 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003848 return;
3849 case 4:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003850 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003851 return;
3852 case 5:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003853 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003854 return;
3855 }
3856 if (__len <= __limit)
3857 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003858 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003859 return;
3860 }
3861 // __len > 5
3862 _RandomAccessIterator __m = __first;
3863 _RandomAccessIterator __lm1 = __last;
3864 --__lm1;
3865 unsigned __n_swaps;
3866 {
3867 difference_type __delta;
3868 if (__len >= 1000)
3869 {
3870 __delta = __len/2;
3871 __m += __delta;
3872 __delta /= 2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003873 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003874 }
3875 else
3876 {
3877 __delta = __len/2;
3878 __m += __delta;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003879 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003880 }
3881 }
3882 // *__m is median
3883 // partition [__first, __m) < *__m and *__m <= [__m, __last)
3884 // (this inhibits tossing elements equivalent to __m around unnecessarily)
3885 _RandomAccessIterator __i = __first;
3886 _RandomAccessIterator __j = __lm1;
3887 // j points beyond range to be tested, *__m is known to be <= *__lm1
3888 // The search going up is known to be guarded but the search coming down isn't.
3889 // Prime the downward search with a guard.
3890 if (!__comp(*__i, *__m)) // if *__first == *__m
3891 {
3892 // *__first == *__m, *__first doesn't go in first part
3893 // manually guard downward moving __j against __i
3894 while (true)
3895 {
3896 if (__i == --__j)
3897 {
3898 // *__first == *__m, *__m <= all other elements
3899 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
3900 ++__i; // __first + 1
3901 __j = __last;
3902 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
3903 {
3904 while (true)
3905 {
3906 if (__i == __j)
3907 return; // [__first, __last) all equivalent elements
3908 if (__comp(*__first, *__i))
3909 {
3910 swap(*__i, *__j);
3911 ++__n_swaps;
3912 ++__i;
3913 break;
3914 }
3915 ++__i;
3916 }
3917 }
3918 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
3919 if (__i == __j)
3920 return;
3921 while (true)
3922 {
3923 while (!__comp(*__first, *__i))
3924 ++__i;
3925 while (__comp(*__first, *--__j))
3926 ;
3927 if (__i >= __j)
3928 break;
3929 swap(*__i, *__j);
3930 ++__n_swaps;
3931 ++__i;
3932 }
3933 // [__first, __i) == *__first and *__first < [__i, __last)
3934 // The first part is sorted, sort the secod part
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003935 // _VSTD::__sort<_Compare>(__i, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003936 __first = __i;
3937 goto __restart;
3938 }
3939 if (__comp(*__j, *__m))
3940 {
3941 swap(*__i, *__j);
3942 ++__n_swaps;
3943 break; // found guard for downward moving __j, now use unguarded partition
3944 }
3945 }
3946 }
3947 // It is known that *__i < *__m
3948 ++__i;
3949 // j points beyond range to be tested, *__m is known to be <= *__lm1
3950 // if not yet partitioned...
3951 if (__i < __j)
3952 {
3953 // known that *(__i - 1) < *__m
3954 // known that __i <= __m
3955 while (true)
3956 {
3957 // __m still guards upward moving __i
3958 while (__comp(*__i, *__m))
3959 ++__i;
3960 // It is now known that a guard exists for downward moving __j
3961 while (!__comp(*--__j, *__m))
3962 ;
3963 if (__i > __j)
3964 break;
3965 swap(*__i, *__j);
3966 ++__n_swaps;
3967 // It is known that __m != __j
3968 // If __m just moved, follow it
3969 if (__m == __i)
3970 __m = __j;
3971 ++__i;
3972 }
3973 }
3974 // [__first, __i) < *__m and *__m <= [__i, __last)
3975 if (__i != __m && __comp(*__m, *__i))
3976 {
3977 swap(*__i, *__m);
3978 ++__n_swaps;
3979 }
3980 // [__first, __i) < *__i and *__i <= [__i+1, __last)
3981 // If we were given a perfect partition, see if insertion sort is quick...
3982 if (__n_swaps == 0)
3983 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003984 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
3985 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
Howard Hinnantc51e1022010-05-11 19:42:16 +00003986 {
3987 if (__fs)
3988 return;
3989 __last = __i;
3990 continue;
3991 }
3992 else
3993 {
3994 if (__fs)
3995 {
3996 __first = ++__i;
3997 continue;
3998 }
3999 }
4000 }
4001 // sort smaller range with recursive call and larger with tail recursion elimination
4002 if (__i - __first < __last - __i)
4003 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004004 _VSTD::__sort<_Compare>(__first, __i, __comp);
4005 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004006 __first = ++__i;
4007 }
4008 else
4009 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004010 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4011 // _VSTD::__sort<_Compare>(__first, __i, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004012 __last = __i;
4013 }
4014 }
4015}
4016
4017// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
4018template <class _RandomAccessIterator, class _Compare>
4019inline _LIBCPP_INLINE_VISIBILITY
4020void
4021sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4022{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004023#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004024 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4025 __debug_less<_Compare> __c(__comp);
4026 __sort<_Comp_ref>(__first, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004027#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004028 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4029 __sort<_Comp_ref>(__first, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004030#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004031}
4032
4033template <class _RandomAccessIterator>
4034inline _LIBCPP_INLINE_VISIBILITY
4035void
4036sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4037{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004038 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004039}
4040
4041template <class _Tp>
4042inline _LIBCPP_INLINE_VISIBILITY
4043void
4044sort(_Tp** __first, _Tp** __last)
4045{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004046 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004047}
4048
4049template <class _Tp>
4050inline _LIBCPP_INLINE_VISIBILITY
4051void
4052sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4053{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004054 _VSTD::sort(__first.base(), __last.base());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004055}
4056
Howard Hinnant27e0e772011-09-14 18:33:51 +00004057template <class _Tp, class _Compare>
4058inline _LIBCPP_INLINE_VISIBILITY
4059void
4060sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4061{
4062 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4063 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4064}
4065
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004066_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4067_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4068_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4069_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4070_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4071_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4072_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4073_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4074_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4075_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4076_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4077_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>&))
4078_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4079_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4080_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 +00004081
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004082_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4083_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4084_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4085_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4086_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4087_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4088_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4089_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4090_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4091_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4092_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4093_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>&))
4094_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4095_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4096_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 +00004097
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004098_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 +00004099
4100// lower_bound
4101
4102template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004103_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004104__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004105{
4106 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004107 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004108 while (__len != 0)
4109 {
Louis Dionnedda14512018-12-17 16:04:39 +00004110 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004111 _ForwardIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004112 _VSTD::advance(__m, __l2);
Howard Hinnantbf074022011-10-22 20:59:45 +00004113 if (__comp(*__m, __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004114 {
4115 __first = ++__m;
4116 __len -= __l2 + 1;
4117 }
4118 else
4119 __len = __l2;
4120 }
4121 return __first;
4122}
4123
4124template <class _ForwardIterator, class _Tp, class _Compare>
Marshall Clowe00916c2018-01-16 02:34:41 +00004125inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004126_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004127lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004128{
Howard Hinnantc51e1022010-05-11 19:42:16 +00004129 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantbf074022011-10-22 20:59:45 +00004130 return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004131}
4132
4133template <class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004134inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004135_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004136lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004137{
Howard Hinnantbf074022011-10-22 20:59:45 +00004138 return _VSTD::lower_bound(__first, __last, __value_,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004139 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4140}
4141
4142// upper_bound
4143
4144template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004145_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004146__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004147{
4148 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004149 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004150 while (__len != 0)
4151 {
Louis Dionnedda14512018-12-17 16:04:39 +00004152 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004153 _ForwardIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004154 _VSTD::advance(__m, __l2);
Howard Hinnantbf074022011-10-22 20:59:45 +00004155 if (__comp(__value_, *__m))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004156 __len = __l2;
4157 else
4158 {
4159 __first = ++__m;
4160 __len -= __l2 + 1;
4161 }
4162 }
4163 return __first;
4164}
4165
4166template <class _ForwardIterator, class _Tp, class _Compare>
Marshall Clowe00916c2018-01-16 02:34:41 +00004167inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004168_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004169upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004170{
Howard Hinnantc51e1022010-05-11 19:42:16 +00004171 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantbf074022011-10-22 20:59:45 +00004172 return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004173}
4174
4175template <class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004176inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004177_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004178upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004179{
Howard Hinnantbf074022011-10-22 20:59:45 +00004180 return _VSTD::upper_bound(__first, __last, __value_,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004181 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4182}
4183
4184// equal_range
4185
4186template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004187_LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_ForwardIterator, _ForwardIterator>
Howard Hinnantbf074022011-10-22 20:59:45 +00004188__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004189{
4190 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004191 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004192 while (__len != 0)
4193 {
Louis Dionnedda14512018-12-17 16:04:39 +00004194 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004195 _ForwardIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004196 _VSTD::advance(__m, __l2);
Howard Hinnantbf074022011-10-22 20:59:45 +00004197 if (__comp(*__m, __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004198 {
4199 __first = ++__m;
4200 __len -= __l2 + 1;
4201 }
Howard Hinnantbf074022011-10-22 20:59:45 +00004202 else if (__comp(__value_, *__m))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004203 {
4204 __last = __m;
4205 __len = __l2;
4206 }
4207 else
4208 {
4209 _ForwardIterator __mp1 = __m;
4210 return pair<_ForwardIterator, _ForwardIterator>
4211 (
Howard Hinnantbf074022011-10-22 20:59:45 +00004212 __lower_bound<_Compare>(__first, __m, __value_, __comp),
4213 __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004214 );
4215 }
4216 }
4217 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4218}
4219
4220template <class _ForwardIterator, class _Tp, class _Compare>
Marshall Clowe00916c2018-01-16 02:34:41 +00004221inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004222pair<_ForwardIterator, _ForwardIterator>
Howard Hinnantbf074022011-10-22 20:59:45 +00004223equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004224{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004225#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4227 __debug_less<_Compare> __c(__comp);
Howard Hinnantbf074022011-10-22 20:59:45 +00004228 return __equal_range<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004229#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004230 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantbf074022011-10-22 20:59:45 +00004231 return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004232#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004233}
4234
4235template <class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004236inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004237pair<_ForwardIterator, _ForwardIterator>
Howard Hinnantbf074022011-10-22 20:59:45 +00004238equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004239{
Howard Hinnantbf074022011-10-22 20:59:45 +00004240 return _VSTD::equal_range(__first, __last, __value_,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004241 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4242}
4243
4244// binary_search
4245
4246template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004247inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004248bool
Howard Hinnantbf074022011-10-22 20:59:45 +00004249__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004250{
Howard Hinnantbf074022011-10-22 20:59:45 +00004251 __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4252 return __first != __last && !__comp(__value_, *__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004253}
4254
4255template <class _ForwardIterator, class _Tp, class _Compare>
Marshall Clowe00916c2018-01-16 02:34:41 +00004256inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004257bool
Howard Hinnantbf074022011-10-22 20:59:45 +00004258binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004259{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004260#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004261 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4262 __debug_less<_Compare> __c(__comp);
Howard Hinnantbf074022011-10-22 20:59:45 +00004263 return __binary_search<_Comp_ref>(__first, __last, __value_, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004264#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004265 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantbf074022011-10-22 20:59:45 +00004266 return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004267#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004268}
4269
4270template <class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004271inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004272bool
Howard Hinnantbf074022011-10-22 20:59:45 +00004273binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004274{
Howard Hinnantbf074022011-10-22 20:59:45 +00004275 return _VSTD::binary_search(__first, __last, __value_,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4277}
4278
4279// merge
4280
4281template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4282_OutputIterator
4283__merge(_InputIterator1 __first1, _InputIterator1 __last1,
4284 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4285{
4286 for (; __first1 != __last1; ++__result)
4287 {
4288 if (__first2 == __last2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004289 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004290 if (__comp(*__first2, *__first1))
4291 {
4292 *__result = *__first2;
4293 ++__first2;
4294 }
4295 else
4296 {
4297 *__result = *__first1;
4298 ++__first1;
4299 }
4300 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004301 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004302}
4303
4304template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4305inline _LIBCPP_INLINE_VISIBILITY
4306_OutputIterator
4307merge(_InputIterator1 __first1, _InputIterator1 __last1,
4308 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4309{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004310#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004311 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4312 __debug_less<_Compare> __c(__comp);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004313 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004314#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004315 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004316 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004317#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004318}
4319
4320template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4321inline _LIBCPP_INLINE_VISIBILITY
4322_OutputIterator
4323merge(_InputIterator1 __first1, _InputIterator1 __last1,
4324 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4325{
4326 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4327 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4328 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4329}
4330
4331// inplace_merge
4332
Marshall Clow1bc51102015-07-29 16:25:45 +00004333template <class _Compare, class _InputIterator1, class _InputIterator2,
4334 class _OutputIterator>
4335void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4336 _InputIterator2 __first2, _InputIterator2 __last2,
4337 _OutputIterator __result, _Compare __comp)
4338{
4339 for (; __first1 != __last1; ++__result)
4340 {
4341 if (__first2 == __last2)
4342 {
4343 _VSTD::move(__first1, __last1, __result);
4344 return;
4345 }
4346
4347 if (__comp(*__first2, *__first1))
4348 {
4349 *__result = _VSTD::move(*__first2);
4350 ++__first2;
4351 }
4352 else
4353 {
4354 *__result = _VSTD::move(*__first1);
4355 ++__first1;
4356 }
4357 }
4358 // __first2 through __last2 are already in the right spot.
4359}
4360
Howard Hinnantc51e1022010-05-11 19:42:16 +00004361template <class _Compare, class _BidirectionalIterator>
4362void
4363__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4364 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4365 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4366 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4367{
4368 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004369 __destruct_n __d(0);
4370 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4371 if (__len1 <= __len2)
4372 {
4373 value_type* __p = __buff;
Eric Fiseliera09a3b42014-10-27 19:28:20 +00004374 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004375 ::new(__p) value_type(_VSTD::move(*__i));
Marshall Clow1bc51102015-07-29 16:25:45 +00004376 __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004377 }
4378 else
4379 {
4380 value_type* __p = __buff;
Eric Fiseliera09a3b42014-10-27 19:28:20 +00004381 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004382 ::new(__p) value_type(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004383 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4384 typedef reverse_iterator<value_type*> _Rv;
Aditya Kumar3a0179a2016-08-25 11:52:38 +00004385 __half_inplace_merge(_Rv(__p), _Rv(__buff),
Marshall Clow1bc51102015-07-29 16:25:45 +00004386 _RBi(__middle), _RBi(__first),
Marshall Clow738d1042017-08-28 23:16:13 +00004387 _RBi(__last), __invert<_Compare>(__comp));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004388 }
4389}
4390
4391template <class _Compare, class _BidirectionalIterator>
4392void
4393__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4394 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4395 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4396 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4397{
Howard Hinnantc51e1022010-05-11 19:42:16 +00004398 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4399 while (true)
4400 {
4401 // if __middle == __last, we're done
4402 if (__len2 == 0)
4403 return;
Marshall Clow8eff8232015-02-02 16:44:11 +00004404 if (__len1 <= __buff_size || __len2 <= __buff_size)
4405 return __buffered_inplace_merge<_Compare>
4406 (__first, __middle, __last, __comp, __len1, __len2, __buff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004407 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
Eric Fiseliera09a3b42014-10-27 19:28:20 +00004408 for (; true; ++__first, (void) --__len1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004409 {
4410 if (__len1 == 0)
4411 return;
4412 if (__comp(*__middle, *__first))
4413 break;
4414 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004415 // __first < __middle < __last
4416 // *__first > *__middle
4417 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4418 // all elements in:
4419 // [__first, __m1) <= [__middle, __m2)
4420 // [__middle, __m2) < [__m1, __middle)
4421 // [__m1, __middle) <= [__m2, __last)
4422 // and __m1 or __m2 is in the middle of its range
4423 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4424 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4425 difference_type __len11; // distance(__first, __m1)
4426 difference_type __len21; // distance(__middle, __m2)
4427 // binary search smaller range
4428 if (__len1 < __len2)
4429 { // __len >= 1, __len2 >= 2
4430 __len21 = __len2 / 2;
4431 __m2 = __middle;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004432 _VSTD::advance(__m2, __len21);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004433 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004434 __len11 = _VSTD::distance(__first, __m1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004435 }
4436 else
4437 {
4438 if (__len1 == 1)
4439 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4440 // It is known *__first > *__middle
4441 swap(*__first, *__middle);
4442 return;
4443 }
4444 // __len1 >= 2, __len2 >= 1
4445 __len11 = __len1 / 2;
4446 __m1 = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004447 _VSTD::advance(__m1, __len11);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004448 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004449 __len21 = _VSTD::distance(__middle, __m2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004450 }
4451 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4452 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4453 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4454 // swap middle two partitions
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004455 __middle = _VSTD::rotate(__m1, __middle, __m2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004456 // __len12 and __len21 now have swapped meanings
4457 // merge smaller range with recurisve call and larger with tail recursion elimination
4458 if (__len11 + __len21 < __len12 + __len22)
4459 {
4460 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4461// __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4462 __first = __middle;
4463 __middle = __m2;
4464 __len1 = __len12;
4465 __len2 = __len22;
4466 }
4467 else
4468 {
4469 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4470// __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4471 __last = __middle;
4472 __middle = __m1;
4473 __len1 = __len11;
4474 __len2 = __len21;
4475 }
4476 }
4477}
4478
Howard Hinnantc51e1022010-05-11 19:42:16 +00004479template <class _BidirectionalIterator, class _Compare>
4480inline _LIBCPP_INLINE_VISIBILITY
4481void
4482inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4483 _Compare __comp)
4484{
4485 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4486 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004487 difference_type __len1 = _VSTD::distance(__first, __middle);
4488 difference_type __len2 = _VSTD::distance(__middle, __last);
4489 difference_type __buf_size = _VSTD::min(__len1, __len2);
Marshall Clow488f19f2015-02-02 17:35:53 +00004490 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4491 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
4492
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004493#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004494 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4495 __debug_less<_Compare> __c(__comp);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004496 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004497 __buf.first, __buf.second);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004498#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004499 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004500 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004501 __buf.first, __buf.second);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004502#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004503}
4504
4505template <class _BidirectionalIterator>
4506inline _LIBCPP_INLINE_VISIBILITY
4507void
4508inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4509{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004510 _VSTD::inplace_merge(__first, __middle, __last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004511 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4512}
4513
4514// stable_sort
4515
4516template <class _Compare, class _InputIterator1, class _InputIterator2>
4517void
4518__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4519 _InputIterator2 __first2, _InputIterator2 __last2,
4520 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4521{
4522 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4523 __destruct_n __d(0);
4524 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4525 for (; true; ++__result)
4526 {
4527 if (__first1 == __last1)
4528 {
4529 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004530 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004531 __h.release();
4532 return;
4533 }
4534 if (__first2 == __last2)
4535 {
4536 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004537 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004538 __h.release();
4539 return;
4540 }
4541 if (__comp(*__first2, *__first1))
4542 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004543 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004544 __d.__incr((value_type*)0);
4545 ++__first2;
4546 }
4547 else
4548 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004549 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004550 __d.__incr((value_type*)0);
4551 ++__first1;
4552 }
4553 }
4554}
4555
4556template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4557void
4558__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4559 _InputIterator2 __first2, _InputIterator2 __last2,
4560 _OutputIterator __result, _Compare __comp)
4561{
4562 for (; __first1 != __last1; ++__result)
4563 {
4564 if (__first2 == __last2)
4565 {
4566 for (; __first1 != __last1; ++__first1, ++__result)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004567 *__result = _VSTD::move(*__first1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004568 return;
4569 }
4570 if (__comp(*__first2, *__first1))
4571 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004572 *__result = _VSTD::move(*__first2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004573 ++__first2;
4574 }
4575 else
4576 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004577 *__result = _VSTD::move(*__first1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004578 ++__first1;
4579 }
4580 }
4581 for (; __first2 != __last2; ++__first2, ++__result)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004582 *__result = _VSTD::move(*__first2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004583}
4584
4585template <class _Compare, class _RandomAccessIterator>
4586void
4587__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4588 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4589 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4590
4591template <class _Compare, class _RandomAccessIterator>
4592void
4593__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4594 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4595 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4596{
4597 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4598 switch (__len)
4599 {
4600 case 0:
4601 return;
4602 case 1:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004603 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004604 return;
4605 case 2:
Marshall Clow32043ac2018-02-06 18:58:05 +00004606 __destruct_n __d(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004607 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
Marshall Clow32043ac2018-02-06 18:58:05 +00004608 if (__comp(*--__last1, *__first1))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004609 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004610 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004611 __d.__incr((value_type*)0);
4612 ++__first2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004613 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004614 }
4615 else
4616 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004617 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004618 __d.__incr((value_type*)0);
4619 ++__first2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004620 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004621 }
4622 __h2.release();
4623 return;
4624 }
4625 if (__len <= 8)
4626 {
4627 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4628 return;
4629 }
4630 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4631 _RandomAccessIterator __m = __first1 + __l2;
4632 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4633 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4634 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4635}
4636
4637template <class _Tp>
4638struct __stable_sort_switch
4639{
Howard Hinnanta9a897e2010-11-19 22:17:28 +00004640 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004641};
4642
4643template <class _Compare, class _RandomAccessIterator>
4644void
4645__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4646 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4647 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4648{
4649 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4650 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4651 switch (__len)
4652 {
4653 case 0:
4654 case 1:
4655 return;
4656 case 2:
4657 if (__comp(*--__last, *__first))
4658 swap(*__first, *__last);
4659 return;
4660 }
4661 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4662 {
4663 __insertion_sort<_Compare>(__first, __last, __comp);
4664 return;
4665 }
4666 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4667 _RandomAccessIterator __m = __first + __l2;
4668 if (__len <= __buff_size)
4669 {
4670 __destruct_n __d(0);
4671 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4672 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4673 __d.__set(__l2, (value_type*)0);
4674 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4675 __d.__set(__len, (value_type*)0);
4676 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4677// __merge<_Compare>(move_iterator<value_type*>(__buff),
4678// move_iterator<value_type*>(__buff + __l2),
4679// move_iterator<_RandomAccessIterator>(__buff + __l2),
4680// move_iterator<_RandomAccessIterator>(__buff + __len),
4681// __first, __comp);
4682 return;
4683 }
4684 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4685 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4686 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4687}
4688
4689template <class _RandomAccessIterator, class _Compare>
4690inline _LIBCPP_INLINE_VISIBILITY
4691void
4692stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4693{
4694 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4695 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4696 difference_type __len = __last - __first;
4697 pair<value_type*, ptrdiff_t> __buf(0, 0);
4698 unique_ptr<value_type, __return_temporary_buffer> __h;
4699 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4700 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004701 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004702 __h.reset(__buf.first);
4703 }
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004704#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004705 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4706 __debug_less<_Compare> __c(__comp);
4707 __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004708#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004709 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4710 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004711#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004712}
4713
4714template <class _RandomAccessIterator>
4715inline _LIBCPP_INLINE_VISIBILITY
4716void
4717stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4718{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004719 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004720}
4721
4722// is_heap_until
4723
4724template <class _RandomAccessIterator, class _Compare>
Marshall Clow96d050a2018-01-15 16:16:32 +00004725_LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00004726is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4727{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004728 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004729 difference_type __len = __last - __first;
4730 difference_type __p = 0;
4731 difference_type __c = 1;
4732 _RandomAccessIterator __pp = __first;
4733 while (__c < __len)
4734 {
4735 _RandomAccessIterator __cp = __first + __c;
4736 if (__comp(*__pp, *__cp))
4737 return __cp;
4738 ++__c;
4739 ++__cp;
4740 if (__c == __len)
4741 return __last;
4742 if (__comp(*__pp, *__cp))
4743 return __cp;
4744 ++__p;
4745 ++__pp;
4746 __c = 2 * __p + 1;
4747 }
4748 return __last;
4749}
4750
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004751template<class _RandomAccessIterator>
Marshall Clow96d050a2018-01-15 16:16:32 +00004752inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004753_RandomAccessIterator
4754is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4755{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004756 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004757}
4758
4759// is_heap
4760
4761template <class _RandomAccessIterator, class _Compare>
Marshall Clow96d050a2018-01-15 16:16:32 +00004762inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004763bool
4764is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4765{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004766 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004767}
4768
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004769template<class _RandomAccessIterator>
Marshall Clow96d050a2018-01-15 16:16:32 +00004770inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004771bool
4772is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4773{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004774 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004775}
4776
4777// push_heap
4778
4779template <class _Compare, class _RandomAccessIterator>
4780void
David Majnemer4468d562014-07-22 06:07:09 +00004781__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4782 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004783{
Howard Hinnantc51e1022010-05-11 19:42:16 +00004784 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4785 if (__len > 1)
4786 {
4787 __len = (__len - 2) / 2;
4788 _RandomAccessIterator __ptr = __first + __len;
4789 if (__comp(*__ptr, *--__last))
4790 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004791 value_type __t(_VSTD::move(*__last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004792 do
4793 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004794 *__last = _VSTD::move(*__ptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004795 __last = __ptr;
4796 if (__len == 0)
4797 break;
4798 __len = (__len - 1) / 2;
4799 __ptr = __first + __len;
4800 } while (__comp(*__ptr, __t));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004801 *__last = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004802 }
4803 }
4804}
4805
4806template <class _RandomAccessIterator, class _Compare>
4807inline _LIBCPP_INLINE_VISIBILITY
4808void
4809push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4810{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004811#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004812 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4813 __debug_less<_Compare> __c(__comp);
David Majnemer4468d562014-07-22 06:07:09 +00004814 __sift_up<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004815#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004816 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
David Majnemer4468d562014-07-22 06:07:09 +00004817 __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004818#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004819}
4820
4821template <class _RandomAccessIterator>
4822inline _LIBCPP_INLINE_VISIBILITY
4823void
4824push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4825{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004826 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004827}
4828
4829// pop_heap
4830
4831template <class _Compare, class _RandomAccessIterator>
David Majnemer4468d562014-07-22 06:07:09 +00004832void
Eric Fiselier6003c772016-12-23 23:37:52 +00004833__sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
4834 _Compare __comp,
David Majnemer4468d562014-07-22 06:07:09 +00004835 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4836 _RandomAccessIterator __start)
4837{
4838 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4839 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4840 // left-child of __start is at 2 * __start + 1
4841 // right-child of __start is at 2 * __start + 2
4842 difference_type __child = __start - __first;
4843
4844 if (__len < 2 || (__len - 2) / 2 < __child)
4845 return;
4846
4847 __child = 2 * __child + 1;
4848 _RandomAccessIterator __child_i = __first + __child;
4849
4850 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4851 // right-child exists and is greater than left-child
4852 ++__child_i;
4853 ++__child;
4854 }
4855
4856 // check if we are in heap-order
4857 if (__comp(*__child_i, *__start))
4858 // we are, __start is larger than it's largest child
4859 return;
4860
4861 value_type __top(_VSTD::move(*__start));
4862 do
4863 {
4864 // we are not in heap-order, swap the parent with it's largest child
4865 *__start = _VSTD::move(*__child_i);
4866 __start = __child_i;
4867
4868 if ((__len - 2) / 2 < __child)
4869 break;
4870
4871 // recompute the child based off of the updated parent
4872 __child = 2 * __child + 1;
4873 __child_i = __first + __child;
4874
4875 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4876 // right-child exists and is greater than left-child
4877 ++__child_i;
4878 ++__child;
4879 }
4880
4881 // check if we are in heap-order
4882 } while (!__comp(*__child_i, __top));
4883 *__start = _VSTD::move(__top);
4884}
4885
4886template <class _Compare, class _RandomAccessIterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004887inline _LIBCPP_INLINE_VISIBILITY
4888void
4889__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4890 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4891{
4892 if (__len > 1)
4893 {
4894 swap(*__first, *--__last);
David Majnemer4468d562014-07-22 06:07:09 +00004895 __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004896 }
4897}
4898
4899template <class _RandomAccessIterator, class _Compare>
4900inline _LIBCPP_INLINE_VISIBILITY
4901void
4902pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4903{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004904#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004905 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4906 __debug_less<_Compare> __c(__comp);
4907 __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004908#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004909 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4910 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004911#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004912}
4913
4914template <class _RandomAccessIterator>
4915inline _LIBCPP_INLINE_VISIBILITY
4916void
4917pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4918{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004919 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004920}
4921
4922// make_heap
4923
4924template <class _Compare, class _RandomAccessIterator>
4925void
4926__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4927{
4928 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4929 difference_type __n = __last - __first;
4930 if (__n > 1)
4931 {
David Majnemer4468d562014-07-22 06:07:09 +00004932 // start from the first parent, there is no need to consider children
4933 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
4934 {
4935 __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
4936 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004937 }
4938}
4939
4940template <class _RandomAccessIterator, class _Compare>
4941inline _LIBCPP_INLINE_VISIBILITY
4942void
4943make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4944{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004945#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004946 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4947 __debug_less<_Compare> __c(__comp);
4948 __make_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004949#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004950 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4951 __make_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004952#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004953}
4954
4955template <class _RandomAccessIterator>
4956inline _LIBCPP_INLINE_VISIBILITY
4957void
4958make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4959{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004960 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004961}
4962
4963// sort_heap
4964
4965template <class _Compare, class _RandomAccessIterator>
4966void
4967__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4968{
4969 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4970 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
4971 __pop_heap<_Compare>(__first, __last, __comp, __n);
4972}
4973
4974template <class _RandomAccessIterator, class _Compare>
4975inline _LIBCPP_INLINE_VISIBILITY
4976void
4977sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4978{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004979#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004980 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
4981 __debug_less<_Compare> __c(__comp);
4982 __sort_heap<_Comp_ref>(__first, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004983#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004984 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4985 __sort_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00004986#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004987}
4988
4989template <class _RandomAccessIterator>
4990inline _LIBCPP_INLINE_VISIBILITY
4991void
4992sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4993{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004994 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004995}
4996
4997// partial_sort
4998
4999template <class _Compare, class _RandomAccessIterator>
5000void
5001__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5002 _Compare __comp)
5003{
5004 __make_heap<_Compare>(__first, __middle, __comp);
5005 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5006 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5007 {
5008 if (__comp(*__i, *__first))
5009 {
5010 swap(*__i, *__first);
David Majnemer4468d562014-07-22 06:07:09 +00005011 __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005012 }
5013 }
5014 __sort_heap<_Compare>(__first, __middle, __comp);
5015}
5016
5017template <class _RandomAccessIterator, class _Compare>
5018inline _LIBCPP_INLINE_VISIBILITY
5019void
5020partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5021 _Compare __comp)
5022{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005023#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005024 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5025 __debug_less<_Compare> __c(__comp);
5026 __partial_sort<_Comp_ref>(__first, __middle, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005027#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005028 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5029 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005030#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005031}
5032
5033template <class _RandomAccessIterator>
5034inline _LIBCPP_INLINE_VISIBILITY
5035void
5036partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5037{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005038 _VSTD::partial_sort(__first, __middle, __last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005039 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5040}
5041
5042// partial_sort_copy
5043
5044template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5045_RandomAccessIterator
5046__partial_sort_copy(_InputIterator __first, _InputIterator __last,
5047 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5048{
5049 _RandomAccessIterator __r = __result_first;
5050 if (__r != __result_last)
5051 {
Eric Fiseliera09a3b42014-10-27 19:28:20 +00005052 for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005053 *__r = *__first;
5054 __make_heap<_Compare>(__result_first, __r, __comp);
David Majnemer4468d562014-07-22 06:07:09 +00005055 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005056 for (; __first != __last; ++__first)
5057 if (__comp(*__first, *__result_first))
5058 {
5059 *__result_first = *__first;
David Majnemer4468d562014-07-22 06:07:09 +00005060 __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005061 }
5062 __sort_heap<_Compare>(__result_first, __r, __comp);
5063 }
5064 return __r;
5065}
5066
5067template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5068inline _LIBCPP_INLINE_VISIBILITY
5069_RandomAccessIterator
5070partial_sort_copy(_InputIterator __first, _InputIterator __last,
5071 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5072{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005073#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005074 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5075 __debug_less<_Compare> __c(__comp);
5076 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005077#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005078 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5079 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005080#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005081}
5082
5083template <class _InputIterator, class _RandomAccessIterator>
5084inline _LIBCPP_INLINE_VISIBILITY
5085_RandomAccessIterator
5086partial_sort_copy(_InputIterator __first, _InputIterator __last,
5087 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5088{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005089 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005090 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5091}
5092
5093// nth_element
5094
5095template <class _Compare, class _RandomAccessIterator>
5096void
5097__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5098{
5099 // _Compare is known to be a reference type
5100 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5101 const difference_type __limit = 7;
5102 while (true)
5103 {
5104 __restart:
Howard Hinnant2fa038c2011-12-29 17:45:35 +00005105 if (__nth == __last)
5106 return;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005107 difference_type __len = __last - __first;
5108 switch (__len)
5109 {
5110 case 0:
5111 case 1:
5112 return;
5113 case 2:
5114 if (__comp(*--__last, *__first))
5115 swap(*__first, *__last);
5116 return;
5117 case 3:
5118 {
5119 _RandomAccessIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005120 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005121 return;
5122 }
5123 }
5124 if (__len <= __limit)
5125 {
5126 __selection_sort<_Compare>(__first, __last, __comp);
5127 return;
5128 }
5129 // __len > __limit >= 3
5130 _RandomAccessIterator __m = __first + __len/2;
5131 _RandomAccessIterator __lm1 = __last;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005132 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005133 // *__m is median
5134 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5135 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5136 _RandomAccessIterator __i = __first;
5137 _RandomAccessIterator __j = __lm1;
5138 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5139 // The search going up is known to be guarded but the search coming down isn't.
5140 // Prime the downward search with a guard.
5141 if (!__comp(*__i, *__m)) // if *__first == *__m
5142 {
5143 // *__first == *__m, *__first doesn't go in first part
5144 // manually guard downward moving __j against __i
5145 while (true)
5146 {
5147 if (__i == --__j)
5148 {
5149 // *__first == *__m, *__m <= all other elements
5150 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5151 ++__i; // __first + 1
5152 __j = __last;
5153 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5154 {
5155 while (true)
5156 {
5157 if (__i == __j)
5158 return; // [__first, __last) all equivalent elements
5159 if (__comp(*__first, *__i))
5160 {
5161 swap(*__i, *__j);
5162 ++__n_swaps;
5163 ++__i;
5164 break;
5165 }
5166 ++__i;
5167 }
5168 }
5169 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5170 if (__i == __j)
5171 return;
5172 while (true)
5173 {
5174 while (!__comp(*__first, *__i))
5175 ++__i;
5176 while (__comp(*__first, *--__j))
5177 ;
5178 if (__i >= __j)
5179 break;
5180 swap(*__i, *__j);
5181 ++__n_swaps;
5182 ++__i;
5183 }
5184 // [__first, __i) == *__first and *__first < [__i, __last)
5185 // The first part is sorted,
5186 if (__nth < __i)
5187 return;
5188 // __nth_element the secod part
5189 // __nth_element<_Compare>(__i, __nth, __last, __comp);
5190 __first = __i;
5191 goto __restart;
5192 }
5193 if (__comp(*__j, *__m))
5194 {
5195 swap(*__i, *__j);
5196 ++__n_swaps;
5197 break; // found guard for downward moving __j, now use unguarded partition
5198 }
5199 }
5200 }
5201 ++__i;
5202 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5203 // if not yet partitioned...
5204 if (__i < __j)
5205 {
5206 // known that *(__i - 1) < *__m
5207 while (true)
5208 {
5209 // __m still guards upward moving __i
5210 while (__comp(*__i, *__m))
5211 ++__i;
5212 // It is now known that a guard exists for downward moving __j
5213 while (!__comp(*--__j, *__m))
5214 ;
5215 if (__i >= __j)
5216 break;
5217 swap(*__i, *__j);
5218 ++__n_swaps;
5219 // It is known that __m != __j
5220 // If __m just moved, follow it
5221 if (__m == __i)
5222 __m = __j;
5223 ++__i;
5224 }
5225 }
5226 // [__first, __i) < *__m and *__m <= [__i, __last)
5227 if (__i != __m && __comp(*__m, *__i))
5228 {
5229 swap(*__i, *__m);
5230 ++__n_swaps;
5231 }
5232 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5233 if (__nth == __i)
5234 return;
5235 if (__n_swaps == 0)
5236 {
5237 // We were given a perfectly partitioned sequence. Coincidence?
5238 if (__nth < __i)
5239 {
5240 // Check for [__first, __i) already sorted
5241 __j = __m = __first;
5242 while (++__j != __i)
5243 {
5244 if (__comp(*__j, *__m))
5245 // not yet sorted, so sort
5246 goto not_sorted;
5247 __m = __j;
5248 }
5249 // [__first, __i) sorted
5250 return;
5251 }
5252 else
5253 {
5254 // Check for [__i, __last) already sorted
5255 __j = __m = __i;
5256 while (++__j != __last)
5257 {
5258 if (__comp(*__j, *__m))
5259 // not yet sorted, so sort
5260 goto not_sorted;
5261 __m = __j;
5262 }
5263 // [__i, __last) sorted
5264 return;
5265 }
5266 }
5267not_sorted:
5268 // __nth_element on range containing __nth
5269 if (__nth < __i)
5270 {
5271 // __nth_element<_Compare>(__first, __nth, __i, __comp);
5272 __last = __i;
5273 }
5274 else
5275 {
5276 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5277 __first = ++__i;
5278 }
5279 }
5280}
5281
5282template <class _RandomAccessIterator, class _Compare>
5283inline _LIBCPP_INLINE_VISIBILITY
5284void
5285nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5286{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005287#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005288 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5289 __debug_less<_Compare> __c(__comp);
5290 __nth_element<_Comp_ref>(__first, __nth, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005291#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005292 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5293 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005294#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005295}
5296
5297template <class _RandomAccessIterator>
5298inline _LIBCPP_INLINE_VISIBILITY
5299void
5300nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5301{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005302 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00005303}
5304
5305// includes
5306
5307template <class _Compare, class _InputIterator1, class _InputIterator2>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005308_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00005309__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5310 _Compare __comp)
5311{
5312 for (; __first2 != __last2; ++__first1)
5313 {
5314 if (__first1 == __last1 || __comp(*__first2, *__first1))
5315 return false;
5316 if (!__comp(*__first1, *__first2))
5317 ++__first2;
5318 }
5319 return true;
5320}
5321
5322template <class _InputIterator1, class _InputIterator2, class _Compare>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005323inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005324bool
5325includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5326 _Compare __comp)
5327{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005328#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005329 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5330 __debug_less<_Compare> __c(__comp);
5331 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005332#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005333 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5334 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005335#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005336}
5337
5338template <class _InputIterator1, class _InputIterator2>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005339inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005340bool
5341includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5342{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005343 return _VSTD::includes(__first1, __last1, __first2, __last2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005344 __less<typename iterator_traits<_InputIterator1>::value_type,
5345 typename iterator_traits<_InputIterator2>::value_type>());
5346}
5347
5348// set_union
5349
5350template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5351_OutputIterator
5352__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5353 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5354{
5355 for (; __first1 != __last1; ++__result)
5356 {
5357 if (__first2 == __last2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005358 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005359 if (__comp(*__first2, *__first1))
5360 {
5361 *__result = *__first2;
5362 ++__first2;
5363 }
5364 else
5365 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00005366 if (!__comp(*__first1, *__first2))
5367 ++__first2;
Marshall Clowb4687412017-10-30 15:50:00 +00005368 *__result = *__first1;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005369 ++__first1;
5370 }
5371 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005372 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005373}
5374
5375template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5376inline _LIBCPP_INLINE_VISIBILITY
5377_OutputIterator
5378set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5379 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5380{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005381#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005382 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5383 __debug_less<_Compare> __c(__comp);
5384 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005385#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005386 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5387 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005388#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005389}
5390
5391template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5392inline _LIBCPP_INLINE_VISIBILITY
5393_OutputIterator
5394set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5395 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5396{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005397 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005398 __less<typename iterator_traits<_InputIterator1>::value_type,
5399 typename iterator_traits<_InputIterator2>::value_type>());
5400}
5401
5402// set_intersection
5403
5404template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005405_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00005406__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5407 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5408{
5409 while (__first1 != __last1 && __first2 != __last2)
5410 {
5411 if (__comp(*__first1, *__first2))
5412 ++__first1;
5413 else
5414 {
5415 if (!__comp(*__first2, *__first1))
5416 {
5417 *__result = *__first1;
5418 ++__result;
5419 ++__first1;
5420 }
5421 ++__first2;
5422 }
5423 }
5424 return __result;
5425}
5426
5427template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005428inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005429_OutputIterator
5430set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5431 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5432{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005433#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005434 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5435 __debug_less<_Compare> __c(__comp);
5436 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005437#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005438 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5439 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005440#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005441}
5442
5443template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005444inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005445_OutputIterator
5446set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5447 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5448{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005449 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005450 __less<typename iterator_traits<_InputIterator1>::value_type,
5451 typename iterator_traits<_InputIterator2>::value_type>());
5452}
5453
5454// set_difference
5455
5456template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5457_OutputIterator
5458__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5459 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5460{
5461 while (__first1 != __last1)
5462 {
5463 if (__first2 == __last2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005464 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005465 if (__comp(*__first1, *__first2))
5466 {
5467 *__result = *__first1;
5468 ++__result;
5469 ++__first1;
5470 }
5471 else
5472 {
5473 if (!__comp(*__first2, *__first1))
5474 ++__first1;
5475 ++__first2;
5476 }
5477 }
5478 return __result;
5479}
5480
5481template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5482inline _LIBCPP_INLINE_VISIBILITY
5483_OutputIterator
5484set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5485 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5486{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005487#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005488 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5489 __debug_less<_Compare> __c(__comp);
5490 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005491#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005492 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5493 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005494#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005495}
5496
5497template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5498inline _LIBCPP_INLINE_VISIBILITY
5499_OutputIterator
5500set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5501 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5502{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005503 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005504 __less<typename iterator_traits<_InputIterator1>::value_type,
5505 typename iterator_traits<_InputIterator2>::value_type>());
5506}
5507
5508// set_symmetric_difference
5509
5510template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5511_OutputIterator
5512__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5513 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5514{
5515 while (__first1 != __last1)
5516 {
5517 if (__first2 == __last2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005518 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005519 if (__comp(*__first1, *__first2))
5520 {
5521 *__result = *__first1;
5522 ++__result;
5523 ++__first1;
5524 }
5525 else
5526 {
5527 if (__comp(*__first2, *__first1))
5528 {
5529 *__result = *__first2;
5530 ++__result;
5531 }
5532 else
5533 ++__first1;
5534 ++__first2;
5535 }
5536 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005537 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005538}
5539
5540template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5541inline _LIBCPP_INLINE_VISIBILITY
5542_OutputIterator
5543set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5544 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5545{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005546#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005547 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5548 __debug_less<_Compare> __c(__comp);
5549 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005550#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005551 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5552 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005553#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005554}
5555
5556template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5557inline _LIBCPP_INLINE_VISIBILITY
5558_OutputIterator
5559set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5560 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5561{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005562 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005563 __less<typename iterator_traits<_InputIterator1>::value_type,
5564 typename iterator_traits<_InputIterator2>::value_type>());
5565}
5566
5567// lexicographical_compare
5568
5569template <class _Compare, class _InputIterator1, class _InputIterator2>
Marshall Clow5492c8a2018-01-22 20:44:33 +00005570_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00005571__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5572 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5573{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00005574 for (; __first2 != __last2; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005575 {
5576 if (__first1 == __last1 || __comp(*__first1, *__first2))
5577 return true;
5578 if (__comp(*__first2, *__first1))
5579 return false;
5580 }
5581 return false;
5582}
5583
5584template <class _InputIterator1, class _InputIterator2, class _Compare>
Marshall Clow5492c8a2018-01-22 20:44:33 +00005585inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005586bool
5587lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5588 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5589{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005590#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005591 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5592 __debug_less<_Compare> __c(__comp);
5593 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005594#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005595 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5596 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005597#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005598}
5599
5600template <class _InputIterator1, class _InputIterator2>
Marshall Clow5492c8a2018-01-22 20:44:33 +00005601inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005602bool
5603lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5604 _InputIterator2 __first2, _InputIterator2 __last2)
5605{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005606 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005607 __less<typename iterator_traits<_InputIterator1>::value_type,
5608 typename iterator_traits<_InputIterator2>::value_type>());
5609}
5610
5611// next_permutation
5612
5613template <class _Compare, class _BidirectionalIterator>
5614bool
5615__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5616{
5617 _BidirectionalIterator __i = __last;
5618 if (__first == __last || __first == --__i)
5619 return false;
5620 while (true)
5621 {
5622 _BidirectionalIterator __ip1 = __i;
5623 if (__comp(*--__i, *__ip1))
5624 {
5625 _BidirectionalIterator __j = __last;
5626 while (!__comp(*__i, *--__j))
5627 ;
5628 swap(*__i, *__j);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005629 _VSTD::reverse(__ip1, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005630 return true;
5631 }
5632 if (__i == __first)
5633 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005634 _VSTD::reverse(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005635 return false;
5636 }
5637 }
5638}
5639
5640template <class _BidirectionalIterator, class _Compare>
5641inline _LIBCPP_INLINE_VISIBILITY
5642bool
5643next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5644{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005645#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005646 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5647 __debug_less<_Compare> __c(__comp);
5648 return __next_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005649#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005650 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5651 return __next_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005652#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005653}
5654
5655template <class _BidirectionalIterator>
5656inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005657bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00005658next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5659{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005660 return _VSTD::next_permutation(__first, __last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005661 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5662}
5663
5664// prev_permutation
5665
5666template <class _Compare, class _BidirectionalIterator>
5667bool
5668__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5669{
5670 _BidirectionalIterator __i = __last;
5671 if (__first == __last || __first == --__i)
5672 return false;
5673 while (true)
5674 {
5675 _BidirectionalIterator __ip1 = __i;
5676 if (__comp(*__ip1, *--__i))
5677 {
5678 _BidirectionalIterator __j = __last;
5679 while (!__comp(*--__j, *__i))
5680 ;
5681 swap(*__i, *__j);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005682 _VSTD::reverse(__ip1, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005683 return true;
5684 }
5685 if (__i == __first)
5686 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005687 _VSTD::reverse(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005688 return false;
5689 }
5690 }
5691}
5692
5693template <class _BidirectionalIterator, class _Compare>
5694inline _LIBCPP_INLINE_VISIBILITY
5695bool
5696prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5697{
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005698#ifdef _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005699 typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref;
5700 __debug_less<_Compare> __c(__comp);
5701 return __prev_permutation<_Comp_ref>(__first, __last, __c);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005702#else // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005703 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
5704 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant6148a9b2013-08-23 20:10:18 +00005705#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00005706}
5707
5708template <class _BidirectionalIterator>
5709inline _LIBCPP_INLINE_VISIBILITY
5710bool
5711prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5712{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005713 return _VSTD::prev_permutation(__first, __last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005714 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5715}
5716
Howard Hinnantc51e1022010-05-11 19:42:16 +00005717_LIBCPP_END_NAMESPACE_STD
5718
Eric Fiselierf4433a32017-05-31 22:07:49 +00005719_LIBCPP_POP_MACROS
5720
Howard Hinnantc51e1022010-05-11 19:42:16 +00005721#endif // _LIBCPP_ALGORITHM