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