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