blob: 7220585d15a3be7a562957ef0b0140b7c4d91d98 [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'Dwyer207d4be2020-12-17 05:40:02388 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'Dwyer207d4be2020-12-17 05:40:02392 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>
649#include <initializer_list>
650#include <type_traits>
651#include <cstring>
Eric Fiselierf07dd8d2016-04-21 23:38:59652#include <utility> // needed to provide swap_ranges.
Howard Hinnant3e519522010-05-11 19:42:16653#include <memory>
Marshall Clowd835e592018-01-08 19:18:00654#include <functional>
Howard Hinnant3e519522010-05-11 19:42:16655#include <iterator>
Howard Hinnanta1d07d52012-07-26 17:09:09656#include <cstddef>
Marshall Clowe02ee4f2018-08-17 16:07:48657#include <bit>
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
820 decltype((void)_VSTD::declval<_Compare&>()(
Eric Fiselier6e4ec602019-03-08 22:58:59821 _VSTD::declval<_LHS &>(), _VSTD::declval<_RHS &>()))
822 __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
Arthur O'Dwyerd41c6d52021-01-15 17:59:561642// __unwrap_iter
Howard Hinnant3e519522010-05-11 19:42:161643
Arthur O'Dwyerd41c6d52021-01-15 17:59:561644// The job of __unwrap_iter is to lower iterators-that-are-tantamount-to-pointers
1645// (such as 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// In debug mode, we don't do this.
1648
1649template <class _Iter, bool = __is_cpp17_contiguous_iterator<_Iter>::value>
1650struct __unwrap_iter_impl {
1651 static _LIBCPP_CONSTEXPR _Iter
1652 __apply(_Iter __i) _NOEXCEPT {
1653 return __i;
1654 }
1655};
Howard Hinnant3e519522010-05-11 19:42:161656
Howard Hinnantfc88dbd2013-08-23 17:37:051657#if _LIBCPP_DEBUG_LEVEL < 2
1658
Arthur O'Dwyerd41c6d52021-01-15 17:59:561659template <class _Iter>
1660struct __unwrap_iter_impl<_Iter, true> {
1661 static _LIBCPP_CONSTEXPR decltype(_VSTD::__to_address(declval<_Iter>()))
1662 __apply(_Iter __i) _NOEXCEPT {
1663 return _VSTD::__to_address(__i);
1664 }
1665};
Eric Fiselier14bd0bf2016-12-28 05:35:321666
Howard Hinnantfc88dbd2013-08-23 17:37:051667#endif // _LIBCPP_DEBUG_LEVEL < 2
1668
Arthur O'Dwyerd41c6d52021-01-15 17:59:561669template<class _Iter, class _Impl = __unwrap_iter_impl<_Iter> >
1670inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1671decltype(_Impl::__apply(_VSTD::declval<_Iter>()))
1672__unwrap_iter(_Iter __i) _NOEXCEPT
1673{
1674 return _Impl::__apply(__i);
1675}
1676
1677// copy
1678
Howard Hinnant3e519522010-05-11 19:42:161679template <class _InputIterator, class _OutputIterator>
Louis Dionne13c90a52019-11-06 12:02:411680inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161681_OutputIterator
Louis Dionne13c90a52019-11-06 12:02:411682__copy_constexpr(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
Howard Hinnant3e519522010-05-11 19:42:161683{
Eric Fiselier910285b2014-10-27 19:28:201684 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant3e519522010-05-11 19:42:161685 *__result = *__first;
1686 return __result;
1687}
1688
Louis Dionne13c90a52019-11-06 12:02:411689template <class _InputIterator, class _OutputIterator>
1690inline _LIBCPP_INLINE_VISIBILITY
1691_OutputIterator
1692__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1693{
Arthur O'Dwyer19688042020-11-22 18:21:111694 return _VSTD::__copy_constexpr(__first, __last, __result);
Louis Dionne13c90a52019-11-06 12:02:411695}
1696
Howard Hinnant3e519522010-05-11 19:42:161697template <class _Tp, class _Up>
1698inline _LIBCPP_INLINE_VISIBILITY
1699typename enable_if
1700<
1701 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnantca740482010-11-19 22:17:281702 is_trivially_copy_assignable<_Up>::value,
Howard Hinnant3e519522010-05-11 19:42:161703 _Up*
1704>::type
1705__copy(_Tp* __first, _Tp* __last, _Up* __result)
1706{
1707 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow5b312052015-06-02 13:52:161708 if (__n > 0)
1709 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnant3e519522010-05-11 19:42:161710 return __result + __n;
1711}
1712
1713template <class _InputIterator, class _OutputIterator>
Arthur O'Dwyeree95c702020-11-23 17:44:411714inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161715_OutputIterator
1716copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1717{
Louis Dionne13c90a52019-11-06 12:02:411718 if (__libcpp_is_constant_evaluated()) {
1719 return _VSTD::__copy_constexpr(
Arthur O'Dwyer19688042020-11-22 18:21:111720 _VSTD::__unwrap_iter(__first), _VSTD::__unwrap_iter(__last), _VSTD::__unwrap_iter(__result));
Louis Dionne13c90a52019-11-06 12:02:411721 } else {
1722 return _VSTD::__copy(
Arthur O'Dwyer19688042020-11-22 18:21:111723 _VSTD::__unwrap_iter(__first), _VSTD::__unwrap_iter(__last), _VSTD::__unwrap_iter(__result));
Louis Dionne13c90a52019-11-06 12:02:411724 }
Howard Hinnant3e519522010-05-11 19:42:161725}
1726
1727// copy_backward
1728
Howard Hinnantd3d43562013-02-06 21:03:391729template <class _BidirectionalIterator, class _OutputIterator>
Louis Dionne13c90a52019-11-06 12:02:411730inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1731_OutputIterator
1732__copy_backward_constexpr(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
1733{
1734 while (__first != __last)
1735 *--__result = *--__last;
1736 return __result;
1737}
1738
1739template <class _BidirectionalIterator, class _OutputIterator>
Howard Hinnant3e519522010-05-11 19:42:161740inline _LIBCPP_INLINE_VISIBILITY
1741_OutputIterator
Howard Hinnantd3d43562013-02-06 21:03:391742__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
Howard Hinnant3e519522010-05-11 19:42:161743{
Arthur O'Dwyer19688042020-11-22 18:21:111744 return _VSTD::__copy_backward_constexpr(__first, __last, __result);
Howard Hinnant3e519522010-05-11 19:42:161745}
1746
1747template <class _Tp, class _Up>
1748inline _LIBCPP_INLINE_VISIBILITY
1749typename enable_if
1750<
1751 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Howard Hinnantca740482010-11-19 22:17:281752 is_trivially_copy_assignable<_Up>::value,
Howard Hinnant3e519522010-05-11 19:42:161753 _Up*
1754>::type
1755__copy_backward(_Tp* __first, _Tp* __last, _Up* __result)
1756{
1757 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow5b312052015-06-02 13:52:161758 if (__n > 0)
1759 {
1760 __result -= __n;
1761 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1762 }
Howard Hinnant3e519522010-05-11 19:42:161763 return __result;
1764}
1765
1766template <class _BidirectionalIterator1, class _BidirectionalIterator2>
Arthur O'Dwyeree95c702020-11-23 17:44:411767inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161768_BidirectionalIterator2
1769copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1770 _BidirectionalIterator2 __result)
1771{
Louis Dionne13c90a52019-11-06 12:02:411772 if (__libcpp_is_constant_evaluated()) {
Arthur O'Dwyer19688042020-11-22 18:21:111773 return _VSTD::__copy_backward_constexpr(_VSTD::__unwrap_iter(__first),
1774 _VSTD::__unwrap_iter(__last),
1775 _VSTD::__unwrap_iter(__result));
Louis Dionne13c90a52019-11-06 12:02:411776 } else {
Arthur O'Dwyer19688042020-11-22 18:21:111777 return _VSTD::__copy_backward(_VSTD::__unwrap_iter(__first),
1778 _VSTD::__unwrap_iter(__last),
1779 _VSTD::__unwrap_iter(__result));
Louis Dionne13c90a52019-11-06 12:02:411780 }
Howard Hinnant3e519522010-05-11 19:42:161781}
1782
1783// copy_if
1784
1785template<class _InputIterator, class _OutputIterator, class _Predicate>
Louis Dionne13c90a52019-11-06 12:02:411786inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161787_OutputIterator
1788copy_if(_InputIterator __first, _InputIterator __last,
1789 _OutputIterator __result, _Predicate __pred)
1790{
1791 for (; __first != __last; ++__first)
1792 {
1793 if (__pred(*__first))
1794 {
1795 *__result = *__first;
1796 ++__result;
1797 }
1798 }
1799 return __result;
1800}
1801
1802// copy_n
1803
1804template<class _InputIterator, class _Size, class _OutputIterator>
Arthur O'Dwyeree95c702020-11-23 17:44:411805inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161806typename enable_if
1807<
Eric Fiselierf82dba02019-11-18 06:46:581808 __is_cpp17_input_iterator<_InputIterator>::value &&
1809 !__is_cpp17_random_access_iterator<_InputIterator>::value,
Howard Hinnant3e519522010-05-11 19:42:161810 _OutputIterator
1811>::type
Eric Fiselier51544022015-02-10 16:46:421812copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnant3e519522010-05-11 19:42:161813{
Arthur O'Dwyerc0428b32020-12-08 04:42:471814 typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
Eric Fiselier51544022015-02-10 16:46:421815 _IntegralSize __n = __orig_n;
Howard Hinnant99847d22011-02-27 20:55:391816 if (__n > 0)
1817 {
Howard Hinnant3e519522010-05-11 19:42:161818 *__result = *__first;
Howard Hinnant99847d22011-02-27 20:55:391819 ++__result;
1820 for (--__n; __n > 0; --__n)
1821 {
1822 ++__first;
1823 *__result = *__first;
1824 ++__result;
1825 }
1826 }
Howard Hinnant3e519522010-05-11 19:42:161827 return __result;
1828}
1829
1830template<class _InputIterator, class _Size, class _OutputIterator>
Arthur O'Dwyeree95c702020-11-23 17:44:411831inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161832typename enable_if
1833<
Eric Fiselierf82dba02019-11-18 06:46:581834 __is_cpp17_random_access_iterator<_InputIterator>::value,
Howard Hinnant3e519522010-05-11 19:42:161835 _OutputIterator
1836>::type
Eric Fiselier51544022015-02-10 16:46:421837copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
Howard Hinnant3e519522010-05-11 19:42:161838{
Arthur O'Dwyerc0428b32020-12-08 04:42:471839 typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
Eric Fiselier51544022015-02-10 16:46:421840 _IntegralSize __n = __orig_n;
Howard Hinnantce48a112011-06-30 21:18:191841 return _VSTD::copy(__first, __first + __n, __result);
Howard Hinnant3e519522010-05-11 19:42:161842}
1843
1844// move
1845
zoecarver3ed89b52020-09-14 22:11:081846// __move_constexpr exists so that __move doesn't call itself when delegating to the constexpr
1847// version of __move.
Howard Hinnant3e519522010-05-11 19:42:161848template <class _InputIterator, class _OutputIterator>
zoecarver3ed89b52020-09-14 22:11:081849inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnant3e519522010-05-11 19:42:161850_OutputIterator
zoecarver3ed89b52020-09-14 22:11:081851__move_constexpr(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
Howard Hinnant3e519522010-05-11 19:42:161852{
Eric Fiselier910285b2014-10-27 19:28:201853 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnantce48a112011-06-30 21:18:191854 *__result = _VSTD::move(*__first);
Howard Hinnant3e519522010-05-11 19:42:161855 return __result;
1856}
1857
zoecarver3ed89b52020-09-14 22:11:081858template <class _InputIterator, class _OutputIterator>
1859inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1860_OutputIterator
1861__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1862{
Arthur O'Dwyer19688042020-11-22 18:21:111863 return _VSTD::__move_constexpr(__first, __last, __result);
zoecarver3ed89b52020-09-14 22:11:081864}
1865
Howard Hinnant3e519522010-05-11 19:42:161866template <class _Tp, class _Up>
zoecarver3ed89b52020-09-14 22:11:081867inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnant3e519522010-05-11 19:42:161868typename enable_if
1869<
1870 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Arthur O'Dwyerd41c6d52021-01-15 17:59:561871 is_trivially_move_assignable<_Up>::value,
Howard Hinnant3e519522010-05-11 19:42:161872 _Up*
1873>::type
1874__move(_Tp* __first, _Tp* __last, _Up* __result)
1875{
zoecarver3ed89b52020-09-14 22:11:081876 if (__libcpp_is_constant_evaluated())
Arthur O'Dwyer19688042020-11-22 18:21:111877 return _VSTD::__move_constexpr(__first, __last, __result);
Howard Hinnant3e519522010-05-11 19:42:161878 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow5b312052015-06-02 13:52:161879 if (__n > 0)
1880 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
Howard Hinnant3e519522010-05-11 19:42:161881 return __result + __n;
1882}
1883
1884template <class _InputIterator, class _OutputIterator>
zoecarver3ed89b52020-09-14 22:11:081885inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161886_OutputIterator
1887move(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1888{
Arthur O'Dwyer19688042020-11-22 18:21:111889 return _VSTD::__move(_VSTD::__unwrap_iter(__first), _VSTD::__unwrap_iter(__last), _VSTD::__unwrap_iter(__result));
Howard Hinnant3e519522010-05-11 19:42:161890}
1891
1892// move_backward
1893
zoecarver3ed89b52020-09-14 22:11:081894// __move_backward_constexpr exists so that __move_backward doesn't call itself when delegating to
1895// the constexpr version of __move_backward.
Howard Hinnant3e519522010-05-11 19:42:161896template <class _InputIterator, class _OutputIterator>
zoecarver3ed89b52020-09-14 22:11:081897inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnant3e519522010-05-11 19:42:161898_OutputIterator
zoecarver3ed89b52020-09-14 22:11:081899__move_backward_constexpr(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
Howard Hinnant3e519522010-05-11 19:42:161900{
1901 while (__first != __last)
Howard Hinnantce48a112011-06-30 21:18:191902 *--__result = _VSTD::move(*--__last);
Howard Hinnant3e519522010-05-11 19:42:161903 return __result;
1904}
1905
zoecarver3ed89b52020-09-14 22:11:081906template <class _InputIterator, class _OutputIterator>
1907inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1908_OutputIterator
1909__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
1910{
Arthur O'Dwyer19688042020-11-22 18:21:111911 return _VSTD::__move_backward_constexpr(__first, __last, __result);
zoecarver3ed89b52020-09-14 22:11:081912}
1913
Howard Hinnant3e519522010-05-11 19:42:161914template <class _Tp, class _Up>
zoecarver3ed89b52020-09-14 22:11:081915inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnant3e519522010-05-11 19:42:161916typename enable_if
1917<
1918 is_same<typename remove_const<_Tp>::type, _Up>::value &&
Arthur O'Dwyerd41c6d52021-01-15 17:59:561919 is_trivially_move_assignable<_Up>::value,
Howard Hinnant3e519522010-05-11 19:42:161920 _Up*
1921>::type
1922__move_backward(_Tp* __first, _Tp* __last, _Up* __result)
1923{
zoecarver3ed89b52020-09-14 22:11:081924 if (__libcpp_is_constant_evaluated())
Arthur O'Dwyer19688042020-11-22 18:21:111925 return _VSTD::__move_backward_constexpr(__first, __last, __result);
Howard Hinnant3e519522010-05-11 19:42:161926 const size_t __n = static_cast<size_t>(__last - __first);
Marshall Clow5b312052015-06-02 13:52:161927 if (__n > 0)
1928 {
1929 __result -= __n;
1930 _VSTD::memmove(__result, __first, __n * sizeof(_Up));
1931 }
Howard Hinnant3e519522010-05-11 19:42:161932 return __result;
1933}
1934
1935template <class _BidirectionalIterator1, class _BidirectionalIterator2>
zoecarver3ed89b52020-09-14 22:11:081936inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161937_BidirectionalIterator2
1938move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
1939 _BidirectionalIterator2 __result)
1940{
Arthur O'Dwyer19688042020-11-22 18:21:111941 return _VSTD::__move_backward(_VSTD::__unwrap_iter(__first), _VSTD::__unwrap_iter(__last), _VSTD::__unwrap_iter(__result));
Howard Hinnant3e519522010-05-11 19:42:161942}
1943
1944// iter_swap
1945
Howard Hinnanta676f7d2011-05-27 15:04:191946// moved to <type_traits> for better swap / noexcept support
Howard Hinnant3e519522010-05-11 19:42:161947
1948// transform
1949
1950template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
Marshall Clow99894b62018-01-19 17:45:391951inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161952_OutputIterator
1953transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
1954{
Eric Fiselier910285b2014-10-27 19:28:201955 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant3e519522010-05-11 19:42:161956 *__result = __op(*__first);
1957 return __result;
1958}
1959
1960template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
Marshall Clow99894b62018-01-19 17:45:391961inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161962_OutputIterator
1963transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
1964 _OutputIterator __result, _BinaryOperation __binary_op)
1965{
Eric Fiselier910285b2014-10-27 19:28:201966 for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result)
Howard Hinnant3e519522010-05-11 19:42:161967 *__result = __binary_op(*__first1, *__first2);
1968 return __result;
1969}
1970
1971// replace
1972
1973template <class _ForwardIterator, class _Tp>
Marshall Clow12c74232018-01-19 18:07:291974inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161975void
1976replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value)
1977{
1978 for (; __first != __last; ++__first)
1979 if (*__first == __old_value)
1980 *__first = __new_value;
1981}
1982
1983// replace_if
1984
1985template <class _ForwardIterator, class _Predicate, class _Tp>
Marshall Clow12c74232018-01-19 18:07:291986inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161987void
1988replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value)
1989{
1990 for (; __first != __last; ++__first)
1991 if (__pred(*__first))
1992 *__first = __new_value;
1993}
1994
1995// replace_copy
1996
1997template <class _InputIterator, class _OutputIterator, class _Tp>
Marshall Clow12c74232018-01-19 18:07:291998inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:161999_OutputIterator
2000replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2001 const _Tp& __old_value, const _Tp& __new_value)
2002{
Eric Fiselier910285b2014-10-27 19:28:202003 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant3e519522010-05-11 19:42:162004 if (*__first == __old_value)
2005 *__result = __new_value;
2006 else
2007 *__result = *__first;
2008 return __result;
2009}
2010
2011// replace_copy_if
2012
2013template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp>
Marshall Clow12c74232018-01-19 18:07:292014inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162015_OutputIterator
2016replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
2017 _Predicate __pred, const _Tp& __new_value)
2018{
Eric Fiselier910285b2014-10-27 19:28:202019 for (; __first != __last; ++__first, (void) ++__result)
Howard Hinnant3e519522010-05-11 19:42:162020 if (__pred(*__first))
2021 *__result = __new_value;
2022 else
2023 *__result = *__first;
2024 return __result;
2025}
2026
2027// fill_n
2028
2029template <class _OutputIterator, class _Size, class _Tp>
Marshall Clow4bfb9312018-01-20 20:14:322030inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162031_OutputIterator
Howard Hinnant0f242be2013-08-01 17:29:282032__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:162033{
Eric Fiselier910285b2014-10-27 19:28:202034 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnante4383372011-10-22 20:59:452035 *__first = __value_;
Howard Hinnant3e519522010-05-11 19:42:162036 return __first;
2037}
2038
Howard Hinnant3e519522010-05-11 19:42:162039template <class _OutputIterator, class _Size, class _Tp>
Marshall Clow4bfb9312018-01-20 20:14:322040inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162041_OutputIterator
Howard Hinnante4383372011-10-22 20:59:452042fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:162043{
Arthur O'Dwyerc0428b32020-12-08 04:42:472044 return _VSTD::__fill_n(__first, _VSTD::__convert_to_integral(__n), __value_);
Howard Hinnant3e519522010-05-11 19:42:162045}
2046
2047// fill
2048
2049template <class _ForwardIterator, class _Tp>
Marshall Clow4bfb9312018-01-20 20:14:322050inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162051void
Howard Hinnante4383372011-10-22 20:59:452052__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag)
Howard Hinnant3e519522010-05-11 19:42:162053{
2054 for (; __first != __last; ++__first)
Howard Hinnante4383372011-10-22 20:59:452055 *__first = __value_;
Howard Hinnant3e519522010-05-11 19:42:162056}
2057
2058template <class _RandomAccessIterator, class _Tp>
Marshall Clow4bfb9312018-01-20 20:14:322059inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162060void
Howard Hinnante4383372011-10-22 20:59:452061__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag)
Howard Hinnant3e519522010-05-11 19:42:162062{
Howard Hinnante4383372011-10-22 20:59:452063 _VSTD::fill_n(__first, __last - __first, __value_);
Howard Hinnant3e519522010-05-11 19:42:162064}
2065
2066template <class _ForwardIterator, class _Tp>
Marshall Clow4bfb9312018-01-20 20:14:322067inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162068void
Howard Hinnante4383372011-10-22 20:59:452069fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:162070{
Howard Hinnante4383372011-10-22 20:59:452071 _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnant3e519522010-05-11 19:42:162072}
2073
2074// generate
2075
2076template <class _ForwardIterator, class _Generator>
Marshall Clow4bfb9312018-01-20 20:14:322077inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162078void
2079generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
2080{
2081 for (; __first != __last; ++__first)
2082 *__first = __gen();
2083}
2084
2085// generate_n
2086
2087template <class _OutputIterator, class _Size, class _Generator>
Nico Weber1362d7e2019-04-03 18:13:082088inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162089_OutputIterator
Eric Fiselier51544022015-02-10 16:46:422090generate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen)
Howard Hinnant3e519522010-05-11 19:42:162091{
Arthur O'Dwyerc0428b32020-12-08 04:42:472092 typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
Eric Fiselier51544022015-02-10 16:46:422093 _IntegralSize __n = __orig_n;
Eric Fiselier910285b2014-10-27 19:28:202094 for (; __n > 0; ++__first, (void) --__n)
Howard Hinnant3e519522010-05-11 19:42:162095 *__first = __gen();
2096 return __first;
2097}
2098
2099// remove
2100
2101template <class _ForwardIterator, class _Tp>
Nico Weber1362d7e2019-04-03 18:13:082102_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnante4383372011-10-22 20:59:452103remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:162104{
Howard Hinnante4383372011-10-22 20:59:452105 __first = _VSTD::find(__first, __last, __value_);
Howard Hinnant3e519522010-05-11 19:42:162106 if (__first != __last)
2107 {
2108 _ForwardIterator __i = __first;
2109 while (++__i != __last)
2110 {
Howard Hinnante4383372011-10-22 20:59:452111 if (!(*__i == __value_))
Howard Hinnant3e519522010-05-11 19:42:162112 {
Howard Hinnantce48a112011-06-30 21:18:192113 *__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:162114 ++__first;
2115 }
2116 }
2117 }
2118 return __first;
2119}
2120
2121// remove_if
2122
2123template <class _ForwardIterator, class _Predicate>
Nico Weber1362d7e2019-04-03 18:13:082124_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:162125remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
2126{
Howard Hinnantce48a112011-06-30 21:18:192127 __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type>
Howard Hinnant3e519522010-05-11 19:42:162128 (__first, __last, __pred);
2129 if (__first != __last)
2130 {
2131 _ForwardIterator __i = __first;
2132 while (++__i != __last)
2133 {
2134 if (!__pred(*__i))
2135 {
Howard Hinnantce48a112011-06-30 21:18:192136 *__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:162137 ++__first;
2138 }
2139 }
2140 }
2141 return __first;
2142}
2143
2144// remove_copy
2145
2146template <class _InputIterator, class _OutputIterator, class _Tp>
Marshall Clowe8ea8292018-01-22 21:43:042147inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162148_OutputIterator
Howard Hinnante4383372011-10-22 20:59:452149remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:162150{
2151 for (; __first != __last; ++__first)
2152 {
Howard Hinnante4383372011-10-22 20:59:452153 if (!(*__first == __value_))
Howard Hinnant3e519522010-05-11 19:42:162154 {
2155 *__result = *__first;
2156 ++__result;
2157 }
2158 }
2159 return __result;
2160}
2161
2162// remove_copy_if
2163
2164template <class _InputIterator, class _OutputIterator, class _Predicate>
Marshall Clowe8ea8292018-01-22 21:43:042165inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162166_OutputIterator
2167remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred)
2168{
2169 for (; __first != __last; ++__first)
2170 {
2171 if (!__pred(*__first))
2172 {
2173 *__result = *__first;
2174 ++__result;
2175 }
2176 }
2177 return __result;
2178}
2179
2180// unique
2181
2182template <class _ForwardIterator, class _BinaryPredicate>
Nico Weber1362d7e2019-04-03 18:13:082183_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:162184unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred)
2185{
Howard Hinnantce48a112011-06-30 21:18:192186 __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnant3e519522010-05-11 19:42:162187 (__first, __last, __pred);
2188 if (__first != __last)
2189 {
2190 // ... a a ? ...
2191 // f i
2192 _ForwardIterator __i = __first;
2193 for (++__i; ++__i != __last;)
2194 if (!__pred(*__first, *__i))
Howard Hinnantce48a112011-06-30 21:18:192195 *++__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:162196 ++__first;
2197 }
2198 return __first;
2199}
2200
2201template <class _ForwardIterator>
Nico Weber1362d7e2019-04-03 18:13:082202_LIBCPP_NODISCARD_EXT inline
2203_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162204_ForwardIterator
2205unique(_ForwardIterator __first, _ForwardIterator __last)
2206{
2207 typedef typename iterator_traits<_ForwardIterator>::value_type __v;
Howard Hinnantce48a112011-06-30 21:18:192208 return _VSTD::unique(__first, __last, __equal_to<__v>());
Howard Hinnant3e519522010-05-11 19:42:162209}
2210
2211// unique_copy
2212
2213template <class _BinaryPredicate, class _InputIterator, class _OutputIterator>
Marshall Clow4bfb9312018-01-20 20:14:322214_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnant3e519522010-05-11 19:42:162215__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2216 input_iterator_tag, output_iterator_tag)
2217{
2218 if (__first != __last)
2219 {
2220 typename iterator_traits<_InputIterator>::value_type __t(*__first);
2221 *__result = __t;
2222 ++__result;
2223 while (++__first != __last)
2224 {
2225 if (!__pred(__t, *__first))
2226 {
2227 __t = *__first;
2228 *__result = __t;
2229 ++__result;
2230 }
2231 }
2232 }
2233 return __result;
2234}
2235
2236template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator>
Marshall Clow4bfb9312018-01-20 20:14:322237_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnant3e519522010-05-11 19:42:162238__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred,
2239 forward_iterator_tag, output_iterator_tag)
2240{
2241 if (__first != __last)
2242 {
2243 _ForwardIterator __i = __first;
2244 *__result = *__i;
2245 ++__result;
2246 while (++__first != __last)
2247 {
2248 if (!__pred(*__i, *__first))
2249 {
2250 *__result = *__first;
2251 ++__result;
2252 __i = __first;
2253 }
2254 }
2255 }
2256 return __result;
2257}
2258
2259template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator>
Marshall Clow4bfb9312018-01-20 20:14:322260_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:162261__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred,
2262 input_iterator_tag, forward_iterator_tag)
2263{
2264 if (__first != __last)
2265 {
2266 *__result = *__first;
2267 while (++__first != __last)
2268 if (!__pred(*__result, *__first))
2269 *++__result = *__first;
2270 ++__result;
2271 }
2272 return __result;
2273}
2274
Howard Hinnant3e519522010-05-11 19:42:162275template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
Marshall Clow4bfb9312018-01-20 20:14:322276inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162277_OutputIterator
2278unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred)
2279{
Howard Hinnantce48a112011-06-30 21:18:192280 return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type>
Howard Hinnant3e519522010-05-11 19:42:162281 (__first, __last, __result, __pred,
2282 typename iterator_traits<_InputIterator>::iterator_category(),
2283 typename iterator_traits<_OutputIterator>::iterator_category());
2284}
2285
2286template <class _InputIterator, class _OutputIterator>
Marshall Clow4bfb9312018-01-20 20:14:322287inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162288_OutputIterator
2289unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
2290{
2291 typedef typename iterator_traits<_InputIterator>::value_type __v;
Howard Hinnantce48a112011-06-30 21:18:192292 return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>());
Howard Hinnant3e519522010-05-11 19:42:162293}
2294
2295// reverse
2296
2297template <class _BidirectionalIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:082298inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162299void
2300__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag)
2301{
2302 while (__first != __last)
2303 {
2304 if (__first == --__last)
2305 break;
Marshall Clowdef501d2015-11-02 21:34:252306 _VSTD::iter_swap(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:162307 ++__first;
2308 }
2309}
2310
2311template <class _RandomAccessIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:082312inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162313void
2314__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag)
2315{
2316 if (__first != __last)
2317 for (; __first < --__last; ++__first)
Marshall Clowdef501d2015-11-02 21:34:252318 _VSTD::iter_swap(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:162319}
2320
2321template <class _BidirectionalIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:082322inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162323void
2324reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
2325{
Howard Hinnantce48a112011-06-30 21:18:192326 _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category());
Howard Hinnant3e519522010-05-11 19:42:162327}
2328
2329// reverse_copy
2330
2331template <class _BidirectionalIterator, class _OutputIterator>
Marshall Clowe8ea8292018-01-22 21:43:042332inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162333_OutputIterator
2334reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result)
2335{
2336 for (; __first != __last; ++__result)
2337 *__result = *--__last;
2338 return __result;
2339}
2340
2341// rotate
2342
2343template <class _ForwardIterator>
zoecarver3ed89b52020-09-14 22:11:082344_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator
Howard Hinnantaca09de2012-08-03 18:01:202345__rotate_left(_ForwardIterator __first, _ForwardIterator __last)
Howard Hinnant3e519522010-05-11 19:42:162346{
Howard Hinnantaca09de2012-08-03 18:01:202347 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
2348 value_type __tmp = _VSTD::move(*__first);
2349 _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first);
2350 *__lm1 = _VSTD::move(__tmp);
2351 return __lm1;
2352}
2353
2354template <class _BidirectionalIterator>
zoecarver3ed89b52020-09-14 22:11:082355_LIBCPP_CONSTEXPR_AFTER_CXX11 _BidirectionalIterator
Howard Hinnantaca09de2012-08-03 18:01:202356__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last)
2357{
2358 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
2359 _BidirectionalIterator __lm1 = _VSTD::prev(__last);
2360 value_type __tmp = _VSTD::move(*__lm1);
2361 _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last);
2362 *__first = _VSTD::move(__tmp);
2363 return __fp1;
2364}
2365
2366template <class _ForwardIterator>
zoecarver3ed89b52020-09-14 22:11:082367_LIBCPP_CONSTEXPR_AFTER_CXX14 _ForwardIterator
Howard Hinnantaca09de2012-08-03 18:01:202368__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2369{
Howard Hinnant3e519522010-05-11 19:42:162370 _ForwardIterator __i = __middle;
2371 while (true)
2372 {
2373 swap(*__first, *__i);
2374 ++__first;
2375 if (++__i == __last)
2376 break;
2377 if (__first == __middle)
2378 __middle = __i;
2379 }
2380 _ForwardIterator __r = __first;
2381 if (__first != __middle)
2382 {
2383 __i = __middle;
2384 while (true)
2385 {
2386 swap(*__first, *__i);
2387 ++__first;
2388 if (++__i == __last)
2389 {
2390 if (__first == __middle)
2391 break;
2392 __i = __middle;
2393 }
2394 else if (__first == __middle)
2395 __middle = __i;
2396 }
2397 }
2398 return __r;
2399}
2400
2401template<typename _Integral>
2402inline _LIBCPP_INLINE_VISIBILITY
zoecarver3ed89b52020-09-14 22:11:082403_LIBCPP_CONSTEXPR_AFTER_CXX14 _Integral
Marshall Clow19b40352016-07-26 14:29:452404__algo_gcd(_Integral __x, _Integral __y)
Howard Hinnant3e519522010-05-11 19:42:162405{
2406 do
2407 {
2408 _Integral __t = __x % __y;
2409 __x = __y;
2410 __y = __t;
2411 } while (__y);
2412 return __x;
2413}
2414
2415template<typename _RandomAccessIterator>
zoecarver3ed89b52020-09-14 22:11:082416_LIBCPP_CONSTEXPR_AFTER_CXX14 _RandomAccessIterator
Howard Hinnantaca09de2012-08-03 18:01:202417__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
Howard Hinnant3e519522010-05-11 19:42:162418{
2419 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
2420 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnantb3371f62010-08-22 00:02:432421
Howard Hinnant3e519522010-05-11 19:42:162422 const difference_type __m1 = __middle - __first;
2423 const difference_type __m2 = __last - __middle;
2424 if (__m1 == __m2)
2425 {
Howard Hinnantce48a112011-06-30 21:18:192426 _VSTD::swap_ranges(__first, __middle, __middle);
Howard Hinnant3e519522010-05-11 19:42:162427 return __middle;
2428 }
Marshall Clow19b40352016-07-26 14:29:452429 const difference_type __g = _VSTD::__algo_gcd(__m1, __m2);
Howard Hinnant3e519522010-05-11 19:42:162430 for (_RandomAccessIterator __p = __first + __g; __p != __first;)
2431 {
Howard Hinnantaca09de2012-08-03 18:01:202432 value_type __t(_VSTD::move(*--__p));
Howard Hinnant3e519522010-05-11 19:42:162433 _RandomAccessIterator __p1 = __p;
2434 _RandomAccessIterator __p2 = __p1 + __m1;
2435 do
2436 {
Howard Hinnantaca09de2012-08-03 18:01:202437 *__p1 = _VSTD::move(*__p2);
Howard Hinnant3e519522010-05-11 19:42:162438 __p1 = __p2;
2439 const difference_type __d = __last - __p2;
2440 if (__m1 < __d)
2441 __p2 += __m1;
2442 else
2443 __p2 = __first + (__m1 - __d);
2444 } while (__p2 != __p);
Howard Hinnantaca09de2012-08-03 18:01:202445 *__p1 = _VSTD::move(__t);
Howard Hinnant3e519522010-05-11 19:42:162446 }
2447 return __first + __m2;
2448}
2449
2450template <class _ForwardIterator>
2451inline _LIBCPP_INLINE_VISIBILITY
zoecarver3ed89b52020-09-14 22:11:082452_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator
Howard Hinnantaca09de2012-08-03 18:01:202453__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last,
2454 _VSTD::forward_iterator_tag)
2455{
2456 typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type;
2457 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2458 {
2459 if (_VSTD::next(__first) == __middle)
2460 return _VSTD::__rotate_left(__first, __last);
2461 }
2462 return _VSTD::__rotate_forward(__first, __middle, __last);
2463}
2464
2465template <class _BidirectionalIterator>
2466inline _LIBCPP_INLINE_VISIBILITY
zoecarver3ed89b52020-09-14 22:11:082467_LIBCPP_CONSTEXPR_AFTER_CXX11 _BidirectionalIterator
Howard Hinnantaca09de2012-08-03 18:01:202468__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
2469 _VSTD::bidirectional_iterator_tag)
2470{
2471 typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type;
2472 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2473 {
2474 if (_VSTD::next(__first) == __middle)
2475 return _VSTD::__rotate_left(__first, __last);
2476 if (_VSTD::next(__middle) == __last)
2477 return _VSTD::__rotate_right(__first, __last);
2478 }
2479 return _VSTD::__rotate_forward(__first, __middle, __last);
2480}
2481
2482template <class _RandomAccessIterator>
2483inline _LIBCPP_INLINE_VISIBILITY
zoecarver3ed89b52020-09-14 22:11:082484_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator
Howard Hinnantaca09de2012-08-03 18:01:202485__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
2486 _VSTD::random_access_iterator_tag)
2487{
2488 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type;
2489 if (_VSTD::is_trivially_move_assignable<value_type>::value)
2490 {
2491 if (_VSTD::next(__first) == __middle)
2492 return _VSTD::__rotate_left(__first, __last);
2493 if (_VSTD::next(__middle) == __last)
2494 return _VSTD::__rotate_right(__first, __last);
2495 return _VSTD::__rotate_gcd(__first, __middle, __last);
2496 }
2497 return _VSTD::__rotate_forward(__first, __middle, __last);
2498}
2499
2500template <class _ForwardIterator>
2501inline _LIBCPP_INLINE_VISIBILITY
zoecarver3ed89b52020-09-14 22:11:082502_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:162503rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
2504{
Howard Hinnantaca09de2012-08-03 18:01:202505 if (__first == __middle)
2506 return __last;
2507 if (__middle == __last)
2508 return __first;
Howard Hinnantce48a112011-06-30 21:18:192509 return _VSTD::__rotate(__first, __middle, __last,
Howard Hinnantaca09de2012-08-03 18:01:202510 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category());
Howard Hinnant3e519522010-05-11 19:42:162511}
2512
2513// rotate_copy
2514
2515template <class _ForwardIterator, class _OutputIterator>
Nicholas-Baronb552a302020-09-14 20:37:412516inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:162517_OutputIterator
2518rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result)
2519{
Howard Hinnantce48a112011-06-30 21:18:192520 return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result));
Howard Hinnant3e519522010-05-11 19:42:162521}
2522
Howard Hinnant3e519522010-05-11 19:42:162523// min_element
2524
2525template <class _ForwardIterator, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:082526_LIBCPP_NODISCARD_EXT inline
2527_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant3e519522010-05-11 19:42:162528_ForwardIterator
Marshall Clow0b0671a2015-05-10 13:53:312529min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:162530{
Eric Fiselierf82dba02019-11-18 06:46:582531 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
Eric Fiseliera60d7fa2018-08-22 17:47:132532 "std::min_element requires a ForwardIterator");
Howard Hinnant3e519522010-05-11 19:42:162533 if (__first != __last)
2534 {
2535 _ForwardIterator __i = __first;
2536 while (++__i != __last)
2537 if (__comp(*__i, *__first))
2538 __first = __i;
2539 }
2540 return __first;
2541}
2542
2543template <class _ForwardIterator>
Nico Weber1362d7e2019-04-03 18:13:082544_LIBCPP_NODISCARD_EXT inline
2545_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant3e519522010-05-11 19:42:162546_ForwardIterator
2547min_element(_ForwardIterator __first, _ForwardIterator __last)
2548{
Marshall Clow0b0671a2015-05-10 13:53:312549 return _VSTD::min_element(__first, __last,
Howard Hinnant4eb27b72010-08-21 20:10:012550 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2551}
2552
2553// min
2554
2555template <class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:082556_LIBCPP_NODISCARD_EXT inline
2557_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012558const _Tp&
2559min(const _Tp& __a, const _Tp& __b, _Compare __comp)
2560{
2561 return __comp(__b, __a) ? __b : __a;
2562}
2563
2564template <class _Tp>
Nico Weber1362d7e2019-04-03 18:13:082565_LIBCPP_NODISCARD_EXT inline
2566_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012567const _Tp&
2568min(const _Tp& __a, const _Tp& __b)
2569{
Howard Hinnantce48a112011-06-30 21:18:192570 return _VSTD::min(__a, __b, __less<_Tp>());
Howard Hinnant4eb27b72010-08-21 20:10:012571}
2572
Eric Fiselierddda4562017-04-18 23:26:472573#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022574
Howard Hinnant4eb27b72010-08-21 20:10:012575template<class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:082576_LIBCPP_NODISCARD_EXT inline
2577_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012578_Tp
2579min(initializer_list<_Tp> __t, _Compare __comp)
2580{
Marshall Clow0b0671a2015-05-10 13:53:312581 return *_VSTD::min_element(__t.begin(), __t.end(), __comp);
Howard Hinnant4eb27b72010-08-21 20:10:012582}
2583
2584template<class _Tp>
Nico Weber1362d7e2019-04-03 18:13:082585_LIBCPP_NODISCARD_EXT inline
2586_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012587_Tp
2588min(initializer_list<_Tp> __t)
2589{
Marshall Clow0b0671a2015-05-10 13:53:312590 return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnant3e519522010-05-11 19:42:162591}
2592
Eric Fiselierddda4562017-04-18 23:26:472593#endif // _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022594
Howard Hinnant3e519522010-05-11 19:42:162595// max_element
2596
2597template <class _ForwardIterator, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:082598_LIBCPP_NODISCARD_EXT inline
2599_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant3e519522010-05-11 19:42:162600_ForwardIterator
Marshall Clow0b0671a2015-05-10 13:53:312601max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:162602{
Eric Fiselierf82dba02019-11-18 06:46:582603 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
Eric Fiseliera60d7fa2018-08-22 17:47:132604 "std::max_element requires a ForwardIterator");
Howard Hinnant3e519522010-05-11 19:42:162605 if (__first != __last)
2606 {
2607 _ForwardIterator __i = __first;
2608 while (++__i != __last)
2609 if (__comp(*__first, *__i))
2610 __first = __i;
2611 }
2612 return __first;
2613}
2614
Marshall Clow9d67c6d2014-02-19 16:51:352615
Howard Hinnant3e519522010-05-11 19:42:162616template <class _ForwardIterator>
Nico Weber1362d7e2019-04-03 18:13:082617_LIBCPP_NODISCARD_EXT inline
2618_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant3e519522010-05-11 19:42:162619_ForwardIterator
2620max_element(_ForwardIterator __first, _ForwardIterator __last)
2621{
Marshall Clow0b0671a2015-05-10 13:53:312622 return _VSTD::max_element(__first, __last,
Howard Hinnant4eb27b72010-08-21 20:10:012623 __less<typename iterator_traits<_ForwardIterator>::value_type>());
2624}
2625
2626// max
2627
2628template <class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:082629_LIBCPP_NODISCARD_EXT inline
2630_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012631const _Tp&
2632max(const _Tp& __a, const _Tp& __b, _Compare __comp)
2633{
2634 return __comp(__a, __b) ? __b : __a;
2635}
2636
2637template <class _Tp>
Nico Weber1362d7e2019-04-03 18:13:082638_LIBCPP_NODISCARD_EXT inline
2639_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012640const _Tp&
2641max(const _Tp& __a, const _Tp& __b)
2642{
Howard Hinnantce48a112011-06-30 21:18:192643 return _VSTD::max(__a, __b, __less<_Tp>());
Howard Hinnant4eb27b72010-08-21 20:10:012644}
2645
Eric Fiselierddda4562017-04-18 23:26:472646#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022647
Howard Hinnant4eb27b72010-08-21 20:10:012648template<class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:082649_LIBCPP_NODISCARD_EXT inline
2650_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012651_Tp
2652max(initializer_list<_Tp> __t, _Compare __comp)
2653{
Marshall Clow0b0671a2015-05-10 13:53:312654 return *_VSTD::max_element(__t.begin(), __t.end(), __comp);
Howard Hinnant4eb27b72010-08-21 20:10:012655}
2656
2657template<class _Tp>
Nico Weber1362d7e2019-04-03 18:13:082658_LIBCPP_NODISCARD_EXT inline
2659_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012660_Tp
2661max(initializer_list<_Tp> __t)
2662{
Marshall Clow0b0671a2015-05-10 13:53:312663 return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>());
Howard Hinnant3e519522010-05-11 19:42:162664}
2665
Eric Fiselierddda4562017-04-18 23:26:472666#endif // _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022667
Marshall Clow146c14a2016-03-07 22:43:492668#if _LIBCPP_STD_VER > 14
2669// clamp
2670template<class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:082671_LIBCPP_NODISCARD_EXT inline
2672_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow146c14a2016-03-07 22:43:492673const _Tp&
2674clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
2675{
2676 _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
2677 return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
2678
2679}
2680
2681template<class _Tp>
Nico Weber1362d7e2019-04-03 18:13:082682_LIBCPP_NODISCARD_EXT inline
2683_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow146c14a2016-03-07 22:43:492684const _Tp&
2685clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
2686{
2687 return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>());
2688}
2689#endif
2690
Howard Hinnant3e519522010-05-11 19:42:162691// minmax_element
2692
2693template <class _ForwardIterator, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:082694_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX11
Arthur O'Dwyerd586f922020-11-27 16:02:062695pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant3e519522010-05-11 19:42:162696minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
2697{
Eric Fiselierf82dba02019-11-18 06:46:582698 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
Eric Fiseliera60d7fa2018-08-22 17:47:132699 "std::minmax_element requires a ForwardIterator");
Arthur O'Dwyerd586f922020-11-27 16:02:062700 pair<_ForwardIterator, _ForwardIterator> __result(__first, __first);
Howard Hinnant3e519522010-05-11 19:42:162701 if (__first != __last)
2702 {
2703 if (++__first != __last)
2704 {
2705 if (__comp(*__first, *__result.first))
Howard Hinnant3e519522010-05-11 19:42:162706 __result.first = __first;
Howard Hinnant3e519522010-05-11 19:42:162707 else
2708 __result.second = __first;
2709 while (++__first != __last)
2710 {
2711 _ForwardIterator __i = __first;
2712 if (++__first == __last)
2713 {
2714 if (__comp(*__i, *__result.first))
2715 __result.first = __i;
2716 else if (!__comp(*__i, *__result.second))
2717 __result.second = __i;
2718 break;
2719 }
2720 else
2721 {
2722 if (__comp(*__first, *__i))
2723 {
2724 if (__comp(*__first, *__result.first))
2725 __result.first = __first;
2726 if (!__comp(*__i, *__result.second))
2727 __result.second = __i;
2728 }
2729 else
2730 {
2731 if (__comp(*__i, *__result.first))
2732 __result.first = __i;
2733 if (!__comp(*__first, *__result.second))
2734 __result.second = __first;
2735 }
2736 }
2737 }
2738 }
2739 }
2740 return __result;
2741}
2742
2743template <class _ForwardIterator>
Nico Weber1362d7e2019-04-03 18:13:082744_LIBCPP_NODISCARD_EXT inline
2745_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Arthur O'Dwyerd586f922020-11-27 16:02:062746pair<_ForwardIterator, _ForwardIterator>
Howard Hinnant3e519522010-05-11 19:42:162747minmax_element(_ForwardIterator __first, _ForwardIterator __last)
2748{
Marshall Clow9d67c6d2014-02-19 16:51:352749 return _VSTD::minmax_element(__first, __last,
2750 __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:162751}
2752
Howard Hinnant4eb27b72010-08-21 20:10:012753// minmax
2754
2755template<class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:082756_LIBCPP_NODISCARD_EXT inline
2757_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012758pair<const _Tp&, const _Tp&>
2759minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
2760{
2761 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) :
2762 pair<const _Tp&, const _Tp&>(__a, __b);
2763}
2764
2765template<class _Tp>
Nico Weber1362d7e2019-04-03 18:13:082766_LIBCPP_NODISCARD_EXT inline
2767_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012768pair<const _Tp&, const _Tp&>
2769minmax(const _Tp& __a, const _Tp& __b)
2770{
Howard Hinnantce48a112011-06-30 21:18:192771 return _VSTD::minmax(__a, __b, __less<_Tp>());
Howard Hinnant4eb27b72010-08-21 20:10:012772}
2773
Eric Fiselierddda4562017-04-18 23:26:472774#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022775
Howard Hinnant4eb27b72010-08-21 20:10:012776template<class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:082777_LIBCPP_NODISCARD_EXT inline
2778_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant4eb27b72010-08-21 20:10:012779pair<_Tp, _Tp>
2780minmax(initializer_list<_Tp> __t, _Compare __comp)
2781{
Marshall Clow9d67c6d2014-02-19 16:51:352782 typedef typename initializer_list<_Tp>::const_iterator _Iter;
2783 _Iter __first = __t.begin();
2784 _Iter __last = __t.end();
Arthur O'Dwyerd586f922020-11-27 16:02:062785 pair<_Tp, _Tp> __result(*__first, *__first);
Marshall Clow9d67c6d2014-02-19 16:51:352786
2787 ++__first;
2788 if (__t.size() % 2 == 0)
2789 {
2790 if (__comp(*__first, __result.first))
2791 __result.first = *__first;
2792 else
2793 __result.second = *__first;
2794 ++__first;
2795 }
Aditya Kumar331fb802016-08-25 11:52:382796
Marshall Clow9d67c6d2014-02-19 16:51:352797 while (__first != __last)
2798 {
2799 _Tp __prev = *__first++;
Marshall Clow002144f2015-02-11 15:41:342800 if (__comp(*__first, __prev)) {
2801 if ( __comp(*__first, __result.first)) __result.first = *__first;
2802 if (!__comp(__prev, __result.second)) __result.second = __prev;
Marshall Clow9d67c6d2014-02-19 16:51:352803 }
2804 else {
Marshall Clow002144f2015-02-11 15:41:342805 if ( __comp(__prev, __result.first)) __result.first = __prev;
2806 if (!__comp(*__first, __result.second)) __result.second = *__first;
Marshall Clow9d67c6d2014-02-19 16:51:352807 }
Aditya Kumar331fb802016-08-25 11:52:382808
Marshall Clow9d67c6d2014-02-19 16:51:352809 __first++;
2810 }
2811 return __result;
2812}
2813
2814template<class _Tp>
Nico Weber1362d7e2019-04-03 18:13:082815_LIBCPP_NODISCARD_EXT inline
2816_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow9d67c6d2014-02-19 16:51:352817pair<_Tp, _Tp>
2818minmax(initializer_list<_Tp> __t)
2819{
2820 return _VSTD::minmax(__t, __less<_Tp>());
Howard Hinnant4eb27b72010-08-21 20:10:012821}
2822
Eric Fiselierddda4562017-04-18 23:26:472823#endif // _LIBCPP_CXX03_LANG
Howard Hinnant54976f22011-08-12 21:56:022824
Howard Hinnant3e519522010-05-11 19:42:162825// random_shuffle
2826
Howard Hinnantf9d540b2010-05-26 17:49:342827// __independent_bits_engine
2828
Howard Hinnantc003db12011-11-29 18:15:502829template <unsigned long long _Xp, size_t _Rp>
Howard Hinnantf9d540b2010-05-26 17:49:342830struct __log2_imp
Howard Hinnant3e519522010-05-11 19:42:162831{
Howard Hinnantc003db12011-11-29 18:15:502832 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
2833 : __log2_imp<_Xp, _Rp - 1>::value;
Howard Hinnant3e519522010-05-11 19:42:162834};
2835
Howard Hinnantc003db12011-11-29 18:15:502836template <unsigned long long _Xp>
2837struct __log2_imp<_Xp, 0>
Howard Hinnant3e519522010-05-11 19:42:162838{
Howard Hinnantf9d540b2010-05-26 17:49:342839 static const size_t value = 0;
Howard Hinnant3e519522010-05-11 19:42:162840};
2841
Howard Hinnantc003db12011-11-29 18:15:502842template <size_t _Rp>
2843struct __log2_imp<0, _Rp>
Howard Hinnant3e519522010-05-11 19:42:162844{
Howard Hinnantc003db12011-11-29 18:15:502845 static const size_t value = _Rp + 1;
Howard Hinnant3e519522010-05-11 19:42:162846};
2847
Eric Fiselier89918ca2017-05-31 21:20:182848template <class _UIntType, _UIntType _Xp>
Howard Hinnantf9d540b2010-05-26 17:49:342849struct __log2
Howard Hinnant3e519522010-05-11 19:42:162850{
Howard Hinnantc003db12011-11-29 18:15:502851 static const size_t value = __log2_imp<_Xp,
Eric Fiselier89918ca2017-05-31 21:20:182852 sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
Howard Hinnant3e519522010-05-11 19:42:162853};
2854
Howard Hinnantf9d540b2010-05-26 17:49:342855template<class _Engine, class _UIntType>
2856class __independent_bits_engine
Howard Hinnant3e519522010-05-11 19:42:162857{
Howard Hinnantf9d540b2010-05-26 17:49:342858public:
2859 // types
2860 typedef _UIntType result_type;
2861
2862private:
2863 typedef typename _Engine::result_type _Engine_result_type;
2864 typedef typename conditional
2865 <
2866 sizeof(_Engine_result_type) <= sizeof(result_type),
2867 result_type,
2868 _Engine_result_type
2869 >::type _Working_result_type;
2870
2871 _Engine& __e_;
2872 size_t __w_;
2873 size_t __w0_;
2874 size_t __n_;
2875 size_t __n0_;
2876 _Working_result_type __y0_;
2877 _Working_result_type __y1_;
2878 _Engine_result_type __mask0_;
2879 _Engine_result_type __mask1_;
2880
Eric Fiselierddda4562017-04-18 23:26:472881#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc003db12011-11-29 18:15:502882 static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min
Howard Hinnante386b7b2012-04-02 21:00:452883 + _Working_result_type(1);
2884#else
2885 static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min()
2886 + _Working_result_type(1);
2887#endif
2888 static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
2889 static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
2890 static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
Howard Hinnantf9d540b2010-05-26 17:49:342891
2892public:
2893 // constructors and seeding functions
2894 __independent_bits_engine(_Engine& __e, size_t __w);
2895
2896 // generating functions
Howard Hinnantc003db12011-11-29 18:15:502897 result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
Howard Hinnantf9d540b2010-05-26 17:49:342898
2899private:
Marshall Clow08bba642017-09-20 19:38:432900 result_type __eval(false_type);
2901 result_type __eval(true_type);
Howard Hinnantf9d540b2010-05-26 17:49:342902};
2903
2904template<class _Engine, class _UIntType>
2905__independent_bits_engine<_Engine, _UIntType>
2906 ::__independent_bits_engine(_Engine& __e, size_t __w)
2907 : __e_(__e),
2908 __w_(__w)
2909{
2910 __n_ = __w_ / __m + (__w_ % __m != 0);
2911 __w0_ = __w_ / __n_;
Howard Hinnantc003db12011-11-29 18:15:502912 if (_Rp == 0)
2913 __y0_ = _Rp;
Howard Hinnantf9d540b2010-05-26 17:49:342914 else if (__w0_ < _WDt)
Howard Hinnantc003db12011-11-29 18:15:502915 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantf9d540b2010-05-26 17:49:342916 else
2917 __y0_ = 0;
Howard Hinnantc003db12011-11-29 18:15:502918 if (_Rp - __y0_ > __y0_ / __n_)
Howard Hinnantf9d540b2010-05-26 17:49:342919 {
2920 ++__n_;
2921 __w0_ = __w_ / __n_;
2922 if (__w0_ < _WDt)
Howard Hinnantc003db12011-11-29 18:15:502923 __y0_ = (_Rp >> __w0_) << __w0_;
Howard Hinnantf9d540b2010-05-26 17:49:342924 else
2925 __y0_ = 0;
2926 }
2927 __n0_ = __n_ - __w_ % __n_;
2928 if (__w0_ < _WDt - 1)
Howard Hinnantc003db12011-11-29 18:15:502929 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
Howard Hinnantf9d540b2010-05-26 17:49:342930 else
2931 __y1_ = 0;
2932 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
2933 _Engine_result_type(0);
2934 __mask1_ = __w0_ < _EDt - 1 ?
2935 _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) :
2936 _Engine_result_type(~0);
Howard Hinnant3e519522010-05-11 19:42:162937}
2938
Howard Hinnantf9d540b2010-05-26 17:49:342939template<class _Engine, class _UIntType>
2940inline
2941_UIntType
Marshall Clow08bba642017-09-20 19:38:432942__independent_bits_engine<_Engine, _UIntType>::__eval(false_type)
Howard Hinnant3e519522010-05-11 19:42:162943{
Howard Hinnantf9d540b2010-05-26 17:49:342944 return static_cast<result_type>(__e_() & __mask0_);
Howard Hinnant3e519522010-05-11 19:42:162945}
2946
Howard Hinnantf9d540b2010-05-26 17:49:342947template<class _Engine, class _UIntType>
2948_UIntType
Marshall Clow08bba642017-09-20 19:38:432949__independent_bits_engine<_Engine, _UIntType>::__eval(true_type)
Howard Hinnant3e519522010-05-11 19:42:162950{
Marshall Clow5beb2c32017-09-20 17:34:112951 const size_t _WRt = numeric_limits<result_type>::digits;
Howard Hinnantc003db12011-11-29 18:15:502952 result_type _Sp = 0;
Howard Hinnantf9d540b2010-05-26 17:49:342953 for (size_t __k = 0; __k < __n0_; ++__k)
2954 {
2955 _Engine_result_type __u;
2956 do
2957 {
2958 __u = __e_() - _Engine::min();
2959 } while (__u >= __y0_);
Marshall Clow5beb2c32017-09-20 17:34:112960 if (__w0_ < _WRt)
Howard Hinnantc003db12011-11-29 18:15:502961 _Sp <<= __w0_;
Howard Hinnantf9d540b2010-05-26 17:49:342962 else
Howard Hinnantc003db12011-11-29 18:15:502963 _Sp = 0;
2964 _Sp += __u & __mask0_;
Howard Hinnantf9d540b2010-05-26 17:49:342965 }
2966 for (size_t __k = __n0_; __k < __n_; ++__k)
2967 {
2968 _Engine_result_type __u;
2969 do
2970 {
2971 __u = __e_() - _Engine::min();
2972 } while (__u >= __y1_);
Marshall Clow5beb2c32017-09-20 17:34:112973 if (__w0_ < _WRt - 1)
Howard Hinnantc003db12011-11-29 18:15:502974 _Sp <<= __w0_ + 1;
Howard Hinnantf9d540b2010-05-26 17:49:342975 else
Howard Hinnantc003db12011-11-29 18:15:502976 _Sp = 0;
2977 _Sp += __u & __mask1_;
Howard Hinnantf9d540b2010-05-26 17:49:342978 }
Howard Hinnantc003db12011-11-29 18:15:502979 return _Sp;
Howard Hinnantf9d540b2010-05-26 17:49:342980}
2981
2982// uniform_int_distribution
2983
2984template<class _IntType = int>
2985class uniform_int_distribution
2986{
2987public:
2988 // types
2989 typedef _IntType result_type;
2990
2991 class param_type
2992 {
2993 result_type __a_;
2994 result_type __b_;
2995 public:
2996 typedef uniform_int_distribution distribution_type;
2997
2998 explicit param_type(result_type __a = 0,
2999 result_type __b = numeric_limits<result_type>::max())
3000 : __a_(__a), __b_(__b) {}
3001
3002 result_type a() const {return __a_;}
3003 result_type b() const {return __b_;}
3004
3005 friend bool operator==(const param_type& __x, const param_type& __y)
3006 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
3007 friend bool operator!=(const param_type& __x, const param_type& __y)
3008 {return !(__x == __y);}
3009 };
3010
3011private:
3012 param_type __p_;
3013
3014public:
3015 // constructors and reset functions
Marek Kurdeja11f8b12021-01-19 07:21:093016#ifndef _LIBCPP_CXX03_LANG
3017 uniform_int_distribution() : uniform_int_distribution(0) {}
3018 explicit uniform_int_distribution(
3019 result_type __a, result_type __b = numeric_limits<result_type>::max())
Howard Hinnantf9d540b2010-05-26 17:49:343020 : __p_(param_type(__a, __b)) {}
Marek Kurdeja11f8b12021-01-19 07:21:093021#else
3022 explicit uniform_int_distribution(
3023 result_type __a = 0,
3024 result_type __b = numeric_limits<result_type>::max())
3025 : __p_(param_type(__a, __b)) {}
3026#endif
Howard Hinnantf9d540b2010-05-26 17:49:343027 explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {}
3028 void reset() {}
3029
3030 // generating functions
3031 template<class _URNG> result_type operator()(_URNG& __g)
3032 {return (*this)(__g, __p_);}
3033 template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
3034
3035 // property functions
3036 result_type a() const {return __p_.a();}
3037 result_type b() const {return __p_.b();}
3038
3039 param_type param() const {return __p_;}
3040 void param(const param_type& __p) {__p_ = __p;}
3041
3042 result_type min() const {return a();}
3043 result_type max() const {return b();}
3044
3045 friend bool operator==(const uniform_int_distribution& __x,
3046 const uniform_int_distribution& __y)
3047 {return __x.__p_ == __y.__p_;}
3048 friend bool operator!=(const uniform_int_distribution& __x,
3049 const uniform_int_distribution& __y)
3050 {return !(__x == __y);}
3051};
3052
3053template<class _IntType>
3054template<class _URNG>
3055typename uniform_int_distribution<_IntType>::result_type
3056uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
Marshall Clow2dd53352018-10-08 20:20:343057_LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantf9d540b2010-05-26 17:49:343058{
3059 typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t),
3060 uint32_t, uint64_t>::type _UIntType;
Marshall Clow2dd53352018-10-08 20:20:343061 const _UIntType _Rp = _UIntType(__p.b()) - _UIntType(__p.a()) + _UIntType(1);
Howard Hinnantc003db12011-11-29 18:15:503062 if (_Rp == 1)
Howard Hinnantf9d540b2010-05-26 17:49:343063 return __p.a();
3064 const size_t _Dt = numeric_limits<_UIntType>::digits;
3065 typedef __independent_bits_engine<_URNG, _UIntType> _Eng;
Howard Hinnantc003db12011-11-29 18:15:503066 if (_Rp == 0)
Howard Hinnantf9d540b2010-05-26 17:49:343067 return static_cast<result_type>(_Eng(__g, _Dt)());
Marshall Clowf3b851f2019-07-12 01:01:553068 size_t __w = _Dt - __libcpp_clz(_Rp) - 1;
Arthur O'Dwyerd586f922020-11-27 16:02:063069 if ((_Rp & (numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
Howard Hinnantf9d540b2010-05-26 17:49:343070 ++__w;
3071 _Eng __e(__g, __w);
3072 _UIntType __u;
Howard Hinnant3e519522010-05-11 19:42:163073 do
Howard Hinnantf9d540b2010-05-26 17:49:343074 {
3075 __u = __e();
Howard Hinnantc003db12011-11-29 18:15:503076 } while (__u >= _Rp);
Howard Hinnantf9d540b2010-05-26 17:49:343077 return static_cast<result_type>(__u + __p.a());
Howard Hinnant3e519522010-05-11 19:42:163078}
3079
Eric Fiselier66f1ec42017-04-03 23:23:443080#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \
3081 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnantf0544c22013-08-12 18:38:343082class _LIBCPP_TYPE_VIS __rs_default;
Howard Hinnant3e519522010-05-11 19:42:163083
Howard Hinnantf0544c22013-08-12 18:38:343084_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnantf9d540b2010-05-26 17:49:343085
Howard Hinnantf0544c22013-08-12 18:38:343086class _LIBCPP_TYPE_VIS __rs_default
Howard Hinnant3e519522010-05-11 19:42:163087{
Howard Hinnantf9d540b2010-05-26 17:49:343088 static unsigned __c_;
3089
3090 __rs_default();
3091public:
Marshall Clowb6e5f852013-02-07 22:12:023092 typedef uint_fast32_t result_type;
Howard Hinnantf9d540b2010-05-26 17:49:343093
3094 static const result_type _Min = 0;
3095 static const result_type _Max = 0xFFFFFFFF;
3096
3097 __rs_default(const __rs_default&);
3098 ~__rs_default();
3099
3100 result_type operator()();
3101
Howard Hinnant788c9972012-04-02 00:40:413102 static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
3103 static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
Howard Hinnantf9d540b2010-05-26 17:49:343104
Howard Hinnantf0544c22013-08-12 18:38:343105 friend _LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnant3e519522010-05-11 19:42:163106};
3107
Howard Hinnantf0544c22013-08-12 18:38:343108_LIBCPP_FUNC_VIS __rs_default __rs_get();
Howard Hinnant3e519522010-05-11 19:42:163109
3110template <class _RandomAccessIterator>
Louis Dionneea5cd3b2018-09-23 18:35:003111_LIBCPP_DEPRECATED_IN_CXX14 void
Howard Hinnant3e519522010-05-11 19:42:163112random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
3113{
3114 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc003db12011-11-29 18:15:503115 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3116 typedef typename _Dp::param_type _Pp;
Howard Hinnant3e519522010-05-11 19:42:163117 difference_type __d = __last - __first;
3118 if (__d > 1)
3119 {
Howard Hinnantc003db12011-11-29 18:15:503120 _Dp __uid;
Howard Hinnantf9d540b2010-05-26 17:49:343121 __rs_default __g = __rs_get();
Marshall Clow5a8525e2019-01-24 19:20:193122 for (--__last, (void) --__d; __first < __last; ++__first, (void) --__d)
Howard Hinnant007b26b2010-10-22 15:26:393123 {
Howard Hinnantc003db12011-11-29 18:15:503124 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant007b26b2010-10-22 15:26:393125 if (__i != difference_type(0))
3126 swap(*__first, *(__first + __i));
3127 }
Howard Hinnant3e519522010-05-11 19:42:163128 }
3129}
3130
3131template <class _RandomAccessIterator, class _RandomNumberGenerator>
Louis Dionneea5cd3b2018-09-23 18:35:003132_LIBCPP_DEPRECATED_IN_CXX14 void
Howard Hinnant3e519522010-05-11 19:42:163133random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Eric Fiselierddda4562017-04-18 23:26:473134#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:163135 _RandomNumberGenerator&& __rand)
3136#else
3137 _RandomNumberGenerator& __rand)
3138#endif
3139{
3140 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
3141 difference_type __d = __last - __first;
3142 if (__d > 1)
3143 {
Marshall Clow5a8525e2019-01-24 19:20:193144 for (--__last; __first < __last; ++__first, (void) --__d)
Howard Hinnant007b26b2010-10-22 15:26:393145 {
3146 difference_type __i = __rand(__d);
Marshall Clowe9cc5452018-09-11 18:33:453147 if (__i != difference_type(0))
Marshall Clow2dd53352018-10-08 20:20:343148 swap(*__first, *(__first + __i));
Howard Hinnant007b26b2010-10-22 15:26:393149 }
Howard Hinnant3e519522010-05-11 19:42:163150 }
3151}
Marshall Clow0f37a412017-03-23 13:43:373152#endif
Howard Hinnant3e519522010-05-11 19:42:163153
Eric Fiseliere7154702016-08-28 22:14:373154template <class _PopulationIterator, class _SampleIterator, class _Distance,
3155 class _UniformRandomNumberGenerator>
3156_LIBCPP_INLINE_VISIBILITY
3157_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardson42bfedd2017-11-14 11:14:253158 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiseliere7154702016-08-28 22:14:373159 _Distance __n,
3160 _UniformRandomNumberGenerator & __g,
3161 input_iterator_tag) {
3162
3163 _Distance __k = 0;
Marshall Clow7fa68652019-08-20 21:31:513164 for (; __first != __last && __k < __n; ++__first, (void) ++__k)
Alexander Richardson42bfedd2017-11-14 11:14:253165 __output_iter[__k] = *__first;
Eric Fiseliere7154702016-08-28 22:14:373166 _Distance __sz = __k;
Marshall Clow7fa68652019-08-20 21:31:513167 for (; __first != __last; ++__first, (void) ++__k) {
Eric Fiseliere7154702016-08-28 22:14:373168 _Distance __r = _VSTD::uniform_int_distribution<_Distance>(0, __k)(__g);
3169 if (__r < __sz)
Alexander Richardson42bfedd2017-11-14 11:14:253170 __output_iter[__r] = *__first;
Eric Fiseliere7154702016-08-28 22:14:373171 }
Alexander Richardson42bfedd2017-11-14 11:14:253172 return __output_iter + _VSTD::min(__n, __k);
Eric Fiseliere7154702016-08-28 22:14:373173}
3174
3175template <class _PopulationIterator, class _SampleIterator, class _Distance,
3176 class _UniformRandomNumberGenerator>
3177_LIBCPP_INLINE_VISIBILITY
3178_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardson42bfedd2017-11-14 11:14:253179 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiseliere7154702016-08-28 22:14:373180 _Distance __n,
3181 _UniformRandomNumberGenerator& __g,
3182 forward_iterator_tag) {
3183 _Distance __unsampled_sz = _VSTD::distance(__first, __last);
3184 for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
3185 _Distance __r =
3186 _VSTD::uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
3187 if (__r < __n) {
Alexander Richardson42bfedd2017-11-14 11:14:253188 *__output_iter++ = *__first;
Eric Fiseliere7154702016-08-28 22:14:373189 --__n;
3190 }
3191 }
Alexander Richardson42bfedd2017-11-14 11:14:253192 return __output_iter;
Eric Fiseliere7154702016-08-28 22:14:373193}
3194
3195template <class _PopulationIterator, class _SampleIterator, class _Distance,
3196 class _UniformRandomNumberGenerator>
3197_LIBCPP_INLINE_VISIBILITY
3198_SampleIterator __sample(_PopulationIterator __first,
Alexander Richardson42bfedd2017-11-14 11:14:253199 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiseliere7154702016-08-28 22:14:373200 _Distance __n, _UniformRandomNumberGenerator& __g) {
3201 typedef typename iterator_traits<_PopulationIterator>::iterator_category
3202 _PopCategory;
3203 typedef typename iterator_traits<_PopulationIterator>::difference_type
3204 _Difference;
Eric Fiselierf82dba02019-11-18 06:46:583205 static_assert(__is_cpp17_forward_iterator<_PopulationIterator>::value ||
3206 __is_cpp17_random_access_iterator<_SampleIterator>::value,
Eric Fiseliere7154702016-08-28 22:14:373207 "SampleIterator must meet the requirements of RandomAccessIterator");
3208 typedef typename common_type<_Distance, _Difference>::type _CommonType;
3209 _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
3210 return _VSTD::__sample(
Alexander Richardson42bfedd2017-11-14 11:14:253211 __first, __last, __output_iter, _CommonType(__n),
Eric Fiseliere7154702016-08-28 22:14:373212 __g, _PopCategory());
3213}
3214
3215#if _LIBCPP_STD_VER > 14
3216template <class _PopulationIterator, class _SampleIterator, class _Distance,
3217 class _UniformRandomNumberGenerator>
3218inline _LIBCPP_INLINE_VISIBILITY
3219_SampleIterator sample(_PopulationIterator __first,
Alexander Richardson42bfedd2017-11-14 11:14:253220 _PopulationIterator __last, _SampleIterator __output_iter,
Eric Fiseliere7154702016-08-28 22:14:373221 _Distance __n, _UniformRandomNumberGenerator&& __g) {
Alexander Richardson42bfedd2017-11-14 11:14:253222 return _VSTD::__sample(__first, __last, __output_iter, __n, __g);
Eric Fiseliere7154702016-08-28 22:14:373223}
3224#endif // _LIBCPP_STD_VER > 14
3225
Howard Hinnantf9d540b2010-05-26 17:49:343226template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
3227 void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
Howard Hinnantfb340102010-11-18 01:47:023228 _UniformRandomNumberGenerator&& __g)
Howard Hinnantf9d540b2010-05-26 17:49:343229{
3230 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnantc003db12011-11-29 18:15:503231 typedef uniform_int_distribution<ptrdiff_t> _Dp;
3232 typedef typename _Dp::param_type _Pp;
Howard Hinnantf9d540b2010-05-26 17:49:343233 difference_type __d = __last - __first;
3234 if (__d > 1)
3235 {
Howard Hinnantc003db12011-11-29 18:15:503236 _Dp __uid;
Marshall Clow7fa68652019-08-20 21:31:513237 for (--__last, (void) --__d; __first < __last; ++__first, (void) --__d)
Howard Hinnant007b26b2010-10-22 15:26:393238 {
Howard Hinnantc003db12011-11-29 18:15:503239 difference_type __i = __uid(__g, _Pp(0, __d));
Howard Hinnant007b26b2010-10-22 15:26:393240 if (__i != difference_type(0))
3241 swap(*__first, *(__first + __i));
3242 }
Howard Hinnantf9d540b2010-05-26 17:49:343243 }
3244}
3245
Arthur O'Dwyer3fbd3ea2020-12-26 06:39:033246#if _LIBCPP_STD_VER > 17
3247
3248// shift_left, shift_right
3249
3250template <class _ForwardIterator>
3251inline _LIBCPP_INLINE_VISIBILITY constexpr
3252_ForwardIterator
3253shift_left(_ForwardIterator __first, _ForwardIterator __last,
3254 typename iterator_traits<_ForwardIterator>::difference_type __n)
3255{
3256 if (__n == 0) {
3257 return __last;
3258 }
3259
3260 _ForwardIterator __m = __first;
3261 if constexpr (__is_cpp17_random_access_iterator<_ForwardIterator>::value) {
3262 if (__n >= __last - __first) {
3263 return __first;
3264 }
3265 __m += __n;
3266 } else {
3267 for (; __n > 0; --__n) {
3268 if (__m == __last) {
3269 return __first;
3270 }
3271 ++__m;
3272 }
3273 }
3274 return _VSTD::move(__m, __last, __first);
3275}
3276
3277template <class _ForwardIterator>
3278inline _LIBCPP_INLINE_VISIBILITY constexpr
3279_ForwardIterator
3280shift_right(_ForwardIterator __first, _ForwardIterator __last,
3281 typename iterator_traits<_ForwardIterator>::difference_type __n)
3282{
3283 if (__n == 0) {
3284 return __first;
3285 }
3286
3287 if constexpr (__is_cpp17_random_access_iterator<_ForwardIterator>::value) {
3288 decltype(__n) __d = __last - __first;
3289 if (__n >= __d) {
3290 return __last;
3291 }
3292 _ForwardIterator __m = __first + (__d - __n);
3293 return _VSTD::move_backward(__first, __m, __last);
3294 } else if constexpr (__is_cpp17_bidirectional_iterator<_ForwardIterator>::value) {
3295 _ForwardIterator __m = __last;
3296 for (; __n > 0; --__n) {
3297 if (__m == __first) {
3298 return __last;
3299 }
3300 --__m;
3301 }
3302 return _VSTD::move_backward(__first, __m, __last);
3303 } else {
3304 _ForwardIterator __ret = __first;
3305 for (; __n > 0; --__n) {
3306 if (__ret == __last) {
3307 return __last;
3308 }
3309 ++__ret;
3310 }
3311
3312 // We have an __n-element scratch space from __first to __ret.
3313 // Slide an __n-element window [__trail, __lead) from left to right.
3314 // We're essentially doing swap_ranges(__first, __ret, __trail, __lead)
3315 // over and over; but once __lead reaches __last we needn't bother
3316 // to save the values of elements [__trail, __last).
3317
3318 auto __trail = __first;
3319 auto __lead = __ret;
3320 while (__trail != __ret) {
3321 if (__lead == __last) {
3322 _VSTD::move(__first, __trail, __ret);
3323 return __ret;
3324 }
3325 ++__trail;
3326 ++__lead;
3327 }
3328
3329 _ForwardIterator __mid = __first;
3330 while (true) {
3331 if (__lead == __last) {
3332 __trail = _VSTD::move(__mid, __ret, __trail);
3333 _VSTD::move(__first, __mid, __trail);
3334 return __ret;
3335 }
3336 swap(*__mid, *__trail);
3337 ++__mid;
3338 ++__trail;
3339 ++__lead;
3340 if (__mid == __ret) {
3341 __mid = __first;
3342 }
3343 }
3344 }
3345}
3346
3347#endif // _LIBCPP_STD_VER > 17
3348
3349// is_partitioned
3350
Howard Hinnant3e519522010-05-11 19:42:163351template <class _InputIterator, class _Predicate>
Nico Weber1362d7e2019-04-03 18:13:083352_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnant3e519522010-05-11 19:42:163353is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred)
3354{
3355 for (; __first != __last; ++__first)
3356 if (!__pred(*__first))
3357 break;
Marshall Clowb9595b72015-02-02 18:16:353358 if ( __first == __last )
3359 return true;
3360 ++__first;
Howard Hinnant3e519522010-05-11 19:42:163361 for (; __first != __last; ++__first)
3362 if (__pred(*__first))
3363 return false;
3364 return true;
3365}
3366
3367// partition
3368
3369template <class _Predicate, class _ForwardIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:083370_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:163371__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag)
3372{
3373 while (true)
3374 {
3375 if (__first == __last)
3376 return __first;
3377 if (!__pred(*__first))
3378 break;
3379 ++__first;
3380 }
3381 for (_ForwardIterator __p = __first; ++__p != __last;)
3382 {
3383 if (__pred(*__p))
3384 {
3385 swap(*__first, *__p);
3386 ++__first;
3387 }
3388 }
3389 return __first;
3390}
3391
3392template <class _Predicate, class _BidirectionalIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:083393_LIBCPP_CONSTEXPR_AFTER_CXX17 _BidirectionalIterator
Howard Hinnant3e519522010-05-11 19:42:163394__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3395 bidirectional_iterator_tag)
3396{
3397 while (true)
3398 {
3399 while (true)
3400 {
3401 if (__first == __last)
3402 return __first;
3403 if (!__pred(*__first))
3404 break;
3405 ++__first;
3406 }
3407 do
3408 {
3409 if (__first == --__last)
3410 return __first;
3411 } while (!__pred(*__last));
3412 swap(*__first, *__last);
3413 ++__first;
3414 }
3415}
3416
3417template <class _ForwardIterator, class _Predicate>
Arthur O'Dwyerf851db32020-12-17 05:01:083418inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:163419_ForwardIterator
3420partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3421{
Howard Hinnantce48a112011-06-30 21:18:193422 return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnant3e519522010-05-11 19:42:163423 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3424}
3425
3426// partition_copy
3427
3428template <class _InputIterator, class _OutputIterator1,
3429 class _OutputIterator2, class _Predicate>
Marshall Clow1b9a4ff2018-01-22 20:44:333430_LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_OutputIterator1, _OutputIterator2>
Howard Hinnant3e519522010-05-11 19:42:163431partition_copy(_InputIterator __first, _InputIterator __last,
3432 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
3433 _Predicate __pred)
3434{
3435 for (; __first != __last; ++__first)
3436 {
3437 if (__pred(*__first))
3438 {
3439 *__out_true = *__first;
3440 ++__out_true;
3441 }
3442 else
3443 {
3444 *__out_false = *__first;
3445 ++__out_false;
3446 }
3447 }
3448 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
3449}
3450
3451// partition_point
3452
3453template<class _ForwardIterator, class _Predicate>
Marshall Clow674f9122018-01-15 17:53:343454_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:163455partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3456{
3457 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantce48a112011-06-30 21:18:193458 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:163459 while (__len != 0)
3460 {
Louis Dionne04695a72018-12-17 16:04:393461 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnant3e519522010-05-11 19:42:163462 _ForwardIterator __m = __first;
Howard Hinnantce48a112011-06-30 21:18:193463 _VSTD::advance(__m, __l2);
Howard Hinnant3e519522010-05-11 19:42:163464 if (__pred(*__m))
3465 {
3466 __first = ++__m;
3467 __len -= __l2 + 1;
3468 }
3469 else
3470 __len = __l2;
3471 }
3472 return __first;
3473}
3474
3475// stable_partition
3476
3477template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair>
3478_ForwardIterator
3479__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3480 _Distance __len, _Pair __p, forward_iterator_tag __fit)
3481{
3482 // *__first is known to be false
3483 // __len >= 1
3484 if (__len == 1)
3485 return __first;
3486 if (__len == 2)
3487 {
3488 _ForwardIterator __m = __first;
3489 if (__pred(*++__m))
3490 {
3491 swap(*__first, *__m);
3492 return __m;
3493 }
3494 return __first;
3495 }
3496 if (__len <= __p.second)
3497 { // The buffer is big enough to use
3498 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3499 __destruct_n __d(0);
3500 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3501 // Move the falses into the temporary buffer, and the trues to the front of the line
3502 // Update __first to always point to the end of the trues
3503 value_type* __t = __p.first;
Arthur O'Dwyerbe4c6572020-12-12 01:30:283504 ::new ((void*)__t) value_type(_VSTD::move(*__first));
Bruce Mitchener527a7fd2020-11-24 17:53:533505 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:163506 ++__t;
3507 _ForwardIterator __i = __first;
3508 while (++__i != __last)
3509 {
3510 if (__pred(*__i))
3511 {
Howard Hinnantce48a112011-06-30 21:18:193512 *__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:163513 ++__first;
3514 }
3515 else
3516 {
Arthur O'Dwyerbe4c6572020-12-12 01:30:283517 ::new ((void*)__t) value_type(_VSTD::move(*__i));
Bruce Mitchener527a7fd2020-11-24 17:53:533518 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:163519 ++__t;
3520 }
3521 }
3522 // All trues now at start of range, all falses in buffer
3523 // Move falses back into range, but don't mess up __first which points to first false
3524 __i = __first;
Marshall Clow7fa68652019-08-20 21:31:513525 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, (void) ++__i)
Howard Hinnantce48a112011-06-30 21:18:193526 *__i = _VSTD::move(*__t2);
Howard Hinnant3e519522010-05-11 19:42:163527 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3528 return __first;
3529 }
3530 // Else not enough buffer, do in place
3531 // __len >= 3
3532 _ForwardIterator __m = __first;
3533 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnantce48a112011-06-30 21:18:193534 _VSTD::advance(__m, __len2);
Howard Hinnant3e519522010-05-11 19:42:163535 // recurse on [__first, __m), *__first know to be false
3536 // F?????????????????
3537 // f m l
3538 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
Arthur O'Dwyer19688042020-11-22 18:21:113539 _ForwardIterator __first_false = _VSTD::__stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit);
Howard Hinnant3e519522010-05-11 19:42:163540 // TTTFFFFF??????????
3541 // f ff m l
3542 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3543 _ForwardIterator __m1 = __m;
3544 _ForwardIterator __second_false = __last;
3545 _Distance __len_half = __len - __len2;
3546 while (__pred(*__m1))
3547 {
3548 if (++__m1 == __last)
3549 goto __second_half_done;
3550 --__len_half;
3551 }
3552 // TTTFFFFFTTTF??????
3553 // f ff m m1 l
Arthur O'Dwyer19688042020-11-22 18:21:113554 __second_false = _VSTD::__stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit);
Howard Hinnant3e519522010-05-11 19:42:163555__second_half_done:
3556 // TTTFFFFFTTTTTFFFFF
3557 // f ff m sf l
Howard Hinnantce48a112011-06-30 21:18:193558 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnant3e519522010-05-11 19:42:163559 // TTTTTTTTFFFFFFFFFF
3560 // |
3561}
3562
3563struct __return_temporary_buffer
3564{
3565 template <class _Tp>
Howard Hinnantce48a112011-06-30 21:18:193566 _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);}
Howard Hinnant3e519522010-05-11 19:42:163567};
3568
3569template <class _Predicate, class _ForwardIterator>
3570_ForwardIterator
3571__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred,
3572 forward_iterator_tag)
3573{
3574 const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment
3575 // Either prove all true and return __first or point to first false
3576 while (true)
3577 {
3578 if (__first == __last)
3579 return __first;
3580 if (!__pred(*__first))
3581 break;
3582 ++__first;
3583 }
3584 // We now have a reduced range [__first, __last)
3585 // *__first is known to be false
3586 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
3587 typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
Howard Hinnantce48a112011-06-30 21:18:193588 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:163589 pair<value_type*, ptrdiff_t> __p(0, 0);
3590 unique_ptr<value_type, __return_temporary_buffer> __h;
3591 if (__len >= __alloc_limit)
3592 {
Howard Hinnantce48a112011-06-30 21:18:193593 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnant3e519522010-05-11 19:42:163594 __h.reset(__p.first);
3595 }
Arthur O'Dwyer19688042020-11-22 18:21:113596 return _VSTD::__stable_partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnant3e519522010-05-11 19:42:163597 (__first, __last, __pred, __len, __p, forward_iterator_tag());
3598}
3599
3600template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair>
3601_BidirectionalIterator
3602__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3603 _Distance __len, _Pair __p, bidirectional_iterator_tag __bit)
3604{
3605 // *__first is known to be false
3606 // *__last is known to be true
3607 // __len >= 2
3608 if (__len == 2)
3609 {
3610 swap(*__first, *__last);
3611 return __last;
3612 }
3613 if (__len == 3)
3614 {
3615 _BidirectionalIterator __m = __first;
3616 if (__pred(*++__m))
3617 {
3618 swap(*__first, *__m);
3619 swap(*__m, *__last);
3620 return __last;
3621 }
3622 swap(*__m, *__last);
3623 swap(*__first, *__m);
3624 return __m;
3625 }
3626 if (__len <= __p.second)
3627 { // The buffer is big enough to use
3628 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3629 __destruct_n __d(0);
3630 unique_ptr<value_type, __destruct_n&> __h(__p.first, __d);
3631 // Move the falses into the temporary buffer, and the trues to the front of the line
3632 // Update __first to always point to the end of the trues
3633 value_type* __t = __p.first;
Arthur O'Dwyerbe4c6572020-12-12 01:30:283634 ::new ((void*)__t) value_type(_VSTD::move(*__first));
Bruce Mitchener527a7fd2020-11-24 17:53:533635 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:163636 ++__t;
3637 _BidirectionalIterator __i = __first;
3638 while (++__i != __last)
3639 {
3640 if (__pred(*__i))
3641 {
Howard Hinnantce48a112011-06-30 21:18:193642 *__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:163643 ++__first;
3644 }
3645 else
3646 {
Arthur O'Dwyerbe4c6572020-12-12 01:30:283647 ::new ((void*)__t) value_type(_VSTD::move(*__i));
Bruce Mitchener527a7fd2020-11-24 17:53:533648 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:163649 ++__t;
3650 }
3651 }
3652 // move *__last, known to be true
Howard Hinnantce48a112011-06-30 21:18:193653 *__first = _VSTD::move(*__i);
Howard Hinnant3e519522010-05-11 19:42:163654 __i = ++__first;
3655 // All trues now at start of range, all falses in buffer
3656 // Move falses back into range, but don't mess up __first which points to first false
Marshall Clow7fa68652019-08-20 21:31:513657 for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, (void) ++__i)
Howard Hinnantce48a112011-06-30 21:18:193658 *__i = _VSTD::move(*__t2);
Howard Hinnant3e519522010-05-11 19:42:163659 // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
3660 return __first;
3661 }
3662 // Else not enough buffer, do in place
3663 // __len >= 4
3664 _BidirectionalIterator __m = __first;
3665 _Distance __len2 = __len / 2; // __len2 >= 2
Howard Hinnantce48a112011-06-30 21:18:193666 _VSTD::advance(__m, __len2);
Howard Hinnant3e519522010-05-11 19:42:163667 // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false
3668 // F????????????????T
3669 // f m l
3670 _BidirectionalIterator __m1 = __m;
3671 _BidirectionalIterator __first_false = __first;
3672 _Distance __len_half = __len2;
3673 while (!__pred(*--__m1))
3674 {
3675 if (__m1 == __first)
3676 goto __first_half_done;
3677 --__len_half;
3678 }
3679 // F???TFFF?????????T
3680 // f m1 m l
3681 typedef typename add_lvalue_reference<_Predicate>::type _PredRef;
Arthur O'Dwyer19688042020-11-22 18:21:113682 __first_false = _VSTD::__stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit);
Howard Hinnant3e519522010-05-11 19:42:163683__first_half_done:
3684 // TTTFFFFF?????????T
3685 // f ff m l
3686 // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true
3687 __m1 = __m;
3688 _BidirectionalIterator __second_false = __last;
3689 ++__second_false;
3690 __len_half = __len - __len2;
3691 while (__pred(*__m1))
3692 {
3693 if (++__m1 == __last)
3694 goto __second_half_done;
3695 --__len_half;
3696 }
3697 // TTTFFFFFTTTF?????T
3698 // f ff m m1 l
Arthur O'Dwyer19688042020-11-22 18:21:113699 __second_false = _VSTD::__stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit);
Howard Hinnant3e519522010-05-11 19:42:163700__second_half_done:
3701 // TTTFFFFFTTTTTFFFFF
3702 // f ff m sf l
Howard Hinnantce48a112011-06-30 21:18:193703 return _VSTD::rotate(__first_false, __m, __second_false);
Howard Hinnant3e519522010-05-11 19:42:163704 // TTTTTTTTFFFFFFFFFF
3705 // |
3706}
3707
3708template <class _Predicate, class _BidirectionalIterator>
3709_BidirectionalIterator
3710__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred,
3711 bidirectional_iterator_tag)
3712{
3713 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3714 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
3715 const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment
3716 // Either prove all true and return __first or point to first false
3717 while (true)
3718 {
3719 if (__first == __last)
3720 return __first;
3721 if (!__pred(*__first))
3722 break;
3723 ++__first;
3724 }
3725 // __first points to first false, everything prior to __first is already set.
3726 // Either prove [__first, __last) is all false and return __first, or point __last to last true
3727 do
3728 {
3729 if (__first == --__last)
3730 return __first;
3731 } while (!__pred(*__last));
3732 // We now have a reduced range [__first, __last]
3733 // *__first is known to be false
3734 // *__last is known to be true
3735 // __len >= 2
Howard Hinnantce48a112011-06-30 21:18:193736 difference_type __len = _VSTD::distance(__first, __last) + 1;
Howard Hinnant3e519522010-05-11 19:42:163737 pair<value_type*, ptrdiff_t> __p(0, 0);
3738 unique_ptr<value_type, __return_temporary_buffer> __h;
3739 if (__len >= __alloc_limit)
3740 {
Howard Hinnantce48a112011-06-30 21:18:193741 __p = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnant3e519522010-05-11 19:42:163742 __h.reset(__p.first);
3743 }
Arthur O'Dwyer19688042020-11-22 18:21:113744 return _VSTD::__stable_partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnant3e519522010-05-11 19:42:163745 (__first, __last, __pred, __len, __p, bidirectional_iterator_tag());
3746}
3747
3748template <class _ForwardIterator, class _Predicate>
3749inline _LIBCPP_INLINE_VISIBILITY
3750_ForwardIterator
3751stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred)
3752{
Arthur O'Dwyer19688042020-11-22 18:21:113753 return _VSTD::__stable_partition<typename add_lvalue_reference<_Predicate>::type>
Howard Hinnant3e519522010-05-11 19:42:163754 (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category());
3755}
3756
3757// is_sorted_until
3758
3759template <class _ForwardIterator, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:083760_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnant3e519522010-05-11 19:42:163761is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3762{
3763 if (__first != __last)
3764 {
3765 _ForwardIterator __i = __first;
3766 while (++__i != __last)
3767 {
3768 if (__comp(*__i, *__first))
3769 return __i;
3770 __first = __i;
3771 }
3772 }
3773 return __last;
3774}
3775
Howard Hinnantb3371f62010-08-22 00:02:433776template<class _ForwardIterator>
Nico Weber1362d7e2019-04-03 18:13:083777_LIBCPP_NODISCARD_EXT inline
3778_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:163779_ForwardIterator
3780is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3781{
Howard Hinnantce48a112011-06-30 21:18:193782 return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:163783}
3784
3785// is_sorted
3786
3787template <class _ForwardIterator, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:083788_LIBCPP_NODISCARD_EXT inline
3789_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:163790bool
3791is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
3792{
Howard Hinnantce48a112011-06-30 21:18:193793 return _VSTD::is_sorted_until(__first, __last, __comp) == __last;
Howard Hinnant3e519522010-05-11 19:42:163794}
3795
Howard Hinnantb3371f62010-08-22 00:02:433796template<class _ForwardIterator>
Nico Weber1362d7e2019-04-03 18:13:083797_LIBCPP_NODISCARD_EXT inline
3798_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:163799bool
3800is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3801{
Howard Hinnantce48a112011-06-30 21:18:193802 return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:163803}
3804
3805// sort
3806
3807// stable, 2-3 compares, 0-2 swaps
3808
3809template <class _Compare, class _ForwardIterator>
Arthur O'Dwyer207d4be2020-12-17 05:40:023810_LIBCPP_CONSTEXPR_AFTER_CXX11 unsigned
Howard Hinnant3e519522010-05-11 19:42:163811__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c)
3812{
3813 unsigned __r = 0;
3814 if (!__c(*__y, *__x)) // if x <= y
3815 {
3816 if (!__c(*__z, *__y)) // if y <= z
3817 return __r; // x <= y && y <= z
3818 // x <= y && y > z
3819 swap(*__y, *__z); // x <= z && y < z
3820 __r = 1;
3821 if (__c(*__y, *__x)) // if x > y
3822 {
3823 swap(*__x, *__y); // x < y && y <= z
3824 __r = 2;
3825 }
3826 return __r; // x <= y && y < z
3827 }
3828 if (__c(*__z, *__y)) // x > y, if y > z
3829 {
3830 swap(*__x, *__z); // x < y && y < z
3831 __r = 1;
3832 return __r;
3833 }
3834 swap(*__x, *__y); // x > y && y <= z
3835 __r = 1; // x < y && x <= z
3836 if (__c(*__z, *__y)) // if y > z
3837 {
3838 swap(*__y, *__z); // x <= y && y < z
3839 __r = 2;
3840 }
3841 return __r;
3842} // x <= y && y <= z
3843
3844// stable, 3-6 compares, 0-5 swaps
3845
3846template <class _Compare, class _ForwardIterator>
3847unsigned
3848__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3849 _ForwardIterator __x4, _Compare __c)
3850{
Arthur O'Dwyer19688042020-11-22 18:21:113851 unsigned __r = _VSTD::__sort3<_Compare>(__x1, __x2, __x3, __c);
Howard Hinnant3e519522010-05-11 19:42:163852 if (__c(*__x4, *__x3))
3853 {
3854 swap(*__x3, *__x4);
3855 ++__r;
3856 if (__c(*__x3, *__x2))
3857 {
3858 swap(*__x2, *__x3);
3859 ++__r;
3860 if (__c(*__x2, *__x1))
3861 {
3862 swap(*__x1, *__x2);
3863 ++__r;
3864 }
3865 }
3866 }
3867 return __r;
3868}
3869
3870// stable, 4-10 compares, 0-9 swaps
3871
3872template <class _Compare, class _ForwardIterator>
Louis Dionne835140a2018-11-21 16:24:463873_LIBCPP_HIDDEN
Howard Hinnant3e519522010-05-11 19:42:163874unsigned
3875__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3,
3876 _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c)
3877{
Arthur O'Dwyer19688042020-11-22 18:21:113878 unsigned __r = _VSTD::__sort4<_Compare>(__x1, __x2, __x3, __x4, __c);
Howard Hinnant3e519522010-05-11 19:42:163879 if (__c(*__x5, *__x4))
3880 {
3881 swap(*__x4, *__x5);
3882 ++__r;
3883 if (__c(*__x4, *__x3))
3884 {
3885 swap(*__x3, *__x4);
3886 ++__r;
3887 if (__c(*__x3, *__x2))
3888 {
3889 swap(*__x2, *__x3);
3890 ++__r;
3891 if (__c(*__x2, *__x1))
3892 {
3893 swap(*__x1, *__x2);
3894 ++__r;
3895 }
3896 }
3897 }
3898 }
3899 return __r;
3900}
3901
3902// Assumes size > 0
Arthur O'Dwyer1d7c39e2020-12-12 16:37:363903template <class _Compare, class _BidirectionalIterator>
Arthur O'Dwyer207d4be2020-12-17 05:40:023904_LIBCPP_CONSTEXPR_AFTER_CXX11 void
Arthur O'Dwyer1d7c39e2020-12-12 16:37:363905__selection_sort(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:163906{
Arthur O'Dwyer1d7c39e2020-12-12 16:37:363907 _BidirectionalIterator __lm1 = __last;
Howard Hinnant3e519522010-05-11 19:42:163908 for (--__lm1; __first != __lm1; ++__first)
3909 {
Arthur O'Dwyer1d7c39e2020-12-12 16:37:363910 _BidirectionalIterator __i = _VSTD::min_element<_BidirectionalIterator,
Howard Hinnant3e519522010-05-11 19:42:163911 typename add_lvalue_reference<_Compare>::type>
3912 (__first, __last, __comp);
3913 if (__i != __first)
3914 swap(*__first, *__i);
3915 }
3916}
3917
Arthur O'Dwyer1d7c39e2020-12-12 16:37:363918template <class _Compare, class _BidirectionalIterator>
Howard Hinnant3e519522010-05-11 19:42:163919void
Arthur O'Dwyer1d7c39e2020-12-12 16:37:363920__insertion_sort(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:163921{
Arthur O'Dwyer1d7c39e2020-12-12 16:37:363922 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
Howard Hinnant3e519522010-05-11 19:42:163923 if (__first != __last)
3924 {
Arthur O'Dwyer1d7c39e2020-12-12 16:37:363925 _BidirectionalIterator __i = __first;
Howard Hinnant3e519522010-05-11 19:42:163926 for (++__i; __i != __last; ++__i)
3927 {
Arthur O'Dwyer1d7c39e2020-12-12 16:37:363928 _BidirectionalIterator __j = __i;
Howard Hinnantce48a112011-06-30 21:18:193929 value_type __t(_VSTD::move(*__j));
Arthur O'Dwyer1d7c39e2020-12-12 16:37:363930 for (_BidirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j)
Howard Hinnantce48a112011-06-30 21:18:193931 *__j = _VSTD::move(*__k);
3932 *__j = _VSTD::move(__t);
Howard Hinnant3e519522010-05-11 19:42:163933 }
3934 }
3935}
3936
3937template <class _Compare, class _RandomAccessIterator>
3938void
3939__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3940{
3941 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3942 _RandomAccessIterator __j = __first+2;
Arthur O'Dwyer19688042020-11-22 18:21:113943 _VSTD::__sort3<_Compare>(__first, __first+1, __j, __comp);
Howard Hinnant3e519522010-05-11 19:42:163944 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3945 {
3946 if (__comp(*__i, *__j))
3947 {
Howard Hinnantce48a112011-06-30 21:18:193948 value_type __t(_VSTD::move(*__i));
Howard Hinnant3e519522010-05-11 19:42:163949 _RandomAccessIterator __k = __j;
3950 __j = __i;
3951 do
3952 {
Howard Hinnantce48a112011-06-30 21:18:193953 *__j = _VSTD::move(*__k);
Howard Hinnant3e519522010-05-11 19:42:163954 __j = __k;
3955 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnantce48a112011-06-30 21:18:193956 *__j = _VSTD::move(__t);
Howard Hinnant3e519522010-05-11 19:42:163957 }
3958 __j = __i;
3959 }
3960}
3961
3962template <class _Compare, class _RandomAccessIterator>
3963bool
3964__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
3965{
3966 switch (__last - __first)
3967 {
3968 case 0:
3969 case 1:
3970 return true;
3971 case 2:
3972 if (__comp(*--__last, *__first))
3973 swap(*__first, *__last);
3974 return true;
3975 case 3:
Howard Hinnantce48a112011-06-30 21:18:193976 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163977 return true;
3978 case 4:
Howard Hinnantce48a112011-06-30 21:18:193979 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163980 return true;
3981 case 5:
Howard Hinnantce48a112011-06-30 21:18:193982 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:163983 return true;
3984 }
3985 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
3986 _RandomAccessIterator __j = __first+2;
Arthur O'Dwyer19688042020-11-22 18:21:113987 _VSTD::__sort3<_Compare>(__first, __first+1, __j, __comp);
Howard Hinnant3e519522010-05-11 19:42:163988 const unsigned __limit = 8;
3989 unsigned __count = 0;
3990 for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
3991 {
3992 if (__comp(*__i, *__j))
3993 {
Howard Hinnantce48a112011-06-30 21:18:193994 value_type __t(_VSTD::move(*__i));
Howard Hinnant3e519522010-05-11 19:42:163995 _RandomAccessIterator __k = __j;
3996 __j = __i;
3997 do
3998 {
Howard Hinnantce48a112011-06-30 21:18:193999 *__j = _VSTD::move(*__k);
Howard Hinnant3e519522010-05-11 19:42:164000 __j = __k;
4001 } while (__j != __first && __comp(__t, *--__k));
Howard Hinnantce48a112011-06-30 21:18:194002 *__j = _VSTD::move(__t);
Howard Hinnant3e519522010-05-11 19:42:164003 if (++__count == __limit)
4004 return ++__i == __last;
4005 }
4006 __j = __i;
4007 }
4008 return true;
4009}
4010
Arthur O'Dwyer1d7c39e2020-12-12 16:37:364011template <class _Compare, class _BidirectionalIterator>
Howard Hinnant3e519522010-05-11 19:42:164012void
Arthur O'Dwyer1d7c39e2020-12-12 16:37:364013__insertion_sort_move(_BidirectionalIterator __first1, _BidirectionalIterator __last1,
4014 typename iterator_traits<_BidirectionalIterator>::value_type* __first2, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164015{
Arthur O'Dwyer1d7c39e2020-12-12 16:37:364016 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
Howard Hinnant3e519522010-05-11 19:42:164017 if (__first1 != __last1)
4018 {
4019 __destruct_n __d(0);
4020 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
4021 value_type* __last2 = __first2;
Arthur O'Dwyerbe4c6572020-12-12 01:30:284022 ::new ((void*)__last2) value_type(_VSTD::move(*__first1));
Bruce Mitchener527a7fd2020-11-24 17:53:534023 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:164024 for (++__last2; ++__first1 != __last1; ++__last2)
4025 {
4026 value_type* __j2 = __last2;
4027 value_type* __i2 = __j2;
4028 if (__comp(*__first1, *--__i2))
4029 {
Arthur O'Dwyerbe4c6572020-12-12 01:30:284030 ::new ((void*)__j2) value_type(_VSTD::move(*__i2));
Bruce Mitchener527a7fd2020-11-24 17:53:534031 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:164032 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
Howard Hinnantce48a112011-06-30 21:18:194033 *__j2 = _VSTD::move(*__i2);
4034 *__j2 = _VSTD::move(*__first1);
Howard Hinnant3e519522010-05-11 19:42:164035 }
4036 else
4037 {
Arthur O'Dwyerbe4c6572020-12-12 01:30:284038 ::new ((void*)__j2) value_type(_VSTD::move(*__first1));
Bruce Mitchener527a7fd2020-11-24 17:53:534039 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:164040 }
4041 }
4042 __h.release();
4043 }
4044}
4045
4046template <class _Compare, class _RandomAccessIterator>
4047void
4048__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4049{
Howard Hinnant3e519522010-05-11 19:42:164050 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4051 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
Howard Hinnantca740482010-11-19 22:17:284052 const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
4053 is_trivially_copy_assignable<value_type>::value ? 30 : 6;
Howard Hinnant3e519522010-05-11 19:42:164054 while (true)
4055 {
4056 __restart:
4057 difference_type __len = __last - __first;
4058 switch (__len)
4059 {
4060 case 0:
4061 case 1:
4062 return;
4063 case 2:
4064 if (__comp(*--__last, *__first))
4065 swap(*__first, *__last);
4066 return;
4067 case 3:
Howard Hinnantce48a112011-06-30 21:18:194068 _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:164069 return;
4070 case 4:
Howard Hinnantce48a112011-06-30 21:18:194071 _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:164072 return;
4073 case 5:
Howard Hinnantce48a112011-06-30 21:18:194074 _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:164075 return;
4076 }
4077 if (__len <= __limit)
4078 {
Howard Hinnantce48a112011-06-30 21:18:194079 _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:164080 return;
4081 }
4082 // __len > 5
4083 _RandomAccessIterator __m = __first;
4084 _RandomAccessIterator __lm1 = __last;
4085 --__lm1;
4086 unsigned __n_swaps;
4087 {
4088 difference_type __delta;
4089 if (__len >= 1000)
4090 {
4091 __delta = __len/2;
4092 __m += __delta;
4093 __delta /= 2;
Howard Hinnantce48a112011-06-30 21:18:194094 __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp);
Howard Hinnant3e519522010-05-11 19:42:164095 }
4096 else
4097 {
4098 __delta = __len/2;
4099 __m += __delta;
Howard Hinnantce48a112011-06-30 21:18:194100 __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp);
Howard Hinnant3e519522010-05-11 19:42:164101 }
4102 }
4103 // *__m is median
4104 // partition [__first, __m) < *__m and *__m <= [__m, __last)
4105 // (this inhibits tossing elements equivalent to __m around unnecessarily)
4106 _RandomAccessIterator __i = __first;
4107 _RandomAccessIterator __j = __lm1;
4108 // j points beyond range to be tested, *__m is known to be <= *__lm1
4109 // The search going up is known to be guarded but the search coming down isn't.
4110 // Prime the downward search with a guard.
4111 if (!__comp(*__i, *__m)) // if *__first == *__m
4112 {
4113 // *__first == *__m, *__first doesn't go in first part
4114 // manually guard downward moving __j against __i
4115 while (true)
4116 {
4117 if (__i == --__j)
4118 {
4119 // *__first == *__m, *__m <= all other elements
4120 // Parition instead into [__first, __i) == *__first and *__first < [__i, __last)
4121 ++__i; // __first + 1
4122 __j = __last;
4123 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
4124 {
4125 while (true)
4126 {
4127 if (__i == __j)
4128 return; // [__first, __last) all equivalent elements
4129 if (__comp(*__first, *__i))
4130 {
4131 swap(*__i, *__j);
4132 ++__n_swaps;
4133 ++__i;
4134 break;
4135 }
4136 ++__i;
4137 }
4138 }
4139 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
4140 if (__i == __j)
4141 return;
4142 while (true)
4143 {
4144 while (!__comp(*__first, *__i))
4145 ++__i;
4146 while (__comp(*__first, *--__j))
4147 ;
4148 if (__i >= __j)
4149 break;
4150 swap(*__i, *__j);
4151 ++__n_swaps;
4152 ++__i;
4153 }
4154 // [__first, __i) == *__first and *__first < [__i, __last)
Arthur O'Dwyerb6f19172020-12-12 16:57:324155 // The first part is sorted, sort the second part
Howard Hinnantce48a112011-06-30 21:18:194156 // _VSTD::__sort<_Compare>(__i, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:164157 __first = __i;
4158 goto __restart;
4159 }
4160 if (__comp(*__j, *__m))
4161 {
4162 swap(*__i, *__j);
4163 ++__n_swaps;
4164 break; // found guard for downward moving __j, now use unguarded partition
4165 }
4166 }
4167 }
4168 // It is known that *__i < *__m
4169 ++__i;
4170 // j points beyond range to be tested, *__m is known to be <= *__lm1
4171 // if not yet partitioned...
4172 if (__i < __j)
4173 {
4174 // known that *(__i - 1) < *__m
4175 // known that __i <= __m
4176 while (true)
4177 {
4178 // __m still guards upward moving __i
4179 while (__comp(*__i, *__m))
4180 ++__i;
4181 // It is now known that a guard exists for downward moving __j
4182 while (!__comp(*--__j, *__m))
4183 ;
4184 if (__i > __j)
4185 break;
4186 swap(*__i, *__j);
4187 ++__n_swaps;
4188 // It is known that __m != __j
4189 // If __m just moved, follow it
4190 if (__m == __i)
4191 __m = __j;
4192 ++__i;
4193 }
4194 }
4195 // [__first, __i) < *__m and *__m <= [__i, __last)
4196 if (__i != __m && __comp(*__m, *__i))
4197 {
4198 swap(*__i, *__m);
4199 ++__n_swaps;
4200 }
4201 // [__first, __i) < *__i and *__i <= [__i+1, __last)
4202 // If we were given a perfect partition, see if insertion sort is quick...
4203 if (__n_swaps == 0)
4204 {
Howard Hinnantce48a112011-06-30 21:18:194205 bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
4206 if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
Howard Hinnant3e519522010-05-11 19:42:164207 {
4208 if (__fs)
4209 return;
4210 __last = __i;
4211 continue;
4212 }
4213 else
4214 {
4215 if (__fs)
4216 {
4217 __first = ++__i;
4218 continue;
4219 }
4220 }
4221 }
4222 // sort smaller range with recursive call and larger with tail recursion elimination
4223 if (__i - __first < __last - __i)
4224 {
Howard Hinnantce48a112011-06-30 21:18:194225 _VSTD::__sort<_Compare>(__first, __i, __comp);
4226 // _VSTD::__sort<_Compare>(__i+1, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:164227 __first = ++__i;
4228 }
4229 else
4230 {
Howard Hinnantce48a112011-06-30 21:18:194231 _VSTD::__sort<_Compare>(__i+1, __last, __comp);
4232 // _VSTD::__sort<_Compare>(__first, __i, __comp);
Howard Hinnant3e519522010-05-11 19:42:164233 __last = __i;
4234 }
4235 }
4236}
4237
Arthur O'Dwyer493f1402020-12-20 20:21:424238template <class _Compare, class _Tp>
Howard Hinnant3e519522010-05-11 19:42:164239inline _LIBCPP_INLINE_VISIBILITY
4240void
Arthur O'Dwyer493f1402020-12-20 20:21:424241__sort(_Tp** __first, _Tp** __last, __less<_Tp*>&)
Howard Hinnant3e519522010-05-11 19:42:164242{
Arthur O'Dwyer493f1402020-12-20 20:21:424243 __less<uintptr_t> __comp;
4244 _VSTD::__sort<__less<uintptr_t>&, uintptr_t*>((uintptr_t*)__first, (uintptr_t*)__last, __comp);
Howard Hinnantf554add2011-09-14 18:33:514245}
4246
Howard Hinnantf0544c22013-08-12 18:38:344247_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&))
4248_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4249_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4250_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4251_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&))
4252_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4253_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&))
4254_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4255_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&))
4256_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4257_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4258_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>&))
4259_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&))
4260_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&))
4261_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:164262
Howard Hinnantf0544c22013-08-12 18:38:344263_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&))
4264_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&))
4265_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&))
4266_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&))
4267_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&))
4268_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&))
4269_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&))
4270_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&))
4271_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&))
4272_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&))
4273_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&))
4274_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>&))
4275_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&))
4276_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&))
4277_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:164278
Howard Hinnantf0544c22013-08-12 18:38:344279_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:164280
4281// lower_bound
4282
4283template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414284_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454285__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164286{
4287 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantce48a112011-06-30 21:18:194288 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:164289 while (__len != 0)
4290 {
Louis Dionne04695a72018-12-17 16:04:394291 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnant3e519522010-05-11 19:42:164292 _ForwardIterator __m = __first;
Howard Hinnantce48a112011-06-30 21:18:194293 _VSTD::advance(__m, __l2);
Howard Hinnante4383372011-10-22 20:59:454294 if (__comp(*__m, __value_))
Howard Hinnant3e519522010-05-11 19:42:164295 {
4296 __first = ++__m;
4297 __len -= __l2 + 1;
4298 }
4299 else
4300 __len = __l2;
4301 }
4302 return __first;
4303}
4304
4305template <class _ForwardIterator, class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:084306_LIBCPP_NODISCARD_EXT inline
4307_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164308_ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454309lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164310{
Howard Hinnant3e519522010-05-11 19:42:164311 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:114312 return _VSTD::__lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant3e519522010-05-11 19:42:164313}
4314
4315template <class _ForwardIterator, class _Tp>
Nico Weber1362d7e2019-04-03 18:13:084316_LIBCPP_NODISCARD_EXT inline
4317_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164318_ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454319lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:164320{
Howard Hinnante4383372011-10-22 20:59:454321 return _VSTD::lower_bound(__first, __last, __value_,
Howard Hinnant3e519522010-05-11 19:42:164322 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4323}
4324
4325// upper_bound
4326
4327template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414328_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454329__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164330{
4331 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantce48a112011-06-30 21:18:194332 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:164333 while (__len != 0)
4334 {
Louis Dionne04695a72018-12-17 16:04:394335 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnant3e519522010-05-11 19:42:164336 _ForwardIterator __m = __first;
Howard Hinnantce48a112011-06-30 21:18:194337 _VSTD::advance(__m, __l2);
Howard Hinnante4383372011-10-22 20:59:454338 if (__comp(__value_, *__m))
Howard Hinnant3e519522010-05-11 19:42:164339 __len = __l2;
4340 else
4341 {
4342 __first = ++__m;
4343 __len -= __l2 + 1;
4344 }
4345 }
4346 return __first;
4347}
4348
4349template <class _ForwardIterator, class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:084350_LIBCPP_NODISCARD_EXT inline
4351_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164352_ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454353upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164354{
Howard Hinnant3e519522010-05-11 19:42:164355 typedef typename add_lvalue_reference<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:114356 return _VSTD::__upper_bound<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant3e519522010-05-11 19:42:164357}
4358
4359template <class _ForwardIterator, class _Tp>
Nico Weber1362d7e2019-04-03 18:13:084360_LIBCPP_NODISCARD_EXT inline
4361_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164362_ForwardIterator
Howard Hinnante4383372011-10-22 20:59:454363upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:164364{
Howard Hinnante4383372011-10-22 20:59:454365 return _VSTD::upper_bound(__first, __last, __value_,
Howard Hinnant3e519522010-05-11 19:42:164366 __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>());
4367}
4368
4369// equal_range
4370
4371template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414372_LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_ForwardIterator, _ForwardIterator>
Howard Hinnante4383372011-10-22 20:59:454373__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164374{
4375 typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type;
Howard Hinnantce48a112011-06-30 21:18:194376 difference_type __len = _VSTD::distance(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:164377 while (__len != 0)
4378 {
Louis Dionne04695a72018-12-17 16:04:394379 difference_type __l2 = _VSTD::__half_positive(__len);
Howard Hinnant3e519522010-05-11 19:42:164380 _ForwardIterator __m = __first;
Howard Hinnantce48a112011-06-30 21:18:194381 _VSTD::advance(__m, __l2);
Howard Hinnante4383372011-10-22 20:59:454382 if (__comp(*__m, __value_))
Howard Hinnant3e519522010-05-11 19:42:164383 {
4384 __first = ++__m;
4385 __len -= __l2 + 1;
4386 }
Howard Hinnante4383372011-10-22 20:59:454387 else if (__comp(__value_, *__m))
Howard Hinnant3e519522010-05-11 19:42:164388 {
4389 __last = __m;
4390 __len = __l2;
4391 }
4392 else
4393 {
4394 _ForwardIterator __mp1 = __m;
4395 return pair<_ForwardIterator, _ForwardIterator>
4396 (
Arthur O'Dwyer19688042020-11-22 18:21:114397 _VSTD::__lower_bound<_Compare>(__first, __m, __value_, __comp),
4398 _VSTD::__upper_bound<_Compare>(++__mp1, __last, __value_, __comp)
Howard Hinnant3e519522010-05-11 19:42:164399 );
4400 }
4401 }
4402 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4403}
4404
4405template <class _ForwardIterator, class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:084406_LIBCPP_NODISCARD_EXT inline
4407_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164408pair<_ForwardIterator, _ForwardIterator>
Howard Hinnante4383372011-10-22 20:59:454409equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164410{
Eric Fiselieraa1cad12019-04-12 05:18:194411 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:114412 return _VSTD::__equal_range<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant3e519522010-05-11 19:42:164413}
4414
4415template <class _ForwardIterator, class _Tp>
Nico Weber1362d7e2019-04-03 18:13:084416_LIBCPP_NODISCARD_EXT inline
4417_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164418pair<_ForwardIterator, _ForwardIterator>
Howard Hinnante4383372011-10-22 20:59:454419equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:164420{
Howard Hinnante4383372011-10-22 20:59:454421 return _VSTD::equal_range(__first, __last, __value_,
Howard Hinnant3e519522010-05-11 19:42:164422 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4423}
4424
4425// binary_search
4426
4427template <class _Compare, class _ForwardIterator, class _Tp>
Marshall Clowd57c03d2018-01-16 02:34:414428inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164429bool
Howard Hinnante4383372011-10-22 20:59:454430__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164431{
Arthur O'Dwyer19688042020-11-22 18:21:114432 __first = _VSTD::__lower_bound<_Compare>(__first, __last, __value_, __comp);
Howard Hinnante4383372011-10-22 20:59:454433 return __first != __last && !__comp(__value_, *__first);
Howard Hinnant3e519522010-05-11 19:42:164434}
4435
4436template <class _ForwardIterator, class _Tp, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:084437_LIBCPP_NODISCARD_EXT inline
4438_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164439bool
Howard Hinnante4383372011-10-22 20:59:454440binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp)
Howard Hinnant3e519522010-05-11 19:42:164441{
Eric Fiselieraa1cad12019-04-12 05:18:194442 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:114443 return _VSTD::__binary_search<_Comp_ref>(__first, __last, __value_, __comp);
Howard Hinnant3e519522010-05-11 19:42:164444}
4445
4446template <class _ForwardIterator, class _Tp>
Nico Weber1362d7e2019-04-03 18:13:084447_LIBCPP_NODISCARD_EXT inline
4448_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164449bool
Howard Hinnante4383372011-10-22 20:59:454450binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:164451{
Howard Hinnante4383372011-10-22 20:59:454452 return _VSTD::binary_search(__first, __last, __value_,
Howard Hinnant3e519522010-05-11 19:42:164453 __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>());
4454}
4455
4456// merge
4457
4458template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
Nicholas-Baronb552a302020-09-14 20:37:414459_LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164460_OutputIterator
4461__merge(_InputIterator1 __first1, _InputIterator1 __last1,
4462 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4463{
4464 for (; __first1 != __last1; ++__result)
4465 {
4466 if (__first2 == __last2)
Howard Hinnantce48a112011-06-30 21:18:194467 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnant3e519522010-05-11 19:42:164468 if (__comp(*__first2, *__first1))
4469 {
4470 *__result = *__first2;
4471 ++__first2;
4472 }
4473 else
4474 {
4475 *__result = *__first1;
4476 ++__first1;
4477 }
4478 }
Howard Hinnantce48a112011-06-30 21:18:194479 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnant3e519522010-05-11 19:42:164480}
4481
4482template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
Nicholas-Baronb552a302020-09-14 20:37:414483inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164484_OutputIterator
4485merge(_InputIterator1 __first1, _InputIterator1 __last1,
4486 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
4487{
Eric Fiselieraa1cad12019-04-12 05:18:194488 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantce48a112011-06-30 21:18:194489 return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant3e519522010-05-11 19:42:164490}
4491
4492template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
Nicholas-Baronb552a302020-09-14 20:37:414493inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164494_OutputIterator
4495merge(_InputIterator1 __first1, _InputIterator1 __last1,
4496 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
4497{
4498 typedef typename iterator_traits<_InputIterator1>::value_type __v1;
4499 typedef typename iterator_traits<_InputIterator2>::value_type __v2;
Marshall Clow4f417792019-08-20 22:23:354500 return _VSTD::merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>());
Howard Hinnant3e519522010-05-11 19:42:164501}
4502
4503// inplace_merge
4504
Marshall Clowadfdae12015-07-29 16:25:454505template <class _Compare, class _InputIterator1, class _InputIterator2,
4506 class _OutputIterator>
4507void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1,
4508 _InputIterator2 __first2, _InputIterator2 __last2,
4509 _OutputIterator __result, _Compare __comp)
4510{
4511 for (; __first1 != __last1; ++__result)
4512 {
4513 if (__first2 == __last2)
4514 {
4515 _VSTD::move(__first1, __last1, __result);
4516 return;
4517 }
4518
4519 if (__comp(*__first2, *__first1))
4520 {
4521 *__result = _VSTD::move(*__first2);
4522 ++__first2;
4523 }
4524 else
4525 {
4526 *__result = _VSTD::move(*__first1);
4527 ++__first1;
4528 }
4529 }
4530 // __first2 through __last2 are already in the right spot.
4531}
4532
Howard Hinnant3e519522010-05-11 19:42:164533template <class _Compare, class _BidirectionalIterator>
4534void
4535__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4536 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4537 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4538 typename iterator_traits<_BidirectionalIterator>::value_type* __buff)
4539{
4540 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
Howard Hinnant3e519522010-05-11 19:42:164541 __destruct_n __d(0);
4542 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
4543 if (__len1 <= __len2)
4544 {
4545 value_type* __p = __buff;
Bruce Mitchener527a7fd2020-11-24 17:53:534546 for (_BidirectionalIterator __i = __first; __i != __middle; __d.template __incr<value_type>(), (void) ++__i, (void) ++__p)
Arthur O'Dwyerbe4c6572020-12-12 01:30:284547 ::new ((void*)__p) value_type(_VSTD::move(*__i));
Arthur O'Dwyereef4bdb2020-12-18 20:11:514548 _VSTD::__half_inplace_merge<_Compare>(__buff, __p, __middle, __last, __first, __comp);
Howard Hinnant3e519522010-05-11 19:42:164549 }
4550 else
4551 {
4552 value_type* __p = __buff;
Bruce Mitchener527a7fd2020-11-24 17:53:534553 for (_BidirectionalIterator __i = __middle; __i != __last; __d.template __incr<value_type>(), (void) ++__i, (void) ++__p)
Arthur O'Dwyerbe4c6572020-12-12 01:30:284554 ::new ((void*)__p) value_type(_VSTD::move(*__i));
Howard Hinnant3e519522010-05-11 19:42:164555 typedef reverse_iterator<_BidirectionalIterator> _RBi;
4556 typedef reverse_iterator<value_type*> _Rv;
Arthur O'Dwyereef4bdb2020-12-18 20:11:514557 typedef __invert<_Compare> _Inverted;
4558 _VSTD::__half_inplace_merge<_Inverted>(_Rv(__p), _Rv(__buff),
Arthur O'Dwyer19688042020-11-22 18:21:114559 _RBi(__middle), _RBi(__first),
Arthur O'Dwyereef4bdb2020-12-18 20:11:514560 _RBi(__last), _Inverted(__comp));
Howard Hinnant3e519522010-05-11 19:42:164561 }
4562}
4563
4564template <class _Compare, class _BidirectionalIterator>
4565void
4566__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4567 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1,
4568 typename iterator_traits<_BidirectionalIterator>::difference_type __len2,
4569 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size)
4570{
Howard Hinnant3e519522010-05-11 19:42:164571 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4572 while (true)
4573 {
4574 // if __middle == __last, we're done
4575 if (__len2 == 0)
4576 return;
Marshall Clow526e0922015-02-02 16:44:114577 if (__len1 <= __buff_size || __len2 <= __buff_size)
Arthur O'Dwyer19688042020-11-22 18:21:114578 return _VSTD::__buffered_inplace_merge<_Compare>
Marshall Clow526e0922015-02-02 16:44:114579 (__first, __middle, __last, __comp, __len1, __len2, __buff);
Howard Hinnant3e519522010-05-11 19:42:164580 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0
Eric Fiselier910285b2014-10-27 19:28:204581 for (; true; ++__first, (void) --__len1)
Howard Hinnant3e519522010-05-11 19:42:164582 {
4583 if (__len1 == 0)
4584 return;
4585 if (__comp(*__middle, *__first))
4586 break;
4587 }
Howard Hinnant3e519522010-05-11 19:42:164588 // __first < __middle < __last
4589 // *__first > *__middle
4590 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that
4591 // all elements in:
4592 // [__first, __m1) <= [__middle, __m2)
4593 // [__middle, __m2) < [__m1, __middle)
4594 // [__m1, __middle) <= [__m2, __last)
4595 // and __m1 or __m2 is in the middle of its range
4596 _BidirectionalIterator __m1; // "median" of [__first, __middle)
4597 _BidirectionalIterator __m2; // "median" of [__middle, __last)
4598 difference_type __len11; // distance(__first, __m1)
4599 difference_type __len21; // distance(__middle, __m2)
4600 // binary search smaller range
4601 if (__len1 < __len2)
4602 { // __len >= 1, __len2 >= 2
4603 __len21 = __len2 / 2;
4604 __m2 = __middle;
Howard Hinnantce48a112011-06-30 21:18:194605 _VSTD::advance(__m2, __len21);
Arthur O'Dwyer19688042020-11-22 18:21:114606 __m1 = _VSTD::__upper_bound<_Compare>(__first, __middle, *__m2, __comp);
Howard Hinnantce48a112011-06-30 21:18:194607 __len11 = _VSTD::distance(__first, __m1);
Howard Hinnant3e519522010-05-11 19:42:164608 }
4609 else
4610 {
4611 if (__len1 == 1)
4612 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1
4613 // It is known *__first > *__middle
4614 swap(*__first, *__middle);
4615 return;
4616 }
4617 // __len1 >= 2, __len2 >= 1
4618 __len11 = __len1 / 2;
4619 __m1 = __first;
Howard Hinnantce48a112011-06-30 21:18:194620 _VSTD::advance(__m1, __len11);
Arthur O'Dwyer19688042020-11-22 18:21:114621 __m2 = _VSTD::__lower_bound<_Compare>(__middle, __last, *__m1, __comp);
Howard Hinnantce48a112011-06-30 21:18:194622 __len21 = _VSTD::distance(__middle, __m2);
Howard Hinnant3e519522010-05-11 19:42:164623 }
4624 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle)
4625 difference_type __len22 = __len2 - __len21; // distance(__m2, __last)
4626 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last)
4627 // swap middle two partitions
Howard Hinnantce48a112011-06-30 21:18:194628 __middle = _VSTD::rotate(__m1, __middle, __m2);
Howard Hinnant3e519522010-05-11 19:42:164629 // __len12 and __len21 now have swapped meanings
Arthur O'Dwyerb6f19172020-12-12 16:57:324630 // merge smaller range with recursive call and larger with tail recursion elimination
Howard Hinnant3e519522010-05-11 19:42:164631 if (__len11 + __len21 < __len12 + __len22)
4632 {
Arthur O'Dwyer19688042020-11-22 18:21:114633 _VSTD::__inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
4634// _VSTD::__inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
Howard Hinnant3e519522010-05-11 19:42:164635 __first = __middle;
4636 __middle = __m2;
4637 __len1 = __len12;
4638 __len2 = __len22;
4639 }
4640 else
4641 {
Arthur O'Dwyer19688042020-11-22 18:21:114642 _VSTD::__inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size);
4643// _VSTD::__inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size);
Howard Hinnant3e519522010-05-11 19:42:164644 __last = __middle;
4645 __middle = __m1;
4646 __len1 = __len11;
4647 __len2 = __len21;
4648 }
4649 }
4650}
4651
Howard Hinnant3e519522010-05-11 19:42:164652template <class _BidirectionalIterator, class _Compare>
4653inline _LIBCPP_INLINE_VISIBILITY
4654void
4655inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last,
4656 _Compare __comp)
4657{
4658 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
4659 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnantce48a112011-06-30 21:18:194660 difference_type __len1 = _VSTD::distance(__first, __middle);
4661 difference_type __len2 = _VSTD::distance(__middle, __last);
4662 difference_type __buf_size = _VSTD::min(__len1, __len2);
Marshall Clow0b48cf92015-02-02 17:35:534663 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size);
4664 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first);
Eric Fiselieraa1cad12019-04-12 05:18:194665 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Howard Hinnantce48a112011-06-30 21:18:194666 return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2,
Howard Hinnant3e519522010-05-11 19:42:164667 __buf.first, __buf.second);
Howard Hinnant3e519522010-05-11 19:42:164668}
4669
4670template <class _BidirectionalIterator>
4671inline _LIBCPP_INLINE_VISIBILITY
4672void
4673inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last)
4674{
Howard Hinnantce48a112011-06-30 21:18:194675 _VSTD::inplace_merge(__first, __middle, __last,
Howard Hinnant3e519522010-05-11 19:42:164676 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
4677}
4678
4679// stable_sort
4680
4681template <class _Compare, class _InputIterator1, class _InputIterator2>
4682void
4683__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
4684 _InputIterator2 __first2, _InputIterator2 __last2,
4685 typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp)
4686{
4687 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
4688 __destruct_n __d(0);
4689 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
4690 for (; true; ++__result)
4691 {
4692 if (__first1 == __last1)
4693 {
Bruce Mitchener527a7fd2020-11-24 17:53:534694 for (; __first2 != __last2; ++__first2, ++__result, (void)__d.template __incr<value_type>())
Arthur O'Dwyerbe4c6572020-12-12 01:30:284695 ::new ((void*)__result) value_type(_VSTD::move(*__first2));
Howard Hinnant3e519522010-05-11 19:42:164696 __h.release();
4697 return;
4698 }
4699 if (__first2 == __last2)
4700 {
Bruce Mitchener527a7fd2020-11-24 17:53:534701 for (; __first1 != __last1; ++__first1, ++__result, (void)__d.template __incr<value_type>())
Arthur O'Dwyerbe4c6572020-12-12 01:30:284702 ::new ((void*)__result) value_type(_VSTD::move(*__first1));
Howard Hinnant3e519522010-05-11 19:42:164703 __h.release();
4704 return;
4705 }
4706 if (__comp(*__first2, *__first1))
4707 {
Arthur O'Dwyerbe4c6572020-12-12 01:30:284708 ::new ((void*)__result) value_type(_VSTD::move(*__first2));
Bruce Mitchener527a7fd2020-11-24 17:53:534709 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:164710 ++__first2;
4711 }
4712 else
4713 {
Arthur O'Dwyerbe4c6572020-12-12 01:30:284714 ::new ((void*)__result) value_type(_VSTD::move(*__first1));
Bruce Mitchener527a7fd2020-11-24 17:53:534715 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:164716 ++__first1;
4717 }
4718 }
4719}
4720
4721template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
4722void
4723__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
4724 _InputIterator2 __first2, _InputIterator2 __last2,
4725 _OutputIterator __result, _Compare __comp)
4726{
4727 for (; __first1 != __last1; ++__result)
4728 {
4729 if (__first2 == __last2)
4730 {
Marshall Clow7fa68652019-08-20 21:31:514731 for (; __first1 != __last1; ++__first1, (void) ++__result)
Howard Hinnantce48a112011-06-30 21:18:194732 *__result = _VSTD::move(*__first1);
Howard Hinnant3e519522010-05-11 19:42:164733 return;
4734 }
4735 if (__comp(*__first2, *__first1))
4736 {
Howard Hinnantce48a112011-06-30 21:18:194737 *__result = _VSTD::move(*__first2);
Howard Hinnant3e519522010-05-11 19:42:164738 ++__first2;
4739 }
4740 else
4741 {
Howard Hinnantce48a112011-06-30 21:18:194742 *__result = _VSTD::move(*__first1);
Howard Hinnant3e519522010-05-11 19:42:164743 ++__first1;
4744 }
4745 }
Marshall Clow7fa68652019-08-20 21:31:514746 for (; __first2 != __last2; ++__first2, (void) ++__result)
Howard Hinnantce48a112011-06-30 21:18:194747 *__result = _VSTD::move(*__first2);
Howard Hinnant3e519522010-05-11 19:42:164748}
4749
4750template <class _Compare, class _RandomAccessIterator>
4751void
4752__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4753 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4754 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size);
4755
4756template <class _Compare, class _RandomAccessIterator>
4757void
4758__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp,
4759 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4760 typename iterator_traits<_RandomAccessIterator>::value_type* __first2)
4761{
4762 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4763 switch (__len)
4764 {
4765 case 0:
4766 return;
4767 case 1:
Arthur O'Dwyerbe4c6572020-12-12 01:30:284768 ::new ((void*)__first2) value_type(_VSTD::move(*__first1));
Howard Hinnant3e519522010-05-11 19:42:164769 return;
4770 case 2:
Marshall Clowf951fc32018-02-06 18:58:054771 __destruct_n __d(0);
Howard Hinnant3e519522010-05-11 19:42:164772 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
Marshall Clowf951fc32018-02-06 18:58:054773 if (__comp(*--__last1, *__first1))
Howard Hinnant3e519522010-05-11 19:42:164774 {
Arthur O'Dwyerbe4c6572020-12-12 01:30:284775 ::new ((void*)__first2) value_type(_VSTD::move(*__last1));
Bruce Mitchener527a7fd2020-11-24 17:53:534776 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:164777 ++__first2;
Arthur O'Dwyerbe4c6572020-12-12 01:30:284778 ::new ((void*)__first2) value_type(_VSTD::move(*__first1));
Howard Hinnant3e519522010-05-11 19:42:164779 }
4780 else
4781 {
Arthur O'Dwyerbe4c6572020-12-12 01:30:284782 ::new ((void*)__first2) value_type(_VSTD::move(*__first1));
Bruce Mitchener527a7fd2020-11-24 17:53:534783 __d.template __incr<value_type>();
Howard Hinnant3e519522010-05-11 19:42:164784 ++__first2;
Arthur O'Dwyerbe4c6572020-12-12 01:30:284785 ::new ((void*)__first2) value_type(_VSTD::move(*__last1));
Howard Hinnant3e519522010-05-11 19:42:164786 }
4787 __h2.release();
4788 return;
4789 }
4790 if (__len <= 8)
4791 {
Arthur O'Dwyer19688042020-11-22 18:21:114792 _VSTD::__insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp);
Howard Hinnant3e519522010-05-11 19:42:164793 return;
4794 }
4795 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4796 _RandomAccessIterator __m = __first1 + __l2;
Arthur O'Dwyer19688042020-11-22 18:21:114797 _VSTD::__stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2);
4798 _VSTD::__stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
4799 _VSTD::__merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp);
Howard Hinnant3e519522010-05-11 19:42:164800}
4801
4802template <class _Tp>
4803struct __stable_sort_switch
4804{
Howard Hinnantca740482010-11-19 22:17:284805 static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value;
Howard Hinnant3e519522010-05-11 19:42:164806};
4807
4808template <class _Compare, class _RandomAccessIterator>
4809void
4810__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4811 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4812 typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size)
4813{
4814 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4815 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4816 switch (__len)
4817 {
4818 case 0:
4819 case 1:
4820 return;
4821 case 2:
4822 if (__comp(*--__last, *__first))
4823 swap(*__first, *__last);
4824 return;
4825 }
4826 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4827 {
Arthur O'Dwyer19688042020-11-22 18:21:114828 _VSTD::__insertion_sort<_Compare>(__first, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:164829 return;
4830 }
4831 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
4832 _RandomAccessIterator __m = __first + __l2;
4833 if (__len <= __buff_size)
4834 {
4835 __destruct_n __d(0);
4836 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
Arthur O'Dwyer19688042020-11-22 18:21:114837 _VSTD::__stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff);
Bruce Mitchener527a7fd2020-11-24 17:53:534838 __d.__set(__l2, (value_type*)nullptr);
Arthur O'Dwyer19688042020-11-22 18:21:114839 _VSTD::__stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
Bruce Mitchener527a7fd2020-11-24 17:53:534840 __d.__set(__len, (value_type*)nullptr);
Arthur O'Dwyer19688042020-11-22 18:21:114841 _VSTD::__merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
4842// _VSTD::__merge<_Compare>(move_iterator<value_type*>(__buff),
4843// move_iterator<value_type*>(__buff + __l2),
4844// move_iterator<_RandomAccessIterator>(__buff + __l2),
4845// move_iterator<_RandomAccessIterator>(__buff + __len),
4846// __first, __comp);
Howard Hinnant3e519522010-05-11 19:42:164847 return;
4848 }
Arthur O'Dwyer19688042020-11-22 18:21:114849 _VSTD::__stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
4850 _VSTD::__stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
4851 _VSTD::__inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
Howard Hinnant3e519522010-05-11 19:42:164852}
4853
4854template <class _RandomAccessIterator, class _Compare>
4855inline _LIBCPP_INLINE_VISIBILITY
4856void
4857stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4858{
4859 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4860 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4861 difference_type __len = __last - __first;
4862 pair<value_type*, ptrdiff_t> __buf(0, 0);
4863 unique_ptr<value_type, __return_temporary_buffer> __h;
4864 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value))
4865 {
Howard Hinnantce48a112011-06-30 21:18:194866 __buf = _VSTD::get_temporary_buffer<value_type>(__len);
Howard Hinnant3e519522010-05-11 19:42:164867 __h.reset(__buf.first);
4868 }
Eric Fiselieraa1cad12019-04-12 05:18:194869 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:114870 _VSTD::__stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second);
Howard Hinnant3e519522010-05-11 19:42:164871}
4872
4873template <class _RandomAccessIterator>
4874inline _LIBCPP_INLINE_VISIBILITY
4875void
4876stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4877{
Howard Hinnantce48a112011-06-30 21:18:194878 _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164879}
4880
4881// is_heap_until
4882
4883template <class _RandomAccessIterator, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:084884_LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
Howard Hinnant3e519522010-05-11 19:42:164885is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4886{
Howard Hinnantce48a112011-06-30 21:18:194887 typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Howard Hinnant3e519522010-05-11 19:42:164888 difference_type __len = __last - __first;
4889 difference_type __p = 0;
4890 difference_type __c = 1;
4891 _RandomAccessIterator __pp = __first;
4892 while (__c < __len)
4893 {
4894 _RandomAccessIterator __cp = __first + __c;
4895 if (__comp(*__pp, *__cp))
4896 return __cp;
4897 ++__c;
4898 ++__cp;
4899 if (__c == __len)
4900 return __last;
4901 if (__comp(*__pp, *__cp))
4902 return __cp;
4903 ++__p;
4904 ++__pp;
4905 __c = 2 * __p + 1;
4906 }
4907 return __last;
4908}
4909
Howard Hinnantb3371f62010-08-22 00:02:434910template<class _RandomAccessIterator>
Nico Weber1362d7e2019-04-03 18:13:084911_LIBCPP_NODISCARD_EXT inline
4912_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164913_RandomAccessIterator
4914is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
4915{
Howard Hinnantce48a112011-06-30 21:18:194916 return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164917}
4918
4919// is_heap
4920
4921template <class _RandomAccessIterator, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:084922_LIBCPP_NODISCARD_EXT inline
4923_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164924bool
4925is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4926{
Howard Hinnantce48a112011-06-30 21:18:194927 return _VSTD::is_heap_until(__first, __last, __comp) == __last;
Howard Hinnant3e519522010-05-11 19:42:164928}
4929
Howard Hinnantb3371f62010-08-22 00:02:434930template<class _RandomAccessIterator>
Nico Weber1362d7e2019-04-03 18:13:084931_LIBCPP_NODISCARD_EXT inline
4932_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164933bool
4934is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4935{
Howard Hinnantce48a112011-06-30 21:18:194936 return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164937}
4938
4939// push_heap
4940
4941template <class _Compare, class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:184942_LIBCPP_CONSTEXPR_AFTER_CXX11 void
David Majnemer8b512602014-07-22 06:07:094943__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
4944 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
Howard Hinnant3e519522010-05-11 19:42:164945{
Howard Hinnant3e519522010-05-11 19:42:164946 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4947 if (__len > 1)
4948 {
4949 __len = (__len - 2) / 2;
4950 _RandomAccessIterator __ptr = __first + __len;
4951 if (__comp(*__ptr, *--__last))
4952 {
Howard Hinnantce48a112011-06-30 21:18:194953 value_type __t(_VSTD::move(*__last));
Howard Hinnant3e519522010-05-11 19:42:164954 do
4955 {
Howard Hinnantce48a112011-06-30 21:18:194956 *__last = _VSTD::move(*__ptr);
Howard Hinnant3e519522010-05-11 19:42:164957 __last = __ptr;
4958 if (__len == 0)
4959 break;
4960 __len = (__len - 1) / 2;
4961 __ptr = __first + __len;
4962 } while (__comp(*__ptr, __t));
Howard Hinnantce48a112011-06-30 21:18:194963 *__last = _VSTD::move(__t);
Howard Hinnant3e519522010-05-11 19:42:164964 }
4965 }
4966}
4967
4968template <class _RandomAccessIterator, class _Compare>
Arthur O'Dwyer5386aa22020-12-17 05:26:184969inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164970void
4971push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
4972{
Eric Fiselieraa1cad12019-04-12 05:18:194973 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:114974 _VSTD::__sift_up<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant3e519522010-05-11 19:42:164975}
4976
4977template <class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:184978inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:164979void
4980push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
4981{
Howard Hinnantce48a112011-06-30 21:18:194982 _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:164983}
4984
4985// pop_heap
4986
4987template <class _Compare, class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:184988_LIBCPP_CONSTEXPR_AFTER_CXX11 void
Eric Fiselierfd838222016-12-23 23:37:524989__sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
4990 _Compare __comp,
David Majnemer8b512602014-07-22 06:07:094991 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
4992 _RandomAccessIterator __start)
4993{
4994 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4995 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
4996 // left-child of __start is at 2 * __start + 1
4997 // right-child of __start is at 2 * __start + 2
4998 difference_type __child = __start - __first;
4999
5000 if (__len < 2 || (__len - 2) / 2 < __child)
5001 return;
5002
5003 __child = 2 * __child + 1;
5004 _RandomAccessIterator __child_i = __first + __child;
5005
5006 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
5007 // right-child exists and is greater than left-child
5008 ++__child_i;
5009 ++__child;
5010 }
5011
5012 // check if we are in heap-order
5013 if (__comp(*__child_i, *__start))
5014 // we are, __start is larger than it's largest child
5015 return;
5016
5017 value_type __top(_VSTD::move(*__start));
5018 do
5019 {
Arthur O'Dwyer5386aa22020-12-17 05:26:185020 // we are not in heap-order, swap the parent with its largest child
David Majnemer8b512602014-07-22 06:07:095021 *__start = _VSTD::move(*__child_i);
5022 __start = __child_i;
5023
5024 if ((__len - 2) / 2 < __child)
5025 break;
5026
5027 // recompute the child based off of the updated parent
5028 __child = 2 * __child + 1;
5029 __child_i = __first + __child;
5030
5031 if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
5032 // right-child exists and is greater than left-child
5033 ++__child_i;
5034 ++__child;
5035 }
5036
5037 // check if we are in heap-order
5038 } while (!__comp(*__child_i, __top));
5039 *__start = _VSTD::move(__top);
5040}
5041
5042template <class _Compare, class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185043inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165044void
5045__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
5046 typename iterator_traits<_RandomAccessIterator>::difference_type __len)
5047{
5048 if (__len > 1)
5049 {
5050 swap(*__first, *--__last);
Arthur O'Dwyer19688042020-11-22 18:21:115051 _VSTD::__sift_down<_Compare>(__first, __last, __comp, __len - 1, __first);
Howard Hinnant3e519522010-05-11 19:42:165052 }
5053}
5054
5055template <class _RandomAccessIterator, class _Compare>
Arthur O'Dwyer5386aa22020-12-17 05:26:185056inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165057void
5058pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5059{
Eric Fiselieraa1cad12019-04-12 05:18:195060 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115061 _VSTD::__pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first);
Howard Hinnant3e519522010-05-11 19:42:165062}
5063
5064template <class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185065inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165066void
5067pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5068{
Howard Hinnantce48a112011-06-30 21:18:195069 _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:165070}
5071
5072// make_heap
5073
5074template <class _Compare, class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185075_LIBCPP_CONSTEXPR_AFTER_CXX11 void
Howard Hinnant3e519522010-05-11 19:42:165076__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5077{
5078 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5079 difference_type __n = __last - __first;
5080 if (__n > 1)
5081 {
David Majnemer8b512602014-07-22 06:07:095082 // start from the first parent, there is no need to consider children
5083 for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start)
5084 {
Arthur O'Dwyer19688042020-11-22 18:21:115085 _VSTD::__sift_down<_Compare>(__first, __last, __comp, __n, __first + __start);
David Majnemer8b512602014-07-22 06:07:095086 }
Howard Hinnant3e519522010-05-11 19:42:165087 }
5088}
5089
5090template <class _RandomAccessIterator, class _Compare>
Arthur O'Dwyer5386aa22020-12-17 05:26:185091inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165092void
5093make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5094{
Eric Fiselieraa1cad12019-04-12 05:18:195095 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115096 _VSTD::__make_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165097}
5098
5099template <class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185100inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165101void
5102make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5103{
Howard Hinnantce48a112011-06-30 21:18:195104 _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:165105}
5106
5107// sort_heap
5108
5109template <class _Compare, class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185110_LIBCPP_CONSTEXPR_AFTER_CXX17 void
Howard Hinnant3e519522010-05-11 19:42:165111__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5112{
5113 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
Marshall Clow7fa68652019-08-20 21:31:515114 for (difference_type __n = __last - __first; __n > 1; --__last, (void) --__n)
Arthur O'Dwyer19688042020-11-22 18:21:115115 _VSTD::__pop_heap<_Compare>(__first, __last, __comp, __n);
Howard Hinnant3e519522010-05-11 19:42:165116}
5117
5118template <class _RandomAccessIterator, class _Compare>
Arthur O'Dwyer5386aa22020-12-17 05:26:185119inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165120void
5121sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5122{
Eric Fiselieraa1cad12019-04-12 05:18:195123 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115124 _VSTD::__sort_heap<_Comp_ref>(__first, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165125}
5126
5127template <class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185128inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165129void
5130sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
5131{
Howard Hinnantce48a112011-06-30 21:18:195132 _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:165133}
5134
5135// partial_sort
5136
5137template <class _Compare, class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185138_LIBCPP_CONSTEXPR_AFTER_CXX17 void
Howard Hinnant3e519522010-05-11 19:42:165139__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5140 _Compare __comp)
5141{
Arthur O'Dwyer19688042020-11-22 18:21:115142 _VSTD::__make_heap<_Compare>(__first, __middle, __comp);
Howard Hinnant3e519522010-05-11 19:42:165143 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first;
5144 for (_RandomAccessIterator __i = __middle; __i != __last; ++__i)
5145 {
5146 if (__comp(*__i, *__first))
5147 {
5148 swap(*__i, *__first);
Arthur O'Dwyer19688042020-11-22 18:21:115149 _VSTD::__sift_down<_Compare>(__first, __middle, __comp, __len, __first);
Howard Hinnant3e519522010-05-11 19:42:165150 }
5151 }
Arthur O'Dwyer19688042020-11-22 18:21:115152 _VSTD::__sort_heap<_Compare>(__first, __middle, __comp);
Howard Hinnant3e519522010-05-11 19:42:165153}
5154
5155template <class _RandomAccessIterator, class _Compare>
Arthur O'Dwyer5386aa22020-12-17 05:26:185156inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165157void
5158partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last,
5159 _Compare __comp)
5160{
Eric Fiselieraa1cad12019-04-12 05:18:195161 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115162 _VSTD::__partial_sort<_Comp_ref>(__first, __middle, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165163}
5164
5165template <class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185166inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165167void
5168partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last)
5169{
Howard Hinnantce48a112011-06-30 21:18:195170 _VSTD::partial_sort(__first, __middle, __last,
Howard Hinnant3e519522010-05-11 19:42:165171 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5172}
5173
5174// partial_sort_copy
5175
5176template <class _Compare, class _InputIterator, class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185177_LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
Howard Hinnant3e519522010-05-11 19:42:165178__partial_sort_copy(_InputIterator __first, _InputIterator __last,
5179 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5180{
5181 _RandomAccessIterator __r = __result_first;
5182 if (__r != __result_last)
5183 {
Marshall Clow7fa68652019-08-20 21:31:515184 for (; __first != __last && __r != __result_last; ++__first, (void) ++__r)
Howard Hinnant3e519522010-05-11 19:42:165185 *__r = *__first;
Arthur O'Dwyer19688042020-11-22 18:21:115186 _VSTD::__make_heap<_Compare>(__result_first, __r, __comp);
David Majnemer8b512602014-07-22 06:07:095187 typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;
Howard Hinnant3e519522010-05-11 19:42:165188 for (; __first != __last; ++__first)
5189 if (__comp(*__first, *__result_first))
5190 {
5191 *__result_first = *__first;
Arthur O'Dwyer19688042020-11-22 18:21:115192 _VSTD::__sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first);
Howard Hinnant3e519522010-05-11 19:42:165193 }
Arthur O'Dwyer19688042020-11-22 18:21:115194 _VSTD::__sort_heap<_Compare>(__result_first, __r, __comp);
Howard Hinnant3e519522010-05-11 19:42:165195 }
5196 return __r;
5197}
5198
5199template <class _InputIterator, class _RandomAccessIterator, class _Compare>
Arthur O'Dwyer5386aa22020-12-17 05:26:185200inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165201_RandomAccessIterator
5202partial_sort_copy(_InputIterator __first, _InputIterator __last,
5203 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp)
5204{
Eric Fiselieraa1cad12019-04-12 05:18:195205 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115206 return _VSTD::__partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165207}
5208
5209template <class _InputIterator, class _RandomAccessIterator>
Arthur O'Dwyer5386aa22020-12-17 05:26:185210inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165211_RandomAccessIterator
5212partial_sort_copy(_InputIterator __first, _InputIterator __last,
5213 _RandomAccessIterator __result_first, _RandomAccessIterator __result_last)
5214{
Howard Hinnantce48a112011-06-30 21:18:195215 return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last,
Howard Hinnant3e519522010-05-11 19:42:165216 __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5217}
5218
5219// nth_element
5220
Arthur O'Dwyer207d4be2020-12-17 05:40:025221template<class _Compare, class _RandomAccessIterator>
5222_LIBCPP_CONSTEXPR_AFTER_CXX11 bool
5223__nth_element_find_guard(_RandomAccessIterator& __i, _RandomAccessIterator& __j,
5224 _RandomAccessIterator& __m, _Compare __comp)
5225{
5226 // manually guard downward moving __j against __i
5227 while (--__j != __i)
5228 {
5229 if (__comp(*__j, *__m))
5230 {
5231 return true;
5232 }
5233 }
5234 return false;
5235}
5236
5237template<class _Compare, class _RandomAccessIterator>
5238_LIBCPP_CONSTEXPR_AFTER_CXX11 bool
5239__nth_element_partloop(_RandomAccessIterator __first, _RandomAccessIterator __last,
5240 _RandomAccessIterator& __i, _RandomAccessIterator& __j,
5241 unsigned& __n_swaps, _Compare __comp)
5242{
5243 // *__first == *__m, *__m <= all other elements
5244 // Partition instead into [__first, __i) == *__first and *__first < [__i, __last)
5245 ++__i; // __first + 1
5246 __j = __last;
5247 if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1)
5248 {
5249 while (true)
5250 {
5251 if (__i == __j)
5252 return true; // [__first, __last) all equivalent elements
5253 if (__comp(*__first, *__i))
5254 {
5255 swap(*__i, *__j);
5256 ++__n_swaps;
5257 ++__i;
5258 break;
5259 }
5260 ++__i;
5261 }
5262 }
5263 // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1
5264 if (__i == __j)
5265 return true;
5266 while (true)
5267 {
5268 while (!__comp(*__first, *__i))
5269 ++__i;
5270 while (__comp(*__first, *--__j))
5271 ;
5272 if (__i >= __j)
5273 break;
5274 swap(*__i, *__j);
5275 ++__n_swaps;
5276 ++__i;
5277 }
5278 // [__first, __i) == *__first and *__first < [__i, __last)
5279 // The first part is sorted.
5280 return false;
5281}
5282
Howard Hinnant3e519522010-05-11 19:42:165283template <class _Compare, class _RandomAccessIterator>
Arthur O'Dwyer207d4be2020-12-17 05:40:025284_LIBCPP_CONSTEXPR_AFTER_CXX11 void
Howard Hinnant3e519522010-05-11 19:42:165285__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5286{
5287 // _Compare is known to be a reference type
5288 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
5289 const difference_type __limit = 7;
5290 while (true)
5291 {
Arthur O'Dwyer207d4be2020-12-17 05:40:025292 // __restart: -- this is the target of a "continue" below
Howard Hinnantb34b48192011-12-29 17:45:355293 if (__nth == __last)
5294 return;
Howard Hinnant3e519522010-05-11 19:42:165295 difference_type __len = __last - __first;
5296 switch (__len)
5297 {
5298 case 0:
5299 case 1:
5300 return;
5301 case 2:
5302 if (__comp(*--__last, *__first))
5303 swap(*__first, *__last);
5304 return;
5305 case 3:
5306 {
5307 _RandomAccessIterator __m = __first;
Howard Hinnantce48a112011-06-30 21:18:195308 _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165309 return;
5310 }
5311 }
5312 if (__len <= __limit)
5313 {
Arthur O'Dwyer19688042020-11-22 18:21:115314 _VSTD::__selection_sort<_Compare>(__first, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165315 return;
5316 }
5317 // __len > __limit >= 3
5318 _RandomAccessIterator __m = __first + __len/2;
5319 _RandomAccessIterator __lm1 = __last;
Howard Hinnantce48a112011-06-30 21:18:195320 unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp);
Howard Hinnant3e519522010-05-11 19:42:165321 // *__m is median
5322 // partition [__first, __m) < *__m and *__m <= [__m, __last)
5323 // (this inhibits tossing elements equivalent to __m around unnecessarily)
5324 _RandomAccessIterator __i = __first;
5325 _RandomAccessIterator __j = __lm1;
5326 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5327 // The search going up is known to be guarded but the search coming down isn't.
5328 // Prime the downward search with a guard.
5329 if (!__comp(*__i, *__m)) // if *__first == *__m
5330 {
5331 // *__first == *__m, *__first doesn't go in first part
Arthur O'Dwyer207d4be2020-12-17 05:40:025332 if (_VSTD::__nth_element_find_guard<_Compare>(__i, __j, __m, __comp)) {
5333 swap(*__i, *__j);
5334 ++__n_swaps;
5335 // found guard for downward moving __j, now use unguarded partition
5336 } else if (_VSTD::__nth_element_partloop<_Compare>(__first, __last, __i, __j, __n_swaps, __comp)) {
5337 return;
5338 } else if (__nth < __i) {
5339 return;
5340 } else {
5341 // __nth_element the second part
5342 // _VSTD::__nth_element<_Compare>(__i, __nth, __last, __comp);
5343 __first = __i;
5344 continue; // i.e., goto __restart
Howard Hinnant3e519522010-05-11 19:42:165345 }
5346 }
5347 ++__i;
5348 // j points beyond range to be tested, *__lm1 is known to be <= *__m
5349 // if not yet partitioned...
5350 if (__i < __j)
5351 {
5352 // known that *(__i - 1) < *__m
5353 while (true)
5354 {
5355 // __m still guards upward moving __i
5356 while (__comp(*__i, *__m))
5357 ++__i;
5358 // It is now known that a guard exists for downward moving __j
5359 while (!__comp(*--__j, *__m))
5360 ;
5361 if (__i >= __j)
5362 break;
5363 swap(*__i, *__j);
5364 ++__n_swaps;
5365 // It is known that __m != __j
5366 // If __m just moved, follow it
5367 if (__m == __i)
5368 __m = __j;
5369 ++__i;
5370 }
5371 }
5372 // [__first, __i) < *__m and *__m <= [__i, __last)
5373 if (__i != __m && __comp(*__m, *__i))
5374 {
5375 swap(*__i, *__m);
5376 ++__n_swaps;
5377 }
5378 // [__first, __i) < *__i and *__i <= [__i+1, __last)
5379 if (__nth == __i)
5380 return;
5381 if (__n_swaps == 0)
5382 {
5383 // We were given a perfectly partitioned sequence. Coincidence?
5384 if (__nth < __i)
5385 {
5386 // Check for [__first, __i) already sorted
5387 __j = __m = __first;
Arthur O'Dwyer207d4be2020-12-17 05:40:025388 while (true)
Howard Hinnant3e519522010-05-11 19:42:165389 {
Arthur O'Dwyer207d4be2020-12-17 05:40:025390 if (++__j == __i)
5391 // [__first, __i) sorted
5392 return;
Howard Hinnant3e519522010-05-11 19:42:165393 if (__comp(*__j, *__m))
5394 // not yet sorted, so sort
Arthur O'Dwyer207d4be2020-12-17 05:40:025395 break;
Howard Hinnant3e519522010-05-11 19:42:165396 __m = __j;
5397 }
Howard Hinnant3e519522010-05-11 19:42:165398 }
5399 else
5400 {
5401 // Check for [__i, __last) already sorted
5402 __j = __m = __i;
5403 while (++__j != __last)
5404 {
Arthur O'Dwyer207d4be2020-12-17 05:40:025405 if (++__j == __last)
5406 // [__i, __last) sorted
5407 return;
Howard Hinnant3e519522010-05-11 19:42:165408 if (__comp(*__j, *__m))
5409 // not yet sorted, so sort
Arthur O'Dwyer207d4be2020-12-17 05:40:025410 break;
Howard Hinnant3e519522010-05-11 19:42:165411 __m = __j;
5412 }
Howard Hinnant3e519522010-05-11 19:42:165413 }
5414 }
Howard Hinnant3e519522010-05-11 19:42:165415 // __nth_element on range containing __nth
5416 if (__nth < __i)
5417 {
Arthur O'Dwyer19688042020-11-22 18:21:115418 // _VSTD::__nth_element<_Compare>(__first, __nth, __i, __comp);
Howard Hinnant3e519522010-05-11 19:42:165419 __last = __i;
5420 }
5421 else
5422 {
Arthur O'Dwyer19688042020-11-22 18:21:115423 // _VSTD::__nth_element<_Compare>(__i+1, __nth, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165424 __first = ++__i;
5425 }
5426 }
5427}
5428
5429template <class _RandomAccessIterator, class _Compare>
Arthur O'Dwyer207d4be2020-12-17 05:40:025430inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165431void
5432nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp)
5433{
Eric Fiselieraa1cad12019-04-12 05:18:195434 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115435 _VSTD::__nth_element<_Comp_ref>(__first, __nth, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165436}
5437
5438template <class _RandomAccessIterator>
Arthur O'Dwyer207d4be2020-12-17 05:40:025439inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165440void
5441nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last)
5442{
Howard Hinnantce48a112011-06-30 21:18:195443 _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
Howard Hinnant3e519522010-05-11 19:42:165444}
5445
Arthur O'Dwyer493f1402020-12-20 20:21:425446// sort
5447
5448template <class _RandomAccessIterator, class _Compare>
5449inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5450void
5451sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
5452{
5453 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
5454 if (__libcpp_is_constant_evaluated()) {
5455 _VSTD::__partial_sort<_Comp_ref>(__first, __last, __last, _Comp_ref(__comp));
5456 } else {
5457 _VSTD::__sort<_Comp_ref>(_VSTD::__unwrap_iter(__first), _VSTD::__unwrap_iter(__last), _Comp_ref(__comp));
5458 }
5459}
5460
5461template <class _RandomAccessIterator>
5462inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
5463void
5464sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
5465{
5466 _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>());
5467}
5468
Howard Hinnant3e519522010-05-11 19:42:165469// includes
5470
5471template <class _Compare, class _InputIterator1, class _InputIterator2>
Marshall Clow8da1a482018-01-22 23:10:405472_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnant3e519522010-05-11 19:42:165473__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5474 _Compare __comp)
5475{
5476 for (; __first2 != __last2; ++__first1)
5477 {
5478 if (__first1 == __last1 || __comp(*__first2, *__first1))
5479 return false;
5480 if (!__comp(*__first1, *__first2))
5481 ++__first2;
5482 }
5483 return true;
5484}
5485
5486template <class _InputIterator1, class _InputIterator2, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:085487_LIBCPP_NODISCARD_EXT inline
5488_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165489bool
5490includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
5491 _Compare __comp)
5492{
Eric Fiselieraa1cad12019-04-12 05:18:195493 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115494 return _VSTD::__includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant3e519522010-05-11 19:42:165495}
5496
5497template <class _InputIterator1, class _InputIterator2>
Nico Weber1362d7e2019-04-03 18:13:085498_LIBCPP_NODISCARD_EXT inline
5499_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165500bool
5501includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
5502{
Howard Hinnantce48a112011-06-30 21:18:195503 return _VSTD::includes(__first1, __last1, __first2, __last2,
Howard Hinnant3e519522010-05-11 19:42:165504 __less<typename iterator_traits<_InputIterator1>::value_type,
5505 typename iterator_traits<_InputIterator2>::value_type>());
5506}
5507
5508// set_union
5509
5510template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
Arthur O'Dwyer14098cf2020-12-04 18:47:125511_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnant3e519522010-05-11 19:42:165512__set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5513 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5514{
5515 for (; __first1 != __last1; ++__result)
5516 {
5517 if (__first2 == __last2)
Howard Hinnantce48a112011-06-30 21:18:195518 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnant3e519522010-05-11 19:42:165519 if (__comp(*__first2, *__first1))
5520 {
5521 *__result = *__first2;
5522 ++__first2;
5523 }
5524 else
5525 {
Howard Hinnant3e519522010-05-11 19:42:165526 if (!__comp(*__first1, *__first2))
5527 ++__first2;
Marshall Clow05da5b02017-10-30 15:50:005528 *__result = *__first1;
Howard Hinnant3e519522010-05-11 19:42:165529 ++__first1;
5530 }
5531 }
Howard Hinnantce48a112011-06-30 21:18:195532 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnant3e519522010-05-11 19:42:165533}
5534
5535template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
Arthur O'Dwyer14098cf2020-12-04 18:47:125536inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165537_OutputIterator
5538set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5539 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5540{
Eric Fiselieraa1cad12019-04-12 05:18:195541 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115542 return _VSTD::__set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant3e519522010-05-11 19:42:165543}
5544
5545template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
Arthur O'Dwyer14098cf2020-12-04 18:47:125546inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165547_OutputIterator
5548set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5549 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5550{
Howard Hinnantce48a112011-06-30 21:18:195551 return _VSTD::set_union(__first1, __last1, __first2, __last2, __result,
Howard Hinnant3e519522010-05-11 19:42:165552 __less<typename iterator_traits<_InputIterator1>::value_type,
5553 typename iterator_traits<_InputIterator2>::value_type>());
5554}
5555
5556// set_intersection
5557
5558template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
Marshall Clow8da1a482018-01-22 23:10:405559_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnant3e519522010-05-11 19:42:165560__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5561 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5562{
5563 while (__first1 != __last1 && __first2 != __last2)
5564 {
5565 if (__comp(*__first1, *__first2))
5566 ++__first1;
5567 else
5568 {
5569 if (!__comp(*__first2, *__first1))
5570 {
5571 *__result = *__first1;
5572 ++__result;
5573 ++__first1;
5574 }
5575 ++__first2;
5576 }
5577 }
5578 return __result;
5579}
5580
5581template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
Marshall Clow8da1a482018-01-22 23:10:405582inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165583_OutputIterator
5584set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5585 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5586{
Eric Fiselieraa1cad12019-04-12 05:18:195587 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115588 return _VSTD::__set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant3e519522010-05-11 19:42:165589}
5590
5591template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
Marshall Clow8da1a482018-01-22 23:10:405592inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165593_OutputIterator
5594set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5595 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5596{
Howard Hinnantce48a112011-06-30 21:18:195597 return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result,
Howard Hinnant3e519522010-05-11 19:42:165598 __less<typename iterator_traits<_InputIterator1>::value_type,
5599 typename iterator_traits<_InputIterator2>::value_type>());
5600}
5601
5602// set_difference
5603
5604template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
Arthur O'Dwyer14098cf2020-12-04 18:47:125605_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnant3e519522010-05-11 19:42:165606__set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5607 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5608{
5609 while (__first1 != __last1)
5610 {
5611 if (__first2 == __last2)
Howard Hinnantce48a112011-06-30 21:18:195612 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnant3e519522010-05-11 19:42:165613 if (__comp(*__first1, *__first2))
5614 {
5615 *__result = *__first1;
5616 ++__result;
5617 ++__first1;
5618 }
5619 else
5620 {
5621 if (!__comp(*__first2, *__first1))
5622 ++__first1;
5623 ++__first2;
5624 }
5625 }
5626 return __result;
5627}
5628
5629template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
Arthur O'Dwyer14098cf2020-12-04 18:47:125630inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165631_OutputIterator
5632set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5633 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5634{
Eric Fiselieraa1cad12019-04-12 05:18:195635 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115636 return _VSTD::__set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant3e519522010-05-11 19:42:165637}
5638
5639template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
Arthur O'Dwyer14098cf2020-12-04 18:47:125640inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165641_OutputIterator
5642set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5643 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5644{
Howard Hinnantce48a112011-06-30 21:18:195645 return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnant3e519522010-05-11 19:42:165646 __less<typename iterator_traits<_InputIterator1>::value_type,
5647 typename iterator_traits<_InputIterator2>::value_type>());
5648}
5649
5650// set_symmetric_difference
5651
5652template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
Arthur O'Dwyer14098cf2020-12-04 18:47:125653_LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator
Howard Hinnant3e519522010-05-11 19:42:165654__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5655 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5656{
5657 while (__first1 != __last1)
5658 {
5659 if (__first2 == __last2)
Howard Hinnantce48a112011-06-30 21:18:195660 return _VSTD::copy(__first1, __last1, __result);
Howard Hinnant3e519522010-05-11 19:42:165661 if (__comp(*__first1, *__first2))
5662 {
5663 *__result = *__first1;
5664 ++__result;
5665 ++__first1;
5666 }
5667 else
5668 {
5669 if (__comp(*__first2, *__first1))
5670 {
5671 *__result = *__first2;
5672 ++__result;
5673 }
5674 else
5675 ++__first1;
5676 ++__first2;
5677 }
5678 }
Howard Hinnantce48a112011-06-30 21:18:195679 return _VSTD::copy(__first2, __last2, __result);
Howard Hinnant3e519522010-05-11 19:42:165680}
5681
5682template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
Arthur O'Dwyer14098cf2020-12-04 18:47:125683inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165684_OutputIterator
5685set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5686 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp)
5687{
Eric Fiselieraa1cad12019-04-12 05:18:195688 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115689 return _VSTD::__set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp);
Howard Hinnant3e519522010-05-11 19:42:165690}
5691
5692template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
Arthur O'Dwyer14098cf2020-12-04 18:47:125693inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165694_OutputIterator
5695set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5696 _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result)
5697{
Howard Hinnantce48a112011-06-30 21:18:195698 return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result,
Howard Hinnant3e519522010-05-11 19:42:165699 __less<typename iterator_traits<_InputIterator1>::value_type,
5700 typename iterator_traits<_InputIterator2>::value_type>());
5701}
5702
5703// lexicographical_compare
5704
5705template <class _Compare, class _InputIterator1, class _InputIterator2>
Marshall Clow1b9a4ff2018-01-22 20:44:335706_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnant3e519522010-05-11 19:42:165707__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5708 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5709{
Eric Fiselier910285b2014-10-27 19:28:205710 for (; __first2 != __last2; ++__first1, (void) ++__first2)
Howard Hinnant3e519522010-05-11 19:42:165711 {
5712 if (__first1 == __last1 || __comp(*__first1, *__first2))
5713 return true;
5714 if (__comp(*__first2, *__first1))
5715 return false;
5716 }
5717 return false;
5718}
5719
5720template <class _InputIterator1, class _InputIterator2, class _Compare>
Nico Weber1362d7e2019-04-03 18:13:085721_LIBCPP_NODISCARD_EXT inline
5722_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165723bool
5724lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5725 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
5726{
Eric Fiselieraa1cad12019-04-12 05:18:195727 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115728 return _VSTD::__lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
Howard Hinnant3e519522010-05-11 19:42:165729}
5730
5731template <class _InputIterator1, class _InputIterator2>
Nico Weber1362d7e2019-04-03 18:13:085732_LIBCPP_NODISCARD_EXT inline
5733_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165734bool
5735lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
5736 _InputIterator2 __first2, _InputIterator2 __last2)
5737{
Howard Hinnantce48a112011-06-30 21:18:195738 return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2,
Howard Hinnant3e519522010-05-11 19:42:165739 __less<typename iterator_traits<_InputIterator1>::value_type,
5740 typename iterator_traits<_InputIterator2>::value_type>());
5741}
5742
5743// next_permutation
5744
5745template <class _Compare, class _BidirectionalIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:085746_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnant3e519522010-05-11 19:42:165747__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5748{
5749 _BidirectionalIterator __i = __last;
5750 if (__first == __last || __first == --__i)
5751 return false;
5752 while (true)
5753 {
5754 _BidirectionalIterator __ip1 = __i;
5755 if (__comp(*--__i, *__ip1))
5756 {
5757 _BidirectionalIterator __j = __last;
5758 while (!__comp(*__i, *--__j))
5759 ;
5760 swap(*__i, *__j);
Howard Hinnantce48a112011-06-30 21:18:195761 _VSTD::reverse(__ip1, __last);
Howard Hinnant3e519522010-05-11 19:42:165762 return true;
5763 }
5764 if (__i == __first)
5765 {
Howard Hinnantce48a112011-06-30 21:18:195766 _VSTD::reverse(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:165767 return false;
5768 }
5769 }
5770}
5771
5772template <class _BidirectionalIterator, class _Compare>
Arthur O'Dwyerf851db32020-12-17 05:01:085773inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165774bool
5775next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5776{
Eric Fiselieraa1cad12019-04-12 05:18:195777 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115778 return _VSTD::__next_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165779}
5780
5781template <class _BidirectionalIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:085782inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantb3371f62010-08-22 00:02:435783bool
Howard Hinnant3e519522010-05-11 19:42:165784next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5785{
Howard Hinnantce48a112011-06-30 21:18:195786 return _VSTD::next_permutation(__first, __last,
Howard Hinnant3e519522010-05-11 19:42:165787 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5788}
5789
5790// prev_permutation
5791
5792template <class _Compare, class _BidirectionalIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:085793_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnant3e519522010-05-11 19:42:165794__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5795{
5796 _BidirectionalIterator __i = __last;
5797 if (__first == __last || __first == --__i)
5798 return false;
5799 while (true)
5800 {
5801 _BidirectionalIterator __ip1 = __i;
5802 if (__comp(*__ip1, *--__i))
5803 {
5804 _BidirectionalIterator __j = __last;
5805 while (!__comp(*--__j, *__i))
5806 ;
5807 swap(*__i, *__j);
Howard Hinnantce48a112011-06-30 21:18:195808 _VSTD::reverse(__ip1, __last);
Howard Hinnant3e519522010-05-11 19:42:165809 return true;
5810 }
5811 if (__i == __first)
5812 {
Howard Hinnantce48a112011-06-30 21:18:195813 _VSTD::reverse(__first, __last);
Howard Hinnant3e519522010-05-11 19:42:165814 return false;
5815 }
5816 }
5817}
5818
5819template <class _BidirectionalIterator, class _Compare>
Arthur O'Dwyerf851db32020-12-17 05:01:085820inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165821bool
5822prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp)
5823{
Eric Fiselieraa1cad12019-04-12 05:18:195824 typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
Arthur O'Dwyer19688042020-11-22 18:21:115825 return _VSTD::__prev_permutation<_Comp_ref>(__first, __last, __comp);
Howard Hinnant3e519522010-05-11 19:42:165826}
5827
5828template <class _BidirectionalIterator>
Arthur O'Dwyerf851db32020-12-17 05:01:085829inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnant3e519522010-05-11 19:42:165830bool
5831prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
5832{
Howard Hinnantce48a112011-06-30 21:18:195833 return _VSTD::prev_permutation(__first, __last,
Howard Hinnant3e519522010-05-11 19:42:165834 __less<typename iterator_traits<_BidirectionalIterator>::value_type>());
5835}
5836
Howard Hinnant3e519522010-05-11 19:42:165837_LIBCPP_END_NAMESPACE_STD
5838
Eric Fiseliera016efb2017-05-31 22:07:495839_LIBCPP_POP_MACROS
5840
Louis Dionne0a06eb92019-08-05 18:29:145841#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
Louis Dionne95689242019-08-06 21:11:245842# include <__pstl_algorithm>
Louis Dionne0a06eb92019-08-05 18:29:145843#endif
5844
Howard Hinnant3e519522010-05-11 19:42:165845#endif // _LIBCPP_ALGORITHM