blob: 22adcc8ed7db9c2e6b9e9c931cbb80eccd25d0c9 [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>
Eric Fiselier7ceab7b2019-04-12 05:18:19 +0000788 _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier6dfe5232019-03-08 22:58:59 +0000789 bool operator()(_Tp& __x, _Up& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000790 {
791 bool __r = __comp_(__x, __y);
792 if (__r)
Eric Fiselier32adec82016-07-19 23:27:18 +0000793 __do_compare_assert(0, __y, __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000794 return __r;
795 }
Eric Fiselier32adec82016-07-19 23:27:18 +0000796
797 template <class _LHS, class _RHS>
Eric Fiselier7ceab7b2019-04-12 05:18:19 +0000798 _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier32adec82016-07-19 23:27:18 +0000799 inline _LIBCPP_INLINE_VISIBILITY
800 decltype((void)_VSTD::declval<_Compare&>()(
Eric Fiselier6dfe5232019-03-08 22:58:59 +0000801 _VSTD::declval<_LHS &>(), _VSTD::declval<_RHS &>()))
802 __do_compare_assert(int, _LHS & __l, _RHS & __r) {
Eric Fiselier32adec82016-07-19 23:27:18 +0000803 _LIBCPP_ASSERT(!__comp_(__l, __r),
804 "Comparator does not induce a strict weak ordering");
805 }
806
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
Eric Fiselier6dfe5232019-03-08 22:58:59 +0000810 void __do_compare_assert(long, _LHS &, _RHS &) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000811};
812
Eric Fiselier6dfe5232019-03-08 22:58:59 +0000813#endif // _LIBCPP_DEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000814
Eric Fiselier7ceab7b2019-04-12 05:18:19 +0000815template <class _Comp>
816struct __comp_ref_type {
817 // Pass the comparator by lvalue reference. Or in debug mode, using a
818 // debugging wrapper that stores a reference.
819#ifndef _LIBCPP_DEBUG
820 typedef typename add_lvalue_reference<_Comp>::type type;
821#else
822 typedef __debug_less<_Comp> type;
823#endif
824};
825
Howard Hinnantc51e1022010-05-11 19:42:16 +0000826// all_of
827
828template <class _InputIterator, class _Predicate>
Nico Weber471b10a2019-04-03 18:13:08 +0000829_LIBCPP_NODISCARD_EXT inline
830_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000831bool
832all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
833{
834 for (; __first != __last; ++__first)
835 if (!__pred(*__first))
836 return false;
837 return true;
838}
839
840// any_of
841
842template <class _InputIterator, class _Predicate>
Nico Weber471b10a2019-04-03 18:13:08 +0000843_LIBCPP_NODISCARD_EXT inline
844_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000845bool
846any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
847{
848 for (; __first != __last; ++__first)
849 if (__pred(*__first))
850 return true;
851 return false;
852}
853
854// none_of
855
856template <class _InputIterator, class _Predicate>
Nico Weber471b10a2019-04-03 18:13:08 +0000857_LIBCPP_NODISCARD_EXT inline
858_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000859bool
860none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
861{
862 for (; __first != __last; ++__first)
863 if (__pred(*__first))
864 return false;
865 return true;
866}
867
868// for_each
869
870template <class _InputIterator, class _Function>
Marshall Clow5492c8a2018-01-22 20:44:33 +0000871inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872_Function
873for_each(_InputIterator __first, _InputIterator __last, _Function __f)
874{
875 for (; __first != __last; ++__first)
876 __f(*__first);
Marshall Clow78dbe462016-11-14 18:22:19 +0000877 return __f;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000878}
879
Marshall Clowc588fd62017-05-25 13:40:57 +0000880#if _LIBCPP_STD_VER > 14
Marshall Clowde0169c2017-05-25 02:29:54 +0000881// for_each_n
882
883template <class _InputIterator, class _Size, class _Function>
Marshall Clow5492c8a2018-01-22 20:44:33 +0000884inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowde0169c2017-05-25 02:29:54 +0000885_InputIterator
886for_each_n(_InputIterator __first, _Size __orig_n, _Function __f)
887{
888 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
889 _IntegralSize __n = __orig_n;
890 while (__n > 0)
891 {
892 __f(*__first);
893 ++__first;
894 --__n;
895 }
896 return __first;
897}
Marshall Clowc588fd62017-05-25 13:40:57 +0000898#endif
Marshall Clowde0169c2017-05-25 02:29:54 +0000899
Howard Hinnantc51e1022010-05-11 19:42:16 +0000900// find
901
902template <class _InputIterator, class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +0000903_LIBCPP_NODISCARD_EXT inline
904_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000905_InputIterator
Howard Hinnantbf074022011-10-22 20:59:45 +0000906find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000907{
908 for (; __first != __last; ++__first)
Howard Hinnantbf074022011-10-22 20:59:45 +0000909 if (*__first == __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910 break;
911 return __first;
912}
913
914// find_if
915
916template <class _InputIterator, class _Predicate>
Nico Weber471b10a2019-04-03 18:13:08 +0000917_LIBCPP_NODISCARD_EXT inline
918_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000919_InputIterator
920find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
921{
922 for (; __first != __last; ++__first)
923 if (__pred(*__first))
924 break;
925 return __first;
926}
927
928// find_if_not
929
930template<class _InputIterator, class _Predicate>
Nico Weber471b10a2019-04-03 18:13:08 +0000931_LIBCPP_NODISCARD_EXT inline
932_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000933_InputIterator
934find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
935{
936 for (; __first != __last; ++__first)
937 if (!__pred(*__first))
938 break;
939 return __first;
940}
941
942// find_end
943
944template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clowee0161e2018-01-15 19:26:05 +0000945_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
947 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
948 forward_iterator_tag, forward_iterator_tag)
949{
950 // modeled after search algorithm
951 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
952 if (__first2 == __last2)
953 return __r;
954 while (true)
955 {
956 while (true)
957 {
958 if (__first1 == __last1) // if source exhausted return last correct answer
959 return __r; // (or __last1 if never found)
960 if (__pred(*__first1, *__first2))
961 break;
962 ++__first1;
963 }
964 // *__first1 matches *__first2, now match elements after here
965 _ForwardIterator1 __m1 = __first1;
966 _ForwardIterator2 __m2 = __first2;
967 while (true)
968 {
969 if (++__m2 == __last2)
970 { // Pattern exhaused, record answer and search for another one
971 __r = __first1;
972 ++__first1;
973 break;
974 }
975 if (++__m1 == __last1) // Source exhausted, return last answer
976 return __r;
977 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
978 {
979 ++__first1;
980 break;
981 } // else there is a match, check next elements
982 }
983 }
984}
985
986template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
Marshall Clowee0161e2018-01-15 19:26:05 +0000987_LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator1
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
989 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
990 bidirectional_iterator_tag, bidirectional_iterator_tag)
991{
992 // modeled after search algorithm (in reverse)
993 if (__first2 == __last2)
994 return __last1; // Everything matches an empty sequence
995 _BidirectionalIterator1 __l1 = __last1;
996 _BidirectionalIterator2 __l2 = __last2;
997 --__l2;
998 while (true)
999 {
1000 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
1001 while (true)
1002 {
1003 if (__first1 == __l1) // return __last1 if no element matches *__first2
1004 return __last1;
1005 if (__pred(*--__l1, *__l2))
1006 break;
1007 }
1008 // *__l1 matches *__l2, now match elements before here
1009 _BidirectionalIterator1 __m1 = __l1;
1010 _BidirectionalIterator2 __m2 = __l2;
1011 while (true)
1012 {
1013 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
1014 return __m1;
1015 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
1016 return __last1;
1017 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
1018 {
1019 break;
1020 } // else there is a match, check next elements
1021 }
1022 }
1023}
1024
1025template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clowedfd25a2014-06-10 18:51:55 +00001026_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1028 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1029 random_access_iterator_tag, random_access_iterator_tag)
1030{
1031 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1032 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
1033 if (__len2 == 0)
1034 return __last1;
1035 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
1036 if (__len1 < __len2)
1037 return __last1;
1038 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
1039 _RandomAccessIterator1 __l1 = __last1;
1040 _RandomAccessIterator2 __l2 = __last2;
1041 --__l2;
1042 while (true)
1043 {
1044 while (true)
1045 {
1046 if (__s == __l1)
1047 return __last1;
1048 if (__pred(*--__l1, *__l2))
1049 break;
1050 }
1051 _RandomAccessIterator1 __m1 = __l1;
1052 _RandomAccessIterator2 __m2 = __l2;
1053 while (true)
1054 {
1055 if (__m2 == __first2)
1056 return __m1;
1057 // no need to check range on __m1 because __s guarantees we have enough source
1058 if (!__pred(*--__m1, *--__m2))
1059 {
1060 break;
1061 }
1062 }
1063 }
1064}
1065
1066template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001067_LIBCPP_NODISCARD_EXT inline
1068_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069_ForwardIterator1
1070find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1071 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1072{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001073 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074 (__first1, __last1, __first2, __last2, __pred,
1075 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1076 typename iterator_traits<_ForwardIterator2>::iterator_category());
1077}
1078
1079template <class _ForwardIterator1, class _ForwardIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00001080_LIBCPP_NODISCARD_EXT inline
1081_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082_ForwardIterator1
1083find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1084 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1085{
1086 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1087 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001088 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089}
1090
1091// find_first_of
1092
1093template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clowedfd25a2014-06-10 18:51:55 +00001094_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1095__find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001096 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1097{
1098 for (; __first1 != __last1; ++__first1)
1099 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1100 if (__pred(*__first1, *__j))
1101 return __first1;
1102 return __last1;
1103}
1104
Marshall Clowedfd25a2014-06-10 18:51:55 +00001105
1106template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001107_LIBCPP_NODISCARD_EXT inline
1108_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowedfd25a2014-06-10 18:51:55 +00001109_ForwardIterator1
1110find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1111 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1112{
1113 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1114}
1115
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116template <class _ForwardIterator1, class _ForwardIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00001117_LIBCPP_NODISCARD_EXT inline
1118_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119_ForwardIterator1
1120find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1121 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1122{
1123 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1124 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Marshall Clowedfd25a2014-06-10 18:51:55 +00001125 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126}
1127
1128// adjacent_find
1129
1130template <class _ForwardIterator, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001131_LIBCPP_NODISCARD_EXT inline
1132_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133_ForwardIterator
1134adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1135{
1136 if (__first != __last)
1137 {
1138 _ForwardIterator __i = __first;
1139 while (++__i != __last)
1140 {
1141 if (__pred(*__first, *__i))
1142 return __first;
1143 __first = __i;
1144 }
1145 }
1146 return __last;
1147}
1148
1149template <class _ForwardIterator>
Nico Weber471b10a2019-04-03 18:13:08 +00001150_LIBCPP_NODISCARD_EXT inline
1151_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152_ForwardIterator
1153adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1154{
1155 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001156 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157}
1158
1159// count
1160
1161template <class _InputIterator, class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00001162_LIBCPP_NODISCARD_EXT inline
1163_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164typename iterator_traits<_InputIterator>::difference_type
Howard Hinnantbf074022011-10-22 20:59:45 +00001165count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166{
1167 typename iterator_traits<_InputIterator>::difference_type __r(0);
1168 for (; __first != __last; ++__first)
Howard Hinnantbf074022011-10-22 20:59:45 +00001169 if (*__first == __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170 ++__r;
1171 return __r;
1172}
1173
1174// count_if
1175
1176template <class _InputIterator, class _Predicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001177_LIBCPP_NODISCARD_EXT inline
1178_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179typename iterator_traits<_InputIterator>::difference_type
1180count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1181{
1182 typename iterator_traits<_InputIterator>::difference_type __r(0);
1183 for (; __first != __last; ++__first)
1184 if (__pred(*__first))
1185 ++__r;
1186 return __r;
1187}
1188
1189// mismatch
1190
1191template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001192_LIBCPP_NODISCARD_EXT inline
1193_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194pair<_InputIterator1, _InputIterator2>
1195mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1196 _InputIterator2 __first2, _BinaryPredicate __pred)
1197{
Marshall Clow2932e512014-09-16 20:40:05 +00001198 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199 if (!__pred(*__first1, *__first2))
1200 break;
1201 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1202}
1203
1204template <class _InputIterator1, class _InputIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00001205_LIBCPP_NODISCARD_EXT inline
1206_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001207pair<_InputIterator1, _InputIterator2>
1208mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1209{
1210 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1211 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001212 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213}
1214
Marshall Clow96b42b22013-05-09 21:14:23 +00001215#if _LIBCPP_STD_VER > 11
1216template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001217_LIBCPP_NODISCARD_EXT inline
1218_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001219pair<_InputIterator1, _InputIterator2>
1220mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1221 _InputIterator2 __first2, _InputIterator2 __last2,
1222 _BinaryPredicate __pred)
1223{
Marshall Clow2932e512014-09-16 20:40:05 +00001224 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow96b42b22013-05-09 21:14:23 +00001225 if (!__pred(*__first1, *__first2))
1226 break;
1227 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1228}
1229
1230template <class _InputIterator1, class _InputIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00001231_LIBCPP_NODISCARD_EXT inline
1232_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001233pair<_InputIterator1, _InputIterator2>
1234mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1235 _InputIterator2 __first2, _InputIterator2 __last2)
1236{
1237 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1238 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1239 return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1240}
1241#endif
1242
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243// equal
1244
1245template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001246_LIBCPP_NODISCARD_EXT inline
1247_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248bool
1249equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1250{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001251 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252 if (!__pred(*__first1, *__first2))
1253 return false;
1254 return true;
1255}
1256
1257template <class _InputIterator1, class _InputIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00001258_LIBCPP_NODISCARD_EXT inline
1259_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260bool
1261equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1262{
1263 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1264 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001265 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001266}
1267
Marshall Clow96b42b22013-05-09 21:14:23 +00001268#if _LIBCPP_STD_VER > 11
1269template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001270inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001271bool
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001272__equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow96b42b22013-05-09 21:14:23 +00001273 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1274 input_iterator_tag, input_iterator_tag )
1275{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001276 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow96b42b22013-05-09 21:14:23 +00001277 if (!__pred(*__first1, *__first2))
1278 return false;
1279 return __first1 == __last1 && __first2 == __last2;
1280}
1281
1282template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow30bf3022018-01-16 02:04:10 +00001283inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001284bool
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001285__equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1286 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
Marshall Clow96b42b22013-05-09 21:14:23 +00001287 random_access_iterator_tag, random_access_iterator_tag )
1288{
1289 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1290 return false;
1291 return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1292 typename add_lvalue_reference<_BinaryPredicate>::type>
1293 (__first1, __last1, __first2, __pred );
1294}
1295
1296template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001297_LIBCPP_NODISCARD_EXT inline
1298_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001299bool
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001300equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow96b42b22013-05-09 21:14:23 +00001301 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1302{
1303 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001304 (__first1, __last1, __first2, __last2, __pred,
Marshall Clow96b42b22013-05-09 21:14:23 +00001305 typename iterator_traits<_InputIterator1>::iterator_category(),
1306 typename iterator_traits<_InputIterator2>::iterator_category());
1307}
1308
1309template <class _InputIterator1, class _InputIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00001310_LIBCPP_NODISCARD_EXT inline
1311_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001312bool
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001313equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow96b42b22013-05-09 21:14:23 +00001314 _InputIterator2 __first2, _InputIterator2 __last2)
1315{
1316 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1317 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1318 return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1319 typename iterator_traits<_InputIterator1>::iterator_category(),
1320 typename iterator_traits<_InputIterator2>::iterator_category());
1321}
1322#endif
1323
Howard Hinnantc51e1022010-05-11 19:42:16 +00001324// is_permutation
1325
1326template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001327_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00001328is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1329 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1330{
Marshall Clow96d050a2018-01-15 16:16:32 +00001331// shorten sequences as much as possible by lopping of any equal prefix
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001332 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001333 if (!__pred(*__first1, *__first2))
Marshall Clow96d050a2018-01-15 16:16:32 +00001334 break;
1335 if (__first1 == __last1)
1336 return true;
1337
1338// __first1 != __last1 && *__first1 != *__first2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001339 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001340 _D1 __l1 = _VSTD::distance(__first1, __last1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001341 if (__l1 == _D1(1))
1342 return false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001343 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001344 // For each element in [f1, l1) see if there are the same number of
1345 // equal elements in [f2, l2)
1346 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1347 {
Marshall Clow96d050a2018-01-15 16:16:32 +00001348 // Have we already counted the number of *__i in [f1, l1)?
Peter Collingbournea5b451b2018-01-26 21:23:27 +00001349 _ForwardIterator1 __match = __first1;
1350 for (; __match != __i; ++__match)
1351 if (__pred(*__match, *__i))
1352 break;
1353 if (__match == __i) {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001354 // Count number of *__i in [f2, l2)
1355 _D1 __c2 = 0;
1356 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1357 if (__pred(*__i, *__j))
1358 ++__c2;
1359 if (__c2 == 0)
1360 return false;
1361 // Count number of *__i in [__i, l1) (we can start with 1)
1362 _D1 __c1 = 1;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001363 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001364 if (__pred(*__i, *__j))
1365 ++__c1;
1366 if (__c1 != __c2)
1367 return false;
1368 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001369 }
1370 return true;
1371}
1372
1373template<class _ForwardIterator1, class _ForwardIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00001374_LIBCPP_NODISCARD_EXT inline
1375_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001376bool
1377is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1378 _ForwardIterator2 __first2)
1379{
1380 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1381 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001382 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001383}
1384
Marshall Clow96b42b22013-05-09 21:14:23 +00001385#if _LIBCPP_STD_VER > 11
1386template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow96d050a2018-01-15 16:16:32 +00001387_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Marshall Clow96b42b22013-05-09 21:14:23 +00001388__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001389 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
Marshall Clow96b42b22013-05-09 21:14:23 +00001390 _BinaryPredicate __pred,
1391 forward_iterator_tag, forward_iterator_tag )
1392{
Marshall Clow96d050a2018-01-15 16:16:32 +00001393// shorten sequences as much as possible by lopping of any equal prefix
Eric Fiselier14673892014-10-27 20:26:25 +00001394 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow96b42b22013-05-09 21:14:23 +00001395 if (!__pred(*__first1, *__first2))
Marshall Clow96d050a2018-01-15 16:16:32 +00001396 break;
1397 if (__first1 == __last1)
Marshall Clow01bbbd22018-01-19 18:07:29 +00001398 return __first2 == __last2;
Marshall Clow96d050a2018-01-15 16:16:32 +00001399 else if (__first2 == __last2)
Marshall Clow01bbbd22018-01-19 18:07:29 +00001400 return false;
Marshall Clow96d050a2018-01-15 16:16:32 +00001401
Marshall Clow96b42b22013-05-09 21:14:23 +00001402 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1403 _D1 __l1 = _VSTD::distance(__first1, __last1);
1404
1405 typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
Marshall Clow42b967c2013-05-10 00:16:10 +00001406 _D2 __l2 = _VSTD::distance(__first2, __last2);
Marshall Clow96b42b22013-05-09 21:14:23 +00001407 if (__l1 != __l2)
1408 return false;
1409
1410 // For each element in [f1, l1) see if there are the same number of
1411 // equal elements in [f2, l2)
1412 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1413 {
Marshall Clow96d050a2018-01-15 16:16:32 +00001414 // Have we already counted the number of *__i in [f1, l1)?
Peter Collingbournea5b451b2018-01-26 21:23:27 +00001415 _ForwardIterator1 __match = __first1;
1416 for (; __match != __i; ++__match)
1417 if (__pred(*__match, *__i))
1418 break;
1419 if (__match == __i) {
Marshall Clow96b42b22013-05-09 21:14:23 +00001420 // Count number of *__i in [f2, l2)
1421 _D1 __c2 = 0;
1422 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1423 if (__pred(*__i, *__j))
1424 ++__c2;
1425 if (__c2 == 0)
1426 return false;
1427 // Count number of *__i in [__i, l1) (we can start with 1)
1428 _D1 __c1 = 1;
1429 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1430 if (__pred(*__i, *__j))
1431 ++__c1;
1432 if (__c1 != __c2)
1433 return false;
1434 }
Marshall Clow96b42b22013-05-09 21:14:23 +00001435 }
1436 return true;
1437}
1438
1439template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow96d050a2018-01-15 16:16:32 +00001440_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Marshall Clow96b42b22013-05-09 21:14:23 +00001441__is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
Aditya Kumar3a0179a2016-08-25 11:52:38 +00001442 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
Marshall Clow96b42b22013-05-09 21:14:23 +00001443 _BinaryPredicate __pred,
1444 random_access_iterator_tag, random_access_iterator_tag )
1445{
1446 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1447 return false;
1448 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1449 typename add_lvalue_reference<_BinaryPredicate>::type>
1450 (__first1, __last1, __first2, __pred );
1451}
1452
1453template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001454_LIBCPP_NODISCARD_EXT inline
1455_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001456bool
1457is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1458 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1459 _BinaryPredicate __pred )
1460{
1461 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1462 (__first1, __last1, __first2, __last2, __pred,
1463 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1464 typename iterator_traits<_ForwardIterator2>::iterator_category());
1465}
1466
1467template<class _ForwardIterator1, class _ForwardIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00001468_LIBCPP_NODISCARD_EXT inline
1469_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow96b42b22013-05-09 21:14:23 +00001470bool
1471is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1472 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1473{
1474 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1475 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1476 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1477 __equal_to<__v1, __v2>(),
1478 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1479 typename iterator_traits<_ForwardIterator2>::iterator_category());
1480}
1481#endif
1482
Howard Hinnantc51e1022010-05-11 19:42:16 +00001483// search
Marshall Clowa40686b2018-01-08 19:18:00 +00001484// __search is in <functional>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001485
1486template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001487_LIBCPP_NODISCARD_EXT inline
1488_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001489_ForwardIterator1
1490search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1491 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1492{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001493 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001494 (__first1, __last1, __first2, __last2, __pred,
Marshall Clowaf8be6c2016-03-08 15:12:52 +00001495 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1496 typename iterator_traits<_ForwardIterator2>::iterator_category())
1497 .first;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001498}
1499
1500template <class _ForwardIterator1, class _ForwardIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00001501_LIBCPP_NODISCARD_EXT inline
1502_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001503_ForwardIterator1
1504search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1505 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1506{
Marshall Clowaf8be6c2016-03-08 15:12:52 +00001507 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1508 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001509 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001510}
1511
Marshall Clowa40686b2018-01-08 19:18:00 +00001512
1513#if _LIBCPP_STD_VER > 14
1514template <class _ForwardIterator, class _Searcher>
Nico Weber471b10a2019-04-03 18:13:08 +00001515_LIBCPP_NODISCARD_EXT _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowa40686b2018-01-08 19:18:00 +00001516_ForwardIterator search(_ForwardIterator __f, _ForwardIterator __l, const _Searcher &__s)
1517{ return __s(__f, __l).first; }
1518#endif
1519
Howard Hinnantc51e1022010-05-11 19:42:16 +00001520// search_n
1521
1522template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001523_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001524__search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnantbf074022011-10-22 20:59:45 +00001525 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001526{
1527 if (__count <= 0)
1528 return __first;
1529 while (true)
1530 {
Howard Hinnantbf074022011-10-22 20:59:45 +00001531 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532 while (true)
1533 {
Howard Hinnantbf074022011-10-22 20:59:45 +00001534 if (__first == __last) // return __last if no element matches __value_
Howard Hinnantc51e1022010-05-11 19:42:16 +00001535 return __last;
Howard Hinnantbf074022011-10-22 20:59:45 +00001536 if (__pred(*__first, __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001537 break;
1538 ++__first;
1539 }
Howard Hinnantbf074022011-10-22 20:59:45 +00001540 // *__first matches __value_, now match elements after here
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541 _ForwardIterator __m = __first;
1542 _Size __c(0);
1543 while (true)
1544 {
1545 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1546 return __first;
1547 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1548 return __last;
Howard Hinnantbf074022011-10-22 20:59:45 +00001549 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550 {
1551 __first = __m;
1552 ++__first;
1553 break;
1554 } // else there is a match, check next elements
1555 }
1556 }
1557}
1558
1559template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
Marshall Clow323fc5b2018-01-16 15:48:27 +00001560_LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnantbf074022011-10-22 20:59:45 +00001562 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563{
1564 if (__count <= 0)
1565 return __first;
1566 _Size __len = static_cast<_Size>(__last - __first);
1567 if (__len < __count)
1568 return __last;
1569 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1570 while (true)
1571 {
Howard Hinnantbf074022011-10-22 20:59:45 +00001572 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 while (true)
1574 {
Howard Hinnantbede4c32013-04-04 15:40:48 +00001575 if (__first >= __s) // return __last if no element matches __value_
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576 return __last;
Howard Hinnantbf074022011-10-22 20:59:45 +00001577 if (__pred(*__first, __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578 break;
1579 ++__first;
1580 }
Howard Hinnantbf074022011-10-22 20:59:45 +00001581 // *__first matches __value_, now match elements after here
Howard Hinnantc51e1022010-05-11 19:42:16 +00001582 _RandomAccessIterator __m = __first;
1583 _Size __c(0);
1584 while (true)
1585 {
1586 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1587 return __first;
1588 ++__m; // no need to check range on __m because __s guarantees we have enough source
Howard Hinnantbf074022011-10-22 20:59:45 +00001589 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnantc51e1022010-05-11 19:42:16 +00001590 {
1591 __first = __m;
1592 ++__first;
1593 break;
1594 } // else there is a match, check next elements
1595 }
1596 }
1597}
1598
1599template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00001600_LIBCPP_NODISCARD_EXT inline
1601_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602_ForwardIterator
1603search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnantbf074022011-10-22 20:59:45 +00001604 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001605{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001606 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001607 (__first, __last, __convert_to_integral(__count), __value_, __pred,
1608 typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001609}
1610
1611template <class _ForwardIterator, class _Size, class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00001612_LIBCPP_NODISCARD_EXT inline
1613_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001614_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00001615search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616{
1617 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001618 return _VSTD::search_n(__first, __last, __convert_to_integral(__count),
1619 __value_, __equal_to<__v, _Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001620}
1621
1622// copy
Howard Hinnantc51e1022010-05-11 19:42:16 +00001623template <class _Iter>
1624inline _LIBCPP_INLINE_VISIBILITY
1625_Iter
1626__unwrap_iter(_Iter __i)
1627{
1628 return __i;
1629}
1630
1631template <class _Tp>
Marshall Clow88880de2017-05-25 14:20:26 +00001632inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001633typename enable_if
1634<
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001635 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001636 _Tp*
1637>::type
1638__unwrap_iter(move_iterator<_Tp*> __i)
1639{
1640 return __i.base();
1641}
1642
Howard Hinnant8ea98242013-08-23 17:37:05 +00001643#if _LIBCPP_DEBUG_LEVEL < 2
1644
Howard Hinnantc51e1022010-05-11 19:42:16 +00001645template <class _Tp>
Marshall Clowae4f8312018-07-13 16:35:26 +00001646inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001647typename enable_if
1648<
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001649 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001650 _Tp*
1651>::type
1652__unwrap_iter(__wrap_iter<_Tp*> __i)
1653{
1654 return __i.base();
1655}
1656
Marshall Clowbae96ac2019-02-06 16:10:25 +00001657template <class _Tp>
1658inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1659typename enable_if
1660<
1661 is_trivially_copy_assignable<_Tp>::value,
1662 const _Tp*
1663>::type
1664__unwrap_iter(__wrap_iter<const _Tp*> __i)
1665{
1666 return __i.base();
1667}
1668
Eric Fiselier38badb82016-12-28 05:35:32 +00001669#else
1670
1671template <class _Tp>
Marshall Clowae4f8312018-07-13 16:35:26 +00001672inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Eric Fiselier38badb82016-12-28 05:35:32 +00001673typename enable_if
1674<
1675 is_trivially_copy_assignable<_Tp>::value,
1676 __wrap_iter<_Tp*>
1677>::type
1678__unwrap_iter(__wrap_iter<_Tp*> __i)
1679{
1680 return __i;
1681}
1682
Howard Hinnant8ea98242013-08-23 17:37:05 +00001683#endif // _LIBCPP_DEBUG_LEVEL < 2
1684
Howard Hinnantc51e1022010-05-11 19:42:16 +00001685template <class _InputIterator, class _OutputIterator>
1686inline _LIBCPP_INLINE_VISIBILITY
1687_OutputIterator
1688__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1689{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001690 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001691 *__result = *__first;
1692 return __result;
1693}
1694
1695template <class _Tp, class _Up>
1696inline _LIBCPP_INLINE_VISIBILITY
1697typename enable_if
1698<
1699 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001700 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001701 _Up*
1702>::type
1703__copy(_Tp* __first, _Tp* __last, _Up* __result)
1704{
1705 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clowc3ef9622015-06-02 13:52:16 +00001706 if (__n > 0)
1707 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708 return __result + __n;
1709}
1710
1711template <class _InputIterator, class _OutputIterator>
1712inline _LIBCPP_INLINE_VISIBILITY
1713_OutputIterator
1714copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1715{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001716 return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717}
1718
1719// copy_backward
1720
Howard Hinnant7f229bc2013-02-06 21:03:39 +00001721template <class _BidirectionalIterator, class _OutputIterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722inline _LIBCPP_INLINE_VISIBILITY
1723_OutputIterator
Howard Hinnant7f229bc2013-02-06 21:03:39 +00001724__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001725{
1726 while (__first != __last)
1727 *--__result = *--__last;
1728 return __result;
1729}
1730
1731template <class _Tp, class _Up>
1732inline _LIBCPP_INLINE_VISIBILITY
1733typename enable_if
1734<
1735 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001736 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001737 _Up*
1738>::type
1739__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1740{
1741 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clowc3ef9622015-06-02 13:52:16 +00001742 if (__n > 0)
1743 {
1744 __result -= __n;
1745 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1746 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001747 return __result;
1748}
1749
1750template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1751inline _LIBCPP_INLINE_VISIBILITY
1752_BidirectionalIterator2
1753copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1754 _BidirectionalIterator2 __result)
1755{
Eric Fiselier6003c772016-12-23 23:37:52 +00001756 return _VSTD::__copy_backward(__unwrap_iter(__first),
1757 __unwrap_iter(__last),
1758 __unwrap_iter(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001759}
1760
1761// copy_if
1762
1763template<class _InputIterator, class _OutputIterator, class _Predicate>
1764inline _LIBCPP_INLINE_VISIBILITY
1765_OutputIterator
1766copy_if(_InputIterator __first, _InputIterator __last,
1767 _OutputIterator __result, _Predicate __pred)
1768{
1769 for (; __first != __last; ++__first)
1770 {
1771 if (__pred(*__first))
1772 {
1773 *__result = *__first;
1774 ++__result;
1775 }
1776 }
1777 return __result;
1778}
1779
1780// copy_n
1781
1782template<class _InputIterator, class _Size, class _OutputIterator>
1783inline _LIBCPP_INLINE_VISIBILITY
1784typename enable_if
1785<
1786 __is_input_iterator<_InputIterator>::value &&
1787 !__is_random_access_iterator<_InputIterator>::value,
1788 _OutputIterator
1789>::type
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001790copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001791{
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001792 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1793 _IntegralSize __n = __orig_n;
Howard Hinnantcbc5dc02011-02-27 20:55:39 +00001794 if (__n > 0)
1795 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796 *__result = *__first;
Howard Hinnantcbc5dc02011-02-27 20:55:39 +00001797 ++__result;
1798 for (--__n; __n > 0; --__n)
1799 {
1800 ++__first;
1801 *__result = *__first;
1802 ++__result;
1803 }
1804 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001805 return __result;
1806}
1807
1808template<class _InputIterator, class _Size, class _OutputIterator>
1809inline _LIBCPP_INLINE_VISIBILITY
1810typename enable_if
1811<
1812 __is_random_access_iterator<_InputIterator>::value,
1813 _OutputIterator
1814>::type
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001815copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001816{
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001817 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
1818 _IntegralSize __n = __orig_n;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001819 return _VSTD::copy(__first, __first + __n, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820}
1821
1822// move
1823
1824template <class _InputIterator, class _OutputIterator>
1825inline _LIBCPP_INLINE_VISIBILITY
1826_OutputIterator
1827__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1828{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001829 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001830 *__result = _VSTD::move(*__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001831 return __result;
1832}
1833
1834template <class _Tp, class _Up>
1835inline _LIBCPP_INLINE_VISIBILITY
1836typename enable_if
1837<
1838 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001839 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001840 _Up*
1841>::type
1842__move(_Tp* __first, _Tp* __last, _Up* __result)
1843{
1844 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clowc3ef9622015-06-02 13:52:16 +00001845 if (__n > 0)
1846 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001847 return __result + __n;
1848}
1849
1850template <class _InputIterator, class _OutputIterator>
1851inline _LIBCPP_INLINE_VISIBILITY
1852_OutputIterator
1853move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1854{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001855 return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001856}
1857
1858// move_backward
1859
1860template <class _InputIterator, class _OutputIterator>
1861inline _LIBCPP_INLINE_VISIBILITY
1862_OutputIterator
1863__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1864{
1865 while (__first != __last)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001866 *--__result = _VSTD::move(*--__last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001867 return __result;
1868}
1869
1870template <class _Tp, class _Up>
1871inline _LIBCPP_INLINE_VISIBILITY
1872typename enable_if
1873<
1874 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnanta9a897e2010-11-19 22:17:28 +00001875 is_trivially_copy_assignable<_Up>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876 _Up*
1877>::type
1878__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1879{
1880 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clowc3ef9622015-06-02 13:52:16 +00001881 if (__n > 0)
1882 {
1883 __result -= __n;
1884 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1885 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886 return __result;
1887}
1888
1889template <class _BidirectionalIterator1, class _BidirectionalIterator2>
1890inline _LIBCPP_INLINE_VISIBILITY
1891_BidirectionalIterator2
1892move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1893 _BidirectionalIterator2 __result)
1894{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001895 return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896}
1897
1898// iter_swap
1899
Howard Hinnantdbfd4b42011-05-27 15:04:19 +00001900// moved to <type_traits> for better swap / noexcept support
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901
1902// transform
1903
1904template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
Marshall Clow31427c62018-01-19 17:45:39 +00001905inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001906_OutputIterator
1907transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1908{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001909 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001910 *__result = __op(*__first);
1911 return __result;
1912}
1913
1914template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
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(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1918 _OutputIterator __result, _BinaryOperation __binary_op)
1919{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001920 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001921 *__result = __binary_op(*__first1, *__first2);
1922 return __result;
1923}
1924
1925// replace
1926
1927template <class _ForwardIterator, class _Tp>
Marshall Clow01bbbd22018-01-19 18:07:29 +00001928inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929void
1930replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1931{
1932 for (; __first != __last; ++__first)
1933 if (*__first == __old_value)
1934 *__first = __new_value;
1935}
1936
1937// replace_if
1938
1939template <class _ForwardIterator, class _Predicate, class _Tp>
Marshall Clow01bbbd22018-01-19 18:07:29 +00001940inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001941void
1942replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1943{
1944 for (; __first != __last; ++__first)
1945 if (__pred(*__first))
1946 *__first = __new_value;
1947}
1948
1949// replace_copy
1950
1951template <class _InputIterator, class _OutputIterator, class _Tp>
Marshall Clow01bbbd22018-01-19 18:07:29 +00001952inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001953_OutputIterator
1954replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1955 const _Tp& __old_value, const _Tp& __new_value)
1956{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001957 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001958 if (*__first == __old_value)
1959 *__result = __new_value;
1960 else
1961 *__result = *__first;
1962 return __result;
1963}
1964
1965// replace_copy_if
1966
1967template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
Marshall Clow01bbbd22018-01-19 18:07:29 +00001968inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001969_OutputIterator
1970replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
1971 _Predicate __pred, const _Tp& __new_value)
1972{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001973 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001974 if (__pred(*__first))
1975 *__result = __new_value;
1976 else
1977 *__result = *__first;
1978 return __result;
1979}
1980
1981// fill_n
1982
1983template <class _OutputIterator, class _Size, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00001984inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001985_OutputIterator
Howard Hinnant0ad1c122013-08-01 17:29:28 +00001986__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00001988 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnantbf074022011-10-22 20:59:45 +00001989 *__first = __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990 return __first;
1991}
1992
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993template <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 Hinnantbf074022011-10-22 20:59:45 +00001996fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001997{
Eric Fiselier97ec07d2015-02-10 16:46:42 +00001998 return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999}
2000
2001// fill
2002
2003template <class _ForwardIterator, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002004inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002005void
Howard Hinnantbf074022011-10-22 20:59:45 +00002006__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002007{
2008 for (; __first != __last; ++__first)
Howard Hinnantbf074022011-10-22 20:59:45 +00002009 *__first = __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002010}
2011
2012template <class _RandomAccessIterator, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002013inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002014void
Howard Hinnantbf074022011-10-22 20:59:45 +00002015__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002016{
Howard Hinnantbf074022011-10-22 20:59:45 +00002017 _VSTD::fill_n(__first, __last - __first, __value_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002018}
2019
2020template <class _ForwardIterator, class _Tp>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002021inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002022void
Howard Hinnantbf074022011-10-22 20:59:45 +00002023fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002024{
Howard Hinnantbf074022011-10-22 20:59:45 +00002025 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002026}
2027
2028// generate
2029
2030template <class _ForwardIterator, class _Generator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002031inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002032void
2033generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2034{
2035 for (; __first != __last; ++__first)
2036 *__first = __gen();
2037}
2038
2039// generate_n
2040
2041template <class _OutputIterator, class _Size, class _Generator>
Nico Weber471b10a2019-04-03 18:13:08 +00002042inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002043_OutputIterator
Eric Fiselier97ec07d2015-02-10 16:46:42 +00002044generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002045{
Eric Fiselier97ec07d2015-02-10 16:46:42 +00002046 typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize;
2047 _IntegralSize __n = __orig_n;
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002048 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002049 *__first = __gen();
2050 return __first;
2051}
2052
2053// remove
2054
2055template <class _ForwardIterator, class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00002056_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00002057remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002058{
Howard Hinnantbf074022011-10-22 20:59:45 +00002059 __first = _VSTD::find(__first, __last, __value_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002060 if (__first != __last)
2061 {
2062 _ForwardIterator __i = __first;
2063 while (++__i != __last)
2064 {
Howard Hinnantbf074022011-10-22 20:59:45 +00002065 if (!(*__i == __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002066 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002067 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002068 ++__first;
2069 }
2070 }
2071 }
2072 return __first;
2073}
2074
2075// remove_if
2076
2077template <class _ForwardIterator, class _Predicate>
Nico Weber471b10a2019-04-03 18:13:08 +00002078_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002079remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2080{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002081 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002082 (__first, __last, __pred);
2083 if (__first != __last)
2084 {
2085 _ForwardIterator __i = __first;
2086 while (++__i != __last)
2087 {
2088 if (!__pred(*__i))
2089 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002090 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002091 ++__first;
2092 }
2093 }
2094 }
2095 return __first;
2096}
2097
2098// remove_copy
2099
2100template <class _InputIterator, class _OutputIterator, class _Tp>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00002101inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002102_OutputIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00002103remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002104{
2105 for (; __first != __last; ++__first)
2106 {
Howard Hinnantbf074022011-10-22 20:59:45 +00002107 if (!(*__first == __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002108 {
2109 *__result = *__first;
2110 ++__result;
2111 }
2112 }
2113 return __result;
2114}
2115
2116// remove_copy_if
2117
2118template <class _InputIterator, class _OutputIterator, class _Predicate>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00002119inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002120_OutputIterator
2121remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2122{
2123 for (; __first != __last; ++__first)
2124 {
2125 if (!__pred(*__first))
2126 {
2127 *__result = *__first;
2128 ++__result;
2129 }
2130 }
2131 return __result;
2132}
2133
2134// unique
2135
2136template <class _ForwardIterator, class _BinaryPredicate>
Nico Weber471b10a2019-04-03 18:13:08 +00002137_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002138unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2139{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002140 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002141 (__first, __last, __pred);
2142 if (__first != __last)
2143 {
2144 // ... a a ? ...
2145 // f i
2146 _ForwardIterator __i = __first;
2147 for (++__i; ++__i != __last;)
2148 if (!__pred(*__first, *__i))
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002149 *++__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150 ++__first;
2151 }
2152 return __first;
2153}
2154
2155template <class _ForwardIterator>
Nico Weber471b10a2019-04-03 18:13:08 +00002156_LIBCPP_NODISCARD_EXT inline
2157_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002158_ForwardIterator
2159unique(_ForwardIterator __first, _ForwardIterator __last)
2160{
2161 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002162 return _VSTD::unique(__first, __last, __equal_to<__v>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002163}
2164
2165// unique_copy
2166
2167template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002168_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002169__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2170 input_iterator_tag, output_iterator_tag)
2171{
2172 if (__first != __last)
2173 {
2174 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2175 *__result = __t;
2176 ++__result;
2177 while (++__first != __last)
2178 {
2179 if (!__pred(__t, *__first))
2180 {
2181 __t = *__first;
2182 *__result = __t;
2183 ++__result;
2184 }
2185 }
2186 }
2187 return __result;
2188}
2189
2190template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002191_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002192__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2193 forward_iterator_tag, output_iterator_tag)
2194{
2195 if (__first != __last)
2196 {
2197 _ForwardIterator __i = __first;
2198 *__result = *__i;
2199 ++__result;
2200 while (++__first != __last)
2201 {
2202 if (!__pred(*__i, *__first))
2203 {
2204 *__result = *__first;
2205 ++__result;
2206 __i = __first;
2207 }
2208 }
2209 }
2210 return __result;
2211}
2212
2213template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002214_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002215__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2216 input_iterator_tag, forward_iterator_tag)
2217{
2218 if (__first != __last)
2219 {
2220 *__result = *__first;
2221 while (++__first != __last)
2222 if (!__pred(*__result, *__first))
2223 *++__result = *__first;
2224 ++__result;
2225 }
2226 return __result;
2227}
2228
Howard Hinnantc51e1022010-05-11 19:42:16 +00002229template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002230inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002231_OutputIterator
2232unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2233{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002234 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002235 (__first, __last, __result, __pred,
2236 typename iterator_traits<_InputIterator>::iterator_category(),
2237 typename iterator_traits<_OutputIterator>::iterator_category());
2238}
2239
2240template <class _InputIterator, class _OutputIterator>
Marshall Clowe9cdc5c2018-01-20 20:14:32 +00002241inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242_OutputIterator
2243unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2244{
2245 typedef typename iterator_traits<_InputIterator>::value_type __v;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002246 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247}
2248
2249// reverse
2250
2251template <class _BidirectionalIterator>
2252inline _LIBCPP_INLINE_VISIBILITY
2253void
2254__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2255{
2256 while (__first != __last)
2257 {
2258 if (__first == --__last)
2259 break;
Marshall Clow2ad71042015-11-02 21:34:25 +00002260 _VSTD::iter_swap(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002261 ++__first;
2262 }
2263}
2264
2265template <class _RandomAccessIterator>
2266inline _LIBCPP_INLINE_VISIBILITY
2267void
2268__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2269{
2270 if (__first != __last)
2271 for (; __first < --__last; ++__first)
Marshall Clow2ad71042015-11-02 21:34:25 +00002272 _VSTD::iter_swap(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002273}
2274
2275template <class _BidirectionalIterator>
2276inline _LIBCPP_INLINE_VISIBILITY
2277void
2278reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2279{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002280 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002281}
2282
2283// reverse_copy
2284
2285template <class _BidirectionalIterator, class _OutputIterator>
Marshall Clow7c0fbd82018-01-22 21:43:04 +00002286inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00002287_OutputIterator
2288reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2289{
2290 for (; __first != __last; ++__result)
2291 *__result = *--__last;
2292 return __result;
2293}
2294
2295// rotate
2296
2297template <class _ForwardIterator>
2298_ForwardIterator
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002299__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002300{
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002301 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2302 value_type __tmp = _VSTD::move(*__first);
2303 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2304 *__lm1 = _VSTD::move(__tmp);
2305 return __lm1;
2306}
2307
2308template <class _BidirectionalIterator>
2309_BidirectionalIterator
2310__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2311{
2312 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2313 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2314 value_type __tmp = _VSTD::move(*__lm1);
2315 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2316 *__first = _VSTD::move(__tmp);
2317 return __fp1;
2318}
2319
2320template <class _ForwardIterator>
2321_ForwardIterator
2322__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2323{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002324 _ForwardIterator __i = __middle;
2325 while (true)
2326 {
2327 swap(*__first, *__i);
2328 ++__first;
2329 if (++__i == __last)
2330 break;
2331 if (__first == __middle)
2332 __middle = __i;
2333 }
2334 _ForwardIterator __r = __first;
2335 if (__first != __middle)
2336 {
2337 __i = __middle;
2338 while (true)
2339 {
2340 swap(*__first, *__i);
2341 ++__first;
2342 if (++__i == __last)
2343 {
2344 if (__first == __middle)
2345 break;
2346 __i = __middle;
2347 }
2348 else if (__first == __middle)
2349 __middle = __i;
2350 }
2351 }
2352 return __r;
2353}
2354
2355template<typename _Integral>
2356inline _LIBCPP_INLINE_VISIBILITY
2357_Integral
Marshall Clowb8bfc2c2016-07-26 14:29:45 +00002358__algo_gcd(_Integral __x, _Integral __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002359{
2360 do
2361 {
2362 _Integral __t = __x % __y;
2363 __x = __y;
2364 __y = __t;
2365 } while (__y);
2366 return __x;
2367}
2368
2369template<typename _RandomAccessIterator>
2370_RandomAccessIterator
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002371__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002372{
2373 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2374 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002375
Howard Hinnantc51e1022010-05-11 19:42:16 +00002376 const difference_type __m1 = __middle - __first;
2377 const difference_type __m2 = __last - __middle;
2378 if (__m1 == __m2)
2379 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002380 _VSTD::swap_ranges(__first, __middle, __middle);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002381 return __middle;
2382 }
Marshall Clowb8bfc2c2016-07-26 14:29:45 +00002383 const difference_type __g = _VSTD::__algo_gcd(__m1, __m2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002384 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2385 {
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002386 value_type __t(_VSTD::move(*--__p));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002387 _RandomAccessIterator __p1 = __p;
2388 _RandomAccessIterator __p2 = __p1 + __m1;
2389 do
2390 {
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002391 *__p1 = _VSTD::move(*__p2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002392 __p1 = __p2;
2393 const difference_type __d = __last - __p2;
2394 if (__m1 < __d)
2395 __p2 += __m1;
2396 else
2397 __p2 = __first + (__m1 - __d);
2398 } while (__p2 != __p);
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002399 *__p1 = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002400 }
2401 return __first + __m2;
2402}
2403
2404template <class _ForwardIterator>
2405inline _LIBCPP_INLINE_VISIBILITY
2406_ForwardIterator
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002407__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2408 _VSTD::forward_iterator_tag)
2409{
2410 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2411 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2412 {
2413 if (_VSTD::next(__first) == __middle)
2414 return _VSTD::__rotate_left(__first, __last);
2415 }
2416 return _VSTD::__rotate_forward(__first, __middle, __last);
2417}
2418
2419template <class _BidirectionalIterator>
2420inline _LIBCPP_INLINE_VISIBILITY
2421_BidirectionalIterator
2422__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2423 _VSTD::bidirectional_iterator_tag)
2424{
2425 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2426 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2427 {
2428 if (_VSTD::next(__first) == __middle)
2429 return _VSTD::__rotate_left(__first, __last);
2430 if (_VSTD::next(__middle) == __last)
2431 return _VSTD::__rotate_right(__first, __last);
2432 }
2433 return _VSTD::__rotate_forward(__first, __middle, __last);
2434}
2435
2436template <class _RandomAccessIterator>
2437inline _LIBCPP_INLINE_VISIBILITY
2438_RandomAccessIterator
2439__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2440 _VSTD::random_access_iterator_tag)
2441{
2442 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2443 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2444 {
2445 if (_VSTD::next(__first) == __middle)
2446 return _VSTD::__rotate_left(__first, __last);
2447 if (_VSTD::next(__middle) == __last)
2448 return _VSTD::__rotate_right(__first, __last);
2449 return _VSTD::__rotate_gcd(__first, __middle, __last);
2450 }
2451 return _VSTD::__rotate_forward(__first, __middle, __last);
2452}
2453
2454template <class _ForwardIterator>
2455inline _LIBCPP_INLINE_VISIBILITY
2456_ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002457rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2458{
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002459 if (__first == __middle)
2460 return __last;
2461 if (__middle == __last)
2462 return __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002463 return _VSTD::__rotate(__first, __middle, __last,
Howard Hinnant5fec4ff2012-08-03 18:01:20 +00002464 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002465}
2466
2467// rotate_copy
2468
2469template <class _ForwardIterator, class _OutputIterator>
2470inline _LIBCPP_INLINE_VISIBILITY
2471_OutputIterator
2472rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2473{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002474 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475}
2476
Howard Hinnantc51e1022010-05-11 19:42:16 +00002477// min_element
2478
2479template <class _ForwardIterator, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00002480_LIBCPP_NODISCARD_EXT inline
2481_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482_ForwardIterator
Marshall Clow9e173072015-05-10 13:53:31 +00002483min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002484{
Eric Fiselierf8ecd942018-08-22 17:47:13 +00002485 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2486 "std::min_element requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487 if (__first != __last)
2488 {
2489 _ForwardIterator __i = __first;
2490 while (++__i != __last)
2491 if (__comp(*__i, *__first))
2492 __first = __i;
2493 }
2494 return __first;
2495}
2496
2497template <class _ForwardIterator>
Nico Weber471b10a2019-04-03 18:13:08 +00002498_LIBCPP_NODISCARD_EXT inline
2499_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002500_ForwardIterator
2501min_element(_ForwardIterator __first, _ForwardIterator __last)
2502{
Marshall Clow9e173072015-05-10 13:53:31 +00002503 return _VSTD::min_element(__first, __last,
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002504 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2505}
2506
2507// min
2508
2509template <class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00002510_LIBCPP_NODISCARD_EXT inline
2511_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002512const _Tp&
2513min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2514{
2515 return __comp(__b, __a) ? __b : __a;
2516}
2517
2518template <class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00002519_LIBCPP_NODISCARD_EXT inline
2520_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002521const _Tp&
2522min(const _Tp& __a, const _Tp& __b)
2523{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002524 return _VSTD::min(__a, __b, __less<_Tp>());
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002525}
2526
Eric Fiselier93dd1372017-04-18 23:26:47 +00002527#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002528
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002529template<class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00002530_LIBCPP_NODISCARD_EXT inline
2531_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002532_Tp
2533min(initializer_list<_Tp> __t, _Compare __comp)
2534{
Marshall Clow9e173072015-05-10 13:53:31 +00002535 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002536}
2537
2538template<class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00002539_LIBCPP_NODISCARD_EXT inline
2540_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002541_Tp
2542min(initializer_list<_Tp> __t)
2543{
Marshall Clow9e173072015-05-10 13:53:31 +00002544 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002545}
2546
Eric Fiselier93dd1372017-04-18 23:26:47 +00002547#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002548
Howard Hinnantc51e1022010-05-11 19:42:16 +00002549// max_element
2550
2551template <class _ForwardIterator, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00002552_LIBCPP_NODISCARD_EXT inline
2553_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002554_ForwardIterator
Marshall Clow9e173072015-05-10 13:53:31 +00002555max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002556{
Eric Fiselierf8ecd942018-08-22 17:47:13 +00002557 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2558 "std::max_element requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002559 if (__first != __last)
2560 {
2561 _ForwardIterator __i = __first;
2562 while (++__i != __last)
2563 if (__comp(*__first, *__i))
2564 __first = __i;
2565 }
2566 return __first;
2567}
2568
Marshall Clowe9dca072014-02-19 16:51:35 +00002569
Howard Hinnantc51e1022010-05-11 19:42:16 +00002570template <class _ForwardIterator>
Nico Weber471b10a2019-04-03 18:13:08 +00002571_LIBCPP_NODISCARD_EXT inline
2572_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002573_ForwardIterator
2574max_element(_ForwardIterator __first, _ForwardIterator __last)
2575{
Marshall Clow9e173072015-05-10 13:53:31 +00002576 return _VSTD::max_element(__first, __last,
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002577 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2578}
2579
2580// max
2581
2582template <class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00002583_LIBCPP_NODISCARD_EXT inline
2584_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002585const _Tp&
2586max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2587{
2588 return __comp(__a, __b) ? __b : __a;
2589}
2590
2591template <class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00002592_LIBCPP_NODISCARD_EXT inline
2593_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002594const _Tp&
2595max(const _Tp& __a, const _Tp& __b)
2596{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002597 return _VSTD::max(__a, __b, __less<_Tp>());
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002598}
2599
Eric Fiselier93dd1372017-04-18 23:26:47 +00002600#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002601
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002602template<class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00002603_LIBCPP_NODISCARD_EXT inline
2604_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002605_Tp
2606max(initializer_list<_Tp> __t, _Compare __comp)
2607{
Marshall Clow9e173072015-05-10 13:53:31 +00002608 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002609}
2610
2611template<class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00002612_LIBCPP_NODISCARD_EXT inline
2613_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002614_Tp
2615max(initializer_list<_Tp> __t)
2616{
Marshall Clow9e173072015-05-10 13:53:31 +00002617 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002618}
2619
Eric Fiselier93dd1372017-04-18 23:26:47 +00002620#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002621
Marshall Clow3e18d0e2016-03-07 22:43:49 +00002622#if _LIBCPP_STD_VER > 14
2623// clamp
2624template<class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00002625_LIBCPP_NODISCARD_EXT inline
2626_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow3e18d0e2016-03-07 22:43:49 +00002627const _Tp&
2628clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
2629{
2630 _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
2631 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
2632
2633}
2634
2635template<class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00002636_LIBCPP_NODISCARD_EXT inline
2637_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow3e18d0e2016-03-07 22:43:49 +00002638const _Tp&
2639clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
2640{
2641 return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
2642}
2643#endif
2644
Howard Hinnantc51e1022010-05-11 19:42:16 +00002645// minmax_element
2646
2647template <class _ForwardIterator, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00002648_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002649std::pair<_ForwardIterator, _ForwardIterator>
2650minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2651{
Eric Fiselierf8ecd942018-08-22 17:47:13 +00002652 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2653 "std::minmax_element requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002654 std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
2655 if (__first != __last)
2656 {
2657 if (++__first != __last)
2658 {
2659 if (__comp(*__first, *__result.first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002660 __result.first = __first;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002661 else
2662 __result.second = __first;
2663 while (++__first != __last)
2664 {
2665 _ForwardIterator __i = __first;
2666 if (++__first == __last)
2667 {
2668 if (__comp(*__i, *__result.first))
2669 __result.first = __i;
2670 else if (!__comp(*__i, *__result.second))
2671 __result.second = __i;
2672 break;
2673 }
2674 else
2675 {
2676 if (__comp(*__first, *__i))
2677 {
2678 if (__comp(*__first, *__result.first))
2679 __result.first = __first;
2680 if (!__comp(*__i, *__result.second))
2681 __result.second = __i;
2682 }
2683 else
2684 {
2685 if (__comp(*__i, *__result.first))
2686 __result.first = __i;
2687 if (!__comp(*__first, *__result.second))
2688 __result.second = __first;
2689 }
2690 }
2691 }
2692 }
2693 }
2694 return __result;
2695}
2696
2697template <class _ForwardIterator>
Nico Weber471b10a2019-04-03 18:13:08 +00002698_LIBCPP_NODISCARD_EXT inline
2699_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00002700std::pair<_ForwardIterator, _ForwardIterator>
2701minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2702{
Marshall Clowe9dca072014-02-19 16:51:35 +00002703 return _VSTD::minmax_element(__first, __last,
2704 __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002705}
2706
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002707// minmax
2708
2709template<class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00002710_LIBCPP_NODISCARD_EXT inline
2711_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002712pair<const _Tp&, const _Tp&>
2713minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2714{
2715 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2716 pair<const _Tp&, const _Tp&>(__a, __b);
2717}
2718
2719template<class _Tp>
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)
2724{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002725 return _VSTD::minmax(__a, __b, __less<_Tp>());
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002726}
2727
Eric Fiselier93dd1372017-04-18 23:26:47 +00002728#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002729
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002730template<class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00002731_LIBCPP_NODISCARD_EXT inline
2732_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002733pair<_Tp, _Tp>
2734minmax(initializer_list<_Tp> __t, _Compare __comp)
2735{
Marshall Clowe9dca072014-02-19 16:51:35 +00002736 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2737 _Iter __first = __t.begin();
2738 _Iter __last = __t.end();
Marshall Clow447713a2015-02-11 15:41:34 +00002739 std::pair<_Tp, _Tp> __result(*__first, *__first);
Marshall Clowe9dca072014-02-19 16:51:35 +00002740
2741 ++__first;
2742 if (__t.size() % 2 == 0)
2743 {
2744 if (__comp(*__first, __result.first))
2745 __result.first = *__first;
2746 else
2747 __result.second = *__first;
2748 ++__first;
2749 }
Aditya Kumar3a0179a2016-08-25 11:52:38 +00002750
Marshall Clowe9dca072014-02-19 16:51:35 +00002751 while (__first != __last)
2752 {
2753 _Tp __prev = *__first++;
Marshall Clow447713a2015-02-11 15:41:34 +00002754 if (__comp(*__first, __prev)) {
2755 if ( __comp(*__first, __result.first)) __result.first = *__first;
2756 if (!__comp(__prev, __result.second)) __result.second = __prev;
Marshall Clowe9dca072014-02-19 16:51:35 +00002757 }
2758 else {
Marshall Clow447713a2015-02-11 15:41:34 +00002759 if ( __comp(__prev, __result.first)) __result.first = __prev;
2760 if (!__comp(*__first, __result.second)) __result.second = *__first;
Marshall Clowe9dca072014-02-19 16:51:35 +00002761 }
Aditya Kumar3a0179a2016-08-25 11:52:38 +00002762
Marshall Clowe9dca072014-02-19 16:51:35 +00002763 __first++;
2764 }
2765 return __result;
2766}
2767
2768template<class _Tp>
Nico Weber471b10a2019-04-03 18:13:08 +00002769_LIBCPP_NODISCARD_EXT inline
2770_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clowe9dca072014-02-19 16:51:35 +00002771pair<_Tp, _Tp>
2772minmax(initializer_list<_Tp> __t)
2773{
2774 return _VSTD::minmax(__t, __less<_Tp>());
Howard Hinnantb120e7a2010-08-21 20:10:01 +00002775}
2776
Eric Fiselier93dd1372017-04-18 23:26:47 +00002777#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002778
Howard Hinnantc51e1022010-05-11 19:42:16 +00002779// random_shuffle
2780
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002781// __independent_bits_engine
2782
Howard Hinnantc834c512011-11-29 18:15:50 +00002783template <unsigned long long _Xp, size_t _Rp>
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002784struct __log2_imp
Howard Hinnantc51e1022010-05-11 19:42:16 +00002785{
Howard Hinnantc834c512011-11-29 18:15:50 +00002786 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2787 : __log2_imp<_Xp, _Rp - 1>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788};
2789
Howard Hinnantc834c512011-11-29 18:15:50 +00002790template <unsigned long long _Xp>
2791struct __log2_imp<_Xp, 0>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002792{
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002793 static const size_t value = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002794};
2795
Howard Hinnantc834c512011-11-29 18:15:50 +00002796template <size_t _Rp>
2797struct __log2_imp<0, _Rp>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002798{
Howard Hinnantc834c512011-11-29 18:15:50 +00002799 static const size_t value = _Rp + 1;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002800};
2801
Eric Fiselier4638fca2017-05-31 21:20:18 +00002802template <class _UIntType, _UIntType _Xp>
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002803struct __log2
Howard Hinnantc51e1022010-05-11 19:42:16 +00002804{
Howard Hinnantc834c512011-11-29 18:15:50 +00002805 static const size_t value = __log2_imp<_Xp,
Eric Fiselier4638fca2017-05-31 21:20:18 +00002806 sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002807};
2808
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002809template<class _Engine, class _UIntType>
2810class __independent_bits_engine
Howard Hinnantc51e1022010-05-11 19:42:16 +00002811{
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002812public:
2813 // types
2814 typedef _UIntType result_type;
2815
2816private:
2817 typedef typename _Engine::result_type _Engine_result_type;
2818 typedef typename conditional
2819 <
2820 sizeof(_Engine_result_type) <= sizeof(result_type),
2821 result_type,
2822 _Engine_result_type
2823 >::type _Working_result_type;
2824
2825 _Engine& __e_;
2826 size_t __w_;
2827 size_t __w0_;
2828 size_t __n_;
2829 size_t __n0_;
2830 _Working_result_type __y0_;
2831 _Working_result_type __y1_;
2832 _Engine_result_type __mask0_;
2833 _Engine_result_type __mask1_;
2834
Eric Fiselier93dd1372017-04-18 23:26:47 +00002835#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc834c512011-11-29 18:15:50 +00002836 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnant5a646852012-04-02 21:00:45 +00002837 + _Working_result_type(1);
2838#else
2839 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2840 + _Working_result_type(1);
2841#endif
2842 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2843 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2844 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002845
2846public:
2847 // constructors and seeding functions
2848 __independent_bits_engine(_Engine& __e, size_t __w);
2849
2850 // generating functions
Howard Hinnantc834c512011-11-29 18:15:50 +00002851 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002852
2853private:
Marshall Clowfe778582017-09-20 19:38:43 +00002854 result_type __eval(false_type);
2855 result_type __eval(true_type);
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002856};
2857
2858template<class _Engine, class _UIntType>
2859__independent_bits_engine<_Engine, _UIntType>
2860 ::__independent_bits_engine(_Engine& __e, size_t __w)
2861 : __e_(__e),
2862 __w_(__w)
2863{
2864 __n_ = __w_ / __m + (__w_ % __m != 0);
2865 __w0_ = __w_ / __n_;
Howard Hinnantc834c512011-11-29 18:15:50 +00002866 if (_Rp == 0)
2867 __y0_ = _Rp;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002868 else if (__w0_ < _WDt)
Howard Hinnantc834c512011-11-29 18:15:50 +00002869 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002870 else
2871 __y0_ = 0;
Howard Hinnantc834c512011-11-29 18:15:50 +00002872 if (_Rp - __y0_ > __y0_ / __n_)
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002873 {
2874 ++__n_;
2875 __w0_ = __w_ / __n_;
2876 if (__w0_ < _WDt)
Howard Hinnantc834c512011-11-29 18:15:50 +00002877 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002878 else
2879 __y0_ = 0;
2880 }
2881 __n0_ = __n_ - __w_ % __n_;
2882 if (__w0_ < _WDt - 1)
Howard Hinnantc834c512011-11-29 18:15:50 +00002883 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002884 else
2885 __y1_ = 0;
2886 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2887 _Engine_result_type(0);
2888 __mask1_ = __w0_ < _EDt - 1 ?
2889 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2890 _Engine_result_type(~0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002891}
2892
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002893template<class _Engine, class _UIntType>
2894inline
2895_UIntType
Marshall Clowfe778582017-09-20 19:38:43 +00002896__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002897{
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002898 return static_cast<result_type>(__e_() & __mask0_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002899}
2900
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002901template<class _Engine, class _UIntType>
2902_UIntType
Marshall Clowfe778582017-09-20 19:38:43 +00002903__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904{
Marshall Clowafc48592017-09-20 17:34:11 +00002905 const size_t _WRt = numeric_limits<result_type>::digits;
Howard Hinnantc834c512011-11-29 18:15:50 +00002906 result_type _Sp = 0;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002907 for (size_t __k = 0; __k < __n0_; ++__k)
2908 {
2909 _Engine_result_type __u;
2910 do
2911 {
2912 __u = __e_() - _Engine::min();
2913 } while (__u >= __y0_);
Marshall Clowafc48592017-09-20 17:34:11 +00002914 if (__w0_ < _WRt)
Howard Hinnantc834c512011-11-29 18:15:50 +00002915 _Sp <<= __w0_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002916 else
Howard Hinnantc834c512011-11-29 18:15:50 +00002917 _Sp = 0;
2918 _Sp += __u & __mask0_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002919 }
2920 for (size_t __k = __n0_; __k < __n_; ++__k)
2921 {
2922 _Engine_result_type __u;
2923 do
2924 {
2925 __u = __e_() - _Engine::min();
2926 } while (__u >= __y1_);
Marshall Clowafc48592017-09-20 17:34:11 +00002927 if (__w0_ < _WRt - 1)
Howard Hinnantc834c512011-11-29 18:15:50 +00002928 _Sp <<= __w0_ + 1;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002929 else
Howard Hinnantc834c512011-11-29 18:15:50 +00002930 _Sp = 0;
2931 _Sp += __u & __mask1_;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002932 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002933 return _Sp;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00002934}
2935
2936// uniform_int_distribution
2937
2938template<class _IntType = int>
2939class uniform_int_distribution
2940{
2941public:
2942 // types
2943 typedef _IntType result_type;
2944
2945 class param_type
2946 {
2947 result_type __a_;
2948 result_type __b_;
2949 public:
2950 typedef uniform_int_distribution distribution_type;
2951
2952 explicit param_type(result_type __a = 0,
2953 result_type __b = numeric_limits<result_type>::max())
2954 : __a_(__a), __b_(__b) {}
2955
2956 result_type a() const {return __a_;}
2957 result_type b() const {return __b_;}
2958
2959 friend bool operator==(const param_type& __x, const param_type& __y)
2960 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
2961 friend bool operator!=(const param_type& __x, const param_type& __y)
2962 {return !(__x == __y);}
2963 };
2964
2965private:
2966 param_type __p_;
2967
2968public:
2969 // constructors and reset functions
2970 explicit uniform_int_distribution(result_type __a = 0,
2971 result_type __b = numeric_limits<result_type>::max())
2972 : __p_(param_type(__a, __b)) {}
2973 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
2974 void reset() {}
2975
2976 // generating functions
2977 template<class _URNG> result_type operator()(_URNG& __g)
2978 {return (*this)(__g, __p_);}
2979 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
2980
2981 // property functions
2982 result_type a() const {return __p_.a();}
2983 result_type b() const {return __p_.b();}
2984
2985 param_type param() const {return __p_;}
2986 void param(const param_type& __p) {__p_ = __p;}
2987
2988 result_type min() const {return a();}
2989 result_type max() const {return b();}
2990
2991 friend bool operator==(const uniform_int_distribution& __x,
2992 const uniform_int_distribution& __y)
2993 {return __x.__p_ == __y.__p_;}
2994 friend bool operator!=(const uniform_int_distribution& __x,
2995 const uniform_int_distribution& __y)
2996 {return !(__x == __y);}
2997};
2998
2999template<class _IntType>
3000template<class _URNG>
3001typename uniform_int_distribution<_IntType>::result_type
3002uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
Marshall Clowf79f4402018-10-08 20:20:34 +00003003_LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003004{
3005 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3006 uint32_t, uint64_t>::type _UIntType;
Marshall Clowf79f4402018-10-08 20:20:34 +00003007 const _UIntType _Rp = _UIntType(__p.b()) - _UIntType(__p.a()) + _UIntType(1);
Howard Hinnantc834c512011-11-29 18:15:50 +00003008 if (_Rp == 1)
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003009 return __p.a();
3010 const size_t _Dt = numeric_limits<_UIntType>::digits;
3011 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
Howard Hinnantc834c512011-11-29 18:15:50 +00003012 if (_Rp == 0)
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003013 return static_cast<result_type>(_Eng(__g, _Dt)());
Howard Hinnantc834c512011-11-29 18:15:50 +00003014 size_t __w = _Dt - __clz(_Rp) - 1;
Marshall Clow40aada52015-07-30 18:26:34 +00003015 if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003016 ++__w;
3017 _Eng __e(__g, __w);
3018 _UIntType __u;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003019 do
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003020 {
3021 __u = __e();
Howard Hinnantc834c512011-11-29 18:15:50 +00003022 } while (__u >= _Rp);
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003023 return static_cast<result_type>(__u + __p.a());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003024}
3025
Eric Fiselierf5fb27c2017-04-03 23:23:44 +00003026#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \
3027 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003028class _LIBCPP_TYPE_VIS __rs_default;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003029
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003030_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003031
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003032class _LIBCPP_TYPE_VIS __rs_default
Howard Hinnantc51e1022010-05-11 19:42:16 +00003033{
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003034 static unsigned __c_;
3035
3036 __rs_default();
3037public:
Marshall Clow9903c5b2013-02-07 22:12:02 +00003038 typedef uint_fast32_t result_type;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003039
3040 static const result_type _Min = 0;
3041 static const result_type _Max = 0xFFFFFFFF;
3042
3043 __rs_default(const __rs_default&);
3044 ~__rs_default();
3045
3046 result_type operator()();
3047
Howard Hinnant664183b2012-04-02 00:40:41 +00003048 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
3049 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003050
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003051 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003052};
3053
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00003054_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003055
3056template <class _RandomAccessIterator>
Louis Dionne481a2662018-09-23 18:35:00 +00003057_LIBCPP_DEPRECATED_IN_CXX14 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00003058random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
3059{
3060 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc834c512011-11-29 18:15:50 +00003061 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3062 typedef typename _Dp::param_type _Pp;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003063 difference_type __d = __last - __first;
3064 if (__d > 1)
3065 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003066 _Dp __uid;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003067 __rs_default __g = __rs_get();
Marshall Clowa224ace2019-01-24 19:20:19 +00003068 for (--__last, (void) --__d; __first < __last; ++__first, (void) --__d)
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003069 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003070 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003071 if (__i != difference_type(0))
3072 swap(*__first, *(__first + __i));
3073 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003074 }
3075}
3076
3077template <class _RandomAccessIterator, class _RandomNumberGenerator>
Louis Dionne481a2662018-09-23 18:35:00 +00003078_LIBCPP_DEPRECATED_IN_CXX14 void
Howard Hinnantc51e1022010-05-11 19:42:16 +00003079random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselier93dd1372017-04-18 23:26:47 +00003080#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00003081 _RandomNumberGenerator&& __rand)
3082#else
3083 _RandomNumberGenerator& __rand)
3084#endif
3085{
3086 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3087 difference_type __d = __last - __first;
3088 if (__d > 1)
3089 {
Marshall Clowa224ace2019-01-24 19:20:19 +00003090 for (--__last; __first < __last; ++__first, (void) --__d)
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003091 {
3092 difference_type __i = __rand(__d);
Marshall Clow5bdfc232018-09-11 18:33:45 +00003093 if (__i != difference_type(0))
Marshall Clowf79f4402018-10-08 20:20:34 +00003094 swap(*__first, *(__first + __i));
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003095 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003096 }
3097}
Marshall Clowfac06e52017-03-23 13:43:37 +00003098#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003099
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003100template <class _PopulationIterator, class _SampleIterator, class _Distance,
3101 class _UniformRandomNumberGenerator>
3102_LIBCPP_INLINE_VISIBILITY
3103_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsonc9637642017-11-14 11:14:25 +00003104 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003105 _Distance __n,
3106 _UniformRandomNumberGenerator & __g,
3107 input_iterator_tag) {
3108
3109 _Distance __k = 0;
3110 for (; __first != __last && __k < __n; ++__first, (void)++__k)
Alexander Richardsonc9637642017-11-14 11:14:25 +00003111 __output_iter[__k] = *__first;
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003112 _Distance __sz = __k;
3113 for (; __first != __last; ++__first, (void)++__k) {
3114 _Distance __r = _VSTD::uniform_int_distribution<_Distance>(0, __k)(__g);
3115 if (__r < __sz)
Alexander Richardsonc9637642017-11-14 11:14:25 +00003116 __output_iter[__r] = *__first;
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003117 }
Alexander Richardsonc9637642017-11-14 11:14:25 +00003118 return __output_iter + _VSTD::min(__n, __k);
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003119}
3120
3121template <class _PopulationIterator, class _SampleIterator, class _Distance,
3122 class _UniformRandomNumberGenerator>
3123_LIBCPP_INLINE_VISIBILITY
3124_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsonc9637642017-11-14 11:14:25 +00003125 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003126 _Distance __n,
3127 _UniformRandomNumberGenerator& __g,
3128 forward_iterator_tag) {
3129 _Distance __unsampled_sz = _VSTD::distance(__first, __last);
3130 for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
3131 _Distance __r =
3132 _VSTD::uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
3133 if (__r < __n) {
Alexander Richardsonc9637642017-11-14 11:14:25 +00003134 *__output_iter++ = *__first;
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003135 --__n;
3136 }
3137 }
Alexander Richardsonc9637642017-11-14 11:14:25 +00003138 return __output_iter;
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003139}
3140
3141template <class _PopulationIterator, class _SampleIterator, class _Distance,
3142 class _UniformRandomNumberGenerator>
3143_LIBCPP_INLINE_VISIBILITY
3144_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardsonc9637642017-11-14 11:14:25 +00003145 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003146 _Distance __n, _UniformRandomNumberGenerator& __g) {
3147 typedef typename iterator_traits<_PopulationIterator>::iterator_category
3148 _PopCategory;
3149 typedef typename iterator_traits<_PopulationIterator>::difference_type
3150 _Difference;
3151 static_assert(__is_forward_iterator<_PopulationIterator>::value ||
3152 __is_random_access_iterator<_SampleIterator>::value,
3153 "SampleIterator must meet the requirements of RandomAccessIterator");
3154 typedef typename common_type<_Distance, _Difference>::type _CommonType;
3155 _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
3156 return _VSTD::__sample(
Alexander Richardsonc9637642017-11-14 11:14:25 +00003157 __first, __last, __output_iter, _CommonType(__n),
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003158 __g, _PopCategory());
3159}
3160
3161#if _LIBCPP_STD_VER > 14
3162template <class _PopulationIterator, class _SampleIterator, class _Distance,
3163 class _UniformRandomNumberGenerator>
3164inline _LIBCPP_INLINE_VISIBILITY
3165_SampleIterator sample(_PopulationIterator __first,
Alexander Richardsonc9637642017-11-14 11:14:25 +00003166 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003167 _Distance __n, _UniformRandomNumberGenerator&& __g) {
Alexander Richardsonc9637642017-11-14 11:14:25 +00003168 return _VSTD::__sample(__first, __last, __output_iter, __n, __g);
Eric Fiselier1208fcd2016-08-28 22:14:37 +00003169}
3170#endif // _LIBCPP_STD_VER > 14
3171
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003172template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3173 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselier93dd1372017-04-18 23:26:47 +00003174#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanta5e71782010-11-18 01:47:02 +00003175 _UniformRandomNumberGenerator&& __g)
3176#else
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003177 _UniformRandomNumberGenerator& __g)
Howard Hinnanta5e71782010-11-18 01:47:02 +00003178#endif
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003179{
3180 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc834c512011-11-29 18:15:50 +00003181 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3182 typedef typename _Dp::param_type _Pp;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003183 difference_type __d = __last - __first;
3184 if (__d > 1)
3185 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003186 _Dp __uid;
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003187 for (--__last, --__d; __first < __last; ++__first, --__d)
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003188 {
Howard Hinnantc834c512011-11-29 18:15:50 +00003189 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnantc9bc02c2010-10-22 15:26:39 +00003190 if (__i != difference_type(0))
3191 swap(*__first, *(__first + __i));
3192 }
Howard Hinnant578ac0f2010-05-26 17:49:34 +00003193 }
3194}
3195
Howard Hinnantc51e1022010-05-11 19:42:16 +00003196template <class _InputIterator, class _Predicate>
Nico Weber471b10a2019-04-03 18:13:08 +00003197_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00003198is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3199{
3200 for (; __first != __last; ++__first)
3201 if (!__pred(*__first))
3202 break;
Marshall Clow3562ed72015-02-02 18:16:35 +00003203 if ( __first == __last )
3204 return true;
3205 ++__first;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003206 for (; __first != __last; ++__first)
3207 if (__pred(*__first))
3208 return false;
3209 return true;
3210}
3211
3212// partition
3213
3214template <class _Predicate, class _ForwardIterator>
3215_ForwardIterator
3216__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3217{
3218 while (true)
3219 {
3220 if (__first == __last)
3221 return __first;
3222 if (!__pred(*__first))
3223 break;
3224 ++__first;
3225 }
3226 for (_ForwardIterator __p = __first; ++__p != __last;)
3227 {
3228 if (__pred(*__p))
3229 {
3230 swap(*__first, *__p);
3231 ++__first;
3232 }
3233 }
3234 return __first;
3235}
3236
3237template <class _Predicate, class _BidirectionalIterator>
3238_BidirectionalIterator
3239__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3240 bidirectional_iterator_tag)
3241{
3242 while (true)
3243 {
3244 while (true)
3245 {
3246 if (__first == __last)
3247 return __first;
3248 if (!__pred(*__first))
3249 break;
3250 ++__first;
3251 }
3252 do
3253 {
3254 if (__first == --__last)
3255 return __first;
3256 } while (!__pred(*__last));
3257 swap(*__first, *__last);
3258 ++__first;
3259 }
3260}
3261
3262template <class _ForwardIterator, class _Predicate>
3263inline _LIBCPP_INLINE_VISIBILITY
3264_ForwardIterator
3265partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3266{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003267 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003268 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3269}
3270
3271// partition_copy
3272
3273template <class _InputIterator, class _OutputIterator1,
3274 class _OutputIterator2, class _Predicate>
Marshall Clow5492c8a2018-01-22 20:44:33 +00003275_LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_OutputIterator1, _OutputIterator2>
Howard Hinnantc51e1022010-05-11 19:42:16 +00003276partition_copy(_InputIterator __first, _InputIterator __last,
3277 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3278 _Predicate __pred)
3279{
3280 for (; __first != __last; ++__first)
3281 {
3282 if (__pred(*__first))
3283 {
3284 *__out_true = *__first;
3285 ++__out_true;
3286 }
3287 else
3288 {
3289 *__out_false = *__first;
3290 ++__out_false;
3291 }
3292 }
3293 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3294}
3295
3296// partition_point
3297
3298template<class _ForwardIterator, class _Predicate>
Marshall Clowcb3c8262018-01-15 17:53:34 +00003299_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003300partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3301{
3302 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003303 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003304 while (__len != 0)
3305 {
Louis Dionnedda14512018-12-17 16:04:39 +00003306 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003307 _ForwardIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003308 _VSTD::advance(__m, __l2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309 if (__pred(*__m))
3310 {
3311 __first = ++__m;
3312 __len -= __l2 + 1;
3313 }
3314 else
3315 __len = __l2;
3316 }
3317 return __first;
3318}
3319
3320// stable_partition
3321
3322template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3323_ForwardIterator
3324__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3325 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3326{
3327 // *__first is known to be false
3328 // __len >= 1
3329 if (__len == 1)
3330 return __first;
3331 if (__len == 2)
3332 {
3333 _ForwardIterator __m = __first;
3334 if (__pred(*++__m))
3335 {
3336 swap(*__first, *__m);
3337 return __m;
3338 }
3339 return __first;
3340 }
3341 if (__len <= __p.second)
3342 { // The buffer is big enough to use
3343 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3344 __destruct_n __d(0);
3345 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3346 // Move the falses into the temporary buffer, and the trues to the front of the line
3347 // Update __first to always point to the end of the trues
3348 value_type* __t = __p.first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003349 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003350 __d.__incr((value_type*)0);
3351 ++__t;
3352 _ForwardIterator __i = __first;
3353 while (++__i != __last)
3354 {
3355 if (__pred(*__i))
3356 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003357 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003358 ++__first;
3359 }
3360 else
3361 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003362 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003363 __d.__incr((value_type*)0);
3364 ++__t;
3365 }
3366 }
3367 // All trues now at start of range, all falses in buffer
3368 // Move falses back into range, but don't mess up __first which points to first false
3369 __i = __first;
3370 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003371 *__i = _VSTD::move(*__t2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003372 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3373 return __first;
3374 }
3375 // Else not enough buffer, do in place
3376 // __len >= 3
3377 _ForwardIterator __m = __first;
3378 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003379 _VSTD::advance(__m, __len2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003380 // recurse on [__first, __m), *__first know to be false
3381 // F?????????????????
3382 // f m l
3383 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3384 _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
3385 // TTTFFFFF??????????
3386 // f ff m l
3387 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3388 _ForwardIterator __m1 = __m;
3389 _ForwardIterator __second_false = __last;
3390 _Distance __len_half = __len - __len2;
3391 while (__pred(*__m1))
3392 {
3393 if (++__m1 == __last)
3394 goto __second_half_done;
3395 --__len_half;
3396 }
3397 // TTTFFFFFTTTF??????
3398 // f ff m m1 l
3399 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
3400__second_half_done:
3401 // TTTFFFFFTTTTTFFFFF
3402 // f ff m sf l
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003403 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003404 // TTTTTTTTFFFFFFFFFF
3405 // |
3406}
3407
3408struct __return_temporary_buffer
3409{
3410 template <class _Tp>
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003411 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003412};
3413
3414template <class _Predicate, class _ForwardIterator>
3415_ForwardIterator
3416__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3417 forward_iterator_tag)
3418{
3419 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3420 // Either prove all true and return __first or point to first false
3421 while (true)
3422 {
3423 if (__first == __last)
3424 return __first;
3425 if (!__pred(*__first))
3426 break;
3427 ++__first;
3428 }
3429 // We now have a reduced range [__first, __last)
3430 // *__first is known to be false
3431 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3432 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003433 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003434 pair<value_type*, ptrdiff_t> __p(0, 0);
3435 unique_ptr<value_type, __return_temporary_buffer> __h;
3436 if (__len >= __alloc_limit)
3437 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003438 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003439 __h.reset(__p.first);
3440 }
3441 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3442 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3443}
3444
3445template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3446_BidirectionalIterator
3447__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3448 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3449{
3450 // *__first is known to be false
3451 // *__last is known to be true
3452 // __len >= 2
3453 if (__len == 2)
3454 {
3455 swap(*__first, *__last);
3456 return __last;
3457 }
3458 if (__len == 3)
3459 {
3460 _BidirectionalIterator __m = __first;
3461 if (__pred(*++__m))
3462 {
3463 swap(*__first, *__m);
3464 swap(*__m, *__last);
3465 return __last;
3466 }
3467 swap(*__m, *__last);
3468 swap(*__first, *__m);
3469 return __m;
3470 }
3471 if (__len <= __p.second)
3472 { // The buffer is big enough to use
3473 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3474 __destruct_n __d(0);
3475 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3476 // Move the falses into the temporary buffer, and the trues to the front of the line
3477 // Update __first to always point to the end of the trues
3478 value_type* __t = __p.first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003479 ::new(__t) value_type(_VSTD::move(*__first));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003480 __d.__incr((value_type*)0);
3481 ++__t;
3482 _BidirectionalIterator __i = __first;
3483 while (++__i != __last)
3484 {
3485 if (__pred(*__i))
3486 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003487 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003488 ++__first;
3489 }
3490 else
3491 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003492 ::new(__t) value_type(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003493 __d.__incr((value_type*)0);
3494 ++__t;
3495 }
3496 }
3497 // move *__last, known to be true
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003498 *__first = _VSTD::move(*__i);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003499 __i = ++__first;
3500 // All trues now at start of range, all falses in buffer
3501 // Move falses back into range, but don't mess up __first which points to first false
3502 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003503 *__i = _VSTD::move(*__t2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003504 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3505 return __first;
3506 }
3507 // Else not enough buffer, do in place
3508 // __len >= 4
3509 _BidirectionalIterator __m = __first;
3510 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003511 _VSTD::advance(__m, __len2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003512 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3513 // F????????????????T
3514 // f m l
3515 _BidirectionalIterator __m1 = __m;
3516 _BidirectionalIterator __first_false = __first;
3517 _Distance __len_half = __len2;
3518 while (!__pred(*--__m1))
3519 {
3520 if (__m1 == __first)
3521 goto __first_half_done;
3522 --__len_half;
3523 }
3524 // F???TFFF?????????T
3525 // f m1 m l
3526 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
3527 __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
3528__first_half_done:
3529 // TTTFFFFF?????????T
3530 // f ff m l
3531 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3532 __m1 = __m;
3533 _BidirectionalIterator __second_false = __last;
3534 ++__second_false;
3535 __len_half = __len - __len2;
3536 while (__pred(*__m1))
3537 {
3538 if (++__m1 == __last)
3539 goto __second_half_done;
3540 --__len_half;
3541 }
3542 // TTTFFFFFTTTF?????T
3543 // f ff m m1 l
3544 __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
3545__second_half_done:
3546 // TTTFFFFFTTTTTFFFFF
3547 // f ff m sf l
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003548 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003549 // TTTTTTTTFFFFFFFFFF
3550 // |
3551}
3552
3553template <class _Predicate, class _BidirectionalIterator>
3554_BidirectionalIterator
3555__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3556 bidirectional_iterator_tag)
3557{
3558 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3559 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3560 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3561 // Either prove all true and return __first or point to first false
3562 while (true)
3563 {
3564 if (__first == __last)
3565 return __first;
3566 if (!__pred(*__first))
3567 break;
3568 ++__first;
3569 }
3570 // __first points to first false, everything prior to __first is already set.
3571 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3572 do
3573 {
3574 if (__first == --__last)
3575 return __first;
3576 } while (!__pred(*__last));
3577 // We now have a reduced range [__first, __last]
3578 // *__first is known to be false
3579 // *__last is known to be true
3580 // __len >= 2
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003581 difference_type __len = _VSTD::distance(__first, __last) + 1;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003582 pair<value_type*, ptrdiff_t> __p(0, 0);
3583 unique_ptr<value_type, __return_temporary_buffer> __h;
3584 if (__len >= __alloc_limit)
3585 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003586 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003587 __h.reset(__p.first);
3588 }
3589 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3590 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3591}
3592
3593template <class _ForwardIterator, class _Predicate>
3594inline _LIBCPP_INLINE_VISIBILITY
3595_ForwardIterator
3596stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3597{
3598 return __stable_partition<typename add_lvalue_reference<_Predicate>::type>
3599 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3600}
3601
3602// is_sorted_until
3603
3604template <class _ForwardIterator, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00003605_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00003606is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3607{
3608 if (__first != __last)
3609 {
3610 _ForwardIterator __i = __first;
3611 while (++__i != __last)
3612 {
3613 if (__comp(*__i, *__first))
3614 return __i;
3615 __first = __i;
3616 }
3617 }
3618 return __last;
3619}
3620
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003621template<class _ForwardIterator>
Nico Weber471b10a2019-04-03 18:13:08 +00003622_LIBCPP_NODISCARD_EXT inline
3623_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003624_ForwardIterator
3625is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3626{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003627 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003628}
3629
3630// is_sorted
3631
3632template <class _ForwardIterator, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00003633_LIBCPP_NODISCARD_EXT inline
3634_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635bool
3636is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3637{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003638 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639}
3640
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003641template<class _ForwardIterator>
Nico Weber471b10a2019-04-03 18:13:08 +00003642_LIBCPP_NODISCARD_EXT inline
3643_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00003644bool
3645is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3646{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003647 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003648}
3649
3650// sort
3651
3652// stable, 2-3 compares, 0-2 swaps
3653
3654template <class _Compare, class _ForwardIterator>
3655unsigned
3656__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3657{
3658 unsigned __r = 0;
3659 if (!__c(*__y, *__x)) // if x <= y
3660 {
3661 if (!__c(*__z, *__y)) // if y <= z
3662 return __r; // x <= y && y <= z
3663 // x <= y && y > z
3664 swap(*__y, *__z); // x <= z && y < z
3665 __r = 1;
3666 if (__c(*__y, *__x)) // if x > y
3667 {
3668 swap(*__x, *__y); // x < y && y <= z
3669 __r = 2;
3670 }
3671 return __r; // x <= y && y < z
3672 }
3673 if (__c(*__z, *__y)) // x > y, if y > z
3674 {
3675 swap(*__x, *__z); // x < y && y < z
3676 __r = 1;
3677 return __r;
3678 }
3679 swap(*__x, *__y); // x > y && y <= z
3680 __r = 1; // x < y && x <= z
3681 if (__c(*__z, *__y)) // if y > z
3682 {
3683 swap(*__y, *__z); // x <= y && y < z
3684 __r = 2;
3685 }
3686 return __r;
3687} // x <= y && y <= z
3688
3689// stable, 3-6 compares, 0-5 swaps
3690
3691template <class _Compare, class _ForwardIterator>
3692unsigned
3693__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3694 _ForwardIterator __x4, _Compare __c)
3695{
3696 unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c);
3697 if (__c(*__x4, *__x3))
3698 {
3699 swap(*__x3, *__x4);
3700 ++__r;
3701 if (__c(*__x3, *__x2))
3702 {
3703 swap(*__x2, *__x3);
3704 ++__r;
3705 if (__c(*__x2, *__x1))
3706 {
3707 swap(*__x1, *__x2);
3708 ++__r;
3709 }
3710 }
3711 }
3712 return __r;
3713}
3714
3715// stable, 4-10 compares, 0-9 swaps
3716
3717template <class _Compare, class _ForwardIterator>
Louis Dionne9e70e362018-11-21 16:24:46 +00003718_LIBCPP_HIDDEN
Howard Hinnantc51e1022010-05-11 19:42:16 +00003719unsigned
3720__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3721 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3722{
3723 unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
3724 if (__c(*__x5, *__x4))
3725 {
3726 swap(*__x4, *__x5);
3727 ++__r;
3728 if (__c(*__x4, *__x3))
3729 {
3730 swap(*__x3, *__x4);
3731 ++__r;
3732 if (__c(*__x3, *__x2))
3733 {
3734 swap(*__x2, *__x3);
3735 ++__r;
3736 if (__c(*__x2, *__x1))
3737 {
3738 swap(*__x1, *__x2);
3739 ++__r;
3740 }
3741 }
3742 }
3743 }
3744 return __r;
3745}
3746
3747// Assumes size > 0
3748template <class _Compare, class _BirdirectionalIterator>
3749void
3750__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3751{
3752 _BirdirectionalIterator __lm1 = __last;
3753 for (--__lm1; __first != __lm1; ++__first)
3754 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003755 _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003756 typename add_lvalue_reference<_Compare>::type>
3757 (__first, __last, __comp);
3758 if (__i != __first)
3759 swap(*__first, *__i);
3760 }
3761}
3762
3763template <class _Compare, class _BirdirectionalIterator>
3764void
3765__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp)
3766{
3767 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3768 if (__first != __last)
3769 {
3770 _BirdirectionalIterator __i = __first;
3771 for (++__i; __i != __last; ++__i)
3772 {
3773 _BirdirectionalIterator __j = __i;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003774 value_type __t(_VSTD::move(*__j));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003775 for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003776 *__j = _VSTD::move(*__k);
3777 *__j = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003778 }
3779 }
3780}
3781
3782template <class _Compare, class _RandomAccessIterator>
3783void
3784__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3785{
3786 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3787 _RandomAccessIterator __j = __first+2;
3788 __sort3<_Compare>(__first, __first+1, __j, __comp);
3789 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3790 {
3791 if (__comp(*__i, *__j))
3792 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003793 value_type __t(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003794 _RandomAccessIterator __k = __j;
3795 __j = __i;
3796 do
3797 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003798 *__j = _VSTD::move(*__k);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003799 __j = __k;
3800 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003801 *__j = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003802 }
3803 __j = __i;
3804 }
3805}
3806
3807template <class _Compare, class _RandomAccessIterator>
3808bool
3809__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3810{
3811 switch (__last - __first)
3812 {
3813 case 0:
3814 case 1:
3815 return true;
3816 case 2:
3817 if (__comp(*--__last, *__first))
3818 swap(*__first, *__last);
3819 return true;
3820 case 3:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003821 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003822 return true;
3823 case 4:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003824 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003825 return true;
3826 case 5:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003827 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003828 return true;
3829 }
3830 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3831 _RandomAccessIterator __j = __first+2;
3832 __sort3<_Compare>(__first, __first+1, __j, __comp);
3833 const unsigned __limit = 8;
3834 unsigned __count = 0;
3835 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3836 {
3837 if (__comp(*__i, *__j))
3838 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003839 value_type __t(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003840 _RandomAccessIterator __k = __j;
3841 __j = __i;
3842 do
3843 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003844 *__j = _VSTD::move(*__k);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003845 __j = __k;
3846 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003847 *__j = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003848 if (++__count == __limit)
3849 return ++__i == __last;
3850 }
3851 __j = __i;
3852 }
3853 return true;
3854}
3855
3856template <class _Compare, class _BirdirectionalIterator>
3857void
3858__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1,
3859 typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp)
3860{
3861 typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type;
3862 if (__first1 != __last1)
3863 {
3864 __destruct_n __d(0);
3865 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
3866 value_type* __last2 = __first2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003867 ::new(__last2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003868 __d.__incr((value_type*)0);
3869 for (++__last2; ++__first1 != __last1; ++__last2)
3870 {
3871 value_type* __j2 = __last2;
3872 value_type* __i2 = __j2;
3873 if (__comp(*__first1, *--__i2))
3874 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003875 ::new(__j2) value_type(_VSTD::move(*__i2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003876 __d.__incr((value_type*)0);
3877 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003878 *__j2 = _VSTD::move(*__i2);
3879 *__j2 = _VSTD::move(*__first1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003880 }
3881 else
3882 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003883 ::new(__j2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003884 __d.__incr((value_type*)0);
3885 }
3886 }
3887 __h.release();
3888 }
3889}
3890
3891template <class _Compare, class _RandomAccessIterator>
3892void
3893__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3894{
3895 // _Compare is known to be a reference type
3896 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3897 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnanta9a897e2010-11-19 22:17:28 +00003898 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
3899 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003900 while (true)
3901 {
3902 __restart:
3903 difference_type __len = __last - __first;
3904 switch (__len)
3905 {
3906 case 0:
3907 case 1:
3908 return;
3909 case 2:
3910 if (__comp(*--__last, *__first))
3911 swap(*__first, *__last);
3912 return;
3913 case 3:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003914 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003915 return;
3916 case 4:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003917 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003918 return;
3919 case 5:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003920 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003921 return;
3922 }
3923 if (__len <= __limit)
3924 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003925 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003926 return;
3927 }
3928 // __len > 5
3929 _RandomAccessIterator __m = __first;
3930 _RandomAccessIterator __lm1 = __last;
3931 --__lm1;
3932 unsigned __n_swaps;
3933 {
3934 difference_type __delta;
3935 if (__len >= 1000)
3936 {
3937 __delta = __len/2;
3938 __m += __delta;
3939 __delta /= 2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003940 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003941 }
3942 else
3943 {
3944 __delta = __len/2;
3945 __m += __delta;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003946 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003947 }
3948 }
3949 // *__m is median
3950 // partition [__first, __m) < *__m and *__m <= [__m, __last)
3951 // (this inhibits tossing elements equivalent to __m around unnecessarily)
3952 _RandomAccessIterator __i = __first;
3953 _RandomAccessIterator __j = __lm1;
3954 // j points beyond range to be tested, *__m is known to be <= *__lm1
3955 // The search going up is known to be guarded but the search coming down isn't.
3956 // Prime the downward search with a guard.
3957 if (!__comp(*__i, *__m)) // if *__first == *__m
3958 {
3959 // *__first == *__m, *__first doesn't go in first part
3960 // manually guard downward moving __j against __i
3961 while (true)
3962 {
3963 if (__i == --__j)
3964 {
3965 // *__first == *__m, *__m <= all other elements
3966 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
3967 ++__i; // __first + 1
3968 __j = __last;
3969 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
3970 {
3971 while (true)
3972 {
3973 if (__i == __j)
3974 return; // [__first, __last) all equivalent elements
3975 if (__comp(*__first, *__i))
3976 {
3977 swap(*__i, *__j);
3978 ++__n_swaps;
3979 ++__i;
3980 break;
3981 }
3982 ++__i;
3983 }
3984 }
3985 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
3986 if (__i == __j)
3987 return;
3988 while (true)
3989 {
3990 while (!__comp(*__first, *__i))
3991 ++__i;
3992 while (__comp(*__first, *--__j))
3993 ;
3994 if (__i >= __j)
3995 break;
3996 swap(*__i, *__j);
3997 ++__n_swaps;
3998 ++__i;
3999 }
4000 // [__first, __i) == *__first and *__first < [__i, __last)
4001 // The first part is sorted, sort the secod part
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004002 // _VSTD::__sort<_Compare>(__i, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004003 __first = __i;
4004 goto __restart;
4005 }
4006 if (__comp(*__j, *__m))
4007 {
4008 swap(*__i, *__j);
4009 ++__n_swaps;
4010 break; // found guard for downward moving __j, now use unguarded partition
4011 }
4012 }
4013 }
4014 // It is known that *__i < *__m
4015 ++__i;
4016 // j points beyond range to be tested, *__m is known to be <= *__lm1
4017 // if not yet partitioned...
4018 if (__i < __j)
4019 {
4020 // known that *(__i - 1) < *__m
4021 // known that __i <= __m
4022 while (true)
4023 {
4024 // __m still guards upward moving __i
4025 while (__comp(*__i, *__m))
4026 ++__i;
4027 // It is now known that a guard exists for downward moving __j
4028 while (!__comp(*--__j, *__m))
4029 ;
4030 if (__i > __j)
4031 break;
4032 swap(*__i, *__j);
4033 ++__n_swaps;
4034 // It is known that __m != __j
4035 // If __m just moved, follow it
4036 if (__m == __i)
4037 __m = __j;
4038 ++__i;
4039 }
4040 }
4041 // [__first, __i) < *__m and *__m <= [__i, __last)
4042 if (__i != __m && __comp(*__m, *__i))
4043 {
4044 swap(*__i, *__m);
4045 ++__n_swaps;
4046 }
4047 // [__first, __i) < *__i and *__i <= [__i+1, __last)
4048 // If we were given a perfect partition, see if insertion sort is quick...
4049 if (__n_swaps == 0)
4050 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004051 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
4052 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004053 {
4054 if (__fs)
4055 return;
4056 __last = __i;
4057 continue;
4058 }
4059 else
4060 {
4061 if (__fs)
4062 {
4063 __first = ++__i;
4064 continue;
4065 }
4066 }
4067 }
4068 // sort smaller range with recursive call and larger with tail recursion elimination
4069 if (__i - __first < __last - __i)
4070 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004071 _VSTD::__sort<_Compare>(__first, __i, __comp);
4072 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004073 __first = ++__i;
4074 }
4075 else
4076 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004077 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4078 // _VSTD::__sort<_Compare>(__first, __i, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004079 __last = __i;
4080 }
4081 }
4082}
4083
4084// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare
4085template <class _RandomAccessIterator, class _Compare>
4086inline _LIBCPP_INLINE_VISIBILITY
4087void
4088sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4089{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00004090 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
4091 _VSTD::__sort<_Comp_ref>(__first, __last, _Comp_ref(__comp));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004092}
4093
4094template <class _RandomAccessIterator>
4095inline _LIBCPP_INLINE_VISIBILITY
4096void
4097sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4098{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004099 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004100}
4101
4102template <class _Tp>
4103inline _LIBCPP_INLINE_VISIBILITY
4104void
4105sort(_Tp** __first, _Tp** __last)
4106{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004107 _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004108}
4109
4110template <class _Tp>
4111inline _LIBCPP_INLINE_VISIBILITY
4112void
4113sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last)
4114{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004115 _VSTD::sort(__first.base(), __last.base());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004116}
4117
Howard Hinnant27e0e772011-09-14 18:33:51 +00004118template <class _Tp, class _Compare>
4119inline _LIBCPP_INLINE_VISIBILITY
4120void
4121sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp)
4122{
4123 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
4124 _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
4125}
4126
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004127_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4128_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4129_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4130_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4131_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4132_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4133_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4134_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4135_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4136_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4137_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4138_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>&))
4139_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4140_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4141_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 +00004142
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004143_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4144_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4145_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4146_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4147_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4148_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4149_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4150_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4151_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4152_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4153_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4154_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>&))
4155_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4156_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4157_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 +00004158
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004159_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 +00004160
4161// lower_bound
4162
4163template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004164_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004165__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004166{
4167 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004168 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169 while (__len != 0)
4170 {
Louis Dionnedda14512018-12-17 16:04:39 +00004171 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004172 _ForwardIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004173 _VSTD::advance(__m, __l2);
Howard Hinnantbf074022011-10-22 20:59:45 +00004174 if (__comp(*__m, __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004175 {
4176 __first = ++__m;
4177 __len -= __l2 + 1;
4178 }
4179 else
4180 __len = __l2;
4181 }
4182 return __first;
4183}
4184
4185template <class _ForwardIterator, class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00004186_LIBCPP_NODISCARD_EXT inline
4187_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004188_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004189lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004190{
Howard Hinnantc51e1022010-05-11 19:42:16 +00004191 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantbf074022011-10-22 20:59:45 +00004192 return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004193}
4194
4195template <class _ForwardIterator, class _Tp>
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_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004200{
Howard Hinnantbf074022011-10-22 20:59:45 +00004201 return _VSTD::lower_bound(__first, __last, __value_,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004202 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4203}
4204
4205// upper_bound
4206
4207template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004208_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004209__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004210{
4211 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004212 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004213 while (__len != 0)
4214 {
Louis Dionnedda14512018-12-17 16:04:39 +00004215 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004216 _ForwardIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004217 _VSTD::advance(__m, __l2);
Howard Hinnantbf074022011-10-22 20:59:45 +00004218 if (__comp(__value_, *__m))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004219 __len = __l2;
4220 else
4221 {
4222 __first = ++__m;
4223 __len -= __l2 + 1;
4224 }
4225 }
4226 return __first;
4227}
4228
4229template <class _ForwardIterator, class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00004230_LIBCPP_NODISCARD_EXT inline
4231_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004232_ForwardIterator
Howard Hinnantbf074022011-10-22 20:59:45 +00004233upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004234{
Howard Hinnantc51e1022010-05-11 19:42:16 +00004235 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Howard Hinnantbf074022011-10-22 20:59:45 +00004236 return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004237}
4238
4239template <class _ForwardIterator, class _Tp>
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_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004244{
Howard Hinnantbf074022011-10-22 20:59:45 +00004245 return _VSTD::upper_bound(__first, __last, __value_,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004246 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4247}
4248
4249// equal_range
4250
4251template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004252_LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_ForwardIterator, _ForwardIterator>
Howard Hinnantbf074022011-10-22 20:59:45 +00004253__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004254{
4255 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004256 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004257 while (__len != 0)
4258 {
Louis Dionnedda14512018-12-17 16:04:39 +00004259 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004260 _ForwardIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004261 _VSTD::advance(__m, __l2);
Howard Hinnantbf074022011-10-22 20:59:45 +00004262 if (__comp(*__m, __value_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004263 {
4264 __first = ++__m;
4265 __len -= __l2 + 1;
4266 }
Howard Hinnantbf074022011-10-22 20:59:45 +00004267 else if (__comp(__value_, *__m))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004268 {
4269 __last = __m;
4270 __len = __l2;
4271 }
4272 else
4273 {
4274 _ForwardIterator __mp1 = __m;
4275 return pair<_ForwardIterator, _ForwardIterator>
4276 (
Howard Hinnantbf074022011-10-22 20:59:45 +00004277 __lower_bound<_Compare>(__first, __m, __value_, __comp),
4278 __upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004279 );
4280 }
4281 }
4282 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4283}
4284
4285template <class _ForwardIterator, class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00004286_LIBCPP_NODISCARD_EXT inline
4287_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004288pair<_ForwardIterator, _ForwardIterator>
Howard Hinnantbf074022011-10-22 20:59:45 +00004289equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004290{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00004291 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantbf074022011-10-22 20:59:45 +00004292 return __equal_range<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004293}
4294
4295template <class _ForwardIterator, class _Tp>
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_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004300{
Howard Hinnantbf074022011-10-22 20:59:45 +00004301 return _VSTD::equal_range(__first, __last, __value_,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004302 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4303}
4304
4305// binary_search
4306
4307template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowe00916c2018-01-16 02:34:41 +00004308inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004309bool
Howard Hinnantbf074022011-10-22 20:59:45 +00004310__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004311{
Howard Hinnantbf074022011-10-22 20:59:45 +00004312 __first = __lower_bound<_Compare>(__first, __last, __value_, __comp);
4313 return __first != __last && !__comp(__value_, *__first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004314}
4315
4316template <class _ForwardIterator, class _Tp, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00004317_LIBCPP_NODISCARD_EXT inline
4318_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004319bool
Howard Hinnantbf074022011-10-22 20:59:45 +00004320binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004321{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00004322 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantbf074022011-10-22 20:59:45 +00004323 return __binary_search<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004324}
4325
4326template <class _ForwardIterator, class _Tp>
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_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004331{
Howard Hinnantbf074022011-10-22 20:59:45 +00004332 return _VSTD::binary_search(__first, __last, __value_,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004333 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4334}
4335
4336// merge
4337
4338template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4339_OutputIterator
4340__merge(_InputIterator1 __first1, _InputIterator1 __last1,
4341 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4342{
4343 for (; __first1 != __last1; ++__result)
4344 {
4345 if (__first2 == __last2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004346 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004347 if (__comp(*__first2, *__first1))
4348 {
4349 *__result = *__first2;
4350 ++__first2;
4351 }
4352 else
4353 {
4354 *__result = *__first1;
4355 ++__first1;
4356 }
4357 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004358 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004359}
4360
4361template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
4362inline _LIBCPP_INLINE_VISIBILITY
4363_OutputIterator
4364merge(_InputIterator1 __first1, _InputIterator1 __last1,
4365 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4366{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00004367 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004368 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004369}
4370
4371template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
4372inline _LIBCPP_INLINE_VISIBILITY
4373_OutputIterator
4374merge(_InputIterator1 __first1, _InputIterator1 __last1,
4375 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4376{
4377 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4378 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
4379 return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
4380}
4381
4382// inplace_merge
4383
Marshall Clow1bc51102015-07-29 16:25:45 +00004384template <class _Compare, class _InputIterator1, class _InputIterator2,
4385 class _OutputIterator>
4386void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4387 _InputIterator2 __first2, _InputIterator2 __last2,
4388 _OutputIterator __result, _Compare __comp)
4389{
4390 for (; __first1 != __last1; ++__result)
4391 {
4392 if (__first2 == __last2)
4393 {
4394 _VSTD::move(__first1, __last1, __result);
4395 return;
4396 }
4397
4398 if (__comp(*__first2, *__first1))
4399 {
4400 *__result = _VSTD::move(*__first2);
4401 ++__first2;
4402 }
4403 else
4404 {
4405 *__result = _VSTD::move(*__first1);
4406 ++__first1;
4407 }
4408 }
4409 // __first2 through __last2 are already in the right spot.
4410}
4411
Howard Hinnantc51e1022010-05-11 19:42:16 +00004412template <class _Compare, class _BidirectionalIterator>
4413void
4414__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4415 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4416 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4417 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4418{
4419 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004420 __destruct_n __d(0);
4421 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4422 if (__len1 <= __len2)
4423 {
4424 value_type* __p = __buff;
Eric Fiseliera09a3b42014-10-27 19:28:20 +00004425 for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004426 ::new(__p) value_type(_VSTD::move(*__i));
Marshall Clow1bc51102015-07-29 16:25:45 +00004427 __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004428 }
4429 else
4430 {
4431 value_type* __p = __buff;
Eric Fiseliera09a3b42014-10-27 19:28:20 +00004432 for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004433 ::new(__p) value_type(_VSTD::move(*__i));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004434 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4435 typedef reverse_iterator<value_type*> _Rv;
Aditya Kumar3a0179a2016-08-25 11:52:38 +00004436 __half_inplace_merge(_Rv(__p), _Rv(__buff),
Marshall Clow1bc51102015-07-29 16:25:45 +00004437 _RBi(__middle), _RBi(__first),
Marshall Clow738d1042017-08-28 23:16:13 +00004438 _RBi(__last), __invert<_Compare>(__comp));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004439 }
4440}
4441
4442template <class _Compare, class _BidirectionalIterator>
4443void
4444__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4445 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4446 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4447 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4448{
Howard Hinnantc51e1022010-05-11 19:42:16 +00004449 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4450 while (true)
4451 {
4452 // if __middle == __last, we're done
4453 if (__len2 == 0)
4454 return;
Marshall Clow8eff8232015-02-02 16:44:11 +00004455 if (__len1 <= __buff_size || __len2 <= __buff_size)
4456 return __buffered_inplace_merge<_Compare>
4457 (__first, __middle, __last, __comp, __len1, __len2, __buff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004458 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
Eric Fiseliera09a3b42014-10-27 19:28:20 +00004459 for (; true; ++__first, (void) --__len1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004460 {
4461 if (__len1 == 0)
4462 return;
4463 if (__comp(*__middle, *__first))
4464 break;
4465 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004466 // __first < __middle < __last
4467 // *__first > *__middle
4468 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4469 // all elements in:
4470 // [__first, __m1) <= [__middle, __m2)
4471 // [__middle, __m2) < [__m1, __middle)
4472 // [__m1, __middle) <= [__m2, __last)
4473 // and __m1 or __m2 is in the middle of its range
4474 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4475 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4476 difference_type __len11; // distance(__first, __m1)
4477 difference_type __len21; // distance(__middle, __m2)
4478 // binary search smaller range
4479 if (__len1 < __len2)
4480 { // __len >= 1, __len2 >= 2
4481 __len21 = __len2 / 2;
4482 __m2 = __middle;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004483 _VSTD::advance(__m2, __len21);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004484 __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004485 __len11 = _VSTD::distance(__first, __m1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004486 }
4487 else
4488 {
4489 if (__len1 == 1)
4490 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4491 // It is known *__first > *__middle
4492 swap(*__first, *__middle);
4493 return;
4494 }
4495 // __len1 >= 2, __len2 >= 1
4496 __len11 = __len1 / 2;
4497 __m1 = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004498 _VSTD::advance(__m1, __len11);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004499 __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004500 __len21 = _VSTD::distance(__middle, __m2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004501 }
4502 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4503 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4504 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4505 // swap middle two partitions
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004506 __middle = _VSTD::rotate(__m1, __middle, __m2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004507 // __len12 and __len21 now have swapped meanings
4508 // merge smaller range with recurisve call and larger with tail recursion elimination
4509 if (__len11 + __len21 < __len12 + __len22)
4510 {
4511 __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4512// __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4513 __first = __middle;
4514 __middle = __m2;
4515 __len1 = __len12;
4516 __len2 = __len22;
4517 }
4518 else
4519 {
4520 __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4521// __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4522 __last = __middle;
4523 __middle = __m1;
4524 __len1 = __len11;
4525 __len2 = __len21;
4526 }
4527 }
4528}
4529
Howard Hinnantc51e1022010-05-11 19:42:16 +00004530template <class _BidirectionalIterator, class _Compare>
4531inline _LIBCPP_INLINE_VISIBILITY
4532void
4533inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4534 _Compare __comp)
4535{
4536 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4537 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004538 difference_type __len1 = _VSTD::distance(__first, __middle);
4539 difference_type __len2 = _VSTD::distance(__middle, __last);
4540 difference_type __buf_size = _VSTD::min(__len1, __len2);
Marshall Clow488f19f2015-02-02 17:35:53 +00004541 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4542 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00004543 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004544 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004545 __buf.first, __buf.second);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004546}
4547
4548template <class _BidirectionalIterator>
4549inline _LIBCPP_INLINE_VISIBILITY
4550void
4551inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4552{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004553 _VSTD::inplace_merge(__first, __middle, __last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00004554 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4555}
4556
4557// stable_sort
4558
4559template <class _Compare, class _InputIterator1, class _InputIterator2>
4560void
4561__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4562 _InputIterator2 __first2, _InputIterator2 __last2,
4563 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4564{
4565 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4566 __destruct_n __d(0);
4567 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4568 for (; true; ++__result)
4569 {
4570 if (__first1 == __last1)
4571 {
4572 for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004573 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004574 __h.release();
4575 return;
4576 }
4577 if (__first2 == __last2)
4578 {
4579 for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004580 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004581 __h.release();
4582 return;
4583 }
4584 if (__comp(*__first2, *__first1))
4585 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004586 ::new (__result) value_type(_VSTD::move(*__first2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004587 __d.__incr((value_type*)0);
4588 ++__first2;
4589 }
4590 else
4591 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004592 ::new (__result) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004593 __d.__incr((value_type*)0);
4594 ++__first1;
4595 }
4596 }
4597}
4598
4599template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4600void
4601__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4602 _InputIterator2 __first2, _InputIterator2 __last2,
4603 _OutputIterator __result, _Compare __comp)
4604{
4605 for (; __first1 != __last1; ++__result)
4606 {
4607 if (__first2 == __last2)
4608 {
4609 for (; __first1 != __last1; ++__first1, ++__result)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004610 *__result = _VSTD::move(*__first1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004611 return;
4612 }
4613 if (__comp(*__first2, *__first1))
4614 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004615 *__result = _VSTD::move(*__first2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004616 ++__first2;
4617 }
4618 else
4619 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004620 *__result = _VSTD::move(*__first1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004621 ++__first1;
4622 }
4623 }
4624 for (; __first2 != __last2; ++__first2, ++__result)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004625 *__result = _VSTD::move(*__first2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004626}
4627
4628template <class _Compare, class _RandomAccessIterator>
4629void
4630__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4631 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4632 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4633
4634template <class _Compare, class _RandomAccessIterator>
4635void
4636__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4637 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4638 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4639{
4640 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4641 switch (__len)
4642 {
4643 case 0:
4644 return;
4645 case 1:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004646 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004647 return;
4648 case 2:
Marshall Clow32043ac2018-02-06 18:58:05 +00004649 __destruct_n __d(0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004650 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
Marshall Clow32043ac2018-02-06 18:58:05 +00004651 if (__comp(*--__last1, *__first1))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004652 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004653 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004654 __d.__incr((value_type*)0);
4655 ++__first2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004656 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004657 }
4658 else
4659 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004660 ::new(__first2) value_type(_VSTD::move(*__first1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004661 __d.__incr((value_type*)0);
4662 ++__first2;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004663 ::new(__first2) value_type(_VSTD::move(*__last1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004664 }
4665 __h2.release();
4666 return;
4667 }
4668 if (__len <= 8)
4669 {
4670 __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
4671 return;
4672 }
4673 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4674 _RandomAccessIterator __m = __first1 + __l2;
4675 __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4676 __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4677 __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
4678}
4679
4680template <class _Tp>
4681struct __stable_sort_switch
4682{
Howard Hinnanta9a897e2010-11-19 22:17:28 +00004683 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004684};
4685
4686template <class _Compare, class _RandomAccessIterator>
4687void
4688__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4689 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4690 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4691{
4692 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4693 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4694 switch (__len)
4695 {
4696 case 0:
4697 case 1:
4698 return;
4699 case 2:
4700 if (__comp(*--__last, *__first))
4701 swap(*__first, *__last);
4702 return;
4703 }
4704 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4705 {
4706 __insertion_sort<_Compare>(__first, __last, __comp);
4707 return;
4708 }
4709 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4710 _RandomAccessIterator __m = __first + __l2;
4711 if (__len <= __buff_size)
4712 {
4713 __destruct_n __d(0);
4714 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4715 __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
4716 __d.__set(__l2, (value_type*)0);
4717 __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
4718 __d.__set(__len, (value_type*)0);
4719 __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4720// __merge<_Compare>(move_iterator<value_type*>(__buff),
4721// move_iterator<value_type*>(__buff + __l2),
4722// move_iterator<_RandomAccessIterator>(__buff + __l2),
4723// move_iterator<_RandomAccessIterator>(__buff + __len),
4724// __first, __comp);
4725 return;
4726 }
4727 __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4728 __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4729 __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
4730}
4731
4732template <class _RandomAccessIterator, class _Compare>
4733inline _LIBCPP_INLINE_VISIBILITY
4734void
4735stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4736{
4737 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4738 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4739 difference_type __len = __last - __first;
4740 pair<value_type*, ptrdiff_t> __buf(0, 0);
4741 unique_ptr<value_type, __return_temporary_buffer> __h;
4742 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4743 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004744 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004745 __h.reset(__buf.first);
4746 }
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00004747 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004748 __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004749}
4750
4751template <class _RandomAccessIterator>
4752inline _LIBCPP_INLINE_VISIBILITY
4753void
4754stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4755{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004756 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004757}
4758
4759// is_heap_until
4760
4761template <class _RandomAccessIterator, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00004762_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00004763is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4764{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004765 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004766 difference_type __len = __last - __first;
4767 difference_type __p = 0;
4768 difference_type __c = 1;
4769 _RandomAccessIterator __pp = __first;
4770 while (__c < __len)
4771 {
4772 _RandomAccessIterator __cp = __first + __c;
4773 if (__comp(*__pp, *__cp))
4774 return __cp;
4775 ++__c;
4776 ++__cp;
4777 if (__c == __len)
4778 return __last;
4779 if (__comp(*__pp, *__cp))
4780 return __cp;
4781 ++__p;
4782 ++__pp;
4783 __c = 2 * __p + 1;
4784 }
4785 return __last;
4786}
4787
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004788template<class _RandomAccessIterator>
Nico Weber471b10a2019-04-03 18:13:08 +00004789_LIBCPP_NODISCARD_EXT inline
4790_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004791_RandomAccessIterator
4792is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4793{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004794 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004795}
4796
4797// is_heap
4798
4799template <class _RandomAccessIterator, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00004800_LIBCPP_NODISCARD_EXT inline
4801_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004802bool
4803is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4804{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004805 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004806}
4807
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004808template<class _RandomAccessIterator>
Nico Weber471b10a2019-04-03 18:13:08 +00004809_LIBCPP_NODISCARD_EXT inline
4810_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00004811bool
4812is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4813{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004814 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004815}
4816
4817// push_heap
4818
4819template <class _Compare, class _RandomAccessIterator>
4820void
David Majnemer4468d562014-07-22 06:07:09 +00004821__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4822 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004823{
Howard Hinnantc51e1022010-05-11 19:42:16 +00004824 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4825 if (__len > 1)
4826 {
4827 __len = (__len - 2) / 2;
4828 _RandomAccessIterator __ptr = __first + __len;
4829 if (__comp(*__ptr, *--__last))
4830 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004831 value_type __t(_VSTD::move(*__last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004832 do
4833 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004834 *__last = _VSTD::move(*__ptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004835 __last = __ptr;
4836 if (__len == 0)
4837 break;
4838 __len = (__len - 1) / 2;
4839 __ptr = __first + __len;
4840 } while (__comp(*__ptr, __t));
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004841 *__last = _VSTD::move(__t);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004842 }
4843 }
4844}
4845
4846template <class _RandomAccessIterator, class _Compare>
4847inline _LIBCPP_INLINE_VISIBILITY
4848void
4849push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4850{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00004851 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
David Majnemer4468d562014-07-22 06:07:09 +00004852 __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004853}
4854
4855template <class _RandomAccessIterator>
4856inline _LIBCPP_INLINE_VISIBILITY
4857void
4858push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4859{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004860 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004861}
4862
4863// pop_heap
4864
4865template <class _Compare, class _RandomAccessIterator>
David Majnemer4468d562014-07-22 06:07:09 +00004866void
Eric Fiselier6003c772016-12-23 23:37:52 +00004867__sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
4868 _Compare __comp,
David Majnemer4468d562014-07-22 06:07:09 +00004869 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4870 _RandomAccessIterator __start)
4871{
4872 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4873 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4874 // left-child of __start is at 2 * __start + 1
4875 // right-child of __start is at 2 * __start + 2
4876 difference_type __child = __start - __first;
4877
4878 if (__len < 2 || (__len - 2) / 2 < __child)
4879 return;
4880
4881 __child = 2 * __child + 1;
4882 _RandomAccessIterator __child_i = __first + __child;
4883
4884 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4885 // right-child exists and is greater than left-child
4886 ++__child_i;
4887 ++__child;
4888 }
4889
4890 // check if we are in heap-order
4891 if (__comp(*__child_i, *__start))
4892 // we are, __start is larger than it's largest child
4893 return;
4894
4895 value_type __top(_VSTD::move(*__start));
4896 do
4897 {
4898 // we are not in heap-order, swap the parent with it's largest child
4899 *__start = _VSTD::move(*__child_i);
4900 __start = __child_i;
4901
4902 if ((__len - 2) / 2 < __child)
4903 break;
4904
4905 // recompute the child based off of the updated parent
4906 __child = 2 * __child + 1;
4907 __child_i = __first + __child;
4908
4909 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
4910 // right-child exists and is greater than left-child
4911 ++__child_i;
4912 ++__child;
4913 }
4914
4915 // check if we are in heap-order
4916 } while (!__comp(*__child_i, __top));
4917 *__start = _VSTD::move(__top);
4918}
4919
4920template <class _Compare, class _RandomAccessIterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004921inline _LIBCPP_INLINE_VISIBILITY
4922void
4923__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4924 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
4925{
4926 if (__len > 1)
4927 {
4928 swap(*__first, *--__last);
David Majnemer4468d562014-07-22 06:07:09 +00004929 __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004930 }
4931}
4932
4933template <class _RandomAccessIterator, class _Compare>
4934inline _LIBCPP_INLINE_VISIBILITY
4935void
4936pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4937{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00004938 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004939 __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004940}
4941
4942template <class _RandomAccessIterator>
4943inline _LIBCPP_INLINE_VISIBILITY
4944void
4945pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4946{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004947 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004948}
4949
4950// make_heap
4951
4952template <class _Compare, class _RandomAccessIterator>
4953void
4954__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4955{
4956 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4957 difference_type __n = __last - __first;
4958 if (__n > 1)
4959 {
David Majnemer4468d562014-07-22 06:07:09 +00004960 // start from the first parent, there is no need to consider children
4961 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
4962 {
4963 __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
4964 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004965 }
4966}
4967
4968template <class _RandomAccessIterator, class _Compare>
4969inline _LIBCPP_INLINE_VISIBILITY
4970void
4971make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4972{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00004973 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004974 __make_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004975}
4976
4977template <class _RandomAccessIterator>
4978inline _LIBCPP_INLINE_VISIBILITY
4979void
4980make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4981{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004982 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004983}
4984
4985// sort_heap
4986
4987template <class _Compare, class _RandomAccessIterator>
4988void
4989__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4990{
4991 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4992 for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
4993 __pop_heap<_Compare>(__first, __last, __comp, __n);
4994}
4995
4996template <class _RandomAccessIterator, class _Compare>
4997inline _LIBCPP_INLINE_VISIBILITY
4998void
4999sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5000{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005001 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005002 __sort_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005003}
5004
5005template <class _RandomAccessIterator>
5006inline _LIBCPP_INLINE_VISIBILITY
5007void
5008sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5009{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005010 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00005011}
5012
5013// partial_sort
5014
5015template <class _Compare, class _RandomAccessIterator>
5016void
5017__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5018 _Compare __comp)
5019{
5020 __make_heap<_Compare>(__first, __middle, __comp);
5021 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5022 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5023 {
5024 if (__comp(*__i, *__first))
5025 {
5026 swap(*__i, *__first);
David Majnemer4468d562014-07-22 06:07:09 +00005027 __sift_down<_Compare>(__first, __middle, __comp, __len, __first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005028 }
5029 }
5030 __sort_heap<_Compare>(__first, __middle, __comp);
5031}
5032
5033template <class _RandomAccessIterator, class _Compare>
5034inline _LIBCPP_INLINE_VISIBILITY
5035void
5036partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5037 _Compare __comp)
5038{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005039 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005040 __partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005041}
5042
5043template <class _RandomAccessIterator>
5044inline _LIBCPP_INLINE_VISIBILITY
5045void
5046partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5047{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005048 _VSTD::partial_sort(__first, __middle, __last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005049 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5050}
5051
5052// partial_sort_copy
5053
5054template <class _Compare, class _InputIterator, class _RandomAccessIterator>
5055_RandomAccessIterator
5056__partial_sort_copy(_InputIterator __first, _InputIterator __last,
5057 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5058{
5059 _RandomAccessIterator __r = __result_first;
5060 if (__r != __result_last)
5061 {
Eric Fiseliera09a3b42014-10-27 19:28:20 +00005062 for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005063 *__r = *__first;
5064 __make_heap<_Compare>(__result_first, __r, __comp);
David Majnemer4468d562014-07-22 06:07:09 +00005065 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005066 for (; __first != __last; ++__first)
5067 if (__comp(*__first, *__result_first))
5068 {
5069 *__result_first = *__first;
David Majnemer4468d562014-07-22 06:07:09 +00005070 __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005071 }
5072 __sort_heap<_Compare>(__result_first, __r, __comp);
5073 }
5074 return __r;
5075}
5076
5077template <class _InputIterator, class _RandomAccessIterator, class _Compare>
5078inline _LIBCPP_INLINE_VISIBILITY
5079_RandomAccessIterator
5080partial_sort_copy(_InputIterator __first, _InputIterator __last,
5081 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5082{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005083 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005084 return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005085}
5086
5087template <class _InputIterator, class _RandomAccessIterator>
5088inline _LIBCPP_INLINE_VISIBILITY
5089_RandomAccessIterator
5090partial_sort_copy(_InputIterator __first, _InputIterator __last,
5091 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5092{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005093 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005094 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5095}
5096
5097// nth_element
5098
5099template <class _Compare, class _RandomAccessIterator>
5100void
5101__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5102{
5103 // _Compare is known to be a reference type
5104 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5105 const difference_type __limit = 7;
5106 while (true)
5107 {
5108 __restart:
Howard Hinnant2fa038c2011-12-29 17:45:35 +00005109 if (__nth == __last)
5110 return;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005111 difference_type __len = __last - __first;
5112 switch (__len)
5113 {
5114 case 0:
5115 case 1:
5116 return;
5117 case 2:
5118 if (__comp(*--__last, *__first))
5119 swap(*__first, *__last);
5120 return;
5121 case 3:
5122 {
5123 _RandomAccessIterator __m = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005124 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005125 return;
5126 }
5127 }
5128 if (__len <= __limit)
5129 {
5130 __selection_sort<_Compare>(__first, __last, __comp);
5131 return;
5132 }
5133 // __len > __limit >= 3
5134 _RandomAccessIterator __m = __first + __len/2;
5135 _RandomAccessIterator __lm1 = __last;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005136 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005137 // *__m is median
5138 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5139 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5140 _RandomAccessIterator __i = __first;
5141 _RandomAccessIterator __j = __lm1;
5142 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5143 // The search going up is known to be guarded but the search coming down isn't.
5144 // Prime the downward search with a guard.
5145 if (!__comp(*__i, *__m)) // if *__first == *__m
5146 {
5147 // *__first == *__m, *__first doesn't go in first part
5148 // manually guard downward moving __j against __i
5149 while (true)
5150 {
5151 if (__i == --__j)
5152 {
5153 // *__first == *__m, *__m <= all other elements
5154 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
5155 ++__i; // __first + 1
5156 __j = __last;
5157 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5158 {
5159 while (true)
5160 {
5161 if (__i == __j)
5162 return; // [__first, __last) all equivalent elements
5163 if (__comp(*__first, *__i))
5164 {
5165 swap(*__i, *__j);
5166 ++__n_swaps;
5167 ++__i;
5168 break;
5169 }
5170 ++__i;
5171 }
5172 }
5173 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5174 if (__i == __j)
5175 return;
5176 while (true)
5177 {
5178 while (!__comp(*__first, *__i))
5179 ++__i;
5180 while (__comp(*__first, *--__j))
5181 ;
5182 if (__i >= __j)
5183 break;
5184 swap(*__i, *__j);
5185 ++__n_swaps;
5186 ++__i;
5187 }
5188 // [__first, __i) == *__first and *__first < [__i, __last)
5189 // The first part is sorted,
5190 if (__nth < __i)
5191 return;
5192 // __nth_element the secod part
5193 // __nth_element<_Compare>(__i, __nth, __last, __comp);
5194 __first = __i;
5195 goto __restart;
5196 }
5197 if (__comp(*__j, *__m))
5198 {
5199 swap(*__i, *__j);
5200 ++__n_swaps;
5201 break; // found guard for downward moving __j, now use unguarded partition
5202 }
5203 }
5204 }
5205 ++__i;
5206 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5207 // if not yet partitioned...
5208 if (__i < __j)
5209 {
5210 // known that *(__i - 1) < *__m
5211 while (true)
5212 {
5213 // __m still guards upward moving __i
5214 while (__comp(*__i, *__m))
5215 ++__i;
5216 // It is now known that a guard exists for downward moving __j
5217 while (!__comp(*--__j, *__m))
5218 ;
5219 if (__i >= __j)
5220 break;
5221 swap(*__i, *__j);
5222 ++__n_swaps;
5223 // It is known that __m != __j
5224 // If __m just moved, follow it
5225 if (__m == __i)
5226 __m = __j;
5227 ++__i;
5228 }
5229 }
5230 // [__first, __i) < *__m and *__m <= [__i, __last)
5231 if (__i != __m && __comp(*__m, *__i))
5232 {
5233 swap(*__i, *__m);
5234 ++__n_swaps;
5235 }
5236 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5237 if (__nth == __i)
5238 return;
5239 if (__n_swaps == 0)
5240 {
5241 // We were given a perfectly partitioned sequence. Coincidence?
5242 if (__nth < __i)
5243 {
5244 // Check for [__first, __i) already sorted
5245 __j = __m = __first;
5246 while (++__j != __i)
5247 {
5248 if (__comp(*__j, *__m))
5249 // not yet sorted, so sort
5250 goto not_sorted;
5251 __m = __j;
5252 }
5253 // [__first, __i) sorted
5254 return;
5255 }
5256 else
5257 {
5258 // Check for [__i, __last) already sorted
5259 __j = __m = __i;
5260 while (++__j != __last)
5261 {
5262 if (__comp(*__j, *__m))
5263 // not yet sorted, so sort
5264 goto not_sorted;
5265 __m = __j;
5266 }
5267 // [__i, __last) sorted
5268 return;
5269 }
5270 }
5271not_sorted:
5272 // __nth_element on range containing __nth
5273 if (__nth < __i)
5274 {
5275 // __nth_element<_Compare>(__first, __nth, __i, __comp);
5276 __last = __i;
5277 }
5278 else
5279 {
5280 // __nth_element<_Compare>(__i+1, __nth, __last, __comp);
5281 __first = ++__i;
5282 }
5283 }
5284}
5285
5286template <class _RandomAccessIterator, class _Compare>
5287inline _LIBCPP_INLINE_VISIBILITY
5288void
5289nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5290{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005291 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005292 __nth_element<_Comp_ref>(__first, __nth, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005293}
5294
5295template <class _RandomAccessIterator>
5296inline _LIBCPP_INLINE_VISIBILITY
5297void
5298nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5299{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005300 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnantc51e1022010-05-11 19:42:16 +00005301}
5302
5303// includes
5304
5305template <class _Compare, class _InputIterator1, class _InputIterator2>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005306_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00005307__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5308 _Compare __comp)
5309{
5310 for (; __first2 != __last2; ++__first1)
5311 {
5312 if (__first1 == __last1 || __comp(*__first2, *__first1))
5313 return false;
5314 if (!__comp(*__first1, *__first2))
5315 ++__first2;
5316 }
5317 return true;
5318}
5319
5320template <class _InputIterator1, class _InputIterator2, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00005321_LIBCPP_NODISCARD_EXT inline
5322_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005323bool
5324includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5325 _Compare __comp)
5326{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005327 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005328 return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005329}
5330
5331template <class _InputIterator1, class _InputIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00005332_LIBCPP_NODISCARD_EXT inline
5333_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005334bool
5335includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5336{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005337 return _VSTD::includes(__first1, __last1, __first2, __last2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005338 __less<typename iterator_traits<_InputIterator1>::value_type,
5339 typename iterator_traits<_InputIterator2>::value_type>());
5340}
5341
5342// set_union
5343
5344template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5345_OutputIterator
5346__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5347 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5348{
5349 for (; __first1 != __last1; ++__result)
5350 {
5351 if (__first2 == __last2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005352 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005353 if (__comp(*__first2, *__first1))
5354 {
5355 *__result = *__first2;
5356 ++__first2;
5357 }
5358 else
5359 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00005360 if (!__comp(*__first1, *__first2))
5361 ++__first2;
Marshall Clowb4687412017-10-30 15:50:00 +00005362 *__result = *__first1;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005363 ++__first1;
5364 }
5365 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005366 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005367}
5368
5369template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5370inline _LIBCPP_INLINE_VISIBILITY
5371_OutputIterator
5372set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5373 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5374{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005375 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005376 return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005377}
5378
5379template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5380inline _LIBCPP_INLINE_VISIBILITY
5381_OutputIterator
5382set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5383 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5384{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005385 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005386 __less<typename iterator_traits<_InputIterator1>::value_type,
5387 typename iterator_traits<_InputIterator2>::value_type>());
5388}
5389
5390// set_intersection
5391
5392template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005393_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00005394__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5395 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5396{
5397 while (__first1 != __last1 && __first2 != __last2)
5398 {
5399 if (__comp(*__first1, *__first2))
5400 ++__first1;
5401 else
5402 {
5403 if (!__comp(*__first2, *__first1))
5404 {
5405 *__result = *__first1;
5406 ++__result;
5407 ++__first1;
5408 }
5409 ++__first2;
5410 }
5411 }
5412 return __result;
5413}
5414
5415template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
Marshall Clowc0b7f972018-01-22 23:10:40 +00005416inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005417_OutputIterator
5418set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5419 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5420{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005421 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005422 return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005423}
5424
5425template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
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)
5430{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005431 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005432 __less<typename iterator_traits<_InputIterator1>::value_type,
5433 typename iterator_traits<_InputIterator2>::value_type>());
5434}
5435
5436// set_difference
5437
5438template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5439_OutputIterator
5440__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5441 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5442{
5443 while (__first1 != __last1)
5444 {
5445 if (__first2 == __last2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005446 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005447 if (__comp(*__first1, *__first2))
5448 {
5449 *__result = *__first1;
5450 ++__result;
5451 ++__first1;
5452 }
5453 else
5454 {
5455 if (!__comp(*__first2, *__first1))
5456 ++__first1;
5457 ++__first2;
5458 }
5459 }
5460 return __result;
5461}
5462
5463template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5464inline _LIBCPP_INLINE_VISIBILITY
5465_OutputIterator
5466set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5467 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5468{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005469 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005470 return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005471}
5472
5473template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5474inline _LIBCPP_INLINE_VISIBILITY
5475_OutputIterator
5476set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5477 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5478{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005479 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005480 __less<typename iterator_traits<_InputIterator1>::value_type,
5481 typename iterator_traits<_InputIterator2>::value_type>());
5482}
5483
5484// set_symmetric_difference
5485
5486template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
5487_OutputIterator
5488__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5489 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5490{
5491 while (__first1 != __last1)
5492 {
5493 if (__first2 == __last2)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005494 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005495 if (__comp(*__first1, *__first2))
5496 {
5497 *__result = *__first1;
5498 ++__result;
5499 ++__first1;
5500 }
5501 else
5502 {
5503 if (__comp(*__first2, *__first1))
5504 {
5505 *__result = *__first2;
5506 ++__result;
5507 }
5508 else
5509 ++__first1;
5510 ++__first2;
5511 }
5512 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005513 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005514}
5515
5516template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
5517inline _LIBCPP_INLINE_VISIBILITY
5518_OutputIterator
5519set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5520 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5521{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005522 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005523 return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005524}
5525
5526template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
5527inline _LIBCPP_INLINE_VISIBILITY
5528_OutputIterator
5529set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5530 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5531{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005532 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005533 __less<typename iterator_traits<_InputIterator1>::value_type,
5534 typename iterator_traits<_InputIterator2>::value_type>());
5535}
5536
5537// lexicographical_compare
5538
5539template <class _Compare, class _InputIterator1, class _InputIterator2>
Marshall Clow5492c8a2018-01-22 20:44:33 +00005540_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00005541__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5542 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5543{
Eric Fiseliera09a3b42014-10-27 19:28:20 +00005544 for (; __first2 != __last2; ++__first1, (void) ++__first2)
Howard Hinnantc51e1022010-05-11 19:42:16 +00005545 {
5546 if (__first1 == __last1 || __comp(*__first1, *__first2))
5547 return true;
5548 if (__comp(*__first2, *__first1))
5549 return false;
5550 }
5551 return false;
5552}
5553
5554template <class _InputIterator1, class _InputIterator2, class _Compare>
Nico Weber471b10a2019-04-03 18:13:08 +00005555_LIBCPP_NODISCARD_EXT inline
5556_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005557bool
5558lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5559 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5560{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005561 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005562 return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005563}
5564
5565template <class _InputIterator1, class _InputIterator2>
Nico Weber471b10a2019-04-03 18:13:08 +00005566_LIBCPP_NODISCARD_EXT inline
5567_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00005568bool
5569lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5570 _InputIterator2 __first2, _InputIterator2 __last2)
5571{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005572 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005573 __less<typename iterator_traits<_InputIterator1>::value_type,
5574 typename iterator_traits<_InputIterator2>::value_type>());
5575}
5576
5577// next_permutation
5578
5579template <class _Compare, class _BidirectionalIterator>
5580bool
5581__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5582{
5583 _BidirectionalIterator __i = __last;
5584 if (__first == __last || __first == --__i)
5585 return false;
5586 while (true)
5587 {
5588 _BidirectionalIterator __ip1 = __i;
5589 if (__comp(*--__i, *__ip1))
5590 {
5591 _BidirectionalIterator __j = __last;
5592 while (!__comp(*__i, *--__j))
5593 ;
5594 swap(*__i, *__j);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005595 _VSTD::reverse(__ip1, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005596 return true;
5597 }
5598 if (__i == __first)
5599 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005600 _VSTD::reverse(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005601 return false;
5602 }
5603 }
5604}
5605
5606template <class _BidirectionalIterator, class _Compare>
5607inline _LIBCPP_INLINE_VISIBILITY
5608bool
5609next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5610{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005611 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005612 return __next_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005613}
5614
5615template <class _BidirectionalIterator>
5616inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3b6579a2010-08-22 00:02:43 +00005617bool
Howard Hinnantc51e1022010-05-11 19:42:16 +00005618next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5619{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005620 return _VSTD::next_permutation(__first, __last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005621 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5622}
5623
5624// prev_permutation
5625
5626template <class _Compare, class _BidirectionalIterator>
5627bool
5628__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5629{
5630 _BidirectionalIterator __i = __last;
5631 if (__first == __last || __first == --__i)
5632 return false;
5633 while (true)
5634 {
5635 _BidirectionalIterator __ip1 = __i;
5636 if (__comp(*__ip1, *--__i))
5637 {
5638 _BidirectionalIterator __j = __last;
5639 while (!__comp(*--__j, *__i))
5640 ;
5641 swap(*__i, *__j);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005642 _VSTD::reverse(__ip1, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005643 return true;
5644 }
5645 if (__i == __first)
5646 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005647 _VSTD::reverse(__first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005648 return false;
5649 }
5650 }
5651}
5652
5653template <class _BidirectionalIterator, class _Compare>
5654inline _LIBCPP_INLINE_VISIBILITY
5655bool
5656prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5657{
Eric Fiselier7ceab7b2019-04-12 05:18:19 +00005658 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantc51e1022010-05-11 19:42:16 +00005659 return __prev_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00005660}
5661
5662template <class _BidirectionalIterator>
5663inline _LIBCPP_INLINE_VISIBILITY
5664bool
5665prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5666{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005667 return _VSTD::prev_permutation(__first, __last,
Howard Hinnantc51e1022010-05-11 19:42:16 +00005668 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5669}
5670
Howard Hinnantc51e1022010-05-11 19:42:16 +00005671_LIBCPP_END_NAMESPACE_STD
5672
Eric Fiselierf4433a32017-05-31 22:07:49 +00005673_LIBCPP_POP_MACROS
5674
Howard Hinnantc51e1022010-05-11 19:42:16 +00005675#endif // _LIBCPP_ALGORITHM