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