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