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