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