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