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