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