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