libstdc++: stl_algo.h Source File

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