blob: 1ae84f7dff7fb29f8817c32852c7c1f8e48c93d0 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:161// -*- C++ -*-
2//===-------------------------- algorithm ---------------------------------===//
3//
Chandler Carruth57b08b02019-01-19 10:56:404// 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 Hinnant3e519522010-05-11 19:42:167//
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 Clow706ffef2018-01-15 17:20:3622 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1623 all_of(InputIterator first, InputIterator last, Predicate pred);
24
25template <class InputIterator, class Predicate>
Marshall Clow706ffef2018-01-15 17:20:3626 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1627 any_of(InputIterator first, InputIterator last, Predicate pred);
28
29template <class InputIterator, class Predicate>
Marshall Clow706ffef2018-01-15 17:20:3630 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1631 none_of(InputIterator first, InputIterator last, Predicate pred);
32
33template <class InputIterator, class Function>
Marshall Clow1b9a4ff2018-01-22 20:44:3334 constexpr Function // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1635 for_each(InputIterator first, InputIterator last, Function f);
36
Marshall Clowd5c65ff2017-05-25 02:29:5437template<class InputIterator, class Size, class Function>
Marshall Clow1b9a4ff2018-01-22 20:44:3338 constexpr InputIterator // constexpr in C++20
39 for_each_n(InputIterator first, Size n, Function f); // C++17
Marshall Clowd5c65ff2017-05-25 02:29:5440
Howard Hinnant3e519522010-05-11 19:42:1641template <class InputIterator, class T>
Marshall Clow86944282018-01-15 19:26:0542 constexpr InputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1643 find(InputIterator first, InputIterator last, const T& value);
44
45template <class InputIterator, class Predicate>
Marshall Clow86944282018-01-15 19:26:0546 constexpr InputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1647 find_if(InputIterator first, InputIterator last, Predicate pred);
48
49template<class InputIterator, class Predicate>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:1850 constexpr InputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1651 find_if_not(InputIterator first, InputIterator last, Predicate pred);
52
53template <class ForwardIterator1, class ForwardIterator2>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:1854 constexpr ForwardIterator1 // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1655 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
56 ForwardIterator2 first2, ForwardIterator2 last2);
57
58template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:1859 constexpr ForwardIterator1 // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1660 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
61 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
62
63template <class ForwardIterator1, class ForwardIterator2>
Marshall Clow86944282018-01-15 19:26:0564 constexpr ForwardIterator1 // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1665 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
66 ForwardIterator2 first2, ForwardIterator2 last2);
67
68template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clow86944282018-01-15 19:26:0569 constexpr ForwardIterator1 // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1670 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1,
71 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
72
73template <class ForwardIterator>
Marshall Clow86944282018-01-15 19:26:0574 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1675 adjacent_find(ForwardIterator first, ForwardIterator last);
76
77template <class ForwardIterator, class BinaryPredicate>
Marshall Clow86944282018-01-15 19:26:0578 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1679 adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
80
81template <class InputIterator, class T>
Marshall Clow056f15e2018-01-15 19:40:3482 constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1683 count(InputIterator first, InputIterator last, const T& value);
84
85template <class InputIterator, class Predicate>
Marshall Clow056f15e2018-01-15 19:40:3486 constexpr typename iterator_traits<InputIterator>::difference_type // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1687 count_if(InputIterator first, InputIterator last, Predicate pred);
88
89template <class InputIterator1, class InputIterator2>
Marshall Clow6538e28d2018-01-16 02:04:1090 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:1691 mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
92
Marshall Clow0b0bbd22013-05-09 21:14:2393template <class InputIterator1, class InputIterator2>
Marshall Clow6538e28d2018-01-16 02:04:1094 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
Aditya Kumar331fb802016-08-25 11:52:3895 mismatch(InputIterator1 first1, InputIterator1 last1,
Marshall Clow0b0bbd22013-05-09 21:14:2396 InputIterator2 first2, InputIterator2 last2); // **C++14**
97
Howard Hinnant3e519522010-05-11 19:42:1698template <class InputIterator1, class InputIterator2, class BinaryPredicate>
Marshall Clow6538e28d2018-01-16 02:04:1099 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16100 mismatch(InputIterator1 first1, InputIterator1 last1,
101 InputIterator2 first2, BinaryPredicate pred);
102
Marshall Clow0b0bbd22013-05-09 21:14:23103template <class InputIterator1, class InputIterator2, class BinaryPredicate>
Marshall Clow6538e28d2018-01-16 02:04:10104 constexpr pair<InputIterator1, InputIterator2> // constexpr in C++20
Marshall Clow0b0bbd22013-05-09 21:14:23105 mismatch(InputIterator1 first1, InputIterator1 last1,
106 InputIterator2 first2, InputIterator2 last2,
107 BinaryPredicate pred); // **C++14**
108
Howard Hinnant3e519522010-05-11 19:42:16109template <class InputIterator1, class InputIterator2>
Marshall Clow6538e28d2018-01-16 02:04:10110 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16111 equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
112
Marshall Clow0b0bbd22013-05-09 21:14:23113template <class InputIterator1, class InputIterator2>
Marshall Clow6538e28d2018-01-16 02:04:10114 constexpr bool // constexpr in C++20
Aditya Kumar331fb802016-08-25 11:52:38115 equal(InputIterator1 first1, InputIterator1 last1,
Marshall Clow0b0bbd22013-05-09 21:14:23116 InputIterator2 first2, InputIterator2 last2); // **C++14**
117
Howard Hinnant3e519522010-05-11 19:42:16118template <class InputIterator1, class InputIterator2, class BinaryPredicate>
Marshall Clow6538e28d2018-01-16 02:04:10119 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16120 equal(InputIterator1 first1, InputIterator1 last1,
121 InputIterator2 first2, BinaryPredicate pred);
122
Marshall Clow0b0bbd22013-05-09 21:14:23123template <class InputIterator1, class InputIterator2, class BinaryPredicate>
Marshall Clow6538e28d2018-01-16 02:04:10124 constexpr bool // constexpr in C++20
Marshall Clow0b0bbd22013-05-09 21:14:23125 equal(InputIterator1 first1, InputIterator1 last1,
126 InputIterator2 first2, InputIterator2 last2,
127 BinaryPredicate pred); // **C++14**
128
Howard Hinnant3e519522010-05-11 19:42:16129template<class ForwardIterator1, class ForwardIterator2>
Marshall Clow49c76432018-01-15 16:16:32130 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16131 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
132 ForwardIterator2 first2);
133
Marshall Clow0b0bbd22013-05-09 21:14:23134template<class ForwardIterator1, class ForwardIterator2>
Marshall Clow49c76432018-01-15 16:16:32135 constexpr bool // constexpr in C++20
Marshall Clow0b0bbd22013-05-09 21:14:23136 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
137 ForwardIterator2 first2, ForwardIterator2 last2); // **C++14**
138
Howard Hinnant3e519522010-05-11 19:42:16139template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clow49c76432018-01-15 16:16:32140 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16141 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
142 ForwardIterator2 first2, BinaryPredicate pred);
143
Marshall Clow0b0bbd22013-05-09 21:14:23144template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clow49c76432018-01-15 16:16:32145 constexpr bool // constexpr in C++20
Marshall Clow0b0bbd22013-05-09 21:14:23146 is_permutation(ForwardIterator1 first1, ForwardIterator1 last1,
147 ForwardIterator2 first2, ForwardIterator2 last2,
148 BinaryPredicate pred); // **C++14**
149
Howard Hinnant3e519522010-05-11 19:42:16150template <class ForwardIterator1, class ForwardIterator2>
Marshall Clow12f0a772018-01-16 15:48:27151 constexpr ForwardIterator1 // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16152 search(ForwardIterator1 first1, ForwardIterator1 last1,
153 ForwardIterator2 first2, ForwardIterator2 last2);
154
155template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
Marshall Clow12f0a772018-01-16 15:48:27156 constexpr ForwardIterator1 // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16157 search(ForwardIterator1 first1, ForwardIterator1 last1,
158 ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
159
160template <class ForwardIterator, class Size, class T>
Marshall Clow12f0a772018-01-16 15:48:27161 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16162 search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
163
164template <class ForwardIterator, class Size, class T, class BinaryPredicate>
Marshall Clow12f0a772018-01-16 15:48:27165 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16166 search_n(ForwardIterator first, ForwardIterator last,
167 Size count, const T& value, BinaryPredicate pred);
168
169template <class InputIterator, class OutputIterator>
Louis Dionne13c90a52019-11-06 12:02:41170 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16171 copy(InputIterator first, InputIterator last, OutputIterator result);
172
173template<class InputIterator, class OutputIterator, class Predicate>
Louis Dionne13c90a52019-11-06 12:02:41174 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16175 copy_if(InputIterator first, InputIterator last,
176 OutputIterator result, Predicate pred);
177
178template<class InputIterator, class Size, class OutputIterator>
Louis Dionne13c90a52019-11-06 12:02:41179 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16180 copy_n(InputIterator first, Size n, OutputIterator result);
181
182template <class BidirectionalIterator1, class BidirectionalIterator2>
Louis Dionne13c90a52019-11-06 12:02:41183 constexpr BidirectionalIterator2 // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16184 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last,
185 BidirectionalIterator2 result);
186
187template <class ForwardIterator1, class ForwardIterator2>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18188 constexpr ForwardIterator2 // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16189 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
190
191template <class ForwardIterator1, class ForwardIterator2>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18192 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16193 iter_swap(ForwardIterator1 a, ForwardIterator2 b);
194
195template <class InputIterator, class OutputIterator, class UnaryOperation>
Marshall Clow99894b62018-01-19 17:45:39196 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16197 transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
198
199template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
Marshall Clow99894b62018-01-19 17:45:39200 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16201 transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
202 OutputIterator result, BinaryOperation binary_op);
203
204template <class ForwardIterator, class T>
Marshall Clow12c74232018-01-19 18:07:29205 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16206 replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
207
208template <class ForwardIterator, class Predicate, class T>
Marshall Clow12c74232018-01-19 18:07:29209 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16210 replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value);
211
212template <class InputIterator, class OutputIterator, class T>
Marshall Clow12c74232018-01-19 18:07:29213 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16214 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 Clow12c74232018-01-19 18:07:29218 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16219 replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value);
220
221template <class ForwardIterator, class T>
Marshall Clow4bfb9312018-01-20 20:14:32222 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16223 fill(ForwardIterator first, ForwardIterator last, const T& value);
224
225template <class OutputIterator, class Size, class T>
Marshall Clow4bfb9312018-01-20 20:14:32226 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16227 fill_n(OutputIterator first, Size n, const T& value);
228
229template <class ForwardIterator, class Generator>
Marshall Clow4bfb9312018-01-20 20:14:32230 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16231 generate(ForwardIterator first, ForwardIterator last, Generator gen);
232
233template <class OutputIterator, class Size, class Generator>
Marshall Clow4bfb9312018-01-20 20:14:32234 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16235 generate_n(OutputIterator first, Size n, Generator gen);
236
237template <class ForwardIterator, class T>
Marshall Clowe8ea8292018-01-22 21:43:04238 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16239 remove(ForwardIterator first, ForwardIterator last, const T& value);
240
241template <class ForwardIterator, class Predicate>
Marshall Clowe8ea8292018-01-22 21:43:04242 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16243 remove_if(ForwardIterator first, ForwardIterator last, Predicate pred);
244
245template <class InputIterator, class OutputIterator, class T>
Marshall Clowe8ea8292018-01-22 21:43:04246 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16247 remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value);
248
249template <class InputIterator, class OutputIterator, class Predicate>
Marshall Clowe8ea8292018-01-22 21:43:04250 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16251 remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred);
252
253template <class ForwardIterator>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18254 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16255 unique(ForwardIterator first, ForwardIterator last);
256
257template <class ForwardIterator, class BinaryPredicate>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18258 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16259 unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
260
261template <class InputIterator, class OutputIterator>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18262 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16263 unique_copy(InputIterator first, InputIterator last, OutputIterator result);
264
265template <class InputIterator, class OutputIterator, class BinaryPredicate>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18266 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16267 unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred);
268
269template <class BidirectionalIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:08270 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16271 reverse(BidirectionalIterator first, BidirectionalIterator last);
272
273template <class BidirectionalIterator, class OutputIterator>
Marshall Clowe8ea8292018-01-22 21:43:04274 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16275 reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);
276
277template <class ForwardIterator>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18278 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16279 rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);
280
281template <class ForwardIterator, class OutputIterator>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18282 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16283 rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result);
284
285template <class RandomAccessIterator>
286 void
Marshall Clow0f37a412017-03-23 13:43:37287 random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14, removed in C++17
Howard Hinnant3e519522010-05-11 19:42:16288
289template <class RandomAccessIterator, class RandomNumberGenerator>
290 void
Marshall Clow06965c12014-03-03 06:14:19291 random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
Marshall Clow0f37a412017-03-23 13:43:37292 RandomNumberGenerator& rand); // deprecated in C++14, removed in C++17
Howard Hinnant3e519522010-05-11 19:42:16293
Eric Fiseliere7154702016-08-28 22:14:37294template<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 Hinnantf9d540b2010-05-26 17:49:34300template<class RandomAccessIterator, class UniformRandomNumberGenerator>
301 void shuffle(RandomAccessIterator first, RandomAccessIterator last,
Howard Hinnantfb340102010-11-18 01:47:02302 UniformRandomNumberGenerator&& g);
Howard Hinnantf9d540b2010-05-26 17:49:34303
Arthur O'Dwyer3fbd3ea2020-12-26 06:39:03304template<class ForwardIterator>
305 constexpr ForwardIterator
306 shift_left(ForwardIterator first, ForwardIterator last,
307 typename iterator_traits<ForwardIterator>::difference_type n); // C++20
308
309template<class ForwardIterator>
310 constexpr ForwardIterator
311 shift_right(ForwardIterator first, ForwardIterator last,
312 typename iterator_traits<ForwardIterator>::difference_type n); // C++20
313
Howard Hinnant3e519522010-05-11 19:42:16314template <class InputIterator, class Predicate>
Marshall Clow49c76432018-01-15 16:16:32315 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16316 is_partitioned(InputIterator first, InputIterator last, Predicate pred);
317
318template <class ForwardIterator, class Predicate>
Arthur O'Dwyerf851db32020-12-17 05:01:08319 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16320 partition(ForwardIterator first, ForwardIterator last, Predicate pred);
321
322template <class InputIterator, class OutputIterator1,
323 class OutputIterator2, class Predicate>
Marshall Clow1b9a4ff2018-01-22 20:44:33324 constexpr pair<OutputIterator1, OutputIterator2> // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16325 partition_copy(InputIterator first, InputIterator last,
326 OutputIterator1 out_true, OutputIterator2 out_false,
327 Predicate pred);
328
329template <class ForwardIterator, class Predicate>
330 ForwardIterator
331 stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);
332
333template<class ForwardIterator, class Predicate>
Marshall Clowd57c03d2018-01-16 02:34:41334 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16335 partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
336
337template <class ForwardIterator>
Marshall Clow49c76432018-01-15 16:16:32338 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16339 is_sorted(ForwardIterator first, ForwardIterator last);
340
341template <class ForwardIterator, class Compare>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18342 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16343 is_sorted(ForwardIterator first, ForwardIterator last, Compare comp);
344
345template<class ForwardIterator>
Marshall Clow056f15e2018-01-15 19:40:34346 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16347 is_sorted_until(ForwardIterator first, ForwardIterator last);
348
349template <class ForwardIterator, class Compare>
Marshall Clow056f15e2018-01-15 19:40:34350 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16351 is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp);
352
353template <class RandomAccessIterator>
Arthur O'Dwyer493f1402020-12-20 20:21:42354 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16355 sort(RandomAccessIterator first, RandomAccessIterator last);
356
357template <class RandomAccessIterator, class Compare>
Arthur O'Dwyer493f1402020-12-20 20:21:42358 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16359 sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
360
361template <class RandomAccessIterator>
362 void
363 stable_sort(RandomAccessIterator first, RandomAccessIterator last);
364
365template <class RandomAccessIterator, class Compare>
366 void
367 stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
368
369template <class RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:18370 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16371 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
372
373template <class RandomAccessIterator, class Compare>
Arthur O'Dwyer5386aa22020-12-17 05:26:18374 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16375 partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
376
377template <class InputIterator, class RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:18378 constexpr RandomAccessIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16379 partial_sort_copy(InputIterator first, InputIterator last,
380 RandomAccessIterator result_first, RandomAccessIterator result_last);
381
382template <class InputIterator, class RandomAccessIterator, class Compare>
Arthur O'Dwyer5386aa22020-12-17 05:26:18383 constexpr RandomAccessIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16384 partial_sort_copy(InputIterator first, InputIterator last,
385 RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp);
386
387template <class RandomAccessIterator>
Arthur O'Dwyer5d956562021-02-04 23:12:52388 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16389 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);
390
391template <class RandomAccessIterator, class Compare>
Arthur O'Dwyer5d956562021-02-04 23:12:52392 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16393 nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp);
394
395template <class ForwardIterator, class T>
Marshall Clowd57c03d2018-01-16 02:34:41396 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16397 lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
398
399template <class ForwardIterator, class T, class Compare>
Marshall Clowd57c03d2018-01-16 02:34:41400 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16401 lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
402
403template <class ForwardIterator, class T>
Marshall Clowd57c03d2018-01-16 02:34:41404 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16405 upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
406
407template <class ForwardIterator, class T, class Compare>
Marshall Clowd57c03d2018-01-16 02:34:41408 constexpr ForwardIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16409 upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
410
411template <class ForwardIterator, class T>
Marshall Clowd57c03d2018-01-16 02:34:41412 constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16413 equal_range(ForwardIterator first, ForwardIterator last, const T& value);
414
415template <class ForwardIterator, class T, class Compare>
Marshall Clowd57c03d2018-01-16 02:34:41416 constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16417 equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
418
419template <class ForwardIterator, class T>
Marshall Clowd57c03d2018-01-16 02:34:41420 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16421 binary_search(ForwardIterator first, ForwardIterator last, const T& value);
422
423template <class ForwardIterator, class T, class Compare>
Marshall Clow8da1a482018-01-22 23:10:40424 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16425 binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
426
427template <class InputIterator1, class InputIterator2, class OutputIterator>
Arthur O'Dwyer14098cf2020-12-04 18:47:12428 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16429 merge(InputIterator1 first1, InputIterator1 last1,
430 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
431
432template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
Arthur O'Dwyer14098cf2020-12-04 18:47:12433 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16434 merge(InputIterator1 first1, InputIterator1 last1,
435 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
436
437template <class BidirectionalIterator>
438 void
439 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last);
440
441template <class BidirectionalIterator, class Compare>
442 void
443 inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp);
444
445template <class InputIterator1, class InputIterator2>
Marshall Clow8da1a482018-01-22 23:10:40446 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16447 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
448
449template <class InputIterator1, class InputIterator2, class Compare>
Marshall Clow8da1a482018-01-22 23:10:40450 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16451 includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
452
453template <class InputIterator1, class InputIterator2, class OutputIterator>
Arthur O'Dwyer14098cf2020-12-04 18:47:12454 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16455 set_union(InputIterator1 first1, InputIterator1 last1,
456 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
457
458template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
Arthur O'Dwyer14098cf2020-12-04 18:47:12459 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16460 set_union(InputIterator1 first1, InputIterator1 last1,
461 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
462
463template <class InputIterator1, class InputIterator2, class OutputIterator>
Marshall Clow8da1a482018-01-22 23:10:40464 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16465 set_intersection(InputIterator1 first1, InputIterator1 last1,
466 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
467
468template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
Marshall Clow8da1a482018-01-22 23:10:40469 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16470 set_intersection(InputIterator1 first1, InputIterator1 last1,
471 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
472
473template <class InputIterator1, class InputIterator2, class OutputIterator>
Arthur O'Dwyer14098cf2020-12-04 18:47:12474 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16475 set_difference(InputIterator1 first1, InputIterator1 last1,
476 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
477
478template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
Arthur O'Dwyer14098cf2020-12-04 18:47:12479 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16480 set_difference(InputIterator1 first1, InputIterator1 last1,
481 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
482
483template <class InputIterator1, class InputIterator2, class OutputIterator>
Arthur O'Dwyer14098cf2020-12-04 18:47:12484 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16485 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
486 InputIterator2 first2, InputIterator2 last2, OutputIterator result);
487
488template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
Arthur O'Dwyer14098cf2020-12-04 18:47:12489 constexpr OutputIterator // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16490 set_symmetric_difference(InputIterator1 first1, InputIterator1 last1,
491 InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
492
493template <class RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:18494 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16495 push_heap(RandomAccessIterator first, RandomAccessIterator last);
496
497template <class RandomAccessIterator, class Compare>
Arthur O'Dwyer5386aa22020-12-17 05:26:18498 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16499 push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
500
501template <class RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:18502 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16503 pop_heap(RandomAccessIterator first, RandomAccessIterator last);
504
505template <class RandomAccessIterator, class Compare>
Arthur O'Dwyer5386aa22020-12-17 05:26:18506 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16507 pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
508
509template <class RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:18510 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16511 make_heap(RandomAccessIterator first, RandomAccessIterator last);
512
513template <class RandomAccessIterator, class Compare>
Arthur O'Dwyer5386aa22020-12-17 05:26:18514 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16515 make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
516
517template <class RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:18518 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16519 sort_heap(RandomAccessIterator first, RandomAccessIterator last);
520
521template <class RandomAccessIterator, class Compare>
Arthur O'Dwyer5386aa22020-12-17 05:26:18522 constexpr void // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16523 sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
524
Howard Hinnantb3371f62010-08-22 00:02:43525template <class RandomAccessIterator>
Marshall Clow49c76432018-01-15 16:16:32526 constexpr bool // constexpr in C++20
Howard Hinnantb3371f62010-08-22 00:02:43527 is_heap(RandomAccessIterator first, RandomAccessiterator last);
Howard Hinnant3e519522010-05-11 19:42:16528
Howard Hinnantb3371f62010-08-22 00:02:43529template <class RandomAccessIterator, class Compare>
Marshall Clow49c76432018-01-15 16:16:32530 constexpr bool // constexpr in C++20
Howard Hinnantb3371f62010-08-22 00:02:43531 is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
Howard Hinnant3e519522010-05-11 19:42:16532
Howard Hinnantb3371f62010-08-22 00:02:43533template <class RandomAccessIterator>
Marshall Clow49c76432018-01-15 16:16:32534 constexpr RandomAccessIterator // constexpr in C++20
Howard Hinnantb3371f62010-08-22 00:02:43535 is_heap_until(RandomAccessIterator first, RandomAccessiterator last);
Howard Hinnant3e519522010-05-11 19:42:16536
Howard Hinnantb3371f62010-08-22 00:02:43537template <class RandomAccessIterator, class Compare>
Marshall Clow49c76432018-01-15 16:16:32538 constexpr RandomAccessIterator // constexpr in C++20
Howard Hinnantb3371f62010-08-22 00:02:43539 is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp);
Howard Hinnant3e519522010-05-11 19:42:16540
Howard Hinnant4eb27b72010-08-21 20:10:01541template <class ForwardIterator>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18542 constexpr ForwardIterator // constexpr in C++14
543 min_element(ForwardIterator first, ForwardIterator last);
Howard Hinnant4eb27b72010-08-21 20:10:01544
545template <class ForwardIterator, class Compare>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18546 constexpr ForwardIterator // constexpr in C++14
547 min_element(ForwardIterator first, ForwardIterator last, Compare comp);
Howard Hinnant4eb27b72010-08-21 20:10:01548
Howard Hinnant3e519522010-05-11 19:42:16549template <class T>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18550 constexpr const T& // constexpr in C++14
551 min(const T& a, const T& b);
Howard Hinnant3e519522010-05-11 19:42:16552
553template <class T, class Compare>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18554 constexpr const T& // constexpr in C++14
555 min(const T& a, const T& b, Compare comp);
Howard Hinnant3e519522010-05-11 19:42:16556
Howard Hinnant4eb27b72010-08-21 20:10:01557template<class T>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18558 constexpr T // constexpr in C++14
559 min(initializer_list<T> t);
Howard Hinnant4eb27b72010-08-21 20:10:01560
561template<class T, class Compare>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18562 constexpr T // constexpr in C++14
563 min(initializer_list<T> t, Compare comp);
Howard Hinnant4eb27b72010-08-21 20:10:01564
Marshall Clow146c14a2016-03-07 22:43:49565template<class T>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18566 constexpr const T& clamp(const T& v, const T& lo, const T& hi); // C++17
Marshall Clow146c14a2016-03-07 22:43:49567
568template<class T, class Compare>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18569 constexpr const T& clamp(const T& v, const T& lo, const T& hi, Compare comp); // C++17
Marshall Clow146c14a2016-03-07 22:43:49570
Howard Hinnant4eb27b72010-08-21 20:10:01571template <class ForwardIterator>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18572 constexpr ForwardIterator // constexpr in C++14
573 max_element(ForwardIterator first, ForwardIterator last);
Howard Hinnant4eb27b72010-08-21 20:10:01574
575template <class ForwardIterator, class Compare>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18576 constexpr ForwardIterator // constexpr in C++14
577 max_element(ForwardIterator first, ForwardIterator last, Compare comp);
Howard Hinnant4eb27b72010-08-21 20:10:01578
Howard Hinnant3e519522010-05-11 19:42:16579template <class T>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18580 constexpr const T& // constexpr in C++14
581 max(const T& a, const T& b);
Howard Hinnant3e519522010-05-11 19:42:16582
583template <class T, class Compare>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18584 constexpr const T& // constexpr in C++14
585 max(const T& a, const T& b, Compare comp);
Howard Hinnant3e519522010-05-11 19:42:16586
Howard Hinnant4eb27b72010-08-21 20:10:01587template<class T>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18588 constexpr T // constexpr in C++14
589 max(initializer_list<T> t);
Howard Hinnant3e519522010-05-11 19:42:16590
Howard Hinnant4eb27b72010-08-21 20:10:01591template<class T, class Compare>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18592 constexpr T // constexpr in C++14
593 max(initializer_list<T> t, Compare comp);
Howard Hinnant3e519522010-05-11 19:42:16594
Howard Hinnant4eb27b72010-08-21 20:10:01595template<class ForwardIterator>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18596 constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++14
597 minmax_element(ForwardIterator first, ForwardIterator last);
Howard Hinnant3e519522010-05-11 19:42:16598
Howard Hinnant4eb27b72010-08-21 20:10:01599template<class ForwardIterator, class Compare>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18600 constexpr pair<ForwardIterator, ForwardIterator> // constexpr in C++14
601 minmax_element(ForwardIterator first, ForwardIterator last, Compare comp);
Howard Hinnant4eb27b72010-08-21 20:10:01602
603template<class T>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18604 constexpr pair<const T&, const T&> // constexpr in C++14
605 minmax(const T& a, const T& b);
Howard Hinnant4eb27b72010-08-21 20:10:01606
607template<class T, class Compare>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18608 constexpr pair<const T&, const T&> // constexpr in C++14
609 minmax(const T& a, const T& b, Compare comp);
Howard Hinnant4eb27b72010-08-21 20:10:01610
611template<class T>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18612 constexpr pair<T, T> // constexpr in C++14
613 minmax(initializer_list<T> t);
Howard Hinnant4eb27b72010-08-21 20:10:01614
615template<class T, class Compare>
Arthur O'Dwyerb8bc4e12020-12-03 01:02:18616 constexpr pair<T, T> // constexpr in C++14
617 minmax(initializer_list<T> t, Compare comp);
Howard Hinnant3e519522010-05-11 19:42:16618
619template <class InputIterator1, class InputIterator2>
Marshall Clow1b9a4ff2018-01-22 20:44:33620 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16621 lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
622
623template <class InputIterator1, class InputIterator2, class Compare>
Marshall Clow1b9a4ff2018-01-22 20:44:33624 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16625 lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
626 InputIterator2 first2, InputIterator2 last2, Compare comp);
627
628template <class BidirectionalIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:08629 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16630 next_permutation(BidirectionalIterator first, BidirectionalIterator last);
631
632template <class BidirectionalIterator, class Compare>
Arthur O'Dwyerf851db32020-12-17 05:01:08633 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16634 next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
635
636template <class BidirectionalIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:08637 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16638 prev_permutation(BidirectionalIterator first, BidirectionalIterator last);
639
640template <class BidirectionalIterator, class Compare>
Arthur O'Dwyerf851db32020-12-17 05:01:08641 constexpr bool // constexpr in C++20
Howard Hinnant3e519522010-05-11 19:42:16642 prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
643
644} // std
645
646*/
647
648#include <__config>
Arthur O'Dwyerbfbd73f2021-05-19 15:57:04649#include <__bits> // __libcpp_clz
Howard Hinnanta1d07d52012-07-26 17:09:09650#include <cstddef>
Arthur O'Dwyerbfbd73f2021-05-19 15:57:04651#include <cstring>
652#include <functional>
653#include <initializer_list>
654#include <iterator>
655#include <memory>
656#include <type_traits>
657#include <utility> // swap_ranges
Marshall Clowf56972e2018-09-12 19:41:40658#include <version>
Howard Hinnant5d1a7012013-08-14 18:00:20659
Eric Fiselierc1bd9192014-08-10 23:53:08660#include <__debug>
661
Howard Hinnant073458b2011-10-17 20:05:10662#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16663#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10664#endif
Howard Hinnant3e519522010-05-11 19:42:16665
Eric Fiseliera016efb2017-05-31 22:07:49666_LIBCPP_PUSH_MACROS
667#include <__undef_macros>
668
669
Howard Hinnant3e519522010-05-11 19:42:16670_LIBCPP_BEGIN_NAMESPACE_STD
671
Marshall Clow9d67c6d2014-02-19 16:51:35672// I'd like to replace these with _VSTD::equal_to<void>, but can't because:
673// * That only works with C++14 and later, and
674// * We haven't included <functional> here.
Howard Hinnant3e519522010-05-11 19:42:16675template <class _T1, class _T2 = _T1>
676struct __equal_to
677{
Marshall Clowd8098f92018-07-14 04:15:19678 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
679 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
680 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
681 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
Howard Hinnant3e519522010-05-11 19:42:16682};
683
684template <class _T1>
685struct __equal_to<_T1, _T1>
686{
Marshall Clow9d67c6d2014-02-19 16:51:35687 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
688 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnant3e519522010-05-11 19:42:16689};
690
691template <class _T1>
692struct __equal_to<const _T1, _T1>
693{
Marshall Clow9d67c6d2014-02-19 16:51:35694 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
695 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnant3e519522010-05-11 19:42:16696};
697
698template <class _T1>
699struct __equal_to<_T1, const _T1>
700{
Marshall Clow9d67c6d2014-02-19 16:51:35701 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
702 bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
Howard Hinnant3e519522010-05-11 19:42:16703};
704
705template <class _T1, class _T2 = _T1>
706struct __less
707{
Aditya Kumar331fb802016-08-25 11:52:38708 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow9d67c6d2014-02-19 16:51:35709 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
710
711 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
712 bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;}
713
714 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
715 bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;}
716
717 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
718 bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;}
Howard Hinnant3e519522010-05-11 19:42:16719};
720
721template <class _T1>
722struct __less<_T1, _T1>
723{
Marshall Clow9d67c6d2014-02-19 16:51:35724 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
725 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnant3e519522010-05-11 19:42:16726};
727
728template <class _T1>
729struct __less<const _T1, _T1>
730{
Marshall Clow9d67c6d2014-02-19 16:51:35731 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
732 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnant3e519522010-05-11 19:42:16733};
734
735template <class _T1>
736struct __less<_T1, const _T1>
737{
Marshall Clow9d67c6d2014-02-19 16:51:35738 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
739 bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
Howard Hinnant3e519522010-05-11 19:42:16740};
741
742template <class _Predicate>
Marshall Clowa763b362017-08-28 23:16:13743class __invert // invert the sense of a comparison
Howard Hinnant3e519522010-05-11 19:42:16744{
745private:
746 _Predicate __p_;
747public:
Marshall Clowa763b362017-08-28 23:16:13748 _LIBCPP_INLINE_VISIBILITY __invert() {}
Howard Hinnant3e519522010-05-11 19:42:16749
750 _LIBCPP_INLINE_VISIBILITY
Marshall Clowa763b362017-08-28 23:16:13751 explicit __invert(_Predicate __p) : __p_(__p) {}
Howard Hinnant3e519522010-05-11 19:42:16752
753 template <class _T1>
754 _LIBCPP_INLINE_VISIBILITY
755 bool operator()(const _T1& __x) {return !__p_(__x);}
756
757 template <class _T1, class _T2>
758 _LIBCPP_INLINE_VISIBILITY
Marshall Clowa763b362017-08-28 23:16:13759 bool operator()(const _T1& __x, const _T2& __y) {return __p_(__y, __x);}
Howard Hinnant3e519522010-05-11 19:42:16760};
761
Louis Dionne04695a72018-12-17 16:04:39762// Perform division by two quickly for positive integers (llvm.org/PR39129)
763
764template <typename _Integral>
765_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
766typename enable_if
767<
768 is_integral<_Integral>::value,
769 _Integral
770>::type
771__half_positive(_Integral __value)
772{
773 return static_cast<_Integral>(static_cast<typename make_unsigned<_Integral>::type>(__value) / 2);
774}
775
776template <typename _Tp>
777_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
778typename enable_if
779<
780 !is_integral<_Tp>::value,
781 _Tp
782>::type
783__half_positive(_Tp __value)
784{
785 return __value / 2;
786}
787
Howard Hinnant145afa12013-08-23 20:10:18788#ifdef _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:16789
790template <class _Compare>
791struct __debug_less
792{
Eric Fiselieraa1cad12019-04-12 05:18:19793 _Compare &__comp_;
794 _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16795 __debug_less(_Compare& __c) : __comp_(__c) {}
Eric Fiselier331d2152016-07-19 23:27:18796
Howard Hinnant3e519522010-05-11 19:42:16797 template <class _Tp, class _Up>
Thomas Anderson6ab51de2019-04-19 00:52:54798 _LIBCPP_CONSTEXPR_AFTER_CXX17
Thomas Anderson3c3ccc02019-04-15 17:02:15799 bool operator()(const _Tp& __x, const _Up& __y)
800 {
801 bool __r = __comp_(__x, __y);
802 if (__r)
803 __do_compare_assert(0, __y, __x);
804 return __r;
805 }
806
807 template <class _Tp, class _Up>
Eric Fiselieraa1cad12019-04-12 05:18:19808 _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier6e4ec602019-03-08 22:58:59809 bool operator()(_Tp& __x, _Up& __y)
Howard Hinnant3e519522010-05-11 19:42:16810 {
811 bool __r = __comp_(__x, __y);
812 if (__r)
Eric Fiselier331d2152016-07-19 23:27:18813 __do_compare_assert(0, __y, __x);
Howard Hinnant3e519522010-05-11 19:42:16814 return __r;
815 }
Eric Fiselier331d2152016-07-19 23:27:18816
817 template <class _LHS, class _RHS>
Eric Fiselieraa1cad12019-04-12 05:18:19818 _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier331d2152016-07-19 23:27:18819 inline _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyerab3fcc52021-05-10 17:04:16820 decltype((void)declval<_Compare&>()(
821 declval<_LHS &>(), declval<_RHS &>()))
Eric Fiselier6e4ec602019-03-08 22:58:59822 __do_compare_assert(int, _LHS & __l, _RHS & __r) {
Eric Fiselier331d2152016-07-19 23:27:18823 _LIBCPP_ASSERT(!__comp_(__l, __r),
824 "Comparator does not induce a strict weak ordering");
825 }
826
827 template <class _LHS, class _RHS>
Eric Fiselieraa1cad12019-04-12 05:18:19828 _LIBCPP_CONSTEXPR_AFTER_CXX17
Eric Fiselier331d2152016-07-19 23:27:18829 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6e4ec602019-03-08 22:58:59830 void __do_compare_assert(long, _LHS &, _RHS &) {}
Howard Hinnant3e519522010-05-11 19:42:16831};
832
Eric Fiselier6e4ec602019-03-08 22:58:59833#endif // _LIBCPP_DEBUG
Howard Hinnant3e519522010-05-11 19:42:16834
Eric Fiselieraa1cad12019-04-12 05:18:19835template <class _Comp>
836struct __comp_ref_type {
837 // Pass the comparator by lvalue reference. Or in debug mode, using a
838 // debugging wrapper that stores a reference.
839#ifndef _LIBCPP_DEBUG
840 typedef typename add_lvalue_reference<_Comp>::type type;
841#else
842 typedef __debug_less<_Comp> type;
843#endif
844};
845
Howard Hinnant3e519522010-05-11 19:42:16846// all_of
847
848template <class _InputIterator, class _Predicate>
Nico Weber1362d7e2019-04-03 18:13:08849_LIBCPP_NODISCARD_EXT inline
850_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16851bool
852all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
853{
854 for (; __first != __last; ++__first)
855 if (!__pred(*__first))
856 return false;
857 return true;
858}
859
860// any_of
861
862template <class _InputIterator, class _Predicate>
Nico Weber1362d7e2019-04-03 18:13:08863_LIBCPP_NODISCARD_EXT inline
864_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16865bool
866any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
867{
868 for (; __first != __last; ++__first)
869 if (__pred(*__first))
870 return true;
871 return false;
872}
873
874// none_of
875
876template <class _InputIterator, class _Predicate>
Nico Weber1362d7e2019-04-03 18:13:08877_LIBCPP_NODISCARD_EXT inline
878_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16879bool
880none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
881{
882 for (; __first != __last; ++__first)
883 if (__pred(*__first))
884 return false;
885 return true;
886}
887
888// for_each
889
890template <class _InputIterator, class _Function>
Marshall Clow1b9a4ff2018-01-22 20:44:33891inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16892_Function
893for_each(_InputIterator __first, _InputIterator __last, _Function __f)
894{
895 for (; __first != __last; ++__first)
896 __f(*__first);
Marshall Clow1c7fe122016-11-14 18:22:19897 return __f;
Howard Hinnant3e519522010-05-11 19:42:16898}
899
Marshall Clow1d029962017-05-25 13:40:57900#if _LIBCPP_STD_VER > 14
Marshall Clowd5c65ff2017-05-25 02:29:54901// for_each_n
902
903template <class _InputIterator, class _Size, class _Function>
Marshall Clow1b9a4ff2018-01-22 20:44:33904inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowd5c65ff2017-05-25 02:29:54905_InputIterator
906for_each_n(_InputIterator __first, _Size __orig_n, _Function __f)
907{
Arthur O'Dwyerc0428b32020-12-08 04:42:47908 typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
Marshall Clowd5c65ff2017-05-25 02:29:54909 _IntegralSize __n = __orig_n;
910 while (__n > 0)
911 {
912 __f(*__first);
913 ++__first;
914 --__n;
915 }
916 return __first;
917}
Marshall Clow1d029962017-05-25 13:40:57918#endif
Marshall Clowd5c65ff2017-05-25 02:29:54919
Howard Hinnant3e519522010-05-11 19:42:16920// find
921
922template <class _InputIterator, class _Tp>
Nico Weber1362d7e2019-04-03 18:13:08923_LIBCPP_NODISCARD_EXT inline
924_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16925_InputIterator
Howard Hinnante4383372011-10-22 20:59:45926find(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:16927{
928 for (; __first != __last; ++__first)
Howard Hinnante4383372011-10-22 20:59:45929 if (*__first == __value_)
Howard Hinnant3e519522010-05-11 19:42:16930 break;
931 return __first;
932}
933
934// find_if
935
936template <class _InputIterator, class _Predicate>
Nico Weber1362d7e2019-04-03 18:13:08937_LIBCPP_NODISCARD_EXT inline
938_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16939_InputIterator
940find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
941{
942 for (; __first != __last; ++__first)
943 if (__pred(*__first))
944 break;
945 return __first;
946}
947
948// find_if_not
949
950template<class _InputIterator, class _Predicate>
Nico Weber1362d7e2019-04-03 18:13:08951_LIBCPP_NODISCARD_EXT inline
952_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:16953_InputIterator
954find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred)
955{
956 for (; __first != __last; ++__first)
957 if (!__pred(*__first))
958 break;
959 return __first;
960}
961
962// find_end
963
964template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow86944282018-01-15 19:26:05965_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator1
Howard Hinnant3e519522010-05-11 19:42:16966__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
967 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
968 forward_iterator_tag, forward_iterator_tag)
969{
970 // modeled after search algorithm
971 _ForwardIterator1 __r = __last1; // __last1 is the "default" answer
972 if (__first2 == __last2)
973 return __r;
974 while (true)
975 {
976 while (true)
977 {
978 if (__first1 == __last1) // if source exhausted return last correct answer
979 return __r; // (or __last1 if never found)
980 if (__pred(*__first1, *__first2))
981 break;
982 ++__first1;
983 }
984 // *__first1 matches *__first2, now match elements after here
985 _ForwardIterator1 __m1 = __first1;
986 _ForwardIterator2 __m2 = __first2;
987 while (true)
988 {
989 if (++__m2 == __last2)
990 { // Pattern exhaused, record answer and search for another one
991 __r = __first1;
992 ++__first1;
993 break;
994 }
995 if (++__m1 == __last1) // Source exhausted, return last answer
996 return __r;
997 if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first
998 {
999 ++__first1;
1000 break;
1001 } // else there is a match, check next elements
1002 }
1003 }
1004}
1005
1006template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2>
Marshall Clow86944282018-01-15 19:26:051007_LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator1
Howard Hinnant3e519522010-05-11 19:42:161008__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
1009 _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred,
1010 bidirectional_iterator_tag, bidirectional_iterator_tag)
1011{
1012 // modeled after search algorithm (in reverse)
1013 if (__first2 == __last2)
1014 return __last1; // Everything matches an empty sequence
1015 _BidirectionalIterator1 __l1 = __last1;
1016 _BidirectionalIterator2 __l2 = __last2;
1017 --__l2;
1018 while (true)
1019 {
1020 // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks
1021 while (true)
1022 {
1023 if (__first1 == __l1) // return __last1 if no element matches *__first2
1024 return __last1;
1025 if (__pred(*--__l1, *__l2))
1026 break;
1027 }
1028 // *__l1 matches *__l2, now match elements before here
1029 _BidirectionalIterator1 __m1 = __l1;
1030 _BidirectionalIterator2 __m2 = __l2;
1031 while (true)
1032 {
1033 if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern)
1034 return __m1;
1035 if (__m1 == __first1) // Otherwise if source exhaused, pattern not found
1036 return __last1;
1037 if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1
1038 {
1039 break;
1040 } // else there is a match, check next elements
1041 }
1042 }
1043}
1044
1045template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow9b0af342014-06-10 18:51:551046_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
Howard Hinnant3e519522010-05-11 19:42:161047__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1048 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
1049 random_access_iterator_tag, random_access_iterator_tag)
1050{
1051 // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
1052 typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
1053 if (__len2 == 0)
1054 return __last1;
1055 typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
1056 if (__len1 < __len2)
1057 return __last1;
1058 const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
1059 _RandomAccessIterator1 __l1 = __last1;
1060 _RandomAccessIterator2 __l2 = __last2;
1061 --__l2;
1062 while (true)
1063 {
1064 while (true)
1065 {
1066 if (__s == __l1)
1067 return __last1;
1068 if (__pred(*--__l1, *__l2))
1069 break;
1070 }
1071 _RandomAccessIterator1 __m1 = __l1;
1072 _RandomAccessIterator2 __m2 = __l2;
1073 while (true)
1074 {
1075 if (__m2 == __first2)
1076 return __m1;
1077 // no need to check range on __m1 because __s guarantees we have enough source
1078 if (!__pred(*--__m1, *--__m2))
1079 {
1080 break;
1081 }
1082 }
1083 }
1084}
1085
1086template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Nico Weber1362d7e2019-04-03 18:13:081087_LIBCPP_NODISCARD_EXT inline
1088_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161089_ForwardIterator1
1090find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1091 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1092{
Howard Hinnantce48a112011-06-30 21:18:191093 return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnant3e519522010-05-11 19:42:161094 (__first1, __last1, __first2, __last2, __pred,
1095 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1096 typename iterator_traits<_ForwardIterator2>::iterator_category());
1097}
1098
1099template <class _ForwardIterator1, class _ForwardIterator2>
Nico Weber1362d7e2019-04-03 18:13:081100_LIBCPP_NODISCARD_EXT inline
1101_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161102_ForwardIterator1
1103find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1104 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1105{
1106 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1107 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantce48a112011-06-30 21:18:191108 return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnant3e519522010-05-11 19:42:161109}
1110
1111// find_first_of
1112
1113template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Marshall Clow9b0af342014-06-10 18:51:551114_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
1115__find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Howard Hinnant3e519522010-05-11 19:42:161116 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1117{
1118 for (; __first1 != __last1; ++__first1)
1119 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1120 if (__pred(*__first1, *__j))
1121 return __first1;
1122 return __last1;
1123}
1124
Marshall Clow9b0af342014-06-10 18:51:551125
1126template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Nico Weber1362d7e2019-04-03 18:13:081127_LIBCPP_NODISCARD_EXT inline
1128_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow9b0af342014-06-10 18:51:551129_ForwardIterator1
1130find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1131 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1132{
1133 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred);
1134}
1135
Howard Hinnant3e519522010-05-11 19:42:161136template <class _ForwardIterator1, class _ForwardIterator2>
Nico Weber1362d7e2019-04-03 18:13:081137_LIBCPP_NODISCARD_EXT inline
1138_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161139_ForwardIterator1
1140find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1141 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1142{
1143 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1144 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Marshall Clow9b0af342014-06-10 18:51:551145 return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnant3e519522010-05-11 19:42:161146}
1147
1148// adjacent_find
1149
1150template <class _ForwardIterator, class _BinaryPredicate>
Nico Weber1362d7e2019-04-03 18:13:081151_LIBCPP_NODISCARD_EXT inline
1152_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161153_ForwardIterator
1154adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
1155{
1156 if (__first != __last)
1157 {
1158 _ForwardIterator __i = __first;
1159 while (++__i != __last)
1160 {
1161 if (__pred(*__first, *__i))
1162 return __first;
1163 __first = __i;
1164 }
1165 }
1166 return __last;
1167}
1168
1169template <class _ForwardIterator>
Nico Weber1362d7e2019-04-03 18:13:081170_LIBCPP_NODISCARD_EXT inline
1171_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161172_ForwardIterator
1173adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
1174{
1175 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnantce48a112011-06-30 21:18:191176 return _VSTD::adjacent_find(__first, __last, __equal_to<__v>());
Howard Hinnant3e519522010-05-11 19:42:161177}
1178
1179// count
1180
1181template <class _InputIterator, class _Tp>
Nico Weber1362d7e2019-04-03 18:13:081182_LIBCPP_NODISCARD_EXT inline
1183_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161184typename iterator_traits<_InputIterator>::difference_type
Howard Hinnante4383372011-10-22 20:59:451185count(_InputIterator __first, _InputIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:161186{
1187 typename iterator_traits<_InputIterator>::difference_type __r(0);
1188 for (; __first != __last; ++__first)
Howard Hinnante4383372011-10-22 20:59:451189 if (*__first == __value_)
Howard Hinnant3e519522010-05-11 19:42:161190 ++__r;
1191 return __r;
1192}
1193
1194// count_if
1195
1196template <class _InputIterator, class _Predicate>
Nico Weber1362d7e2019-04-03 18:13:081197_LIBCPP_NODISCARD_EXT inline
1198_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161199typename iterator_traits<_InputIterator>::difference_type
1200count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1201{
1202 typename iterator_traits<_InputIterator>::difference_type __r(0);
1203 for (; __first != __last; ++__first)
1204 if (__pred(*__first))
1205 ++__r;
1206 return __r;
1207}
1208
1209// mismatch
1210
1211template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Nico Weber1362d7e2019-04-03 18:13:081212_LIBCPP_NODISCARD_EXT inline
1213_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161214pair<_InputIterator1, _InputIterator2>
1215mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1216 _InputIterator2 __first2, _BinaryPredicate __pred)
1217{
Marshall Clowbd7c7b52014-09-16 20:40:051218 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnant3e519522010-05-11 19:42:161219 if (!__pred(*__first1, *__first2))
1220 break;
1221 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1222}
1223
1224template <class _InputIterator1, class _InputIterator2>
Nico Weber1362d7e2019-04-03 18:13:081225_LIBCPP_NODISCARD_EXT inline
1226_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161227pair<_InputIterator1, _InputIterator2>
1228mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1229{
1230 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1231 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnantce48a112011-06-30 21:18:191232 return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnant3e519522010-05-11 19:42:161233}
1234
Marshall Clow0b0bbd22013-05-09 21:14:231235#if _LIBCPP_STD_VER > 11
1236template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Nico Weber1362d7e2019-04-03 18:13:081237_LIBCPP_NODISCARD_EXT inline
1238_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231239pair<_InputIterator1, _InputIterator2>
1240mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1241 _InputIterator2 __first2, _InputIterator2 __last2,
1242 _BinaryPredicate __pred)
1243{
Marshall Clowbd7c7b52014-09-16 20:40:051244 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow0b0bbd22013-05-09 21:14:231245 if (!__pred(*__first1, *__first2))
1246 break;
1247 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1248}
1249
1250template <class _InputIterator1, class _InputIterator2>
Nico Weber1362d7e2019-04-03 18:13:081251_LIBCPP_NODISCARD_EXT inline
1252_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231253pair<_InputIterator1, _InputIterator2>
1254mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1255 _InputIterator2 __first2, _InputIterator2 __last2)
1256{
1257 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1258 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1259 return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
1260}
1261#endif
1262
Howard Hinnant3e519522010-05-11 19:42:161263// equal
1264
1265template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Nico Weber1362d7e2019-04-03 18:13:081266_LIBCPP_NODISCARD_EXT inline
1267_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161268bool
1269equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred)
1270{
Eric Fiselier910285b2014-10-27 19:28:201271 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnant3e519522010-05-11 19:42:161272 if (!__pred(*__first1, *__first2))
1273 return false;
1274 return true;
1275}
1276
1277template <class _InputIterator1, class _InputIterator2>
Nico Weber1362d7e2019-04-03 18:13:081278_LIBCPP_NODISCARD_EXT inline
1279_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161280bool
1281equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
1282{
1283 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1284 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Howard Hinnantce48a112011-06-30 21:18:191285 return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnant3e519522010-05-11 19:42:161286}
1287
Marshall Clow0b0bbd22013-05-09 21:14:231288#if _LIBCPP_STD_VER > 11
1289template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
Marshall Clow6538e28d2018-01-16 02:04:101290inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231291bool
Aditya Kumar331fb802016-08-25 11:52:381292__equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow0b0bbd22013-05-09 21:14:231293 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred,
1294 input_iterator_tag, input_iterator_tag )
1295{
Eric Fiselier910285b2014-10-27 19:28:201296 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow0b0bbd22013-05-09 21:14:231297 if (!__pred(*__first1, *__first2))
1298 return false;
1299 return __first1 == __last1 && __first2 == __last2;
1300}
1301
1302template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow6538e28d2018-01-16 02:04:101303inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231304bool
Aditya Kumar331fb802016-08-25 11:52:381305__equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
1306 _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
Marshall Clow0b0bbd22013-05-09 21:14:231307 random_access_iterator_tag, random_access_iterator_tag )
1308{
1309 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1310 return false;
1311 return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
1312 typename add_lvalue_reference<_BinaryPredicate>::type>
1313 (__first1, __last1, __first2, __pred );
1314}
1315
1316template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
Nico Weber1362d7e2019-04-03 18:13:081317_LIBCPP_NODISCARD_EXT inline
1318_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231319bool
Aditya Kumar331fb802016-08-25 11:52:381320equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow0b0bbd22013-05-09 21:14:231321 _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred )
1322{
1323 return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type>
Aditya Kumar331fb802016-08-25 11:52:381324 (__first1, __last1, __first2, __last2, __pred,
Marshall Clow0b0bbd22013-05-09 21:14:231325 typename iterator_traits<_InputIterator1>::iterator_category(),
1326 typename iterator_traits<_InputIterator2>::iterator_category());
1327}
1328
1329template <class _InputIterator1, class _InputIterator2>
Nico Weber1362d7e2019-04-03 18:13:081330_LIBCPP_NODISCARD_EXT inline
1331_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231332bool
Aditya Kumar331fb802016-08-25 11:52:381333equal(_InputIterator1 __first1, _InputIterator1 __last1,
Marshall Clow0b0bbd22013-05-09 21:14:231334 _InputIterator2 __first2, _InputIterator2 __last2)
1335{
1336 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
1337 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
1338 return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
1339 typename iterator_traits<_InputIterator1>::iterator_category(),
1340 typename iterator_traits<_InputIterator2>::iterator_category());
1341}
1342#endif
1343
Howard Hinnant3e519522010-05-11 19:42:161344// is_permutation
1345
1346template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Nico Weber1362d7e2019-04-03 18:13:081347_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnant3e519522010-05-11 19:42:161348is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1349 _ForwardIterator2 __first2, _BinaryPredicate __pred)
1350{
Marshall Clow49c76432018-01-15 16:16:321351// shorten sequences as much as possible by lopping of any equal prefix
Eric Fiselier910285b2014-10-27 19:28:201352 for (; __first1 != __last1; ++__first1, (void) ++__first2)
Howard Hinnant3e519522010-05-11 19:42:161353 if (!__pred(*__first1, *__first2))
Marshall Clow49c76432018-01-15 16:16:321354 break;
1355 if (__first1 == __last1)
1356 return true;
1357
1358// __first1 != __last1 && *__first1 != *__first2
Howard Hinnant3e519522010-05-11 19:42:161359 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
Howard Hinnantce48a112011-06-30 21:18:191360 _D1 __l1 = _VSTD::distance(__first1, __last1);
Howard Hinnant3e519522010-05-11 19:42:161361 if (__l1 == _D1(1))
1362 return false;
Howard Hinnantce48a112011-06-30 21:18:191363 _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
Howard Hinnant3e519522010-05-11 19:42:161364 // For each element in [f1, l1) see if there are the same number of
1365 // equal elements in [f2, l2)
1366 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1367 {
Marshall Clow49c76432018-01-15 16:16:321368 // Have we already counted the number of *__i in [f1, l1)?
Peter Collingbourne939b1622018-01-26 21:23:271369 _ForwardIterator1 __match = __first1;
1370 for (; __match != __i; ++__match)
1371 if (__pred(*__match, *__i))
1372 break;
1373 if (__match == __i) {
Howard Hinnant3e519522010-05-11 19:42:161374 // Count number of *__i in [f2, l2)
1375 _D1 __c2 = 0;
1376 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1377 if (__pred(*__i, *__j))
1378 ++__c2;
1379 if (__c2 == 0)
1380 return false;
1381 // Count number of *__i in [__i, l1) (we can start with 1)
1382 _D1 __c1 = 1;
Howard Hinnantce48a112011-06-30 21:18:191383 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
Howard Hinnant3e519522010-05-11 19:42:161384 if (__pred(*__i, *__j))
1385 ++__c1;
1386 if (__c1 != __c2)
1387 return false;
1388 }
Howard Hinnant3e519522010-05-11 19:42:161389 }
1390 return true;
1391}
1392
1393template<class _ForwardIterator1, class _ForwardIterator2>
Nico Weber1362d7e2019-04-03 18:13:081394_LIBCPP_NODISCARD_EXT inline
1395_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161396bool
1397is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1398 _ForwardIterator2 __first2)
1399{
1400 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1401 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantce48a112011-06-30 21:18:191402 return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
Howard Hinnant3e519522010-05-11 19:42:161403}
1404
Marshall Clow0b0bbd22013-05-09 21:14:231405#if _LIBCPP_STD_VER > 11
1406template<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
Marshall Clow49c76432018-01-15 16:16:321407_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Marshall Clow0b0bbd22013-05-09 21:14:231408__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
Aditya Kumar331fb802016-08-25 11:52:381409 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
Marshall Clow0b0bbd22013-05-09 21:14:231410 _BinaryPredicate __pred,
1411 forward_iterator_tag, forward_iterator_tag )
1412{
Marshall Clow49c76432018-01-15 16:16:321413// shorten sequences as much as possible by lopping of any equal prefix
Eric Fiselier847ee132014-10-27 20:26:251414 for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2)
Marshall Clow0b0bbd22013-05-09 21:14:231415 if (!__pred(*__first1, *__first2))
Marshall Clow49c76432018-01-15 16:16:321416 break;
1417 if (__first1 == __last1)
Marshall Clow12c74232018-01-19 18:07:291418 return __first2 == __last2;
Marshall Clow49c76432018-01-15 16:16:321419 else if (__first2 == __last2)
Marshall Clow12c74232018-01-19 18:07:291420 return false;
Marshall Clow49c76432018-01-15 16:16:321421
Marshall Clow0b0bbd22013-05-09 21:14:231422 typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
1423 _D1 __l1 = _VSTD::distance(__first1, __last1);
1424
1425 typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
Marshall Clowfce85ba2013-05-10 00:16:101426 _D2 __l2 = _VSTD::distance(__first2, __last2);
Marshall Clow0b0bbd22013-05-09 21:14:231427 if (__l1 != __l2)
1428 return false;
1429
1430 // For each element in [f1, l1) see if there are the same number of
1431 // equal elements in [f2, l2)
1432 for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i)
1433 {
Marshall Clow49c76432018-01-15 16:16:321434 // Have we already counted the number of *__i in [f1, l1)?
Peter Collingbourne939b1622018-01-26 21:23:271435 _ForwardIterator1 __match = __first1;
1436 for (; __match != __i; ++__match)
1437 if (__pred(*__match, *__i))
1438 break;
1439 if (__match == __i) {
Marshall Clow0b0bbd22013-05-09 21:14:231440 // Count number of *__i in [f2, l2)
1441 _D1 __c2 = 0;
1442 for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
1443 if (__pred(*__i, *__j))
1444 ++__c2;
1445 if (__c2 == 0)
1446 return false;
1447 // Count number of *__i in [__i, l1) (we can start with 1)
1448 _D1 __c1 = 1;
1449 for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
1450 if (__pred(*__i, *__j))
1451 ++__c1;
1452 if (__c1 != __c2)
1453 return false;
1454 }
Marshall Clow0b0bbd22013-05-09 21:14:231455 }
1456 return true;
1457}
1458
1459template<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
Marshall Clow49c76432018-01-15 16:16:321460_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Marshall Clow0b0bbd22013-05-09 21:14:231461__is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
Aditya Kumar331fb802016-08-25 11:52:381462 _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
Marshall Clow0b0bbd22013-05-09 21:14:231463 _BinaryPredicate __pred,
1464 random_access_iterator_tag, random_access_iterator_tag )
1465{
1466 if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
1467 return false;
1468 return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
1469 typename add_lvalue_reference<_BinaryPredicate>::type>
1470 (__first1, __last1, __first2, __pred );
1471}
1472
1473template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Nico Weber1362d7e2019-04-03 18:13:081474_LIBCPP_NODISCARD_EXT inline
1475_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231476bool
1477is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1478 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
1479 _BinaryPredicate __pred )
1480{
1481 return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>
1482 (__first1, __last1, __first2, __last2, __pred,
1483 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1484 typename iterator_traits<_ForwardIterator2>::iterator_category());
1485}
1486
1487template<class _ForwardIterator1, class _ForwardIterator2>
Nico Weber1362d7e2019-04-03 18:13:081488_LIBCPP_NODISCARD_EXT inline
1489_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clow0b0bbd22013-05-09 21:14:231490bool
1491is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1492 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1493{
1494 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1495 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
1496 return _VSTD::__is_permutation(__first1, __last1, __first2, __last2,
1497 __equal_to<__v1, __v2>(),
1498 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1499 typename iterator_traits<_ForwardIterator2>::iterator_category());
1500}
1501#endif
1502
Howard Hinnant3e519522010-05-11 19:42:161503// search
Marshall Clowd835e592018-01-08 19:18:001504// __search is in <functional>
Howard Hinnant3e519522010-05-11 19:42:161505
1506template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
Nico Weber1362d7e2019-04-03 18:13:081507_LIBCPP_NODISCARD_EXT inline
1508_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161509_ForwardIterator1
1510search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1511 _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
1512{
Howard Hinnantce48a112011-06-30 21:18:191513 return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnant3e519522010-05-11 19:42:161514 (__first1, __last1, __first2, __last2, __pred,
Marshall Clow28cc4dd2016-03-08 15:12:521515 typename iterator_traits<_ForwardIterator1>::iterator_category(),
1516 typename iterator_traits<_ForwardIterator2>::iterator_category())
1517 .first;
Howard Hinnant3e519522010-05-11 19:42:161518}
1519
1520template <class _ForwardIterator1, class _ForwardIterator2>
Nico Weber1362d7e2019-04-03 18:13:081521_LIBCPP_NODISCARD_EXT inline
1522_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161523_ForwardIterator1
1524search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1525 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
1526{
Marshall Clow28cc4dd2016-03-08 15:12:521527 typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
1528 typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
Howard Hinnantce48a112011-06-30 21:18:191529 return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>());
Howard Hinnant3e519522010-05-11 19:42:161530}
1531
Marshall Clowd835e592018-01-08 19:18:001532
1533#if _LIBCPP_STD_VER > 14
1534template <class _ForwardIterator, class _Searcher>
Nico Weber1362d7e2019-04-03 18:13:081535_LIBCPP_NODISCARD_EXT _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowd835e592018-01-08 19:18:001536_ForwardIterator search(_ForwardIterator __f, _ForwardIterator __l, const _Searcher &__s)
1537{ return __s(__f, __l).first; }
1538#endif
1539
Howard Hinnant3e519522010-05-11 19:42:161540// search_n
1541
1542template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
Marshall Clow12f0a772018-01-16 15:48:271543_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:161544__search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnante4383372011-10-22 20:59:451545 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
Howard Hinnant3e519522010-05-11 19:42:161546{
1547 if (__count <= 0)
1548 return __first;
1549 while (true)
1550 {
Howard Hinnante4383372011-10-22 20:59:451551 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnant3e519522010-05-11 19:42:161552 while (true)
1553 {
Howard Hinnante4383372011-10-22 20:59:451554 if (__first == __last) // return __last if no element matches __value_
Howard Hinnant3e519522010-05-11 19:42:161555 return __last;
Howard Hinnante4383372011-10-22 20:59:451556 if (__pred(*__first, __value_))
Howard Hinnant3e519522010-05-11 19:42:161557 break;
1558 ++__first;
1559 }
Howard Hinnante4383372011-10-22 20:59:451560 // *__first matches __value_, now match elements after here
Howard Hinnant3e519522010-05-11 19:42:161561 _ForwardIterator __m = __first;
1562 _Size __c(0);
1563 while (true)
1564 {
1565 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1566 return __first;
1567 if (++__m == __last) // Otherwise if source exhaused, pattern not found
1568 return __last;
Howard Hinnante4383372011-10-22 20:59:451569 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnant3e519522010-05-11 19:42:161570 {
1571 __first = __m;
1572 ++__first;
1573 break;
1574 } // else there is a match, check next elements
1575 }
1576 }
1577}
1578
1579template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
Marshall Clow12f0a772018-01-16 15:48:271580_LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
Howard Hinnant3e519522010-05-11 19:42:161581__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnante4383372011-10-22 20:59:451582 _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
Howard Hinnant3e519522010-05-11 19:42:161583{
1584 if (__count <= 0)
1585 return __first;
1586 _Size __len = static_cast<_Size>(__last - __first);
1587 if (__len < __count)
1588 return __last;
1589 const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
1590 while (true)
1591 {
Howard Hinnante4383372011-10-22 20:59:451592 // Find first element in sequence that matchs __value_, with a mininum of loop checks
Howard Hinnant3e519522010-05-11 19:42:161593 while (true)
1594 {
Howard Hinnantb13fcad2013-04-04 15:40:481595 if (__first >= __s) // return __last if no element matches __value_
Howard Hinnant3e519522010-05-11 19:42:161596 return __last;
Howard Hinnante4383372011-10-22 20:59:451597 if (__pred(*__first, __value_))
Howard Hinnant3e519522010-05-11 19:42:161598 break;
1599 ++__first;
1600 }
Howard Hinnante4383372011-10-22 20:59:451601 // *__first matches __value_, now match elements after here
Howard Hinnant3e519522010-05-11 19:42:161602 _RandomAccessIterator __m = __first;
1603 _Size __c(0);
1604 while (true)
1605 {
1606 if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern)
1607 return __first;
1608 ++__m; // no need to check range on __m because __s guarantees we have enough source
Howard Hinnante4383372011-10-22 20:59:451609 if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first
Howard Hinnant3e519522010-05-11 19:42:161610 {
1611 __first = __m;
1612 ++__first;
1613 break;
1614 } // else there is a match, check next elements
1615 }
1616 }
1617}
1618
1619template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
Nico Weber1362d7e2019-04-03 18:13:081620_LIBCPP_NODISCARD_EXT inline
1621_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161622_ForwardIterator
1623search_n(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnante4383372011-10-22 20:59:451624 _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
Howard Hinnant3e519522010-05-11 19:42:161625{
Howard Hinnantce48a112011-06-30 21:18:191626 return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type>
Arthur O'Dwyerc0428b32020-12-08 04:42:471627 (__first, __last, _VSTD::__convert_to_integral(__count), __value_, __pred,
Eric Fiselier51544022015-02-10 16:46:421628 typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnant3e519522010-05-11 19:42:161629}
1630
1631template <class _ForwardIterator, class _Size, class _Tp>
Nico Weber1362d7e2019-04-03 18:13:081632_LIBCPP_NODISCARD_EXT inline
1633_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161634_ForwardIterator
Howard Hinnante4383372011-10-22 20:59:451635search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:161636{
1637 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Arthur O'Dwyerc0428b32020-12-08 04:42:471638 return _VSTD::search_n(__first, __last, _VSTD::__convert_to_integral(__count),
Eric Fiselier51544022015-02-10 16:46:421639 __value_, __equal_to<__v, _Tp>());
Howard Hinnant3e519522010-05-11 19:42:161640}
1641
Mark de Weverb13edf62021-05-29 07:50:261642// __unwrap_iter, __rewrap_iter
1643
1644// The job of __unwrap_iter is to lower contiguous iterators (such as
1645// vector<T>::iterator) into pointers, to reduce the number of template
1646// instantiations and to enable pointer-based optimizations e.g. in std::copy.
1647// For iterators that are not contiguous, it must be a no-op.
1648// In debug mode, we don't do this.
1649//
1650// __unwrap_iter is non-constexpr for user-defined iterators whose
1651// `to_address` and/or `operator->` is non-constexpr. This is okay; but we
1652// try to avoid doing __unwrap_iter in constant-evaluated contexts anyway.
1653//
1654// Some algorithms (e.g. std::copy, but not std::sort) need to convert an
1655// "unwrapped" result back into a contiguous iterator. Since contiguous iterators
1656// are random-access, we can do this portably using iterator arithmetic; this
1657// is the job of __rewrap_iter.
1658
1659template <class _Iter, bool = __is_cpp17_contiguous_iterator<_Iter>::value>
1660struct __unwrap_iter_impl {
1661 static _LIBCPP_CONSTEXPR _Iter
1662 __apply(_Iter __i) _NOEXCEPT {
1663 return __i;
1664 }
1665};
1666
1667#if _LIBCPP_DEBUG_LEVEL < 2
1668
1669template <class _Iter>
1670struct __unwrap_iter_impl<_Iter, true> {
1671 static _LIBCPP_CONSTEXPR decltype(_VSTD::__to_address(declval<_Iter>()))
1672 __apply(_Iter __i) _NOEXCEPT {
1673 return _VSTD::__to_address(__i);
1674 }
1675};
1676
1677#endif // _LIBCPP_DEBUG_LEVEL < 2
1678
1679template<class _Iter, class _Impl = __unwrap_iter_impl<_Iter> >
1680inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1681decltype(_Impl::__apply(declval<_Iter>()))
1682__unwrap_iter(_Iter __i) _NOEXCEPT
1683{
1684 return _Impl::__apply(__i);
1685}
1686
1687template<class _OrigIter>
1688_OrigIter __rewrap_iter(_OrigIter, _OrigIter __result)
1689{
1690 return __result;
1691}
1692
1693template<class _OrigIter, class _UnwrappedIter>
1694_OrigIter __rewrap_iter(_OrigIter __first, _UnwrappedIter __result)
1695{
1696 // Precondition: __result is reachable from __first
1697 // Precondition: _OrigIter is a contiguous iterator
1698 return __first + (__result - _VSTD::__unwrap_iter(__first));
1699}
1700
Arthur O'Dwyerd41c6d52021-01-15 17:59:561701// copy
1702
Howard Hinnant3e519522010-05-11 19:42:161703template <class _InputIterator, class _OutputIterator>
Louis Dionne13c90a52019-11-06 12:02:411704inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161705_OutputIterator
Louis Dionne13c90a52019-11-06 12:02:411706__copy_constexpr(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
Howard Hinnant3e519522010-05-11 19:42:161707{
Eric Fiselier910285b2014-10-27 19:28:201708 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant3e519522010-05-11 19:42:161709 *__result = *__first;
1710 return __result;
1711}
1712
Louis Dionne13c90a52019-11-06 12:02:411713template <class _InputIterator, class _OutputIterator>
1714inline _LIBCPP_INLINE_VISIBILITY
1715_OutputIterator
1716__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1717{
Arthur O'Dwyer19688042020-11-22 18:21:111718 return _VSTD::__copy_constexpr(__first, __last, __result);
Louis Dionne13c90a52019-11-06 12:02:411719}
1720
Howard Hinnant3e519522010-05-11 19:42:161721template <class _Tp, class _Up>
1722inline _LIBCPP_INLINE_VISIBILITY
1723typename enable_if
1724<
1725 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnantca740482010-11-19 22:17:281726 is_trivially_copy_assignable<_Up>::value,
Howard Hinnant3e519522010-05-11 19:42:161727 _Up*
1728>::type
1729__copy(_Tp* __first, _Tp* __last, _Up* __result)
1730{
1731 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow5b312052015-06-02 13:52:161732 if (__n > 0)
1733 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnant3e519522010-05-11 19:42:161734 return __result + __n;
1735}
1736
1737template <class _InputIterator, class _OutputIterator>
Arthur O'Dwyeree95c702020-11-23 17:44:411738inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161739_OutputIterator
1740copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1741{
Louis Dionne13c90a52019-11-06 12:02:411742 if (__libcpp_is_constant_evaluated()) {
Arthur O'Dwyer85167fb2021-02-03 22:54:131743 return _VSTD::__copy_constexpr(__first, __last, __result);
Louis Dionne13c90a52019-11-06 12:02:411744 } else {
Arthur O'Dwyer85167fb2021-02-03 22:54:131745 return _VSTD::__rewrap_iter(__result,
1746 _VSTD::__copy(_VSTD::__unwrap_iter(__first),
1747 _VSTD::__unwrap_iter(__last),
1748 _VSTD::__unwrap_iter(__result)));
Louis Dionne13c90a52019-11-06 12:02:411749 }
Howard Hinnant3e519522010-05-11 19:42:161750}
1751
1752// copy_backward
1753
Howard Hinnantd3d43562013-02-06 21:03:391754template <class _BidirectionalIterator, class _OutputIterator>
Louis Dionne13c90a52019-11-06 12:02:411755inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1756_OutputIterator
1757__copy_backward_constexpr(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
1758{
1759 while (__first != __last)
1760 *--__result = *--__last;
1761 return __result;
1762}
1763
1764template <class _BidirectionalIterator, class _OutputIterator>
Howard Hinnant3e519522010-05-11 19:42:161765inline _LIBCPP_INLINE_VISIBILITY
1766_OutputIterator
Howard Hinnantd3d43562013-02-06 21:03:391767__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
Howard Hinnant3e519522010-05-11 19:42:161768{
Arthur O'Dwyer19688042020-11-22 18:21:111769 return _VSTD::__copy_backward_constexpr(__first, __last, __result);
Howard Hinnant3e519522010-05-11 19:42:161770}
1771
1772template <class _Tp, class _Up>
1773inline _LIBCPP_INLINE_VISIBILITY
1774typename enable_if
1775<
1776 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnantca740482010-11-19 22:17:281777 is_trivially_copy_assignable<_Up>::value,
Howard Hinnant3e519522010-05-11 19:42:161778 _Up*
1779>::type
1780__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1781{
1782 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow5b312052015-06-02 13:52:161783 if (__n > 0)
1784 {
1785 __result -= __n;
1786 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1787 }
Howard Hinnant3e519522010-05-11 19:42:161788 return __result;
1789}
1790
1791template <class _BidirectionalIterator1, class _BidirectionalIterator2>
Arthur O'Dwyeree95c702020-11-23 17:44:411792inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161793_BidirectionalIterator2
1794copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1795 _BidirectionalIterator2 __result)
1796{
Louis Dionne13c90a52019-11-06 12:02:411797 if (__libcpp_is_constant_evaluated()) {
Arthur O'Dwyer85167fb2021-02-03 22:54:131798 return _VSTD::__copy_backward_constexpr(__first, __last, __result);
Louis Dionne13c90a52019-11-06 12:02:411799 } else {
Arthur O'Dwyer85167fb2021-02-03 22:54:131800 return _VSTD::__rewrap_iter(__result,
1801 _VSTD::__copy_backward(_VSTD::__unwrap_iter(__first),
1802 _VSTD::__unwrap_iter(__last),
1803 _VSTD::__unwrap_iter(__result)));
Louis Dionne13c90a52019-11-06 12:02:411804 }
Howard Hinnant3e519522010-05-11 19:42:161805}
1806
1807// copy_if
1808
1809template<class _InputIterator, class _OutputIterator, class _Predicate>
Louis Dionne13c90a52019-11-06 12:02:411810inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161811_OutputIterator
1812copy_if(_InputIterator __first, _InputIterator __last,
1813 _OutputIterator __result, _Predicate __pred)
1814{
1815 for (; __first != __last; ++__first)
1816 {
1817 if (__pred(*__first))
1818 {
1819 *__result = *__first;
1820 ++__result;
1821 }
1822 }
1823 return __result;
1824}
1825
1826// copy_n
1827
1828template<class _InputIterator, class _Size, class _OutputIterator>
Arthur O'Dwyeree95c702020-11-23 17:44:411829inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161830typename enable_if
1831<
Eric Fiselierf82dba02019-11-18 06:46:581832 __is_cpp17_input_iterator<_InputIterator>::value &&
1833 !__is_cpp17_random_access_iterator<_InputIterator>::value,
Howard Hinnant3e519522010-05-11 19:42:161834 _OutputIterator
1835>::type
Eric Fiselier51544022015-02-10 16:46:421836copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnant3e519522010-05-11 19:42:161837{
Arthur O'Dwyerc0428b32020-12-08 04:42:471838 typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
Eric Fiselier51544022015-02-10 16:46:421839 _IntegralSize __n = __orig_n;
Howard Hinnant99847d22011-02-27 20:55:391840 if (__n > 0)
1841 {
Howard Hinnant3e519522010-05-11 19:42:161842 *__result = *__first;
Howard Hinnant99847d22011-02-27 20:55:391843 ++__result;
1844 for (--__n; __n > 0; --__n)
1845 {
1846 ++__first;
1847 *__result = *__first;
1848 ++__result;
1849 }
1850 }
Howard Hinnant3e519522010-05-11 19:42:161851 return __result;
1852}
1853
1854template<class _InputIterator, class _Size, class _OutputIterator>
Arthur O'Dwyeree95c702020-11-23 17:44:411855inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161856typename enable_if
1857<
Eric Fiselierf82dba02019-11-18 06:46:581858 __is_cpp17_random_access_iterator<_InputIterator>::value,
Howard Hinnant3e519522010-05-11 19:42:161859 _OutputIterator
1860>::type
Eric Fiselier51544022015-02-10 16:46:421861copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnant3e519522010-05-11 19:42:161862{
Arthur O'Dwyerc0428b32020-12-08 04:42:471863 typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
Eric Fiselier51544022015-02-10 16:46:421864 _IntegralSize __n = __orig_n;
Howard Hinnantce48a112011-06-30 21:18:191865 return _VSTD::copy(__first, __first + __n, __result);
Howard Hinnant3e519522010-05-11 19:42:161866}
1867
1868// move
1869
Howard Hinnant3e519522010-05-11 19:42:161870template <class _InputIterator, class _OutputIterator>
zoecarver3ed89b52020-09-14 22:11:081871inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnant3e519522010-05-11 19:42:161872_OutputIterator
zoecarver3ed89b52020-09-14 22:11:081873__move_constexpr(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
Howard Hinnant3e519522010-05-11 19:42:161874{
Eric Fiselier910285b2014-10-27 19:28:201875 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantce48a112011-06-30 21:18:191876 *__result = _VSTD::move(*__first);
Howard Hinnant3e519522010-05-11 19:42:161877 return __result;
1878}
1879
zoecarver3ed89b52020-09-14 22:11:081880template <class _InputIterator, class _OutputIterator>
1881inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1882_OutputIterator
1883__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1884{
Arthur O'Dwyer19688042020-11-22 18:21:111885 return _VSTD::__move_constexpr(__first, __last, __result);
zoecarver3ed89b52020-09-14 22:11:081886}
1887
Howard Hinnant3e519522010-05-11 19:42:161888template <class _Tp, class _Up>
zoecarver3ed89b52020-09-14 22:11:081889inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnant3e519522010-05-11 19:42:161890typename enable_if
1891<
1892 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Arthur O'Dwyerd41c6d52021-01-15 17:59:561893 is_trivially_move_assignable<_Up>::value,
Howard Hinnant3e519522010-05-11 19:42:161894 _Up*
1895>::type
1896__move(_Tp* __first, _Tp* __last, _Up* __result)
1897{
Howard Hinnant3e519522010-05-11 19:42:161898 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow5b312052015-06-02 13:52:161899 if (__n > 0)
1900 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnant3e519522010-05-11 19:42:161901 return __result + __n;
1902}
1903
1904template <class _InputIterator, class _OutputIterator>
zoecarver3ed89b52020-09-14 22:11:081905inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161906_OutputIterator
1907move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1908{
Arthur O'Dwyer85167fb2021-02-03 22:54:131909 if (__libcpp_is_constant_evaluated()) {
1910 return _VSTD::__move_constexpr(__first, __last, __result);
1911 } else {
1912 return _VSTD::__rewrap_iter(__result,
1913 _VSTD::__move(_VSTD::__unwrap_iter(__first),
1914 _VSTD::__unwrap_iter(__last),
1915 _VSTD::__unwrap_iter(__result)));
1916 }
Howard Hinnant3e519522010-05-11 19:42:161917}
1918
1919// move_backward
1920
Howard Hinnant3e519522010-05-11 19:42:161921template <class _InputIterator, class _OutputIterator>
zoecarver3ed89b52020-09-14 22:11:081922inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnant3e519522010-05-11 19:42:161923_OutputIterator
zoecarver3ed89b52020-09-14 22:11:081924__move_backward_constexpr(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
Howard Hinnant3e519522010-05-11 19:42:161925{
1926 while (__first != __last)
Howard Hinnantce48a112011-06-30 21:18:191927 *--__result = _VSTD::move(*--__last);
Howard Hinnant3e519522010-05-11 19:42:161928 return __result;
1929}
1930
zoecarver3ed89b52020-09-14 22:11:081931template <class _InputIterator, class _OutputIterator>
1932inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1933_OutputIterator
1934__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1935{
Arthur O'Dwyer19688042020-11-22 18:21:111936 return _VSTD::__move_backward_constexpr(__first, __last, __result);
zoecarver3ed89b52020-09-14 22:11:081937}
1938
Howard Hinnant3e519522010-05-11 19:42:161939template <class _Tp, class _Up>
zoecarver3ed89b52020-09-14 22:11:081940inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnant3e519522010-05-11 19:42:161941typename enable_if
1942<
1943 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Arthur O'Dwyerd41c6d52021-01-15 17:59:561944 is_trivially_move_assignable<_Up>::value,
Howard Hinnant3e519522010-05-11 19:42:161945 _Up*
1946>::type
1947__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1948{
Howard Hinnant3e519522010-05-11 19:42:161949 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow5b312052015-06-02 13:52:161950 if (__n > 0)
1951 {
1952 __result -= __n;
1953 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1954 }
Howard Hinnant3e519522010-05-11 19:42:161955 return __result;
1956}
1957
1958template <class _BidirectionalIterator1, class _BidirectionalIterator2>
zoecarver3ed89b52020-09-14 22:11:081959inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161960_BidirectionalIterator2
1961move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1962 _BidirectionalIterator2 __result)
1963{
Arthur O'Dwyer85167fb2021-02-03 22:54:131964 if (__libcpp_is_constant_evaluated()) {
1965 return _VSTD::__move_backward_constexpr(__first, __last, __result);
1966 } else {
1967 return _VSTD::__rewrap_iter(__result,
1968 _VSTD::__move_backward(_VSTD::__unwrap_iter(__first),
1969 _VSTD::__unwrap_iter(__last),
1970 _VSTD::__unwrap_iter(__result)));
1971 }
Howard Hinnant3e519522010-05-11 19:42:161972}
1973
1974// iter_swap
1975
Howard Hinnanta676f7d2011-05-27 15:04:191976// moved to <type_traits> for better swap / noexcept support
Howard Hinnant3e519522010-05-11 19:42:161977
1978// transform
1979
1980template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
Marshall Clow99894b62018-01-19 17:45:391981inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161982_OutputIterator
1983transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1984{
Eric Fiselier910285b2014-10-27 19:28:201985 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant3e519522010-05-11 19:42:161986 *__result = __op(*__first);
1987 return __result;
1988}
1989
1990template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
Marshall Clow99894b62018-01-19 17:45:391991inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161992_OutputIterator
1993transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1994 _OutputIterator __result, _BinaryOperation __binary_op)
1995{
Eric Fiselier910285b2014-10-27 19:28:201996 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
Howard Hinnant3e519522010-05-11 19:42:161997 *__result = __binary_op(*__first1, *__first2);
1998 return __result;
1999}
2000
2001// replace
2002
2003template <class _ForwardIterator, class _Tp>
Marshall Clow12c74232018-01-19 18:07:292004inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162005void
2006replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
2007{
2008 for (; __first != __last; ++__first)
2009 if (*__first == __old_value)
2010 *__first = __new_value;
2011}
2012
2013// replace_if
2014
2015template <class _ForwardIterator, class _Predicate, class _Tp>
Marshall Clow12c74232018-01-19 18:07:292016inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162017void
2018replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
2019{
2020 for (; __first != __last; ++__first)
2021 if (__pred(*__first))
2022 *__first = __new_value;
2023}
2024
2025// replace_copy
2026
2027template <class _InputIterator, class _OutputIterator, class _Tp>
Marshall Clow12c74232018-01-19 18:07:292028inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162029_OutputIterator
2030replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2031 const _Tp& __old_value, const _Tp& __new_value)
2032{
Eric Fiselier910285b2014-10-27 19:28:202033 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant3e519522010-05-11 19:42:162034 if (*__first == __old_value)
2035 *__result = __new_value;
2036 else
2037 *__result = *__first;
2038 return __result;
2039}
2040
2041// replace_copy_if
2042
2043template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
Marshall Clow12c74232018-01-19 18:07:292044inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162045_OutputIterator
2046replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2047 _Predicate __pred, const _Tp& __new_value)
2048{
Eric Fiselier910285b2014-10-27 19:28:202049 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant3e519522010-05-11 19:42:162050 if (__pred(*__first))
2051 *__result = __new_value;
2052 else
2053 *__result = *__first;
2054 return __result;
2055}
2056
2057// fill_n
2058
2059template <class _OutputIterator, class _Size, class _Tp>
Marshall Clow4bfb9312018-01-20 20:14:322060inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162061_OutputIterator
Howard Hinnant0f242be2013-08-01 17:29:282062__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:162063{
Eric Fiselier910285b2014-10-27 19:28:202064 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnante4383372011-10-22 20:59:452065 *__first = __value_;
Howard Hinnant3e519522010-05-11 19:42:162066 return __first;
2067}
2068
Howard Hinnant3e519522010-05-11 19:42:162069template <class _OutputIterator, class _Size, class _Tp>
Marshall Clow4bfb9312018-01-20 20:14:322070inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162071_OutputIterator
Howard Hinnante4383372011-10-22 20:59:452072fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:162073{
Arthur O'Dwyerc0428b32020-12-08 04:42:472074 return _VSTD::__fill_n(__first, _VSTD::__convert_to_integral(__n), __value_);
Howard Hinnant3e519522010-05-11 19:42:162075}
2076
2077// fill
2078
2079template <class _ForwardIterator, class _Tp>
Marshall Clow4bfb9312018-01-20 20:14:322080inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162081void
Howard Hinnante4383372011-10-22 20:59:452082__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
Howard Hinnant3e519522010-05-11 19:42:162083{
2084 for (; __first != __last; ++__first)
Howard Hinnante4383372011-10-22 20:59:452085 *__first = __value_;
Howard Hinnant3e519522010-05-11 19:42:162086}
2087
2088template <class _RandomAccessIterator, class _Tp>
Marshall Clow4bfb9312018-01-20 20:14:322089inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162090void
Howard Hinnante4383372011-10-22 20:59:452091__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
Howard Hinnant3e519522010-05-11 19:42:162092{
Howard Hinnante4383372011-10-22 20:59:452093 _VSTD::fill_n(__first, __last - __first, __value_);
Howard Hinnant3e519522010-05-11 19:42:162094}
2095
2096template <class _ForwardIterator, class _Tp>
Marshall Clow4bfb9312018-01-20 20:14:322097inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162098void
Howard Hinnante4383372011-10-22 20:59:452099fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:162100{
Howard Hinnante4383372011-10-22 20:59:452101 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnant3e519522010-05-11 19:42:162102}
2103
2104// generate
2105
2106template <class _ForwardIterator, class _Generator>
Marshall Clow4bfb9312018-01-20 20:14:322107inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162108void
2109generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2110{
2111 for (; __first != __last; ++__first)
2112 *__first = __gen();
2113}
2114
2115// generate_n
2116
2117template <class _OutputIterator, class _Size, class _Generator>
Nico Weber1362d7e2019-04-03 18:13:082118inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162119_OutputIterator
Eric Fiselier51544022015-02-10 16:46:422120generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
Howard Hinnant3e519522010-05-11 19:42:162121{
Arthur O'Dwyerc0428b32020-12-08 04:42:472122 typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
Eric Fiselier51544022015-02-10 16:46:422123 _IntegralSize __n = __orig_n;
Eric Fiselier910285b2014-10-27 19:28:202124 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnant3e519522010-05-11 19:42:162125 *__first = __gen();
2126 return __first;
2127}
2128
2129// remove
2130
2131template <class _ForwardIterator, class _Tp>
Nico Weber1362d7e2019-04-03 18:13:082132_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnante4383372011-10-22 20:59:452133remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:162134{
Howard Hinnante4383372011-10-22 20:59:452135 __first = _VSTD::find(__first, __last, __value_);
Howard Hinnant3e519522010-05-11 19:42:162136 if (__first != __last)
2137 {
2138 _ForwardIterator __i = __first;
2139 while (++__i != __last)
2140 {
Howard Hinnante4383372011-10-22 20:59:452141 if (!(*__i == __value_))
Howard Hinnant3e519522010-05-11 19:42:162142 {
Howard Hinnantce48a112011-06-30 21:18:192143 *__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:162144 ++__first;
2145 }
2146 }
2147 }
2148 return __first;
2149}
2150
2151// remove_if
2152
2153template <class _ForwardIterator, class _Predicate>
Nico Weber1362d7e2019-04-03 18:13:082154_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:162155remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2156{
Howard Hinnantce48a112011-06-30 21:18:192157 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
Howard Hinnant3e519522010-05-11 19:42:162158 (__first, __last, __pred);
2159 if (__first != __last)
2160 {
2161 _ForwardIterator __i = __first;
2162 while (++__i != __last)
2163 {
2164 if (!__pred(*__i))
2165 {
Howard Hinnantce48a112011-06-30 21:18:192166 *__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:162167 ++__first;
2168 }
2169 }
2170 }
2171 return __first;
2172}
2173
2174// remove_copy
2175
2176template <class _InputIterator, class _OutputIterator, class _Tp>
Marshall Clowe8ea8292018-01-22 21:43:042177inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162178_OutputIterator
Howard Hinnante4383372011-10-22 20:59:452179remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:162180{
2181 for (; __first != __last; ++__first)
2182 {
Howard Hinnante4383372011-10-22 20:59:452183 if (!(*__first == __value_))
Howard Hinnant3e519522010-05-11 19:42:162184 {
2185 *__result = *__first;
2186 ++__result;
2187 }
2188 }
2189 return __result;
2190}
2191
2192// remove_copy_if
2193
2194template <class _InputIterator, class _OutputIterator, class _Predicate>
Marshall Clowe8ea8292018-01-22 21:43:042195inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162196_OutputIterator
2197remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2198{
2199 for (; __first != __last; ++__first)
2200 {
2201 if (!__pred(*__first))
2202 {
2203 *__result = *__first;
2204 ++__result;
2205 }
2206 }
2207 return __result;
2208}
2209
2210// unique
2211
2212template <class _ForwardIterator, class _BinaryPredicate>
Nico Weber1362d7e2019-04-03 18:13:082213_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:162214unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2215{
Howard Hinnantce48a112011-06-30 21:18:192216 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnant3e519522010-05-11 19:42:162217 (__first, __last, __pred);
2218 if (__first != __last)
2219 {
2220 // ... a a ? ...
2221 // f i
2222 _ForwardIterator __i = __first;
2223 for (++__i; ++__i != __last;)
2224 if (!__pred(*__first, *__i))
Howard Hinnantce48a112011-06-30 21:18:192225 *++__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:162226 ++__first;
2227 }
2228 return __first;
2229}
2230
2231template <class _ForwardIterator>
Nico Weber1362d7e2019-04-03 18:13:082232_LIBCPP_NODISCARD_EXT inline
2233_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162234_ForwardIterator
2235unique(_ForwardIterator __first, _ForwardIterator __last)
2236{
2237 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnantce48a112011-06-30 21:18:192238 return _VSTD::unique(__first, __last, __equal_to<__v>());
Howard Hinnant3e519522010-05-11 19:42:162239}
2240
2241// unique_copy
2242
2243template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
Marshall Clow4bfb9312018-01-20 20:14:322244_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnant3e519522010-05-11 19:42:162245__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2246 input_iterator_tag, output_iterator_tag)
2247{
2248 if (__first != __last)
2249 {
2250 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2251 *__result = __t;
2252 ++__result;
2253 while (++__first != __last)
2254 {
2255 if (!__pred(__t, *__first))
2256 {
2257 __t = *__first;
2258 *__result = __t;
2259 ++__result;
2260 }
2261 }
2262 }
2263 return __result;
2264}
2265
2266template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
Marshall Clow4bfb9312018-01-20 20:14:322267_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnant3e519522010-05-11 19:42:162268__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2269 forward_iterator_tag, output_iterator_tag)
2270{
2271 if (__first != __last)
2272 {
2273 _ForwardIterator __i = __first;
2274 *__result = *__i;
2275 ++__result;
2276 while (++__first != __last)
2277 {
2278 if (!__pred(*__i, *__first))
2279 {
2280 *__result = *__first;
2281 ++__result;
2282 __i = __first;
2283 }
2284 }
2285 }
2286 return __result;
2287}
2288
2289template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
Marshall Clow4bfb9312018-01-20 20:14:322290_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:162291__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2292 input_iterator_tag, forward_iterator_tag)
2293{
2294 if (__first != __last)
2295 {
2296 *__result = *__first;
2297 while (++__first != __last)
2298 if (!__pred(*__result, *__first))
2299 *++__result = *__first;
2300 ++__result;
2301 }
2302 return __result;
2303}
2304
Howard Hinnant3e519522010-05-11 19:42:162305template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
Marshall Clow4bfb9312018-01-20 20:14:322306inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162307_OutputIterator
2308unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2309{
Howard Hinnantce48a112011-06-30 21:18:192310 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnant3e519522010-05-11 19:42:162311 (__first, __last, __result, __pred,
2312 typename iterator_traits<_InputIterator>::iterator_category(),
2313 typename iterator_traits<_OutputIterator>::iterator_category());
2314}
2315
2316template <class _InputIterator, class _OutputIterator>
Marshall Clow4bfb9312018-01-20 20:14:322317inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162318_OutputIterator
2319unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2320{
2321 typedef typename iterator_traits<_InputIterator>::value_type __v;
Howard Hinnantce48a112011-06-30 21:18:192322 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
Howard Hinnant3e519522010-05-11 19:42:162323}
2324
2325// reverse
2326
2327template <class _BidirectionalIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:082328inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162329void
2330__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2331{
2332 while (__first != __last)
2333 {
2334 if (__first == --__last)
2335 break;
Marshall Clowdef501d2015-11-02 21:34:252336 _VSTD::iter_swap(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:162337 ++__first;
2338 }
2339}
2340
2341template <class _RandomAccessIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:082342inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162343void
2344__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2345{
2346 if (__first != __last)
2347 for (; __first < --__last; ++__first)
Marshall Clowdef501d2015-11-02 21:34:252348 _VSTD::iter_swap(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:162349}
2350
2351template <class _BidirectionalIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:082352inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162353void
2354reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2355{
Howard Hinnantce48a112011-06-30 21:18:192356 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
Howard Hinnant3e519522010-05-11 19:42:162357}
2358
2359// reverse_copy
2360
2361template <class _BidirectionalIterator, class _OutputIterator>
Marshall Clowe8ea8292018-01-22 21:43:042362inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162363_OutputIterator
2364reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2365{
2366 for (; __first != __last; ++__result)
2367 *__result = *--__last;
2368 return __result;
2369}
2370
2371// rotate
2372
2373template <class _ForwardIterator>
zoecarver3ed89b52020-09-14 22:11:082374_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator
Howard Hinnantaca09de2012-08-03 18:01:202375__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
Howard Hinnant3e519522010-05-11 19:42:162376{
Howard Hinnantaca09de2012-08-03 18:01:202377 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2378 value_type __tmp = _VSTD::move(*__first);
2379 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2380 *__lm1 = _VSTD::move(__tmp);
2381 return __lm1;
2382}
2383
2384template <class _BidirectionalIterator>
zoecarver3ed89b52020-09-14 22:11:082385_LIBCPP_CONSTEXPR_AFTER_CXX11 _BidirectionalIterator
Howard Hinnantaca09de2012-08-03 18:01:202386__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2387{
2388 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2389 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2390 value_type __tmp = _VSTD::move(*__lm1);
2391 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2392 *__first = _VSTD::move(__tmp);
2393 return __fp1;
2394}
2395
2396template <class _ForwardIterator>
zoecarver3ed89b52020-09-14 22:11:082397_LIBCPP_CONSTEXPR_AFTER_CXX14 _ForwardIterator
Howard Hinnantaca09de2012-08-03 18:01:202398__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2399{
Howard Hinnant3e519522010-05-11 19:42:162400 _ForwardIterator __i = __middle;
2401 while (true)
2402 {
2403 swap(*__first, *__i);
2404 ++__first;
2405 if (++__i == __last)
2406 break;
2407 if (__first == __middle)
2408 __middle = __i;
2409 }
2410 _ForwardIterator __r = __first;
2411 if (__first != __middle)
2412 {
2413 __i = __middle;
2414 while (true)
2415 {
2416 swap(*__first, *__i);
2417 ++__first;
2418 if (++__i == __last)
2419 {
2420 if (__first == __middle)
2421 break;
2422 __i = __middle;
2423 }
2424 else if (__first == __middle)
2425 __middle = __i;
2426 }
2427 }
2428 return __r;
2429}
2430
2431template<typename _Integral>
2432inline _LIBCPP_INLINE_VISIBILITY
zoecarver3ed89b52020-09-14 22:11:082433_LIBCPP_CONSTEXPR_AFTER_CXX14 _Integral
Marshall Clow19b40352016-07-26 14:29:452434__algo_gcd(_Integral __x, _Integral __y)
Howard Hinnant3e519522010-05-11 19:42:162435{
2436 do
2437 {
2438 _Integral __t = __x % __y;
2439 __x = __y;
2440 __y = __t;
2441 } while (__y);
2442 return __x;
2443}
2444
2445template<typename _RandomAccessIterator>
zoecarver3ed89b52020-09-14 22:11:082446_LIBCPP_CONSTEXPR_AFTER_CXX14 _RandomAccessIterator
Howard Hinnantaca09de2012-08-03 18:01:202447__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
Howard Hinnant3e519522010-05-11 19:42:162448{
2449 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2450 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnantb3371f62010-08-22 00:02:432451
Howard Hinnant3e519522010-05-11 19:42:162452 const difference_type __m1 = __middle - __first;
2453 const difference_type __m2 = __last - __middle;
2454 if (__m1 == __m2)
2455 {
Howard Hinnantce48a112011-06-30 21:18:192456 _VSTD::swap_ranges(__first, __middle, __middle);
Howard Hinnant3e519522010-05-11 19:42:162457 return __middle;
2458 }
Marshall Clow19b40352016-07-26 14:29:452459 const difference_type __g = _VSTD::__algo_gcd(__m1, __m2);
Howard Hinnant3e519522010-05-11 19:42:162460 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2461 {
Howard Hinnantaca09de2012-08-03 18:01:202462 value_type __t(_VSTD::move(*--__p));
Howard Hinnant3e519522010-05-11 19:42:162463 _RandomAccessIterator __p1 = __p;
2464 _RandomAccessIterator __p2 = __p1 + __m1;
2465 do
2466 {
Howard Hinnantaca09de2012-08-03 18:01:202467 *__p1 = _VSTD::move(*__p2);
Howard Hinnant3e519522010-05-11 19:42:162468 __p1 = __p2;
2469 const difference_type __d = __last - __p2;
2470 if (__m1 < __d)
2471 __p2 += __m1;
2472 else
2473 __p2 = __first + (__m1 - __d);
2474 } while (__p2 != __p);
Howard Hinnantaca09de2012-08-03 18:01:202475 *__p1 = _VSTD::move(__t);
Howard Hinnant3e519522010-05-11 19:42:162476 }
2477 return __first + __m2;
2478}
2479
2480template <class _ForwardIterator>
2481inline _LIBCPP_INLINE_VISIBILITY
zoecarver3ed89b52020-09-14 22:11:082482_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator
Howard Hinnantaca09de2012-08-03 18:01:202483__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2484 _VSTD::forward_iterator_tag)
2485{
Mark de Wevercfef7c92021-05-09 16:22:522486 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2487 if (is_trivially_move_assignable<value_type>::value)
Howard Hinnantaca09de2012-08-03 18:01:202488 {
2489 if (_VSTD::next(__first) == __middle)
2490 return _VSTD::__rotate_left(__first, __last);
2491 }
2492 return _VSTD::__rotate_forward(__first, __middle, __last);
2493}
2494
2495template <class _BidirectionalIterator>
2496inline _LIBCPP_INLINE_VISIBILITY
zoecarver3ed89b52020-09-14 22:11:082497_LIBCPP_CONSTEXPR_AFTER_CXX11 _BidirectionalIterator
Howard Hinnantaca09de2012-08-03 18:01:202498__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
Mark de Wevercfef7c92021-05-09 16:22:522499 bidirectional_iterator_tag)
Howard Hinnantaca09de2012-08-03 18:01:202500{
Mark de Wevercfef7c92021-05-09 16:22:522501 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2502 if (is_trivially_move_assignable<value_type>::value)
Howard Hinnantaca09de2012-08-03 18:01:202503 {
2504 if (_VSTD::next(__first) == __middle)
2505 return _VSTD::__rotate_left(__first, __last);
2506 if (_VSTD::next(__middle) == __last)
2507 return _VSTD::__rotate_right(__first, __last);
2508 }
2509 return _VSTD::__rotate_forward(__first, __middle, __last);
2510}
2511
2512template <class _RandomAccessIterator>
2513inline _LIBCPP_INLINE_VISIBILITY
zoecarver3ed89b52020-09-14 22:11:082514_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator
Howard Hinnantaca09de2012-08-03 18:01:202515__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
Mark de Wevercfef7c92021-05-09 16:22:522516 random_access_iterator_tag)
Howard Hinnantaca09de2012-08-03 18:01:202517{
Mark de Wevercfef7c92021-05-09 16:22:522518 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
2519 if (is_trivially_move_assignable<value_type>::value)
Howard Hinnantaca09de2012-08-03 18:01:202520 {
2521 if (_VSTD::next(__first) == __middle)
2522 return _VSTD::__rotate_left(__first, __last);
2523 if (_VSTD::next(__middle) == __last)
2524 return _VSTD::__rotate_right(__first, __last);
2525 return _VSTD::__rotate_gcd(__first, __middle, __last);
2526 }
2527 return _VSTD::__rotate_forward(__first, __middle, __last);
2528}
2529
2530template <class _ForwardIterator>
2531inline _LIBCPP_INLINE_VISIBILITY
zoecarver3ed89b52020-09-14 22:11:082532_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:162533rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2534{
Howard Hinnantaca09de2012-08-03 18:01:202535 if (__first == __middle)
2536 return __last;
2537 if (__middle == __last)
2538 return __first;
Howard Hinnantce48a112011-06-30 21:18:192539 return _VSTD::__rotate(__first, __middle, __last,
Mark de Wevercfef7c92021-05-09 16:22:522540 typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnant3e519522010-05-11 19:42:162541}
2542
2543// rotate_copy
2544
2545template <class _ForwardIterator, class _OutputIterator>
Nicholas-Baronb552a302020-09-14 20:37:412546inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162547_OutputIterator
2548rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2549{
Howard Hinnantce48a112011-06-30 21:18:192550 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
Howard Hinnant3e519522010-05-11 19:42:162551}
2552
Howard Hinnant3e519522010-05-11 19:42:162553// min_element
2554
2555template <class _ForwardIterator, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:082556_LIBCPP_NODISCARD_EXT inline
2557_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant3e519522010-05-11 19:42:162558_ForwardIterator
Marshall Clow0b0671a2015-05-10 13:53:312559min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:162560{
Eric Fiselierf82dba02019-11-18 06:46:582561 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
Eric Fiseliera60d7fa2018-08-22 17:47:132562 "std::min_element requires a ForwardIterator");
Howard Hinnant3e519522010-05-11 19:42:162563 if (__first != __last)
2564 {
2565 _ForwardIterator __i = __first;
2566 while (++__i != __last)
2567 if (__comp(*__i, *__first))
2568 __first = __i;
2569 }
2570 return __first;
2571}
2572
2573template <class _ForwardIterator>
Nico Weber1362d7e2019-04-03 18:13:082574_LIBCPP_NODISCARD_EXT inline
2575_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant3e519522010-05-11 19:42:162576_ForwardIterator
2577min_element(_ForwardIterator __first, _ForwardIterator __last)
2578{
Marshall Clow0b0671a2015-05-10 13:53:312579 return _VSTD::min_element(__first, __last,
Howard Hinnant4eb27b72010-08-21 20:10:012580 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2581}
2582
2583// min
2584
2585template <class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:082586_LIBCPP_NODISCARD_EXT inline
2587_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012588const _Tp&
2589min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2590{
2591 return __comp(__b, __a) ? __b : __a;
2592}
2593
2594template <class _Tp>
Nico Weber1362d7e2019-04-03 18:13:082595_LIBCPP_NODISCARD_EXT inline
2596_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012597const _Tp&
2598min(const _Tp& __a, const _Tp& __b)
2599{
Howard Hinnantce48a112011-06-30 21:18:192600 return _VSTD::min(__a, __b, __less<_Tp>());
Howard Hinnant4eb27b72010-08-21 20:10:012601}
2602
Eric Fiselierddda4562017-04-18 23:26:472603#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022604
Howard Hinnant4eb27b72010-08-21 20:10:012605template<class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:082606_LIBCPP_NODISCARD_EXT inline
2607_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012608_Tp
2609min(initializer_list<_Tp> __t, _Compare __comp)
2610{
Marshall Clow0b0671a2015-05-10 13:53:312611 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
Howard Hinnant4eb27b72010-08-21 20:10:012612}
2613
2614template<class _Tp>
Nico Weber1362d7e2019-04-03 18:13:082615_LIBCPP_NODISCARD_EXT inline
2616_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012617_Tp
2618min(initializer_list<_Tp> __t)
2619{
Marshall Clow0b0671a2015-05-10 13:53:312620 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnant3e519522010-05-11 19:42:162621}
2622
Louis Dionne4cd6ca12021-04-20 16:03:322623#endif // _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022624
Howard Hinnant3e519522010-05-11 19:42:162625// max_element
2626
2627template <class _ForwardIterator, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:082628_LIBCPP_NODISCARD_EXT inline
2629_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant3e519522010-05-11 19:42:162630_ForwardIterator
Marshall Clow0b0671a2015-05-10 13:53:312631max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:162632{
Eric Fiselierf82dba02019-11-18 06:46:582633 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
Eric Fiseliera60d7fa2018-08-22 17:47:132634 "std::max_element requires a ForwardIterator");
Howard Hinnant3e519522010-05-11 19:42:162635 if (__first != __last)
2636 {
2637 _ForwardIterator __i = __first;
2638 while (++__i != __last)
2639 if (__comp(*__first, *__i))
2640 __first = __i;
2641 }
2642 return __first;
2643}
2644
Marshall Clow9d67c6d2014-02-19 16:51:352645
Howard Hinnant3e519522010-05-11 19:42:162646template <class _ForwardIterator>
Nico Weber1362d7e2019-04-03 18:13:082647_LIBCPP_NODISCARD_EXT inline
2648_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant3e519522010-05-11 19:42:162649_ForwardIterator
2650max_element(_ForwardIterator __first, _ForwardIterator __last)
2651{
Marshall Clow0b0671a2015-05-10 13:53:312652 return _VSTD::max_element(__first, __last,
Howard Hinnant4eb27b72010-08-21 20:10:012653 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2654}
2655
2656// max
2657
2658template <class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:082659_LIBCPP_NODISCARD_EXT inline
2660_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012661const _Tp&
2662max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2663{
2664 return __comp(__a, __b) ? __b : __a;
2665}
2666
2667template <class _Tp>
Nico Weber1362d7e2019-04-03 18:13:082668_LIBCPP_NODISCARD_EXT inline
2669_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012670const _Tp&
2671max(const _Tp& __a, const _Tp& __b)
2672{
Howard Hinnantce48a112011-06-30 21:18:192673 return _VSTD::max(__a, __b, __less<_Tp>());
Howard Hinnant4eb27b72010-08-21 20:10:012674}
2675
Eric Fiselierddda4562017-04-18 23:26:472676#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022677
Howard Hinnant4eb27b72010-08-21 20:10:012678template<class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:082679_LIBCPP_NODISCARD_EXT inline
2680_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012681_Tp
2682max(initializer_list<_Tp> __t, _Compare __comp)
2683{
Marshall Clow0b0671a2015-05-10 13:53:312684 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
Howard Hinnant4eb27b72010-08-21 20:10:012685}
2686
2687template<class _Tp>
Nico Weber1362d7e2019-04-03 18:13:082688_LIBCPP_NODISCARD_EXT inline
2689_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012690_Tp
2691max(initializer_list<_Tp> __t)
2692{
Marshall Clow0b0671a2015-05-10 13:53:312693 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnant3e519522010-05-11 19:42:162694}
2695
Louis Dionne4cd6ca12021-04-20 16:03:322696#endif // _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022697
Marshall Clow146c14a2016-03-07 22:43:492698#if _LIBCPP_STD_VER > 14
2699// clamp
2700template<class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:082701_LIBCPP_NODISCARD_EXT inline
2702_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow146c14a2016-03-07 22:43:492703const _Tp&
2704clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
2705{
2706 _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
2707 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
2708
2709}
2710
2711template<class _Tp>
Nico Weber1362d7e2019-04-03 18:13:082712_LIBCPP_NODISCARD_EXT inline
2713_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow146c14a2016-03-07 22:43:492714const _Tp&
2715clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
2716{
2717 return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
2718}
2719#endif
2720
Howard Hinnant3e519522010-05-11 19:42:162721// minmax_element
2722
2723template <class _ForwardIterator, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:082724_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX11
Arthur O'Dwyerd586f922020-11-27 16:02:062725pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant3e519522010-05-11 19:42:162726minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2727{
Eric Fiselierf82dba02019-11-18 06:46:582728 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
Eric Fiseliera60d7fa2018-08-22 17:47:132729 "std::minmax_element requires a ForwardIterator");
Arthur O'Dwyerd586f922020-11-27 16:02:062730 pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
Howard Hinnant3e519522010-05-11 19:42:162731 if (__first != __last)
2732 {
2733 if (++__first != __last)
2734 {
2735 if (__comp(*__first, *__result.first))
Howard Hinnant3e519522010-05-11 19:42:162736 __result.first = __first;
Howard Hinnant3e519522010-05-11 19:42:162737 else
2738 __result.second = __first;
2739 while (++__first != __last)
2740 {
2741 _ForwardIterator __i = __first;
2742 if (++__first == __last)
2743 {
2744 if (__comp(*__i, *__result.first))
2745 __result.first = __i;
2746 else if (!__comp(*__i, *__result.second))
2747 __result.second = __i;
2748 break;
2749 }
2750 else
2751 {
2752 if (__comp(*__first, *__i))
2753 {
2754 if (__comp(*__first, *__result.first))
2755 __result.first = __first;
2756 if (!__comp(*__i, *__result.second))
2757 __result.second = __i;
2758 }
2759 else
2760 {
2761 if (__comp(*__i, *__result.first))
2762 __result.first = __i;
2763 if (!__comp(*__first, *__result.second))
2764 __result.second = __first;
2765 }
2766 }
2767 }
2768 }
2769 }
2770 return __result;
2771}
2772
2773template <class _ForwardIterator>
Nico Weber1362d7e2019-04-03 18:13:082774_LIBCPP_NODISCARD_EXT inline
2775_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Arthur O'Dwyerd586f922020-11-27 16:02:062776pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant3e519522010-05-11 19:42:162777minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2778{
Marshall Clow9d67c6d2014-02-19 16:51:352779 return _VSTD::minmax_element(__first, __last,
2780 __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:162781}
2782
Howard Hinnant4eb27b72010-08-21 20:10:012783// minmax
2784
2785template<class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:082786_LIBCPP_NODISCARD_EXT inline
2787_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012788pair<const _Tp&, const _Tp&>
2789minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2790{
2791 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2792 pair<const _Tp&, const _Tp&>(__a, __b);
2793}
2794
2795template<class _Tp>
Nico Weber1362d7e2019-04-03 18:13:082796_LIBCPP_NODISCARD_EXT inline
2797_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012798pair<const _Tp&, const _Tp&>
2799minmax(const _Tp& __a, const _Tp& __b)
2800{
Howard Hinnantce48a112011-06-30 21:18:192801 return _VSTD::minmax(__a, __b, __less<_Tp>());
Howard Hinnant4eb27b72010-08-21 20:10:012802}
2803
Eric Fiselierddda4562017-04-18 23:26:472804#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022805
Howard Hinnant4eb27b72010-08-21 20:10:012806template<class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:082807_LIBCPP_NODISCARD_EXT inline
2808_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012809pair<_Tp, _Tp>
2810minmax(initializer_list<_Tp> __t, _Compare __comp)
2811{
Marshall Clow9d67c6d2014-02-19 16:51:352812 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2813 _Iter __first = __t.begin();
2814 _Iter __last = __t.end();
Arthur O'Dwyerd586f922020-11-27 16:02:062815 pair<_Tp, _Tp> __result(*__first, *__first);
Marshall Clow9d67c6d2014-02-19 16:51:352816
2817 ++__first;
2818 if (__t.size() % 2 == 0)
2819 {
2820 if (__comp(*__first, __result.first))
2821 __result.first = *__first;
2822 else
2823 __result.second = *__first;
2824 ++__first;
2825 }
Aditya Kumar331fb802016-08-25 11:52:382826
Marshall Clow9d67c6d2014-02-19 16:51:352827 while (__first != __last)
2828 {
2829 _Tp __prev = *__first++;
Marshall Clow002144f2015-02-11 15:41:342830 if (__comp(*__first, __prev)) {
2831 if ( __comp(*__first, __result.first)) __result.first = *__first;
2832 if (!__comp(__prev, __result.second)) __result.second = __prev;
Marshall Clow9d67c6d2014-02-19 16:51:352833 }
2834 else {
Marshall Clow002144f2015-02-11 15:41:342835 if ( __comp(__prev, __result.first)) __result.first = __prev;
2836 if (!__comp(*__first, __result.second)) __result.second = *__first;
Marshall Clow9d67c6d2014-02-19 16:51:352837 }
Aditya Kumar331fb802016-08-25 11:52:382838
Marshall Clow9d67c6d2014-02-19 16:51:352839 __first++;
2840 }
2841 return __result;
2842}
2843
2844template<class _Tp>
Nico Weber1362d7e2019-04-03 18:13:082845_LIBCPP_NODISCARD_EXT inline
2846_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow9d67c6d2014-02-19 16:51:352847pair<_Tp, _Tp>
2848minmax(initializer_list<_Tp> __t)
2849{
2850 return _VSTD::minmax(__t, __less<_Tp>());
Howard Hinnant4eb27b72010-08-21 20:10:012851}
2852
Louis Dionne4cd6ca12021-04-20 16:03:322853#endif // _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022854
Howard Hinnant3e519522010-05-11 19:42:162855// random_shuffle
2856
Howard Hinnantf9d540b2010-05-26 17:49:342857// __independent_bits_engine
2858
Howard Hinnantc003db12011-11-29 18:15:502859template <unsigned long long _Xp, size_t _Rp>
Howard Hinnantf9d540b2010-05-26 17:49:342860struct __log2_imp
Howard Hinnant3e519522010-05-11 19:42:162861{
Howard Hinnantc003db12011-11-29 18:15:502862 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2863 : __log2_imp<_Xp, _Rp - 1>::value;
Howard Hinnant3e519522010-05-11 19:42:162864};
2865
Howard Hinnantc003db12011-11-29 18:15:502866template <unsigned long long _Xp>
2867struct __log2_imp<_Xp, 0>
Howard Hinnant3e519522010-05-11 19:42:162868{
Howard Hinnantf9d540b2010-05-26 17:49:342869 static const size_t value = 0;
Howard Hinnant3e519522010-05-11 19:42:162870};
2871
Howard Hinnantc003db12011-11-29 18:15:502872template <size_t _Rp>
2873struct __log2_imp<0, _Rp>
Howard Hinnant3e519522010-05-11 19:42:162874{
Howard Hinnantc003db12011-11-29 18:15:502875 static const size_t value = _Rp + 1;
Howard Hinnant3e519522010-05-11 19:42:162876};
2877
Eric Fiselier89918ca2017-05-31 21:20:182878template <class _UIntType, _UIntType _Xp>
Howard Hinnantf9d540b2010-05-26 17:49:342879struct __log2
Howard Hinnant3e519522010-05-11 19:42:162880{
Howard Hinnantc003db12011-11-29 18:15:502881 static const size_t value = __log2_imp<_Xp,
Eric Fiselier89918ca2017-05-31 21:20:182882 sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
Howard Hinnant3e519522010-05-11 19:42:162883};
2884
Howard Hinnantf9d540b2010-05-26 17:49:342885template<class _Engine, class _UIntType>
2886class __independent_bits_engine
Howard Hinnant3e519522010-05-11 19:42:162887{
Howard Hinnantf9d540b2010-05-26 17:49:342888public:
2889 // types
2890 typedef _UIntType result_type;
2891
2892private:
2893 typedef typename _Engine::result_type _Engine_result_type;
2894 typedef typename conditional
2895 <
2896 sizeof(_Engine_result_type) <= sizeof(result_type),
2897 result_type,
2898 _Engine_result_type
2899 >::type _Working_result_type;
2900
2901 _Engine& __e_;
2902 size_t __w_;
2903 size_t __w0_;
2904 size_t __n_;
2905 size_t __n0_;
2906 _Working_result_type __y0_;
2907 _Working_result_type __y1_;
2908 _Engine_result_type __mask0_;
2909 _Engine_result_type __mask1_;
2910
Eric Fiselierddda4562017-04-18 23:26:472911#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc003db12011-11-29 18:15:502912 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnante386b7b2012-04-02 21:00:452913 + _Working_result_type(1);
2914#else
2915 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2916 + _Working_result_type(1);
2917#endif
2918 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2919 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2920 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
Howard Hinnantf9d540b2010-05-26 17:49:342921
2922public:
2923 // constructors and seeding functions
2924 __independent_bits_engine(_Engine& __e, size_t __w);
2925
2926 // generating functions
Howard Hinnantc003db12011-11-29 18:15:502927 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantf9d540b2010-05-26 17:49:342928
2929private:
Marshall Clow08bba642017-09-20 19:38:432930 result_type __eval(false_type);
2931 result_type __eval(true_type);
Howard Hinnantf9d540b2010-05-26 17:49:342932};
2933
2934template<class _Engine, class _UIntType>
2935__independent_bits_engine<_Engine, _UIntType>
2936 ::__independent_bits_engine(_Engine& __e, size_t __w)
2937 : __e_(__e),
2938 __w_(__w)
2939{
2940 __n_ = __w_ / __m + (__w_ % __m != 0);
2941 __w0_ = __w_ / __n_;
Howard Hinnantc003db12011-11-29 18:15:502942 if (_Rp == 0)
2943 __y0_ = _Rp;
Howard Hinnantf9d540b2010-05-26 17:49:342944 else if (__w0_ < _WDt)
Howard Hinnantc003db12011-11-29 18:15:502945 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantf9d540b2010-05-26 17:49:342946 else
2947 __y0_ = 0;
Howard Hinnantc003db12011-11-29 18:15:502948 if (_Rp - __y0_ > __y0_ / __n_)
Howard Hinnantf9d540b2010-05-26 17:49:342949 {
2950 ++__n_;
2951 __w0_ = __w_ / __n_;
2952 if (__w0_ < _WDt)
Howard Hinnantc003db12011-11-29 18:15:502953 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantf9d540b2010-05-26 17:49:342954 else
2955 __y0_ = 0;
2956 }
2957 __n0_ = __n_ - __w_ % __n_;
2958 if (__w0_ < _WDt - 1)
Howard Hinnantc003db12011-11-29 18:15:502959 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
Howard Hinnantf9d540b2010-05-26 17:49:342960 else
2961 __y1_ = 0;
2962 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2963 _Engine_result_type(0);
2964 __mask1_ = __w0_ < _EDt - 1 ?
2965 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2966 _Engine_result_type(~0);
Howard Hinnant3e519522010-05-11 19:42:162967}
2968
Howard Hinnantf9d540b2010-05-26 17:49:342969template<class _Engine, class _UIntType>
2970inline
2971_UIntType
Marshall Clow08bba642017-09-20 19:38:432972__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
Howard Hinnant3e519522010-05-11 19:42:162973{
Howard Hinnantf9d540b2010-05-26 17:49:342974 return static_cast<result_type>(__e_() & __mask0_);
Howard Hinnant3e519522010-05-11 19:42:162975}
2976
Howard Hinnantf9d540b2010-05-26 17:49:342977template<class _Engine, class _UIntType>
2978_UIntType
Marshall Clow08bba642017-09-20 19:38:432979__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
Howard Hinnant3e519522010-05-11 19:42:162980{
Marshall Clow5beb2c32017-09-20 17:34:112981 const size_t _WRt = numeric_limits<result_type>::digits;
Howard Hinnantc003db12011-11-29 18:15:502982 result_type _Sp = 0;
Howard Hinnantf9d540b2010-05-26 17:49:342983 for (size_t __k = 0; __k < __n0_; ++__k)
2984 {
2985 _Engine_result_type __u;
2986 do
2987 {
2988 __u = __e_() - _Engine::min();
2989 } while (__u >= __y0_);
Marshall Clow5beb2c32017-09-20 17:34:112990 if (__w0_ < _WRt)
Howard Hinnantc003db12011-11-29 18:15:502991 _Sp <<= __w0_;
Howard Hinnantf9d540b2010-05-26 17:49:342992 else
Howard Hinnantc003db12011-11-29 18:15:502993 _Sp = 0;
2994 _Sp += __u & __mask0_;
Howard Hinnantf9d540b2010-05-26 17:49:342995 }
2996 for (size_t __k = __n0_; __k < __n_; ++__k)
2997 {
2998 _Engine_result_type __u;
2999 do
3000 {
3001 __u = __e_() - _Engine::min();
3002 } while (__u >= __y1_);
Marshall Clow5beb2c32017-09-20 17:34:113003 if (__w0_ < _WRt - 1)
Howard Hinnantc003db12011-11-29 18:15:503004 _Sp <<= __w0_ + 1;
Howard Hinnantf9d540b2010-05-26 17:49:343005 else
Howard Hinnantc003db12011-11-29 18:15:503006 _Sp = 0;
3007 _Sp += __u & __mask1_;
Howard Hinnantf9d540b2010-05-26 17:49:343008 }
Howard Hinnantc003db12011-11-29 18:15:503009 return _Sp;
Howard Hinnantf9d540b2010-05-26 17:49:343010}
3011
3012// uniform_int_distribution
3013
3014template<class _IntType = int>
3015class uniform_int_distribution
3016{
3017public:
3018 // types
3019 typedef _IntType result_type;
3020
3021 class param_type
3022 {
3023 result_type __a_;
3024 result_type __b_;
3025 public:
3026 typedef uniform_int_distribution distribution_type;
3027
3028 explicit param_type(result_type __a = 0,
3029 result_type __b = numeric_limits<result_type>::max())
3030 : __a_(__a), __b_(__b) {}
3031
3032 result_type a() const {return __a_;}
3033 result_type b() const {return __b_;}
3034
3035 friend bool operator==(const param_type& __x, const param_type& __y)
3036 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3037 friend bool operator!=(const param_type& __x, const param_type& __y)
3038 {return !(__x == __y);}
3039 };
3040
3041private:
3042 param_type __p_;
3043
3044public:
3045 // constructors and reset functions
Marek Kurdeja11f8b12021-01-19 07:21:093046#ifndef _LIBCPP_CXX03_LANG
3047 uniform_int_distribution() : uniform_int_distribution(0) {}
3048 explicit uniform_int_distribution(
3049 result_type __a, result_type __b = numeric_limits<result_type>::max())
Howard Hinnantf9d540b2010-05-26 17:49:343050 : __p_(param_type(__a, __b)) {}
Marek Kurdeja11f8b12021-01-19 07:21:093051#else
3052 explicit uniform_int_distribution(
3053 result_type __a = 0,
3054 result_type __b = numeric_limits<result_type>::max())
3055 : __p_(param_type(__a, __b)) {}
3056#endif
Howard Hinnantf9d540b2010-05-26 17:49:343057 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
3058 void reset() {}
3059
3060 // generating functions
3061 template<class _URNG> result_type operator()(_URNG& __g)
3062 {return (*this)(__g, __p_);}
3063 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3064
3065 // property functions
3066 result_type a() const {return __p_.a();}
3067 result_type b() const {return __p_.b();}
3068
3069 param_type param() const {return __p_;}
3070 void param(const param_type& __p) {__p_ = __p;}
3071
3072 result_type min() const {return a();}
3073 result_type max() const {return b();}
3074
3075 friend bool operator==(const uniform_int_distribution& __x,
3076 const uniform_int_distribution& __y)
3077 {return __x.__p_ == __y.__p_;}
3078 friend bool operator!=(const uniform_int_distribution& __x,
3079 const uniform_int_distribution& __y)
3080 {return !(__x == __y);}
3081};
3082
3083template<class _IntType>
3084template<class _URNG>
3085typename uniform_int_distribution<_IntType>::result_type
3086uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
Marshall Clow2dd53352018-10-08 20:20:343087_LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantf9d540b2010-05-26 17:49:343088{
3089 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3090 uint32_t, uint64_t>::type _UIntType;
Marshall Clow2dd53352018-10-08 20:20:343091 const _UIntType _Rp = _UIntType(__p.b()) - _UIntType(__p.a()) + _UIntType(1);
Howard Hinnantc003db12011-11-29 18:15:503092 if (_Rp == 1)
Howard Hinnantf9d540b2010-05-26 17:49:343093 return __p.a();
3094 const size_t _Dt = numeric_limits<_UIntType>::digits;
3095 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
Howard Hinnantc003db12011-11-29 18:15:503096 if (_Rp == 0)
Howard Hinnantf9d540b2010-05-26 17:49:343097 return static_cast<result_type>(_Eng(__g, _Dt)());
Marshall Clowf3b851f2019-07-12 01:01:553098 size_t __w = _Dt - __libcpp_clz(_Rp) - 1;
Arthur O'Dwyerd586f922020-11-27 16:02:063099 if ((_Rp & (numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
Howard Hinnantf9d540b2010-05-26 17:49:343100 ++__w;
3101 _Eng __e(__g, __w);
3102 _UIntType __u;
Howard Hinnant3e519522010-05-11 19:42:163103 do
Howard Hinnantf9d540b2010-05-26 17:49:343104 {
3105 __u = __e();
Howard Hinnantc003db12011-11-29 18:15:503106 } while (__u >= _Rp);
Howard Hinnantf9d540b2010-05-26 17:49:343107 return static_cast<result_type>(__u + __p.a());
Howard Hinnant3e519522010-05-11 19:42:163108}
3109
Eric Fiselier66f1ec42017-04-03 23:23:443110#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \
3111 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnantf0544c22013-08-12 18:38:343112class _LIBCPP_TYPE_VIS __rs_default;
Howard Hinnant3e519522010-05-11 19:42:163113
Howard Hinnantf0544c22013-08-12 18:38:343114_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantf9d540b2010-05-26 17:49:343115
Howard Hinnantf0544c22013-08-12 18:38:343116class _LIBCPP_TYPE_VIS __rs_default
Howard Hinnant3e519522010-05-11 19:42:163117{
Howard Hinnantf9d540b2010-05-26 17:49:343118 static unsigned __c_;
3119
3120 __rs_default();
3121public:
Marshall Clowb6e5f852013-02-07 22:12:023122 typedef uint_fast32_t result_type;
Howard Hinnantf9d540b2010-05-26 17:49:343123
3124 static const result_type _Min = 0;
3125 static const result_type _Max = 0xFFFFFFFF;
3126
3127 __rs_default(const __rs_default&);
3128 ~__rs_default();
3129
3130 result_type operator()();
3131
Howard Hinnant788c9972012-04-02 00:40:413132 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
3133 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
Howard Hinnantf9d540b2010-05-26 17:49:343134
Howard Hinnantf0544c22013-08-12 18:38:343135 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnant3e519522010-05-11 19:42:163136};
3137
Howard Hinnantf0544c22013-08-12 18:38:343138_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnant3e519522010-05-11 19:42:163139
3140template <class _RandomAccessIterator>
Louis Dionneea5cd3b2018-09-23 18:35:003141_LIBCPP_DEPRECATED_IN_CXX14 void
Howard Hinnant3e519522010-05-11 19:42:163142random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
3143{
3144 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc003db12011-11-29 18:15:503145 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3146 typedef typename _Dp::param_type _Pp;
Howard Hinnant3e519522010-05-11 19:42:163147 difference_type __d = __last - __first;
3148 if (__d > 1)
3149 {
Howard Hinnantc003db12011-11-29 18:15:503150 _Dp __uid;
Howard Hinnantf9d540b2010-05-26 17:49:343151 __rs_default __g = __rs_get();
Marshall Clow5a8525e2019-01-24 19:20:193152 for (--__last, (void) --__d; __first < __last; ++__first, (void) --__d)
Howard Hinnant007b26b2010-10-22 15:26:393153 {
Howard Hinnantc003db12011-11-29 18:15:503154 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant007b26b2010-10-22 15:26:393155 if (__i != difference_type(0))
3156 swap(*__first, *(__first + __i));
3157 }
Howard Hinnant3e519522010-05-11 19:42:163158 }
3159}
3160
3161template <class _RandomAccessIterator, class _RandomNumberGenerator>
Louis Dionneea5cd3b2018-09-23 18:35:003162_LIBCPP_DEPRECATED_IN_CXX14 void
Howard Hinnant3e519522010-05-11 19:42:163163random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselierddda4562017-04-18 23:26:473164#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:163165 _RandomNumberGenerator&& __rand)
3166#else
3167 _RandomNumberGenerator& __rand)
3168#endif
3169{
3170 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3171 difference_type __d = __last - __first;
3172 if (__d > 1)
3173 {
Marshall Clow5a8525e2019-01-24 19:20:193174 for (--__last; __first < __last; ++__first, (void) --__d)
Howard Hinnant007b26b2010-10-22 15:26:393175 {
3176 difference_type __i = __rand(__d);
Marshall Clowe9cc5452018-09-11 18:33:453177 if (__i != difference_type(0))
Marshall Clow2dd53352018-10-08 20:20:343178 swap(*__first, *(__first + __i));
Howard Hinnant007b26b2010-10-22 15:26:393179 }
Howard Hinnant3e519522010-05-11 19:42:163180 }
3181}
Marshall Clow0f37a412017-03-23 13:43:373182#endif
Howard Hinnant3e519522010-05-11 19:42:163183
Eric Fiseliere7154702016-08-28 22:14:373184template <class _PopulationIterator, class _SampleIterator, class _Distance,
3185 class _UniformRandomNumberGenerator>
3186_LIBCPP_INLINE_VISIBILITY
3187_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardson42bfedd2017-11-14 11:14:253188 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiseliere7154702016-08-28 22:14:373189 _Distance __n,
3190 _UniformRandomNumberGenerator & __g,
3191 input_iterator_tag) {
3192
3193 _Distance __k = 0;
Marshall Clow7fa68652019-08-20 21:31:513194 for (; __first != __last && __k < __n; ++__first, (void) ++__k)
Alexander Richardson42bfedd2017-11-14 11:14:253195 __output_iter[__k] = *__first;
Eric Fiseliere7154702016-08-28 22:14:373196 _Distance __sz = __k;
Marshall Clow7fa68652019-08-20 21:31:513197 for (; __first != __last; ++__first, (void) ++__k) {
Arthur O'Dwyer6491d992021-05-10 17:13:043198 _Distance __r = uniform_int_distribution<_Distance>(0, __k)(__g);
Eric Fiseliere7154702016-08-28 22:14:373199 if (__r < __sz)
Alexander Richardson42bfedd2017-11-14 11:14:253200 __output_iter[__r] = *__first;
Eric Fiseliere7154702016-08-28 22:14:373201 }
Alexander Richardson42bfedd2017-11-14 11:14:253202 return __output_iter + _VSTD::min(__n, __k);
Eric Fiseliere7154702016-08-28 22:14:373203}
3204
3205template <class _PopulationIterator, class _SampleIterator, class _Distance,
3206 class _UniformRandomNumberGenerator>
3207_LIBCPP_INLINE_VISIBILITY
3208_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardson42bfedd2017-11-14 11:14:253209 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiseliere7154702016-08-28 22:14:373210 _Distance __n,
3211 _UniformRandomNumberGenerator& __g,
3212 forward_iterator_tag) {
3213 _Distance __unsampled_sz = _VSTD::distance(__first, __last);
3214 for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
Arthur O'Dwyer6491d992021-05-10 17:13:043215 _Distance __r = uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
Eric Fiseliere7154702016-08-28 22:14:373216 if (__r < __n) {
Alexander Richardson42bfedd2017-11-14 11:14:253217 *__output_iter++ = *__first;
Eric Fiseliere7154702016-08-28 22:14:373218 --__n;
3219 }
3220 }
Alexander Richardson42bfedd2017-11-14 11:14:253221 return __output_iter;
Eric Fiseliere7154702016-08-28 22:14:373222}
3223
3224template <class _PopulationIterator, class _SampleIterator, class _Distance,
3225 class _UniformRandomNumberGenerator>
3226_LIBCPP_INLINE_VISIBILITY
3227_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardson42bfedd2017-11-14 11:14:253228 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiseliere7154702016-08-28 22:14:373229 _Distance __n, _UniformRandomNumberGenerator& __g) {
3230 typedef typename iterator_traits<_PopulationIterator>::iterator_category
3231 _PopCategory;
3232 typedef typename iterator_traits<_PopulationIterator>::difference_type
3233 _Difference;
Eric Fiselierf82dba02019-11-18 06:46:583234 static_assert(__is_cpp17_forward_iterator<_PopulationIterator>::value ||
3235 __is_cpp17_random_access_iterator<_SampleIterator>::value,
Eric Fiseliere7154702016-08-28 22:14:373236 "SampleIterator must meet the requirements of RandomAccessIterator");
3237 typedef typename common_type<_Distance, _Difference>::type _CommonType;
3238 _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
3239 return _VSTD::__sample(
Alexander Richardson42bfedd2017-11-14 11:14:253240 __first, __last, __output_iter, _CommonType(__n),
Eric Fiseliere7154702016-08-28 22:14:373241 __g, _PopCategory());
3242}
3243
3244#if _LIBCPP_STD_VER > 14
3245template <class _PopulationIterator, class _SampleIterator, class _Distance,
3246 class _UniformRandomNumberGenerator>
3247inline _LIBCPP_INLINE_VISIBILITY
3248_SampleIterator sample(_PopulationIterator __first,
Alexander Richardson42bfedd2017-11-14 11:14:253249 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiseliere7154702016-08-28 22:14:373250 _Distance __n, _UniformRandomNumberGenerator&& __g) {
Alexander Richardson42bfedd2017-11-14 11:14:253251 return _VSTD::__sample(__first, __last, __output_iter, __n, __g);
Eric Fiseliere7154702016-08-28 22:14:373252}
3253#endif // _LIBCPP_STD_VER > 14
3254
Howard Hinnantf9d540b2010-05-26 17:49:343255template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3256 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnantfb340102010-11-18 01:47:023257 _UniformRandomNumberGenerator&& __g)
Howard Hinnantf9d540b2010-05-26 17:49:343258{
3259 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc003db12011-11-29 18:15:503260 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3261 typedef typename _Dp::param_type _Pp;
Howard Hinnantf9d540b2010-05-26 17:49:343262 difference_type __d = __last - __first;
3263 if (__d > 1)
3264 {
Howard Hinnantc003db12011-11-29 18:15:503265 _Dp __uid;
Marshall Clow7fa68652019-08-20 21:31:513266 for (--__last, (void) --__d; __first < __last; ++__first, (void) --__d)
Howard Hinnant007b26b2010-10-22 15:26:393267 {
Howard Hinnantc003db12011-11-29 18:15:503268 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant007b26b2010-10-22 15:26:393269 if (__i != difference_type(0))
3270 swap(*__first, *(__first + __i));
3271 }
Howard Hinnantf9d540b2010-05-26 17:49:343272 }
3273}
3274
Arthur O'Dwyer3fbd3ea2020-12-26 06:39:033275#if _LIBCPP_STD_VER > 17
3276
3277// shift_left, shift_right
3278
3279template <class _ForwardIterator>
3280inline _LIBCPP_INLINE_VISIBILITY constexpr
3281_ForwardIterator
3282shift_left(_ForwardIterator __first, _ForwardIterator __last,
3283 typename iterator_traits<_ForwardIterator>::difference_type __n)
3284{
3285 if (__n == 0) {
3286 return __last;
3287 }
3288
3289 _ForwardIterator __m = __first;
3290 if constexpr (__is_cpp17_random_access_iterator<_ForwardIterator>::value) {
3291 if (__n >= __last - __first) {
3292 return __first;
3293 }
3294 __m += __n;
3295 } else {
3296 for (; __n > 0; --__n) {
3297 if (__m == __last) {
3298 return __first;
3299 }
3300 ++__m;
3301 }
3302 }
3303 return _VSTD::move(__m, __last, __first);
3304}
3305
3306template <class _ForwardIterator>
3307inline _LIBCPP_INLINE_VISIBILITY constexpr
3308_ForwardIterator
3309shift_right(_ForwardIterator __first, _ForwardIterator __last,
3310 typename iterator_traits<_ForwardIterator>::difference_type __n)
3311{
3312 if (__n == 0) {
3313 return __first;
3314 }
3315
3316 if constexpr (__is_cpp17_random_access_iterator<_ForwardIterator>::value) {
3317 decltype(__n) __d = __last - __first;
3318 if (__n >= __d) {
3319 return __last;
3320 }
3321 _ForwardIterator __m = __first + (__d - __n);
3322 return _VSTD::move_backward(__first, __m, __last);
3323 } else if constexpr (__is_cpp17_bidirectional_iterator<_ForwardIterator>::value) {
3324 _ForwardIterator __m = __last;
3325 for (; __n > 0; --__n) {
3326 if (__m == __first) {
3327 return __last;
3328 }
3329 --__m;
3330 }
3331 return _VSTD::move_backward(__first, __m, __last);
3332 } else {
3333 _ForwardIterator __ret = __first;
3334 for (; __n > 0; --__n) {
3335 if (__ret == __last) {
3336 return __last;
3337 }
3338 ++__ret;
3339 }
3340
3341 // We have an __n-element scratch space from __first to __ret.
3342 // Slide an __n-element window [__trail, __lead) from left to right.
3343 // We're essentially doing swap_ranges(__first, __ret, __trail, __lead)
3344 // over and over; but once __lead reaches __last we needn't bother
3345 // to save the values of elements [__trail, __last).
3346
3347 auto __trail = __first;
3348 auto __lead = __ret;
3349 while (__trail != __ret) {
3350 if (__lead == __last) {
3351 _VSTD::move(__first, __trail, __ret);
3352 return __ret;
3353 }
3354 ++__trail;
3355 ++__lead;
3356 }
3357
3358 _ForwardIterator __mid = __first;
3359 while (true) {
3360 if (__lead == __last) {
3361 __trail = _VSTD::move(__mid, __ret, __trail);
3362 _VSTD::move(__first, __mid, __trail);
3363 return __ret;
3364 }
3365 swap(*__mid, *__trail);
3366 ++__mid;
3367 ++__trail;
3368 ++__lead;
3369 if (__mid == __ret) {
3370 __mid = __first;
3371 }
3372 }
3373 }
3374}
3375
3376#endif // _LIBCPP_STD_VER > 17
3377
3378// is_partitioned
3379
Howard Hinnant3e519522010-05-11 19:42:163380template <class _InputIterator, class _Predicate>
Nico Weber1362d7e2019-04-03 18:13:083381_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnant3e519522010-05-11 19:42:163382is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3383{
3384 for (; __first != __last; ++__first)
3385 if (!__pred(*__first))
3386 break;
Marshall Clowb9595b72015-02-02 18:16:353387 if ( __first == __last )
3388 return true;
3389 ++__first;
Howard Hinnant3e519522010-05-11 19:42:163390 for (; __first != __last; ++__first)
3391 if (__pred(*__first))
3392 return false;
3393 return true;
3394}
3395
3396// partition
3397
3398template <class _Predicate, class _ForwardIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:083399_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:163400__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3401{
3402 while (true)
3403 {
3404 if (__first == __last)
3405 return __first;
3406 if (!__pred(*__first))
3407 break;
3408 ++__first;
3409 }
3410 for (_ForwardIterator __p = __first; ++__p != __last;)
3411 {
3412 if (__pred(*__p))
3413 {
3414 swap(*__first, *__p);
3415 ++__first;
3416 }
3417 }
3418 return __first;
3419}
3420
3421template <class _Predicate, class _BidirectionalIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:083422_LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator
Howard Hinnant3e519522010-05-11 19:42:163423__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3424 bidirectional_iterator_tag)
3425{
3426 while (true)
3427 {
3428 while (true)
3429 {
3430 if (__first == __last)
3431 return __first;
3432 if (!__pred(*__first))
3433 break;
3434 ++__first;
3435 }
3436 do
3437 {
3438 if (__first == --__last)
3439 return __first;
3440 } while (!__pred(*__last));
3441 swap(*__first, *__last);
3442 ++__first;
3443 }
3444}
3445
3446template <class _ForwardIterator, class _Predicate>
Arthur O'Dwyerf851db32020-12-17 05:01:083447inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:163448_ForwardIterator
3449partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3450{
Howard Hinnantce48a112011-06-30 21:18:193451 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnant3e519522010-05-11 19:42:163452 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3453}
3454
3455// partition_copy
3456
3457template <class _InputIterator, class _OutputIterator1,
3458 class _OutputIterator2, class _Predicate>
Marshall Clow1b9a4ff2018-01-22 20:44:333459_LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_OutputIterator1, _OutputIterator2>
Howard Hinnant3e519522010-05-11 19:42:163460partition_copy(_InputIterator __first, _InputIterator __last,
3461 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3462 _Predicate __pred)
3463{
3464 for (; __first != __last; ++__first)
3465 {
3466 if (__pred(*__first))
3467 {
3468 *__out_true = *__first;
3469 ++__out_true;
3470 }
3471 else
3472 {
3473 *__out_false = *__first;
3474 ++__out_false;
3475 }
3476 }
3477 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3478}
3479
3480// partition_point
3481
3482template<class _ForwardIterator, class _Predicate>
Marshall Clow674f9122018-01-15 17:53:343483_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:163484partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3485{
3486 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantce48a112011-06-30 21:18:193487 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:163488 while (__len != 0)
3489 {
Louis Dionne04695a72018-12-17 16:04:393490 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnant3e519522010-05-11 19:42:163491 _ForwardIterator __m = __first;
Howard Hinnantce48a112011-06-30 21:18:193492 _VSTD::advance(__m, __l2);
Howard Hinnant3e519522010-05-11 19:42:163493 if (__pred(*__m))
3494 {
3495 __first = ++__m;
3496 __len -= __l2 + 1;
3497 }
3498 else
3499 __len = __l2;
3500 }
3501 return __first;
3502}
3503
3504// stable_partition
3505
3506template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3507_ForwardIterator
3508__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3509 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3510{
3511 // *__first is known to be false
3512 // __len >= 1
3513 if (__len == 1)
3514 return __first;
3515 if (__len == 2)
3516 {
3517 _ForwardIterator __m = __first;
3518 if (__pred(*++__m))
3519 {
3520 swap(*__first, *__m);
3521 return __m;
3522 }
3523 return __first;
3524 }
3525 if (__len <= __p.second)
3526 { // The buffer is big enough to use
3527 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3528 __destruct_n __d(0);
3529 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3530 // Move the falses into the temporary buffer, and the trues to the front of the line
3531 // Update __first to always point to the end of the trues
3532 value_type* __t = __p.first;
Arthur O'Dwyerbe4c6572020-12-12 01:30:283533 ::new ((void*)__t) value_type(_VSTD::move(*__first));
Bruce Mitchener527a7fd2020-11-24 17:53:533534 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:163535 ++__t;
3536 _ForwardIterator __i = __first;
3537 while (++__i != __last)
3538 {
3539 if (__pred(*__i))
3540 {
Howard Hinnantce48a112011-06-30 21:18:193541 *__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:163542 ++__first;
3543 }
3544 else
3545 {
Arthur O'Dwyerbe4c6572020-12-12 01:30:283546 ::new ((void*)__t) value_type(_VSTD::move(*__i));
Bruce Mitchener527a7fd2020-11-24 17:53:533547 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:163548 ++__t;
3549 }
3550 }
3551 // All trues now at start of range, all falses in buffer
3552 // Move falses back into range, but don't mess up __first which points to first false
3553 __i = __first;
Marshall Clow7fa68652019-08-20 21:31:513554 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, (void) ++__i)
Howard Hinnantce48a112011-06-30 21:18:193555 *__i = _VSTD::move(*__t2);
Howard Hinnant3e519522010-05-11 19:42:163556 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3557 return __first;
3558 }
3559 // Else not enough buffer, do in place
3560 // __len >= 3
3561 _ForwardIterator __m = __first;
3562 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnantce48a112011-06-30 21:18:193563 _VSTD::advance(__m, __len2);
Howard Hinnant3e519522010-05-11 19:42:163564 // recurse on [__first, __m), *__first know to be false
3565 // F?????????????????
3566 // f m l
3567 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
Arthur O'Dwyer19688042020-11-22 18:21:113568 _ForwardIterator __first_false = _VSTD::__stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
Howard Hinnant3e519522010-05-11 19:42:163569 // TTTFFFFF??????????
3570 // f ff m l
3571 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3572 _ForwardIterator __m1 = __m;
3573 _ForwardIterator __second_false = __last;
3574 _Distance __len_half = __len - __len2;
3575 while (__pred(*__m1))
3576 {
3577 if (++__m1 == __last)
3578 goto __second_half_done;
3579 --__len_half;
3580 }
3581 // TTTFFFFFTTTF??????
3582 // f ff m m1 l
Arthur O'Dwyer19688042020-11-22 18:21:113583 __second_false = _VSTD::__stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
Howard Hinnant3e519522010-05-11 19:42:163584__second_half_done:
3585 // TTTFFFFFTTTTTFFFFF
3586 // f ff m sf l
Howard Hinnantce48a112011-06-30 21:18:193587 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnant3e519522010-05-11 19:42:163588 // TTTTTTTTFFFFFFFFFF
3589 // |
3590}
3591
3592struct __return_temporary_buffer
3593{
3594 template <class _Tp>
Howard Hinnantce48a112011-06-30 21:18:193595 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
Howard Hinnant3e519522010-05-11 19:42:163596};
3597
3598template <class _Predicate, class _ForwardIterator>
3599_ForwardIterator
3600__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3601 forward_iterator_tag)
3602{
3603 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3604 // Either prove all true and return __first or point to first false
3605 while (true)
3606 {
3607 if (__first == __last)
3608 return __first;
3609 if (!__pred(*__first))
3610 break;
3611 ++__first;
3612 }
3613 // We now have a reduced range [__first, __last)
3614 // *__first is known to be false
3615 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3616 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnantce48a112011-06-30 21:18:193617 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:163618 pair<value_type*, ptrdiff_t> __p(0, 0);
3619 unique_ptr<value_type, __return_temporary_buffer> __h;
3620 if (__len >= __alloc_limit)
3621 {
Howard Hinnantce48a112011-06-30 21:18:193622 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnant3e519522010-05-11 19:42:163623 __h.reset(__p.first);
3624 }
Arthur O'Dwyer19688042020-11-22 18:21:113625 return _VSTD::__stable_partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnant3e519522010-05-11 19:42:163626 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3627}
3628
3629template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3630_BidirectionalIterator
3631__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3632 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3633{
3634 // *__first is known to be false
3635 // *__last is known to be true
3636 // __len >= 2
3637 if (__len == 2)
3638 {
3639 swap(*__first, *__last);
3640 return __last;
3641 }
3642 if (__len == 3)
3643 {
3644 _BidirectionalIterator __m = __first;
3645 if (__pred(*++__m))
3646 {
3647 swap(*__first, *__m);
3648 swap(*__m, *__last);
3649 return __last;
3650 }
3651 swap(*__m, *__last);
3652 swap(*__first, *__m);
3653 return __m;
3654 }
3655 if (__len <= __p.second)
3656 { // The buffer is big enough to use
3657 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3658 __destruct_n __d(0);
3659 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3660 // Move the falses into the temporary buffer, and the trues to the front of the line
3661 // Update __first to always point to the end of the trues
3662 value_type* __t = __p.first;
Arthur O'Dwyerbe4c6572020-12-12 01:30:283663 ::new ((void*)__t) value_type(_VSTD::move(*__first));
Bruce Mitchener527a7fd2020-11-24 17:53:533664 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:163665 ++__t;
3666 _BidirectionalIterator __i = __first;
3667 while (++__i != __last)
3668 {
3669 if (__pred(*__i))
3670 {
Howard Hinnantce48a112011-06-30 21:18:193671 *__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:163672 ++__first;
3673 }
3674 else
3675 {
Arthur O'Dwyerbe4c6572020-12-12 01:30:283676 ::new ((void*)__t) value_type(_VSTD::move(*__i));
Bruce Mitchener527a7fd2020-11-24 17:53:533677 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:163678 ++__t;
3679 }
3680 }
3681 // move *__last, known to be true
Howard Hinnantce48a112011-06-30 21:18:193682 *__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:163683 __i = ++__first;
3684 // All trues now at start of range, all falses in buffer
3685 // Move falses back into range, but don't mess up __first which points to first false
Marshall Clow7fa68652019-08-20 21:31:513686 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, (void) ++__i)
Howard Hinnantce48a112011-06-30 21:18:193687 *__i = _VSTD::move(*__t2);
Howard Hinnant3e519522010-05-11 19:42:163688 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3689 return __first;
3690 }
3691 // Else not enough buffer, do in place
3692 // __len >= 4
3693 _BidirectionalIterator __m = __first;
3694 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnantce48a112011-06-30 21:18:193695 _VSTD::advance(__m, __len2);
Howard Hinnant3e519522010-05-11 19:42:163696 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3697 // F????????????????T
3698 // f m l
3699 _BidirectionalIterator __m1 = __m;
3700 _BidirectionalIterator __first_false = __first;
3701 _Distance __len_half = __len2;
3702 while (!__pred(*--__m1))
3703 {
3704 if (__m1 == __first)
3705 goto __first_half_done;
3706 --__len_half;
3707 }
3708 // F???TFFF?????????T
3709 // f m1 m l
3710 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
Arthur O'Dwyer19688042020-11-22 18:21:113711 __first_false = _VSTD::__stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
Howard Hinnant3e519522010-05-11 19:42:163712__first_half_done:
3713 // TTTFFFFF?????????T
3714 // f ff m l
3715 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3716 __m1 = __m;
3717 _BidirectionalIterator __second_false = __last;
3718 ++__second_false;
3719 __len_half = __len - __len2;
3720 while (__pred(*__m1))
3721 {
3722 if (++__m1 == __last)
3723 goto __second_half_done;
3724 --__len_half;
3725 }
3726 // TTTFFFFFTTTF?????T
3727 // f ff m m1 l
Arthur O'Dwyer19688042020-11-22 18:21:113728 __second_false = _VSTD::__stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
Howard Hinnant3e519522010-05-11 19:42:163729__second_half_done:
3730 // TTTFFFFFTTTTTFFFFF
3731 // f ff m sf l
Howard Hinnantce48a112011-06-30 21:18:193732 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnant3e519522010-05-11 19:42:163733 // TTTTTTTTFFFFFFFFFF
3734 // |
3735}
3736
3737template <class _Predicate, class _BidirectionalIterator>
3738_BidirectionalIterator
3739__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3740 bidirectional_iterator_tag)
3741{
3742 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3743 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3744 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3745 // Either prove all true and return __first or point to first false
3746 while (true)
3747 {
3748 if (__first == __last)
3749 return __first;
3750 if (!__pred(*__first))
3751 break;
3752 ++__first;
3753 }
3754 // __first points to first false, everything prior to __first is already set.
3755 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3756 do
3757 {
3758 if (__first == --__last)
3759 return __first;
3760 } while (!__pred(*__last));
3761 // We now have a reduced range [__first, __last]
3762 // *__first is known to be false
3763 // *__last is known to be true
3764 // __len >= 2
Howard Hinnantce48a112011-06-30 21:18:193765 difference_type __len = _VSTD::distance(__first, __last) + 1;
Howard Hinnant3e519522010-05-11 19:42:163766 pair<value_type*, ptrdiff_t> __p(0, 0);
3767 unique_ptr<value_type, __return_temporary_buffer> __h;
3768 if (__len >= __alloc_limit)
3769 {
Howard Hinnantce48a112011-06-30 21:18:193770 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnant3e519522010-05-11 19:42:163771 __h.reset(__p.first);
3772 }
Arthur O'Dwyer19688042020-11-22 18:21:113773 return _VSTD::__stable_partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnant3e519522010-05-11 19:42:163774 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3775}
3776
3777template <class _ForwardIterator, class _Predicate>
3778inline _LIBCPP_INLINE_VISIBILITY
3779_ForwardIterator
3780stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3781{
Arthur O'Dwyer19688042020-11-22 18:21:113782 return _VSTD::__stable_partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnant3e519522010-05-11 19:42:163783 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3784}
3785
3786// is_sorted_until
3787
3788template <class _ForwardIterator, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:083789_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:163790is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3791{
3792 if (__first != __last)
3793 {
3794 _ForwardIterator __i = __first;
3795 while (++__i != __last)
3796 {
3797 if (__comp(*__i, *__first))
3798 return __i;
3799 __first = __i;
3800 }
3801 }
3802 return __last;
3803}
3804
Howard Hinnantb3371f62010-08-22 00:02:433805template<class _ForwardIterator>
Nico Weber1362d7e2019-04-03 18:13:083806_LIBCPP_NODISCARD_EXT inline
3807_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:163808_ForwardIterator
3809is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3810{
Howard Hinnantce48a112011-06-30 21:18:193811 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:163812}
3813
3814// is_sorted
3815
3816template <class _ForwardIterator, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:083817_LIBCPP_NODISCARD_EXT inline
3818_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:163819bool
3820is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3821{
Howard Hinnantce48a112011-06-30 21:18:193822 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
Howard Hinnant3e519522010-05-11 19:42:163823}
3824
Howard Hinnantb3371f62010-08-22 00:02:433825template<class _ForwardIterator>
Nico Weber1362d7e2019-04-03 18:13:083826_LIBCPP_NODISCARD_EXT inline
3827_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:163828bool
3829is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3830{
Howard Hinnantce48a112011-06-30 21:18:193831 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:163832}
3833
3834// sort
3835
3836// stable, 2-3 compares, 0-2 swaps
3837
3838template <class _Compare, class _ForwardIterator>
Arthur O'Dwyer5d956562021-02-04 23:12:523839_LIBCPP_CONSTEXPR_AFTER_CXX11 unsigned
Howard Hinnant3e519522010-05-11 19:42:163840__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3841{
3842 unsigned __r = 0;
3843 if (!__c(*__y, *__x)) // if x <= y
3844 {
3845 if (!__c(*__z, *__y)) // if y <= z
3846 return __r; // x <= y && y <= z
3847 // x <= y && y > z
3848 swap(*__y, *__z); // x <= z && y < z
3849 __r = 1;
3850 if (__c(*__y, *__x)) // if x > y
3851 {
3852 swap(*__x, *__y); // x < y && y <= z
3853 __r = 2;
3854 }
3855 return __r; // x <= y && y < z
3856 }
3857 if (__c(*__z, *__y)) // x > y, if y > z
3858 {
3859 swap(*__x, *__z); // x < y && y < z
3860 __r = 1;
3861 return __r;
3862 }
3863 swap(*__x, *__y); // x > y && y <= z
3864 __r = 1; // x < y && x <= z
3865 if (__c(*__z, *__y)) // if y > z
3866 {
3867 swap(*__y, *__z); // x <= y && y < z
3868 __r = 2;
3869 }
3870 return __r;
3871} // x <= y && y <= z
3872
3873// stable, 3-6 compares, 0-5 swaps
3874
3875template <class _Compare, class _ForwardIterator>
3876unsigned
3877__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3878 _ForwardIterator __x4, _Compare __c)
3879{
Arthur O'Dwyer19688042020-11-22 18:21:113880 unsigned __r = _VSTD::__sort3<_Compare>(__x1, __x2, __x3, __c);
Howard Hinnant3e519522010-05-11 19:42:163881 if (__c(*__x4, *__x3))
3882 {
3883 swap(*__x3, *__x4);
3884 ++__r;
3885 if (__c(*__x3, *__x2))
3886 {
3887 swap(*__x2, *__x3);
3888 ++__r;
3889 if (__c(*__x2, *__x1))
3890 {
3891 swap(*__x1, *__x2);
3892 ++__r;
3893 }
3894 }
3895 }
3896 return __r;
3897}
3898
3899// stable, 4-10 compares, 0-9 swaps
3900
3901template <class _Compare, class _ForwardIterator>
Louis Dionne835140a2018-11-21 16:24:463902_LIBCPP_HIDDEN
Howard Hinnant3e519522010-05-11 19:42:163903unsigned
3904__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3905 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3906{
Arthur O'Dwyer19688042020-11-22 18:21:113907 unsigned __r = _VSTD::__sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
Howard Hinnant3e519522010-05-11 19:42:163908 if (__c(*__x5, *__x4))
3909 {
3910 swap(*__x4, *__x5);
3911 ++__r;
3912 if (__c(*__x4, *__x3))
3913 {
3914 swap(*__x3, *__x4);
3915 ++__r;
3916 if (__c(*__x3, *__x2))
3917 {
3918 swap(*__x2, *__x3);
3919 ++__r;
3920 if (__c(*__x2, *__x1))
3921 {
3922 swap(*__x1, *__x2);
3923 ++__r;
3924 }
3925 }
3926 }
3927 }
3928 return __r;
3929}
3930
3931// Assumes size > 0
Arthur O'Dwyer1d7c39e2020-12-12 16:37:363932template <class _Compare, class _BidirectionalIterator>
Arthur O'Dwyer5d956562021-02-04 23:12:523933_LIBCPP_CONSTEXPR_AFTER_CXX11 void
Arthur O'Dwyer1d7c39e2020-12-12 16:37:363934__selection_sort(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:163935{
Arthur O'Dwyer1d7c39e2020-12-12 16:37:363936 _BidirectionalIterator __lm1 = __last;
Howard Hinnant3e519522010-05-11 19:42:163937 for (--__lm1; __first != __lm1; ++__first)
3938 {
Arthur O'Dwyer1d7c39e2020-12-12 16:37:363939 _BidirectionalIterator __i = _VSTD::min_element<_BidirectionalIterator,
Howard Hinnant3e519522010-05-11 19:42:163940 typename add_lvalue_reference<_Compare>::type>
3941 (__first, __last, __comp);
3942 if (__i != __first)
3943 swap(*__first, *__i);
3944 }
3945}
3946
Arthur O'Dwyer1d7c39e2020-12-12 16:37:363947template <class _Compare, class _BidirectionalIterator>
Howard Hinnant3e519522010-05-11 19:42:163948void
Arthur O'Dwyer1d7c39e2020-12-12 16:37:363949__insertion_sort(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:163950{
Arthur O'Dwyer1d7c39e2020-12-12 16:37:363951 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
Howard Hinnant3e519522010-05-11 19:42:163952 if (__first != __last)
3953 {
Arthur O'Dwyer1d7c39e2020-12-12 16:37:363954 _BidirectionalIterator __i = __first;
Howard Hinnant3e519522010-05-11 19:42:163955 for (++__i; __i != __last; ++__i)
3956 {
Arthur O'Dwyer1d7c39e2020-12-12 16:37:363957 _BidirectionalIterator __j = __i;
Howard Hinnantce48a112011-06-30 21:18:193958 value_type __t(_VSTD::move(*__j));
Arthur O'Dwyer1d7c39e2020-12-12 16:37:363959 for (_BidirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
Howard Hinnantce48a112011-06-30 21:18:193960 *__j = _VSTD::move(*__k);
3961 *__j = _VSTD::move(__t);
Howard Hinnant3e519522010-05-11 19:42:163962 }
3963 }
3964}
3965
3966template <class _Compare, class _RandomAccessIterator>
3967void
3968__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3969{
3970 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3971 _RandomAccessIterator __j = __first+2;
Arthur O'Dwyer19688042020-11-22 18:21:113972 _VSTD::__sort3<_Compare>(__first, __first+1, __j, __comp);
Howard Hinnant3e519522010-05-11 19:42:163973 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3974 {
3975 if (__comp(*__i, *__j))
3976 {
Howard Hinnantce48a112011-06-30 21:18:193977 value_type __t(_VSTD::move(*__i));
Howard Hinnant3e519522010-05-11 19:42:163978 _RandomAccessIterator __k = __j;
3979 __j = __i;
3980 do
3981 {
Howard Hinnantce48a112011-06-30 21:18:193982 *__j = _VSTD::move(*__k);
Howard Hinnant3e519522010-05-11 19:42:163983 __j = __k;
3984 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnantce48a112011-06-30 21:18:193985 *__j = _VSTD::move(__t);
Howard Hinnant3e519522010-05-11 19:42:163986 }
3987 __j = __i;
3988 }
3989}
3990
3991template <class _Compare, class _RandomAccessIterator>
3992bool
3993__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3994{
3995 switch (__last - __first)
3996 {
3997 case 0:
3998 case 1:
3999 return true;
4000 case 2:
4001 if (__comp(*--__last, *__first))
4002 swap(*__first, *__last);
4003 return true;
4004 case 3:
Howard Hinnantce48a112011-06-30 21:18:194005 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:164006 return true;
4007 case 4:
Howard Hinnantce48a112011-06-30 21:18:194008 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:164009 return true;
4010 case 5:
Howard Hinnantce48a112011-06-30 21:18:194011 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:164012 return true;
4013 }
4014 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4015 _RandomAccessIterator __j = __first+2;
Arthur O'Dwyer19688042020-11-22 18:21:114016 _VSTD::__sort3<_Compare>(__first, __first+1, __j, __comp);
Howard Hinnant3e519522010-05-11 19:42:164017 const unsigned __limit = 8;
4018 unsigned __count = 0;
4019 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
4020 {
4021 if (__comp(*__i, *__j))
4022 {
Howard Hinnantce48a112011-06-30 21:18:194023 value_type __t(_VSTD::move(*__i));
Howard Hinnant3e519522010-05-11 19:42:164024 _RandomAccessIterator __k = __j;
4025 __j = __i;
4026 do
4027 {
Howard Hinnantce48a112011-06-30 21:18:194028 *__j = _VSTD::move(*__k);
Howard Hinnant3e519522010-05-11 19:42:164029 __j = __k;
4030 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnantce48a112011-06-30 21:18:194031 *__j = _VSTD::move(__t);
Howard Hinnant3e519522010-05-11 19:42:164032 if (++__count == __limit)
4033 return ++__i == __last;
4034 }
4035 __j = __i;
4036 }
4037 return true;
4038}
4039
Arthur O'Dwyer1d7c39e2020-12-12 16:37:364040template <class _Compare, class _BidirectionalIterator>
Howard Hinnant3e519522010-05-11 19:42:164041void
Arthur O'Dwyer1d7c39e2020-12-12 16:37:364042__insertion_sort_move(_BidirectionalIterator __first1, _BidirectionalIterator __last1,
4043 typename iterator_traits<_BidirectionalIterator>::value_type* __first2, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164044{
Arthur O'Dwyer1d7c39e2020-12-12 16:37:364045 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
Howard Hinnant3e519522010-05-11 19:42:164046 if (__first1 != __last1)
4047 {
4048 __destruct_n __d(0);
4049 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
4050 value_type* __last2 = __first2;
Arthur O'Dwyerbe4c6572020-12-12 01:30:284051 ::new ((void*)__last2) value_type(_VSTD::move(*__first1));
Bruce Mitchener527a7fd2020-11-24 17:53:534052 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:164053 for (++__last2; ++__first1 != __last1; ++__last2)
4054 {
4055 value_type* __j2 = __last2;
4056 value_type* __i2 = __j2;
4057 if (__comp(*__first1, *--__i2))
4058 {
Arthur O'Dwyerbe4c6572020-12-12 01:30:284059 ::new ((void*)__j2) value_type(_VSTD::move(*__i2));
Bruce Mitchener527a7fd2020-11-24 17:53:534060 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:164061 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
Howard Hinnantce48a112011-06-30 21:18:194062 *__j2 = _VSTD::move(*__i2);
4063 *__j2 = _VSTD::move(*__first1);
Howard Hinnant3e519522010-05-11 19:42:164064 }
4065 else
4066 {
Arthur O'Dwyerbe4c6572020-12-12 01:30:284067 ::new ((void*)__j2) value_type(_VSTD::move(*__first1));
Bruce Mitchener527a7fd2020-11-24 17:53:534068 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:164069 }
4070 }
4071 __h.release();
4072 }
4073}
4074
4075template <class _Compare, class _RandomAccessIterator>
4076void
4077__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4078{
Howard Hinnant3e519522010-05-11 19:42:164079 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4080 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnantca740482010-11-19 22:17:284081 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
4082 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
Howard Hinnant3e519522010-05-11 19:42:164083 while (true)
4084 {
4085 __restart:
4086 difference_type __len = __last - __first;
4087 switch (__len)
4088 {
4089 case 0:
4090 case 1:
4091 return;
4092 case 2:
4093 if (__comp(*--__last, *__first))
4094 swap(*__first, *__last);
4095 return;
4096 case 3:
Howard Hinnantce48a112011-06-30 21:18:194097 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:164098 return;
4099 case 4:
Howard Hinnantce48a112011-06-30 21:18:194100 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:164101 return;
4102 case 5:
Howard Hinnantce48a112011-06-30 21:18:194103 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:164104 return;
4105 }
4106 if (__len <= __limit)
4107 {
Howard Hinnantce48a112011-06-30 21:18:194108 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:164109 return;
4110 }
4111 // __len > 5
4112 _RandomAccessIterator __m = __first;
4113 _RandomAccessIterator __lm1 = __last;
4114 --__lm1;
4115 unsigned __n_swaps;
4116 {
4117 difference_type __delta;
4118 if (__len >= 1000)
4119 {
4120 __delta = __len/2;
4121 __m += __delta;
4122 __delta /= 2;
Howard Hinnantce48a112011-06-30 21:18:194123 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
Howard Hinnant3e519522010-05-11 19:42:164124 }
4125 else
4126 {
4127 __delta = __len/2;
4128 __m += __delta;
Howard Hinnantce48a112011-06-30 21:18:194129 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
Howard Hinnant3e519522010-05-11 19:42:164130 }
4131 }
4132 // *__m is median
4133 // partition [__first, __m) < *__m and *__m <= [__m, __last)
4134 // (this inhibits tossing elements equivalent to __m around unnecessarily)
4135 _RandomAccessIterator __i = __first;
4136 _RandomAccessIterator __j = __lm1;
4137 // j points beyond range to be tested, *__m is known to be <= *__lm1
4138 // The search going up is known to be guarded but the search coming down isn't.
4139 // Prime the downward search with a guard.
4140 if (!__comp(*__i, *__m)) // if *__first == *__m
4141 {
4142 // *__first == *__m, *__first doesn't go in first part
4143 // manually guard downward moving __j against __i
4144 while (true)
4145 {
4146 if (__i == --__j)
4147 {
4148 // *__first == *__m, *__m <= all other elements
4149 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
4150 ++__i; // __first + 1
4151 __j = __last;
4152 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
4153 {
4154 while (true)
4155 {
4156 if (__i == __j)
4157 return; // [__first, __last) all equivalent elements
4158 if (__comp(*__first, *__i))
4159 {
4160 swap(*__i, *__j);
4161 ++__n_swaps;
4162 ++__i;
4163 break;
4164 }
4165 ++__i;
4166 }
4167 }
4168 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
4169 if (__i == __j)
4170 return;
4171 while (true)
4172 {
4173 while (!__comp(*__first, *__i))
4174 ++__i;
4175 while (__comp(*__first, *--__j))
4176 ;
4177 if (__i >= __j)
4178 break;
4179 swap(*__i, *__j);
4180 ++__n_swaps;
4181 ++__i;
4182 }
4183 // [__first, __i) == *__first and *__first < [__i, __last)
Arthur O'Dwyerb6f19172020-12-12 16:57:324184 // The first part is sorted, sort the second part
Howard Hinnantce48a112011-06-30 21:18:194185 // _VSTD::__sort<_Compare>(__i, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:164186 __first = __i;
4187 goto __restart;
4188 }
4189 if (__comp(*__j, *__m))
4190 {
4191 swap(*__i, *__j);
4192 ++__n_swaps;
4193 break; // found guard for downward moving __j, now use unguarded partition
4194 }
4195 }
4196 }
4197 // It is known that *__i < *__m
4198 ++__i;
4199 // j points beyond range to be tested, *__m is known to be <= *__lm1
4200 // if not yet partitioned...
4201 if (__i < __j)
4202 {
4203 // known that *(__i - 1) < *__m
4204 // known that __i <= __m
4205 while (true)
4206 {
4207 // __m still guards upward moving __i
4208 while (__comp(*__i, *__m))
4209 ++__i;
4210 // It is now known that a guard exists for downward moving __j
4211 while (!__comp(*--__j, *__m))
4212 ;
4213 if (__i > __j)
4214 break;
4215 swap(*__i, *__j);
4216 ++__n_swaps;
4217 // It is known that __m != __j
4218 // If __m just moved, follow it
4219 if (__m == __i)
4220 __m = __j;
4221 ++__i;
4222 }
4223 }
4224 // [__first, __i) < *__m and *__m <= [__i, __last)
4225 if (__i != __m && __comp(*__m, *__i))
4226 {
4227 swap(*__i, *__m);
4228 ++__n_swaps;
4229 }
4230 // [__first, __i) < *__i and *__i <= [__i+1, __last)
4231 // If we were given a perfect partition, see if insertion sort is quick...
4232 if (__n_swaps == 0)
4233 {
Howard Hinnantce48a112011-06-30 21:18:194234 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
4235 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
Howard Hinnant3e519522010-05-11 19:42:164236 {
4237 if (__fs)
4238 return;
4239 __last = __i;
4240 continue;
4241 }
4242 else
4243 {
4244 if (__fs)
4245 {
4246 __first = ++__i;
4247 continue;
4248 }
4249 }
4250 }
4251 // sort smaller range with recursive call and larger with tail recursion elimination
4252 if (__i - __first < __last - __i)
4253 {
Howard Hinnantce48a112011-06-30 21:18:194254 _VSTD::__sort<_Compare>(__first, __i, __comp);
4255 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:164256 __first = ++__i;
4257 }
4258 else
4259 {
Howard Hinnantce48a112011-06-30 21:18:194260 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4261 // _VSTD::__sort<_Compare>(__first, __i, __comp);
Howard Hinnant3e519522010-05-11 19:42:164262 __last = __i;
4263 }
4264 }
4265}
4266
Arthur O'Dwyer493f1402020-12-20 20:21:424267template <class _Compare, class _Tp>
Howard Hinnant3e519522010-05-11 19:42:164268inline _LIBCPP_INLINE_VISIBILITY
4269void
Arthur O'Dwyer493f1402020-12-20 20:21:424270__sort(_Tp** __first, _Tp** __last, __less<_Tp*>&)
Howard Hinnant3e519522010-05-11 19:42:164271{
Arthur O'Dwyer493f1402020-12-20 20:21:424272 __less<uintptr_t> __comp;
4273 _VSTD::__sort<__less<uintptr_t>&, uintptr_t*>((uintptr_t*)__first, (uintptr_t*)__last, __comp);
Howard Hinnantf554add2011-09-14 18:33:514274}
4275
Howard Hinnantf0544c22013-08-12 18:38:344276_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4277_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4278_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4279_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4280_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4281_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4282_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4283_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4284_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4285_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4286_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4287_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>&))
4288_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4289_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4290_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
Howard Hinnant3e519522010-05-11 19:42:164291
Howard Hinnantf0544c22013-08-12 18:38:344292_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4293_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4294_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4295_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4296_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4297_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4298_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4299_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4300_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4301_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4302_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4303_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>&))
4304_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4305_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4306_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&))
Howard Hinnant3e519522010-05-11 19:42:164307
Howard Hinnantf0544c22013-08-12 18:38:344308_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 Hinnant3e519522010-05-11 19:42:164309
4310// lower_bound
4311
4312template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414313_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454314__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164315{
4316 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantce48a112011-06-30 21:18:194317 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:164318 while (__len != 0)
4319 {
Louis Dionne04695a72018-12-17 16:04:394320 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnant3e519522010-05-11 19:42:164321 _ForwardIterator __m = __first;
Howard Hinnantce48a112011-06-30 21:18:194322 _VSTD::advance(__m, __l2);
Howard Hinnante4383372011-10-22 20:59:454323 if (__comp(*__m, __value_))
Howard Hinnant3e519522010-05-11 19:42:164324 {
4325 __first = ++__m;
4326 __len -= __l2 + 1;
4327 }
4328 else
4329 __len = __l2;
4330 }
4331 return __first;
4332}
4333
4334template <class _ForwardIterator, class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:084335_LIBCPP_NODISCARD_EXT inline
4336_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164337_ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454338lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164339{
Howard Hinnant3e519522010-05-11 19:42:164340 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:114341 return _VSTD::__lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant3e519522010-05-11 19:42:164342}
4343
4344template <class _ForwardIterator, class _Tp>
Nico Weber1362d7e2019-04-03 18:13:084345_LIBCPP_NODISCARD_EXT inline
4346_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164347_ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454348lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:164349{
Howard Hinnante4383372011-10-22 20:59:454350 return _VSTD::lower_bound(__first, __last, __value_,
Howard Hinnant3e519522010-05-11 19:42:164351 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4352}
4353
4354// upper_bound
4355
4356template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414357_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454358__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164359{
4360 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantce48a112011-06-30 21:18:194361 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:164362 while (__len != 0)
4363 {
Louis Dionne04695a72018-12-17 16:04:394364 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnant3e519522010-05-11 19:42:164365 _ForwardIterator __m = __first;
Howard Hinnantce48a112011-06-30 21:18:194366 _VSTD::advance(__m, __l2);
Howard Hinnante4383372011-10-22 20:59:454367 if (__comp(__value_, *__m))
Howard Hinnant3e519522010-05-11 19:42:164368 __len = __l2;
4369 else
4370 {
4371 __first = ++__m;
4372 __len -= __l2 + 1;
4373 }
4374 }
4375 return __first;
4376}
4377
4378template <class _ForwardIterator, class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:084379_LIBCPP_NODISCARD_EXT inline
4380_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164381_ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454382upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164383{
Howard Hinnant3e519522010-05-11 19:42:164384 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:114385 return _VSTD::__upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant3e519522010-05-11 19:42:164386}
4387
4388template <class _ForwardIterator, class _Tp>
Nico Weber1362d7e2019-04-03 18:13:084389_LIBCPP_NODISCARD_EXT inline
4390_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164391_ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454392upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:164393{
Howard Hinnante4383372011-10-22 20:59:454394 return _VSTD::upper_bound(__first, __last, __value_,
Howard Hinnant3e519522010-05-11 19:42:164395 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4396}
4397
4398// equal_range
4399
4400template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414401_LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_ForwardIterator, _ForwardIterator>
Howard Hinnante4383372011-10-22 20:59:454402__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164403{
4404 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantce48a112011-06-30 21:18:194405 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:164406 while (__len != 0)
4407 {
Louis Dionne04695a72018-12-17 16:04:394408 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnant3e519522010-05-11 19:42:164409 _ForwardIterator __m = __first;
Howard Hinnantce48a112011-06-30 21:18:194410 _VSTD::advance(__m, __l2);
Howard Hinnante4383372011-10-22 20:59:454411 if (__comp(*__m, __value_))
Howard Hinnant3e519522010-05-11 19:42:164412 {
4413 __first = ++__m;
4414 __len -= __l2 + 1;
4415 }
Howard Hinnante4383372011-10-22 20:59:454416 else if (__comp(__value_, *__m))
Howard Hinnant3e519522010-05-11 19:42:164417 {
4418 __last = __m;
4419 __len = __l2;
4420 }
4421 else
4422 {
4423 _ForwardIterator __mp1 = __m;
4424 return pair<_ForwardIterator, _ForwardIterator>
4425 (
Arthur O'Dwyer19688042020-11-22 18:21:114426 _VSTD::__lower_bound<_Compare>(__first, __m, __value_, __comp),
4427 _VSTD::__upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
Howard Hinnant3e519522010-05-11 19:42:164428 );
4429 }
4430 }
4431 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4432}
4433
4434template <class _ForwardIterator, class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:084435_LIBCPP_NODISCARD_EXT inline
4436_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164437pair<_ForwardIterator, _ForwardIterator>
Howard Hinnante4383372011-10-22 20:59:454438equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164439{
Eric Fiselieraa1cad12019-04-12 05:18:194440 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:114441 return _VSTD::__equal_range<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant3e519522010-05-11 19:42:164442}
4443
4444template <class _ForwardIterator, class _Tp>
Nico Weber1362d7e2019-04-03 18:13:084445_LIBCPP_NODISCARD_EXT inline
4446_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164447pair<_ForwardIterator, _ForwardIterator>
Howard Hinnante4383372011-10-22 20:59:454448equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:164449{
Howard Hinnante4383372011-10-22 20:59:454450 return _VSTD::equal_range(__first, __last, __value_,
Howard Hinnant3e519522010-05-11 19:42:164451 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4452}
4453
4454// binary_search
4455
4456template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414457inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164458bool
Howard Hinnante4383372011-10-22 20:59:454459__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164460{
Arthur O'Dwyer19688042020-11-22 18:21:114461 __first = _VSTD::__lower_bound<_Compare>(__first, __last, __value_, __comp);
Howard Hinnante4383372011-10-22 20:59:454462 return __first != __last && !__comp(__value_, *__first);
Howard Hinnant3e519522010-05-11 19:42:164463}
4464
4465template <class _ForwardIterator, class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:084466_LIBCPP_NODISCARD_EXT inline
4467_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164468bool
Howard Hinnante4383372011-10-22 20:59:454469binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164470{
Eric Fiselieraa1cad12019-04-12 05:18:194471 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:114472 return _VSTD::__binary_search<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant3e519522010-05-11 19:42:164473}
4474
4475template <class _ForwardIterator, class _Tp>
Nico Weber1362d7e2019-04-03 18:13:084476_LIBCPP_NODISCARD_EXT inline
4477_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164478bool
Howard Hinnante4383372011-10-22 20:59:454479binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:164480{
Howard Hinnante4383372011-10-22 20:59:454481 return _VSTD::binary_search(__first, __last, __value_,
Howard Hinnant3e519522010-05-11 19:42:164482 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4483}
4484
4485// merge
4486
4487template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
Nicholas-Baronb552a302020-09-14 20:37:414488_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164489_OutputIterator
4490__merge(_InputIterator1 __first1, _InputIterator1 __last1,
4491 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4492{
4493 for (; __first1 != __last1; ++__result)
4494 {
4495 if (__first2 == __last2)
Howard Hinnantce48a112011-06-30 21:18:194496 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnant3e519522010-05-11 19:42:164497 if (__comp(*__first2, *__first1))
4498 {
4499 *__result = *__first2;
4500 ++__first2;
4501 }
4502 else
4503 {
4504 *__result = *__first1;
4505 ++__first1;
4506 }
4507 }
Howard Hinnantce48a112011-06-30 21:18:194508 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnant3e519522010-05-11 19:42:164509}
4510
4511template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
Nicholas-Baronb552a302020-09-14 20:37:414512inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164513_OutputIterator
4514merge(_InputIterator1 __first1, _InputIterator1 __last1,
4515 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4516{
Eric Fiselieraa1cad12019-04-12 05:18:194517 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantce48a112011-06-30 21:18:194518 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant3e519522010-05-11 19:42:164519}
4520
4521template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
Nicholas-Baronb552a302020-09-14 20:37:414522inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164523_OutputIterator
4524merge(_InputIterator1 __first1, _InputIterator1 __last1,
4525 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4526{
4527 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4528 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Marshall Clow4f417792019-08-20 22:23:354529 return _VSTD::merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
Howard Hinnant3e519522010-05-11 19:42:164530}
4531
4532// inplace_merge
4533
Marshall Clowadfdae12015-07-29 16:25:454534template <class _Compare, class _InputIterator1, class _InputIterator2,
4535 class _OutputIterator>
4536void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4537 _InputIterator2 __first2, _InputIterator2 __last2,
4538 _OutputIterator __result, _Compare __comp)
4539{
4540 for (; __first1 != __last1; ++__result)
4541 {
4542 if (__first2 == __last2)
4543 {
4544 _VSTD::move(__first1, __last1, __result);
4545 return;
4546 }
4547
4548 if (__comp(*__first2, *__first1))
4549 {
4550 *__result = _VSTD::move(*__first2);
4551 ++__first2;
4552 }
4553 else
4554 {
4555 *__result = _VSTD::move(*__first1);
4556 ++__first1;
4557 }
4558 }
4559 // __first2 through __last2 are already in the right spot.
4560}
4561
Howard Hinnant3e519522010-05-11 19:42:164562template <class _Compare, class _BidirectionalIterator>
4563void
4564__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4565 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4566 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4567 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4568{
4569 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
Howard Hinnant3e519522010-05-11 19:42:164570 __destruct_n __d(0);
4571 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4572 if (__len1 <= __len2)
4573 {
4574 value_type* __p = __buff;
Bruce Mitchener527a7fd2020-11-24 17:53:534575 for (_BidirectionalIterator __i = __first; __i != __middle; __d.template __incr<value_type>(), (void) ++__i, (void) ++__p)
Arthur O'Dwyerbe4c6572020-12-12 01:30:284576 ::new ((void*)__p) value_type(_VSTD::move(*__i));
Arthur O'Dwyereef4bdb2020-12-18 20:11:514577 _VSTD::__half_inplace_merge<_Compare>(__buff, __p, __middle, __last, __first, __comp);
Howard Hinnant3e519522010-05-11 19:42:164578 }
4579 else
4580 {
4581 value_type* __p = __buff;
Bruce Mitchener527a7fd2020-11-24 17:53:534582 for (_BidirectionalIterator __i = __middle; __i != __last; __d.template __incr<value_type>(), (void) ++__i, (void) ++__p)
Arthur O'Dwyerbe4c6572020-12-12 01:30:284583 ::new ((void*)__p) value_type(_VSTD::move(*__i));
Howard Hinnant3e519522010-05-11 19:42:164584 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4585 typedef reverse_iterator<value_type*> _Rv;
Arthur O'Dwyereef4bdb2020-12-18 20:11:514586 typedef __invert<_Compare> _Inverted;
4587 _VSTD::__half_inplace_merge<_Inverted>(_Rv(__p), _Rv(__buff),
Arthur O'Dwyer19688042020-11-22 18:21:114588 _RBi(__middle), _RBi(__first),
Arthur O'Dwyereef4bdb2020-12-18 20:11:514589 _RBi(__last), _Inverted(__comp));
Howard Hinnant3e519522010-05-11 19:42:164590 }
4591}
4592
4593template <class _Compare, class _BidirectionalIterator>
4594void
4595__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4596 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4597 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4598 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4599{
Howard Hinnant3e519522010-05-11 19:42:164600 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4601 while (true)
4602 {
4603 // if __middle == __last, we're done
4604 if (__len2 == 0)
4605 return;
Marshall Clow526e0922015-02-02 16:44:114606 if (__len1 <= __buff_size || __len2 <= __buff_size)
Arthur O'Dwyer19688042020-11-22 18:21:114607 return _VSTD::__buffered_inplace_merge<_Compare>
Marshall Clow526e0922015-02-02 16:44:114608 (__first, __middle, __last, __comp, __len1, __len2, __buff);
Howard Hinnant3e519522010-05-11 19:42:164609 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
Eric Fiselier910285b2014-10-27 19:28:204610 for (; true; ++__first, (void) --__len1)
Howard Hinnant3e519522010-05-11 19:42:164611 {
4612 if (__len1 == 0)
4613 return;
4614 if (__comp(*__middle, *__first))
4615 break;
4616 }
Howard Hinnant3e519522010-05-11 19:42:164617 // __first < __middle < __last
4618 // *__first > *__middle
4619 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4620 // all elements in:
4621 // [__first, __m1) <= [__middle, __m2)
4622 // [__middle, __m2) < [__m1, __middle)
4623 // [__m1, __middle) <= [__m2, __last)
4624 // and __m1 or __m2 is in the middle of its range
4625 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4626 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4627 difference_type __len11; // distance(__first, __m1)
4628 difference_type __len21; // distance(__middle, __m2)
4629 // binary search smaller range
4630 if (__len1 < __len2)
4631 { // __len >= 1, __len2 >= 2
4632 __len21 = __len2 / 2;
4633 __m2 = __middle;
Howard Hinnantce48a112011-06-30 21:18:194634 _VSTD::advance(__m2, __len21);
Arthur O'Dwyer19688042020-11-22 18:21:114635 __m1 = _VSTD::__upper_bound<_Compare>(__first, __middle, *__m2, __comp);
Howard Hinnantce48a112011-06-30 21:18:194636 __len11 = _VSTD::distance(__first, __m1);
Howard Hinnant3e519522010-05-11 19:42:164637 }
4638 else
4639 {
4640 if (__len1 == 1)
4641 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4642 // It is known *__first > *__middle
4643 swap(*__first, *__middle);
4644 return;
4645 }
4646 // __len1 >= 2, __len2 >= 1
4647 __len11 = __len1 / 2;
4648 __m1 = __first;
Howard Hinnantce48a112011-06-30 21:18:194649 _VSTD::advance(__m1, __len11);
Arthur O'Dwyer19688042020-11-22 18:21:114650 __m2 = _VSTD::__lower_bound<_Compare>(__middle, __last, *__m1, __comp);
Howard Hinnantce48a112011-06-30 21:18:194651 __len21 = _VSTD::distance(__middle, __m2);
Howard Hinnant3e519522010-05-11 19:42:164652 }
4653 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4654 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4655 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4656 // swap middle two partitions
Howard Hinnantce48a112011-06-30 21:18:194657 __middle = _VSTD::rotate(__m1, __middle, __m2);
Howard Hinnant3e519522010-05-11 19:42:164658 // __len12 and __len21 now have swapped meanings
Arthur O'Dwyerb6f19172020-12-12 16:57:324659 // merge smaller range with recursive call and larger with tail recursion elimination
Howard Hinnant3e519522010-05-11 19:42:164660 if (__len11 + __len21 < __len12 + __len22)
4661 {
Arthur O'Dwyer19688042020-11-22 18:21:114662 _VSTD::__inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4663// _VSTD::__inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
Howard Hinnant3e519522010-05-11 19:42:164664 __first = __middle;
4665 __middle = __m2;
4666 __len1 = __len12;
4667 __len2 = __len22;
4668 }
4669 else
4670 {
Arthur O'Dwyer19688042020-11-22 18:21:114671 _VSTD::__inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4672// _VSTD::__inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
Howard Hinnant3e519522010-05-11 19:42:164673 __last = __middle;
4674 __middle = __m1;
4675 __len1 = __len11;
4676 __len2 = __len21;
4677 }
4678 }
4679}
4680
Howard Hinnant3e519522010-05-11 19:42:164681template <class _BidirectionalIterator, class _Compare>
4682inline _LIBCPP_INLINE_VISIBILITY
4683void
4684inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4685 _Compare __comp)
4686{
4687 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4688 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnantce48a112011-06-30 21:18:194689 difference_type __len1 = _VSTD::distance(__first, __middle);
4690 difference_type __len2 = _VSTD::distance(__middle, __last);
4691 difference_type __buf_size = _VSTD::min(__len1, __len2);
Marshall Clow0b48cf92015-02-02 17:35:534692 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4693 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
Eric Fiselieraa1cad12019-04-12 05:18:194694 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantce48a112011-06-30 21:18:194695 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
Howard Hinnant3e519522010-05-11 19:42:164696 __buf.first, __buf.second);
Howard Hinnant3e519522010-05-11 19:42:164697}
4698
4699template <class _BidirectionalIterator>
4700inline _LIBCPP_INLINE_VISIBILITY
4701void
4702inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4703{
Howard Hinnantce48a112011-06-30 21:18:194704 _VSTD::inplace_merge(__first, __middle, __last,
Howard Hinnant3e519522010-05-11 19:42:164705 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4706}
4707
4708// stable_sort
4709
4710template <class _Compare, class _InputIterator1, class _InputIterator2>
4711void
4712__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4713 _InputIterator2 __first2, _InputIterator2 __last2,
4714 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4715{
4716 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4717 __destruct_n __d(0);
4718 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4719 for (; true; ++__result)
4720 {
4721 if (__first1 == __last1)
4722 {
Bruce Mitchener527a7fd2020-11-24 17:53:534723 for (; __first2 != __last2; ++__first2, ++__result, (void)__d.template __incr<value_type>())
Arthur O'Dwyerbe4c6572020-12-12 01:30:284724 ::new ((void*)__result) value_type(_VSTD::move(*__first2));
Howard Hinnant3e519522010-05-11 19:42:164725 __h.release();
4726 return;
4727 }
4728 if (__first2 == __last2)
4729 {
Bruce Mitchener527a7fd2020-11-24 17:53:534730 for (; __first1 != __last1; ++__first1, ++__result, (void)__d.template __incr<value_type>())
Arthur O'Dwyerbe4c6572020-12-12 01:30:284731 ::new ((void*)__result) value_type(_VSTD::move(*__first1));
Howard Hinnant3e519522010-05-11 19:42:164732 __h.release();
4733 return;
4734 }
4735 if (__comp(*__first2, *__first1))
4736 {
Arthur O'Dwyerbe4c6572020-12-12 01:30:284737 ::new ((void*)__result) value_type(_VSTD::move(*__first2));
Bruce Mitchener527a7fd2020-11-24 17:53:534738 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:164739 ++__first2;
4740 }
4741 else
4742 {
Arthur O'Dwyerbe4c6572020-12-12 01:30:284743 ::new ((void*)__result) value_type(_VSTD::move(*__first1));
Bruce Mitchener527a7fd2020-11-24 17:53:534744 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:164745 ++__first1;
4746 }
4747 }
4748}
4749
4750template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4751void
4752__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4753 _InputIterator2 __first2, _InputIterator2 __last2,
4754 _OutputIterator __result, _Compare __comp)
4755{
4756 for (; __first1 != __last1; ++__result)
4757 {
4758 if (__first2 == __last2)
4759 {
Marshall Clow7fa68652019-08-20 21:31:514760 for (; __first1 != __last1; ++__first1, (void) ++__result)
Howard Hinnantce48a112011-06-30 21:18:194761 *__result = _VSTD::move(*__first1);
Howard Hinnant3e519522010-05-11 19:42:164762 return;
4763 }
4764 if (__comp(*__first2, *__first1))
4765 {
Howard Hinnantce48a112011-06-30 21:18:194766 *__result = _VSTD::move(*__first2);
Howard Hinnant3e519522010-05-11 19:42:164767 ++__first2;
4768 }
4769 else
4770 {
Howard Hinnantce48a112011-06-30 21:18:194771 *__result = _VSTD::move(*__first1);
Howard Hinnant3e519522010-05-11 19:42:164772 ++__first1;
4773 }
4774 }
Marshall Clow7fa68652019-08-20 21:31:514775 for (; __first2 != __last2; ++__first2, (void) ++__result)
Howard Hinnantce48a112011-06-30 21:18:194776 *__result = _VSTD::move(*__first2);
Howard Hinnant3e519522010-05-11 19:42:164777}
4778
4779template <class _Compare, class _RandomAccessIterator>
4780void
4781__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4782 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4783 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4784
4785template <class _Compare, class _RandomAccessIterator>
4786void
4787__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4788 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4789 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4790{
4791 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4792 switch (__len)
4793 {
4794 case 0:
4795 return;
4796 case 1:
Arthur O'Dwyerbe4c6572020-12-12 01:30:284797 ::new ((void*)__first2) value_type(_VSTD::move(*__first1));
Howard Hinnant3e519522010-05-11 19:42:164798 return;
4799 case 2:
Marshall Clowf951fc32018-02-06 18:58:054800 __destruct_n __d(0);
Howard Hinnant3e519522010-05-11 19:42:164801 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
Marshall Clowf951fc32018-02-06 18:58:054802 if (__comp(*--__last1, *__first1))
Howard Hinnant3e519522010-05-11 19:42:164803 {
Arthur O'Dwyerbe4c6572020-12-12 01:30:284804 ::new ((void*)__first2) value_type(_VSTD::move(*__last1));
Bruce Mitchener527a7fd2020-11-24 17:53:534805 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:164806 ++__first2;
Arthur O'Dwyerbe4c6572020-12-12 01:30:284807 ::new ((void*)__first2) value_type(_VSTD::move(*__first1));
Howard Hinnant3e519522010-05-11 19:42:164808 }
4809 else
4810 {
Arthur O'Dwyerbe4c6572020-12-12 01:30:284811 ::new ((void*)__first2) value_type(_VSTD::move(*__first1));
Bruce Mitchener527a7fd2020-11-24 17:53:534812 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:164813 ++__first2;
Arthur O'Dwyerbe4c6572020-12-12 01:30:284814 ::new ((void*)__first2) value_type(_VSTD::move(*__last1));
Howard Hinnant3e519522010-05-11 19:42:164815 }
4816 __h2.release();
4817 return;
4818 }
4819 if (__len <= 8)
4820 {
Arthur O'Dwyer19688042020-11-22 18:21:114821 _VSTD::__insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
Howard Hinnant3e519522010-05-11 19:42:164822 return;
4823 }
4824 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4825 _RandomAccessIterator __m = __first1 + __l2;
Arthur O'Dwyer19688042020-11-22 18:21:114826 _VSTD::__stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4827 _VSTD::__stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4828 _VSTD::__merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
Howard Hinnant3e519522010-05-11 19:42:164829}
4830
4831template <class _Tp>
4832struct __stable_sort_switch
4833{
Howard Hinnantca740482010-11-19 22:17:284834 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
Howard Hinnant3e519522010-05-11 19:42:164835};
4836
4837template <class _Compare, class _RandomAccessIterator>
4838void
4839__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4840 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4841 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4842{
4843 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4844 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4845 switch (__len)
4846 {
4847 case 0:
4848 case 1:
4849 return;
4850 case 2:
4851 if (__comp(*--__last, *__first))
4852 swap(*__first, *__last);
4853 return;
4854 }
4855 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4856 {
Arthur O'Dwyer19688042020-11-22 18:21:114857 _VSTD::__insertion_sort<_Compare>(__first, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:164858 return;
4859 }
4860 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4861 _RandomAccessIterator __m = __first + __l2;
4862 if (__len <= __buff_size)
4863 {
4864 __destruct_n __d(0);
4865 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
Arthur O'Dwyer19688042020-11-22 18:21:114866 _VSTD::__stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
Bruce Mitchener527a7fd2020-11-24 17:53:534867 __d.__set(__l2, (value_type*)nullptr);
Arthur O'Dwyer19688042020-11-22 18:21:114868 _VSTD::__stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
Bruce Mitchener527a7fd2020-11-24 17:53:534869 __d.__set(__len, (value_type*)nullptr);
Arthur O'Dwyer19688042020-11-22 18:21:114870 _VSTD::__merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4871// _VSTD::__merge<_Compare>(move_iterator<value_type*>(__buff),
4872// move_iterator<value_type*>(__buff + __l2),
4873// move_iterator<_RandomAccessIterator>(__buff + __l2),
4874// move_iterator<_RandomAccessIterator>(__buff + __len),
4875// __first, __comp);
Howard Hinnant3e519522010-05-11 19:42:164876 return;
4877 }
Arthur O'Dwyer19688042020-11-22 18:21:114878 _VSTD::__stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4879 _VSTD::__stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4880 _VSTD::__inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
Howard Hinnant3e519522010-05-11 19:42:164881}
4882
4883template <class _RandomAccessIterator, class _Compare>
4884inline _LIBCPP_INLINE_VISIBILITY
4885void
4886stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4887{
4888 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4889 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4890 difference_type __len = __last - __first;
4891 pair<value_type*, ptrdiff_t> __buf(0, 0);
4892 unique_ptr<value_type, __return_temporary_buffer> __h;
4893 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4894 {
Howard Hinnantce48a112011-06-30 21:18:194895 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnant3e519522010-05-11 19:42:164896 __h.reset(__buf.first);
4897 }
Eric Fiselieraa1cad12019-04-12 05:18:194898 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:114899 _VSTD::__stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
Howard Hinnant3e519522010-05-11 19:42:164900}
4901
4902template <class _RandomAccessIterator>
4903inline _LIBCPP_INLINE_VISIBILITY
4904void
4905stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4906{
Howard Hinnantce48a112011-06-30 21:18:194907 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164908}
4909
4910// is_heap_until
4911
4912template <class _RandomAccessIterator, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:084913_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
Howard Hinnant3e519522010-05-11 19:42:164914is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4915{
Arthur O'Dwyer6491d992021-05-10 17:13:044916 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnant3e519522010-05-11 19:42:164917 difference_type __len = __last - __first;
4918 difference_type __p = 0;
4919 difference_type __c = 1;
4920 _RandomAccessIterator __pp = __first;
4921 while (__c < __len)
4922 {
4923 _RandomAccessIterator __cp = __first + __c;
4924 if (__comp(*__pp, *__cp))
4925 return __cp;
4926 ++__c;
4927 ++__cp;
4928 if (__c == __len)
4929 return __last;
4930 if (__comp(*__pp, *__cp))
4931 return __cp;
4932 ++__p;
4933 ++__pp;
4934 __c = 2 * __p + 1;
4935 }
4936 return __last;
4937}
4938
Howard Hinnantb3371f62010-08-22 00:02:434939template<class _RandomAccessIterator>
Nico Weber1362d7e2019-04-03 18:13:084940_LIBCPP_NODISCARD_EXT inline
4941_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164942_RandomAccessIterator
4943is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4944{
Howard Hinnantce48a112011-06-30 21:18:194945 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164946}
4947
4948// is_heap
4949
4950template <class _RandomAccessIterator, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:084951_LIBCPP_NODISCARD_EXT inline
4952_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164953bool
4954is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4955{
Howard Hinnantce48a112011-06-30 21:18:194956 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
Howard Hinnant3e519522010-05-11 19:42:164957}
4958
Howard Hinnantb3371f62010-08-22 00:02:434959template<class _RandomAccessIterator>
Nico Weber1362d7e2019-04-03 18:13:084960_LIBCPP_NODISCARD_EXT inline
4961_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164962bool
4963is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4964{
Howard Hinnantce48a112011-06-30 21:18:194965 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164966}
4967
4968// push_heap
4969
4970template <class _Compare, class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:184971_LIBCPP_CONSTEXPR_AFTER_CXX11 void
David Majnemer8b512602014-07-22 06:07:094972__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4973 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
Howard Hinnant3e519522010-05-11 19:42:164974{
Howard Hinnant3e519522010-05-11 19:42:164975 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4976 if (__len > 1)
4977 {
4978 __len = (__len - 2) / 2;
4979 _RandomAccessIterator __ptr = __first + __len;
4980 if (__comp(*__ptr, *--__last))
4981 {
Howard Hinnantce48a112011-06-30 21:18:194982 value_type __t(_VSTD::move(*__last));
Howard Hinnant3e519522010-05-11 19:42:164983 do
4984 {
Howard Hinnantce48a112011-06-30 21:18:194985 *__last = _VSTD::move(*__ptr);
Howard Hinnant3e519522010-05-11 19:42:164986 __last = __ptr;
4987 if (__len == 0)
4988 break;
4989 __len = (__len - 1) / 2;
4990 __ptr = __first + __len;
4991 } while (__comp(*__ptr, __t));
Howard Hinnantce48a112011-06-30 21:18:194992 *__last = _VSTD::move(__t);
Howard Hinnant3e519522010-05-11 19:42:164993 }
4994 }
4995}
4996
4997template <class _RandomAccessIterator, class _Compare>
Arthur O'Dwyer5386aa22020-12-17 05:26:184998inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164999void
5000push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5001{
Eric Fiselieraa1cad12019-04-12 05:18:195002 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115003 _VSTD::__sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant3e519522010-05-11 19:42:165004}
5005
5006template <class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185007inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165008void
5009push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5010{
Howard Hinnantce48a112011-06-30 21:18:195011 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:165012}
5013
5014// pop_heap
5015
5016template <class _Compare, class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185017_LIBCPP_CONSTEXPR_AFTER_CXX11 void
Eric Fiselierfd838222016-12-23 23:37:525018__sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
5019 _Compare __comp,
David Majnemer8b512602014-07-22 06:07:095020 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
5021 _RandomAccessIterator __start)
5022{
5023 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5024 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
5025 // left-child of __start is at 2 * __start + 1
5026 // right-child of __start is at 2 * __start + 2
5027 difference_type __child = __start - __first;
5028
5029 if (__len < 2 || (__len - 2) / 2 < __child)
5030 return;
5031
5032 __child = 2 * __child + 1;
5033 _RandomAccessIterator __child_i = __first + __child;
5034
5035 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
5036 // right-child exists and is greater than left-child
5037 ++__child_i;
5038 ++__child;
5039 }
5040
5041 // check if we are in heap-order
5042 if (__comp(*__child_i, *__start))
5043 // we are, __start is larger than it's largest child
5044 return;
5045
5046 value_type __top(_VSTD::move(*__start));
5047 do
5048 {
Arthur O'Dwyer5386aa22020-12-17 05:26:185049 // we are not in heap-order, swap the parent with its largest child
David Majnemer8b512602014-07-22 06:07:095050 *__start = _VSTD::move(*__child_i);
5051 __start = __child_i;
5052
5053 if ((__len - 2) / 2 < __child)
5054 break;
5055
5056 // recompute the child based off of the updated parent
5057 __child = 2 * __child + 1;
5058 __child_i = __first + __child;
5059
5060 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
5061 // right-child exists and is greater than left-child
5062 ++__child_i;
5063 ++__child;
5064 }
5065
5066 // check if we are in heap-order
5067 } while (!__comp(*__child_i, __top));
5068 *__start = _VSTD::move(__top);
5069}
5070
5071template <class _Compare, class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185072inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165073void
5074__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
5075 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
5076{
5077 if (__len > 1)
5078 {
5079 swap(*__first, *--__last);
Arthur O'Dwyer19688042020-11-22 18:21:115080 _VSTD::__sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
Howard Hinnant3e519522010-05-11 19:42:165081 }
5082}
5083
5084template <class _RandomAccessIterator, class _Compare>
Arthur O'Dwyer5386aa22020-12-17 05:26:185085inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165086void
5087pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5088{
Eric Fiselieraa1cad12019-04-12 05:18:195089 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115090 _VSTD::__pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant3e519522010-05-11 19:42:165091}
5092
5093template <class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185094inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165095void
5096pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5097{
Howard Hinnantce48a112011-06-30 21:18:195098 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:165099}
5100
5101// make_heap
5102
5103template <class _Compare, class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185104_LIBCPP_CONSTEXPR_AFTER_CXX11 void
Howard Hinnant3e519522010-05-11 19:42:165105__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5106{
5107 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5108 difference_type __n = __last - __first;
5109 if (__n > 1)
5110 {
David Majnemer8b512602014-07-22 06:07:095111 // start from the first parent, there is no need to consider children
5112 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
5113 {
Arthur O'Dwyer19688042020-11-22 18:21:115114 _VSTD::__sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
David Majnemer8b512602014-07-22 06:07:095115 }
Howard Hinnant3e519522010-05-11 19:42:165116 }
5117}
5118
5119template <class _RandomAccessIterator, class _Compare>
Arthur O'Dwyer5386aa22020-12-17 05:26:185120inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165121void
5122make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5123{
Eric Fiselieraa1cad12019-04-12 05:18:195124 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115125 _VSTD::__make_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165126}
5127
5128template <class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185129inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165130void
5131make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5132{
Howard Hinnantce48a112011-06-30 21:18:195133 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:165134}
5135
5136// sort_heap
5137
5138template <class _Compare, class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185139_LIBCPP_CONSTEXPR_AFTER_CXX17 void
Howard Hinnant3e519522010-05-11 19:42:165140__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5141{
5142 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Marshall Clow7fa68652019-08-20 21:31:515143 for (difference_type __n = __last - __first; __n > 1; --__last, (void) --__n)
Arthur O'Dwyer19688042020-11-22 18:21:115144 _VSTD::__pop_heap<_Compare>(__first, __last, __comp, __n);
Howard Hinnant3e519522010-05-11 19:42:165145}
5146
5147template <class _RandomAccessIterator, class _Compare>
Arthur O'Dwyer5386aa22020-12-17 05:26:185148inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165149void
5150sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5151{
Eric Fiselieraa1cad12019-04-12 05:18:195152 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115153 _VSTD::__sort_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165154}
5155
5156template <class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185157inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165158void
5159sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5160{
Howard Hinnantce48a112011-06-30 21:18:195161 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:165162}
5163
5164// partial_sort
5165
5166template <class _Compare, class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185167_LIBCPP_CONSTEXPR_AFTER_CXX17 void
Howard Hinnant3e519522010-05-11 19:42:165168__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5169 _Compare __comp)
5170{
Arthur O'Dwyer19688042020-11-22 18:21:115171 _VSTD::__make_heap<_Compare>(__first, __middle, __comp);
Howard Hinnant3e519522010-05-11 19:42:165172 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5173 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5174 {
5175 if (__comp(*__i, *__first))
5176 {
5177 swap(*__i, *__first);
Arthur O'Dwyer19688042020-11-22 18:21:115178 _VSTD::__sift_down<_Compare>(__first, __middle, __comp, __len, __first);
Howard Hinnant3e519522010-05-11 19:42:165179 }
5180 }
Arthur O'Dwyer19688042020-11-22 18:21:115181 _VSTD::__sort_heap<_Compare>(__first, __middle, __comp);
Howard Hinnant3e519522010-05-11 19:42:165182}
5183
5184template <class _RandomAccessIterator, class _Compare>
Arthur O'Dwyer5386aa22020-12-17 05:26:185185inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165186void
5187partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5188 _Compare __comp)
5189{
Eric Fiselieraa1cad12019-04-12 05:18:195190 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115191 _VSTD::__partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165192}
5193
5194template <class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185195inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165196void
5197partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5198{
Howard Hinnantce48a112011-06-30 21:18:195199 _VSTD::partial_sort(__first, __middle, __last,
Howard Hinnant3e519522010-05-11 19:42:165200 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5201}
5202
5203// partial_sort_copy
5204
5205template <class _Compare, class _InputIterator, class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185206_LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
Howard Hinnant3e519522010-05-11 19:42:165207__partial_sort_copy(_InputIterator __first, _InputIterator __last,
5208 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5209{
5210 _RandomAccessIterator __r = __result_first;
5211 if (__r != __result_last)
5212 {
Marshall Clow7fa68652019-08-20 21:31:515213 for (; __first != __last && __r != __result_last; ++__first, (void) ++__r)
Howard Hinnant3e519522010-05-11 19:42:165214 *__r = *__first;
Arthur O'Dwyer19688042020-11-22 18:21:115215 _VSTD::__make_heap<_Compare>(__result_first, __r, __comp);
David Majnemer8b512602014-07-22 06:07:095216 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
Howard Hinnant3e519522010-05-11 19:42:165217 for (; __first != __last; ++__first)
5218 if (__comp(*__first, *__result_first))
5219 {
5220 *__result_first = *__first;
Arthur O'Dwyer19688042020-11-22 18:21:115221 _VSTD::__sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
Howard Hinnant3e519522010-05-11 19:42:165222 }
Arthur O'Dwyer19688042020-11-22 18:21:115223 _VSTD::__sort_heap<_Compare>(__result_first, __r, __comp);
Howard Hinnant3e519522010-05-11 19:42:165224 }
5225 return __r;
5226}
5227
5228template <class _InputIterator, class _RandomAccessIterator, class _Compare>
Arthur O'Dwyer5386aa22020-12-17 05:26:185229inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165230_RandomAccessIterator
5231partial_sort_copy(_InputIterator __first, _InputIterator __last,
5232 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5233{
Eric Fiselieraa1cad12019-04-12 05:18:195234 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115235 return _VSTD::__partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165236}
5237
5238template <class _InputIterator, class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185239inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165240_RandomAccessIterator
5241partial_sort_copy(_InputIterator __first, _InputIterator __last,
5242 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5243{
Howard Hinnantce48a112011-06-30 21:18:195244 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
Howard Hinnant3e519522010-05-11 19:42:165245 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5246}
5247
5248// nth_element
5249
Arthur O'Dwyer5d956562021-02-04 23:12:525250template<class _Compare, class _RandomAccessIterator>
5251_LIBCPP_CONSTEXPR_AFTER_CXX11 bool
5252__nth_element_find_guard(_RandomAccessIterator& __i, _RandomAccessIterator& __j,
5253 _RandomAccessIterator __m, _Compare __comp)
5254{
5255 // manually guard downward moving __j against __i
5256 while (true) {
5257 if (__i == --__j) {
5258 return false;
5259 }
5260 if (__comp(*__j, *__m)) {
5261 return true; // found guard for downward moving __j, now use unguarded partition
5262 }
5263 }
5264}
5265
Howard Hinnant3e519522010-05-11 19:42:165266template <class _Compare, class _RandomAccessIterator>
Arthur O'Dwyer5d956562021-02-04 23:12:525267_LIBCPP_CONSTEXPR_AFTER_CXX11 void
Howard Hinnant3e519522010-05-11 19:42:165268__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5269{
5270 // _Compare is known to be a reference type
5271 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5272 const difference_type __limit = 7;
5273 while (true)
5274 {
Howard Hinnantb34b48192011-12-29 17:45:355275 if (__nth == __last)
5276 return;
Howard Hinnant3e519522010-05-11 19:42:165277 difference_type __len = __last - __first;
5278 switch (__len)
5279 {
5280 case 0:
5281 case 1:
5282 return;
5283 case 2:
5284 if (__comp(*--__last, *__first))
5285 swap(*__first, *__last);
5286 return;
5287 case 3:
5288 {
5289 _RandomAccessIterator __m = __first;
Howard Hinnantce48a112011-06-30 21:18:195290 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165291 return;
5292 }
5293 }
5294 if (__len <= __limit)
5295 {
Arthur O'Dwyer19688042020-11-22 18:21:115296 _VSTD::__selection_sort<_Compare>(__first, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165297 return;
5298 }
5299 // __len > __limit >= 3
5300 _RandomAccessIterator __m = __first + __len/2;
5301 _RandomAccessIterator __lm1 = __last;
Howard Hinnantce48a112011-06-30 21:18:195302 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
Howard Hinnant3e519522010-05-11 19:42:165303 // *__m is median
5304 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5305 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5306 _RandomAccessIterator __i = __first;
5307 _RandomAccessIterator __j = __lm1;
5308 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5309 // The search going up is known to be guarded but the search coming down isn't.
5310 // Prime the downward search with a guard.
5311 if (!__comp(*__i, *__m)) // if *__first == *__m
5312 {
5313 // *__first == *__m, *__first doesn't go in first part
Arthur O'Dwyer5d956562021-02-04 23:12:525314 if (_VSTD::__nth_element_find_guard<_Compare>(__i, __j, __m, __comp)) {
5315 swap(*__i, *__j);
5316 ++__n_swaps;
5317 } else {
5318 // *__first == *__m, *__m <= all other elements
5319 // Partition instead into [__first, __i) == *__first and *__first < [__i, __last)
5320 ++__i; // __first + 1
5321 __j = __last;
5322 if (!__comp(*__first, *--__j)) { // we need a guard if *__first == *(__last-1)
5323 while (true) {
5324 if (__i == __j) {
5325 return; // [__first, __last) all equivalent elements
5326 } else if (__comp(*__first, *__i)) {
5327 swap(*__i, *__j);
5328 ++__n_swaps;
Jordan Rupprechtb6ffece2021-02-04 21:56:415329 ++__i;
Jordan Rupprechtb6ffece2021-02-04 21:56:415330 break;
Arthur O'Dwyer5d956562021-02-04 23:12:525331 }
Jordan Rupprechtb6ffece2021-02-04 21:56:415332 ++__i;
5333 }
Jordan Rupprechtb6ffece2021-02-04 21:56:415334 }
Arthur O'Dwyer5d956562021-02-04 23:12:525335 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5336 if (__i == __j) {
5337 return;
5338 }
5339 while (true) {
5340 while (!__comp(*__first, *__i))
5341 ++__i;
5342 while (__comp(*__first, *--__j))
5343 ;
5344 if (__i >= __j)
5345 break;
Jordan Rupprechtb6ffece2021-02-04 21:56:415346 swap(*__i, *__j);
5347 ++__n_swaps;
Arthur O'Dwyer5d956562021-02-04 23:12:525348 ++__i;
Jordan Rupprechtb6ffece2021-02-04 21:56:415349 }
Arthur O'Dwyer5d956562021-02-04 23:12:525350 // [__first, __i) == *__first and *__first < [__i, __last)
5351 // The first part is sorted,
5352 if (__nth < __i) {
5353 return;
5354 }
5355 // __nth_element the second part
5356 // _VSTD::__nth_element<_Compare>(__i, __nth, __last, __comp);
5357 __first = __i;
5358 continue;
Howard Hinnant3e519522010-05-11 19:42:165359 }
5360 }
5361 ++__i;
5362 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5363 // if not yet partitioned...
5364 if (__i < __j)
5365 {
5366 // known that *(__i - 1) < *__m
5367 while (true)
5368 {
5369 // __m still guards upward moving __i
5370 while (__comp(*__i, *__m))
5371 ++__i;
5372 // It is now known that a guard exists for downward moving __j
5373 while (!__comp(*--__j, *__m))
5374 ;
5375 if (__i >= __j)
5376 break;
5377 swap(*__i, *__j);
5378 ++__n_swaps;
5379 // It is known that __m != __j
5380 // If __m just moved, follow it
5381 if (__m == __i)
5382 __m = __j;
5383 ++__i;
5384 }
5385 }
5386 // [__first, __i) < *__m and *__m <= [__i, __last)
5387 if (__i != __m && __comp(*__m, *__i))
5388 {
5389 swap(*__i, *__m);
5390 ++__n_swaps;
5391 }
5392 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5393 if (__nth == __i)
5394 return;
5395 if (__n_swaps == 0)
5396 {
5397 // We were given a perfectly partitioned sequence. Coincidence?
5398 if (__nth < __i)
5399 {
5400 // Check for [__first, __i) already sorted
5401 __j = __m = __first;
Arthur O'Dwyer5d956562021-02-04 23:12:525402 while (true) {
5403 if (++__j == __i) {
5404 // [__first, __i) sorted
5405 return;
5406 }
5407 if (__comp(*__j, *__m)) {
Howard Hinnant3e519522010-05-11 19:42:165408 // not yet sorted, so sort
Arthur O'Dwyer5d956562021-02-04 23:12:525409 break;
5410 }
Howard Hinnant3e519522010-05-11 19:42:165411 __m = __j;
5412 }
Howard Hinnant3e519522010-05-11 19:42:165413 }
5414 else
5415 {
5416 // Check for [__i, __last) already sorted
5417 __j = __m = __i;
Arthur O'Dwyer5d956562021-02-04 23:12:525418 while (true) {
5419 if (++__j == __last) {
5420 // [__i, __last) sorted
5421 return;
5422 }
5423 if (__comp(*__j, *__m)) {
Howard Hinnant3e519522010-05-11 19:42:165424 // not yet sorted, so sort
Arthur O'Dwyer5d956562021-02-04 23:12:525425 break;
5426 }
Howard Hinnant3e519522010-05-11 19:42:165427 __m = __j;
5428 }
Howard Hinnant3e519522010-05-11 19:42:165429 }
5430 }
Howard Hinnant3e519522010-05-11 19:42:165431 // __nth_element on range containing __nth
5432 if (__nth < __i)
5433 {
Arthur O'Dwyer19688042020-11-22 18:21:115434 // _VSTD::__nth_element<_Compare>(__first, __nth, __i, __comp);
Howard Hinnant3e519522010-05-11 19:42:165435 __last = __i;
5436 }
5437 else
5438 {
Arthur O'Dwyer19688042020-11-22 18:21:115439 // _VSTD::__nth_element<_Compare>(__i+1, __nth, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165440 __first = ++__i;
5441 }
5442 }
5443}
5444
5445template <class _RandomAccessIterator, class _Compare>
Arthur O'Dwyer5d956562021-02-04 23:12:525446inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165447void
5448nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5449{
Eric Fiselieraa1cad12019-04-12 05:18:195450 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115451 _VSTD::__nth_element<_Comp_ref>(__first, __nth, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165452}
5453
5454template <class _RandomAccessIterator>
Arthur O'Dwyer5d956562021-02-04 23:12:525455inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165456void
5457nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5458{
Howard Hinnantce48a112011-06-30 21:18:195459 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:165460}
5461
Arthur O'Dwyer493f1402020-12-20 20:21:425462// sort
5463
5464template <class _RandomAccessIterator, class _Compare>
5465inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5466void
5467sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5468{
5469 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5470 if (__libcpp_is_constant_evaluated()) {
5471 _VSTD::__partial_sort<_Comp_ref>(__first, __last, __last, _Comp_ref(__comp));
5472 } else {
5473 _VSTD::__sort<_Comp_ref>(_VSTD::__unwrap_iter(__first), _VSTD::__unwrap_iter(__last), _Comp_ref(__comp));
5474 }
5475}
5476
5477template <class _RandomAccessIterator>
5478inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5479void
5480sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
5481{
5482 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5483}
5484
Howard Hinnant3e519522010-05-11 19:42:165485// includes
5486
5487template <class _Compare, class _InputIterator1, class _InputIterator2>
Marshall Clow8da1a482018-01-22 23:10:405488_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnant3e519522010-05-11 19:42:165489__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5490 _Compare __comp)
5491{
5492 for (; __first2 != __last2; ++__first1)
5493 {
5494 if (__first1 == __last1 || __comp(*__first2, *__first1))
5495 return false;
5496 if (!__comp(*__first1, *__first2))
5497 ++__first2;
5498 }
5499 return true;
5500}
5501
5502template <class _InputIterator1, class _InputIterator2, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:085503_LIBCPP_NODISCARD_EXT inline
5504_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165505bool
5506includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5507 _Compare __comp)
5508{
Eric Fiselieraa1cad12019-04-12 05:18:195509 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115510 return _VSTD::__includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant3e519522010-05-11 19:42:165511}
5512
5513template <class _InputIterator1, class _InputIterator2>
Nico Weber1362d7e2019-04-03 18:13:085514_LIBCPP_NODISCARD_EXT inline
5515_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165516bool
5517includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5518{
Howard Hinnantce48a112011-06-30 21:18:195519 return _VSTD::includes(__first1, __last1, __first2, __last2,
Howard Hinnant3e519522010-05-11 19:42:165520 __less<typename iterator_traits<_InputIterator1>::value_type,
5521 typename iterator_traits<_InputIterator2>::value_type>());
5522}
5523
5524// set_union
5525
5526template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
Arthur O'Dwyer14098cf2020-12-04 18:47:125527_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnant3e519522010-05-11 19:42:165528__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5529 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5530{
5531 for (; __first1 != __last1; ++__result)
5532 {
5533 if (__first2 == __last2)
Howard Hinnantce48a112011-06-30 21:18:195534 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnant3e519522010-05-11 19:42:165535 if (__comp(*__first2, *__first1))
5536 {
5537 *__result = *__first2;
5538 ++__first2;
5539 }
5540 else
5541 {
Howard Hinnant3e519522010-05-11 19:42:165542 if (!__comp(*__first1, *__first2))
5543 ++__first2;
Marshall Clow05da5b02017-10-30 15:50:005544 *__result = *__first1;
Howard Hinnant3e519522010-05-11 19:42:165545 ++__first1;
5546 }
5547 }
Howard Hinnantce48a112011-06-30 21:18:195548 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnant3e519522010-05-11 19:42:165549}
5550
5551template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
Arthur O'Dwyer14098cf2020-12-04 18:47:125552inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165553_OutputIterator
5554set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5555 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5556{
Eric Fiselieraa1cad12019-04-12 05:18:195557 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115558 return _VSTD::__set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant3e519522010-05-11 19:42:165559}
5560
5561template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
Arthur O'Dwyer14098cf2020-12-04 18:47:125562inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165563_OutputIterator
5564set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5565 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5566{
Howard Hinnantce48a112011-06-30 21:18:195567 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
Howard Hinnant3e519522010-05-11 19:42:165568 __less<typename iterator_traits<_InputIterator1>::value_type,
5569 typename iterator_traits<_InputIterator2>::value_type>());
5570}
5571
5572// set_intersection
5573
5574template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
Marshall Clow8da1a482018-01-22 23:10:405575_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnant3e519522010-05-11 19:42:165576__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5577 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5578{
5579 while (__first1 != __last1 && __first2 != __last2)
5580 {
5581 if (__comp(*__first1, *__first2))
5582 ++__first1;
5583 else
5584 {
5585 if (!__comp(*__first2, *__first1))
5586 {
5587 *__result = *__first1;
5588 ++__result;
5589 ++__first1;
5590 }
5591 ++__first2;
5592 }
5593 }
5594 return __result;
5595}
5596
5597template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
Marshall Clow8da1a482018-01-22 23:10:405598inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165599_OutputIterator
5600set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5601 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5602{
Eric Fiselieraa1cad12019-04-12 05:18:195603 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115604 return _VSTD::__set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant3e519522010-05-11 19:42:165605}
5606
5607template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
Marshall Clow8da1a482018-01-22 23:10:405608inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165609_OutputIterator
5610set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5611 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5612{
Howard Hinnantce48a112011-06-30 21:18:195613 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
Howard Hinnant3e519522010-05-11 19:42:165614 __less<typename iterator_traits<_InputIterator1>::value_type,
5615 typename iterator_traits<_InputIterator2>::value_type>());
5616}
5617
5618// set_difference
5619
5620template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
Arthur O'Dwyer14098cf2020-12-04 18:47:125621_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnant3e519522010-05-11 19:42:165622__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5623 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5624{
5625 while (__first1 != __last1)
5626 {
5627 if (__first2 == __last2)
Howard Hinnantce48a112011-06-30 21:18:195628 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnant3e519522010-05-11 19:42:165629 if (__comp(*__first1, *__first2))
5630 {
5631 *__result = *__first1;
5632 ++__result;
5633 ++__first1;
5634 }
5635 else
5636 {
5637 if (!__comp(*__first2, *__first1))
5638 ++__first1;
5639 ++__first2;
5640 }
5641 }
5642 return __result;
5643}
5644
5645template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
Arthur O'Dwyer14098cf2020-12-04 18:47:125646inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165647_OutputIterator
5648set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5649 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5650{
Eric Fiselieraa1cad12019-04-12 05:18:195651 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115652 return _VSTD::__set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant3e519522010-05-11 19:42:165653}
5654
5655template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
Arthur O'Dwyer14098cf2020-12-04 18:47:125656inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165657_OutputIterator
5658set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5659 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5660{
Howard Hinnantce48a112011-06-30 21:18:195661 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnant3e519522010-05-11 19:42:165662 __less<typename iterator_traits<_InputIterator1>::value_type,
5663 typename iterator_traits<_InputIterator2>::value_type>());
5664}
5665
5666// set_symmetric_difference
5667
5668template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
Arthur O'Dwyer14098cf2020-12-04 18:47:125669_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnant3e519522010-05-11 19:42:165670__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5671 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5672{
5673 while (__first1 != __last1)
5674 {
5675 if (__first2 == __last2)
Howard Hinnantce48a112011-06-30 21:18:195676 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnant3e519522010-05-11 19:42:165677 if (__comp(*__first1, *__first2))
5678 {
5679 *__result = *__first1;
5680 ++__result;
5681 ++__first1;
5682 }
5683 else
5684 {
5685 if (__comp(*__first2, *__first1))
5686 {
5687 *__result = *__first2;
5688 ++__result;
5689 }
5690 else
5691 ++__first1;
5692 ++__first2;
5693 }
5694 }
Howard Hinnantce48a112011-06-30 21:18:195695 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnant3e519522010-05-11 19:42:165696}
5697
5698template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
Arthur O'Dwyer14098cf2020-12-04 18:47:125699inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165700_OutputIterator
5701set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5702 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5703{
Eric Fiselieraa1cad12019-04-12 05:18:195704 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115705 return _VSTD::__set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant3e519522010-05-11 19:42:165706}
5707
5708template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
Arthur O'Dwyer14098cf2020-12-04 18:47:125709inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165710_OutputIterator
5711set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5712 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5713{
Howard Hinnantce48a112011-06-30 21:18:195714 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnant3e519522010-05-11 19:42:165715 __less<typename iterator_traits<_InputIterator1>::value_type,
5716 typename iterator_traits<_InputIterator2>::value_type>());
5717}
5718
5719// lexicographical_compare
5720
5721template <class _Compare, class _InputIterator1, class _InputIterator2>
Marshall Clow1b9a4ff2018-01-22 20:44:335722_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnant3e519522010-05-11 19:42:165723__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5724 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5725{
Eric Fiselier910285b2014-10-27 19:28:205726 for (; __first2 != __last2; ++__first1, (void) ++__first2)
Howard Hinnant3e519522010-05-11 19:42:165727 {
5728 if (__first1 == __last1 || __comp(*__first1, *__first2))
5729 return true;
5730 if (__comp(*__first2, *__first1))
5731 return false;
5732 }
5733 return false;
5734}
5735
5736template <class _InputIterator1, class _InputIterator2, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:085737_LIBCPP_NODISCARD_EXT inline
5738_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165739bool
5740lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5741 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5742{
Eric Fiselieraa1cad12019-04-12 05:18:195743 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115744 return _VSTD::__lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant3e519522010-05-11 19:42:165745}
5746
5747template <class _InputIterator1, class _InputIterator2>
Nico Weber1362d7e2019-04-03 18:13:085748_LIBCPP_NODISCARD_EXT inline
5749_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165750bool
5751lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5752 _InputIterator2 __first2, _InputIterator2 __last2)
5753{
Howard Hinnantce48a112011-06-30 21:18:195754 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
Howard Hinnant3e519522010-05-11 19:42:165755 __less<typename iterator_traits<_InputIterator1>::value_type,
5756 typename iterator_traits<_InputIterator2>::value_type>());
5757}
5758
5759// next_permutation
5760
5761template <class _Compare, class _BidirectionalIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:085762_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnant3e519522010-05-11 19:42:165763__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5764{
5765 _BidirectionalIterator __i = __last;
5766 if (__first == __last || __first == --__i)
5767 return false;
5768 while (true)
5769 {
5770 _BidirectionalIterator __ip1 = __i;
5771 if (__comp(*--__i, *__ip1))
5772 {
5773 _BidirectionalIterator __j = __last;
5774 while (!__comp(*__i, *--__j))
5775 ;
5776 swap(*__i, *__j);
Howard Hinnantce48a112011-06-30 21:18:195777 _VSTD::reverse(__ip1, __last);
Howard Hinnant3e519522010-05-11 19:42:165778 return true;
5779 }
5780 if (__i == __first)
5781 {
Howard Hinnantce48a112011-06-30 21:18:195782 _VSTD::reverse(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:165783 return false;
5784 }
5785 }
5786}
5787
5788template <class _BidirectionalIterator, class _Compare>
Arthur O'Dwyerf851db32020-12-17 05:01:085789inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165790bool
5791next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5792{
Eric Fiselieraa1cad12019-04-12 05:18:195793 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115794 return _VSTD::__next_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165795}
5796
5797template <class _BidirectionalIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:085798inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantb3371f62010-08-22 00:02:435799bool
Howard Hinnant3e519522010-05-11 19:42:165800next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5801{
Howard Hinnantce48a112011-06-30 21:18:195802 return _VSTD::next_permutation(__first, __last,
Howard Hinnant3e519522010-05-11 19:42:165803 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5804}
5805
5806// prev_permutation
5807
5808template <class _Compare, class _BidirectionalIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:085809_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnant3e519522010-05-11 19:42:165810__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5811{
5812 _BidirectionalIterator __i = __last;
5813 if (__first == __last || __first == --__i)
5814 return false;
5815 while (true)
5816 {
5817 _BidirectionalIterator __ip1 = __i;
5818 if (__comp(*__ip1, *--__i))
5819 {
5820 _BidirectionalIterator __j = __last;
5821 while (!__comp(*--__j, *__i))
5822 ;
5823 swap(*__i, *__j);
Howard Hinnantce48a112011-06-30 21:18:195824 _VSTD::reverse(__ip1, __last);
Howard Hinnant3e519522010-05-11 19:42:165825 return true;
5826 }
5827 if (__i == __first)
5828 {
Howard Hinnantce48a112011-06-30 21:18:195829 _VSTD::reverse(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:165830 return false;
5831 }
5832 }
5833}
5834
5835template <class _BidirectionalIterator, class _Compare>
Arthur O'Dwyerf851db32020-12-17 05:01:085836inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165837bool
5838prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5839{
Eric Fiselieraa1cad12019-04-12 05:18:195840 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115841 return _VSTD::__prev_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165842}
5843
5844template <class _BidirectionalIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:085845inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165846bool
5847prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5848{
Howard Hinnantce48a112011-06-30 21:18:195849 return _VSTD::prev_permutation(__first, __last,
Howard Hinnant3e519522010-05-11 19:42:165850 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5851}
5852
Howard Hinnant3e519522010-05-11 19:42:165853_LIBCPP_END_NAMESPACE_STD
5854
Eric Fiseliera016efb2017-05-31 22:07:495855_LIBCPP_POP_MACROS
5856
Louis Dionne0a06eb92019-08-05 18:29:145857#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionne95689242019-08-06 21:11:245858# include <__pstl_algorithm>
Louis Dionne0a06eb92019-08-05 18:29:145859#endif
5860
Louis Dionne4cd6ca12021-04-20 16:03:325861#endif // _LIBCPP_ALGORITHM