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