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