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 - _DistanceType(__count); // Success
208 }
209 __remainder = __count + 1 - (__first - __backTrack);
210 }
211 return __last; // Failure
212 }
213
214 template<typename _ForwardIterator, typename _Integer,
215 typename _UnaryPredicate>
216 _GLIBCXX20_CONSTEXPR
217 _ForwardIterator
218 __search_n(_ForwardIterator __first, _ForwardIterator __last,
219 _Integer __count,
220 _UnaryPredicate __unary_pred)
221 {
222 if (__count <= 0)
223 return __first;
224
225 if (__count == 1)
226 return std::__find_if(__first, __last, __unary_pred);
227
228 return std::__search_n_aux(__first, __last, __count, __unary_pred,
229 std::__iterator_category(__first));
230 }
231
232 // find_end for forward iterators.
233 template<typename _ForwardIterator1, typename _ForwardIterator2,
234 typename _BinaryPredicate>
235 _GLIBCXX20_CONSTEXPR
236 _ForwardIterator1
237 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
238 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
240 _BinaryPredicate __comp)
241 {
242 if (__first2 == __last2)
243 return __last1;
244
245 _ForwardIterator1 __result = __last1;
246 while (1)
247 {
248 _ForwardIterator1 __new_result
249 = std::__search(__first1, __last1, __first2, __last2, __comp);
250 if (__new_result == __last1)
251 return __result;
252 else
253 {
254 __result = __new_result;
255 __first1 = __new_result;
256 ++__first1;
257 }
258 }
259 }
260
261 // find_end for bidirectional iterators (much faster).
262 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
263 typename _BinaryPredicate>
264 _GLIBCXX20_CONSTEXPR
265 _BidirectionalIterator1
266 __find_end(_BidirectionalIterator1 __first1,
267 _BidirectionalIterator1 __last1,
268 _BidirectionalIterator2 __first2,
269 _BidirectionalIterator2 __last2,
271 _BinaryPredicate __comp)
272 {
273 // concept requirements
274 __glibcxx_function_requires(_BidirectionalIteratorConcept<
275 _BidirectionalIterator1>)
276 __glibcxx_function_requires(_BidirectionalIteratorConcept<
277 _BidirectionalIterator2>)
278
279 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
280 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
281
282 _RevIterator1 __rlast1(__first1);
283 _RevIterator2 __rlast2(__first2);
284 _RevIterator1 __rresult = std::__search(_RevIterator1(__last1), __rlast1,
285 _RevIterator2(__last2), __rlast2,
286 __comp);
287
288 if (__rresult == __rlast1)
289 return __last1;
290 else
291 {
292 _BidirectionalIterator1 __result = __rresult.base();
293 std::advance(__result, -std::distance(__first2, __last2));
294 return __result;
295 }
296 }
297
298 /**
299 * @brief Find last matching subsequence in a sequence.
300 * @ingroup non_mutating_algorithms
301 * @param __first1 Start of range to search.
302 * @param __last1 End of range to search.
303 * @param __first2 Start of sequence to match.
304 * @param __last2 End of sequence to match.
305 * @return The last iterator @c i in the range
306 * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) ==
307 * @p *(__first2+N) for each @c N in the range @p
308 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
309 *
310 * Searches the range @p [__first1,__last1) for a sub-sequence that
311 * compares equal value-by-value with the sequence given by @p
312 * [__first2,__last2) and returns an iterator to the __first
313 * element of the sub-sequence, or @p __last1 if the sub-sequence
314 * is not found. The sub-sequence will be the last such
315 * subsequence contained in [__first1,__last1).
316 *
317 * Because the sub-sequence must lie completely within the range @p
318 * [__first1,__last1) it must start at a position less than @p
319 * __last1-(__last2-__first2) where @p __last2-__first2 is the
320 * length of the sub-sequence. This means that the returned
321 * iterator @c i will be in the range @p
322 * [__first1,__last1-(__last2-__first2))
323 */
324 template<typename _ForwardIterator1, typename _ForwardIterator2>
325 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
326 inline _ForwardIterator1
327 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
328 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
329 {
330 // concept requirements
331 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
332 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
333 __glibcxx_function_requires(_EqualOpConcept<
336 __glibcxx_requires_valid_range(__first1, __last1);
337 __glibcxx_requires_valid_range(__first2, __last2);
338
339 return std::__find_end(__first1, __last1, __first2, __last2,
340 std::__iterator_category(__first1),
341 std::__iterator_category(__first2),
342 __gnu_cxx::__ops::__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 _RandomAccessIterator __mid = __p + _Distance(__n - 1);
1262 _RandomAccessIterator __end = __mid;
1263 ++__end;
1264 _ValueType __t = _GLIBCXX_MOVE(*__p);
1265 _GLIBCXX_MOVE3(__p + _Distance(1), __end, __p);
1266 *__mid = _GLIBCXX_MOVE(__t);
1267 return __ret;
1268 }
1269 _RandomAccessIterator __q = __p + __k;
1270 for (_Distance __i = 0; __i < __n - __k; ++ __i)
1271 {
1272 std::iter_swap(__p, __q);
1273 ++__p;
1274 ++__q;
1275 }
1276 __n = static_cast<_UDistance>(__n) % static_cast<_UDistance>(__k);
1277 if (__n == 0)
1278 return __ret;
1279 std::swap(__n, __k);
1280 __k = __n - __k;
1281 }
1282 else
1283 {
1284 __k = __n - __k;
1285 if (__is_pod(_ValueType) && __k == 1)
1286 {
1287 _RandomAccessIterator __mid = __p + _Distance(__n - 1);
1288 _RandomAccessIterator __end = __mid;
1289 ++__end;
1290 _ValueType __t = _GLIBCXX_MOVE(*__mid);
1291 _GLIBCXX_MOVE_BACKWARD3(__p, __mid, __end);
1292 *__p = _GLIBCXX_MOVE(__t);
1293 return __ret;
1294 }
1295 _RandomAccessIterator __q = __p + __n;
1296 __p = __q - __k;
1297 for (_Distance __i = 0; __i < __n - __k; ++ __i)
1298 {
1299 --__p;
1300 --__q;
1301 std::iter_swap(__p, __q);
1302 }
1303 __n = static_cast<_UDistance>(__n) % static_cast<_UDistance>(__k);
1304 if (__n == 0)
1305 return __ret;
1306 std::swap(__n, __k);
1307 }
1308 }
1309 }
1310
1311 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1312 // DR 488. rotate throws away useful information
1313 /**
1314 * @brief Rotate the elements of a sequence.
1315 * @ingroup mutating_algorithms
1316 * @param __first A forward iterator.
1317 * @param __middle A forward iterator.
1318 * @param __last A forward iterator.
1319 * @return first + (last - middle).
1320 *
1321 * Rotates the elements of the range @p [__first,__last) by
1322 * @p (__middle - __first) positions so that the element at @p __middle
1323 * is moved to @p __first, the element at @p __middle+1 is moved to
1324 * @p __first+1 and so on for each element in the range
1325 * @p [__first,__last).
1326 *
1327 * This effectively swaps the ranges @p [__first,__middle) and
1328 * @p [__middle,__last).
1329 *
1330 * Performs
1331 * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1332 * for each @p n in the range @p [0,__last-__first).
1333 */
1334 template<typename _ForwardIterator>
1335 _GLIBCXX20_CONSTEXPR
1336 inline _ForwardIterator
1337 rotate(_ForwardIterator __first, _ForwardIterator __middle,
1338 _ForwardIterator __last)
1339 {
1340 // concept requirements
1341 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1342 _ForwardIterator>)
1343 __glibcxx_requires_valid_range(__first, __middle);
1344 __glibcxx_requires_valid_range(__middle, __last);
1345
1346 return std::__rotate(__first, __middle, __last,
1347 std::__iterator_category(__first));
1348 }
1349
1350_GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2)
1351
1352 /**
1353 * @brief Copy a sequence, rotating its elements.
1354 * @ingroup mutating_algorithms
1355 * @param __first A forward iterator.
1356 * @param __middle A forward iterator.
1357 * @param __last A forward iterator.
1358 * @param __result An output iterator.
1359 * @return An iterator designating the end of the resulting sequence.
1360 *
1361 * Copies the elements of the range @p [__first,__last) to the
1362 * range beginning at @result, rotating the copied elements by
1363 * @p (__middle-__first) positions so that the element at @p __middle
1364 * is moved to @p __result, the element at @p __middle+1 is moved
1365 * to @p __result+1 and so on for each element in the range @p
1366 * [__first,__last).
1367 *
1368 * Performs
1369 * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n)
1370 * for each @p n in the range @p [0,__last-__first).
1371 */
1372 template<typename _ForwardIterator, typename _OutputIterator>
1373 _GLIBCXX20_CONSTEXPR
1374 inline _OutputIterator
1375 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle,
1376 _ForwardIterator __last, _OutputIterator __result)
1377 {
1378 // concept requirements
1379 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1380 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1382 __glibcxx_requires_valid_range(__first, __middle);
1383 __glibcxx_requires_valid_range(__middle, __last);
1384
1385 return std::copy(__first, __middle,
1386 std::copy(__middle, __last, __result));
1387 }
1388
1389 /// This is a helper function...
1390 template<typename _ForwardIterator, typename _Predicate>
1391 _GLIBCXX20_CONSTEXPR
1392 _ForwardIterator
1393 __partition(_ForwardIterator __first, _ForwardIterator __last,
1394 _Predicate __pred, forward_iterator_tag)
1395 {
1396 if (__first == __last)
1397 return __first;
1398
1399 while (__pred(*__first))
1400 if (++__first == __last)
1401 return __first;
1402
1403 _ForwardIterator __next = __first;
1404
1405 while (++__next != __last)
1406 if (__pred(*__next))
1407 {
1408 std::iter_swap(__first, __next);
1409 ++__first;
1410 }
1411
1412 return __first;
1413 }
1414
1415 /// This is a helper function...
1416 template<typename _BidirectionalIterator, typename _Predicate>
1417 _GLIBCXX20_CONSTEXPR
1418 _BidirectionalIterator
1419 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last,
1420 _Predicate __pred, bidirectional_iterator_tag)
1421 {
1422 while (true)
1423 {
1424 while (true)
1425 if (__first == __last)
1426 return __first;
1427 else if (__pred(*__first))
1428 ++__first;
1429 else
1430 break;
1431 --__last;
1432 while (true)
1433 if (__first == __last)
1434 return __first;
1435 else if (!bool(__pred(*__last)))
1436 --__last;
1437 else
1438 break;
1439 std::iter_swap(__first, __last);
1440 ++__first;
1441 }
1442 }
1443
1444#if _GLIBCXX_HOSTED
1445 // partition
1446
1447 /// This is a helper function...
1448 /// Requires __first != __last and !__pred(__first)
1449 /// and __len == distance(__first, __last).
1450 ///
1451 /// !__pred(__first) allows us to guarantee that we don't
1452 /// move-assign an element onto itself.
1453 template<typename _ForwardIterator, typename _Pointer, typename _Predicate,
1454 typename _Distance>
1455 _GLIBCXX26_CONSTEXPR
1456 _ForwardIterator
1457 __stable_partition_adaptive(_ForwardIterator __first,
1458 _ForwardIterator __last,
1459 _Predicate __pred, _Distance __len,
1460 _Pointer __buffer,
1461 _Distance __buffer_size)
1462 {
1463 if (__len == 1)
1464 return __first;
1465
1466 if (__len <= __buffer_size)
1467 {
1468 _ForwardIterator __result1 = __first;
1469 _Pointer __result2 = __buffer;
1470
1471 // The precondition guarantees that !__pred(__first), so
1472 // move that element to the buffer before starting the loop.
1473 // This ensures that we only call __pred once per element.
1474 *__result2 = _GLIBCXX_MOVE(*__first);
1475 ++__result2;
1476 ++__first;
1477 for (; __first != __last; ++__first)
1478 if (__pred(__first))
1479 {
1480 *__result1 = _GLIBCXX_MOVE(*__first);
1481 ++__result1;
1482 }
1483 else
1484 {
1485 *__result2 = _GLIBCXX_MOVE(*__first);
1486 ++__result2;
1487 }
1488
1489 _GLIBCXX_MOVE3(__buffer, __result2, __result1);
1490 return __result1;
1491 }
1492
1493 _ForwardIterator __middle = __first;
1494 std::advance(__middle, __len / 2);
1495 _ForwardIterator __left_split =
1496 std::__stable_partition_adaptive(__first, __middle, __pred,
1497 __len / 2, __buffer,
1498 __buffer_size);
1499
1500 // Advance past true-predicate values to satisfy this
1501 // function's preconditions.
1502 _Distance __right_len = __len - __len / 2;
1503 _ForwardIterator __right_split =
1504 std::__find_if_not_n(__middle, __right_len, __pred);
1505
1506 if (__right_len)
1507 __right_split =
1508 std::__stable_partition_adaptive(__right_split, __last, __pred,
1509 __right_len,
1510 __buffer, __buffer_size);
1511
1512 return std::rotate(__left_split, __middle, __right_split);
1513 }
1514
1515 template<typename _ForwardIterator, typename _Predicate>
1516 _GLIBCXX26_CONSTEXPR
1517 _ForwardIterator
1518 __stable_partition(_ForwardIterator __first, _ForwardIterator __last,
1519 _Predicate __pred)
1520 {
1521 __first = std::__find_if_not(__first, __last, __pred);
1522
1523 if (__first == __last)
1524 return __first;
1525
1526 typedef typename iterator_traits<_ForwardIterator>::value_type
1527 _ValueType;
1528 typedef typename iterator_traits<_ForwardIterator>::difference_type
1529 _DistanceType;
1530
1531 const _DistanceType __len = std::distance(__first, __last);
1532
1533#if __glibcxx_constexpr_algorithms >= 202306L // >= C++26
1534 if consteval {
1535 // Simulate a _Temporary_buffer of length 1:
1536 _ValueType __buf = std::move(*__first);
1537 *__first = std::move(__buf);
1538 return std::__stable_partition_adaptive(__first, __last, __pred,
1539 __len,
1540 &__buf,
1541 _DistanceType(1));
1542 }
1543#endif
1544
1546 __buf(__first, __len);
1547 return
1548 std::__stable_partition_adaptive(__first, __last, __pred,
1549 __len,
1550 __buf.begin(),
1551 _DistanceType(__buf.size()));
1552 }
1553
1554 /**
1555 * @brief Move elements for which a predicate is true to the beginning
1556 * of a sequence, preserving relative ordering.
1557 * @ingroup mutating_algorithms
1558 * @param __first A forward iterator.
1559 * @param __last A forward iterator.
1560 * @param __pred A predicate functor.
1561 * @return An iterator @p middle such that @p __pred(i) is true for each
1562 * iterator @p i in the range @p [first,middle) and false for each @p i
1563 * in the range @p [middle,last).
1564 *
1565 * Performs the same function as @p partition() with the additional
1566 * guarantee that the relative ordering of elements in each group is
1567 * preserved, so any two elements @p x and @p y in the range
1568 * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same
1569 * relative ordering after calling @p stable_partition().
1570 */
1571 template<typename _ForwardIterator, typename _Predicate>
1572 _GLIBCXX26_CONSTEXPR
1573 inline _ForwardIterator
1574 stable_partition(_ForwardIterator __first, _ForwardIterator __last,
1575 _Predicate __pred)
1576 {
1577 // concept requirements
1578 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1579 _ForwardIterator>)
1580 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1582 __glibcxx_requires_valid_range(__first, __last);
1583
1584 return std::__stable_partition(__first, __last,
1585 __gnu_cxx::__ops::__pred_iter(__pred));
1586 }
1587#endif // HOSTED
1588
1589 /// @cond undocumented
1590
1591 /// This is a helper function for the sort routines.
1592 template<typename _RandomAccessIterator, typename _Compare>
1593 _GLIBCXX20_CONSTEXPR
1594 void
1595 __heap_select(_RandomAccessIterator __first,
1596 _RandomAccessIterator __middle,
1597 _RandomAccessIterator __last, _Compare __comp)
1598 {
1599 std::__make_heap(__first, __middle, __comp);
1600 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
1601 if (__comp(__i, __first))
1602 std::__pop_heap(__first, __middle, __i, __comp);
1603 }
1604
1605 // partial_sort
1606
1607 template<typename _InputIterator, typename _RandomAccessIterator,
1608 typename _Compare>
1609 _GLIBCXX20_CONSTEXPR
1610 _RandomAccessIterator
1611 __partial_sort_copy(_InputIterator __first, _InputIterator __last,
1612 _RandomAccessIterator __result_first,
1613 _RandomAccessIterator __result_last,
1614 _Compare __comp)
1615 {
1617 _InputValueType;
1618 typedef iterator_traits<_RandomAccessIterator> _RItTraits;
1619 typedef typename _RItTraits::difference_type _DistanceType;
1620
1621 if (__result_first == __result_last)
1622 return __result_last;
1623 _RandomAccessIterator __result_real_last = __result_first;
1624 while (__first != __last && __result_real_last != __result_last)
1625 {
1626 *__result_real_last = *__first;
1627 ++__result_real_last;
1628 ++__first;
1629 }
1630
1631 std::__make_heap(__result_first, __result_real_last, __comp);
1632 while (__first != __last)
1633 {
1634 if (__comp(__first, __result_first))
1635 std::__adjust_heap(__result_first, _DistanceType(0),
1636 _DistanceType(__result_real_last
1637 - __result_first),
1638 _InputValueType(*__first), __comp);
1639 ++__first;
1640 }
1641 std::__sort_heap(__result_first, __result_real_last, __comp);
1642 return __result_real_last;
1643 }
1644
1645 /// @endcond
1646
1647 /**
1648 * @brief Copy the smallest elements of a sequence.
1649 * @ingroup sorting_algorithms
1650 * @param __first An iterator.
1651 * @param __last Another iterator.
1652 * @param __result_first A random-access iterator.
1653 * @param __result_last Another random-access iterator.
1654 * @return An iterator indicating the end of the resulting sequence.
1655 *
1656 * Copies and sorts the smallest `N` values from the range
1657 * `[__first, __last)` to the range beginning at `__result_first`, where
1658 * the number of elements to be copied, `N`, is the smaller of
1659 * `(__last - __first)` and `(__result_last - __result_first)`.
1660 * After the sort if `i` and `j` are iterators in the range
1661 * `[__result_first,__result_first + N)` such that `i` precedes `j` then
1662 * `*j < *i` is false.
1663 * The value returned is `__result_first + N`.
1664 */
1665 template<typename _InputIterator, typename _RandomAccessIterator>
1666 _GLIBCXX20_CONSTEXPR
1667 inline _RandomAccessIterator
1668 partial_sort_copy(_InputIterator __first, _InputIterator __last,
1669 _RandomAccessIterator __result_first,
1670 _RandomAccessIterator __result_last)
1671 {
1672#ifdef _GLIBCXX_CONCEPT_CHECKS
1674 _InputValueType;
1676 _OutputValueType;
1677#endif
1678
1679 // concept requirements
1680 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1681 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
1682 _OutputValueType>)
1683 __glibcxx_function_requires(_LessThanOpConcept<_InputValueType,
1684 _OutputValueType>)
1685 __glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>)
1686 __glibcxx_requires_valid_range(__first, __last);
1687 __glibcxx_requires_irreflexive(__first, __last);
1688 __glibcxx_requires_valid_range(__result_first, __result_last);
1689
1690 return std::__partial_sort_copy(__first, __last,
1691 __result_first, __result_last,
1692 __gnu_cxx::__ops::__iter_less_iter());
1693 }
1694
1695 /**
1696 * @brief Copy the smallest elements of a sequence using a predicate for
1697 * comparison.
1698 * @ingroup sorting_algorithms
1699 * @param __first An input iterator.
1700 * @param __last Another input iterator.
1701 * @param __result_first A random-access iterator.
1702 * @param __result_last Another random-access iterator.
1703 * @param __comp A comparison functor.
1704 * @return An iterator indicating the end of the resulting sequence.
1705 *
1706 * Copies and sorts the smallest `N` values from the range
1707 * `[__first, __last)` to the range beginning at `result_first`, where
1708 * the number of elements to be copied, `N`, is the smaller of
1709 * `(__last - __first)` and `(__result_last - __result_first)`.
1710 * After the sort if `i` and `j` are iterators in the range
1711 * `[__result_first, __result_first + N)` such that `i` precedes `j` then
1712 * `__comp(*j, *i)` is false.
1713 * The value returned is `__result_first + N`.
1714 */
1715 template<typename _InputIterator, typename _RandomAccessIterator,
1716 typename _Compare>
1717 _GLIBCXX20_CONSTEXPR
1718 inline _RandomAccessIterator
1719 partial_sort_copy(_InputIterator __first, _InputIterator __last,
1720 _RandomAccessIterator __result_first,
1721 _RandomAccessIterator __result_last,
1722 _Compare __comp)
1723 {
1724#ifdef _GLIBCXX_CONCEPT_CHECKS
1726 _InputValueType;
1728 _OutputValueType;
1729#endif
1730
1731 // concept requirements
1732 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1733 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
1734 _RandomAccessIterator>)
1735 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
1736 _OutputValueType>)
1737 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
1738 _InputValueType, _OutputValueType>)
1739 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
1740 _OutputValueType, _OutputValueType>)
1741 __glibcxx_requires_valid_range(__first, __last);
1742 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
1743 __glibcxx_requires_valid_range(__result_first, __result_last);
1744
1745 return std::__partial_sort_copy(__first, __last,
1746 __result_first, __result_last,
1747 __gnu_cxx::__ops::__iter_comp_iter(__comp));
1748 }
1749
1750 /// @cond undocumented
1751
1752 /// This is a helper function for the sort routine.
1753 template<typename _RandomAccessIterator, typename _Compare>
1754 _GLIBCXX20_CONSTEXPR
1755 void
1756 __unguarded_linear_insert(_RandomAccessIterator __last,
1757 _Compare __comp)
1758 {
1759 typename iterator_traits<_RandomAccessIterator>::value_type
1760 __val = _GLIBCXX_MOVE(*__last);
1761 _RandomAccessIterator __next = __last;
1762 --__next;
1763 while (__comp(__val, __next))
1764 {
1765 *__last = _GLIBCXX_MOVE(*__next);
1766 __last = __next;
1767 --__next;
1768 }
1769 *__last = _GLIBCXX_MOVE(__val);
1770 }
1771
1772 /// This is a helper function for the sort routine.
1773 template<typename _RandomAccessIterator, typename _Compare>
1774 _GLIBCXX20_CONSTEXPR
1775 void
1776 __insertion_sort(_RandomAccessIterator __first,
1777 _RandomAccessIterator __last, _Compare __comp)
1778 {
1779 if (__first == __last)
1780 return;
1781
1782 typedef iterator_traits<_RandomAccessIterator> _IterTraits;
1783 typedef typename _IterTraits::difference_type _Dist;
1784
1785 for (_RandomAccessIterator __i = __first + _Dist(1); __i != __last; ++__i)
1786 {
1787 if (__comp(__i, __first))
1788 {
1789 typename _IterTraits::value_type __val = _GLIBCXX_MOVE(*__i);
1790 _GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + _Dist(1));
1791 *__first = _GLIBCXX_MOVE(__val);
1792 }
1793 else
1794 std::__unguarded_linear_insert(__i,
1795 __gnu_cxx::__ops::__val_comp_iter(__comp));
1796 }
1797 }
1798
1799 /// This is a helper function for the sort routine.
1800 template<typename _RandomAccessIterator, typename _Compare>
1801 _GLIBCXX20_CONSTEXPR
1802 inline void
1803 __unguarded_insertion_sort(_RandomAccessIterator __first,
1804 _RandomAccessIterator __last, _Compare __comp)
1805 {
1806 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
1807 std::__unguarded_linear_insert(__i,
1808 __gnu_cxx::__ops::__val_comp_iter(__comp));
1809 }
1810
1811 /**
1812 * @doctodo
1813 * This controls some aspect of the sort routines.
1814 */
1815 enum { _S_threshold = 16 };
1816
1817 /// This is a helper function for the sort routine.
1818 template<typename _RandomAccessIterator, typename _Compare>
1819 _GLIBCXX20_CONSTEXPR
1820 void
1821 __final_insertion_sort(_RandomAccessIterator __first,
1822 _RandomAccessIterator __last, _Compare __comp)
1823 {
1825 __threshold = _S_threshold;
1826
1827 if (__last - __first > __threshold)
1828 {
1829 std::__insertion_sort(__first, __first + __threshold, __comp);
1830 std::__unguarded_insertion_sort(__first + __threshold, __last,
1831 __comp);
1832 }
1833 else
1834 std::__insertion_sort(__first, __last, __comp);
1835 }
1836
1837 /// This is a helper function...
1838 template<typename _RandomAccessIterator, typename _Compare>
1839 _GLIBCXX20_CONSTEXPR
1840 _RandomAccessIterator
1841 __unguarded_partition(_RandomAccessIterator __first,
1842 _RandomAccessIterator __last,
1843 _RandomAccessIterator __pivot, _Compare __comp)
1844 {
1845 while (true)
1846 {
1847 while (__comp(__first, __pivot))
1848 ++__first;
1849 --__last;
1850 while (__comp(__pivot, __last))
1851 --__last;
1852 if (!(__first < __last))
1853 return __first;
1854 std::iter_swap(__first, __last);
1855 ++__first;
1856 }
1857 }
1858
1859 /// This is a helper function...
1860 template<typename _RandomAccessIterator, typename _Compare>
1861 _GLIBCXX20_CONSTEXPR
1862 inline _RandomAccessIterator
1863 __unguarded_partition_pivot(_RandomAccessIterator __first,
1864 _RandomAccessIterator __last, _Compare __comp)
1865 {
1866 typedef iterator_traits<_RandomAccessIterator> _IterTraits;
1867 typedef typename _IterTraits::difference_type _Dist;
1868
1869 _RandomAccessIterator __mid = __first + _Dist((__last - __first) / 2);
1870 _RandomAccessIterator __second = __first + _Dist(1);
1871 std::__move_median_to_first(__first, __second, __mid, __last - _Dist(1),
1872 __comp);
1873 return std::__unguarded_partition(__second, __last, __first, __comp);
1874 }
1875
1876 template<typename _RandomAccessIterator, typename _Compare>
1877 _GLIBCXX20_CONSTEXPR
1878 inline void
1879 __partial_sort(_RandomAccessIterator __first,
1880 _RandomAccessIterator __middle,
1881 _RandomAccessIterator __last,
1882 _Compare __comp)
1883 {
1884 std::__heap_select(__first, __middle, __last, __comp);
1885 std::__sort_heap(__first, __middle, __comp);
1886 }
1887
1888 /// This is a helper function for the sort routine.
1889 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
1890 _GLIBCXX20_CONSTEXPR
1891 void
1892 __introsort_loop(_RandomAccessIterator __first,
1893 _RandomAccessIterator __last,
1894 _Size __depth_limit, _Compare __comp)
1895 {
1896 while (__last - __first > int(_S_threshold))
1897 {
1898 if (__depth_limit == 0)
1899 {
1900 std::__partial_sort(__first, __last, __last, __comp);
1901 return;
1902 }
1903 --__depth_limit;
1904 _RandomAccessIterator __cut =
1905 std::__unguarded_partition_pivot(__first, __last, __comp);
1906 std::__introsort_loop(__cut, __last, __depth_limit, __comp);
1907 __last = __cut;
1908 }
1909 }
1910
1911 // sort
1912
1913 template<typename _RandomAccessIterator, typename _Compare>
1914 _GLIBCXX20_CONSTEXPR
1915 inline void
1916 __sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
1917 _Compare __comp)
1918 {
1919 if (__first != __last)
1920 {
1921 std::__introsort_loop(__first, __last,
1922 std::__lg(__last - __first) * 2,
1923 __comp);
1924 std::__final_insertion_sort(__first, __last, __comp);
1925 }
1926 }
1927
1928 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
1929 _GLIBCXX20_CONSTEXPR
1930 void
1931 __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
1932 _RandomAccessIterator __last, _Size __depth_limit,
1933 _Compare __comp)
1934 {
1935 _RandomAccessIterator __after_nth = __nth;
1936 ++__after_nth;
1937
1938 while (__last - __first > 3)
1939 {
1940 if (__depth_limit == 0)
1941 {
1942 std::__heap_select(__first, __after_nth, __last, __comp);
1943 // Place the nth largest element in its final position.
1944 std::iter_swap(__first, __nth);
1945 return;
1946 }
1947 --__depth_limit;
1948 _RandomAccessIterator __cut =
1949 std::__unguarded_partition_pivot(__first, __last, __comp);
1950 if (__cut <= __nth)
1951 __first = __cut;
1952 else
1953 __last = __cut;
1954 }
1955 std::__insertion_sort(__first, __last, __comp);
1956 }
1957
1958 /// @endcond
1959
1960 // nth_element
1961
1962 // lower_bound moved to stl_algobase.h
1963
1964 /**
1965 * @brief Finds the first position in which `__val` could be inserted
1966 * without changing the ordering.
1967 * @ingroup binary_search_algorithms
1968 * @param __first An iterator to the start of a sorted range.
1969 * @param __last A past-the-end iterator for the sorted range.
1970 * @param __val The search term.
1971 * @param __comp A functor to use for comparisons.
1972 * @return An iterator pointing to the first element _not less than_
1973 * `__val`, or `end()` if every element is less than `__val`.
1974 * @ingroup binary_search_algorithms
1975 *
1976 * The comparison function should have the same effects on ordering as
1977 * the function used for the initial sort.
1978 */
1979 template<typename _ForwardIterator, typename _Tp, typename _Compare>
1980 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1981 inline _ForwardIterator
1982 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
1983 const _Tp& __val, _Compare __comp)
1984 {
1985 // concept requirements
1986 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1987 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
1989 __glibcxx_requires_partitioned_lower_pred(__first, __last,
1990 __val, __comp);
1991
1992 return std::__lower_bound(__first, __last, __val,
1993 __gnu_cxx::__ops::__iter_comp_val(__comp));
1994 }
1995
1996 template<typename _ForwardIterator, typename _Tp, typename _Compare>
1997 _GLIBCXX20_CONSTEXPR
1998 _ForwardIterator
1999 __upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2000 const _Tp& __val, _Compare __comp)
2001 {
2002 typedef typename iterator_traits<_ForwardIterator>::difference_type
2003 _DistanceType;
2004
2005 _DistanceType __len = std::distance(__first, __last);
2006
2007 while (__len > 0)
2008 {
2009 _DistanceType __half = __len >> 1;
2010 _ForwardIterator __middle = __first;
2011 std::advance(__middle, __half);
2012 if (__comp(__val, __middle))
2013 __len = __half;
2014 else
2015 {
2016 __first = __middle;
2017 ++__first;
2018 __len = __len - __half - 1;
2019 }
2020 }
2021 return __first;
2022 }
2023
2024 /**
2025 * @brief Finds the last position in which @p __val could be inserted
2026 * without changing the ordering.
2027 * @ingroup binary_search_algorithms
2028 * @param __first An iterator.
2029 * @param __last Another iterator.
2030 * @param __val The search term.
2031 * @return An iterator pointing to the first element greater than @p __val,
2032 * or end() if no elements are greater than @p __val.
2033 * @ingroup binary_search_algorithms
2034 */
2035 template<typename _ForwardIterator, typename _Tp>
2036 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2037 inline _ForwardIterator
2038 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2039 const _Tp& __val)
2040 {
2041 // concept requirements
2042 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2043 __glibcxx_function_requires(_LessThanOpConcept<
2045 __glibcxx_requires_partitioned_upper(__first, __last, __val);
2046
2047 return std::__upper_bound(__first, __last, __val,
2048 __gnu_cxx::__ops::__val_less_iter());
2049 }
2050
2051 /**
2052 * @brief Finds the last position in which @p __val could be inserted
2053 * without changing the ordering.
2054 * @ingroup binary_search_algorithms
2055 * @param __first An iterator.
2056 * @param __last Another iterator.
2057 * @param __val The search term.
2058 * @param __comp A functor to use for comparisons.
2059 * @return An iterator pointing to the first element greater than @p __val,
2060 * or end() if no elements are greater than @p __val.
2061 * @ingroup binary_search_algorithms
2062 *
2063 * The comparison function should have the same effects on ordering as
2064 * the function used for the initial sort.
2065 */
2066 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2067 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2068 inline _ForwardIterator
2069 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2070 const _Tp& __val, _Compare __comp)
2071 {
2072 // concept requirements
2073 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2074 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2076 __glibcxx_requires_partitioned_upper_pred(__first, __last,
2077 __val, __comp);
2078
2079 return std::__upper_bound(__first, __last, __val,
2080 __gnu_cxx::__ops::__val_comp_iter(__comp));
2081 }
2082
2083 template<typename _ForwardIterator, typename _Tp,
2084 typename _CompareItTp, typename _CompareTpIt>
2085 _GLIBCXX20_CONSTEXPR
2087 __equal_range(_ForwardIterator __first, _ForwardIterator __last,
2088 const _Tp& __val,
2089 _CompareItTp __comp_it_val, _CompareTpIt __comp_val_it)
2090 {
2091 typedef typename iterator_traits<_ForwardIterator>::difference_type
2092 _DistanceType;
2093
2094 _DistanceType __len = std::distance(__first, __last);
2095
2096 while (__len > 0)
2097 {
2098 _DistanceType __half = __len >> 1;
2099 _ForwardIterator __middle = __first;
2100 std::advance(__middle, __half);
2101 if (__comp_it_val(__middle, __val))
2102 {
2103 __first = __middle;
2104 ++__first;
2105 __len = __len - __half - 1;
2106 }
2107 else if (__comp_val_it(__val, __middle))
2108 __len = __half;
2109 else
2110 {
2111 _ForwardIterator __left
2112 = std::__lower_bound(__first, __middle, __val, __comp_it_val);
2113 std::advance(__first, __len);
2114 _ForwardIterator __right
2115 = std::__upper_bound(++__middle, __first, __val, __comp_val_it);
2116 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
2117 }
2118 }
2119 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
2120 }
2121
2122 /**
2123 * @brief Finds the largest subrange in which @p __val could be inserted
2124 * at any place in it without changing the ordering.
2125 * @ingroup binary_search_algorithms
2126 * @param __first An iterator.
2127 * @param __last Another iterator.
2128 * @param __val The search term.
2129 * @return An pair of iterators defining the subrange.
2130 * @ingroup binary_search_algorithms
2131 *
2132 * This is equivalent to
2133 * @code
2134 * std::make_pair(lower_bound(__first, __last, __val),
2135 * upper_bound(__first, __last, __val))
2136 * @endcode
2137 * but does not actually call those functions.
2138 */
2139 template<typename _ForwardIterator, typename _Tp>
2140 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2142 equal_range(_ForwardIterator __first, _ForwardIterator __last,
2143 const _Tp& __val)
2144 {
2145 // concept requirements
2146 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2147 __glibcxx_function_requires(_LessThanOpConcept<
2149 __glibcxx_function_requires(_LessThanOpConcept<
2151 __glibcxx_requires_partitioned_lower(__first, __last, __val);
2152 __glibcxx_requires_partitioned_upper(__first, __last, __val);
2153
2154 return std::__equal_range(__first, __last, __val,
2155 __gnu_cxx::__ops::__iter_less_val(),
2156 __gnu_cxx::__ops::__val_less_iter());
2157 }
2158
2159 /**
2160 * @brief Finds the largest subrange in which @p __val could be inserted
2161 * at any place in it without changing the ordering.
2162 * @param __first An iterator.
2163 * @param __last Another iterator.
2164 * @param __val The search term.
2165 * @param __comp A functor to use for comparisons.
2166 * @return An pair of iterators defining the subrange.
2167 * @ingroup binary_search_algorithms
2168 *
2169 * This is equivalent to
2170 * @code
2171 * std::make_pair(lower_bound(__first, __last, __val, __comp),
2172 * upper_bound(__first, __last, __val, __comp))
2173 * @endcode
2174 * but does not actually call those functions.
2175 */
2176 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2177 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2179 equal_range(_ForwardIterator __first, _ForwardIterator __last,
2180 const _Tp& __val, _Compare __comp)
2181 {
2182 // concept requirements
2183 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2184 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2186 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2188 __glibcxx_requires_partitioned_lower_pred(__first, __last,
2189 __val, __comp);
2190 __glibcxx_requires_partitioned_upper_pred(__first, __last,
2191 __val, __comp);
2192
2193 return std::__equal_range(__first, __last, __val,
2194 __gnu_cxx::__ops::__iter_comp_val(__comp),
2195 __gnu_cxx::__ops::__val_comp_iter(__comp));
2196 }
2197
2198 /**
2199 * @brief Determines whether an element exists in a range.
2200 * @ingroup binary_search_algorithms
2201 * @param __first An iterator.
2202 * @param __last Another iterator.
2203 * @param __val The search term.
2204 * @return True if @p __val (or its equivalent) is in [@p
2205 * __first,@p __last ].
2206 *
2207 * Note that this does not actually return an iterator to @p __val. For
2208 * that, use std::find or a container's specialized find member functions.
2209 */
2210 template<typename _ForwardIterator, typename _Tp>
2211 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2212 bool
2213 binary_search(_ForwardIterator __first, _ForwardIterator __last,
2214 const _Tp& __val)
2215 {
2216 // concept requirements
2217 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2218 __glibcxx_function_requires(_LessThanOpConcept<
2220 __glibcxx_requires_partitioned_lower(__first, __last, __val);
2221 __glibcxx_requires_partitioned_upper(__first, __last, __val);
2222
2223 _ForwardIterator __i
2224 = std::__lower_bound(__first, __last, __val,
2225 __gnu_cxx::__ops::__iter_less_val());
2226 return __i != __last && !(__val < *__i);
2227 }
2228
2229 /**
2230 * @brief Determines whether an element exists in a range.
2231 * @ingroup binary_search_algorithms
2232 * @param __first An iterator.
2233 * @param __last Another iterator.
2234 * @param __val The search term.
2235 * @param __comp A functor to use for comparisons.
2236 * @return True if @p __val (or its equivalent) is in @p [__first,__last].
2237 *
2238 * Note that this does not actually return an iterator to @p __val. For
2239 * that, use std::find or a container's specialized find member functions.
2240 *
2241 * The comparison function should have the same effects on ordering as
2242 * the function used for the initial sort.
2243 */
2244 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2245 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2246 bool
2247 binary_search(_ForwardIterator __first, _ForwardIterator __last,
2248 const _Tp& __val, _Compare __comp)
2249 {
2250 // concept requirements
2251 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2252 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2254 __glibcxx_requires_partitioned_lower_pred(__first, __last,
2255 __val, __comp);
2256 __glibcxx_requires_partitioned_upper_pred(__first, __last,
2257 __val, __comp);
2258
2259 _ForwardIterator __i
2260 = std::__lower_bound(__first, __last, __val,
2261 __gnu_cxx::__ops::__iter_comp_val(__comp));
2262 return __i != __last && !bool(__comp(__val, *__i));
2263 }
2264
2265 // merge
2266
2267 /// This is a helper function for the __merge_adaptive routines.
2268 template<typename _InputIterator1, typename _InputIterator2,
2269 typename _OutputIterator, typename _Compare>
2270 void
2271 __move_merge_adaptive(_InputIterator1 __first1, _InputIterator1 __last1,
2272 _InputIterator2 __first2, _InputIterator2 __last2,
2273 _OutputIterator __result, _Compare __comp)
2274 {
2275 while (__first1 != __last1 && __first2 != __last2)
2276 {
2277 if (__comp(__first2, __first1))
2278 {
2279 *__result = _GLIBCXX_MOVE(*__first2);
2280 ++__first2;
2281 }
2282 else
2283 {
2284 *__result = _GLIBCXX_MOVE(*__first1);
2285 ++__first1;
2286 }
2287 ++__result;
2288 }
2289 if (__first1 != __last1)
2290 _GLIBCXX_MOVE3(__first1, __last1, __result);
2291 }
2292
2293 /// This is a helper function for the __merge_adaptive routines.
2294 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2295 typename _BidirectionalIterator3, typename _Compare>
2296 void
2297 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1,
2298 _BidirectionalIterator1 __last1,
2299 _BidirectionalIterator2 __first2,
2300 _BidirectionalIterator2 __last2,
2301 _BidirectionalIterator3 __result,
2302 _Compare __comp)
2303 {
2304 if (__first1 == __last1)
2305 {
2306 _GLIBCXX_MOVE_BACKWARD3(__first2, __last2, __result);
2307 return;
2308 }
2309 else if (__first2 == __last2)
2310 return;
2311
2312 --__last1;
2313 --__last2;
2314 while (true)
2315 {
2316 if (__comp(__last2, __last1))
2317 {
2318 *--__result = _GLIBCXX_MOVE(*__last1);
2319 if (__first1 == __last1)
2320 {
2321 _GLIBCXX_MOVE_BACKWARD3(__first2, ++__last2, __result);
2322 return;
2323 }
2324 --__last1;
2325 }
2326 else
2327 {
2328 *--__result = _GLIBCXX_MOVE(*__last2);
2329 if (__first2 == __last2)
2330 return;
2331 --__last2;
2332 }
2333 }
2334 }
2335
2336 /// This is a helper function for the merge routines.
2337 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
2338 typename _Distance>
2339 _BidirectionalIterator1
2340 __rotate_adaptive(_BidirectionalIterator1 __first,
2341 _BidirectionalIterator1 __middle,
2342 _BidirectionalIterator1 __last,
2343 _Distance __len1, _Distance __len2,
2344 _BidirectionalIterator2 __buffer,
2345 _Distance __buffer_size)
2346 {
2347 _BidirectionalIterator2 __buffer_end;
2348 if (__len1 > __len2 && __len2 <= __buffer_size)
2349 {
2350 if (__len2)
2351 {
2352 __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
2353 _GLIBCXX_MOVE_BACKWARD3(__first, __middle, __last);
2354 return _GLIBCXX_MOVE3(__buffer, __buffer_end, __first);
2355 }
2356 else
2357 return __first;
2358 }
2359 else if (__len1 <= __buffer_size)
2360 {
2361 if (__len1)
2362 {
2363 __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
2364 _GLIBCXX_MOVE3(__middle, __last, __first);
2365 return _GLIBCXX_MOVE_BACKWARD3(__buffer, __buffer_end, __last);
2366 }
2367 else
2368 return __last;
2369 }
2370 else
2371 return std::rotate(__first, __middle, __last);
2372 }
2373
2374 /// This is a helper function for the merge routines.
2375 template<typename _BidirectionalIterator, typename _Distance,
2376 typename _Pointer, typename _Compare>
2377 void
2378 __merge_adaptive(_BidirectionalIterator __first,
2379 _BidirectionalIterator __middle,
2380 _BidirectionalIterator __last,
2381 _Distance __len1, _Distance __len2,
2382 _Pointer __buffer, _Compare __comp)
2383 {
2384 if (__len1 <= __len2)
2385 {
2386 _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
2387 std::__move_merge_adaptive(__buffer, __buffer_end, __middle, __last,
2388 __first, __comp);
2389 }
2390 else
2391 {
2392 _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
2393 std::__move_merge_adaptive_backward(__first, __middle, __buffer,
2394 __buffer_end, __last, __comp);
2395 }
2396 }
2397
2398 template<typename _BidirectionalIterator, typename _Distance,
2399 typename _Pointer, typename _Compare>
2400 void
2401 __merge_adaptive_resize(_BidirectionalIterator __first,
2402 _BidirectionalIterator __middle,
2403 _BidirectionalIterator __last,
2404 _Distance __len1, _Distance __len2,
2405 _Pointer __buffer, _Distance __buffer_size,
2406 _Compare __comp)
2407 {
2408 if (__len1 <= __buffer_size || __len2 <= __buffer_size)
2409 std::__merge_adaptive(__first, __middle, __last,
2410 __len1, __len2, __buffer, __comp);
2411 else
2412 {
2413 _BidirectionalIterator __first_cut = __first;
2414 _BidirectionalIterator __second_cut = __middle;
2415 _Distance __len11 = 0;
2416 _Distance __len22 = 0;
2417 if (__len1 > __len2)
2418 {
2419 __len11 = __len1 / 2;
2420 std::advance(__first_cut, __len11);
2421 __second_cut
2422 = std::__lower_bound(__middle, __last, *__first_cut,
2423 __gnu_cxx::__ops::__iter_comp_val(__comp));
2424 __len22 = std::distance(__middle, __second_cut);
2425 }
2426 else
2427 {
2428 __len22 = __len2 / 2;
2429 std::advance(__second_cut, __len22);
2430 __first_cut
2431 = std::__upper_bound(__first, __middle, *__second_cut,
2432 __gnu_cxx::__ops::__val_comp_iter(__comp));
2433 __len11 = std::distance(__first, __first_cut);
2434 }
2435
2436 _BidirectionalIterator __new_middle
2437 = std::__rotate_adaptive(__first_cut, __middle, __second_cut,
2438 _Distance(__len1 - __len11), __len22,
2439 __buffer, __buffer_size);
2440 std::__merge_adaptive_resize(__first, __first_cut, __new_middle,
2441 __len11, __len22,
2442 __buffer, __buffer_size, __comp);
2443 std::__merge_adaptive_resize(__new_middle, __second_cut, __last,
2444 _Distance(__len1 - __len11),
2445 _Distance(__len2 - __len22),
2446 __buffer, __buffer_size, __comp);
2447 }
2448 }
2449
2450 /// This is a helper function for the merge routines.
2451 template<typename _BidirectionalIterator, typename _Distance,
2452 typename _Compare>
2453 _GLIBCXX26_CONSTEXPR
2454 void
2455 __merge_without_buffer(_BidirectionalIterator __first,
2456 _BidirectionalIterator __middle,
2457 _BidirectionalIterator __last,
2458 _Distance __len1, _Distance __len2,
2459 _Compare __comp)
2460 {
2461 if (__len1 == 0 || __len2 == 0)
2462 return;
2463
2464 if (__len1 + __len2 == 2)
2465 {
2466 if (__comp(__middle, __first))
2467 std::iter_swap(__first, __middle);
2468 return;
2469 }
2470
2471 _BidirectionalIterator __first_cut = __first;
2472 _BidirectionalIterator __second_cut = __middle;
2473 _Distance __len11 = 0;
2474 _Distance __len22 = 0;
2475 if (__len1 > __len2)
2476 {
2477 __len11 = __len1 / 2;
2478 std::advance(__first_cut, __len11);
2479 __second_cut
2480 = std::__lower_bound(__middle, __last, *__first_cut,
2481 __gnu_cxx::__ops::__iter_comp_val(__comp));
2482 __len22 = std::distance(__middle, __second_cut);
2483 }
2484 else
2485 {
2486 __len22 = __len2 / 2;
2487 std::advance(__second_cut, __len22);
2488 __first_cut
2489 = std::__upper_bound(__first, __middle, *__second_cut,
2490 __gnu_cxx::__ops::__val_comp_iter(__comp));
2491 __len11 = std::distance(__first, __first_cut);
2492 }
2493
2494 _BidirectionalIterator __new_middle
2495 = std::rotate(__first_cut, __middle, __second_cut);
2496 std::__merge_without_buffer(__first, __first_cut, __new_middle,
2497 __len11, __len22, __comp);
2498 std::__merge_without_buffer(__new_middle, __second_cut, __last,
2499 __len1 - __len11, __len2 - __len22, __comp);
2500 }
2501
2502 template<typename _BidirectionalIterator, typename _Compare>
2503 _GLIBCXX26_CONSTEXPR
2504 void
2505 __inplace_merge(_BidirectionalIterator __first,
2506 _BidirectionalIterator __middle,
2507 _BidirectionalIterator __last,
2508 _Compare __comp)
2509 {
2510 typedef typename iterator_traits<_BidirectionalIterator>::value_type
2511 _ValueType;
2512 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
2513 _DistanceType;
2514
2515 if (__first == __middle || __middle == __last)
2516 return;
2517
2518 const _DistanceType __len1 = std::distance(__first, __middle);
2519 const _DistanceType __len2 = std::distance(__middle, __last);
2520
2521#if _GLIBCXX_HOSTED
2522# if __glibcxx_constexpr_algorithms >= 202306L // >= C++26
2523 if consteval {
2525 (__first, __middle, __last, __len1, __len2, __comp);
2526 }
2527# endif
2529 // __merge_adaptive will use a buffer for the smaller of
2530 // [first,middle) and [middle,last).
2531 _TmpBuf __buf(__first, std::min(__len1, __len2));
2532
2533 if (__builtin_expect(__buf.size() == __buf._M_requested_size(), true))
2535 (__first, __middle, __last, __len1, __len2, __buf.begin(), __comp);
2536 else if (__builtin_expect(__buf.begin() == 0, false))
2538 (__first, __middle, __last, __len1, __len2, __comp);
2539 else
2540 std::__merge_adaptive_resize
2541 (__first, __middle, __last, __len1, __len2, __buf.begin(),
2542 _DistanceType(__buf.size()), __comp);
2543#else
2545 (__first, __middle, __last, __len1, __len2, __comp);
2546#endif
2547 }
2548
2549 /**
2550 * @brief Merges two sorted ranges in place.
2551 * @ingroup sorting_algorithms
2552 * @param __first An iterator.
2553 * @param __middle Another iterator.
2554 * @param __last Another iterator.
2555 * @return Nothing.
2556 *
2557 * Merges two sorted and consecutive ranges, [__first,__middle) and
2558 * [__middle,__last), and puts the result in [__first,__last). The
2559 * output will be sorted. The sort is @e stable, that is, for
2560 * equivalent elements in the two ranges, elements from the first
2561 * range will always come before elements from the second.
2562 *
2563 * If enough additional memory is available, this takes (__last-__first)-1
2564 * comparisons. Otherwise an NlogN algorithm is used, where N is
2565 * distance(__first,__last).
2566 */
2567 template<typename _BidirectionalIterator>
2568 _GLIBCXX26_CONSTEXPR
2569 inline void
2570 inplace_merge(_BidirectionalIterator __first,
2571 _BidirectionalIterator __middle,
2572 _BidirectionalIterator __last)
2573 {
2574 // concept requirements
2575 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
2576 _BidirectionalIterator>)
2577 __glibcxx_function_requires(_LessThanComparableConcept<
2579 __glibcxx_requires_sorted(__first, __middle);
2580 __glibcxx_requires_sorted(__middle, __last);
2581 __glibcxx_requires_irreflexive(__first, __last);
2582
2583 std::__inplace_merge(__first, __middle, __last,
2584 __gnu_cxx::__ops::__iter_less_iter());
2585 }
2586
2587 /**
2588 * @brief Merges two sorted ranges in place.
2589 * @ingroup sorting_algorithms
2590 * @param __first An iterator.
2591 * @param __middle Another iterator.
2592 * @param __last Another iterator.
2593 * @param __comp A functor to use for comparisons.
2594 * @return Nothing.
2595 *
2596 * Merges two sorted and consecutive ranges, [__first,__middle) and
2597 * [middle,last), and puts the result in [__first,__last). The output will
2598 * be sorted. The sort is @e stable, that is, for equivalent
2599 * elements in the two ranges, elements from the first range will always
2600 * come before elements from the second.
2601 *
2602 * If enough additional memory is available, this takes (__last-__first)-1
2603 * comparisons. Otherwise an NlogN algorithm is used, where N is
2604 * distance(__first,__last).
2605 *
2606 * The comparison function should have the same effects on ordering as
2607 * the function used for the initial sort.
2608 */
2609 template<typename _BidirectionalIterator, typename _Compare>
2610 _GLIBCXX26_CONSTEXPR
2611 inline void
2612 inplace_merge(_BidirectionalIterator __first,
2613 _BidirectionalIterator __middle,
2614 _BidirectionalIterator __last,
2615 _Compare __comp)
2616 {
2617 // concept requirements
2618 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
2619 _BidirectionalIterator>)
2620 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2623 __glibcxx_requires_sorted_pred(__first, __middle, __comp);
2624 __glibcxx_requires_sorted_pred(__middle, __last, __comp);
2625 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
2626
2627 std::__inplace_merge(__first, __middle, __last,
2628 __gnu_cxx::__ops::__iter_comp_iter(__comp));
2629 }
2630
2631
2632 /// This is a helper function for the __merge_sort_loop routines.
2633 template<typename _InputIterator, typename _OutputIterator,
2634 typename _Compare>
2635 _OutputIterator
2636 __move_merge(_InputIterator __first1, _InputIterator __last1,
2637 _InputIterator __first2, _InputIterator __last2,
2638 _OutputIterator __result, _Compare __comp)
2639 {
2640 while (__first1 != __last1 && __first2 != __last2)
2641 {
2642 if (__comp(__first2, __first1))
2643 {
2644 *__result = _GLIBCXX_MOVE(*__first2);
2645 ++__first2;
2646 }
2647 else
2648 {
2649 *__result = _GLIBCXX_MOVE(*__first1);
2650 ++__first1;
2651 }
2652 ++__result;
2653 }
2654 return _GLIBCXX_MOVE3(__first2, __last2,
2655 _GLIBCXX_MOVE3(__first1, __last1,
2656 __result));
2657 }
2658
2659 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
2660 typename _Distance, typename _Compare>
2661 void
2662 __merge_sort_loop(_RandomAccessIterator1 __first,
2663 _RandomAccessIterator1 __last,
2664 _RandomAccessIterator2 __result, _Distance __step_size,
2665 _Compare __comp)
2666 {
2667 const _Distance __two_step = 2 * __step_size;
2668
2669 while (__last - __first >= __two_step)
2670 {
2671 __result = std::__move_merge(__first, __first + __step_size,
2672 __first + __step_size,
2673 __first + __two_step,
2674 __result, __comp);
2675 __first += __two_step;
2676 }
2677 __step_size = std::min(_Distance(__last - __first), __step_size);
2678
2679 std::__move_merge(__first, __first + __step_size,
2680 __first + __step_size, __last, __result, __comp);
2681 }
2682
2683 template<typename _RandomAccessIterator, typename _Distance,
2684 typename _Compare>
2685 _GLIBCXX20_CONSTEXPR
2686 void
2687 __chunk_insertion_sort(_RandomAccessIterator __first,
2688 _RandomAccessIterator __last,
2689 _Distance __chunk_size, _Compare __comp)
2690 {
2691 while (__last - __first >= __chunk_size)
2692 {
2693 std::__insertion_sort(__first, __first + __chunk_size, __comp);
2694 __first += __chunk_size;
2695 }
2696 std::__insertion_sort(__first, __last, __comp);
2697 }
2698
2699 enum { _S_chunk_size = 7 };
2700
2701 template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
2702 void
2703 __merge_sort_with_buffer(_RandomAccessIterator __first,
2704 _RandomAccessIterator __last,
2705 _Pointer __buffer, _Compare __comp)
2706 {
2708 _Distance;
2709
2710 const _Distance __len = __last - __first;
2711 const _Pointer __buffer_last = __buffer + __len;
2712
2713 _Distance __step_size = _S_chunk_size;
2714 std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
2715
2716 while (__step_size < __len)
2717 {
2718 std::__merge_sort_loop(__first, __last, __buffer,
2719 __step_size, __comp);
2720 __step_size *= 2;
2721 std::__merge_sort_loop(__buffer, __buffer_last, __first,
2722 __step_size, __comp);
2723 __step_size *= 2;
2724 }
2725 }
2726
2727 template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
2728 void
2729 __stable_sort_adaptive(_RandomAccessIterator __first,
2730 _RandomAccessIterator __middle,
2731 _RandomAccessIterator __last,
2732 _Pointer __buffer, _Compare __comp)
2733 {
2734 std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
2735 std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
2736
2737 std::__merge_adaptive(__first, __middle, __last,
2738 __middle - __first, __last - __middle,
2739 __buffer, __comp);
2740 }
2741
2742 template<typename _RandomAccessIterator, typename _Pointer,
2743 typename _Distance, typename _Compare>
2744 void
2745 __stable_sort_adaptive_resize(_RandomAccessIterator __first,
2746 _RandomAccessIterator __last,
2747 _Pointer __buffer, _Distance __buffer_size,
2748 _Compare __comp)
2749 {
2750 const _Distance __len = (__last - __first + 1) / 2;
2751 const _RandomAccessIterator __middle = __first + __len;
2752 if (__len > __buffer_size)
2753 {
2754 std::__stable_sort_adaptive_resize(__first, __middle, __buffer,
2755 __buffer_size, __comp);
2756 std::__stable_sort_adaptive_resize(__middle, __last, __buffer,
2757 __buffer_size, __comp);
2758 std::__merge_adaptive_resize(__first, __middle, __last,
2759 _Distance(__middle - __first),
2760 _Distance(__last - __middle),
2761 __buffer, __buffer_size,
2762 __comp);
2763 }
2764 else
2765 std::__stable_sort_adaptive(__first, __middle, __last,
2766 __buffer, __comp);
2767 }
2768
2769 /// This is a helper function for the stable sorting routines.
2770 template<typename _RandomAccessIterator, typename _Compare>
2771 _GLIBCXX26_CONSTEXPR
2772 void
2773 __inplace_stable_sort(_RandomAccessIterator __first,
2774 _RandomAccessIterator __last, _Compare __comp)
2775 {
2776 if (__last - __first < 15)
2777 {
2778 std::__insertion_sort(__first, __last, __comp);
2779 return;
2780 }
2781 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
2782 std::__inplace_stable_sort(__first, __middle, __comp);
2783 std::__inplace_stable_sort(__middle, __last, __comp);
2784 std::__merge_without_buffer(__first, __middle, __last,
2785 __middle - __first,
2786 __last - __middle,
2787 __comp);
2788 }
2789
2790 // stable_sort
2791
2792 // Set algorithms: includes, set_union, set_intersection, set_difference,
2793 // set_symmetric_difference. All of these algorithms have the precondition
2794 // that their input ranges are sorted and the postcondition that their output
2795 // ranges are sorted.
2796
2797 template<typename _InputIterator1, typename _InputIterator2,
2798 typename _Compare>
2799 _GLIBCXX20_CONSTEXPR
2800 bool
2801 __includes(_InputIterator1 __first1, _InputIterator1 __last1,
2802 _InputIterator2 __first2, _InputIterator2 __last2,
2803 _Compare __comp)
2804 {
2805 while (__first1 != __last1 && __first2 != __last2)
2806 {
2807 if (__comp(__first2, __first1))
2808 return false;
2809 if (!__comp(__first1, __first2))
2810 ++__first2;
2811 ++__first1;
2812 }
2813
2814 return __first2 == __last2;
2815 }
2816
2817 /**
2818 * @brief Determines whether all elements of a sequence exists in a range.
2819 * @param __first1 Start of search range.
2820 * @param __last1 End of search range.
2821 * @param __first2 Start of sequence
2822 * @param __last2 End of sequence.
2823 * @return True if each element in [__first2,__last2) is contained in order
2824 * within [__first1,__last1). False otherwise.
2825 * @ingroup set_algorithms
2826 *
2827 * This operation expects both [__first1,__last1) and
2828 * [__first2,__last2) to be sorted. Searches for the presence of
2829 * each element in [__first2,__last2) within [__first1,__last1).
2830 * The iterators over each range only move forward, so this is a
2831 * linear algorithm. If an element in [__first2,__last2) is not
2832 * found before the search iterator reaches @p __last2, false is
2833 * returned.
2834 */
2835 template<typename _InputIterator1, typename _InputIterator2>
2836 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2837 inline bool
2838 includes(_InputIterator1 __first1, _InputIterator1 __last1,
2839 _InputIterator2 __first2, _InputIterator2 __last2)
2840 {
2841 // concept requirements
2842 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
2843 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
2844 __glibcxx_function_requires(_LessThanOpConcept<
2847 __glibcxx_function_requires(_LessThanOpConcept<
2850 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
2851 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
2852 __glibcxx_requires_irreflexive2(__first1, __last1);
2853 __glibcxx_requires_irreflexive2(__first2, __last2);
2854
2855 return std::__includes(__first1, __last1, __first2, __last2,
2856 __gnu_cxx::__ops::__iter_less_iter());
2857 }
2858
2859 /**
2860 * @brief Determines whether all elements of a sequence exists in a range
2861 * using comparison.
2862 * @ingroup set_algorithms
2863 * @param __first1 Start of search range.
2864 * @param __last1 End of search range.
2865 * @param __first2 Start of sequence
2866 * @param __last2 End of sequence.
2867 * @param __comp Comparison function to use.
2868 * @return True if each element in [__first2,__last2) is contained
2869 * in order within [__first1,__last1) according to comp. False
2870 * otherwise. @ingroup set_algorithms
2871 *
2872 * This operation expects both [__first1,__last1) and
2873 * [__first2,__last2) to be sorted. Searches for the presence of
2874 * each element in [__first2,__last2) within [__first1,__last1),
2875 * using comp to decide. The iterators over each range only move
2876 * forward, so this is a linear algorithm. If an element in
2877 * [__first2,__last2) is not found before the search iterator
2878 * reaches @p __last2, false is returned.
2879 */
2880 template<typename _InputIterator1, typename _InputIterator2,
2881 typename _Compare>
2882 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2883 inline bool
2884 includes(_InputIterator1 __first1, _InputIterator1 __last1,
2885 _InputIterator2 __first2, _InputIterator2 __last2,
2886 _Compare __comp)
2887 {
2888 // concept requirements
2889 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
2890 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
2891 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2894 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2897 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
2898 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
2899 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
2900 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
2901
2902 return std::__includes(__first1, __last1, __first2, __last2,
2903 __gnu_cxx::__ops::__iter_comp_iter(__comp));
2904 }
2905
2906 // nth_element
2907 // merge
2908 // set_difference
2909 // set_intersection
2910 // set_union
2911 // stable_sort
2912 // set_symmetric_difference
2913 // min_element
2914 // max_element
2915
2916 template<typename _BidirectionalIterator, typename _Compare>
2917 _GLIBCXX20_CONSTEXPR
2918 bool
2919 __next_permutation(_BidirectionalIterator __first,
2920 _BidirectionalIterator __last, _Compare __comp)
2921 {
2922 if (__first == __last)
2923 return false;
2924 _BidirectionalIterator __i = __first;
2925 ++__i;
2926 if (__i == __last)
2927 return false;
2928 __i = __last;
2929 --__i;
2930
2931 for(;;)
2932 {
2933 _BidirectionalIterator __ii = __i;
2934 --__i;
2935 if (__comp(__i, __ii))
2936 {
2937 _BidirectionalIterator __j = __last;
2938 while (!__comp(__i, --__j))
2939 {}
2940 std::iter_swap(__i, __j);
2941 std::__reverse(__ii, __last,
2942 std::__iterator_category(__first));
2943 return true;
2944 }
2945 if (__i == __first)
2946 {
2947 std::__reverse(__first, __last,
2948 std::__iterator_category(__first));
2949 return false;
2950 }
2951 }
2952 }
2953
2954 /**
2955 * @brief Permute range into the next @e dictionary ordering.
2956 * @ingroup sorting_algorithms
2957 * @param __first Start of range.
2958 * @param __last End of range.
2959 * @return False if wrapped to first permutation, true otherwise.
2960 *
2961 * Treats all permutations of the range as a set of @e dictionary sorted
2962 * sequences. Permutes the current sequence into the next one of this set.
2963 * Returns true if there are more sequences to generate. If the sequence
2964 * is the largest of the set, the smallest is generated and false returned.
2965 */
2966 template<typename _BidirectionalIterator>
2967 _GLIBCXX20_CONSTEXPR
2968 inline bool
2969 next_permutation(_BidirectionalIterator __first,
2970 _BidirectionalIterator __last)
2971 {
2972 // concept requirements
2973 __glibcxx_function_requires(_BidirectionalIteratorConcept<
2974 _BidirectionalIterator>)
2975 __glibcxx_function_requires(_LessThanComparableConcept<
2977 __glibcxx_requires_valid_range(__first, __last);
2978 __glibcxx_requires_irreflexive(__first, __last);
2979
2980 return std::__next_permutation
2981 (__first, __last, __gnu_cxx::__ops::__iter_less_iter());
2982 }
2983
2984 /**
2985 * @brief Permute range into the next @e dictionary ordering using
2986 * comparison functor.
2987 * @ingroup sorting_algorithms
2988 * @param __first Start of range.
2989 * @param __last End of range.
2990 * @param __comp A comparison functor.
2991 * @return False if wrapped to first permutation, true otherwise.
2992 *
2993 * Treats all permutations of the range [__first,__last) as a set of
2994 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
2995 * sequence into the next one of this set. Returns true if there are more
2996 * sequences to generate. If the sequence is the largest of the set, the
2997 * smallest is generated and false returned.
2998 */
2999 template<typename _BidirectionalIterator, typename _Compare>
3000 _GLIBCXX20_CONSTEXPR
3001 inline bool
3002 next_permutation(_BidirectionalIterator __first,
3003 _BidirectionalIterator __last, _Compare __comp)
3004 {
3005 // concept requirements
3006 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3007 _BidirectionalIterator>)
3008 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3011 __glibcxx_requires_valid_range(__first, __last);
3012 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
3013
3014 return std::__next_permutation
3015 (__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
3016 }
3017
3018 template<typename _BidirectionalIterator, typename _Compare>
3019 _GLIBCXX20_CONSTEXPR
3020 bool
3021 __prev_permutation(_BidirectionalIterator __first,
3022 _BidirectionalIterator __last, _Compare __comp)
3023 {
3024 if (__first == __last)
3025 return false;
3026 _BidirectionalIterator __i = __first;
3027 ++__i;
3028 if (__i == __last)
3029 return false;
3030 __i = __last;
3031 --__i;
3032
3033 for(;;)
3034 {
3035 _BidirectionalIterator __ii = __i;
3036 --__i;
3037 if (__comp(__ii, __i))
3038 {
3039 _BidirectionalIterator __j = __last;
3040 while (!__comp(--__j, __i))
3041 {}
3042 std::iter_swap(__i, __j);
3043 std::__reverse(__ii, __last,
3044 std::__iterator_category(__first));
3045 return true;
3046 }
3047 if (__i == __first)
3048 {
3049 std::__reverse(__first, __last,
3050 std::__iterator_category(__first));
3051 return false;
3052 }
3053 }
3054 }
3055
3056 /**
3057 * @brief Permute range into the previous @e dictionary ordering.
3058 * @ingroup sorting_algorithms
3059 * @param __first Start of range.
3060 * @param __last End of range.
3061 * @return False if wrapped to last permutation, true otherwise.
3062 *
3063 * Treats all permutations of the range as a set of @e dictionary sorted
3064 * sequences. Permutes the current sequence into the previous one of this
3065 * set. Returns true if there are more sequences to generate. If the
3066 * sequence is the smallest of the set, the largest is generated and false
3067 * returned.
3068 */
3069 template<typename _BidirectionalIterator>
3070 _GLIBCXX20_CONSTEXPR
3071 inline bool
3072 prev_permutation(_BidirectionalIterator __first,
3073 _BidirectionalIterator __last)
3074 {
3075 // concept requirements
3076 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3077 _BidirectionalIterator>)
3078 __glibcxx_function_requires(_LessThanComparableConcept<
3080 __glibcxx_requires_valid_range(__first, __last);
3081 __glibcxx_requires_irreflexive(__first, __last);
3082
3083 return std::__prev_permutation(__first, __last,
3084 __gnu_cxx::__ops::__iter_less_iter());
3085 }
3086
3087 /**
3088 * @brief Permute range into the previous @e dictionary ordering using
3089 * comparison functor.
3090 * @ingroup sorting_algorithms
3091 * @param __first Start of range.
3092 * @param __last End of range.
3093 * @param __comp A comparison functor.
3094 * @return False if wrapped to last permutation, true otherwise.
3095 *
3096 * Treats all permutations of the range [__first,__last) as a set of
3097 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current
3098 * sequence into the previous one of this set. Returns true if there are
3099 * more sequences to generate. If the sequence is the smallest of the set,
3100 * the largest is generated and false returned.
3101 */
3102 template<typename _BidirectionalIterator, typename _Compare>
3103 _GLIBCXX20_CONSTEXPR
3104 inline bool
3105 prev_permutation(_BidirectionalIterator __first,
3106 _BidirectionalIterator __last, _Compare __comp)
3107 {
3108 // concept requirements
3109 __glibcxx_function_requires(_BidirectionalIteratorConcept<
3110 _BidirectionalIterator>)
3111 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3114 __glibcxx_requires_valid_range(__first, __last);
3115 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
3116
3117 return std::__prev_permutation(__first, __last,
3118 __gnu_cxx::__ops::__iter_comp_iter(__comp));
3119 }
3120
3121 // replace
3122 // replace_if
3123
3124 template<typename _InputIterator, typename _OutputIterator,
3125 typename _Predicate, typename _Tp>
3126 _GLIBCXX20_CONSTEXPR
3127 _OutputIterator
3128 __replace_copy_if(_InputIterator __first, _InputIterator __last,
3129 _OutputIterator __result,
3130 _Predicate __pred, const _Tp& __new_value)
3131 {
3132 for (; __first != __last; ++__first, (void)++__result)
3133 if (__pred(__first))
3134 *__result = __new_value;
3135 else
3136 *__result = *__first;
3137 return __result;
3138 }
3139
3140 /**
3141 * @brief Copy a sequence, replacing each element of one value with another
3142 * value.
3143 * @param __first An input iterator.
3144 * @param __last An input iterator.
3145 * @param __result An output iterator.
3146 * @param __old_value The value to be replaced.
3147 * @param __new_value The replacement value.
3148 * @return The end of the output sequence, @p result+(last-first).
3149 *
3150 * Copies each element in the input range @p [__first,__last) to the
3151 * output range @p [__result,__result+(__last-__first)) replacing elements
3152 * equal to @p __old_value with @p __new_value.
3153 */
3154 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
3155 _GLIBCXX20_CONSTEXPR
3156 inline _OutputIterator
3157 replace_copy(_InputIterator __first, _InputIterator __last,
3158 _OutputIterator __result,
3159 const _Tp& __old_value, const _Tp& __new_value)
3160 {
3161 // concept requirements
3162 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3163 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3165 __glibcxx_function_requires(_EqualOpConcept<
3167 __glibcxx_requires_valid_range(__first, __last);
3168
3169 return std::__replace_copy_if(__first, __last, __result,
3170 __gnu_cxx::__ops::__iter_equals_val(__old_value),
3171 __new_value);
3172 }
3173
3174 /**
3175 * @brief Copy a sequence, replacing each value for which a predicate
3176 * returns true with another value.
3177 * @ingroup mutating_algorithms
3178 * @param __first An input iterator.
3179 * @param __last An input iterator.
3180 * @param __result An output iterator.
3181 * @param __pred A predicate.
3182 * @param __new_value The replacement value.
3183 * @return The end of the output sequence, @p __result+(__last-__first).
3184 *
3185 * Copies each element in the range @p [__first,__last) to the range
3186 * @p [__result,__result+(__last-__first)) replacing elements for which
3187 * @p __pred returns true with @p __new_value.
3188 */
3189 template<typename _InputIterator, typename _OutputIterator,
3190 typename _Predicate, typename _Tp>
3191 _GLIBCXX20_CONSTEXPR
3192 inline _OutputIterator
3193 replace_copy_if(_InputIterator __first, _InputIterator __last,
3194 _OutputIterator __result,
3195 _Predicate __pred, const _Tp& __new_value)
3196 {
3197 // concept requirements
3198 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3199 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3201 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
3203 __glibcxx_requires_valid_range(__first, __last);
3204
3205 return std::__replace_copy_if(__first, __last, __result,
3206 __gnu_cxx::__ops::__pred_iter(__pred),
3207 __new_value);
3208 }
3209
3210#if __cplusplus >= 201103L
3211 /**
3212 * @brief Determines whether the elements of a sequence are sorted.
3213 * @ingroup sorting_algorithms
3214 * @param __first An iterator.
3215 * @param __last Another iterator.
3216 * @return True if the elements are sorted, false otherwise.
3217 */
3218 template<typename _ForwardIterator>
3219 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3220 inline bool
3221 is_sorted(_ForwardIterator __first, _ForwardIterator __last)
3222 { return std::is_sorted_until(__first, __last) == __last; }
3223
3224 /**
3225 * @brief Determines whether the elements of a sequence are sorted
3226 * according to a comparison functor.
3227 * @ingroup sorting_algorithms
3228 * @param __first An iterator.
3229 * @param __last Another iterator.
3230 * @param __comp A comparison functor.
3231 * @return True if the elements are sorted, false otherwise.
3232 */
3233 template<typename _ForwardIterator, typename _Compare>
3234 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3235 inline bool
3236 is_sorted(_ForwardIterator __first, _ForwardIterator __last,
3237 _Compare __comp)
3238 { return std::is_sorted_until(__first, __last, __comp) == __last; }
3239
3240 template<typename _ForwardIterator, typename _Compare>
3241 _GLIBCXX20_CONSTEXPR
3242 _ForwardIterator
3243 __is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
3244 _Compare __comp)
3245 {
3246 if (__first == __last)
3247 return __last;
3248
3249 _ForwardIterator __next = __first;
3250 for (++__next; __next != __last; __first = __next, (void)++__next)
3251 if (__comp(__next, __first))
3252 return __next;
3253 return __next;
3254 }
3255
3256 /**
3257 * @brief Determines the end of a sorted sequence.
3258 * @ingroup sorting_algorithms
3259 * @param __first An iterator.
3260 * @param __last Another iterator.
3261 * @return An iterator pointing to the last iterator i in [__first, __last)
3262 * for which the range [__first, i) is sorted.
3263 */
3264 template<typename _ForwardIterator>
3265 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3266 inline _ForwardIterator
3267 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
3268 {
3269 // concept requirements
3270 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3271 __glibcxx_function_requires(_LessThanComparableConcept<
3273 __glibcxx_requires_valid_range(__first, __last);
3274 __glibcxx_requires_irreflexive(__first, __last);
3275
3276 return std::__is_sorted_until(__first, __last,
3277 __gnu_cxx::__ops::__iter_less_iter());
3278 }
3279
3280 /**
3281 * @brief Determines the end of a sorted sequence using comparison functor.
3282 * @ingroup sorting_algorithms
3283 * @param __first An iterator.
3284 * @param __last Another iterator.
3285 * @param __comp A comparison functor.
3286 * @return An iterator pointing to the last iterator i in [__first, __last)
3287 * for which the range [__first, i) is sorted.
3288 */
3289 template<typename _ForwardIterator, typename _Compare>
3290 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3291 inline _ForwardIterator
3292 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
3293 _Compare __comp)
3294 {
3295 // concept requirements
3296 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3297 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3300 __glibcxx_requires_valid_range(__first, __last);
3301 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
3302
3303 return std::__is_sorted_until(__first, __last,
3304 __gnu_cxx::__ops::__iter_comp_iter(__comp));
3305 }
3306
3307 /**
3308 * @brief Determines min and max at once as an ordered pair.
3309 * @ingroup sorting_algorithms
3310 * @param __a A thing of arbitrary type.
3311 * @param __b Another thing of arbitrary type.
3312 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3313 * __b) otherwise.
3314 */
3315 template<typename _Tp>
3316 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
3318 minmax(const _Tp& __a, const _Tp& __b)
3319 {
3320 // concept requirements
3321 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
3322
3323 return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a)
3324 : pair<const _Tp&, const _Tp&>(__a, __b);
3325 }
3326
3327 /**
3328 * @brief Determines min and max at once as an ordered pair.
3329 * @ingroup sorting_algorithms
3330 * @param __a A thing of arbitrary type.
3331 * @param __b Another thing of arbitrary type.
3332 * @param __comp A @link comparison_functors comparison functor @endlink.
3333 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a,
3334 * __b) otherwise.
3335 */
3336 template<typename _Tp, typename _Compare>
3337 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
3339 minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
3340 {
3341 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a)
3342 : pair<const _Tp&, const _Tp&>(__a, __b);
3343 }
3344
3345 template<typename _ForwardIterator, typename _Compare>
3346 _GLIBCXX14_CONSTEXPR
3348 __minmax_element(_ForwardIterator __first, _ForwardIterator __last,
3349 _Compare __comp)
3350 {
3351 _ForwardIterator __next = __first;
3352 if (__first == __last
3353 || ++__next == __last)
3354 return std::make_pair(__first, __first);
3355
3356 _ForwardIterator __min{}, __max{};
3357 if (__comp(__next, __first))
3358 {
3359 __min = __next;
3360 __max = __first;
3361 }
3362 else
3363 {
3364 __min = __first;
3365 __max = __next;
3366 }
3367
3368 __first = __next;
3369 ++__first;
3370
3371 while (__first != __last)
3372 {
3373 __next = __first;
3374 if (++__next == __last)
3375 {
3376 if (__comp(__first, __min))
3377 __min = __first;
3378 else if (!__comp(__first, __max))
3379 __max = __first;
3380 break;
3381 }
3382
3383 if (__comp(__next, __first))
3384 {
3385 if (__comp(__next, __min))
3386 __min = __next;
3387 if (!__comp(__first, __max))
3388 __max = __first;
3389 }
3390 else
3391 {
3392 if (__comp(__first, __min))
3393 __min = __first;
3394 if (!__comp(__next, __max))
3395 __max = __next;
3396 }
3397
3398 __first = __next;
3399 ++__first;
3400 }
3401
3402 return std::make_pair(__min, __max);
3403 }
3404
3405 /**
3406 * @brief Return a pair of iterators pointing to the minimum and maximum
3407 * elements in a range.
3408 * @ingroup sorting_algorithms
3409 * @param __first Start of range.
3410 * @param __last End of range.
3411 * @return make_pair(m, M), where m is the first iterator i in
3412 * [__first, __last) such that no other element in the range is
3413 * smaller, and where M is the last iterator i in [__first, __last)
3414 * such that no other element in the range is larger.
3415 */
3416 template<typename _ForwardIterator>
3417 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
3419 minmax_element(_ForwardIterator __first, _ForwardIterator __last)
3420 {
3421 // concept requirements
3422 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3423 __glibcxx_function_requires(_LessThanComparableConcept<
3425 __glibcxx_requires_valid_range(__first, __last);
3426 __glibcxx_requires_irreflexive(__first, __last);
3427
3428 return std::__minmax_element(__first, __last,
3429 __gnu_cxx::__ops::__iter_less_iter());
3430 }
3431
3432 /**
3433 * @brief Return a pair of iterators pointing to the minimum and maximum
3434 * elements in a range.
3435 * @ingroup sorting_algorithms
3436 * @param __first Start of range.
3437 * @param __last End of range.
3438 * @param __comp Comparison functor.
3439 * @return make_pair(m, M), where m is the first iterator i in
3440 * [__first, __last) such that no other element in the range is
3441 * smaller, and where M is the last iterator i in [__first, __last)
3442 * such that no other element in the range is larger.
3443 */
3444 template<typename _ForwardIterator, typename _Compare>
3445 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
3447 minmax_element(_ForwardIterator __first, _ForwardIterator __last,
3448 _Compare __comp)
3449 {
3450 // concept requirements
3451 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3452 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3455 __glibcxx_requires_valid_range(__first, __last);
3456 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
3457
3458 return std::__minmax_element(__first, __last,
3459 __gnu_cxx::__ops::__iter_comp_iter(__comp));
3460 }
3461
3462 template<typename _Tp>
3463 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
3464 inline pair<_Tp, _Tp>
3465 minmax(initializer_list<_Tp> __l)
3466 {
3467 __glibcxx_requires_irreflexive(__l.begin(), __l.end());
3469 std::__minmax_element(__l.begin(), __l.end(),
3470 __gnu_cxx::__ops::__iter_less_iter());
3471 return std::make_pair(*__p.first, *__p.second);
3472 }
3473
3474 template<typename _Tp, typename _Compare>
3475 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
3476 inline pair<_Tp, _Tp>
3477 minmax(initializer_list<_Tp> __l, _Compare __comp)
3478 {
3479 __glibcxx_requires_irreflexive_pred(__l.begin(), __l.end(), __comp);
3481 std::__minmax_element(__l.begin(), __l.end(),
3482 __gnu_cxx::__ops::__iter_comp_iter(__comp));
3483 return std::make_pair(*__p.first, *__p.second);
3484 }
3485
3486 /**
3487 * @brief Checks whether a permutation of the second sequence is equal
3488 * to the first sequence.
3489 * @ingroup non_mutating_algorithms
3490 * @param __first1 Start of first range.
3491 * @param __last1 End of first range.
3492 * @param __first2 Start of second range.
3493 * @param __pred A binary predicate.
3494 * @return true if there exists a permutation of the elements in
3495 * the range [__first2, __first2 + (__last1 - __first1)),
3496 * beginning with ForwardIterator2 begin, such that
3497 * equal(__first1, __last1, __begin, __pred) returns true;
3498 * otherwise, returns false.
3499 */
3500 template<typename _ForwardIterator1, typename _ForwardIterator2,
3501 typename _BinaryPredicate>
3502 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3503 inline bool
3504 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3505 _ForwardIterator2 __first2, _BinaryPredicate __pred)
3506 {
3507 // concept requirements
3508 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
3509 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
3510 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
3513 __glibcxx_requires_valid_range(__first1, __last1);
3514
3515 return std::__is_permutation(__first1, __last1, __first2,
3516 __gnu_cxx::__ops::__iter_comp_iter(__pred));
3517 }
3518
3519#if __glibcxx_robust_nonmodifying_seq_ops // C++ >= 14
3520#pragma GCC diagnostic push
3521#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
3522 template<typename _ForwardIterator1, typename _ForwardIterator2,
3523 typename _BinaryPredicate>
3524 _GLIBCXX20_CONSTEXPR
3525 bool
3526 __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3527 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
3528 _BinaryPredicate __pred)
3529 {
3530 using _Cat1
3531 = typename iterator_traits<_ForwardIterator1>::iterator_category;
3532 using _Cat2
3533 = typename iterator_traits<_ForwardIterator2>::iterator_category;
3534 using _It1_is_RA = is_same<_Cat1, random_access_iterator_tag>;
3535 using _It2_is_RA = is_same<_Cat2, random_access_iterator_tag>;
3536 constexpr bool __ra_iters = __and_<_It1_is_RA, _It2_is_RA>::value;
3537 if constexpr (__ra_iters)
3538 {
3539 if ((__last1 - __first1) != (__last2 - __first2))
3540 return false;
3541 }
3542
3543 // Efficiently compare identical prefixes: O(N) if sequences
3544 // have the same elements in the same order.
3545 for (; __first1 != __last1 && __first2 != __last2;
3546 ++__first1, (void)++__first2)
3547 if (!__pred(__first1, __first2))
3548 break;
3549
3550 if constexpr (__ra_iters)
3551 {
3552 if (__first1 == __last1)
3553 return true;
3554 }
3555 else
3556 {
3557 auto __d1 = std::distance(__first1, __last1);
3558 auto __d2 = std::distance(__first2, __last2);
3559 if (__d1 == 0 && __d2 == 0)
3560 return true;
3561 if (__d1 != __d2)
3562 return false;
3563 }
3564
3565 for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan)
3566 {
3567 if (__scan != std::__find_if(__first1, __scan,
3568 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)))
3569 continue; // We've seen this one before.
3570
3571 auto __matches = std::__count_if(__first2, __last2,
3572 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan));
3573 if (0 == __matches
3574 || std::__count_if(__scan, __last1,
3575 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))
3576 != __matches)
3577 return false;
3578 }
3579 return true;
3580 }
3581#pragma GCC diagnostic pop
3582
3583 /**
3584 * @brief Checks whether a permutaion of the second sequence is equal
3585 * to the first sequence.
3586 * @ingroup non_mutating_algorithms
3587 * @param __first1 Start of first range.
3588 * @param __last1 End of first range.
3589 * @param __first2 Start of second range.
3590 * @param __last2 End of first range.
3591 * @return true if there exists a permutation of the elements in the range
3592 * [__first2, __last2), beginning with ForwardIterator2 begin,
3593 * such that equal(__first1, __last1, begin) returns true;
3594 * otherwise, returns false.
3595 */
3596 template<typename _ForwardIterator1, typename _ForwardIterator2>
3597 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3598 inline bool
3599 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3600 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
3601 {
3602 __glibcxx_requires_valid_range(__first1, __last1);
3603 __glibcxx_requires_valid_range(__first2, __last2);
3604
3605 return
3606 std::__is_permutation(__first1, __last1, __first2, __last2,
3607 __gnu_cxx::__ops::__iter_equal_to_iter());
3608 }
3609
3610 /**
3611 * @brief Checks whether a permutation of the second sequence is equal
3612 * to the first sequence.
3613 * @ingroup non_mutating_algorithms
3614 * @param __first1 Start of first range.
3615 * @param __last1 End of first range.
3616 * @param __first2 Start of second range.
3617 * @param __last2 End of first range.
3618 * @param __pred A binary predicate.
3619 * @return true if there exists a permutation of the elements in the range
3620 * [__first2, __last2), beginning with ForwardIterator2 begin,
3621 * such that equal(__first1, __last1, __begin, __pred) returns true;
3622 * otherwise, returns false.
3623 */
3624 template<typename _ForwardIterator1, typename _ForwardIterator2,
3625 typename _BinaryPredicate>
3626 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3627 inline bool
3628 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3629 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
3630 _BinaryPredicate __pred)
3631 {
3632 __glibcxx_requires_valid_range(__first1, __last1);
3633 __glibcxx_requires_valid_range(__first2, __last2);
3634
3635 return std::__is_permutation(__first1, __last1, __first2, __last2,
3636 __gnu_cxx::__ops::__iter_comp_iter(__pred));
3637 }
3638#endif // __glibcxx_robust_nonmodifying_seq_ops
3639
3640#ifdef __glibcxx_clamp // C++ >= 17
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 * @retval `__lo` if `__val < __lo`
3648 * @retval `__hi` if `__hi < __val`
3649 * @retval `__val` otherwise.
3650 * @pre `_Tp` is LessThanComparable and `(__hi < __lo)` is false.
3651 */
3652 template<typename _Tp>
3653 [[nodiscard]] constexpr const _Tp&
3654 clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi)
3655 {
3656 __glibcxx_assert(!(__hi < __lo));
3657 return std::min(std::max(__val, __lo), __hi);
3658 }
3659
3660 /**
3661 * @brief Returns the value clamped between lo and hi.
3662 * @ingroup sorting_algorithms
3663 * @param __val A value of arbitrary type.
3664 * @param __lo A lower limit of arbitrary type.
3665 * @param __hi An upper limit of arbitrary type.
3666 * @param __comp A comparison functor.
3667 * @retval `__lo` if `__comp(__val, __lo)`
3668 * @retval `__hi` if `__comp(__hi, __val)`
3669 * @retval `__val` otherwise.
3670 * @pre `__comp(__hi, __lo)` is false.
3671 */
3672 template<typename _Tp, typename _Compare>
3673 [[nodiscard]] constexpr const _Tp&
3674 clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
3675 {
3676 __glibcxx_assert(!__comp(__hi, __lo));
3677 return std::min(std::max(__val, __lo, __comp), __hi, __comp);
3678 }
3679#endif // __glibcxx_clamp
3680
3681 /**
3682 * @brief Generate two uniformly distributed integers using a
3683 * single distribution invocation.
3684 * @param __b0 The upper bound for the first integer.
3685 * @param __b1 The upper bound for the second integer.
3686 * @param __g A UniformRandomBitGenerator.
3687 * @return A pair (i, j) with i and j uniformly distributed
3688 * over [0, __b0) and [0, __b1), respectively.
3689 *
3690 * Requires: __b0 * __b1 <= __g.max() - __g.min().
3691 *
3692 * Using uniform_int_distribution with a range that is very
3693 * small relative to the range of the generator ends up wasting
3694 * potentially expensively generated randomness, since
3695 * uniform_int_distribution does not store leftover randomness
3696 * between invocations.
3697 *
3698 * If we know we want two integers in ranges that are sufficiently
3699 * small, we can compose the ranges, use a single distribution
3700 * invocation, and significantly reduce the waste.
3701 */
3702 template<typename _IntType, typename _UniformRandomBitGenerator>
3704 __gen_two_uniform_ints(_IntType __b0, _IntType __b1,
3705 _UniformRandomBitGenerator&& __g)
3706 {
3707 _IntType __x
3708 = uniform_int_distribution<_IntType>{0, (__b0 * __b1) - 1}(__g);
3709 return std::make_pair(__x / __b1, __x % __b1);
3710 }
3711
3712 /**
3713 * @brief Shuffle the elements of a sequence using a uniform random
3714 * number generator.
3715 * @ingroup mutating_algorithms
3716 * @param __first A forward iterator.
3717 * @param __last A forward iterator.
3718 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
3719 * @return Nothing.
3720 *
3721 * Reorders the elements in the range @p [__first,__last) using @p __g to
3722 * provide random numbers.
3723 */
3724 template<typename _RandomAccessIterator,
3725 typename _UniformRandomNumberGenerator>
3726 void
3727 shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3728 _UniformRandomNumberGenerator&& __g)
3729 {
3730 // concept requirements
3731 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
3732 _RandomAccessIterator>)
3733 __glibcxx_requires_valid_range(__first, __last);
3734
3735 if (__first == __last)
3736 return;
3737
3739 _DistanceType;
3740
3741 typedef typename std::make_unsigned<_DistanceType>::type __ud_type;
3742 typedef typename std::uniform_int_distribution<__ud_type> __distr_type;
3743 typedef typename __distr_type::param_type __p_type;
3744
3745 typedef typename remove_reference<_UniformRandomNumberGenerator>::type
3746 _Gen;
3748 __uc_type;
3749
3750 const __uc_type __urngrange = __g.max() - __g.min();
3751 const __uc_type __urange = __uc_type(__last - __first);
3752
3753 if (__urngrange / __urange >= __urange)
3754 // I.e. (__urngrange >= __urange * __urange) but without wrap issues.
3755 {
3756 _RandomAccessIterator __i = __first + 1;
3757
3758 // Since we know the range isn't empty, an even number of elements
3759 // means an uneven number of elements /to swap/, in which case we
3760 // do the first one up front:
3761
3762 if ((__urange % 2) == 0)
3763 {
3764 __distr_type __d{0, 1};
3765 std::iter_swap(__i++, __first + __d(__g));
3766 }
3767
3768 // Now we know that __last - __i is even, so we do the rest in pairs,
3769 // using a single distribution invocation to produce swap positions
3770 // for two successive elements at a time:
3771
3772 while (__i != __last)
3773 {
3774 const __uc_type __swap_range = __uc_type(__i - __first) + 1;
3775
3776 const pair<__uc_type, __uc_type> __pospos =
3777 __gen_two_uniform_ints(__swap_range, __swap_range + 1, __g);
3778
3779 std::iter_swap(__i++, __first + __pospos.first);
3780 std::iter_swap(__i++, __first + __pospos.second);
3781 }
3782
3783 return;
3784 }
3785
3786 __distr_type __d;
3787
3788 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
3789 std::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first)));
3790 }
3791#endif // C++11
3792
3793_GLIBCXX_BEGIN_NAMESPACE_ALGO
3794
3795 /**
3796 * @brief Apply a function to every element of a sequence.
3797 * @ingroup non_mutating_algorithms
3798 * @param __first An input iterator.
3799 * @param __last An input iterator.
3800 * @param __f A unary function object.
3801 * @return @p __f
3802 *
3803 * Applies the function object @p __f to each element in the range
3804 * @p [first,last). @p __f must not modify the order of the sequence.
3805 * If @p __f has a return value it is ignored.
3806 */
3807 template<typename _InputIterator, typename _Function>
3808 _GLIBCXX20_CONSTEXPR
3809 _Function
3810 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
3811 {
3812 // concept requirements
3813 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3814 __glibcxx_requires_valid_range(__first, __last);
3815 for (; __first != __last; ++__first)
3816 __f(*__first);
3817 return __f; // N.B. [alg.foreach] says std::move(f) but it's redundant.
3818 }
3819
3820#if __cplusplus >= 201703L
3821 /**
3822 * @brief Apply a function to every element of a sequence.
3823 * @ingroup non_mutating_algorithms
3824 * @param __first An input iterator.
3825 * @param __n A value convertible to an integer.
3826 * @param __f A unary function object.
3827 * @return `__first+__n`
3828 *
3829 * Applies the function object `__f` to each element in the range
3830 * `[first, first+n)`. `__f` must not modify the order of the sequence.
3831 * If `__f` has a return value it is ignored.
3832 */
3833 template<typename _InputIterator, typename _Size, typename _Function>
3834 _GLIBCXX20_CONSTEXPR
3835 _InputIterator
3836 for_each_n(_InputIterator __first, _Size __n, _Function __f)
3837 {
3838 auto __n2 = std::__size_to_integer(__n);
3840 if constexpr (is_base_of_v<random_access_iterator_tag, _Cat>)
3841 {
3842 if (__n2 <= 0)
3843 return __first;
3845 auto __last = __first + __d;
3846 std::for_each(__first, __last, std::move(__f));
3847 return __last;
3848 }
3849 else
3850 {
3851 while (__n2-->0)
3852 {
3853 __f(*__first);
3854 ++__first;
3855 }
3856 return __first;
3857 }
3858 }
3859#endif // C++17
3860
3861 /**
3862 * @brief Find the first occurrence of a value in a sequence.
3863 * @ingroup non_mutating_algorithms
3864 * @param __first An input iterator.
3865 * @param __last An input iterator.
3866 * @param __val The value to find.
3867 * @return The first iterator @c i in the range @p [__first,__last)
3868 * such that @c *i == @p __val, or @p __last if no such iterator exists.
3869 */
3870 template<typename _InputIterator, typename _Tp>
3871 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3872 inline _InputIterator
3873 find(_InputIterator __first, _InputIterator __last, const _Tp& __val)
3874 {
3875 // concept requirements
3876 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3877 __glibcxx_function_requires(_EqualOpConcept<
3879 __glibcxx_requires_valid_range(__first, __last);
3880
3881#if __cpp_if_constexpr && __glibcxx_type_trait_variable_templates
3882 using _ValT = typename iterator_traits<_InputIterator>::value_type;
3883 if constexpr (__can_use_memchr_for_find<_ValT, _Tp>)
3884 if constexpr (is_pointer_v<decltype(std::__niter_base(__first))>
3885#if __glibcxx_concepts && __glibcxx_to_address
3886 || contiguous_iterator<_InputIterator>
3887#endif
3888 )
3889 {
3890 // If conversion to the 1-byte value_type alters the value,
3891 // it would not be found by std::find using equality comparison.
3892 // We need to check this here, because otherwise something like
3893 // memchr("a", 'a'+256, 1) would give a false positive match.
3894 if (!(static_cast<_ValT>(__val) == __val))
3895 return __last;
3896 else if (!__is_constant_evaluated())
3897 {
3898 const int __ival = static_cast<int>(__val);
3899 if (auto __n = __last - __first; __n > 0)
3900 {
3901#if __glibcxx_concepts && __glibcxx_to_address
3902 const void* __p0 = std::to_address(__first);
3903#else
3904 const void* __p0 = std::__niter_base(__first);
3905#endif
3906 if (auto __p1 = __builtin_memchr(__p0, __ival, __n))
3907 return __first + ((const char*)__p1 - (const char*)__p0);
3908 }
3909 return __last;
3910 }
3911 }
3912#endif
3913
3914 return std::__find_if(__first, __last,
3915 __gnu_cxx::__ops::__iter_equals_val(__val));
3916 }
3917
3918 /**
3919 * @brief Find the first element in a sequence for which a
3920 * predicate is true.
3921 * @ingroup non_mutating_algorithms
3922 * @param __first An input iterator.
3923 * @param __last An input iterator.
3924 * @param __pred A predicate.
3925 * @return The first iterator @c i in the range @p [__first,__last)
3926 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
3927 */
3928 template<typename _InputIterator, typename _Predicate>
3929 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3930 inline _InputIterator
3931 find_if(_InputIterator __first, _InputIterator __last,
3932 _Predicate __pred)
3933 {
3934 // concept requirements
3935 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3936 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
3938 __glibcxx_requires_valid_range(__first, __last);
3939
3940 return std::__find_if(__first, __last,
3941 __gnu_cxx::__ops::__pred_iter(__pred));
3942 }
3943
3944 /**
3945 * @brief Find element from a set in a sequence.
3946 * @ingroup non_mutating_algorithms
3947 * @param __first1 Start of range to search.
3948 * @param __last1 End of range to search.
3949 * @param __first2 Start of match candidates.
3950 * @param __last2 End of match candidates.
3951 * @return The first iterator @c i in the range
3952 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
3953 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
3954 *
3955 * Searches the range @p [__first1,__last1) for an element that is
3956 * equal to some element in the range [__first2,__last2). If
3957 * found, returns an iterator in the range [__first1,__last1),
3958 * otherwise returns @p __last1.
3959 */
3960 template<typename _InputIterator, typename _ForwardIterator>
3961 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3962 _InputIterator
3963 find_first_of(_InputIterator __first1, _InputIterator __last1,
3964 _ForwardIterator __first2, _ForwardIterator __last2)
3965 {
3966 // concept requirements
3967 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3968 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3969 __glibcxx_function_requires(_EqualOpConcept<
3972 __glibcxx_requires_valid_range(__first1, __last1);
3973 __glibcxx_requires_valid_range(__first2, __last2);
3974
3975 for (; __first1 != __last1; ++__first1)
3976 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
3977 if (*__first1 == *__iter)
3978 return __first1;
3979 return __last1;
3980 }
3981
3982 /**
3983 * @brief Find element from a set in a sequence using a predicate.
3984 * @ingroup non_mutating_algorithms
3985 * @param __first1 Start of range to search.
3986 * @param __last1 End of range to search.
3987 * @param __first2 Start of match candidates.
3988 * @param __last2 End of match candidates.
3989 * @param __comp Predicate to use.
3990 * @return The first iterator @c i in the range
3991 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
3992 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
3993 * such iterator exists.
3994 *
3995
3996 * Searches the range @p [__first1,__last1) for an element that is
3997 * equal to some element in the range [__first2,__last2). If
3998 * found, returns an iterator in the range [__first1,__last1),
3999 * otherwise returns @p __last1.
4000 */
4001 template<typename _InputIterator, typename _ForwardIterator,
4002 typename _BinaryPredicate>
4003 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4004 _InputIterator
4005 find_first_of(_InputIterator __first1, _InputIterator __last1,
4006 _ForwardIterator __first2, _ForwardIterator __last2,
4007 _BinaryPredicate __comp)
4008 {
4009 // concept requirements
4010 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4011 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4012 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4015 __glibcxx_requires_valid_range(__first1, __last1);
4016 __glibcxx_requires_valid_range(__first2, __last2);
4017
4018 for (; __first1 != __last1; ++__first1)
4019 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
4020 if (__comp(*__first1, *__iter))
4021 return __first1;
4022 return __last1;
4023 }
4024
4025 /**
4026 * @brief Find two adjacent values in a sequence that are equal.
4027 * @ingroup non_mutating_algorithms
4028 * @param __first A forward iterator.
4029 * @param __last A forward iterator.
4030 * @return The first iterator @c i such that @c i and @c i+1 are both
4031 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
4032 * or @p __last if no such iterator exists.
4033 */
4034 template<typename _ForwardIterator>
4035 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4036 inline _ForwardIterator
4037 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
4038 {
4039 // concept requirements
4040 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4041 __glibcxx_function_requires(_EqualityComparableConcept<
4043 __glibcxx_requires_valid_range(__first, __last);
4044
4045 return std::__adjacent_find(__first, __last,
4046 __gnu_cxx::__ops::__iter_equal_to_iter());
4047 }
4048
4049 /**
4050 * @brief Find two adjacent values in a sequence using a predicate.
4051 * @ingroup non_mutating_algorithms
4052 * @param __first A forward iterator.
4053 * @param __last A forward iterator.
4054 * @param __binary_pred A binary predicate.
4055 * @return The first iterator @c i such that @c i and @c i+1 are both
4056 * valid iterators in @p [__first,__last) and such that
4057 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
4058 * exists.
4059 */
4060 template<typename _ForwardIterator, typename _BinaryPredicate>
4061 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4062 inline _ForwardIterator
4063 adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
4064 _BinaryPredicate __binary_pred)
4065 {
4066 // concept requirements
4067 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4068 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4071 __glibcxx_requires_valid_range(__first, __last);
4072
4073 return std::__adjacent_find(__first, __last,
4074 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
4075 }
4076
4077 /**
4078 * @brief Count the number of copies of a value in a sequence.
4079 * @ingroup non_mutating_algorithms
4080 * @param __first An input iterator.
4081 * @param __last An input iterator.
4082 * @param __value The value to be counted.
4083 * @return The number of iterators @c i in the range @p [__first,__last)
4084 * for which @c *i == @p __value
4085 */
4086 template<typename _InputIterator, typename _Tp>
4087 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4088 inline typename iterator_traits<_InputIterator>::difference_type
4089 count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
4090 {
4091 // concept requirements
4092 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4093 __glibcxx_function_requires(_EqualOpConcept<
4095 __glibcxx_requires_valid_range(__first, __last);
4096
4097 return std::__count_if(__first, __last,
4098 __gnu_cxx::__ops::__iter_equals_val(__value));
4099 }
4100
4101 /**
4102 * @brief Count the elements of a sequence for which a predicate is true.
4103 * @ingroup non_mutating_algorithms
4104 * @param __first An input iterator.
4105 * @param __last An input iterator.
4106 * @param __pred A predicate.
4107 * @return The number of iterators @c i in the range @p [__first,__last)
4108 * for which @p __pred(*i) is true.
4109 */
4110 template<typename _InputIterator, typename _Predicate>
4111 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4112 inline typename iterator_traits<_InputIterator>::difference_type
4113 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
4114 {
4115 // concept requirements
4116 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4117 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4119 __glibcxx_requires_valid_range(__first, __last);
4120
4121 return std::__count_if(__first, __last,
4122 __gnu_cxx::__ops::__pred_iter(__pred));
4123 }
4124
4125 /**
4126 * @brief Search a sequence for a matching sub-sequence.
4127 * @ingroup non_mutating_algorithms
4128 * @param __first1 A forward iterator.
4129 * @param __last1 A forward iterator.
4130 * @param __first2 A forward iterator.
4131 * @param __last2 A forward iterator.
4132 * @return The first iterator @c i in the range @p
4133 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
4134 * *(__first2+N) for each @c N in the range @p
4135 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
4136 *
4137 * Searches the range @p [__first1,__last1) for a sub-sequence that
4138 * compares equal value-by-value with the sequence given by @p
4139 * [__first2,__last2) and returns an iterator to the first element
4140 * of the sub-sequence, or @p __last1 if the sub-sequence is not
4141 * found.
4142 *
4143 * Because the sub-sequence must lie completely within the range @p
4144 * [__first1,__last1) it must start at a position less than @p
4145 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4146 * length of the sub-sequence.
4147 *
4148 * This means that the returned iterator @c i will be in the range
4149 * @p [__first1,__last1-(__last2-__first2))
4150 */
4151 template<typename _ForwardIterator1, typename _ForwardIterator2>
4152 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4153 inline _ForwardIterator1
4154 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4155 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
4156 {
4157 // concept requirements
4158 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4159 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4160 __glibcxx_function_requires(_EqualOpConcept<
4163 __glibcxx_requires_valid_range(__first1, __last1);
4164 __glibcxx_requires_valid_range(__first2, __last2);
4165
4166 return std::__search(__first1, __last1, __first2, __last2,
4167 __gnu_cxx::__ops::__iter_equal_to_iter());
4168 }
4169
4170 /**
4171 * @brief Search a sequence for a number of consecutive values.
4172 * @ingroup non_mutating_algorithms
4173 * @param __first A forward iterator.
4174 * @param __last A forward iterator.
4175 * @param __count The number of consecutive values.
4176 * @param __val The value to find.
4177 * @return The first iterator @c i in the range @p
4178 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4179 * each @c N in the range @p [0,__count), or @p __last if no such
4180 * iterator exists.
4181 *
4182 * Searches the range @p [__first,__last) for @p count consecutive elements
4183 * equal to @p __val.
4184 */
4185 template<typename _ForwardIterator, typename _Integer, typename _Tp>
4186 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4187 inline _ForwardIterator
4188 search_n(_ForwardIterator __first, _ForwardIterator __last,
4189 _Integer __count, const _Tp& __val)
4190 {
4191 // concept requirements
4192 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4193 __glibcxx_function_requires(_EqualOpConcept<
4195 __glibcxx_requires_valid_range(__first, __last);
4196
4197 return std::__search_n(__first, __last, __count,
4198 __gnu_cxx::__ops::__iter_equals_val(__val));
4199 }
4200
4201
4202 /**
4203 * @brief Search a sequence for a number of consecutive values using a
4204 * predicate.
4205 * @ingroup non_mutating_algorithms
4206 * @param __first A forward iterator.
4207 * @param __last A forward iterator.
4208 * @param __count The number of consecutive values.
4209 * @param __val The value to find.
4210 * @param __binary_pred A binary predicate.
4211 * @return The first iterator @c i in the range @p
4212 * [__first,__last-__count) such that @p
4213 * __binary_pred(*(i+N),__val) is true for each @c N in the range
4214 * @p [0,__count), or @p __last if no such iterator exists.
4215 *
4216 * Searches the range @p [__first,__last) for @p __count
4217 * consecutive elements for which the predicate returns true.
4218 */
4219 template<typename _ForwardIterator, typename _Integer, typename _Tp,
4220 typename _BinaryPredicate>
4221 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4222 inline _ForwardIterator
4223 search_n(_ForwardIterator __first, _ForwardIterator __last,
4224 _Integer __count, const _Tp& __val,
4225 _BinaryPredicate __binary_pred)
4226 {
4227 // concept requirements
4228 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4229 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4231 __glibcxx_requires_valid_range(__first, __last);
4232
4233 return std::__search_n(__first, __last, __count,
4234 __gnu_cxx::__ops::__iter_comp_val(__binary_pred, __val));
4235 }
4236
4237#if __cplusplus >= 201703L
4238 /** @brief Search a sequence using a Searcher object.
4239 *
4240 * @param __first A forward iterator.
4241 * @param __last A forward iterator.
4242 * @param __searcher A callable object.
4243 * @return @p __searcher(__first,__last).first
4244 */
4245 template<typename _ForwardIterator, typename _Searcher>
4246 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4247 inline _ForwardIterator
4248 search(_ForwardIterator __first, _ForwardIterator __last,
4249 const _Searcher& __searcher)
4250 { return __searcher(__first, __last).first; }
4251#endif
4252
4253 /**
4254 * @brief Perform an operation on a sequence.
4255 * @ingroup mutating_algorithms
4256 * @param __first An input iterator.
4257 * @param __last An input iterator.
4258 * @param __result An output iterator.
4259 * @param __unary_op A unary operator.
4260 * @return An output iterator equal to @p __result+(__last-__first).
4261 *
4262 * Applies the operator to each element in the input range and assigns
4263 * the results to successive elements of the output sequence.
4264 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
4265 * range @p [0,__last-__first).
4266 *
4267 * @p unary_op must not alter its argument.
4268 */
4269 template<typename _InputIterator, typename _OutputIterator,
4270 typename _UnaryOperation>
4271 _GLIBCXX20_CONSTEXPR
4272 _OutputIterator
4273 transform(_InputIterator __first, _InputIterator __last,
4274 _OutputIterator __result, _UnaryOperation __unary_op)
4275 {
4276 // concept requirements
4277 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4278 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4279 // "the type returned by a _UnaryOperation"
4280 __typeof__(__unary_op(*__first))>)
4281 __glibcxx_requires_valid_range(__first, __last);
4282
4283 for (; __first != __last; ++__first, (void)++__result)
4284 *__result = __unary_op(*__first);
4285 return __result;
4286 }
4287
4288 /**
4289 * @brief Perform an operation on corresponding elements of two sequences.
4290 * @ingroup mutating_algorithms
4291 * @param __first1 An input iterator.
4292 * @param __last1 An input iterator.
4293 * @param __first2 An input iterator.
4294 * @param __result An output iterator.
4295 * @param __binary_op A binary operator.
4296 * @return An output iterator equal to @p result+(last-first).
4297 *
4298 * Applies the operator to the corresponding elements in the two
4299 * input ranges and assigns the results to successive elements of the
4300 * output sequence.
4301 * Evaluates @p
4302 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
4303 * @c N in the range @p [0,__last1-__first1).
4304 *
4305 * @p binary_op must not alter either of its arguments.
4306 */
4307 template<typename _InputIterator1, typename _InputIterator2,
4308 typename _OutputIterator, typename _BinaryOperation>
4309 _GLIBCXX20_CONSTEXPR
4310 _OutputIterator
4311 transform(_InputIterator1 __first1, _InputIterator1 __last1,
4312 _InputIterator2 __first2, _OutputIterator __result,
4313 _BinaryOperation __binary_op)
4314 {
4315 // concept requirements
4316 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4317 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4318 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4319 // "the type returned by a _BinaryOperation"
4320 __typeof__(__binary_op(*__first1,*__first2))>)
4321 __glibcxx_requires_valid_range(__first1, __last1);
4322
4323 for (; __first1 != __last1; ++__first1, (void)++__first2, ++__result)
4324 *__result = __binary_op(*__first1, *__first2);
4325 return __result;
4326 }
4327
4328 /**
4329 * @brief Replace each occurrence of one value in a sequence with another
4330 * value.
4331 * @ingroup mutating_algorithms
4332 * @param __first A forward iterator.
4333 * @param __last A forward iterator.
4334 * @param __old_value The value to be replaced.
4335 * @param __new_value The replacement value.
4336 * @return replace() returns no value.
4337 *
4338 * For each iterator `i` in the range `[__first,__last)` if
4339 * `*i == __old_value` then the assignment `*i = __new_value` is performed.
4340 */
4341 template<typename _ForwardIterator, typename _Tp>
4342 _GLIBCXX20_CONSTEXPR
4343 void
4344 replace(_ForwardIterator __first, _ForwardIterator __last,
4345 const _Tp& __old_value, const _Tp& __new_value)
4346 {
4347 // concept requirements
4348 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4349 _ForwardIterator>)
4350 __glibcxx_function_requires(_EqualOpConcept<
4352 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4354 __glibcxx_requires_valid_range(__first, __last);
4355
4356 for (; __first != __last; ++__first)
4357 if (*__first == __old_value)
4358 *__first = __new_value;
4359 }
4360
4361 /**
4362 * @brief Replace each value in a sequence for which a predicate returns
4363 * true with another value.
4364 * @ingroup mutating_algorithms
4365 * @param __first A forward iterator.
4366 * @param __last A forward iterator.
4367 * @param __pred A predicate.
4368 * @param __new_value The replacement value.
4369 * @return replace_if() returns no value.
4370 *
4371 * For each iterator `i` in the range `[__first,__last)` if `__pred(*i)`
4372 * is true then the assignment `*i = __new_value` is performed.
4373 */
4374 template<typename _ForwardIterator, typename _Predicate, typename _Tp>
4375 _GLIBCXX20_CONSTEXPR
4376 void
4377 replace_if(_ForwardIterator __first, _ForwardIterator __last,
4378 _Predicate __pred, const _Tp& __new_value)
4379 {
4380 // concept requirements
4381 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4382 _ForwardIterator>)
4383 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4385 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4387 __glibcxx_requires_valid_range(__first, __last);
4388
4389 for (; __first != __last; ++__first)
4390 if (__pred(*__first))
4391 *__first = __new_value;
4392 }
4393
4394 /**
4395 * @brief Assign the result of a function object to each value in a
4396 * sequence.
4397 * @ingroup mutating_algorithms
4398 * @param __first A forward iterator.
4399 * @param __last A forward iterator.
4400 * @param __gen A function object callable with no arguments.
4401 * @return generate() returns no value.
4402 *
4403 * Performs the assignment `*i = __gen()` for each `i` in the range
4404 * `[__first, __last)`.
4405 */
4406 template<typename _ForwardIterator, typename _Generator>
4407 _GLIBCXX20_CONSTEXPR
4408 void
4409 generate(_ForwardIterator __first, _ForwardIterator __last,
4410 _Generator __gen)
4411 {
4412 // concept requirements
4413 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4414 __glibcxx_function_requires(_GeneratorConcept<_Generator,
4416 __glibcxx_requires_valid_range(__first, __last);
4417
4418 for (; __first != __last; ++__first)
4419 *__first = __gen();
4420 }
4421
4422 /**
4423 * @brief Assign the result of a function object to each value in a
4424 * sequence.
4425 * @ingroup mutating_algorithms
4426 * @param __first A forward iterator.
4427 * @param __n The length of the sequence.
4428 * @param __gen A function object callable with no arguments.
4429 * @return The end of the sequence, i.e., `__first + __n`
4430 *
4431 * Performs the assignment `*i = __gen()` for each `i` in the range
4432 * `[__first, __first + __n)`.
4433 *
4434 * If `__n` is negative, the function does nothing and returns `__first`.
4435 */
4436 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4437 // DR 865. More algorithms that throw away information
4438 // DR 426. search_n(), fill_n(), and generate_n() with negative n
4439 template<typename _OutputIterator, typename _Size, typename _Generator>
4440 _GLIBCXX20_CONSTEXPR
4441 _OutputIterator
4442 generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
4443 {
4444 // concept requirements
4445 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4446 // "the type returned by a _Generator"
4447 __typeof__(__gen())>)
4448
4449 typedef __decltype(std::__size_to_integer(__n)) _IntSize;
4450 for (_IntSize __niter = std::__size_to_integer(__n);
4451 __niter > 0; --__niter, (void) ++__first)
4452 *__first = __gen();
4453 return __first;
4454 }
4455
4456 /**
4457 * @brief Copy a sequence, removing consecutive duplicate values.
4458 * @ingroup mutating_algorithms
4459 * @param __first An input iterator.
4460 * @param __last An input iterator.
4461 * @param __result An output iterator.
4462 * @return An iterator designating the end of the resulting sequence.
4463 *
4464 * Copies each element in the range `[__first, __last)` to the range
4465 * beginning at `__result`, except that only the first element is copied
4466 * from groups of consecutive elements that compare equal.
4467 * `unique_copy()` is stable, so the relative order of elements that are
4468 * copied is unchanged.
4469 */
4470 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4471 // DR 241. Does unique_copy() require CopyConstructible and Assignable?
4472 // DR 538. 241 again: Does unique_copy() require CopyConstructible and
4473 // Assignable?
4474 template<typename _InputIterator, typename _OutputIterator>
4475 _GLIBCXX20_CONSTEXPR
4476 inline _OutputIterator
4477 unique_copy(_InputIterator __first, _InputIterator __last,
4478 _OutputIterator __result)
4479 {
4480 // concept requirements
4481 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4482 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4484 __glibcxx_function_requires(_EqualityComparableConcept<
4486 __glibcxx_requires_valid_range(__first, __last);
4487
4488 if (__first == __last)
4489 return __result;
4490 return std::__unique_copy(__first, __last, __result,
4491 __gnu_cxx::__ops::__iter_equal_to_iter(),
4492 std::__iterator_category(__first));
4493 }
4494
4495 /**
4496 * @brief Copy a sequence, removing consecutive values using a predicate.
4497 * @ingroup mutating_algorithms
4498 * @param __first An input iterator.
4499 * @param __last An input iterator.
4500 * @param __result An output iterator.
4501 * @param __binary_pred A binary predicate.
4502 * @return An iterator designating the end of the resulting sequence.
4503 *
4504 * Copies each element in the range `[__first, __last)` to the range
4505 * beginning at `__result`, except that only the first element is copied
4506 * from groups of consecutive elements for which `__binary_pred` returns
4507 * true.
4508 * `unique_copy()` is stable, so the relative order of elements that are
4509 * copied is unchanged.
4510 */
4511 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4512 // DR 241. Does unique_copy() require CopyConstructible and Assignable?
4513 template<typename _InputIterator, typename _OutputIterator,
4514 typename _BinaryPredicate>
4515 _GLIBCXX20_CONSTEXPR
4516 inline _OutputIterator
4517 unique_copy(_InputIterator __first, _InputIterator __last,
4518 _OutputIterator __result,
4519 _BinaryPredicate __binary_pred)
4520 {
4521 // concept requirements -- predicates checked later
4522 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4523 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4525 __glibcxx_requires_valid_range(__first, __last);
4526 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4529
4530 if (__first == __last)
4531 return __result;
4532 return std::__unique_copy(__first, __last, __result,
4533 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred),
4534 std::__iterator_category(__first));
4535 }
4536
4537#if __cplusplus <= 201103L || _GLIBCXX_USE_DEPRECATED
4538#if _GLIBCXX_HOSTED
4539 /**
4540 * @brief Randomly shuffle the elements of a sequence.
4541 * @ingroup mutating_algorithms
4542 * @param __first A forward iterator.
4543 * @param __last A forward iterator.
4544 * @return Nothing.
4545 *
4546 * Reorder the elements in the range `[__first, __last)` using a random
4547 * distribution, so that every possible ordering of the sequence is
4548 * equally likely.
4549 *
4550 * @deprecated
4551 * Since C++17, `std::random_shuffle` is not part of the C++ standard.
4552 * Use `std::shuffle` instead, which was introduced in C++11.
4553 */
4554 template<typename _RandomAccessIterator>
4555 _GLIBCXX14_DEPRECATED_SUGGEST("std::shuffle")
4556 inline void
4557 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
4558 {
4559 // concept requirements
4560 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4561 _RandomAccessIterator>)
4562 __glibcxx_requires_valid_range(__first, __last);
4563
4564 if (__first == __last)
4565 return;
4566
4568 _Dist;
4569
4570#if RAND_MAX < __INT_MAX__
4571 if (__builtin_expect((__last - __first) >= RAND_MAX / 4, 0))
4572 {
4573 // Use a xorshift implementation seeded by two calls to rand()
4574 // instead of using rand() for all the random numbers needed.
4575 unsigned __xss
4576 = (unsigned)std::rand() ^ ((unsigned)std::rand() << 15);
4577 for (_RandomAccessIterator __i = __first + _Dist(1); __i != __last;
4578 ++__i)
4579 {
4580 __xss += !__xss;
4581 __xss ^= __xss << 13;
4582 __xss ^= __xss >> 17;
4583 __xss ^= __xss << 5;
4584 _RandomAccessIterator __j
4585 = __first + _Dist(__xss % ((__i - __first) + 1));
4586 if (__i != __j)
4587 std::iter_swap(__i, __j);
4588 }
4589 return;
4590 }
4591#endif
4592
4593 for (_RandomAccessIterator __i = __first + _Dist(1); __i != __last; ++__i)
4594 {
4595 // XXX rand() % N is not uniformly distributed
4596 _RandomAccessIterator __j
4597 = __first + _Dist(std::rand() % ((__i - __first) + 1));
4598 if (__i != __j)
4599 std::iter_swap(__i, __j);
4600 }
4601 }
4602
4603 /**
4604 * @brief Shuffle the elements of a sequence using a random number
4605 * generator.
4606 * @ingroup mutating_algorithms
4607 * @param __first A forward iterator.
4608 * @param __last A forward iterator.
4609 * @param __rand The RNG functor or function.
4610 * @return Nothing.
4611 *
4612 * Reorders the elements in the range `[__first, __last)` using `__rand`
4613 * to provide a random distribution. Calling `__rand(N)` for a positive
4614 * integer `N` should return a randomly chosen integer from the
4615 * range `[0, N)`.
4616 *
4617 * @deprecated
4618 * Since C++17, `std::random_shuffle` is not part of the C++ standard.
4619 * Use `std::shuffle` instead, which was introduced in C++11.
4620 */
4621 template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
4622 _GLIBCXX14_DEPRECATED_SUGGEST("std::shuffle")
4623 void
4624 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
4625#if __cplusplus >= 201103L
4626 _RandomNumberGenerator&& __rand)
4627#else
4628 _RandomNumberGenerator& __rand)
4629#endif
4630 {
4631 // concept requirements
4632 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4633 _RandomAccessIterator>)
4634 __glibcxx_requires_valid_range(__first, __last);
4635
4636 if (__first == __last)
4637 return;
4638
4640 _Dist;
4641
4642 for (_RandomAccessIterator __i = __first + _Dist(1); __i != __last; ++__i)
4643 {
4644 _RandomAccessIterator __j
4645 = __first + _Dist(__rand((__i - __first) + 1));
4646 if (__i != __j)
4647 std::iter_swap(__i, __j);
4648 }
4649 }
4650#endif // HOSTED
4651#endif // <= C++11 || USE_DEPRECATED
4652
4653 /**
4654 * @brief Move elements for which a predicate is true to the beginning
4655 * of a sequence.
4656 * @ingroup mutating_algorithms
4657 * @param __first A forward iterator.
4658 * @param __last A forward iterator.
4659 * @param __pred A predicate functor.
4660 * @return An iterator `middle` such that `__pred(i)` is true for each
4661 * iterator `i` in the range `[__first, middle)` and false for each `i`
4662 * in the range `[middle, __last)`.
4663 *
4664 * `__pred` must not modify its operand. `partition()` does not preserve
4665 * the relative ordering of elements in each group, use
4666 * `stable_partition()` if this is needed.
4667 */
4668 template<typename _ForwardIterator, typename _Predicate>
4669 _GLIBCXX20_CONSTEXPR
4670 inline _ForwardIterator
4671 partition(_ForwardIterator __first, _ForwardIterator __last,
4672 _Predicate __pred)
4673 {
4674 // concept requirements
4675 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4676 _ForwardIterator>)
4677 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4679 __glibcxx_requires_valid_range(__first, __last);
4680
4681 return std::__partition(__first, __last, __pred,
4682 std::__iterator_category(__first));
4683 }
4684
4685
4686 /**
4687 * @brief Sort the smallest elements of a sequence.
4688 * @ingroup sorting_algorithms
4689 * @param __first An iterator.
4690 * @param __middle Another iterator.
4691 * @param __last Another iterator.
4692 * @return Nothing.
4693 *
4694 * Sorts the smallest `(__middle - __first)` elements in the range
4695 * `[first, last)` and moves them to the range `[__first, __middle)`. The
4696 * order of the remaining elements in the range `[__middle, __last)` is
4697 * unspecified.
4698 * After the sort if `i` and `j` are iterators in the range
4699 * `[__first, __middle)` such that `i` precedes `j` and `k` is an iterator
4700 * in the range `[__middle, __last)` then `*j < *i` and `*k < *i` are
4701 * both false.
4702 */
4703 template<typename _RandomAccessIterator>
4704 _GLIBCXX20_CONSTEXPR
4705 inline void
4706 partial_sort(_RandomAccessIterator __first,
4707 _RandomAccessIterator __middle,
4708 _RandomAccessIterator __last)
4709 {
4710 // concept requirements
4711 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4712 _RandomAccessIterator>)
4713 __glibcxx_function_requires(_LessThanComparableConcept<
4715 __glibcxx_requires_valid_range(__first, __middle);
4716 __glibcxx_requires_valid_range(__middle, __last);
4717 __glibcxx_requires_irreflexive(__first, __last);
4718
4719 std::__partial_sort(__first, __middle, __last,
4720 __gnu_cxx::__ops::__iter_less_iter());
4721 }
4722
4723 /**
4724 * @brief Sort the smallest elements of a sequence using a predicate
4725 * for comparison.
4726 * @ingroup sorting_algorithms
4727 * @param __first An iterator.
4728 * @param __middle Another iterator.
4729 * @param __last Another iterator.
4730 * @param __comp A comparison functor.
4731 * @return Nothing.
4732 *
4733 * Sorts the smallest `(__middle - __first)` elements in the range
4734 * `[__first, __last)` and moves them to the range `[__first, __middle)`.
4735 * The order of the remaining elements in the range `[__middle, __last)` is
4736 * unspecified.
4737 * After the sort if `i` and `j` are iterators in the range
4738 * `[__first, __middle)` such that `i` precedes `j` and `k` is an iterator
4739 * in the range `[__middle, __last)` then `*__comp(j, *i)` and
4740 * `__comp(*k, *i)` are both false.
4741 */
4742 template<typename _RandomAccessIterator, typename _Compare>
4743 _GLIBCXX20_CONSTEXPR
4744 inline void
4745 partial_sort(_RandomAccessIterator __first,
4746 _RandomAccessIterator __middle,
4747 _RandomAccessIterator __last,
4748 _Compare __comp)
4749 {
4750 // concept requirements
4751 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4752 _RandomAccessIterator>)
4753 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4756 __glibcxx_requires_valid_range(__first, __middle);
4757 __glibcxx_requires_valid_range(__middle, __last);
4758 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
4759
4760 std::__partial_sort(__first, __middle, __last,
4761 __gnu_cxx::__ops::__iter_comp_iter(__comp));
4762 }
4763
4764 /**
4765 * @brief Sort a sequence just enough to find a particular position.
4766 * @ingroup sorting_algorithms
4767 * @param __first An iterator.
4768 * @param __nth Another iterator.
4769 * @param __last Another iterator.
4770 * @return Nothing.
4771 *
4772 * Rearranges the elements in the range `[__first, __last)` so that `*__nth`
4773 * is the same element that would have been in that position had the
4774 * whole sequence been sorted. The elements either side of `*__nth` are
4775 * not completely sorted, but for any iterator `i` in the range
4776 * `[__first, __nth)` and any iterator `j` in the range `[__nth, __last)` it
4777 * holds that `*j < *i` is false.
4778 */
4779 template<typename _RandomAccessIterator>
4780 _GLIBCXX20_CONSTEXPR
4781 inline void
4782 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
4783 _RandomAccessIterator __last)
4784 {
4785 // concept requirements
4786 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4787 _RandomAccessIterator>)
4788 __glibcxx_function_requires(_LessThanComparableConcept<
4790 __glibcxx_requires_valid_range(__first, __nth);
4791 __glibcxx_requires_valid_range(__nth, __last);
4792 __glibcxx_requires_irreflexive(__first, __last);
4793
4794 if (__first == __last || __nth == __last)
4795 return;
4796
4797 std::__introselect(__first, __nth, __last,
4798 std::__lg(__last - __first) * 2,
4799 __gnu_cxx::__ops::__iter_less_iter());
4800 }
4801
4802 /**
4803 * @brief Sort a sequence just enough to find a particular position
4804 * using a predicate for comparison.
4805 * @ingroup sorting_algorithms
4806 * @param __first An iterator.
4807 * @param __nth Another iterator.
4808 * @param __last Another iterator.
4809 * @param __comp A comparison functor.
4810 * @return Nothing.
4811 *
4812 * Rearranges the elements in the range `[__first, __last)` so that `*__nth`
4813 * is the same element that would have been in that position had the
4814 * whole sequence been sorted. The elements either side of `*__nth` are
4815 * not completely sorted, but for any iterator `i` in the range
4816 * `[__first, __nth)` and any iterator `j` in the range `[__nth, __last)`
4817 * it holds that `__comp(*j, *i)` is false.
4818 */
4819 template<typename _RandomAccessIterator, typename _Compare>
4820 _GLIBCXX20_CONSTEXPR
4821 inline void
4822 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
4823 _RandomAccessIterator __last, _Compare __comp)
4824 {
4825 // concept requirements
4826 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4827 _RandomAccessIterator>)
4828 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4831 __glibcxx_requires_valid_range(__first, __nth);
4832 __glibcxx_requires_valid_range(__nth, __last);
4833 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
4834
4835 if (__first == __last || __nth == __last)
4836 return;
4837
4838 std::__introselect(__first, __nth, __last,
4839 std::__lg(__last - __first) * 2,
4840 __gnu_cxx::__ops::__iter_comp_iter(__comp));
4841 }
4842
4843 /**
4844 * @brief Sort the elements of a sequence.
4845 * @ingroup sorting_algorithms
4846 * @param __first An iterator.
4847 * @param __last Another iterator.
4848 * @return Nothing.
4849 *
4850 * Sorts the elements in the range `[__first, __last)` in ascending order,
4851 * such that for each iterator `i` in the range `[__first, __last - 1)`,
4852 * `*(i+1) < *i` is false.
4853 *
4854 * The relative ordering of equivalent elements is not preserved, use
4855 * `stable_sort()` if this is needed.
4856 */
4857 template<typename _RandomAccessIterator>
4858 _GLIBCXX20_CONSTEXPR
4859 inline void
4860 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4861 {
4862 // concept requirements
4863 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4864 _RandomAccessIterator>)
4865 __glibcxx_function_requires(_LessThanComparableConcept<
4867 __glibcxx_requires_valid_range(__first, __last);
4868 __glibcxx_requires_irreflexive(__first, __last);
4869
4870 std::__sort(__first, __last, __gnu_cxx::__ops::__iter_less_iter());
4871 }
4872
4873 /**
4874 * @brief Sort the elements of a sequence using a predicate for comparison.
4875 * @ingroup sorting_algorithms
4876 * @param __first An iterator.
4877 * @param __last Another iterator.
4878 * @param __comp A comparison functor.
4879 * @return Nothing.
4880 *
4881 * Sorts the elements in the range `[__first, __last)` in ascending order,
4882 * such that `__comp(*(i+1), *i)` is false for every iterator `i` in the
4883 * range `[__first, __last - 1)`.
4884 *
4885 * The relative ordering of equivalent elements is not preserved, use
4886 * `stable_sort()` if this is needed.
4887 */
4888 template<typename _RandomAccessIterator, typename _Compare>
4889 _GLIBCXX20_CONSTEXPR
4890 inline void
4891 sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
4892 _Compare __comp)
4893 {
4894 // concept requirements
4895 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4896 _RandomAccessIterator>)
4897 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4900 __glibcxx_requires_valid_range(__first, __last);
4901 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
4902
4903 std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
4904 }
4905
4906 template<typename _InputIterator1, typename _InputIterator2,
4907 typename _OutputIterator, typename _Compare>
4908 _GLIBCXX20_CONSTEXPR
4909 _OutputIterator
4910 __merge(_InputIterator1 __first1, _InputIterator1 __last1,
4911 _InputIterator2 __first2, _InputIterator2 __last2,
4912 _OutputIterator __result, _Compare __comp)
4913 {
4914 while (__first1 != __last1 && __first2 != __last2)
4915 {
4916 if (__comp(__first2, __first1))
4917 {
4918 *__result = *__first2;
4919 ++__first2;
4920 }
4921 else
4922 {
4923 *__result = *__first1;
4924 ++__first1;
4925 }
4926 ++__result;
4927 }
4928 return std::copy(__first2, __last2,
4929 std::copy(__first1, __last1, __result));
4930 }
4931
4932 /**
4933 * @brief Merges two sorted ranges.
4934 * @ingroup sorting_algorithms
4935 * @param __first1 An iterator.
4936 * @param __first2 Another iterator.
4937 * @param __last1 Another iterator.
4938 * @param __last2 Another iterator.
4939 * @param __result An iterator pointing to the end of the merged range.
4940 * @return An output iterator equal to @p __result + (__last1 - __first1)
4941 * + (__last2 - __first2).
4942 *
4943 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4944 * the sorted range @p [__result, __result + (__last1-__first1) +
4945 * (__last2-__first2)). Both input ranges must be sorted, and the
4946 * output range must not overlap with either of the input ranges.
4947 * The sort is @e stable, that is, for equivalent elements in the
4948 * two ranges, elements from the first range will always come
4949 * before elements from the second.
4950 */
4951 template<typename _InputIterator1, typename _InputIterator2,
4952 typename _OutputIterator>
4953 _GLIBCXX20_CONSTEXPR
4954 inline _OutputIterator
4955 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4956 _InputIterator2 __first2, _InputIterator2 __last2,
4957 _OutputIterator __result)
4958 {
4959 // concept requirements
4960 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4961 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4962 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4964 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4966 __glibcxx_function_requires(_LessThanOpConcept<
4969 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
4970 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
4971 __glibcxx_requires_irreflexive2(__first1, __last1);
4972 __glibcxx_requires_irreflexive2(__first2, __last2);
4973
4974 return _GLIBCXX_STD_A::__merge(__first1, __last1,
4975 __first2, __last2, __result,
4976 __gnu_cxx::__ops::__iter_less_iter());
4977 }
4978
4979 /**
4980 * @brief Merges two sorted ranges.
4981 * @ingroup sorting_algorithms
4982 * @param __first1 An iterator.
4983 * @param __first2 Another iterator.
4984 * @param __last1 Another iterator.
4985 * @param __last2 Another iterator.
4986 * @param __result An iterator pointing to the end of the merged range.
4987 * @param __comp A functor to use for comparisons.
4988 * @return An output iterator equal to @p __result + (__last1 - __first1)
4989 * + (__last2 - __first2).
4990 *
4991 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4992 * the sorted range @p [__result, __result + (__last1-__first1) +
4993 * (__last2-__first2)). Both input ranges must be sorted, and the
4994 * output range must not overlap with either of the input ranges.
4995 * The sort is @e stable, that is, for equivalent elements in the
4996 * two ranges, elements from the first range will always come
4997 * before elements from the second.
4998 *
4999 * The comparison function should have the same effects on ordering as
5000 * the function used for the initial sort.
5001 */
5002 template<typename _InputIterator1, typename _InputIterator2,
5003 typename _OutputIterator, typename _Compare>
5004 _GLIBCXX20_CONSTEXPR
5005 inline _OutputIterator
5006 merge(_InputIterator1 __first1, _InputIterator1 __last1,
5007 _InputIterator2 __first2, _InputIterator2 __last2,
5008 _OutputIterator __result, _Compare __comp)
5009 {
5010 // concept requirements
5011 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5012 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5013 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5015 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5017 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5020 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5021 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5022 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
5023 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
5024
5025 return _GLIBCXX_STD_A::__merge(__first1, __last1,
5026 __first2, __last2, __result,
5027 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5028 }
5029
5030 template<typename _RandomAccessIterator, typename _Compare>
5031 _GLIBCXX26_CONSTEXPR
5032 inline void
5033 __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
5034 _Compare __comp)
5035 {
5036 typedef typename iterator_traits<_RandomAccessIterator>::value_type
5037 _ValueType;
5038 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
5039 _DistanceType;
5040
5041 if (__first == __last)
5042 return;
5043
5044#if _GLIBCXX_HOSTED
5045# if __glibcxx_constexpr_algorithms >= 202306L // >= C++26
5046 if consteval {
5047 return std::__inplace_stable_sort(__first, __last, __comp);
5048 }
5049# endif
5050
5052 // __stable_sort_adaptive sorts the range in two halves,
5053 // so the buffer only needs to fit half the range at once.
5054 _TmpBuf __buf(__first, (__last - __first + 1) / 2);
5055
5056 if (__builtin_expect(__buf._M_requested_size() == __buf.size(), true))
5057 std::__stable_sort_adaptive(__first,
5058 __first + _DistanceType(__buf.size()),
5059 __last, __buf.begin(), __comp);
5060 else if (__builtin_expect(__buf.begin() == 0, false))
5061 std::__inplace_stable_sort(__first, __last, __comp);
5062 else
5063 std::__stable_sort_adaptive_resize(__first, __last, __buf.begin(),
5064 _DistanceType(__buf.size()), __comp);
5065#else
5066 std::__inplace_stable_sort(__first, __last, __comp);
5067#endif
5068 }
5069
5070 /**
5071 * @brief Sort the elements of a sequence, preserving the relative order
5072 * of equivalent elements.
5073 * @ingroup sorting_algorithms
5074 * @param __first An iterator.
5075 * @param __last Another iterator.
5076 * @return Nothing.
5077 *
5078 * Sorts the elements in the range @p [__first,__last) in ascending order,
5079 * such that for each iterator @p i in the range @p [__first,__last-1),
5080 * @p *(i+1)<*i is false.
5081 *
5082 * The relative ordering of equivalent elements is preserved, so any two
5083 * elements @p x and @p y in the range @p [__first,__last) such that
5084 * @p x<y is false and @p y<x is false will have the same relative
5085 * ordering after calling @p stable_sort().
5086 */
5087 template<typename _RandomAccessIterator>
5088 _GLIBCXX26_CONSTEXPR
5089 inline void
5090 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
5091 {
5092 // concept requirements
5093 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5094 _RandomAccessIterator>)
5095 __glibcxx_function_requires(_LessThanComparableConcept<
5097 __glibcxx_requires_valid_range(__first, __last);
5098 __glibcxx_requires_irreflexive(__first, __last);
5099
5100 _GLIBCXX_STD_A::__stable_sort(__first, __last,
5101 __gnu_cxx::__ops::__iter_less_iter());
5102 }
5103
5104 /**
5105 * @brief Sort the elements of a sequence using a predicate for comparison,
5106 * preserving the relative order of equivalent elements.
5107 * @ingroup sorting_algorithms
5108 * @param __first An iterator.
5109 * @param __last Another iterator.
5110 * @param __comp A comparison functor.
5111 * @return Nothing.
5112 *
5113 * Sorts the elements in the range @p [__first,__last) in ascending order,
5114 * such that for each iterator @p i in the range @p [__first,__last-1),
5115 * @p __comp(*(i+1),*i) is false.
5116 *
5117 * The relative ordering of equivalent elements is preserved, so any two
5118 * elements @p x and @p y in the range @p [__first,__last) such that
5119 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
5120 * relative ordering after calling @p stable_sort().
5121 */
5122 template<typename _RandomAccessIterator, typename _Compare>
5123 _GLIBCXX26_CONSTEXPR
5124 inline void
5125 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
5126 _Compare __comp)
5127 {
5128 // concept requirements
5129 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5130 _RandomAccessIterator>)
5131 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5134 __glibcxx_requires_valid_range(__first, __last);
5135 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
5136
5137 _GLIBCXX_STD_A::__stable_sort(__first, __last,
5138 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5139 }
5140
5141 template<typename _InputIterator1, typename _InputIterator2,
5142 typename _OutputIterator,
5143 typename _Compare>
5144 _GLIBCXX20_CONSTEXPR
5145 _OutputIterator
5146 __set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5147 _InputIterator2 __first2, _InputIterator2 __last2,
5148 _OutputIterator __result, _Compare __comp)
5149 {
5150 while (__first1 != __last1 && __first2 != __last2)
5151 {
5152 if (__comp(__first1, __first2))
5153 {
5154 *__result = *__first1;
5155 ++__first1;
5156 }
5157 else if (__comp(__first2, __first1))
5158 {
5159 *__result = *__first2;
5160 ++__first2;
5161 }
5162 else
5163 {
5164 *__result = *__first1;
5165 ++__first1;
5166 ++__first2;
5167 }
5168 ++__result;
5169 }
5170 return std::copy(__first2, __last2,
5171 std::copy(__first1, __last1, __result));
5172 }
5173
5174 /**
5175 * @brief Return the union of two sorted ranges.
5176 * @ingroup set_algorithms
5177 * @param __first1 Start of first range.
5178 * @param __last1 End of first range.
5179 * @param __first2 Start of second range.
5180 * @param __last2 End of second range.
5181 * @param __result Start of output range.
5182 * @return End of the output range.
5183 * @ingroup set_algorithms
5184 *
5185 * This operation iterates over both ranges, copying elements present in
5186 * each range in order to the output range. Iterators increment for each
5187 * range. When the current element of one range is less than the other,
5188 * that element is copied and the iterator advanced. If an element is
5189 * contained in both ranges, the element from the first range is copied and
5190 * both ranges advance. The output range may not overlap either input
5191 * range.
5192 */
5193 template<typename _InputIterator1, typename _InputIterator2,
5194 typename _OutputIterator>
5195 _GLIBCXX20_CONSTEXPR
5196 inline _OutputIterator
5197 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5198 _InputIterator2 __first2, _InputIterator2 __last2,
5199 _OutputIterator __result)
5200 {
5201 // concept requirements
5202 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5203 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5204 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5206 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5208 __glibcxx_function_requires(_LessThanOpConcept<
5211 __glibcxx_function_requires(_LessThanOpConcept<
5214 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5215 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5216 __glibcxx_requires_irreflexive2(__first1, __last1);
5217 __glibcxx_requires_irreflexive2(__first2, __last2);
5218
5219 return _GLIBCXX_STD_A::__set_union(__first1, __last1,
5220 __first2, __last2, __result,
5221 __gnu_cxx::__ops::__iter_less_iter());
5222 }
5223
5224 /**
5225 * @brief Return the union of two sorted ranges using a comparison functor.
5226 * @ingroup set_algorithms
5227 * @param __first1 Start of first range.
5228 * @param __last1 End of first range.
5229 * @param __first2 Start of second range.
5230 * @param __last2 End of second range.
5231 * @param __result Start of output range.
5232 * @param __comp The comparison functor.
5233 * @return End of the output range.
5234 * @ingroup set_algorithms
5235 *
5236 * This operation iterates over both ranges, copying elements present in
5237 * each range in order to the output range. Iterators increment for each
5238 * range. When the current element of one range is less than the other
5239 * according to @p __comp, that element is copied and the iterator advanced.
5240 * If an equivalent element according to @p __comp is contained in both
5241 * ranges, the element from the first range is copied and both ranges
5242 * advance. The output range may not overlap either input range.
5243 */
5244 template<typename _InputIterator1, typename _InputIterator2,
5245 typename _OutputIterator, typename _Compare>
5246 _GLIBCXX20_CONSTEXPR
5247 inline _OutputIterator
5248 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5249 _InputIterator2 __first2, _InputIterator2 __last2,
5250 _OutputIterator __result, _Compare __comp)
5251 {
5252 // concept requirements
5253 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5254 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5255 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5257 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5259 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5262 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5265 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5266 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5267 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
5268 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
5269
5270 return _GLIBCXX_STD_A::__set_union(__first1, __last1,
5271 __first2, __last2, __result,
5272 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5273 }
5274
5275 template<typename _InputIterator1, typename _InputIterator2,
5276 typename _OutputIterator,
5277 typename _Compare>
5278 _GLIBCXX20_CONSTEXPR
5279 _OutputIterator
5280 __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5281 _InputIterator2 __first2, _InputIterator2 __last2,
5282 _OutputIterator __result, _Compare __comp)
5283 {
5284 while (__first1 != __last1 && __first2 != __last2)
5285 if (__comp(__first1, __first2))
5286 ++__first1;
5287 else if (__comp(__first2, __first1))
5288 ++__first2;
5289 else
5290 {
5291 *__result = *__first1;
5292 ++__first1;
5293 ++__first2;
5294 ++__result;
5295 }
5296 return __result;
5297 }
5298
5299 /**
5300 * @brief Return the intersection of two sorted ranges.
5301 * @ingroup set_algorithms
5302 * @param __first1 Start of first range.
5303 * @param __last1 End of first range.
5304 * @param __first2 Start of second range.
5305 * @param __last2 End of second range.
5306 * @param __result Start of output range.
5307 * @return End of the output range.
5308 * @ingroup set_algorithms
5309 *
5310 * This operation iterates over both ranges, copying elements present in
5311 * both ranges in order to the output range. Iterators increment for each
5312 * range. When the current element of one range is less than the other,
5313 * that iterator advances. If an element is contained in both ranges, the
5314 * element from the first range is copied and both ranges advance. The
5315 * output range may not overlap either input range.
5316 */
5317 template<typename _InputIterator1, typename _InputIterator2,
5318 typename _OutputIterator>
5319 _GLIBCXX20_CONSTEXPR
5320 inline _OutputIterator
5321 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5322 _InputIterator2 __first2, _InputIterator2 __last2,
5323 _OutputIterator __result)
5324 {
5325 // concept requirements
5326 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5327 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5328 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5330 __glibcxx_function_requires(_LessThanOpConcept<
5333 __glibcxx_function_requires(_LessThanOpConcept<
5336 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5337 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5338 __glibcxx_requires_irreflexive2(__first1, __last1);
5339 __glibcxx_requires_irreflexive2(__first2, __last2);
5340
5341 return _GLIBCXX_STD_A::__set_intersection(__first1, __last1,
5342 __first2, __last2, __result,
5343 __gnu_cxx::__ops::__iter_less_iter());
5344 }
5345
5346 /**
5347 * @brief Return the intersection of two sorted ranges using comparison
5348 * functor.
5349 * @ingroup set_algorithms
5350 * @param __first1 Start of first range.
5351 * @param __last1 End of first range.
5352 * @param __first2 Start of second range.
5353 * @param __last2 End of second range.
5354 * @param __result Start of output range.
5355 * @param __comp The comparison functor.
5356 * @return End of the output range.
5357 * @ingroup set_algorithms
5358 *
5359 * This operation iterates over both ranges, copying elements present in
5360 * both ranges in order to the output range. Iterators increment for each
5361 * range. When the current element of one range is less than the other
5362 * according to @p __comp, that iterator advances. If an element is
5363 * contained in both ranges according to @p __comp, the element from the
5364 * first range is copied and both ranges advance. The output range may not
5365 * overlap either input range.
5366 */
5367 template<typename _InputIterator1, typename _InputIterator2,
5368 typename _OutputIterator, typename _Compare>
5369 _GLIBCXX20_CONSTEXPR
5370 inline _OutputIterator
5371 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5372 _InputIterator2 __first2, _InputIterator2 __last2,
5373 _OutputIterator __result, _Compare __comp)
5374 {
5375 // concept requirements
5376 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5377 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5378 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5380 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5383 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5386 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5387 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5388 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
5389 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
5390
5391 return _GLIBCXX_STD_A::__set_intersection(__first1, __last1,
5392 __first2, __last2, __result,
5393 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5394 }
5395
5396 template<typename _InputIterator1, typename _InputIterator2,
5397 typename _OutputIterator,
5398 typename _Compare>
5399 _GLIBCXX20_CONSTEXPR
5400 _OutputIterator
5401 __set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5402 _InputIterator2 __first2, _InputIterator2 __last2,
5403 _OutputIterator __result, _Compare __comp)
5404 {
5405 while (__first1 != __last1 && __first2 != __last2)
5406 if (__comp(__first1, __first2))
5407 {
5408 *__result = *__first1;
5409 ++__first1;
5410 ++__result;
5411 }
5412 else if (__comp(__first2, __first1))
5413 ++__first2;
5414 else
5415 {
5416 ++__first1;
5417 ++__first2;
5418 }
5419 return std::copy(__first1, __last1, __result);
5420 }
5421
5422 /**
5423 * @brief Return the difference of two sorted ranges.
5424 * @ingroup set_algorithms
5425 * @param __first1 Start of first range.
5426 * @param __last1 End of first range.
5427 * @param __first2 Start of second range.
5428 * @param __last2 End of second range.
5429 * @param __result Start of output range.
5430 * @return End of the output range.
5431 * @ingroup set_algorithms
5432 *
5433 * This operation iterates over both ranges, copying elements present in
5434 * the first range but not the second in order to the output range.
5435 * Iterators increment for each range. When the current element of the
5436 * first range is less than the second, that element is copied and the
5437 * iterator advances. If the current element of the second range is less,
5438 * the iterator advances, but no element is copied. If an element is
5439 * contained in both ranges, no elements are copied and both ranges
5440 * advance. The output range may not overlap either input range.
5441 */
5442 template<typename _InputIterator1, typename _InputIterator2,
5443 typename _OutputIterator>
5444 _GLIBCXX20_CONSTEXPR
5445 inline _OutputIterator
5446 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5447 _InputIterator2 __first2, _InputIterator2 __last2,
5448 _OutputIterator __result)
5449 {
5450 // concept requirements
5451 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5452 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5453 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5455 __glibcxx_function_requires(_LessThanOpConcept<
5458 __glibcxx_function_requires(_LessThanOpConcept<
5461 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5462 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5463 __glibcxx_requires_irreflexive2(__first1, __last1);
5464 __glibcxx_requires_irreflexive2(__first2, __last2);
5465
5466 return _GLIBCXX_STD_A::__set_difference(__first1, __last1,
5467 __first2, __last2, __result,
5468 __gnu_cxx::__ops::__iter_less_iter());
5469 }
5470
5471 /**
5472 * @brief Return the difference of two sorted ranges using comparison
5473 * functor.
5474 * @ingroup set_algorithms
5475 * @param __first1 Start of first range.
5476 * @param __last1 End of first range.
5477 * @param __first2 Start of second range.
5478 * @param __last2 End of second range.
5479 * @param __result Start of output range.
5480 * @param __comp The comparison functor.
5481 * @return End of the output range.
5482 * @ingroup set_algorithms
5483 *
5484 * This operation iterates over both ranges, copying elements present in
5485 * the first range but not the second in order to the output range.
5486 * Iterators increment for each range. When the current element of the
5487 * first range is less than the second according to @p __comp, that element
5488 * is copied and the iterator advances. If the current element of the
5489 * second range is less, no element is copied and the iterator advances.
5490 * If an element is contained in both ranges according to @p __comp, no
5491 * elements are copied and both ranges advance. The output range may not
5492 * overlap either input range.
5493 */
5494 template<typename _InputIterator1, typename _InputIterator2,
5495 typename _OutputIterator, typename _Compare>
5496 _GLIBCXX20_CONSTEXPR
5497 inline _OutputIterator
5498 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5499 _InputIterator2 __first2, _InputIterator2 __last2,
5500 _OutputIterator __result, _Compare __comp)
5501 {
5502 // concept requirements
5503 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5504 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5505 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5507 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5510 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5513 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5514 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5515 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
5516 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
5517
5518 return _GLIBCXX_STD_A::__set_difference(__first1, __last1,
5519 __first2, __last2, __result,
5520 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5521 }
5522
5523 template<typename _InputIterator1, typename _InputIterator2,
5524 typename _OutputIterator,
5525 typename _Compare>
5526 _GLIBCXX20_CONSTEXPR
5527 _OutputIterator
5528 __set_symmetric_difference(_InputIterator1 __first1,
5529 _InputIterator1 __last1,
5530 _InputIterator2 __first2,
5531 _InputIterator2 __last2,
5532 _OutputIterator __result,
5533 _Compare __comp)
5534 {
5535 while (__first1 != __last1 && __first2 != __last2)
5536 if (__comp(__first1, __first2))
5537 {
5538 *__result = *__first1;
5539 ++__first1;
5540 ++__result;
5541 }
5542 else if (__comp(__first2, __first1))
5543 {
5544 *__result = *__first2;
5545 ++__first2;
5546 ++__result;
5547 }
5548 else
5549 {
5550 ++__first1;
5551 ++__first2;
5552 }
5553 return std::copy(__first2, __last2,
5554 std::copy(__first1, __last1, __result));
5555 }
5556
5557 /**
5558 * @brief Return the symmetric difference of two sorted ranges.
5559 * @ingroup set_algorithms
5560 * @param __first1 Start of first range.
5561 * @param __last1 End of first range.
5562 * @param __first2 Start of second range.
5563 * @param __last2 End of second range.
5564 * @param __result Start of output range.
5565 * @return End of the output range.
5566 * @ingroup set_algorithms
5567 *
5568 * This operation iterates over both ranges, copying elements present in
5569 * one range but not the other in order to the output range. Iterators
5570 * increment for each range. When the current element of one range is less
5571 * than the other, that element is copied and the iterator advances. If an
5572 * element is contained in both ranges, no elements are copied and both
5573 * ranges advance. The output range may not overlap either input range.
5574 */
5575 template<typename _InputIterator1, typename _InputIterator2,
5576 typename _OutputIterator>
5577 _GLIBCXX20_CONSTEXPR
5578 inline _OutputIterator
5579 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5580 _InputIterator2 __first2, _InputIterator2 __last2,
5581 _OutputIterator __result)
5582 {
5583 // concept requirements
5584 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5585 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5586 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5588 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5590 __glibcxx_function_requires(_LessThanOpConcept<
5593 __glibcxx_function_requires(_LessThanOpConcept<
5596 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5597 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5598 __glibcxx_requires_irreflexive2(__first1, __last1);
5599 __glibcxx_requires_irreflexive2(__first2, __last2);
5600
5601 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1,
5602 __first2, __last2, __result,
5603 __gnu_cxx::__ops::__iter_less_iter());
5604 }
5605
5606 /**
5607 * @brief Return the symmetric difference of two sorted ranges using
5608 * comparison functor.
5609 * @ingroup set_algorithms
5610 * @param __first1 Start of first range.
5611 * @param __last1 End of first range.
5612 * @param __first2 Start of second range.
5613 * @param __last2 End of second range.
5614 * @param __result Start of output range.
5615 * @param __comp The comparison functor.
5616 * @return End of the output range.
5617 * @ingroup set_algorithms
5618 *
5619 * This operation iterates over both ranges, copying elements present in
5620 * one range but not the other in order to the output range. Iterators
5621 * increment for each range. When the current element of one range is less
5622 * than the other according to @p comp, that element is copied and the
5623 * iterator advances. If an element is contained in both ranges according
5624 * to @p __comp, no elements are copied and both ranges advance. The output
5625 * range may not overlap either input range.
5626 */
5627 template<typename _InputIterator1, typename _InputIterator2,
5628 typename _OutputIterator, typename _Compare>
5629 _GLIBCXX20_CONSTEXPR
5630 inline _OutputIterator
5631 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5632 _InputIterator2 __first2, _InputIterator2 __last2,
5633 _OutputIterator __result,
5634 _Compare __comp)
5635 {
5636 // concept requirements
5637 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5638 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5639 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5641 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5643 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5646 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5649 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5650 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5651 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
5652 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
5653
5654 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1,
5655 __first2, __last2, __result,
5656 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5657 }
5658
5659 template<typename _ForwardIterator, typename _Compare>
5660 _GLIBCXX14_CONSTEXPR
5661 _ForwardIterator
5662 __min_element(_ForwardIterator __first, _ForwardIterator __last,
5663 _Compare __comp)
5664 {
5665 if (__first == __last)
5666 return __first;
5667 _ForwardIterator __result = __first;
5668 while (++__first != __last)
5669 if (__comp(__first, __result))
5670 __result = __first;
5671 return __result;
5672 }
5673
5674 /**
5675 * @brief Return the minimum element in a range.
5676 * @ingroup sorting_algorithms
5677 * @param __first Start of range.
5678 * @param __last End of range.
5679 * @return Iterator referencing the first instance of the smallest value.
5680 */
5681 template<typename _ForwardIterator>
5682 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
5683 _ForwardIterator
5684 inline min_element(_ForwardIterator __first, _ForwardIterator __last)
5685 {
5686 // concept requirements
5687 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5688 __glibcxx_function_requires(_LessThanComparableConcept<
5690 __glibcxx_requires_valid_range(__first, __last);
5691 __glibcxx_requires_irreflexive(__first, __last);
5692
5693 return _GLIBCXX_STD_A::__min_element(__first, __last,
5694 __gnu_cxx::__ops::__iter_less_iter());
5695 }
5696
5697 /**
5698 * @brief Return the minimum element in a range using comparison functor.
5699 * @ingroup sorting_algorithms
5700 * @param __first Start of range.
5701 * @param __last End of range.
5702 * @param __comp Comparison functor.
5703 * @return Iterator referencing the first instance of the smallest value
5704 * according to __comp.
5705 */
5706 template<typename _ForwardIterator, typename _Compare>
5707 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
5708 inline _ForwardIterator
5709 min_element(_ForwardIterator __first, _ForwardIterator __last,
5710 _Compare __comp)
5711 {
5712 // concept requirements
5713 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5714 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5717 __glibcxx_requires_valid_range(__first, __last);
5718 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
5719
5720 return _GLIBCXX_STD_A::__min_element(__first, __last,
5721 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5722 }
5723
5724 template<typename _ForwardIterator, typename _Compare>
5725 _GLIBCXX14_CONSTEXPR
5726 _ForwardIterator
5727 __max_element(_ForwardIterator __first, _ForwardIterator __last,
5728 _Compare __comp)
5729 {
5730 if (__first == __last) return __first;
5731 _ForwardIterator __result = __first;
5732 while (++__first != __last)
5733 if (__comp(__result, __first))
5734 __result = __first;
5735 return __result;
5736 }
5737
5738 /**
5739 * @brief Return the maximum element in a range.
5740 * @ingroup sorting_algorithms
5741 * @param __first Start of range.
5742 * @param __last End of range.
5743 * @return Iterator referencing the first instance of the largest value.
5744 */
5745 template<typename _ForwardIterator>
5746 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
5747 inline _ForwardIterator
5748 max_element(_ForwardIterator __first, _ForwardIterator __last)
5749 {
5750 // concept requirements
5751 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5752 __glibcxx_function_requires(_LessThanComparableConcept<
5754 __glibcxx_requires_valid_range(__first, __last);
5755 __glibcxx_requires_irreflexive(__first, __last);
5756
5757 return _GLIBCXX_STD_A::__max_element(__first, __last,
5758 __gnu_cxx::__ops::__iter_less_iter());
5759 }
5760
5761 /**
5762 * @brief Return the maximum element in a range using comparison functor.
5763 * @ingroup sorting_algorithms
5764 * @param __first Start of range.
5765 * @param __last End of range.
5766 * @param __comp Comparison functor.
5767 * @return Iterator referencing the first instance of the largest value
5768 * according to __comp.
5769 */
5770 template<typename _ForwardIterator, typename _Compare>
5771 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
5772 inline _ForwardIterator
5773 max_element(_ForwardIterator __first, _ForwardIterator __last,
5774 _Compare __comp)
5775 {
5776 // concept requirements
5777 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5778 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5781 __glibcxx_requires_valid_range(__first, __last);
5782 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
5783
5784 return _GLIBCXX_STD_A::__max_element(__first, __last,
5785 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5786 }
5787
5788#if __cplusplus >= 201103L
5789 // N2722 + DR 915.
5790 template<typename _Tp>
5791 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
5792 inline _Tp
5793 min(initializer_list<_Tp> __l)
5794 {
5795 __glibcxx_requires_irreflexive(__l.begin(), __l.end());
5796 return *_GLIBCXX_STD_A::__min_element(__l.begin(), __l.end(),
5797 __gnu_cxx::__ops::__iter_less_iter());
5798 }
5799
5800 template<typename _Tp, typename _Compare>
5801 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
5802 inline _Tp
5803 min(initializer_list<_Tp> __l, _Compare __comp)
5804 {
5805 __glibcxx_requires_irreflexive_pred(__l.begin(), __l.end(), __comp);
5806 return *_GLIBCXX_STD_A::__min_element(__l.begin(), __l.end(),
5807 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5808 }
5809
5810 template<typename _Tp>
5811 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
5812 inline _Tp
5814 {
5815 __glibcxx_requires_irreflexive(__l.begin(), __l.end());
5816 return *_GLIBCXX_STD_A::__max_element(__l.begin(), __l.end(),
5817 __gnu_cxx::__ops::__iter_less_iter());
5818 }
5819
5820 template<typename _Tp, typename _Compare>
5821 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
5822 inline _Tp
5823 max(initializer_list<_Tp> __l, _Compare __comp)
5824 {
5825 __glibcxx_requires_irreflexive_pred(__l.begin(), __l.end(), __comp);
5826 return *_GLIBCXX_STD_A::__max_element(__l.begin(), __l.end(),
5827 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5828 }
5829#endif // C++11
5830
5831#if __cplusplus >= 201402L // C++17 std::sample and C++14 experimental::sample
5832 /// Reservoir sampling algorithm.
5833 template<typename _InputIterator, typename _RandomAccessIterator,
5834 typename _Size, typename _UniformRandomBitGenerator>
5835 _RandomAccessIterator
5836 __sample(_InputIterator __first, _InputIterator __last, input_iterator_tag,
5837 _RandomAccessIterator __out, random_access_iterator_tag,
5838 _Size __n, _UniformRandomBitGenerator&& __g)
5839 {
5840 using __distrib_type = uniform_int_distribution<_Size>;
5841 using __param_type = typename __distrib_type::param_type;
5842 __distrib_type __d{};
5843 _Size __sample_sz = 0;
5844 while (__first != __last && __sample_sz != __n)
5845 {
5846 __out[__sample_sz++] = *__first;
5847 ++__first;
5848 }
5849 for (auto __pop_sz = __sample_sz; __first != __last;
5850 ++__first, (void) ++__pop_sz)
5851 {
5852 const auto __k = __d(__g, __param_type{0, __pop_sz});
5853 if (__k < __n)
5854 __out[__k] = *__first;
5855 }
5856 return __out + __sample_sz;
5857 }
5858
5859 /// Selection sampling algorithm.
5860 template<typename _ForwardIterator, typename _OutputIterator, typename _Cat,
5861 typename _Size, typename _UniformRandomBitGenerator>
5862 _OutputIterator
5863 __sample(_ForwardIterator __first, _ForwardIterator __last,
5865 _OutputIterator __out, _Cat,
5866 _Size __n, _UniformRandomBitGenerator&& __g)
5867 {
5868 using __distrib_type = uniform_int_distribution<_Size>;
5869 using __param_type = typename __distrib_type::param_type;
5870 using _USize = make_unsigned_t<_Size>;
5873
5874 if (__first == __last)
5875 return __out;
5876
5877 __distrib_type __d{};
5878 _Size __unsampled_sz = std::distance(__first, __last);
5879 __n = std::min(__n, __unsampled_sz);
5880
5881 // If possible, we use __gen_two_uniform_ints to efficiently produce
5882 // two random numbers using a single distribution invocation:
5883
5884 const __uc_type __urngrange = __g.max() - __g.min();
5885 if (__urngrange / __uc_type(__unsampled_sz) >= __uc_type(__unsampled_sz))
5886 // I.e. (__urngrange >= __unsampled_sz * __unsampled_sz) but without
5887 // wrapping issues.
5888 {
5889 while (__n != 0 && __unsampled_sz >= 2)
5890 {
5891 const pair<_Size, _Size> __p =
5892 __gen_two_uniform_ints(__unsampled_sz, __unsampled_sz - 1, __g);
5893
5894 --__unsampled_sz;
5895 if (__p.first < __n)
5896 {
5897 *__out++ = *__first;
5898 --__n;
5899 }
5900
5901 ++__first;
5902
5903 if (__n == 0) break;
5904
5905 --__unsampled_sz;
5906 if (__p.second < __n)
5907 {
5908 *__out++ = *__first;
5909 --__n;
5910 }
5911
5912 ++__first;
5913 }
5914 }
5915
5916 // The loop above is otherwise equivalent to this one-at-a-time version:
5917
5918 for (; __n != 0; ++__first)
5919 if (__d(__g, __param_type{0, --__unsampled_sz}) < __n)
5920 {
5921 *__out++ = *__first;
5922 --__n;
5923 }
5924 return __out;
5925 }
5926#endif // C++14
5927
5928#ifdef __glibcxx_sample // C++ >= 17
5929 /// Take a random sample from a population.
5930 template<typename _PopulationIterator, typename _SampleIterator,
5931 typename _Distance, typename _UniformRandomBitGenerator>
5932 _SampleIterator
5933 sample(_PopulationIterator __first, _PopulationIterator __last,
5934 _SampleIterator __out, _Distance __n,
5935 _UniformRandomBitGenerator&& __g)
5936 {
5937 using __pop_cat = typename
5939 using __samp_cat = typename
5941
5942 static_assert(
5943 __or_<is_convertible<__pop_cat, forward_iterator_tag>,
5944 is_convertible<__samp_cat, random_access_iterator_tag>>::value,
5945 "output range must use a RandomAccessIterator when input range"
5946 " does not meet the ForwardIterator requirements");
5947
5948 static_assert(is_integral<_Distance>::value,
5949 "sample size must be an integer type");
5950
5952 return _GLIBCXX_STD_A::
5953 __sample(__first, __last, __pop_cat{}, __out, __samp_cat{}, __d,
5955 }
5956#endif // __glibcxx_sample
5957
5958_GLIBCXX_END_NAMESPACE_ALGO
5959_GLIBCXX_END_NAMESPACE_VERSION
5960} // namespace std
5961
5962#pragma GCC diagnostic pop
5963
5964#endif /* _STL_ALGO_H */
constexpr _Tp * to_address(_Tp *__ptr) noexcept
Obtain address referenced by a pointer to an object.
Definition ptr_traits.h:232
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
Definition type_traits:1843
typename common_type< _Tp... >::type common_type_t
Alias template for common_type.
Definition type_traits:2905
typename make_unsigned< _Tp >::type make_unsigned_t
Alias template for make_unsigned.
Definition type_traits:2203
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs are equal iff their members are equal.
constexpr pair< typename __decay_and_strip< _T1 >::__type, typename __decay_and_strip< _T2 >::__type > make_pair(_T1 &&__x, _T2 &&__y)
A convenience wrapper for creating a pair from two objects.
Definition stl_pair.h:1166
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr _Tp * __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:3836
constexpr const _Tp & clamp(const _Tp &, const _Tp &, const _Tp &)
Returns the value clamped between lo and hi.
Definition stl_algo.h:3654
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:3318
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:2340
_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:5836
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:2378
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:2455
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:3704
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:2773
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:1393
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:2271
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:5933
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:2297
_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:1457
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:2636
initializer_list
is_integral
Definition type_traits:486
common_type
Definition type_traits:2530
constexpr iterator_type base() const noexcept(/*conditional */)
Struct holding two objects of arbitrary type.
Definition stl_pair.h:304
_T1 first
The first member.
Definition stl_pair.h:308
_T2 second
The second member.
Definition stl_pair.h:309
Marking input iterators.
Forward iterators support a superset of input iterator operations.
Bidirectional iterators support a superset of forward iterator operations.
Random-access iterators support a superset of bidirectional iterator operations.
Traits class for iterators.
Uniform discrete distribution for random numbers. A discrete random distribution on the range with e...