blob: 326e566fbf78b80c90e464c153a6c2aa571f4a74 [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{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +0000783 _Compare &__comp_;
784 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000785 __debug_less(_Compare& __c) : __comp_(__c) {}
Eric Fiselier32adec82016-07-19 23:27:18 +0000786
Howard Hinnantc51e1022010-05-11 19:42:16 +0000787 template <class _Tp, class _Up>
Thomas Andersona371b732019-04-19 00:52:54 +0000788 _LIBCPP_CONSTEXPR_AFTER_CXX17
Thomas Andersond364ec92019-04-15 17:02:15 +0000789 bool operator()(const _Tp& __x, const _Up& __y)
790 {
791 bool __r = __comp_(__x, __y);
792 if (__r)
793 __do_compare_assert(0, __y, __x);
794 return __r;
795 }
796
797 template <class _Tp, class _Up>
Eric Fiselier7ceab7b2019-04-12 05:18:19 +0000798 _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier6dfe5232019-03-08 22:58:59 +0000799 bool operator()(_Tp& __x, _Up& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000800 {
801 bool __r = __comp_(__x, __y);
802 if (__r)
Eric Fiselier32adec82016-07-19 23:27:18 +0000803 __do_compare_assert(0, __y, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000804 return __r;
805 }
Eric Fiselier32adec82016-07-19 23:27:18 +0000806
807 template <class _LHS, class _RHS>
Eric Fiselier7ceab7b2019-04-12 05:18:19 +0000808 _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier32adec82016-07-19 23:27:18 +0000809 inline _LIBCPP_INLINE_VISIBILITY
810 decltype((void)_VSTD::declval<_Compare&>()(
Eric Fiselier6dfe5232019-03-08 22:58:59 +0000811 _VSTD::declval<_LHS &>(), _VSTD::declval<_RHS &>()))
812 __do_compare_assert(int, _LHS & __l, _RHS & __r) {
Eric Fiselier32adec82016-07-19 23:27:18 +0000813 _LIBCPP_ASSERT(!__comp_(__l, __r),
814 "Comparator does not induce a strict weak ordering");
815 }
816
817 template <class _LHS, class _RHS>
Eric Fiselier7ceab7b2019-04-12 05:18:19 +0000818 _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier32adec82016-07-19 23:27:18 +0000819 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6dfe5232019-03-08 22:58:59 +0000820 void __do_compare_assert(long, _LHS &, _RHS &) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821};
822
Eric Fiselier6dfe5232019-03-08 22:58:59 +0000823#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000824
Eric Fiselier7ceab7b2019-04-12 05:18:19 +0000825template <class _Comp>
826struct __comp_ref_type {
827 // Pass the comparator by lvalue reference. Or in debug mode, using a
828 // debugging wrapper that stores a reference.
829#ifndef _LIBCPP_DEBUG
830 typedef typename add_lvalue_reference<_Comp>::type type;
831#else
832 typedef __debug_less<_Comp> type;
833#endif
834};
835
Howard Hinnantc51e1022010-05-11 19:42:16 +0000836// all_of
837
838template <class _InputIterator, class _Predicate>
Nico Weber471b10a2019-04-03 18:13:08 +0000839_LIBCPP_NODISCARD_EXT inline
840_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000841bool
842all_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// any_of
851
852template <class _InputIterator, class _Predicate>
Nico Weber471b10a2019-04-03 18:13:08 +0000853_LIBCPP_NODISCARD_EXT inline
854_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000855bool
856any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
857{
858 for (; __first != __last; ++__first)
859 if (__pred(*__first))
860 return true;
861 return false;
862}
863
864// none_of
865
866template <class _InputIterator, class _Predicate>
Nico Weber471b10a2019-04-03 18:13:08 +0000867_LIBCPP_NODISCARD_EXT inline
868_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000869bool
870none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
871{
872 for (; __first != __last; ++__first)
873 if (__pred(*__first))
874 return false;
875 return true;
876}
877
878// for_each
879
880template <class _InputIterator, class _Function>
Marshall Clow5492c8a2018-01-22 20:44:33 +0000881inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000882_Function
883for_each(_InputIterator __first, _InputIterator __last, _Function __f)
884{
885 for (; __first != __last; ++__first)
886 __f(*__first);
Marshall Clow78dbe462016-11-14 18:22:19 +0000887 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000888}
889
Marshall Clowc588fd62017-05-25 13:40:57 +0000890#if _LIBCPP_STD_VER > 14
Marshall Clowde0169c2017-05-25 02:29:54 +0000891// for_each_n
892
893template <class _InputIterator, class _Size, class _Function>
Marshall Clow5492c8a2018-01-22 20:44:33 +0000894inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowde0169c2017-05-25 02:29:54 +0000895_InputIterator
896for_each_n(_InputIterator __first, _Size __orig_n, _Function __f)
897{
898 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
899 _IntegralSize __n = __orig_n;
900 while (__n > 0)
901 {
902 __f(*__first);
903 ++__first;
904 --__n;
905 }
906 return __first;
907}
Marshall Clowc588fd62017-05-25 13:40:57 +0000908#endif
Marshall Clowde0169c2017-05-25 02:29:54 +0000909
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910// find
911
912template <class _InputIterator, class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +0000913_LIBCPP_NODISCARD_EXT inline
914_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000915_InputIterator
Howard Hinnantbf074022011-10-22 20:59:45 +0000916find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000917{
918 for (; __first != __last; ++__first)
Howard Hinnantbf074022011-10-22 20:59:45 +0000919 if (*__first == __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000920 break;
921 return __first;
922}
923
924// find_if
925
926template <class _InputIterator, class _Predicate>
Nico Weber471b10a2019-04-03 18:13:08 +0000927_LIBCPP_NODISCARD_EXT inline
928_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000929_InputIterator
930find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
931{
932 for (; __first != __last; ++__first)
933 if (__pred(*__first))
934 break;
935 return __first;
936}
937
938// find_if_not
939
940template<class _InputIterator, class _Predicate>
Nico Weber471b10a2019-04-03 18:13:08 +0000941_LIBCPP_NODISCARD_EXT inline
942_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000943_InputIterator
944find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
945{
946 for (; __first != __last; ++__first)
947 if (!__pred(*__first))
948 break;
949 return __first;
950}
951
952// find_end
953
954template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clowee0161e2018-01-15 19:26:05 +0000955_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
957 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
958 forward_iterator_tag, forward_iterator_tag)
959{
960 // modeled after search algorithm
961 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
962 if (__first2 == __last2)
963 return __r;
964 while (true)
965 {
966 while (true)
967 {
968 if (__first1 == __last1) // if source exhausted return last correct answer
969 return __r; // (or __last1 if never found)
970 if (__pred(*__first1, *__first2))
971 break;
972 ++__first1;
973 }
974 // *__first1 matches *__first2, now match elements after here
975 _ForwardIterator1 __m1 = __first1;
976 _ForwardIterator2 __m2 = __first2;
977 while (true)
978 {
979 if (++__m2 == __last2)
980 { // Pattern exhaused, record answer and search for another one
981 __r = __first1;
982 ++__first1;
983 break;
984 }
985 if (++__m1 == __last1) // Source exhausted, return last answer
986 return __r;
987 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
988 {
989 ++__first1;
990 break;
991 } // else there is a match, check next elements
992 }
993 }
994}
995
996template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
Marshall Clowee0161e2018-01-15 19:26:05 +0000997_LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator1
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
999 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
1000 bidirectional_iterator_tag, bidirectional_iterator_tag)
1001{
1002 // modeled after search algorithm (in reverse)
1003 if (__first2 == __last2)
1004 return __last1; // Everything matches an empty sequence
1005 _BidirectionalIterator1 __l1 = __last1;
1006 _BidirectionalIterator2 __l2 = __last2;
1007 --__l2;
1008 while (true)
1009 {
1010 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
1011 while (true)
1012 {
1013 if (__first1 == __l1) // return __last1 if no element matches *__first2
1014 return __last1;
1015 if (__pred(*--__l1, *__l2))
1016 break;
1017 }
1018 // *__l1 matches *__l2, now match elements before here
1019 _BidirectionalIterator1 __m1 = __l1;
1020 _BidirectionalIterator2 __m2 = __l2;
1021 while (true)
1022 {
1023 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
1024 return __m1;
1025 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
1026 return __last1;
1027 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
1028 {
1029 break;
1030 } // else there is a match, check next elements
1031 }
1032 }
1033}
1034
1035template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clowedfd25a2014-06-10 18:51:55 +00001036_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1038 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1039 random_access_iterator_tag, random_access_iterator_tag)
1040{
1041 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1042 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
1043 if (__len2 == 0)
1044 return __last1;
1045 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
1046 if (__len1 < __len2)
1047 return __last1;
1048 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
1049 _RandomAccessIterator1 __l1 = __last1;
1050 _RandomAccessIterator2 __l2 = __last2;
1051 --__l2;
1052 while (true)
1053 {
1054 while (true)
1055 {
1056 if (__s == __l1)
1057 return __last1;
1058 if (__pred(*--__l1, *__l2))
1059 break;
1060 }
1061 _RandomAccessIterator1 __m1 = __l1;
1062 _RandomAccessIterator2 __m2 = __l2;
1063 while (true)
1064 {
1065 if (__m2 == __first2)
1066 return __m1;
1067 // no need to check range on __m1 because __s guarantees we have enough source
1068 if (!__pred(*--__m1, *--__m2))
1069 {
1070 break;
1071 }
1072 }
1073 }
1074}
1075
1076template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001077_LIBCPP_NODISCARD_EXT inline
1078_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079_ForwardIterator1
1080find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1081 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1082{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001083 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084 (__first1, __last1, __first2, __last2, __pred,
1085 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1086 typename iterator_traits<_ForwardIterator2>::iterator_category());
1087}
1088
1089template <class _ForwardIterator1, class _ForwardIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00001090_LIBCPP_NODISCARD_EXT inline
1091_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092_ForwardIterator1
1093find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1094 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1095{
1096 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1097 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001098 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099}
1100
1101// find_first_of
1102
1103template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clowedfd25a2014-06-10 18:51:55 +00001104_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1105__find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1107{
1108 for (; __first1 != __last1; ++__first1)
1109 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1110 if (__pred(*__first1, *__j))
1111 return __first1;
1112 return __last1;
1113}
1114
Marshall Clowedfd25a2014-06-10 18:51:55 +00001115
1116template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001117_LIBCPP_NODISCARD_EXT inline
1118_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowedfd25a2014-06-10 18:51:55 +00001119_ForwardIterator1
1120find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1121 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1122{
1123 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1124}
1125
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126template <class _ForwardIterator1, class _ForwardIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00001127_LIBCPP_NODISCARD_EXT inline
1128_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129_ForwardIterator1
1130find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1131 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1132{
1133 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1134 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Marshall Clowedfd25a2014-06-10 18:51:55 +00001135 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136}
1137
1138// adjacent_find
1139
1140template <class _ForwardIterator, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001141_LIBCPP_NODISCARD_EXT inline
1142_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143_ForwardIterator
1144adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1145{
1146 if (__first != __last)
1147 {
1148 _ForwardIterator __i = __first;
1149 while (++__i != __last)
1150 {
1151 if (__pred(*__first, *__i))
1152 return __first;
1153 __first = __i;
1154 }
1155 }
1156 return __last;
1157}
1158
1159template <class _ForwardIterator>
Nico Weber471b10a2019-04-03 18:13:08 +00001160_LIBCPP_NODISCARD_EXT inline
1161_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162_ForwardIterator
1163adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1164{
1165 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001166 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167}
1168
1169// count
1170
1171template <class _InputIterator, class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00001172_LIBCPP_NODISCARD_EXT inline
1173_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174typename iterator_traits<_InputIterator>::difference_type
Howard Hinnantbf074022011-10-22 20:59:45 +00001175count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176{
1177 typename iterator_traits<_InputIterator>::difference_type __r(0);
1178 for (; __first != __last; ++__first)
Howard Hinnantbf074022011-10-22 20:59:45 +00001179 if (*__first == __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180 ++__r;
1181 return __r;
1182}
1183
1184// count_if
1185
1186template <class _InputIterator, class _Predicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001187_LIBCPP_NODISCARD_EXT inline
1188_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189typename iterator_traits<_InputIterator>::difference_type
1190count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1191{
1192 typename iterator_traits<_InputIterator>::difference_type __r(0);
1193 for (; __first != __last; ++__first)
1194 if (__pred(*__first))
1195 ++__r;
1196 return __r;
1197}
1198
1199// mismatch
1200
1201template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001202_LIBCPP_NODISCARD_EXT inline
1203_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001204pair<_InputIterator1, _InputIterator2>
1205mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1206 _InputIterator2 __first2, _BinaryPredicate __pred)
1207{
Marshall Clow2932e512014-09-16 20:40:05 +00001208 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001209 if (!__pred(*__first1, *__first2))
1210 break;
1211 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1212}
1213
1214template <class _InputIterator1, class _InputIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00001215_LIBCPP_NODISCARD_EXT inline
1216_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217pair<_InputIterator1, _InputIterator2>
1218mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1219{
1220 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1221 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001222 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001223}
1224
Marshall Clow96b42b22013-05-09 21:14:23 +00001225#if _LIBCPP_STD_VER > 11
1226template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001227_LIBCPP_NODISCARD_EXT inline
1228_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001229pair<_InputIterator1, _InputIterator2>
1230mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1231 _InputIterator2 __first2, _InputIterator2 __last2,
1232 _BinaryPredicate __pred)
1233{
Marshall Clow2932e512014-09-16 20:40:05 +00001234 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow96b42b22013-05-09 21:14:23 +00001235 if (!__pred(*__first1, *__first2))
1236 break;
1237 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1238}
1239
1240template <class _InputIterator1, class _InputIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00001241_LIBCPP_NODISCARD_EXT inline
1242_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001243pair<_InputIterator1, _InputIterator2>
1244mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1245 _InputIterator2 __first2, _InputIterator2 __last2)
1246{
1247 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1248 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1249 return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1250}
1251#endif
1252
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253// equal
1254
1255template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001256_LIBCPP_NODISCARD_EXT inline
1257_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258bool
1259equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1260{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001261 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262 if (!__pred(*__first1, *__first2))
1263 return false;
1264 return true;
1265}
1266
1267template <class _InputIterator1, class _InputIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00001268_LIBCPP_NODISCARD_EXT inline
1269_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270bool
1271equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1272{
1273 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1274 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001275 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001276}
1277
Marshall Clow96b42b22013-05-09 21:14:23 +00001278#if _LIBCPP_STD_VER > 11
1279template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001280inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001281bool
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001282__equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow96b42b22013-05-09 21:14:23 +00001283 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1284 input_iterator_tag, input_iterator_tag )
1285{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001286 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow96b42b22013-05-09 21:14:23 +00001287 if (!__pred(*__first1, *__first2))
1288 return false;
1289 return __first1 == __last1 && __first2 == __last2;
1290}
1291
1292template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001293inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001294bool
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001295__equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1296 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
Marshall Clow96b42b22013-05-09 21:14:23 +00001297 random_access_iterator_tag, random_access_iterator_tag )
1298{
1299 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1300 return false;
1301 return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1302 typename add_lvalue_reference<_BinaryPredicate>::type>
1303 (__first1, __last1, __first2, __pred );
1304}
1305
1306template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001307_LIBCPP_NODISCARD_EXT inline
1308_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001309bool
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001310equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow96b42b22013-05-09 21:14:23 +00001311 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1312{
1313 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001314 (__first1, __last1, __first2, __last2, __pred,
Marshall Clow96b42b22013-05-09 21:14:23 +00001315 typename iterator_traits<_InputIterator1>::iterator_category(),
1316 typename iterator_traits<_InputIterator2>::iterator_category());
1317}
1318
1319template <class _InputIterator1, class _InputIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00001320_LIBCPP_NODISCARD_EXT inline
1321_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001322bool
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001323equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow96b42b22013-05-09 21:14:23 +00001324 _InputIterator2 __first2, _InputIterator2 __last2)
1325{
1326 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1327 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1328 return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1329 typename iterator_traits<_InputIterator1>::iterator_category(),
1330 typename iterator_traits<_InputIterator2>::iterator_category());
1331}
1332#endif
1333
Howard Hinnantc51e1022010-05-11 19:42:16 +00001334// is_permutation
1335
1336template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001337_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1339 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1340{
Marshall Clow96d050a2018-01-15 16:16:32 +00001341// shorten sequences as much as possible by lopping of any equal prefix
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001342 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001343 if (!__pred(*__first1, *__first2))
Marshall Clow96d050a2018-01-15 16:16:32 +00001344 break;
1345 if (__first1 == __last1)
1346 return true;
1347
1348// __first1 != __last1 && *__first1 != *__first2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001349 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001350 _D1 __l1 = _VSTD::distance(__first1, __last1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001351 if (__l1 == _D1(1))
1352 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001353 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001354 // For each element in [f1, l1) see if there are the same number of
1355 // equal elements in [f2, l2)
1356 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1357 {
Marshall Clow96d050a2018-01-15 16:16:32 +00001358 // Have we already counted the number of *__i in [f1, l1)?
Peter Collingbournea5b451b2018-01-26 21:23:27 +00001359 _ForwardIterator1 __match = __first1;
1360 for (; __match != __i; ++__match)
1361 if (__pred(*__match, *__i))
1362 break;
1363 if (__match == __i) {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001364 // Count number of *__i in [f2, l2)
1365 _D1 __c2 = 0;
1366 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1367 if (__pred(*__i, *__j))
1368 ++__c2;
1369 if (__c2 == 0)
1370 return false;
1371 // Count number of *__i in [__i, l1) (we can start with 1)
1372 _D1 __c1 = 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001373 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001374 if (__pred(*__i, *__j))
1375 ++__c1;
1376 if (__c1 != __c2)
1377 return false;
1378 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001379 }
1380 return true;
1381}
1382
1383template<class _ForwardIterator1, class _ForwardIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00001384_LIBCPP_NODISCARD_EXT inline
1385_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001386bool
1387is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1388 _ForwardIterator2 __first2)
1389{
1390 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1391 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001392 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393}
1394
Marshall Clow96b42b22013-05-09 21:14:23 +00001395#if _LIBCPP_STD_VER > 11
1396template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow96d050a2018-01-15 16:16:32 +00001397_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Marshall Clow96b42b22013-05-09 21:14:23 +00001398__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001399 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
Marshall Clow96b42b22013-05-09 21:14:23 +00001400 _BinaryPredicate __pred,
1401 forward_iterator_tag, forward_iterator_tag )
1402{
Marshall Clow96d050a2018-01-15 16:16:32 +00001403// shorten sequences as much as possible by lopping of any equal prefix
Eric Fiselier14673892014-10-27 20:26:25 +00001404 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow96b42b22013-05-09 21:14:23 +00001405 if (!__pred(*__first1, *__first2))
Marshall Clow96d050a2018-01-15 16:16:32 +00001406 break;
1407 if (__first1 == __last1)
Marshall Clow01bbbd22018-01-19 18:07:29 +00001408 return __first2 == __last2;
Marshall Clow96d050a2018-01-15 16:16:32 +00001409 else if (__first2 == __last2)
Marshall Clow01bbbd22018-01-19 18:07:29 +00001410 return false;
Marshall Clow96d050a2018-01-15 16:16:32 +00001411
Marshall Clow96b42b22013-05-09 21:14:23 +00001412 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1413 _D1 __l1 = _VSTD::distance(__first1, __last1);
1414
1415 typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
Marshall Clow42b967c2013-05-10 00:16:10 +00001416 _D2 __l2 = _VSTD::distance(__first2, __last2);
Marshall Clow96b42b22013-05-09 21:14:23 +00001417 if (__l1 != __l2)
1418 return false;
1419
1420 // For each element in [f1, l1) see if there are the same number of
1421 // equal elements in [f2, l2)
1422 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1423 {
Marshall Clow96d050a2018-01-15 16:16:32 +00001424 // Have we already counted the number of *__i in [f1, l1)?
Peter Collingbournea5b451b2018-01-26 21:23:27 +00001425 _ForwardIterator1 __match = __first1;
1426 for (; __match != __i; ++__match)
1427 if (__pred(*__match, *__i))
1428 break;
1429 if (__match == __i) {
Marshall Clow96b42b22013-05-09 21:14:23 +00001430 // Count number of *__i in [f2, l2)
1431 _D1 __c2 = 0;
1432 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1433 if (__pred(*__i, *__j))
1434 ++__c2;
1435 if (__c2 == 0)
1436 return false;
1437 // Count number of *__i in [__i, l1) (we can start with 1)
1438 _D1 __c1 = 1;
1439 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1440 if (__pred(*__i, *__j))
1441 ++__c1;
1442 if (__c1 != __c2)
1443 return false;
1444 }
Marshall Clow96b42b22013-05-09 21:14:23 +00001445 }
1446 return true;
1447}
1448
1449template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow96d050a2018-01-15 16:16:32 +00001450_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Marshall Clow96b42b22013-05-09 21:14:23 +00001451__is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001452 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
Marshall Clow96b42b22013-05-09 21:14:23 +00001453 _BinaryPredicate __pred,
1454 random_access_iterator_tag, random_access_iterator_tag )
1455{
1456 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1457 return false;
1458 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1459 typename add_lvalue_reference<_BinaryPredicate>::type>
1460 (__first1, __last1, __first2, __pred );
1461}
1462
1463template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001464_LIBCPP_NODISCARD_EXT inline
1465_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001466bool
1467is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1468 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1469 _BinaryPredicate __pred )
1470{
1471 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1472 (__first1, __last1, __first2, __last2, __pred,
1473 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1474 typename iterator_traits<_ForwardIterator2>::iterator_category());
1475}
1476
1477template<class _ForwardIterator1, class _ForwardIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00001478_LIBCPP_NODISCARD_EXT inline
1479_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001480bool
1481is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1482 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1483{
1484 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1485 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1486 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1487 __equal_to<__v1, __v2>(),
1488 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1489 typename iterator_traits<_ForwardIterator2>::iterator_category());
1490}
1491#endif
1492
Howard Hinnantc51e1022010-05-11 19:42:16 +00001493// search
Marshall Clowa40686b2018-01-08 19:18:00 +00001494// __search is in <functional>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001495
1496template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001497_LIBCPP_NODISCARD_EXT inline
1498_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499_ForwardIterator1
1500search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1501 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1502{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001503 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001504 (__first1, __last1, __first2, __last2, __pred,
Marshall Clowaf8be6c2016-03-08 15:12:52 +00001505 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1506 typename iterator_traits<_ForwardIterator2>::iterator_category())
1507 .first;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001508}
1509
1510template <class _ForwardIterator1, class _ForwardIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00001511_LIBCPP_NODISCARD_EXT inline
1512_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001513_ForwardIterator1
1514search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1515 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1516{
Marshall Clowaf8be6c2016-03-08 15:12:52 +00001517 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1518 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001519 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001520}
1521
Marshall Clowa40686b2018-01-08 19:18:00 +00001522
1523#if _LIBCPP_STD_VER > 14
1524template <class _ForwardIterator, class _Searcher>
Nico Weber471b10a2019-04-03 18:13:08 +00001525_LIBCPP_NODISCARD_EXT _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowa40686b2018-01-08 19:18:00 +00001526_ForwardIterator search(_ForwardIterator __f, _ForwardIterator __l, const _Searcher &__s)
1527{ return __s(__f, __l).first; }
1528#endif
1529
Howard Hinnantc51e1022010-05-11 19:42:16 +00001530// search_n
1531
1532template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001533_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534__search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnantbf074022011-10-22 20:59:45 +00001535 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536{
1537 if (__count <= 0)
1538 return __first;
1539 while (true)
1540 {
Howard Hinnantbf074022011-10-22 20:59:45 +00001541 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantc51e1022010-05-11 19:42:16 +00001542 while (true)
1543 {
Howard Hinnantbf074022011-10-22 20:59:45 +00001544 if (__first == __last) // return __last if no element matches __value_
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545 return __last;
Howard Hinnantbf074022011-10-22 20:59:45 +00001546 if (__pred(*__first, __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547 break;
1548 ++__first;
1549 }
Howard Hinnantbf074022011-10-22 20:59:45 +00001550 // *__first matches __value_, now match elements after here
Howard Hinnantc51e1022010-05-11 19:42:16 +00001551 _ForwardIterator __m = __first;
1552 _Size __c(0);
1553 while (true)
1554 {
1555 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1556 return __first;
1557 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1558 return __last;
Howard Hinnantbf074022011-10-22 20:59:45 +00001559 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560 {
1561 __first = __m;
1562 ++__first;
1563 break;
1564 } // else there is a match, check next elements
1565 }
1566 }
1567}
1568
1569template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001570_LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001571__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnantbf074022011-10-22 20:59:45 +00001572 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573{
1574 if (__count <= 0)
1575 return __first;
1576 _Size __len = static_cast<_Size>(__last - __first);
1577 if (__len < __count)
1578 return __last;
1579 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1580 while (true)
1581 {
Howard Hinnantbf074022011-10-22 20:59:45 +00001582 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583 while (true)
1584 {
Howard Hinnantbede4c32013-04-04 15:40:48 +00001585 if (__first >= __s) // return __last if no element matches __value_
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586 return __last;
Howard Hinnantbf074022011-10-22 20:59:45 +00001587 if (__pred(*__first, __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 break;
1589 ++__first;
1590 }
Howard Hinnantbf074022011-10-22 20:59:45 +00001591 // *__first matches __value_, now match elements after here
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592 _RandomAccessIterator __m = __first;
1593 _Size __c(0);
1594 while (true)
1595 {
1596 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1597 return __first;
1598 ++__m; // no need to check range on __m because __s guarantees we have enough source
Howard Hinnantbf074022011-10-22 20:59:45 +00001599 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 {
1601 __first = __m;
1602 ++__first;
1603 break;
1604 } // else there is a match, check next elements
1605 }
1606 }
1607}
1608
1609template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001610_LIBCPP_NODISCARD_EXT inline
1611_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612_ForwardIterator
1613search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnantbf074022011-10-22 20:59:45 +00001614 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001615{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001616 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001617 (__first, __last, __convert_to_integral(__count), __value_, __pred,
1618 typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001619}
1620
1621template <class _ForwardIterator, class _Size, class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00001622_LIBCPP_NODISCARD_EXT inline
1623_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001624_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00001625search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001626{
1627 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001628 return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
1629 __value_, __equal_to<__v, _Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001630}
1631
1632// copy
Howard Hinnantc51e1022010-05-11 19:42:16 +00001633template <class _Iter>
1634inline _LIBCPP_INLINE_VISIBILITY
1635_Iter
1636__unwrap_iter(_Iter __i)
1637{
1638 return __i;
1639}
1640
1641template <class _Tp>
Marshall Clow88880de2017-05-25 14:20:26 +00001642inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001643typename enable_if
1644<
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001645 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001646 _Tp*
1647>::type
1648__unwrap_iter(move_iterator<_Tp*> __i)
1649{
1650 return __i.base();
1651}
1652
Howard Hinnant8ea98242013-08-23 17:37:05 +00001653#if _LIBCPP_DEBUG_LEVEL < 2
1654
Howard Hinnantc51e1022010-05-11 19:42:16 +00001655template <class _Tp>
Marshall Clowae4f8312018-07-13 16:35:26 +00001656inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001657typename enable_if
1658<
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001659 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001660 _Tp*
1661>::type
1662__unwrap_iter(__wrap_iter<_Tp*> __i)
1663{
1664 return __i.base();
1665}
1666
Marshall Clowbae96ac2019-02-06 16:10:25 +00001667template <class _Tp>
1668inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1669typename enable_if
1670<
1671 is_trivially_copy_assignable<_Tp>::value,
1672 const _Tp*
1673>::type
1674__unwrap_iter(__wrap_iter<const _Tp*> __i)
1675{
1676 return __i.base();
1677}
1678
Eric Fiselier38badb82016-12-28 05:35:32 +00001679#else
1680
1681template <class _Tp>
Marshall Clowae4f8312018-07-13 16:35:26 +00001682inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Eric Fiselier38badb82016-12-28 05:35:32 +00001683typename enable_if
1684<
1685 is_trivially_copy_assignable<_Tp>::value,
1686 __wrap_iter<_Tp*>
1687>::type
1688__unwrap_iter(__wrap_iter<_Tp*> __i)
1689{
1690 return __i;
1691}
1692
Howard Hinnant8ea98242013-08-23 17:37:05 +00001693#endif // _LIBCPP_DEBUG_LEVEL < 2
1694
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695template <class _InputIterator, class _OutputIterator>
1696inline _LIBCPP_INLINE_VISIBILITY
1697_OutputIterator
1698__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1699{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001700 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001701 *__result = *__first;
1702 return __result;
1703}
1704
1705template <class _Tp, class _Up>
1706inline _LIBCPP_INLINE_VISIBILITY
1707typename enable_if
1708<
1709 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001710 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001711 _Up*
1712>::type
1713__copy(_Tp* __first, _Tp* __last, _Up* __result)
1714{
1715 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clowc3ef9622015-06-02 13:52:16 +00001716 if (__n > 0)
1717 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718 return __result + __n;
1719}
1720
1721template <class _InputIterator, class _OutputIterator>
1722inline _LIBCPP_INLINE_VISIBILITY
1723_OutputIterator
1724copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1725{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001726 return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001727}
1728
1729// copy_backward
1730
Howard Hinnant7f229bc2013-02-06 21:03:39 +00001731template <class _BidirectionalIterator, class _OutputIterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001732inline _LIBCPP_INLINE_VISIBILITY
1733_OutputIterator
Howard Hinnant7f229bc2013-02-06 21:03:39 +00001734__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735{
1736 while (__first != __last)
1737 *--__result = *--__last;
1738 return __result;
1739}
1740
1741template <class _Tp, class _Up>
1742inline _LIBCPP_INLINE_VISIBILITY
1743typename enable_if
1744<
1745 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001746 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001747 _Up*
1748>::type
1749__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1750{
1751 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clowc3ef9622015-06-02 13:52:16 +00001752 if (__n > 0)
1753 {
1754 __result -= __n;
1755 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1756 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001757 return __result;
1758}
1759
1760template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1761inline _LIBCPP_INLINE_VISIBILITY
1762_BidirectionalIterator2
1763copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1764 _BidirectionalIterator2 __result)
1765{
Eric Fiselier6003c772016-12-23 23:37:52 +00001766 return _VSTD::__copy_backward(__unwrap_iter(__first),
1767 __unwrap_iter(__last),
1768 __unwrap_iter(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001769}
1770
1771// copy_if
1772
1773template<class _InputIterator, class _OutputIterator, class _Predicate>
1774inline _LIBCPP_INLINE_VISIBILITY
1775_OutputIterator
1776copy_if(_InputIterator __first, _InputIterator __last,
1777 _OutputIterator __result, _Predicate __pred)
1778{
1779 for (; __first != __last; ++__first)
1780 {
1781 if (__pred(*__first))
1782 {
1783 *__result = *__first;
1784 ++__result;
1785 }
1786 }
1787 return __result;
1788}
1789
1790// copy_n
1791
1792template<class _InputIterator, class _Size, class _OutputIterator>
1793inline _LIBCPP_INLINE_VISIBILITY
1794typename enable_if
1795<
1796 __is_input_iterator<_InputIterator>::value &&
1797 !__is_random_access_iterator<_InputIterator>::value,
1798 _OutputIterator
1799>::type
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001800copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801{
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001802 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1803 _IntegralSize __n = __orig_n;
Howard Hinnantcbc5dc02011-02-27 20:55:39 +00001804 if (__n > 0)
1805 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806 *__result = *__first;
Howard Hinnantcbc5dc02011-02-27 20:55:39 +00001807 ++__result;
1808 for (--__n; __n > 0; --__n)
1809 {
1810 ++__first;
1811 *__result = *__first;
1812 ++__result;
1813 }
1814 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001815 return __result;
1816}
1817
1818template<class _InputIterator, class _Size, class _OutputIterator>
1819inline _LIBCPP_INLINE_VISIBILITY
1820typename enable_if
1821<
1822 __is_random_access_iterator<_InputIterator>::value,
1823 _OutputIterator
1824>::type
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001825copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001826{
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001827 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1828 _IntegralSize __n = __orig_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001829 return _VSTD::copy(__first, __first + __n, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001830}
1831
1832// move
1833
1834template <class _InputIterator, class _OutputIterator>
1835inline _LIBCPP_INLINE_VISIBILITY
1836_OutputIterator
1837__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1838{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001839 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001840 *__result = _VSTD::move(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001841 return __result;
1842}
1843
1844template <class _Tp, class _Up>
1845inline _LIBCPP_INLINE_VISIBILITY
1846typename enable_if
1847<
1848 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001849 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001850 _Up*
1851>::type
1852__move(_Tp* __first, _Tp* __last, _Up* __result)
1853{
1854 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clowc3ef9622015-06-02 13:52:16 +00001855 if (__n > 0)
1856 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001857 return __result + __n;
1858}
1859
1860template <class _InputIterator, class _OutputIterator>
1861inline _LIBCPP_INLINE_VISIBILITY
1862_OutputIterator
1863move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1864{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001865 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001866}
1867
1868// move_backward
1869
1870template <class _InputIterator, class _OutputIterator>
1871inline _LIBCPP_INLINE_VISIBILITY
1872_OutputIterator
1873__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1874{
1875 while (__first != __last)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001876 *--__result = _VSTD::move(*--__last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001877 return __result;
1878}
1879
1880template <class _Tp, class _Up>
1881inline _LIBCPP_INLINE_VISIBILITY
1882typename enable_if
1883<
1884 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001885 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886 _Up*
1887>::type
1888__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1889{
1890 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clowc3ef9622015-06-02 13:52:16 +00001891 if (__n > 0)
1892 {
1893 __result -= __n;
1894 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1895 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896 return __result;
1897}
1898
1899template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1900inline _LIBCPP_INLINE_VISIBILITY
1901_BidirectionalIterator2
1902move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1903 _BidirectionalIterator2 __result)
1904{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001905 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001906}
1907
1908// iter_swap
1909
Howard Hinnantdbfd4b42011-05-27 15:04:19 +00001910// moved to <type_traits> for better swap / noexcept support
Howard Hinnantc51e1022010-05-11 19:42:16 +00001911
1912// transform
1913
1914template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
Marshall Clow31427c62018-01-19 17:45:39 +00001915inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916_OutputIterator
1917transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1918{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001919 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001920 *__result = __op(*__first);
1921 return __result;
1922}
1923
1924template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
Marshall Clow31427c62018-01-19 17:45:39 +00001925inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001926_OutputIterator
1927transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1928 _OutputIterator __result, _BinaryOperation __binary_op)
1929{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001930 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001931 *__result = __binary_op(*__first1, *__first2);
1932 return __result;
1933}
1934
1935// replace
1936
1937template <class _ForwardIterator, class _Tp>
Marshall Clow01bbbd22018-01-19 18:07:29 +00001938inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001939void
1940replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1941{
1942 for (; __first != __last; ++__first)
1943 if (*__first == __old_value)
1944 *__first = __new_value;
1945}
1946
1947// replace_if
1948
1949template <class _ForwardIterator, class _Predicate, class _Tp>
Marshall Clow01bbbd22018-01-19 18:07:29 +00001950inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001951void
1952replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1953{
1954 for (; __first != __last; ++__first)
1955 if (__pred(*__first))
1956 *__first = __new_value;
1957}
1958
1959// replace_copy
1960
1961template <class _InputIterator, class _OutputIterator, class _Tp>
Marshall Clow01bbbd22018-01-19 18:07:29 +00001962inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001963_OutputIterator
1964replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1965 const _Tp& __old_value, const _Tp& __new_value)
1966{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001967 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001968 if (*__first == __old_value)
1969 *__result = __new_value;
1970 else
1971 *__result = *__first;
1972 return __result;
1973}
1974
1975// replace_copy_if
1976
1977template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
Marshall Clow01bbbd22018-01-19 18:07:29 +00001978inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001979_OutputIterator
1980replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1981 _Predicate __pred, const _Tp& __new_value)
1982{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001983 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001984 if (__pred(*__first))
1985 *__result = __new_value;
1986 else
1987 *__result = *__first;
1988 return __result;
1989}
1990
1991// fill_n
1992
1993template <class _OutputIterator, class _Size, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001994inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001995_OutputIterator
Howard Hinnant0ad1c122013-08-01 17:29:28 +00001996__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001997{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001998 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnantbf074022011-10-22 20:59:45 +00001999 *__first = __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002000 return __first;
2001}
2002
Howard Hinnantc51e1022010-05-11 19:42:16 +00002003template <class _OutputIterator, class _Size, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002004inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002005_OutputIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00002006fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002007{
Eric Fiselier97ec07d2015-02-10 16:46:42 +00002008 return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002009}
2010
2011// fill
2012
2013template <class _ForwardIterator, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002014inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015void
Howard Hinnantbf074022011-10-22 20:59:45 +00002016__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002017{
2018 for (; __first != __last; ++__first)
Howard Hinnantbf074022011-10-22 20:59:45 +00002019 *__first = __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002020}
2021
2022template <class _RandomAccessIterator, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002023inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002024void
Howard Hinnantbf074022011-10-22 20:59:45 +00002025__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002026{
Howard Hinnantbf074022011-10-22 20:59:45 +00002027 _VSTD::fill_n(__first, __last - __first, __value_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002028}
2029
2030template <class _ForwardIterator, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002031inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002032void
Howard Hinnantbf074022011-10-22 20:59:45 +00002033fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002034{
Howard Hinnantbf074022011-10-22 20:59:45 +00002035 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002036}
2037
2038// generate
2039
2040template <class _ForwardIterator, class _Generator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002041inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002042void
2043generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2044{
2045 for (; __first != __last; ++__first)
2046 *__first = __gen();
2047}
2048
2049// generate_n
2050
2051template <class _OutputIterator, class _Size, class _Generator>
Nico Weber471b10a2019-04-03 18:13:08 +00002052inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002053_OutputIterator
Eric Fiselier97ec07d2015-02-10 16:46:42 +00002054generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002055{
Eric Fiselier97ec07d2015-02-10 16:46:42 +00002056 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
2057 _IntegralSize __n = __orig_n;
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002058 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002059 *__first = __gen();
2060 return __first;
2061}
2062
2063// remove
2064
2065template <class _ForwardIterator, class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00002066_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00002067remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002068{
Howard Hinnantbf074022011-10-22 20:59:45 +00002069 __first = _VSTD::find(__first, __last, __value_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002070 if (__first != __last)
2071 {
2072 _ForwardIterator __i = __first;
2073 while (++__i != __last)
2074 {
Howard Hinnantbf074022011-10-22 20:59:45 +00002075 if (!(*__i == __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002076 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002077 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002078 ++__first;
2079 }
2080 }
2081 }
2082 return __first;
2083}
2084
2085// remove_if
2086
2087template <class _ForwardIterator, class _Predicate>
Nico Weber471b10a2019-04-03 18:13:08 +00002088_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002089remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2090{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002091 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002092 (__first, __last, __pred);
2093 if (__first != __last)
2094 {
2095 _ForwardIterator __i = __first;
2096 while (++__i != __last)
2097 {
2098 if (!__pred(*__i))
2099 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002100 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002101 ++__first;
2102 }
2103 }
2104 }
2105 return __first;
2106}
2107
2108// remove_copy
2109
2110template <class _InputIterator, class _OutputIterator, class _Tp>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00002111inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002112_OutputIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00002113remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002114{
2115 for (; __first != __last; ++__first)
2116 {
Howard Hinnantbf074022011-10-22 20:59:45 +00002117 if (!(*__first == __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002118 {
2119 *__result = *__first;
2120 ++__result;
2121 }
2122 }
2123 return __result;
2124}
2125
2126// remove_copy_if
2127
2128template <class _InputIterator, class _OutputIterator, class _Predicate>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00002129inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002130_OutputIterator
2131remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2132{
2133 for (; __first != __last; ++__first)
2134 {
2135 if (!__pred(*__first))
2136 {
2137 *__result = *__first;
2138 ++__result;
2139 }
2140 }
2141 return __result;
2142}
2143
2144// unique
2145
2146template <class _ForwardIterator, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00002147_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002148unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2149{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002150 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002151 (__first, __last, __pred);
2152 if (__first != __last)
2153 {
2154 // ... a a ? ...
2155 // f i
2156 _ForwardIterator __i = __first;
2157 for (++__i; ++__i != __last;)
2158 if (!__pred(*__first, *__i))
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002159 *++__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160 ++__first;
2161 }
2162 return __first;
2163}
2164
2165template <class _ForwardIterator>
Nico Weber471b10a2019-04-03 18:13:08 +00002166_LIBCPP_NODISCARD_EXT inline
2167_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168_ForwardIterator
2169unique(_ForwardIterator __first, _ForwardIterator __last)
2170{
2171 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002172 return _VSTD::unique(__first, __last, __equal_to<__v>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002173}
2174
2175// unique_copy
2176
2177template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002178_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2180 input_iterator_tag, output_iterator_tag)
2181{
2182 if (__first != __last)
2183 {
2184 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2185 *__result = __t;
2186 ++__result;
2187 while (++__first != __last)
2188 {
2189 if (!__pred(__t, *__first))
2190 {
2191 __t = *__first;
2192 *__result = __t;
2193 ++__result;
2194 }
2195 }
2196 }
2197 return __result;
2198}
2199
2200template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002201_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002202__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2203 forward_iterator_tag, output_iterator_tag)
2204{
2205 if (__first != __last)
2206 {
2207 _ForwardIterator __i = __first;
2208 *__result = *__i;
2209 ++__result;
2210 while (++__first != __last)
2211 {
2212 if (!__pred(*__i, *__first))
2213 {
2214 *__result = *__first;
2215 ++__result;
2216 __i = __first;
2217 }
2218 }
2219 }
2220 return __result;
2221}
2222
2223template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002224_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2226 input_iterator_tag, forward_iterator_tag)
2227{
2228 if (__first != __last)
2229 {
2230 *__result = *__first;
2231 while (++__first != __last)
2232 if (!__pred(*__result, *__first))
2233 *++__result = *__first;
2234 ++__result;
2235 }
2236 return __result;
2237}
2238
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002240inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002241_OutputIterator
2242unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2243{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002244 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245 (__first, __last, __result, __pred,
2246 typename iterator_traits<_InputIterator>::iterator_category(),
2247 typename iterator_traits<_OutputIterator>::iterator_category());
2248}
2249
2250template <class _InputIterator, class _OutputIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002251inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252_OutputIterator
2253unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2254{
2255 typedef typename iterator_traits<_InputIterator>::value_type __v;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002256 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002257}
2258
2259// reverse
2260
2261template <class _BidirectionalIterator>
2262inline _LIBCPP_INLINE_VISIBILITY
2263void
2264__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2265{
2266 while (__first != __last)
2267 {
2268 if (__first == --__last)
2269 break;
Marshall Clow2ad71042015-11-02 21:34:25 +00002270 _VSTD::iter_swap(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002271 ++__first;
2272 }
2273}
2274
2275template <class _RandomAccessIterator>
2276inline _LIBCPP_INLINE_VISIBILITY
2277void
2278__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2279{
2280 if (__first != __last)
2281 for (; __first < --__last; ++__first)
Marshall Clow2ad71042015-11-02 21:34:25 +00002282 _VSTD::iter_swap(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283}
2284
2285template <class _BidirectionalIterator>
2286inline _LIBCPP_INLINE_VISIBILITY
2287void
2288reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2289{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002290 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002291}
2292
2293// reverse_copy
2294
2295template <class _BidirectionalIterator, class _OutputIterator>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00002296inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002297_OutputIterator
2298reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2299{
2300 for (; __first != __last; ++__result)
2301 *__result = *--__last;
2302 return __result;
2303}
2304
2305// rotate
2306
2307template <class _ForwardIterator>
2308_ForwardIterator
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002309__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002310{
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002311 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2312 value_type __tmp = _VSTD::move(*__first);
2313 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2314 *__lm1 = _VSTD::move(__tmp);
2315 return __lm1;
2316}
2317
2318template <class _BidirectionalIterator>
2319_BidirectionalIterator
2320__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2321{
2322 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2323 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2324 value_type __tmp = _VSTD::move(*__lm1);
2325 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2326 *__first = _VSTD::move(__tmp);
2327 return __fp1;
2328}
2329
2330template <class _ForwardIterator>
2331_ForwardIterator
2332__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2333{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002334 _ForwardIterator __i = __middle;
2335 while (true)
2336 {
2337 swap(*__first, *__i);
2338 ++__first;
2339 if (++__i == __last)
2340 break;
2341 if (__first == __middle)
2342 __middle = __i;
2343 }
2344 _ForwardIterator __r = __first;
2345 if (__first != __middle)
2346 {
2347 __i = __middle;
2348 while (true)
2349 {
2350 swap(*__first, *__i);
2351 ++__first;
2352 if (++__i == __last)
2353 {
2354 if (__first == __middle)
2355 break;
2356 __i = __middle;
2357 }
2358 else if (__first == __middle)
2359 __middle = __i;
2360 }
2361 }
2362 return __r;
2363}
2364
2365template<typename _Integral>
2366inline _LIBCPP_INLINE_VISIBILITY
2367_Integral
Marshall Clowb8bfc2c2016-07-26 14:29:45 +00002368__algo_gcd(_Integral __x, _Integral __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002369{
2370 do
2371 {
2372 _Integral __t = __x % __y;
2373 __x = __y;
2374 __y = __t;
2375 } while (__y);
2376 return __x;
2377}
2378
2379template<typename _RandomAccessIterator>
2380_RandomAccessIterator
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002381__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002382{
2383 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2384 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002385
Howard Hinnantc51e1022010-05-11 19:42:16 +00002386 const difference_type __m1 = __middle - __first;
2387 const difference_type __m2 = __last - __middle;
2388 if (__m1 == __m2)
2389 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002390 _VSTD::swap_ranges(__first, __middle, __middle);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002391 return __middle;
2392 }
Marshall Clowb8bfc2c2016-07-26 14:29:45 +00002393 const difference_type __g = _VSTD::__algo_gcd(__m1, __m2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002394 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2395 {
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002396 value_type __t(_VSTD::move(*--__p));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002397 _RandomAccessIterator __p1 = __p;
2398 _RandomAccessIterator __p2 = __p1 + __m1;
2399 do
2400 {
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002401 *__p1 = _VSTD::move(*__p2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002402 __p1 = __p2;
2403 const difference_type __d = __last - __p2;
2404 if (__m1 < __d)
2405 __p2 += __m1;
2406 else
2407 __p2 = __first + (__m1 - __d);
2408 } while (__p2 != __p);
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002409 *__p1 = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002410 }
2411 return __first + __m2;
2412}
2413
2414template <class _ForwardIterator>
2415inline _LIBCPP_INLINE_VISIBILITY
2416_ForwardIterator
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002417__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2418 _VSTD::forward_iterator_tag)
2419{
2420 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2421 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2422 {
2423 if (_VSTD::next(__first) == __middle)
2424 return _VSTD::__rotate_left(__first, __last);
2425 }
2426 return _VSTD::__rotate_forward(__first, __middle, __last);
2427}
2428
2429template <class _BidirectionalIterator>
2430inline _LIBCPP_INLINE_VISIBILITY
2431_BidirectionalIterator
2432__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2433 _VSTD::bidirectional_iterator_tag)
2434{
2435 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2436 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2437 {
2438 if (_VSTD::next(__first) == __middle)
2439 return _VSTD::__rotate_left(__first, __last);
2440 if (_VSTD::next(__middle) == __last)
2441 return _VSTD::__rotate_right(__first, __last);
2442 }
2443 return _VSTD::__rotate_forward(__first, __middle, __last);
2444}
2445
2446template <class _RandomAccessIterator>
2447inline _LIBCPP_INLINE_VISIBILITY
2448_RandomAccessIterator
2449__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2450 _VSTD::random_access_iterator_tag)
2451{
2452 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2453 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2454 {
2455 if (_VSTD::next(__first) == __middle)
2456 return _VSTD::__rotate_left(__first, __last);
2457 if (_VSTD::next(__middle) == __last)
2458 return _VSTD::__rotate_right(__first, __last);
2459 return _VSTD::__rotate_gcd(__first, __middle, __last);
2460 }
2461 return _VSTD::__rotate_forward(__first, __middle, __last);
2462}
2463
2464template <class _ForwardIterator>
2465inline _LIBCPP_INLINE_VISIBILITY
2466_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002467rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2468{
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002469 if (__first == __middle)
2470 return __last;
2471 if (__middle == __last)
2472 return __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002473 return _VSTD::__rotate(__first, __middle, __last,
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002474 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475}
2476
2477// rotate_copy
2478
2479template <class _ForwardIterator, class _OutputIterator>
2480inline _LIBCPP_INLINE_VISIBILITY
2481_OutputIterator
2482rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2483{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002484 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002485}
2486
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487// min_element
2488
2489template <class _ForwardIterator, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00002490_LIBCPP_NODISCARD_EXT inline
2491_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002492_ForwardIterator
Marshall Clow9e173072015-05-10 13:53:31 +00002493min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002494{
Eric Fiselierf8ecd942018-08-22 17:47:13 +00002495 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2496 "std::min_element requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002497 if (__first != __last)
2498 {
2499 _ForwardIterator __i = __first;
2500 while (++__i != __last)
2501 if (__comp(*__i, *__first))
2502 __first = __i;
2503 }
2504 return __first;
2505}
2506
2507template <class _ForwardIterator>
Nico Weber471b10a2019-04-03 18:13:08 +00002508_LIBCPP_NODISCARD_EXT inline
2509_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002510_ForwardIterator
2511min_element(_ForwardIterator __first, _ForwardIterator __last)
2512{
Marshall Clow9e173072015-05-10 13:53:31 +00002513 return _VSTD::min_element(__first, __last,
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002514 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2515}
2516
2517// min
2518
2519template <class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00002520_LIBCPP_NODISCARD_EXT inline
2521_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002522const _Tp&
2523min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2524{
2525 return __comp(__b, __a) ? __b : __a;
2526}
2527
2528template <class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00002529_LIBCPP_NODISCARD_EXT inline
2530_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002531const _Tp&
2532min(const _Tp& __a, const _Tp& __b)
2533{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002534 return _VSTD::min(__a, __b, __less<_Tp>());
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002535}
2536
Eric Fiselier93dd1372017-04-18 23:26:47 +00002537#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002538
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002539template<class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00002540_LIBCPP_NODISCARD_EXT inline
2541_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002542_Tp
2543min(initializer_list<_Tp> __t, _Compare __comp)
2544{
Marshall Clow9e173072015-05-10 13:53:31 +00002545 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002546}
2547
2548template<class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00002549_LIBCPP_NODISCARD_EXT inline
2550_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002551_Tp
2552min(initializer_list<_Tp> __t)
2553{
Marshall Clow9e173072015-05-10 13:53:31 +00002554 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555}
2556
Eric Fiselier93dd1372017-04-18 23:26:47 +00002557#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002558
Howard Hinnantc51e1022010-05-11 19:42:16 +00002559// max_element
2560
2561template <class _ForwardIterator, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00002562_LIBCPP_NODISCARD_EXT inline
2563_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002564_ForwardIterator
Marshall Clow9e173072015-05-10 13:53:31 +00002565max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002566{
Eric Fiselierf8ecd942018-08-22 17:47:13 +00002567 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2568 "std::max_element requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002569 if (__first != __last)
2570 {
2571 _ForwardIterator __i = __first;
2572 while (++__i != __last)
2573 if (__comp(*__first, *__i))
2574 __first = __i;
2575 }
2576 return __first;
2577}
2578
Marshall Clowe9dca072014-02-19 16:51:35 +00002579
Howard Hinnantc51e1022010-05-11 19:42:16 +00002580template <class _ForwardIterator>
Nico Weber471b10a2019-04-03 18:13:08 +00002581_LIBCPP_NODISCARD_EXT inline
2582_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002583_ForwardIterator
2584max_element(_ForwardIterator __first, _ForwardIterator __last)
2585{
Marshall Clow9e173072015-05-10 13:53:31 +00002586 return _VSTD::max_element(__first, __last,
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002587 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2588}
2589
2590// max
2591
2592template <class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00002593_LIBCPP_NODISCARD_EXT inline
2594_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002595const _Tp&
2596max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2597{
2598 return __comp(__a, __b) ? __b : __a;
2599}
2600
2601template <class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00002602_LIBCPP_NODISCARD_EXT inline
2603_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002604const _Tp&
2605max(const _Tp& __a, const _Tp& __b)
2606{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002607 return _VSTD::max(__a, __b, __less<_Tp>());
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002608}
2609
Eric Fiselier93dd1372017-04-18 23:26:47 +00002610#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002611
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002612template<class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00002613_LIBCPP_NODISCARD_EXT inline
2614_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002615_Tp
2616max(initializer_list<_Tp> __t, _Compare __comp)
2617{
Marshall Clow9e173072015-05-10 13:53:31 +00002618 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002619}
2620
2621template<class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00002622_LIBCPP_NODISCARD_EXT inline
2623_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002624_Tp
2625max(initializer_list<_Tp> __t)
2626{
Marshall Clow9e173072015-05-10 13:53:31 +00002627 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628}
2629
Eric Fiselier93dd1372017-04-18 23:26:47 +00002630#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002631
Marshall Clow3e18d0e2016-03-07 22:43:49 +00002632#if _LIBCPP_STD_VER > 14
2633// clamp
2634template<class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00002635_LIBCPP_NODISCARD_EXT inline
2636_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow3e18d0e2016-03-07 22:43:49 +00002637const _Tp&
2638clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
2639{
2640 _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
2641 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
2642
2643}
2644
2645template<class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00002646_LIBCPP_NODISCARD_EXT inline
2647_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow3e18d0e2016-03-07 22:43:49 +00002648const _Tp&
2649clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
2650{
2651 return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
2652}
2653#endif
2654
Howard Hinnantc51e1022010-05-11 19:42:16 +00002655// minmax_element
2656
2657template <class _ForwardIterator, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00002658_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659std::pair<_ForwardIterator, _ForwardIterator>
2660minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2661{
Eric Fiselierf8ecd942018-08-22 17:47:13 +00002662 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2663 "std::minmax_element requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002664 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2665 if (__first != __last)
2666 {
2667 if (++__first != __last)
2668 {
2669 if (__comp(*__first, *__result.first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002670 __result.first = __first;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002671 else
2672 __result.second = __first;
2673 while (++__first != __last)
2674 {
2675 _ForwardIterator __i = __first;
2676 if (++__first == __last)
2677 {
2678 if (__comp(*__i, *__result.first))
2679 __result.first = __i;
2680 else if (!__comp(*__i, *__result.second))
2681 __result.second = __i;
2682 break;
2683 }
2684 else
2685 {
2686 if (__comp(*__first, *__i))
2687 {
2688 if (__comp(*__first, *__result.first))
2689 __result.first = __first;
2690 if (!__comp(*__i, *__result.second))
2691 __result.second = __i;
2692 }
2693 else
2694 {
2695 if (__comp(*__i, *__result.first))
2696 __result.first = __i;
2697 if (!__comp(*__first, *__result.second))
2698 __result.second = __first;
2699 }
2700 }
2701 }
2702 }
2703 }
2704 return __result;
2705}
2706
2707template <class _ForwardIterator>
Nico Weber471b10a2019-04-03 18:13:08 +00002708_LIBCPP_NODISCARD_EXT inline
2709_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710std::pair<_ForwardIterator, _ForwardIterator>
2711minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2712{
Marshall Clowe9dca072014-02-19 16:51:35 +00002713 return _VSTD::minmax_element(__first, __last,
2714 __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002715}
2716
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002717// minmax
2718
2719template<class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00002720_LIBCPP_NODISCARD_EXT inline
2721_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002722pair<const _Tp&, const _Tp&>
2723minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2724{
2725 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2726 pair<const _Tp&, const _Tp&>(__a, __b);
2727}
2728
2729template<class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00002730_LIBCPP_NODISCARD_EXT inline
2731_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002732pair<const _Tp&, const _Tp&>
2733minmax(const _Tp& __a, const _Tp& __b)
2734{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002735 return _VSTD::minmax(__a, __b, __less<_Tp>());
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002736}
2737
Eric Fiselier93dd1372017-04-18 23:26:47 +00002738#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002739
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002740template<class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00002741_LIBCPP_NODISCARD_EXT inline
2742_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002743pair<_Tp, _Tp>
2744minmax(initializer_list<_Tp> __t, _Compare __comp)
2745{
Marshall Clowe9dca072014-02-19 16:51:35 +00002746 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2747 _Iter __first = __t.begin();
2748 _Iter __last = __t.end();
Marshall Clow447713a2015-02-11 15:41:34 +00002749 std::pair<_Tp, _Tp> __result(*__first, *__first);
Marshall Clowe9dca072014-02-19 16:51:35 +00002750
2751 ++__first;
2752 if (__t.size() % 2 == 0)
2753 {
2754 if (__comp(*__first, __result.first))
2755 __result.first = *__first;
2756 else
2757 __result.second = *__first;
2758 ++__first;
2759 }
Aditya Kumar3a0179a2016-08-25 11:52:38 +00002760
Marshall Clowe9dca072014-02-19 16:51:35 +00002761 while (__first != __last)
2762 {
2763 _Tp __prev = *__first++;
Marshall Clow447713a2015-02-11 15:41:34 +00002764 if (__comp(*__first, __prev)) {
2765 if ( __comp(*__first, __result.first)) __result.first = *__first;
2766 if (!__comp(__prev, __result.second)) __result.second = __prev;
Marshall Clowe9dca072014-02-19 16:51:35 +00002767 }
2768 else {
Marshall Clow447713a2015-02-11 15:41:34 +00002769 if ( __comp(__prev, __result.first)) __result.first = __prev;
2770 if (!__comp(*__first, __result.second)) __result.second = *__first;
Marshall Clowe9dca072014-02-19 16:51:35 +00002771 }
Aditya Kumar3a0179a2016-08-25 11:52:38 +00002772
Marshall Clowe9dca072014-02-19 16:51:35 +00002773 __first++;
2774 }
2775 return __result;
2776}
2777
2778template<class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00002779_LIBCPP_NODISCARD_EXT inline
2780_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowe9dca072014-02-19 16:51:35 +00002781pair<_Tp, _Tp>
2782minmax(initializer_list<_Tp> __t)
2783{
2784 return _VSTD::minmax(__t, __less<_Tp>());
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002785}
2786
Eric Fiselier93dd1372017-04-18 23:26:47 +00002787#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002788
Howard Hinnantc51e1022010-05-11 19:42:16 +00002789// random_shuffle
2790
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002791// __independent_bits_engine
2792
Howard Hinnantc834c512011-11-29 18:15:50 +00002793template <unsigned long long _Xp, size_t _Rp>
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002794struct __log2_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00002795{
Howard Hinnantc834c512011-11-29 18:15:50 +00002796 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2797 : __log2_imp<_Xp, _Rp - 1>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002798};
2799
Howard Hinnantc834c512011-11-29 18:15:50 +00002800template <unsigned long long _Xp>
2801struct __log2_imp<_Xp, 0>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002802{
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002803 static const size_t value = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002804};
2805
Howard Hinnantc834c512011-11-29 18:15:50 +00002806template <size_t _Rp>
2807struct __log2_imp<0, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002808{
Howard Hinnantc834c512011-11-29 18:15:50 +00002809 static const size_t value = _Rp + 1;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002810};
2811
Eric Fiselier4638fca2017-05-31 21:20:18 +00002812template <class _UIntType, _UIntType _Xp>
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002813struct __log2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002814{
Howard Hinnantc834c512011-11-29 18:15:50 +00002815 static const size_t value = __log2_imp<_Xp,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002816 sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002817};
2818
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002819template<class _Engine, class _UIntType>
2820class __independent_bits_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00002821{
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002822public:
2823 // types
2824 typedef _UIntType result_type;
2825
2826private:
2827 typedef typename _Engine::result_type _Engine_result_type;
2828 typedef typename conditional
2829 <
2830 sizeof(_Engine_result_type) <= sizeof(result_type),
2831 result_type,
2832 _Engine_result_type
2833 >::type _Working_result_type;
2834
2835 _Engine& __e_;
2836 size_t __w_;
2837 size_t __w0_;
2838 size_t __n_;
2839 size_t __n0_;
2840 _Working_result_type __y0_;
2841 _Working_result_type __y1_;
2842 _Engine_result_type __mask0_;
2843 _Engine_result_type __mask1_;
2844
Eric Fiselier93dd1372017-04-18 23:26:47 +00002845#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc834c512011-11-29 18:15:50 +00002846 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant5a646852012-04-02 21:00:45 +00002847 + _Working_result_type(1);
2848#else
2849 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2850 + _Working_result_type(1);
2851#endif
2852 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2853 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2854 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002855
2856public:
2857 // constructors and seeding functions
2858 __independent_bits_engine(_Engine& __e, size_t __w);
2859
2860 // generating functions
Howard Hinnantc834c512011-11-29 18:15:50 +00002861 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002862
2863private:
Marshall Clowfe778582017-09-20 19:38:43 +00002864 result_type __eval(false_type);
2865 result_type __eval(true_type);
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002866};
2867
2868template<class _Engine, class _UIntType>
2869__independent_bits_engine<_Engine, _UIntType>
2870 ::__independent_bits_engine(_Engine& __e, size_t __w)
2871 : __e_(__e),
2872 __w_(__w)
2873{
2874 __n_ = __w_ / __m + (__w_ % __m != 0);
2875 __w0_ = __w_ / __n_;
Howard Hinnantc834c512011-11-29 18:15:50 +00002876 if (_Rp == 0)
2877 __y0_ = _Rp;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002878 else if (__w0_ < _WDt)
Howard Hinnantc834c512011-11-29 18:15:50 +00002879 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002880 else
2881 __y0_ = 0;
Howard Hinnantc834c512011-11-29 18:15:50 +00002882 if (_Rp - __y0_ > __y0_ / __n_)
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002883 {
2884 ++__n_;
2885 __w0_ = __w_ / __n_;
2886 if (__w0_ < _WDt)
Howard Hinnantc834c512011-11-29 18:15:50 +00002887 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002888 else
2889 __y0_ = 0;
2890 }
2891 __n0_ = __n_ - __w_ % __n_;
2892 if (__w0_ < _WDt - 1)
Howard Hinnantc834c512011-11-29 18:15:50 +00002893 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002894 else
2895 __y1_ = 0;
2896 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2897 _Engine_result_type(0);
2898 __mask1_ = __w0_ < _EDt - 1 ?
2899 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2900 _Engine_result_type(~0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002901}
2902
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002903template<class _Engine, class _UIntType>
2904inline
2905_UIntType
Marshall Clowfe778582017-09-20 19:38:43 +00002906__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002907{
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002908 return static_cast<result_type>(__e_() & __mask0_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909}
2910
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002911template<class _Engine, class _UIntType>
2912_UIntType
Marshall Clowfe778582017-09-20 19:38:43 +00002913__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914{
Marshall Clowafc48592017-09-20 17:34:11 +00002915 const size_t _WRt = numeric_limits<result_type>::digits;
Howard Hinnantc834c512011-11-29 18:15:50 +00002916 result_type _Sp = 0;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002917 for (size_t __k = 0; __k < __n0_; ++__k)
2918 {
2919 _Engine_result_type __u;
2920 do
2921 {
2922 __u = __e_() - _Engine::min();
2923 } while (__u >= __y0_);
Marshall Clowafc48592017-09-20 17:34:11 +00002924 if (__w0_ < _WRt)
Howard Hinnantc834c512011-11-29 18:15:50 +00002925 _Sp <<= __w0_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002926 else
Howard Hinnantc834c512011-11-29 18:15:50 +00002927 _Sp = 0;
2928 _Sp += __u & __mask0_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002929 }
2930 for (size_t __k = __n0_; __k < __n_; ++__k)
2931 {
2932 _Engine_result_type __u;
2933 do
2934 {
2935 __u = __e_() - _Engine::min();
2936 } while (__u >= __y1_);
Marshall Clowafc48592017-09-20 17:34:11 +00002937 if (__w0_ < _WRt - 1)
Howard Hinnantc834c512011-11-29 18:15:50 +00002938 _Sp <<= __w0_ + 1;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002939 else
Howard Hinnantc834c512011-11-29 18:15:50 +00002940 _Sp = 0;
2941 _Sp += __u & __mask1_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002942 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002943 return _Sp;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002944}
2945
2946// uniform_int_distribution
2947
2948template<class _IntType = int>
2949class uniform_int_distribution
2950{
2951public:
2952 // types
2953 typedef _IntType result_type;
2954
2955 class param_type
2956 {
2957 result_type __a_;
2958 result_type __b_;
2959 public:
2960 typedef uniform_int_distribution distribution_type;
2961
2962 explicit param_type(result_type __a = 0,
2963 result_type __b = numeric_limits<result_type>::max())
2964 : __a_(__a), __b_(__b) {}
2965
2966 result_type a() const {return __a_;}
2967 result_type b() const {return __b_;}
2968
2969 friend bool operator==(const param_type& __x, const param_type& __y)
2970 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2971 friend bool operator!=(const param_type& __x, const param_type& __y)
2972 {return !(__x == __y);}
2973 };
2974
2975private:
2976 param_type __p_;
2977
2978public:
2979 // constructors and reset functions
2980 explicit uniform_int_distribution(result_type __a = 0,
2981 result_type __b = numeric_limits<result_type>::max())
2982 : __p_(param_type(__a, __b)) {}
2983 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
2984 void reset() {}
2985
2986 // generating functions
2987 template<class _URNG> result_type operator()(_URNG& __g)
2988 {return (*this)(__g, __p_);}
2989 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2990
2991 // property functions
2992 result_type a() const {return __p_.a();}
2993 result_type b() const {return __p_.b();}
2994
2995 param_type param() const {return __p_;}
2996 void param(const param_type& __p) {__p_ = __p;}
2997
2998 result_type min() const {return a();}
2999 result_type max() const {return b();}
3000
3001 friend bool operator==(const uniform_int_distribution& __x,
3002 const uniform_int_distribution& __y)
3003 {return __x.__p_ == __y.__p_;}
3004 friend bool operator!=(const uniform_int_distribution& __x,
3005 const uniform_int_distribution& __y)
3006 {return !(__x == __y);}
3007};
3008
3009template<class _IntType>
3010template<class _URNG>
3011typename uniform_int_distribution<_IntType>::result_type
3012uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
Marshall Clowf79f4402018-10-08 20:20:34 +00003013_LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003014{
3015 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3016 uint32_t, uint64_t>::type _UIntType;
Marshall Clowf79f4402018-10-08 20:20:34 +00003017 const _UIntType _Rp = _UIntType(__p.b()) - _UIntType(__p.a()) + _UIntType(1);
Howard Hinnantc834c512011-11-29 18:15:50 +00003018 if (_Rp == 1)
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003019 return __p.a();
3020 const size_t _Dt = numeric_limits<_UIntType>::digits;
3021 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
Howard Hinnantc834c512011-11-29 18:15:50 +00003022 if (_Rp == 0)
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003023 return static_cast<result_type>(_Eng(__g, _Dt)());
Howard Hinnantc834c512011-11-29 18:15:50 +00003024 size_t __w = _Dt - __clz(_Rp) - 1;
Marshall Clow40aada52015-07-30 18:26:34 +00003025 if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003026 ++__w;
3027 _Eng __e(__g, __w);
3028 _UIntType __u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003029 do
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003030 {
3031 __u = __e();
Howard Hinnantc834c512011-11-29 18:15:50 +00003032 } while (__u >= _Rp);
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003033 return static_cast<result_type>(__u + __p.a());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003034}
3035
Eric Fiselierf5fb27c2017-04-03 23:23:44 +00003036#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \
3037 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003038class _LIBCPP_TYPE_VIS __rs_default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003039
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003040_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003041
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003042class _LIBCPP_TYPE_VIS __rs_default
Howard Hinnantc51e1022010-05-11 19:42:16 +00003043{
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003044 static unsigned __c_;
3045
3046 __rs_default();
3047public:
Marshall Clow9903c5b2013-02-07 22:12:02 +00003048 typedef uint_fast32_t result_type;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003049
3050 static const result_type _Min = 0;
3051 static const result_type _Max = 0xFFFFFFFF;
3052
3053 __rs_default(const __rs_default&);
3054 ~__rs_default();
3055
3056 result_type operator()();
3057
Howard Hinnant664183b2012-04-02 00:40:41 +00003058 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
3059 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003060
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003061 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003062};
3063
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003064_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003065
3066template <class _RandomAccessIterator>
Louis Dionne481a2662018-09-23 18:35:00 +00003067_LIBCPP_DEPRECATED_IN_CXX14 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00003068random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
3069{
3070 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc834c512011-11-29 18:15:50 +00003071 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3072 typedef typename _Dp::param_type _Pp;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003073 difference_type __d = __last - __first;
3074 if (__d > 1)
3075 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003076 _Dp __uid;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003077 __rs_default __g = __rs_get();
Marshall Clowa224ace2019-01-24 19:20:19 +00003078 for (--__last, (void) --__d; __first < __last; ++__first, (void) --__d)
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003079 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003080 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003081 if (__i != difference_type(0))
3082 swap(*__first, *(__first + __i));
3083 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003084 }
3085}
3086
3087template <class _RandomAccessIterator, class _RandomNumberGenerator>
Louis Dionne481a2662018-09-23 18:35:00 +00003088_LIBCPP_DEPRECATED_IN_CXX14 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00003089random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselier93dd1372017-04-18 23:26:47 +00003090#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003091 _RandomNumberGenerator&& __rand)
3092#else
3093 _RandomNumberGenerator& __rand)
3094#endif
3095{
3096 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3097 difference_type __d = __last - __first;
3098 if (__d > 1)
3099 {
Marshall Clowa224ace2019-01-24 19:20:19 +00003100 for (--__last; __first < __last; ++__first, (void) --__d)
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003101 {
3102 difference_type __i = __rand(__d);
Marshall Clow5bdfc232018-09-11 18:33:45 +00003103 if (__i != difference_type(0))
Marshall Clowf79f4402018-10-08 20:20:34 +00003104 swap(*__first, *(__first + __i));
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003105 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003106 }
3107}
Marshall Clowfac06e52017-03-23 13:43:37 +00003108#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003109
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003110template <class _PopulationIterator, class _SampleIterator, class _Distance,
3111 class _UniformRandomNumberGenerator>
3112_LIBCPP_INLINE_VISIBILITY
3113_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsonc9637642017-11-14 11:14:25 +00003114 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003115 _Distance __n,
3116 _UniformRandomNumberGenerator & __g,
3117 input_iterator_tag) {
3118
3119 _Distance __k = 0;
3120 for (; __first != __last && __k < __n; ++__first, (void)++__k)
Alexander Richardsonc9637642017-11-14 11:14:25 +00003121 __output_iter[__k] = *__first;
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003122 _Distance __sz = __k;
3123 for (; __first != __last; ++__first, (void)++__k) {
3124 _Distance __r = _VSTD::uniform_int_distribution<_Distance>(0, __k)(__g);
3125 if (__r < __sz)
Alexander Richardsonc9637642017-11-14 11:14:25 +00003126 __output_iter[__r] = *__first;
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003127 }
Alexander Richardsonc9637642017-11-14 11:14:25 +00003128 return __output_iter + _VSTD::min(__n, __k);
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003129}
3130
3131template <class _PopulationIterator, class _SampleIterator, class _Distance,
3132 class _UniformRandomNumberGenerator>
3133_LIBCPP_INLINE_VISIBILITY
3134_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsonc9637642017-11-14 11:14:25 +00003135 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003136 _Distance __n,
3137 _UniformRandomNumberGenerator& __g,
3138 forward_iterator_tag) {
3139 _Distance __unsampled_sz = _VSTD::distance(__first, __last);
3140 for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
3141 _Distance __r =
3142 _VSTD::uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
3143 if (__r < __n) {
Alexander Richardsonc9637642017-11-14 11:14:25 +00003144 *__output_iter++ = *__first;
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003145 --__n;
3146 }
3147 }
Alexander Richardsonc9637642017-11-14 11:14:25 +00003148 return __output_iter;
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003149}
3150
3151template <class _PopulationIterator, class _SampleIterator, class _Distance,
3152 class _UniformRandomNumberGenerator>
3153_LIBCPP_INLINE_VISIBILITY
3154_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsonc9637642017-11-14 11:14:25 +00003155 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003156 _Distance __n, _UniformRandomNumberGenerator& __g) {
3157 typedef typename iterator_traits<_PopulationIterator>::iterator_category
3158 _PopCategory;
3159 typedef typename iterator_traits<_PopulationIterator>::difference_type
3160 _Difference;
3161 static_assert(__is_forward_iterator<_PopulationIterator>::value ||
3162 __is_random_access_iterator<_SampleIterator>::value,
3163 "SampleIterator must meet the requirements of RandomAccessIterator");
3164 typedef typename common_type<_Distance, _Difference>::type _CommonType;
3165 _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
3166 return _VSTD::__sample(
Alexander Richardsonc9637642017-11-14 11:14:25 +00003167 __first, __last, __output_iter, _CommonType(__n),
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003168 __g, _PopCategory());
3169}
3170
3171#if _LIBCPP_STD_VER > 14
3172template <class _PopulationIterator, class _SampleIterator, class _Distance,
3173 class _UniformRandomNumberGenerator>
3174inline _LIBCPP_INLINE_VISIBILITY
3175_SampleIterator sample(_PopulationIterator __first,
Alexander Richardsonc9637642017-11-14 11:14:25 +00003176 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003177 _Distance __n, _UniformRandomNumberGenerator&& __g) {
Alexander Richardsonc9637642017-11-14 11:14:25 +00003178 return _VSTD::__sample(__first, __last, __output_iter, __n, __g);
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003179}
3180#endif // _LIBCPP_STD_VER > 14
3181
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003182template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3183 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselier93dd1372017-04-18 23:26:47 +00003184#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanta5e71782010-11-18 01:47:02 +00003185 _UniformRandomNumberGenerator&& __g)
3186#else
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003187 _UniformRandomNumberGenerator& __g)
Howard Hinnanta5e71782010-11-18 01:47:02 +00003188#endif
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003189{
3190 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc834c512011-11-29 18:15:50 +00003191 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3192 typedef typename _Dp::param_type _Pp;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003193 difference_type __d = __last - __first;
3194 if (__d > 1)
3195 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003196 _Dp __uid;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003197 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003198 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003199 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003200 if (__i != difference_type(0))
3201 swap(*__first, *(__first + __i));
3202 }
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003203 }
3204}
3205
Howard Hinnantc51e1022010-05-11 19:42:16 +00003206template <class _InputIterator, class _Predicate>
Nico Weber471b10a2019-04-03 18:13:08 +00003207_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00003208is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3209{
3210 for (; __first != __last; ++__first)
3211 if (!__pred(*__first))
3212 break;
Marshall Clow3562ed72015-02-02 18:16:35 +00003213 if ( __first == __last )
3214 return true;
3215 ++__first;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003216 for (; __first != __last; ++__first)
3217 if (__pred(*__first))
3218 return false;
3219 return true;
3220}
3221
3222// partition
3223
3224template <class _Predicate, class _ForwardIterator>
3225_ForwardIterator
3226__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3227{
3228 while (true)
3229 {
3230 if (__first == __last)
3231 return __first;
3232 if (!__pred(*__first))
3233 break;
3234 ++__first;
3235 }
3236 for (_ForwardIterator __p = __first; ++__p != __last;)
3237 {
3238 if (__pred(*__p))
3239 {
3240 swap(*__first, *__p);
3241 ++__first;
3242 }
3243 }
3244 return __first;
3245}
3246
3247template <class _Predicate, class _BidirectionalIterator>
3248_BidirectionalIterator
3249__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3250 bidirectional_iterator_tag)
3251{
3252 while (true)
3253 {
3254 while (true)
3255 {
3256 if (__first == __last)
3257 return __first;
3258 if (!__pred(*__first))
3259 break;
3260 ++__first;
3261 }
3262 do
3263 {
3264 if (__first == --__last)
3265 return __first;
3266 } while (!__pred(*__last));
3267 swap(*__first, *__last);
3268 ++__first;
3269 }
3270}
3271
3272template <class _ForwardIterator, class _Predicate>
3273inline _LIBCPP_INLINE_VISIBILITY
3274_ForwardIterator
3275partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3276{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003277 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003278 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3279}
3280
3281// partition_copy
3282
3283template <class _InputIterator, class _OutputIterator1,
3284 class _OutputIterator2, class _Predicate>
Marshall Clow5492c8a2018-01-22 20:44:33 +00003285_LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_OutputIterator1, _OutputIterator2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003286partition_copy(_InputIterator __first, _InputIterator __last,
3287 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3288 _Predicate __pred)
3289{
3290 for (; __first != __last; ++__first)
3291 {
3292 if (__pred(*__first))
3293 {
3294 *__out_true = *__first;
3295 ++__out_true;
3296 }
3297 else
3298 {
3299 *__out_false = *__first;
3300 ++__out_false;
3301 }
3302 }
3303 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3304}
3305
3306// partition_point
3307
3308template<class _ForwardIterator, class _Predicate>
Marshall Clowcb3c8262018-01-15 17:53:34 +00003309_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003310partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3311{
3312 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003313 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003314 while (__len != 0)
3315 {
Louis Dionnedda14512018-12-17 16:04:39 +00003316 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003317 _ForwardIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003318 _VSTD::advance(__m, __l2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003319 if (__pred(*__m))
3320 {
3321 __first = ++__m;
3322 __len -= __l2 + 1;
3323 }
3324 else
3325 __len = __l2;
3326 }
3327 return __first;
3328}
3329
3330// stable_partition
3331
3332template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3333_ForwardIterator
3334__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3335 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3336{
3337 // *__first is known to be false
3338 // __len >= 1
3339 if (__len == 1)
3340 return __first;
3341 if (__len == 2)
3342 {
3343 _ForwardIterator __m = __first;
3344 if (__pred(*++__m))
3345 {
3346 swap(*__first, *__m);
3347 return __m;
3348 }
3349 return __first;
3350 }
3351 if (__len <= __p.second)
3352 { // The buffer is big enough to use
3353 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3354 __destruct_n __d(0);
3355 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3356 // Move the falses into the temporary buffer, and the trues to the front of the line
3357 // Update __first to always point to the end of the trues
3358 value_type* __t = __p.first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003359 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003360 __d.__incr((value_type*)0);
3361 ++__t;
3362 _ForwardIterator __i = __first;
3363 while (++__i != __last)
3364 {
3365 if (__pred(*__i))
3366 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003367 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003368 ++__first;
3369 }
3370 else
3371 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003372 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003373 __d.__incr((value_type*)0);
3374 ++__t;
3375 }
3376 }
3377 // All trues now at start of range, all falses in buffer
3378 // Move falses back into range, but don't mess up __first which points to first false
3379 __i = __first;
3380 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003381 *__i = _VSTD::move(*__t2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003382 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3383 return __first;
3384 }
3385 // Else not enough buffer, do in place
3386 // __len >= 3
3387 _ForwardIterator __m = __first;
3388 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003389 _VSTD::advance(__m, __len2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003390 // recurse on [__first, __m), *__first know to be false
3391 // F?????????????????
3392 // f m l
3393 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3394 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3395 // TTTFFFFF??????????
3396 // f ff m l
3397 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3398 _ForwardIterator __m1 = __m;
3399 _ForwardIterator __second_false = __last;
3400 _Distance __len_half = __len - __len2;
3401 while (__pred(*__m1))
3402 {
3403 if (++__m1 == __last)
3404 goto __second_half_done;
3405 --__len_half;
3406 }
3407 // TTTFFFFFTTTF??????
3408 // f ff m m1 l
3409 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3410__second_half_done:
3411 // TTTFFFFFTTTTTFFFFF
3412 // f ff m sf l
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003413 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003414 // TTTTTTTTFFFFFFFFFF
3415 // |
3416}
3417
3418struct __return_temporary_buffer
3419{
3420 template <class _Tp>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003421 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003422};
3423
3424template <class _Predicate, class _ForwardIterator>
3425_ForwardIterator
3426__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3427 forward_iterator_tag)
3428{
3429 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3430 // Either prove all true and return __first or point to first false
3431 while (true)
3432 {
3433 if (__first == __last)
3434 return __first;
3435 if (!__pred(*__first))
3436 break;
3437 ++__first;
3438 }
3439 // We now have a reduced range [__first, __last)
3440 // *__first is known to be false
3441 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3442 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003443 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003444 pair<value_type*, ptrdiff_t> __p(0, 0);
3445 unique_ptr<value_type, __return_temporary_buffer> __h;
3446 if (__len >= __alloc_limit)
3447 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003448 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003449 __h.reset(__p.first);
3450 }
3451 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3452 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3453}
3454
3455template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3456_BidirectionalIterator
3457__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3458 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3459{
3460 // *__first is known to be false
3461 // *__last is known to be true
3462 // __len >= 2
3463 if (__len == 2)
3464 {
3465 swap(*__first, *__last);
3466 return __last;
3467 }
3468 if (__len == 3)
3469 {
3470 _BidirectionalIterator __m = __first;
3471 if (__pred(*++__m))
3472 {
3473 swap(*__first, *__m);
3474 swap(*__m, *__last);
3475 return __last;
3476 }
3477 swap(*__m, *__last);
3478 swap(*__first, *__m);
3479 return __m;
3480 }
3481 if (__len <= __p.second)
3482 { // The buffer is big enough to use
3483 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3484 __destruct_n __d(0);
3485 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3486 // Move the falses into the temporary buffer, and the trues to the front of the line
3487 // Update __first to always point to the end of the trues
3488 value_type* __t = __p.first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003489 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003490 __d.__incr((value_type*)0);
3491 ++__t;
3492 _BidirectionalIterator __i = __first;
3493 while (++__i != __last)
3494 {
3495 if (__pred(*__i))
3496 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003497 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003498 ++__first;
3499 }
3500 else
3501 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003502 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003503 __d.__incr((value_type*)0);
3504 ++__t;
3505 }
3506 }
3507 // move *__last, known to be true
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003508 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003509 __i = ++__first;
3510 // All trues now at start of range, all falses in buffer
3511 // Move falses back into range, but don't mess up __first which points to first false
3512 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003513 *__i = _VSTD::move(*__t2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003514 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3515 return __first;
3516 }
3517 // Else not enough buffer, do in place
3518 // __len >= 4
3519 _BidirectionalIterator __m = __first;
3520 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003521 _VSTD::advance(__m, __len2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003522 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3523 // F????????????????T
3524 // f m l
3525 _BidirectionalIterator __m1 = __m;
3526 _BidirectionalIterator __first_false = __first;
3527 _Distance __len_half = __len2;
3528 while (!__pred(*--__m1))
3529 {
3530 if (__m1 == __first)
3531 goto __first_half_done;
3532 --__len_half;
3533 }
3534 // F???TFFF?????????T
3535 // f m1 m l
3536 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3537 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3538__first_half_done:
3539 // TTTFFFFF?????????T
3540 // f ff m l
3541 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3542 __m1 = __m;
3543 _BidirectionalIterator __second_false = __last;
3544 ++__second_false;
3545 __len_half = __len - __len2;
3546 while (__pred(*__m1))
3547 {
3548 if (++__m1 == __last)
3549 goto __second_half_done;
3550 --__len_half;
3551 }
3552 // TTTFFFFFTTTF?????T
3553 // f ff m m1 l
3554 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3555__second_half_done:
3556 // TTTFFFFFTTTTTFFFFF
3557 // f ff m sf l
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003558 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559 // TTTTTTTTFFFFFFFFFF
3560 // |
3561}
3562
3563template <class _Predicate, class _BidirectionalIterator>
3564_BidirectionalIterator
3565__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3566 bidirectional_iterator_tag)
3567{
3568 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3569 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3570 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3571 // Either prove all true and return __first or point to first false
3572 while (true)
3573 {
3574 if (__first == __last)
3575 return __first;
3576 if (!__pred(*__first))
3577 break;
3578 ++__first;
3579 }
3580 // __first points to first false, everything prior to __first is already set.
3581 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3582 do
3583 {
3584 if (__first == --__last)
3585 return __first;
3586 } while (!__pred(*__last));
3587 // We now have a reduced range [__first, __last]
3588 // *__first is known to be false
3589 // *__last is known to be true
3590 // __len >= 2
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003591 difference_type __len = _VSTD::distance(__first, __last) + 1;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003592 pair<value_type*, ptrdiff_t> __p(0, 0);
3593 unique_ptr<value_type, __return_temporary_buffer> __h;
3594 if (__len >= __alloc_limit)
3595 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003596 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003597 __h.reset(__p.first);
3598 }
3599 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3600 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3601}
3602
3603template <class _ForwardIterator, class _Predicate>
3604inline _LIBCPP_INLINE_VISIBILITY
3605_ForwardIterator
3606stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3607{
3608 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3609 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3610}
3611
3612// is_sorted_until
3613
3614template <class _ForwardIterator, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00003615_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003616is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3617{
3618 if (__first != __last)
3619 {
3620 _ForwardIterator __i = __first;
3621 while (++__i != __last)
3622 {
3623 if (__comp(*__i, *__first))
3624 return __i;
3625 __first = __i;
3626 }
3627 }
3628 return __last;
3629}
3630
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003631template<class _ForwardIterator>
Nico Weber471b10a2019-04-03 18:13:08 +00003632_LIBCPP_NODISCARD_EXT inline
3633_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003634_ForwardIterator
3635is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3636{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003637 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003638}
3639
3640// is_sorted
3641
3642template <class _ForwardIterator, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00003643_LIBCPP_NODISCARD_EXT inline
3644_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003645bool
3646is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3647{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003648 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003649}
3650
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003651template<class _ForwardIterator>
Nico Weber471b10a2019-04-03 18:13:08 +00003652_LIBCPP_NODISCARD_EXT inline
3653_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003654bool
3655is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3656{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003657 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003658}
3659
3660// sort
3661
3662// stable, 2-3 compares, 0-2 swaps
3663
3664template <class _Compare, class _ForwardIterator>
3665unsigned
3666__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3667{
3668 unsigned __r = 0;
3669 if (!__c(*__y, *__x)) // if x <= y
3670 {
3671 if (!__c(*__z, *__y)) // if y <= z
3672 return __r; // x <= y && y <= z
3673 // x <= y && y > z
3674 swap(*__y, *__z); // x <= z && y < z
3675 __r = 1;
3676 if (__c(*__y, *__x)) // if x > y
3677 {
3678 swap(*__x, *__y); // x < y && y <= z
3679 __r = 2;
3680 }
3681 return __r; // x <= y && y < z
3682 }
3683 if (__c(*__z, *__y)) // x > y, if y > z
3684 {
3685 swap(*__x, *__z); // x < y && y < z
3686 __r = 1;
3687 return __r;
3688 }
3689 swap(*__x, *__y); // x > y && y <= z
3690 __r = 1; // x < y && x <= z
3691 if (__c(*__z, *__y)) // if y > z
3692 {
3693 swap(*__y, *__z); // x <= y && y < z
3694 __r = 2;
3695 }
3696 return __r;
3697} // x <= y && y <= z
3698
3699// stable, 3-6 compares, 0-5 swaps
3700
3701template <class _Compare, class _ForwardIterator>
3702unsigned
3703__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3704 _ForwardIterator __x4, _Compare __c)
3705{
3706 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3707 if (__c(*__x4, *__x3))
3708 {
3709 swap(*__x3, *__x4);
3710 ++__r;
3711 if (__c(*__x3, *__x2))
3712 {
3713 swap(*__x2, *__x3);
3714 ++__r;
3715 if (__c(*__x2, *__x1))
3716 {
3717 swap(*__x1, *__x2);
3718 ++__r;
3719 }
3720 }
3721 }
3722 return __r;
3723}
3724
3725// stable, 4-10 compares, 0-9 swaps
3726
3727template <class _Compare, class _ForwardIterator>
Louis Dionne9e70e362018-11-21 16:24:46 +00003728_LIBCPP_HIDDEN
Howard Hinnantc51e1022010-05-11 19:42:16 +00003729unsigned
3730__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3731 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3732{
3733 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3734 if (__c(*__x5, *__x4))
3735 {
3736 swap(*__x4, *__x5);
3737 ++__r;
3738 if (__c(*__x4, *__x3))
3739 {
3740 swap(*__x3, *__x4);
3741 ++__r;
3742 if (__c(*__x3, *__x2))
3743 {
3744 swap(*__x2, *__x3);
3745 ++__r;
3746 if (__c(*__x2, *__x1))
3747 {
3748 swap(*__x1, *__x2);
3749 ++__r;
3750 }
3751 }
3752 }
3753 }
3754 return __r;
3755}
3756
3757// Assumes size > 0
3758template <class _Compare, class _BirdirectionalIterator>
3759void
3760__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3761{
3762 _BirdirectionalIterator __lm1 = __last;
3763 for (--__lm1; __first != __lm1; ++__first)
3764 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003765 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003766 typename add_lvalue_reference<_Compare>::type>
3767 (__first, __last, __comp);
3768 if (__i != __first)
3769 swap(*__first, *__i);
3770 }
3771}
3772
3773template <class _Compare, class _BirdirectionalIterator>
3774void
3775__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3776{
3777 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3778 if (__first != __last)
3779 {
3780 _BirdirectionalIterator __i = __first;
3781 for (++__i; __i != __last; ++__i)
3782 {
3783 _BirdirectionalIterator __j = __i;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003784 value_type __t(_VSTD::move(*__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003785 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003786 *__j = _VSTD::move(*__k);
3787 *__j = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003788 }
3789 }
3790}
3791
3792template <class _Compare, class _RandomAccessIterator>
3793void
3794__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3795{
3796 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3797 _RandomAccessIterator __j = __first+2;
3798 __sort3<_Compare>(__first, __first+1, __j, __comp);
3799 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3800 {
3801 if (__comp(*__i, *__j))
3802 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003803 value_type __t(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003804 _RandomAccessIterator __k = __j;
3805 __j = __i;
3806 do
3807 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003808 *__j = _VSTD::move(*__k);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003809 __j = __k;
3810 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003811 *__j = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003812 }
3813 __j = __i;
3814 }
3815}
3816
3817template <class _Compare, class _RandomAccessIterator>
3818bool
3819__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3820{
3821 switch (__last - __first)
3822 {
3823 case 0:
3824 case 1:
3825 return true;
3826 case 2:
3827 if (__comp(*--__last, *__first))
3828 swap(*__first, *__last);
3829 return true;
3830 case 3:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003831 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003832 return true;
3833 case 4:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003834 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003835 return true;
3836 case 5:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003837 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003838 return true;
3839 }
3840 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3841 _RandomAccessIterator __j = __first+2;
3842 __sort3<_Compare>(__first, __first+1, __j, __comp);
3843 const unsigned __limit = 8;
3844 unsigned __count = 0;
3845 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3846 {
3847 if (__comp(*__i, *__j))
3848 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003849 value_type __t(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003850 _RandomAccessIterator __k = __j;
3851 __j = __i;
3852 do
3853 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003854 *__j = _VSTD::move(*__k);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003855 __j = __k;
3856 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003857 *__j = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003858 if (++__count == __limit)
3859 return ++__i == __last;
3860 }
3861 __j = __i;
3862 }
3863 return true;
3864}
3865
3866template <class _Compare, class _BirdirectionalIterator>
3867void
3868__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3869 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3870{
3871 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3872 if (__first1 != __last1)
3873 {
3874 __destruct_n __d(0);
3875 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3876 value_type* __last2 = __first2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003877 ::new(__last2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003878 __d.__incr((value_type*)0);
3879 for (++__last2; ++__first1 != __last1; ++__last2)
3880 {
3881 value_type* __j2 = __last2;
3882 value_type* __i2 = __j2;
3883 if (__comp(*__first1, *--__i2))
3884 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003885 ::new(__j2) value_type(_VSTD::move(*__i2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003886 __d.__incr((value_type*)0);
3887 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003888 *__j2 = _VSTD::move(*__i2);
3889 *__j2 = _VSTD::move(*__first1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003890 }
3891 else
3892 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003893 ::new(__j2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003894 __d.__incr((value_type*)0);
3895 }
3896 }
3897 __h.release();
3898 }
3899}
3900
3901template <class _Compare, class _RandomAccessIterator>
3902void
3903__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3904{
3905 // _Compare is known to be a reference type
3906 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3907 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003908 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3909 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003910 while (true)
3911 {
3912 __restart:
3913 difference_type __len = __last - __first;
3914 switch (__len)
3915 {
3916 case 0:
3917 case 1:
3918 return;
3919 case 2:
3920 if (__comp(*--__last, *__first))
3921 swap(*__first, *__last);
3922 return;
3923 case 3:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003924 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003925 return;
3926 case 4:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003927 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003928 return;
3929 case 5:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003930 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003931 return;
3932 }
3933 if (__len <= __limit)
3934 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003935 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003936 return;
3937 }
3938 // __len > 5
3939 _RandomAccessIterator __m = __first;
3940 _RandomAccessIterator __lm1 = __last;
3941 --__lm1;
3942 unsigned __n_swaps;
3943 {
3944 difference_type __delta;
3945 if (__len >= 1000)
3946 {
3947 __delta = __len/2;
3948 __m += __delta;
3949 __delta /= 2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003950 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003951 }
3952 else
3953 {
3954 __delta = __len/2;
3955 __m += __delta;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003956 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003957 }
3958 }
3959 // *__m is median
3960 // partition [__first, __m) < *__m and *__m <= [__m, __last)
3961 // (this inhibits tossing elements equivalent to __m around unnecessarily)
3962 _RandomAccessIterator __i = __first;
3963 _RandomAccessIterator __j = __lm1;
3964 // j points beyond range to be tested, *__m is known to be <= *__lm1
3965 // The search going up is known to be guarded but the search coming down isn't.
3966 // Prime the downward search with a guard.
3967 if (!__comp(*__i, *__m)) // if *__first == *__m
3968 {
3969 // *__first == *__m, *__first doesn't go in first part
3970 // manually guard downward moving __j against __i
3971 while (true)
3972 {
3973 if (__i == --__j)
3974 {
3975 // *__first == *__m, *__m <= all other elements
3976 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
3977 ++__i; // __first + 1
3978 __j = __last;
3979 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
3980 {
3981 while (true)
3982 {
3983 if (__i == __j)
3984 return; // [__first, __last) all equivalent elements
3985 if (__comp(*__first, *__i))
3986 {
3987 swap(*__i, *__j);
3988 ++__n_swaps;
3989 ++__i;
3990 break;
3991 }
3992 ++__i;
3993 }
3994 }
3995 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
3996 if (__i == __j)
3997 return;
3998 while (true)
3999 {
4000 while (!__comp(*__first, *__i))
4001 ++__i;
4002 while (__comp(*__first, *--__j))
4003 ;
4004 if (__i >= __j)
4005 break;
4006 swap(*__i, *__j);
4007 ++__n_swaps;
4008 ++__i;
4009 }
4010 // [__first, __i) == *__first and *__first < [__i, __last)
4011 // The first part is sorted, sort the secod part
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004012 // _VSTD::__sort<_Compare>(__i, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004013 __first = __i;
4014 goto __restart;
4015 }
4016 if (__comp(*__j, *__m))
4017 {
4018 swap(*__i, *__j);
4019 ++__n_swaps;
4020 break; // found guard for downward moving __j, now use unguarded partition
4021 }
4022 }
4023 }
4024 // It is known that *__i < *__m
4025 ++__i;
4026 // j points beyond range to be tested, *__m is known to be <= *__lm1
4027 // if not yet partitioned...
4028 if (__i < __j)
4029 {
4030 // known that *(__i - 1) < *__m
4031 // known that __i <= __m
4032 while (true)
4033 {
4034 // __m still guards upward moving __i
4035 while (__comp(*__i, *__m))
4036 ++__i;
4037 // It is now known that a guard exists for downward moving __j
4038 while (!__comp(*--__j, *__m))
4039 ;
4040 if (__i > __j)
4041 break;
4042 swap(*__i, *__j);
4043 ++__n_swaps;
4044 // It is known that __m != __j
4045 // If __m just moved, follow it
4046 if (__m == __i)
4047 __m = __j;
4048 ++__i;
4049 }
4050 }
4051 // [__first, __i) < *__m and *__m <= [__i, __last)
4052 if (__i != __m && __comp(*__m, *__i))
4053 {
4054 swap(*__i, *__m);
4055 ++__n_swaps;
4056 }
4057 // [__first, __i) < *__i and *__i <= [__i+1, __last)
4058 // If we were given a perfect partition, see if insertion sort is quick...
4059 if (__n_swaps == 0)
4060 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004061 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
4062 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004063 {
4064 if (__fs)
4065 return;
4066 __last = __i;
4067 continue;
4068 }
4069 else
4070 {
4071 if (__fs)
4072 {
4073 __first = ++__i;
4074 continue;
4075 }
4076 }
4077 }
4078 // sort smaller range with recursive call and larger with tail recursion elimination
4079 if (__i - __first < __last - __i)
4080 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004081 _VSTD::__sort<_Compare>(__first, __i, __comp);
4082 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004083 __first = ++__i;
4084 }
4085 else
4086 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004087 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4088 // _VSTD::__sort<_Compare>(__first, __i, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004089 __last = __i;
4090 }
4091 }
4092}
4093
4094// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
4095template <class _RandomAccessIterator, class _Compare>
4096inline _LIBCPP_INLINE_VISIBILITY
4097void
4098sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4099{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00004100 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4101 _VSTD::__sort<_Comp_ref>(__first, __last, _Comp_ref(__comp));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004102}
4103
4104template <class _RandomAccessIterator>
4105inline _LIBCPP_INLINE_VISIBILITY
4106void
4107sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4108{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004109 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004110}
4111
4112template <class _Tp>
4113inline _LIBCPP_INLINE_VISIBILITY
4114void
4115sort(_Tp** __first, _Tp** __last)
4116{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004117 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004118}
4119
4120template <class _Tp>
4121inline _LIBCPP_INLINE_VISIBILITY
4122void
4123sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4124{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004125 _VSTD::sort(__first.base(), __last.base());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004126}
4127
Howard Hinnant27e0e772011-09-14 18:33:51 +00004128template <class _Tp, class _Compare>
4129inline _LIBCPP_INLINE_VISIBILITY
4130void
4131sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4132{
4133 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4134 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4135}
4136
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004137_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4138_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4139_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4140_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4141_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4142_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4143_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4144_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4145_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4146_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4147_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4148_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>&))
4149_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4150_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4151_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 +00004152
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004153_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4154_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4155_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4156_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4157_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4158_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4159_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4160_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4161_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4162_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4163_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4164_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>&))
4165_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4166_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4167_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 +00004168
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004169_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 +00004170
4171// lower_bound
4172
4173template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004174_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004175__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004176{
4177 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004178 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004179 while (__len != 0)
4180 {
Louis Dionnedda14512018-12-17 16:04:39 +00004181 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004182 _ForwardIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004183 _VSTD::advance(__m, __l2);
Howard Hinnantbf074022011-10-22 20:59:45 +00004184 if (__comp(*__m, __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004185 {
4186 __first = ++__m;
4187 __len -= __l2 + 1;
4188 }
4189 else
4190 __len = __l2;
4191 }
4192 return __first;
4193}
4194
4195template <class _ForwardIterator, class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00004196_LIBCPP_NODISCARD_EXT inline
4197_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004198_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004199lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004200{
Howard Hinnantc51e1022010-05-11 19:42:16 +00004201 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantbf074022011-10-22 20:59:45 +00004202 return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004203}
4204
4205template <class _ForwardIterator, class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00004206_LIBCPP_NODISCARD_EXT inline
4207_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004208_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004209lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004210{
Howard Hinnantbf074022011-10-22 20:59:45 +00004211 return _VSTD::lower_bound(__first, __last, __value_,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004212 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4213}
4214
4215// upper_bound
4216
4217template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004218_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004219__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004220{
4221 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004222 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004223 while (__len != 0)
4224 {
Louis Dionnedda14512018-12-17 16:04:39 +00004225 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226 _ForwardIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004227 _VSTD::advance(__m, __l2);
Howard Hinnantbf074022011-10-22 20:59:45 +00004228 if (__comp(__value_, *__m))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004229 __len = __l2;
4230 else
4231 {
4232 __first = ++__m;
4233 __len -= __l2 + 1;
4234 }
4235 }
4236 return __first;
4237}
4238
4239template <class _ForwardIterator, class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00004240_LIBCPP_NODISCARD_EXT inline
4241_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004242_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004243upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004244{
Howard Hinnantc51e1022010-05-11 19:42:16 +00004245 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantbf074022011-10-22 20:59:45 +00004246 return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247}
4248
4249template <class _ForwardIterator, class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00004250_LIBCPP_NODISCARD_EXT inline
4251_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004252_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004253upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004254{
Howard Hinnantbf074022011-10-22 20:59:45 +00004255 return _VSTD::upper_bound(__first, __last, __value_,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004256 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4257}
4258
4259// equal_range
4260
4261template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004262_LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_ForwardIterator, _ForwardIterator>
Howard Hinnantbf074022011-10-22 20:59:45 +00004263__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004264{
4265 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004266 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004267 while (__len != 0)
4268 {
Louis Dionnedda14512018-12-17 16:04:39 +00004269 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004270 _ForwardIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004271 _VSTD::advance(__m, __l2);
Howard Hinnantbf074022011-10-22 20:59:45 +00004272 if (__comp(*__m, __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004273 {
4274 __first = ++__m;
4275 __len -= __l2 + 1;
4276 }
Howard Hinnantbf074022011-10-22 20:59:45 +00004277 else if (__comp(__value_, *__m))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278 {
4279 __last = __m;
4280 __len = __l2;
4281 }
4282 else
4283 {
4284 _ForwardIterator __mp1 = __m;
4285 return pair<_ForwardIterator, _ForwardIterator>
4286 (
Howard Hinnantbf074022011-10-22 20:59:45 +00004287 __lower_bound<_Compare>(__first, __m, __value_, __comp),
4288 __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004289 );
4290 }
4291 }
4292 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4293}
4294
4295template <class _ForwardIterator, class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00004296_LIBCPP_NODISCARD_EXT inline
4297_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004298pair<_ForwardIterator, _ForwardIterator>
Howard Hinnantbf074022011-10-22 20:59:45 +00004299equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004300{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00004301 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantbf074022011-10-22 20:59:45 +00004302 return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004303}
4304
4305template <class _ForwardIterator, class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00004306_LIBCPP_NODISCARD_EXT inline
4307_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004308pair<_ForwardIterator, _ForwardIterator>
Howard Hinnantbf074022011-10-22 20:59:45 +00004309equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004310{
Howard Hinnantbf074022011-10-22 20:59:45 +00004311 return _VSTD::equal_range(__first, __last, __value_,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004312 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4313}
4314
4315// binary_search
4316
4317template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004318inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004319bool
Howard Hinnantbf074022011-10-22 20:59:45 +00004320__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004321{
Howard Hinnantbf074022011-10-22 20:59:45 +00004322 __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4323 return __first != __last && !__comp(__value_, *__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004324}
4325
4326template <class _ForwardIterator, class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00004327_LIBCPP_NODISCARD_EXT inline
4328_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004329bool
Howard Hinnantbf074022011-10-22 20:59:45 +00004330binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004331{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00004332 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantbf074022011-10-22 20:59:45 +00004333 return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004334}
4335
4336template <class _ForwardIterator, class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00004337_LIBCPP_NODISCARD_EXT inline
4338_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004339bool
Howard Hinnantbf074022011-10-22 20:59:45 +00004340binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004341{
Howard Hinnantbf074022011-10-22 20:59:45 +00004342 return _VSTD::binary_search(__first, __last, __value_,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004343 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4344}
4345
4346// merge
4347
4348template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4349_OutputIterator
4350__merge(_InputIterator1 __first1, _InputIterator1 __last1,
4351 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4352{
4353 for (; __first1 != __last1; ++__result)
4354 {
4355 if (__first2 == __last2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004356 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004357 if (__comp(*__first2, *__first1))
4358 {
4359 *__result = *__first2;
4360 ++__first2;
4361 }
4362 else
4363 {
4364 *__result = *__first1;
4365 ++__first1;
4366 }
4367 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004368 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004369}
4370
4371template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4372inline _LIBCPP_INLINE_VISIBILITY
4373_OutputIterator
4374merge(_InputIterator1 __first1, _InputIterator1 __last1,
4375 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4376{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00004377 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004378 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004379}
4380
4381template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4382inline _LIBCPP_INLINE_VISIBILITY
4383_OutputIterator
4384merge(_InputIterator1 __first1, _InputIterator1 __last1,
4385 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4386{
4387 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4388 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4389 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4390}
4391
4392// inplace_merge
4393
Marshall Clow1bc51102015-07-29 16:25:45 +00004394template <class _Compare, class _InputIterator1, class _InputIterator2,
4395 class _OutputIterator>
4396void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4397 _InputIterator2 __first2, _InputIterator2 __last2,
4398 _OutputIterator __result, _Compare __comp)
4399{
4400 for (; __first1 != __last1; ++__result)
4401 {
4402 if (__first2 == __last2)
4403 {
4404 _VSTD::move(__first1, __last1, __result);
4405 return;
4406 }
4407
4408 if (__comp(*__first2, *__first1))
4409 {
4410 *__result = _VSTD::move(*__first2);
4411 ++__first2;
4412 }
4413 else
4414 {
4415 *__result = _VSTD::move(*__first1);
4416 ++__first1;
4417 }
4418 }
4419 // __first2 through __last2 are already in the right spot.
4420}
4421
Howard Hinnantc51e1022010-05-11 19:42:16 +00004422template <class _Compare, class _BidirectionalIterator>
4423void
4424__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4425 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4426 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4427 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4428{
4429 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004430 __destruct_n __d(0);
4431 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4432 if (__len1 <= __len2)
4433 {
4434 value_type* __p = __buff;
Eric Fiseliera09a3b42014-10-27 19:28:20 +00004435 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004436 ::new(__p) value_type(_VSTD::move(*__i));
Marshall Clow1bc51102015-07-29 16:25:45 +00004437 __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004438 }
4439 else
4440 {
4441 value_type* __p = __buff;
Eric Fiseliera09a3b42014-10-27 19:28:20 +00004442 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004443 ::new(__p) value_type(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004444 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4445 typedef reverse_iterator<value_type*> _Rv;
Aditya Kumar3a0179a2016-08-25 11:52:38 +00004446 __half_inplace_merge(_Rv(__p), _Rv(__buff),
Marshall Clow1bc51102015-07-29 16:25:45 +00004447 _RBi(__middle), _RBi(__first),
Marshall Clow738d1042017-08-28 23:16:13 +00004448 _RBi(__last), __invert<_Compare>(__comp));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004449 }
4450}
4451
4452template <class _Compare, class _BidirectionalIterator>
4453void
4454__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4455 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4456 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4457 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4458{
Howard Hinnantc51e1022010-05-11 19:42:16 +00004459 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4460 while (true)
4461 {
4462 // if __middle == __last, we're done
4463 if (__len2 == 0)
4464 return;
Marshall Clow8eff8232015-02-02 16:44:11 +00004465 if (__len1 <= __buff_size || __len2 <= __buff_size)
4466 return __buffered_inplace_merge<_Compare>
4467 (__first, __middle, __last, __comp, __len1, __len2, __buff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004468 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
Eric Fiseliera09a3b42014-10-27 19:28:20 +00004469 for (; true; ++__first, (void) --__len1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004470 {
4471 if (__len1 == 0)
4472 return;
4473 if (__comp(*__middle, *__first))
4474 break;
4475 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004476 // __first < __middle < __last
4477 // *__first > *__middle
4478 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4479 // all elements in:
4480 // [__first, __m1) <= [__middle, __m2)
4481 // [__middle, __m2) < [__m1, __middle)
4482 // [__m1, __middle) <= [__m2, __last)
4483 // and __m1 or __m2 is in the middle of its range
4484 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4485 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4486 difference_type __len11; // distance(__first, __m1)
4487 difference_type __len21; // distance(__middle, __m2)
4488 // binary search smaller range
4489 if (__len1 < __len2)
4490 { // __len >= 1, __len2 >= 2
4491 __len21 = __len2 / 2;
4492 __m2 = __middle;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004493 _VSTD::advance(__m2, __len21);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004494 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004495 __len11 = _VSTD::distance(__first, __m1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004496 }
4497 else
4498 {
4499 if (__len1 == 1)
4500 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4501 // It is known *__first > *__middle
4502 swap(*__first, *__middle);
4503 return;
4504 }
4505 // __len1 >= 2, __len2 >= 1
4506 __len11 = __len1 / 2;
4507 __m1 = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004508 _VSTD::advance(__m1, __len11);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004509 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004510 __len21 = _VSTD::distance(__middle, __m2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004511 }
4512 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4513 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4514 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4515 // swap middle two partitions
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004516 __middle = _VSTD::rotate(__m1, __middle, __m2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004517 // __len12 and __len21 now have swapped meanings
4518 // merge smaller range with recurisve call and larger with tail recursion elimination
4519 if (__len11 + __len21 < __len12 + __len22)
4520 {
4521 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4522// __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4523 __first = __middle;
4524 __middle = __m2;
4525 __len1 = __len12;
4526 __len2 = __len22;
4527 }
4528 else
4529 {
4530 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4531// __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4532 __last = __middle;
4533 __middle = __m1;
4534 __len1 = __len11;
4535 __len2 = __len21;
4536 }
4537 }
4538}
4539
Howard Hinnantc51e1022010-05-11 19:42:16 +00004540template <class _BidirectionalIterator, class _Compare>
4541inline _LIBCPP_INLINE_VISIBILITY
4542void
4543inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4544 _Compare __comp)
4545{
4546 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4547 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004548 difference_type __len1 = _VSTD::distance(__first, __middle);
4549 difference_type __len2 = _VSTD::distance(__middle, __last);
4550 difference_type __buf_size = _VSTD::min(__len1, __len2);
Marshall Clow488f19f2015-02-02 17:35:53 +00004551 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4552 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00004553 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004554 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004555 __buf.first, __buf.second);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004556}
4557
4558template <class _BidirectionalIterator>
4559inline _LIBCPP_INLINE_VISIBILITY
4560void
4561inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4562{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004563 _VSTD::inplace_merge(__first, __middle, __last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004564 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4565}
4566
4567// stable_sort
4568
4569template <class _Compare, class _InputIterator1, class _InputIterator2>
4570void
4571__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4572 _InputIterator2 __first2, _InputIterator2 __last2,
4573 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4574{
4575 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4576 __destruct_n __d(0);
4577 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4578 for (; true; ++__result)
4579 {
4580 if (__first1 == __last1)
4581 {
4582 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004583 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004584 __h.release();
4585 return;
4586 }
4587 if (__first2 == __last2)
4588 {
4589 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004590 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004591 __h.release();
4592 return;
4593 }
4594 if (__comp(*__first2, *__first1))
4595 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004596 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004597 __d.__incr((value_type*)0);
4598 ++__first2;
4599 }
4600 else
4601 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004602 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004603 __d.__incr((value_type*)0);
4604 ++__first1;
4605 }
4606 }
4607}
4608
4609template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4610void
4611__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4612 _InputIterator2 __first2, _InputIterator2 __last2,
4613 _OutputIterator __result, _Compare __comp)
4614{
4615 for (; __first1 != __last1; ++__result)
4616 {
4617 if (__first2 == __last2)
4618 {
4619 for (; __first1 != __last1; ++__first1, ++__result)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004620 *__result = _VSTD::move(*__first1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004621 return;
4622 }
4623 if (__comp(*__first2, *__first1))
4624 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004625 *__result = _VSTD::move(*__first2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004626 ++__first2;
4627 }
4628 else
4629 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004630 *__result = _VSTD::move(*__first1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004631 ++__first1;
4632 }
4633 }
4634 for (; __first2 != __last2; ++__first2, ++__result)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004635 *__result = _VSTD::move(*__first2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004636}
4637
4638template <class _Compare, class _RandomAccessIterator>
4639void
4640__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4641 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4642 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4643
4644template <class _Compare, class _RandomAccessIterator>
4645void
4646__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4647 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4648 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4649{
4650 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4651 switch (__len)
4652 {
4653 case 0:
4654 return;
4655 case 1:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004656 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004657 return;
4658 case 2:
Marshall Clow32043ac2018-02-06 18:58:05 +00004659 __destruct_n __d(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004660 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
Marshall Clow32043ac2018-02-06 18:58:05 +00004661 if (__comp(*--__last1, *__first1))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004662 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004663 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004664 __d.__incr((value_type*)0);
4665 ++__first2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004666 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004667 }
4668 else
4669 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004670 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004671 __d.__incr((value_type*)0);
4672 ++__first2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004673 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004674 }
4675 __h2.release();
4676 return;
4677 }
4678 if (__len <= 8)
4679 {
4680 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4681 return;
4682 }
4683 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4684 _RandomAccessIterator __m = __first1 + __l2;
4685 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4686 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4687 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4688}
4689
4690template <class _Tp>
4691struct __stable_sort_switch
4692{
Howard Hinnanta9a897e2010-11-19 22:17:28 +00004693 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004694};
4695
4696template <class _Compare, class _RandomAccessIterator>
4697void
4698__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4699 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4700 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4701{
4702 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4703 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4704 switch (__len)
4705 {
4706 case 0:
4707 case 1:
4708 return;
4709 case 2:
4710 if (__comp(*--__last, *__first))
4711 swap(*__first, *__last);
4712 return;
4713 }
4714 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4715 {
4716 __insertion_sort<_Compare>(__first, __last, __comp);
4717 return;
4718 }
4719 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4720 _RandomAccessIterator __m = __first + __l2;
4721 if (__len <= __buff_size)
4722 {
4723 __destruct_n __d(0);
4724 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4725 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4726 __d.__set(__l2, (value_type*)0);
4727 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4728 __d.__set(__len, (value_type*)0);
4729 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4730// __merge<_Compare>(move_iterator<value_type*>(__buff),
4731// move_iterator<value_type*>(__buff + __l2),
4732// move_iterator<_RandomAccessIterator>(__buff + __l2),
4733// move_iterator<_RandomAccessIterator>(__buff + __len),
4734// __first, __comp);
4735 return;
4736 }
4737 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4738 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4739 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4740}
4741
4742template <class _RandomAccessIterator, class _Compare>
4743inline _LIBCPP_INLINE_VISIBILITY
4744void
4745stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4746{
4747 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4748 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4749 difference_type __len = __last - __first;
4750 pair<value_type*, ptrdiff_t> __buf(0, 0);
4751 unique_ptr<value_type, __return_temporary_buffer> __h;
4752 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4753 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004754 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004755 __h.reset(__buf.first);
4756 }
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00004757 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004758 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004759}
4760
4761template <class _RandomAccessIterator>
4762inline _LIBCPP_INLINE_VISIBILITY
4763void
4764stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4765{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004766 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004767}
4768
4769// is_heap_until
4770
4771template <class _RandomAccessIterator, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00004772_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00004773is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4774{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004775 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004776 difference_type __len = __last - __first;
4777 difference_type __p = 0;
4778 difference_type __c = 1;
4779 _RandomAccessIterator __pp = __first;
4780 while (__c < __len)
4781 {
4782 _RandomAccessIterator __cp = __first + __c;
4783 if (__comp(*__pp, *__cp))
4784 return __cp;
4785 ++__c;
4786 ++__cp;
4787 if (__c == __len)
4788 return __last;
4789 if (__comp(*__pp, *__cp))
4790 return __cp;
4791 ++__p;
4792 ++__pp;
4793 __c = 2 * __p + 1;
4794 }
4795 return __last;
4796}
4797
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004798template<class _RandomAccessIterator>
Nico Weber471b10a2019-04-03 18:13:08 +00004799_LIBCPP_NODISCARD_EXT inline
4800_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004801_RandomAccessIterator
4802is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4803{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004804 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004805}
4806
4807// is_heap
4808
4809template <class _RandomAccessIterator, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00004810_LIBCPP_NODISCARD_EXT inline
4811_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004812bool
4813is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4814{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004815 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004816}
4817
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004818template<class _RandomAccessIterator>
Nico Weber471b10a2019-04-03 18:13:08 +00004819_LIBCPP_NODISCARD_EXT inline
4820_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004821bool
4822is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4823{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004824 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004825}
4826
4827// push_heap
4828
4829template <class _Compare, class _RandomAccessIterator>
4830void
David Majnemer4468d562014-07-22 06:07:09 +00004831__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4832 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004833{
Howard Hinnantc51e1022010-05-11 19:42:16 +00004834 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4835 if (__len > 1)
4836 {
4837 __len = (__len - 2) / 2;
4838 _RandomAccessIterator __ptr = __first + __len;
4839 if (__comp(*__ptr, *--__last))
4840 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004841 value_type __t(_VSTD::move(*__last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004842 do
4843 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004844 *__last = _VSTD::move(*__ptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004845 __last = __ptr;
4846 if (__len == 0)
4847 break;
4848 __len = (__len - 1) / 2;
4849 __ptr = __first + __len;
4850 } while (__comp(*__ptr, __t));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004851 *__last = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004852 }
4853 }
4854}
4855
4856template <class _RandomAccessIterator, class _Compare>
4857inline _LIBCPP_INLINE_VISIBILITY
4858void
4859push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4860{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00004861 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
David Majnemer4468d562014-07-22 06:07:09 +00004862 __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004863}
4864
4865template <class _RandomAccessIterator>
4866inline _LIBCPP_INLINE_VISIBILITY
4867void
4868push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4869{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004870 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004871}
4872
4873// pop_heap
4874
4875template <class _Compare, class _RandomAccessIterator>
David Majnemer4468d562014-07-22 06:07:09 +00004876void
Eric Fiselier6003c772016-12-23 23:37:52 +00004877__sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
4878 _Compare __comp,
David Majnemer4468d562014-07-22 06:07:09 +00004879 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4880 _RandomAccessIterator __start)
4881{
4882 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4883 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4884 // left-child of __start is at 2 * __start + 1
4885 // right-child of __start is at 2 * __start + 2
4886 difference_type __child = __start - __first;
4887
4888 if (__len < 2 || (__len - 2) / 2 < __child)
4889 return;
4890
4891 __child = 2 * __child + 1;
4892 _RandomAccessIterator __child_i = __first + __child;
4893
4894 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4895 // right-child exists and is greater than left-child
4896 ++__child_i;
4897 ++__child;
4898 }
4899
4900 // check if we are in heap-order
4901 if (__comp(*__child_i, *__start))
4902 // we are, __start is larger than it's largest child
4903 return;
4904
4905 value_type __top(_VSTD::move(*__start));
4906 do
4907 {
4908 // we are not in heap-order, swap the parent with it's largest child
4909 *__start = _VSTD::move(*__child_i);
4910 __start = __child_i;
4911
4912 if ((__len - 2) / 2 < __child)
4913 break;
4914
4915 // recompute the child based off of the updated parent
4916 __child = 2 * __child + 1;
4917 __child_i = __first + __child;
4918
4919 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4920 // right-child exists and is greater than left-child
4921 ++__child_i;
4922 ++__child;
4923 }
4924
4925 // check if we are in heap-order
4926 } while (!__comp(*__child_i, __top));
4927 *__start = _VSTD::move(__top);
4928}
4929
4930template <class _Compare, class _RandomAccessIterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004931inline _LIBCPP_INLINE_VISIBILITY
4932void
4933__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4934 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4935{
4936 if (__len > 1)
4937 {
4938 swap(*__first, *--__last);
David Majnemer4468d562014-07-22 06:07:09 +00004939 __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004940 }
4941}
4942
4943template <class _RandomAccessIterator, class _Compare>
4944inline _LIBCPP_INLINE_VISIBILITY
4945void
4946pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4947{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00004948 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004949 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004950}
4951
4952template <class _RandomAccessIterator>
4953inline _LIBCPP_INLINE_VISIBILITY
4954void
4955pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4956{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004957 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004958}
4959
4960// make_heap
4961
4962template <class _Compare, class _RandomAccessIterator>
4963void
4964__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4965{
4966 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4967 difference_type __n = __last - __first;
4968 if (__n > 1)
4969 {
David Majnemer4468d562014-07-22 06:07:09 +00004970 // start from the first parent, there is no need to consider children
4971 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
4972 {
4973 __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
4974 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004975 }
4976}
4977
4978template <class _RandomAccessIterator, class _Compare>
4979inline _LIBCPP_INLINE_VISIBILITY
4980void
4981make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4982{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00004983 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004984 __make_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004985}
4986
4987template <class _RandomAccessIterator>
4988inline _LIBCPP_INLINE_VISIBILITY
4989void
4990make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4991{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004992 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004993}
4994
4995// sort_heap
4996
4997template <class _Compare, class _RandomAccessIterator>
4998void
4999__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5000{
5001 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5002 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
5003 __pop_heap<_Compare>(__first, __last, __comp, __n);
5004}
5005
5006template <class _RandomAccessIterator, class _Compare>
5007inline _LIBCPP_INLINE_VISIBILITY
5008void
5009sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5010{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005011 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005012 __sort_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005013}
5014
5015template <class _RandomAccessIterator>
5016inline _LIBCPP_INLINE_VISIBILITY
5017void
5018sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5019{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005020 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00005021}
5022
5023// partial_sort
5024
5025template <class _Compare, class _RandomAccessIterator>
5026void
5027__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5028 _Compare __comp)
5029{
5030 __make_heap<_Compare>(__first, __middle, __comp);
5031 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5032 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5033 {
5034 if (__comp(*__i, *__first))
5035 {
5036 swap(*__i, *__first);
David Majnemer4468d562014-07-22 06:07:09 +00005037 __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005038 }
5039 }
5040 __sort_heap<_Compare>(__first, __middle, __comp);
5041}
5042
5043template <class _RandomAccessIterator, class _Compare>
5044inline _LIBCPP_INLINE_VISIBILITY
5045void
5046partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5047 _Compare __comp)
5048{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005049 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005050 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005051}
5052
5053template <class _RandomAccessIterator>
5054inline _LIBCPP_INLINE_VISIBILITY
5055void
5056partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5057{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005058 _VSTD::partial_sort(__first, __middle, __last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005059 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5060}
5061
5062// partial_sort_copy
5063
5064template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5065_RandomAccessIterator
5066__partial_sort_copy(_InputIterator __first, _InputIterator __last,
5067 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5068{
5069 _RandomAccessIterator __r = __result_first;
5070 if (__r != __result_last)
5071 {
Eric Fiseliera09a3b42014-10-27 19:28:20 +00005072 for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005073 *__r = *__first;
5074 __make_heap<_Compare>(__result_first, __r, __comp);
David Majnemer4468d562014-07-22 06:07:09 +00005075 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005076 for (; __first != __last; ++__first)
5077 if (__comp(*__first, *__result_first))
5078 {
5079 *__result_first = *__first;
David Majnemer4468d562014-07-22 06:07:09 +00005080 __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005081 }
5082 __sort_heap<_Compare>(__result_first, __r, __comp);
5083 }
5084 return __r;
5085}
5086
5087template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5088inline _LIBCPP_INLINE_VISIBILITY
5089_RandomAccessIterator
5090partial_sort_copy(_InputIterator __first, _InputIterator __last,
5091 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5092{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005093 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005094 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005095}
5096
5097template <class _InputIterator, class _RandomAccessIterator>
5098inline _LIBCPP_INLINE_VISIBILITY
5099_RandomAccessIterator
5100partial_sort_copy(_InputIterator __first, _InputIterator __last,
5101 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5102{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005103 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005104 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5105}
5106
5107// nth_element
5108
5109template <class _Compare, class _RandomAccessIterator>
5110void
5111__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5112{
5113 // _Compare is known to be a reference type
5114 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5115 const difference_type __limit = 7;
5116 while (true)
5117 {
5118 __restart:
Howard Hinnant2fa038c2011-12-29 17:45:35 +00005119 if (__nth == __last)
5120 return;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005121 difference_type __len = __last - __first;
5122 switch (__len)
5123 {
5124 case 0:
5125 case 1:
5126 return;
5127 case 2:
5128 if (__comp(*--__last, *__first))
5129 swap(*__first, *__last);
5130 return;
5131 case 3:
5132 {
5133 _RandomAccessIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005134 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005135 return;
5136 }
5137 }
5138 if (__len <= __limit)
5139 {
5140 __selection_sort<_Compare>(__first, __last, __comp);
5141 return;
5142 }
5143 // __len > __limit >= 3
5144 _RandomAccessIterator __m = __first + __len/2;
5145 _RandomAccessIterator __lm1 = __last;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005146 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005147 // *__m is median
5148 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5149 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5150 _RandomAccessIterator __i = __first;
5151 _RandomAccessIterator __j = __lm1;
5152 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5153 // The search going up is known to be guarded but the search coming down isn't.
5154 // Prime the downward search with a guard.
5155 if (!__comp(*__i, *__m)) // if *__first == *__m
5156 {
5157 // *__first == *__m, *__first doesn't go in first part
5158 // manually guard downward moving __j against __i
5159 while (true)
5160 {
5161 if (__i == --__j)
5162 {
5163 // *__first == *__m, *__m <= all other elements
5164 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5165 ++__i; // __first + 1
5166 __j = __last;
5167 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5168 {
5169 while (true)
5170 {
5171 if (__i == __j)
5172 return; // [__first, __last) all equivalent elements
5173 if (__comp(*__first, *__i))
5174 {
5175 swap(*__i, *__j);
5176 ++__n_swaps;
5177 ++__i;
5178 break;
5179 }
5180 ++__i;
5181 }
5182 }
5183 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5184 if (__i == __j)
5185 return;
5186 while (true)
5187 {
5188 while (!__comp(*__first, *__i))
5189 ++__i;
5190 while (__comp(*__first, *--__j))
5191 ;
5192 if (__i >= __j)
5193 break;
5194 swap(*__i, *__j);
5195 ++__n_swaps;
5196 ++__i;
5197 }
5198 // [__first, __i) == *__first and *__first < [__i, __last)
5199 // The first part is sorted,
5200 if (__nth < __i)
5201 return;
5202 // __nth_element the secod part
5203 // __nth_element<_Compare>(__i, __nth, __last, __comp);
5204 __first = __i;
5205 goto __restart;
5206 }
5207 if (__comp(*__j, *__m))
5208 {
5209 swap(*__i, *__j);
5210 ++__n_swaps;
5211 break; // found guard for downward moving __j, now use unguarded partition
5212 }
5213 }
5214 }
5215 ++__i;
5216 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5217 // if not yet partitioned...
5218 if (__i < __j)
5219 {
5220 // known that *(__i - 1) < *__m
5221 while (true)
5222 {
5223 // __m still guards upward moving __i
5224 while (__comp(*__i, *__m))
5225 ++__i;
5226 // It is now known that a guard exists for downward moving __j
5227 while (!__comp(*--__j, *__m))
5228 ;
5229 if (__i >= __j)
5230 break;
5231 swap(*__i, *__j);
5232 ++__n_swaps;
5233 // It is known that __m != __j
5234 // If __m just moved, follow it
5235 if (__m == __i)
5236 __m = __j;
5237 ++__i;
5238 }
5239 }
5240 // [__first, __i) < *__m and *__m <= [__i, __last)
5241 if (__i != __m && __comp(*__m, *__i))
5242 {
5243 swap(*__i, *__m);
5244 ++__n_swaps;
5245 }
5246 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5247 if (__nth == __i)
5248 return;
5249 if (__n_swaps == 0)
5250 {
5251 // We were given a perfectly partitioned sequence. Coincidence?
5252 if (__nth < __i)
5253 {
5254 // Check for [__first, __i) already sorted
5255 __j = __m = __first;
5256 while (++__j != __i)
5257 {
5258 if (__comp(*__j, *__m))
5259 // not yet sorted, so sort
5260 goto not_sorted;
5261 __m = __j;
5262 }
5263 // [__first, __i) sorted
5264 return;
5265 }
5266 else
5267 {
5268 // Check for [__i, __last) already sorted
5269 __j = __m = __i;
5270 while (++__j != __last)
5271 {
5272 if (__comp(*__j, *__m))
5273 // not yet sorted, so sort
5274 goto not_sorted;
5275 __m = __j;
5276 }
5277 // [__i, __last) sorted
5278 return;
5279 }
5280 }
5281not_sorted:
5282 // __nth_element on range containing __nth
5283 if (__nth < __i)
5284 {
5285 // __nth_element<_Compare>(__first, __nth, __i, __comp);
5286 __last = __i;
5287 }
5288 else
5289 {
5290 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5291 __first = ++__i;
5292 }
5293 }
5294}
5295
5296template <class _RandomAccessIterator, class _Compare>
5297inline _LIBCPP_INLINE_VISIBILITY
5298void
5299nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5300{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005301 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005302 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005303}
5304
5305template <class _RandomAccessIterator>
5306inline _LIBCPP_INLINE_VISIBILITY
5307void
5308nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5309{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005310 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00005311}
5312
5313// includes
5314
5315template <class _Compare, class _InputIterator1, class _InputIterator2>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005316_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00005317__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5318 _Compare __comp)
5319{
5320 for (; __first2 != __last2; ++__first1)
5321 {
5322 if (__first1 == __last1 || __comp(*__first2, *__first1))
5323 return false;
5324 if (!__comp(*__first1, *__first2))
5325 ++__first2;
5326 }
5327 return true;
5328}
5329
5330template <class _InputIterator1, class _InputIterator2, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00005331_LIBCPP_NODISCARD_EXT inline
5332_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005333bool
5334includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5335 _Compare __comp)
5336{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005337 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005338 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005339}
5340
5341template <class _InputIterator1, class _InputIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00005342_LIBCPP_NODISCARD_EXT inline
5343_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005344bool
5345includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5346{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005347 return _VSTD::includes(__first1, __last1, __first2, __last2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005348 __less<typename iterator_traits<_InputIterator1>::value_type,
5349 typename iterator_traits<_InputIterator2>::value_type>());
5350}
5351
5352// set_union
5353
5354template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5355_OutputIterator
5356__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5357 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5358{
5359 for (; __first1 != __last1; ++__result)
5360 {
5361 if (__first2 == __last2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005362 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005363 if (__comp(*__first2, *__first1))
5364 {
5365 *__result = *__first2;
5366 ++__first2;
5367 }
5368 else
5369 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00005370 if (!__comp(*__first1, *__first2))
5371 ++__first2;
Marshall Clowb4687412017-10-30 15:50:00 +00005372 *__result = *__first1;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005373 ++__first1;
5374 }
5375 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005376 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005377}
5378
5379template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5380inline _LIBCPP_INLINE_VISIBILITY
5381_OutputIterator
5382set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5383 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5384{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005385 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005386 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005387}
5388
5389template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5390inline _LIBCPP_INLINE_VISIBILITY
5391_OutputIterator
5392set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5393 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5394{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005395 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005396 __less<typename iterator_traits<_InputIterator1>::value_type,
5397 typename iterator_traits<_InputIterator2>::value_type>());
5398}
5399
5400// set_intersection
5401
5402template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005403_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00005404__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5405 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5406{
5407 while (__first1 != __last1 && __first2 != __last2)
5408 {
5409 if (__comp(*__first1, *__first2))
5410 ++__first1;
5411 else
5412 {
5413 if (!__comp(*__first2, *__first1))
5414 {
5415 *__result = *__first1;
5416 ++__result;
5417 ++__first1;
5418 }
5419 ++__first2;
5420 }
5421 }
5422 return __result;
5423}
5424
5425template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005426inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005427_OutputIterator
5428set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5429 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5430{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005431 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005432 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005433}
5434
5435template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005436inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005437_OutputIterator
5438set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5439 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5440{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005441 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005442 __less<typename iterator_traits<_InputIterator1>::value_type,
5443 typename iterator_traits<_InputIterator2>::value_type>());
5444}
5445
5446// set_difference
5447
5448template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5449_OutputIterator
5450__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5451 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5452{
5453 while (__first1 != __last1)
5454 {
5455 if (__first2 == __last2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005456 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005457 if (__comp(*__first1, *__first2))
5458 {
5459 *__result = *__first1;
5460 ++__result;
5461 ++__first1;
5462 }
5463 else
5464 {
5465 if (!__comp(*__first2, *__first1))
5466 ++__first1;
5467 ++__first2;
5468 }
5469 }
5470 return __result;
5471}
5472
5473template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5474inline _LIBCPP_INLINE_VISIBILITY
5475_OutputIterator
5476set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5477 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5478{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005479 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005480 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005481}
5482
5483template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5484inline _LIBCPP_INLINE_VISIBILITY
5485_OutputIterator
5486set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5487 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5488{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005489 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005490 __less<typename iterator_traits<_InputIterator1>::value_type,
5491 typename iterator_traits<_InputIterator2>::value_type>());
5492}
5493
5494// set_symmetric_difference
5495
5496template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5497_OutputIterator
5498__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5499 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5500{
5501 while (__first1 != __last1)
5502 {
5503 if (__first2 == __last2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005504 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005505 if (__comp(*__first1, *__first2))
5506 {
5507 *__result = *__first1;
5508 ++__result;
5509 ++__first1;
5510 }
5511 else
5512 {
5513 if (__comp(*__first2, *__first1))
5514 {
5515 *__result = *__first2;
5516 ++__result;
5517 }
5518 else
5519 ++__first1;
5520 ++__first2;
5521 }
5522 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005523 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005524}
5525
5526template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5527inline _LIBCPP_INLINE_VISIBILITY
5528_OutputIterator
5529set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5530 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5531{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005532 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005533 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005534}
5535
5536template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5537inline _LIBCPP_INLINE_VISIBILITY
5538_OutputIterator
5539set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5540 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5541{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005542 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005543 __less<typename iterator_traits<_InputIterator1>::value_type,
5544 typename iterator_traits<_InputIterator2>::value_type>());
5545}
5546
5547// lexicographical_compare
5548
5549template <class _Compare, class _InputIterator1, class _InputIterator2>
Marshall Clow5492c8a2018-01-22 20:44:33 +00005550_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00005551__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5552 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5553{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00005554 for (; __first2 != __last2; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005555 {
5556 if (__first1 == __last1 || __comp(*__first1, *__first2))
5557 return true;
5558 if (__comp(*__first2, *__first1))
5559 return false;
5560 }
5561 return false;
5562}
5563
5564template <class _InputIterator1, class _InputIterator2, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00005565_LIBCPP_NODISCARD_EXT inline
5566_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005567bool
5568lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5569 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5570{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005571 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005572 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005573}
5574
5575template <class _InputIterator1, class _InputIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00005576_LIBCPP_NODISCARD_EXT inline
5577_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005578bool
5579lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5580 _InputIterator2 __first2, _InputIterator2 __last2)
5581{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005582 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005583 __less<typename iterator_traits<_InputIterator1>::value_type,
5584 typename iterator_traits<_InputIterator2>::value_type>());
5585}
5586
5587// next_permutation
5588
5589template <class _Compare, class _BidirectionalIterator>
5590bool
5591__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5592{
5593 _BidirectionalIterator __i = __last;
5594 if (__first == __last || __first == --__i)
5595 return false;
5596 while (true)
5597 {
5598 _BidirectionalIterator __ip1 = __i;
5599 if (__comp(*--__i, *__ip1))
5600 {
5601 _BidirectionalIterator __j = __last;
5602 while (!__comp(*__i, *--__j))
5603 ;
5604 swap(*__i, *__j);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005605 _VSTD::reverse(__ip1, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005606 return true;
5607 }
5608 if (__i == __first)
5609 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005610 _VSTD::reverse(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005611 return false;
5612 }
5613 }
5614}
5615
5616template <class _BidirectionalIterator, class _Compare>
5617inline _LIBCPP_INLINE_VISIBILITY
5618bool
5619next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5620{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005621 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005622 return __next_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005623}
5624
5625template <class _BidirectionalIterator>
5626inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005627bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00005628next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5629{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005630 return _VSTD::next_permutation(__first, __last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005631 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5632}
5633
5634// prev_permutation
5635
5636template <class _Compare, class _BidirectionalIterator>
5637bool
5638__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5639{
5640 _BidirectionalIterator __i = __last;
5641 if (__first == __last || __first == --__i)
5642 return false;
5643 while (true)
5644 {
5645 _BidirectionalIterator __ip1 = __i;
5646 if (__comp(*__ip1, *--__i))
5647 {
5648 _BidirectionalIterator __j = __last;
5649 while (!__comp(*--__j, *__i))
5650 ;
5651 swap(*__i, *__j);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005652 _VSTD::reverse(__ip1, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005653 return true;
5654 }
5655 if (__i == __first)
5656 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005657 _VSTD::reverse(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005658 return false;
5659 }
5660 }
5661}
5662
5663template <class _BidirectionalIterator, class _Compare>
5664inline _LIBCPP_INLINE_VISIBILITY
5665bool
5666prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5667{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005668 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005669 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005670}
5671
5672template <class _BidirectionalIterator>
5673inline _LIBCPP_INLINE_VISIBILITY
5674bool
5675prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5676{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005677 return _VSTD::prev_permutation(__first, __last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005678 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5679}
5680
Howard Hinnantc51e1022010-05-11 19:42:16 +00005681_LIBCPP_END_NAMESPACE_STD
5682
Eric Fiselierf4433a32017-05-31 22:07:49 +00005683_LIBCPP_POP_MACROS
5684
Howard Hinnantc51e1022010-05-11 19:42:16 +00005685#endif // _LIBCPP_ALGORITHM