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::__iterator_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::__iterator_category(__first1),
341 std::__iterator_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::__iterator_category(__first1),
392 std::__iterator_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
3498 = typename iterator_traits<_ForwardIterator1>::iterator_category;
3499 using _Cat2
3500 = typename iterator_traits<_ForwardIterator2>::iterator_category;
3501 using _It1_is_RA = is_same<_Cat1, random_access_iterator_tag>;
3502 using _It2_is_RA = is_same<_Cat2, random_access_iterator_tag>;
3503 constexpr bool __ra_iters = __and_<_It1_is_RA, _It2_is_RA>::value;
3504 if constexpr (__ra_iters)
3505 {
3506 if ((__last1 - __first1) != (__last2 - __first2))
3507 return false;
3508 }
3509
3510 // Efficiently compare identical prefixes: O(N) if sequences
3511 // have the same elements in the same order.
3512 for (; __first1 != __last1 && __first2 != __last2;
3513 ++__first1, (void)++__first2)
3514 if (!__pred(*__first1, *__first2))
3515 break;
3516
3517 if constexpr (__ra_iters)
3518 {
3519 if (__first1 == __last1)
3520 return true;
3521 }
3522 else
3523 {
3524 auto __d1 = std::distance(__first1, __last1);
3525 auto __d2 = std::distance(__first2, __last2);
3526 if (__d1 == 0 && __d2 == 0)
3527 return true;
3528 if (__d1 != __d2)
3529 return false;
3530 }
3531
3532 for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan)
3533 {
3534 auto&& __scan_val = *__scan;
3535 auto __scaneq = __gnu_cxx::__ops::bind1st(__pred, __scan_val);
3536 if (__scan != std::__find_if(__first1, __scan, __scaneq))
3537 continue; // We've seen this one before.
3538
3539 auto __matches = std::__count_if(__first2, __last2, __scaneq);
3540 if (0 == __matches
3541 || std::__count_if(__scan, __last1, __scaneq) != __matches)
3542 return false;
3543 }
3544 return true;
3545 }
3546#pragma GCC diagnostic pop
3547
3548 /**
3549 * @brief Checks whether a permutaion of the second sequence is equal
3550 * to the first sequence.
3551 * @ingroup non_mutating_algorithms
3552 * @param __first1 Start of first range.
3553 * @param __last1 End of first range.
3554 * @param __first2 Start of second range.
3555 * @param __last2 End of first range.
3556 * @return true if there exists a permutation of the elements in the range
3557 * [__first2, __last2), beginning with ForwardIterator2 begin,
3558 * such that equal(__first1, __last1, begin) returns true;
3559 * otherwise, returns false.
3560 */
3561 template<typename _ForwardIterator1, typename _ForwardIterator2>
3562 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3563 inline bool
3564 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3565 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
3566 {
3567 __glibcxx_requires_valid_range(__first1, __last1);
3568 __glibcxx_requires_valid_range(__first2, __last2);
3569
3570 return std::__is_permutation(__first1, __last1, __first2, __last2,
3571 __gnu_cxx::__ops::equal_to());
3572 }
3573
3574 /**
3575 * @brief Checks whether a permutation of the second sequence is equal
3576 * to the first sequence.
3577 * @ingroup non_mutating_algorithms
3578 * @param __first1 Start of first range.
3579 * @param __last1 End of first range.
3580 * @param __first2 Start of second range.
3581 * @param __last2 End of first range.
3582 * @param __pred A binary predicate.
3583 * @return true if there exists a permutation of the elements in the range
3584 * [__first2, __last2), beginning with ForwardIterator2 begin,
3585 * such that equal(__first1, __last1, __begin, __pred) returns true;
3586 * otherwise, returns false.
3587 */
3588 template<typename _ForwardIterator1, typename _ForwardIterator2,
3589 typename _BinaryPredicate>
3590 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3591 inline bool
3592 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3593 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
3594 _BinaryPredicate __pred)
3595 {
3596 __glibcxx_requires_valid_range(__first1, __last1);
3597 __glibcxx_requires_valid_range(__first2, __last2);
3598
3599 return std::__is_permutation(__first1, __last1, __first2, __last2,
3600 __pred);
3601 }
3602#endif // __glibcxx_robust_nonmodifying_seq_ops
3603
3604#ifdef __glibcxx_clamp // C++ >= 17
3605 /**
3606 * @brief Returns the value clamped between lo and hi.
3607 * @ingroup sorting_algorithms
3608 * @param __val A value of arbitrary type.
3609 * @param __lo A lower limit of arbitrary type.
3610 * @param __hi An upper limit of arbitrary type.
3611 * @retval `__lo` if `__val < __lo`
3612 * @retval `__hi` if `__hi < __val`
3613 * @retval `__val` otherwise.
3614 * @pre `_Tp` is LessThanComparable and `(__hi < __lo)` is false.
3615 */
3616 template<typename _Tp>
3617 [[nodiscard]] constexpr const _Tp&
3618 clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi)
3619 {
3620 __glibcxx_assert(!(__hi < __lo));
3621 return std::min(std::max(__val, __lo), __hi);
3622 }
3623
3624 /**
3625 * @brief Returns the value clamped between lo and hi.
3626 * @ingroup sorting_algorithms
3627 * @param __val A value of arbitrary type.
3628 * @param __lo A lower limit of arbitrary type.
3629 * @param __hi An upper limit of arbitrary type.
3630 * @param __comp A comparison functor.
3631 * @retval `__lo` if `__comp(__val, __lo)`
3632 * @retval `__hi` if `__comp(__hi, __val)`
3633 * @retval `__val` otherwise.
3634 * @pre `__comp(__hi, __lo)` is false.
3635 */
3636 template<typename _Tp, typename _Compare>
3637 [[nodiscard]] constexpr const _Tp&
3638 clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
3639 {
3640 __glibcxx_assert(!__comp(__hi, __lo));
3641 return std::min(std::max(__val, __lo, __comp), __hi, __comp);
3642 }
3643#endif // __glibcxx_clamp
3644
3645 /**
3646 * @brief Generate two uniformly distributed integers using a
3647 * single distribution invocation.
3648 * @param __b0 The upper bound for the first integer.
3649 * @param __b1 The upper bound for the second integer.
3650 * @param __g A UniformRandomBitGenerator.
3651 * @return A pair (i, j) with i and j uniformly distributed
3652 * over [0, __b0) and [0, __b1), respectively.
3653 *
3654 * Requires: __b0 * __b1 <= __g.max() - __g.min().
3655 *
3656 * Using uniform_int_distribution with a range that is very
3657 * small relative to the range of the generator ends up wasting
3658 * potentially expensively generated randomness, since
3659 * uniform_int_distribution does not store leftover randomness
3660 * between invocations.
3661 *
3662 * If we know we want two integers in ranges that are sufficiently
3663 * small, we can compose the ranges, use a single distribution
3664 * invocation, and significantly reduce the waste.
3665 */
3666 template<typename _IntType, typename _UniformRandomBitGenerator>
3668 __gen_two_uniform_ints(_IntType __b0, _IntType __b1,
3669 _UniformRandomBitGenerator&& __g)
3670 {
3671 _IntType __x
3672 = uniform_int_distribution<_IntType>{0, (__b0 * __b1) - 1}(__g);
3673 return std::make_pair(__x / __b1, __x % __b1);
3674 }
3675
3676 /**
3677 * @brief Shuffle the elements of a sequence using a uniform random
3678 * number generator.
3679 * @ingroup mutating_algorithms
3680 * @param __first A forward iterator.
3681 * @param __last A forward iterator.
3682 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
3683 * @return Nothing.
3684 *
3685 * Reorders the elements in the range @p [__first,__last) using @p __g to
3686 * provide random numbers.
3687 */
3688 template<typename _RandomAccessIterator,
3689 typename _UniformRandomNumberGenerator>
3690 void
3691 shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3692 _UniformRandomNumberGenerator&& __g)
3693 {
3694 // concept requirements
3695 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
3696 _RandomAccessIterator>)
3697 __glibcxx_requires_valid_range(__first, __last);
3698
3699 if (__first == __last)
3700 return;
3701
3703 _DistanceType;
3704
3705 typedef typename std::make_unsigned<_DistanceType>::type __ud_type;
3706 typedef typename std::uniform_int_distribution<__ud_type> __distr_type;
3707 typedef typename __distr_type::param_type __p_type;
3708
3709 typedef typename remove_reference<_UniformRandomNumberGenerator>::type
3710 _Gen;
3712 __uc_type;
3713
3714 const __uc_type __urngrange = __g.max() - __g.min();
3715 const __uc_type __urange = __uc_type(__last - __first);
3716
3717 if (__urngrange / __urange >= __urange)
3718 // I.e. (__urngrange >= __urange * __urange) but without wrap issues.
3719 {
3720 _RandomAccessIterator __i = __first + 1;
3721
3722 // Since we know the range isn't empty, an even number of elements
3723 // means an uneven number of elements /to swap/, in which case we
3724 // do the first one up front:
3725
3726 if ((__urange % 2) == 0)
3727 {
3728 __distr_type __d{0, 1};
3729 std::iter_swap(__i++, __first + __d(__g));
3730 }
3731
3732 // Now we know that __last - __i is even, so we do the rest in pairs,
3733 // using a single distribution invocation to produce swap positions
3734 // for two successive elements at a time:
3735
3736 while (__i != __last)
3737 {
3738 const __uc_type __swap_range = __uc_type(__i - __first) + 1;
3739
3740 const pair<__uc_type, __uc_type> __pospos =
3741 __gen_two_uniform_ints(__swap_range, __swap_range + 1, __g);
3742
3743 std::iter_swap(__i++, __first + __pospos.first);
3744 std::iter_swap(__i++, __first + __pospos.second);
3745 }
3746
3747 return;
3748 }
3749
3750 __distr_type __d;
3751
3752 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
3753 std::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first)));
3754 }
3755#endif // C++11
3756
3757_GLIBCXX_BEGIN_NAMESPACE_ALGO
3758
3759 /**
3760 * @brief Apply a function to every element of a sequence.
3761 * @ingroup non_mutating_algorithms
3762 * @param __first An input iterator.
3763 * @param __last An input iterator.
3764 * @param __f A unary function object.
3765 * @return @p __f
3766 *
3767 * Applies the function object @p __f to each element in the range
3768 * @p [first,last). @p __f must not modify the order of the sequence.
3769 * If @p __f has a return value it is ignored.
3770 */
3771 template<typename _InputIterator, typename _Function>
3772 _GLIBCXX20_CONSTEXPR
3773 _Function
3774 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
3775 {
3776 // concept requirements
3777 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3778 __glibcxx_requires_valid_range(__first, __last);
3779 for (; __first != __last; ++__first)
3780 __f(*__first);
3781 return __f; // N.B. [alg.foreach] says std::move(f) but it's redundant.
3782 }
3783
3784#if __cplusplus >= 201703L
3785 /**
3786 * @brief Apply a function to every element of a sequence.
3787 * @ingroup non_mutating_algorithms
3788 * @param __first An input iterator.
3789 * @param __n A value convertible to an integer.
3790 * @param __f A unary function object.
3791 * @return `__first+__n`
3792 *
3793 * Applies the function object `__f` to each element in the range
3794 * `[first, first+n)`. `__f` must not modify the order of the sequence.
3795 * If `__f` has a return value it is ignored.
3796 */
3797 template<typename _InputIterator, typename _Size, typename _Function>
3798 _GLIBCXX20_CONSTEXPR
3799 _InputIterator
3800 for_each_n(_InputIterator __first, _Size __n, _Function __f)
3801 {
3802 auto __n2 = std::__size_to_integer(__n);
3804 if constexpr (is_base_of_v<random_access_iterator_tag, _Cat>)
3805 {
3806 if (__n2 <= 0)
3807 return __first;
3809 auto __last = __first + __d;
3810 std::for_each(__first, __last, std::move(__f));
3811 return __last;
3812 }
3813 else
3814 {
3815 while (__n2-->0)
3816 {
3817 __f(*__first);
3818 ++__first;
3819 }
3820 return __first;
3821 }
3822 }
3823#endif // C++17
3824
3825 /**
3826 * @brief Find the first occurrence of a value in a sequence.
3827 * @ingroup non_mutating_algorithms
3828 * @param __first An input iterator.
3829 * @param __last An input iterator.
3830 * @param __val The value to find.
3831 * @return The first iterator @c i in the range @p [__first,__last)
3832 * such that @c *i == @p __val, or @p __last if no such iterator exists.
3833 */
3834 template<typename _InputIterator, typename _Tp>
3835 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3836 inline _InputIterator
3837 find(_InputIterator __first, _InputIterator __last, const _Tp& __val)
3838 {
3839 // concept requirements
3840 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3841 __glibcxx_function_requires(_EqualOpConcept<
3843 __glibcxx_requires_valid_range(__first, __last);
3844
3845#if __cpp_if_constexpr && __glibcxx_type_trait_variable_templates
3846 using _ValT = typename iterator_traits<_InputIterator>::value_type;
3847 if constexpr (__can_use_memchr_for_find<_ValT, _Tp>)
3848 if constexpr (is_pointer_v<decltype(std::__niter_base(__first))>
3849#if __glibcxx_concepts && __glibcxx_to_address
3850 || contiguous_iterator<_InputIterator>
3851#endif
3852 )
3853 {
3854 // If conversion to the 1-byte value_type alters the value,
3855 // it would not be found by std::find using equality comparison.
3856 // We need to check this here, because otherwise something like
3857 // memchr("a", 'a'+256, 1) would give a false positive match.
3858 if (!(static_cast<_ValT>(__val) == __val))
3859 return __last;
3860 else if (!__is_constant_evaluated())
3861 {
3862 const int __ival = static_cast<int>(__val);
3863 if (auto __n = __last - __first; __n > 0)
3864 {
3865#if __glibcxx_concepts && __glibcxx_to_address
3866 const void* __p0 = std::to_address(__first);
3867#else
3868 const void* __p0 = std::__niter_base(__first);
3869#endif
3870 if (auto __p1 = __builtin_memchr(__p0, __ival, __n))
3871 return __first + ((const char*)__p1 - (const char*)__p0);
3872 }
3873 return __last;
3874 }
3875 }
3876#endif
3877
3878 return std::__find_if(__first, __last,
3879 __gnu_cxx::__ops::__equal_to(__val));
3880 }
3881
3882 /**
3883 * @brief Find the first element in a sequence for which a
3884 * predicate is true.
3885 * @ingroup non_mutating_algorithms
3886 * @param __first An input iterator.
3887 * @param __last An input iterator.
3888 * @param __pred A predicate.
3889 * @return The first iterator @c i in the range @p [__first,__last)
3890 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
3891 */
3892 template<typename _InputIterator, typename _Predicate>
3893 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3894 inline _InputIterator
3895 find_if(_InputIterator __first, _InputIterator __last,
3896 _Predicate __pred)
3897 {
3898 // concept requirements
3899 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3900 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
3902 __glibcxx_requires_valid_range(__first, __last);
3903
3904 return std::__find_if(__first, __last, __pred);
3905 }
3906
3907 /**
3908 * @brief Find element from a set in a sequence.
3909 * @ingroup non_mutating_algorithms
3910 * @param __first1 Start of range to search.
3911 * @param __last1 End of range to search.
3912 * @param __first2 Start of match candidates.
3913 * @param __last2 End of match candidates.
3914 * @return The first iterator @c i in the range
3915 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
3916 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
3917 *
3918 * Searches the range @p [__first1,__last1) for an element that is
3919 * equal to some element in the range [__first2,__last2). If
3920 * found, returns an iterator in the range [__first1,__last1),
3921 * otherwise returns @p __last1.
3922 */
3923 template<typename _InputIterator, typename _ForwardIterator>
3924 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3925 _InputIterator
3926 find_first_of(_InputIterator __first1, _InputIterator __last1,
3927 _ForwardIterator __first2, _ForwardIterator __last2)
3928 {
3929 // concept requirements
3930 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3931 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3932 __glibcxx_function_requires(_EqualOpConcept<
3935 __glibcxx_requires_valid_range(__first1, __last1);
3936 __glibcxx_requires_valid_range(__first2, __last2);
3937
3938 for (; __first1 != __last1; ++__first1)
3939 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
3940 if (*__first1 == *__iter)
3941 return __first1;
3942 return __last1;
3943 }
3944
3945 /**
3946 * @brief Find element from a set in a sequence using a predicate.
3947 * @ingroup non_mutating_algorithms
3948 * @param __first1 Start of range to search.
3949 * @param __last1 End of range to search.
3950 * @param __first2 Start of match candidates.
3951 * @param __last2 End of match candidates.
3952 * @param __comp Predicate to use.
3953 * @return The first iterator @c i in the range
3954 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
3955 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
3956 * such iterator exists.
3957 *
3958
3959 * Searches the range @p [__first1,__last1) for an element that is
3960 * equal to some element in the range [__first2,__last2). If
3961 * found, returns an iterator in the range [__first1,__last1),
3962 * otherwise returns @p __last1.
3963 */
3964 template<typename _InputIterator, typename _ForwardIterator,
3965 typename _BinaryPredicate>
3966 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3967 _InputIterator
3968 find_first_of(_InputIterator __first1, _InputIterator __last1,
3969 _ForwardIterator __first2, _ForwardIterator __last2,
3970 _BinaryPredicate __comp)
3971 {
3972 // concept requirements
3973 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3974 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3975 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
3978 __glibcxx_requires_valid_range(__first1, __last1);
3979 __glibcxx_requires_valid_range(__first2, __last2);
3980
3981 for (; __first1 != __last1; ++__first1)
3982 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
3983 if (__comp(*__first1, *__iter))
3984 return __first1;
3985 return __last1;
3986 }
3987
3988 /**
3989 * @brief Find two adjacent values in a sequence that are equal.
3990 * @ingroup non_mutating_algorithms
3991 * @param __first A forward iterator.
3992 * @param __last A forward iterator.
3993 * @return The first iterator @c i such that @c i and @c i+1 are both
3994 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
3995 * or @p __last if no such iterator exists.
3996 */
3997 template<typename _ForwardIterator>
3998 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3999 inline _ForwardIterator
4000 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
4001 {
4002 // concept requirements
4003 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4004 __glibcxx_function_requires(_EqualityComparableConcept<
4006 __glibcxx_requires_valid_range(__first, __last);
4007
4008 return std::__adjacent_find(__first, __last,
4009 __gnu_cxx::__ops::equal_to());
4010 }
4011
4012 /**
4013 * @brief Find two adjacent values in a sequence using a predicate.
4014 * @ingroup non_mutating_algorithms
4015 * @param __first A forward iterator.
4016 * @param __last A forward iterator.
4017 * @param __binary_pred A binary predicate.
4018 * @return The first iterator @c i such that @c i and @c i+1 are both
4019 * valid iterators in @p [__first,__last) and such that
4020 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
4021 * exists.
4022 */
4023 template<typename _ForwardIterator, typename _BinaryPredicate>
4024 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4025 inline _ForwardIterator
4026 adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
4027 _BinaryPredicate __binary_pred)
4028 {
4029 // concept requirements
4030 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4031 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4034 __glibcxx_requires_valid_range(__first, __last);
4035
4036 return std::__adjacent_find(__first, __last, __binary_pred);
4037 }
4038
4039 /**
4040 * @brief Count the number of copies of a value in a sequence.
4041 * @ingroup non_mutating_algorithms
4042 * @param __first An input iterator.
4043 * @param __last An input iterator.
4044 * @param __value The value to be counted.
4045 * @return The number of iterators @c i in the range @p [__first,__last)
4046 * for which @c *i == @p __value
4047 */
4048 template<typename _InputIterator, typename _Tp>
4049 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4050 inline typename iterator_traits<_InputIterator>::difference_type
4051 count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
4052 {
4053 // concept requirements
4054 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4055 __glibcxx_function_requires(_EqualOpConcept<
4057 __glibcxx_requires_valid_range(__first, __last);
4058
4059 return std::__count_if(__first, __last,
4060 __gnu_cxx::__ops::__equal_to(__value));
4061 }
4062
4063 /**
4064 * @brief Count the elements of a sequence for which a predicate is true.
4065 * @ingroup non_mutating_algorithms
4066 * @param __first An input iterator.
4067 * @param __last An input iterator.
4068 * @param __pred A predicate.
4069 * @return The number of iterators @c i in the range @p [__first,__last)
4070 * for which @p __pred(*i) is true.
4071 */
4072 template<typename _InputIterator, typename _Predicate>
4073 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4074 inline typename iterator_traits<_InputIterator>::difference_type
4075 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
4076 {
4077 // concept requirements
4078 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4079 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4081 __glibcxx_requires_valid_range(__first, __last);
4082
4083 return std::__count_if(__first, __last, __pred);
4084 }
4085
4086 /**
4087 * @brief Search a sequence for a matching sub-sequence.
4088 * @ingroup non_mutating_algorithms
4089 * @param __first1 A forward iterator.
4090 * @param __last1 A forward iterator.
4091 * @param __first2 A forward iterator.
4092 * @param __last2 A forward iterator.
4093 * @return The first iterator @c i in the range @p
4094 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
4095 * *(__first2+N) for each @c N in the range @p
4096 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
4097 *
4098 * Searches the range @p [__first1,__last1) for a sub-sequence that
4099 * compares equal value-by-value with the sequence given by @p
4100 * [__first2,__last2) and returns an iterator to the first element
4101 * of the sub-sequence, or @p __last1 if the sub-sequence is not
4102 * found.
4103 *
4104 * Because the sub-sequence must lie completely within the range @p
4105 * [__first1,__last1) it must start at a position less than @p
4106 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4107 * length of the sub-sequence.
4108 *
4109 * This means that the returned iterator @c i will be in the range
4110 * @p [__first1,__last1-(__last2-__first2))
4111 */
4112 template<typename _ForwardIterator1, typename _ForwardIterator2>
4113 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4114 inline _ForwardIterator1
4115 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4116 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
4117 {
4118 // concept requirements
4119 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4120 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4121 __glibcxx_function_requires(_EqualOpConcept<
4124 __glibcxx_requires_valid_range(__first1, __last1);
4125 __glibcxx_requires_valid_range(__first2, __last2);
4126
4127 return std::__search(__first1, __last1, __first2, __last2,
4128 __gnu_cxx::__ops::equal_to());
4129 }
4130
4131 /**
4132 * @brief Search a sequence for a number of consecutive values.
4133 * @ingroup non_mutating_algorithms
4134 * @param __first A forward iterator.
4135 * @param __last A forward iterator.
4136 * @param __count The number of consecutive values.
4137 * @param __val The value to find.
4138 * @return The first iterator @c i in the range @p
4139 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4140 * each @c N in the range @p [0,__count), or @p __last if no such
4141 * iterator exists.
4142 *
4143 * Searches the range @p [__first,__last) for @p count consecutive elements
4144 * equal to @p __val.
4145 */
4146 template<typename _ForwardIterator, typename _Integer, typename _Tp>
4147 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4148 inline _ForwardIterator
4149 search_n(_ForwardIterator __first, _ForwardIterator __last,
4150 _Integer __count, const _Tp& __val)
4151 {
4152 // concept requirements
4153 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4154 __glibcxx_function_requires(_EqualOpConcept<
4156 __glibcxx_requires_valid_range(__first, __last);
4157
4158 return std::__search_n(__first, __last, __count,
4159 __gnu_cxx::__ops::__equal_to(__val));
4160 }
4161
4162
4163 /**
4164 * @brief Search a sequence for a number of consecutive values using a
4165 * predicate.
4166 * @ingroup non_mutating_algorithms
4167 * @param __first A forward iterator.
4168 * @param __last A forward iterator.
4169 * @param __count The number of consecutive values.
4170 * @param __val The value to find.
4171 * @param __binary_pred A binary predicate.
4172 * @return The first iterator @c i in the range @p
4173 * [__first,__last-__count) such that @p
4174 * __binary_pred(*(i+N),__val) is true for each @c N in the range
4175 * @p [0,__count), or @p __last if no such iterator exists.
4176 *
4177 * Searches the range @p [__first,__last) for @p __count
4178 * consecutive elements for which the predicate returns true.
4179 */
4180 template<typename _ForwardIterator, typename _Integer, typename _Tp,
4181 typename _BinaryPredicate>
4182 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4183 inline _ForwardIterator
4184 search_n(_ForwardIterator __first, _ForwardIterator __last,
4185 _Integer __count, const _Tp& __val,
4186 _BinaryPredicate __binary_pred)
4187 {
4188 // concept requirements
4189 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4190 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4192 __glibcxx_requires_valid_range(__first, __last);
4193
4194 return std::__search_n(__first, __last, __count,
4195 __gnu_cxx::__ops::bind2nd(__binary_pred, __val));
4196 }
4197
4198#if __cplusplus >= 201703L
4199 /** @brief Search a sequence using a Searcher object.
4200 *
4201 * @param __first A forward iterator.
4202 * @param __last A forward iterator.
4203 * @param __searcher A callable object.
4204 * @return @p __searcher(__first,__last).first
4205 */
4206 template<typename _ForwardIterator, typename _Searcher>
4207 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4208 inline _ForwardIterator
4209 search(_ForwardIterator __first, _ForwardIterator __last,
4210 const _Searcher& __searcher)
4211 { return __searcher(__first, __last).first; }
4212#endif
4213
4214 /**
4215 * @brief Perform an operation on a sequence.
4216 * @ingroup mutating_algorithms
4217 * @param __first An input iterator.
4218 * @param __last An input iterator.
4219 * @param __result An output iterator.
4220 * @param __unary_op A unary operator.
4221 * @return An output iterator equal to @p __result+(__last-__first).
4222 *
4223 * Applies the operator to each element in the input range and assigns
4224 * the results to successive elements of the output sequence.
4225 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
4226 * range @p [0,__last-__first).
4227 *
4228 * @p unary_op must not alter its argument.
4229 */
4230 template<typename _InputIterator, typename _OutputIterator,
4231 typename _UnaryOperation>
4232 _GLIBCXX20_CONSTEXPR
4233 _OutputIterator
4234 transform(_InputIterator __first, _InputIterator __last,
4235 _OutputIterator __result, _UnaryOperation __unary_op)
4236 {
4237 // concept requirements
4238 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4239 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4240 // "the type returned by a _UnaryOperation"
4241 __typeof__(__unary_op(*__first))>)
4242 __glibcxx_requires_valid_range(__first, __last);
4243
4244 for (; __first != __last; ++__first, (void)++__result)
4245 *__result = __unary_op(*__first);
4246 return __result;
4247 }
4248
4249 /**
4250 * @brief Perform an operation on corresponding elements of two sequences.
4251 * @ingroup mutating_algorithms
4252 * @param __first1 An input iterator.
4253 * @param __last1 An input iterator.
4254 * @param __first2 An input iterator.
4255 * @param __result An output iterator.
4256 * @param __binary_op A binary operator.
4257 * @return An output iterator equal to @p result+(last-first).
4258 *
4259 * Applies the operator to the corresponding elements in the two
4260 * input ranges and assigns the results to successive elements of the
4261 * output sequence.
4262 * Evaluates @p
4263 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
4264 * @c N in the range @p [0,__last1-__first1).
4265 *
4266 * @p binary_op must not alter either of its arguments.
4267 */
4268 template<typename _InputIterator1, typename _InputIterator2,
4269 typename _OutputIterator, typename _BinaryOperation>
4270 _GLIBCXX20_CONSTEXPR
4271 _OutputIterator
4272 transform(_InputIterator1 __first1, _InputIterator1 __last1,
4273 _InputIterator2 __first2, _OutputIterator __result,
4274 _BinaryOperation __binary_op)
4275 {
4276 // concept requirements
4277 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4278 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4279 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4280 // "the type returned by a _BinaryOperation"
4281 __typeof__(__binary_op(*__first1,*__first2))>)
4282 __glibcxx_requires_valid_range(__first1, __last1);
4283
4284 for (; __first1 != __last1; ++__first1, (void)++__first2, ++__result)
4285 *__result = __binary_op(*__first1, *__first2);
4286 return __result;
4287 }
4288
4289 /**
4290 * @brief Replace each occurrence of one value in a sequence with another
4291 * value.
4292 * @ingroup mutating_algorithms
4293 * @param __first A forward iterator.
4294 * @param __last A forward iterator.
4295 * @param __old_value The value to be replaced.
4296 * @param __new_value The replacement value.
4297 * @return replace() returns no value.
4298 *
4299 * For each iterator `i` in the range `[__first,__last)` if
4300 * `*i == __old_value` then the assignment `*i = __new_value` is performed.
4301 */
4302 template<typename _ForwardIterator, typename _Tp>
4303 _GLIBCXX20_CONSTEXPR
4304 void
4305 replace(_ForwardIterator __first, _ForwardIterator __last,
4306 const _Tp& __old_value, const _Tp& __new_value)
4307 {
4308 // concept requirements
4309 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4310 _ForwardIterator>)
4311 __glibcxx_function_requires(_EqualOpConcept<
4313 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4315 __glibcxx_requires_valid_range(__first, __last);
4316
4317 for (; __first != __last; ++__first)
4318 if (*__first == __old_value)
4319 *__first = __new_value;
4320 }
4321
4322 /**
4323 * @brief Replace each value in a sequence for which a predicate returns
4324 * true with another value.
4325 * @ingroup mutating_algorithms
4326 * @param __first A forward iterator.
4327 * @param __last A forward iterator.
4328 * @param __pred A predicate.
4329 * @param __new_value The replacement value.
4330 * @return replace_if() returns no value.
4331 *
4332 * For each iterator `i` in the range `[__first,__last)` if `__pred(*i)`
4333 * is true then the assignment `*i = __new_value` is performed.
4334 */
4335 template<typename _ForwardIterator, typename _Predicate, typename _Tp>
4336 _GLIBCXX20_CONSTEXPR
4337 void
4338 replace_if(_ForwardIterator __first, _ForwardIterator __last,
4339 _Predicate __pred, const _Tp& __new_value)
4340 {
4341 // concept requirements
4342 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4343 _ForwardIterator>)
4344 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4346 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4348 __glibcxx_requires_valid_range(__first, __last);
4349
4350 for (; __first != __last; ++__first)
4351 if (__pred(*__first))
4352 *__first = __new_value;
4353 }
4354
4355 /**
4356 * @brief Assign the result of a function object to each value in a
4357 * sequence.
4358 * @ingroup mutating_algorithms
4359 * @param __first A forward iterator.
4360 * @param __last A forward iterator.
4361 * @param __gen A function object callable with no arguments.
4362 * @return generate() returns no value.
4363 *
4364 * Performs the assignment `*i = __gen()` for each `i` in the range
4365 * `[__first, __last)`.
4366 */
4367 template<typename _ForwardIterator, typename _Generator>
4368 _GLIBCXX20_CONSTEXPR
4369 void
4370 generate(_ForwardIterator __first, _ForwardIterator __last,
4371 _Generator __gen)
4372 {
4373 // concept requirements
4374 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4375 __glibcxx_function_requires(_GeneratorConcept<_Generator,
4377 __glibcxx_requires_valid_range(__first, __last);
4378
4379 for (; __first != __last; ++__first)
4380 *__first = __gen();
4381 }
4382
4383 /**
4384 * @brief Assign the result of a function object to each value in a
4385 * sequence.
4386 * @ingroup mutating_algorithms
4387 * @param __first A forward iterator.
4388 * @param __n The length of the sequence.
4389 * @param __gen A function object callable with no arguments.
4390 * @return The end of the sequence, i.e., `__first + __n`
4391 *
4392 * Performs the assignment `*i = __gen()` for each `i` in the range
4393 * `[__first, __first + __n)`.
4394 *
4395 * If `__n` is negative, the function does nothing and returns `__first`.
4396 */
4397 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4398 // DR 865. More algorithms that throw away information
4399 // DR 426. search_n(), fill_n(), and generate_n() with negative n
4400 template<typename _OutputIterator, typename _Size, typename _Generator>
4401 _GLIBCXX20_CONSTEXPR
4402 _OutputIterator
4403 generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
4404 {
4405 // concept requirements
4406 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4407 // "the type returned by a _Generator"
4408 __typeof__(__gen())>)
4409
4410 typedef __decltype(std::__size_to_integer(__n)) _IntSize;
4411 for (_IntSize __niter = std::__size_to_integer(__n);
4412 __niter > 0; --__niter, (void) ++__first)
4413 *__first = __gen();
4414 return __first;
4415 }
4416
4417 /**
4418 * @brief Copy a sequence, removing consecutive duplicate values.
4419 * @ingroup mutating_algorithms
4420 * @param __first An input iterator.
4421 * @param __last An input iterator.
4422 * @param __result An output iterator.
4423 * @return An iterator designating the end of the resulting sequence.
4424 *
4425 * Copies each element in the range `[__first, __last)` to the range
4426 * beginning at `__result`, except that only the first element is copied
4427 * from groups of consecutive elements that compare equal.
4428 * `unique_copy()` is stable, so the relative order of elements that are
4429 * copied is unchanged.
4430 */
4431 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4432 // DR 241. Does unique_copy() require CopyConstructible and Assignable?
4433 // DR 538. 241 again: Does unique_copy() require CopyConstructible and
4434 // Assignable?
4435 template<typename _InputIterator, typename _OutputIterator>
4436 _GLIBCXX20_CONSTEXPR
4437 inline _OutputIterator
4438 unique_copy(_InputIterator __first, _InputIterator __last,
4439 _OutputIterator __result)
4440 {
4441 // concept requirements
4442 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4443 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4445 __glibcxx_function_requires(_EqualityComparableConcept<
4447 __glibcxx_requires_valid_range(__first, __last);
4448
4449 if (__first == __last)
4450 return __result;
4451 return std::__unique_copy(__first, __last, __result,
4452 __gnu_cxx::__ops::equal_to(),
4453 std::__iterator_category(__first));
4454 }
4455
4456 /**
4457 * @brief Copy a sequence, removing consecutive values using a predicate.
4458 * @ingroup mutating_algorithms
4459 * @param __first An input iterator.
4460 * @param __last An input iterator.
4461 * @param __result An output iterator.
4462 * @param __binary_pred A binary predicate.
4463 * @return An iterator designating the end of the resulting sequence.
4464 *
4465 * Copies each element in the range `[__first, __last)` to the range
4466 * beginning at `__result`, except that only the first element is copied
4467 * from groups of consecutive elements for which `__binary_pred` returns
4468 * true.
4469 * `unique_copy()` is stable, so the relative order of elements that are
4470 * copied is unchanged.
4471 */
4472 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4473 // DR 241. Does unique_copy() require CopyConstructible and Assignable?
4474 template<typename _InputIterator, typename _OutputIterator,
4475 typename _BinaryPredicate>
4476 _GLIBCXX20_CONSTEXPR
4477 inline _OutputIterator
4478 unique_copy(_InputIterator __first, _InputIterator __last,
4479 _OutputIterator __result,
4480 _BinaryPredicate __binary_pred)
4481 {
4482 // concept requirements -- predicates checked later
4483 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4484 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4486 __glibcxx_requires_valid_range(__first, __last);
4487 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4490
4491 if (__first == __last)
4492 return __result;
4493 return std::__unique_copy(__first, __last, __result, __binary_pred,
4494 std::__iterator_category(__first));
4495 }
4496
4497#if __cplusplus <= 201103L || _GLIBCXX_USE_DEPRECATED
4498#if _GLIBCXX_HOSTED
4499 /**
4500 * @brief Randomly shuffle the elements of a sequence.
4501 * @ingroup mutating_algorithms
4502 * @param __first A forward iterator.
4503 * @param __last A forward iterator.
4504 * @return Nothing.
4505 *
4506 * Reorder the elements in the range `[__first, __last)` using a random
4507 * distribution, so that every possible ordering of the sequence is
4508 * equally likely.
4509 *
4510 * @deprecated
4511 * Since C++17, `std::random_shuffle` is not part of the C++ standard.
4512 * Use `std::shuffle` instead, which was introduced in C++11.
4513 */
4514 template<typename _RandomAccessIterator>
4515 _GLIBCXX14_DEPRECATED_SUGGEST("std::shuffle")
4516 inline void
4517 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
4518 {
4519 // concept requirements
4520 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4521 _RandomAccessIterator>)
4522 __glibcxx_requires_valid_range(__first, __last);
4523
4524 if (__first == __last)
4525 return;
4526
4528 _Dist;
4529
4530#if RAND_MAX < __INT_MAX__
4531 if (__builtin_expect((__last - __first) >= RAND_MAX / 4, 0))
4532 {
4533 // Use a xorshift implementation seeded by two calls to rand()
4534 // instead of using rand() for all the random numbers needed.
4535 unsigned __xss
4536 = (unsigned)std::rand() ^ ((unsigned)std::rand() << 15);
4537 for (_RandomAccessIterator __i = __first + _Dist(1); __i != __last;
4538 ++__i)
4539 {
4540 __xss += !__xss;
4541 __xss ^= __xss << 13;
4542 __xss ^= __xss >> 17;
4543 __xss ^= __xss << 5;
4544 _RandomAccessIterator __j
4545 = __first + _Dist(__xss % ((__i - __first) + 1));
4546 if (__i != __j)
4547 std::iter_swap(__i, __j);
4548 }
4549 return;
4550 }
4551#endif
4552
4553 for (_RandomAccessIterator __i = __first + _Dist(1); __i != __last; ++__i)
4554 {
4555 // XXX rand() % N is not uniformly distributed
4556 _RandomAccessIterator __j
4557 = __first + _Dist(std::rand() % ((__i - __first) + 1));
4558 if (__i != __j)
4559 std::iter_swap(__i, __j);
4560 }
4561 }
4562
4563 /**
4564 * @brief Shuffle the elements of a sequence using a random number
4565 * generator.
4566 * @ingroup mutating_algorithms
4567 * @param __first A forward iterator.
4568 * @param __last A forward iterator.
4569 * @param __rand The RNG functor or function.
4570 * @return Nothing.
4571 *
4572 * Reorders the elements in the range `[__first, __last)` using `__rand`
4573 * to provide a random distribution. Calling `__rand(N)` for a positive
4574 * integer `N` should return a randomly chosen integer from the
4575 * range `[0, N)`.
4576 *
4577 * @deprecated
4578 * Since C++17, `std::random_shuffle` is not part of the C++ standard.
4579 * Use `std::shuffle` instead, which was introduced in C++11.
4580 */
4581 template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
4582 _GLIBCXX14_DEPRECATED_SUGGEST("std::shuffle")
4583 void
4584 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
4585#if __cplusplus >= 201103L
4586 _RandomNumberGenerator&& __rand)
4587#else
4588 _RandomNumberGenerator& __rand)
4589#endif
4590 {
4591 // concept requirements
4592 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4593 _RandomAccessIterator>)
4594 __glibcxx_requires_valid_range(__first, __last);
4595
4596 if (__first == __last)
4597 return;
4598
4600 _Dist;
4601
4602 for (_RandomAccessIterator __i = __first + _Dist(1); __i != __last; ++__i)
4603 {
4604 _RandomAccessIterator __j
4605 = __first + _Dist(__rand((__i - __first) + 1));
4606 if (__i != __j)
4607 std::iter_swap(__i, __j);
4608 }
4609 }
4610#endif // HOSTED
4611#endif // <= C++11 || USE_DEPRECATED
4612
4613 /**
4614 * @brief Move elements for which a predicate is true to the beginning
4615 * of a sequence.
4616 * @ingroup mutating_algorithms
4617 * @param __first A forward iterator.
4618 * @param __last A forward iterator.
4619 * @param __pred A predicate functor.
4620 * @return An iterator `middle` such that `__pred(i)` is true for each
4621 * iterator `i` in the range `[__first, middle)` and false for each `i`
4622 * in the range `[middle, __last)`.
4623 *
4624 * `__pred` must not modify its operand. `partition()` does not preserve
4625 * the relative ordering of elements in each group, use
4626 * `stable_partition()` if this is needed.
4627 */
4628 template<typename _ForwardIterator, typename _Predicate>
4629 _GLIBCXX20_CONSTEXPR
4630 inline _ForwardIterator
4631 partition(_ForwardIterator __first, _ForwardIterator __last,
4632 _Predicate __pred)
4633 {
4634 // concept requirements
4635 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4636 _ForwardIterator>)
4637 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4639 __glibcxx_requires_valid_range(__first, __last);
4640
4641 return std::__partition(__first, __last, __pred,
4642 std::__iterator_category(__first));
4643 }
4644
4645
4646 /**
4647 * @brief Sort the smallest elements of a sequence.
4648 * @ingroup sorting_algorithms
4649 * @param __first An iterator.
4650 * @param __middle Another iterator.
4651 * @param __last Another iterator.
4652 * @return Nothing.
4653 *
4654 * Sorts the smallest `(__middle - __first)` elements in the range
4655 * `[first, last)` and moves them to the range `[__first, __middle)`. The
4656 * order of the remaining elements in the range `[__middle, __last)` is
4657 * unspecified.
4658 * After the sort if `i` and `j` are iterators in the range
4659 * `[__first, __middle)` such that `i` precedes `j` and `k` is an iterator
4660 * in the range `[__middle, __last)` then `*j < *i` and `*k < *i` are
4661 * both false.
4662 */
4663 template<typename _RandomAccessIterator>
4664 _GLIBCXX20_CONSTEXPR
4665 inline void
4666 partial_sort(_RandomAccessIterator __first,
4667 _RandomAccessIterator __middle,
4668 _RandomAccessIterator __last)
4669 {
4670 // concept requirements
4671 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4672 _RandomAccessIterator>)
4673 __glibcxx_function_requires(_LessThanComparableConcept<
4675 __glibcxx_requires_valid_range(__first, __middle);
4676 __glibcxx_requires_valid_range(__middle, __last);
4677 __glibcxx_requires_irreflexive(__first, __last);
4678
4679 std::__partial_sort(__first, __middle, __last,
4680 __gnu_cxx::__ops::less());
4681 }
4682
4683 /**
4684 * @brief Sort the smallest elements of a sequence using a predicate
4685 * for comparison.
4686 * @ingroup sorting_algorithms
4687 * @param __first An iterator.
4688 * @param __middle Another iterator.
4689 * @param __last Another iterator.
4690 * @param __comp A comparison functor.
4691 * @return Nothing.
4692 *
4693 * Sorts the smallest `(__middle - __first)` elements in the range
4694 * `[__first, __last)` and moves them to the range `[__first, __middle)`.
4695 * The order of the remaining elements in the range `[__middle, __last)` is
4696 * unspecified.
4697 * After the sort if `i` and `j` are iterators in the range
4698 * `[__first, __middle)` such that `i` precedes `j` and `k` is an iterator
4699 * in the range `[__middle, __last)` then `*__comp(j, *i)` and
4700 * `__comp(*k, *i)` are both false.
4701 */
4702 template<typename _RandomAccessIterator, typename _Compare>
4703 _GLIBCXX20_CONSTEXPR
4704 inline void
4705 partial_sort(_RandomAccessIterator __first,
4706 _RandomAccessIterator __middle,
4707 _RandomAccessIterator __last,
4708 _Compare __comp)
4709 {
4710 // concept requirements
4711 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4712 _RandomAccessIterator>)
4713 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4716 __glibcxx_requires_valid_range(__first, __middle);
4717 __glibcxx_requires_valid_range(__middle, __last);
4718 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
4719
4720 std::__partial_sort(__first, __middle, __last, __comp);
4721 }
4722
4723 /**
4724 * @brief Sort a sequence just enough to find a particular position.
4725 * @ingroup sorting_algorithms
4726 * @param __first An iterator.
4727 * @param __nth Another iterator.
4728 * @param __last Another iterator.
4729 * @return Nothing.
4730 *
4731 * Rearranges the elements in the range `[__first, __last)` so that `*__nth`
4732 * is the same element that would have been in that position had the
4733 * whole sequence been sorted. The elements either side of `*__nth` are
4734 * not completely sorted, but for any iterator `i` in the range
4735 * `[__first, __nth)` and any iterator `j` in the range `[__nth, __last)` it
4736 * holds that `*j < *i` is false.
4737 */
4738 template<typename _RandomAccessIterator>
4739 _GLIBCXX20_CONSTEXPR
4740 inline void
4741 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
4742 _RandomAccessIterator __last)
4743 {
4744 // concept requirements
4745 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4746 _RandomAccessIterator>)
4747 __glibcxx_function_requires(_LessThanComparableConcept<
4749 __glibcxx_requires_valid_range(__first, __nth);
4750 __glibcxx_requires_valid_range(__nth, __last);
4751 __glibcxx_requires_irreflexive(__first, __last);
4752
4753 if (__first == __last || __nth == __last)
4754 return;
4755
4756 std::__introselect(__first, __nth, __last,
4757 std::__lg(__last - __first) * 2,
4758 __gnu_cxx::__ops::less());
4759 }
4760
4761 /**
4762 * @brief Sort a sequence just enough to find a particular position
4763 * using a predicate for comparison.
4764 * @ingroup sorting_algorithms
4765 * @param __first An iterator.
4766 * @param __nth Another iterator.
4767 * @param __last Another iterator.
4768 * @param __comp A comparison functor.
4769 * @return Nothing.
4770 *
4771 * Rearranges the elements in the range `[__first, __last)` so that `*__nth`
4772 * is the same element that would have been in that position had the
4773 * whole sequence been sorted. The elements either side of `*__nth` are
4774 * not completely sorted, but for any iterator `i` in the range
4775 * `[__first, __nth)` and any iterator `j` in the range `[__nth, __last)`
4776 * it holds that `__comp(*j, *i)` is false.
4777 */
4778 template<typename _RandomAccessIterator, typename _Compare>
4779 _GLIBCXX20_CONSTEXPR
4780 inline void
4781 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
4782 _RandomAccessIterator __last, _Compare __comp)
4783 {
4784 // concept requirements
4785 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4786 _RandomAccessIterator>)
4787 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4790 __glibcxx_requires_valid_range(__first, __nth);
4791 __glibcxx_requires_valid_range(__nth, __last);
4792 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
4793
4794 if (__first == __last || __nth == __last)
4795 return;
4796
4797 std::__introselect(__first, __nth, __last,
4798 std::__lg(__last - __first) * 2,
4799 __comp);
4800 }
4801
4802 /**
4803 * @brief Sort the elements of a sequence.
4804 * @ingroup sorting_algorithms
4805 * @param __first An iterator.
4806 * @param __last Another iterator.
4807 * @return Nothing.
4808 *
4809 * Sorts the elements in the range `[__first, __last)` in ascending order,
4810 * such that for each iterator `i` in the range `[__first, __last - 1)`,
4811 * `*(i+1) < *i` is false.
4812 *
4813 * The relative ordering of equivalent elements is not preserved, use
4814 * `stable_sort()` if this is needed.
4815 */
4816 template<typename _RandomAccessIterator>
4817 _GLIBCXX20_CONSTEXPR
4818 inline void
4819 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4820 {
4821 // concept requirements
4822 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4823 _RandomAccessIterator>)
4824 __glibcxx_function_requires(_LessThanComparableConcept<
4826 __glibcxx_requires_valid_range(__first, __last);
4827 __glibcxx_requires_irreflexive(__first, __last);
4828
4829 std::__sort(__first, __last, __gnu_cxx::__ops::less());
4830 }
4831
4832 /**
4833 * @brief Sort the elements of a sequence using a predicate for comparison.
4834 * @ingroup sorting_algorithms
4835 * @param __first An iterator.
4836 * @param __last Another iterator.
4837 * @param __comp A comparison functor.
4838 * @return Nothing.
4839 *
4840 * Sorts the elements in the range `[__first, __last)` in ascending order,
4841 * such that `__comp(*(i+1), *i)` is false for every iterator `i` in the
4842 * range `[__first, __last - 1)`.
4843 *
4844 * The relative ordering of equivalent elements is not preserved, use
4845 * `stable_sort()` if this is needed.
4846 */
4847 template<typename _RandomAccessIterator, typename _Compare>
4848 _GLIBCXX20_CONSTEXPR
4849 inline void
4850 sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
4851 _Compare __comp)
4852 {
4853 // concept requirements
4854 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4855 _RandomAccessIterator>)
4856 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4859 __glibcxx_requires_valid_range(__first, __last);
4860 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
4861
4862 std::__sort(__first, __last, __comp);
4863 }
4864
4865 template<typename _InputIterator1, typename _InputIterator2,
4866 typename _OutputIterator, typename _Compare>
4867 _GLIBCXX20_CONSTEXPR
4868 _OutputIterator
4869 __merge(_InputIterator1 __first1, _InputIterator1 __last1,
4870 _InputIterator2 __first2, _InputIterator2 __last2,
4871 _OutputIterator __result, _Compare __comp)
4872 {
4873 while (__first1 != __last1 && __first2 != __last2)
4874 {
4875 if (__comp(*__first2, *__first1))
4876 {
4877 *__result = *__first2;
4878 ++__first2;
4879 }
4880 else
4881 {
4882 *__result = *__first1;
4883 ++__first1;
4884 }
4885 ++__result;
4886 }
4887 return std::copy(__first2, __last2,
4888 std::copy(__first1, __last1, __result));
4889 }
4890
4891 /**
4892 * @brief Merges two sorted ranges.
4893 * @ingroup sorting_algorithms
4894 * @param __first1 An iterator.
4895 * @param __first2 Another iterator.
4896 * @param __last1 Another iterator.
4897 * @param __last2 Another iterator.
4898 * @param __result An iterator pointing to the end of the merged range.
4899 * @return An output iterator equal to @p __result + (__last1 - __first1)
4900 * + (__last2 - __first2).
4901 *
4902 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4903 * the sorted range @p [__result, __result + (__last1-__first1) +
4904 * (__last2-__first2)). Both input ranges must be sorted, and the
4905 * output range must not overlap with either of the input ranges.
4906 * The sort is @e stable, that is, for equivalent elements in the
4907 * two ranges, elements from the first range will always come
4908 * before elements from the second.
4909 */
4910 template<typename _InputIterator1, typename _InputIterator2,
4911 typename _OutputIterator>
4912 _GLIBCXX20_CONSTEXPR
4913 inline _OutputIterator
4914 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4915 _InputIterator2 __first2, _InputIterator2 __last2,
4916 _OutputIterator __result)
4917 {
4918 // concept requirements
4919 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4920 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4921 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4923 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4925 __glibcxx_function_requires(_LessThanOpConcept<
4928 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
4929 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
4930 __glibcxx_requires_irreflexive2(__first1, __last1);
4931 __glibcxx_requires_irreflexive2(__first2, __last2);
4932
4933 return _GLIBCXX_STD_A::__merge(__first1, __last1, __first2, __last2,
4934 __result, __gnu_cxx::__ops::less());
4935 }
4936
4937 /**
4938 * @brief Merges two sorted ranges.
4939 * @ingroup sorting_algorithms
4940 * @param __first1 An iterator.
4941 * @param __first2 Another iterator.
4942 * @param __last1 Another iterator.
4943 * @param __last2 Another iterator.
4944 * @param __result An iterator pointing to the end of the merged range.
4945 * @param __comp A functor to use for comparisons.
4946 * @return An output iterator equal to @p __result + (__last1 - __first1)
4947 * + (__last2 - __first2).
4948 *
4949 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4950 * the sorted range @p [__result, __result + (__last1-__first1) +
4951 * (__last2-__first2)). Both input ranges must be sorted, and the
4952 * output range must not overlap with either of the input ranges.
4953 * The sort is @e stable, that is, for equivalent elements in the
4954 * two ranges, elements from the first range will always come
4955 * before elements from the second.
4956 *
4957 * The comparison function should have the same effects on ordering as
4958 * the function used for the initial sort.
4959 */
4960 template<typename _InputIterator1, typename _InputIterator2,
4961 typename _OutputIterator, typename _Compare>
4962 _GLIBCXX20_CONSTEXPR
4963 inline _OutputIterator
4964 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4965 _InputIterator2 __first2, _InputIterator2 __last2,
4966 _OutputIterator __result, _Compare __comp)
4967 {
4968 // concept requirements
4969 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4970 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4971 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4973 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4975 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4978 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
4979 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
4980 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
4981 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
4982
4983 return _GLIBCXX_STD_A::__merge(__first1, __last1, __first2, __last2,
4984 __result, __comp);
4985 }
4986
4987 template<typename _RandomAccessIterator, typename _Compare>
4988 _GLIBCXX26_CONSTEXPR
4989 inline void
4990 __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
4991 _Compare __comp)
4992 {
4993 typedef typename iterator_traits<_RandomAccessIterator>::value_type
4994 _ValueType;
4995 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
4996 _DistanceType;
4997
4998 if (__first == __last)
4999 return;
5000
5001#if _GLIBCXX_HOSTED
5002# if __glibcxx_constexpr_algorithms >= 202306L // >= C++26
5003 if consteval {
5004 return std::__inplace_stable_sort(__first, __last, __comp);
5005 }
5006# endif
5007
5009 // __stable_sort_adaptive sorts the range in two halves,
5010 // so the buffer only needs to fit half the range at once.
5011 _TmpBuf __buf(__first, (__last - __first + 1) / 2);
5012
5013 if (__builtin_expect(__buf._M_requested_size() == __buf.size(), true))
5014 std::__stable_sort_adaptive(__first,
5015 __first + _DistanceType(__buf.size()),
5016 __last, __buf.begin(), __comp);
5017 else if (__builtin_expect(__buf.begin() == 0, false))
5018 std::__inplace_stable_sort(__first, __last, __comp);
5019 else
5020 std::__stable_sort_adaptive_resize(__first, __last, __buf.begin(),
5021 _DistanceType(__buf.size()), __comp);
5022#else
5023 std::__inplace_stable_sort(__first, __last, __comp);
5024#endif
5025 }
5026
5027 /**
5028 * @brief Sort the elements of a sequence, preserving the relative order
5029 * of equivalent elements.
5030 * @ingroup sorting_algorithms
5031 * @param __first An iterator.
5032 * @param __last Another iterator.
5033 * @return Nothing.
5034 *
5035 * Sorts the elements in the range @p [__first,__last) in ascending order,
5036 * such that for each iterator @p i in the range @p [__first,__last-1),
5037 * @p *(i+1)<*i is false.
5038 *
5039 * The relative ordering of equivalent elements is preserved, so any two
5040 * elements @p x and @p y in the range @p [__first,__last) such that
5041 * @p x<y is false and @p y<x is false will have the same relative
5042 * ordering after calling @p stable_sort().
5043 */
5044 template<typename _RandomAccessIterator>
5045 _GLIBCXX26_CONSTEXPR
5046 inline void
5047 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
5048 {
5049 // concept requirements
5050 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5051 _RandomAccessIterator>)
5052 __glibcxx_function_requires(_LessThanComparableConcept<
5054 __glibcxx_requires_valid_range(__first, __last);
5055 __glibcxx_requires_irreflexive(__first, __last);
5056
5057 _GLIBCXX_STD_A::__stable_sort(__first, __last,
5058 __gnu_cxx::__ops::less());
5059 }
5060
5061 /**
5062 * @brief Sort the elements of a sequence using a predicate for comparison,
5063 * preserving the relative order of equivalent elements.
5064 * @ingroup sorting_algorithms
5065 * @param __first An iterator.
5066 * @param __last Another iterator.
5067 * @param __comp A comparison functor.
5068 * @return Nothing.
5069 *
5070 * Sorts the elements in the range @p [__first,__last) in ascending order,
5071 * such that for each iterator @p i in the range @p [__first,__last-1),
5072 * @p __comp(*(i+1),*i) is false.
5073 *
5074 * The relative ordering of equivalent elements is preserved, so any two
5075 * elements @p x and @p y in the range @p [__first,__last) such that
5076 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
5077 * relative ordering after calling @p stable_sort().
5078 */
5079 template<typename _RandomAccessIterator, typename _Compare>
5080 _GLIBCXX26_CONSTEXPR
5081 inline void
5082 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
5083 _Compare __comp)
5084 {
5085 // concept requirements
5086 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5087 _RandomAccessIterator>)
5088 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5091 __glibcxx_requires_valid_range(__first, __last);
5092 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
5093
5094 _GLIBCXX_STD_A::__stable_sort(__first, __last, __comp);
5095 }
5096
5097 template<typename _InputIterator1, typename _InputIterator2,
5098 typename _OutputIterator, typename _Compare>
5099 _GLIBCXX20_CONSTEXPR
5100 _OutputIterator
5101 __set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5102 _InputIterator2 __first2, _InputIterator2 __last2,
5103 _OutputIterator __result, _Compare __comp)
5104 {
5105 while (__first1 != __last1 && __first2 != __last2)
5106 {
5107 if (__comp(*__first1, *__first2))
5108 {
5109 *__result = *__first1;
5110 ++__first1;
5111 }
5112 else if (__comp(*__first2, *__first1))
5113 {
5114 *__result = *__first2;
5115 ++__first2;
5116 }
5117 else
5118 {
5119 *__result = *__first1;
5120 ++__first1;
5121 ++__first2;
5122 }
5123 ++__result;
5124 }
5125 return std::copy(__first2, __last2,
5126 std::copy(__first1, __last1, __result));
5127 }
5128
5129 /**
5130 * @brief Return the union of two sorted ranges.
5131 * @ingroup set_algorithms
5132 * @param __first1 Start of first range.
5133 * @param __last1 End of first range.
5134 * @param __first2 Start of second range.
5135 * @param __last2 End of second range.
5136 * @param __result Start of output range.
5137 * @return End of the output range.
5138 * @ingroup set_algorithms
5139 *
5140 * This operation iterates over both ranges, copying elements present in
5141 * each range in order to the output range. Iterators increment for each
5142 * range. When the current element of one range is less than the other,
5143 * that element is copied and the iterator advanced. If an element is
5144 * contained in both ranges, the element from the first range is copied and
5145 * both ranges advance. The output range may not overlap either input
5146 * range.
5147 */
5148 template<typename _InputIterator1, typename _InputIterator2,
5149 typename _OutputIterator>
5150 _GLIBCXX20_CONSTEXPR
5151 inline _OutputIterator
5152 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5153 _InputIterator2 __first2, _InputIterator2 __last2,
5154 _OutputIterator __result)
5155 {
5156 // concept requirements
5157 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5158 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5159 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5161 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5163 __glibcxx_function_requires(_LessThanOpConcept<
5166 __glibcxx_function_requires(_LessThanOpConcept<
5169 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5170 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5171 __glibcxx_requires_irreflexive2(__first1, __last1);
5172 __glibcxx_requires_irreflexive2(__first2, __last2);
5173
5174 return _GLIBCXX_STD_A::__set_union(__first1, __last1, __first2, __last2,
5175 __result, __gnu_cxx::__ops::less());
5176 }
5177
5178 /**
5179 * @brief Return the union of two sorted ranges using a comparison functor.
5180 * @ingroup set_algorithms
5181 * @param __first1 Start of first range.
5182 * @param __last1 End of first range.
5183 * @param __first2 Start of second range.
5184 * @param __last2 End of second range.
5185 * @param __result Start of output range.
5186 * @param __comp The comparison functor.
5187 * @return End of the output range.
5188 * @ingroup set_algorithms
5189 *
5190 * This operation iterates over both ranges, copying elements present in
5191 * each range in order to the output range. Iterators increment for each
5192 * range. When the current element of one range is less than the other
5193 * according to @p __comp, that element is copied and the iterator advanced.
5194 * If an equivalent element according to @p __comp is contained in both
5195 * ranges, the element from the first range is copied and both ranges
5196 * advance. The output range may not overlap either input range.
5197 */
5198 template<typename _InputIterator1, typename _InputIterator2,
5199 typename _OutputIterator, typename _Compare>
5200 _GLIBCXX20_CONSTEXPR
5201 inline _OutputIterator
5202 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5203 _InputIterator2 __first2, _InputIterator2 __last2,
5204 _OutputIterator __result, _Compare __comp)
5205 {
5206 // concept requirements
5207 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5208 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5209 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5211 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5213 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5216 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5219 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5220 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5221 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
5222 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
5223
5224 return _GLIBCXX_STD_A::__set_union(__first1, __last1, __first2, __last2,
5225 __result, __comp);
5226 }
5227
5228 template<typename _InputIterator1, typename _InputIterator2,
5229 typename _OutputIterator, typename _Compare>
5230 _GLIBCXX20_CONSTEXPR
5231 _OutputIterator
5232 __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5233 _InputIterator2 __first2, _InputIterator2 __last2,
5234 _OutputIterator __result, _Compare __comp)
5235 {
5236 while (__first1 != __last1 && __first2 != __last2)
5237 if (__comp(*__first1, *__first2))
5238 ++__first1;
5239 else if (__comp(*__first2, *__first1))
5240 ++__first2;
5241 else
5242 {
5243 *__result = *__first1;
5244 ++__first1;
5245 ++__first2;
5246 ++__result;
5247 }
5248 return __result;
5249 }
5250
5251 /**
5252 * @brief Return the intersection of two sorted ranges.
5253 * @ingroup set_algorithms
5254 * @param __first1 Start of first range.
5255 * @param __last1 End of first range.
5256 * @param __first2 Start of second range.
5257 * @param __last2 End of second range.
5258 * @param __result Start of output range.
5259 * @return End of the output range.
5260 * @ingroup set_algorithms
5261 *
5262 * This operation iterates over both ranges, copying elements present in
5263 * both ranges in order to the output range. Iterators increment for each
5264 * range. When the current element of one range is less than the other,
5265 * that iterator advances. If an element is contained in both ranges, the
5266 * element from the first range is copied and both ranges advance. The
5267 * output range may not overlap either input range.
5268 */
5269 template<typename _InputIterator1, typename _InputIterator2,
5270 typename _OutputIterator>
5271 _GLIBCXX20_CONSTEXPR
5272 inline _OutputIterator
5273 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5274 _InputIterator2 __first2, _InputIterator2 __last2,
5275 _OutputIterator __result)
5276 {
5277 // concept requirements
5278 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5279 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5280 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5282 __glibcxx_function_requires(_LessThanOpConcept<
5285 __glibcxx_function_requires(_LessThanOpConcept<
5288 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5289 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5290 __glibcxx_requires_irreflexive2(__first1, __last1);
5291 __glibcxx_requires_irreflexive2(__first2, __last2);
5292
5293 return _GLIBCXX_STD_A::
5294 __set_intersection(__first1, __last1, __first2, __last2,
5295 __result, __gnu_cxx::__ops::less());
5296 }
5297
5298 /**
5299 * @brief Return the intersection of two sorted ranges using comparison
5300 * functor.
5301 * @ingroup set_algorithms
5302 * @param __first1 Start of first range.
5303 * @param __last1 End of first range.
5304 * @param __first2 Start of second range.
5305 * @param __last2 End of second range.
5306 * @param __result Start of output range.
5307 * @param __comp The comparison functor.
5308 * @return End of the output range.
5309 * @ingroup set_algorithms
5310 *
5311 * This operation iterates over both ranges, copying elements present in
5312 * both ranges in order to the output range. Iterators increment for each
5313 * range. When the current element of one range is less than the other
5314 * according to @p __comp, that iterator advances. If an element is
5315 * contained in both ranges according to @p __comp, the element from the
5316 * first range is copied and both ranges advance. The output range may not
5317 * overlap either input range.
5318 */
5319 template<typename _InputIterator1, typename _InputIterator2,
5320 typename _OutputIterator, typename _Compare>
5321 _GLIBCXX20_CONSTEXPR
5322 inline _OutputIterator
5323 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5324 _InputIterator2 __first2, _InputIterator2 __last2,
5325 _OutputIterator __result, _Compare __comp)
5326 {
5327 // concept requirements
5328 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5329 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5330 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5332 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5335 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5338 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5339 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5340 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
5341 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
5342
5343 return _GLIBCXX_STD_A::
5344 __set_intersection(__first1, __last1, __first2, __last2,
5345 __result, __comp);
5346 }
5347
5348 template<typename _InputIterator1, typename _InputIterator2,
5349 typename _OutputIterator, typename _Compare>
5350 _GLIBCXX20_CONSTEXPR
5351 _OutputIterator
5352 __set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5353 _InputIterator2 __first2, _InputIterator2 __last2,
5354 _OutputIterator __result, _Compare __comp)
5355 {
5356 while (__first1 != __last1 && __first2 != __last2)
5357 if (__comp(*__first1, *__first2))
5358 {
5359 *__result = *__first1;
5360 ++__first1;
5361 ++__result;
5362 }
5363 else if (__comp(*__first2, *__first1))
5364 ++__first2;
5365 else
5366 {
5367 ++__first1;
5368 ++__first2;
5369 }
5370 return std::copy(__first1, __last1, __result);
5371 }
5372
5373 /**
5374 * @brief Return the difference of two sorted ranges.
5375 * @ingroup set_algorithms
5376 * @param __first1 Start of first range.
5377 * @param __last1 End of first range.
5378 * @param __first2 Start of second range.
5379 * @param __last2 End of second range.
5380 * @param __result Start of output range.
5381 * @return End of the output range.
5382 * @ingroup set_algorithms
5383 *
5384 * This operation iterates over both ranges, copying elements present in
5385 * the first range but not the second in order to the output range.
5386 * Iterators increment for each range. When the current element of the
5387 * first range is less than the second, that element is copied and the
5388 * iterator advances. If the current element of the second range is less,
5389 * the iterator advances, but no element is copied. If an element is
5390 * contained in both ranges, no elements are copied and both ranges
5391 * advance. The output range may not overlap either input range.
5392 */
5393 template<typename _InputIterator1, typename _InputIterator2,
5394 typename _OutputIterator>
5395 _GLIBCXX20_CONSTEXPR
5396 inline _OutputIterator
5397 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5398 _InputIterator2 __first2, _InputIterator2 __last2,
5399 _OutputIterator __result)
5400 {
5401 // concept requirements
5402 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5403 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5404 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5406 __glibcxx_function_requires(_LessThanOpConcept<
5409 __glibcxx_function_requires(_LessThanOpConcept<
5412 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5413 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5414 __glibcxx_requires_irreflexive2(__first1, __last1);
5415 __glibcxx_requires_irreflexive2(__first2, __last2);
5416
5417 return _GLIBCXX_STD_A::
5418 __set_difference(__first1, __last1, __first2, __last2, __result,
5419 __gnu_cxx::__ops::less());
5420 }
5421
5422 /**
5423 * @brief Return the difference of two sorted ranges using comparison
5424 * functor.
5425 * @ingroup set_algorithms
5426 * @param __first1 Start of first range.
5427 * @param __last1 End of first range.
5428 * @param __first2 Start of second range.
5429 * @param __last2 End of second range.
5430 * @param __result Start of output range.
5431 * @param __comp The comparison functor.
5432 * @return End of the output range.
5433 * @ingroup set_algorithms
5434 *
5435 * This operation iterates over both ranges, copying elements present in
5436 * the first range but not the second in order to the output range.
5437 * Iterators increment for each range. When the current element of the
5438 * first range is less than the second according to @p __comp, that element
5439 * is copied and the iterator advances. If the current element of the
5440 * second range is less, no element is copied and the iterator advances.
5441 * If an element is contained in both ranges according to @p __comp, no
5442 * elements are copied and both ranges advance. The output range may not
5443 * overlap either input range.
5444 */
5445 template<typename _InputIterator1, typename _InputIterator2,
5446 typename _OutputIterator, typename _Compare>
5447 _GLIBCXX20_CONSTEXPR
5448 inline _OutputIterator
5449 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5450 _InputIterator2 __first2, _InputIterator2 __last2,
5451 _OutputIterator __result, _Compare __comp)
5452 {
5453 // concept requirements
5454 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5455 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5456 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5458 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5461 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5464 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5465 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5466 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
5467 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
5468
5469 return _GLIBCXX_STD_A::
5470 __set_difference(__first1, __last1, __first2, __last2, __result,
5471 __comp);
5472 }
5473
5474 template<typename _InputIterator1, typename _InputIterator2,
5475 typename _OutputIterator,
5476 typename _Compare>
5477 _GLIBCXX20_CONSTEXPR
5478 _OutputIterator
5479 __set_symmetric_difference(_InputIterator1 __first1,
5480 _InputIterator1 __last1,
5481 _InputIterator2 __first2,
5482 _InputIterator2 __last2,
5483 _OutputIterator __result,
5484 _Compare __comp)
5485 {
5486 while (__first1 != __last1 && __first2 != __last2)
5487 if (__comp(*__first1, *__first2))
5488 {
5489 *__result = *__first1;
5490 ++__first1;
5491 ++__result;
5492 }
5493 else if (__comp(*__first2, *__first1))
5494 {
5495 *__result = *__first2;
5496 ++__first2;
5497 ++__result;
5498 }
5499 else
5500 {
5501 ++__first1;
5502 ++__first2;
5503 }
5504 return std::copy(__first2, __last2,
5505 std::copy(__first1, __last1, __result));
5506 }
5507
5508 /**
5509 * @brief Return the symmetric difference of two sorted ranges.
5510 * @ingroup set_algorithms
5511 * @param __first1 Start of first range.
5512 * @param __last1 End of first range.
5513 * @param __first2 Start of second range.
5514 * @param __last2 End of second range.
5515 * @param __result Start of output range.
5516 * @return End of the output range.
5517 * @ingroup set_algorithms
5518 *
5519 * This operation iterates over both ranges, copying elements present in
5520 * one range but not the other in order to the output range. Iterators
5521 * increment for each range. When the current element of one range is less
5522 * than the other, that element is copied and the iterator advances. If an
5523 * element is contained in both ranges, no elements are copied and both
5524 * ranges advance. The output range may not overlap either input range.
5525 */
5526 template<typename _InputIterator1, typename _InputIterator2,
5527 typename _OutputIterator>
5528 _GLIBCXX20_CONSTEXPR
5529 inline _OutputIterator
5530 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5531 _InputIterator2 __first2, _InputIterator2 __last2,
5532 _OutputIterator __result)
5533 {
5534 // concept requirements
5535 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5536 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5537 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5539 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5541 __glibcxx_function_requires(_LessThanOpConcept<
5544 __glibcxx_function_requires(_LessThanOpConcept<
5547 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5548 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5549 __glibcxx_requires_irreflexive2(__first1, __last1);
5550 __glibcxx_requires_irreflexive2(__first2, __last2);
5551
5552 return _GLIBCXX_STD_A::
5553 __set_symmetric_difference(__first1, __last1, __first2, __last2,
5554 __result, __gnu_cxx::__ops::less());
5555 }
5556
5557 /**
5558 * @brief Return the symmetric difference of two sorted ranges using
5559 * comparison functor.
5560 * @ingroup set_algorithms
5561 * @param __first1 Start of first range.
5562 * @param __last1 End of first range.
5563 * @param __first2 Start of second range.
5564 * @param __last2 End of second range.
5565 * @param __result Start of output range.
5566 * @param __comp The comparison functor.
5567 * @return End of the output range.
5568 * @ingroup set_algorithms
5569 *
5570 * This operation iterates over both ranges, copying elements present in
5571 * one range but not the other in order to the output range. Iterators
5572 * increment for each range. When the current element of one range is less
5573 * than the other according to @p comp, that element is copied and the
5574 * iterator advances. If an element is contained in both ranges according
5575 * to @p __comp, no elements are copied and both ranges advance. The output
5576 * range may not overlap either input range.
5577 */
5578 template<typename _InputIterator1, typename _InputIterator2,
5579 typename _OutputIterator, typename _Compare>
5580 _GLIBCXX20_CONSTEXPR
5581 inline _OutputIterator
5582 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5583 _InputIterator2 __first2, _InputIterator2 __last2,
5584 _OutputIterator __result,
5585 _Compare __comp)
5586 {
5587 // concept requirements
5588 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5589 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5590 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5592 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5594 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5597 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5600 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5601 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5602 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
5603 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
5604
5605 return _GLIBCXX_STD_A::
5606 __set_symmetric_difference(__first1, __last1, __first2, __last2,
5607 __result, __comp);
5608 }
5609
5610 template<typename _ForwardIterator, typename _Compare>
5611 _GLIBCXX14_CONSTEXPR
5612 _ForwardIterator
5613 __min_element(_ForwardIterator __first, _ForwardIterator __last,
5614 _Compare __comp)
5615 {
5616 if (__first == __last)
5617 return __first;
5618 _ForwardIterator __result = __first;
5619 while (++__first != __last)
5620 if (__comp(*__first, *__result))
5621 __result = __first;
5622 return __result;
5623 }
5624
5625 /**
5626 * @brief Return the minimum element in a range.
5627 * @ingroup sorting_algorithms
5628 * @param __first Start of range.
5629 * @param __last End of range.
5630 * @return Iterator referencing the first instance of the smallest value.
5631 */
5632 template<typename _ForwardIterator>
5633 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
5634 inline _ForwardIterator
5635 min_element(_ForwardIterator __first, _ForwardIterator __last)
5636 {
5637 // concept requirements
5638 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5639 __glibcxx_function_requires(_LessThanComparableConcept<
5641 __glibcxx_requires_valid_range(__first, __last);
5642 __glibcxx_requires_irreflexive(__first, __last);
5643
5644 return _GLIBCXX_STD_A::__min_element(__first, __last,
5645 __gnu_cxx::__ops::less());
5646 }
5647
5648 /**
5649 * @brief Return the minimum element in a range using comparison functor.
5650 * @ingroup sorting_algorithms
5651 * @param __first Start of range.
5652 * @param __last End of range.
5653 * @param __comp Comparison functor.
5654 * @return Iterator referencing the first instance of the smallest value
5655 * according to __comp.
5656 */
5657 template<typename _ForwardIterator, typename _Compare>
5658 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
5659 inline _ForwardIterator
5660 min_element(_ForwardIterator __first, _ForwardIterator __last,
5661 _Compare __comp)
5662 {
5663 // concept requirements
5664 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5665 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5668 __glibcxx_requires_valid_range(__first, __last);
5669 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
5670
5671 return _GLIBCXX_STD_A::__min_element(__first, __last, __comp);
5672 }
5673
5674 template<typename _ForwardIterator, typename _Compare>
5675 _GLIBCXX14_CONSTEXPR
5676 _ForwardIterator
5677 __max_element(_ForwardIterator __first, _ForwardIterator __last,
5678 _Compare __comp)
5679 {
5680 if (__first == __last) return __first;
5681 _ForwardIterator __result = __first;
5682 while (++__first != __last)
5683 if (__comp(*__result, *__first))
5684 __result = __first;
5685 return __result;
5686 }
5687
5688 /**
5689 * @brief Return the maximum element in a range.
5690 * @ingroup sorting_algorithms
5691 * @param __first Start of range.
5692 * @param __last End of range.
5693 * @return Iterator referencing the first instance of the largest value.
5694 */
5695 template<typename _ForwardIterator>
5696 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
5697 inline _ForwardIterator
5698 max_element(_ForwardIterator __first, _ForwardIterator __last)
5699 {
5700 // concept requirements
5701 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5702 __glibcxx_function_requires(_LessThanComparableConcept<
5704 __glibcxx_requires_valid_range(__first, __last);
5705 __glibcxx_requires_irreflexive(__first, __last);
5706
5707 return _GLIBCXX_STD_A::__max_element(__first, __last,
5708 __gnu_cxx::__ops::less());
5709 }
5710
5711 /**
5712 * @brief Return the maximum element in a range using comparison functor.
5713 * @ingroup sorting_algorithms
5714 * @param __first Start of range.
5715 * @param __last End of range.
5716 * @param __comp Comparison functor.
5717 * @return Iterator referencing the first instance of the largest value
5718 * according to __comp.
5719 */
5720 template<typename _ForwardIterator, typename _Compare>
5721 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
5722 inline _ForwardIterator
5723 max_element(_ForwardIterator __first, _ForwardIterator __last,
5724 _Compare __comp)
5725 {
5726 // concept requirements
5727 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5728 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5731 __glibcxx_requires_valid_range(__first, __last);
5732 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
5733
5734 return _GLIBCXX_STD_A::__max_element(__first, __last, __comp);
5735 }
5736
5737#if __cplusplus >= 201103L
5738 // N2722 + DR 915.
5739 template<typename _Tp>
5740 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
5741 inline _Tp
5742 min(initializer_list<_Tp> __l)
5743 {
5744 __glibcxx_requires_irreflexive(__l.begin(), __l.end());
5745 return *_GLIBCXX_STD_A::__min_element(__l.begin(), __l.end(),
5746 __gnu_cxx::__ops::less());
5747 }
5748
5749 template<typename _Tp, typename _Compare>
5750 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
5751 inline _Tp
5752 min(initializer_list<_Tp> __l, _Compare __comp)
5753 {
5754 __glibcxx_requires_irreflexive_pred(__l.begin(), __l.end(), __comp);
5755 return *_GLIBCXX_STD_A::__min_element(__l.begin(), __l.end(), __comp);
5756 }
5757
5758 template<typename _Tp>
5759 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
5760 inline _Tp
5762 {
5763 __glibcxx_requires_irreflexive(__l.begin(), __l.end());
5764 return *_GLIBCXX_STD_A::__max_element(__l.begin(), __l.end(),
5765 __gnu_cxx::__ops::less());
5766 }
5767
5768 template<typename _Tp, typename _Compare>
5769 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
5770 inline _Tp
5771 max(initializer_list<_Tp> __l, _Compare __comp)
5772 {
5773 __glibcxx_requires_irreflexive_pred(__l.begin(), __l.end(), __comp);
5774 return *_GLIBCXX_STD_A::__max_element(__l.begin(), __l.end(), __comp);
5775 }
5776#endif // C++11
5777
5778#if __cplusplus >= 201402L // C++17 std::sample and C++14 experimental::sample
5779 /// Reservoir sampling algorithm.
5780 template<typename _InputIterator, typename _RandomAccessIterator,
5781 typename _Size, typename _UniformRandomBitGenerator>
5782 _RandomAccessIterator
5783 __sample(_InputIterator __first, _InputIterator __last, input_iterator_tag,
5784 _RandomAccessIterator __out, random_access_iterator_tag,
5785 _Size __n, _UniformRandomBitGenerator&& __g)
5786 {
5787 using __distrib_type = uniform_int_distribution<_Size>;
5788 using __param_type = typename __distrib_type::param_type;
5789 __distrib_type __d{};
5790 _Size __sample_sz = 0;
5791 while (__first != __last && __sample_sz != __n)
5792 {
5793 __out[__sample_sz++] = *__first;
5794 ++__first;
5795 }
5796 for (auto __pop_sz = __sample_sz; __first != __last;
5797 ++__first, (void) ++__pop_sz)
5798 {
5799 const auto __k = __d(__g, __param_type{0, __pop_sz});
5800 if (__k < __n)
5801 __out[__k] = *__first;
5802 }
5803 return __out + __sample_sz;
5804 }
5805
5806 /// Selection sampling algorithm.
5807 template<typename _ForwardIterator, typename _OutputIterator, typename _Cat,
5808 typename _Size, typename _UniformRandomBitGenerator>
5809 _OutputIterator
5810 __sample(_ForwardIterator __first, _ForwardIterator __last,
5812 _OutputIterator __out, _Cat,
5813 _Size __n, _UniformRandomBitGenerator&& __g)
5814 {
5815 using __distrib_type = uniform_int_distribution<_Size>;
5816 using __param_type = typename __distrib_type::param_type;
5817 using _USize = make_unsigned_t<_Size>;
5820
5821 if (__first == __last)
5822 return __out;
5823
5824 __distrib_type __d{};
5825 _Size __unsampled_sz = std::distance(__first, __last);
5826 __n = std::min(__n, __unsampled_sz);
5827
5828 // If possible, we use __gen_two_uniform_ints to efficiently produce
5829 // two random numbers using a single distribution invocation:
5830
5831 const __uc_type __urngrange = __g.max() - __g.min();
5832 if (__urngrange / __uc_type(__unsampled_sz) >= __uc_type(__unsampled_sz))
5833 // I.e. (__urngrange >= __unsampled_sz * __unsampled_sz) but without
5834 // wrapping issues.
5835 {
5836 while (__n != 0 && __unsampled_sz >= 2)
5837 {
5838 const pair<_Size, _Size> __p =
5839 __gen_two_uniform_ints(__unsampled_sz, __unsampled_sz - 1, __g);
5840
5841 --__unsampled_sz;
5842 if (__p.first < __n)
5843 {
5844 *__out++ = *__first;
5845 --__n;
5846 }
5847
5848 ++__first;
5849
5850 if (__n == 0) break;
5851
5852 --__unsampled_sz;
5853 if (__p.second < __n)
5854 {
5855 *__out++ = *__first;
5856 --__n;
5857 }
5858
5859 ++__first;
5860 }
5861 }
5862
5863 // The loop above is otherwise equivalent to this one-at-a-time version:
5864
5865 for (; __n != 0; ++__first)
5866 if (__d(__g, __param_type{0, --__unsampled_sz}) < __n)
5867 {
5868 *__out++ = *__first;
5869 --__n;
5870 }
5871 return __out;
5872 }
5873#endif // C++14
5874
5875#ifdef __glibcxx_sample // C++ >= 17
5876 /// Take a random sample from a population.
5877 template<typename _PopulationIterator, typename _SampleIterator,
5878 typename _Distance, typename _UniformRandomBitGenerator>
5879 _SampleIterator
5880 sample(_PopulationIterator __first, _PopulationIterator __last,
5881 _SampleIterator __out, _Distance __n,
5882 _UniformRandomBitGenerator&& __g)
5883 {
5884 using __pop_cat = typename
5886 using __samp_cat = typename
5888
5889 static_assert(
5890 __or_<is_convertible<__pop_cat, forward_iterator_tag>,
5891 is_convertible<__samp_cat, random_access_iterator_tag>>::value,
5892 "output range must use a RandomAccessIterator when input range"
5893 " does not meet the ForwardIterator requirements");
5894
5895 static_assert(is_integral<_Distance>::value,
5896 "sample size must be an integer type");
5897
5899 return _GLIBCXX_STD_A::
5900 __sample(__first, __last, __pop_cat{}, __out, __samp_cat{}, __d,
5902 }
5903#endif // __glibcxx_sample
5904
5905_GLIBCXX_END_NAMESPACE_ALGO
5906_GLIBCXX_END_NAMESPACE_VERSION
5907} // namespace std
5908
5909#pragma GCC diagnostic pop
5910
5911#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:1843
typename common_type< _Tp... >::type common_type_t
Alias template for common_type.
Definition type_traits:2905
typename make_unsigned< _Tp >::type make_unsigned_t
Alias template for make_unsigned.
Definition type_traits:2203
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:3800
constexpr const _Tp & clamp(const _Tp &, const _Tp &, const _Tp &)
Returns the value clamped between lo and hi.
Definition stl_algo.h:3618
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:5783
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:3668
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:5880
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:486
common_type
Definition type_traits:2530
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.
Traits class for iterators.
Uniform discrete distribution for random numbers. A discrete random distribution on the range with e...