]>
Commit | Line | Data |
---|---|---|
c0d88161 | 1 | // Algorithm implementation -*- C++ -*- |
42526146 | 2 | |
a5544970 | 3 | // Copyright (C) 2001-2019 Free Software Foundation, Inc. |
42526146 PE |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
42526146 PE |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
42526146 | 19 | |
748086b7 JJ |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
42526146 | 24 | |
725dc051 BK |
25 | /* |
26 | * | |
27 | * Copyright (c) 1994 | |
28 | * Hewlett-Packard Company | |
29 | * | |
30 | * Permission to use, copy, modify, distribute and sell this software | |
31 | * and its documentation for any purpose is hereby granted without fee, | |
32 | * provided that the above copyright notice appear in all copies and | |
33 | * that both that copyright notice and this permission notice appear | |
34 | * in supporting documentation. Hewlett-Packard Company makes no | |
35 | * representations about the suitability of this software for any | |
36 | * purpose. It is provided "as is" without express or implied warranty. | |
37 | * | |
38 | * | |
39 | * Copyright (c) 1996 | |
40 | * Silicon Graphics Computer Systems, Inc. | |
41 | * | |
42 | * Permission to use, copy, modify, distribute and sell this software | |
43 | * and its documentation for any purpose is hereby granted without fee, | |
44 | * provided that the above copyright notice appear in all copies and | |
45 | * that both that copyright notice and this permission notice appear | |
46 | * in supporting documentation. Silicon Graphics makes no | |
47 | * representations about the suitability of this software for any | |
48 | * purpose. It is provided "as is" without express or implied warranty. | |
49 | */ | |
50 | ||
f910786b | 51 | /** @file bits/stl_algo.h |
729e3d3f | 52 | * This is an internal header file, included by other library headers. |
f910786b | 53 | * Do not attempt to use it directly. @headername{algorithm} |
725dc051 BK |
54 | */ |
55 | ||
046d30f4 PC |
56 | #ifndef _STL_ALGO_H |
57 | #define _STL_ALGO_H 1 | |
725dc051 | 58 | |
ec494945 | 59 | #include <cstdlib> // for rand |
4f39bf5c | 60 | #include <bits/algorithmfwd.h> |
725dc051 | 61 | #include <bits/stl_heap.h> |
39b8cd70 | 62 | #include <bits/stl_tempbuf.h> // for _Temporary_buffer |
ea89b248 | 63 | #include <bits/predefined_ops.h> |
725dc051 | 64 | |
734f5023 | 65 | #if __cplusplus >= 201103L |
2944621e | 66 | #include <bits/uniform_int_dist.h> |
247d8075 PC |
67 | #endif |
68 | ||
3d7c150e | 69 | // See concept_check.h for the __glibcxx_*_requires macros. |
725dc051 | 70 | |
12ffa228 BK |
71 | namespace std _GLIBCXX_VISIBILITY(default) |
72 | { | |
73 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cbc7af0 | 74 | |
a10bad86 | 75 | /// Swaps the median value of *__a, *__b and *__c under __comp to *__result |
f9590eae CJ |
76 | template<typename _Iterator, typename _Compare> |
77 | void | |
a10bad86 CJ |
78 | __move_median_to_first(_Iterator __result,_Iterator __a, _Iterator __b, |
79 | _Iterator __c, _Compare __comp) | |
f9590eae | 80 | { |
ea89b248 | 81 | if (__comp(__a, __b)) |
f9590eae | 82 | { |
ea89b248 | 83 | if (__comp(__b, __c)) |
a10bad86 | 84 | std::iter_swap(__result, __b); |
ea89b248 | 85 | else if (__comp(__a, __c)) |
a10bad86 CJ |
86 | std::iter_swap(__result, __c); |
87 | else | |
88 | std::iter_swap(__result, __a); | |
f9590eae | 89 | } |
ea89b248 | 90 | else if (__comp(__a, __c)) |
a10bad86 | 91 | std::iter_swap(__result, __a); |
ea89b248 | 92 | else if (__comp(__b, __c)) |
a10bad86 | 93 | std::iter_swap(__result, __c); |
f9590eae | 94 | else |
a10bad86 | 95 | std::iter_swap(__result, __b); |
f9590eae CJ |
96 | } |
97 | ||
ea89b248 | 98 | /// This is an overload used by find algos for the Input Iterator case. |
08addde6 PE |
99 | template<typename _InputIterator, typename _Predicate> |
100 | inline _InputIterator | |
c1806f89 PC |
101 | __find_if(_InputIterator __first, _InputIterator __last, |
102 | _Predicate __pred, input_iterator_tag) | |
02d92e3b | 103 | { |
ea89b248 | 104 | while (__first != __last && !__pred(__first)) |
02d92e3b SW |
105 | ++__first; |
106 | return __first; | |
107 | } | |
725dc051 | 108 | |
ea89b248 | 109 | /// This is an overload used by find algos for the RAI case. |
08addde6 PE |
110 | template<typename _RandomAccessIterator, typename _Predicate> |
111 | _RandomAccessIterator | |
c1806f89 PC |
112 | __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last, |
113 | _Predicate __pred, random_access_iterator_tag) | |
02d92e3b | 114 | { |
ffa67767 PC |
115 | typename iterator_traits<_RandomAccessIterator>::difference_type |
116 | __trip_count = (__last - __first) >> 2; | |
725dc051 | 117 | |
da5a2efd | 118 | for (; __trip_count > 0; --__trip_count) |
ffa67767 | 119 | { |
ea89b248 | 120 | if (__pred(__first)) |
ffa67767 PC |
121 | return __first; |
122 | ++__first; | |
725dc051 | 123 | |
ea89b248 | 124 | if (__pred(__first)) |
ffa67767 PC |
125 | return __first; |
126 | ++__first; | |
725dc051 | 127 | |
ea89b248 | 128 | if (__pred(__first)) |
ffa67767 PC |
129 | return __first; |
130 | ++__first; | |
725dc051 | 131 | |
ea89b248 | 132 | if (__pred(__first)) |
ffa67767 PC |
133 | return __first; |
134 | ++__first; | |
135 | } | |
725dc051 | 136 | |
ffa67767 PC |
137 | switch (__last - __first) |
138 | { | |
139 | case 3: | |
ea89b248 | 140 | if (__pred(__first)) |
ffa67767 PC |
141 | return __first; |
142 | ++__first; | |
143 | case 2: | |
ea89b248 | 144 | if (__pred(__first)) |
ffa67767 PC |
145 | return __first; |
146 | ++__first; | |
147 | case 1: | |
ea89b248 | 148 | if (__pred(__first)) |
ffa67767 PC |
149 | return __first; |
150 | ++__first; | |
151 | case 0: | |
152 | default: | |
153 | return __last; | |
154 | } | |
725dc051 BK |
155 | } |
156 | ||
ea89b248 FD |
157 | template<typename _Iterator, typename _Predicate> |
158 | inline _Iterator | |
159 | __find_if(_Iterator __first, _Iterator __last, _Predicate __pred) | |
76cc1b70 | 160 | { |
ea89b248 FD |
161 | return __find_if(__first, __last, __pred, |
162 | std::__iterator_category(__first)); | |
76cc1b70 | 163 | } |
2fc9b37d JY |
164 | |
165 | /// Provided for stable_partition to use. | |
166 | template<typename _InputIterator, typename _Predicate> | |
167 | inline _InputIterator | |
168 | __find_if_not(_InputIterator __first, _InputIterator __last, | |
169 | _Predicate __pred) | |
170 | { | |
ea89b248 FD |
171 | return std::__find_if(__first, __last, |
172 | __gnu_cxx::__ops::__negate(__pred), | |
173 | std::__iterator_category(__first)); | |
2fc9b37d JY |
174 | } |
175 | ||
176 | /// Like find_if_not(), but uses and updates a count of the | |
177 | /// remaining range length instead of comparing against an end | |
178 | /// iterator. | |
179 | template<typename _InputIterator, typename _Predicate, typename _Distance> | |
180 | _InputIterator | |
181 | __find_if_not_n(_InputIterator __first, _Distance& __len, _Predicate __pred) | |
182 | { | |
d67be443 | 183 | for (; __len; --__len, (void) ++__first) |
ea89b248 | 184 | if (!__pred(__first)) |
2fc9b37d JY |
185 | break; |
186 | return __first; | |
187 | } | |
76cc1b70 | 188 | |
c2ba9709 JS |
189 | // set_difference |
190 | // set_intersection | |
191 | // set_symmetric_difference | |
192 | // set_union | |
193 | // for_each | |
194 | // find | |
195 | // find_if | |
196 | // find_first_of | |
197 | // adjacent_find | |
198 | // count | |
199 | // count_if | |
200 | // search | |
02d92e3b | 201 | |
ea89b248 FD |
202 | template<typename _ForwardIterator1, typename _ForwardIterator2, |
203 | typename _BinaryPredicate> | |
204 | _ForwardIterator1 | |
205 | __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, | |
206 | _ForwardIterator2 __first2, _ForwardIterator2 __last2, | |
207 | _BinaryPredicate __predicate) | |
02d92e3b | 208 | { |
ea89b248 FD |
209 | // Test for empty ranges |
210 | if (__first1 == __last1 || __first2 == __last2) | |
211 | return __first1; | |
02d92e3b | 212 | |
ea89b248 FD |
213 | // Test for a pattern of length 1. |
214 | _ForwardIterator2 __p1(__first2); | |
215 | if (++__p1 == __last2) | |
216 | return std::__find_if(__first1, __last1, | |
217 | __gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2)); | |
da5a2efd | 218 | |
ea89b248 FD |
219 | // General case. |
220 | _ForwardIterator2 __p; | |
221 | _ForwardIterator1 __current = __first1; | |
da5a2efd | 222 | |
ea89b248 | 223 | for (;;) |
c2ba9709 | 224 | { |
ea89b248 FD |
225 | __first1 = |
226 | std::__find_if(__first1, __last1, | |
227 | __gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2)); | |
228 | ||
229 | if (__first1 == __last1) | |
230 | return __last1; | |
231 | ||
232 | __p = __p1; | |
233 | __current = __first1; | |
234 | if (++__current == __last1) | |
235 | return __last1; | |
236 | ||
237 | while (__predicate(__current, __p)) | |
c2ba9709 | 238 | { |
ea89b248 FD |
239 | if (++__p == __last2) |
240 | return __first1; | |
241 | if (++__current == __last1) | |
242 | return __last1; | |
c2ba9709 | 243 | } |
ea89b248 | 244 | ++__first1; |
c2ba9709 | 245 | } |
ea89b248 | 246 | return __first1; |
da5a2efd PC |
247 | } |
248 | ||
c2ba9709 JS |
249 | // search_n |
250 | ||
c0d88161 | 251 | /** |
ea89b248 | 252 | * This is an helper function for search_n overloaded for forward iterators. |
c0d88161 | 253 | */ |
ea89b248 FD |
254 | template<typename _ForwardIterator, typename _Integer, |
255 | typename _UnaryPredicate> | |
08addde6 | 256 | _ForwardIterator |
ea89b248 FD |
257 | __search_n_aux(_ForwardIterator __first, _ForwardIterator __last, |
258 | _Integer __count, _UnaryPredicate __unary_pred, | |
259 | std::forward_iterator_tag) | |
02d92e3b | 260 | { |
ea89b248 | 261 | __first = std::__find_if(__first, __last, __unary_pred); |
c2ba9709 | 262 | while (__first != __last) |
ffa67767 | 263 | { |
c2ba9709 JS |
264 | typename iterator_traits<_ForwardIterator>::difference_type |
265 | __n = __count; | |
266 | _ForwardIterator __i = __first; | |
267 | ++__i; | |
ea89b248 | 268 | while (__i != __last && __n != 1 && __unary_pred(__i)) |
c2ba9709 JS |
269 | { |
270 | ++__i; | |
271 | --__n; | |
272 | } | |
273 | if (__n == 1) | |
ffa67767 | 274 | return __first; |
c2ba9709 JS |
275 | if (__i == __last) |
276 | return __last; | |
ea89b248 | 277 | __first = std::__find_if(++__i, __last, __unary_pred); |
ffa67767 | 278 | } |
02d92e3b SW |
279 | return __last; |
280 | } | |
281 | ||
c0d88161 | 282 | /** |
ea89b248 FD |
283 | * This is an helper function for search_n overloaded for random access |
284 | * iterators. | |
c2ba9709 | 285 | */ |
ea89b248 FD |
286 | template<typename _RandomAccessIter, typename _Integer, |
287 | typename _UnaryPredicate> | |
c2ba9709 | 288 | _RandomAccessIter |
ea89b248 FD |
289 | __search_n_aux(_RandomAccessIter __first, _RandomAccessIter __last, |
290 | _Integer __count, _UnaryPredicate __unary_pred, | |
291 | std::random_access_iterator_tag) | |
02d92e3b | 292 | { |
c2ba9709 JS |
293 | typedef typename std::iterator_traits<_RandomAccessIter>::difference_type |
294 | _DistanceType; | |
295 | ||
296 | _DistanceType __tailSize = __last - __first; | |
4b47d655 | 297 | _DistanceType __remainder = __count; |
c2ba9709 | 298 | |
4b47d655 | 299 | while (__remainder <= __tailSize) // the main loop... |
ffa67767 | 300 | { |
4b47d655 MK |
301 | __first += __remainder; |
302 | __tailSize -= __remainder; | |
303 | // __first here is always pointing to one past the last element of | |
304 | // next possible match. | |
305 | _RandomAccessIter __backTrack = __first; | |
ea89b248 | 306 | while (__unary_pred(--__backTrack)) |
c2ba9709 JS |
307 | { |
308 | if (--__remainder == 0) | |
ec494945 | 309 | return (__first - __count); // Success |
c2ba9709 | 310 | } |
4b47d655 | 311 | __remainder = __count + 1 - (__first - __backTrack); |
ffa67767 | 312 | } |
4b47d655 | 313 | return __last; // Failure |
02d92e3b SW |
314 | } |
315 | ||
ea89b248 | 316 | template<typename _ForwardIterator, typename _Integer, |
ec494945 | 317 | typename _UnaryPredicate> |
ea89b248 FD |
318 | _ForwardIterator |
319 | __search_n(_ForwardIterator __first, _ForwardIterator __last, | |
320 | _Integer __count, | |
321 | _UnaryPredicate __unary_pred) | |
02d92e3b | 322 | { |
ea89b248 FD |
323 | if (__count <= 0) |
324 | return __first; | |
325 | ||
326 | if (__count == 1) | |
327 | return std::__find_if(__first, __last, __unary_pred); | |
328 | ||
329 | return std::__search_n_aux(__first, __last, __count, __unary_pred, | |
330 | std::__iterator_category(__first)); | |
02d92e3b SW |
331 | } |
332 | ||
ea89b248 | 333 | // find_end for forward iterators. |
c2ba9709 JS |
334 | template<typename _ForwardIterator1, typename _ForwardIterator2, |
335 | typename _BinaryPredicate> | |
336 | _ForwardIterator1 | |
337 | __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, | |
338 | _ForwardIterator2 __first2, _ForwardIterator2 __last2, | |
339 | forward_iterator_tag, forward_iterator_tag, | |
340 | _BinaryPredicate __comp) | |
02d92e3b | 341 | { |
c2ba9709 JS |
342 | if (__first2 == __last2) |
343 | return __last1; | |
ea89b248 FD |
344 | |
345 | _ForwardIterator1 __result = __last1; | |
346 | while (1) | |
c2ba9709 | 347 | { |
ea89b248 FD |
348 | _ForwardIterator1 __new_result |
349 | = std::__search(__first1, __last1, __first2, __last2, __comp); | |
350 | if (__new_result == __last1) | |
351 | return __result; | |
352 | else | |
c2ba9709 | 353 | { |
ea89b248 FD |
354 | __result = __new_result; |
355 | __first1 = __new_result; | |
356 | ++__first1; | |
c2ba9709 JS |
357 | } |
358 | } | |
02d92e3b SW |
359 | } |
360 | ||
c2ba9709 | 361 | // find_end for bidirectional iterators (much faster). |
c2ba9709 | 362 | template<typename _BidirectionalIterator1, typename _BidirectionalIterator2, |
1d1c53d5 | 363 | typename _BinaryPredicate> |
c2ba9709 JS |
364 | _BidirectionalIterator1 |
365 | __find_end(_BidirectionalIterator1 __first1, | |
366 | _BidirectionalIterator1 __last1, | |
367 | _BidirectionalIterator2 __first2, | |
368 | _BidirectionalIterator2 __last2, | |
369 | bidirectional_iterator_tag, bidirectional_iterator_tag, | |
370 | _BinaryPredicate __comp) | |
1d1c53d5 PC |
371 | { |
372 | // concept requirements | |
c2ba9709 JS |
373 | __glibcxx_function_requires(_BidirectionalIteratorConcept< |
374 | _BidirectionalIterator1>) | |
375 | __glibcxx_function_requires(_BidirectionalIteratorConcept< | |
376 | _BidirectionalIterator2>) | |
1d1c53d5 | 377 | |
c2ba9709 JS |
378 | typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1; |
379 | typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2; | |
380 | ||
381 | _RevIterator1 __rlast1(__first1); | |
382 | _RevIterator2 __rlast2(__first2); | |
ea89b248 FD |
383 | _RevIterator1 __rresult = std::__search(_RevIterator1(__last1), __rlast1, |
384 | _RevIterator2(__last2), __rlast2, | |
385 | __comp); | |
c2ba9709 JS |
386 | |
387 | if (__rresult == __rlast1) | |
388 | return __last1; | |
389 | else | |
390 | { | |
391 | _BidirectionalIterator1 __result = __rresult.base(); | |
392 | std::advance(__result, -std::distance(__first2, __last2)); | |
393 | return __result; | |
394 | } | |
1d1c53d5 PC |
395 | } |
396 | ||
c0d88161 | 397 | /** |
c2ba9709 | 398 | * @brief Find last matching subsequence in a sequence. |
5b9daa7e | 399 | * @ingroup non_mutating_algorithms |
93c66bc6 BK |
400 | * @param __first1 Start of range to search. |
401 | * @param __last1 End of range to search. | |
402 | * @param __first2 Start of sequence to match. | |
403 | * @param __last2 End of sequence to match. | |
c2ba9709 | 404 | * @return The last iterator @c i in the range |
93c66bc6 BK |
405 | * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == |
406 | * @p *(__first2+N) for each @c N in the range @p | |
407 | * [0,__last2-__first2), or @p __last1 if no such iterator exists. | |
c0d88161 | 408 | * |
93c66bc6 BK |
409 | * Searches the range @p [__first1,__last1) for a sub-sequence that |
410 | * compares equal value-by-value with the sequence given by @p | |
411 | * [__first2,__last2) and returns an iterator to the __first | |
412 | * element of the sub-sequence, or @p __last1 if the sub-sequence | |
413 | * is not found. The sub-sequence will be the last such | |
ea89b248 | 414 | * subsequence contained in [__first1,__last1). |
c0d88161 | 415 | * |
93c66bc6 BK |
416 | * Because the sub-sequence must lie completely within the range @p |
417 | * [__first1,__last1) it must start at a position less than @p | |
418 | * __last1-(__last2-__first2) where @p __last2-__first2 is the | |
419 | * length of the sub-sequence. This means that the returned | |
420 | * iterator @c i will be in the range @p | |
421 | * [__first1,__last1-(__last2-__first2)) | |
c0d88161 | 422 | */ |
08addde6 | 423 | template<typename _ForwardIterator1, typename _ForwardIterator2> |
c2ba9709 JS |
424 | inline _ForwardIterator1 |
425 | find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, | |
426 | _ForwardIterator2 __first2, _ForwardIterator2 __last2) | |
02d92e3b SW |
427 | { |
428 | // concept requirements | |
3d7c150e BK |
429 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) |
430 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) | |
431 | __glibcxx_function_requires(_EqualOpConcept< | |
08addde6 PE |
432 | typename iterator_traits<_ForwardIterator1>::value_type, |
433 | typename iterator_traits<_ForwardIterator2>::value_type>) | |
285b36d6 BK |
434 | __glibcxx_requires_valid_range(__first1, __last1); |
435 | __glibcxx_requires_valid_range(__first2, __last2); | |
58b9f26a | 436 | |
c2ba9709 JS |
437 | return std::__find_end(__first1, __last1, __first2, __last2, |
438 | std::__iterator_category(__first1), | |
ea89b248 FD |
439 | std::__iterator_category(__first2), |
440 | __gnu_cxx::__ops::__iter_equal_to_iter()); | |
02d92e3b SW |
441 | } |
442 | ||
c0d88161 | 443 | /** |
c2ba9709 | 444 | * @brief Find last matching subsequence in a sequence using a predicate. |
5b9daa7e | 445 | * @ingroup non_mutating_algorithms |
93c66bc6 BK |
446 | * @param __first1 Start of range to search. |
447 | * @param __last1 End of range to search. | |
448 | * @param __first2 Start of sequence to match. | |
449 | * @param __last2 End of sequence to match. | |
450 | * @param __comp The predicate to use. | |
451 | * @return The last iterator @c i in the range @p | |
452 | * [__first1,__last1-(__last2-__first2)) such that @c | |
453 | * predicate(*(i+N), @p (__first2+N)) is true for each @c N in the | |
454 | * range @p [0,__last2-__first2), or @p __last1 if no such iterator | |
455 | * exists. | |
c0d88161 | 456 | * |
93c66bc6 BK |
457 | * Searches the range @p [__first1,__last1) for a sub-sequence that |
458 | * compares equal value-by-value with the sequence given by @p | |
459 | * [__first2,__last2) using comp as a predicate and returns an | |
460 | * iterator to the first element of the sub-sequence, or @p __last1 | |
461 | * if the sub-sequence is not found. The sub-sequence will be the | |
462 | * last such subsequence contained in [__first,__last1). | |
c0d88161 | 463 | * |
93c66bc6 BK |
464 | * Because the sub-sequence must lie completely within the range @p |
465 | * [__first1,__last1) it must start at a position less than @p | |
466 | * __last1-(__last2-__first2) where @p __last2-__first2 is the | |
467 | * length of the sub-sequence. This means that the returned | |
468 | * iterator @c i will be in the range @p | |
469 | * [__first1,__last1-(__last2-__first2)) | |
c0d88161 | 470 | */ |
ffa67767 PC |
471 | template<typename _ForwardIterator1, typename _ForwardIterator2, |
472 | typename _BinaryPredicate> | |
c2ba9709 JS |
473 | inline _ForwardIterator1 |
474 | find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, | |
475 | _ForwardIterator2 __first2, _ForwardIterator2 __last2, | |
476 | _BinaryPredicate __comp) | |
02d92e3b SW |
477 | { |
478 | // concept requirements | |
3d7c150e BK |
479 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) |
480 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) | |
481 | __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, | |
08addde6 PE |
482 | typename iterator_traits<_ForwardIterator1>::value_type, |
483 | typename iterator_traits<_ForwardIterator2>::value_type>) | |
285b36d6 BK |
484 | __glibcxx_requires_valid_range(__first1, __last1); |
485 | __glibcxx_requires_valid_range(__first2, __last2); | |
02d92e3b | 486 | |
c2ba9709 JS |
487 | return std::__find_end(__first1, __last1, __first2, __last2, |
488 | std::__iterator_category(__first1), | |
489 | std::__iterator_category(__first2), | |
ea89b248 | 490 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
c2ba9709 | 491 | } |
725dc051 | 492 | |
734f5023 | 493 | #if __cplusplus >= 201103L |
76cc1b70 | 494 | /** |
04dbd891 PC |
495 | * @brief Checks that a predicate is true for all the elements |
496 | * of a sequence. | |
5b9daa7e | 497 | * @ingroup non_mutating_algorithms |
93c66bc6 BK |
498 | * @param __first An input iterator. |
499 | * @param __last An input iterator. | |
500 | * @param __pred A predicate. | |
76cc1b70 PC |
501 | * @return True if the check is true, false otherwise. |
502 | * | |
93c66bc6 BK |
503 | * Returns true if @p __pred is true for each element in the range |
504 | * @p [__first,__last), and false otherwise. | |
76cc1b70 PC |
505 | */ |
506 | template<typename _InputIterator, typename _Predicate> | |
507 | inline bool | |
508 | all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) | |
509 | { return __last == std::find_if_not(__first, __last, __pred); } | |
510 | ||
511 | /** | |
04dbd891 PC |
512 | * @brief Checks that a predicate is false for all the elements |
513 | * of a sequence. | |
5b9daa7e | 514 | * @ingroup non_mutating_algorithms |
93c66bc6 BK |
515 | * @param __first An input iterator. |
516 | * @param __last An input iterator. | |
517 | * @param __pred A predicate. | |
76cc1b70 PC |
518 | * @return True if the check is true, false otherwise. |
519 | * | |
93c66bc6 BK |
520 | * Returns true if @p __pred is false for each element in the range |
521 | * @p [__first,__last), and false otherwise. | |
76cc1b70 PC |
522 | */ |
523 | template<typename _InputIterator, typename _Predicate> | |
524 | inline bool | |
525 | none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) | |
12ffa228 | 526 | { return __last == _GLIBCXX_STD_A::find_if(__first, __last, __pred); } |
76cc1b70 PC |
527 | |
528 | /** | |
04dbd891 PC |
529 | * @brief Checks that a predicate is false for at least an element |
530 | * of a sequence. | |
5b9daa7e | 531 | * @ingroup non_mutating_algorithms |
93c66bc6 BK |
532 | * @param __first An input iterator. |
533 | * @param __last An input iterator. | |
534 | * @param __pred A predicate. | |
76cc1b70 PC |
535 | * @return True if the check is true, false otherwise. |
536 | * | |
93c66bc6 BK |
537 | * Returns true if an element exists in the range @p |
538 | * [__first,__last) such that @p __pred is true, and false | |
539 | * otherwise. | |
76cc1b70 PC |
540 | */ |
541 | template<typename _InputIterator, typename _Predicate> | |
542 | inline bool | |
543 | any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) | |
544 | { return !std::none_of(__first, __last, __pred); } | |
545 | ||
546 | /** | |
04dbd891 PC |
547 | * @brief Find the first element in a sequence for which a |
548 | * predicate is false. | |
5b9daa7e | 549 | * @ingroup non_mutating_algorithms |
93c66bc6 BK |
550 | * @param __first An input iterator. |
551 | * @param __last An input iterator. | |
552 | * @param __pred A predicate. | |
553 | * @return The first iterator @c i in the range @p [__first,__last) | |
554 | * such that @p __pred(*i) is false, or @p __last if no such iterator exists. | |
76cc1b70 PC |
555 | */ |
556 | template<typename _InputIterator, typename _Predicate> | |
557 | inline _InputIterator | |
558 | find_if_not(_InputIterator __first, _InputIterator __last, | |
559 | _Predicate __pred) | |
560 | { | |
561 | // concept requirements | |
562 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) | |
563 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, | |
564 | typename iterator_traits<_InputIterator>::value_type>) | |
565 | __glibcxx_requires_valid_range(__first, __last); | |
ea89b248 FD |
566 | return std::__find_if_not(__first, __last, |
567 | __gnu_cxx::__ops::__pred_iter(__pred)); | |
76cc1b70 | 568 | } |
04dbd891 PC |
569 | |
570 | /** | |
571 | * @brief Checks whether the sequence is partitioned. | |
5b9daa7e | 572 | * @ingroup mutating_algorithms |
93c66bc6 BK |
573 | * @param __first An input iterator. |
574 | * @param __last An input iterator. | |
575 | * @param __pred A predicate. | |
576 | * @return True if the range @p [__first,__last) is partioned by @p __pred, | |
577 | * i.e. if all elements that satisfy @p __pred appear before those that | |
04dbd891 PC |
578 | * do not. |
579 | */ | |
580 | template<typename _InputIterator, typename _Predicate> | |
581 | inline bool | |
582 | is_partitioned(_InputIterator __first, _InputIterator __last, | |
583 | _Predicate __pred) | |
584 | { | |
585 | __first = std::find_if_not(__first, __last, __pred); | |
c4e5baa6 JW |
586 | if (__first == __last) |
587 | return true; | |
5b4ca306 | 588 | ++__first; |
04dbd891 PC |
589 | return std::none_of(__first, __last, __pred); |
590 | } | |
d9be9bb3 PC |
591 | |
592 | /** | |
593 | * @brief Find the partition point of a partitioned range. | |
5b9daa7e | 594 | * @ingroup mutating_algorithms |
93c66bc6 BK |
595 | * @param __first An iterator. |
596 | * @param __last Another iterator. | |
597 | * @param __pred A predicate. | |
598 | * @return An iterator @p mid such that @p all_of(__first, mid, __pred) | |
599 | * and @p none_of(mid, __last, __pred) are both true. | |
d9be9bb3 PC |
600 | */ |
601 | template<typename _ForwardIterator, typename _Predicate> | |
602 | _ForwardIterator | |
603 | partition_point(_ForwardIterator __first, _ForwardIterator __last, | |
604 | _Predicate __pred) | |
605 | { | |
606 | // concept requirements | |
607 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
608 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, | |
609 | typename iterator_traits<_ForwardIterator>::value_type>) | |
610 | ||
611 | // A specific debug-mode test will be necessary... | |
612 | __glibcxx_requires_valid_range(__first, __last); | |
613 | ||
614 | typedef typename iterator_traits<_ForwardIterator>::difference_type | |
615 | _DistanceType; | |
616 | ||
617 | _DistanceType __len = std::distance(__first, __last); | |
618 | _DistanceType __half; | |
619 | _ForwardIterator __middle; | |
620 | ||
621 | while (__len > 0) | |
622 | { | |
623 | __half = __len >> 1; | |
624 | __middle = __first; | |
625 | std::advance(__middle, __half); | |
626 | if (__pred(*__middle)) | |
627 | { | |
628 | __first = __middle; | |
629 | ++__first; | |
630 | __len = __len - __half - 1; | |
631 | } | |
632 | else | |
633 | __len = __half; | |
634 | } | |
635 | return __first; | |
636 | } | |
76cc1b70 PC |
637 | #endif |
638 | ||
ea89b248 FD |
639 | template<typename _InputIterator, typename _OutputIterator, |
640 | typename _Predicate> | |
641 | _OutputIterator | |
642 | __remove_copy_if(_InputIterator __first, _InputIterator __last, | |
643 | _OutputIterator __result, _Predicate __pred) | |
644 | { | |
645 | for (; __first != __last; ++__first) | |
646 | if (!__pred(__first)) | |
647 | { | |
648 | *__result = *__first; | |
649 | ++__result; | |
650 | } | |
651 | return __result; | |
652 | } | |
653 | ||
654 | /** | |
655 | * @brief Copy a sequence, removing elements of a given value. | |
656 | * @ingroup mutating_algorithms | |
657 | * @param __first An input iterator. | |
658 | * @param __last An input iterator. | |
93c66bc6 BK |
659 | * @param __result An output iterator. |
660 | * @param __value The value to be removed. | |
c2ba9709 JS |
661 | * @return An iterator designating the end of the resulting sequence. |
662 | * | |
93c66bc6 BK |
663 | * Copies each element in the range @p [__first,__last) not equal |
664 | * to @p __value to the range beginning at @p __result. | |
665 | * remove_copy() is stable, so the relative order of elements that | |
666 | * are copied is unchanged. | |
c2ba9709 JS |
667 | */ |
668 | template<typename _InputIterator, typename _OutputIterator, typename _Tp> | |
3bd2644c | 669 | inline _OutputIterator |
c2ba9709 JS |
670 | remove_copy(_InputIterator __first, _InputIterator __last, |
671 | _OutputIterator __result, const _Tp& __value) | |
672 | { | |
673 | // concept requirements | |
674 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) | |
675 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
676 | typename iterator_traits<_InputIterator>::value_type>) | |
677 | __glibcxx_function_requires(_EqualOpConcept< | |
678 | typename iterator_traits<_InputIterator>::value_type, _Tp>) | |
679 | __glibcxx_requires_valid_range(__first, __last); | |
02d92e3b | 680 | |
ea89b248 FD |
681 | return std::__remove_copy_if(__first, __last, __result, |
682 | __gnu_cxx::__ops::__iter_equals_val(__value)); | |
725dc051 | 683 | } |
725dc051 | 684 | |
b35c0822 | 685 | /** |
c2ba9709 | 686 | * @brief Copy a sequence, removing elements for which a predicate is true. |
5b9daa7e | 687 | * @ingroup mutating_algorithms |
93c66bc6 BK |
688 | * @param __first An input iterator. |
689 | * @param __last An input iterator. | |
690 | * @param __result An output iterator. | |
691 | * @param __pred A predicate. | |
c2ba9709 JS |
692 | * @return An iterator designating the end of the resulting sequence. |
693 | * | |
93c66bc6 BK |
694 | * Copies each element in the range @p [__first,__last) for which |
695 | * @p __pred returns false to the range beginning at @p __result. | |
c2ba9709 JS |
696 | * |
697 | * remove_copy_if() is stable, so the relative order of elements that are | |
698 | * copied is unchanged. | |
b35c0822 | 699 | */ |
c2ba9709 JS |
700 | template<typename _InputIterator, typename _OutputIterator, |
701 | typename _Predicate> | |
3bd2644c | 702 | inline _OutputIterator |
c2ba9709 JS |
703 | remove_copy_if(_InputIterator __first, _InputIterator __last, |
704 | _OutputIterator __result, _Predicate __pred) | |
b35c0822 | 705 | { |
c2ba9709 JS |
706 | // concept requirements |
707 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) | |
708 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
709 | typename iterator_traits<_InputIterator>::value_type>) | |
710 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, | |
711 | typename iterator_traits<_InputIterator>::value_type>) | |
712 | __glibcxx_requires_valid_range(__first, __last); | |
713 | ||
ea89b248 FD |
714 | return std::__remove_copy_if(__first, __last, __result, |
715 | __gnu_cxx::__ops::__pred_iter(__pred)); | |
b35c0822 PC |
716 | } |
717 | ||
734f5023 | 718 | #if __cplusplus >= 201103L |
a057a4f1 PC |
719 | /** |
720 | * @brief Copy the elements of a sequence for which a predicate is true. | |
5b9daa7e | 721 | * @ingroup mutating_algorithms |
93c66bc6 BK |
722 | * @param __first An input iterator. |
723 | * @param __last An input iterator. | |
724 | * @param __result An output iterator. | |
725 | * @param __pred A predicate. | |
a057a4f1 PC |
726 | * @return An iterator designating the end of the resulting sequence. |
727 | * | |
93c66bc6 BK |
728 | * Copies each element in the range @p [__first,__last) for which |
729 | * @p __pred returns true to the range beginning at @p __result. | |
a057a4f1 PC |
730 | * |
731 | * copy_if() is stable, so the relative order of elements that are | |
732 | * copied is unchanged. | |
733 | */ | |
734 | template<typename _InputIterator, typename _OutputIterator, | |
735 | typename _Predicate> | |
736 | _OutputIterator | |
737 | copy_if(_InputIterator __first, _InputIterator __last, | |
738 | _OutputIterator __result, _Predicate __pred) | |
739 | { | |
740 | // concept requirements | |
741 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) | |
742 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
743 | typename iterator_traits<_InputIterator>::value_type>) | |
744 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, | |
745 | typename iterator_traits<_InputIterator>::value_type>) | |
746 | __glibcxx_requires_valid_range(__first, __last); | |
747 | ||
748 | for (; __first != __last; ++__first) | |
749 | if (__pred(*__first)) | |
750 | { | |
751 | *__result = *__first; | |
752 | ++__result; | |
753 | } | |
754 | return __result; | |
755 | } | |
688a7a07 | 756 | |
b0371776 PC |
757 | template<typename _InputIterator, typename _Size, typename _OutputIterator> |
758 | _OutputIterator | |
759 | __copy_n(_InputIterator __first, _Size __n, | |
760 | _OutputIterator __result, input_iterator_tag) | |
761 | { | |
6ba30237 | 762 | if (__n > 0) |
b0371776 | 763 | { |
6ba30237 PC |
764 | while (true) |
765 | { | |
766 | *__result = *__first; | |
767 | ++__result; | |
768 | if (--__n > 0) | |
769 | ++__first; | |
770 | else | |
771 | break; | |
772 | } | |
b0371776 PC |
773 | } |
774 | return __result; | |
775 | } | |
776 | ||
777 | template<typename _RandomAccessIterator, typename _Size, | |
778 | typename _OutputIterator> | |
779 | inline _OutputIterator | |
780 | __copy_n(_RandomAccessIterator __first, _Size __n, | |
781 | _OutputIterator __result, random_access_iterator_tag) | |
782 | { return std::copy(__first, __first + __n, __result); } | |
783 | ||
784 | /** | |
785 | * @brief Copies the range [first,first+n) into [result,result+n). | |
5b9daa7e | 786 | * @ingroup mutating_algorithms |
93c66bc6 BK |
787 | * @param __first An input iterator. |
788 | * @param __n The number of elements to copy. | |
789 | * @param __result An output iterator. | |
b0371776 PC |
790 | * @return result+n. |
791 | * | |
792 | * This inline function will boil down to a call to @c memmove whenever | |
793 | * possible. Failing that, if random access iterators are passed, then the | |
794 | * loop count will be known (and therefore a candidate for compiler | |
795 | * optimizations such as unrolling). | |
796 | */ | |
797 | template<typename _InputIterator, typename _Size, typename _OutputIterator> | |
798 | inline _OutputIterator | |
799 | copy_n(_InputIterator __first, _Size __n, _OutputIterator __result) | |
800 | { | |
801 | // concept requirements | |
802 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) | |
803 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
804 | typename iterator_traits<_InputIterator>::value_type>) | |
805 | ||
806 | return std::__copy_n(__first, __n, __result, | |
807 | std::__iterator_category(__first)); | |
808 | } | |
809 | ||
688a7a07 PC |
810 | /** |
811 | * @brief Copy the elements of a sequence to separate output sequences | |
812 | * depending on the truth value of a predicate. | |
5b9daa7e | 813 | * @ingroup mutating_algorithms |
93c66bc6 BK |
814 | * @param __first An input iterator. |
815 | * @param __last An input iterator. | |
816 | * @param __out_true An output iterator. | |
817 | * @param __out_false An output iterator. | |
818 | * @param __pred A predicate. | |
688a7a07 PC |
819 | * @return A pair designating the ends of the resulting sequences. |
820 | * | |
93c66bc6 BK |
821 | * Copies each element in the range @p [__first,__last) for which |
822 | * @p __pred returns true to the range beginning at @p out_true | |
823 | * and each element for which @p __pred returns false to @p __out_false. | |
688a7a07 PC |
824 | */ |
825 | template<typename _InputIterator, typename _OutputIterator1, | |
826 | typename _OutputIterator2, typename _Predicate> | |
827 | pair<_OutputIterator1, _OutputIterator2> | |
828 | partition_copy(_InputIterator __first, _InputIterator __last, | |
829 | _OutputIterator1 __out_true, _OutputIterator2 __out_false, | |
830 | _Predicate __pred) | |
831 | { | |
832 | // concept requirements | |
833 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) | |
834 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator1, | |
835 | typename iterator_traits<_InputIterator>::value_type>) | |
836 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator2, | |
837 | typename iterator_traits<_InputIterator>::value_type>) | |
838 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, | |
839 | typename iterator_traits<_InputIterator>::value_type>) | |
840 | __glibcxx_requires_valid_range(__first, __last); | |
841 | ||
842 | for (; __first != __last; ++__first) | |
843 | if (__pred(*__first)) | |
844 | { | |
845 | *__out_true = *__first; | |
846 | ++__out_true; | |
847 | } | |
848 | else | |
849 | { | |
850 | *__out_false = *__first; | |
851 | ++__out_false; | |
852 | } | |
853 | ||
854 | return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false); | |
855 | } | |
a057a4f1 PC |
856 | #endif |
857 | ||
ea89b248 FD |
858 | template<typename _ForwardIterator, typename _Predicate> |
859 | _ForwardIterator | |
860 | __remove_if(_ForwardIterator __first, _ForwardIterator __last, | |
861 | _Predicate __pred) | |
862 | { | |
863 | __first = std::__find_if(__first, __last, __pred); | |
864 | if (__first == __last) | |
ec494945 | 865 | return __first; |
ea89b248 FD |
866 | _ForwardIterator __result = __first; |
867 | ++__first; | |
868 | for (; __first != __last; ++__first) | |
ec494945 FD |
869 | if (!__pred(__first)) |
870 | { | |
871 | *__result = _GLIBCXX_MOVE(*__first); | |
872 | ++__result; | |
873 | } | |
ea89b248 FD |
874 | return __result; |
875 | } | |
876 | ||
b35c0822 | 877 | /** |
c2ba9709 | 878 | * @brief Remove elements from a sequence. |
5b9daa7e | 879 | * @ingroup mutating_algorithms |
93c66bc6 BK |
880 | * @param __first An input iterator. |
881 | * @param __last An input iterator. | |
882 | * @param __value The value to be removed. | |
c2ba9709 JS |
883 | * @return An iterator designating the end of the resulting sequence. |
884 | * | |
93c66bc6 BK |
885 | * All elements equal to @p __value are removed from the range |
886 | * @p [__first,__last). | |
c2ba9709 JS |
887 | * |
888 | * remove() is stable, so the relative order of elements that are | |
889 | * not removed is unchanged. | |
890 | * | |
93c66bc6 | 891 | * Elements between the end of the resulting sequence and @p __last |
c2ba9709 | 892 | * are still present, but their value is unspecified. |
b35c0822 | 893 | */ |
c2ba9709 | 894 | template<typename _ForwardIterator, typename _Tp> |
ea89b248 | 895 | inline _ForwardIterator |
c2ba9709 JS |
896 | remove(_ForwardIterator __first, _ForwardIterator __last, |
897 | const _Tp& __value) | |
b35c0822 | 898 | { |
c2ba9709 JS |
899 | // concept requirements |
900 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< | |
901 | _ForwardIterator>) | |
902 | __glibcxx_function_requires(_EqualOpConcept< | |
903 | typename iterator_traits<_ForwardIterator>::value_type, _Tp>) | |
904 | __glibcxx_requires_valid_range(__first, __last); | |
b35c0822 | 905 | |
ea89b248 FD |
906 | return std::__remove_if(__first, __last, |
907 | __gnu_cxx::__ops::__iter_equals_val(__value)); | |
c2ba9709 | 908 | } |
b35c0822 | 909 | |
c0d88161 | 910 | /** |
c2ba9709 | 911 | * @brief Remove elements from a sequence using a predicate. |
5b9daa7e | 912 | * @ingroup mutating_algorithms |
93c66bc6 BK |
913 | * @param __first A forward iterator. |
914 | * @param __last A forward iterator. | |
915 | * @param __pred A predicate. | |
c2ba9709 | 916 | * @return An iterator designating the end of the resulting sequence. |
c0d88161 | 917 | * |
93c66bc6 BK |
918 | * All elements for which @p __pred returns true are removed from the range |
919 | * @p [__first,__last). | |
c2ba9709 JS |
920 | * |
921 | * remove_if() is stable, so the relative order of elements that are | |
922 | * not removed is unchanged. | |
923 | * | |
93c66bc6 | 924 | * Elements between the end of the resulting sequence and @p __last |
c2ba9709 | 925 | * are still present, but their value is unspecified. |
c0d88161 | 926 | */ |
c2ba9709 | 927 | template<typename _ForwardIterator, typename _Predicate> |
ea89b248 | 928 | inline _ForwardIterator |
c2ba9709 JS |
929 | remove_if(_ForwardIterator __first, _ForwardIterator __last, |
930 | _Predicate __pred) | |
02d92e3b SW |
931 | { |
932 | // concept requirements | |
c2ba9709 JS |
933 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
934 | _ForwardIterator>) | |
935 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, | |
936 | typename iterator_traits<_ForwardIterator>::value_type>) | |
285b36d6 | 937 | __glibcxx_requires_valid_range(__first, __last); |
02d92e3b | 938 | |
ea89b248 FD |
939 | return std::__remove_if(__first, __last, |
940 | __gnu_cxx::__ops::__pred_iter(__pred)); | |
941 | } | |
942 | ||
943 | template<typename _ForwardIterator, typename _BinaryPredicate> | |
944 | _ForwardIterator | |
945 | __adjacent_find(_ForwardIterator __first, _ForwardIterator __last, | |
946 | _BinaryPredicate __binary_pred) | |
947 | { | |
948 | if (__first == __last) | |
949 | return __last; | |
950 | _ForwardIterator __next = __first; | |
951 | while (++__next != __last) | |
952 | { | |
953 | if (__binary_pred(__first, __next)) | |
954 | return __first; | |
955 | __first = __next; | |
956 | } | |
957 | return __last; | |
958 | } | |
959 | ||
960 | template<typename _ForwardIterator, typename _BinaryPredicate> | |
961 | _ForwardIterator | |
962 | __unique(_ForwardIterator __first, _ForwardIterator __last, | |
963 | _BinaryPredicate __binary_pred) | |
964 | { | |
965 | // Skip the beginning, if already unique. | |
966 | __first = std::__adjacent_find(__first, __last, __binary_pred); | |
967 | if (__first == __last) | |
968 | return __last; | |
969 | ||
970 | // Do the real copy work. | |
971 | _ForwardIterator __dest = __first; | |
3a6b0f54 | 972 | ++__first; |
ea89b248 FD |
973 | while (++__first != __last) |
974 | if (!__binary_pred(__dest, __first)) | |
975 | *++__dest = _GLIBCXX_MOVE(*__first); | |
976 | return ++__dest; | |
b35c0822 PC |
977 | } |
978 | ||
979 | /** | |
c2ba9709 | 980 | * @brief Remove consecutive duplicate values from a sequence. |
5b9daa7e | 981 | * @ingroup mutating_algorithms |
93c66bc6 BK |
982 | * @param __first A forward iterator. |
983 | * @param __last A forward iterator. | |
c2ba9709 JS |
984 | * @return An iterator designating the end of the resulting sequence. |
985 | * | |
986 | * Removes all but the first element from each group of consecutive | |
987 | * values that compare equal. | |
988 | * unique() is stable, so the relative order of elements that are | |
989 | * not removed is unchanged. | |
93c66bc6 | 990 | * Elements between the end of the resulting sequence and @p __last |
c2ba9709 | 991 | * are still present, but their value is unspecified. |
b35c0822 | 992 | */ |
c2ba9709 | 993 | template<typename _ForwardIterator> |
ea89b248 | 994 | inline _ForwardIterator |
c2ba9709 | 995 | unique(_ForwardIterator __first, _ForwardIterator __last) |
b35c0822 | 996 | { |
c2ba9709 JS |
997 | // concept requirements |
998 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< | |
999 | _ForwardIterator>) | |
1000 | __glibcxx_function_requires(_EqualityComparableConcept< | |
1001 | typename iterator_traits<_ForwardIterator>::value_type>) | |
1002 | __glibcxx_requires_valid_range(__first, __last); | |
b35c0822 | 1003 | |
ea89b248 FD |
1004 | return std::__unique(__first, __last, |
1005 | __gnu_cxx::__ops::__iter_equal_to_iter()); | |
02d92e3b SW |
1006 | } |
1007 | ||
c0d88161 | 1008 | /** |
c2ba9709 | 1009 | * @brief Remove consecutive values from a sequence using a predicate. |
5b9daa7e | 1010 | * @ingroup mutating_algorithms |
93c66bc6 BK |
1011 | * @param __first A forward iterator. |
1012 | * @param __last A forward iterator. | |
1013 | * @param __binary_pred A binary predicate. | |
c2ba9709 | 1014 | * @return An iterator designating the end of the resulting sequence. |
c0d88161 | 1015 | * |
c2ba9709 | 1016 | * Removes all but the first element from each group of consecutive |
93c66bc6 | 1017 | * values for which @p __binary_pred returns true. |
c2ba9709 JS |
1018 | * unique() is stable, so the relative order of elements that are |
1019 | * not removed is unchanged. | |
93c66bc6 | 1020 | * Elements between the end of the resulting sequence and @p __last |
c2ba9709 | 1021 | * are still present, but their value is unspecified. |
c0d88161 | 1022 | */ |
c2ba9709 | 1023 | template<typename _ForwardIterator, typename _BinaryPredicate> |
ea89b248 | 1024 | inline _ForwardIterator |
c2ba9709 | 1025 | unique(_ForwardIterator __first, _ForwardIterator __last, |
ec494945 | 1026 | _BinaryPredicate __binary_pred) |
02d92e3b SW |
1027 | { |
1028 | // concept requirements | |
c2ba9709 JS |
1029 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
1030 | _ForwardIterator>) | |
3d7c150e | 1031 | __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
c2ba9709 JS |
1032 | typename iterator_traits<_ForwardIterator>::value_type, |
1033 | typename iterator_traits<_ForwardIterator>::value_type>) | |
285b36d6 | 1034 | __glibcxx_requires_valid_range(__first, __last); |
02d92e3b | 1035 | |
ea89b248 FD |
1036 | return std::__unique(__first, __last, |
1037 | __gnu_cxx::__ops::__iter_comp_iter(__binary_pred)); | |
02d92e3b SW |
1038 | } |
1039 | ||
c2ba9709 | 1040 | /** |
c2ba9709 JS |
1041 | * This is an uglified |
1042 | * unique_copy(_InputIterator, _InputIterator, _OutputIterator, | |
1043 | * _BinaryPredicate) | |
1044 | * overloaded for forward iterators and output iterator as result. | |
c2ba9709 JS |
1045 | */ |
1046 | template<typename _ForwardIterator, typename _OutputIterator, | |
da5a2efd | 1047 | typename _BinaryPredicate> |
c2ba9709 JS |
1048 | _OutputIterator |
1049 | __unique_copy(_ForwardIterator __first, _ForwardIterator __last, | |
1050 | _OutputIterator __result, _BinaryPredicate __binary_pred, | |
1051 | forward_iterator_tag, output_iterator_tag) | |
02d92e3b | 1052 | { |
c2ba9709 JS |
1053 | // concept requirements -- iterators already checked |
1054 | __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, | |
1055 | typename iterator_traits<_ForwardIterator>::value_type, | |
1056 | typename iterator_traits<_ForwardIterator>::value_type>) | |
da5a2efd | 1057 | |
c2ba9709 JS |
1058 | _ForwardIterator __next = __first; |
1059 | *__result = *__first; | |
1060 | while (++__next != __last) | |
ea89b248 | 1061 | if (!__binary_pred(__first, __next)) |
c2ba9709 JS |
1062 | { |
1063 | __first = __next; | |
1064 | *++__result = *__first; | |
1065 | } | |
1066 | return ++__result; | |
da5a2efd PC |
1067 | } |
1068 | ||
1069 | /** | |
c2ba9709 JS |
1070 | * This is an uglified |
1071 | * unique_copy(_InputIterator, _InputIterator, _OutputIterator, | |
1072 | * _BinaryPredicate) | |
1073 | * overloaded for input iterators and output iterator as result. | |
da5a2efd | 1074 | */ |
c2ba9709 JS |
1075 | template<typename _InputIterator, typename _OutputIterator, |
1076 | typename _BinaryPredicate> | |
1077 | _OutputIterator | |
1078 | __unique_copy(_InputIterator __first, _InputIterator __last, | |
1079 | _OutputIterator __result, _BinaryPredicate __binary_pred, | |
1080 | input_iterator_tag, output_iterator_tag) | |
da5a2efd | 1081 | { |
c2ba9709 JS |
1082 | // concept requirements -- iterators already checked |
1083 | __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, | |
1084 | typename iterator_traits<_InputIterator>::value_type, | |
1085 | typename iterator_traits<_InputIterator>::value_type>) | |
da5a2efd | 1086 | |
c2ba9709 | 1087 | typename iterator_traits<_InputIterator>::value_type __value = *__first; |
ea89b248 FD |
1088 | __decltype(__gnu_cxx::__ops::__iter_comp_val(__binary_pred)) |
1089 | __rebound_pred | |
1090 | = __gnu_cxx::__ops::__iter_comp_val(__binary_pred); | |
c2ba9709 JS |
1091 | *__result = __value; |
1092 | while (++__first != __last) | |
ea89b248 | 1093 | if (!__rebound_pred(__first, __value)) |
c2ba9709 JS |
1094 | { |
1095 | __value = *__first; | |
1096 | *++__result = __value; | |
1097 | } | |
1098 | return ++__result; | |
da5a2efd PC |
1099 | } |
1100 | ||
1101 | /** | |
c2ba9709 JS |
1102 | * This is an uglified |
1103 | * unique_copy(_InputIterator, _InputIterator, _OutputIterator, | |
1104 | * _BinaryPredicate) | |
1105 | * overloaded for input iterators and forward iterator as result. | |
da5a2efd | 1106 | */ |
c2ba9709 | 1107 | template<typename _InputIterator, typename _ForwardIterator, |
da5a2efd | 1108 | typename _BinaryPredicate> |
c2ba9709 JS |
1109 | _ForwardIterator |
1110 | __unique_copy(_InputIterator __first, _InputIterator __last, | |
1111 | _ForwardIterator __result, _BinaryPredicate __binary_pred, | |
1112 | input_iterator_tag, forward_iterator_tag) | |
da5a2efd | 1113 | { |
c2ba9709 | 1114 | // concept requirements -- iterators already checked |
da5a2efd | 1115 | __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
c2ba9709 JS |
1116 | typename iterator_traits<_ForwardIterator>::value_type, |
1117 | typename iterator_traits<_InputIterator>::value_type>) | |
c2ba9709 JS |
1118 | *__result = *__first; |
1119 | while (++__first != __last) | |
ea89b248 | 1120 | if (!__binary_pred(__result, __first)) |
c2ba9709 JS |
1121 | *++__result = *__first; |
1122 | return ++__result; | |
da5a2efd PC |
1123 | } |
1124 | ||
1125 | /** | |
c2ba9709 JS |
1126 | * This is an uglified reverse(_BidirectionalIterator, |
1127 | * _BidirectionalIterator) | |
1128 | * overloaded for bidirectional iterators. | |
da5a2efd | 1129 | */ |
c2ba9709 JS |
1130 | template<typename _BidirectionalIterator> |
1131 | void | |
1132 | __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, | |
1133 | bidirectional_iterator_tag) | |
da5a2efd | 1134 | { |
c2ba9709 JS |
1135 | while (true) |
1136 | if (__first == __last || __first == --__last) | |
1137 | return; | |
1138 | else | |
1139 | { | |
1140 | std::iter_swap(__first, __last); | |
1141 | ++__first; | |
1142 | } | |
da5a2efd PC |
1143 | } |
1144 | ||
1145 | /** | |
c2ba9709 JS |
1146 | * This is an uglified reverse(_BidirectionalIterator, |
1147 | * _BidirectionalIterator) | |
1148 | * overloaded for random access iterators. | |
da5a2efd | 1149 | */ |
c2ba9709 | 1150 | template<typename _RandomAccessIterator> |
da5a2efd | 1151 | void |
c2ba9709 JS |
1152 | __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, |
1153 | random_access_iterator_tag) | |
da5a2efd | 1154 | { |
c2ba9709 JS |
1155 | if (__first == __last) |
1156 | return; | |
1157 | --__last; | |
1158 | while (__first < __last) | |
1159 | { | |
1160 | std::iter_swap(__first, __last); | |
1161 | ++__first; | |
1162 | --__last; | |
1163 | } | |
da5a2efd PC |
1164 | } |
1165 | ||
1166 | /** | |
c2ba9709 | 1167 | * @brief Reverse a sequence. |
5b9daa7e | 1168 | * @ingroup mutating_algorithms |
93c66bc6 BK |
1169 | * @param __first A bidirectional iterator. |
1170 | * @param __last A bidirectional iterator. | |
c2ba9709 | 1171 | * @return reverse() returns no value. |
c0d88161 | 1172 | * |
93c66bc6 | 1173 | * Reverses the order of the elements in the range @p [__first,__last), |
c2ba9709 | 1174 | * so that the first element becomes the last etc. |
93c66bc6 BK |
1175 | * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse() |
1176 | * swaps @p *(__first+i) and @p *(__last-(i+1)) | |
c0d88161 | 1177 | */ |
c2ba9709 JS |
1178 | template<typename _BidirectionalIterator> |
1179 | inline void | |
1180 | reverse(_BidirectionalIterator __first, _BidirectionalIterator __last) | |
02d92e3b SW |
1181 | { |
1182 | // concept requirements | |
c2ba9709 JS |
1183 | __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< |
1184 | _BidirectionalIterator>) | |
285b36d6 | 1185 | __glibcxx_requires_valid_range(__first, __last); |
c2ba9709 | 1186 | std::__reverse(__first, __last, std::__iterator_category(__first)); |
02d92e3b SW |
1187 | } |
1188 | ||
c0d88161 | 1189 | /** |
c2ba9709 | 1190 | * @brief Copy a sequence, reversing its elements. |
5b9daa7e | 1191 | * @ingroup mutating_algorithms |
93c66bc6 BK |
1192 | * @param __first A bidirectional iterator. |
1193 | * @param __last A bidirectional iterator. | |
1194 | * @param __result An output iterator. | |
c2ba9709 | 1195 | * @return An iterator designating the end of the resulting sequence. |
c0d88161 | 1196 | * |
93c66bc6 BK |
1197 | * Copies the elements in the range @p [__first,__last) to the |
1198 | * range @p [__result,__result+(__last-__first)) such that the | |
1199 | * order of the elements is reversed. For every @c i such that @p | |
1200 | * 0<=i<=(__last-__first), @p reverse_copy() performs the | |
1b5dc776 | 1201 | * assignment @p *(__result+(__last-__first)-1-i) = *(__first+i). |
93c66bc6 BK |
1202 | * The ranges @p [__first,__last) and @p |
1203 | * [__result,__result+(__last-__first)) must not overlap. | |
c0d88161 | 1204 | */ |
c2ba9709 | 1205 | template<typename _BidirectionalIterator, typename _OutputIterator> |
08addde6 | 1206 | _OutputIterator |
c2ba9709 | 1207 | reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, |
76cc1b70 | 1208 | _OutputIterator __result) |
02d92e3b SW |
1209 | { |
1210 | // concept requirements | |
c2ba9709 JS |
1211 | __glibcxx_function_requires(_BidirectionalIteratorConcept< |
1212 | _BidirectionalIterator>) | |
3d7c150e | 1213 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
c2ba9709 | 1214 | typename iterator_traits<_BidirectionalIterator>::value_type>) |
285b36d6 | 1215 | __glibcxx_requires_valid_range(__first, __last); |
02d92e3b | 1216 | |
c2ba9709 JS |
1217 | while (__first != __last) |
1218 | { | |
1219 | --__last; | |
1220 | *__result = *__last; | |
1221 | ++__result; | |
1222 | } | |
02d92e3b SW |
1223 | return __result; |
1224 | } | |
1225 | ||
c0d88161 | 1226 | /** |
c2ba9709 JS |
1227 | * This is a helper function for the rotate algorithm specialized on RAIs. |
1228 | * It returns the greatest common divisor of two integer values. | |
c0d88161 | 1229 | */ |
c2ba9709 JS |
1230 | template<typename _EuclideanRingElement> |
1231 | _EuclideanRingElement | |
1232 | __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n) | |
02d92e3b | 1233 | { |
c2ba9709 JS |
1234 | while (__n != 0) |
1235 | { | |
1236 | _EuclideanRingElement __t = __m % __n; | |
1237 | __m = __n; | |
1238 | __n = __t; | |
1239 | } | |
1240 | return __m; | |
02d92e3b SW |
1241 | } |
1242 | ||
87839f22 JW |
1243 | inline namespace _V2 |
1244 | { | |
1245 | ||
4312e020 | 1246 | /// This is a helper function for the rotate algorithm. |
c2ba9709 | 1247 | template<typename _ForwardIterator> |
1d0a12dc | 1248 | _ForwardIterator |
c2ba9709 JS |
1249 | __rotate(_ForwardIterator __first, |
1250 | _ForwardIterator __middle, | |
1251 | _ForwardIterator __last, | |
1252 | forward_iterator_tag) | |
02d92e3b | 1253 | { |
c2ba9709 JS |
1254 | _ForwardIterator __first2 = __middle; |
1255 | do | |
1256 | { | |
3a6b0f54 | 1257 | std::iter_swap(__first, __first2); |
c2ba9709 JS |
1258 | ++__first; |
1259 | ++__first2; | |
1260 | if (__first == __middle) | |
1261 | __middle = __first2; | |
1262 | } | |
1263 | while (__first2 != __last); | |
1264 | ||
1d0a12dc JW |
1265 | _ForwardIterator __ret = __first; |
1266 | ||
c2ba9709 JS |
1267 | __first2 = __middle; |
1268 | ||
1269 | while (__first2 != __last) | |
1270 | { | |
3a6b0f54 | 1271 | std::iter_swap(__first, __first2); |
c2ba9709 JS |
1272 | ++__first; |
1273 | ++__first2; | |
1274 | if (__first == __middle) | |
1275 | __middle = __first2; | |
1276 | else if (__first2 == __last) | |
1277 | __first2 = __middle; | |
1278 | } | |
1d0a12dc | 1279 | return __ret; |
02d92e3b SW |
1280 | } |
1281 | ||
4312e020 | 1282 | /// This is a helper function for the rotate algorithm. |
c2ba9709 | 1283 | template<typename _BidirectionalIterator> |
1d0a12dc | 1284 | _BidirectionalIterator |
c2ba9709 JS |
1285 | __rotate(_BidirectionalIterator __first, |
1286 | _BidirectionalIterator __middle, | |
1287 | _BidirectionalIterator __last, | |
1288 | bidirectional_iterator_tag) | |
02d92e3b | 1289 | { |
02669b68 | 1290 | // concept requirements |
c2ba9709 JS |
1291 | __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< |
1292 | _BidirectionalIterator>) | |
02d92e3b | 1293 | |
c2ba9709 JS |
1294 | std::__reverse(__first, __middle, bidirectional_iterator_tag()); |
1295 | std::__reverse(__middle, __last, bidirectional_iterator_tag()); | |
1296 | ||
1297 | while (__first != __middle && __middle != __last) | |
1298 | { | |
3a6b0f54 | 1299 | std::iter_swap(__first, --__last); |
c2ba9709 JS |
1300 | ++__first; |
1301 | } | |
1302 | ||
1303 | if (__first == __middle) | |
1d0a12dc JW |
1304 | { |
1305 | std::__reverse(__middle, __last, bidirectional_iterator_tag()); | |
1306 | return __last; | |
1307 | } | |
c2ba9709 | 1308 | else |
1d0a12dc JW |
1309 | { |
1310 | std::__reverse(__first, __middle, bidirectional_iterator_tag()); | |
1311 | return __first; | |
1312 | } | |
725dc051 | 1313 | } |
725dc051 | 1314 | |
4312e020 | 1315 | /// This is a helper function for the rotate algorithm. |
c2ba9709 | 1316 | template<typename _RandomAccessIterator> |
1d0a12dc | 1317 | _RandomAccessIterator |
c2ba9709 JS |
1318 | __rotate(_RandomAccessIterator __first, |
1319 | _RandomAccessIterator __middle, | |
1320 | _RandomAccessIterator __last, | |
1321 | random_access_iterator_tag) | |
02d92e3b SW |
1322 | { |
1323 | // concept requirements | |
c2ba9709 JS |
1324 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
1325 | _RandomAccessIterator>) | |
02d92e3b | 1326 | |
c2ba9709 JS |
1327 | typedef typename iterator_traits<_RandomAccessIterator>::difference_type |
1328 | _Distance; | |
1329 | typedef typename iterator_traits<_RandomAccessIterator>::value_type | |
1330 | _ValueType; | |
1331 | ||
6e924e07 DK |
1332 | _Distance __n = __last - __first; |
1333 | _Distance __k = __middle - __first; | |
c2ba9709 | 1334 | |
6e924e07 | 1335 | if (__k == __n - __k) |
c2ba9709 JS |
1336 | { |
1337 | std::swap_ranges(__first, __middle, __middle); | |
1d0a12dc | 1338 | return __middle; |
c2ba9709 JS |
1339 | } |
1340 | ||
6e924e07 | 1341 | _RandomAccessIterator __p = __first; |
1d0a12dc | 1342 | _RandomAccessIterator __ret = __first + (__last - __middle); |
c2ba9709 | 1343 | |
6e924e07 | 1344 | for (;;) |
c2ba9709 | 1345 | { |
6e924e07 | 1346 | if (__k < __n - __k) |
c2ba9709 | 1347 | { |
6e924e07 DK |
1348 | if (__is_pod(_ValueType) && __k == 1) |
1349 | { | |
1350 | _ValueType __t = _GLIBCXX_MOVE(*__p); | |
1351 | _GLIBCXX_MOVE3(__p + 1, __p + __n, __p); | |
1352 | *(__p + __n - 1) = _GLIBCXX_MOVE(__t); | |
1d0a12dc | 1353 | return __ret; |
6e924e07 DK |
1354 | } |
1355 | _RandomAccessIterator __q = __p + __k; | |
1356 | for (_Distance __i = 0; __i < __n - __k; ++ __i) | |
c2ba9709 | 1357 | { |
6e924e07 DK |
1358 | std::iter_swap(__p, __q); |
1359 | ++__p; | |
1360 | ++__q; | |
c2ba9709 | 1361 | } |
6e924e07 DK |
1362 | __n %= __k; |
1363 | if (__n == 0) | |
1d0a12dc | 1364 | return __ret; |
6e924e07 DK |
1365 | std::swap(__n, __k); |
1366 | __k = __n - __k; | |
c2ba9709 JS |
1367 | } |
1368 | else | |
1369 | { | |
6e924e07 DK |
1370 | __k = __n - __k; |
1371 | if (__is_pod(_ValueType) && __k == 1) | |
c2ba9709 | 1372 | { |
6e924e07 DK |
1373 | _ValueType __t = _GLIBCXX_MOVE(*(__p + __n - 1)); |
1374 | _GLIBCXX_MOVE_BACKWARD3(__p, __p + __n - 1, __p + __n); | |
1375 | *__p = _GLIBCXX_MOVE(__t); | |
1d0a12dc | 1376 | return __ret; |
c2ba9709 | 1377 | } |
6e924e07 DK |
1378 | _RandomAccessIterator __q = __p + __n; |
1379 | __p = __q - __k; | |
1380 | for (_Distance __i = 0; __i < __n - __k; ++ __i) | |
1381 | { | |
1382 | --__p; | |
1383 | --__q; | |
1384 | std::iter_swap(__p, __q); | |
1385 | } | |
1386 | __n %= __k; | |
1387 | if (__n == 0) | |
1d0a12dc | 1388 | return __ret; |
6e924e07 | 1389 | std::swap(__n, __k); |
c2ba9709 | 1390 | } |
c2ba9709 | 1391 | } |
02d92e3b SW |
1392 | } |
1393 | ||
1d0a12dc JW |
1394 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
1395 | // DR 488. rotate throws away useful information | |
c0d88161 | 1396 | /** |
c2ba9709 | 1397 | * @brief Rotate the elements of a sequence. |
5b9daa7e | 1398 | * @ingroup mutating_algorithms |
93c66bc6 BK |
1399 | * @param __first A forward iterator. |
1400 | * @param __middle A forward iterator. | |
1401 | * @param __last A forward iterator. | |
1d0a12dc | 1402 | * @return first + (last - middle). |
c0d88161 | 1403 | * |
93c66bc6 | 1404 | * Rotates the elements of the range @p [__first,__last) by |
7897a1c0 | 1405 | * @p (__middle - __first) positions so that the element at @p __middle |
93c66bc6 | 1406 | * is moved to @p __first, the element at @p __middle+1 is moved to |
7897a1c0 | 1407 | * @p __first+1 and so on for each element in the range |
93c66bc6 | 1408 | * @p [__first,__last). |
c0d88161 | 1409 | * |
93c66bc6 BK |
1410 | * This effectively swaps the ranges @p [__first,__middle) and |
1411 | * @p [__middle,__last). | |
c2ba9709 | 1412 | * |
93c66bc6 BK |
1413 | * Performs |
1414 | * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n) | |
1415 | * for each @p n in the range @p [0,__last-__first). | |
c0d88161 | 1416 | */ |
c2ba9709 | 1417 | template<typename _ForwardIterator> |
1d0a12dc | 1418 | inline _ForwardIterator |
c2ba9709 JS |
1419 | rotate(_ForwardIterator __first, _ForwardIterator __middle, |
1420 | _ForwardIterator __last) | |
02d92e3b SW |
1421 | { |
1422 | // concept requirements | |
ffa67767 PC |
1423 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
1424 | _ForwardIterator>) | |
c2ba9709 JS |
1425 | __glibcxx_requires_valid_range(__first, __middle); |
1426 | __glibcxx_requires_valid_range(__middle, __last); | |
02d92e3b | 1427 | |
29a9c26c FD |
1428 | if (__first == __middle) |
1429 | return __last; | |
1430 | else if (__last == __middle) | |
1431 | return __first; | |
1432 | ||
1d0a12dc JW |
1433 | return std::__rotate(__first, __middle, __last, |
1434 | std::__iterator_category(__first)); | |
02d92e3b | 1435 | } |
725dc051 | 1436 | |
87839f22 JW |
1437 | } // namespace _V2 |
1438 | ||
c0d88161 | 1439 | /** |
c2ba9709 | 1440 | * @brief Copy a sequence, rotating its elements. |
5b9daa7e | 1441 | * @ingroup mutating_algorithms |
93c66bc6 BK |
1442 | * @param __first A forward iterator. |
1443 | * @param __middle A forward iterator. | |
1444 | * @param __last A forward iterator. | |
1445 | * @param __result An output iterator. | |
c0d88161 JW |
1446 | * @return An iterator designating the end of the resulting sequence. |
1447 | * | |
93c66bc6 BK |
1448 | * Copies the elements of the range @p [__first,__last) to the |
1449 | * range beginning at @result, rotating the copied elements by | |
1450 | * @p (__middle-__first) positions so that the element at @p __middle | |
1451 | * is moved to @p __result, the element at @p __middle+1 is moved | |
7897a1c0 | 1452 | * to @p __result+1 and so on for each element in the range @p |
93c66bc6 | 1453 | * [__first,__last). |
c0d88161 | 1454 | * |
93c66bc6 BK |
1455 | * Performs |
1456 | * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n) | |
1457 | * for each @p n in the range @p [0,__last-__first). | |
c0d88161 | 1458 | */ |
c2ba9709 | 1459 | template<typename _ForwardIterator, typename _OutputIterator> |
3bd2644c | 1460 | inline _OutputIterator |
c2ba9709 | 1461 | rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, |
ec494945 | 1462 | _ForwardIterator __last, _OutputIterator __result) |
02d92e3b SW |
1463 | { |
1464 | // concept requirements | |
c2ba9709 JS |
1465 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
1466 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
1467 | typename iterator_traits<_ForwardIterator>::value_type>) | |
1468 | __glibcxx_requires_valid_range(__first, __middle); | |
1469 | __glibcxx_requires_valid_range(__middle, __last); | |
30a20a1e | 1470 | |
c2ba9709 | 1471 | return std::copy(__first, __middle, |
ec494945 | 1472 | std::copy(__middle, __last, __result)); |
02d92e3b SW |
1473 | } |
1474 | ||
4312e020 | 1475 | /// This is a helper function... |
c2ba9709 | 1476 | template<typename _ForwardIterator, typename _Predicate> |
da5a2efd | 1477 | _ForwardIterator |
c2ba9709 | 1478 | __partition(_ForwardIterator __first, _ForwardIterator __last, |
3a6b0f54 | 1479 | _Predicate __pred, forward_iterator_tag) |
02d92e3b | 1480 | { |
da5a2efd | 1481 | if (__first == __last) |
c2ba9709 | 1482 | return __first; |
da5a2efd | 1483 | |
c2ba9709 JS |
1484 | while (__pred(*__first)) |
1485 | if (++__first == __last) | |
1486 | return __first; | |
da5a2efd | 1487 | |
c2ba9709 | 1488 | _ForwardIterator __next = __first; |
da5a2efd | 1489 | |
c2ba9709 JS |
1490 | while (++__next != __last) |
1491 | if (__pred(*__next)) | |
1492 | { | |
3a6b0f54 | 1493 | std::iter_swap(__first, __next); |
c2ba9709 JS |
1494 | ++__first; |
1495 | } | |
da5a2efd | 1496 | |
c2ba9709 | 1497 | return __first; |
da5a2efd PC |
1498 | } |
1499 | ||
4312e020 | 1500 | /// This is a helper function... |
c2ba9709 JS |
1501 | template<typename _BidirectionalIterator, typename _Predicate> |
1502 | _BidirectionalIterator | |
1503 | __partition(_BidirectionalIterator __first, _BidirectionalIterator __last, | |
3a6b0f54 | 1504 | _Predicate __pred, bidirectional_iterator_tag) |
da5a2efd | 1505 | { |
c2ba9709 JS |
1506 | while (true) |
1507 | { | |
1508 | while (true) | |
1509 | if (__first == __last) | |
1510 | return __first; | |
1511 | else if (__pred(*__first)) | |
1512 | ++__first; | |
1513 | else | |
1514 | break; | |
1515 | --__last; | |
1516 | while (true) | |
1517 | if (__first == __last) | |
1518 | return __first; | |
1519 | else if (!bool(__pred(*__last))) | |
1520 | --__last; | |
1521 | else | |
1522 | break; | |
1523 | std::iter_swap(__first, __last); | |
1524 | ++__first; | |
1525 | } | |
da5a2efd PC |
1526 | } |
1527 | ||
c2ba9709 JS |
1528 | // partition |
1529 | ||
4312e020 | 1530 | /// This is a helper function... |
ea89b248 | 1531 | /// Requires __first != __last and !__pred(__first) |
2fc9b37d JY |
1532 | /// and __len == distance(__first, __last). |
1533 | /// | |
ea89b248 | 1534 | /// !__pred(__first) allows us to guarantee that we don't |
2fc9b37d | 1535 | /// move-assign an element onto itself. |
c2ba9709 JS |
1536 | template<typename _ForwardIterator, typename _Pointer, typename _Predicate, |
1537 | typename _Distance> | |
08addde6 | 1538 | _ForwardIterator |
c2ba9709 JS |
1539 | __stable_partition_adaptive(_ForwardIterator __first, |
1540 | _ForwardIterator __last, | |
1541 | _Predicate __pred, _Distance __len, | |
1542 | _Pointer __buffer, | |
1543 | _Distance __buffer_size) | |
02d92e3b | 1544 | { |
970a9caa FD |
1545 | if (__len == 1) |
1546 | return __first; | |
1547 | ||
c2ba9709 JS |
1548 | if (__len <= __buffer_size) |
1549 | { | |
1550 | _ForwardIterator __result1 = __first; | |
1551 | _Pointer __result2 = __buffer; | |
970a9caa | 1552 | |
ea89b248 | 1553 | // The precondition guarantees that !__pred(__first), so |
2fc9b37d JY |
1554 | // move that element to the buffer before starting the loop. |
1555 | // This ensures that we only call __pred once per element. | |
1556 | *__result2 = _GLIBCXX_MOVE(*__first); | |
1557 | ++__result2; | |
1558 | ++__first; | |
c2ba9709 | 1559 | for (; __first != __last; ++__first) |
ea89b248 | 1560 | if (__pred(__first)) |
c2ba9709 | 1561 | { |
fe03b3df | 1562 | *__result1 = _GLIBCXX_MOVE(*__first); |
c2ba9709 JS |
1563 | ++__result1; |
1564 | } | |
1565 | else | |
1566 | { | |
fe03b3df | 1567 | *__result2 = _GLIBCXX_MOVE(*__first); |
c2ba9709 JS |
1568 | ++__result2; |
1569 | } | |
970a9caa | 1570 | |
fe03b3df | 1571 | _GLIBCXX_MOVE3(__buffer, __result2, __result1); |
c2ba9709 JS |
1572 | return __result1; |
1573 | } | |
970a9caa FD |
1574 | |
1575 | _ForwardIterator __middle = __first; | |
1576 | std::advance(__middle, __len / 2); | |
1577 | _ForwardIterator __left_split = | |
1578 | std::__stable_partition_adaptive(__first, __middle, __pred, | |
1579 | __len / 2, __buffer, | |
1580 | __buffer_size); | |
1581 | ||
1582 | // Advance past true-predicate values to satisfy this | |
1583 | // function's preconditions. | |
1584 | _Distance __right_len = __len - __len / 2; | |
1585 | _ForwardIterator __right_split = | |
1586 | std::__find_if_not_n(__middle, __right_len, __pred); | |
1587 | ||
1588 | if (__right_len) | |
1589 | __right_split = | |
1590 | std::__stable_partition_adaptive(__right_split, __last, __pred, | |
1591 | __right_len, | |
1592 | __buffer, __buffer_size); | |
1593 | ||
29a9c26c | 1594 | return std::rotate(__left_split, __middle, __right_split); |
02d92e3b SW |
1595 | } |
1596 | ||
ea89b248 FD |
1597 | template<typename _ForwardIterator, typename _Predicate> |
1598 | _ForwardIterator | |
1599 | __stable_partition(_ForwardIterator __first, _ForwardIterator __last, | |
1600 | _Predicate __pred) | |
1601 | { | |
1602 | __first = std::__find_if_not(__first, __last, __pred); | |
1603 | ||
1604 | if (__first == __last) | |
1605 | return __first; | |
1606 | ||
1607 | typedef typename iterator_traits<_ForwardIterator>::value_type | |
1608 | _ValueType; | |
1609 | typedef typename iterator_traits<_ForwardIterator>::difference_type | |
1610 | _DistanceType; | |
1611 | ||
d31238cf FD |
1612 | _Temporary_buffer<_ForwardIterator, _ValueType> |
1613 | __buf(__first, std::distance(__first, __last)); | |
970a9caa FD |
1614 | return |
1615 | std::__stable_partition_adaptive(__first, __last, __pred, | |
1616 | _DistanceType(__buf.requested_size()), | |
1617 | __buf.begin(), | |
1618 | _DistanceType(__buf.size())); | |
ea89b248 FD |
1619 | } |
1620 | ||
c0d88161 | 1621 | /** |
c2ba9709 JS |
1622 | * @brief Move elements for which a predicate is true to the beginning |
1623 | * of a sequence, preserving relative ordering. | |
5b9daa7e | 1624 | * @ingroup mutating_algorithms |
93c66bc6 BK |
1625 | * @param __first A forward iterator. |
1626 | * @param __last A forward iterator. | |
1627 | * @param __pred A predicate functor. | |
1628 | * @return An iterator @p middle such that @p __pred(i) is true for each | |
c2ba9709 JS |
1629 | * iterator @p i in the range @p [first,middle) and false for each @p i |
1630 | * in the range @p [middle,last). | |
1631 | * | |
1632 | * Performs the same function as @p partition() with the additional | |
1633 | * guarantee that the relative ordering of elements in each group is | |
1634 | * preserved, so any two elements @p x and @p y in the range | |
93c66bc6 | 1635 | * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same |
c2ba9709 | 1636 | * relative ordering after calling @p stable_partition(). |
3089d5a4 | 1637 | */ |
c2ba9709 | 1638 | template<typename _ForwardIterator, typename _Predicate> |
3bd2644c | 1639 | inline _ForwardIterator |
c2ba9709 JS |
1640 | stable_partition(_ForwardIterator __first, _ForwardIterator __last, |
1641 | _Predicate __pred) | |
3089d5a4 | 1642 | { |
c2ba9709 JS |
1643 | // concept requirements |
1644 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< | |
1645 | _ForwardIterator>) | |
1646 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, | |
1647 | typename iterator_traits<_ForwardIterator>::value_type>) | |
1648 | __glibcxx_requires_valid_range(__first, __last); | |
3089d5a4 | 1649 | |
ea89b248 FD |
1650 | return std::__stable_partition(__first, __last, |
1651 | __gnu_cxx::__ops::__pred_iter(__pred)); | |
02d92e3b SW |
1652 | } |
1653 | ||
4312e020 | 1654 | /// This is a helper function for the sort routines. |
c2ba9709 JS |
1655 | template<typename _RandomAccessIterator, typename _Compare> |
1656 | void | |
1657 | __heap_select(_RandomAccessIterator __first, | |
1658 | _RandomAccessIterator __middle, | |
1659 | _RandomAccessIterator __last, _Compare __comp) | |
02d92e3b | 1660 | { |
ea89b248 | 1661 | std::__make_heap(__first, __middle, __comp); |
c2ba9709 | 1662 | for (_RandomAccessIterator __i = __middle; __i < __last; ++__i) |
ea89b248 | 1663 | if (__comp(__i, __first)) |
d70e9d81 | 1664 | std::__pop_heap(__first, __middle, __i, __comp); |
02d92e3b SW |
1665 | } |
1666 | ||
c2ba9709 JS |
1667 | // partial_sort |
1668 | ||
ea89b248 FD |
1669 | template<typename _InputIterator, typename _RandomAccessIterator, |
1670 | typename _Compare> | |
1671 | _RandomAccessIterator | |
1672 | __partial_sort_copy(_InputIterator __first, _InputIterator __last, | |
1673 | _RandomAccessIterator __result_first, | |
1674 | _RandomAccessIterator __result_last, | |
1675 | _Compare __comp) | |
1676 | { | |
1677 | typedef typename iterator_traits<_InputIterator>::value_type | |
1678 | _InputValueType; | |
1679 | typedef iterator_traits<_RandomAccessIterator> _RItTraits; | |
1680 | typedef typename _RItTraits::difference_type _DistanceType; | |
1681 | ||
1682 | if (__result_first == __result_last) | |
1683 | return __result_last; | |
1684 | _RandomAccessIterator __result_real_last = __result_first; | |
1685 | while (__first != __last && __result_real_last != __result_last) | |
1686 | { | |
1687 | *__result_real_last = *__first; | |
1688 | ++__result_real_last; | |
1689 | ++__first; | |
1690 | } | |
1691 | ||
1692 | std::__make_heap(__result_first, __result_real_last, __comp); | |
1693 | while (__first != __last) | |
1694 | { | |
1695 | if (__comp(__first, __result_first)) | |
1696 | std::__adjust_heap(__result_first, _DistanceType(0), | |
1697 | _DistanceType(__result_real_last | |
1698 | - __result_first), | |
1699 | _InputValueType(*__first), __comp); | |
1700 | ++__first; | |
1701 | } | |
1702 | std::__sort_heap(__result_first, __result_real_last, __comp); | |
1703 | return __result_real_last; | |
1704 | } | |
1705 | ||
c868f37e | 1706 | /** |
c2ba9709 | 1707 | * @brief Copy the smallest elements of a sequence. |
5b9daa7e | 1708 | * @ingroup sorting_algorithms |
93c66bc6 BK |
1709 | * @param __first An iterator. |
1710 | * @param __last Another iterator. | |
1711 | * @param __result_first A random-access iterator. | |
1712 | * @param __result_last Another random-access iterator. | |
c2ba9709 | 1713 | * @return An iterator indicating the end of the resulting sequence. |
3089d5a4 | 1714 | * |
93c66bc6 BK |
1715 | * Copies and sorts the smallest N values from the range @p [__first,__last) |
1716 | * to the range beginning at @p __result_first, where the number of | |
1717 | * elements to be copied, @p N, is the smaller of @p (__last-__first) and | |
1718 | * @p (__result_last-__result_first). | |
7897a1c0 BK |
1719 | * After the sort if @e i and @e j are iterators in the range |
1720 | * @p [__result_first,__result_first+N) such that i precedes j then | |
1721 | * *j<*i is false. | |
93c66bc6 | 1722 | * The value returned is @p __result_first+N. |
c868f37e | 1723 | */ |
c2ba9709 | 1724 | template<typename _InputIterator, typename _RandomAccessIterator> |
3bd2644c | 1725 | inline _RandomAccessIterator |
c2ba9709 JS |
1726 | partial_sort_copy(_InputIterator __first, _InputIterator __last, |
1727 | _RandomAccessIterator __result_first, | |
1728 | _RandomAccessIterator __result_last) | |
c868f37e | 1729 | { |
ec494945 | 1730 | #ifdef _GLIBCXX_CONCEPT_CHECKS |
c2ba9709 JS |
1731 | typedef typename iterator_traits<_InputIterator>::value_type |
1732 | _InputValueType; | |
1733 | typedef typename iterator_traits<_RandomAccessIterator>::value_type | |
1734 | _OutputValueType; | |
ec494945 | 1735 | #endif |
c2ba9709 JS |
1736 | |
1737 | // concept requirements | |
1738 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) | |
1739 | __glibcxx_function_requires(_ConvertibleConcept<_InputValueType, | |
1740 | _OutputValueType>) | |
1741 | __glibcxx_function_requires(_LessThanOpConcept<_InputValueType, | |
ec494945 | 1742 | _OutputValueType>) |
c2ba9709 | 1743 | __glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>) |
285b36d6 | 1744 | __glibcxx_requires_valid_range(__first, __last); |
630a286a | 1745 | __glibcxx_requires_irreflexive(__first, __last); |
c2ba9709 | 1746 | __glibcxx_requires_valid_range(__result_first, __result_last); |
c868f37e | 1747 | |
ea89b248 FD |
1748 | return std::__partial_sort_copy(__first, __last, |
1749 | __result_first, __result_last, | |
1750 | __gnu_cxx::__ops::__iter_less_iter()); | |
c868f37e PC |
1751 | } |
1752 | ||
c0d88161 | 1753 | /** |
c2ba9709 JS |
1754 | * @brief Copy the smallest elements of a sequence using a predicate for |
1755 | * comparison. | |
5b9daa7e | 1756 | * @ingroup sorting_algorithms |
93c66bc6 BK |
1757 | * @param __first An input iterator. |
1758 | * @param __last Another input iterator. | |
1759 | * @param __result_first A random-access iterator. | |
1760 | * @param __result_last Another random-access iterator. | |
1761 | * @param __comp A comparison functor. | |
c2ba9709 | 1762 | * @return An iterator indicating the end of the resulting sequence. |
3089d5a4 | 1763 | * |
93c66bc6 | 1764 | * Copies and sorts the smallest N values from the range @p [__first,__last) |
c2ba9709 | 1765 | * to the range beginning at @p result_first, where the number of |
93c66bc6 BK |
1766 | * elements to be copied, @p N, is the smaller of @p (__last-__first) and |
1767 | * @p (__result_last-__result_first). | |
7897a1c0 BK |
1768 | * After the sort if @e i and @e j are iterators in the range |
1769 | * @p [__result_first,__result_first+N) such that i precedes j then | |
93c66bc6 BK |
1770 | * @p __comp(*j,*i) is false. |
1771 | * The value returned is @p __result_first+N. | |
c0d88161 | 1772 | */ |
ea89b248 FD |
1773 | template<typename _InputIterator, typename _RandomAccessIterator, |
1774 | typename _Compare> | |
3bd2644c | 1775 | inline _RandomAccessIterator |
c2ba9709 JS |
1776 | partial_sort_copy(_InputIterator __first, _InputIterator __last, |
1777 | _RandomAccessIterator __result_first, | |
1778 | _RandomAccessIterator __result_last, | |
1779 | _Compare __comp) | |
02d92e3b | 1780 | { |
ec494945 | 1781 | #ifdef _GLIBCXX_CONCEPT_CHECKS |
c2ba9709 JS |
1782 | typedef typename iterator_traits<_InputIterator>::value_type |
1783 | _InputValueType; | |
1784 | typedef typename iterator_traits<_RandomAccessIterator>::value_type | |
1785 | _OutputValueType; | |
ec494945 | 1786 | #endif |
c2ba9709 JS |
1787 | |
1788 | // concept requirements | |
3d7c150e | 1789 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
c2ba9709 JS |
1790 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
1791 | _RandomAccessIterator>) | |
1792 | __glibcxx_function_requires(_ConvertibleConcept<_InputValueType, | |
1793 | _OutputValueType>) | |
1794 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, | |
1795 | _InputValueType, _OutputValueType>) | |
1796 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, | |
1797 | _OutputValueType, _OutputValueType>) | |
285b36d6 | 1798 | __glibcxx_requires_valid_range(__first, __last); |
630a286a | 1799 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
c2ba9709 | 1800 | __glibcxx_requires_valid_range(__result_first, __result_last); |
c0d88161 | 1801 | |
ea89b248 FD |
1802 | return std::__partial_sort_copy(__first, __last, |
1803 | __result_first, __result_last, | |
1804 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); | |
02d92e3b SW |
1805 | } |
1806 | ||
4312e020 | 1807 | /// This is a helper function for the sort routine. |
f9590eae | 1808 | template<typename _RandomAccessIterator, typename _Compare> |
02d92e3b | 1809 | void |
f9590eae | 1810 | __unguarded_linear_insert(_RandomAccessIterator __last, |
c2ba9709 | 1811 | _Compare __comp) |
02d92e3b | 1812 | { |
f9590eae CJ |
1813 | typename iterator_traits<_RandomAccessIterator>::value_type |
1814 | __val = _GLIBCXX_MOVE(*__last); | |
c2ba9709 JS |
1815 | _RandomAccessIterator __next = __last; |
1816 | --__next; | |
ea89b248 | 1817 | while (__comp(__val, __next)) |
dbb8bfe8 | 1818 | { |
f9590eae | 1819 | *__last = _GLIBCXX_MOVE(*__next); |
c2ba9709 JS |
1820 | __last = __next; |
1821 | --__next; | |
dbb8bfe8 | 1822 | } |
f9590eae | 1823 | *__last = _GLIBCXX_MOVE(__val); |
02d92e3b SW |
1824 | } |
1825 | ||
4312e020 | 1826 | /// This is a helper function for the sort routine. |
c2ba9709 JS |
1827 | template<typename _RandomAccessIterator, typename _Compare> |
1828 | void | |
1829 | __insertion_sort(_RandomAccessIterator __first, | |
1830 | _RandomAccessIterator __last, _Compare __comp) | |
02d92e3b | 1831 | { |
c2ba9709 JS |
1832 | if (__first == __last) return; |
1833 | ||
1834 | for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) | |
ffa67767 | 1835 | { |
ea89b248 | 1836 | if (__comp(__i, __first)) |
c2ba9709 | 1837 | { |
f9590eae CJ |
1838 | typename iterator_traits<_RandomAccessIterator>::value_type |
1839 | __val = _GLIBCXX_MOVE(*__i); | |
1840 | _GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1); | |
1841 | *__first = _GLIBCXX_MOVE(__val); | |
c2ba9709 JS |
1842 | } |
1843 | else | |
ea89b248 FD |
1844 | std::__unguarded_linear_insert(__i, |
1845 | __gnu_cxx::__ops::__val_comp_iter(__comp)); | |
ffa67767 | 1846 | } |
02d92e3b SW |
1847 | } |
1848 | ||
4312e020 | 1849 | /// This is a helper function for the sort routine. |
c2ba9709 JS |
1850 | template<typename _RandomAccessIterator, typename _Compare> |
1851 | inline void | |
1852 | __unguarded_insertion_sort(_RandomAccessIterator __first, | |
1853 | _RandomAccessIterator __last, _Compare __comp) | |
02d92e3b | 1854 | { |
c2ba9709 | 1855 | for (_RandomAccessIterator __i = __first; __i != __last; ++__i) |
ea89b248 FD |
1856 | std::__unguarded_linear_insert(__i, |
1857 | __gnu_cxx::__ops::__val_comp_iter(__comp)); | |
c2ba9709 | 1858 | } |
c0d88161 | 1859 | |
c2ba9709 | 1860 | /** |
c2ba9709 JS |
1861 | * @doctodo |
1862 | * This controls some aspect of the sort routines. | |
c2ba9709 JS |
1863 | */ |
1864 | enum { _S_threshold = 16 }; | |
c0d88161 | 1865 | |
4312e020 | 1866 | /// This is a helper function for the sort routine. |
c2ba9709 | 1867 | template<typename _RandomAccessIterator, typename _Compare> |
02d92e3b | 1868 | void |
c2ba9709 JS |
1869 | __final_insertion_sort(_RandomAccessIterator __first, |
1870 | _RandomAccessIterator __last, _Compare __comp) | |
02d92e3b | 1871 | { |
c2ba9709 | 1872 | if (__last - __first > int(_S_threshold)) |
ffa67767 | 1873 | { |
c2ba9709 JS |
1874 | std::__insertion_sort(__first, __first + int(_S_threshold), __comp); |
1875 | std::__unguarded_insertion_sort(__first + int(_S_threshold), __last, | |
1876 | __comp); | |
02d92e3b | 1877 | } |
c2ba9709 JS |
1878 | else |
1879 | std::__insertion_sort(__first, __last, __comp); | |
1880 | } | |
c0d88161 | 1881 | |
4312e020 | 1882 | /// This is a helper function... |
ea89b248 | 1883 | template<typename _RandomAccessIterator, typename _Compare> |
c2ba9709 JS |
1884 | _RandomAccessIterator |
1885 | __unguarded_partition(_RandomAccessIterator __first, | |
1886 | _RandomAccessIterator __last, | |
ea89b248 | 1887 | _RandomAccessIterator __pivot, _Compare __comp) |
02d92e3b | 1888 | { |
c2ba9709 JS |
1889 | while (true) |
1890 | { | |
ea89b248 | 1891 | while (__comp(__first, __pivot)) |
c2ba9709 JS |
1892 | ++__first; |
1893 | --__last; | |
ea89b248 | 1894 | while (__comp(__pivot, __last)) |
c2ba9709 JS |
1895 | --__last; |
1896 | if (!(__first < __last)) | |
1897 | return __first; | |
1898 | std::iter_swap(__first, __last); | |
1899 | ++__first; | |
1900 | } | |
02d92e3b SW |
1901 | } |
1902 | ||
f9590eae CJ |
1903 | /// This is a helper function... |
1904 | template<typename _RandomAccessIterator, typename _Compare> | |
1905 | inline _RandomAccessIterator | |
1906 | __unguarded_partition_pivot(_RandomAccessIterator __first, | |
1907 | _RandomAccessIterator __last, _Compare __comp) | |
1908 | { | |
1909 | _RandomAccessIterator __mid = __first + (__last - __first) / 2; | |
f5b9d5e8 | 1910 | std::__move_median_to_first(__first, __first + 1, __mid, __last - 1, |
a10bad86 | 1911 | __comp); |
ea89b248 | 1912 | return std::__unguarded_partition(__first + 1, __last, __first, __comp); |
f9590eae CJ |
1913 | } |
1914 | ||
ea89b248 FD |
1915 | template<typename _RandomAccessIterator, typename _Compare> |
1916 | inline void | |
1917 | __partial_sort(_RandomAccessIterator __first, | |
1918 | _RandomAccessIterator __middle, | |
1919 | _RandomAccessIterator __last, | |
1920 | _Compare __comp) | |
02d92e3b | 1921 | { |
ea89b248 FD |
1922 | std::__heap_select(__first, __middle, __last, __comp); |
1923 | std::__sort_heap(__first, __middle, __comp); | |
02d92e3b SW |
1924 | } |
1925 | ||
4312e020 | 1926 | /// This is a helper function for the sort routine. |
c2ba9709 | 1927 | template<typename _RandomAccessIterator, typename _Size, typename _Compare> |
02d92e3b | 1928 | void |
c2ba9709 JS |
1929 | __introsort_loop(_RandomAccessIterator __first, |
1930 | _RandomAccessIterator __last, | |
1931 | _Size __depth_limit, _Compare __comp) | |
02d92e3b | 1932 | { |
c2ba9709 JS |
1933 | while (__last - __first > int(_S_threshold)) |
1934 | { | |
1935 | if (__depth_limit == 0) | |
1936 | { | |
ea89b248 | 1937 | std::__partial_sort(__first, __last, __last, __comp); |
c2ba9709 JS |
1938 | return; |
1939 | } | |
1940 | --__depth_limit; | |
1941 | _RandomAccessIterator __cut = | |
f9590eae | 1942 | std::__unguarded_partition_pivot(__first, __last, __comp); |
c2ba9709 JS |
1943 | std::__introsort_loop(__cut, __last, __depth_limit, __comp); |
1944 | __last = __cut; | |
1945 | } | |
02d92e3b SW |
1946 | } |
1947 | ||
c2ba9709 | 1948 | // sort |
725dc051 | 1949 | |
ea89b248 FD |
1950 | template<typename _RandomAccessIterator, typename _Compare> |
1951 | inline void | |
1952 | __sort(_RandomAccessIterator __first, _RandomAccessIterator __last, | |
1953 | _Compare __comp) | |
c2ba9709 | 1954 | { |
ea89b248 | 1955 | if (__first != __last) |
c2ba9709 | 1956 | { |
ea89b248 FD |
1957 | std::__introsort_loop(__first, __last, |
1958 | std::__lg(__last - __first) * 2, | |
1959 | __comp); | |
1960 | std::__final_insertion_sort(__first, __last, __comp); | |
c2ba9709 | 1961 | } |
02d92e3b | 1962 | } |
725dc051 | 1963 | |
c2ba9709 JS |
1964 | template<typename _RandomAccessIterator, typename _Size, typename _Compare> |
1965 | void | |
1966 | __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth, | |
1967 | _RandomAccessIterator __last, _Size __depth_limit, | |
1968 | _Compare __comp) | |
02d92e3b | 1969 | { |
c2ba9709 | 1970 | while (__last - __first > 3) |
ffa67767 | 1971 | { |
c2ba9709 JS |
1972 | if (__depth_limit == 0) |
1973 | { | |
1974 | std::__heap_select(__first, __nth + 1, __last, __comp); | |
1975 | // Place the nth largest element in its final position. | |
1976 | std::iter_swap(__first, __nth); | |
1977 | return; | |
1978 | } | |
1979 | --__depth_limit; | |
1980 | _RandomAccessIterator __cut = | |
f9590eae | 1981 | std::__unguarded_partition_pivot(__first, __last, __comp); |
c2ba9709 JS |
1982 | if (__cut <= __nth) |
1983 | __first = __cut; | |
1984 | else | |
1985 | __last = __cut; | |
ffa67767 | 1986 | } |
c2ba9709 | 1987 | std::__insertion_sort(__first, __last, __comp); |
02d92e3b | 1988 | } |
725dc051 | 1989 | |
c2ba9709 JS |
1990 | // nth_element |
1991 | ||
247d8075 | 1992 | // lower_bound moved to stl_algobase.h |
02d92e3b | 1993 | |
7c842056 | 1994 | /** |
7897a1c0 | 1995 | * @brief Finds the first position in which @p __val could be inserted |
7c842056 PC |
1996 | * without changing the ordering. |
1997 | * @ingroup binary_search_algorithms | |
93c66bc6 BK |
1998 | * @param __first An iterator. |
1999 | * @param __last Another iterator. | |
2000 | * @param __val The search term. | |
2001 | * @param __comp A functor to use for comparisons. | |
7c842056 | 2002 | * @return An iterator pointing to the first element <em>not less |
7897a1c0 BK |
2003 | * than</em> @p __val, or end() if every element is less |
2004 | * than @p __val. | |
7c842056 PC |
2005 | * @ingroup binary_search_algorithms |
2006 | * | |
2007 | * The comparison function should have the same effects on ordering as | |
2008 | * the function used for the initial sort. | |
2009 | */ | |
2010 | template<typename _ForwardIterator, typename _Tp, typename _Compare> | |
3bd2644c | 2011 | inline _ForwardIterator |
7c842056 PC |
2012 | lower_bound(_ForwardIterator __first, _ForwardIterator __last, |
2013 | const _Tp& __val, _Compare __comp) | |
2014 | { | |
7c842056 PC |
2015 | // concept requirements |
2016 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
2017 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, | |
ec494945 | 2018 | typename iterator_traits<_ForwardIterator>::value_type, _Tp>) |
7c842056 PC |
2019 | __glibcxx_requires_partitioned_lower_pred(__first, __last, |
2020 | __val, __comp); | |
2021 | ||
ea89b248 FD |
2022 | return std::__lower_bound(__first, __last, __val, |
2023 | __gnu_cxx::__ops::__iter_comp_val(__comp)); | |
2024 | } | |
2025 | ||
2026 | template<typename _ForwardIterator, typename _Tp, typename _Compare> | |
2027 | _ForwardIterator | |
2028 | __upper_bound(_ForwardIterator __first, _ForwardIterator __last, | |
2029 | const _Tp& __val, _Compare __comp) | |
2030 | { | |
2031 | typedef typename iterator_traits<_ForwardIterator>::difference_type | |
2032 | _DistanceType; | |
2033 | ||
7c842056 | 2034 | _DistanceType __len = std::distance(__first, __last); |
7c842056 PC |
2035 | |
2036 | while (__len > 0) | |
2037 | { | |
1ed78d6c CY |
2038 | _DistanceType __half = __len >> 1; |
2039 | _ForwardIterator __middle = __first; | |
7c842056 | 2040 | std::advance(__middle, __half); |
ea89b248 FD |
2041 | if (__comp(__val, __middle)) |
2042 | __len = __half; | |
2043 | else | |
7c842056 PC |
2044 | { |
2045 | __first = __middle; | |
2046 | ++__first; | |
2047 | __len = __len - __half - 1; | |
2048 | } | |
7c842056 PC |
2049 | } |
2050 | return __first; | |
2051 | } | |
2052 | ||
037cfe71 | 2053 | /** |
7897a1c0 | 2054 | * @brief Finds the last position in which @p __val could be inserted |
c2ba9709 | 2055 | * without changing the ordering. |
5b9daa7e | 2056 | * @ingroup binary_search_algorithms |
93c66bc6 BK |
2057 | * @param __first An iterator. |
2058 | * @param __last Another iterator. | |
2059 | * @param __val The search term. | |
7897a1c0 BK |
2060 | * @return An iterator pointing to the first element greater than @p __val, |
2061 | * or end() if no elements are greater than @p __val. | |
aac2878e | 2062 | * @ingroup binary_search_algorithms |
037cfe71 | 2063 | */ |
c2ba9709 | 2064 | template<typename _ForwardIterator, typename _Tp> |
3bd2644c | 2065 | inline _ForwardIterator |
c2ba9709 JS |
2066 | upper_bound(_ForwardIterator __first, _ForwardIterator __last, |
2067 | const _Tp& __val) | |
02d92e3b | 2068 | { |
c2ba9709 JS |
2069 | // concept requirements |
2070 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
ec494945 FD |
2071 | __glibcxx_function_requires(_LessThanOpConcept< |
2072 | _Tp, typename iterator_traits<_ForwardIterator>::value_type>) | |
f5ad3163 | 2073 | __glibcxx_requires_partitioned_upper(__first, __last, __val); |
c2ba9709 | 2074 | |
ea89b248 FD |
2075 | return std::__upper_bound(__first, __last, __val, |
2076 | __gnu_cxx::__ops::__val_less_iter()); | |
725dc051 BK |
2077 | } |
2078 | ||
037cfe71 | 2079 | /** |
7897a1c0 | 2080 | * @brief Finds the last position in which @p __val could be inserted |
c2ba9709 | 2081 | * without changing the ordering. |
5b9daa7e | 2082 | * @ingroup binary_search_algorithms |
93c66bc6 BK |
2083 | * @param __first An iterator. |
2084 | * @param __last Another iterator. | |
2085 | * @param __val The search term. | |
2086 | * @param __comp A functor to use for comparisons. | |
7897a1c0 BK |
2087 | * @return An iterator pointing to the first element greater than @p __val, |
2088 | * or end() if no elements are greater than @p __val. | |
aac2878e | 2089 | * @ingroup binary_search_algorithms |
ed6814f7 | 2090 | * |
c2ba9709 JS |
2091 | * The comparison function should have the same effects on ordering as |
2092 | * the function used for the initial sort. | |
037cfe71 | 2093 | */ |
c2ba9709 | 2094 | template<typename _ForwardIterator, typename _Tp, typename _Compare> |
3bd2644c | 2095 | inline _ForwardIterator |
c2ba9709 JS |
2096 | upper_bound(_ForwardIterator __first, _ForwardIterator __last, |
2097 | const _Tp& __val, _Compare __comp) | |
02d92e3b SW |
2098 | { |
2099 | // concept requirements | |
c2ba9709 JS |
2100 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
2101 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, | |
ec494945 | 2102 | _Tp, typename iterator_traits<_ForwardIterator>::value_type>) |
f5ad3163 PC |
2103 | __glibcxx_requires_partitioned_upper_pred(__first, __last, |
2104 | __val, __comp); | |
c0d88161 | 2105 | |
ea89b248 FD |
2106 | return std::__upper_bound(__first, __last, __val, |
2107 | __gnu_cxx::__ops::__val_comp_iter(__comp)); | |
2108 | } | |
2109 | ||
2110 | template<typename _ForwardIterator, typename _Tp, | |
2111 | typename _CompareItTp, typename _CompareTpIt> | |
2112 | pair<_ForwardIterator, _ForwardIterator> | |
2113 | __equal_range(_ForwardIterator __first, _ForwardIterator __last, | |
2114 | const _Tp& __val, | |
2115 | _CompareItTp __comp_it_val, _CompareTpIt __comp_val_it) | |
2116 | { | |
2117 | typedef typename iterator_traits<_ForwardIterator>::difference_type | |
2118 | _DistanceType; | |
2119 | ||
c2ba9709 | 2120 | _DistanceType __len = std::distance(__first, __last); |
ffa67767 | 2121 | |
c2ba9709 JS |
2122 | while (__len > 0) |
2123 | { | |
1ed78d6c CY |
2124 | _DistanceType __half = __len >> 1; |
2125 | _ForwardIterator __middle = __first; | |
c2ba9709 | 2126 | std::advance(__middle, __half); |
ea89b248 | 2127 | if (__comp_it_val(__middle, __val)) |
c2ba9709 JS |
2128 | { |
2129 | __first = __middle; | |
2130 | ++__first; | |
2131 | __len = __len - __half - 1; | |
2132 | } | |
ea89b248 FD |
2133 | else if (__comp_val_it(__val, __middle)) |
2134 | __len = __half; | |
2135 | else | |
2136 | { | |
2137 | _ForwardIterator __left | |
2138 | = std::__lower_bound(__first, __middle, __val, __comp_it_val); | |
2139 | std::advance(__first, __len); | |
2140 | _ForwardIterator __right | |
2141 | = std::__upper_bound(++__middle, __first, __val, __comp_val_it); | |
2142 | return pair<_ForwardIterator, _ForwardIterator>(__left, __right); | |
2143 | } | |
ffa67767 | 2144 | } |
ea89b248 | 2145 | return pair<_ForwardIterator, _ForwardIterator>(__first, __first); |
02d92e3b | 2146 | } |
725dc051 | 2147 | |
037cfe71 | 2148 | /** |
7897a1c0 | 2149 | * @brief Finds the largest subrange in which @p __val could be inserted |
c2ba9709 | 2150 | * at any place in it without changing the ordering. |
5b9daa7e | 2151 | * @ingroup binary_search_algorithms |
93c66bc6 BK |
2152 | * @param __first An iterator. |
2153 | * @param __last Another iterator. | |
2154 | * @param __val The search term. | |
c2ba9709 | 2155 | * @return An pair of iterators defining the subrange. |
aac2878e | 2156 | * @ingroup binary_search_algorithms |
c2ba9709 JS |
2157 | * |
2158 | * This is equivalent to | |
2159 | * @code | |
93c66bc6 BK |
2160 | * std::make_pair(lower_bound(__first, __last, __val), |
2161 | * upper_bound(__first, __last, __val)) | |
c2ba9709 JS |
2162 | * @endcode |
2163 | * but does not actually call those functions. | |
037cfe71 | 2164 | */ |
c2ba9709 | 2165 | template<typename _ForwardIterator, typename _Tp> |
3bd2644c | 2166 | inline pair<_ForwardIterator, _ForwardIterator> |
c2ba9709 JS |
2167 | equal_range(_ForwardIterator __first, _ForwardIterator __last, |
2168 | const _Tp& __val) | |
02d92e3b | 2169 | { |
c2ba9709 JS |
2170 | // concept requirements |
2171 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
ec494945 FD |
2172 | __glibcxx_function_requires(_LessThanOpConcept< |
2173 | typename iterator_traits<_ForwardIterator>::value_type, _Tp>) | |
2174 | __glibcxx_function_requires(_LessThanOpConcept< | |
2175 | _Tp, typename iterator_traits<_ForwardIterator>::value_type>) | |
f5ad3163 | 2176 | __glibcxx_requires_partitioned_lower(__first, __last, __val); |
630a286a | 2177 | __glibcxx_requires_partitioned_upper(__first, __last, __val); |
c2ba9709 | 2178 | |
ea89b248 FD |
2179 | return std::__equal_range(__first, __last, __val, |
2180 | __gnu_cxx::__ops::__iter_less_val(), | |
2181 | __gnu_cxx::__ops::__val_less_iter()); | |
725dc051 BK |
2182 | } |
2183 | ||
037cfe71 | 2184 | /** |
7897a1c0 | 2185 | * @brief Finds the largest subrange in which @p __val could be inserted |
c2ba9709 | 2186 | * at any place in it without changing the ordering. |
93c66bc6 BK |
2187 | * @param __first An iterator. |
2188 | * @param __last Another iterator. | |
2189 | * @param __val The search term. | |
2190 | * @param __comp A functor to use for comparisons. | |
c2ba9709 | 2191 | * @return An pair of iterators defining the subrange. |
aac2878e | 2192 | * @ingroup binary_search_algorithms |
c2ba9709 JS |
2193 | * |
2194 | * This is equivalent to | |
2195 | * @code | |
93c66bc6 BK |
2196 | * std::make_pair(lower_bound(__first, __last, __val, __comp), |
2197 | * upper_bound(__first, __last, __val, __comp)) | |
c2ba9709 JS |
2198 | * @endcode |
2199 | * but does not actually call those functions. | |
037cfe71 | 2200 | */ |
c2ba9709 | 2201 | template<typename _ForwardIterator, typename _Tp, typename _Compare> |
3bd2644c | 2202 | inline pair<_ForwardIterator, _ForwardIterator> |
c2ba9709 | 2203 | equal_range(_ForwardIterator __first, _ForwardIterator __last, |
1ed78d6c | 2204 | const _Tp& __val, _Compare __comp) |
02d92e3b | 2205 | { |
c2ba9709 JS |
2206 | // concept requirements |
2207 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
2208 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, | |
ec494945 | 2209 | typename iterator_traits<_ForwardIterator>::value_type, _Tp>) |
c2ba9709 | 2210 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
ec494945 | 2211 | _Tp, typename iterator_traits<_ForwardIterator>::value_type>) |
f5ad3163 PC |
2212 | __glibcxx_requires_partitioned_lower_pred(__first, __last, |
2213 | __val, __comp); | |
2214 | __glibcxx_requires_partitioned_upper_pred(__first, __last, | |
2215 | __val, __comp); | |
c2ba9709 | 2216 | |
ea89b248 FD |
2217 | return std::__equal_range(__first, __last, __val, |
2218 | __gnu_cxx::__ops::__iter_comp_val(__comp), | |
2219 | __gnu_cxx::__ops::__val_comp_iter(__comp)); | |
02d92e3b SW |
2220 | } |
2221 | ||
c868f37e | 2222 | /** |
c2ba9709 | 2223 | * @brief Determines whether an element exists in a range. |
5b9daa7e | 2224 | * @ingroup binary_search_algorithms |
93c66bc6 BK |
2225 | * @param __first An iterator. |
2226 | * @param __last Another iterator. | |
2227 | * @param __val The search term. | |
7897a1c0 BK |
2228 | * @return True if @p __val (or its equivalent) is in [@p |
2229 | * __first,@p __last ]. | |
c868f37e | 2230 | * |
7897a1c0 | 2231 | * Note that this does not actually return an iterator to @p __val. For |
c2ba9709 | 2232 | * that, use std::find or a container's specialized find member functions. |
c868f37e | 2233 | */ |
c2ba9709 JS |
2234 | template<typename _ForwardIterator, typename _Tp> |
2235 | bool | |
2236 | binary_search(_ForwardIterator __first, _ForwardIterator __last, | |
ec494945 | 2237 | const _Tp& __val) |
c868f37e | 2238 | { |
c868f37e | 2239 | // concept requirements |
c2ba9709 | 2240 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
ec494945 FD |
2241 | __glibcxx_function_requires(_LessThanOpConcept< |
2242 | _Tp, typename iterator_traits<_ForwardIterator>::value_type>) | |
f5ad3163 PC |
2243 | __glibcxx_requires_partitioned_lower(__first, __last, __val); |
2244 | __glibcxx_requires_partitioned_upper(__first, __last, __val); | |
c868f37e | 2245 | |
ea89b248 FD |
2246 | _ForwardIterator __i |
2247 | = std::__lower_bound(__first, __last, __val, | |
2248 | __gnu_cxx::__ops::__iter_less_val()); | |
c2ba9709 | 2249 | return __i != __last && !(__val < *__i); |
c868f37e PC |
2250 | } |
2251 | ||
2252 | /** | |
c2ba9709 | 2253 | * @brief Determines whether an element exists in a range. |
5b9daa7e | 2254 | * @ingroup binary_search_algorithms |
93c66bc6 BK |
2255 | * @param __first An iterator. |
2256 | * @param __last Another iterator. | |
2257 | * @param __val The search term. | |
2258 | * @param __comp A functor to use for comparisons. | |
7897a1c0 | 2259 | * @return True if @p __val (or its equivalent) is in @p [__first,__last]. |
c868f37e | 2260 | * |
7897a1c0 | 2261 | * Note that this does not actually return an iterator to @p __val. For |
c2ba9709 JS |
2262 | * that, use std::find or a container's specialized find member functions. |
2263 | * | |
2264 | * The comparison function should have the same effects on ordering as | |
2265 | * the function used for the initial sort. | |
c868f37e | 2266 | */ |
c2ba9709 JS |
2267 | template<typename _ForwardIterator, typename _Tp, typename _Compare> |
2268 | bool | |
2269 | binary_search(_ForwardIterator __first, _ForwardIterator __last, | |
ec494945 | 2270 | const _Tp& __val, _Compare __comp) |
c868f37e | 2271 | { |
c868f37e | 2272 | // concept requirements |
c2ba9709 | 2273 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
3d7c150e | 2274 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
ec494945 | 2275 | _Tp, typename iterator_traits<_ForwardIterator>::value_type>) |
f5ad3163 PC |
2276 | __glibcxx_requires_partitioned_lower_pred(__first, __last, |
2277 | __val, __comp); | |
2278 | __glibcxx_requires_partitioned_upper_pred(__first, __last, | |
2279 | __val, __comp); | |
c868f37e | 2280 | |
ea89b248 FD |
2281 | _ForwardIterator __i |
2282 | = std::__lower_bound(__first, __last, __val, | |
2283 | __gnu_cxx::__ops::__iter_comp_val(__comp)); | |
c2ba9709 | 2284 | return __i != __last && !bool(__comp(__val, *__i)); |
c868f37e PC |
2285 | } |
2286 | ||
c2ba9709 JS |
2287 | // merge |
2288 | ||
848ca96f PC |
2289 | /// This is a helper function for the __merge_adaptive routines. |
2290 | template<typename _InputIterator1, typename _InputIterator2, | |
2291 | typename _OutputIterator, typename _Compare> | |
2292 | void | |
2293 | __move_merge_adaptive(_InputIterator1 __first1, _InputIterator1 __last1, | |
2294 | _InputIterator2 __first2, _InputIterator2 __last2, | |
2295 | _OutputIterator __result, _Compare __comp) | |
2296 | { | |
2297 | while (__first1 != __last1 && __first2 != __last2) | |
2298 | { | |
ea89b248 | 2299 | if (__comp(__first2, __first1)) |
848ca96f PC |
2300 | { |
2301 | *__result = _GLIBCXX_MOVE(*__first2); | |
2302 | ++__first2; | |
2303 | } | |
2304 | else | |
2305 | { | |
2306 | *__result = _GLIBCXX_MOVE(*__first1); | |
2307 | ++__first1; | |
2308 | } | |
2309 | ++__result; | |
2310 | } | |
2311 | if (__first1 != __last1) | |
2312 | _GLIBCXX_MOVE3(__first1, __last1, __result); | |
2313 | } | |
2314 | ||
848ca96f | 2315 | /// This is a helper function for the __merge_adaptive routines. |
c2ba9709 JS |
2316 | template<typename _BidirectionalIterator1, typename _BidirectionalIterator2, |
2317 | typename _BidirectionalIterator3, typename _Compare> | |
848ca96f PC |
2318 | void |
2319 | __move_merge_adaptive_backward(_BidirectionalIterator1 __first1, | |
2320 | _BidirectionalIterator1 __last1, | |
2321 | _BidirectionalIterator2 __first2, | |
2322 | _BidirectionalIterator2 __last2, | |
2323 | _BidirectionalIterator3 __result, | |
2324 | _Compare __comp) | |
02d92e3b | 2325 | { |
c2ba9709 | 2326 | if (__first1 == __last1) |
848ca96f PC |
2327 | { |
2328 | _GLIBCXX_MOVE_BACKWARD3(__first2, __last2, __result); | |
2329 | return; | |
2330 | } | |
2331 | else if (__first2 == __last2) | |
2332 | return; | |
2333 | ||
c2ba9709 JS |
2334 | --__last1; |
2335 | --__last2; | |
2336 | while (true) | |
ffa67767 | 2337 | { |
ea89b248 | 2338 | if (__comp(__last2, __last1)) |
c2ba9709 | 2339 | { |
60c5236e | 2340 | *--__result = _GLIBCXX_MOVE(*__last1); |
c2ba9709 | 2341 | if (__first1 == __last1) |
848ca96f PC |
2342 | { |
2343 | _GLIBCXX_MOVE_BACKWARD3(__first2, ++__last2, __result); | |
2344 | return; | |
2345 | } | |
c2ba9709 JS |
2346 | --__last1; |
2347 | } | |
2348 | else | |
2349 | { | |
60c5236e | 2350 | *--__result = _GLIBCXX_MOVE(*__last2); |
c2ba9709 | 2351 | if (__first2 == __last2) |
848ca96f | 2352 | return; |
c2ba9709 JS |
2353 | --__last2; |
2354 | } | |
02d92e3b | 2355 | } |
02d92e3b | 2356 | } |
725dc051 | 2357 | |
4312e020 | 2358 | /// This is a helper function for the merge routines. |
c2ba9709 JS |
2359 | template<typename _BidirectionalIterator1, typename _BidirectionalIterator2, |
2360 | typename _Distance> | |
2361 | _BidirectionalIterator1 | |
2362 | __rotate_adaptive(_BidirectionalIterator1 __first, | |
2363 | _BidirectionalIterator1 __middle, | |
2364 | _BidirectionalIterator1 __last, | |
2365 | _Distance __len1, _Distance __len2, | |
2366 | _BidirectionalIterator2 __buffer, | |
2367 | _Distance __buffer_size) | |
02d92e3b | 2368 | { |
c2ba9709 JS |
2369 | _BidirectionalIterator2 __buffer_end; |
2370 | if (__len1 > __len2 && __len2 <= __buffer_size) | |
ffa67767 | 2371 | { |
848ca96f PC |
2372 | if (__len2) |
2373 | { | |
2374 | __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer); | |
2375 | _GLIBCXX_MOVE_BACKWARD3(__first, __middle, __last); | |
2376 | return _GLIBCXX_MOVE3(__buffer, __buffer_end, __first); | |
2377 | } | |
2378 | else | |
2379 | return __first; | |
c2ba9709 JS |
2380 | } |
2381 | else if (__len1 <= __buffer_size) | |
2382 | { | |
848ca96f PC |
2383 | if (__len1) |
2384 | { | |
2385 | __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer); | |
2386 | _GLIBCXX_MOVE3(__middle, __last, __first); | |
2387 | return _GLIBCXX_MOVE_BACKWARD3(__buffer, __buffer_end, __last); | |
2388 | } | |
2389 | else | |
2390 | return __last; | |
c2ba9709 JS |
2391 | } |
2392 | else | |
29a9c26c | 2393 | return std::rotate(__first, __middle, __last); |
02d92e3b | 2394 | } |
725dc051 | 2395 | |
4312e020 | 2396 | /// This is a helper function for the merge routines. |
c2ba9709 JS |
2397 | template<typename _BidirectionalIterator, typename _Distance, |
2398 | typename _Pointer, typename _Compare> | |
da5a2efd | 2399 | void |
c2ba9709 | 2400 | __merge_adaptive(_BidirectionalIterator __first, |
ec494945 | 2401 | _BidirectionalIterator __middle, |
c2ba9709 JS |
2402 | _BidirectionalIterator __last, |
2403 | _Distance __len1, _Distance __len2, | |
2404 | _Pointer __buffer, _Distance __buffer_size, | |
2405 | _Compare __comp) | |
02d92e3b | 2406 | { |
c2ba9709 | 2407 | if (__len1 <= __len2 && __len1 <= __buffer_size) |
ffa67767 | 2408 | { |
fe03b3df | 2409 | _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer); |
848ca96f PC |
2410 | std::__move_merge_adaptive(__buffer, __buffer_end, __middle, __last, |
2411 | __first, __comp); | |
c2ba9709 JS |
2412 | } |
2413 | else if (__len2 <= __buffer_size) | |
2414 | { | |
fe03b3df | 2415 | _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer); |
848ca96f PC |
2416 | std::__move_merge_adaptive_backward(__first, __middle, __buffer, |
2417 | __buffer_end, __last, __comp); | |
c2ba9709 JS |
2418 | } |
2419 | else | |
2420 | { | |
2421 | _BidirectionalIterator __first_cut = __first; | |
2422 | _BidirectionalIterator __second_cut = __middle; | |
2423 | _Distance __len11 = 0; | |
2424 | _Distance __len22 = 0; | |
2425 | if (__len1 > __len2) | |
da5a2efd | 2426 | { |
c2ba9709 JS |
2427 | __len11 = __len1 / 2; |
2428 | std::advance(__first_cut, __len11); | |
ea89b248 FD |
2429 | __second_cut |
2430 | = std::__lower_bound(__middle, __last, *__first_cut, | |
2431 | __gnu_cxx::__ops::__iter_comp_val(__comp)); | |
c2ba9709 | 2432 | __len22 = std::distance(__middle, __second_cut); |
da5a2efd PC |
2433 | } |
2434 | else | |
c2ba9709 JS |
2435 | { |
2436 | __len22 = __len2 / 2; | |
2437 | std::advance(__second_cut, __len22); | |
ea89b248 FD |
2438 | __first_cut |
2439 | = std::__upper_bound(__first, __middle, *__second_cut, | |
2440 | __gnu_cxx::__ops::__val_comp_iter(__comp)); | |
c2ba9709 JS |
2441 | __len11 = std::distance(__first, __first_cut); |
2442 | } | |
970a9caa | 2443 | |
ea89b248 FD |
2444 | _BidirectionalIterator __new_middle |
2445 | = std::__rotate_adaptive(__first_cut, __middle, __second_cut, | |
2446 | __len1 - __len11, __len22, __buffer, | |
2447 | __buffer_size); | |
c2ba9709 JS |
2448 | std::__merge_adaptive(__first, __first_cut, __new_middle, __len11, |
2449 | __len22, __buffer, __buffer_size, __comp); | |
2450 | std::__merge_adaptive(__new_middle, __second_cut, __last, | |
2451 | __len1 - __len11, | |
2452 | __len2 - __len22, __buffer, | |
2453 | __buffer_size, __comp); | |
ffa67767 | 2454 | } |
02d92e3b | 2455 | } |
725dc051 | 2456 | |
4312e020 | 2457 | /// This is a helper function for the merge routines. |
c2ba9709 JS |
2458 | template<typename _BidirectionalIterator, typename _Distance, |
2459 | typename _Compare> | |
da5a2efd | 2460 | void |
c2ba9709 | 2461 | __merge_without_buffer(_BidirectionalIterator __first, |
ec494945 | 2462 | _BidirectionalIterator __middle, |
c2ba9709 JS |
2463 | _BidirectionalIterator __last, |
2464 | _Distance __len1, _Distance __len2, | |
2465 | _Compare __comp) | |
02d92e3b | 2466 | { |
c2ba9709 JS |
2467 | if (__len1 == 0 || __len2 == 0) |
2468 | return; | |
970a9caa | 2469 | |
c2ba9709 | 2470 | if (__len1 + __len2 == 2) |
ffa67767 | 2471 | { |
ea89b248 | 2472 | if (__comp(__middle, __first)) |
c2ba9709 JS |
2473 | std::iter_swap(__first, __middle); |
2474 | return; | |
2475 | } | |
970a9caa | 2476 | |
c2ba9709 JS |
2477 | _BidirectionalIterator __first_cut = __first; |
2478 | _BidirectionalIterator __second_cut = __middle; | |
2479 | _Distance __len11 = 0; | |
2480 | _Distance __len22 = 0; | |
2481 | if (__len1 > __len2) | |
2482 | { | |
2483 | __len11 = __len1 / 2; | |
2484 | std::advance(__first_cut, __len11); | |
ea89b248 FD |
2485 | __second_cut |
2486 | = std::__lower_bound(__middle, __last, *__first_cut, | |
2487 | __gnu_cxx::__ops::__iter_comp_val(__comp)); | |
c2ba9709 | 2488 | __len22 = std::distance(__middle, __second_cut); |
c868f37e | 2489 | } |
da5a2efd | 2490 | else |
ffa67767 | 2491 | { |
c2ba9709 JS |
2492 | __len22 = __len2 / 2; |
2493 | std::advance(__second_cut, __len22); | |
ea89b248 FD |
2494 | __first_cut |
2495 | = std::__upper_bound(__first, __middle, *__second_cut, | |
2496 | __gnu_cxx::__ops::__val_comp_iter(__comp)); | |
c2ba9709 | 2497 | __len11 = std::distance(__first, __first_cut); |
c868f37e | 2498 | } |
970a9caa | 2499 | |
29a9c26c FD |
2500 | _BidirectionalIterator __new_middle |
2501 | = std::rotate(__first_cut, __middle, __second_cut); | |
c2ba9709 JS |
2502 | std::__merge_without_buffer(__first, __first_cut, __new_middle, |
2503 | __len11, __len22, __comp); | |
2504 | std::__merge_without_buffer(__new_middle, __second_cut, __last, | |
2505 | __len1 - __len11, __len2 - __len22, __comp); | |
02d92e3b SW |
2506 | } |
2507 | ||
ea89b248 FD |
2508 | template<typename _BidirectionalIterator, typename _Compare> |
2509 | void | |
2510 | __inplace_merge(_BidirectionalIterator __first, | |
2511 | _BidirectionalIterator __middle, | |
2512 | _BidirectionalIterator __last, | |
2513 | _Compare __comp) | |
2514 | { | |
2515 | typedef typename iterator_traits<_BidirectionalIterator>::value_type | |
ec494945 | 2516 | _ValueType; |
ea89b248 | 2517 | typedef typename iterator_traits<_BidirectionalIterator>::difference_type |
ec494945 | 2518 | _DistanceType; |
ea89b248 FD |
2519 | |
2520 | if (__first == __middle || __middle == __last) | |
2521 | return; | |
2522 | ||
2523 | const _DistanceType __len1 = std::distance(__first, __middle); | |
2524 | const _DistanceType __len2 = std::distance(__middle, __last); | |
2525 | ||
2526 | typedef _Temporary_buffer<_BidirectionalIterator, _ValueType> _TmpBuf; | |
d31238cf | 2527 | _TmpBuf __buf(__first, __len1 + __len2); |
ea89b248 FD |
2528 | |
2529 | if (__buf.begin() == 0) | |
2530 | std::__merge_without_buffer | |
2531 | (__first, __middle, __last, __len1, __len2, __comp); | |
2532 | else | |
2533 | std::__merge_adaptive | |
2534 | (__first, __middle, __last, __len1, __len2, __buf.begin(), | |
2535 | _DistanceType(__buf.size()), __comp); | |
2536 | } | |
2537 | ||
c868f37e | 2538 | /** |
c2ba9709 | 2539 | * @brief Merges two sorted ranges in place. |
5b9daa7e | 2540 | * @ingroup sorting_algorithms |
93c66bc6 BK |
2541 | * @param __first An iterator. |
2542 | * @param __middle Another iterator. | |
2543 | * @param __last Another iterator. | |
da5a2efd | 2544 | * @return Nothing. |
037cfe71 | 2545 | * |
93c66bc6 BK |
2546 | * Merges two sorted and consecutive ranges, [__first,__middle) and |
2547 | * [__middle,__last), and puts the result in [__first,__last). The | |
2548 | * output will be sorted. The sort is @e stable, that is, for | |
2549 | * equivalent elements in the two ranges, elements from the first | |
2550 | * range will always come before elements from the second. | |
da5a2efd | 2551 | * |
93c66bc6 | 2552 | * If enough additional memory is available, this takes (__last-__first)-1 |
c2ba9709 | 2553 | * comparisons. Otherwise an NlogN algorithm is used, where N is |
93c66bc6 | 2554 | * distance(__first,__last). |
037cfe71 | 2555 | */ |
c2ba9709 | 2556 | template<typename _BidirectionalIterator> |
3bd2644c | 2557 | inline void |
c2ba9709 JS |
2558 | inplace_merge(_BidirectionalIterator __first, |
2559 | _BidirectionalIterator __middle, | |
2560 | _BidirectionalIterator __last) | |
02d92e3b | 2561 | { |
02d92e3b | 2562 | // concept requirements |
c2ba9709 JS |
2563 | __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< |
2564 | _BidirectionalIterator>) | |
ea89b248 FD |
2565 | __glibcxx_function_requires(_LessThanComparableConcept< |
2566 | typename iterator_traits<_BidirectionalIterator>::value_type>) | |
c2ba9709 JS |
2567 | __glibcxx_requires_sorted(__first, __middle); |
2568 | __glibcxx_requires_sorted(__middle, __last); | |
630a286a | 2569 | __glibcxx_requires_irreflexive(__first, __last); |
c0d88161 | 2570 | |
ea89b248 FD |
2571 | std::__inplace_merge(__first, __middle, __last, |
2572 | __gnu_cxx::__ops::__iter_less_iter()); | |
02d92e3b SW |
2573 | } |
2574 | ||
037cfe71 | 2575 | /** |
c2ba9709 | 2576 | * @brief Merges two sorted ranges in place. |
5b9daa7e | 2577 | * @ingroup sorting_algorithms |
93c66bc6 BK |
2578 | * @param __first An iterator. |
2579 | * @param __middle Another iterator. | |
2580 | * @param __last Another iterator. | |
2581 | * @param __comp A functor to use for comparisons. | |
da5a2efd | 2582 | * @return Nothing. |
037cfe71 | 2583 | * |
93c66bc6 BK |
2584 | * Merges two sorted and consecutive ranges, [__first,__middle) and |
2585 | * [middle,last), and puts the result in [__first,__last). The output will | |
c2ba9709 JS |
2586 | * be sorted. The sort is @e stable, that is, for equivalent |
2587 | * elements in the two ranges, elements from the first range will always | |
2588 | * come before elements from the second. | |
c868f37e | 2589 | * |
93c66bc6 | 2590 | * If enough additional memory is available, this takes (__last-__first)-1 |
c2ba9709 | 2591 | * comparisons. Otherwise an NlogN algorithm is used, where N is |
93c66bc6 | 2592 | * distance(__first,__last). |
c2ba9709 JS |
2593 | * |
2594 | * The comparison function should have the same effects on ordering as | |
2595 | * the function used for the initial sort. | |
037cfe71 | 2596 | */ |
c2ba9709 | 2597 | template<typename _BidirectionalIterator, typename _Compare> |
3bd2644c | 2598 | inline void |
c2ba9709 JS |
2599 | inplace_merge(_BidirectionalIterator __first, |
2600 | _BidirectionalIterator __middle, | |
2601 | _BidirectionalIterator __last, | |
2602 | _Compare __comp) | |
02d92e3b | 2603 | { |
02d92e3b | 2604 | // concept requirements |
c2ba9709 JS |
2605 | __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< |
2606 | _BidirectionalIterator>) | |
2607 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, | |
ea89b248 FD |
2608 | typename iterator_traits<_BidirectionalIterator>::value_type, |
2609 | typename iterator_traits<_BidirectionalIterator>::value_type>) | |
c2ba9709 JS |
2610 | __glibcxx_requires_sorted_pred(__first, __middle, __comp); |
2611 | __glibcxx_requires_sorted_pred(__middle, __last, __comp); | |
630a286a | 2612 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
c0d88161 | 2613 | |
ea89b248 FD |
2614 | std::__inplace_merge(__first, __middle, __last, |
2615 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); | |
02d92e3b SW |
2616 | } |
2617 | ||
848ca96f PC |
2618 | |
2619 | /// This is a helper function for the __merge_sort_loop routines. | |
ea89b248 FD |
2620 | template<typename _InputIterator, typename _OutputIterator, |
2621 | typename _Compare> | |
848ca96f | 2622 | _OutputIterator |
ea89b248 FD |
2623 | __move_merge(_InputIterator __first1, _InputIterator __last1, |
2624 | _InputIterator __first2, _InputIterator __last2, | |
848ca96f PC |
2625 | _OutputIterator __result, _Compare __comp) |
2626 | { | |
2627 | while (__first1 != __last1 && __first2 != __last2) | |
2628 | { | |
ea89b248 | 2629 | if (__comp(__first2, __first1)) |
848ca96f PC |
2630 | { |
2631 | *__result = _GLIBCXX_MOVE(*__first2); | |
2632 | ++__first2; | |
2633 | } | |
2634 | else | |
2635 | { | |
2636 | *__result = _GLIBCXX_MOVE(*__first1); | |
2637 | ++__first1; | |
2638 | } | |
2639 | ++__result; | |
2640 | } | |
2641 | return _GLIBCXX_MOVE3(__first2, __last2, | |
2642 | _GLIBCXX_MOVE3(__first1, __last1, | |
2643 | __result)); | |
2644 | } | |
2645 | ||
c2ba9709 JS |
2646 | template<typename _RandomAccessIterator1, typename _RandomAccessIterator2, |
2647 | typename _Distance, typename _Compare> | |
02d92e3b | 2648 | void |
c2ba9709 JS |
2649 | __merge_sort_loop(_RandomAccessIterator1 __first, |
2650 | _RandomAccessIterator1 __last, | |
2651 | _RandomAccessIterator2 __result, _Distance __step_size, | |
2652 | _Compare __comp) | |
02d92e3b | 2653 | { |
c2ba9709 | 2654 | const _Distance __two_step = 2 * __step_size; |
c0d88161 | 2655 | |
c2ba9709 | 2656 | while (__last - __first >= __two_step) |
ffa67767 | 2657 | { |
60c5236e PC |
2658 | __result = std::__move_merge(__first, __first + __step_size, |
2659 | __first + __step_size, | |
2660 | __first + __two_step, | |
2661 | __result, __comp); | |
c2ba9709 | 2662 | __first += __two_step; |
ffa67767 | 2663 | } |
c2ba9709 JS |
2664 | __step_size = std::min(_Distance(__last - __first), __step_size); |
2665 | ||
ea89b248 | 2666 | std::__move_merge(__first, __first + __step_size, |
60c5236e | 2667 | __first + __step_size, __last, __result, __comp); |
c868f37e PC |
2668 | } |
2669 | ||
d98f312c PC |
2670 | template<typename _RandomAccessIterator, typename _Distance, |
2671 | typename _Compare> | |
c2ba9709 JS |
2672 | void |
2673 | __chunk_insertion_sort(_RandomAccessIterator __first, | |
2674 | _RandomAccessIterator __last, | |
2675 | _Distance __chunk_size, _Compare __comp) | |
02d92e3b | 2676 | { |
c2ba9709 JS |
2677 | while (__last - __first >= __chunk_size) |
2678 | { | |
2679 | std::__insertion_sort(__first, __first + __chunk_size, __comp); | |
2680 | __first += __chunk_size; | |
2681 | } | |
2682 | std::__insertion_sort(__first, __last, __comp); | |
02d92e3b | 2683 | } |
725dc051 | 2684 | |
c2ba9709 JS |
2685 | enum { _S_chunk_size = 7 }; |
2686 | ||
c2ba9709 JS |
2687 | template<typename _RandomAccessIterator, typename _Pointer, typename _Compare> |
2688 | void | |
2689 | __merge_sort_with_buffer(_RandomAccessIterator __first, | |
2690 | _RandomAccessIterator __last, | |
ec494945 | 2691 | _Pointer __buffer, _Compare __comp) |
02d92e3b | 2692 | { |
c2ba9709 JS |
2693 | typedef typename iterator_traits<_RandomAccessIterator>::difference_type |
2694 | _Distance; | |
da5a2efd | 2695 | |
c2ba9709 JS |
2696 | const _Distance __len = __last - __first; |
2697 | const _Pointer __buffer_last = __buffer + __len; | |
da5a2efd | 2698 | |
c2ba9709 JS |
2699 | _Distance __step_size = _S_chunk_size; |
2700 | std::__chunk_insertion_sort(__first, __last, __step_size, __comp); | |
da5a2efd | 2701 | |
c2ba9709 | 2702 | while (__step_size < __len) |
ffa67767 | 2703 | { |
c2ba9709 JS |
2704 | std::__merge_sort_loop(__first, __last, __buffer, |
2705 | __step_size, __comp); | |
2706 | __step_size *= 2; | |
2707 | std::__merge_sort_loop(__buffer, __buffer_last, __first, | |
2708 | __step_size, __comp); | |
2709 | __step_size *= 2; | |
02d92e3b | 2710 | } |
02d92e3b | 2711 | } |
c0d88161 | 2712 | |
c2ba9709 | 2713 | template<typename _RandomAccessIterator, typename _Pointer, |
ea89b248 | 2714 | typename _Distance, typename _Compare> |
c2ba9709 JS |
2715 | void |
2716 | __stable_sort_adaptive(_RandomAccessIterator __first, | |
2717 | _RandomAccessIterator __last, | |
ec494945 FD |
2718 | _Pointer __buffer, _Distance __buffer_size, |
2719 | _Compare __comp) | |
02d92e3b | 2720 | { |
c2ba9709 JS |
2721 | const _Distance __len = (__last - __first + 1) / 2; |
2722 | const _RandomAccessIterator __middle = __first + __len; | |
2723 | if (__len > __buffer_size) | |
2724 | { | |
ea89b248 FD |
2725 | std::__stable_sort_adaptive(__first, __middle, __buffer, |
2726 | __buffer_size, __comp); | |
2727 | std::__stable_sort_adaptive(__middle, __last, __buffer, | |
2728 | __buffer_size, __comp); | |
c2ba9709 JS |
2729 | } |
2730 | else | |
2731 | { | |
ea89b248 FD |
2732 | std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp); |
2733 | std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp); | |
c2ba9709 JS |
2734 | } |
2735 | std::__merge_adaptive(__first, __middle, __last, | |
2736 | _Distance(__middle - __first), | |
2737 | _Distance(__last - __middle), | |
ea89b248 FD |
2738 | __buffer, __buffer_size, |
2739 | __comp); | |
02d92e3b | 2740 | } |
c0d88161 | 2741 | |
4312e020 | 2742 | /// This is a helper function for the stable sorting routines. |
c2ba9709 JS |
2743 | template<typename _RandomAccessIterator, typename _Compare> |
2744 | void | |
2745 | __inplace_stable_sort(_RandomAccessIterator __first, | |
2746 | _RandomAccessIterator __last, _Compare __comp) | |
02d92e3b | 2747 | { |
c2ba9709 | 2748 | if (__last - __first < 15) |
ffa67767 | 2749 | { |
c2ba9709 JS |
2750 | std::__insertion_sort(__first, __last, __comp); |
2751 | return; | |
ffa67767 | 2752 | } |
c2ba9709 JS |
2753 | _RandomAccessIterator __middle = __first + (__last - __first) / 2; |
2754 | std::__inplace_stable_sort(__first, __middle, __comp); | |
2755 | std::__inplace_stable_sort(__middle, __last, __comp); | |
2756 | std::__merge_without_buffer(__first, __middle, __last, | |
2757 | __middle - __first, | |
2758 | __last - __middle, | |
2759 | __comp); | |
c0d88161 | 2760 | } |
02d92e3b | 2761 | |
c2ba9709 JS |
2762 | // stable_sort |
2763 | ||
2764 | // Set algorithms: includes, set_union, set_intersection, set_difference, | |
2765 | // set_symmetric_difference. All of these algorithms have the precondition | |
2766 | // that their input ranges are sorted and the postcondition that their output | |
2767 | // ranges are sorted. | |
2768 | ||
ea89b248 FD |
2769 | template<typename _InputIterator1, typename _InputIterator2, |
2770 | typename _Compare> | |
2771 | bool | |
2772 | __includes(_InputIterator1 __first1, _InputIterator1 __last1, | |
2773 | _InputIterator2 __first2, _InputIterator2 __last2, | |
2774 | _Compare __comp) | |
2775 | { | |
2776 | while (__first1 != __last1 && __first2 != __last2) | |
2777 | if (__comp(__first2, __first1)) | |
2778 | return false; | |
2779 | else if (__comp(__first1, __first2)) | |
2780 | ++__first1; | |
2781 | else | |
f970a17d JW |
2782 | { |
2783 | ++__first1; | |
2784 | ++__first2; | |
2785 | } | |
ea89b248 FD |
2786 | |
2787 | return __first2 == __last2; | |
2788 | } | |
2789 | ||
bf551f96 | 2790 | /** |
c2ba9709 | 2791 | * @brief Determines whether all elements of a sequence exists in a range. |
93c66bc6 BK |
2792 | * @param __first1 Start of search range. |
2793 | * @param __last1 End of search range. | |
2794 | * @param __first2 Start of sequence | |
2795 | * @param __last2 End of sequence. | |
2796 | * @return True if each element in [__first2,__last2) is contained in order | |
2797 | * within [__first1,__last1). False otherwise. | |
aac2878e | 2798 | * @ingroup set_algorithms |
c868f37e | 2799 | * |
93c66bc6 BK |
2800 | * This operation expects both [__first1,__last1) and |
2801 | * [__first2,__last2) to be sorted. Searches for the presence of | |
2802 | * each element in [__first2,__last2) within [__first1,__last1). | |
2803 | * The iterators over each range only move forward, so this is a | |
2804 | * linear algorithm. If an element in [__first2,__last2) is not | |
7897a1c0 | 2805 | * found before the search iterator reaches @p __last2, false is |
93c66bc6 | 2806 | * returned. |
bf551f96 | 2807 | */ |
c2ba9709 | 2808 | template<typename _InputIterator1, typename _InputIterator2> |
3bd2644c | 2809 | inline bool |
c2ba9709 JS |
2810 | includes(_InputIterator1 __first1, _InputIterator1 __last1, |
2811 | _InputIterator2 __first2, _InputIterator2 __last2) | |
02d92e3b SW |
2812 | { |
2813 | // concept requirements | |
da5a2efd PC |
2814 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
2815 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) | |
ea89b248 FD |
2816 | __glibcxx_function_requires(_LessThanOpConcept< |
2817 | typename iterator_traits<_InputIterator1>::value_type, | |
2818 | typename iterator_traits<_InputIterator2>::value_type>) | |
2819 | __glibcxx_function_requires(_LessThanOpConcept< | |
2820 | typename iterator_traits<_InputIterator2>::value_type, | |
2821 | typename iterator_traits<_InputIterator1>::value_type>) | |
a4c07f2d PC |
2822 | __glibcxx_requires_sorted_set(__first1, __last1, __first2); |
2823 | __glibcxx_requires_sorted_set(__first2, __last2, __first1); | |
630a286a FD |
2824 | __glibcxx_requires_irreflexive2(__first1, __last1); |
2825 | __glibcxx_requires_irreflexive2(__first2, __last2); | |
8c093661 | 2826 | |
ea89b248 FD |
2827 | return std::__includes(__first1, __last1, __first2, __last2, |
2828 | __gnu_cxx::__ops::__iter_less_iter()); | |
8c093661 RS |
2829 | } |
2830 | ||
da5a2efd | 2831 | /** |
c2ba9709 JS |
2832 | * @brief Determines whether all elements of a sequence exists in a range |
2833 | * using comparison. | |
5b9daa7e | 2834 | * @ingroup set_algorithms |
93c66bc6 BK |
2835 | * @param __first1 Start of search range. |
2836 | * @param __last1 End of search range. | |
2837 | * @param __first2 Start of sequence | |
2838 | * @param __last2 End of sequence. | |
2839 | * @param __comp Comparison function to use. | |
2840 | * @return True if each element in [__first2,__last2) is contained | |
2841 | * in order within [__first1,__last1) according to comp. False | |
2842 | * otherwise. @ingroup set_algorithms | |
da5a2efd | 2843 | * |
93c66bc6 BK |
2844 | * This operation expects both [__first1,__last1) and |
2845 | * [__first2,__last2) to be sorted. Searches for the presence of | |
2846 | * each element in [__first2,__last2) within [__first1,__last1), | |
2847 | * using comp to decide. The iterators over each range only move | |
2848 | * forward, so this is a linear algorithm. If an element in | |
2849 | * [__first2,__last2) is not found before the search iterator | |
7897a1c0 | 2850 | * reaches @p __last2, false is returned. |
da5a2efd PC |
2851 | */ |
2852 | template<typename _InputIterator1, typename _InputIterator2, | |
c2ba9709 | 2853 | typename _Compare> |
3bd2644c | 2854 | inline bool |
c2ba9709 | 2855 | includes(_InputIterator1 __first1, _InputIterator1 __last1, |
a4c07f2d PC |
2856 | _InputIterator2 __first2, _InputIterator2 __last2, |
2857 | _Compare __comp) | |
8c093661 | 2858 | { |
da5a2efd PC |
2859 | // concept requirements |
2860 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) | |
2861 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) | |
c2ba9709 | 2862 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
ea89b248 FD |
2863 | typename iterator_traits<_InputIterator1>::value_type, |
2864 | typename iterator_traits<_InputIterator2>::value_type>) | |
da5a2efd | 2865 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
ea89b248 FD |
2866 | typename iterator_traits<_InputIterator2>::value_type, |
2867 | typename iterator_traits<_InputIterator1>::value_type>) | |
a4c07f2d PC |
2868 | __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); |
2869 | __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); | |
630a286a FD |
2870 | __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp); |
2871 | __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp); | |
da5a2efd | 2872 | |
ea89b248 FD |
2873 | return std::__includes(__first1, __last1, __first2, __last2, |
2874 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); | |
8c093661 RS |
2875 | } |
2876 | ||
c2ba9709 JS |
2877 | // nth_element |
2878 | // merge | |
2879 | // set_difference | |
2880 | // set_intersection | |
2881 | // set_union | |
2882 | // stable_sort | |
2883 | // set_symmetric_difference | |
2884 | // min_element | |
2885 | // max_element | |
2886 | ||
ea89b248 | 2887 | template<typename _BidirectionalIterator, typename _Compare> |
c2ba9709 | 2888 | bool |
ea89b248 FD |
2889 | __next_permutation(_BidirectionalIterator __first, |
2890 | _BidirectionalIterator __last, _Compare __comp) | |
02d92e3b | 2891 | { |
c2ba9709 JS |
2892 | if (__first == __last) |
2893 | return false; | |
2894 | _BidirectionalIterator __i = __first; | |
2895 | ++__i; | |
2896 | if (__i == __last) | |
2897 | return false; | |
2898 | __i = __last; | |
2899 | --__i; | |
2900 | ||
2901 | for(;;) | |
da5a2efd | 2902 | { |
c2ba9709 JS |
2903 | _BidirectionalIterator __ii = __i; |
2904 | --__i; | |
ea89b248 | 2905 | if (__comp(__i, __ii)) |
da5a2efd | 2906 | { |
c2ba9709 | 2907 | _BidirectionalIterator __j = __last; |
ea89b248 | 2908 | while (!__comp(__i, --__j)) |
c2ba9709 JS |
2909 | {} |
2910 | std::iter_swap(__i, __j); | |
ea89b248 FD |
2911 | std::__reverse(__ii, __last, |
2912 | std::__iterator_category(__first)); | |
c2ba9709 | 2913 | return true; |
da5a2efd | 2914 | } |
c2ba9709 | 2915 | if (__i == __first) |
da5a2efd | 2916 | { |
ea89b248 FD |
2917 | std::__reverse(__first, __last, |
2918 | std::__iterator_category(__first)); | |
c2ba9709 | 2919 | return false; |
da5a2efd PC |
2920 | } |
2921 | } | |
02d92e3b SW |
2922 | } |
2923 | ||
ea89b248 FD |
2924 | /** |
2925 | * @brief Permute range into the next @e dictionary ordering. | |
2926 | * @ingroup sorting_algorithms | |
2927 | * @param __first Start of range. | |
2928 | * @param __last End of range. | |
2929 | * @return False if wrapped to first permutation, true otherwise. | |
2930 | * | |
2931 | * Treats all permutations of the range as a set of @e dictionary sorted | |
2932 | * sequences. Permutes the current sequence into the next one of this set. | |
2933 | * Returns true if there are more sequences to generate. If the sequence | |
2934 | * is the largest of the set, the smallest is generated and false returned. | |
2935 | */ | |
2936 | template<typename _BidirectionalIterator> | |
3bd2644c | 2937 | inline bool |
ea89b248 FD |
2938 | next_permutation(_BidirectionalIterator __first, |
2939 | _BidirectionalIterator __last) | |
2940 | { | |
2941 | // concept requirements | |
2942 | __glibcxx_function_requires(_BidirectionalIteratorConcept< | |
2943 | _BidirectionalIterator>) | |
2944 | __glibcxx_function_requires(_LessThanComparableConcept< | |
2945 | typename iterator_traits<_BidirectionalIterator>::value_type>) | |
2946 | __glibcxx_requires_valid_range(__first, __last); | |
630a286a | 2947 | __glibcxx_requires_irreflexive(__first, __last); |
ea89b248 FD |
2948 | |
2949 | return std::__next_permutation | |
2950 | (__first, __last, __gnu_cxx::__ops::__iter_less_iter()); | |
2951 | } | |
2952 | ||
bf551f96 | 2953 | /** |
7897a1c0 | 2954 | * @brief Permute range into the next @e dictionary ordering using |
4b7ed13a | 2955 | * comparison functor. |
5b9daa7e | 2956 | * @ingroup sorting_algorithms |
93c66bc6 BK |
2957 | * @param __first Start of range. |
2958 | * @param __last End of range. | |
2959 | * @param __comp A comparison functor. | |
c2ba9709 JS |
2960 | * @return False if wrapped to first permutation, true otherwise. |
2961 | * | |
93c66bc6 | 2962 | * Treats all permutations of the range [__first,__last) as a set of |
7897a1c0 | 2963 | * @e dictionary sorted sequences ordered by @p __comp. Permutes the current |
c2ba9709 JS |
2964 | * sequence into the next one of this set. Returns true if there are more |
2965 | * sequences to generate. If the sequence is the largest of the set, the | |
2966 | * smallest is generated and false returned. | |
da5a2efd | 2967 | */ |
c2ba9709 | 2968 | template<typename _BidirectionalIterator, typename _Compare> |
3bd2644c | 2969 | inline bool |
c2ba9709 JS |
2970 | next_permutation(_BidirectionalIterator __first, |
2971 | _BidirectionalIterator __last, _Compare __comp) | |
da5a2efd | 2972 | { |
c2ba9709 JS |
2973 | // concept requirements |
2974 | __glibcxx_function_requires(_BidirectionalIteratorConcept< | |
2975 | _BidirectionalIterator>) | |
2976 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, | |
2977 | typename iterator_traits<_BidirectionalIterator>::value_type, | |
2978 | typename iterator_traits<_BidirectionalIterator>::value_type>) | |
2979 | __glibcxx_requires_valid_range(__first, __last); | |
630a286a | 2980 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
c2ba9709 | 2981 | |
ea89b248 FD |
2982 | return std::__next_permutation |
2983 | (__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp)); | |
2984 | } | |
2985 | ||
2986 | template<typename _BidirectionalIterator, typename _Compare> | |
2987 | bool | |
2988 | __prev_permutation(_BidirectionalIterator __first, | |
2989 | _BidirectionalIterator __last, _Compare __comp) | |
2990 | { | |
c2ba9709 JS |
2991 | if (__first == __last) |
2992 | return false; | |
2993 | _BidirectionalIterator __i = __first; | |
2994 | ++__i; | |
2995 | if (__i == __last) | |
2996 | return false; | |
2997 | __i = __last; | |
2998 | --__i; | |
2999 | ||
3000 | for(;;) | |
da5a2efd | 3001 | { |
c2ba9709 JS |
3002 | _BidirectionalIterator __ii = __i; |
3003 | --__i; | |
ea89b248 | 3004 | if (__comp(__ii, __i)) |
da5a2efd | 3005 | { |
c2ba9709 | 3006 | _BidirectionalIterator __j = __last; |
ea89b248 | 3007 | while (!__comp(--__j, __i)) |
c2ba9709 JS |
3008 | {} |
3009 | std::iter_swap(__i, __j); | |
ea89b248 FD |
3010 | std::__reverse(__ii, __last, |
3011 | std::__iterator_category(__first)); | |
c2ba9709 | 3012 | return true; |
da5a2efd | 3013 | } |
c2ba9709 | 3014 | if (__i == __first) |
da5a2efd | 3015 | { |
ea89b248 FD |
3016 | std::__reverse(__first, __last, |
3017 | std::__iterator_category(__first)); | |
c2ba9709 | 3018 | return false; |
da5a2efd PC |
3019 | } |
3020 | } | |
3021 | } | |
3022 | ||
3023 | /** | |
7897a1c0 | 3024 | * @brief Permute range into the previous @e dictionary ordering. |
5b9daa7e | 3025 | * @ingroup sorting_algorithms |
93c66bc6 BK |
3026 | * @param __first Start of range. |
3027 | * @param __last End of range. | |
c2ba9709 JS |
3028 | * @return False if wrapped to last permutation, true otherwise. |
3029 | * | |
7897a1c0 | 3030 | * Treats all permutations of the range as a set of @e dictionary sorted |
c2ba9709 JS |
3031 | * sequences. Permutes the current sequence into the previous one of this |
3032 | * set. Returns true if there are more sequences to generate. If the | |
3033 | * sequence is the smallest of the set, the largest is generated and false | |
3034 | * returned. | |
da5a2efd | 3035 | */ |
c2ba9709 | 3036 | template<typename _BidirectionalIterator> |
3bd2644c | 3037 | inline bool |
c2ba9709 JS |
3038 | prev_permutation(_BidirectionalIterator __first, |
3039 | _BidirectionalIterator __last) | |
da5a2efd | 3040 | { |
c2ba9709 JS |
3041 | // concept requirements |
3042 | __glibcxx_function_requires(_BidirectionalIteratorConcept< | |
3043 | _BidirectionalIterator>) | |
3044 | __glibcxx_function_requires(_LessThanComparableConcept< | |
3045 | typename iterator_traits<_BidirectionalIterator>::value_type>) | |
3046 | __glibcxx_requires_valid_range(__first, __last); | |
630a286a | 3047 | __glibcxx_requires_irreflexive(__first, __last); |
da5a2efd | 3048 | |
ea89b248 FD |
3049 | return std::__prev_permutation(__first, __last, |
3050 | __gnu_cxx::__ops::__iter_less_iter()); | |
da5a2efd PC |
3051 | } |
3052 | ||
3053 | /** | |
7897a1c0 | 3054 | * @brief Permute range into the previous @e dictionary ordering using |
4b7ed13a | 3055 | * comparison functor. |
5b9daa7e | 3056 | * @ingroup sorting_algorithms |
93c66bc6 BK |
3057 | * @param __first Start of range. |
3058 | * @param __last End of range. | |
3059 | * @param __comp A comparison functor. | |
c2ba9709 JS |
3060 | * @return False if wrapped to last permutation, true otherwise. |
3061 | * | |
93c66bc6 | 3062 | * Treats all permutations of the range [__first,__last) as a set of |
7897a1c0 | 3063 | * @e dictionary sorted sequences ordered by @p __comp. Permutes the current |
c2ba9709 JS |
3064 | * sequence into the previous one of this set. Returns true if there are |
3065 | * more sequences to generate. If the sequence is the smallest of the set, | |
3066 | * the largest is generated and false returned. | |
da5a2efd | 3067 | */ |
c2ba9709 | 3068 | template<typename _BidirectionalIterator, typename _Compare> |
3bd2644c | 3069 | inline bool |
c2ba9709 JS |
3070 | prev_permutation(_BidirectionalIterator __first, |
3071 | _BidirectionalIterator __last, _Compare __comp) | |
da5a2efd | 3072 | { |
c2ba9709 JS |
3073 | // concept requirements |
3074 | __glibcxx_function_requires(_BidirectionalIteratorConcept< | |
3075 | _BidirectionalIterator>) | |
3076 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, | |
3077 | typename iterator_traits<_BidirectionalIterator>::value_type, | |
3078 | typename iterator_traits<_BidirectionalIterator>::value_type>) | |
3079 | __glibcxx_requires_valid_range(__first, __last); | |
630a286a | 3080 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
c2ba9709 | 3081 | |
ea89b248 FD |
3082 | return std::__prev_permutation(__first, __last, |
3083 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); | |
da5a2efd PC |
3084 | } |
3085 | ||
c2ba9709 JS |
3086 | // replace |
3087 | // replace_if | |
3088 | ||
ea89b248 FD |
3089 | template<typename _InputIterator, typename _OutputIterator, |
3090 | typename _Predicate, typename _Tp> | |
3091 | _OutputIterator | |
3092 | __replace_copy_if(_InputIterator __first, _InputIterator __last, | |
3093 | _OutputIterator __result, | |
3094 | _Predicate __pred, const _Tp& __new_value) | |
3095 | { | |
f970a17d | 3096 | for (; __first != __last; ++__first, (void)++__result) |
ea89b248 FD |
3097 | if (__pred(__first)) |
3098 | *__result = __new_value; | |
3099 | else | |
3100 | *__result = *__first; | |
3101 | return __result; | |
3102 | } | |
3103 | ||
da5a2efd | 3104 | /** |
c2ba9709 JS |
3105 | * @brief Copy a sequence, replacing each element of one value with another |
3106 | * value. | |
93c66bc6 BK |
3107 | * @param __first An input iterator. |
3108 | * @param __last An input iterator. | |
3109 | * @param __result An output iterator. | |
3110 | * @param __old_value The value to be replaced. | |
3111 | * @param __new_value The replacement value. | |
c2ba9709 JS |
3112 | * @return The end of the output sequence, @p result+(last-first). |
3113 | * | |
93c66bc6 BK |
3114 | * Copies each element in the input range @p [__first,__last) to the |
3115 | * output range @p [__result,__result+(__last-__first)) replacing elements | |
3116 | * equal to @p __old_value with @p __new_value. | |
da5a2efd | 3117 | */ |
c2ba9709 | 3118 | template<typename _InputIterator, typename _OutputIterator, typename _Tp> |
3bd2644c | 3119 | inline _OutputIterator |
c2ba9709 JS |
3120 | replace_copy(_InputIterator __first, _InputIterator __last, |
3121 | _OutputIterator __result, | |
3122 | const _Tp& __old_value, const _Tp& __new_value) | |
da5a2efd | 3123 | { |
c2ba9709 JS |
3124 | // concept requirements |
3125 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) | |
3126 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
3127 | typename iterator_traits<_InputIterator>::value_type>) | |
3128 | __glibcxx_function_requires(_EqualOpConcept< | |
3129 | typename iterator_traits<_InputIterator>::value_type, _Tp>) | |
3130 | __glibcxx_requires_valid_range(__first, __last); | |
3131 | ||
ea89b248 FD |
3132 | return std::__replace_copy_if(__first, __last, __result, |
3133 | __gnu_cxx::__ops::__iter_equals_val(__old_value), | |
3134 | __new_value); | |
da5a2efd PC |
3135 | } |
3136 | ||
3137 | /** | |
c2ba9709 JS |
3138 | * @brief Copy a sequence, replacing each value for which a predicate |
3139 | * returns true with another value. | |
5b9daa7e | 3140 | * @ingroup mutating_algorithms |
93c66bc6 BK |
3141 | * @param __first An input iterator. |
3142 | * @param __last An input iterator. | |
3143 | * @param __result An output iterator. | |
3144 | * @param __pred A predicate. | |
3145 | * @param __new_value The replacement value. | |
3146 | * @return The end of the output sequence, @p __result+(__last-__first). | |
c2ba9709 | 3147 | * |
93c66bc6 BK |
3148 | * Copies each element in the range @p [__first,__last) to the range |
3149 | * @p [__result,__result+(__last-__first)) replacing elements for which | |
3150 | * @p __pred returns true with @p __new_value. | |
da5a2efd | 3151 | */ |
c2ba9709 JS |
3152 | template<typename _InputIterator, typename _OutputIterator, |
3153 | typename _Predicate, typename _Tp> | |
3bd2644c | 3154 | inline _OutputIterator |
c2ba9709 JS |
3155 | replace_copy_if(_InputIterator __first, _InputIterator __last, |
3156 | _OutputIterator __result, | |
3157 | _Predicate __pred, const _Tp& __new_value) | |
da5a2efd | 3158 | { |
c2ba9709 JS |
3159 | // concept requirements |
3160 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) | |
3161 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
3162 | typename iterator_traits<_InputIterator>::value_type>) | |
3163 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, | |
3164 | typename iterator_traits<_InputIterator>::value_type>) | |
3165 | __glibcxx_requires_valid_range(__first, __last); | |
3166 | ||
ea89b248 FD |
3167 | return std::__replace_copy_if(__first, __last, __result, |
3168 | __gnu_cxx::__ops::__pred_iter(__pred), | |
3169 | __new_value); | |
3170 | } | |
3171 | ||
3172 | template<typename _InputIterator, typename _Predicate> | |
3173 | typename iterator_traits<_InputIterator>::difference_type | |
3174 | __count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) | |
3175 | { | |
3176 | typename iterator_traits<_InputIterator>::difference_type __n = 0; | |
3177 | for (; __first != __last; ++__first) | |
3178 | if (__pred(__first)) | |
3179 | ++__n; | |
3180 | return __n; | |
c2ba9709 JS |
3181 | } |
3182 | ||
734f5023 | 3183 | #if __cplusplus >= 201103L |
4b7ed13a PC |
3184 | /** |
3185 | * @brief Determines whether the elements of a sequence are sorted. | |
5b9daa7e | 3186 | * @ingroup sorting_algorithms |
93c66bc6 BK |
3187 | * @param __first An iterator. |
3188 | * @param __last Another iterator. | |
4b7ed13a PC |
3189 | * @return True if the elements are sorted, false otherwise. |
3190 | */ | |
3191 | template<typename _ForwardIterator> | |
3192 | inline bool | |
3193 | is_sorted(_ForwardIterator __first, _ForwardIterator __last) | |
3194 | { return std::is_sorted_until(__first, __last) == __last; } | |
3195 | ||
3196 | /** | |
3197 | * @brief Determines whether the elements of a sequence are sorted | |
3198 | * according to a comparison functor. | |
5b9daa7e | 3199 | * @ingroup sorting_algorithms |
93c66bc6 BK |
3200 | * @param __first An iterator. |
3201 | * @param __last Another iterator. | |
3202 | * @param __comp A comparison functor. | |
4b7ed13a PC |
3203 | * @return True if the elements are sorted, false otherwise. |
3204 | */ | |
3205 | template<typename _ForwardIterator, typename _Compare> | |
3206 | inline bool | |
3207 | is_sorted(_ForwardIterator __first, _ForwardIterator __last, | |
3208 | _Compare __comp) | |
3209 | { return std::is_sorted_until(__first, __last, __comp) == __last; } | |
3210 | ||
ea89b248 FD |
3211 | template<typename _ForwardIterator, typename _Compare> |
3212 | _ForwardIterator | |
3213 | __is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, | |
3214 | _Compare __comp) | |
3215 | { | |
3216 | if (__first == __last) | |
3217 | return __last; | |
3218 | ||
3219 | _ForwardIterator __next = __first; | |
f970a17d | 3220 | for (++__next; __next != __last; __first = __next, (void)++__next) |
ea89b248 FD |
3221 | if (__comp(__next, __first)) |
3222 | return __next; | |
3223 | return __next; | |
3224 | } | |
3225 | ||
4b7ed13a PC |
3226 | /** |
3227 | * @brief Determines the end of a sorted sequence. | |
5b9daa7e | 3228 | * @ingroup sorting_algorithms |
93c66bc6 BK |
3229 | * @param __first An iterator. |
3230 | * @param __last Another iterator. | |
3231 | * @return An iterator pointing to the last iterator i in [__first, __last) | |
3232 | * for which the range [__first, i) is sorted. | |
4b7ed13a PC |
3233 | */ |
3234 | template<typename _ForwardIterator> | |
3bd2644c | 3235 | inline _ForwardIterator |
4b7ed13a PC |
3236 | is_sorted_until(_ForwardIterator __first, _ForwardIterator __last) |
3237 | { | |
3238 | // concept requirements | |
3239 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
3240 | __glibcxx_function_requires(_LessThanComparableConcept< | |
3241 | typename iterator_traits<_ForwardIterator>::value_type>) | |
3242 | __glibcxx_requires_valid_range(__first, __last); | |
630a286a | 3243 | __glibcxx_requires_irreflexive(__first, __last); |
4b7ed13a | 3244 | |
ea89b248 FD |
3245 | return std::__is_sorted_until(__first, __last, |
3246 | __gnu_cxx::__ops::__iter_less_iter()); | |
4b7ed13a PC |
3247 | } |
3248 | ||
3249 | /** | |
3250 | * @brief Determines the end of a sorted sequence using comparison functor. | |
5b9daa7e | 3251 | * @ingroup sorting_algorithms |
93c66bc6 BK |
3252 | * @param __first An iterator. |
3253 | * @param __last Another iterator. | |
3254 | * @param __comp A comparison functor. | |
3255 | * @return An iterator pointing to the last iterator i in [__first, __last) | |
3256 | * for which the range [__first, i) is sorted. | |
4b7ed13a PC |
3257 | */ |
3258 | template<typename _ForwardIterator, typename _Compare> | |
3bd2644c | 3259 | inline _ForwardIterator |
4b7ed13a PC |
3260 | is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, |
3261 | _Compare __comp) | |
3262 | { | |
3263 | // concept requirements | |
3264 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
3265 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, | |
3266 | typename iterator_traits<_ForwardIterator>::value_type, | |
3267 | typename iterator_traits<_ForwardIterator>::value_type>) | |
3268 | __glibcxx_requires_valid_range(__first, __last); | |
630a286a | 3269 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
4b7ed13a | 3270 | |
ea89b248 FD |
3271 | return std::__is_sorted_until(__first, __last, |
3272 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); | |
4b7ed13a | 3273 | } |
f6547b68 PC |
3274 | |
3275 | /** | |
3276 | * @brief Determines min and max at once as an ordered pair. | |
5b9daa7e | 3277 | * @ingroup sorting_algorithms |
93c66bc6 BK |
3278 | * @param __a A thing of arbitrary type. |
3279 | * @param __b Another thing of arbitrary type. | |
3280 | * @return A pair(__b, __a) if __b is smaller than __a, pair(__a, | |
3281 | * __b) otherwise. | |
f6547b68 PC |
3282 | */ |
3283 | template<typename _Tp> | |
8dff34fe | 3284 | _GLIBCXX14_CONSTEXPR |
f6547b68 PC |
3285 | inline pair<const _Tp&, const _Tp&> |
3286 | minmax(const _Tp& __a, const _Tp& __b) | |
3287 | { | |
3288 | // concept requirements | |
3289 | __glibcxx_function_requires(_LessThanComparableConcept<_Tp>) | |
3290 | ||
3291 | return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a) | |
ec494945 | 3292 | : pair<const _Tp&, const _Tp&>(__a, __b); |
f6547b68 PC |
3293 | } |
3294 | ||
3295 | /** | |
3296 | * @brief Determines min and max at once as an ordered pair. | |
5b9daa7e | 3297 | * @ingroup sorting_algorithms |
93c66bc6 BK |
3298 | * @param __a A thing of arbitrary type. |
3299 | * @param __b Another thing of arbitrary type. | |
7897a1c0 | 3300 | * @param __comp A @link comparison_functors comparison functor @endlink. |
93c66bc6 BK |
3301 | * @return A pair(__b, __a) if __b is smaller than __a, pair(__a, |
3302 | * __b) otherwise. | |
f6547b68 PC |
3303 | */ |
3304 | template<typename _Tp, typename _Compare> | |
8dff34fe | 3305 | _GLIBCXX14_CONSTEXPR |
f6547b68 PC |
3306 | inline pair<const _Tp&, const _Tp&> |
3307 | minmax(const _Tp& __a, const _Tp& __b, _Compare __comp) | |
3308 | { | |
3309 | return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) | |
ec494945 | 3310 | : pair<const _Tp&, const _Tp&>(__a, __b); |
f6547b68 PC |
3311 | } |
3312 | ||
ea89b248 | 3313 | template<typename _ForwardIterator, typename _Compare> |
8dff34fe | 3314 | _GLIBCXX14_CONSTEXPR |
f6547b68 | 3315 | pair<_ForwardIterator, _ForwardIterator> |
ea89b248 FD |
3316 | __minmax_element(_ForwardIterator __first, _ForwardIterator __last, |
3317 | _Compare __comp) | |
f6547b68 | 3318 | { |
f6547b68 PC |
3319 | _ForwardIterator __next = __first; |
3320 | if (__first == __last | |
3321 | || ++__next == __last) | |
3322 | return std::make_pair(__first, __first); | |
3323 | ||
8dff34fe | 3324 | _ForwardIterator __min{}, __max{}; |
ea89b248 | 3325 | if (__comp(__next, __first)) |
f6547b68 PC |
3326 | { |
3327 | __min = __next; | |
3328 | __max = __first; | |
3329 | } | |
3330 | else | |
3331 | { | |
3332 | __min = __first; | |
3333 | __max = __next; | |
3334 | } | |
3335 | ||
3336 | __first = __next; | |
3337 | ++__first; | |
3338 | ||
3339 | while (__first != __last) | |
3340 | { | |
3341 | __next = __first; | |
3342 | if (++__next == __last) | |
3343 | { | |
ea89b248 | 3344 | if (__comp(__first, __min)) |
f6547b68 | 3345 | __min = __first; |
ea89b248 | 3346 | else if (!__comp(__first, __max)) |
f6547b68 PC |
3347 | __max = __first; |
3348 | break; | |
3349 | } | |
3350 | ||
ea89b248 | 3351 | if (__comp(__next, __first)) |
f6547b68 | 3352 | { |
ea89b248 | 3353 | if (__comp(__next, __min)) |
f6547b68 | 3354 | __min = __next; |
ea89b248 | 3355 | if (!__comp(__first, __max)) |
f6547b68 PC |
3356 | __max = __first; |
3357 | } | |
3358 | else | |
3359 | { | |
ea89b248 | 3360 | if (__comp(__first, __min)) |
f6547b68 | 3361 | __min = __first; |
ea89b248 | 3362 | if (!__comp(__next, __max)) |
f6547b68 PC |
3363 | __max = __next; |
3364 | } | |
3365 | ||
3366 | __first = __next; | |
3367 | ++__first; | |
3368 | } | |
3369 | ||
3370 | return std::make_pair(__min, __max); | |
3371 | } | |
3372 | ||
ea89b248 FD |
3373 | /** |
3374 | * @brief Return a pair of iterators pointing to the minimum and maximum | |
3375 | * elements in a range. | |
3376 | * @ingroup sorting_algorithms | |
3377 | * @param __first Start of range. | |
3378 | * @param __last End of range. | |
3379 | * @return make_pair(m, M), where m is the first iterator i in | |
3380 | * [__first, __last) such that no other element in the range is | |
3381 | * smaller, and where M is the last iterator i in [__first, __last) | |
3382 | * such that no other element in the range is larger. | |
3383 | */ | |
3384 | template<typename _ForwardIterator> | |
8dff34fe | 3385 | _GLIBCXX14_CONSTEXPR |
3bd2644c | 3386 | inline pair<_ForwardIterator, _ForwardIterator> |
ea89b248 FD |
3387 | minmax_element(_ForwardIterator __first, _ForwardIterator __last) |
3388 | { | |
3389 | // concept requirements | |
3390 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
3391 | __glibcxx_function_requires(_LessThanComparableConcept< | |
3392 | typename iterator_traits<_ForwardIterator>::value_type>) | |
3393 | __glibcxx_requires_valid_range(__first, __last); | |
630a286a | 3394 | __glibcxx_requires_irreflexive(__first, __last); |
ea89b248 FD |
3395 | |
3396 | return std::__minmax_element(__first, __last, | |
3397 | __gnu_cxx::__ops::__iter_less_iter()); | |
3398 | } | |
3399 | ||
f6547b68 PC |
3400 | /** |
3401 | * @brief Return a pair of iterators pointing to the minimum and maximum | |
3402 | * elements in a range. | |
5b9daa7e | 3403 | * @ingroup sorting_algorithms |
93c66bc6 BK |
3404 | * @param __first Start of range. |
3405 | * @param __last End of range. | |
3406 | * @param __comp Comparison functor. | |
f6547b68 | 3407 | * @return make_pair(m, M), where m is the first iterator i in |
93c66bc6 BK |
3408 | * [__first, __last) such that no other element in the range is |
3409 | * smaller, and where M is the last iterator i in [__first, __last) | |
f6547b68 PC |
3410 | * such that no other element in the range is larger. |
3411 | */ | |
3412 | template<typename _ForwardIterator, typename _Compare> | |
8dff34fe | 3413 | _GLIBCXX14_CONSTEXPR |
3bd2644c | 3414 | inline pair<_ForwardIterator, _ForwardIterator> |
f6547b68 PC |
3415 | minmax_element(_ForwardIterator __first, _ForwardIterator __last, |
3416 | _Compare __comp) | |
3417 | { | |
3418 | // concept requirements | |
3419 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
3420 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, | |
3421 | typename iterator_traits<_ForwardIterator>::value_type, | |
3422 | typename iterator_traits<_ForwardIterator>::value_type>) | |
3423 | __glibcxx_requires_valid_range(__first, __last); | |
630a286a | 3424 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
f6547b68 | 3425 | |
ea89b248 FD |
3426 | return std::__minmax_element(__first, __last, |
3427 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); | |
f6547b68 | 3428 | } |
1edd1a83 | 3429 | |
e962c1b0 | 3430 | // N2722 + DR 915. |
1edd1a83 | 3431 | template<typename _Tp> |
8dff34fe | 3432 | _GLIBCXX14_CONSTEXPR |
116a365b | 3433 | inline _Tp |
1edd1a83 PC |
3434 | min(initializer_list<_Tp> __l) |
3435 | { return *std::min_element(__l.begin(), __l.end()); } | |
3436 | ||
3437 | template<typename _Tp, typename _Compare> | |
8dff34fe | 3438 | _GLIBCXX14_CONSTEXPR |
116a365b | 3439 | inline _Tp |
1edd1a83 PC |
3440 | min(initializer_list<_Tp> __l, _Compare __comp) |
3441 | { return *std::min_element(__l.begin(), __l.end(), __comp); } | |
3442 | ||
3443 | template<typename _Tp> | |
8dff34fe | 3444 | _GLIBCXX14_CONSTEXPR |
116a365b | 3445 | inline _Tp |
1edd1a83 PC |
3446 | max(initializer_list<_Tp> __l) |
3447 | { return *std::max_element(__l.begin(), __l.end()); } | |
3448 | ||
3449 | template<typename _Tp, typename _Compare> | |
8dff34fe | 3450 | _GLIBCXX14_CONSTEXPR |
116a365b | 3451 | inline _Tp |
1edd1a83 PC |
3452 | max(initializer_list<_Tp> __l, _Compare __comp) |
3453 | { return *std::max_element(__l.begin(), __l.end(), __comp); } | |
3454 | ||
3455 | template<typename _Tp> | |
8dff34fe | 3456 | _GLIBCXX14_CONSTEXPR |
116a365b | 3457 | inline pair<_Tp, _Tp> |
1edd1a83 PC |
3458 | minmax(initializer_list<_Tp> __l) |
3459 | { | |
3460 | pair<const _Tp*, const _Tp*> __p = | |
3461 | std::minmax_element(__l.begin(), __l.end()); | |
aa4652fa | 3462 | return std::make_pair(*__p.first, *__p.second); |
1edd1a83 PC |
3463 | } |
3464 | ||
3465 | template<typename _Tp, typename _Compare> | |
8dff34fe | 3466 | _GLIBCXX14_CONSTEXPR |
116a365b | 3467 | inline pair<_Tp, _Tp> |
1edd1a83 PC |
3468 | minmax(initializer_list<_Tp> __l, _Compare __comp) |
3469 | { | |
3470 | pair<const _Tp*, const _Tp*> __p = | |
3471 | std::minmax_element(__l.begin(), __l.end(), __comp); | |
3472 | return std::make_pair(*__p.first, *__p.second); | |
3473 | } | |
247d8075 | 3474 | |
632469d0 PC |
3475 | template<typename _ForwardIterator1, typename _ForwardIterator2, |
3476 | typename _BinaryPredicate> | |
3477 | bool | |
ea89b248 FD |
3478 | __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
3479 | _ForwardIterator2 __first2, _BinaryPredicate __pred) | |
632469d0 PC |
3480 | { |
3481 | // Efficiently compare identical prefixes: O(N) if sequences | |
3482 | // have the same elements in the same order. | |
f970a17d | 3483 | for (; __first1 != __last1; ++__first1, (void)++__first2) |
ea89b248 | 3484 | if (!__pred(__first1, __first2)) |
632469d0 PC |
3485 | break; |
3486 | ||
3487 | if (__first1 == __last1) | |
3488 | return true; | |
3489 | ||
3490 | // Establish __last2 assuming equal ranges by iterating over the | |
3491 | // rest of the list. | |
3492 | _ForwardIterator2 __last2 = __first2; | |
3493 | std::advance(__last2, std::distance(__first1, __last1)); | |
3494 | for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan) | |
3495 | { | |
ea89b248 FD |
3496 | if (__scan != std::__find_if(__first1, __scan, |
3497 | __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))) | |
632469d0 PC |
3498 | continue; // We've seen this one before. |
3499 | ||
ea89b248 FD |
3500 | auto __matches |
3501 | = std::__count_if(__first2, __last2, | |
3502 | __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)); | |
3503 | if (0 == __matches || | |
3504 | std::__count_if(__scan, __last1, | |
3505 | __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)) | |
3506 | != __matches) | |
632469d0 PC |
3507 | return false; |
3508 | } | |
3509 | return true; | |
3510 | } | |
3511 | ||
f7fbb003 | 3512 | /** |
ea89b248 | 3513 | * @brief Checks whether a permutation of the second sequence is equal |
f7fbb003 JW |
3514 | * to the first sequence. |
3515 | * @ingroup non_mutating_algorithms | |
3516 | * @param __first1 Start of first range. | |
3517 | * @param __last1 End of first range. | |
3518 | * @param __first2 Start of second range. | |
f7fbb003 | 3519 | * @return true if there exists a permutation of the elements in the range |
ea89b248 FD |
3520 | * [__first2, __first2 + (__last1 - __first1)), beginning with |
3521 | * ForwardIterator2 begin, such that equal(__first1, __last1, begin) | |
3522 | * returns true; otherwise, returns false. | |
f7fbb003 JW |
3523 | */ |
3524 | template<typename _ForwardIterator1, typename _ForwardIterator2> | |
3bd2644c | 3525 | inline bool |
f7fbb003 | 3526 | is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
ea89b248 | 3527 | _ForwardIterator2 __first2) |
f7fbb003 | 3528 | { |
ea89b248 FD |
3529 | // concept requirements |
3530 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) | |
3531 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) | |
3532 | __glibcxx_function_requires(_EqualOpConcept< | |
3533 | typename iterator_traits<_ForwardIterator1>::value_type, | |
3534 | typename iterator_traits<_ForwardIterator2>::value_type>) | |
3535 | __glibcxx_requires_valid_range(__first1, __last1); | |
f7fbb003 | 3536 | |
ea89b248 FD |
3537 | return std::__is_permutation(__first1, __last1, __first2, |
3538 | __gnu_cxx::__ops::__iter_equal_to_iter()); | |
f7fbb003 JW |
3539 | } |
3540 | ||
3541 | /** | |
3542 | * @brief Checks whether a permutation of the second sequence is equal | |
3543 | * to the first sequence. | |
3544 | * @ingroup non_mutating_algorithms | |
3545 | * @param __first1 Start of first range. | |
3546 | * @param __last1 End of first range. | |
3547 | * @param __first2 Start of second range. | |
f7fbb003 | 3548 | * @param __pred A binary predicate. |
ea89b248 FD |
3549 | * @return true if there exists a permutation of the elements in |
3550 | * the range [__first2, __first2 + (__last1 - __first1)), | |
3551 | * beginning with ForwardIterator2 begin, such that | |
3552 | * equal(__first1, __last1, __begin, __pred) returns true; | |
f7fbb003 JW |
3553 | * otherwise, returns false. |
3554 | */ | |
3555 | template<typename _ForwardIterator1, typename _ForwardIterator2, | |
3556 | typename _BinaryPredicate> | |
3bd2644c | 3557 | inline bool |
ea89b248 FD |
3558 | is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
3559 | _ForwardIterator2 __first2, _BinaryPredicate __pred) | |
3560 | { | |
3561 | // concept requirements | |
3562 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) | |
3563 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) | |
3564 | __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, | |
3565 | typename iterator_traits<_ForwardIterator1>::value_type, | |
3566 | typename iterator_traits<_ForwardIterator2>::value_type>) | |
3567 | __glibcxx_requires_valid_range(__first1, __last1); | |
3568 | ||
3569 | return std::__is_permutation(__first1, __last1, __first2, | |
3570 | __gnu_cxx::__ops::__iter_comp_iter(__pred)); | |
3571 | } | |
3572 | ||
3573 | #if __cplusplus > 201103L | |
3574 | template<typename _ForwardIterator1, typename _ForwardIterator2, | |
3575 | typename _BinaryPredicate> | |
3576 | bool | |
3577 | __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, | |
3578 | _ForwardIterator2 __first2, _ForwardIterator2 __last2, | |
3579 | _BinaryPredicate __pred) | |
f7fbb003 JW |
3580 | { |
3581 | using _Cat1 | |
3582 | = typename iterator_traits<_ForwardIterator1>::iterator_category; | |
3583 | using _Cat2 | |
3584 | = typename iterator_traits<_ForwardIterator2>::iterator_category; | |
3585 | using _It1_is_RA = is_same<_Cat1, random_access_iterator_tag>; | |
3586 | using _It2_is_RA = is_same<_Cat2, random_access_iterator_tag>; | |
31eb8a18 | 3587 | constexpr bool __ra_iters = _It1_is_RA() && _It2_is_RA(); |
f7fbb003 JW |
3588 | if (__ra_iters) |
3589 | { | |
3590 | auto __d1 = std::distance(__first1, __last1); | |
3591 | auto __d2 = std::distance(__first2, __last2); | |
3592 | if (__d1 != __d2) | |
3593 | return false; | |
3594 | } | |
3595 | ||
3596 | // Efficiently compare identical prefixes: O(N) if sequences | |
3597 | // have the same elements in the same order. | |
257024e3 | 3598 | for (; __first1 != __last1 && __first2 != __last2; |
f970a17d | 3599 | ++__first1, (void)++__first2) |
ea89b248 | 3600 | if (!__pred(__first1, __first2)) |
f7fbb003 JW |
3601 | break; |
3602 | ||
3603 | if (__ra_iters) | |
3604 | { | |
3605 | if (__first1 == __last1) | |
3606 | return true; | |
3607 | } | |
3608 | else | |
3609 | { | |
3610 | auto __d1 = std::distance(__first1, __last1); | |
3611 | auto __d2 = std::distance(__first2, __last2); | |
3612 | if (__d1 == 0 && __d2 == 0) | |
3613 | return true; | |
3614 | if (__d1 != __d2) | |
3615 | return false; | |
3616 | } | |
3617 | ||
3618 | for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan) | |
3619 | { | |
ea89b248 FD |
3620 | if (__scan != std::__find_if(__first1, __scan, |
3621 | __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))) | |
f7fbb003 JW |
3622 | continue; // We've seen this one before. |
3623 | ||
ea89b248 FD |
3624 | auto __matches = std::__count_if(__first2, __last2, |
3625 | __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)); | |
f7fbb003 | 3626 | if (0 == __matches |
ea89b248 FD |
3627 | || std::__count_if(__scan, __last1, |
3628 | __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)) | |
3629 | != __matches) | |
f7fbb003 JW |
3630 | return false; |
3631 | } | |
3632 | return true; | |
3633 | } | |
ea89b248 FD |
3634 | |
3635 | /** | |
3636 | * @brief Checks whether a permutaion of the second sequence is equal | |
3637 | * to the first sequence. | |
3638 | * @ingroup non_mutating_algorithms | |
3639 | * @param __first1 Start of first range. | |
3640 | * @param __last1 End of first range. | |
3641 | * @param __first2 Start of second range. | |
3642 | * @param __last2 End of first range. | |
3643 | * @return true if there exists a permutation of the elements in the range | |
3644 | * [__first2, __last2), beginning with ForwardIterator2 begin, | |
3645 | * such that equal(__first1, __last1, begin) returns true; | |
3646 | * otherwise, returns false. | |
3647 | */ | |
3648 | template<typename _ForwardIterator1, typename _ForwardIterator2> | |
3bd2644c | 3649 | inline bool |
ea89b248 FD |
3650 | is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
3651 | _ForwardIterator2 __first2, _ForwardIterator2 __last2) | |
3652 | { | |
3653 | __glibcxx_requires_valid_range(__first1, __last1); | |
3654 | __glibcxx_requires_valid_range(__first2, __last2); | |
3655 | ||
3656 | return | |
3657 | std::__is_permutation(__first1, __last1, __first2, __last2, | |
3658 | __gnu_cxx::__ops::__iter_equal_to_iter()); | |
3659 | } | |
3660 | ||
3661 | /** | |
3662 | * @brief Checks whether a permutation of the second sequence is equal | |
3663 | * to the first sequence. | |
3664 | * @ingroup non_mutating_algorithms | |
3665 | * @param __first1 Start of first range. | |
3666 | * @param __last1 End of first range. | |
3667 | * @param __first2 Start of second range. | |
3668 | * @param __last2 End of first range. | |
3669 | * @param __pred A binary predicate. | |
3670 | * @return true if there exists a permutation of the elements in the range | |
3671 | * [__first2, __last2), beginning with ForwardIterator2 begin, | |
3672 | * such that equal(__first1, __last1, __begin, __pred) returns true; | |
3673 | * otherwise, returns false. | |
3674 | */ | |
3675 | template<typename _ForwardIterator1, typename _ForwardIterator2, | |
3676 | typename _BinaryPredicate> | |
3bd2644c | 3677 | inline bool |
ea89b248 FD |
3678 | is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
3679 | _ForwardIterator2 __first2, _ForwardIterator2 __last2, | |
3680 | _BinaryPredicate __pred) | |
3681 | { | |
3682 | __glibcxx_requires_valid_range(__first1, __last1); | |
3683 | __glibcxx_requires_valid_range(__first2, __last2); | |
3684 | ||
3685 | return std::__is_permutation(__first1, __last1, __first2, __last2, | |
3686 | __gnu_cxx::__ops::__iter_comp_iter(__pred)); | |
3687 | } | |
4db1cb44 ESR |
3688 | |
3689 | #if __cplusplus > 201402L | |
3690 | ||
3691 | #define __cpp_lib_clamp 201603 | |
3692 | ||
3693 | /** | |
3694 | * @brief Returns the value clamped between lo and hi. | |
3695 | * @ingroup sorting_algorithms | |
3696 | * @param __val A value of arbitrary type. | |
3697 | * @param __lo A lower limit of arbitrary type. | |
3698 | * @param __hi An upper limit of arbitrary type. | |
3699 | * @return max(__val, __lo) if __val < __hi or min(__val, __hi) otherwise. | |
3700 | */ | |
3701 | template<typename _Tp> | |
3702 | constexpr const _Tp& | |
3703 | clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi) | |
3704 | { | |
3705 | __glibcxx_assert(!(__hi < __lo)); | |
3706 | return (__val < __lo) ? __lo : (__hi < __val) ? __hi : __val; | |
3707 | } | |
3708 | ||
3709 | /** | |
3710 | * @brief Returns the value clamped between lo and hi. | |
3711 | * @ingroup sorting_algorithms | |
3712 | * @param __val A value of arbitrary type. | |
3713 | * @param __lo A lower limit of arbitrary type. | |
3714 | * @param __hi An upper limit of arbitrary type. | |
3715 | * @param __comp A comparison functor. | |
3716 | * @return max(__val, __lo, __comp) if __comp(__val, __hi) | |
3717 | * or min(__val, __hi, __comp) otherwise. | |
3718 | */ | |
3719 | template<typename _Tp, typename _Compare> | |
3720 | constexpr const _Tp& | |
3721 | clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi, _Compare __comp) | |
3722 | { | |
3723 | __glibcxx_assert(!__comp(__hi, __lo)); | |
3724 | return __comp(__val, __lo) ? __lo : __comp(__hi, __val) ? __hi : __val; | |
3725 | } | |
3726 | #endif // C++17 | |
3727 | #endif // C++14 | |
f7fbb003 | 3728 | |
247d8075 | 3729 | #ifdef _GLIBCXX_USE_C99_STDINT_TR1 |
9d8ff2f6 EW |
3730 | /** |
3731 | * @brief Generate two uniformly distributed integers using a | |
3732 | * single distribution invocation. | |
3733 | * @param __b0 The upper bound for the first integer. | |
3734 | * @param __b1 The upper bound for the second integer. | |
3735 | * @param __g A UniformRandomBitGenerator. | |
3736 | * @return A pair (i, j) with i and j uniformly distributed | |
3737 | * over [0, __b0) and [0, __b1), respectively. | |
3738 | * | |
3739 | * Requires: __b0 * __b1 <= __g.max() - __g.min(). | |
3740 | * | |
3741 | * Using uniform_int_distribution with a range that is very | |
3742 | * small relative to the range of the generator ends up wasting | |
3743 | * potentially expensively generated randomness, since | |
3744 | * uniform_int_distribution does not store leftover randomness | |
3745 | * between invocations. | |
3746 | * | |
3747 | * If we know we want two integers in ranges that are sufficiently | |
3748 | * small, we can compose the ranges, use a single distribution | |
3749 | * invocation, and significantly reduce the waste. | |
3750 | */ | |
3751 | template<typename _IntType, typename _UniformRandomBitGenerator> | |
3752 | pair<_IntType, _IntType> | |
3753 | __gen_two_uniform_ints(_IntType __b0, _IntType __b1, | |
3754 | _UniformRandomBitGenerator&& __g) | |
3755 | { | |
3756 | _IntType __x | |
3757 | = uniform_int_distribution<_IntType>{0, (__b0 * __b1) - 1}(__g); | |
3758 | return std::make_pair(__x / __b1, __x % __b1); | |
3759 | } | |
3760 | ||
247d8075 PC |
3761 | /** |
3762 | * @brief Shuffle the elements of a sequence using a uniform random | |
3763 | * number generator. | |
3764 | * @ingroup mutating_algorithms | |
93c66bc6 BK |
3765 | * @param __first A forward iterator. |
3766 | * @param __last A forward iterator. | |
3767 | * @param __g A UniformRandomNumberGenerator (26.5.1.3). | |
247d8075 PC |
3768 | * @return Nothing. |
3769 | * | |
93c66bc6 | 3770 | * Reorders the elements in the range @p [__first,__last) using @p __g to |
247d8075 PC |
3771 | * provide random numbers. |
3772 | */ | |
3773 | template<typename _RandomAccessIterator, | |
3774 | typename _UniformRandomNumberGenerator> | |
3775 | void | |
3776 | shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last, | |
633e8e19 | 3777 | _UniformRandomNumberGenerator&& __g) |
247d8075 PC |
3778 | { |
3779 | // concept requirements | |
3780 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< | |
3781 | _RandomAccessIterator>) | |
3782 | __glibcxx_requires_valid_range(__first, __last); | |
3783 | ||
3784 | if (__first == __last) | |
3785 | return; | |
3786 | ||
3787 | typedef typename iterator_traits<_RandomAccessIterator>::difference_type | |
3788 | _DistanceType; | |
3789 | ||
3790 | typedef typename std::make_unsigned<_DistanceType>::type __ud_type; | |
3791 | typedef typename std::uniform_int_distribution<__ud_type> __distr_type; | |
3792 | typedef typename __distr_type::param_type __p_type; | |
38e34671 | 3793 | |
9d8ff2f6 EW |
3794 | typedef typename remove_reference<_UniformRandomNumberGenerator>::type |
3795 | _Gen; | |
3796 | typedef typename common_type<typename _Gen::result_type, __ud_type>::type | |
3797 | __uc_type; | |
38e34671 EW |
3798 | |
3799 | const __uc_type __urngrange = __g.max() - __g.min(); | |
3800 | const __uc_type __urange = __uc_type(__last - __first); | |
3801 | ||
3802 | if (__urngrange / __urange >= __urange) | |
3803 | // I.e. (__urngrange >= __urange * __urange) but without wrap issues. | |
3804 | { | |
3805 | _RandomAccessIterator __i = __first + 1; | |
3806 | ||
3807 | // Since we know the range isn't empty, an even number of elements | |
3808 | // means an uneven number of elements /to swap/, in which case we | |
3809 | // do the first one up front: | |
3810 | ||
3811 | if ((__urange % 2) == 0) | |
3812 | { | |
3813 | __distr_type __d{0, 1}; | |
3814 | std::iter_swap(__i++, __first + __d(__g)); | |
3815 | } | |
3816 | ||
3817 | // Now we know that __last - __i is even, so we do the rest in pairs, | |
3818 | // using a single distribution invocation to produce swap positions | |
3819 | // for two successive elements at a time: | |
3820 | ||
3821 | while (__i != __last) | |
3822 | { | |
3823 | const __uc_type __swap_range = __uc_type(__i - __first) + 1; | |
38e34671 | 3824 | |
9d8ff2f6 EW |
3825 | const pair<__uc_type, __uc_type> __pospos = |
3826 | __gen_two_uniform_ints(__swap_range, __swap_range + 1, __g); | |
38e34671 | 3827 | |
9d8ff2f6 EW |
3828 | std::iter_swap(__i++, __first + __pospos.first); |
3829 | std::iter_swap(__i++, __first + __pospos.second); | |
38e34671 EW |
3830 | } |
3831 | ||
3832 | return; | |
3833 | } | |
3834 | ||
247d8075 PC |
3835 | __distr_type __d; |
3836 | ||
3837 | for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) | |
3838 | std::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first))); | |
3839 | } | |
3840 | #endif | |
3841 | ||
734f5023 | 3842 | #endif // C++11 |
4b7ed13a | 3843 | |
12ffa228 | 3844 | _GLIBCXX_BEGIN_NAMESPACE_ALGO |
c2ba9709 JS |
3845 | |
3846 | /** | |
3847 | * @brief Apply a function to every element of a sequence. | |
5b9daa7e | 3848 | * @ingroup non_mutating_algorithms |
93c66bc6 BK |
3849 | * @param __first An input iterator. |
3850 | * @param __last An input iterator. | |
3851 | * @param __f A unary function object. | |
d1129d45 | 3852 | * @return @p __f |
c2ba9709 | 3853 | * |
93c66bc6 BK |
3854 | * Applies the function object @p __f to each element in the range |
3855 | * @p [first,last). @p __f must not modify the order of the sequence. | |
3856 | * If @p __f has a return value it is ignored. | |
c2ba9709 JS |
3857 | */ |
3858 | template<typename _InputIterator, typename _Function> | |
3859 | _Function | |
3860 | for_each(_InputIterator __first, _InputIterator __last, _Function __f) | |
3861 | { | |
3862 | // concept requirements | |
3863 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) | |
3864 | __glibcxx_requires_valid_range(__first, __last); | |
3865 | for (; __first != __last; ++__first) | |
3866 | __f(*__first); | |
d1129d45 | 3867 | return __f; // N.B. [alg.foreach] says std::move(f) but it's redundant. |
c2ba9709 JS |
3868 | } |
3869 | ||
ed920373 JW |
3870 | #if __cplusplus >= 201703L |
3871 | /** | |
3872 | * @brief Apply a function to every element of a sequence. | |
3873 | * @ingroup non_mutating_algorithms | |
3874 | * @param __first An input iterator. | |
3875 | * @param __n A value convertible to an integer. | |
3876 | * @param __f A unary function object. | |
3877 | * @return `__first+__n` | |
3878 | * | |
3879 | * Applies the function object `__f` to each element in the range | |
3880 | * `[first, first+n)`. `__f` must not modify the order of the sequence. | |
3881 | * If `__f` has a return value it is ignored. | |
3882 | */ | |
3883 | template<typename _InputIterator, typename _Size, typename _Function> | |
3884 | _InputIterator | |
3885 | for_each_n(_InputIterator __first, _Size __n, _Function __f) | |
3886 | { | |
3887 | auto __n2 = std::__size_to_integer(__n); | |
3888 | using _Cat = typename iterator_traits<_InputIterator>::iterator_category; | |
3889 | if constexpr (is_base_of_v<random_access_iterator_tag, _Cat>) | |
3890 | return std::for_each(__first, __first + __n2, __f); | |
3891 | else | |
3892 | { | |
3893 | while (__n2-->0) | |
3894 | { | |
3895 | __f(*__first); | |
3896 | ++__first; | |
3897 | } | |
3898 | return __first; | |
3899 | } | |
3900 | } | |
3901 | #endif // C++17 | |
3902 | ||
c2ba9709 JS |
3903 | /** |
3904 | * @brief Find the first occurrence of a value in a sequence. | |
5b9daa7e | 3905 | * @ingroup non_mutating_algorithms |
93c66bc6 BK |
3906 | * @param __first An input iterator. |
3907 | * @param __last An input iterator. | |
3908 | * @param __val The value to find. | |
3909 | * @return The first iterator @c i in the range @p [__first,__last) | |
3910 | * such that @c *i == @p __val, or @p __last if no such iterator exists. | |
c2ba9709 JS |
3911 | */ |
3912 | template<typename _InputIterator, typename _Tp> | |
3913 | inline _InputIterator | |
3914 | find(_InputIterator __first, _InputIterator __last, | |
3915 | const _Tp& __val) | |
3916 | { | |
3917 | // concept requirements | |
3918 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) | |
3919 | __glibcxx_function_requires(_EqualOpConcept< | |
3920 | typename iterator_traits<_InputIterator>::value_type, _Tp>) | |
3921 | __glibcxx_requires_valid_range(__first, __last); | |
ea89b248 FD |
3922 | return std::__find_if(__first, __last, |
3923 | __gnu_cxx::__ops::__iter_equals_val(__val)); | |
c2ba9709 JS |
3924 | } |
3925 | ||
3926 | /** | |
3927 | * @brief Find the first element in a sequence for which a | |
3928 | * predicate is true. | |
5b9daa7e | 3929 | * @ingroup non_mutating_algorithms |
93c66bc6 BK |
3930 | * @param __first An input iterator. |
3931 | * @param __last An input iterator. | |
3932 | * @param __pred A predicate. | |
3933 | * @return The first iterator @c i in the range @p [__first,__last) | |
3934 | * such that @p __pred(*i) is true, or @p __last if no such iterator exists. | |
c2ba9709 JS |
3935 | */ |
3936 | template<typename _InputIterator, typename _Predicate> | |
3937 | inline _InputIterator | |
3938 | find_if(_InputIterator __first, _InputIterator __last, | |
3939 | _Predicate __pred) | |
3940 | { | |
3941 | // concept requirements | |
3942 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) | |
3943 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, | |
3944 | typename iterator_traits<_InputIterator>::value_type>) | |
3945 | __glibcxx_requires_valid_range(__first, __last); | |
ea89b248 FD |
3946 | |
3947 | return std::__find_if(__first, __last, | |
3948 | __gnu_cxx::__ops::__pred_iter(__pred)); | |
c2ba9709 JS |
3949 | } |
3950 | ||
3951 | /** | |
3952 | * @brief Find element from a set in a sequence. | |
5b9daa7e | 3953 | * @ingroup non_mutating_algorithms |
93c66bc6 BK |
3954 | * @param __first1 Start of range to search. |
3955 | * @param __last1 End of range to search. | |
3956 | * @param __first2 Start of match candidates. | |
3957 | * @param __last2 End of match candidates. | |
c2ba9709 | 3958 | * @return The first iterator @c i in the range |
93c66bc6 BK |
3959 | * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an |
3960 | * iterator in [__first2,__last2), or @p __last1 if no such iterator exists. | |
c2ba9709 | 3961 | * |
93c66bc6 BK |
3962 | * Searches the range @p [__first1,__last1) for an element that is |
3963 | * equal to some element in the range [__first2,__last2). If | |
3964 | * found, returns an iterator in the range [__first1,__last1), | |
3965 | * otherwise returns @p __last1. | |
c2ba9709 JS |
3966 | */ |
3967 | template<typename _InputIterator, typename _ForwardIterator> | |
3968 | _InputIterator | |
3969 | find_first_of(_InputIterator __first1, _InputIterator __last1, | |
3970 | _ForwardIterator __first2, _ForwardIterator __last2) | |
3971 | { | |
3972 | // concept requirements | |
3973 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) | |
3974 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
3975 | __glibcxx_function_requires(_EqualOpConcept< | |
3976 | typename iterator_traits<_InputIterator>::value_type, | |
3977 | typename iterator_traits<_ForwardIterator>::value_type>) | |
3978 | __glibcxx_requires_valid_range(__first1, __last1); | |
3979 | __glibcxx_requires_valid_range(__first2, __last2); | |
3980 | ||
3981 | for (; __first1 != __last1; ++__first1) | |
3982 | for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter) | |
3983 | if (*__first1 == *__iter) | |
3984 | return __first1; | |
3985 | return __last1; | |
3986 | } | |
3987 | ||
3988 | /** | |
3989 | * @brief Find element from a set in a sequence using a predicate. | |
5b9daa7e | 3990 | * @ingroup non_mutating_algorithms |
93c66bc6 BK |
3991 | * @param __first1 Start of range to search. |
3992 | * @param __last1 End of range to search. | |
3993 | * @param __first2 Start of match candidates. | |
3994 | * @param __last2 End of match candidates. | |
3995 | * @param __comp Predicate to use. | |
c2ba9709 | 3996 | * @return The first iterator @c i in the range |
93c66bc6 BK |
3997 | * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true |
3998 | * and i2 is an iterator in [__first2,__last2), or @p __last1 if no | |
3999 | * such iterator exists. | |
c2ba9709 JS |
4000 | * |
4001 | ||
93c66bc6 BK |
4002 | * Searches the range @p [__first1,__last1) for an element that is |
4003 | * equal to some element in the range [__first2,__last2). If | |
4004 | * found, returns an iterator in the range [__first1,__last1), | |
4005 | * otherwise returns @p __last1. | |
c2ba9709 JS |
4006 | */ |
4007 | template<typename _InputIterator, typename _ForwardIterator, | |
4008 | typename _BinaryPredicate> | |
4009 | _InputIterator | |
4010 | find_first_of(_InputIterator __first1, _InputIterator __last1, | |
4011 | _ForwardIterator __first2, _ForwardIterator __last2, | |
4012 | _BinaryPredicate __comp) | |
4013 | { | |
4014 | // concept requirements | |
4015 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) | |
4016 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
4017 | __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, | |
4018 | typename iterator_traits<_InputIterator>::value_type, | |
4019 | typename iterator_traits<_ForwardIterator>::value_type>) | |
4020 | __glibcxx_requires_valid_range(__first1, __last1); | |
4021 | __glibcxx_requires_valid_range(__first2, __last2); | |
4022 | ||
4023 | for (; __first1 != __last1; ++__first1) | |
4024 | for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter) | |
4025 | if (__comp(*__first1, *__iter)) | |
4026 | return __first1; | |
4027 | return __last1; | |
4028 | } | |
4029 | ||
4030 | /** | |
4031 | * @brief Find two adjacent values in a sequence that are equal. | |
5b9daa7e | 4032 | * @ingroup non_mutating_algorithms |
93c66bc6 BK |
4033 | * @param __first A forward iterator. |
4034 | * @param __last A forward iterator. | |
c2ba9709 | 4035 | * @return The first iterator @c i such that @c i and @c i+1 are both |
93c66bc6 BK |
4036 | * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1), |
4037 | * or @p __last if no such iterator exists. | |
c2ba9709 JS |
4038 | */ |
4039 | template<typename _ForwardIterator> | |
3bd2644c | 4040 | inline _ForwardIterator |
c2ba9709 JS |
4041 | adjacent_find(_ForwardIterator __first, _ForwardIterator __last) |
4042 | { | |
4043 | // concept requirements | |
4044 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
4045 | __glibcxx_function_requires(_EqualityComparableConcept< | |
4046 | typename iterator_traits<_ForwardIterator>::value_type>) | |
4047 | __glibcxx_requires_valid_range(__first, __last); | |
ea89b248 FD |
4048 | |
4049 | return std::__adjacent_find(__first, __last, | |
4050 | __gnu_cxx::__ops::__iter_equal_to_iter()); | |
c2ba9709 JS |
4051 | } |
4052 | ||
4053 | /** | |
4054 | * @brief Find two adjacent values in a sequence using a predicate. | |
5b9daa7e | 4055 | * @ingroup non_mutating_algorithms |
93c66bc6 BK |
4056 | * @param __first A forward iterator. |
4057 | * @param __last A forward iterator. | |
4058 | * @param __binary_pred A binary predicate. | |
c2ba9709 | 4059 | * @return The first iterator @c i such that @c i and @c i+1 are both |
93c66bc6 BK |
4060 | * valid iterators in @p [__first,__last) and such that |
4061 | * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator | |
c2ba9709 JS |
4062 | * exists. |
4063 | */ | |
4064 | template<typename _ForwardIterator, typename _BinaryPredicate> | |
3bd2644c | 4065 | inline _ForwardIterator |
c2ba9709 JS |
4066 | adjacent_find(_ForwardIterator __first, _ForwardIterator __last, |
4067 | _BinaryPredicate __binary_pred) | |
4068 | { | |
4069 | // concept requirements | |
4070 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
4071 | __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, | |
4072 | typename iterator_traits<_ForwardIterator>::value_type, | |
4073 | typename iterator_traits<_ForwardIterator>::value_type>) | |
4074 | __glibcxx_requires_valid_range(__first, __last); | |
ea89b248 FD |
4075 | |
4076 | return std::__adjacent_find(__first, __last, | |
4077 | __gnu_cxx::__ops::__iter_comp_iter(__binary_pred)); | |
c2ba9709 JS |
4078 | } |
4079 | ||
4080 | /** | |
4081 | * @brief Count the number of copies of a value in a sequence. | |
5b9daa7e | 4082 | * @ingroup non_mutating_algorithms |
93c66bc6 BK |
4083 | * @param __first An input iterator. |
4084 | * @param __last An input iterator. | |
4085 | * @param __value The value to be counted. | |
4086 | * @return The number of iterators @c i in the range @p [__first,__last) | |
4087 | * for which @c *i == @p __value | |
c2ba9709 JS |
4088 | */ |
4089 | template<typename _InputIterator, typename _Tp> | |
3bd2644c | 4090 | inline typename iterator_traits<_InputIterator>::difference_type |
c2ba9709 JS |
4091 | count(_InputIterator __first, _InputIterator __last, const _Tp& __value) |
4092 | { | |
4093 | // concept requirements | |
4094 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) | |
4095 | __glibcxx_function_requires(_EqualOpConcept< | |
ea89b248 | 4096 | typename iterator_traits<_InputIterator>::value_type, _Tp>) |
c2ba9709 | 4097 | __glibcxx_requires_valid_range(__first, __last); |
ea89b248 FD |
4098 | |
4099 | return std::__count_if(__first, __last, | |
4100 | __gnu_cxx::__ops::__iter_equals_val(__value)); | |
c2ba9709 JS |
4101 | } |
4102 | ||
4103 | /** | |
4104 | * @brief Count the elements of a sequence for which a predicate is true. | |
5b9daa7e | 4105 | * @ingroup non_mutating_algorithms |
93c66bc6 BK |
4106 | * @param __first An input iterator. |
4107 | * @param __last An input iterator. | |
4108 | * @param __pred A predicate. | |
4109 | * @return The number of iterators @c i in the range @p [__first,__last) | |
4110 | * for which @p __pred(*i) is true. | |
c2ba9709 JS |
4111 | */ |
4112 | template<typename _InputIterator, typename _Predicate> | |
3bd2644c | 4113 | inline typename iterator_traits<_InputIterator>::difference_type |
c2ba9709 JS |
4114 | count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) |
4115 | { | |
4116 | // concept requirements | |
4117 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) | |
4118 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, | |
4119 | typename iterator_traits<_InputIterator>::value_type>) | |
4120 | __glibcxx_requires_valid_range(__first, __last); | |
ea89b248 FD |
4121 | |
4122 | return std::__count_if(__first, __last, | |
4123 | __gnu_cxx::__ops::__pred_iter(__pred)); | |
c2ba9709 JS |
4124 | } |
4125 | ||
4126 | /** | |
4127 | * @brief Search a sequence for a matching sub-sequence. | |
5b9daa7e | 4128 | * @ingroup non_mutating_algorithms |
93c66bc6 BK |
4129 | * @param __first1 A forward iterator. |
4130 | * @param __last1 A forward iterator. | |
4131 | * @param __first2 A forward iterator. | |
4132 | * @param __last2 A forward iterator. | |
4133 | * @return The first iterator @c i in the range @p | |
4134 | * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p | |
4135 | * *(__first2+N) for each @c N in the range @p | |
4136 | * [0,__last2-__first2), or @p __last1 if no such iterator exists. | |
c2ba9709 | 4137 | * |
93c66bc6 BK |
4138 | * Searches the range @p [__first1,__last1) for a sub-sequence that |
4139 | * compares equal value-by-value with the sequence given by @p | |
4140 | * [__first2,__last2) and returns an iterator to the first element | |
4141 | * of the sub-sequence, or @p __last1 if the sub-sequence is not | |
4142 | * found. | |
4143 | * | |
4144 | * Because the sub-sequence must lie completely within the range @p | |
4145 | * [__first1,__last1) it must start at a position less than @p | |
4146 | * __last1-(__last2-__first2) where @p __last2-__first2 is the | |
4147 | * length of the sub-sequence. | |
c2ba9709 | 4148 | * |
c2ba9709 | 4149 | * This means that the returned iterator @c i will be in the range |
93c66bc6 | 4150 | * @p [__first1,__last1-(__last2-__first2)) |
c2ba9709 JS |
4151 | */ |
4152 | template<typename _ForwardIterator1, typename _ForwardIterator2> | |
3bd2644c | 4153 | inline _ForwardIterator1 |
c2ba9709 JS |
4154 | search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
4155 | _ForwardIterator2 __first2, _ForwardIterator2 __last2) | |
4156 | { | |
4157 | // concept requirements | |
4158 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) | |
4159 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) | |
4160 | __glibcxx_function_requires(_EqualOpConcept< | |
4161 | typename iterator_traits<_ForwardIterator1>::value_type, | |
4162 | typename iterator_traits<_ForwardIterator2>::value_type>) | |
4163 | __glibcxx_requires_valid_range(__first1, __last1); | |
4164 | __glibcxx_requires_valid_range(__first2, __last2); | |
4165 | ||
ea89b248 FD |
4166 | return std::__search(__first1, __last1, __first2, __last2, |
4167 | __gnu_cxx::__ops::__iter_equal_to_iter()); | |
c2ba9709 JS |
4168 | } |
4169 | ||
4170 | /** | |
4171 | * @brief Search a sequence for a matching sub-sequence using a predicate. | |
5b9daa7e | 4172 | * @ingroup non_mutating_algorithms |
93c66bc6 BK |
4173 | * @param __first1 A forward iterator. |
4174 | * @param __last1 A forward iterator. | |
4175 | * @param __first2 A forward iterator. | |
4176 | * @param __last2 A forward iterator. | |
4177 | * @param __predicate A binary predicate. | |
c2ba9709 | 4178 | * @return The first iterator @c i in the range |
93c66bc6 BK |
4179 | * @p [__first1,__last1-(__last2-__first2)) such that |
4180 | * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range | |
4181 | * @p [0,__last2-__first2), or @p __last1 if no such iterator exists. | |
c2ba9709 | 4182 | * |
93c66bc6 BK |
4183 | * Searches the range @p [__first1,__last1) for a sub-sequence that |
4184 | * compares equal value-by-value with the sequence given by @p | |
4185 | * [__first2,__last2), using @p __predicate to determine equality, | |
4186 | * and returns an iterator to the first element of the | |
4187 | * sub-sequence, or @p __last1 if no such iterator exists. | |
c2ba9709 JS |
4188 | * |
4189 | * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2) | |
4190 | */ | |
4191 | template<typename _ForwardIterator1, typename _ForwardIterator2, | |
4192 | typename _BinaryPredicate> | |
3bd2644c | 4193 | inline _ForwardIterator1 |
c2ba9709 JS |
4194 | search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
4195 | _ForwardIterator2 __first2, _ForwardIterator2 __last2, | |
4196 | _BinaryPredicate __predicate) | |
4197 | { | |
4198 | // concept requirements | |
4199 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) | |
4200 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) | |
4201 | __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, | |
4202 | typename iterator_traits<_ForwardIterator1>::value_type, | |
4203 | typename iterator_traits<_ForwardIterator2>::value_type>) | |
4204 | __glibcxx_requires_valid_range(__first1, __last1); | |
4205 | __glibcxx_requires_valid_range(__first2, __last2); | |
4206 | ||
ea89b248 FD |
4207 | return std::__search(__first1, __last1, __first2, __last2, |
4208 | __gnu_cxx::__ops::__iter_comp_iter(__predicate)); | |
c2ba9709 JS |
4209 | } |
4210 | ||
c2ba9709 JS |
4211 | /** |
4212 | * @brief Search a sequence for a number of consecutive values. | |
5b9daa7e | 4213 | * @ingroup non_mutating_algorithms |
93c66bc6 BK |
4214 | * @param __first A forward iterator. |
4215 | * @param __last A forward iterator. | |
4216 | * @param __count The number of consecutive values. | |
4217 | * @param __val The value to find. | |
4218 | * @return The first iterator @c i in the range @p | |
4219 | * [__first,__last-__count) such that @c *(i+N) == @p __val for | |
4220 | * each @c N in the range @p [0,__count), or @p __last if no such | |
4221 | * iterator exists. | |
c2ba9709 | 4222 | * |
93c66bc6 BK |
4223 | * Searches the range @p [__first,__last) for @p count consecutive elements |
4224 | * equal to @p __val. | |
c2ba9709 JS |
4225 | */ |
4226 | template<typename _ForwardIterator, typename _Integer, typename _Tp> | |
3bd2644c | 4227 | inline _ForwardIterator |
c2ba9709 JS |
4228 | search_n(_ForwardIterator __first, _ForwardIterator __last, |
4229 | _Integer __count, const _Tp& __val) | |
4230 | { | |
4231 | // concept requirements | |
4232 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
4233 | __glibcxx_function_requires(_EqualOpConcept< | |
ea89b248 | 4234 | typename iterator_traits<_ForwardIterator>::value_type, _Tp>) |
c2ba9709 JS |
4235 | __glibcxx_requires_valid_range(__first, __last); |
4236 | ||
ea89b248 FD |
4237 | return std::__search_n(__first, __last, __count, |
4238 | __gnu_cxx::__ops::__iter_equals_val(__val)); | |
c2ba9709 JS |
4239 | } |
4240 | ||
4241 | ||
4242 | /** | |
4243 | * @brief Search a sequence for a number of consecutive values using a | |
4244 | * predicate. | |
5b9daa7e | 4245 | * @ingroup non_mutating_algorithms |
93c66bc6 BK |
4246 | * @param __first A forward iterator. |
4247 | * @param __last A forward iterator. | |
4248 | * @param __count The number of consecutive values. | |
4249 | * @param __val The value to find. | |
4250 | * @param __binary_pred A binary predicate. | |
4251 | * @return The first iterator @c i in the range @p | |
4252 | * [__first,__last-__count) such that @p | |
4253 | * __binary_pred(*(i+N),__val) is true for each @c N in the range | |
4254 | * @p [0,__count), or @p __last if no such iterator exists. | |
c2ba9709 | 4255 | * |
93c66bc6 BK |
4256 | * Searches the range @p [__first,__last) for @p __count |
4257 | * consecutive elements for which the predicate returns true. | |
c2ba9709 JS |
4258 | */ |
4259 | template<typename _ForwardIterator, typename _Integer, typename _Tp, | |
ec494945 | 4260 | typename _BinaryPredicate> |
3bd2644c | 4261 | inline _ForwardIterator |
c2ba9709 JS |
4262 | search_n(_ForwardIterator __first, _ForwardIterator __last, |
4263 | _Integer __count, const _Tp& __val, | |
4264 | _BinaryPredicate __binary_pred) | |
4265 | { | |
4266 | // concept requirements | |
4267 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
4268 | __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, | |
4269 | typename iterator_traits<_ForwardIterator>::value_type, _Tp>) | |
4270 | __glibcxx_requires_valid_range(__first, __last); | |
4271 | ||
ea89b248 FD |
4272 | return std::__search_n(__first, __last, __count, |
4273 | __gnu_cxx::__ops::__iter_comp_val(__binary_pred, __val)); | |
c2ba9709 JS |
4274 | } |
4275 | ||
f710ca61 JW |
4276 | #if __cplusplus > 201402L |
4277 | /** @brief Search a sequence using a Searcher object. | |
4278 | * | |
4279 | * @param __first A forward iterator. | |
4280 | * @param __last A forward iterator. | |
4281 | * @param __searcher A callable object. | |
4282 | * @return @p __searcher(__first,__last).first | |
4283 | */ | |
4284 | template<typename _ForwardIterator, typename _Searcher> | |
4285 | inline _ForwardIterator | |
4286 | search(_ForwardIterator __first, _ForwardIterator __last, | |
4287 | const _Searcher& __searcher) | |
4288 | { return __searcher(__first, __last).first; } | |
4289 | #endif | |
c2ba9709 JS |
4290 | |
4291 | /** | |
4292 | * @brief Perform an operation on a sequence. | |
5b9daa7e | 4293 | * @ingroup mutating_algorithms |
93c66bc6 BK |
4294 | * @param __first An input iterator. |
4295 | * @param __last An input iterator. | |
4296 | * @param __result An output iterator. | |
4297 | * @param __unary_op A unary operator. | |
4298 | * @return An output iterator equal to @p __result+(__last-__first). | |
c2ba9709 JS |
4299 | * |
4300 | * Applies the operator to each element in the input range and assigns | |
4301 | * the results to successive elements of the output sequence. | |
93c66bc6 BK |
4302 | * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the |
4303 | * range @p [0,__last-__first). | |
c2ba9709 JS |
4304 | * |
4305 | * @p unary_op must not alter its argument. | |
4306 | */ | |
4307 | template<typename _InputIterator, typename _OutputIterator, | |
4308 | typename _UnaryOperation> | |
4309 | _OutputIterator | |
4310 | transform(_InputIterator __first, _InputIterator __last, | |
4311 | _OutputIterator __result, _UnaryOperation __unary_op) | |
4312 | { | |
4313 | // concept requirements | |
4314 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) | |
4315 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
ec494945 FD |
4316 | // "the type returned by a _UnaryOperation" |
4317 | __typeof__(__unary_op(*__first))>) | |
c2ba9709 JS |
4318 | __glibcxx_requires_valid_range(__first, __last); |
4319 | ||
f970a17d | 4320 | for (; __first != __last; ++__first, (void)++__result) |
c2ba9709 JS |
4321 | *__result = __unary_op(*__first); |
4322 | return __result; | |
4323 | } | |
4324 | ||
4325 | /** | |
4326 | * @brief Perform an operation on corresponding elements of two sequences. | |
5b9daa7e | 4327 | * @ingroup mutating_algorithms |
93c66bc6 BK |
4328 | * @param __first1 An input iterator. |
4329 | * @param __last1 An input iterator. | |
4330 | * @param __first2 An input iterator. | |
4331 | * @param __result An output iterator. | |
4332 | * @param __binary_op A binary operator. | |
c2ba9709 JS |
4333 | * @return An output iterator equal to @p result+(last-first). |
4334 | * | |
4335 | * Applies the operator to the corresponding elements in the two | |
4336 | * input ranges and assigns the results to successive elements of the | |
4337 | * output sequence. | |
93c66bc6 BK |
4338 | * Evaluates @p |
4339 | * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each | |
4340 | * @c N in the range @p [0,__last1-__first1). | |
c2ba9709 JS |
4341 | * |
4342 | * @p binary_op must not alter either of its arguments. | |
4343 | */ | |
4344 | template<typename _InputIterator1, typename _InputIterator2, | |
4345 | typename _OutputIterator, typename _BinaryOperation> | |
4346 | _OutputIterator | |
4347 | transform(_InputIterator1 __first1, _InputIterator1 __last1, | |
4348 | _InputIterator2 __first2, _OutputIterator __result, | |
4349 | _BinaryOperation __binary_op) | |
4350 | { | |
4351 | // concept requirements | |
4352 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) | |
4353 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) | |
4354 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
ec494945 FD |
4355 | // "the type returned by a _BinaryOperation" |
4356 | __typeof__(__binary_op(*__first1,*__first2))>) | |
c2ba9709 JS |
4357 | __glibcxx_requires_valid_range(__first1, __last1); |
4358 | ||
f970a17d | 4359 | for (; __first1 != __last1; ++__first1, (void)++__first2, ++__result) |
c2ba9709 JS |
4360 | *__result = __binary_op(*__first1, *__first2); |
4361 | return __result; | |
4362 | } | |
4363 | ||
4364 | /** | |
4365 | * @brief Replace each occurrence of one value in a sequence with another | |
4366 | * value. | |
5b9daa7e | 4367 | * @ingroup mutating_algorithms |
93c66bc6 BK |
4368 | * @param __first A forward iterator. |
4369 | * @param __last A forward iterator. | |
4370 | * @param __old_value The value to be replaced. | |
4371 | * @param __new_value The replacement value. | |
c2ba9709 JS |
4372 | * @return replace() returns no value. |
4373 | * | |
93c66bc6 BK |
4374 | * For each iterator @c i in the range @p [__first,__last) if @c *i == |
4375 | * @p __old_value then the assignment @c *i = @p __new_value is performed. | |
c2ba9709 JS |
4376 | */ |
4377 | template<typename _ForwardIterator, typename _Tp> | |
4378 | void | |
4379 | replace(_ForwardIterator __first, _ForwardIterator __last, | |
4380 | const _Tp& __old_value, const _Tp& __new_value) | |
4381 | { | |
4382 | // concept requirements | |
4383 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< | |
4384 | _ForwardIterator>) | |
4385 | __glibcxx_function_requires(_EqualOpConcept< | |
4386 | typename iterator_traits<_ForwardIterator>::value_type, _Tp>) | |
4387 | __glibcxx_function_requires(_ConvertibleConcept<_Tp, | |
4388 | typename iterator_traits<_ForwardIterator>::value_type>) | |
4389 | __glibcxx_requires_valid_range(__first, __last); | |
4390 | ||
4391 | for (; __first != __last; ++__first) | |
4392 | if (*__first == __old_value) | |
4393 | *__first = __new_value; | |
4394 | } | |
4395 | ||
4396 | /** | |
4397 | * @brief Replace each value in a sequence for which a predicate returns | |
4398 | * true with another value. | |
5b9daa7e | 4399 | * @ingroup mutating_algorithms |
93c66bc6 BK |
4400 | * @param __first A forward iterator. |
4401 | * @param __last A forward iterator. | |
4402 | * @param __pred A predicate. | |
4403 | * @param __new_value The replacement value. | |
c2ba9709 JS |
4404 | * @return replace_if() returns no value. |
4405 | * | |
93c66bc6 BK |
4406 | * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i) |
4407 | * is true then the assignment @c *i = @p __new_value is performed. | |
c2ba9709 JS |
4408 | */ |
4409 | template<typename _ForwardIterator, typename _Predicate, typename _Tp> | |
4410 | void | |
4411 | replace_if(_ForwardIterator __first, _ForwardIterator __last, | |
4412 | _Predicate __pred, const _Tp& __new_value) | |
4413 | { | |
4414 | // concept requirements | |
4415 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< | |
4416 | _ForwardIterator>) | |
4417 | __glibcxx_function_requires(_ConvertibleConcept<_Tp, | |
4418 | typename iterator_traits<_ForwardIterator>::value_type>) | |
4419 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, | |
4420 | typename iterator_traits<_ForwardIterator>::value_type>) | |
4421 | __glibcxx_requires_valid_range(__first, __last); | |
4422 | ||
4423 | for (; __first != __last; ++__first) | |
4424 | if (__pred(*__first)) | |
4425 | *__first = __new_value; | |
4426 | } | |
4427 | ||
4428 | /** | |
4429 | * @brief Assign the result of a function object to each value in a | |
4430 | * sequence. | |
5b9daa7e | 4431 | * @ingroup mutating_algorithms |
93c66bc6 BK |
4432 | * @param __first A forward iterator. |
4433 | * @param __last A forward iterator. | |
4434 | * @param __gen A function object taking no arguments and returning | |
4f99f3d0 | 4435 | * std::iterator_traits<_ForwardIterator>::value_type |
c2ba9709 JS |
4436 | * @return generate() returns no value. |
4437 | * | |
93c66bc6 BK |
4438 | * Performs the assignment @c *i = @p __gen() for each @c i in the range |
4439 | * @p [__first,__last). | |
c2ba9709 JS |
4440 | */ |
4441 | template<typename _ForwardIterator, typename _Generator> | |
4442 | void | |
4443 | generate(_ForwardIterator __first, _ForwardIterator __last, | |
4444 | _Generator __gen) | |
4445 | { | |
4446 | // concept requirements | |
4447 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
4448 | __glibcxx_function_requires(_GeneratorConcept<_Generator, | |
4449 | typename iterator_traits<_ForwardIterator>::value_type>) | |
4450 | __glibcxx_requires_valid_range(__first, __last); | |
4451 | ||
4452 | for (; __first != __last; ++__first) | |
4453 | *__first = __gen(); | |
4454 | } | |
4455 | ||
4456 | /** | |
4457 | * @brief Assign the result of a function object to each value in a | |
4458 | * sequence. | |
5b9daa7e | 4459 | * @ingroup mutating_algorithms |
93c66bc6 BK |
4460 | * @param __first A forward iterator. |
4461 | * @param __n The length of the sequence. | |
4462 | * @param __gen A function object taking no arguments and returning | |
4f99f3d0 | 4463 | * std::iterator_traits<_ForwardIterator>::value_type |
93c66bc6 | 4464 | * @return The end of the sequence, @p __first+__n |
c2ba9709 | 4465 | * |
93c66bc6 BK |
4466 | * Performs the assignment @c *i = @p __gen() for each @c i in the range |
4467 | * @p [__first,__first+__n). | |
82ab4b64 | 4468 | * |
f373ad68 | 4469 | * If @p __n is negative, the function does nothing and returns @p __first. |
c2ba9709 | 4470 | */ |
846541dd JW |
4471 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
4472 | // DR 865. More algorithms that throw away information | |
4473 | // DR 426. search_n(), fill_n(), and generate_n() with negative n | |
c2ba9709 JS |
4474 | template<typename _OutputIterator, typename _Size, typename _Generator> |
4475 | _OutputIterator | |
4476 | generate_n(_OutputIterator __first, _Size __n, _Generator __gen) | |
4477 | { | |
4478 | // concept requirements | |
4479 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
ec494945 FD |
4480 | // "the type returned by a _Generator" |
4481 | __typeof__(__gen())>) | |
c2ba9709 | 4482 | |
846541dd JW |
4483 | typedef __decltype(std::__size_to_integer(__n)) _IntSize; |
4484 | for (_IntSize __niter = std::__size_to_integer(__n); | |
d67be443 | 4485 | __niter > 0; --__niter, (void) ++__first) |
c2ba9709 JS |
4486 | *__first = __gen(); |
4487 | return __first; | |
4488 | } | |
4489 | ||
c2ba9709 JS |
4490 | /** |
4491 | * @brief Copy a sequence, removing consecutive duplicate values. | |
5b9daa7e | 4492 | * @ingroup mutating_algorithms |
93c66bc6 BK |
4493 | * @param __first An input iterator. |
4494 | * @param __last An input iterator. | |
4495 | * @param __result An output iterator. | |
c2ba9709 JS |
4496 | * @return An iterator designating the end of the resulting sequence. |
4497 | * | |
93c66bc6 BK |
4498 | * Copies each element in the range @p [__first,__last) to the range |
4499 | * beginning at @p __result, except that only the first element is copied | |
c2ba9709 JS |
4500 | * from groups of consecutive elements that compare equal. |
4501 | * unique_copy() is stable, so the relative order of elements that are | |
4502 | * copied is unchanged. | |
4503 | * | |
c2ba9709 JS |
4504 | * _GLIBCXX_RESOLVE_LIB_DEFECTS |
4505 | * DR 241. Does unique_copy() require CopyConstructible and Assignable? | |
4506 | * | |
4507 | * _GLIBCXX_RESOLVE_LIB_DEFECTS | |
4508 | * DR 538. 241 again: Does unique_copy() require CopyConstructible and | |
4509 | * Assignable? | |
c2ba9709 JS |
4510 | */ |
4511 | template<typename _InputIterator, typename _OutputIterator> | |
4512 | inline _OutputIterator | |
4513 | unique_copy(_InputIterator __first, _InputIterator __last, | |
4514 | _OutputIterator __result) | |
4515 | { | |
4516 | // concept requirements | |
4517 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) | |
4518 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
4519 | typename iterator_traits<_InputIterator>::value_type>) | |
4520 | __glibcxx_function_requires(_EqualityComparableConcept< | |
4521 | typename iterator_traits<_InputIterator>::value_type>) | |
4522 | __glibcxx_requires_valid_range(__first, __last); | |
4523 | ||
4524 | if (__first == __last) | |
4525 | return __result; | |
4526 | return std::__unique_copy(__first, __last, __result, | |
ea89b248 | 4527 | __gnu_cxx::__ops::__iter_equal_to_iter(), |
c2ba9709 JS |
4528 | std::__iterator_category(__first), |
4529 | std::__iterator_category(__result)); | |
da5a2efd PC |
4530 | } |
4531 | ||
4532 | /** | |
c2ba9709 | 4533 | * @brief Copy a sequence, removing consecutive values using a predicate. |
5b9daa7e | 4534 | * @ingroup mutating_algorithms |
93c66bc6 BK |
4535 | * @param __first An input iterator. |
4536 | * @param __last An input iterator. | |
4537 | * @param __result An output iterator. | |
4538 | * @param __binary_pred A binary predicate. | |
c2ba9709 | 4539 | * @return An iterator designating the end of the resulting sequence. |
c868f37e | 4540 | * |
93c66bc6 BK |
4541 | * Copies each element in the range @p [__first,__last) to the range |
4542 | * beginning at @p __result, except that only the first element is copied | |
4543 | * from groups of consecutive elements for which @p __binary_pred returns | |
c2ba9709 JS |
4544 | * true. |
4545 | * unique_copy() is stable, so the relative order of elements that are | |
4546 | * copied is unchanged. | |
da5a2efd | 4547 | * |
c2ba9709 JS |
4548 | * _GLIBCXX_RESOLVE_LIB_DEFECTS |
4549 | * DR 241. Does unique_copy() require CopyConstructible and Assignable? | |
bf551f96 | 4550 | */ |
c2ba9709 JS |
4551 | template<typename _InputIterator, typename _OutputIterator, |
4552 | typename _BinaryPredicate> | |
4553 | inline _OutputIterator | |
4554 | unique_copy(_InputIterator __first, _InputIterator __last, | |
4555 | _OutputIterator __result, | |
4556 | _BinaryPredicate __binary_pred) | |
02d92e3b | 4557 | { |
c2ba9709 JS |
4558 | // concept requirements -- predicates checked later |
4559 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) | |
4560 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
4561 | typename iterator_traits<_InputIterator>::value_type>) | |
4562 | __glibcxx_requires_valid_range(__first, __last); | |
da5a2efd | 4563 | |
c2ba9709 JS |
4564 | if (__first == __last) |
4565 | return __result; | |
ea89b248 FD |
4566 | return std::__unique_copy(__first, __last, __result, |
4567 | __gnu_cxx::__ops::__iter_comp_iter(__binary_pred), | |
c2ba9709 JS |
4568 | std::__iterator_category(__first), |
4569 | std::__iterator_category(__result)); | |
4570 | } | |
da5a2efd | 4571 | |
2e7e2075 | 4572 | #if _GLIBCXX_HOSTED |
c2ba9709 JS |
4573 | /** |
4574 | * @brief Randomly shuffle the elements of a sequence. | |
5b9daa7e | 4575 | * @ingroup mutating_algorithms |
93c66bc6 BK |
4576 | * @param __first A forward iterator. |
4577 | * @param __last A forward iterator. | |
c2ba9709 JS |
4578 | * @return Nothing. |
4579 | * | |
93c66bc6 | 4580 | * Reorder the elements in the range @p [__first,__last) using a random |
c2ba9709 JS |
4581 | * distribution, so that every possible ordering of the sequence is |
4582 | * equally likely. | |
4583 | */ | |
4584 | template<typename _RandomAccessIterator> | |
4585 | inline void | |
4586 | random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last) | |
4587 | { | |
4588 | // concept requirements | |
4589 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< | |
4590 | _RandomAccessIterator>) | |
4591 | __glibcxx_requires_valid_range(__first, __last); | |
da5a2efd | 4592 | |
c2ba9709 JS |
4593 | if (__first != __last) |
4594 | for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) | |
bc5022d0 JW |
4595 | { |
4596 | // XXX rand() % N is not uniformly distributed | |
4597 | _RandomAccessIterator __j = __first | |
4598 | + std::rand() % ((__i - __first) + 1); | |
4599 | if (__i != __j) | |
4600 | std::iter_swap(__i, __j); | |
4601 | } | |
da5a2efd | 4602 | } |
2e7e2075 | 4603 | #endif |
da5a2efd PC |
4604 | |
4605 | /** | |
c2ba9709 JS |
4606 | * @brief Shuffle the elements of a sequence using a random number |
4607 | * generator. | |
5b9daa7e | 4608 | * @ingroup mutating_algorithms |
93c66bc6 BK |
4609 | * @param __first A forward iterator. |
4610 | * @param __last A forward iterator. | |
4611 | * @param __rand The RNG functor or function. | |
da5a2efd PC |
4612 | * @return Nothing. |
4613 | * | |
93c66bc6 BK |
4614 | * Reorders the elements in the range @p [__first,__last) using @p __rand to |
4615 | * provide a random distribution. Calling @p __rand(N) for a positive | |
c2ba9709 JS |
4616 | * integer @p N should return a randomly chosen integer from the |
4617 | * range [0,N). | |
da5a2efd | 4618 | */ |
c2ba9709 | 4619 | template<typename _RandomAccessIterator, typename _RandomNumberGenerator> |
da5a2efd | 4620 | void |
c2ba9709 | 4621 | random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last, |
734f5023 | 4622 | #if __cplusplus >= 201103L |
247d8075 PC |
4623 | _RandomNumberGenerator&& __rand) |
4624 | #else | |
c2ba9709 | 4625 | _RandomNumberGenerator& __rand) |
247d8075 | 4626 | #endif |
da5a2efd | 4627 | { |
da5a2efd | 4628 | // concept requirements |
c2ba9709 JS |
4629 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
4630 | _RandomAccessIterator>) | |
4631 | __glibcxx_requires_valid_range(__first, __last); | |
da5a2efd | 4632 | |
c2ba9709 | 4633 | if (__first == __last) |
da5a2efd | 4634 | return; |
c2ba9709 | 4635 | for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) |
bc5022d0 JW |
4636 | { |
4637 | _RandomAccessIterator __j = __first + __rand((__i - __first) + 1); | |
4638 | if (__i != __j) | |
4639 | std::iter_swap(__i, __j); | |
4640 | } | |
da5a2efd PC |
4641 | } |
4642 | ||
da5a2efd | 4643 | |
c2ba9709 JS |
4644 | /** |
4645 | * @brief Move elements for which a predicate is true to the beginning | |
4646 | * of a sequence. | |
5b9daa7e | 4647 | * @ingroup mutating_algorithms |
93c66bc6 BK |
4648 | * @param __first A forward iterator. |
4649 | * @param __last A forward iterator. | |
4650 | * @param __pred A predicate functor. | |
4651 | * @return An iterator @p middle such that @p __pred(i) is true for each | |
4652 | * iterator @p i in the range @p [__first,middle) and false for each @p i | |
4653 | * in the range @p [middle,__last). | |
c2ba9709 | 4654 | * |
93c66bc6 | 4655 | * @p __pred must not modify its operand. @p partition() does not preserve |
c2ba9709 JS |
4656 | * the relative ordering of elements in each group, use |
4657 | * @p stable_partition() if this is needed. | |
4658 | */ | |
4659 | template<typename _ForwardIterator, typename _Predicate> | |
4660 | inline _ForwardIterator | |
4661 | partition(_ForwardIterator __first, _ForwardIterator __last, | |
4662 | _Predicate __pred) | |
4663 | { | |
4664 | // concept requirements | |
4665 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< | |
4666 | _ForwardIterator>) | |
4667 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, | |
4668 | typename iterator_traits<_ForwardIterator>::value_type>) | |
4669 | __glibcxx_requires_valid_range(__first, __last); | |
da5a2efd | 4670 | |
c2ba9709 JS |
4671 | return std::__partition(__first, __last, __pred, |
4672 | std::__iterator_category(__first)); | |
da5a2efd PC |
4673 | } |
4674 | ||
c2ba9709 | 4675 | |
c2ba9709 JS |
4676 | /** |
4677 | * @brief Sort the smallest elements of a sequence. | |
5b9daa7e | 4678 | * @ingroup sorting_algorithms |
93c66bc6 BK |
4679 | * @param __first An iterator. |
4680 | * @param __middle Another iterator. | |
4681 | * @param __last Another iterator. | |
c2ba9709 JS |
4682 | * @return Nothing. |
4683 | * | |
93c66bc6 BK |
4684 | * Sorts the smallest @p (__middle-__first) elements in the range |
4685 | * @p [first,last) and moves them to the range @p [__first,__middle). The | |
4686 | * order of the remaining elements in the range @p [__middle,__last) is | |
c2ba9709 | 4687 | * undefined. |
7897a1c0 BK |
4688 | * After the sort if @e i and @e j are iterators in the range |
4689 | * @p [__first,__middle) such that i precedes j and @e k is an iterator in | |
4690 | * the range @p [__middle,__last) then *j<*i and *k<*i are both false. | |
c2ba9709 JS |
4691 | */ |
4692 | template<typename _RandomAccessIterator> | |
4693 | inline void | |
4694 | partial_sort(_RandomAccessIterator __first, | |
4695 | _RandomAccessIterator __middle, | |
4696 | _RandomAccessIterator __last) | |
da5a2efd | 4697 | { |
c2ba9709 JS |
4698 | // concept requirements |
4699 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< | |
4700 | _RandomAccessIterator>) | |
ea89b248 FD |
4701 | __glibcxx_function_requires(_LessThanComparableConcept< |
4702 | typename iterator_traits<_RandomAccessIterator>::value_type>) | |
c2ba9709 JS |
4703 | __glibcxx_requires_valid_range(__first, __middle); |
4704 | __glibcxx_requires_valid_range(__middle, __last); | |
630a286a | 4705 | __glibcxx_requires_irreflexive(__first, __last); |
da5a2efd | 4706 | |
ea89b248 FD |
4707 | std::__partial_sort(__first, __middle, __last, |
4708 | __gnu_cxx::__ops::__iter_less_iter()); | |
da5a2efd PC |
4709 | } |
4710 | ||
c2ba9709 JS |
4711 | /** |
4712 | * @brief Sort the smallest elements of a sequence using a predicate | |
4713 | * for comparison. | |
5b9daa7e | 4714 | * @ingroup sorting_algorithms |
93c66bc6 BK |
4715 | * @param __first An iterator. |
4716 | * @param __middle Another iterator. | |
4717 | * @param __last Another iterator. | |
4718 | * @param __comp A comparison functor. | |
c2ba9709 JS |
4719 | * @return Nothing. |
4720 | * | |
93c66bc6 BK |
4721 | * Sorts the smallest @p (__middle-__first) elements in the range |
4722 | * @p [__first,__last) and moves them to the range @p [__first,__middle). The | |
4723 | * order of the remaining elements in the range @p [__middle,__last) is | |
c2ba9709 | 4724 | * undefined. |
7897a1c0 BK |
4725 | * After the sort if @e i and @e j are iterators in the range |
4726 | * @p [__first,__middle) such that i precedes j and @e k is an iterator in | |
93c66bc6 | 4727 | * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i) |
c2ba9709 JS |
4728 | * are both false. |
4729 | */ | |
4730 | template<typename _RandomAccessIterator, typename _Compare> | |
4731 | inline void | |
4732 | partial_sort(_RandomAccessIterator __first, | |
4733 | _RandomAccessIterator __middle, | |
4734 | _RandomAccessIterator __last, | |
4735 | _Compare __comp) | |
da5a2efd | 4736 | { |
c2ba9709 JS |
4737 | // concept requirements |
4738 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< | |
4739 | _RandomAccessIterator>) | |
4740 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, | |
ea89b248 FD |
4741 | typename iterator_traits<_RandomAccessIterator>::value_type, |
4742 | typename iterator_traits<_RandomAccessIterator>::value_type>) | |
c2ba9709 JS |
4743 | __glibcxx_requires_valid_range(__first, __middle); |
4744 | __glibcxx_requires_valid_range(__middle, __last); | |
630a286a | 4745 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
da5a2efd | 4746 | |
ea89b248 FD |
4747 | std::__partial_sort(__first, __middle, __last, |
4748 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); | |
c2ba9709 | 4749 | } |
da5a2efd | 4750 | |
c2ba9709 JS |
4751 | /** |
4752 | * @brief Sort a sequence just enough to find a particular position. | |
5b9daa7e | 4753 | * @ingroup sorting_algorithms |
93c66bc6 BK |
4754 | * @param __first An iterator. |
4755 | * @param __nth Another iterator. | |
4756 | * @param __last Another iterator. | |
c2ba9709 JS |
4757 | * @return Nothing. |
4758 | * | |
93c66bc6 | 4759 | * Rearranges the elements in the range @p [__first,__last) so that @p *__nth |
c2ba9709 | 4760 | * is the same element that would have been in that position had the |
93c66bc6 | 4761 | * whole sequence been sorted. The elements either side of @p *__nth are |
7897a1c0 BK |
4762 | * not completely sorted, but for any iterator @e i in the range |
4763 | * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it | |
4764 | * holds that *j < *i is false. | |
c2ba9709 JS |
4765 | */ |
4766 | template<typename _RandomAccessIterator> | |
4767 | inline void | |
4768 | nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, | |
4769 | _RandomAccessIterator __last) | |
da5a2efd | 4770 | { |
c2ba9709 JS |
4771 | // concept requirements |
4772 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< | |
4773 | _RandomAccessIterator>) | |
ea89b248 FD |
4774 | __glibcxx_function_requires(_LessThanComparableConcept< |
4775 | typename iterator_traits<_RandomAccessIterator>::value_type>) | |
c2ba9709 JS |
4776 | __glibcxx_requires_valid_range(__first, __nth); |
4777 | __glibcxx_requires_valid_range(__nth, __last); | |
630a286a | 4778 | __glibcxx_requires_irreflexive(__first, __last); |
c868f37e | 4779 | |
c2ba9709 JS |
4780 | if (__first == __last || __nth == __last) |
4781 | return; | |
8c093661 | 4782 | |
c2ba9709 | 4783 | std::__introselect(__first, __nth, __last, |
ea89b248 FD |
4784 | std::__lg(__last - __first) * 2, |
4785 | __gnu_cxx::__ops::__iter_less_iter()); | |
02d92e3b | 4786 | } |
725dc051 | 4787 | |
c2ba9709 JS |
4788 | /** |
4789 | * @brief Sort a sequence just enough to find a particular position | |
4790 | * using a predicate for comparison. | |
5b9daa7e | 4791 | * @ingroup sorting_algorithms |
93c66bc6 BK |
4792 | * @param __first An iterator. |
4793 | * @param __nth Another iterator. | |
4794 | * @param __last Another iterator. | |
4795 | * @param __comp A comparison functor. | |
c2ba9709 JS |
4796 | * @return Nothing. |
4797 | * | |
93c66bc6 | 4798 | * Rearranges the elements in the range @p [__first,__last) so that @p *__nth |
c2ba9709 | 4799 | * is the same element that would have been in that position had the |
93c66bc6 | 4800 | * whole sequence been sorted. The elements either side of @p *__nth are |
7897a1c0 BK |
4801 | * not completely sorted, but for any iterator @e i in the range |
4802 | * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it | |
93c66bc6 | 4803 | * holds that @p __comp(*j,*i) is false. |
c2ba9709 JS |
4804 | */ |
4805 | template<typename _RandomAccessIterator, typename _Compare> | |
4806 | inline void | |
4807 | nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, | |
4808 | _RandomAccessIterator __last, _Compare __comp) | |
02d92e3b | 4809 | { |
c2ba9709 JS |
4810 | // concept requirements |
4811 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< | |
4812 | _RandomAccessIterator>) | |
4813 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, | |
ea89b248 FD |
4814 | typename iterator_traits<_RandomAccessIterator>::value_type, |
4815 | typename iterator_traits<_RandomAccessIterator>::value_type>) | |
c2ba9709 JS |
4816 | __glibcxx_requires_valid_range(__first, __nth); |
4817 | __glibcxx_requires_valid_range(__nth, __last); | |
630a286a | 4818 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
c868f37e | 4819 | |
c2ba9709 JS |
4820 | if (__first == __last || __nth == __last) |
4821 | return; | |
c868f37e | 4822 | |
c2ba9709 | 4823 | std::__introselect(__first, __nth, __last, |
ea89b248 FD |
4824 | std::__lg(__last - __first) * 2, |
4825 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); | |
725dc051 | 4826 | } |
02d92e3b | 4827 | |
c2ba9709 JS |
4828 | /** |
4829 | * @brief Sort the elements of a sequence. | |
5b9daa7e | 4830 | * @ingroup sorting_algorithms |
93c66bc6 BK |
4831 | * @param __first An iterator. |
4832 | * @param __last Another iterator. | |
c2ba9709 JS |
4833 | * @return Nothing. |
4834 | * | |
93c66bc6 | 4835 | * Sorts the elements in the range @p [__first,__last) in ascending order, |
7897a1c0 BK |
4836 | * such that for each iterator @e i in the range @p [__first,__last-1), |
4837 | * *(i+1)<*i is false. | |
c2ba9709 JS |
4838 | * |
4839 | * The relative ordering of equivalent elements is not preserved, use | |
4840 | * @p stable_sort() if this is needed. | |
4841 | */ | |
4842 | template<typename _RandomAccessIterator> | |
4843 | inline void | |
4844 | sort(_RandomAccessIterator __first, _RandomAccessIterator __last) | |
02d92e3b | 4845 | { |
c2ba9709 JS |
4846 | // concept requirements |
4847 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< | |
4848 | _RandomAccessIterator>) | |
ea89b248 FD |
4849 | __glibcxx_function_requires(_LessThanComparableConcept< |
4850 | typename iterator_traits<_RandomAccessIterator>::value_type>) | |
c2ba9709 | 4851 | __glibcxx_requires_valid_range(__first, __last); |
630a286a | 4852 | __glibcxx_requires_irreflexive(__first, __last); |
c2ba9709 | 4853 | |
ea89b248 | 4854 | std::__sort(__first, __last, __gnu_cxx::__ops::__iter_less_iter()); |
da5a2efd | 4855 | } |
c868f37e | 4856 | |
c2ba9709 JS |
4857 | /** |
4858 | * @brief Sort the elements of a sequence using a predicate for comparison. | |
5b9daa7e | 4859 | * @ingroup sorting_algorithms |
93c66bc6 BK |
4860 | * @param __first An iterator. |
4861 | * @param __last Another iterator. | |
4862 | * @param __comp A comparison functor. | |
c2ba9709 JS |
4863 | * @return Nothing. |
4864 | * | |
93c66bc6 | 4865 | * Sorts the elements in the range @p [__first,__last) in ascending order, |
7897a1c0 | 4866 | * such that @p __comp(*(i+1),*i) is false for every iterator @e i in the |
93c66bc6 | 4867 | * range @p [__first,__last-1). |
c2ba9709 JS |
4868 | * |
4869 | * The relative ordering of equivalent elements is not preserved, use | |
4870 | * @p stable_sort() if this is needed. | |
4871 | */ | |
4872 | template<typename _RandomAccessIterator, typename _Compare> | |
4873 | inline void | |
4874 | sort(_RandomAccessIterator __first, _RandomAccessIterator __last, | |
4875 | _Compare __comp) | |
da5a2efd | 4876 | { |
c2ba9709 JS |
4877 | // concept requirements |
4878 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< | |
4879 | _RandomAccessIterator>) | |
ea89b248 FD |
4880 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
4881 | typename iterator_traits<_RandomAccessIterator>::value_type, | |
4882 | typename iterator_traits<_RandomAccessIterator>::value_type>) | |
c2ba9709 | 4883 | __glibcxx_requires_valid_range(__first, __last); |
630a286a | 4884 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
c2ba9709 | 4885 | |
ea89b248 FD |
4886 | std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
4887 | } | |
4888 | ||
4889 | template<typename _InputIterator1, typename _InputIterator2, | |
4890 | typename _OutputIterator, typename _Compare> | |
4891 | _OutputIterator | |
4892 | __merge(_InputIterator1 __first1, _InputIterator1 __last1, | |
4893 | _InputIterator2 __first2, _InputIterator2 __last2, | |
4894 | _OutputIterator __result, _Compare __comp) | |
4895 | { | |
4896 | while (__first1 != __last1 && __first2 != __last2) | |
da5a2efd | 4897 | { |
ea89b248 FD |
4898 | if (__comp(__first2, __first1)) |
4899 | { | |
4900 | *__result = *__first2; | |
4901 | ++__first2; | |
4902 | } | |
4903 | else | |
4904 | { | |
4905 | *__result = *__first1; | |
4906 | ++__first1; | |
4907 | } | |
4908 | ++__result; | |
da5a2efd | 4909 | } |
ea89b248 FD |
4910 | return std::copy(__first2, __last2, |
4911 | std::copy(__first1, __last1, __result)); | |
da5a2efd | 4912 | } |
c868f37e | 4913 | |
da5a2efd | 4914 | /** |
c2ba9709 | 4915 | * @brief Merges two sorted ranges. |
5b9daa7e | 4916 | * @ingroup sorting_algorithms |
93c66bc6 BK |
4917 | * @param __first1 An iterator. |
4918 | * @param __first2 Another iterator. | |
4919 | * @param __last1 Another iterator. | |
4920 | * @param __last2 Another iterator. | |
4921 | * @param __result An iterator pointing to the end of the merged range. | |
2a60a9f6 | 4922 | * @return An iterator pointing to the first element <em>not less |
7897a1c0 | 4923 | * than</em> @e val. |
c2ba9709 | 4924 | * |
7897a1c0 BK |
4925 | * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into |
4926 | * the sorted range @p [__result, __result + (__last1-__first1) + | |
93c66bc6 BK |
4927 | * (__last2-__first2)). Both input ranges must be sorted, and the |
4928 | * output range must not overlap with either of the input ranges. | |
4929 | * The sort is @e stable, that is, for equivalent elements in the | |
4930 | * two ranges, elements from the first range will always come | |
4931 | * before elements from the second. | |
da5a2efd | 4932 | */ |
c2ba9709 JS |
4933 | template<typename _InputIterator1, typename _InputIterator2, |
4934 | typename _OutputIterator> | |
3bd2644c | 4935 | inline _OutputIterator |
c2ba9709 JS |
4936 | merge(_InputIterator1 __first1, _InputIterator1 __last1, |
4937 | _InputIterator2 __first2, _InputIterator2 __last2, | |
4938 | _OutputIterator __result) | |
da5a2efd | 4939 | { |
c2ba9709 JS |
4940 | // concept requirements |
4941 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) | |
4942 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) | |
4943 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
ea89b248 | 4944 | typename iterator_traits<_InputIterator1>::value_type>) |
c2ba9709 | 4945 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
ea89b248 FD |
4946 | typename iterator_traits<_InputIterator2>::value_type>) |
4947 | __glibcxx_function_requires(_LessThanOpConcept< | |
4948 | typename iterator_traits<_InputIterator2>::value_type, | |
4949 | typename iterator_traits<_InputIterator1>::value_type>) | |
a4c07f2d PC |
4950 | __glibcxx_requires_sorted_set(__first1, __last1, __first2); |
4951 | __glibcxx_requires_sorted_set(__first2, __last2, __first1); | |
630a286a FD |
4952 | __glibcxx_requires_irreflexive2(__first1, __last1); |
4953 | __glibcxx_requires_irreflexive2(__first2, __last2); | |
c2ba9709 | 4954 | |
ea89b248 FD |
4955 | return _GLIBCXX_STD_A::__merge(__first1, __last1, |
4956 | __first2, __last2, __result, | |
4957 | __gnu_cxx::__ops::__iter_less_iter()); | |
da5a2efd | 4958 | } |
c868f37e | 4959 | |
da5a2efd | 4960 | /** |
c2ba9709 | 4961 | * @brief Merges two sorted ranges. |
5b9daa7e | 4962 | * @ingroup sorting_algorithms |
93c66bc6 BK |
4963 | * @param __first1 An iterator. |
4964 | * @param __first2 Another iterator. | |
4965 | * @param __last1 Another iterator. | |
4966 | * @param __last2 Another iterator. | |
4967 | * @param __result An iterator pointing to the end of the merged range. | |
4968 | * @param __comp A functor to use for comparisons. | |
c2ba9709 | 4969 | * @return An iterator pointing to the first element "not less |
7897a1c0 | 4970 | * than" @e val. |
c2ba9709 | 4971 | * |
7897a1c0 BK |
4972 | * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into |
4973 | * the sorted range @p [__result, __result + (__last1-__first1) + | |
93c66bc6 BK |
4974 | * (__last2-__first2)). Both input ranges must be sorted, and the |
4975 | * output range must not overlap with either of the input ranges. | |
4976 | * The sort is @e stable, that is, for equivalent elements in the | |
4977 | * two ranges, elements from the first range will always come | |
4978 | * before elements from the second. | |
c2ba9709 JS |
4979 | * |
4980 | * The comparison function should have the same effects on ordering as | |
4981 | * the function used for the initial sort. | |
da5a2efd | 4982 | */ |
c2ba9709 JS |
4983 | template<typename _InputIterator1, typename _InputIterator2, |
4984 | typename _OutputIterator, typename _Compare> | |
3bd2644c | 4985 | inline _OutputIterator |
c2ba9709 JS |
4986 | merge(_InputIterator1 __first1, _InputIterator1 __last1, |
4987 | _InputIterator2 __first2, _InputIterator2 __last2, | |
4988 | _OutputIterator __result, _Compare __comp) | |
ea89b248 FD |
4989 | { |
4990 | // concept requirements | |
4991 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) | |
4992 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) | |
4993 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
4994 | typename iterator_traits<_InputIterator1>::value_type>) | |
4995 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
4996 | typename iterator_traits<_InputIterator2>::value_type>) | |
4997 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, | |
4998 | typename iterator_traits<_InputIterator2>::value_type, | |
4999 | typename iterator_traits<_InputIterator1>::value_type>) | |
5000 | __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); | |
5001 | __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); | |
630a286a FD |
5002 | __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp); |
5003 | __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp); | |
ea89b248 FD |
5004 | |
5005 | return _GLIBCXX_STD_A::__merge(__first1, __last1, | |
5006 | __first2, __last2, __result, | |
5007 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); | |
725dc051 | 5008 | } |
725dc051 | 5009 | |
ea89b248 FD |
5010 | template<typename _RandomAccessIterator, typename _Compare> |
5011 | inline void | |
5012 | __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, | |
5013 | _Compare __comp) | |
5014 | { | |
5015 | typedef typename iterator_traits<_RandomAccessIterator>::value_type | |
5016 | _ValueType; | |
5017 | typedef typename iterator_traits<_RandomAccessIterator>::difference_type | |
5018 | _DistanceType; | |
5019 | ||
5020 | typedef _Temporary_buffer<_RandomAccessIterator, _ValueType> _TmpBuf; | |
d31238cf | 5021 | _TmpBuf __buf(__first, std::distance(__first, __last)); |
ea89b248 FD |
5022 | |
5023 | if (__buf.begin() == 0) | |
5024 | std::__inplace_stable_sort(__first, __last, __comp); | |
5025 | else | |
5026 | std::__stable_sort_adaptive(__first, __last, __buf.begin(), | |
5027 | _DistanceType(__buf.size()), __comp); | |
5028 | } | |
c2ba9709 | 5029 | |
bf551f96 | 5030 | /** |
da5a2efd PC |
5031 | * @brief Sort the elements of a sequence, preserving the relative order |
5032 | * of equivalent elements. | |
5b9daa7e | 5033 | * @ingroup sorting_algorithms |
93c66bc6 BK |
5034 | * @param __first An iterator. |
5035 | * @param __last Another iterator. | |
da5a2efd | 5036 | * @return Nothing. |
bf551f96 | 5037 | * |
93c66bc6 | 5038 | * Sorts the elements in the range @p [__first,__last) in ascending order, |
7897a1c0 BK |
5039 | * such that for each iterator @p i in the range @p [__first,__last-1), |
5040 | * @p *(i+1)<*i is false. | |
da5a2efd PC |
5041 | * |
5042 | * The relative ordering of equivalent elements is preserved, so any two | |
93c66bc6 | 5043 | * elements @p x and @p y in the range @p [__first,__last) such that |
da5a2efd PC |
5044 | * @p x<y is false and @p y<x is false will have the same relative |
5045 | * ordering after calling @p stable_sort(). | |
bf551f96 | 5046 | */ |
da5a2efd PC |
5047 | template<typename _RandomAccessIterator> |
5048 | inline void | |
5049 | stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last) | |
02d92e3b | 5050 | { |
02d92e3b | 5051 | // concept requirements |
da5a2efd PC |
5052 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
5053 | _RandomAccessIterator>) | |
ea89b248 FD |
5054 | __glibcxx_function_requires(_LessThanComparableConcept< |
5055 | typename iterator_traits<_RandomAccessIterator>::value_type>) | |
da5a2efd | 5056 | __glibcxx_requires_valid_range(__first, __last); |
630a286a | 5057 | __glibcxx_requires_irreflexive(__first, __last); |
c0d88161 | 5058 | |
ea89b248 FD |
5059 | _GLIBCXX_STD_A::__stable_sort(__first, __last, |
5060 | __gnu_cxx::__ops::__iter_less_iter()); | |
02d92e3b SW |
5061 | } |
5062 | ||
bf551f96 | 5063 | /** |
da5a2efd PC |
5064 | * @brief Sort the elements of a sequence using a predicate for comparison, |
5065 | * preserving the relative order of equivalent elements. | |
5b9daa7e | 5066 | * @ingroup sorting_algorithms |
93c66bc6 BK |
5067 | * @param __first An iterator. |
5068 | * @param __last Another iterator. | |
5069 | * @param __comp A comparison functor. | |
da5a2efd | 5070 | * @return Nothing. |
bf551f96 | 5071 | * |
93c66bc6 | 5072 | * Sorts the elements in the range @p [__first,__last) in ascending order, |
7897a1c0 BK |
5073 | * such that for each iterator @p i in the range @p [__first,__last-1), |
5074 | * @p __comp(*(i+1),*i) is false. | |
bf551f96 | 5075 | * |
da5a2efd | 5076 | * The relative ordering of equivalent elements is preserved, so any two |
93c66bc6 BK |
5077 | * elements @p x and @p y in the range @p [__first,__last) such that |
5078 | * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same | |
da5a2efd | 5079 | * relative ordering after calling @p stable_sort(). |
bf551f96 | 5080 | */ |
da5a2efd PC |
5081 | template<typename _RandomAccessIterator, typename _Compare> |
5082 | inline void | |
5083 | stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, | |
5084 | _Compare __comp) | |
02d92e3b | 5085 | { |
02d92e3b | 5086 | // concept requirements |
c2ba9709 JS |
5087 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
5088 | _RandomAccessIterator>) | |
e75ea710 | 5089 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
ea89b248 FD |
5090 | typename iterator_traits<_RandomAccessIterator>::value_type, |
5091 | typename iterator_traits<_RandomAccessIterator>::value_type>) | |
c2ba9709 | 5092 | __glibcxx_requires_valid_range(__first, __last); |
630a286a | 5093 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
02d92e3b | 5094 | |
ea89b248 FD |
5095 | _GLIBCXX_STD_A::__stable_sort(__first, __last, |
5096 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); | |
02d92e3b SW |
5097 | } |
5098 | ||
ea89b248 FD |
5099 | template<typename _InputIterator1, typename _InputIterator2, |
5100 | typename _OutputIterator, | |
5101 | typename _Compare> | |
5102 | _OutputIterator | |
5103 | __set_union(_InputIterator1 __first1, _InputIterator1 __last1, | |
5104 | _InputIterator2 __first2, _InputIterator2 __last2, | |
5105 | _OutputIterator __result, _Compare __comp) | |
5106 | { | |
5107 | while (__first1 != __last1 && __first2 != __last2) | |
5108 | { | |
5109 | if (__comp(__first1, __first2)) | |
5110 | { | |
5111 | *__result = *__first1; | |
5112 | ++__first1; | |
5113 | } | |
5114 | else if (__comp(__first2, __first1)) | |
5115 | { | |
5116 | *__result = *__first2; | |
5117 | ++__first2; | |
5118 | } | |
5119 | else | |
5120 | { | |
5121 | *__result = *__first1; | |
5122 | ++__first1; | |
5123 | ++__first2; | |
5124 | } | |
5125 | ++__result; | |
5126 | } | |
5127 | return std::copy(__first2, __last2, | |
5128 | std::copy(__first1, __last1, __result)); | |
5129 | } | |
c2ba9709 | 5130 | |
119dbb1f JQ |
5131 | /** |
5132 | * @brief Return the union of two sorted ranges. | |
5b9daa7e | 5133 | * @ingroup set_algorithms |
93c66bc6 BK |
5134 | * @param __first1 Start of first range. |
5135 | * @param __last1 End of first range. | |
5136 | * @param __first2 Start of second range. | |
5137 | * @param __last2 End of second range. | |
26bddba3 | 5138 | * @param __result Start of output range. |
119dbb1f | 5139 | * @return End of the output range. |
aac2878e | 5140 | * @ingroup set_algorithms |
119dbb1f JQ |
5141 | * |
5142 | * This operation iterates over both ranges, copying elements present in | |
5143 | * each range in order to the output range. Iterators increment for each | |
5144 | * range. When the current element of one range is less than the other, | |
5145 | * that element is copied and the iterator advanced. If an element is | |
5146 | * contained in both ranges, the element from the first range is copied and | |
5147 | * both ranges advance. The output range may not overlap either input | |
5148 | * range. | |
5149 | */ | |
ffa67767 PC |
5150 | template<typename _InputIterator1, typename _InputIterator2, |
5151 | typename _OutputIterator> | |
3bd2644c | 5152 | inline _OutputIterator |
08addde6 PE |
5153 | set_union(_InputIterator1 __first1, _InputIterator1 __last1, |
5154 | _InputIterator2 __first2, _InputIterator2 __last2, | |
5155 | _OutputIterator __result) | |
02d92e3b SW |
5156 | { |
5157 | // concept requirements | |
3d7c150e BK |
5158 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
5159 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) | |
5160 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
ea89b248 | 5161 | typename iterator_traits<_InputIterator1>::value_type>) |
e75ea710 | 5162 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
ea89b248 FD |
5163 | typename iterator_traits<_InputIterator2>::value_type>) |
5164 | __glibcxx_function_requires(_LessThanOpConcept< | |
5165 | typename iterator_traits<_InputIterator1>::value_type, | |
5166 | typename iterator_traits<_InputIterator2>::value_type>) | |
5167 | __glibcxx_function_requires(_LessThanOpConcept< | |
5168 | typename iterator_traits<_InputIterator2>::value_type, | |
5169 | typename iterator_traits<_InputIterator1>::value_type>) | |
a4c07f2d PC |
5170 | __glibcxx_requires_sorted_set(__first1, __last1, __first2); |
5171 | __glibcxx_requires_sorted_set(__first2, __last2, __first1); | |
630a286a FD |
5172 | __glibcxx_requires_irreflexive2(__first1, __last1); |
5173 | __glibcxx_requires_irreflexive2(__first2, __last2); | |
02d92e3b | 5174 | |
ea89b248 FD |
5175 | return _GLIBCXX_STD_A::__set_union(__first1, __last1, |
5176 | __first2, __last2, __result, | |
5177 | __gnu_cxx::__ops::__iter_less_iter()); | |
02d92e3b SW |
5178 | } |
5179 | ||
119dbb1f JQ |
5180 | /** |
5181 | * @brief Return the union of two sorted ranges using a comparison functor. | |
5b9daa7e | 5182 | * @ingroup set_algorithms |
93c66bc6 BK |
5183 | * @param __first1 Start of first range. |
5184 | * @param __last1 End of first range. | |
5185 | * @param __first2 Start of second range. | |
5186 | * @param __last2 End of second range. | |
26bddba3 | 5187 | * @param __result Start of output range. |
93c66bc6 | 5188 | * @param __comp The comparison functor. |
119dbb1f | 5189 | * @return End of the output range. |
aac2878e | 5190 | * @ingroup set_algorithms |
119dbb1f JQ |
5191 | * |
5192 | * This operation iterates over both ranges, copying elements present in | |
5193 | * each range in order to the output range. Iterators increment for each | |
5194 | * range. When the current element of one range is less than the other | |
7897a1c0 BK |
5195 | * according to @p __comp, that element is copied and the iterator advanced. |
5196 | * If an equivalent element according to @p __comp is contained in both | |
119dbb1f JQ |
5197 | * ranges, the element from the first range is copied and both ranges |
5198 | * advance. The output range may not overlap either input range. | |
5199 | */ | |
ffa67767 PC |
5200 | template<typename _InputIterator1, typename _InputIterator2, |
5201 | typename _OutputIterator, typename _Compare> | |
3bd2644c | 5202 | inline _OutputIterator |
08addde6 PE |
5203 | set_union(_InputIterator1 __first1, _InputIterator1 __last1, |
5204 | _InputIterator2 __first2, _InputIterator2 __last2, | |
5205 | _OutputIterator __result, _Compare __comp) | |
02d92e3b SW |
5206 | { |
5207 | // concept requirements | |
3d7c150e BK |
5208 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
5209 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) | |
3d7c150e | 5210 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
ea89b248 | 5211 | typename iterator_traits<_InputIterator1>::value_type>) |
e75ea710 | 5212 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
ea89b248 | 5213 | typename iterator_traits<_InputIterator2>::value_type>) |
e75ea710 | 5214 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
ea89b248 FD |
5215 | typename iterator_traits<_InputIterator1>::value_type, |
5216 | typename iterator_traits<_InputIterator2>::value_type>) | |
3d7c150e | 5217 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
ea89b248 FD |
5218 | typename iterator_traits<_InputIterator2>::value_type, |
5219 | typename iterator_traits<_InputIterator1>::value_type>) | |
a4c07f2d PC |
5220 | __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); |
5221 | __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); | |
630a286a FD |
5222 | __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp); |
5223 | __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp); | |
02d92e3b | 5224 | |
ea89b248 FD |
5225 | return _GLIBCXX_STD_A::__set_union(__first1, __last1, |
5226 | __first2, __last2, __result, | |
5227 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); | |
5228 | } | |
5229 | ||
5230 | template<typename _InputIterator1, typename _InputIterator2, | |
5231 | typename _OutputIterator, | |
5232 | typename _Compare> | |
5233 | _OutputIterator | |
5234 | __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, | |
5235 | _InputIterator2 __first2, _InputIterator2 __last2, | |
5236 | _OutputIterator __result, _Compare __comp) | |
5237 | { | |
ffa67767 | 5238 | while (__first1 != __last1 && __first2 != __last2) |
ea89b248 FD |
5239 | if (__comp(__first1, __first2)) |
5240 | ++__first1; | |
5241 | else if (__comp(__first2, __first1)) | |
5242 | ++__first2; | |
5243 | else | |
5244 | { | |
5245 | *__result = *__first1; | |
5246 | ++__first1; | |
5247 | ++__first2; | |
5248 | ++__result; | |
5249 | } | |
5250 | return __result; | |
02d92e3b SW |
5251 | } |
5252 | ||
119dbb1f JQ |
5253 | /** |
5254 | * @brief Return the intersection of two sorted ranges. | |
5b9daa7e | 5255 | * @ingroup set_algorithms |
93c66bc6 BK |
5256 | * @param __first1 Start of first range. |
5257 | * @param __last1 End of first range. | |
5258 | * @param __first2 Start of second range. | |
5259 | * @param __last2 End of second range. | |
26bddba3 | 5260 | * @param __result Start of output range. |
119dbb1f | 5261 | * @return End of the output range. |
aac2878e | 5262 | * @ingroup set_algorithms |
119dbb1f JQ |
5263 | * |
5264 | * This operation iterates over both ranges, copying elements present in | |
5265 | * both ranges in order to the output range. Iterators increment for each | |
5266 | * range. When the current element of one range is less than the other, | |
5267 | * that iterator advances. If an element is contained in both ranges, the | |
5268 | * element from the first range is copied and both ranges advance. The | |
5269 | * output range may not overlap either input range. | |
5270 | */ | |
ffa67767 PC |
5271 | template<typename _InputIterator1, typename _InputIterator2, |
5272 | typename _OutputIterator> | |
3bd2644c | 5273 | inline _OutputIterator |
08addde6 PE |
5274 | set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, |
5275 | _InputIterator2 __first2, _InputIterator2 __last2, | |
5276 | _OutputIterator __result) | |
02d92e3b SW |
5277 | { |
5278 | // concept requirements | |
3d7c150e BK |
5279 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
5280 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) | |
5281 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
ea89b248 FD |
5282 | typename iterator_traits<_InputIterator1>::value_type>) |
5283 | __glibcxx_function_requires(_LessThanOpConcept< | |
5284 | typename iterator_traits<_InputIterator1>::value_type, | |
5285 | typename iterator_traits<_InputIterator2>::value_type>) | |
5286 | __glibcxx_function_requires(_LessThanOpConcept< | |
5287 | typename iterator_traits<_InputIterator2>::value_type, | |
5288 | typename iterator_traits<_InputIterator1>::value_type>) | |
a4c07f2d PC |
5289 | __glibcxx_requires_sorted_set(__first1, __last1, __first2); |
5290 | __glibcxx_requires_sorted_set(__first2, __last2, __first1); | |
630a286a FD |
5291 | __glibcxx_requires_irreflexive2(__first1, __last1); |
5292 | __glibcxx_requires_irreflexive2(__first2, __last2); | |
02d92e3b | 5293 | |
ea89b248 FD |
5294 | return _GLIBCXX_STD_A::__set_intersection(__first1, __last1, |
5295 | __first2, __last2, __result, | |
5296 | __gnu_cxx::__ops::__iter_less_iter()); | |
02d92e3b SW |
5297 | } |
5298 | ||
119dbb1f JQ |
5299 | /** |
5300 | * @brief Return the intersection of two sorted ranges using comparison | |
ed6814f7 | 5301 | * functor. |
5b9daa7e | 5302 | * @ingroup set_algorithms |
93c66bc6 BK |
5303 | * @param __first1 Start of first range. |
5304 | * @param __last1 End of first range. | |
5305 | * @param __first2 Start of second range. | |
5306 | * @param __last2 End of second range. | |
26bddba3 | 5307 | * @param __result Start of output range. |
93c66bc6 | 5308 | * @param __comp The comparison functor. |
119dbb1f | 5309 | * @return End of the output range. |
aac2878e | 5310 | * @ingroup set_algorithms |
119dbb1f JQ |
5311 | * |
5312 | * This operation iterates over both ranges, copying elements present in | |
5313 | * both ranges in order to the output range. Iterators increment for each | |
5314 | * range. When the current element of one range is less than the other | |
7897a1c0 BK |
5315 | * according to @p __comp, that iterator advances. If an element is |
5316 | * contained in both ranges according to @p __comp, the element from the | |
119dbb1f JQ |
5317 | * first range is copied and both ranges advance. The output range may not |
5318 | * overlap either input range. | |
5319 | */ | |
ffa67767 PC |
5320 | template<typename _InputIterator1, typename _InputIterator2, |
5321 | typename _OutputIterator, typename _Compare> | |
3bd2644c | 5322 | inline _OutputIterator |
08addde6 PE |
5323 | set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, |
5324 | _InputIterator2 __first2, _InputIterator2 __last2, | |
5325 | _OutputIterator __result, _Compare __comp) | |
02d92e3b SW |
5326 | { |
5327 | // concept requirements | |
3d7c150e BK |
5328 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
5329 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) | |
3d7c150e | 5330 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
ea89b248 | 5331 | typename iterator_traits<_InputIterator1>::value_type>) |
3d7c150e | 5332 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
ea89b248 FD |
5333 | typename iterator_traits<_InputIterator1>::value_type, |
5334 | typename iterator_traits<_InputIterator2>::value_type>) | |
e75ea710 | 5335 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
ea89b248 FD |
5336 | typename iterator_traits<_InputIterator2>::value_type, |
5337 | typename iterator_traits<_InputIterator1>::value_type>) | |
a4c07f2d PC |
5338 | __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); |
5339 | __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); | |
630a286a FD |
5340 | __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp); |
5341 | __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp); | |
02d92e3b | 5342 | |
ea89b248 FD |
5343 | return _GLIBCXX_STD_A::__set_intersection(__first1, __last1, |
5344 | __first2, __last2, __result, | |
5345 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); | |
5346 | } | |
5347 | ||
5348 | template<typename _InputIterator1, typename _InputIterator2, | |
5349 | typename _OutputIterator, | |
5350 | typename _Compare> | |
5351 | _OutputIterator | |
5352 | __set_difference(_InputIterator1 __first1, _InputIterator1 __last1, | |
5353 | _InputIterator2 __first2, _InputIterator2 __last2, | |
5354 | _OutputIterator __result, _Compare __comp) | |
5355 | { | |
02d92e3b | 5356 | while (__first1 != __last1 && __first2 != __last2) |
ea89b248 FD |
5357 | if (__comp(__first1, __first2)) |
5358 | { | |
5359 | *__result = *__first1; | |
5360 | ++__first1; | |
5361 | ++__result; | |
5362 | } | |
5363 | else if (__comp(__first2, __first1)) | |
02d92e3b | 5364 | ++__first2; |
ffa67767 PC |
5365 | else |
5366 | { | |
ffa67767 PC |
5367 | ++__first1; |
5368 | ++__first2; | |
ffa67767 | 5369 | } |
ea89b248 | 5370 | return std::copy(__first1, __last1, __result); |
02d92e3b SW |
5371 | } |
5372 | ||
119dbb1f JQ |
5373 | /** |
5374 | * @brief Return the difference of two sorted ranges. | |
5b9daa7e | 5375 | * @ingroup set_algorithms |
93c66bc6 BK |
5376 | * @param __first1 Start of first range. |
5377 | * @param __last1 End of first range. | |
5378 | * @param __first2 Start of second range. | |
5379 | * @param __last2 End of second range. | |
26bddba3 | 5380 | * @param __result Start of output range. |
119dbb1f | 5381 | * @return End of the output range. |
aac2878e | 5382 | * @ingroup set_algorithms |
119dbb1f JQ |
5383 | * |
5384 | * This operation iterates over both ranges, copying elements present in | |
5385 | * the first range but not the second in order to the output range. | |
5386 | * Iterators increment for each range. When the current element of the | |
5387 | * first range is less than the second, that element is copied and the | |
5388 | * iterator advances. If the current element of the second range is less, | |
5389 | * the iterator advances, but no element is copied. If an element is | |
5390 | * contained in both ranges, no elements are copied and both ranges | |
5391 | * advance. The output range may not overlap either input range. | |
5392 | */ | |
ffa67767 PC |
5393 | template<typename _InputIterator1, typename _InputIterator2, |
5394 | typename _OutputIterator> | |
3bd2644c | 5395 | inline _OutputIterator |
08addde6 PE |
5396 | set_difference(_InputIterator1 __first1, _InputIterator1 __last1, |
5397 | _InputIterator2 __first2, _InputIterator2 __last2, | |
5398 | _OutputIterator __result) | |
02d92e3b SW |
5399 | { |
5400 | // concept requirements | |
3d7c150e BK |
5401 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
5402 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) | |
5403 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
ea89b248 FD |
5404 | typename iterator_traits<_InputIterator1>::value_type>) |
5405 | __glibcxx_function_requires(_LessThanOpConcept< | |
5406 | typename iterator_traits<_InputIterator1>::value_type, | |
5407 | typename iterator_traits<_InputIterator2>::value_type>) | |
5408 | __glibcxx_function_requires(_LessThanOpConcept< | |
5409 | typename iterator_traits<_InputIterator2>::value_type, | |
5410 | typename iterator_traits<_InputIterator1>::value_type>) | |
a4c07f2d PC |
5411 | __glibcxx_requires_sorted_set(__first1, __last1, __first2); |
5412 | __glibcxx_requires_sorted_set(__first2, __last2, __first1); | |
630a286a FD |
5413 | __glibcxx_requires_irreflexive2(__first1, __last1); |
5414 | __glibcxx_requires_irreflexive2(__first2, __last2); | |
02d92e3b | 5415 | |
ea89b248 FD |
5416 | return _GLIBCXX_STD_A::__set_difference(__first1, __last1, |
5417 | __first2, __last2, __result, | |
5418 | __gnu_cxx::__ops::__iter_less_iter()); | |
02d92e3b SW |
5419 | } |
5420 | ||
119dbb1f JQ |
5421 | /** |
5422 | * @brief Return the difference of two sorted ranges using comparison | |
5423 | * functor. | |
5b9daa7e | 5424 | * @ingroup set_algorithms |
93c66bc6 BK |
5425 | * @param __first1 Start of first range. |
5426 | * @param __last1 End of first range. | |
5427 | * @param __first2 Start of second range. | |
5428 | * @param __last2 End of second range. | |
26bddba3 | 5429 | * @param __result Start of output range. |
93c66bc6 | 5430 | * @param __comp The comparison functor. |
119dbb1f | 5431 | * @return End of the output range. |
aac2878e | 5432 | * @ingroup set_algorithms |
119dbb1f JQ |
5433 | * |
5434 | * This operation iterates over both ranges, copying elements present in | |
5435 | * the first range but not the second in order to the output range. | |
5436 | * Iterators increment for each range. When the current element of the | |
7897a1c0 | 5437 | * first range is less than the second according to @p __comp, that element |
119dbb1f JQ |
5438 | * is copied and the iterator advances. If the current element of the |
5439 | * second range is less, no element is copied and the iterator advances. | |
7897a1c0 | 5440 | * If an element is contained in both ranges according to @p __comp, no |
119dbb1f JQ |
5441 | * elements are copied and both ranges advance. The output range may not |
5442 | * overlap either input range. | |
5443 | */ | |
ffa67767 PC |
5444 | template<typename _InputIterator1, typename _InputIterator2, |
5445 | typename _OutputIterator, typename _Compare> | |
3bd2644c | 5446 | inline _OutputIterator |
08addde6 PE |
5447 | set_difference(_InputIterator1 __first1, _InputIterator1 __last1, |
5448 | _InputIterator2 __first2, _InputIterator2 __last2, | |
5449 | _OutputIterator __result, _Compare __comp) | |
02d92e3b SW |
5450 | { |
5451 | // concept requirements | |
3d7c150e BK |
5452 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
5453 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) | |
3d7c150e | 5454 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
ea89b248 | 5455 | typename iterator_traits<_InputIterator1>::value_type>) |
3d7c150e | 5456 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
ea89b248 FD |
5457 | typename iterator_traits<_InputIterator1>::value_type, |
5458 | typename iterator_traits<_InputIterator2>::value_type>) | |
e75ea710 | 5459 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
ea89b248 FD |
5460 | typename iterator_traits<_InputIterator2>::value_type, |
5461 | typename iterator_traits<_InputIterator1>::value_type>) | |
a4c07f2d PC |
5462 | __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); |
5463 | __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); | |
630a286a FD |
5464 | __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp); |
5465 | __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp); | |
02d92e3b | 5466 | |
ea89b248 FD |
5467 | return _GLIBCXX_STD_A::__set_difference(__first1, __last1, |
5468 | __first2, __last2, __result, | |
5469 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); | |
5470 | } | |
5471 | ||
5472 | template<typename _InputIterator1, typename _InputIterator2, | |
5473 | typename _OutputIterator, | |
5474 | typename _Compare> | |
5475 | _OutputIterator | |
5476 | __set_symmetric_difference(_InputIterator1 __first1, | |
5477 | _InputIterator1 __last1, | |
5478 | _InputIterator2 __first2, | |
5479 | _InputIterator2 __last2, | |
5480 | _OutputIterator __result, | |
5481 | _Compare __comp) | |
5482 | { | |
02d92e3b | 5483 | while (__first1 != __last1 && __first2 != __last2) |
ea89b248 | 5484 | if (__comp(__first1, __first2)) |
ffa67767 PC |
5485 | { |
5486 | *__result = *__first1; | |
5487 | ++__first1; | |
5488 | ++__result; | |
5489 | } | |
ea89b248 FD |
5490 | else if (__comp(__first2, __first1)) |
5491 | { | |
5492 | *__result = *__first2; | |
5493 | ++__first2; | |
5494 | ++__result; | |
5495 | } | |
ffa67767 PC |
5496 | else |
5497 | { | |
5498 | ++__first1; | |
5499 | ++__first2; | |
5500 | } | |
ea89b248 FD |
5501 | return std::copy(__first2, __last2, |
5502 | std::copy(__first1, __last1, __result)); | |
02d92e3b SW |
5503 | } |
5504 | ||
119dbb1f JQ |
5505 | /** |
5506 | * @brief Return the symmetric difference of two sorted ranges. | |
5b9daa7e | 5507 | * @ingroup set_algorithms |
93c66bc6 BK |
5508 | * @param __first1 Start of first range. |
5509 | * @param __last1 End of first range. | |
5510 | * @param __first2 Start of second range. | |
5511 | * @param __last2 End of second range. | |
26bddba3 | 5512 | * @param __result Start of output range. |
119dbb1f | 5513 | * @return End of the output range. |
aac2878e | 5514 | * @ingroup set_algorithms |
119dbb1f JQ |
5515 | * |
5516 | * This operation iterates over both ranges, copying elements present in | |
5517 | * one range but not the other in order to the output range. Iterators | |
5518 | * increment for each range. When the current element of one range is less | |
5519 | * than the other, that element is copied and the iterator advances. If an | |
5520 | * element is contained in both ranges, no elements are copied and both | |
5521 | * ranges advance. The output range may not overlap either input range. | |
5522 | */ | |
ffa67767 PC |
5523 | template<typename _InputIterator1, typename _InputIterator2, |
5524 | typename _OutputIterator> | |
3bd2644c | 5525 | inline _OutputIterator |
08addde6 PE |
5526 | set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1, |
5527 | _InputIterator2 __first2, _InputIterator2 __last2, | |
5528 | _OutputIterator __result) | |
02d92e3b SW |
5529 | { |
5530 | // concept requirements | |
3d7c150e BK |
5531 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
5532 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) | |
5533 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
ea89b248 | 5534 | typename iterator_traits<_InputIterator1>::value_type>) |
e75ea710 | 5535 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
ea89b248 FD |
5536 | typename iterator_traits<_InputIterator2>::value_type>) |
5537 | __glibcxx_function_requires(_LessThanOpConcept< | |
5538 | typename iterator_traits<_InputIterator1>::value_type, | |
5539 | typename iterator_traits<_InputIterator2>::value_type>) | |
5540 | __glibcxx_function_requires(_LessThanOpConcept< | |
5541 | typename iterator_traits<_InputIterator2>::value_type, | |
5542 | typename iterator_traits<_InputIterator1>::value_type>) | |
a4c07f2d PC |
5543 | __glibcxx_requires_sorted_set(__first1, __last1, __first2); |
5544 | __glibcxx_requires_sorted_set(__first2, __last2, __first1); | |
630a286a FD |
5545 | __glibcxx_requires_irreflexive2(__first1, __last1); |
5546 | __glibcxx_requires_irreflexive2(__first2, __last2); | |
02d92e3b | 5547 | |
ea89b248 FD |
5548 | return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1, |
5549 | __first2, __last2, __result, | |
5550 | __gnu_cxx::__ops::__iter_less_iter()); | |
02d92e3b SW |
5551 | } |
5552 | ||
119dbb1f JQ |
5553 | /** |
5554 | * @brief Return the symmetric difference of two sorted ranges using | |
5555 | * comparison functor. | |
5b9daa7e | 5556 | * @ingroup set_algorithms |
93c66bc6 BK |
5557 | * @param __first1 Start of first range. |
5558 | * @param __last1 End of first range. | |
5559 | * @param __first2 Start of second range. | |
5560 | * @param __last2 End of second range. | |
26bddba3 | 5561 | * @param __result Start of output range. |
93c66bc6 | 5562 | * @param __comp The comparison functor. |
119dbb1f | 5563 | * @return End of the output range. |
aac2878e | 5564 | * @ingroup set_algorithms |
119dbb1f JQ |
5565 | * |
5566 | * This operation iterates over both ranges, copying elements present in | |
5567 | * one range but not the other in order to the output range. Iterators | |
5568 | * increment for each range. When the current element of one range is less | |
7897a1c0 | 5569 | * than the other according to @p comp, that element is copied and the |
119dbb1f | 5570 | * iterator advances. If an element is contained in both ranges according |
7897a1c0 | 5571 | * to @p __comp, no elements are copied and both ranges advance. The output |
119dbb1f JQ |
5572 | * range may not overlap either input range. |
5573 | */ | |
ffa67767 PC |
5574 | template<typename _InputIterator1, typename _InputIterator2, |
5575 | typename _OutputIterator, typename _Compare> | |
3bd2644c | 5576 | inline _OutputIterator |
08addde6 PE |
5577 | set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1, |
5578 | _InputIterator2 __first2, _InputIterator2 __last2, | |
5579 | _OutputIterator __result, | |
02d92e3b SW |
5580 | _Compare __comp) |
5581 | { | |
5582 | // concept requirements | |
3d7c150e BK |
5583 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
5584 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) | |
3d7c150e | 5585 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
ea89b248 | 5586 | typename iterator_traits<_InputIterator1>::value_type>) |
e75ea710 | 5587 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
ea89b248 | 5588 | typename iterator_traits<_InputIterator2>::value_type>) |
e75ea710 | 5589 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
ea89b248 FD |
5590 | typename iterator_traits<_InputIterator1>::value_type, |
5591 | typename iterator_traits<_InputIterator2>::value_type>) | |
3d7c150e | 5592 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
ea89b248 FD |
5593 | typename iterator_traits<_InputIterator2>::value_type, |
5594 | typename iterator_traits<_InputIterator1>::value_type>) | |
a4c07f2d PC |
5595 | __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); |
5596 | __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); | |
630a286a FD |
5597 | __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp); |
5598 | __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp); | |
02d92e3b | 5599 | |
ea89b248 FD |
5600 | return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1, |
5601 | __first2, __last2, __result, | |
5602 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); | |
02d92e3b SW |
5603 | } |
5604 | ||
ea89b248 | 5605 | template<typename _ForwardIterator, typename _Compare> |
8dff34fe | 5606 | _GLIBCXX14_CONSTEXPR |
ea89b248 FD |
5607 | _ForwardIterator |
5608 | __min_element(_ForwardIterator __first, _ForwardIterator __last, | |
5609 | _Compare __comp) | |
5610 | { | |
5611 | if (__first == __last) | |
5612 | return __first; | |
5613 | _ForwardIterator __result = __first; | |
5614 | while (++__first != __last) | |
5615 | if (__comp(__first, __result)) | |
5616 | __result = __first; | |
5617 | return __result; | |
5618 | } | |
02d92e3b | 5619 | |
119dbb1f JQ |
5620 | /** |
5621 | * @brief Return the minimum element in a range. | |
5b9daa7e | 5622 | * @ingroup sorting_algorithms |
93c66bc6 BK |
5623 | * @param __first Start of range. |
5624 | * @param __last End of range. | |
119dbb1f JQ |
5625 | * @return Iterator referencing the first instance of the smallest value. |
5626 | */ | |
08addde6 | 5627 | template<typename _ForwardIterator> |
8dff34fe | 5628 | _GLIBCXX14_CONSTEXPR |
08addde6 | 5629 | _ForwardIterator |
3bd2644c | 5630 | inline min_element(_ForwardIterator __first, _ForwardIterator __last) |
02d92e3b SW |
5631 | { |
5632 | // concept requirements | |
3d7c150e BK |
5633 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
5634 | __glibcxx_function_requires(_LessThanComparableConcept< | |
08addde6 | 5635 | typename iterator_traits<_ForwardIterator>::value_type>) |
285b36d6 | 5636 | __glibcxx_requires_valid_range(__first, __last); |
630a286a | 5637 | __glibcxx_requires_irreflexive(__first, __last); |
02d92e3b | 5638 | |
ea89b248 FD |
5639 | return _GLIBCXX_STD_A::__min_element(__first, __last, |
5640 | __gnu_cxx::__ops::__iter_less_iter()); | |
02d92e3b SW |
5641 | } |
5642 | ||
119dbb1f JQ |
5643 | /** |
5644 | * @brief Return the minimum element in a range using comparison functor. | |
5b9daa7e | 5645 | * @ingroup sorting_algorithms |
93c66bc6 BK |
5646 | * @param __first Start of range. |
5647 | * @param __last End of range. | |
5648 | * @param __comp Comparison functor. | |
119dbb1f | 5649 | * @return Iterator referencing the first instance of the smallest value |
7897a1c0 | 5650 | * according to __comp. |
119dbb1f | 5651 | */ |
08addde6 | 5652 | template<typename _ForwardIterator, typename _Compare> |
8dff34fe | 5653 | _GLIBCXX14_CONSTEXPR |
3bd2644c | 5654 | inline _ForwardIterator |
08addde6 | 5655 | min_element(_ForwardIterator __first, _ForwardIterator __last, |
02d92e3b SW |
5656 | _Compare __comp) |
5657 | { | |
5658 | // concept requirements | |
3d7c150e BK |
5659 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
5660 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, | |
08addde6 PE |
5661 | typename iterator_traits<_ForwardIterator>::value_type, |
5662 | typename iterator_traits<_ForwardIterator>::value_type>) | |
285b36d6 | 5663 | __glibcxx_requires_valid_range(__first, __last); |
630a286a | 5664 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
02d92e3b | 5665 | |
ea89b248 FD |
5666 | return _GLIBCXX_STD_A::__min_element(__first, __last, |
5667 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); | |
5668 | } | |
5669 | ||
5670 | template<typename _ForwardIterator, typename _Compare> | |
8dff34fe | 5671 | _GLIBCXX14_CONSTEXPR |
ea89b248 FD |
5672 | _ForwardIterator |
5673 | __max_element(_ForwardIterator __first, _ForwardIterator __last, | |
5674 | _Compare __comp) | |
5675 | { | |
5676 | if (__first == __last) return __first; | |
08addde6 | 5677 | _ForwardIterator __result = __first; |
c0d88161 | 5678 | while (++__first != __last) |
ea89b248 | 5679 | if (__comp(__result, __first)) |
02d92e3b SW |
5680 | __result = __first; |
5681 | return __result; | |
5682 | } | |
5683 | ||
119dbb1f | 5684 | /** |
da5a2efd | 5685 | * @brief Return the maximum element in a range. |
5b9daa7e | 5686 | * @ingroup sorting_algorithms |
93c66bc6 BK |
5687 | * @param __first Start of range. |
5688 | * @param __last End of range. | |
da5a2efd | 5689 | * @return Iterator referencing the first instance of the largest value. |
119dbb1f | 5690 | */ |
da5a2efd | 5691 | template<typename _ForwardIterator> |
8dff34fe | 5692 | _GLIBCXX14_CONSTEXPR |
3bd2644c | 5693 | inline _ForwardIterator |
da5a2efd | 5694 | max_element(_ForwardIterator __first, _ForwardIterator __last) |
02d92e3b SW |
5695 | { |
5696 | // concept requirements | |
da5a2efd | 5697 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
3d7c150e | 5698 | __glibcxx_function_requires(_LessThanComparableConcept< |
da5a2efd | 5699 | typename iterator_traits<_ForwardIterator>::value_type>) |
285b36d6 | 5700 | __glibcxx_requires_valid_range(__first, __last); |
630a286a | 5701 | __glibcxx_requires_irreflexive(__first, __last); |
02d92e3b | 5702 | |
ea89b248 FD |
5703 | return _GLIBCXX_STD_A::__max_element(__first, __last, |
5704 | __gnu_cxx::__ops::__iter_less_iter()); | |
725dc051 | 5705 | } |
725dc051 | 5706 | |
119dbb1f | 5707 | /** |
da5a2efd | 5708 | * @brief Return the maximum element in a range using comparison functor. |
5b9daa7e | 5709 | * @ingroup sorting_algorithms |
93c66bc6 BK |
5710 | * @param __first Start of range. |
5711 | * @param __last End of range. | |
5712 | * @param __comp Comparison functor. | |
da5a2efd | 5713 | * @return Iterator referencing the first instance of the largest value |
93c66bc6 | 5714 | * according to __comp. |
119dbb1f | 5715 | */ |
da5a2efd | 5716 | template<typename _ForwardIterator, typename _Compare> |
8dff34fe | 5717 | _GLIBCXX14_CONSTEXPR |
3bd2644c | 5718 | inline _ForwardIterator |
da5a2efd PC |
5719 | max_element(_ForwardIterator __first, _ForwardIterator __last, |
5720 | _Compare __comp) | |
02d92e3b SW |
5721 | { |
5722 | // concept requirements | |
da5a2efd PC |
5723 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
5724 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, | |
5725 | typename iterator_traits<_ForwardIterator>::value_type, | |
5726 | typename iterator_traits<_ForwardIterator>::value_type>) | |
285b36d6 | 5727 | __glibcxx_requires_valid_range(__first, __last); |
630a286a | 5728 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
02d92e3b | 5729 | |
ea89b248 FD |
5730 | return _GLIBCXX_STD_A::__max_element(__first, __last, |
5731 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); | |
725dc051 | 5732 | } |
02d92e3b | 5733 | |
e7722f11 JW |
5734 | #if __cplusplus >= 201402L |
5735 | /// Reservoir sampling algorithm. | |
5736 | template<typename _InputIterator, typename _RandomAccessIterator, | |
5737 | typename _Size, typename _UniformRandomBitGenerator> | |
5738 | _RandomAccessIterator | |
5739 | __sample(_InputIterator __first, _InputIterator __last, input_iterator_tag, | |
5740 | _RandomAccessIterator __out, random_access_iterator_tag, | |
5741 | _Size __n, _UniformRandomBitGenerator&& __g) | |
5742 | { | |
5743 | using __distrib_type = uniform_int_distribution<_Size>; | |
5744 | using __param_type = typename __distrib_type::param_type; | |
5745 | __distrib_type __d{}; | |
5746 | _Size __sample_sz = 0; | |
5747 | while (__first != __last && __sample_sz != __n) | |
5748 | { | |
5749 | __out[__sample_sz++] = *__first; | |
5750 | ++__first; | |
5751 | } | |
5752 | for (auto __pop_sz = __sample_sz; __first != __last; | |
5753 | ++__first, (void) ++__pop_sz) | |
5754 | { | |
5755 | const auto __k = __d(__g, __param_type{0, __pop_sz}); | |
5756 | if (__k < __n) | |
5757 | __out[__k] = *__first; | |
5758 | } | |
5759 | return __out + __sample_sz; | |
5760 | } | |
5761 | ||
5762 | /// Selection sampling algorithm. | |
5763 | template<typename _ForwardIterator, typename _OutputIterator, typename _Cat, | |
5764 | typename _Size, typename _UniformRandomBitGenerator> | |
5765 | _OutputIterator | |
5766 | __sample(_ForwardIterator __first, _ForwardIterator __last, | |
5767 | forward_iterator_tag, | |
5768 | _OutputIterator __out, _Cat, | |
5769 | _Size __n, _UniformRandomBitGenerator&& __g) | |
5770 | { | |
5771 | using __distrib_type = uniform_int_distribution<_Size>; | |
5772 | using __param_type = typename __distrib_type::param_type; | |
9d8ff2f6 EW |
5773 | using _USize = make_unsigned_t<_Size>; |
5774 | using _Gen = remove_reference_t<_UniformRandomBitGenerator>; | |
5775 | using __uc_type = common_type_t<typename _Gen::result_type, _USize>; | |
5776 | ||
e7722f11 JW |
5777 | __distrib_type __d{}; |
5778 | _Size __unsampled_sz = std::distance(__first, __last); | |
9d8ff2f6 EW |
5779 | __n = std::min(__n, __unsampled_sz); |
5780 | ||
5781 | // If possible, we use __gen_two_uniform_ints to efficiently produce | |
5782 | // two random numbers using a single distribution invocation: | |
5783 | ||
5784 | const __uc_type __urngrange = __g.max() - __g.min(); | |
5785 | if (__urngrange / __uc_type(__unsampled_sz) >= __uc_type(__unsampled_sz)) | |
5786 | // I.e. (__urngrange >= __unsampled_sz * __unsampled_sz) but without | |
5787 | // wrapping issues. | |
5788 | { | |
5789 | while (__n != 0 && __unsampled_sz >= 2) | |
5790 | { | |
d34d36ef | 5791 | const pair<_Size, _Size> __p = |
9d8ff2f6 EW |
5792 | __gen_two_uniform_ints(__unsampled_sz, __unsampled_sz - 1, __g); |
5793 | ||
5794 | --__unsampled_sz; | |
d34d36ef | 5795 | if (__p.first < __n) |
9d8ff2f6 EW |
5796 | { |
5797 | *__out++ = *__first; | |
5798 | --__n; | |
5799 | } | |
5800 | ||
5801 | ++__first; | |
5802 | ||
5803 | if (__n == 0) break; | |
5804 | ||
5805 | --__unsampled_sz; | |
d34d36ef | 5806 | if (__p.second < __n) |
9d8ff2f6 EW |
5807 | { |
5808 | *__out++ = *__first; | |
5809 | --__n; | |
5810 | } | |
5811 | ||
5812 | ++__first; | |
5813 | } | |
5814 | } | |
5815 | ||
5816 | // The loop above is otherwise equivalent to this one-at-a-time version: | |
5817 | ||
5818 | for (; __n != 0; ++__first) | |
e7722f11 JW |
5819 | if (__d(__g, __param_type{0, --__unsampled_sz}) < __n) |
5820 | { | |
5821 | *__out++ = *__first; | |
5822 | --__n; | |
5823 | } | |
5824 | return __out; | |
5825 | } | |
5826 | ||
5827 | #if __cplusplus > 201402L | |
5828 | #define __cpp_lib_sample 201603 | |
5829 | /// Take a random sample from a population. | |
5830 | template<typename _PopulationIterator, typename _SampleIterator, | |
5831 | typename _Distance, typename _UniformRandomBitGenerator> | |
5832 | _SampleIterator | |
5833 | sample(_PopulationIterator __first, _PopulationIterator __last, | |
5834 | _SampleIterator __out, _Distance __n, | |
5835 | _UniformRandomBitGenerator&& __g) | |
5836 | { | |
5837 | using __pop_cat = typename | |
5838 | std::iterator_traits<_PopulationIterator>::iterator_category; | |
5839 | using __samp_cat = typename | |
5840 | std::iterator_traits<_SampleIterator>::iterator_category; | |
5841 | ||
5842 | static_assert( | |
5843 | __or_<is_convertible<__pop_cat, forward_iterator_tag>, | |
5844 | is_convertible<__samp_cat, random_access_iterator_tag>>::value, | |
5845 | "output range must use a RandomAccessIterator when input range" | |
5846 | " does not meet the ForwardIterator requirements"); | |
5847 | ||
5848 | static_assert(is_integral<_Distance>::value, | |
5849 | "sample size must be an integer type"); | |
5850 | ||
12d3f34b | 5851 | typename iterator_traits<_PopulationIterator>::difference_type __d = __n; |
e708b94b JW |
5852 | return _GLIBCXX_STD_A:: |
5853 | __sample(__first, __last, __pop_cat{}, __out, __samp_cat{}, __d, | |
5854 | std::forward<_UniformRandomBitGenerator>(__g)); | |
e7722f11 JW |
5855 | } |
5856 | #endif // C++17 | |
5857 | #endif // C++14 | |
5858 | ||
12ffa228 | 5859 | _GLIBCXX_END_NAMESPACE_ALGO |
4a15d842 | 5860 | _GLIBCXX_END_NAMESPACE_VERSION |
12ffa228 | 5861 | } // namespace std |
725dc051 | 5862 | |
046d30f4 | 5863 | #endif /* _STL_ALGO_H */ |