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 std::__iterator_category(__first));
115 }
116
117 /// Like find_if_not(), but uses and updates a count of the
118 /// remaining range length instead of comparing against an end
119 /// iterator.
120 template<typename _InputIterator, typename _Predicate, typename _Distance>
121 _GLIBCXX20_CONSTEXPR
122 _InputIterator
123 __find_if_not_n(_InputIterator __first, _Distance& __len, _Predicate __pred)
124 {
125 for (; __len; --__len, (void) ++__first)
126 if (!__pred(__first))
127 break;
128 return __first;
129 }
130
131 // set_difference
132 // set_intersection
133 // set_symmetric_difference
134 // set_union
135 // for_each
136 // find
137 // find_if
138 // find_first_of
139 // adjacent_find
140 // count
141 // count_if
142 // search
143 // search_n
144
145 /**
146 * This is an helper function for search_n overloaded for forward iterators.
147 */
148 template<typename _ForwardIterator, typename _Integer,
149 typename _UnaryPredicate>
150 _GLIBCXX20_CONSTEXPR
151 _ForwardIterator
152 __search_n_aux(_ForwardIterator __first, _ForwardIterator __last,
153 _Integer __count, _UnaryPredicate __unary_pred,
155 {
156 __first = std::__find_if(__first, __last, __unary_pred);
157 while (__first != __last)
158 {
160 __n = __count;
161 _ForwardIterator __i = __first;
162 ++__i;
163 while (__i != __last && __n != 1 && __unary_pred(__i))
164 {
165 ++__i;
166 --__n;
167 }
168 if (__n == 1)
169 return __first;
170 if (__i == __last)
171 return __last;
172 __first = std::__find_if(++__i, __last, __unary_pred);
173 }
174 return __last;
175 }
176
177 /**
178 * This is an helper function for search_n overloaded for random access
179 * iterators.
180 */
181 template<typename _RandomAccessIter, typename _Integer,
182 typename _UnaryPredicate>
183 _GLIBCXX20_CONSTEXPR
184 _RandomAccessIter
185 __search_n_aux(_RandomAccessIter __first, _RandomAccessIter __last,
186 _Integer __count, _UnaryPredicate __unary_pred,
188 {
190 _DistanceType;
191
192 _DistanceType __tailSize = __last - __first;
193 _DistanceType __remainder = __count;
194
195 while (__remainder <= __tailSize) // the main loop...
196 {
197 __first += __remainder;
198 __tailSize -= __remainder;
199 // __first here is always pointing to one past the last element of
200 // next possible match.
201 _RandomAccessIter __backTrack = __first;
202 while (__unary_pred(--__backTrack))
203 {
204 if (--__remainder == 0)
205 return (__first - __count); // Success
206 }
207 __remainder = __count + 1 - (__first - __backTrack);
208 }
209 return __last; // Failure
210 }
211
212 template<typename _ForwardIterator, typename _Integer,
213 typename _UnaryPredicate>
214 _GLIBCXX20_CONSTEXPR
215 _ForwardIterator
216 __search_n(_ForwardIterator __first, _ForwardIterator __last,
217 _Integer __count,
218 _UnaryPredicate __unary_pred)
219 {
220 if (__count <= 0)
221 return __first;
222
223 if (__count == 1)
224 return std::__find_if(__first, __last, __unary_pred);
225
226 return std::__search_n_aux(__first, __last, __count, __unary_pred,
227 std::__iterator_category(__first));
228 }
229
230 // find_end for forward iterators.
231 template<typename _ForwardIterator1, typename _ForwardIterator2,
232 typename _BinaryPredicate>
233 _GLIBCXX20_CONSTEXPR
234 _ForwardIterator1
235 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
236 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
238 _BinaryPredicate __comp)
239 {
240 if (__first2 == __last2)
241 return __last1;
242
243 _ForwardIterator1 __result = __last1;
244 while (1)
245 {
246 _ForwardIterator1 __new_result
247 = std::__search(__first1, __last1, __first2, __last2, __comp);
248 if (__new_result == __last1)
249 return __result;
250 else
251 {
252 __result = __new_result;
253 __first1 = __new_result;
254 ++__first1;
255 }
256 }
257 }
258
259 // find_end for bidirectional iterators (much faster).
260 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
261 typename _BinaryPredicate>
262 _GLIBCXX20_CONSTEXPR
263 _BidirectionalIterator1
264 __find_end(_BidirectionalIterator1 __first1,
265 _BidirectionalIterator1 __last1,
266 _BidirectionalIterator2 __first2,
267 _BidirectionalIterator2 __last2,
269 _BinaryPredicate __comp)
270 {
271 // concept requirements
272 __glibcxx_function_requires(_BidirectionalIteratorConcept<
273 _BidirectionalIterator1>)
274 __glibcxx_function_requires(_BidirectionalIteratorConcept<
275 _BidirectionalIterator2>)
276
277 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
278 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
279
280 _RevIterator1 __rlast1(__first1);
281 _RevIterator2 __rlast2(__first2);
282 _RevIterator1 __rresult = std::__search(_RevIterator1(__last1), __rlast1,
283 _RevIterator2(__last2), __rlast2,
284 __comp);
285
286 if (__rresult == __rlast1)
287 return __last1;
288 else
289 {
290 _BidirectionalIterator1 __result = __rresult.base();
291 std::advance(__result, -std::distance(__first2, __last2));
292 return __result;
293 }
294 }
295
296 /**
297 * @brief Find last matching subsequence in a sequence.
298 * @ingroup non_mutating_algorithms
299 * @param __first1 Start of range to search.
300 * @param __last1 End of range to search.
301 * @param __first2 Start of sequence to match.
302 * @param __last2 End of sequence to match.
303 * @return The last iterator @c i in the range
304 * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) ==
305 * @p *(__first2+N) for each @c N in the range @p
306 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
307 *
308 * Searches the range @p [__first1,__last1) for a sub-sequence that
309 * compares equal value-by-value with the sequence given by @p
310 * [__first2,__last2) and returns an iterator to the __first
311 * element of the sub-sequence, or @p __last1 if the sub-sequence
312 * is not found. The sub-sequence will be the last such
313 * subsequence contained in [__first1,__last1).
314 *
315 * Because the sub-sequence must lie completely within the range @p
316 * [__first1,__last1) it must start at a position less than @p
317 * __last1-(__last2-__first2) where @p __last2-__first2 is the
318 * length of the sub-sequence. This means that the returned
319 * iterator @c i will be in the range @p
320 * [__first1,__last1-(__last2-__first2))
321 */
322 template<typename _ForwardIterator1, typename _ForwardIterator2>
323 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
324 inline _ForwardIterator1
325 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
326 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
327 {
328 // concept requirements
329 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
330 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
331 __glibcxx_function_requires(_EqualOpConcept<
334 __glibcxx_requires_valid_range(__first1, __last1);
335 __glibcxx_requires_valid_range(__first2, __last2);
336
337 return std::__find_end(__first1, __last1, __first2, __last2,
338 std::__iterator_category(__first1),
339 std::__iterator_category(__first2),
340 __gnu_cxx::__ops::__iter_equal_to_iter());
341 }
342
343 /**
344 * @brief Find last matching subsequence in a sequence using a predicate.
345 * @ingroup non_mutating_algorithms
346 * @param __first1 Start of range to search.
347 * @param __last1 End of range to search.
348 * @param __first2 Start of sequence to match.
349 * @param __last2 End of sequence to match.
350 * @param __comp The predicate to use.
351 * @return The last iterator @c i in the range @p
352 * [__first1,__last1-(__last2-__first2)) such that @c
353 * predicate(*(i+N), @p (__first2+N)) is true for each @c N in the
354 * range @p [0,__last2-__first2), or @p __last1 if no such iterator
355 * exists.
356 *
357 * Searches the range @p [__first1,__last1) for a sub-sequence that
358 * compares equal value-by-value with the sequence given by @p
359 * [__first2,__last2) using comp as a predicate and returns an
360 * iterator to the first element of the sub-sequence, or @p __last1
361 * if the sub-sequence is not found. The sub-sequence will be the
362 * last such subsequence contained in [__first,__last1).
363 *
364 * Because the sub-sequence must lie completely within the range @p
365 * [__first1,__last1) it must start at a position less than @p
366 * __last1-(__last2-__first2) where @p __last2-__first2 is the
367 * length of the sub-sequence. This means that the returned
368 * iterator @c i will be in the range @p
369 * [__first1,__last1-(__last2-__first2))
370 */
371 template<typename _ForwardIterator1, typename _ForwardIterator2,
372 typename _BinaryPredicate>
373 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
374 inline _ForwardIterator1
375 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
376 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
377 _BinaryPredicate __comp)
378 {
379 // concept requirements
380 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
381 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
382 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
385 __glibcxx_requires_valid_range(__first1, __last1);
386 __glibcxx_requires_valid_range(__first2, __last2);
387
388 return std::__find_end(__first1, __last1, __first2, __last2,
389 std::__iterator_category(__first1),
390 std::__iterator_category(__first2),
391 __gnu_cxx::__ops::__iter_comp_iter(__comp));
392 }
393
394#if __cplusplus >= 201103L
395 /**
396 * @brief Checks that a predicate is true for all the elements
397 * of a sequence.
398 * @ingroup non_mutating_algorithms
399 * @param __first An input iterator.
400 * @param __last An input iterator.
401 * @param __pred A predicate.
402 * @return True if the check is true, false otherwise.
403 *
404 * Returns true if @p __pred is true for each element in the range
405 * @p [__first,__last), and false otherwise.
406 */
407 template<typename _InputIterator, typename _Predicate>
408 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
409 inline bool
410 all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
411 { return __last == std::find_if_not(__first, __last, __pred); }
412
413 /**
414 * @brief Checks that a predicate is false for all the elements
415 * of a sequence.
416 * @ingroup non_mutating_algorithms
417 * @param __first An input iterator.
418 * @param __last An input iterator.
419 * @param __pred A predicate.
420 * @return True if the check is true, false otherwise.
421 *
422 * Returns true if @p __pred is false for each element in the range
423 * @p [__first,__last), and false otherwise.
424 */
425 template<typename _InputIterator, typename _Predicate>
426 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
427 inline bool
428 none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
429 { return __last == _GLIBCXX_STD_A::find_if(__first, __last, __pred); }
430
431 /**
432 * @brief Checks that a predicate is true for at least one element
433 * of a sequence.
434 * @ingroup non_mutating_algorithms
435 * @param __first An input iterator.
436 * @param __last An input iterator.
437 * @param __pred A predicate.
438 * @return True if the check is true, false otherwise.
439 *
440 * Returns true if an element exists in the range @p
441 * [__first,__last) such that @p __pred is true, and false
442 * otherwise.
443 */
444 template<typename _InputIterator, typename _Predicate>
445 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
446 inline bool
447 any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
448 { return !std::none_of(__first, __last, __pred); }
449
450 /**
451 * @brief Find the first element in a sequence for which a
452 * predicate is false.
453 * @ingroup non_mutating_algorithms
454 * @param __first An input iterator.
455 * @param __last An input iterator.
456 * @param __pred A predicate.
457 * @return The first iterator @c i in the range @p [__first,__last)
458 * such that @p __pred(*i) is false, or @p __last if no such iterator exists.
459 */
460 template<typename _InputIterator, typename _Predicate>
461 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
462 inline _InputIterator
463 find_if_not(_InputIterator __first, _InputIterator __last,
464 _Predicate __pred)
465 {
466 // concept requirements
467 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
468 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
470 __glibcxx_requires_valid_range(__first, __last);
471 return std::__find_if_not(__first, __last,
472 __gnu_cxx::__ops::__pred_iter(__pred));
473 }
474
475 /**
476 * @brief Checks whether the sequence is partitioned.
477 * @ingroup mutating_algorithms
478 * @param __first An input iterator.
479 * @param __last An input iterator.
480 * @param __pred A predicate.
481 * @return True if the range @p [__first,__last) is partioned by @p __pred,
482 * i.e. if all elements that satisfy @p __pred appear before those that
483 * do not.
484 */
485 template<typename _InputIterator, typename _Predicate>
486 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
487 inline bool
488 is_partitioned(_InputIterator __first, _InputIterator __last,
489 _Predicate __pred)
490 {
491 __first = std::find_if_not(__first, __last, __pred);
492 if (__first == __last)
493 return true;
494 ++__first;
495 return std::none_of(__first, __last, __pred);
496 }
497
498 /**
499 * @brief Find the partition point of a partitioned range.
500 * @ingroup mutating_algorithms
501 * @param __first An iterator.
502 * @param __last Another iterator.
503 * @param __pred A predicate.
504 * @return An iterator @p mid such that @p all_of(__first, mid, __pred)
505 * and @p none_of(mid, __last, __pred) are both true.
506 */
507 template<typename _ForwardIterator, typename _Predicate>
508 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
509 _ForwardIterator
510 partition_point(_ForwardIterator __first, _ForwardIterator __last,
511 _Predicate __pred)
512 {
513 // concept requirements
514 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
515 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
517
518 // A specific debug-mode test will be necessary...
519 __glibcxx_requires_valid_range(__first, __last);
520
522 _DistanceType;
523
524 _DistanceType __len = std::distance(__first, __last);
525
526 while (__len > 0)
527 {
528 _DistanceType __half = __len >> 1;
529 _ForwardIterator __middle = __first;
530 std::advance(__middle, __half);
531 if (__pred(*__middle))
532 {
533 __first = __middle;
534 ++__first;
535 __len = __len - __half - 1;
536 }
537 else
538 __len = __half;
539 }
540 return __first;
541 }
542#endif
543
544 template<typename _InputIterator, typename _OutputIterator,
545 typename _Predicate>
546 _GLIBCXX20_CONSTEXPR
547 _OutputIterator
548 __remove_copy_if(_InputIterator __first, _InputIterator __last,
549 _OutputIterator __result, _Predicate __pred)
550 {
551 for (; __first != __last; ++__first)
552 if (!__pred(__first))
553 {
554 *__result = *__first;
555 ++__result;
556 }
557 return __result;
558 }
559
560 /**
561 * @brief Copy a sequence, removing elements of a given value.
562 * @ingroup mutating_algorithms
563 * @param __first An input iterator.
564 * @param __last An input iterator.
565 * @param __result An output iterator.
566 * @param __value The value to be removed.
567 * @return An iterator designating the end of the resulting sequence.
568 *
569 * Copies each element in the range @p [__first,__last) not equal
570 * to @p __value to the range beginning at @p __result.
571 * remove_copy() is stable, so the relative order of elements that
572 * are copied is unchanged.
573 */
574 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
575 _GLIBCXX20_CONSTEXPR
576 inline _OutputIterator
577 remove_copy(_InputIterator __first, _InputIterator __last,
578 _OutputIterator __result, const _Tp& __value)
579 {
580 // concept requirements
581 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
582 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
584 __glibcxx_function_requires(_EqualOpConcept<
586 __glibcxx_requires_valid_range(__first, __last);
587
588 return std::__remove_copy_if(__first, __last, __result,
589 __gnu_cxx::__ops::__iter_equals_val(__value));
590 }
591
592 /**
593 * @brief Copy a sequence, removing elements for which a predicate is true.
594 * @ingroup mutating_algorithms
595 * @param __first An input iterator.
596 * @param __last An input iterator.
597 * @param __result An output iterator.
598 * @param __pred A predicate.
599 * @return An iterator designating the end of the resulting sequence.
600 *
601 * Copies each element in the range @p [__first,__last) for which
602 * @p __pred returns false to the range beginning at @p __result.
603 *
604 * remove_copy_if() is stable, so the relative order of elements that are
605 * copied is unchanged.
606 */
607 template<typename _InputIterator, typename _OutputIterator,
608 typename _Predicate>
609 _GLIBCXX20_CONSTEXPR
610 inline _OutputIterator
611 remove_copy_if(_InputIterator __first, _InputIterator __last,
612 _OutputIterator __result, _Predicate __pred)
613 {
614 // concept requirements
615 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
616 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
618 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
620 __glibcxx_requires_valid_range(__first, __last);
621
622 return std::__remove_copy_if(__first, __last, __result,
623 __gnu_cxx::__ops::__pred_iter(__pred));
624 }
625
626#if __cplusplus >= 201103L
627 /**
628 * @brief Copy the elements of a sequence for which a predicate is true.
629 * @ingroup mutating_algorithms
630 * @param __first An input iterator.
631 * @param __last An input iterator.
632 * @param __result An output iterator.
633 * @param __pred A predicate.
634 * @return An iterator designating the end of the resulting sequence.
635 *
636 * Copies each element in the range @p [__first,__last) for which
637 * @p __pred returns true to the range beginning at @p __result.
638 *
639 * copy_if() is stable, so the relative order of elements that are
640 * copied is unchanged.
641 */
642 template<typename _InputIterator, typename _OutputIterator,
643 typename _Predicate>
644 _GLIBCXX20_CONSTEXPR
645 _OutputIterator
646 copy_if(_InputIterator __first, _InputIterator __last,
647 _OutputIterator __result, _Predicate __pred)
648 {
649 // concept requirements
650 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
651 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
653 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
655 __glibcxx_requires_valid_range(__first, __last);
656
657 for (; __first != __last; ++__first)
658 if (__pred(*__first))
659 {
660 *__result = *__first;
661 ++__result;
662 }
663 return __result;
664 }
665
666 template<typename _InputIterator, typename _Size, typename _OutputIterator>
667 _GLIBCXX20_CONSTEXPR
668 _OutputIterator
669 __copy_n(_InputIterator __first, _Size __n,
670 _OutputIterator __result, input_iterator_tag)
671 {
672 return std::__niter_wrap(__result,
673 __copy_n_a(__first, __n,
674 std::__niter_base(__result), true));
675 }
676
677 template<typename _RandomAccessIterator, typename _Size,
678 typename _OutputIterator>
679 _GLIBCXX20_CONSTEXPR
680 inline _OutputIterator
681 __copy_n(_RandomAccessIterator __first, _Size __n,
682 _OutputIterator __result, random_access_iterator_tag)
683 { return std::copy(__first, __first + __n, __result); }
684
685 /**
686 * @brief Copies the range [first,first+n) into [result,result+n).
687 * @ingroup mutating_algorithms
688 * @param __first An input iterator.
689 * @param __n The number of elements to copy.
690 * @param __result An output iterator.
691 * @return result+n.
692 *
693 * This inline function will boil down to a call to @c memmove whenever
694 * possible. Failing that, if random access iterators are passed, then the
695 * loop count will be known (and therefore a candidate for compiler
696 * optimizations such as unrolling).
697 */
698 template<typename _InputIterator, typename _Size, typename _OutputIterator>
699 _GLIBCXX20_CONSTEXPR
700 inline _OutputIterator
701 copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
702 {
703 // concept requirements
704 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
705 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
707
708 const auto __n2 = std::__size_to_integer(__n);
709 if (__n2 <= 0)
710 return __result;
711
712 __glibcxx_requires_can_increment(__first, __n2);
713 __glibcxx_requires_can_increment(__result, __n2);
714
715 return std::__copy_n(__first, __n2, __result,
716 std::__iterator_category(__first));
717 }
718
719 /**
720 * @brief Copy the elements of a sequence to separate output sequences
721 * depending on the truth value of a predicate.
722 * @ingroup mutating_algorithms
723 * @param __first An input iterator.
724 * @param __last An input iterator.
725 * @param __out_true An output iterator.
726 * @param __out_false An output iterator.
727 * @param __pred A predicate.
728 * @return A pair designating the ends of the resulting sequences.
729 *
730 * Copies each element in the range @p [__first,__last) for which
731 * @p __pred returns true to the range beginning at @p out_true
732 * and each element for which @p __pred returns false to @p __out_false.
733 */
734 template<typename _InputIterator, typename _OutputIterator1,
735 typename _OutputIterator2, typename _Predicate>
736 _GLIBCXX20_CONSTEXPR
738 partition_copy(_InputIterator __first, _InputIterator __last,
739 _OutputIterator1 __out_true, _OutputIterator2 __out_false,
740 _Predicate __pred)
741 {
742 // concept requirements
743 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
744 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator1,
746 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator2,
748 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
750 __glibcxx_requires_valid_range(__first, __last);
751
752 for (; __first != __last; ++__first)
753 if (__pred(*__first))
754 {
755 *__out_true = *__first;
756 ++__out_true;
757 }
758 else
759 {
760 *__out_false = *__first;
761 ++__out_false;
762 }
763
764 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
765 }
766#endif // C++11
767
768 /**
769 * @brief Remove elements from a sequence.
770 * @ingroup mutating_algorithms
771 * @param __first An input iterator.
772 * @param __last An input iterator.
773 * @param __value The value to be removed.
774 * @return An iterator designating the end of the resulting sequence.
775 *
776 * All elements equal to @p __value are removed from the range
777 * @p [__first,__last).
778 *
779 * remove() is stable, so the relative order of elements that are
780 * not removed is unchanged.
781 *
782 * Elements between the end of the resulting sequence and @p __last
783 * are still present, but their value is unspecified.
784 */
785 template<typename _ForwardIterator, typename _Tp>
786 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
787 inline _ForwardIterator
788 remove(_ForwardIterator __first, _ForwardIterator __last,
789 const _Tp& __value)
790 {
791 // concept requirements
792 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
793 _ForwardIterator>)
794 __glibcxx_function_requires(_EqualOpConcept<
796 __glibcxx_requires_valid_range(__first, __last);
797
798 return std::__remove_if(__first, __last,
799 __gnu_cxx::__ops::__iter_equals_val(__value));
800 }
801
802 /**
803 * @brief Remove elements from a sequence using a predicate.
804 * @ingroup mutating_algorithms
805 * @param __first A forward iterator.
806 * @param __last A forward iterator.
807 * @param __pred A predicate.
808 * @return An iterator designating the end of the resulting sequence.
809 *
810 * All elements for which @p __pred returns true are removed from the range
811 * @p [__first,__last).
812 *
813 * remove_if() is stable, so the relative order of elements that are
814 * not removed is unchanged.
815 *
816 * Elements between the end of the resulting sequence and @p __last
817 * are still present, but their value is unspecified.
818 */
819 template<typename _ForwardIterator, typename _Predicate>
820 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
821 inline _ForwardIterator
822 remove_if(_ForwardIterator __first, _ForwardIterator __last,
823 _Predicate __pred)
824 {
825 // concept requirements
826 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
827 _ForwardIterator>)
828 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
830 __glibcxx_requires_valid_range(__first, __last);
831
832 return std::__remove_if(__first, __last,
833 __gnu_cxx::__ops::__pred_iter(__pred));
834 }
835
836 template<typename _ForwardIterator, typename _BinaryPredicate>
837 _GLIBCXX20_CONSTEXPR
838 _ForwardIterator
839 __adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
840 _BinaryPredicate __binary_pred)
841 {
842 if (__first == __last)
843 return __last;
844 _ForwardIterator __next = __first;
845 while (++__next != __last)
846 {
847 if (__binary_pred(__first, __next))
848 return __first;
849 __first = __next;
850 }
851 return __last;
852 }
853
854 template<typename _ForwardIterator, typename _BinaryPredicate>
855 _GLIBCXX20_CONSTEXPR
856 _ForwardIterator
857 __unique(_ForwardIterator __first, _ForwardIterator __last,
858 _BinaryPredicate __binary_pred)
859 {
860 // Skip the beginning, if already unique.
861 __first = std::__adjacent_find(__first, __last, __binary_pred);
862 if (__first == __last)
863 return __last;
864
865 // Do the real copy work.
866 _ForwardIterator __dest = __first;
867 ++__first;
868 while (++__first != __last)
869 if (!__binary_pred(__dest, __first))
870 *++__dest = _GLIBCXX_MOVE(*__first);
871 return ++__dest;
872 }
873
874 /**
875 * @brief Remove consecutive duplicate values from a sequence.
876 * @ingroup mutating_algorithms
877 * @param __first A forward iterator.
878 * @param __last A forward iterator.
879 * @return An iterator designating the end of the resulting sequence.
880 *
881 * Removes all but the first element from each group of consecutive
882 * values that compare equal.
883 * unique() is stable, so the relative order of elements that are
884 * not removed is unchanged.
885 * Elements between the end of the resulting sequence and @p __last
886 * are still present, but their value is unspecified.
887 */
888 template<typename _ForwardIterator>
889 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
890 inline _ForwardIterator
891 unique(_ForwardIterator __first, _ForwardIterator __last)
892 {
893 // concept requirements
894 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
895 _ForwardIterator>)
896 __glibcxx_function_requires(_EqualityComparableConcept<
898 __glibcxx_requires_valid_range(__first, __last);
899
900 return std::__unique(__first, __last,
901 __gnu_cxx::__ops::__iter_equal_to_iter());
902 }
903
904 /**
905 * @brief Remove consecutive values from a sequence using a predicate.
906 * @ingroup mutating_algorithms
907 * @param __first A forward iterator.
908 * @param __last A forward iterator.
909 * @param __binary_pred A binary predicate.
910 * @return An iterator designating the end of the resulting sequence.
911 *
912 * Removes all but the first element from each group of consecutive
913 * values for which @p __binary_pred returns true.
914 * unique() is stable, so the relative order of elements that are
915 * not removed is unchanged.
916 * Elements between the end of the resulting sequence and @p __last
917 * are still present, but their value is unspecified.
918 */
919 template<typename _ForwardIterator, typename _BinaryPredicate>
920 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
921 inline _ForwardIterator
922 unique(_ForwardIterator __first, _ForwardIterator __last,
923 _BinaryPredicate __binary_pred)
924 {
925 // concept requirements
926 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
927 _ForwardIterator>)
928 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
931 __glibcxx_requires_valid_range(__first, __last);
932
933 return std::__unique(__first, __last,
934 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
935 }
936
937 // _GLIBCXX_RESOLVE_LIB_DEFECTS
938 // 4269. unique_copy passes arguments to its predicate backwards
939
940 // Implementation of std::unique_copy for forward iterators.
941 // This case is easy, just compare *i with *(i-1).
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,
948 forward_iterator_tag)
949 {
950 _ForwardIterator __prev = __first;
951 *__result = *__first;
952 while (++__first != __last)
953 if (!__binary_pred(__prev, __first))
954 {
955 *++__result = *__first;
956 __prev = __first;
957 }
958 return ++__result;
959 }
960
961 // Implementation of std::unique_copy for non-forward iterators,
962 // where we cannot compare with elements written to the output.
963 template<typename _InputIterator, typename _OutputIterator,
964 typename _BinaryPredicate>
965 _GLIBCXX20_CONSTEXPR
966 _OutputIterator
967 __unique_copy_1(_InputIterator __first, _InputIterator __last,
968 _OutputIterator __result, _BinaryPredicate __binary_pred,
969 __false_type)
970 {
972 _Val __value = *__first;
973 *__result = __value;
974 while (++__first != __last)
975 if (!__binary_pred(std::__addressof(__value), __first))
976 {
977 __value = *__first;
978 *++__result = __value;
979 }
980 return ++__result;
981 }
982
983 // Implementation of std::unique_copy for non-forward iterators,
984 // where we can compare with the last element written to the output.
985 template<typename _InputIterator, typename _ForwardIterator,
986 typename _BinaryPredicate>
987 _ForwardIterator
988 __unique_copy_1(_InputIterator __first, _InputIterator __last,
989 _ForwardIterator __result, _BinaryPredicate __binary_pred,
990 __true_type)
991 {
992 *__result = *__first;
993 while (++__first != __last)
994 if (!__binary_pred(__result, __first))
995 *++__result = *__first;
996 return ++__result;
997 }
998
999 // Implementation of std::unique_copy for non-forward iterators.
1000 // We cannot compare *i to *(i-1) so we need to either make a copy
1001 // or compare with the last element written to the output range.
1002 template<typename _InputIterator, typename _OutputIterator,
1003 typename _BinaryPredicate>
1004 _GLIBCXX20_CONSTEXPR
1005 _OutputIterator
1006 __unique_copy(_InputIterator __first, _InputIterator __last,
1007 _OutputIterator __result, _BinaryPredicate __binary_pred,
1009 {
1010 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1011 // 2439. unique_copy() sometimes can't fall back to reading its output
1012 typedef iterator_traits<_InputIterator> _InItTraits;
1013 typedef iterator_traits<_OutputIterator> _OutItTraits;
1014 typedef typename _OutItTraits::iterator_category _Cat;
1015 const bool __output_is_fwd = __is_base_of(forward_iterator_tag, _Cat);
1016 const bool __same_type = __is_same(typename _OutItTraits::value_type,
1017 typename _InItTraits::value_type);
1018 typedef __truth_type<__output_is_fwd && __same_type> __cmp_with_output;
1019 return std::__unique_copy_1(__first, __last, __result, __binary_pred,
1020 typename __cmp_with_output::__type());
1021 }
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 {
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 {
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
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
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
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 {
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
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
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
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
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
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());
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);
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#pragma GCC diagnostic push
3490#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
3491 template<typename _ForwardIterator1, typename _ForwardIterator2,
3492 typename _BinaryPredicate>
3493 _GLIBCXX20_CONSTEXPR
3494 bool
3495 __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3496 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
3497 _BinaryPredicate __pred)
3498 {
3499 using _Cat1
3500 = typename iterator_traits<_ForwardIterator1>::iterator_category;
3501 using _Cat2
3502 = typename iterator_traits<_ForwardIterator2>::iterator_category;
3503 using _It1_is_RA = is_same<_Cat1, random_access_iterator_tag>;
3504 using _It2_is_RA = is_same<_Cat2, random_access_iterator_tag>;
3505 constexpr bool __ra_iters = __and_<_It1_is_RA, _It2_is_RA>::value;
3506 if constexpr (__ra_iters)
3507 {
3508 if ((__last1 - __first1) != (__last2 - __first2))
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 constexpr (__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#pragma GCC diagnostic pop
3551
3552 /**
3553 * @brief Checks whether a permutaion of the second sequence is equal
3554 * to the first sequence.
3555 * @ingroup non_mutating_algorithms
3556 * @param __first1 Start of first range.
3557 * @param __last1 End of first range.
3558 * @param __first2 Start of second range.
3559 * @param __last2 End of first range.
3560 * @return true if there exists a permutation of the elements in the range
3561 * [__first2, __last2), beginning with ForwardIterator2 begin,
3562 * such that equal(__first1, __last1, begin) returns true;
3563 * otherwise, returns false.
3564 */
3565 template<typename _ForwardIterator1, typename _ForwardIterator2>
3566 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3567 inline bool
3568 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3569 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
3570 {
3571 __glibcxx_requires_valid_range(__first1, __last1);
3572 __glibcxx_requires_valid_range(__first2, __last2);
3573
3574 return
3575 std::__is_permutation(__first1, __last1, __first2, __last2,
3576 __gnu_cxx::__ops::__iter_equal_to_iter());
3577 }
3578
3579 /**
3580 * @brief Checks whether a permutation of the second sequence is equal
3581 * to the first sequence.
3582 * @ingroup non_mutating_algorithms
3583 * @param __first1 Start of first range.
3584 * @param __last1 End of first range.
3585 * @param __first2 Start of second range.
3586 * @param __last2 End of first range.
3587 * @param __pred A binary predicate.
3588 * @return true if there exists a permutation of the elements in the range
3589 * [__first2, __last2), beginning with ForwardIterator2 begin,
3590 * such that equal(__first1, __last1, __begin, __pred) returns true;
3591 * otherwise, returns false.
3592 */
3593 template<typename _ForwardIterator1, typename _ForwardIterator2,
3594 typename _BinaryPredicate>
3595 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3596 inline bool
3597 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
3598 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
3599 _BinaryPredicate __pred)
3600 {
3601 __glibcxx_requires_valid_range(__first1, __last1);
3602 __glibcxx_requires_valid_range(__first2, __last2);
3603
3604 return std::__is_permutation(__first1, __last1, __first2, __last2,
3605 __gnu_cxx::__ops::__iter_comp_iter(__pred));
3606 }
3607#endif // C++14
3608
3609#ifdef __glibcxx_clamp // C++ >= 17
3610 /**
3611 * @brief Returns the value clamped between lo and hi.
3612 * @ingroup sorting_algorithms
3613 * @param __val A value of arbitrary type.
3614 * @param __lo A lower limit of arbitrary type.
3615 * @param __hi An upper limit of arbitrary type.
3616 * @retval `__lo` if `__val < __lo`
3617 * @retval `__hi` if `__hi < __val`
3618 * @retval `__val` otherwise.
3619 * @pre `_Tp` is LessThanComparable and `(__hi < __lo)` is false.
3620 */
3621 template<typename _Tp>
3622 [[nodiscard]] constexpr const _Tp&
3623 clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi)
3624 {
3625 __glibcxx_assert(!(__hi < __lo));
3626 return std::min(std::max(__val, __lo), __hi);
3627 }
3628
3629 /**
3630 * @brief Returns the value clamped between lo and hi.
3631 * @ingroup sorting_algorithms
3632 * @param __val A value of arbitrary type.
3633 * @param __lo A lower limit of arbitrary type.
3634 * @param __hi An upper limit of arbitrary type.
3635 * @param __comp A comparison functor.
3636 * @retval `__lo` if `__comp(__val, __lo)`
3637 * @retval `__hi` if `__comp(__hi, __val)`
3638 * @retval `__val` otherwise.
3639 * @pre `__comp(__hi, __lo)` is false.
3640 */
3641 template<typename _Tp, typename _Compare>
3642 [[nodiscard]] constexpr const _Tp&
3643 clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
3644 {
3645 __glibcxx_assert(!__comp(__hi, __lo));
3646 return std::min(std::max(__val, __lo, __comp), __hi, __comp);
3647 }
3648#endif // __glibcxx_clamp
3649
3650 /**
3651 * @brief Generate two uniformly distributed integers using a
3652 * single distribution invocation.
3653 * @param __b0 The upper bound for the first integer.
3654 * @param __b1 The upper bound for the second integer.
3655 * @param __g A UniformRandomBitGenerator.
3656 * @return A pair (i, j) with i and j uniformly distributed
3657 * over [0, __b0) and [0, __b1), respectively.
3658 *
3659 * Requires: __b0 * __b1 <= __g.max() - __g.min().
3660 *
3661 * Using uniform_int_distribution with a range that is very
3662 * small relative to the range of the generator ends up wasting
3663 * potentially expensively generated randomness, since
3664 * uniform_int_distribution does not store leftover randomness
3665 * between invocations.
3666 *
3667 * If we know we want two integers in ranges that are sufficiently
3668 * small, we can compose the ranges, use a single distribution
3669 * invocation, and significantly reduce the waste.
3670 */
3671 template<typename _IntType, typename _UniformRandomBitGenerator>
3673 __gen_two_uniform_ints(_IntType __b0, _IntType __b1,
3674 _UniformRandomBitGenerator&& __g)
3675 {
3676 _IntType __x
3677 = uniform_int_distribution<_IntType>{0, (__b0 * __b1) - 1}(__g);
3678 return std::make_pair(__x / __b1, __x % __b1);
3679 }
3680
3681 /**
3682 * @brief Shuffle the elements of a sequence using a uniform random
3683 * number generator.
3684 * @ingroup mutating_algorithms
3685 * @param __first A forward iterator.
3686 * @param __last A forward iterator.
3687 * @param __g A UniformRandomNumberGenerator (26.5.1.3).
3688 * @return Nothing.
3689 *
3690 * Reorders the elements in the range @p [__first,__last) using @p __g to
3691 * provide random numbers.
3692 */
3693 template<typename _RandomAccessIterator,
3694 typename _UniformRandomNumberGenerator>
3695 void
3696 shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
3697 _UniformRandomNumberGenerator&& __g)
3698 {
3699 // concept requirements
3700 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
3701 _RandomAccessIterator>)
3702 __glibcxx_requires_valid_range(__first, __last);
3703
3704 if (__first == __last)
3705 return;
3706
3708 _DistanceType;
3709
3710 typedef typename std::make_unsigned<_DistanceType>::type __ud_type;
3711 typedef typename std::uniform_int_distribution<__ud_type> __distr_type;
3712 typedef typename __distr_type::param_type __p_type;
3713
3714 typedef typename remove_reference<_UniformRandomNumberGenerator>::type
3715 _Gen;
3717 __uc_type;
3718
3719 const __uc_type __urngrange = __g.max() - __g.min();
3720 const __uc_type __urange = __uc_type(__last - __first);
3721
3722 if (__urngrange / __urange >= __urange)
3723 // I.e. (__urngrange >= __urange * __urange) but without wrap issues.
3724 {
3725 _RandomAccessIterator __i = __first + 1;
3726
3727 // Since we know the range isn't empty, an even number of elements
3728 // means an uneven number of elements /to swap/, in which case we
3729 // do the first one up front:
3730
3731 if ((__urange % 2) == 0)
3732 {
3733 __distr_type __d{0, 1};
3734 std::iter_swap(__i++, __first + __d(__g));
3735 }
3736
3737 // Now we know that __last - __i is even, so we do the rest in pairs,
3738 // using a single distribution invocation to produce swap positions
3739 // for two successive elements at a time:
3740
3741 while (__i != __last)
3742 {
3743 const __uc_type __swap_range = __uc_type(__i - __first) + 1;
3744
3745 const pair<__uc_type, __uc_type> __pospos =
3746 __gen_two_uniform_ints(__swap_range, __swap_range + 1, __g);
3747
3748 std::iter_swap(__i++, __first + __pospos.first);
3749 std::iter_swap(__i++, __first + __pospos.second);
3750 }
3751
3752 return;
3753 }
3754
3755 __distr_type __d;
3756
3757 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
3758 std::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first)));
3759 }
3760#endif // C++11
3761
3762_GLIBCXX_BEGIN_NAMESPACE_ALGO
3763
3764 /**
3765 * @brief Apply a function to every element of a sequence.
3766 * @ingroup non_mutating_algorithms
3767 * @param __first An input iterator.
3768 * @param __last An input iterator.
3769 * @param __f A unary function object.
3770 * @return @p __f
3771 *
3772 * Applies the function object @p __f to each element in the range
3773 * @p [first,last). @p __f must not modify the order of the sequence.
3774 * If @p __f has a return value it is ignored.
3775 */
3776 template<typename _InputIterator, typename _Function>
3777 _GLIBCXX20_CONSTEXPR
3778 _Function
3779 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
3780 {
3781 // concept requirements
3782 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3783 __glibcxx_requires_valid_range(__first, __last);
3784 for (; __first != __last; ++__first)
3785 __f(*__first);
3786 return __f; // N.B. [alg.foreach] says std::move(f) but it's redundant.
3787 }
3788
3789#if __cplusplus >= 201703L
3790 /**
3791 * @brief Apply a function to every element of a sequence.
3792 * @ingroup non_mutating_algorithms
3793 * @param __first An input iterator.
3794 * @param __n A value convertible to an integer.
3795 * @param __f A unary function object.
3796 * @return `__first+__n`
3797 *
3798 * Applies the function object `__f` to each element in the range
3799 * `[first, first+n)`. `__f` must not modify the order of the sequence.
3800 * If `__f` has a return value it is ignored.
3801 */
3802 template<typename _InputIterator, typename _Size, typename _Function>
3803 _GLIBCXX20_CONSTEXPR
3804 _InputIterator
3805 for_each_n(_InputIterator __first, _Size __n, _Function __f)
3806 {
3807 auto __n2 = std::__size_to_integer(__n);
3809 if constexpr (is_base_of_v<random_access_iterator_tag, _Cat>)
3810 {
3811 if (__n2 <= 0)
3812 return __first;
3813 auto __last = __first + __n2;
3814 std::for_each(__first, __last, std::move(__f));
3815 return __last;
3816 }
3817 else
3818 {
3819 while (__n2-->0)
3820 {
3821 __f(*__first);
3822 ++__first;
3823 }
3824 return __first;
3825 }
3826 }
3827#endif // C++17
3828
3829 /**
3830 * @brief Find the first occurrence of a value in a sequence.
3831 * @ingroup non_mutating_algorithms
3832 * @param __first An input iterator.
3833 * @param __last An input iterator.
3834 * @param __val The value to find.
3835 * @return The first iterator @c i in the range @p [__first,__last)
3836 * such that @c *i == @p __val, or @p __last if no such iterator exists.
3837 */
3838 template<typename _InputIterator, typename _Tp>
3839 _GLIBCXX20_CONSTEXPR
3840 inline _InputIterator
3841 find(_InputIterator __first, _InputIterator __last,
3842 const _Tp& __val)
3843 {
3844 // concept requirements
3845 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3846 __glibcxx_function_requires(_EqualOpConcept<
3848 __glibcxx_requires_valid_range(__first, __last);
3849 return std::__find_if(__first, __last,
3850 __gnu_cxx::__ops::__iter_equals_val(__val));
3851 }
3852
3853 /**
3854 * @brief Find the first element in a sequence for which a
3855 * predicate is true.
3856 * @ingroup non_mutating_algorithms
3857 * @param __first An input iterator.
3858 * @param __last An input iterator.
3859 * @param __pred A predicate.
3860 * @return The first iterator @c i in the range @p [__first,__last)
3861 * such that @p __pred(*i) is true, or @p __last if no such iterator exists.
3862 */
3863 template<typename _InputIterator, typename _Predicate>
3864 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3865 inline _InputIterator
3866 find_if(_InputIterator __first, _InputIterator __last,
3867 _Predicate __pred)
3868 {
3869 // concept requirements
3870 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3871 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
3873 __glibcxx_requires_valid_range(__first, __last);
3874
3875 return std::__find_if(__first, __last,
3876 __gnu_cxx::__ops::__pred_iter(__pred));
3877 }
3878
3879 /**
3880 * @brief Find element from a set in a sequence.
3881 * @ingroup non_mutating_algorithms
3882 * @param __first1 Start of range to search.
3883 * @param __last1 End of range to search.
3884 * @param __first2 Start of match candidates.
3885 * @param __last2 End of match candidates.
3886 * @return The first iterator @c i in the range
3887 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an
3888 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists.
3889 *
3890 * Searches the range @p [__first1,__last1) for an element that is
3891 * equal to some element in the range [__first2,__last2). If
3892 * found, returns an iterator in the range [__first1,__last1),
3893 * otherwise returns @p __last1.
3894 */
3895 template<typename _InputIterator, typename _ForwardIterator>
3896 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3897 _InputIterator
3898 find_first_of(_InputIterator __first1, _InputIterator __last1,
3899 _ForwardIterator __first2, _ForwardIterator __last2)
3900 {
3901 // concept requirements
3902 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3903 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3904 __glibcxx_function_requires(_EqualOpConcept<
3907 __glibcxx_requires_valid_range(__first1, __last1);
3908 __glibcxx_requires_valid_range(__first2, __last2);
3909
3910 for (; __first1 != __last1; ++__first1)
3911 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
3912 if (*__first1 == *__iter)
3913 return __first1;
3914 return __last1;
3915 }
3916
3917 /**
3918 * @brief Find element from a set in a sequence using a predicate.
3919 * @ingroup non_mutating_algorithms
3920 * @param __first1 Start of range to search.
3921 * @param __last1 End of range to search.
3922 * @param __first2 Start of match candidates.
3923 * @param __last2 End of match candidates.
3924 * @param __comp Predicate to use.
3925 * @return The first iterator @c i in the range
3926 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true
3927 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no
3928 * such iterator exists.
3929 *
3930
3931 * Searches the range @p [__first1,__last1) for an element that is
3932 * equal to some element in the range [__first2,__last2). If
3933 * found, returns an iterator in the range [__first1,__last1),
3934 * otherwise returns @p __last1.
3935 */
3936 template<typename _InputIterator, typename _ForwardIterator,
3937 typename _BinaryPredicate>
3938 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3939 _InputIterator
3940 find_first_of(_InputIterator __first1, _InputIterator __last1,
3941 _ForwardIterator __first2, _ForwardIterator __last2,
3942 _BinaryPredicate __comp)
3943 {
3944 // concept requirements
3945 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
3946 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3947 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
3950 __glibcxx_requires_valid_range(__first1, __last1);
3951 __glibcxx_requires_valid_range(__first2, __last2);
3952
3953 for (; __first1 != __last1; ++__first1)
3954 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
3955 if (__comp(*__first1, *__iter))
3956 return __first1;
3957 return __last1;
3958 }
3959
3960 /**
3961 * @brief Find two adjacent values in a sequence that are equal.
3962 * @ingroup non_mutating_algorithms
3963 * @param __first A forward iterator.
3964 * @param __last A forward iterator.
3965 * @return The first iterator @c i such that @c i and @c i+1 are both
3966 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1),
3967 * or @p __last if no such iterator exists.
3968 */
3969 template<typename _ForwardIterator>
3970 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3971 inline _ForwardIterator
3972 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
3973 {
3974 // concept requirements
3975 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3976 __glibcxx_function_requires(_EqualityComparableConcept<
3978 __glibcxx_requires_valid_range(__first, __last);
3979
3980 return std::__adjacent_find(__first, __last,
3981 __gnu_cxx::__ops::__iter_equal_to_iter());
3982 }
3983
3984 /**
3985 * @brief Find two adjacent values in a sequence using a predicate.
3986 * @ingroup non_mutating_algorithms
3987 * @param __first A forward iterator.
3988 * @param __last A forward iterator.
3989 * @param __binary_pred A binary predicate.
3990 * @return The first iterator @c i such that @c i and @c i+1 are both
3991 * valid iterators in @p [__first,__last) and such that
3992 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator
3993 * exists.
3994 */
3995 template<typename _ForwardIterator, typename _BinaryPredicate>
3996 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3997 inline _ForwardIterator
3998 adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
3999 _BinaryPredicate __binary_pred)
4000 {
4001 // concept requirements
4002 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4003 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4006 __glibcxx_requires_valid_range(__first, __last);
4007
4008 return std::__adjacent_find(__first, __last,
4009 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
4010 }
4011
4012 /**
4013 * @brief Count the number of copies of a value in a sequence.
4014 * @ingroup non_mutating_algorithms
4015 * @param __first An input iterator.
4016 * @param __last An input iterator.
4017 * @param __value The value to be counted.
4018 * @return The number of iterators @c i in the range @p [__first,__last)
4019 * for which @c *i == @p __value
4020 */
4021 template<typename _InputIterator, typename _Tp>
4022 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4023 inline typename iterator_traits<_InputIterator>::difference_type
4024 count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
4025 {
4026 // concept requirements
4027 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4028 __glibcxx_function_requires(_EqualOpConcept<
4030 __glibcxx_requires_valid_range(__first, __last);
4031
4032 return std::__count_if(__first, __last,
4033 __gnu_cxx::__ops::__iter_equals_val(__value));
4034 }
4035
4036 /**
4037 * @brief Count the elements of a sequence for which a predicate is true.
4038 * @ingroup non_mutating_algorithms
4039 * @param __first An input iterator.
4040 * @param __last An input iterator.
4041 * @param __pred A predicate.
4042 * @return The number of iterators @c i in the range @p [__first,__last)
4043 * for which @p __pred(*i) is true.
4044 */
4045 template<typename _InputIterator, typename _Predicate>
4046 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4047 inline typename iterator_traits<_InputIterator>::difference_type
4048 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
4049 {
4050 // concept requirements
4051 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4052 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4054 __glibcxx_requires_valid_range(__first, __last);
4055
4056 return std::__count_if(__first, __last,
4057 __gnu_cxx::__ops::__pred_iter(__pred));
4058 }
4059
4060 /**
4061 * @brief Search a sequence for a matching sub-sequence.
4062 * @ingroup non_mutating_algorithms
4063 * @param __first1 A forward iterator.
4064 * @param __last1 A forward iterator.
4065 * @param __first2 A forward iterator.
4066 * @param __last2 A forward iterator.
4067 * @return The first iterator @c i in the range @p
4068 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p
4069 * *(__first2+N) for each @c N in the range @p
4070 * [0,__last2-__first2), or @p __last1 if no such iterator exists.
4071 *
4072 * Searches the range @p [__first1,__last1) for a sub-sequence that
4073 * compares equal value-by-value with the sequence given by @p
4074 * [__first2,__last2) and returns an iterator to the first element
4075 * of the sub-sequence, or @p __last1 if the sub-sequence is not
4076 * found.
4077 *
4078 * Because the sub-sequence must lie completely within the range @p
4079 * [__first1,__last1) it must start at a position less than @p
4080 * __last1-(__last2-__first2) where @p __last2-__first2 is the
4081 * length of the sub-sequence.
4082 *
4083 * This means that the returned iterator @c i will be in the range
4084 * @p [__first1,__last1-(__last2-__first2))
4085 */
4086 template<typename _ForwardIterator1, typename _ForwardIterator2>
4087 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4088 inline _ForwardIterator1
4089 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
4090 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
4091 {
4092 // concept requirements
4093 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
4094 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
4095 __glibcxx_function_requires(_EqualOpConcept<
4098 __glibcxx_requires_valid_range(__first1, __last1);
4099 __glibcxx_requires_valid_range(__first2, __last2);
4100
4101 return std::__search(__first1, __last1, __first2, __last2,
4102 __gnu_cxx::__ops::__iter_equal_to_iter());
4103 }
4104
4105 /**
4106 * @brief Search a sequence for a number of consecutive values.
4107 * @ingroup non_mutating_algorithms
4108 * @param __first A forward iterator.
4109 * @param __last A forward iterator.
4110 * @param __count The number of consecutive values.
4111 * @param __val The value to find.
4112 * @return The first iterator @c i in the range @p
4113 * [__first,__last-__count) such that @c *(i+N) == @p __val for
4114 * each @c N in the range @p [0,__count), or @p __last if no such
4115 * iterator exists.
4116 *
4117 * Searches the range @p [__first,__last) for @p count consecutive elements
4118 * equal to @p __val.
4119 */
4120 template<typename _ForwardIterator, typename _Integer, typename _Tp>
4121 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4122 inline _ForwardIterator
4123 search_n(_ForwardIterator __first, _ForwardIterator __last,
4124 _Integer __count, const _Tp& __val)
4125 {
4126 // concept requirements
4127 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4128 __glibcxx_function_requires(_EqualOpConcept<
4130 __glibcxx_requires_valid_range(__first, __last);
4131
4132 return std::__search_n(__first, __last, __count,
4133 __gnu_cxx::__ops::__iter_equals_val(__val));
4134 }
4135
4136
4137 /**
4138 * @brief Search a sequence for a number of consecutive values using a
4139 * predicate.
4140 * @ingroup non_mutating_algorithms
4141 * @param __first A forward iterator.
4142 * @param __last A forward iterator.
4143 * @param __count The number of consecutive values.
4144 * @param __val The value to find.
4145 * @param __binary_pred A binary predicate.
4146 * @return The first iterator @c i in the range @p
4147 * [__first,__last-__count) such that @p
4148 * __binary_pred(*(i+N),__val) is true for each @c N in the range
4149 * @p [0,__count), or @p __last if no such iterator exists.
4150 *
4151 * Searches the range @p [__first,__last) for @p __count
4152 * consecutive elements for which the predicate returns true.
4153 */
4154 template<typename _ForwardIterator, typename _Integer, typename _Tp,
4155 typename _BinaryPredicate>
4156 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4157 inline _ForwardIterator
4158 search_n(_ForwardIterator __first, _ForwardIterator __last,
4159 _Integer __count, const _Tp& __val,
4160 _BinaryPredicate __binary_pred)
4161 {
4162 // concept requirements
4163 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4164 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4166 __glibcxx_requires_valid_range(__first, __last);
4167
4168 return std::__search_n(__first, __last, __count,
4169 __gnu_cxx::__ops::__iter_comp_val(__binary_pred, __val));
4170 }
4171
4172#if __cplusplus >= 201703L
4173 /** @brief Search a sequence using a Searcher object.
4174 *
4175 * @param __first A forward iterator.
4176 * @param __last A forward iterator.
4177 * @param __searcher A callable object.
4178 * @return @p __searcher(__first,__last).first
4179 */
4180 template<typename _ForwardIterator, typename _Searcher>
4181 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4182 inline _ForwardIterator
4183 search(_ForwardIterator __first, _ForwardIterator __last,
4184 const _Searcher& __searcher)
4185 { return __searcher(__first, __last).first; }
4186#endif
4187
4188 /**
4189 * @brief Perform an operation on a sequence.
4190 * @ingroup mutating_algorithms
4191 * @param __first An input iterator.
4192 * @param __last An input iterator.
4193 * @param __result An output iterator.
4194 * @param __unary_op A unary operator.
4195 * @return An output iterator equal to @p __result+(__last-__first).
4196 *
4197 * Applies the operator to each element in the input range and assigns
4198 * the results to successive elements of the output sequence.
4199 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the
4200 * range @p [0,__last-__first).
4201 *
4202 * @p unary_op must not alter its argument.
4203 */
4204 template<typename _InputIterator, typename _OutputIterator,
4205 typename _UnaryOperation>
4206 _GLIBCXX20_CONSTEXPR
4207 _OutputIterator
4208 transform(_InputIterator __first, _InputIterator __last,
4209 _OutputIterator __result, _UnaryOperation __unary_op)
4210 {
4211 // concept requirements
4212 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4213 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4214 // "the type returned by a _UnaryOperation"
4215 __typeof__(__unary_op(*__first))>)
4216 __glibcxx_requires_valid_range(__first, __last);
4217
4218 for (; __first != __last; ++__first, (void)++__result)
4219 *__result = __unary_op(*__first);
4220 return __result;
4221 }
4222
4223 /**
4224 * @brief Perform an operation on corresponding elements of two sequences.
4225 * @ingroup mutating_algorithms
4226 * @param __first1 An input iterator.
4227 * @param __last1 An input iterator.
4228 * @param __first2 An input iterator.
4229 * @param __result An output iterator.
4230 * @param __binary_op A binary operator.
4231 * @return An output iterator equal to @p result+(last-first).
4232 *
4233 * Applies the operator to the corresponding elements in the two
4234 * input ranges and assigns the results to successive elements of the
4235 * output sequence.
4236 * Evaluates @p
4237 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each
4238 * @c N in the range @p [0,__last1-__first1).
4239 *
4240 * @p binary_op must not alter either of its arguments.
4241 */
4242 template<typename _InputIterator1, typename _InputIterator2,
4243 typename _OutputIterator, typename _BinaryOperation>
4244 _GLIBCXX20_CONSTEXPR
4245 _OutputIterator
4246 transform(_InputIterator1 __first1, _InputIterator1 __last1,
4247 _InputIterator2 __first2, _OutputIterator __result,
4248 _BinaryOperation __binary_op)
4249 {
4250 // concept requirements
4251 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4252 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4253 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4254 // "the type returned by a _BinaryOperation"
4255 __typeof__(__binary_op(*__first1,*__first2))>)
4256 __glibcxx_requires_valid_range(__first1, __last1);
4257
4258 for (; __first1 != __last1; ++__first1, (void)++__first2, ++__result)
4259 *__result = __binary_op(*__first1, *__first2);
4260 return __result;
4261 }
4262
4263 /**
4264 * @brief Replace each occurrence of one value in a sequence with another
4265 * value.
4266 * @ingroup mutating_algorithms
4267 * @param __first A forward iterator.
4268 * @param __last A forward iterator.
4269 * @param __old_value The value to be replaced.
4270 * @param __new_value The replacement value.
4271 * @return replace() returns no value.
4272 *
4273 * For each iterator `i` in the range `[__first,__last)` if
4274 * `*i == __old_value` then the assignment `*i = __new_value` is performed.
4275 */
4276 template<typename _ForwardIterator, typename _Tp>
4277 _GLIBCXX20_CONSTEXPR
4278 void
4279 replace(_ForwardIterator __first, _ForwardIterator __last,
4280 const _Tp& __old_value, const _Tp& __new_value)
4281 {
4282 // concept requirements
4283 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4284 _ForwardIterator>)
4285 __glibcxx_function_requires(_EqualOpConcept<
4287 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4289 __glibcxx_requires_valid_range(__first, __last);
4290
4291 for (; __first != __last; ++__first)
4292 if (*__first == __old_value)
4293 *__first = __new_value;
4294 }
4295
4296 /**
4297 * @brief Replace each value in a sequence for which a predicate returns
4298 * true with another value.
4299 * @ingroup mutating_algorithms
4300 * @param __first A forward iterator.
4301 * @param __last A forward iterator.
4302 * @param __pred A predicate.
4303 * @param __new_value The replacement value.
4304 * @return replace_if() returns no value.
4305 *
4306 * For each iterator `i` in the range `[__first,__last)` if `__pred(*i)`
4307 * is true then the assignment `*i = __new_value` is performed.
4308 */
4309 template<typename _ForwardIterator, typename _Predicate, typename _Tp>
4310 _GLIBCXX20_CONSTEXPR
4311 void
4312 replace_if(_ForwardIterator __first, _ForwardIterator __last,
4313 _Predicate __pred, const _Tp& __new_value)
4314 {
4315 // concept requirements
4316 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4317 _ForwardIterator>)
4318 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
4320 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4322 __glibcxx_requires_valid_range(__first, __last);
4323
4324 for (; __first != __last; ++__first)
4325 if (__pred(*__first))
4326 *__first = __new_value;
4327 }
4328
4329 /**
4330 * @brief Assign the result of a function object to each value in a
4331 * sequence.
4332 * @ingroup mutating_algorithms
4333 * @param __first A forward iterator.
4334 * @param __last A forward iterator.
4335 * @param __gen A function object callable with no arguments.
4336 * @return generate() returns no value.
4337 *
4338 * Performs the assignment `*i = __gen()` for each `i` in the range
4339 * `[__first, __last)`.
4340 */
4341 template<typename _ForwardIterator, typename _Generator>
4342 _GLIBCXX20_CONSTEXPR
4343 void
4344 generate(_ForwardIterator __first, _ForwardIterator __last,
4345 _Generator __gen)
4346 {
4347 // concept requirements
4348 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4349 __glibcxx_function_requires(_GeneratorConcept<_Generator,
4351 __glibcxx_requires_valid_range(__first, __last);
4352
4353 for (; __first != __last; ++__first)
4354 *__first = __gen();
4355 }
4356
4357 /**
4358 * @brief Assign the result of a function object to each value in a
4359 * sequence.
4360 * @ingroup mutating_algorithms
4361 * @param __first A forward iterator.
4362 * @param __n The length of the sequence.
4363 * @param __gen A function object callable with no arguments.
4364 * @return The end of the sequence, i.e., `__first + __n`
4365 *
4366 * Performs the assignment `*i = __gen()` for each `i` in the range
4367 * `[__first, __first + __n)`.
4368 *
4369 * If `__n` is negative, the function does nothing and returns `__first`.
4370 */
4371 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4372 // DR 865. More algorithms that throw away information
4373 // DR 426. search_n(), fill_n(), and generate_n() with negative n
4374 template<typename _OutputIterator, typename _Size, typename _Generator>
4375 _GLIBCXX20_CONSTEXPR
4376 _OutputIterator
4377 generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
4378 {
4379 // concept requirements
4380 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4381 // "the type returned by a _Generator"
4382 __typeof__(__gen())>)
4383
4384 typedef __decltype(std::__size_to_integer(__n)) _IntSize;
4385 for (_IntSize __niter = std::__size_to_integer(__n);
4386 __niter > 0; --__niter, (void) ++__first)
4387 *__first = __gen();
4388 return __first;
4389 }
4390
4391 /**
4392 * @brief Copy a sequence, removing consecutive duplicate values.
4393 * @ingroup mutating_algorithms
4394 * @param __first An input iterator.
4395 * @param __last An input iterator.
4396 * @param __result An output iterator.
4397 * @return An iterator designating the end of the resulting sequence.
4398 *
4399 * Copies each element in the range `[__first, __last)` to the range
4400 * beginning at `__result`, except that only the first element is copied
4401 * from groups of consecutive elements that compare equal.
4402 * `unique_copy()` is stable, so the relative order of elements that are
4403 * copied is unchanged.
4404 */
4405 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4406 // DR 241. Does unique_copy() require CopyConstructible and Assignable?
4407 // DR 538. 241 again: Does unique_copy() require CopyConstructible and
4408 // Assignable?
4409 template<typename _InputIterator, typename _OutputIterator>
4410 _GLIBCXX20_CONSTEXPR
4411 inline _OutputIterator
4412 unique_copy(_InputIterator __first, _InputIterator __last,
4413 _OutputIterator __result)
4414 {
4415 // concept requirements
4416 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4417 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4419 __glibcxx_function_requires(_EqualityComparableConcept<
4421 __glibcxx_requires_valid_range(__first, __last);
4422
4423 if (__first == __last)
4424 return __result;
4425 return std::__unique_copy(__first, __last, __result,
4426 __gnu_cxx::__ops::__iter_equal_to_iter(),
4427 std::__iterator_category(__first));
4428 }
4429
4430 /**
4431 * @brief Copy a sequence, removing consecutive values using a predicate.
4432 * @ingroup mutating_algorithms
4433 * @param __first An input iterator.
4434 * @param __last An input iterator.
4435 * @param __result An output iterator.
4436 * @param __binary_pred A binary predicate.
4437 * @return An iterator designating the end of the resulting sequence.
4438 *
4439 * Copies each element in the range `[__first, __last)` to the range
4440 * beginning at `__result`, except that only the first element is copied
4441 * from groups of consecutive elements for which `__binary_pred` returns
4442 * true.
4443 * `unique_copy()` is stable, so the relative order of elements that are
4444 * copied is unchanged.
4445 */
4446 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4447 // DR 241. Does unique_copy() require CopyConstructible and Assignable?
4448 template<typename _InputIterator, typename _OutputIterator,
4449 typename _BinaryPredicate>
4450 _GLIBCXX20_CONSTEXPR
4451 inline _OutputIterator
4452 unique_copy(_InputIterator __first, _InputIterator __last,
4453 _OutputIterator __result,
4454 _BinaryPredicate __binary_pred)
4455 {
4456 // concept requirements -- predicates checked later
4457 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
4458 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4460 __glibcxx_requires_valid_range(__first, __last);
4461 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
4464
4465 if (__first == __last)
4466 return __result;
4467 return std::__unique_copy(__first, __last, __result,
4468 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred),
4469 std::__iterator_category(__first));
4470 }
4471
4472#if __cplusplus <= 201103L || _GLIBCXX_USE_DEPRECATED
4473#if _GLIBCXX_HOSTED
4474 /**
4475 * @brief Randomly shuffle the elements of a sequence.
4476 * @ingroup mutating_algorithms
4477 * @param __first A forward iterator.
4478 * @param __last A forward iterator.
4479 * @return Nothing.
4480 *
4481 * Reorder the elements in the range `[__first, __last)` using a random
4482 * distribution, so that every possible ordering of the sequence is
4483 * equally likely.
4484 *
4485 * @deprecated
4486 * Since C++17, `std::random_shuffle` is not part of the C++ standard.
4487 * Use `std::shuffle` instead, which was introduced in C++11.
4488 */
4489 template<typename _RandomAccessIterator>
4490 _GLIBCXX14_DEPRECATED_SUGGEST("std::shuffle")
4491 inline void
4492 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
4493 {
4494 // concept requirements
4495 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4496 _RandomAccessIterator>)
4497 __glibcxx_requires_valid_range(__first, __last);
4498
4499 if (__first != __last)
4500 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
4501 {
4502 // XXX rand() % N is not uniformly distributed
4503 _RandomAccessIterator __j = __first
4504 + std::rand() % ((__i - __first) + 1);
4505 if (__i != __j)
4506 std::iter_swap(__i, __j);
4507 }
4508 }
4509
4510 /**
4511 * @brief Shuffle the elements of a sequence using a random number
4512 * generator.
4513 * @ingroup mutating_algorithms
4514 * @param __first A forward iterator.
4515 * @param __last A forward iterator.
4516 * @param __rand The RNG functor or function.
4517 * @return Nothing.
4518 *
4519 * Reorders the elements in the range `[__first, __last)` using `__rand`
4520 * to provide a random distribution. Calling `__rand(N)` for a positive
4521 * integer `N` should return a randomly chosen integer from the
4522 * range `[0, N)`.
4523 *
4524 * @deprecated
4525 * Since C++17, `std::random_shuffle` is not part of the C++ standard.
4526 * Use `std::shuffle` instead, which was introduced in C++11.
4527 */
4528 template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
4529 _GLIBCXX14_DEPRECATED_SUGGEST("std::shuffle")
4530 void
4531 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
4532#if __cplusplus >= 201103L
4533 _RandomNumberGenerator&& __rand)
4534#else
4535 _RandomNumberGenerator& __rand)
4536#endif
4537 {
4538 // concept requirements
4539 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4540 _RandomAccessIterator>)
4541 __glibcxx_requires_valid_range(__first, __last);
4542
4543 if (__first == __last)
4544 return;
4545 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
4546 {
4547 _RandomAccessIterator __j = __first + __rand((__i - __first) + 1);
4548 if (__i != __j)
4549 std::iter_swap(__i, __j);
4550 }
4551 }
4552#endif // HOSTED
4553#endif // <= C++11 || USE_DEPRECATED
4554
4555 /**
4556 * @brief Move elements for which a predicate is true to the beginning
4557 * of a sequence.
4558 * @ingroup mutating_algorithms
4559 * @param __first A forward iterator.
4560 * @param __last A forward iterator.
4561 * @param __pred A predicate functor.
4562 * @return An iterator `middle` such that `__pred(i)` is true for each
4563 * iterator `i` in the range `[__first, middle)` and false for each `i`
4564 * in the range `[middle, __last)`.
4565 *
4566 * `__pred` must not modify its operand. `partition()` does not preserve
4567 * the relative ordering of elements in each group, use
4568 * `stable_partition()` if this is needed.
4569 */
4570 template<typename _ForwardIterator, typename _Predicate>
4571 _GLIBCXX20_CONSTEXPR
4572 inline _ForwardIterator
4573 partition(_ForwardIterator __first, _ForwardIterator __last,
4574 _Predicate __pred)
4575 {
4576 // concept requirements
4577 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
4578 _ForwardIterator>)
4579 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
4581 __glibcxx_requires_valid_range(__first, __last);
4582
4583 return std::__partition(__first, __last, __pred,
4584 std::__iterator_category(__first));
4585 }
4586
4587
4588 /**
4589 * @brief Sort the smallest elements of a sequence.
4590 * @ingroup sorting_algorithms
4591 * @param __first An iterator.
4592 * @param __middle Another iterator.
4593 * @param __last Another iterator.
4594 * @return Nothing.
4595 *
4596 * Sorts the smallest `(__middle - __first)` elements in the range
4597 * `[first, last)` and moves them to the range `[__first, __middle)`. The
4598 * order of the remaining elements in the range `[__middle, __last)` is
4599 * unspecified.
4600 * After the sort if `i` and `j` are iterators in the range
4601 * `[__first, __middle)` such that `i` precedes `j` and `k` is an iterator
4602 * in the range `[__middle, __last)` then `*j < *i` and `*k < *i` are
4603 * both false.
4604 */
4605 template<typename _RandomAccessIterator>
4606 _GLIBCXX20_CONSTEXPR
4607 inline void
4608 partial_sort(_RandomAccessIterator __first,
4609 _RandomAccessIterator __middle,
4610 _RandomAccessIterator __last)
4611 {
4612 // concept requirements
4613 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4614 _RandomAccessIterator>)
4615 __glibcxx_function_requires(_LessThanComparableConcept<
4617 __glibcxx_requires_valid_range(__first, __middle);
4618 __glibcxx_requires_valid_range(__middle, __last);
4619 __glibcxx_requires_irreflexive(__first, __last);
4620
4621 std::__partial_sort(__first, __middle, __last,
4622 __gnu_cxx::__ops::__iter_less_iter());
4623 }
4624
4625 /**
4626 * @brief Sort the smallest elements of a sequence using a predicate
4627 * for comparison.
4628 * @ingroup sorting_algorithms
4629 * @param __first An iterator.
4630 * @param __middle Another iterator.
4631 * @param __last Another iterator.
4632 * @param __comp A comparison functor.
4633 * @return Nothing.
4634 *
4635 * Sorts the smallest `(__middle - __first)` elements in the range
4636 * `[__first, __last)` and moves them to the range `[__first, __middle)`.
4637 * The order of the remaining elements in the range `[__middle, __last)` is
4638 * unspecified.
4639 * After the sort if `i` and `j` are iterators in the range
4640 * `[__first, __middle)` such that `i` precedes `j` and `k` is an iterator
4641 * in the range `[__middle, __last)` then `*__comp(j, *i)` and
4642 * `__comp(*k, *i)` are both false.
4643 */
4644 template<typename _RandomAccessIterator, typename _Compare>
4645 _GLIBCXX20_CONSTEXPR
4646 inline void
4647 partial_sort(_RandomAccessIterator __first,
4648 _RandomAccessIterator __middle,
4649 _RandomAccessIterator __last,
4650 _Compare __comp)
4651 {
4652 // concept requirements
4653 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4654 _RandomAccessIterator>)
4655 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4658 __glibcxx_requires_valid_range(__first, __middle);
4659 __glibcxx_requires_valid_range(__middle, __last);
4660 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
4661
4662 std::__partial_sort(__first, __middle, __last,
4663 __gnu_cxx::__ops::__iter_comp_iter(__comp));
4664 }
4665
4666 /**
4667 * @brief Sort a sequence just enough to find a particular position.
4668 * @ingroup sorting_algorithms
4669 * @param __first An iterator.
4670 * @param __nth Another iterator.
4671 * @param __last Another iterator.
4672 * @return Nothing.
4673 *
4674 * Rearranges the elements in the range `[__first, __last)` so that `*__nth`
4675 * is the same element that would have been in that position had the
4676 * whole sequence been sorted. The elements either side of `*__nth` are
4677 * not completely sorted, but for any iterator `i` in the range
4678 * `[__first, __nth)` and any iterator `j` in the range `[__nth, __last)` it
4679 * holds that `*j < *i` is false.
4680 */
4681 template<typename _RandomAccessIterator>
4682 _GLIBCXX20_CONSTEXPR
4683 inline void
4684 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
4685 _RandomAccessIterator __last)
4686 {
4687 // concept requirements
4688 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4689 _RandomAccessIterator>)
4690 __glibcxx_function_requires(_LessThanComparableConcept<
4692 __glibcxx_requires_valid_range(__first, __nth);
4693 __glibcxx_requires_valid_range(__nth, __last);
4694 __glibcxx_requires_irreflexive(__first, __last);
4695
4696 if (__first == __last || __nth == __last)
4697 return;
4698
4699 std::__introselect(__first, __nth, __last,
4700 std::__lg(__last - __first) * 2,
4701 __gnu_cxx::__ops::__iter_less_iter());
4702 }
4703
4704 /**
4705 * @brief Sort a sequence just enough to find a particular position
4706 * using a predicate for comparison.
4707 * @ingroup sorting_algorithms
4708 * @param __first An iterator.
4709 * @param __nth Another iterator.
4710 * @param __last Another iterator.
4711 * @param __comp A comparison functor.
4712 * @return Nothing.
4713 *
4714 * Rearranges the elements in the range `[__first, __last)` so that `*__nth`
4715 * is the same element that would have been in that position had the
4716 * whole sequence been sorted. The elements either side of `*__nth` are
4717 * not completely sorted, but for any iterator `i` in the range
4718 * `[__first, __nth)` and any iterator `j` in the range `[__nth, __last)`
4719 * it holds that `__comp(*j, *i)` is false.
4720 */
4721 template<typename _RandomAccessIterator, typename _Compare>
4722 _GLIBCXX20_CONSTEXPR
4723 inline void
4724 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
4725 _RandomAccessIterator __last, _Compare __comp)
4726 {
4727 // concept requirements
4728 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4729 _RandomAccessIterator>)
4730 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4733 __glibcxx_requires_valid_range(__first, __nth);
4734 __glibcxx_requires_valid_range(__nth, __last);
4735 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
4736
4737 if (__first == __last || __nth == __last)
4738 return;
4739
4740 std::__introselect(__first, __nth, __last,
4741 std::__lg(__last - __first) * 2,
4742 __gnu_cxx::__ops::__iter_comp_iter(__comp));
4743 }
4744
4745 /**
4746 * @brief Sort the elements of a sequence.
4747 * @ingroup sorting_algorithms
4748 * @param __first An iterator.
4749 * @param __last Another iterator.
4750 * @return Nothing.
4751 *
4752 * Sorts the elements in the range `[__first, __last)` in ascending order,
4753 * such that for each iterator `i` in the range `[__first, __last - 1)`,
4754 * `*(i+1) < *i` is false.
4755 *
4756 * The relative ordering of equivalent elements is not preserved, use
4757 * `stable_sort()` if this is needed.
4758 */
4759 template<typename _RandomAccessIterator>
4760 _GLIBCXX20_CONSTEXPR
4761 inline void
4762 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4763 {
4764 // concept requirements
4765 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4766 _RandomAccessIterator>)
4767 __glibcxx_function_requires(_LessThanComparableConcept<
4769 __glibcxx_requires_valid_range(__first, __last);
4770 __glibcxx_requires_irreflexive(__first, __last);
4771
4772 std::__sort(__first, __last, __gnu_cxx::__ops::__iter_less_iter());
4773 }
4774
4775 /**
4776 * @brief Sort the elements of a sequence using a predicate for comparison.
4777 * @ingroup sorting_algorithms
4778 * @param __first An iterator.
4779 * @param __last Another iterator.
4780 * @param __comp A comparison functor.
4781 * @return Nothing.
4782 *
4783 * Sorts the elements in the range `[__first, __last)` in ascending order,
4784 * such that `__comp(*(i+1), *i)` is false for every iterator `i` in the
4785 * range `[__first, __last - 1)`.
4786 *
4787 * The relative ordering of equivalent elements is not preserved, use
4788 * `stable_sort()` if this is needed.
4789 */
4790 template<typename _RandomAccessIterator, typename _Compare>
4791 _GLIBCXX20_CONSTEXPR
4792 inline void
4793 sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
4794 _Compare __comp)
4795 {
4796 // concept requirements
4797 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4798 _RandomAccessIterator>)
4799 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4802 __glibcxx_requires_valid_range(__first, __last);
4803 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
4804
4805 std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
4806 }
4807
4808 template<typename _InputIterator1, typename _InputIterator2,
4809 typename _OutputIterator, typename _Compare>
4810 _GLIBCXX20_CONSTEXPR
4811 _OutputIterator
4812 __merge(_InputIterator1 __first1, _InputIterator1 __last1,
4813 _InputIterator2 __first2, _InputIterator2 __last2,
4814 _OutputIterator __result, _Compare __comp)
4815 {
4816 while (__first1 != __last1 && __first2 != __last2)
4817 {
4818 if (__comp(__first2, __first1))
4819 {
4820 *__result = *__first2;
4821 ++__first2;
4822 }
4823 else
4824 {
4825 *__result = *__first1;
4826 ++__first1;
4827 }
4828 ++__result;
4829 }
4830 return std::copy(__first2, __last2,
4831 std::copy(__first1, __last1, __result));
4832 }
4833
4834 /**
4835 * @brief Merges two sorted ranges.
4836 * @ingroup sorting_algorithms
4837 * @param __first1 An iterator.
4838 * @param __first2 Another iterator.
4839 * @param __last1 Another iterator.
4840 * @param __last2 Another iterator.
4841 * @param __result An iterator pointing to the end of the merged range.
4842 * @return An output iterator equal to @p __result + (__last1 - __first1)
4843 * + (__last2 - __first2).
4844 *
4845 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4846 * the sorted range @p [__result, __result + (__last1-__first1) +
4847 * (__last2-__first2)). Both input ranges must be sorted, and the
4848 * output range must not overlap with either of the input ranges.
4849 * The sort is @e stable, that is, for equivalent elements in the
4850 * two ranges, elements from the first range will always come
4851 * before elements from the second.
4852 */
4853 template<typename _InputIterator1, typename _InputIterator2,
4854 typename _OutputIterator>
4855 _GLIBCXX20_CONSTEXPR
4856 inline _OutputIterator
4857 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4858 _InputIterator2 __first2, _InputIterator2 __last2,
4859 _OutputIterator __result)
4860 {
4861 // concept requirements
4862 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4863 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4864 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4866 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4868 __glibcxx_function_requires(_LessThanOpConcept<
4871 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
4872 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
4873 __glibcxx_requires_irreflexive2(__first1, __last1);
4874 __glibcxx_requires_irreflexive2(__first2, __last2);
4875
4876 return _GLIBCXX_STD_A::__merge(__first1, __last1,
4877 __first2, __last2, __result,
4878 __gnu_cxx::__ops::__iter_less_iter());
4879 }
4880
4881 /**
4882 * @brief Merges two sorted ranges.
4883 * @ingroup sorting_algorithms
4884 * @param __first1 An iterator.
4885 * @param __first2 Another iterator.
4886 * @param __last1 Another iterator.
4887 * @param __last2 Another iterator.
4888 * @param __result An iterator pointing to the end of the merged range.
4889 * @param __comp A functor to use for comparisons.
4890 * @return An output iterator equal to @p __result + (__last1 - __first1)
4891 * + (__last2 - __first2).
4892 *
4893 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into
4894 * the sorted range @p [__result, __result + (__last1-__first1) +
4895 * (__last2-__first2)). Both input ranges must be sorted, and the
4896 * output range must not overlap with either of the input ranges.
4897 * The sort is @e stable, that is, for equivalent elements in the
4898 * two ranges, elements from the first range will always come
4899 * before elements from the second.
4900 *
4901 * The comparison function should have the same effects on ordering as
4902 * the function used for the initial sort.
4903 */
4904 template<typename _InputIterator1, typename _InputIterator2,
4905 typename _OutputIterator, typename _Compare>
4906 _GLIBCXX20_CONSTEXPR
4907 inline _OutputIterator
4908 merge(_InputIterator1 __first1, _InputIterator1 __last1,
4909 _InputIterator2 __first2, _InputIterator2 __last2,
4910 _OutputIterator __result, _Compare __comp)
4911 {
4912 // concept requirements
4913 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4914 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4915 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4917 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4919 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4922 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
4923 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
4924 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
4925 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
4926
4927 return _GLIBCXX_STD_A::__merge(__first1, __last1,
4928 __first2, __last2, __result,
4929 __gnu_cxx::__ops::__iter_comp_iter(__comp));
4930 }
4931
4932 template<typename _RandomAccessIterator, typename _Compare>
4933 inline void
4934 __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
4935 _Compare __comp)
4936 {
4937 typedef typename iterator_traits<_RandomAccessIterator>::value_type
4938 _ValueType;
4939 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
4940 _DistanceType;
4941
4942 if (__first == __last)
4943 return;
4944
4945#if _GLIBCXX_HOSTED
4946 typedef _Temporary_buffer<_RandomAccessIterator, _ValueType> _TmpBuf;
4947 // __stable_sort_adaptive sorts the range in two halves,
4948 // so the buffer only needs to fit half the range at once.
4949 _TmpBuf __buf(__first, (__last - __first + 1) / 2);
4950
4951 if (__builtin_expect(__buf.requested_size() == __buf.size(), true))
4952 std::__stable_sort_adaptive(__first,
4953 __first + _DistanceType(__buf.size()),
4954 __last, __buf.begin(), __comp);
4955 else if (__builtin_expect(__buf.begin() == 0, false))
4956 std::__inplace_stable_sort(__first, __last, __comp);
4957 else
4958 std::__stable_sort_adaptive_resize(__first, __last, __buf.begin(),
4959 _DistanceType(__buf.size()), __comp);
4960#else
4961 std::__inplace_stable_sort(__first, __last, __comp);
4962#endif
4963 }
4964
4965 /**
4966 * @brief Sort the elements of a sequence, preserving the relative order
4967 * of equivalent elements.
4968 * @ingroup sorting_algorithms
4969 * @param __first An iterator.
4970 * @param __last Another iterator.
4971 * @return Nothing.
4972 *
4973 * Sorts the elements in the range @p [__first,__last) in ascending order,
4974 * such that for each iterator @p i in the range @p [__first,__last-1),
4975 * @p *(i+1)<*i is false.
4976 *
4977 * The relative ordering of equivalent elements is preserved, so any two
4978 * elements @p x and @p y in the range @p [__first,__last) such that
4979 * @p x<y is false and @p y<x is false will have the same relative
4980 * ordering after calling @p stable_sort().
4981 */
4982 template<typename _RandomAccessIterator>
4983 inline void
4984 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
4985 {
4986 // concept requirements
4987 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4988 _RandomAccessIterator>)
4989 __glibcxx_function_requires(_LessThanComparableConcept<
4991 __glibcxx_requires_valid_range(__first, __last);
4992 __glibcxx_requires_irreflexive(__first, __last);
4993
4994 _GLIBCXX_STD_A::__stable_sort(__first, __last,
4995 __gnu_cxx::__ops::__iter_less_iter());
4996 }
4997
4998 /**
4999 * @brief Sort the elements of a sequence using a predicate for comparison,
5000 * preserving the relative order of equivalent elements.
5001 * @ingroup sorting_algorithms
5002 * @param __first An iterator.
5003 * @param __last Another iterator.
5004 * @param __comp A comparison functor.
5005 * @return Nothing.
5006 *
5007 * Sorts the elements in the range @p [__first,__last) in ascending order,
5008 * such that for each iterator @p i in the range @p [__first,__last-1),
5009 * @p __comp(*(i+1),*i) is false.
5010 *
5011 * The relative ordering of equivalent elements is preserved, so any two
5012 * elements @p x and @p y in the range @p [__first,__last) such that
5013 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same
5014 * relative ordering after calling @p stable_sort().
5015 */
5016 template<typename _RandomAccessIterator, typename _Compare>
5017 inline void
5018 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
5019 _Compare __comp)
5020 {
5021 // concept requirements
5022 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
5023 _RandomAccessIterator>)
5024 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5027 __glibcxx_requires_valid_range(__first, __last);
5028 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
5029
5030 _GLIBCXX_STD_A::__stable_sort(__first, __last,
5031 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5032 }
5033
5034 template<typename _InputIterator1, typename _InputIterator2,
5035 typename _OutputIterator,
5036 typename _Compare>
5037 _GLIBCXX20_CONSTEXPR
5038 _OutputIterator
5039 __set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5040 _InputIterator2 __first2, _InputIterator2 __last2,
5041 _OutputIterator __result, _Compare __comp)
5042 {
5043 while (__first1 != __last1 && __first2 != __last2)
5044 {
5045 if (__comp(__first1, __first2))
5046 {
5047 *__result = *__first1;
5048 ++__first1;
5049 }
5050 else if (__comp(__first2, __first1))
5051 {
5052 *__result = *__first2;
5053 ++__first2;
5054 }
5055 else
5056 {
5057 *__result = *__first1;
5058 ++__first1;
5059 ++__first2;
5060 }
5061 ++__result;
5062 }
5063 return std::copy(__first2, __last2,
5064 std::copy(__first1, __last1, __result));
5065 }
5066
5067 /**
5068 * @brief Return the union of two sorted ranges.
5069 * @ingroup set_algorithms
5070 * @param __first1 Start of first range.
5071 * @param __last1 End of first range.
5072 * @param __first2 Start of second range.
5073 * @param __last2 End of second range.
5074 * @param __result Start of output range.
5075 * @return End of the output range.
5076 * @ingroup set_algorithms
5077 *
5078 * This operation iterates over both ranges, copying elements present in
5079 * each range in order to the output range. Iterators increment for each
5080 * range. When the current element of one range is less than the other,
5081 * that element is copied and the iterator advanced. If an element is
5082 * contained in both ranges, the element from the first range is copied and
5083 * both ranges advance. The output range may not overlap either input
5084 * range.
5085 */
5086 template<typename _InputIterator1, typename _InputIterator2,
5087 typename _OutputIterator>
5088 _GLIBCXX20_CONSTEXPR
5089 inline _OutputIterator
5090 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5091 _InputIterator2 __first2, _InputIterator2 __last2,
5092 _OutputIterator __result)
5093 {
5094 // concept requirements
5095 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5096 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5097 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5099 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5101 __glibcxx_function_requires(_LessThanOpConcept<
5104 __glibcxx_function_requires(_LessThanOpConcept<
5107 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5108 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5109 __glibcxx_requires_irreflexive2(__first1, __last1);
5110 __glibcxx_requires_irreflexive2(__first2, __last2);
5111
5112 return _GLIBCXX_STD_A::__set_union(__first1, __last1,
5113 __first2, __last2, __result,
5114 __gnu_cxx::__ops::__iter_less_iter());
5115 }
5116
5117 /**
5118 * @brief Return the union of two sorted ranges using a comparison functor.
5119 * @ingroup set_algorithms
5120 * @param __first1 Start of first range.
5121 * @param __last1 End of first range.
5122 * @param __first2 Start of second range.
5123 * @param __last2 End of second range.
5124 * @param __result Start of output range.
5125 * @param __comp The comparison functor.
5126 * @return End of the output range.
5127 * @ingroup set_algorithms
5128 *
5129 * This operation iterates over both ranges, copying elements present in
5130 * each range in order to the output range. Iterators increment for each
5131 * range. When the current element of one range is less than the other
5132 * according to @p __comp, that element is copied and the iterator advanced.
5133 * If an equivalent element according to @p __comp is contained in both
5134 * ranges, the element from the first range is copied and both ranges
5135 * advance. The output range may not overlap either input range.
5136 */
5137 template<typename _InputIterator1, typename _InputIterator2,
5138 typename _OutputIterator, typename _Compare>
5139 _GLIBCXX20_CONSTEXPR
5140 inline _OutputIterator
5141 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
5142 _InputIterator2 __first2, _InputIterator2 __last2,
5143 _OutputIterator __result, _Compare __comp)
5144 {
5145 // concept requirements
5146 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5147 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5148 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5150 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5152 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5155 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5158 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5159 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5160 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
5161 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
5162
5163 return _GLIBCXX_STD_A::__set_union(__first1, __last1,
5164 __first2, __last2, __result,
5165 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5166 }
5167
5168 template<typename _InputIterator1, typename _InputIterator2,
5169 typename _OutputIterator,
5170 typename _Compare>
5171 _GLIBCXX20_CONSTEXPR
5172 _OutputIterator
5173 __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5174 _InputIterator2 __first2, _InputIterator2 __last2,
5175 _OutputIterator __result, _Compare __comp)
5176 {
5177 while (__first1 != __last1 && __first2 != __last2)
5178 if (__comp(__first1, __first2))
5179 ++__first1;
5180 else if (__comp(__first2, __first1))
5181 ++__first2;
5182 else
5183 {
5184 *__result = *__first1;
5185 ++__first1;
5186 ++__first2;
5187 ++__result;
5188 }
5189 return __result;
5190 }
5191
5192 /**
5193 * @brief Return the intersection of two sorted ranges.
5194 * @ingroup set_algorithms
5195 * @param __first1 Start of first range.
5196 * @param __last1 End of first range.
5197 * @param __first2 Start of second range.
5198 * @param __last2 End of second range.
5199 * @param __result Start of output range.
5200 * @return End of the output range.
5201 * @ingroup set_algorithms
5202 *
5203 * This operation iterates over both ranges, copying elements present in
5204 * both ranges in order to the output range. Iterators increment for each
5205 * range. When the current element of one range is less than the other,
5206 * that iterator advances. If an element is contained in both ranges, the
5207 * element from the first range is copied and both ranges advance. The
5208 * output range may not overlap either input range.
5209 */
5210 template<typename _InputIterator1, typename _InputIterator2,
5211 typename _OutputIterator>
5212 _GLIBCXX20_CONSTEXPR
5213 inline _OutputIterator
5214 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5215 _InputIterator2 __first2, _InputIterator2 __last2,
5216 _OutputIterator __result)
5217 {
5218 // concept requirements
5219 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5220 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5221 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5223 __glibcxx_function_requires(_LessThanOpConcept<
5226 __glibcxx_function_requires(_LessThanOpConcept<
5229 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5230 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5231 __glibcxx_requires_irreflexive2(__first1, __last1);
5232 __glibcxx_requires_irreflexive2(__first2, __last2);
5233
5234 return _GLIBCXX_STD_A::__set_intersection(__first1, __last1,
5235 __first2, __last2, __result,
5236 __gnu_cxx::__ops::__iter_less_iter());
5237 }
5238
5239 /**
5240 * @brief Return the intersection of two sorted ranges using comparison
5241 * functor.
5242 * @ingroup set_algorithms
5243 * @param __first1 Start of first range.
5244 * @param __last1 End of first range.
5245 * @param __first2 Start of second range.
5246 * @param __last2 End of second range.
5247 * @param __result Start of output range.
5248 * @param __comp The comparison functor.
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 * according to @p __comp, that iterator advances. If an element is
5256 * contained in both ranges according to @p __comp, the element from the
5257 * first range is copied and both ranges advance. The output range may not
5258 * overlap either input range.
5259 */
5260 template<typename _InputIterator1, typename _InputIterator2,
5261 typename _OutputIterator, typename _Compare>
5262 _GLIBCXX20_CONSTEXPR
5263 inline _OutputIterator
5264 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
5265 _InputIterator2 __first2, _InputIterator2 __last2,
5266 _OutputIterator __result, _Compare __comp)
5267 {
5268 // concept requirements
5269 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5270 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5271 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5273 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5276 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5279 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5280 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5281 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
5282 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
5283
5284 return _GLIBCXX_STD_A::__set_intersection(__first1, __last1,
5285 __first2, __last2, __result,
5286 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5287 }
5288
5289 template<typename _InputIterator1, typename _InputIterator2,
5290 typename _OutputIterator,
5291 typename _Compare>
5292 _GLIBCXX20_CONSTEXPR
5293 _OutputIterator
5294 __set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5295 _InputIterator2 __first2, _InputIterator2 __last2,
5296 _OutputIterator __result, _Compare __comp)
5297 {
5298 while (__first1 != __last1 && __first2 != __last2)
5299 if (__comp(__first1, __first2))
5300 {
5301 *__result = *__first1;
5302 ++__first1;
5303 ++__result;
5304 }
5305 else if (__comp(__first2, __first1))
5306 ++__first2;
5307 else
5308 {
5309 ++__first1;
5310 ++__first2;
5311 }
5312 return std::copy(__first1, __last1, __result);
5313 }
5314
5315 /**
5316 * @brief Return the difference of two sorted ranges.
5317 * @ingroup set_algorithms
5318 * @param __first1 Start of first range.
5319 * @param __last1 End of first range.
5320 * @param __first2 Start of second range.
5321 * @param __last2 End of second range.
5322 * @param __result Start of output range.
5323 * @return End of the output range.
5324 * @ingroup set_algorithms
5325 *
5326 * This operation iterates over both ranges, copying elements present in
5327 * the first range but not the second in order to the output range.
5328 * Iterators increment for each range. When the current element of the
5329 * first range is less than the second, that element is copied and the
5330 * iterator advances. If the current element of the second range is less,
5331 * the iterator advances, but no element is copied. If an element is
5332 * contained in both ranges, no elements are copied and both ranges
5333 * advance. The output range may not overlap either input range.
5334 */
5335 template<typename _InputIterator1, typename _InputIterator2,
5336 typename _OutputIterator>
5337 _GLIBCXX20_CONSTEXPR
5338 inline _OutputIterator
5339 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5340 _InputIterator2 __first2, _InputIterator2 __last2,
5341 _OutputIterator __result)
5342 {
5343 // concept requirements
5344 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5345 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5346 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5348 __glibcxx_function_requires(_LessThanOpConcept<
5351 __glibcxx_function_requires(_LessThanOpConcept<
5354 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5355 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5356 __glibcxx_requires_irreflexive2(__first1, __last1);
5357 __glibcxx_requires_irreflexive2(__first2, __last2);
5358
5359 return _GLIBCXX_STD_A::__set_difference(__first1, __last1,
5360 __first2, __last2, __result,
5361 __gnu_cxx::__ops::__iter_less_iter());
5362 }
5363
5364 /**
5365 * @brief Return the difference of two sorted ranges using comparison
5366 * functor.
5367 * @ingroup set_algorithms
5368 * @param __first1 Start of first range.
5369 * @param __last1 End of first range.
5370 * @param __first2 Start of second range.
5371 * @param __last2 End of second range.
5372 * @param __result Start of output range.
5373 * @param __comp The comparison functor.
5374 * @return End of the output range.
5375 * @ingroup set_algorithms
5376 *
5377 * This operation iterates over both ranges, copying elements present in
5378 * the first range but not the second in order to the output range.
5379 * Iterators increment for each range. When the current element of the
5380 * first range is less than the second according to @p __comp, that element
5381 * is copied and the iterator advances. If the current element of the
5382 * second range is less, no element is copied and the iterator advances.
5383 * If an element is contained in both ranges according to @p __comp, no
5384 * elements are copied and both ranges advance. The output range may not
5385 * overlap either input range.
5386 */
5387 template<typename _InputIterator1, typename _InputIterator2,
5388 typename _OutputIterator, typename _Compare>
5389 _GLIBCXX20_CONSTEXPR
5390 inline _OutputIterator
5391 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5392 _InputIterator2 __first2, _InputIterator2 __last2,
5393 _OutputIterator __result, _Compare __comp)
5394 {
5395 // concept requirements
5396 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5397 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5398 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5400 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5403 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5406 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5407 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5408 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
5409 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
5410
5411 return _GLIBCXX_STD_A::__set_difference(__first1, __last1,
5412 __first2, __last2, __result,
5413 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5414 }
5415
5416 template<typename _InputIterator1, typename _InputIterator2,
5417 typename _OutputIterator,
5418 typename _Compare>
5419 _GLIBCXX20_CONSTEXPR
5420 _OutputIterator
5421 __set_symmetric_difference(_InputIterator1 __first1,
5422 _InputIterator1 __last1,
5423 _InputIterator2 __first2,
5424 _InputIterator2 __last2,
5425 _OutputIterator __result,
5426 _Compare __comp)
5427 {
5428 while (__first1 != __last1 && __first2 != __last2)
5429 if (__comp(__first1, __first2))
5430 {
5431 *__result = *__first1;
5432 ++__first1;
5433 ++__result;
5434 }
5435 else if (__comp(__first2, __first1))
5436 {
5437 *__result = *__first2;
5438 ++__first2;
5439 ++__result;
5440 }
5441 else
5442 {
5443 ++__first1;
5444 ++__first2;
5445 }
5446 return std::copy(__first2, __last2,
5447 std::copy(__first1, __last1, __result));
5448 }
5449
5450 /**
5451 * @brief Return the symmetric difference of two sorted ranges.
5452 * @ingroup set_algorithms
5453 * @param __first1 Start of first range.
5454 * @param __last1 End of first range.
5455 * @param __first2 Start of second range.
5456 * @param __last2 End of second range.
5457 * @param __result Start of output range.
5458 * @return End of the output range.
5459 * @ingroup set_algorithms
5460 *
5461 * This operation iterates over both ranges, copying elements present in
5462 * one range but not the other in order to the output range. Iterators
5463 * increment for each range. When the current element of one range is less
5464 * than the other, that element is copied and the iterator advances. If an
5465 * element is contained in both ranges, no elements are copied and both
5466 * ranges advance. The output range may not overlap either input range.
5467 */
5468 template<typename _InputIterator1, typename _InputIterator2,
5469 typename _OutputIterator>
5470 _GLIBCXX20_CONSTEXPR
5471 inline _OutputIterator
5472 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5473 _InputIterator2 __first2, _InputIterator2 __last2,
5474 _OutputIterator __result)
5475 {
5476 // concept requirements
5477 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5478 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5479 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5481 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5483 __glibcxx_function_requires(_LessThanOpConcept<
5486 __glibcxx_function_requires(_LessThanOpConcept<
5489 __glibcxx_requires_sorted_set(__first1, __last1, __first2);
5490 __glibcxx_requires_sorted_set(__first2, __last2, __first1);
5491 __glibcxx_requires_irreflexive2(__first1, __last1);
5492 __glibcxx_requires_irreflexive2(__first2, __last2);
5493
5494 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1,
5495 __first2, __last2, __result,
5496 __gnu_cxx::__ops::__iter_less_iter());
5497 }
5498
5499 /**
5500 * @brief Return the symmetric difference of two sorted ranges using
5501 * comparison functor.
5502 * @ingroup set_algorithms
5503 * @param __first1 Start of first range.
5504 * @param __last1 End of first range.
5505 * @param __first2 Start of second range.
5506 * @param __last2 End of second range.
5507 * @param __result Start of output range.
5508 * @param __comp The comparison functor.
5509 * @return End of the output range.
5510 * @ingroup set_algorithms
5511 *
5512 * This operation iterates over both ranges, copying elements present in
5513 * one range but not the other in order to the output range. Iterators
5514 * increment for each range. When the current element of one range is less
5515 * than the other according to @p comp, that element is copied and the
5516 * iterator advances. If an element is contained in both ranges according
5517 * to @p __comp, no elements are copied and both ranges advance. The output
5518 * range may not overlap either input range.
5519 */
5520 template<typename _InputIterator1, typename _InputIterator2,
5521 typename _OutputIterator, typename _Compare>
5522 _GLIBCXX20_CONSTEXPR
5523 inline _OutputIterator
5524 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
5525 _InputIterator2 __first2, _InputIterator2 __last2,
5526 _OutputIterator __result,
5527 _Compare __comp)
5528 {
5529 // concept requirements
5530 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
5531 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
5532 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5534 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
5536 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5539 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5542 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
5543 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
5544 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
5545 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
5546
5547 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1,
5548 __first2, __last2, __result,
5549 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5550 }
5551
5552 template<typename _ForwardIterator, typename _Compare>
5553 _GLIBCXX14_CONSTEXPR
5554 _ForwardIterator
5555 __min_element(_ForwardIterator __first, _ForwardIterator __last,
5556 _Compare __comp)
5557 {
5558 if (__first == __last)
5559 return __first;
5560 _ForwardIterator __result = __first;
5561 while (++__first != __last)
5562 if (__comp(__first, __result))
5563 __result = __first;
5564 return __result;
5565 }
5566
5567 /**
5568 * @brief Return the minimum element in a range.
5569 * @ingroup sorting_algorithms
5570 * @param __first Start of range.
5571 * @param __last End of range.
5572 * @return Iterator referencing the first instance of the smallest value.
5573 */
5574 template<typename _ForwardIterator>
5575 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
5576 _ForwardIterator
5577 inline min_element(_ForwardIterator __first, _ForwardIterator __last)
5578 {
5579 // concept requirements
5580 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5581 __glibcxx_function_requires(_LessThanComparableConcept<
5583 __glibcxx_requires_valid_range(__first, __last);
5584 __glibcxx_requires_irreflexive(__first, __last);
5585
5586 return _GLIBCXX_STD_A::__min_element(__first, __last,
5587 __gnu_cxx::__ops::__iter_less_iter());
5588 }
5589
5590 /**
5591 * @brief Return the minimum element in a range using comparison functor.
5592 * @ingroup sorting_algorithms
5593 * @param __first Start of range.
5594 * @param __last End of range.
5595 * @param __comp Comparison functor.
5596 * @return Iterator referencing the first instance of the smallest value
5597 * according to __comp.
5598 */
5599 template<typename _ForwardIterator, typename _Compare>
5600 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
5601 inline _ForwardIterator
5602 min_element(_ForwardIterator __first, _ForwardIterator __last,
5603 _Compare __comp)
5604 {
5605 // concept requirements
5606 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5607 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5610 __glibcxx_requires_valid_range(__first, __last);
5611 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
5612
5613 return _GLIBCXX_STD_A::__min_element(__first, __last,
5614 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5615 }
5616
5617 template<typename _ForwardIterator, typename _Compare>
5618 _GLIBCXX14_CONSTEXPR
5619 _ForwardIterator
5620 __max_element(_ForwardIterator __first, _ForwardIterator __last,
5621 _Compare __comp)
5622 {
5623 if (__first == __last) return __first;
5624 _ForwardIterator __result = __first;
5625 while (++__first != __last)
5626 if (__comp(__result, __first))
5627 __result = __first;
5628 return __result;
5629 }
5630
5631 /**
5632 * @brief Return the maximum element in a range.
5633 * @ingroup sorting_algorithms
5634 * @param __first Start of range.
5635 * @param __last End of range.
5636 * @return Iterator referencing the first instance of the largest value.
5637 */
5638 template<typename _ForwardIterator>
5639 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
5640 inline _ForwardIterator
5641 max_element(_ForwardIterator __first, _ForwardIterator __last)
5642 {
5643 // concept requirements
5644 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5645 __glibcxx_function_requires(_LessThanComparableConcept<
5647 __glibcxx_requires_valid_range(__first, __last);
5648 __glibcxx_requires_irreflexive(__first, __last);
5649
5650 return _GLIBCXX_STD_A::__max_element(__first, __last,
5651 __gnu_cxx::__ops::__iter_less_iter());
5652 }
5653
5654 /**
5655 * @brief Return the maximum element in a range using comparison functor.
5656 * @ingroup sorting_algorithms
5657 * @param __first Start of range.
5658 * @param __last End of range.
5659 * @param __comp Comparison functor.
5660 * @return Iterator referencing the first instance of the largest value
5661 * according to __comp.
5662 */
5663 template<typename _ForwardIterator, typename _Compare>
5664 _GLIBCXX_NODISCARD _GLIBCXX14_CONSTEXPR
5665 inline _ForwardIterator
5666 max_element(_ForwardIterator __first, _ForwardIterator __last,
5667 _Compare __comp)
5668 {
5669 // concept requirements
5670 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5671 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5674 __glibcxx_requires_valid_range(__first, __last);
5675 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
5676
5677 return _GLIBCXX_STD_A::__max_element(__first, __last,
5678 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5679 }
5680
5681#if __cplusplus >= 201103L
5682 // N2722 + DR 915.
5683 template<typename _Tp>
5684 _GLIBCXX14_CONSTEXPR
5685 inline _Tp
5686 min(initializer_list<_Tp> __l)
5687 {
5688 __glibcxx_requires_irreflexive(__l.begin(), __l.end());
5689 return *_GLIBCXX_STD_A::__min_element(__l.begin(), __l.end(),
5690 __gnu_cxx::__ops::__iter_less_iter());
5691 }
5692
5693 template<typename _Tp, typename _Compare>
5694 _GLIBCXX14_CONSTEXPR
5695 inline _Tp
5696 min(initializer_list<_Tp> __l, _Compare __comp)
5697 {
5698 __glibcxx_requires_irreflexive_pred(__l.begin(), __l.end(), __comp);
5699 return *_GLIBCXX_STD_A::__min_element(__l.begin(), __l.end(),
5700 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5701 }
5702
5703 template<typename _Tp>
5704 _GLIBCXX14_CONSTEXPR
5705 inline _Tp
5707 {
5708 __glibcxx_requires_irreflexive(__l.begin(), __l.end());
5709 return *_GLIBCXX_STD_A::__max_element(__l.begin(), __l.end(),
5710 __gnu_cxx::__ops::__iter_less_iter());
5711 }
5712
5713 template<typename _Tp, typename _Compare>
5714 _GLIBCXX14_CONSTEXPR
5715 inline _Tp
5716 max(initializer_list<_Tp> __l, _Compare __comp)
5717 {
5718 __glibcxx_requires_irreflexive_pred(__l.begin(), __l.end(), __comp);
5719 return *_GLIBCXX_STD_A::__max_element(__l.begin(), __l.end(),
5720 __gnu_cxx::__ops::__iter_comp_iter(__comp));
5721 }
5722#endif // C++11
5723
5724#if __cplusplus >= 201402L
5725 /// Reservoir sampling algorithm.
5726 template<typename _InputIterator, typename _RandomAccessIterator,
5727 typename _Size, typename _UniformRandomBitGenerator>
5728 _RandomAccessIterator
5729 __sample(_InputIterator __first, _InputIterator __last, input_iterator_tag,
5730 _RandomAccessIterator __out, random_access_iterator_tag,
5731 _Size __n, _UniformRandomBitGenerator&& __g)
5732 {
5733 using __distrib_type = uniform_int_distribution<_Size>;
5734 using __param_type = typename __distrib_type::param_type;
5735 __distrib_type __d{};
5736 _Size __sample_sz = 0;
5737 while (__first != __last && __sample_sz != __n)
5738 {
5739 __out[__sample_sz++] = *__first;
5740 ++__first;
5741 }
5742 for (auto __pop_sz = __sample_sz; __first != __last;
5743 ++__first, (void) ++__pop_sz)
5744 {
5745 const auto __k = __d(__g, __param_type{0, __pop_sz});
5746 if (__k < __n)
5747 __out[__k] = *__first;
5748 }
5749 return __out + __sample_sz;
5750 }
5751
5752 /// Selection sampling algorithm.
5753 template<typename _ForwardIterator, typename _OutputIterator, typename _Cat,
5754 typename _Size, typename _UniformRandomBitGenerator>
5755 _OutputIterator
5756 __sample(_ForwardIterator __first, _ForwardIterator __last,
5758 _OutputIterator __out, _Cat,
5759 _Size __n, _UniformRandomBitGenerator&& __g)
5760 {
5761 using __distrib_type = uniform_int_distribution<_Size>;
5762 using __param_type = typename __distrib_type::param_type;
5763 using _USize = make_unsigned_t<_Size>;
5766
5767 if (__first == __last)
5768 return __out;
5769
5770 __distrib_type __d{};
5771 _Size __unsampled_sz = std::distance(__first, __last);
5772 __n = std::min(__n, __unsampled_sz);
5773
5774 // If possible, we use __gen_two_uniform_ints to efficiently produce
5775 // two random numbers using a single distribution invocation:
5776
5777 const __uc_type __urngrange = __g.max() - __g.min();
5778 if (__urngrange / __uc_type(__unsampled_sz) >= __uc_type(__unsampled_sz))
5779 // I.e. (__urngrange >= __unsampled_sz * __unsampled_sz) but without
5780 // wrapping issues.
5781 {
5782 while (__n != 0 && __unsampled_sz >= 2)
5783 {
5784 const pair<_Size, _Size> __p =
5785 __gen_two_uniform_ints(__unsampled_sz, __unsampled_sz - 1, __g);
5786
5787 --__unsampled_sz;
5788 if (__p.first < __n)
5789 {
5790 *__out++ = *__first;
5791 --__n;
5792 }
5793
5794 ++__first;
5795
5796 if (__n == 0) break;
5797
5798 --__unsampled_sz;
5799 if (__p.second < __n)
5800 {
5801 *__out++ = *__first;
5802 --__n;
5803 }
5804
5805 ++__first;
5806 }
5807 }
5808
5809 // The loop above is otherwise equivalent to this one-at-a-time version:
5810
5811 for (; __n != 0; ++__first)
5812 if (__d(__g, __param_type{0, --__unsampled_sz}) < __n)
5813 {
5814 *__out++ = *__first;
5815 --__n;
5816 }
5817 return __out;
5818 }
5819#endif // C++14
5820
5821#ifdef __glibcxx_sample // C++ >= 17
5822 /// Take a random sample from a population.
5823 template<typename _PopulationIterator, typename _SampleIterator,
5824 typename _Distance, typename _UniformRandomBitGenerator>
5825 _SampleIterator
5826 sample(_PopulationIterator __first, _PopulationIterator __last,
5827 _SampleIterator __out, _Distance __n,
5828 _UniformRandomBitGenerator&& __g)
5829 {
5830 using __pop_cat = typename
5832 using __samp_cat = typename
5834
5835 static_assert(
5836 __or_<is_convertible<__pop_cat, forward_iterator_tag>,
5837 is_convertible<__samp_cat, random_access_iterator_tag>>::value,
5838 "output range must use a RandomAccessIterator when input range"
5839 " does not meet the ForwardIterator requirements");
5840
5841 static_assert(is_integral<_Distance>::value,
5842 "sample size must be an integer type");
5843
5845 return _GLIBCXX_STD_A::
5846 __sample(__first, __last, __pop_cat{}, __out, __samp_cat{}, __d,
5848 }
5849#endif // __glibcxx_sample
5850
5851_GLIBCXX_END_NAMESPACE_ALGO
5852_GLIBCXX_END_NAMESPACE_VERSION
5853} // namespace std
5854
5855#endif /* _STL_ALGO_H */
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
Definition type_traits:1731
typename common_type< _Tp... >::type common_type_t
Alias template for common_type.
Definition type_traits:2717
typename make_unsigned< _Tp >::type make_unsigned_t
Alias template for make_unsigned.
Definition type_traits:2074
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs of the same type are equal iff their members are equal.
constexpr pair< typename __decay_and_strip< _T1 >::__type, typename __decay_and_strip< _T2 >::__type > make_pair(_T1 &&__x, _T2 &&__y)
A convenience wrapper for creating a pair from two objects.
Definition stl_pair.h:1132
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:137
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:51
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:71
constexpr _InputIterator for_each_n(_InputIterator __first, _Size __n, _Function __f)
Apply a function to every element of a sequence.
Definition stl_algo.h:3805
constexpr const _Tp & clamp(const _Tp &, const _Tp &, const _Tp &)
Returns the value clamped between lo and hi.
Definition stl_algo.h:3623
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:5729
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:123
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:3673
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 _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 _Tp __lg(_Tp __n)
This is a helper function for the sort routines and for random.tcc.
constexpr _InputIterator __find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred, input_iterator_tag)
This is an overload used by find algos for the Input Iterator case.
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:5826
constexpr void advance(_InputIterator &__i, _Distance __n)
A generalization of pointer arithmetic.
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:152
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
initializer_list
is_integral
Definition type_traits:462
common_type
Definition type_traits:2342
Traits class for iterators.
constexpr iterator_type base() const noexcept(/*conditional */)
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.
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...