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