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