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