1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
65 #include <bits/stl_heap.h>
66 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
67 #include <debug/debug.h>
69 // See concept_check.h for the __glibcxx_*_requires macros.
71 _GLIBCXX_BEGIN_NAMESPACE(std)
74 * @brief Find the median of three values.
78 * @return One of @p a, @p b or @p c.
80 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
81 * then the value returned will be @c m.
82 * This is an SGI extension.
83 * @ingroup SGIextensions
85 template<typename _Tp>
87 __median(const _Tp& __a, const _Tp& __b, const _Tp& __c)
89 // concept requirements
90 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
107 * @brief Find the median of three values using a predicate for comparison.
111 * @param comp A binary predicate.
112 * @return One of @p a, @p b or @p c.
114 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
115 * and @p comp(m,n) are both true then the value returned will be @c m.
116 * This is an SGI extension.
117 * @ingroup SGIextensions
119 template<typename _Tp, typename _Compare>
121 __median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp)
123 // concept requirements
124 __glibcxx_function_requires(_BinaryFunctionConcept<_Compare,bool,_Tp,_Tp>)
125 if (__comp(__a, __b))
126 if (__comp(__b, __c))
128 else if (__comp(__a, __c))
132 else if (__comp(__a, __c))
134 else if (__comp(__b, __c))
141 * @brief Apply a function to every element of a sequence.
142 * @param first An input iterator.
143 * @param last An input iterator.
144 * @param f A unary function object.
147 * Applies the function object @p f to each element in the range
148 * @p [first,last). @p f must not modify the order of the sequence.
149 * If @p f has a return value it is ignored.
151 template<typename _InputIterator, typename _Function>
153 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
155 // concept requirements
156 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
157 __glibcxx_requires_valid_range(__first, __last);
158 for ( ; __first != __last; ++__first)
165 * This is an overload used by find() for the Input Iterator case.
168 template<typename _InputIterator, typename _Tp>
169 inline _InputIterator
170 __find(_InputIterator __first, _InputIterator __last,
171 const _Tp& __val, input_iterator_tag)
173 while (__first != __last && !(*__first == __val))
180 * This is an overload used by find_if() for the Input Iterator case.
183 template<typename _InputIterator, typename _Predicate>
184 inline _InputIterator
185 __find_if(_InputIterator __first, _InputIterator __last,
186 _Predicate __pred, input_iterator_tag)
188 while (__first != __last && !__pred(*__first))
195 * This is an overload used by find() for the RAI case.
198 template<typename _RandomAccessIterator, typename _Tp>
199 _RandomAccessIterator
200 __find(_RandomAccessIterator __first, _RandomAccessIterator __last,
201 const _Tp& __val, random_access_iterator_tag)
203 typename iterator_traits<_RandomAccessIterator>::difference_type
204 __trip_count = (__last - __first) >> 2;
206 for ( ; __trip_count > 0 ; --__trip_count)
208 if (*__first == __val)
212 if (*__first == __val)
216 if (*__first == __val)
220 if (*__first == __val)
225 switch (__last - __first)
228 if (*__first == __val)
232 if (*__first == __val)
236 if (*__first == __val)
247 * This is an overload used by find_if() for the RAI case.
250 template<typename _RandomAccessIterator, typename _Predicate>
251 _RandomAccessIterator
252 __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
253 _Predicate __pred, random_access_iterator_tag)
255 typename iterator_traits<_RandomAccessIterator>::difference_type
256 __trip_count = (__last - __first) >> 2;
258 for ( ; __trip_count > 0 ; --__trip_count)
260 if (__pred(*__first))
264 if (__pred(*__first))
268 if (__pred(*__first))
272 if (__pred(*__first))
277 switch (__last - __first)
280 if (__pred(*__first))
284 if (__pred(*__first))
288 if (__pred(*__first))
298 * @brief Find the first occurrence of a value in a sequence.
299 * @param first An input iterator.
300 * @param last An input iterator.
301 * @param val The value to find.
302 * @return The first iterator @c i in the range @p [first,last)
303 * such that @c *i == @p val, or @p last if no such iterator exists.
305 template<typename _InputIterator, typename _Tp>
306 inline _InputIterator
307 find(_InputIterator __first, _InputIterator __last,
310 // concept requirements
311 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
312 __glibcxx_function_requires(_EqualOpConcept<
313 typename iterator_traits<_InputIterator>::value_type, _Tp>)
314 __glibcxx_requires_valid_range(__first, __last);
315 return std::__find(__first, __last, __val,
316 std::__iterator_category(__first));
320 * @brief Find the first element in a sequence for which a predicate is true.
321 * @param first An input iterator.
322 * @param last An input iterator.
323 * @param pred A predicate.
324 * @return The first iterator @c i in the range @p [first,last)
325 * such that @p pred(*i) is true, or @p last if no such iterator exists.
327 template<typename _InputIterator, typename _Predicate>
328 inline _InputIterator
329 find_if(_InputIterator __first, _InputIterator __last,
332 // concept requirements
333 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
334 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
335 typename iterator_traits<_InputIterator>::value_type>)
336 __glibcxx_requires_valid_range(__first, __last);
337 return std::__find_if(__first, __last, __pred,
338 std::__iterator_category(__first));
342 * @brief Find two adjacent values in a sequence that are equal.
343 * @param first A forward iterator.
344 * @param last A forward iterator.
345 * @return The first iterator @c i such that @c i and @c i+1 are both
346 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
347 * or @p last if no such iterator exists.
349 template<typename _ForwardIterator>
351 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
353 // concept requirements
354 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
355 __glibcxx_function_requires(_EqualityComparableConcept<
356 typename iterator_traits<_ForwardIterator>::value_type>)
357 __glibcxx_requires_valid_range(__first, __last);
358 if (__first == __last)
360 _ForwardIterator __next = __first;
361 while(++__next != __last)
363 if (*__first == *__next)
371 * @brief Find two adjacent values in a sequence using a predicate.
372 * @param first A forward iterator.
373 * @param last A forward iterator.
374 * @param binary_pred A binary predicate.
375 * @return The first iterator @c i such that @c i and @c i+1 are both
376 * valid iterators in @p [first,last) and such that
377 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
380 template<typename _ForwardIterator, typename _BinaryPredicate>
382 adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
383 _BinaryPredicate __binary_pred)
385 // concept requirements
386 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
387 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
388 typename iterator_traits<_ForwardIterator>::value_type,
389 typename iterator_traits<_ForwardIterator>::value_type>)
390 __glibcxx_requires_valid_range(__first, __last);
391 if (__first == __last)
393 _ForwardIterator __next = __first;
394 while(++__next != __last)
396 if (__binary_pred(*__first, *__next))
404 * @brief Count the number of copies of a value in a sequence.
405 * @param first An input iterator.
406 * @param last An input iterator.
407 * @param value The value to be counted.
408 * @return The number of iterators @c i in the range @p [first,last)
409 * for which @c *i == @p value
411 template<typename _InputIterator, typename _Tp>
412 typename iterator_traits<_InputIterator>::difference_type
413 count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
415 // concept requirements
416 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
417 __glibcxx_function_requires(_EqualOpConcept<
418 typename iterator_traits<_InputIterator>::value_type, _Tp>)
419 __glibcxx_requires_valid_range(__first, __last);
420 typename iterator_traits<_InputIterator>::difference_type __n = 0;
421 for ( ; __first != __last; ++__first)
422 if (*__first == __value)
428 * @brief Count the elements of a sequence for which a predicate is true.
429 * @param first An input iterator.
430 * @param last An input iterator.
431 * @param pred A predicate.
432 * @return The number of iterators @c i in the range @p [first,last)
433 * for which @p pred(*i) is true.
435 template<typename _InputIterator, typename _Predicate>
436 typename iterator_traits<_InputIterator>::difference_type
437 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
439 // concept requirements
440 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
441 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
442 typename iterator_traits<_InputIterator>::value_type>)
443 __glibcxx_requires_valid_range(__first, __last);
444 typename iterator_traits<_InputIterator>::difference_type __n = 0;
445 for ( ; __first != __last; ++__first)
446 if (__pred(*__first))
452 * @brief Search a sequence for a matching sub-sequence.
453 * @param first1 A forward iterator.
454 * @param last1 A forward iterator.
455 * @param first2 A forward iterator.
456 * @param last2 A forward iterator.
457 * @return The first iterator @c i in the range
458 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
459 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
460 * such iterator exists.
462 * Searches the range @p [first1,last1) for a sub-sequence that compares
463 * equal value-by-value with the sequence given by @p [first2,last2) and
464 * returns an iterator to the first element of the sub-sequence, or
465 * @p last1 if the sub-sequence is not found.
467 * Because the sub-sequence must lie completely within the range
468 * @p [first1,last1) it must start at a position less than
469 * @p last1-(last2-first2) where @p last2-first2 is the length of the
471 * This means that the returned iterator @c i will be in the range
472 * @p [first1,last1-(last2-first2))
474 template<typename _ForwardIterator1, typename _ForwardIterator2>
476 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
477 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
479 // concept requirements
480 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
481 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
482 __glibcxx_function_requires(_EqualOpConcept<
483 typename iterator_traits<_ForwardIterator1>::value_type,
484 typename iterator_traits<_ForwardIterator2>::value_type>)
485 __glibcxx_requires_valid_range(__first1, __last1);
486 __glibcxx_requires_valid_range(__first2, __last2);
487 // Test for empty ranges
488 if (__first1 == __last1 || __first2 == __last2)
491 // Test for a pattern of length 1.
492 _ForwardIterator2 __tmp(__first2);
494 if (__tmp == __last2)
495 return std::find(__first1, __last1, *__first2);
498 _ForwardIterator2 __p1, __p;
499 __p1 = __first2; ++__p1;
500 _ForwardIterator1 __current = __first1;
502 while (__first1 != __last1)
504 __first1 = std::find(__first1, __last1, *__first2);
505 if (__first1 == __last1)
509 __current = __first1;
510 if (++__current == __last1)
513 while (*__current == *__p)
515 if (++__p == __last2)
517 if (++__current == __last1)
526 * @brief Search a sequence for a matching sub-sequence using a predicate.
527 * @param first1 A forward iterator.
528 * @param last1 A forward iterator.
529 * @param first2 A forward iterator.
530 * @param last2 A forward iterator.
531 * @param predicate A binary predicate.
532 * @return The first iterator @c i in the range
533 * @p [first1,last1-(last2-first2)) such that
534 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
535 * @p [0,last2-first2), or @p last1 if no such iterator exists.
537 * Searches the range @p [first1,last1) for a sub-sequence that compares
538 * equal value-by-value with the sequence given by @p [first2,last2),
539 * using @p predicate to determine equality, and returns an iterator
540 * to the first element of the sub-sequence, or @p last1 if no such
543 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
545 template<typename _ForwardIterator1, typename _ForwardIterator2,
546 typename _BinaryPredicate>
548 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
549 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
550 _BinaryPredicate __predicate)
552 // concept requirements
553 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
554 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
555 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
556 typename iterator_traits<_ForwardIterator1>::value_type,
557 typename iterator_traits<_ForwardIterator2>::value_type>)
558 __glibcxx_requires_valid_range(__first1, __last1);
559 __glibcxx_requires_valid_range(__first2, __last2);
561 // Test for empty ranges
562 if (__first1 == __last1 || __first2 == __last2)
565 // Test for a pattern of length 1.
566 _ForwardIterator2 __tmp(__first2);
568 if (__tmp == __last2)
570 while (__first1 != __last1 && !__predicate(*__first1, *__first2))
576 _ForwardIterator2 __p1, __p;
577 __p1 = __first2; ++__p1;
578 _ForwardIterator1 __current = __first1;
580 while (__first1 != __last1)
582 while (__first1 != __last1)
584 if (__predicate(*__first1, *__first2))
588 while (__first1 != __last1 && !__predicate(*__first1, *__first2))
590 if (__first1 == __last1)
594 __current = __first1;
595 if (++__current == __last1)
598 while (__predicate(*__current, *__p))
600 if (++__p == __last2)
602 if (++__current == __last1)
612 * This is an uglified
613 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
614 * overloaded for forward iterators.
617 template<typename _ForwardIterator, typename _Integer, typename _Tp>
619 __search_n(_ForwardIterator __first, _ForwardIterator __last,
620 _Integer __count, const _Tp& __val,
621 std::forward_iterator_tag)
623 __first = std::find(__first, __last, __val);
624 while (__first != __last)
626 typename iterator_traits<_ForwardIterator>::difference_type
628 _ForwardIterator __i = __first;
630 while (__i != __last && __n != 1 && *__i == __val)
639 __first = std::find(++__i, __last, __val);
646 * This is an uglified
647 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
648 * overloaded for random access iterators.
651 template<typename _RandomAccessIter, typename _Integer, typename _Tp>
653 __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
654 _Integer __count, const _Tp& __val,
655 std::random_access_iterator_tag)
658 typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
661 _DistanceType __tailSize = __last - __first;
662 const _DistanceType __pattSize = __count;
664 if (__tailSize < __pattSize)
667 const _DistanceType __skipOffset = __pattSize - 1;
668 _RandomAccessIter __lookAhead = __first + __skipOffset;
669 __tailSize -= __pattSize;
671 while (1) // the main loop...
673 // __lookAhead here is always pointing to the last element of next
675 while (!(*__lookAhead == __val)) // the skip loop...
677 if (__tailSize < __pattSize)
678 return __last; // Failure
679 __lookAhead += __pattSize;
680 __tailSize -= __pattSize;
682 _DistanceType __remainder = __skipOffset;
683 for (_RandomAccessIter __backTrack = __lookAhead - 1;
684 *__backTrack == __val; --__backTrack)
686 if (--__remainder == 0)
687 return (__lookAhead - __skipOffset); // Success
689 if (__remainder > __tailSize)
690 return __last; // Failure
691 __lookAhead += __remainder;
692 __tailSize -= __remainder;
697 * @brief Search a sequence for a number of consecutive values.
698 * @param first A forward iterator.
699 * @param last A forward iterator.
700 * @param count The number of consecutive values.
701 * @param val The value to find.
702 * @return The first iterator @c i in the range @p [first,last-count)
703 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
704 * or @p last if no such iterator exists.
706 * Searches the range @p [first,last) for @p count consecutive elements
709 template<typename _ForwardIterator, typename _Integer, typename _Tp>
711 search_n(_ForwardIterator __first, _ForwardIterator __last,
712 _Integer __count, const _Tp& __val)
714 // concept requirements
715 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
716 __glibcxx_function_requires(_EqualOpConcept<
717 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
718 __glibcxx_requires_valid_range(__first, __last);
723 return std::find(__first, __last, __val);
724 return std::__search_n(__first, __last, __count, __val,
725 std::__iterator_category(__first));
730 * This is an uglified
731 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
733 * overloaded for forward iterators.
736 template<typename _ForwardIterator, typename _Integer, typename _Tp,
737 typename _BinaryPredicate>
739 __search_n(_ForwardIterator __first, _ForwardIterator __last,
740 _Integer __count, const _Tp& __val,
741 _BinaryPredicate __binary_pred, std::forward_iterator_tag)
743 while (__first != __last && !__binary_pred(*__first, __val))
746 while (__first != __last)
748 typename iterator_traits<_ForwardIterator>::difference_type
750 _ForwardIterator __i = __first;
752 while (__i != __last && __n != 1 && *__i == __val)
762 while (__first != __last && !__binary_pred(*__first, __val))
770 * This is an uglified
771 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
773 * overloaded for random access iterators.
776 template<typename _RandomAccessIter, typename _Integer, typename _Tp,
777 typename _BinaryPredicate>
779 __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
780 _Integer __count, const _Tp& __val,
781 _BinaryPredicate __binary_pred, std::random_access_iterator_tag)
784 typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
787 _DistanceType __tailSize = __last - __first;
788 const _DistanceType __pattSize = __count;
790 if (__tailSize < __pattSize)
793 const _DistanceType __skipOffset = __pattSize - 1;
794 _RandomAccessIter __lookAhead = __first + __skipOffset;
795 __tailSize -= __pattSize;
797 while (1) // the main loop...
799 // __lookAhead here is always pointing to the last element of next
801 while (!__binary_pred(*__lookAhead, __val)) // the skip loop...
803 if (__tailSize < __pattSize)
804 return __last; // Failure
805 __lookAhead += __pattSize;
806 __tailSize -= __pattSize;
808 _DistanceType __remainder = __skipOffset;
809 for (_RandomAccessIter __backTrack = __lookAhead - 1;
810 __binary_pred(*__backTrack, __val); --__backTrack)
812 if (--__remainder == 0)
813 return (__lookAhead - __skipOffset); // Success
815 if (__remainder > __tailSize)
816 return __last; // Failure
817 __lookAhead += __remainder;
818 __tailSize -= __remainder;
823 * @brief Search a sequence for a number of consecutive values using a
825 * @param first A forward iterator.
826 * @param last A forward iterator.
827 * @param count The number of consecutive values.
828 * @param val The value to find.
829 * @param binary_pred A binary predicate.
830 * @return The first iterator @c i in the range @p [first,last-count)
831 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
832 * range @p [0,count), or @p last if no such iterator exists.
834 * Searches the range @p [first,last) for @p count consecutive elements
835 * for which the predicate returns true.
837 template<typename _ForwardIterator, typename _Integer, typename _Tp,
838 typename _BinaryPredicate>
840 search_n(_ForwardIterator __first, _ForwardIterator __last,
841 _Integer __count, const _Tp& __val,
842 _BinaryPredicate __binary_pred)
844 // concept requirements
845 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
846 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
847 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
848 __glibcxx_requires_valid_range(__first, __last);
854 while (__first != __last && !__binary_pred(*__first, __val))
858 return std::__search_n(__first, __last, __count, __val, __binary_pred,
859 std::__iterator_category(__first));
863 * @brief Swap the elements of two sequences.
864 * @param first1 A forward iterator.
865 * @param last1 A forward iterator.
866 * @param first2 A forward iterator.
867 * @return An iterator equal to @p first2+(last1-first1).
869 * Swaps each element in the range @p [first1,last1) with the
870 * corresponding element in the range @p [first2,(last1-first1)).
871 * The ranges must not overlap.
873 template<typename _ForwardIterator1, typename _ForwardIterator2>
875 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
876 _ForwardIterator2 __first2)
878 // concept requirements
879 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
881 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
883 __glibcxx_function_requires(_ConvertibleConcept<
884 typename iterator_traits<_ForwardIterator1>::value_type,
885 typename iterator_traits<_ForwardIterator2>::value_type>)
886 __glibcxx_function_requires(_ConvertibleConcept<
887 typename iterator_traits<_ForwardIterator2>::value_type,
888 typename iterator_traits<_ForwardIterator1>::value_type>)
889 __glibcxx_requires_valid_range(__first1, __last1);
891 for ( ; __first1 != __last1; ++__first1, ++__first2)
892 std::iter_swap(__first1, __first2);
897 * @brief Perform an operation on a sequence.
898 * @param first An input iterator.
899 * @param last An input iterator.
900 * @param result An output iterator.
901 * @param unary_op A unary operator.
902 * @return An output iterator equal to @p result+(last-first).
904 * Applies the operator to each element in the input range and assigns
905 * the results to successive elements of the output sequence.
906 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
907 * range @p [0,last-first).
909 * @p unary_op must not alter its argument.
911 template<typename _InputIterator, typename _OutputIterator,
912 typename _UnaryOperation>
914 transform(_InputIterator __first, _InputIterator __last,
915 _OutputIterator __result, _UnaryOperation __unary_op)
917 // concept requirements
918 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
919 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
920 // "the type returned by a _UnaryOperation"
921 __typeof__(__unary_op(*__first))>)
922 __glibcxx_requires_valid_range(__first, __last);
924 for ( ; __first != __last; ++__first, ++__result)
925 *__result = __unary_op(*__first);
930 * @brief Perform an operation on corresponding elements of two sequences.
931 * @param first1 An input iterator.
932 * @param last1 An input iterator.
933 * @param first2 An input iterator.
934 * @param result An output iterator.
935 * @param binary_op A binary operator.
936 * @return An output iterator equal to @p result+(last-first).
938 * Applies the operator to the corresponding elements in the two
939 * input ranges and assigns the results to successive elements of the
941 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
942 * @c N in the range @p [0,last1-first1).
944 * @p binary_op must not alter either of its arguments.
946 template<typename _InputIterator1, typename _InputIterator2,
947 typename _OutputIterator, typename _BinaryOperation>
949 transform(_InputIterator1 __first1, _InputIterator1 __last1,
950 _InputIterator2 __first2, _OutputIterator __result,
951 _BinaryOperation __binary_op)
953 // concept requirements
954 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
955 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
956 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
957 // "the type returned by a _BinaryOperation"
958 __typeof__(__binary_op(*__first1,*__first2))>)
959 __glibcxx_requires_valid_range(__first1, __last1);
961 for ( ; __first1 != __last1; ++__first1, ++__first2, ++__result)
962 *__result = __binary_op(*__first1, *__first2);
967 * @brief Replace each occurrence of one value in a sequence with another
969 * @param first A forward iterator.
970 * @param last A forward iterator.
971 * @param old_value The value to be replaced.
972 * @param new_value The replacement value.
973 * @return replace() returns no value.
975 * For each iterator @c i in the range @p [first,last) if @c *i ==
976 * @p old_value then the assignment @c *i = @p new_value is performed.
978 template<typename _ForwardIterator, typename _Tp>
980 replace(_ForwardIterator __first, _ForwardIterator __last,
981 const _Tp& __old_value, const _Tp& __new_value)
983 // concept requirements
984 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
986 __glibcxx_function_requires(_EqualOpConcept<
987 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
988 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
989 typename iterator_traits<_ForwardIterator>::value_type>)
990 __glibcxx_requires_valid_range(__first, __last);
992 for ( ; __first != __last; ++__first)
993 if (*__first == __old_value)
994 *__first = __new_value;
998 * @brief Replace each value in a sequence for which a predicate returns
999 * true with another value.
1000 * @param first A forward iterator.
1001 * @param last A forward iterator.
1002 * @param pred A predicate.
1003 * @param new_value The replacement value.
1004 * @return replace_if() returns no value.
1006 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
1007 * is true then the assignment @c *i = @p new_value is performed.
1009 template<typename _ForwardIterator, typename _Predicate, typename _Tp>
1011 replace_if(_ForwardIterator __first, _ForwardIterator __last,
1012 _Predicate __pred, const _Tp& __new_value)
1014 // concept requirements
1015 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1017 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
1018 typename iterator_traits<_ForwardIterator>::value_type>)
1019 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1020 typename iterator_traits<_ForwardIterator>::value_type>)
1021 __glibcxx_requires_valid_range(__first, __last);
1023 for ( ; __first != __last; ++__first)
1024 if (__pred(*__first))
1025 *__first = __new_value;
1029 * @brief Copy a sequence, replacing each element of one value with another
1031 * @param first An input iterator.
1032 * @param last An input iterator.
1033 * @param result An output iterator.
1034 * @param old_value The value to be replaced.
1035 * @param new_value The replacement value.
1036 * @return The end of the output sequence, @p result+(last-first).
1038 * Copies each element in the input range @p [first,last) to the
1039 * output range @p [result,result+(last-first)) replacing elements
1040 * equal to @p old_value with @p new_value.
1042 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
1044 replace_copy(_InputIterator __first, _InputIterator __last,
1045 _OutputIterator __result,
1046 const _Tp& __old_value, const _Tp& __new_value)
1048 // concept requirements
1049 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1050 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1051 typename iterator_traits<_InputIterator>::value_type>)
1052 __glibcxx_function_requires(_EqualOpConcept<
1053 typename iterator_traits<_InputIterator>::value_type, _Tp>)
1054 __glibcxx_requires_valid_range(__first, __last);
1056 for ( ; __first != __last; ++__first, ++__result)
1057 if (*__first == __old_value)
1058 *__result = __new_value;
1060 *__result = *__first;
1065 * @brief Copy a sequence, replacing each value for which a predicate
1066 * returns true with another value.
1067 * @param first An input iterator.
1068 * @param last An input iterator.
1069 * @param result An output iterator.
1070 * @param pred A predicate.
1071 * @param new_value The replacement value.
1072 * @return The end of the output sequence, @p result+(last-first).
1074 * Copies each element in the range @p [first,last) to the range
1075 * @p [result,result+(last-first)) replacing elements for which
1076 * @p pred returns true with @p new_value.
1078 template<typename _InputIterator, typename _OutputIterator,
1079 typename _Predicate, typename _Tp>
1081 replace_copy_if(_InputIterator __first, _InputIterator __last,
1082 _OutputIterator __result,
1083 _Predicate __pred, const _Tp& __new_value)
1085 // concept requirements
1086 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1087 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1088 typename iterator_traits<_InputIterator>::value_type>)
1089 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1090 typename iterator_traits<_InputIterator>::value_type>)
1091 __glibcxx_requires_valid_range(__first, __last);
1093 for ( ; __first != __last; ++__first, ++__result)
1094 if (__pred(*__first))
1095 *__result = __new_value;
1097 *__result = *__first;
1102 * @brief Assign the result of a function object to each value in a
1104 * @param first A forward iterator.
1105 * @param last A forward iterator.
1106 * @param gen A function object taking no arguments.
1107 * @return generate() returns no value.
1109 * Performs the assignment @c *i = @p gen() for each @c i in the range
1112 template<typename _ForwardIterator, typename _Generator>
1114 generate(_ForwardIterator __first, _ForwardIterator __last,
1117 // concept requirements
1118 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1119 __glibcxx_function_requires(_GeneratorConcept<_Generator,
1120 typename iterator_traits<_ForwardIterator>::value_type>)
1121 __glibcxx_requires_valid_range(__first, __last);
1123 for ( ; __first != __last; ++__first)
1128 * @brief Assign the result of a function object to each value in a
1130 * @param first A forward iterator.
1131 * @param n The length of the sequence.
1132 * @param gen A function object taking no arguments.
1133 * @return The end of the sequence, @p first+n
1135 * Performs the assignment @c *i = @p gen() for each @c i in the range
1136 * @p [first,first+n).
1138 template<typename _OutputIterator, typename _Size, typename _Generator>
1140 generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
1142 // concept requirements
1143 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1144 // "the type returned by a _Generator"
1145 __typeof__(__gen())>)
1147 for ( ; __n > 0; --__n, ++__first)
1153 * @brief Copy a sequence, removing elements of a given value.
1154 * @param first An input iterator.
1155 * @param last An input iterator.
1156 * @param result An output iterator.
1157 * @param value The value to be removed.
1158 * @return An iterator designating the end of the resulting sequence.
1160 * Copies each element in the range @p [first,last) not equal to @p value
1161 * to the range beginning at @p result.
1162 * remove_copy() is stable, so the relative order of elements that are
1163 * copied is unchanged.
1165 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
1167 remove_copy(_InputIterator __first, _InputIterator __last,
1168 _OutputIterator __result, const _Tp& __value)
1170 // concept requirements
1171 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1172 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1173 typename iterator_traits<_InputIterator>::value_type>)
1174 __glibcxx_function_requires(_EqualOpConcept<
1175 typename iterator_traits<_InputIterator>::value_type, _Tp>)
1176 __glibcxx_requires_valid_range(__first, __last);
1178 for ( ; __first != __last; ++__first)
1179 if (!(*__first == __value))
1181 *__result = *__first;
1188 * @brief Copy a sequence, removing elements for which a predicate is true.
1189 * @param first An input iterator.
1190 * @param last An input iterator.
1191 * @param result An output iterator.
1192 * @param pred A predicate.
1193 * @return An iterator designating the end of the resulting sequence.
1195 * Copies each element in the range @p [first,last) for which
1196 * @p pred returns true to the range beginning at @p result.
1198 * remove_copy_if() is stable, so the relative order of elements that are
1199 * copied is unchanged.
1201 template<typename _InputIterator, typename _OutputIterator,
1202 typename _Predicate>
1204 remove_copy_if(_InputIterator __first, _InputIterator __last,
1205 _OutputIterator __result, _Predicate __pred)
1207 // concept requirements
1208 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1209 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1210 typename iterator_traits<_InputIterator>::value_type>)
1211 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1212 typename iterator_traits<_InputIterator>::value_type>)
1213 __glibcxx_requires_valid_range(__first, __last);
1215 for ( ; __first != __last; ++__first)
1216 if (!__pred(*__first))
1218 *__result = *__first;
1225 * @brief Remove elements from a sequence.
1226 * @param first An input iterator.
1227 * @param last An input iterator.
1228 * @param value The value to be removed.
1229 * @return An iterator designating the end of the resulting sequence.
1231 * All elements equal to @p value are removed from the range
1234 * remove() is stable, so the relative order of elements that are
1235 * not removed is unchanged.
1237 * Elements between the end of the resulting sequence and @p last
1238 * are still present, but their value is unspecified.
1240 template<typename _ForwardIterator, typename _Tp>
1242 remove(_ForwardIterator __first, _ForwardIterator __last,
1245 // concept requirements
1246 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1248 __glibcxx_function_requires(_EqualOpConcept<
1249 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
1250 __glibcxx_requires_valid_range(__first, __last);
1252 __first = std::find(__first, __last, __value);
1253 _ForwardIterator __i = __first;
1254 return __first == __last ? __first
1255 : std::remove_copy(++__i, __last,
1260 * @brief Remove elements from a sequence using a predicate.
1261 * @param first A forward iterator.
1262 * @param last A forward iterator.
1263 * @param pred A predicate.
1264 * @return An iterator designating the end of the resulting sequence.
1266 * All elements for which @p pred returns true are removed from the range
1269 * remove_if() is stable, so the relative order of elements that are
1270 * not removed is unchanged.
1272 * Elements between the end of the resulting sequence and @p last
1273 * are still present, but their value is unspecified.
1275 template<typename _ForwardIterator, typename _Predicate>
1277 remove_if(_ForwardIterator __first, _ForwardIterator __last,
1280 // concept requirements
1281 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1283 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1284 typename iterator_traits<_ForwardIterator>::value_type>)
1285 __glibcxx_requires_valid_range(__first, __last);
1287 __first = std::find_if(__first, __last, __pred);
1288 _ForwardIterator __i = __first;
1289 return __first == __last ? __first
1290 : std::remove_copy_if(++__i, __last,
1296 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1298 * overloaded for forward iterators and output iterator as result.
1301 template<typename _ForwardIterator, typename _OutputIterator>
1303 __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
1304 _OutputIterator __result,
1305 forward_iterator_tag, output_iterator_tag)
1307 // concept requirements -- taken care of in dispatching function
1308 _ForwardIterator __next = __first;
1309 *__result = *__first;
1310 while (++__next != __last)
1311 if (!(*__first == *__next))
1314 *++__result = *__first;
1321 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1323 * overloaded for input iterators and output iterator as result.
1326 template<typename _InputIterator, typename _OutputIterator>
1328 __unique_copy(_InputIterator __first, _InputIterator __last,
1329 _OutputIterator __result,
1330 input_iterator_tag, output_iterator_tag)
1332 // concept requirements -- taken care of in dispatching function
1333 *__result = *__first;
1337 iterator_traits<_InputIterator>::value_type __value = *__first;
1339 if (++__first == __last)
1342 if (!(__value == *__first))
1343 *++__result = *__first;
1350 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1352 * overloaded for input iterators and forward iterator as result.
1355 template<typename _InputIterator, typename _ForwardIterator>
1357 __unique_copy(_InputIterator __first, _InputIterator __last,
1358 _ForwardIterator __result,
1359 input_iterator_tag, forward_iterator_tag)
1361 // concept requirements -- taken care of in dispatching function
1362 *__result = *__first;
1363 while (++__first != __last)
1364 if (!(*__result == *__first))
1365 *++__result = *__first;
1371 * This is an uglified
1372 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1374 * overloaded for forward iterators and output iterator as result.
1377 template<typename _ForwardIterator, typename _OutputIterator,
1378 typename _BinaryPredicate>
1380 __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
1381 _OutputIterator __result, _BinaryPredicate __binary_pred,
1382 forward_iterator_tag, output_iterator_tag)
1384 // concept requirements -- iterators already checked
1385 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1386 typename iterator_traits<_ForwardIterator>::value_type,
1387 typename iterator_traits<_ForwardIterator>::value_type>)
1389 _ForwardIterator __next = __first;
1390 *__result = *__first;
1391 while (++__next != __last)
1392 if (!__binary_pred(*__first, *__next))
1395 *++__result = *__first;
1402 * This is an uglified
1403 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1405 * overloaded for input iterators and output iterator as result.
1408 template<typename _InputIterator, typename _OutputIterator,
1409 typename _BinaryPredicate>
1411 __unique_copy(_InputIterator __first, _InputIterator __last,
1412 _OutputIterator __result, _BinaryPredicate __binary_pred,
1413 input_iterator_tag, output_iterator_tag)
1415 // concept requirements -- iterators already checked
1416 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1417 typename iterator_traits<_InputIterator>::value_type,
1418 typename iterator_traits<_InputIterator>::value_type>)
1420 *__result = *__first;
1424 iterator_traits<_InputIterator>::value_type __value = *__first;
1426 if (++__first == __last)
1429 if (!__binary_pred(__value, *__first))
1430 *++__result = *__first;
1437 * This is an uglified
1438 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1440 * overloaded for input iterators and forward iterator as result.
1443 template<typename _InputIterator, typename _ForwardIterator,
1444 typename _BinaryPredicate>
1446 __unique_copy(_InputIterator __first, _InputIterator __last,
1447 _ForwardIterator __result, _BinaryPredicate __binary_pred,
1448 input_iterator_tag, forward_iterator_tag)
1450 // concept requirements -- iterators already checked
1451 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1452 typename iterator_traits<_ForwardIterator>::value_type,
1453 typename iterator_traits<_InputIterator>::value_type>)
1455 *__result = *__first;
1456 while (++__first != __last)
1457 if (!__binary_pred(*__result, *__first))
1458 *++__result = *__first;
1463 * @brief Copy a sequence, removing consecutive duplicate values.
1464 * @param first An input iterator.
1465 * @param last An input iterator.
1466 * @param result An output iterator.
1467 * @return An iterator designating the end of the resulting sequence.
1469 * Copies each element in the range @p [first,last) to the range
1470 * beginning at @p result, except that only the first element is copied
1471 * from groups of consecutive elements that compare equal.
1472 * unique_copy() is stable, so the relative order of elements that are
1473 * copied is unchanged.
1476 * _GLIBCXX_RESOLVE_LIB_DEFECTS
1477 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
1480 template<typename _InputIterator, typename _OutputIterator>
1481 inline _OutputIterator
1482 unique_copy(_InputIterator __first, _InputIterator __last,
1483 _OutputIterator __result)
1485 // concept requirements
1486 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1487 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1488 typename iterator_traits<_InputIterator>::value_type>)
1489 __glibcxx_function_requires(_EqualityComparableConcept<
1490 typename iterator_traits<_InputIterator>::value_type>)
1491 __glibcxx_requires_valid_range(__first, __last);
1493 if (__first == __last)
1495 return std::__unique_copy(__first, __last, __result,
1496 std::__iterator_category(__first),
1497 std::__iterator_category(__result));
1501 * @brief Copy a sequence, removing consecutive values using a predicate.
1502 * @param first An input iterator.
1503 * @param last An input iterator.
1504 * @param result An output iterator.
1505 * @param binary_pred A binary predicate.
1506 * @return An iterator designating the end of the resulting sequence.
1508 * Copies each element in the range @p [first,last) to the range
1509 * beginning at @p result, except that only the first element is copied
1510 * from groups of consecutive elements for which @p binary_pred returns
1512 * unique_copy() is stable, so the relative order of elements that are
1513 * copied is unchanged.
1516 * _GLIBCXX_RESOLVE_LIB_DEFECTS
1517 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
1520 template<typename _InputIterator, typename _OutputIterator,
1521 typename _BinaryPredicate>
1522 inline _OutputIterator
1523 unique_copy(_InputIterator __first, _InputIterator __last,
1524 _OutputIterator __result,
1525 _BinaryPredicate __binary_pred)
1527 // concept requirements -- predicates checked later
1528 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1529 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1530 typename iterator_traits<_InputIterator>::value_type>)
1531 __glibcxx_requires_valid_range(__first, __last);
1533 if (__first == __last)
1535 return std::__unique_copy(__first, __last, __result, __binary_pred,
1536 std::__iterator_category(__first),
1537 std::__iterator_category(__result));
1541 * @brief Remove consecutive duplicate values from a sequence.
1542 * @param first A forward iterator.
1543 * @param last A forward iterator.
1544 * @return An iterator designating the end of the resulting sequence.
1546 * Removes all but the first element from each group of consecutive
1547 * values that compare equal.
1548 * unique() is stable, so the relative order of elements that are
1549 * not removed is unchanged.
1550 * Elements between the end of the resulting sequence and @p last
1551 * are still present, but their value is unspecified.
1553 template<typename _ForwardIterator>
1555 unique(_ForwardIterator __first, _ForwardIterator __last)
1557 // concept requirements
1558 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1560 __glibcxx_function_requires(_EqualityComparableConcept<
1561 typename iterator_traits<_ForwardIterator>::value_type>)
1562 __glibcxx_requires_valid_range(__first, __last);
1564 // Skip the beginning, if already unique.
1565 __first = std::adjacent_find(__first, __last);
1566 if (__first == __last)
1569 // Do the real copy work.
1570 _ForwardIterator __dest = __first;
1572 while (++__first != __last)
1573 if (!(*__dest == *__first))
1574 *++__dest = *__first;
1579 * @brief Remove consecutive values from a sequence using a predicate.
1580 * @param first A forward iterator.
1581 * @param last A forward iterator.
1582 * @param binary_pred A binary predicate.
1583 * @return An iterator designating the end of the resulting sequence.
1585 * Removes all but the first element from each group of consecutive
1586 * values for which @p binary_pred returns true.
1587 * unique() is stable, so the relative order of elements that are
1588 * not removed is unchanged.
1589 * Elements between the end of the resulting sequence and @p last
1590 * are still present, but their value is unspecified.
1592 template<typename _ForwardIterator, typename _BinaryPredicate>
1594 unique(_ForwardIterator __first, _ForwardIterator __last,
1595 _BinaryPredicate __binary_pred)
1597 // concept requirements
1598 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1600 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1601 typename iterator_traits<_ForwardIterator>::value_type,
1602 typename iterator_traits<_ForwardIterator>::value_type>)
1603 __glibcxx_requires_valid_range(__first, __last);
1605 // Skip the beginning, if already unique.
1606 __first = std::adjacent_find(__first, __last, __binary_pred);
1607 if (__first == __last)
1610 // Do the real copy work.
1611 _ForwardIterator __dest = __first;
1613 while (++__first != __last)
1614 if (!__binary_pred(*__dest, *__first))
1615 *++__dest = *__first;
1621 * This is an uglified reverse(_BidirectionalIterator,
1622 * _BidirectionalIterator)
1623 * overloaded for bidirectional iterators.
1626 template<typename _BidirectionalIterator>
1628 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last,
1629 bidirectional_iterator_tag)
1632 if (__first == __last || __first == --__last)
1636 std::iter_swap(__first, __last);
1643 * This is an uglified reverse(_BidirectionalIterator,
1644 * _BidirectionalIterator)
1645 * overloaded for random access iterators.
1648 template<typename _RandomAccessIterator>
1650 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last,
1651 random_access_iterator_tag)
1653 if (__first == __last)
1656 while (__first < __last)
1658 std::iter_swap(__first, __last);
1665 * @brief Reverse a sequence.
1666 * @param first A bidirectional iterator.
1667 * @param last A bidirectional iterator.
1668 * @return reverse() returns no value.
1670 * Reverses the order of the elements in the range @p [first,last),
1671 * so that the first element becomes the last etc.
1672 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1673 * swaps @p *(first+i) and @p *(last-(i+1))
1675 template<typename _BidirectionalIterator>
1677 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
1679 // concept requirements
1680 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
1681 _BidirectionalIterator>)
1682 __glibcxx_requires_valid_range(__first, __last);
1683 std::__reverse(__first, __last, std::__iterator_category(__first));
1687 * @brief Copy a sequence, reversing its elements.
1688 * @param first A bidirectional iterator.
1689 * @param last A bidirectional iterator.
1690 * @param result An output iterator.
1691 * @return An iterator designating the end of the resulting sequence.
1693 * Copies the elements in the range @p [first,last) to the range
1694 * @p [result,result+(last-first)) such that the order of the
1695 * elements is reversed.
1696 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1697 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1698 * The ranges @p [first,last) and @p [result,result+(last-first))
1701 template<typename _BidirectionalIterator, typename _OutputIterator>
1703 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last,
1704 _OutputIterator __result)
1706 // concept requirements
1707 __glibcxx_function_requires(_BidirectionalIteratorConcept<
1708 _BidirectionalIterator>)
1709 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1710 typename iterator_traits<_BidirectionalIterator>::value_type>)
1711 __glibcxx_requires_valid_range(__first, __last);
1713 while (__first != __last)
1716 *__result = *__last;
1725 * This is a helper function for the rotate algorithm specialized on RAIs.
1726 * It returns the greatest common divisor of two integer values.
1729 template<typename _EuclideanRingElement>
1730 _EuclideanRingElement
1731 __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n)
1735 _EuclideanRingElement __t = __m % __n;
1744 * This is a helper function for the rotate algorithm.
1747 template<typename _ForwardIterator>
1749 __rotate(_ForwardIterator __first,
1750 _ForwardIterator __middle,
1751 _ForwardIterator __last,
1752 forward_iterator_tag)
1754 if (__first == __middle || __last == __middle)
1757 _ForwardIterator __first2 = __middle;
1760 swap(*__first, *__first2);
1763 if (__first == __middle)
1764 __middle = __first2;
1766 while (__first2 != __last);
1768 __first2 = __middle;
1770 while (__first2 != __last)
1772 swap(*__first, *__first2);
1775 if (__first == __middle)
1776 __middle = __first2;
1777 else if (__first2 == __last)
1778 __first2 = __middle;
1784 * This is a helper function for the rotate algorithm.
1787 template<typename _BidirectionalIterator>
1789 __rotate(_BidirectionalIterator __first,
1790 _BidirectionalIterator __middle,
1791 _BidirectionalIterator __last,
1792 bidirectional_iterator_tag)
1794 // concept requirements
1795 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
1796 _BidirectionalIterator>)
1798 if (__first == __middle || __last == __middle)
1801 std::__reverse(__first, __middle, bidirectional_iterator_tag());
1802 std::__reverse(__middle, __last, bidirectional_iterator_tag());
1804 while (__first != __middle && __middle != __last)
1806 swap(*__first, *--__last);
1810 if (__first == __middle)
1811 std::__reverse(__middle, __last, bidirectional_iterator_tag());
1813 std::__reverse(__first, __middle, bidirectional_iterator_tag());
1818 * This is a helper function for the rotate algorithm.
1821 template<typename _RandomAccessIterator>
1823 __rotate(_RandomAccessIterator __first,
1824 _RandomAccessIterator __middle,
1825 _RandomAccessIterator __last,
1826 random_access_iterator_tag)
1828 // concept requirements
1829 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
1830 _RandomAccessIterator>)
1832 if (__first == __middle || __last == __middle)
1835 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1837 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1840 const _Distance __n = __last - __first;
1841 const _Distance __k = __middle - __first;
1842 const _Distance __l = __n - __k;
1846 std::swap_ranges(__first, __middle, __middle);
1850 const _Distance __d = __gcd(__n, __k);
1852 for (_Distance __i = 0; __i < __d; __i++)
1854 _ValueType __tmp = *__first;
1855 _RandomAccessIterator __p = __first;
1859 for (_Distance __j = 0; __j < __l / __d; __j++)
1861 if (__p > __first + __l)
1863 *__p = *(__p - __l);
1867 *__p = *(__p + __k);
1873 for (_Distance __j = 0; __j < __k / __d - 1; __j ++)
1875 if (__p < __last - __k)
1877 *__p = *(__p + __k);
1880 *__p = * (__p - __l);
1891 * @brief Rotate the elements of a sequence.
1892 * @param first A forward iterator.
1893 * @param middle A forward iterator.
1894 * @param last A forward iterator.
1897 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1898 * positions so that the element at @p middle is moved to @p first, the
1899 * element at @p middle+1 is moved to @first+1 and so on for each element
1900 * in the range @p [first,last).
1902 * This effectively swaps the ranges @p [first,middle) and
1905 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1906 * each @p n in the range @p [0,last-first).
1908 template<typename _ForwardIterator>
1910 rotate(_ForwardIterator __first, _ForwardIterator __middle,
1911 _ForwardIterator __last)
1913 // concept requirements
1914 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1916 __glibcxx_requires_valid_range(__first, __middle);
1917 __glibcxx_requires_valid_range(__middle, __last);
1919 typedef typename iterator_traits<_ForwardIterator>::iterator_category
1921 std::__rotate(__first, __middle, __last, _IterType());
1925 * @brief Copy a sequence, rotating its elements.
1926 * @param first A forward iterator.
1927 * @param middle A forward iterator.
1928 * @param last A forward iterator.
1929 * @param result An output iterator.
1930 * @return An iterator designating the end of the resulting sequence.
1932 * Copies the elements of the range @p [first,last) to the range
1933 * beginning at @result, rotating the copied elements by @p (middle-first)
1934 * positions so that the element at @p middle is moved to @p result, the
1935 * element at @p middle+1 is moved to @result+1 and so on for each element
1936 * in the range @p [first,last).
1938 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1939 * each @p n in the range @p [0,last-first).
1941 template<typename _ForwardIterator, typename _OutputIterator>
1943 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle,
1944 _ForwardIterator __last, _OutputIterator __result)
1946 // concept requirements
1947 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1948 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1949 typename iterator_traits<_ForwardIterator>::value_type>)
1950 __glibcxx_requires_valid_range(__first, __middle);
1951 __glibcxx_requires_valid_range(__middle, __last);
1953 return std::copy(__first, __middle,
1954 std::copy(__middle, __last, __result));
1958 * @brief Randomly shuffle the elements of a sequence.
1959 * @param first A forward iterator.
1960 * @param last A forward iterator.
1963 * Reorder the elements in the range @p [first,last) using a random
1964 * distribution, so that every possible ordering of the sequence is
1967 template<typename _RandomAccessIterator>
1969 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
1971 // concept requirements
1972 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
1973 _RandomAccessIterator>)
1974 __glibcxx_requires_valid_range(__first, __last);
1976 if (__first != __last)
1977 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
1978 std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1)));
1982 * @brief Shuffle the elements of a sequence using a random number
1984 * @param first A forward iterator.
1985 * @param last A forward iterator.
1986 * @param rand The RNG functor or function.
1989 * Reorders the elements in the range @p [first,last) using @p rand to
1990 * provide a random distribution. Calling @p rand(N) for a positive
1991 * integer @p N should return a randomly chosen integer from the
1994 template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
1996 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
1997 _RandomNumberGenerator& __rand)
1999 // concept requirements
2000 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
2001 _RandomAccessIterator>)
2002 __glibcxx_requires_valid_range(__first, __last);
2004 if (__first == __last)
2006 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
2007 std::iter_swap(__i, __first + __rand((__i - __first) + 1));
2013 * This is a helper function...
2016 template<typename _ForwardIterator, typename _Predicate>
2018 __partition(_ForwardIterator __first, _ForwardIterator __last,
2020 forward_iterator_tag)
2022 if (__first == __last)
2025 while (__pred(*__first))
2026 if (++__first == __last)
2029 _ForwardIterator __next = __first;
2031 while (++__next != __last)
2032 if (__pred(*__next))
2034 swap(*__first, *__next);
2043 * This is a helper function...
2046 template<typename _BidirectionalIterator, typename _Predicate>
2047 _BidirectionalIterator
2048 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last,
2050 bidirectional_iterator_tag)
2055 if (__first == __last)
2057 else if (__pred(*__first))
2063 if (__first == __last)
2065 else if (!__pred(*__last))
2069 std::iter_swap(__first, __last);
2075 * @brief Move elements for which a predicate is true to the beginning
2077 * @param first A forward iterator.
2078 * @param last A forward iterator.
2079 * @param pred A predicate functor.
2080 * @return An iterator @p middle such that @p pred(i) is true for each
2081 * iterator @p i in the range @p [first,middle) and false for each @p i
2082 * in the range @p [middle,last).
2084 * @p pred must not modify its operand. @p partition() does not preserve
2085 * the relative ordering of elements in each group, use
2086 * @p stable_partition() if this is needed.
2088 template<typename _ForwardIterator, typename _Predicate>
2089 inline _ForwardIterator
2090 partition(_ForwardIterator __first, _ForwardIterator __last,
2093 // concept requirements
2094 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
2096 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
2097 typename iterator_traits<_ForwardIterator>::value_type>)
2098 __glibcxx_requires_valid_range(__first, __last);
2100 return std::__partition(__first, __last, __pred,
2101 std::__iterator_category(__first));
2107 * This is a helper function...
2110 template<typename _ForwardIterator, typename _Predicate, typename _Distance>
2112 __inplace_stable_partition(_ForwardIterator __first,
2113 _ForwardIterator __last,
2114 _Predicate __pred, _Distance __len)
2117 return __pred(*__first) ? __last : __first;
2118 _ForwardIterator __middle = __first;
2119 std::advance(__middle, __len / 2);
2120 _ForwardIterator __begin = std::__inplace_stable_partition(__first,
2124 _ForwardIterator __end = std::__inplace_stable_partition(__middle, __last,
2128 std::rotate(__begin, __middle, __end);
2129 std::advance(__begin, std::distance(__middle, __end));
2135 * This is a helper function...
2138 template<typename _ForwardIterator, typename _Pointer, typename _Predicate,
2141 __stable_partition_adaptive(_ForwardIterator __first,
2142 _ForwardIterator __last,
2143 _Predicate __pred, _Distance __len,
2145 _Distance __buffer_size)
2147 if (__len <= __buffer_size)
2149 _ForwardIterator __result1 = __first;
2150 _Pointer __result2 = __buffer;
2151 for ( ; __first != __last ; ++__first)
2152 if (__pred(*__first))
2154 *__result1 = *__first;
2159 *__result2 = *__first;
2162 std::copy(__buffer, __result2, __result1);
2167 _ForwardIterator __middle = __first;
2168 std::advance(__middle, __len / 2);
2169 _ForwardIterator __begin =
2170 std::__stable_partition_adaptive(__first, __middle, __pred,
2171 __len / 2, __buffer,
2173 _ForwardIterator __end =
2174 std::__stable_partition_adaptive(__middle, __last, __pred,
2176 __buffer, __buffer_size);
2177 std::rotate(__begin, __middle, __end);
2178 std::advance(__begin, std::distance(__middle, __end));
2184 * @brief Move elements for which a predicate is true to the beginning
2185 * of a sequence, preserving relative ordering.
2186 * @param first A forward iterator.
2187 * @param last A forward iterator.
2188 * @param pred A predicate functor.
2189 * @return An iterator @p middle such that @p pred(i) is true for each
2190 * iterator @p i in the range @p [first,middle) and false for each @p i
2191 * in the range @p [middle,last).
2193 * Performs the same function as @p partition() with the additional
2194 * guarantee that the relative ordering of elements in each group is
2195 * preserved, so any two elements @p x and @p y in the range
2196 * @p [first,last) such that @p pred(x)==pred(y) will have the same
2197 * relative ordering after calling @p stable_partition().
2199 template<typename _ForwardIterator, typename _Predicate>
2201 stable_partition(_ForwardIterator __first, _ForwardIterator __last,
2204 // concept requirements
2205 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
2207 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
2208 typename iterator_traits<_ForwardIterator>::value_type>)
2209 __glibcxx_requires_valid_range(__first, __last);
2211 if (__first == __last)
2215 typedef typename iterator_traits<_ForwardIterator>::value_type
2217 typedef typename iterator_traits<_ForwardIterator>::difference_type
2220 _Temporary_buffer<_ForwardIterator, _ValueType> __buf(__first,
2222 if (__buf.size() > 0)
2224 std::__stable_partition_adaptive(__first, __last, __pred,
2225 _DistanceType(__buf.requested_size()),
2226 __buf.begin(), __buf.size());
2229 std::__inplace_stable_partition(__first, __last, __pred,
2230 _DistanceType(__buf.requested_size()));
2236 * This is a helper function...
2239 template<typename _RandomAccessIterator, typename _Tp>
2240 _RandomAccessIterator
2241 __unguarded_partition(_RandomAccessIterator __first,
2242 _RandomAccessIterator __last, _Tp __pivot)
2246 while (*__first < __pivot)
2249 while (__pivot < *__last)
2251 if (!(__first < __last))
2253 std::iter_swap(__first, __last);
2260 * This is a helper function...
2263 template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
2264 _RandomAccessIterator
2265 __unguarded_partition(_RandomAccessIterator __first,
2266 _RandomAccessIterator __last,
2267 _Tp __pivot, _Compare __comp)
2271 while (__comp(*__first, __pivot))
2274 while (__comp(__pivot, *__last))
2276 if (!(__first < __last))
2278 std::iter_swap(__first, __last);
2286 * This controls some aspect of the sort routines.
2289 enum { _S_threshold = 16 };
2293 * This is a helper function for the sort routine.
2296 template<typename _RandomAccessIterator, typename _Tp>
2298 __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val)
2300 _RandomAccessIterator __next = __last;
2302 while (__val < *__next)
2313 * This is a helper function for the sort routine.
2316 template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
2318 __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val,
2321 _RandomAccessIterator __next = __last;
2323 while (__comp(__val, *__next))
2334 * This is a helper function for the sort routine.
2337 template<typename _RandomAccessIterator>
2339 __insertion_sort(_RandomAccessIterator __first,
2340 _RandomAccessIterator __last)
2342 if (__first == __last)
2345 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
2347 typename iterator_traits<_RandomAccessIterator>::value_type
2349 if (__val < *__first)
2351 std::copy_backward(__first, __i, __i + 1);
2355 std::__unguarded_linear_insert(__i, __val);
2361 * This is a helper function for the sort routine.
2364 template<typename _RandomAccessIterator, typename _Compare>
2366 __insertion_sort(_RandomAccessIterator __first,
2367 _RandomAccessIterator __last, _Compare __comp)
2369 if (__first == __last) return;
2371 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
2373 typename iterator_traits<_RandomAccessIterator>::value_type
2375 if (__comp(__val, *__first))
2377 std::copy_backward(__first, __i, __i + 1);
2381 std::__unguarded_linear_insert(__i, __val, __comp);
2387 * This is a helper function for the sort routine.
2390 template<typename _RandomAccessIterator>
2392 __unguarded_insertion_sort(_RandomAccessIterator __first,
2393 _RandomAccessIterator __last)
2395 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2398 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
2399 std::__unguarded_linear_insert(__i, _ValueType(*__i));
2404 * This is a helper function for the sort routine.
2407 template<typename _RandomAccessIterator, typename _Compare>
2409 __unguarded_insertion_sort(_RandomAccessIterator __first,
2410 _RandomAccessIterator __last, _Compare __comp)
2412 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2415 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
2416 std::__unguarded_linear_insert(__i, _ValueType(*__i), __comp);
2421 * This is a helper function for the sort routine.
2424 template<typename _RandomAccessIterator>
2426 __final_insertion_sort(_RandomAccessIterator __first,
2427 _RandomAccessIterator __last)
2429 if (__last - __first > int(_S_threshold))
2431 std::__insertion_sort(__first, __first + int(_S_threshold));
2432 std::__unguarded_insertion_sort(__first + int(_S_threshold), __last);
2435 std::__insertion_sort(__first, __last);
2440 * This is a helper function for the sort routine.
2443 template<typename _RandomAccessIterator, typename _Compare>
2445 __final_insertion_sort(_RandomAccessIterator __first,
2446 _RandomAccessIterator __last, _Compare __comp)
2448 if (__last - __first > int(_S_threshold))
2450 std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
2451 std::__unguarded_insertion_sort(__first + int(_S_threshold), __last,
2455 std::__insertion_sort(__first, __last, __comp);
2460 * This is a helper function for the sort routine.
2463 template<typename _Size>
2468 for (__k = 0; __n != 1; __n >>= 1)
2474 * @brief Sort the smallest elements of a sequence.
2475 * @param first An iterator.
2476 * @param middle Another iterator.
2477 * @param last Another iterator.
2480 * Sorts the smallest @p (middle-first) elements in the range
2481 * @p [first,last) and moves them to the range @p [first,middle). The
2482 * order of the remaining elements in the range @p [middle,last) is
2484 * After the sort if @p i and @j are iterators in the range
2485 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2486 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
2488 template<typename _RandomAccessIterator>
2490 partial_sort(_RandomAccessIterator __first,
2491 _RandomAccessIterator __middle,
2492 _RandomAccessIterator __last)
2494 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2497 // concept requirements
2498 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
2499 _RandomAccessIterator>)
2500 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
2501 __glibcxx_requires_valid_range(__first, __middle);
2502 __glibcxx_requires_valid_range(__middle, __last);
2504 std::make_heap(__first, __middle);
2505 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
2506 if (*__i < *__first)
2507 std::__pop_heap(__first, __middle, __i, _ValueType(*__i));
2508 std::sort_heap(__first, __middle);
2512 * @brief Sort the smallest elements of a sequence using a predicate
2514 * @param first An iterator.
2515 * @param middle Another iterator.
2516 * @param last Another iterator.
2517 * @param comp A comparison functor.
2520 * Sorts the smallest @p (middle-first) elements in the range
2521 * @p [first,last) and moves them to the range @p [first,middle). The
2522 * order of the remaining elements in the range @p [middle,last) is
2524 * After the sort if @p i and @j are iterators in the range
2525 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2526 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
2529 template<typename _RandomAccessIterator, typename _Compare>
2531 partial_sort(_RandomAccessIterator __first,
2532 _RandomAccessIterator __middle,
2533 _RandomAccessIterator __last,
2536 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2539 // concept requirements
2540 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
2541 _RandomAccessIterator>)
2542 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2543 _ValueType, _ValueType>)
2544 __glibcxx_requires_valid_range(__first, __middle);
2545 __glibcxx_requires_valid_range(__middle, __last);
2547 std::make_heap(__first, __middle, __comp);
2548 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
2549 if (__comp(*__i, *__first))
2550 std::__pop_heap(__first, __middle, __i, _ValueType(*__i), __comp);
2551 std::sort_heap(__first, __middle, __comp);
2555 * @brief Copy the smallest elements of a sequence.
2556 * @param first An iterator.
2557 * @param last Another iterator.
2558 * @param result_first A random-access iterator.
2559 * @param result_last Another random-access iterator.
2560 * @return An iterator indicating the end of the resulting sequence.
2562 * Copies and sorts the smallest N values from the range @p [first,last)
2563 * to the range beginning at @p result_first, where the number of
2564 * elements to be copied, @p N, is the smaller of @p (last-first) and
2565 * @p (result_last-result_first).
2566 * After the sort if @p i and @j are iterators in the range
2567 * @p [result_first,result_first+N) such that @i precedes @j then
2568 * @p *j<*i is false.
2569 * The value returned is @p result_first+N.
2571 template<typename _InputIterator, typename _RandomAccessIterator>
2572 _RandomAccessIterator
2573 partial_sort_copy(_InputIterator __first, _InputIterator __last,
2574 _RandomAccessIterator __result_first,
2575 _RandomAccessIterator __result_last)
2577 typedef typename iterator_traits<_InputIterator>::value_type
2579 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2581 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
2584 // concept requirements
2585 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
2586 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
2588 __glibcxx_function_requires(_LessThanOpConcept<_InputValueType,
2590 __glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>)
2591 __glibcxx_requires_valid_range(__first, __last);
2592 __glibcxx_requires_valid_range(__result_first, __result_last);
2594 if (__result_first == __result_last)
2595 return __result_last;
2596 _RandomAccessIterator __result_real_last = __result_first;
2597 while(__first != __last && __result_real_last != __result_last)
2599 *__result_real_last = *__first;
2600 ++__result_real_last;
2603 std::make_heap(__result_first, __result_real_last);
2604 while (__first != __last)
2606 if (*__first < *__result_first)
2607 std::__adjust_heap(__result_first, _DistanceType(0),
2608 _DistanceType(__result_real_last
2610 _InputValueType(*__first));
2613 std::sort_heap(__result_first, __result_real_last);
2614 return __result_real_last;
2618 * @brief Copy the smallest elements of a sequence using a predicate for
2620 * @param first An input iterator.
2621 * @param last Another input iterator.
2622 * @param result_first A random-access iterator.
2623 * @param result_last Another random-access iterator.
2624 * @param comp A comparison functor.
2625 * @return An iterator indicating the end of the resulting sequence.
2627 * Copies and sorts the smallest N values from the range @p [first,last)
2628 * to the range beginning at @p result_first, where the number of
2629 * elements to be copied, @p N, is the smaller of @p (last-first) and
2630 * @p (result_last-result_first).
2631 * After the sort if @p i and @j are iterators in the range
2632 * @p [result_first,result_first+N) such that @i precedes @j then
2633 * @p comp(*j,*i) is false.
2634 * The value returned is @p result_first+N.
2636 template<typename _InputIterator, typename _RandomAccessIterator, typename _Compare>
2637 _RandomAccessIterator
2638 partial_sort_copy(_InputIterator __first, _InputIterator __last,
2639 _RandomAccessIterator __result_first,
2640 _RandomAccessIterator __result_last,
2643 typedef typename iterator_traits<_InputIterator>::value_type
2645 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2647 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
2650 // concept requirements
2651 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
2652 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
2653 _RandomAccessIterator>)
2654 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
2656 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2657 _InputValueType, _OutputValueType>)
2658 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2659 _OutputValueType, _OutputValueType>)
2660 __glibcxx_requires_valid_range(__first, __last);
2661 __glibcxx_requires_valid_range(__result_first, __result_last);
2663 if (__result_first == __result_last)
2664 return __result_last;
2665 _RandomAccessIterator __result_real_last = __result_first;
2666 while(__first != __last && __result_real_last != __result_last)
2668 *__result_real_last = *__first;
2669 ++__result_real_last;
2672 std::make_heap(__result_first, __result_real_last, __comp);
2673 while (__first != __last)
2675 if (__comp(*__first, *__result_first))
2676 std::__adjust_heap(__result_first, _DistanceType(0),
2677 _DistanceType(__result_real_last
2679 _InputValueType(*__first),
2683 std::sort_heap(__result_first, __result_real_last, __comp);
2684 return __result_real_last;
2689 * This is a helper function for the sort routine.
2692 template<typename _RandomAccessIterator, typename _Size>
2694 __introsort_loop(_RandomAccessIterator __first,
2695 _RandomAccessIterator __last,
2696 _Size __depth_limit)
2698 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2701 while (__last - __first > int(_S_threshold))
2703 if (__depth_limit == 0)
2705 std::partial_sort(__first, __last, __last);
2709 _RandomAccessIterator __cut =
2710 std::__unguarded_partition(__first, __last,
2711 _ValueType(std::__median(*__first,
2718 std::__introsort_loop(__cut, __last, __depth_limit);
2725 * This is a helper function for the sort routine.
2728 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
2730 __introsort_loop(_RandomAccessIterator __first,
2731 _RandomAccessIterator __last,
2732 _Size __depth_limit, _Compare __comp)
2734 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2737 while (__last - __first > int(_S_threshold))
2739 if (__depth_limit == 0)
2741 std::partial_sort(__first, __last, __last, __comp);
2745 _RandomAccessIterator __cut =
2746 std::__unguarded_partition(__first, __last,
2747 _ValueType(std::__median(*__first,
2755 std::__introsort_loop(__cut, __last, __depth_limit, __comp);
2761 * @brief Sort the elements of a sequence.
2762 * @param first An iterator.
2763 * @param last Another iterator.
2766 * Sorts the elements in the range @p [first,last) in ascending order,
2767 * such that @p *(i+1)<*i is false for each iterator @p i in the range
2768 * @p [first,last-1).
2770 * The relative ordering of equivalent elements is not preserved, use
2771 * @p stable_sort() if this is needed.
2773 template<typename _RandomAccessIterator>
2775 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
2777 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2780 // concept requirements
2781 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
2782 _RandomAccessIterator>)
2783 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
2784 __glibcxx_requires_valid_range(__first, __last);
2786 if (__first != __last)
2788 std::__introsort_loop(__first, __last, __lg(__last - __first) * 2);
2789 std::__final_insertion_sort(__first, __last);
2794 * @brief Sort the elements of a sequence using a predicate for comparison.
2795 * @param first An iterator.
2796 * @param last Another iterator.
2797 * @param comp A comparison functor.
2800 * Sorts the elements in the range @p [first,last) in ascending order,
2801 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
2802 * range @p [first,last-1).
2804 * The relative ordering of equivalent elements is not preserved, use
2805 * @p stable_sort() if this is needed.
2807 template<typename _RandomAccessIterator, typename _Compare>
2809 sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
2812 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2815 // concept requirements
2816 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
2817 _RandomAccessIterator>)
2818 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType,
2820 __glibcxx_requires_valid_range(__first, __last);
2822 if (__first != __last)
2824 std::__introsort_loop(__first, __last, __lg(__last - __first) * 2,
2826 std::__final_insertion_sort(__first, __last, __comp);
2831 * @brief Finds the first position in which @a val could be inserted
2832 * without changing the ordering.
2833 * @param first An iterator.
2834 * @param last Another iterator.
2835 * @param val The search term.
2836 * @return An iterator pointing to the first element "not less than" @a val,
2837 * or end() if every element is less than @a val.
2838 * @ingroup binarysearch
2840 template<typename _ForwardIterator, typename _Tp>
2842 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
2845 typedef typename iterator_traits<_ForwardIterator>::value_type
2847 typedef typename iterator_traits<_ForwardIterator>::difference_type
2850 // concept requirements
2851 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2852 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
2853 __glibcxx_requires_partitioned(__first, __last, __val);
2855 _DistanceType __len = std::distance(__first, __last);
2856 _DistanceType __half;
2857 _ForwardIterator __middle;
2861 __half = __len >> 1;
2863 std::advance(__middle, __half);
2864 if (*__middle < __val)
2868 __len = __len - __half - 1;
2877 * @brief Finds the first position in which @a val could be inserted
2878 * without changing the ordering.
2879 * @param first An iterator.
2880 * @param last Another iterator.
2881 * @param val The search term.
2882 * @param comp A functor to use for comparisons.
2883 * @return An iterator pointing to the first element "not less than" @a val,
2884 * or end() if every element is less than @a val.
2885 * @ingroup binarysearch
2887 * The comparison function should have the same effects on ordering as
2888 * the function used for the initial sort.
2890 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2892 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
2893 const _Tp& __val, _Compare __comp)
2895 typedef typename iterator_traits<_ForwardIterator>::value_type
2897 typedef typename iterator_traits<_ForwardIterator>::difference_type
2900 // concept requirements
2901 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2902 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2904 __glibcxx_requires_partitioned_pred(__first, __last, __val, __comp);
2906 _DistanceType __len = std::distance(__first, __last);
2907 _DistanceType __half;
2908 _ForwardIterator __middle;
2912 __half = __len >> 1;
2914 std::advance(__middle, __half);
2915 if (__comp(*__middle, __val))
2919 __len = __len - __half - 1;
2928 * @brief Finds the last position in which @a val could be inserted
2929 * without changing the ordering.
2930 * @param first An iterator.
2931 * @param last Another iterator.
2932 * @param val The search term.
2933 * @return An iterator pointing to the first element greater than @a val,
2934 * or end() if no elements are greater than @a val.
2935 * @ingroup binarysearch
2937 template<typename _ForwardIterator, typename _Tp>
2939 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2942 typedef typename iterator_traits<_ForwardIterator>::value_type
2944 typedef typename iterator_traits<_ForwardIterator>::difference_type
2947 // concept requirements
2948 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2949 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2950 __glibcxx_requires_partitioned(__first, __last, __val);
2952 _DistanceType __len = std::distance(__first, __last);
2953 _DistanceType __half;
2954 _ForwardIterator __middle;
2958 __half = __len >> 1;
2960 std::advance(__middle, __half);
2961 if (__val < *__middle)
2967 __len = __len - __half - 1;
2974 * @brief Finds the last position in which @a val could be inserted
2975 * without changing the ordering.
2976 * @param first An iterator.
2977 * @param last Another iterator.
2978 * @param val The search term.
2979 * @param comp A functor to use for comparisons.
2980 * @return An iterator pointing to the first element greater than @a val,
2981 * or end() if no elements are greater than @a val.
2982 * @ingroup binarysearch
2984 * The comparison function should have the same effects on ordering as
2985 * the function used for the initial sort.
2987 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2989 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2990 const _Tp& __val, _Compare __comp)
2992 typedef typename iterator_traits<_ForwardIterator>::value_type
2994 typedef typename iterator_traits<_ForwardIterator>::difference_type
2997 // concept requirements
2998 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2999 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3001 __glibcxx_requires_partitioned_pred(__first, __last, __val, __comp);
3003 _DistanceType __len = std::distance(__first, __last);
3004 _DistanceType __half;
3005 _ForwardIterator __middle;
3009 __half = __len >> 1;
3011 std::advance(__middle, __half);
3012 if (__comp(__val, *__middle))
3018 __len = __len - __half - 1;
3026 * This is a helper function for the merge routines.
3029 template<typename _BidirectionalIterator, typename _Distance>
3031 __merge_without_buffer(_BidirectionalIterator __first,
3032 _BidirectionalIterator __middle,
3033 _BidirectionalIterator __last,
3034 _Distance __len1, _Distance __len2)
3036 if (__len1 == 0 || __len2 == 0)
3038 if (__len1 + __len2 == 2)
3040 if (*__middle < *__first)
3041 std::iter_swap(__first, __middle);
3044 _BidirectionalIterator __first_cut = __first;
3045 _BidirectionalIterator __second_cut = __middle;
3046 _Distance __len11 = 0;
3047 _Distance __len22 = 0;
3048 if (__len1 > __len2)
3050 __len11 = __len1 / 2;
3051 std::advance(__first_cut, __len11);
3052 __second_cut = std::lower_bound(__middle, __last, *__first_cut);
3053 __len22 = std::distance(__middle, __second_cut);
3057 __len22 = __len2 / 2;
3058 std::advance(__second_cut, __len22);
3059 __first_cut = std::upper_bound(__first, __middle, *__second_cut);
3060 __len11 = std::distance(__first, __first_cut);
3062 std::rotate(__first_cut, __middle, __second_cut);
3063 _BidirectionalIterator __new_middle = __first_cut;
3064 std::advance(__new_middle, std::distance(__middle, __second_cut));
3065 std::__merge_without_buffer(__first, __first_cut, __new_middle,
3067 std::__merge_without_buffer(__new_middle, __second_cut, __last,
3068 __len1 - __len11, __len2 - __len22);
3073 * This is a helper function for the merge routines.
3076 template<typename _BidirectionalIterator, typename _Distance,
3079 __merge_without_buffer(_BidirectionalIterator __first,
3080 _BidirectionalIterator __middle,
3081 _BidirectionalIterator __last,
3082 _Distance __len1, _Distance __len2,
3085 if (__len1 == 0 || __len2 == 0)
3087 if (__len1 + __len2 == 2)
3089 if (__comp(*__middle, *__first))
3090 std::iter_swap(__first, __middle);
3093 _BidirectionalIterator __first_cut = __first;
3094 _BidirectionalIterator __second_cut = __middle;
3095 _Distance __len11 = 0;
3096 _Distance __len22 = 0;
3097 if (__len1 > __len2)
3099 __len11 = __len1 / 2;
3100 std::advance(__first_cut, __len11);
3101 __second_cut = std::lower_bound(__middle, __last, *__first_cut,
3103 __len22 = std::distance(__middle, __second_cut);
3107 __len22 = __len2 / 2;
3108 std::advance(__second_cut, __len22);
3109 __first_cut = std::upper_bound(__first, __middle, *__second_cut,
3111 __len11 = std::distance(__first, __first_cut);
3113 std::rotate(__first_cut, __middle, __second_cut);
3114 _BidirectionalIterator __new_middle = __first_cut;
3115 std::advance(__new_middle, std::distance(__middle, __second_cut));
3116 std::__merge_without_buffer(__first, __first_cut, __new_middle,
3117 __len11, __len22, __comp);
3118 std::__merge_without_buffer(__new_middle, __second_cut, __last,
3119 __len1 - __len11, __len2 - __len22, __comp);
3124 * This is a helper function for the stable sorting routines.
3127 template<typename _RandomAccessIterator>
3129 __inplace_stable_sort(_RandomAccessIterator __first,
3130 _RandomAccessIterator __last)
3132 if (__last - __first < 15)
3134 std::__insertion_sort(__first, __last);
3137 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
3138 std::__inplace_stable_sort(__first, __middle);
3139 std::__inplace_stable_sort(__middle, __last);
3140 std::__merge_without_buffer(__first, __middle, __last,
3147 * This is a helper function for the stable sorting routines.
3150 template<typename _RandomAccessIterator, typename _Compare>
3152 __inplace_stable_sort(_RandomAccessIterator __first,
3153 _RandomAccessIterator __last, _Compare __comp)
3155 if (__last - __first < 15)
3157 std::__insertion_sort(__first, __last, __comp);
3160 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
3161 std::__inplace_stable_sort(__first, __middle, __comp);
3162 std::__inplace_stable_sort(__middle, __last, __comp);
3163 std::__merge_without_buffer(__first, __middle, __last,
3170 * @brief Merges two sorted ranges.
3171 * @param first1 An iterator.
3172 * @param first2 Another iterator.
3173 * @param last1 Another iterator.
3174 * @param last2 Another iterator.
3175 * @param result An iterator pointing to the end of the merged range.
3176 * @return An iterator pointing to the first element "not less than" @a val.
3178 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
3179 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
3180 * must be sorted, and the output range must not overlap with either of
3181 * the input ranges. The sort is @e stable, that is, for equivalent
3182 * elements in the two ranges, elements from the first range will always
3183 * come before elements from the second.
3185 template<typename _InputIterator1, typename _InputIterator2,
3186 typename _OutputIterator>
3188 merge(_InputIterator1 __first1, _InputIterator1 __last1,
3189 _InputIterator2 __first2, _InputIterator2 __last2,
3190 _OutputIterator __result)
3192 typedef typename iterator_traits<_InputIterator1>::value_type
3194 typedef typename iterator_traits<_InputIterator2>::value_type
3197 // concept requirements
3198 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
3199 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
3200 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3202 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3204 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
3205 __glibcxx_requires_sorted(__first1, __last1);
3206 __glibcxx_requires_sorted(__first2, __last2);
3208 while (__first1 != __last1 && __first2 != __last2)
3210 if (*__first2 < *__first1)
3212 *__result = *__first2;
3217 *__result = *__first1;
3222 return std::copy(__first2, __last2, std::copy(__first1, __last1,
3227 * @brief Merges two sorted ranges.
3228 * @param first1 An iterator.
3229 * @param first2 Another iterator.
3230 * @param last1 Another iterator.
3231 * @param last2 Another iterator.
3232 * @param result An iterator pointing to the end of the merged range.
3233 * @param comp A functor to use for comparisons.
3234 * @return An iterator pointing to the first element "not less than" @a val.
3236 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
3237 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
3238 * must be sorted, and the output range must not overlap with either of
3239 * the input ranges. The sort is @e stable, that is, for equivalent
3240 * elements in the two ranges, elements from the first range will always
3241 * come before elements from the second.
3243 * The comparison function should have the same effects on ordering as
3244 * the function used for the initial sort.
3246 template<typename _InputIterator1, typename _InputIterator2,
3247 typename _OutputIterator, typename _Compare>
3249 merge(_InputIterator1 __first1, _InputIterator1 __last1,
3250 _InputIterator2 __first2, _InputIterator2 __last2,
3251 _OutputIterator __result, _Compare __comp)
3253 typedef typename iterator_traits<_InputIterator1>::value_type
3255 typedef typename iterator_traits<_InputIterator2>::value_type
3258 // concept requirements
3259 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
3260 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
3261 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3263 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3265 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3266 _ValueType2, _ValueType1>)
3267 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
3268 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
3270 while (__first1 != __last1 && __first2 != __last2)
3272 if (__comp(*__first2, *__first1))
3274 *__result = *__first2;
3279 *__result = *__first1;
3284 return std::copy(__first2, __last2, std::copy(__first1, __last1,
3288 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
3291 __merge_sort_loop(_RandomAccessIterator1 __first,
3292 _RandomAccessIterator1 __last,
3293 _RandomAccessIterator2 __result,
3294 _Distance __step_size)
3296 const _Distance __two_step = 2 * __step_size;
3298 while (__last - __first >= __two_step)
3300 __result = std::merge(__first, __first + __step_size,
3301 __first + __step_size, __first + __two_step,
3303 __first += __two_step;
3306 __step_size = std::min(_Distance(__last - __first), __step_size);
3307 std::merge(__first, __first + __step_size, __first + __step_size, __last,
3311 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
3312 typename _Distance, typename _Compare>
3314 __merge_sort_loop(_RandomAccessIterator1 __first,
3315 _RandomAccessIterator1 __last,
3316 _RandomAccessIterator2 __result, _Distance __step_size,
3319 const _Distance __two_step = 2 * __step_size;
3321 while (__last - __first >= __two_step)
3323 __result = std::merge(__first, __first + __step_size,
3324 __first + __step_size, __first + __two_step,
3327 __first += __two_step;
3329 __step_size = std::min(_Distance(__last - __first), __step_size);
3331 std::merge(__first, __first + __step_size,
3332 __first + __step_size, __last,
3337 enum { _S_chunk_size = 7 };
3339 template<typename _RandomAccessIterator, typename _Distance>
3341 __chunk_insertion_sort(_RandomAccessIterator __first,
3342 _RandomAccessIterator __last,
3343 _Distance __chunk_size)
3345 while (__last - __first >= __chunk_size)
3347 std::__insertion_sort(__first, __first + __chunk_size);
3348 __first += __chunk_size;
3350 std::__insertion_sort(__first, __last);
3353 template<typename _RandomAccessIterator, typename _Distance, typename _Compare>
3355 __chunk_insertion_sort(_RandomAccessIterator __first,
3356 _RandomAccessIterator __last,
3357 _Distance __chunk_size, _Compare __comp)
3359 while (__last - __first >= __chunk_size)
3361 std::__insertion_sort(__first, __first + __chunk_size, __comp);
3362 __first += __chunk_size;
3364 std::__insertion_sort(__first, __last, __comp);
3367 template<typename _RandomAccessIterator, typename _Pointer>
3369 __merge_sort_with_buffer(_RandomAccessIterator __first,
3370 _RandomAccessIterator __last,
3373 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3376 const _Distance __len = __last - __first;
3377 const _Pointer __buffer_last = __buffer + __len;
3379 _Distance __step_size = _S_chunk_size;
3380 std::__chunk_insertion_sort(__first, __last, __step_size);
3382 while (__step_size < __len)
3384 std::__merge_sort_loop(__first, __last, __buffer, __step_size);
3386 std::__merge_sort_loop(__buffer, __buffer_last, __first, __step_size);
3391 template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
3393 __merge_sort_with_buffer(_RandomAccessIterator __first,
3394 _RandomAccessIterator __last,
3395 _Pointer __buffer, _Compare __comp)
3397 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3400 const _Distance __len = __last - __first;
3401 const _Pointer __buffer_last = __buffer + __len;
3403 _Distance __step_size = _S_chunk_size;
3404 std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
3406 while (__step_size < __len)
3408 std::__merge_sort_loop(__first, __last, __buffer,
3409 __step_size, __comp);
3411 std::__merge_sort_loop(__buffer, __buffer_last, __first,
3412 __step_size, __comp);
3419 * This is a helper function for the merge routines.
3422 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
3423 typename _BidirectionalIterator3>
3424 _BidirectionalIterator3
3425 __merge_backward(_BidirectionalIterator1 __first1,
3426 _BidirectionalIterator1 __last1,
3427 _BidirectionalIterator2 __first2,
3428 _BidirectionalIterator2 __last2,
3429 _BidirectionalIterator3 __result)
3431 if (__first1 == __last1)
3432 return std::copy_backward(__first2, __last2, __result);
3433 if (__first2 == __last2)
3434 return std::copy_backward(__first1, __last1, __result);
3439 if (*__last2 < *__last1)
3441 *--__result = *__last1;
3442 if (__first1 == __last1)
3443 return std::copy_backward(__first2, ++__last2, __result);
3448 *--__result = *__last2;
3449 if (__first2 == __last2)
3450 return std::copy_backward(__first1, ++__last1, __result);
3458 * This is a helper function for the merge routines.
3461 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
3462 typename _BidirectionalIterator3, typename _Compare>
3463 _BidirectionalIterator3
3464 __merge_backward(_BidirectionalIterator1 __first1,
3465 _BidirectionalIterator1 __last1,
3466 _BidirectionalIterator2 __first2,
3467 _BidirectionalIterator2 __last2,
3468 _BidirectionalIterator3 __result,
3471 if (__first1 == __last1)
3472 return std::copy_backward(__first2, __last2, __result);
3473 if (__first2 == __last2)
3474 return std::copy_backward(__first1, __last1, __result);
3479 if (__comp(*__last2, *__last1))
3481 *--__result = *__last1;
3482 if (__first1 == __last1)
3483 return std::copy_backward(__first2, ++__last2, __result);
3488 *--__result = *__last2;
3489 if (__first2 == __last2)
3490 return std::copy_backward(__first1, ++__last1, __result);
3498 * This is a helper function for the merge routines.
3501 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
3503 _BidirectionalIterator1
3504 __rotate_adaptive(_BidirectionalIterator1 __first,
3505 _BidirectionalIterator1 __middle,
3506 _BidirectionalIterator1 __last,
3507 _Distance __len1, _Distance __len2,
3508 _BidirectionalIterator2 __buffer,
3509 _Distance __buffer_size)
3511 _BidirectionalIterator2 __buffer_end;
3512 if (__len1 > __len2 && __len2 <= __buffer_size)
3514 __buffer_end = std::copy(__middle, __last, __buffer);
3515 std::copy_backward(__first, __middle, __last);
3516 return std::copy(__buffer, __buffer_end, __first);
3518 else if (__len1 <= __buffer_size)
3520 __buffer_end = std::copy(__first, __middle, __buffer);
3521 std::copy(__middle, __last, __first);
3522 return std::copy_backward(__buffer, __buffer_end, __last);
3526 std::rotate(__first, __middle, __last);
3527 std::advance(__first, std::distance(__middle, __last));
3534 * This is a helper function for the merge routines.
3537 template<typename _BidirectionalIterator, typename _Distance,
3540 __merge_adaptive(_BidirectionalIterator __first,
3541 _BidirectionalIterator __middle,
3542 _BidirectionalIterator __last,
3543 _Distance __len1, _Distance __len2,
3544 _Pointer __buffer, _Distance __buffer_size)
3546 if (__len1 <= __len2 && __len1 <= __buffer_size)
3548 _Pointer __buffer_end = std::copy(__first, __middle, __buffer);
3549 std::merge(__buffer, __buffer_end, __middle, __last, __first);
3551 else if (__len2 <= __buffer_size)
3553 _Pointer __buffer_end = std::copy(__middle, __last, __buffer);
3554 std::__merge_backward(__first, __middle, __buffer,
3555 __buffer_end, __last);
3559 _BidirectionalIterator __first_cut = __first;
3560 _BidirectionalIterator __second_cut = __middle;
3561 _Distance __len11 = 0;
3562 _Distance __len22 = 0;
3563 if (__len1 > __len2)
3565 __len11 = __len1 / 2;
3566 std::advance(__first_cut, __len11);
3567 __second_cut = std::lower_bound(__middle, __last,
3569 __len22 = std::distance(__middle, __second_cut);
3573 __len22 = __len2 / 2;
3574 std::advance(__second_cut, __len22);
3575 __first_cut = std::upper_bound(__first, __middle,
3577 __len11 = std::distance(__first, __first_cut);
3579 _BidirectionalIterator __new_middle =
3580 std::__rotate_adaptive(__first_cut, __middle, __second_cut,
3581 __len1 - __len11, __len22, __buffer,
3583 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
3584 __len22, __buffer, __buffer_size);
3585 std::__merge_adaptive(__new_middle, __second_cut, __last,
3587 __len2 - __len22, __buffer, __buffer_size);
3593 * This is a helper function for the merge routines.
3596 template<typename _BidirectionalIterator, typename _Distance, typename _Pointer,
3599 __merge_adaptive(_BidirectionalIterator __first,
3600 _BidirectionalIterator __middle,
3601 _BidirectionalIterator __last,
3602 _Distance __len1, _Distance __len2,
3603 _Pointer __buffer, _Distance __buffer_size,
3606 if (__len1 <= __len2 && __len1 <= __buffer_size)
3608 _Pointer __buffer_end = std::copy(__first, __middle, __buffer);
3609 std::merge(__buffer, __buffer_end, __middle, __last, __first, __comp);
3611 else if (__len2 <= __buffer_size)
3613 _Pointer __buffer_end = std::copy(__middle, __last, __buffer);
3614 std::__merge_backward(__first, __middle, __buffer, __buffer_end,
3619 _BidirectionalIterator __first_cut = __first;
3620 _BidirectionalIterator __second_cut = __middle;
3621 _Distance __len11 = 0;
3622 _Distance __len22 = 0;
3623 if (__len1 > __len2)
3625 __len11 = __len1 / 2;
3626 std::advance(__first_cut, __len11);
3627 __second_cut = std::lower_bound(__middle, __last, *__first_cut,
3629 __len22 = std::distance(__middle, __second_cut);
3633 __len22 = __len2 / 2;
3634 std::advance(__second_cut, __len22);
3635 __first_cut = std::upper_bound(__first, __middle, *__second_cut,
3637 __len11 = std::distance(__first, __first_cut);
3639 _BidirectionalIterator __new_middle =
3640 std::__rotate_adaptive(__first_cut, __middle, __second_cut,
3641 __len1 - __len11, __len22, __buffer,
3643 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
3644 __len22, __buffer, __buffer_size, __comp);
3645 std::__merge_adaptive(__new_middle, __second_cut, __last,
3647 __len2 - __len22, __buffer,
3648 __buffer_size, __comp);
3653 * @brief Merges two sorted ranges in place.
3654 * @param first An iterator.
3655 * @param middle Another iterator.
3656 * @param last Another iterator.
3659 * Merges two sorted and consecutive ranges, [first,middle) and
3660 * [middle,last), and puts the result in [first,last). The output will
3661 * be sorted. The sort is @e stable, that is, for equivalent
3662 * elements in the two ranges, elements from the first range will always
3663 * come before elements from the second.
3665 * If enough additional memory is available, this takes (last-first)-1
3666 * comparisons. Otherwise an NlogN algorithm is used, where N is
3667 * distance(first,last).
3669 template<typename _BidirectionalIterator>
3671 inplace_merge(_BidirectionalIterator __first,
3672 _BidirectionalIterator __middle,
3673 _BidirectionalIterator __last)
3675 typedef typename iterator_traits<_BidirectionalIterator>::value_type
3677 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
3680 // concept requirements
3681 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
3682 _BidirectionalIterator>)
3683 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
3684 __glibcxx_requires_sorted(__first, __middle);
3685 __glibcxx_requires_sorted(__middle, __last);
3687 if (__first == __middle || __middle == __last)
3690 _DistanceType __len1 = std::distance(__first, __middle);
3691 _DistanceType __len2 = std::distance(__middle, __last);
3693 _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
3695 if (__buf.begin() == 0)
3696 std::__merge_without_buffer(__first, __middle, __last, __len1, __len2);
3698 std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
3699 __buf.begin(), _DistanceType(__buf.size()));
3703 * @brief Merges two sorted ranges in place.
3704 * @param first An iterator.
3705 * @param middle Another iterator.
3706 * @param last Another iterator.
3707 * @param comp A functor to use for comparisons.
3710 * Merges two sorted and consecutive ranges, [first,middle) and
3711 * [middle,last), and puts the result in [first,last). The output will
3712 * be sorted. The sort is @e stable, that is, for equivalent
3713 * elements in the two ranges, elements from the first range will always
3714 * come before elements from the second.
3716 * If enough additional memory is available, this takes (last-first)-1
3717 * comparisons. Otherwise an NlogN algorithm is used, where N is
3718 * distance(first,last).
3720 * The comparison function should have the same effects on ordering as
3721 * the function used for the initial sort.
3723 template<typename _BidirectionalIterator, typename _Compare>
3725 inplace_merge(_BidirectionalIterator __first,
3726 _BidirectionalIterator __middle,
3727 _BidirectionalIterator __last,
3730 typedef typename iterator_traits<_BidirectionalIterator>::value_type
3732 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
3735 // concept requirements
3736 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
3737 _BidirectionalIterator>)
3738 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3739 _ValueType, _ValueType>)
3740 __glibcxx_requires_sorted_pred(__first, __middle, __comp);
3741 __glibcxx_requires_sorted_pred(__middle, __last, __comp);
3743 if (__first == __middle || __middle == __last)
3746 const _DistanceType __len1 = std::distance(__first, __middle);
3747 const _DistanceType __len2 = std::distance(__middle, __last);
3749 _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
3751 if (__buf.begin() == 0)
3752 std::__merge_without_buffer(__first, __middle, __last, __len1,
3755 std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
3756 __buf.begin(), _DistanceType(__buf.size()),
3760 template<typename _RandomAccessIterator, typename _Pointer,
3763 __stable_sort_adaptive(_RandomAccessIterator __first,
3764 _RandomAccessIterator __last,
3765 _Pointer __buffer, _Distance __buffer_size)
3767 const _Distance __len = (__last - __first + 1) / 2;
3768 const _RandomAccessIterator __middle = __first + __len;
3769 if (__len > __buffer_size)
3771 std::__stable_sort_adaptive(__first, __middle,
3772 __buffer, __buffer_size);
3773 std::__stable_sort_adaptive(__middle, __last,
3774 __buffer, __buffer_size);
3778 std::__merge_sort_with_buffer(__first, __middle, __buffer);
3779 std::__merge_sort_with_buffer(__middle, __last, __buffer);
3781 std::__merge_adaptive(__first, __middle, __last,
3782 _Distance(__middle - __first),
3783 _Distance(__last - __middle),
3784 __buffer, __buffer_size);
3787 template<typename _RandomAccessIterator, typename _Pointer,
3788 typename _Distance, typename _Compare>
3790 __stable_sort_adaptive(_RandomAccessIterator __first,
3791 _RandomAccessIterator __last,
3792 _Pointer __buffer, _Distance __buffer_size,
3795 const _Distance __len = (__last - __first + 1) / 2;
3796 const _RandomAccessIterator __middle = __first + __len;
3797 if (__len > __buffer_size)
3799 std::__stable_sort_adaptive(__first, __middle, __buffer,
3800 __buffer_size, __comp);
3801 std::__stable_sort_adaptive(__middle, __last, __buffer,
3802 __buffer_size, __comp);
3806 std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
3807 std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
3809 std::__merge_adaptive(__first, __middle, __last,
3810 _Distance(__middle - __first),
3811 _Distance(__last - __middle),
3812 __buffer, __buffer_size,
3817 * @brief Sort the elements of a sequence, preserving the relative order
3818 * of equivalent elements.
3819 * @param first An iterator.
3820 * @param last Another iterator.
3823 * Sorts the elements in the range @p [first,last) in ascending order,
3824 * such that @p *(i+1)<*i is false for each iterator @p i in the range
3825 * @p [first,last-1).
3827 * The relative ordering of equivalent elements is preserved, so any two
3828 * elements @p x and @p y in the range @p [first,last) such that
3829 * @p x<y is false and @p y<x is false will have the same relative
3830 * ordering after calling @p stable_sort().
3832 template<typename _RandomAccessIterator>
3834 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
3836 typedef typename iterator_traits<_RandomAccessIterator>::value_type
3838 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3841 // concept requirements
3842 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
3843 _RandomAccessIterator>)
3844 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
3845 __glibcxx_requires_valid_range(__first, __last);
3847 _Temporary_buffer<_RandomAccessIterator, _ValueType>
3848 buf(__first, __last);
3849 if (buf.begin() == 0)
3850 std::__inplace_stable_sort(__first, __last);
3852 std::__stable_sort_adaptive(__first, __last, buf.begin(),
3853 _DistanceType(buf.size()));
3857 * @brief Sort the elements of a sequence using a predicate for comparison,
3858 * preserving the relative order of equivalent elements.
3859 * @param first An iterator.
3860 * @param last Another iterator.
3861 * @param comp A comparison functor.
3864 * Sorts the elements in the range @p [first,last) in ascending order,
3865 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
3866 * range @p [first,last-1).
3868 * The relative ordering of equivalent elements is preserved, so any two
3869 * elements @p x and @p y in the range @p [first,last) such that
3870 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
3871 * relative ordering after calling @p stable_sort().
3873 template<typename _RandomAccessIterator, typename _Compare>
3875 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
3878 typedef typename iterator_traits<_RandomAccessIterator>::value_type
3880 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3883 // concept requirements
3884 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
3885 _RandomAccessIterator>)
3886 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3889 __glibcxx_requires_valid_range(__first, __last);
3891 _Temporary_buffer<_RandomAccessIterator, _ValueType> buf(__first, __last);
3892 if (buf.begin() == 0)
3893 std::__inplace_stable_sort(__first, __last, __comp);
3895 std::__stable_sort_adaptive(__first, __last, buf.begin(),
3896 _DistanceType(buf.size()), __comp);
3900 * @brief Sort a sequence just enough to find a particular position.
3901 * @param first An iterator.
3902 * @param nth Another iterator.
3903 * @param last Another iterator.
3906 * Rearranges the elements in the range @p [first,last) so that @p *nth
3907 * is the same element that would have been in that position had the
3908 * whole sequence been sorted.
3909 * whole sequence been sorted. The elements either side of @p *nth are
3910 * not completely sorted, but for any iterator @i in the range
3911 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3912 * holds that @p *j<*i is false.
3914 template<typename _RandomAccessIterator>
3916 nth_element(_RandomAccessIterator __first,
3917 _RandomAccessIterator __nth,
3918 _RandomAccessIterator __last)
3920 typedef typename iterator_traits<_RandomAccessIterator>::value_type
3923 // concept requirements
3924 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
3925 _RandomAccessIterator>)
3926 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
3927 __glibcxx_requires_valid_range(__first, __nth);
3928 __glibcxx_requires_valid_range(__nth, __last);
3930 while (__last - __first > 3)
3932 _RandomAccessIterator __cut =
3933 std::__unguarded_partition(__first, __last,
3934 _ValueType(std::__median(*__first,
3946 std::__insertion_sort(__first, __last);
3950 * @brief Sort a sequence just enough to find a particular position
3951 * using a predicate for comparison.
3952 * @param first An iterator.
3953 * @param nth Another iterator.
3954 * @param last Another iterator.
3955 * @param comp A comparison functor.
3958 * Rearranges the elements in the range @p [first,last) so that @p *nth
3959 * is the same element that would have been in that position had the
3960 * whole sequence been sorted. The elements either side of @p *nth are
3961 * not completely sorted, but for any iterator @i in the range
3962 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3963 * holds that @p comp(*j,*i) is false.
3965 template<typename _RandomAccessIterator, typename _Compare>
3967 nth_element(_RandomAccessIterator __first,
3968 _RandomAccessIterator __nth,
3969 _RandomAccessIterator __last,
3972 typedef typename iterator_traits<_RandomAccessIterator>::value_type
3975 // concept requirements
3976 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
3977 _RandomAccessIterator>)
3978 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3979 _ValueType, _ValueType>)
3980 __glibcxx_requires_valid_range(__first, __nth);
3981 __glibcxx_requires_valid_range(__nth, __last);
3983 while (__last - __first > 3)
3985 _RandomAccessIterator __cut =
3986 std::__unguarded_partition(__first, __last,
3987 _ValueType(std::__median(*__first,
3999 std::__insertion_sort(__first, __last, __comp);
4003 * @brief Finds the largest subrange in which @a val could be inserted
4004 * at any place in it without changing the ordering.
4005 * @param first An iterator.
4006 * @param last Another iterator.
4007 * @param val The search term.
4008 * @return An pair of iterators defining the subrange.
4009 * @ingroup binarysearch
4011 * This is equivalent to
4013 * std::make_pair(lower_bound(first, last, val),
4014 * upper_bound(first, last, val))
4016 * but does not actually call those functions.
4018 template<typename _ForwardIterator, typename _Tp>
4019 pair<_ForwardIterator, _ForwardIterator>
4020 equal_range(_ForwardIterator __first, _ForwardIterator __last,
4023 typedef typename iterator_traits<_ForwardIterator>::value_type
4025 typedef typename iterator_traits<_ForwardIterator>::difference_type
4028 // concept requirements
4029 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4030 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
4031 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
4032 __glibcxx_requires_partitioned(__first, __last, __val);
4034 _DistanceType __len = std::distance(__first, __last);
4035 _DistanceType __half;
4036 _ForwardIterator __middle, __left, __right;
4040 __half = __len >> 1;
4042 std::advance(__middle, __half);
4043 if (*__middle < __val)
4047 __len = __len - __half - 1;
4049 else if (__val < *__middle)
4053 __left = std::lower_bound(__first, __middle, __val);
4054 std::advance(__first, __len);
4055 __right = std::upper_bound(++__middle, __first, __val);
4056 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
4059 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4063 * @brief Finds the largest subrange in which @a val could be inserted
4064 * at any place in it without changing the ordering.
4065 * @param first An iterator.
4066 * @param last Another iterator.
4067 * @param val The search term.
4068 * @param comp A functor to use for comparisons.
4069 * @return An pair of iterators defining the subrange.
4070 * @ingroup binarysearch
4072 * This is equivalent to
4074 * std::make_pair(lower_bound(first, last, val, comp),
4075 * upper_bound(first, last, val, comp))
4077 * but does not actually call those functions.
4079 template<typename _ForwardIterator, typename _Tp, typename _Compare>
4080 pair<_ForwardIterator, _ForwardIterator>
4081 equal_range(_ForwardIterator __first, _ForwardIterator __last,
4085 typedef typename iterator_traits<_ForwardIterator>::value_type
4087 typedef typename iterator_traits<_ForwardIterator>::difference_type
4090 // concept requirements
4091 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4092 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4094 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4096 __glibcxx_requires_partitioned_pred(__first, __last, __val, __comp);
4098 _DistanceType __len = std::distance(__first, __last);
4099 _DistanceType __half;
4100 _ForwardIterator __middle, __left, __right;
4104 __half = __len >> 1;
4106 std::advance(__middle, __half);
4107 if (__comp(*__middle, __val))
4111 __len = __len - __half - 1;
4113 else if (__comp(__val, *__middle))
4117 __left = std::lower_bound(__first, __middle, __val, __comp);
4118 std::advance(__first, __len);
4119 __right = std::upper_bound(++__middle, __first, __val, __comp);
4120 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
4123 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4127 * @brief Determines whether an element exists in a range.
4128 * @param first An iterator.
4129 * @param last Another iterator.
4130 * @param val The search term.
4131 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
4132 * @ingroup binarysearch
4134 * Note that this does not actually return an iterator to @a val. For
4135 * that, use std::find or a container's specialized find member functions.
4137 template<typename _ForwardIterator, typename _Tp>
4139 binary_search(_ForwardIterator __first, _ForwardIterator __last,
4142 typedef typename iterator_traits<_ForwardIterator>::value_type
4145 // concept requirements
4146 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4147 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
4148 __glibcxx_requires_partitioned(__first, __last, __val);
4150 _ForwardIterator __i = std::lower_bound(__first, __last, __val);
4151 return __i != __last && !(__val < *__i);
4155 * @brief Determines whether an element exists in a range.
4156 * @param first An iterator.
4157 * @param last Another iterator.
4158 * @param val The search term.
4159 * @param comp A functor to use for comparisons.
4160 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
4161 * @ingroup binarysearch
4163 * Note that this does not actually return an iterator to @a val. For
4164 * that, use std::find or a container's specialized find member functions.
4166 * The comparison function should have the same effects on ordering as
4167 * the function used for the initial sort.
4169 template<typename _ForwardIterator, typename _Tp, typename _Compare>
4171 binary_search(_ForwardIterator __first, _ForwardIterator __last,
4172 const _Tp& __val, _Compare __comp)
4174 typedef typename iterator_traits<_ForwardIterator>::value_type
4177 // concept requirements
4178 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4179 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4181 __glibcxx_requires_partitioned_pred(__first, __last, __val, __comp);
4183 _ForwardIterator __i = std::lower_bound(__first, __last, __val, __comp);
4184 return __i != __last && !__comp(__val, *__i);
4187 // Set algorithms: includes, set_union, set_intersection, set_difference,
4188 // set_symmetric_difference. All of these algorithms have the precondition
4189 // that their input ranges are sorted and the postcondition that their output
4190 // ranges are sorted.
4193 * @brief Determines whether all elements of a sequence exists in a range.
4194 * @param first1 Start of search range.
4195 * @param last1 End of search range.
4196 * @param first2 Start of sequence
4197 * @param last2 End of sequence.
4198 * @return True if each element in [first2,last2) is contained in order
4199 * within [first1,last1). False otherwise.
4200 * @ingroup setoperations
4202 * This operation expects both [first1,last1) and [first2,last2) to be
4203 * sorted. Searches for the presence of each element in [first2,last2)
4204 * within [first1,last1). The iterators over each range only move forward,
4205 * so this is a linear algorithm. If an element in [first2,last2) is not
4206 * found before the search iterator reaches @a last2, false is returned.
4208 template<typename _InputIterator1, typename _InputIterator2>
4210 includes(_InputIterator1 __first1, _InputIterator1 __last1,
4211 _InputIterator2 __first2, _InputIterator2 __last2)
4213 typedef typename iterator_traits<_InputIterator1>::value_type
4215 typedef typename iterator_traits<_InputIterator2>::value_type
4218 // concept requirements
4219 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4220 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4221 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
4222 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
4223 __glibcxx_requires_sorted(__first1, __last1);
4224 __glibcxx_requires_sorted(__first2, __last2);
4226 while (__first1 != __last1 && __first2 != __last2)
4227 if (*__first2 < *__first1)
4229 else if(*__first1 < *__first2)
4232 ++__first1, ++__first2;
4234 return __first2 == __last2;
4238 * @brief Determines whether all elements of a sequence exists in a range
4240 * @param first1 Start of search range.
4241 * @param last1 End of search range.
4242 * @param first2 Start of sequence
4243 * @param last2 End of sequence.
4244 * @param comp Comparison function to use.
4245 * @return True if each element in [first2,last2) is contained in order
4246 * within [first1,last1) according to comp. False otherwise.
4247 * @ingroup setoperations
4249 * This operation expects both [first1,last1) and [first2,last2) to be
4250 * sorted. Searches for the presence of each element in [first2,last2)
4251 * within [first1,last1), using comp to decide. The iterators over each
4252 * range only move forward, so this is a linear algorithm. If an element
4253 * in [first2,last2) is not found before the search iterator reaches @a
4254 * last2, false is returned.
4256 template<typename _InputIterator1, typename _InputIterator2,
4259 includes(_InputIterator1 __first1, _InputIterator1 __last1,
4260 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
4262 typedef typename iterator_traits<_InputIterator1>::value_type
4264 typedef typename iterator_traits<_InputIterator2>::value_type
4267 // concept requirements
4268 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4269 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4270 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4271 _ValueType1, _ValueType2>)
4272 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4273 _ValueType2, _ValueType1>)
4274 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
4275 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
4277 while (__first1 != __last1 && __first2 != __last2)
4278 if (__comp(*__first2, *__first1))
4280 else if(__comp(*__first1, *__first2))
4283 ++__first1, ++__first2;
4285 return __first2 == __last2;
4289 * @brief Return the union of two sorted ranges.
4290 * @param first1 Start of first range.
4291 * @param last1 End of first range.
4292 * @param first2 Start of second range.
4293 * @param last2 End of second range.
4294 * @return End of the output range.
4295 * @ingroup setoperations
4297 * This operation iterates over both ranges, copying elements present in
4298 * each range in order to the output range. Iterators increment for each
4299 * range. When the current element of one range is less than the other,
4300 * that element is copied and the iterator advanced. If an element is
4301 * contained in both ranges, the element from the first range is copied and
4302 * both ranges advance. The output range may not overlap either input
4305 template<typename _InputIterator1, typename _InputIterator2,
4306 typename _OutputIterator>
4308 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
4309 _InputIterator2 __first2, _InputIterator2 __last2,
4310 _OutputIterator __result)
4312 typedef typename iterator_traits<_InputIterator1>::value_type
4314 typedef typename iterator_traits<_InputIterator2>::value_type
4317 // concept requirements
4318 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4319 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4320 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4322 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4324 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
4325 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
4326 __glibcxx_requires_sorted(__first1, __last1);
4327 __glibcxx_requires_sorted(__first2, __last2);
4329 while (__first1 != __last1 && __first2 != __last2)
4331 if (*__first1 < *__first2)
4333 *__result = *__first1;
4336 else if (*__first2 < *__first1)
4338 *__result = *__first2;
4343 *__result = *__first1;
4349 return std::copy(__first2, __last2, std::copy(__first1, __last1,
4354 * @brief Return the union of two sorted ranges using a comparison functor.
4355 * @param first1 Start of first range.
4356 * @param last1 End of first range.
4357 * @param first2 Start of second range.
4358 * @param last2 End of second range.
4359 * @param comp The comparison functor.
4360 * @return End of the output range.
4361 * @ingroup setoperations
4363 * This operation iterates over both ranges, copying elements present in
4364 * each range in order to the output range. Iterators increment for each
4365 * range. When the current element of one range is less than the other
4366 * according to @a comp, that element is copied and the iterator advanced.
4367 * If an equivalent element according to @a comp is contained in both
4368 * ranges, the element from the first range is copied and both ranges
4369 * advance. The output range may not overlap either input range.
4371 template<typename _InputIterator1, typename _InputIterator2,
4372 typename _OutputIterator, typename _Compare>
4374 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
4375 _InputIterator2 __first2, _InputIterator2 __last2,
4376 _OutputIterator __result, _Compare __comp)
4378 typedef typename iterator_traits<_InputIterator1>::value_type
4380 typedef typename iterator_traits<_InputIterator2>::value_type
4383 // concept requirements
4384 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4385 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4386 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4388 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4390 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4391 _ValueType1, _ValueType2>)
4392 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4393 _ValueType2, _ValueType1>)
4394 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
4395 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
4397 while (__first1 != __last1 && __first2 != __last2)
4399 if (__comp(*__first1, *__first2))
4401 *__result = *__first1;
4404 else if (__comp(*__first2, *__first1))
4406 *__result = *__first2;
4411 *__result = *__first1;
4417 return std::copy(__first2, __last2, std::copy(__first1, __last1,
4422 * @brief Return the intersection of two sorted ranges.
4423 * @param first1 Start of first range.
4424 * @param last1 End of first range.
4425 * @param first2 Start of second range.
4426 * @param last2 End of second range.
4427 * @return End of the output range.
4428 * @ingroup setoperations
4430 * This operation iterates over both ranges, copying elements present in
4431 * both ranges in order to the output range. Iterators increment for each
4432 * range. When the current element of one range is less than the other,
4433 * that iterator advances. If an element is contained in both ranges, the
4434 * element from the first range is copied and both ranges advance. The
4435 * output range may not overlap either input range.
4437 template<typename _InputIterator1, typename _InputIterator2,
4438 typename _OutputIterator>
4440 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
4441 _InputIterator2 __first2, _InputIterator2 __last2,
4442 _OutputIterator __result)
4444 typedef typename iterator_traits<_InputIterator1>::value_type
4446 typedef typename iterator_traits<_InputIterator2>::value_type
4449 // concept requirements
4450 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4451 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4452 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4454 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
4455 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
4456 __glibcxx_requires_sorted(__first1, __last1);
4457 __glibcxx_requires_sorted(__first2, __last2);
4459 while (__first1 != __last1 && __first2 != __last2)
4460 if (*__first1 < *__first2)
4462 else if (*__first2 < *__first1)
4466 *__result = *__first1;
4475 * @brief Return the intersection of two sorted ranges using comparison
4477 * @param first1 Start of first range.
4478 * @param last1 End of first range.
4479 * @param first2 Start of second range.
4480 * @param last2 End of second range.
4481 * @param comp The comparison functor.
4482 * @return End of the output range.
4483 * @ingroup setoperations
4485 * This operation iterates over both ranges, copying elements present in
4486 * both ranges in order to the output range. Iterators increment for each
4487 * range. When the current element of one range is less than the other
4488 * according to @a comp, that iterator advances. If an element is
4489 * contained in both ranges according to @a comp, the element from the
4490 * first range is copied and both ranges advance. The output range may not
4491 * overlap either input range.
4493 template<typename _InputIterator1, typename _InputIterator2,
4494 typename _OutputIterator, typename _Compare>
4496 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
4497 _InputIterator2 __first2, _InputIterator2 __last2,
4498 _OutputIterator __result, _Compare __comp)
4500 typedef typename iterator_traits<_InputIterator1>::value_type
4502 typedef typename iterator_traits<_InputIterator2>::value_type
4505 // concept requirements
4506 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4507 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4508 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4510 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4511 _ValueType1, _ValueType2>)
4512 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4513 _ValueType2, _ValueType1>)
4514 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
4515 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
4517 while (__first1 != __last1 && __first2 != __last2)
4518 if (__comp(*__first1, *__first2))
4520 else if (__comp(*__first2, *__first1))
4524 *__result = *__first1;
4533 * @brief Return the difference of two sorted ranges.
4534 * @param first1 Start of first range.
4535 * @param last1 End of first range.
4536 * @param first2 Start of second range.
4537 * @param last2 End of second range.
4538 * @return End of the output range.
4539 * @ingroup setoperations
4541 * This operation iterates over both ranges, copying elements present in
4542 * the first range but not the second in order to the output range.
4543 * Iterators increment for each range. When the current element of the
4544 * first range is less than the second, that element is copied and the
4545 * iterator advances. If the current element of the second range is less,
4546 * the iterator advances, but no element is copied. If an element is
4547 * contained in both ranges, no elements are copied and both ranges
4548 * advance. The output range may not overlap either input range.
4550 template<typename _InputIterator1, typename _InputIterator2,
4551 typename _OutputIterator>
4553 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
4554 _InputIterator2 __first2, _InputIterator2 __last2,
4555 _OutputIterator __result)
4557 typedef typename iterator_traits<_InputIterator1>::value_type
4559 typedef typename iterator_traits<_InputIterator2>::value_type
4562 // concept requirements
4563 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4564 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4565 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4567 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
4568 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
4569 __glibcxx_requires_sorted(__first1, __last1);
4570 __glibcxx_requires_sorted(__first2, __last2);
4572 while (__first1 != __last1 && __first2 != __last2)
4573 if (*__first1 < *__first2)
4575 *__result = *__first1;
4579 else if (*__first2 < *__first1)
4586 return std::copy(__first1, __last1, __result);
4590 * @brief Return the difference of two sorted ranges using comparison
4592 * @param first1 Start of first range.
4593 * @param last1 End of first range.
4594 * @param first2 Start of second range.
4595 * @param last2 End of second range.
4596 * @param comp The comparison functor.
4597 * @return End of the output range.
4598 * @ingroup setoperations
4600 * This operation iterates over both ranges, copying elements present in
4601 * the first range but not the second in order to the output range.
4602 * Iterators increment for each range. When the current element of the
4603 * first range is less than the second according to @a comp, that element
4604 * is copied and the iterator advances. If the current element of the
4605 * second range is less, no element is copied and the iterator advances.
4606 * If an element is contained in both ranges according to @a comp, no
4607 * elements are copied and both ranges advance. The output range may not
4608 * overlap either input range.
4610 template<typename _InputIterator1, typename _InputIterator2,
4611 typename _OutputIterator, typename _Compare>
4613 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
4614 _InputIterator2 __first2, _InputIterator2 __last2,
4615 _OutputIterator __result, _Compare __comp)
4617 typedef typename iterator_traits<_InputIterator1>::value_type
4619 typedef typename iterator_traits<_InputIterator2>::value_type
4622 // concept requirements
4623 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4624 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4625 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4627 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4628 _ValueType1, _ValueType2>)
4629 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4630 _ValueType2, _ValueType1>)
4631 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
4632 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
4634 while (__first1 != __last1 && __first2 != __last2)
4635 if (__comp(*__first1, *__first2))
4637 *__result = *__first1;
4641 else if (__comp(*__first2, *__first1))
4648 return std::copy(__first1, __last1, __result);
4652 * @brief Return the symmetric difference of two sorted ranges.
4653 * @param first1 Start of first range.
4654 * @param last1 End of first range.
4655 * @param first2 Start of second range.
4656 * @param last2 End of second range.
4657 * @return End of the output range.
4658 * @ingroup setoperations
4660 * This operation iterates over both ranges, copying elements present in
4661 * one range but not the other in order to the output range. Iterators
4662 * increment for each range. When the current element of one range is less
4663 * than the other, that element is copied and the iterator advances. If an
4664 * element is contained in both ranges, no elements are copied and both
4665 * ranges advance. The output range may not overlap either input range.
4667 template<typename _InputIterator1, typename _InputIterator2,
4668 typename _OutputIterator>
4670 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
4671 _InputIterator2 __first2, _InputIterator2 __last2,
4672 _OutputIterator __result)
4674 typedef typename iterator_traits<_InputIterator1>::value_type
4676 typedef typename iterator_traits<_InputIterator2>::value_type
4679 // concept requirements
4680 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4681 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4682 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4684 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4686 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
4687 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
4688 __glibcxx_requires_sorted(__first1, __last1);
4689 __glibcxx_requires_sorted(__first2, __last2);
4691 while (__first1 != __last1 && __first2 != __last2)
4692 if (*__first1 < *__first2)
4694 *__result = *__first1;
4698 else if (*__first2 < *__first1)
4700 *__result = *__first2;
4709 return std::copy(__first2, __last2, std::copy(__first1,
4710 __last1, __result));
4714 * @brief Return the symmetric difference of two sorted ranges using
4715 * comparison functor.
4716 * @param first1 Start of first range.
4717 * @param last1 End of first range.
4718 * @param first2 Start of second range.
4719 * @param last2 End of second range.
4720 * @param comp The comparison functor.
4721 * @return End of the output range.
4722 * @ingroup setoperations
4724 * This operation iterates over both ranges, copying elements present in
4725 * one range but not the other in order to the output range. Iterators
4726 * increment for each range. When the current element of one range is less
4727 * than the other according to @a comp, that element is copied and the
4728 * iterator advances. If an element is contained in both ranges according
4729 * to @a comp, no elements are copied and both ranges advance. The output
4730 * range may not overlap either input range.
4732 template<typename _InputIterator1, typename _InputIterator2,
4733 typename _OutputIterator, typename _Compare>
4735 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
4736 _InputIterator2 __first2, _InputIterator2 __last2,
4737 _OutputIterator __result,
4740 typedef typename iterator_traits<_InputIterator1>::value_type
4742 typedef typename iterator_traits<_InputIterator2>::value_type
4745 // concept requirements
4746 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4747 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4748 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4750 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4752 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4753 _ValueType1, _ValueType2>)
4754 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4755 _ValueType2, _ValueType1>)
4756 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
4757 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
4759 while (__first1 != __last1 && __first2 != __last2)
4760 if (__comp(*__first1, *__first2))
4762 *__result = *__first1;
4766 else if (__comp(*__first2, *__first1))
4768 *__result = *__first2;
4777 return std::copy(__first2, __last2, std::copy(__first1,
4778 __last1, __result));
4781 // min_element and max_element, with and without an explicitly supplied
4782 // comparison function.
4785 * @brief Return the maximum element in a range.
4786 * @param first Start of range.
4787 * @param last End of range.
4788 * @return Iterator referencing the first instance of the largest value.
4790 template<typename _ForwardIterator>
4792 max_element(_ForwardIterator __first, _ForwardIterator __last)
4794 // concept requirements
4795 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4796 __glibcxx_function_requires(_LessThanComparableConcept<
4797 typename iterator_traits<_ForwardIterator>::value_type>)
4798 __glibcxx_requires_valid_range(__first, __last);
4800 if (__first == __last)
4802 _ForwardIterator __result = __first;
4803 while (++__first != __last)
4804 if (*__result < *__first)
4810 * @brief Return the maximum element in a range using comparison functor.
4811 * @param first Start of range.
4812 * @param last End of range.
4813 * @param comp Comparison functor.
4814 * @return Iterator referencing the first instance of the largest value
4815 * according to comp.
4817 template<typename _ForwardIterator, typename _Compare>
4819 max_element(_ForwardIterator __first, _ForwardIterator __last,
4822 // concept requirements
4823 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4824 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4825 typename iterator_traits<_ForwardIterator>::value_type,
4826 typename iterator_traits<_ForwardIterator>::value_type>)
4827 __glibcxx_requires_valid_range(__first, __last);
4829 if (__first == __last) return __first;
4830 _ForwardIterator __result = __first;
4831 while (++__first != __last)
4832 if (__comp(*__result, *__first)) __result = __first;
4837 * @brief Return the minimum element in a range.
4838 * @param first Start of range.
4839 * @param last End of range.
4840 * @return Iterator referencing the first instance of the smallest value.
4842 template<typename _ForwardIterator>
4844 min_element(_ForwardIterator __first, _ForwardIterator __last)
4846 // concept requirements
4847 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4848 __glibcxx_function_requires(_LessThanComparableConcept<
4849 typename iterator_traits<_ForwardIterator>::value_type>)
4850 __glibcxx_requires_valid_range(__first, __last);
4852 if (__first == __last)
4854 _ForwardIterator __result = __first;
4855 while (++__first != __last)
4856 if (*__first < *__result)
4862 * @brief Return the minimum element in a range using comparison functor.
4863 * @param first Start of range.
4864 * @param last End of range.
4865 * @param comp Comparison functor.
4866 * @return Iterator referencing the first instance of the smallest value
4867 * according to comp.
4869 template<typename _ForwardIterator, typename _Compare>
4871 min_element(_ForwardIterator __first, _ForwardIterator __last,
4874 // concept requirements
4875 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4876 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4877 typename iterator_traits<_ForwardIterator>::value_type,
4878 typename iterator_traits<_ForwardIterator>::value_type>)
4879 __glibcxx_requires_valid_range(__first, __last);
4881 if (__first == __last)
4883 _ForwardIterator __result = __first;
4884 while (++__first != __last)
4885 if (__comp(*__first, *__result))
4890 // next_permutation and prev_permutation, with and without an explicitly
4891 // supplied comparison function.
4894 * @brief Permute range into the next "dictionary" ordering.
4895 * @param first Start of range.
4896 * @param last End of range.
4897 * @return False if wrapped to first permutation, true otherwise.
4899 * Treats all permutations of the range as a set of "dictionary" sorted
4900 * sequences. Permutes the current sequence into the next one of this set.
4901 * Returns true if there are more sequences to generate. If the sequence
4902 * is the largest of the set, the smallest is generated and false returned.
4904 template<typename _BidirectionalIterator>
4906 next_permutation(_BidirectionalIterator __first,
4907 _BidirectionalIterator __last)
4909 // concept requirements
4910 __glibcxx_function_requires(_BidirectionalIteratorConcept<
4911 _BidirectionalIterator>)
4912 __glibcxx_function_requires(_LessThanComparableConcept<
4913 typename iterator_traits<_BidirectionalIterator>::value_type>)
4914 __glibcxx_requires_valid_range(__first, __last);
4916 if (__first == __last)
4918 _BidirectionalIterator __i = __first;
4927 _BidirectionalIterator __ii = __i;
4931 _BidirectionalIterator __j = __last;
4932 while (!(*__i < *--__j))
4934 std::iter_swap(__i, __j);
4935 std::reverse(__ii, __last);
4940 std::reverse(__first, __last);
4947 * @brief Permute range into the next "dictionary" ordering using
4948 * comparison functor.
4949 * @param first Start of range.
4950 * @param last End of range.
4952 * @return False if wrapped to first permutation, true otherwise.
4954 * Treats all permutations of the range [first,last) as a set of
4955 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
4956 * sequence into the next one of this set. Returns true if there are more
4957 * sequences to generate. If the sequence is the largest of the set, the
4958 * smallest is generated and false returned.
4960 template<typename _BidirectionalIterator, typename _Compare>
4962 next_permutation(_BidirectionalIterator __first,
4963 _BidirectionalIterator __last, _Compare __comp)
4965 // concept requirements
4966 __glibcxx_function_requires(_BidirectionalIteratorConcept<
4967 _BidirectionalIterator>)
4968 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4969 typename iterator_traits<_BidirectionalIterator>::value_type,
4970 typename iterator_traits<_BidirectionalIterator>::value_type>)
4971 __glibcxx_requires_valid_range(__first, __last);
4973 if (__first == __last)
4975 _BidirectionalIterator __i = __first;
4984 _BidirectionalIterator __ii = __i;
4986 if (__comp(*__i, *__ii))
4988 _BidirectionalIterator __j = __last;
4989 while (!__comp(*__i, *--__j))
4991 std::iter_swap(__i, __j);
4992 std::reverse(__ii, __last);
4997 std::reverse(__first, __last);
5004 * @brief Permute range into the previous "dictionary" ordering.
5005 * @param first Start of range.
5006 * @param last End of range.
5007 * @return False if wrapped to last permutation, true otherwise.
5009 * Treats all permutations of the range as a set of "dictionary" sorted
5010 * sequences. Permutes the current sequence into the previous one of this
5011 * set. Returns true if there are more sequences to generate. If the
5012 * sequence is the smallest of the set, the largest is generated and false
5015 template<typename _BidirectionalIterator>
5017 prev_permutation(_BidirectionalIterator __first,
5018 _BidirectionalIterator __last)
5020 // concept requirements
5021 __glibcxx_function_requires(_BidirectionalIteratorConcept<
5022 _BidirectionalIterator>)
5023 __glibcxx_function_requires(_LessThanComparableConcept<
5024 typename iterator_traits<_BidirectionalIterator>::value_type>)
5025 __glibcxx_requires_valid_range(__first, __last);
5027 if (__first == __last)
5029 _BidirectionalIterator __i = __first;
5038 _BidirectionalIterator __ii = __i;
5042 _BidirectionalIterator __j = __last;
5043 while (!(*--__j < *__i))
5045 std::iter_swap(__i, __j);
5046 std::reverse(__ii, __last);
5051 std::reverse(__first, __last);
5058 * @brief Permute range into the previous "dictionary" ordering using
5059 * comparison functor.
5060 * @param first Start of range.
5061 * @param last End of range.
5063 * @return False if wrapped to last permutation, true otherwise.
5065 * Treats all permutations of the range [first,last) as a set of
5066 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
5067 * sequence into the previous one of this set. Returns true if there are
5068 * more sequences to generate. If the sequence is the smallest of the set,
5069 * the largest is generated and false returned.
5071 template<typename _BidirectionalIterator, typename _Compare>
5073 prev_permutation(_BidirectionalIterator __first,
5074 _BidirectionalIterator __last, _Compare __comp)
5076 // concept requirements
5077 __glibcxx_function_requires(_BidirectionalIteratorConcept<
5078 _BidirectionalIterator>)
5079 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5080 typename iterator_traits<_BidirectionalIterator>::value_type,
5081 typename iterator_traits<_BidirectionalIterator>::value_type>)
5082 __glibcxx_requires_valid_range(__first, __last);
5084 if (__first == __last)
5086 _BidirectionalIterator __i = __first;
5095 _BidirectionalIterator __ii = __i;
5097 if (__comp(*__ii, *__i))
5099 _BidirectionalIterator __j = __last;
5100 while (!__comp(*--__j, *__i))
5102 std::iter_swap(__i, __j);
5103 std::reverse(__ii, __last);
5108 std::reverse(__first, __last);
5114 // find_first_of, with and without an explicitly supplied comparison function.
5117 * @brief Find element from a set in a sequence.
5118 * @param first1 Start of range to search.
5119 * @param last1 End of range to search.
5120 * @param first2 Start of match candidates.
5121 * @param last2 End of match candidates.
5122 * @return The first iterator @c i in the range
5123 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
5124 * interator in [first2,last2), or @p last1 if no such iterator exists.
5126 * Searches the range @p [first1,last1) for an element that is equal to
5127 * some element in the range [first2,last2). If found, returns an iterator
5128 * in the range [first1,last1), otherwise returns @p last1.
5130 template<typename _InputIterator, typename _ForwardIterator>
5132 find_first_of(_InputIterator __first1, _InputIterator __last1,
5133 _ForwardIterator __first2, _ForwardIterator __last2)
5135 // concept requirements
5136 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
5137 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5138 __glibcxx_function_requires(_EqualOpConcept<
5139 typename iterator_traits<_InputIterator>::value_type,
5140 typename iterator_traits<_ForwardIterator>::value_type>)
5141 __glibcxx_requires_valid_range(__first1, __last1);
5142 __glibcxx_requires_valid_range(__first2, __last2);
5144 for ( ; __first1 != __last1; ++__first1)
5145 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
5146 if (*__first1 == *__iter)
5152 * @brief Find element from a set in a sequence using a predicate.
5153 * @param first1 Start of range to search.
5154 * @param last1 End of range to search.
5155 * @param first2 Start of match candidates.
5156 * @param last2 End of match candidates.
5157 * @param comp Predicate to use.
5158 * @return The first iterator @c i in the range
5159 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
5160 * interator in [first2,last2), or @p last1 if no such iterator exists.
5162 * Searches the range @p [first1,last1) for an element that is equal to
5163 * some element in the range [first2,last2). If found, returns an iterator in
5164 * the range [first1,last1), otherwise returns @p last1.
5166 template<typename _InputIterator, typename _ForwardIterator,
5167 typename _BinaryPredicate>
5169 find_first_of(_InputIterator __first1, _InputIterator __last1,
5170 _ForwardIterator __first2, _ForwardIterator __last2,
5171 _BinaryPredicate __comp)
5173 // concept requirements
5174 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
5175 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5176 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
5177 typename iterator_traits<_InputIterator>::value_type,
5178 typename iterator_traits<_ForwardIterator>::value_type>)
5179 __glibcxx_requires_valid_range(__first1, __last1);
5180 __glibcxx_requires_valid_range(__first2, __last2);
5182 for ( ; __first1 != __last1; ++__first1)
5183 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
5184 if (__comp(*__first1, *__iter))
5190 // find_end, with and without an explicitly supplied comparison function.
5191 // Search [first2, last2) as a subsequence in [first1, last1), and return
5192 // the *last* possible match. Note that find_end for bidirectional iterators
5193 // is much faster than for forward iterators.
5195 // find_end for forward iterators.
5196 template<typename _ForwardIterator1, typename _ForwardIterator2>
5198 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
5199 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
5200 forward_iterator_tag, forward_iterator_tag)
5202 if (__first2 == __last2)
5206 _ForwardIterator1 __result = __last1;
5209 _ForwardIterator1 __new_result
5210 = std::search(__first1, __last1, __first2, __last2);
5211 if (__new_result == __last1)
5215 __result = __new_result;
5216 __first1 = __new_result;
5223 template<typename _ForwardIterator1, typename _ForwardIterator2,
5224 typename _BinaryPredicate>
5226 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
5227 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
5228 forward_iterator_tag, forward_iterator_tag,
5229 _BinaryPredicate __comp)
5231 if (__first2 == __last2)
5235 _ForwardIterator1 __result = __last1;
5238 _ForwardIterator1 __new_result
5239 = std::search(__first1, __last1, __first2, __last2, __comp);
5240 if (__new_result == __last1)
5244 __result = __new_result;
5245 __first1 = __new_result;
5252 // find_end for bidirectional iterators. Requires partial specialization.
5253 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2>
5254 _BidirectionalIterator1
5255 __find_end(_BidirectionalIterator1 __first1,
5256 _BidirectionalIterator1 __last1,
5257 _BidirectionalIterator2 __first2,
5258 _BidirectionalIterator2 __last2,
5259 bidirectional_iterator_tag, bidirectional_iterator_tag)
5261 // concept requirements
5262 __glibcxx_function_requires(_BidirectionalIteratorConcept<
5263 _BidirectionalIterator1>)
5264 __glibcxx_function_requires(_BidirectionalIteratorConcept<
5265 _BidirectionalIterator2>)
5267 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
5268 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
5270 _RevIterator1 __rlast1(__first1);
5271 _RevIterator2 __rlast2(__first2);
5272 _RevIterator1 __rresult = std::search(_RevIterator1(__last1), __rlast1,
5273 _RevIterator2(__last2), __rlast2);
5275 if (__rresult == __rlast1)
5279 _BidirectionalIterator1 __result = __rresult.base();
5280 std::advance(__result, -std::distance(__first2, __last2));
5285 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
5286 typename _BinaryPredicate>
5287 _BidirectionalIterator1
5288 __find_end(_BidirectionalIterator1 __first1,
5289 _BidirectionalIterator1 __last1,
5290 _BidirectionalIterator2 __first2,
5291 _BidirectionalIterator2 __last2,
5292 bidirectional_iterator_tag, bidirectional_iterator_tag,
5293 _BinaryPredicate __comp)
5295 // concept requirements
5296 __glibcxx_function_requires(_BidirectionalIteratorConcept<
5297 _BidirectionalIterator1>)
5298 __glibcxx_function_requires(_BidirectionalIteratorConcept<
5299 _BidirectionalIterator2>)
5301 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
5302 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
5304 _RevIterator1 __rlast1(__first1);
5305 _RevIterator2 __rlast2(__first2);
5306 _RevIterator1 __rresult = std::search(_RevIterator1(__last1), __rlast1,
5307 _RevIterator2(__last2), __rlast2,
5310 if (__rresult == __rlast1)
5314 _BidirectionalIterator1 __result = __rresult.base();
5315 std::advance(__result, -std::distance(__first2, __last2));
5320 // Dispatching functions for find_end.
5323 * @brief Find last matching subsequence in a sequence.
5324 * @param first1 Start of range to search.
5325 * @param last1 End of range to search.
5326 * @param first2 Start of sequence to match.
5327 * @param last2 End of sequence to match.
5328 * @return The last iterator @c i in the range
5329 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
5330 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
5331 * such iterator exists.
5333 * Searches the range @p [first1,last1) for a sub-sequence that compares
5334 * equal value-by-value with the sequence given by @p [first2,last2) and
5335 * returns an iterator to the first element of the sub-sequence, or
5336 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
5337 * last such subsequence contained in [first,last1).
5339 * Because the sub-sequence must lie completely within the range
5340 * @p [first1,last1) it must start at a position less than
5341 * @p last1-(last2-first2) where @p last2-first2 is the length of the
5343 * This means that the returned iterator @c i will be in the range
5344 * @p [first1,last1-(last2-first2))
5346 template<typename _ForwardIterator1, typename _ForwardIterator2>
5347 inline _ForwardIterator1
5348 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
5349 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
5351 // concept requirements
5352 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
5353 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
5354 __glibcxx_function_requires(_EqualOpConcept<
5355 typename iterator_traits<_ForwardIterator1>::value_type,
5356 typename iterator_traits<_ForwardIterator2>::value_type>)
5357 __glibcxx_requires_valid_range(__first1, __last1);
5358 __glibcxx_requires_valid_range(__first2, __last2);
5360 return std::__find_end(__first1, __last1, __first2, __last2,
5361 std::__iterator_category(__first1),
5362 std::__iterator_category(__first2));
5366 * @brief Find last matching subsequence in a sequence using a predicate.
5367 * @param first1 Start of range to search.
5368 * @param last1 End of range to search.
5369 * @param first2 Start of sequence to match.
5370 * @param last2 End of sequence to match.
5371 * @param comp The predicate to use.
5372 * @return The last iterator @c i in the range
5373 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
5374 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
5375 * @p last1 if no such iterator exists.
5377 * Searches the range @p [first1,last1) for a sub-sequence that compares
5378 * equal value-by-value with the sequence given by @p [first2,last2) using
5379 * comp as a predicate and returns an iterator to the first element of the
5380 * sub-sequence, or @p last1 if the sub-sequence is not found. The
5381 * sub-sequence will be the last such subsequence contained in
5384 * Because the sub-sequence must lie completely within the range
5385 * @p [first1,last1) it must start at a position less than
5386 * @p last1-(last2-first2) where @p last2-first2 is the length of the
5388 * This means that the returned iterator @c i will be in the range
5389 * @p [first1,last1-(last2-first2))
5391 template<typename _ForwardIterator1, typename _ForwardIterator2,
5392 typename _BinaryPredicate>
5393 inline _ForwardIterator1
5394 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
5395 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
5396 _BinaryPredicate __comp)
5398 // concept requirements
5399 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
5400 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
5401 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
5402 typename iterator_traits<_ForwardIterator1>::value_type,
5403 typename iterator_traits<_ForwardIterator2>::value_type>)
5404 __glibcxx_requires_valid_range(__first1, __last1);
5405 __glibcxx_requires_valid_range(__first2, __last2);
5407 return std::__find_end(__first1, __last1, __first2, __last2,
5408 std::__iterator_category(__first1),
5409 std::__iterator_category(__first2),
5413 _GLIBCXX_END_NAMESPACE
5415 #endif /* _ALGO_H */