1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
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 <cstdlib> // for rand
68 #include <debug/debug.h>
70 // See concept_check.h for the __glibcxx_*_requires macros.
72 _GLIBCXX_BEGIN_NAMESPACE(std)
75 * @brief Find the median of three values.
79 * @return One of @p a, @p b or @p c.
81 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
82 * then the value returned will be @c m.
83 * This is an SGI extension.
84 * @ingroup SGIextensions
86 template<typename _Tp>
88 __median(const _Tp& __a, const _Tp& __b, const _Tp& __c)
90 // concept requirements
91 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
108 * @brief Find the median of three values using a predicate for comparison.
112 * @param comp A binary predicate.
113 * @return One of @p a, @p b or @p c.
115 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
116 * and @p comp(m,n) are both true then the value returned will be @c m.
117 * This is an SGI extension.
118 * @ingroup SGIextensions
120 template<typename _Tp, typename _Compare>
122 __median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp)
124 // concept requirements
125 __glibcxx_function_requires(_BinaryFunctionConcept<_Compare,bool,_Tp,_Tp>)
126 if (__comp(__a, __b))
127 if (__comp(__b, __c))
129 else if (__comp(__a, __c))
133 else if (__comp(__a, __c))
135 else if (__comp(__b, __c))
142 * @brief Apply a function to every element of a sequence.
143 * @param first An input iterator.
144 * @param last An input iterator.
145 * @param f A unary function object.
148 * Applies the function object @p f to each element in the range
149 * @p [first,last). @p f must not modify the order of the sequence.
150 * If @p f has a return value it is ignored.
152 template<typename _InputIterator, typename _Function>
154 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
156 // concept requirements
157 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
158 __glibcxx_requires_valid_range(__first, __last);
159 for ( ; __first != __last; ++__first)
166 * This is an overload used by find() for the Input Iterator case.
169 template<typename _InputIterator, typename _Tp>
170 inline _InputIterator
171 __find(_InputIterator __first, _InputIterator __last,
172 const _Tp& __val, input_iterator_tag)
174 while (__first != __last && !(*__first == __val))
181 * This is an overload used by find_if() for the Input Iterator case.
184 template<typename _InputIterator, typename _Predicate>
185 inline _InputIterator
186 __find_if(_InputIterator __first, _InputIterator __last,
187 _Predicate __pred, input_iterator_tag)
189 while (__first != __last && !bool(__pred(*__first)))
196 * This is an overload used by find() for the RAI case.
199 template<typename _RandomAccessIterator, typename _Tp>
200 _RandomAccessIterator
201 __find(_RandomAccessIterator __first, _RandomAccessIterator __last,
202 const _Tp& __val, random_access_iterator_tag)
204 typename iterator_traits<_RandomAccessIterator>::difference_type
205 __trip_count = (__last - __first) >> 2;
207 for ( ; __trip_count > 0 ; --__trip_count)
209 if (*__first == __val)
213 if (*__first == __val)
217 if (*__first == __val)
221 if (*__first == __val)
226 switch (__last - __first)
229 if (*__first == __val)
233 if (*__first == __val)
237 if (*__first == __val)
248 * This is an overload used by find_if() for the RAI case.
251 template<typename _RandomAccessIterator, typename _Predicate>
252 _RandomAccessIterator
253 __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
254 _Predicate __pred, random_access_iterator_tag)
256 typename iterator_traits<_RandomAccessIterator>::difference_type
257 __trip_count = (__last - __first) >> 2;
259 for ( ; __trip_count > 0 ; --__trip_count)
261 if (__pred(*__first))
265 if (__pred(*__first))
269 if (__pred(*__first))
273 if (__pred(*__first))
278 switch (__last - __first)
281 if (__pred(*__first))
285 if (__pred(*__first))
289 if (__pred(*__first))
299 * @brief Find the first occurrence of a value in a sequence.
300 * @param first An input iterator.
301 * @param last An input iterator.
302 * @param val The value to find.
303 * @return The first iterator @c i in the range @p [first,last)
304 * such that @c *i == @p val, or @p last if no such iterator exists.
306 template<typename _InputIterator, typename _Tp>
307 inline _InputIterator
308 find(_InputIterator __first, _InputIterator __last,
311 // concept requirements
312 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
313 __glibcxx_function_requires(_EqualOpConcept<
314 typename iterator_traits<_InputIterator>::value_type, _Tp>)
315 __glibcxx_requires_valid_range(__first, __last);
316 return std::__find(__first, __last, __val,
317 std::__iterator_category(__first));
321 * @brief Find the first element in a sequence for which a predicate is true.
322 * @param first An input iterator.
323 * @param last An input iterator.
324 * @param pred A predicate.
325 * @return The first iterator @c i in the range @p [first,last)
326 * such that @p pred(*i) is true, or @p last if no such iterator exists.
328 template<typename _InputIterator, typename _Predicate>
329 inline _InputIterator
330 find_if(_InputIterator __first, _InputIterator __last,
333 // concept requirements
334 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
335 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
336 typename iterator_traits<_InputIterator>::value_type>)
337 __glibcxx_requires_valid_range(__first, __last);
338 return std::__find_if(__first, __last, __pred,
339 std::__iterator_category(__first));
343 * @brief Find two adjacent values in a sequence that are equal.
344 * @param first A forward iterator.
345 * @param last A forward iterator.
346 * @return The first iterator @c i such that @c i and @c i+1 are both
347 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
348 * or @p last if no such iterator exists.
350 template<typename _ForwardIterator>
352 adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
354 // concept requirements
355 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
356 __glibcxx_function_requires(_EqualityComparableConcept<
357 typename iterator_traits<_ForwardIterator>::value_type>)
358 __glibcxx_requires_valid_range(__first, __last);
359 if (__first == __last)
361 _ForwardIterator __next = __first;
362 while(++__next != __last)
364 if (*__first == *__next)
372 * @brief Find two adjacent values in a sequence using a predicate.
373 * @param first A forward iterator.
374 * @param last A forward iterator.
375 * @param binary_pred A binary predicate.
376 * @return The first iterator @c i such that @c i and @c i+1 are both
377 * valid iterators in @p [first,last) and such that
378 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
381 template<typename _ForwardIterator, typename _BinaryPredicate>
383 adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
384 _BinaryPredicate __binary_pred)
386 // concept requirements
387 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
388 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
389 typename iterator_traits<_ForwardIterator>::value_type,
390 typename iterator_traits<_ForwardIterator>::value_type>)
391 __glibcxx_requires_valid_range(__first, __last);
392 if (__first == __last)
394 _ForwardIterator __next = __first;
395 while(++__next != __last)
397 if (__binary_pred(*__first, *__next))
405 * @brief Count the number of copies of a value in a sequence.
406 * @param first An input iterator.
407 * @param last An input iterator.
408 * @param value The value to be counted.
409 * @return The number of iterators @c i in the range @p [first,last)
410 * for which @c *i == @p value
412 template<typename _InputIterator, typename _Tp>
413 typename iterator_traits<_InputIterator>::difference_type
414 count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
416 // concept requirements
417 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
418 __glibcxx_function_requires(_EqualOpConcept<
419 typename iterator_traits<_InputIterator>::value_type, _Tp>)
420 __glibcxx_requires_valid_range(__first, __last);
421 typename iterator_traits<_InputIterator>::difference_type __n = 0;
422 for ( ; __first != __last; ++__first)
423 if (*__first == __value)
429 * @brief Count the elements of a sequence for which a predicate is true.
430 * @param first An input iterator.
431 * @param last An input iterator.
432 * @param pred A predicate.
433 * @return The number of iterators @c i in the range @p [first,last)
434 * for which @p pred(*i) is true.
436 template<typename _InputIterator, typename _Predicate>
437 typename iterator_traits<_InputIterator>::difference_type
438 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
440 // concept requirements
441 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
442 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
443 typename iterator_traits<_InputIterator>::value_type>)
444 __glibcxx_requires_valid_range(__first, __last);
445 typename iterator_traits<_InputIterator>::difference_type __n = 0;
446 for ( ; __first != __last; ++__first)
447 if (__pred(*__first))
453 * @brief Search a sequence for a matching sub-sequence.
454 * @param first1 A forward iterator.
455 * @param last1 A forward iterator.
456 * @param first2 A forward iterator.
457 * @param last2 A forward iterator.
458 * @return The first iterator @c i in the range
459 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
460 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
461 * such iterator exists.
463 * Searches the range @p [first1,last1) for a sub-sequence that compares
464 * equal value-by-value with the sequence given by @p [first2,last2) and
465 * returns an iterator to the first element of the sub-sequence, or
466 * @p last1 if the sub-sequence is not found.
468 * Because the sub-sequence must lie completely within the range
469 * @p [first1,last1) it must start at a position less than
470 * @p last1-(last2-first2) where @p last2-first2 is the length of the
472 * This means that the returned iterator @c i will be in the range
473 * @p [first1,last1-(last2-first2))
475 template<typename _ForwardIterator1, typename _ForwardIterator2>
477 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
478 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
480 // concept requirements
481 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
482 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
483 __glibcxx_function_requires(_EqualOpConcept<
484 typename iterator_traits<_ForwardIterator1>::value_type,
485 typename iterator_traits<_ForwardIterator2>::value_type>)
486 __glibcxx_requires_valid_range(__first1, __last1);
487 __glibcxx_requires_valid_range(__first2, __last2);
488 // Test for empty ranges
489 if (__first1 == __last1 || __first2 == __last2)
492 // Test for a pattern of length 1.
493 _ForwardIterator2 __tmp(__first2);
495 if (__tmp == __last2)
496 return std::find(__first1, __last1, *__first2);
499 _ForwardIterator2 __p1, __p;
500 __p1 = __first2; ++__p1;
501 _ForwardIterator1 __current = __first1;
503 while (__first1 != __last1)
505 __first1 = std::find(__first1, __last1, *__first2);
506 if (__first1 == __last1)
510 __current = __first1;
511 if (++__current == __last1)
514 while (*__current == *__p)
516 if (++__p == __last2)
518 if (++__current == __last1)
527 * @brief Search a sequence for a matching sub-sequence using a predicate.
528 * @param first1 A forward iterator.
529 * @param last1 A forward iterator.
530 * @param first2 A forward iterator.
531 * @param last2 A forward iterator.
532 * @param predicate A binary predicate.
533 * @return The first iterator @c i in the range
534 * @p [first1,last1-(last2-first2)) such that
535 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
536 * @p [0,last2-first2), or @p last1 if no such iterator exists.
538 * Searches the range @p [first1,last1) for a sub-sequence that compares
539 * equal value-by-value with the sequence given by @p [first2,last2),
540 * using @p predicate to determine equality, and returns an iterator
541 * to the first element of the sub-sequence, or @p last1 if no such
544 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
546 template<typename _ForwardIterator1, typename _ForwardIterator2,
547 typename _BinaryPredicate>
549 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
550 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
551 _BinaryPredicate __predicate)
553 // concept requirements
554 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
555 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
556 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
557 typename iterator_traits<_ForwardIterator1>::value_type,
558 typename iterator_traits<_ForwardIterator2>::value_type>)
559 __glibcxx_requires_valid_range(__first1, __last1);
560 __glibcxx_requires_valid_range(__first2, __last2);
562 // Test for empty ranges
563 if (__first1 == __last1 || __first2 == __last2)
566 // Test for a pattern of length 1.
567 _ForwardIterator2 __tmp(__first2);
569 if (__tmp == __last2)
571 while (__first1 != __last1
572 && !bool(__predicate(*__first1, *__first2)))
578 _ForwardIterator2 __p1, __p;
579 __p1 = __first2; ++__p1;
580 _ForwardIterator1 __current = __first1;
582 while (__first1 != __last1)
584 while (__first1 != __last1)
586 if (__predicate(*__first1, *__first2))
590 while (__first1 != __last1 &&
591 !bool(__predicate(*__first1, *__first2)))
593 if (__first1 == __last1)
597 __current = __first1;
598 if (++__current == __last1)
601 while (__predicate(*__current, *__p))
603 if (++__p == __last2)
605 if (++__current == __last1)
615 * This is an uglified
616 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
617 * overloaded for forward iterators.
620 template<typename _ForwardIterator, typename _Integer, typename _Tp>
622 __search_n(_ForwardIterator __first, _ForwardIterator __last,
623 _Integer __count, const _Tp& __val,
624 std::forward_iterator_tag)
626 __first = std::find(__first, __last, __val);
627 while (__first != __last)
629 typename iterator_traits<_ForwardIterator>::difference_type
631 _ForwardIterator __i = __first;
633 while (__i != __last && __n != 1 && *__i == __val)
642 __first = std::find(++__i, __last, __val);
649 * This is an uglified
650 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
651 * overloaded for random access iterators.
654 template<typename _RandomAccessIter, typename _Integer, typename _Tp>
656 __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
657 _Integer __count, const _Tp& __val,
658 std::random_access_iterator_tag)
661 typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
664 _DistanceType __tailSize = __last - __first;
665 const _DistanceType __pattSize = __count;
667 if (__tailSize < __pattSize)
670 const _DistanceType __skipOffset = __pattSize - 1;
671 _RandomAccessIter __lookAhead = __first + __skipOffset;
672 __tailSize -= __pattSize;
674 while (1) // the main loop...
676 // __lookAhead here is always pointing to the last element of next
678 while (!(*__lookAhead == __val)) // the skip loop...
680 if (__tailSize < __pattSize)
681 return __last; // Failure
682 __lookAhead += __pattSize;
683 __tailSize -= __pattSize;
685 _DistanceType __remainder = __skipOffset;
686 for (_RandomAccessIter __backTrack = __lookAhead - 1;
687 *__backTrack == __val; --__backTrack)
689 if (--__remainder == 0)
690 return (__lookAhead - __skipOffset); // Success
692 if (__remainder > __tailSize)
693 return __last; // Failure
694 __lookAhead += __remainder;
695 __tailSize -= __remainder;
700 * @brief Search a sequence for a number of consecutive values.
701 * @param first A forward iterator.
702 * @param last A forward iterator.
703 * @param count The number of consecutive values.
704 * @param val The value to find.
705 * @return The first iterator @c i in the range @p [first,last-count)
706 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
707 * or @p last if no such iterator exists.
709 * Searches the range @p [first,last) for @p count consecutive elements
712 template<typename _ForwardIterator, typename _Integer, typename _Tp>
714 search_n(_ForwardIterator __first, _ForwardIterator __last,
715 _Integer __count, const _Tp& __val)
717 // concept requirements
718 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
719 __glibcxx_function_requires(_EqualOpConcept<
720 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
721 __glibcxx_requires_valid_range(__first, __last);
726 return std::find(__first, __last, __val);
727 return std::__search_n(__first, __last, __count, __val,
728 std::__iterator_category(__first));
733 * This is an uglified
734 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
736 * overloaded for forward iterators.
739 template<typename _ForwardIterator, typename _Integer, typename _Tp,
740 typename _BinaryPredicate>
742 __search_n(_ForwardIterator __first, _ForwardIterator __last,
743 _Integer __count, const _Tp& __val,
744 _BinaryPredicate __binary_pred, std::forward_iterator_tag)
746 while (__first != __last && !bool(__binary_pred(*__first, __val)))
749 while (__first != __last)
751 typename iterator_traits<_ForwardIterator>::difference_type
753 _ForwardIterator __i = __first;
755 while (__i != __last && __n != 1 && bool(__binary_pred(*__i, __val)))
765 while (__first != __last
766 && !bool(__binary_pred(*__first, __val)))
774 * This is an uglified
775 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
777 * overloaded for random access iterators.
780 template<typename _RandomAccessIter, typename _Integer, typename _Tp,
781 typename _BinaryPredicate>
783 __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
784 _Integer __count, const _Tp& __val,
785 _BinaryPredicate __binary_pred, std::random_access_iterator_tag)
788 typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
791 _DistanceType __tailSize = __last - __first;
792 const _DistanceType __pattSize = __count;
794 if (__tailSize < __pattSize)
797 const _DistanceType __skipOffset = __pattSize - 1;
798 _RandomAccessIter __lookAhead = __first + __skipOffset;
799 __tailSize -= __pattSize;
801 while (1) // the main loop...
803 // __lookAhead here is always pointing to the last element of next
805 while (!bool(__binary_pred(*__lookAhead, __val))) // the skip loop...
807 if (__tailSize < __pattSize)
808 return __last; // Failure
809 __lookAhead += __pattSize;
810 __tailSize -= __pattSize;
812 _DistanceType __remainder = __skipOffset;
813 for (_RandomAccessIter __backTrack = __lookAhead - 1;
814 __binary_pred(*__backTrack, __val); --__backTrack)
816 if (--__remainder == 0)
817 return (__lookAhead - __skipOffset); // Success
819 if (__remainder > __tailSize)
820 return __last; // Failure
821 __lookAhead += __remainder;
822 __tailSize -= __remainder;
827 * @brief Search a sequence for a number of consecutive values using a
829 * @param first A forward iterator.
830 * @param last A forward iterator.
831 * @param count The number of consecutive values.
832 * @param val The value to find.
833 * @param binary_pred A binary predicate.
834 * @return The first iterator @c i in the range @p [first,last-count)
835 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
836 * range @p [0,count), or @p last if no such iterator exists.
838 * Searches the range @p [first,last) for @p count consecutive elements
839 * for which the predicate returns true.
841 template<typename _ForwardIterator, typename _Integer, typename _Tp,
842 typename _BinaryPredicate>
844 search_n(_ForwardIterator __first, _ForwardIterator __last,
845 _Integer __count, const _Tp& __val,
846 _BinaryPredicate __binary_pred)
848 // concept requirements
849 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
850 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
851 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
852 __glibcxx_requires_valid_range(__first, __last);
858 while (__first != __last && !bool(__binary_pred(*__first, __val)))
862 return std::__search_n(__first, __last, __count, __val, __binary_pred,
863 std::__iterator_category(__first));
867 * @brief Perform an operation on a sequence.
868 * @param first An input iterator.
869 * @param last An input iterator.
870 * @param result An output iterator.
871 * @param unary_op A unary operator.
872 * @return An output iterator equal to @p result+(last-first).
874 * Applies the operator to each element in the input range and assigns
875 * the results to successive elements of the output sequence.
876 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
877 * range @p [0,last-first).
879 * @p unary_op must not alter its argument.
881 template<typename _InputIterator, typename _OutputIterator,
882 typename _UnaryOperation>
884 transform(_InputIterator __first, _InputIterator __last,
885 _OutputIterator __result, _UnaryOperation __unary_op)
887 // concept requirements
888 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
889 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
890 // "the type returned by a _UnaryOperation"
891 __typeof__(__unary_op(*__first))>)
892 __glibcxx_requires_valid_range(__first, __last);
894 for ( ; __first != __last; ++__first, ++__result)
895 *__result = __unary_op(*__first);
900 * @brief Perform an operation on corresponding elements of two sequences.
901 * @param first1 An input iterator.
902 * @param last1 An input iterator.
903 * @param first2 An input iterator.
904 * @param result An output iterator.
905 * @param binary_op A binary operator.
906 * @return An output iterator equal to @p result+(last-first).
908 * Applies the operator to the corresponding elements in the two
909 * input ranges and assigns the results to successive elements of the
911 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
912 * @c N in the range @p [0,last1-first1).
914 * @p binary_op must not alter either of its arguments.
916 template<typename _InputIterator1, typename _InputIterator2,
917 typename _OutputIterator, typename _BinaryOperation>
919 transform(_InputIterator1 __first1, _InputIterator1 __last1,
920 _InputIterator2 __first2, _OutputIterator __result,
921 _BinaryOperation __binary_op)
923 // concept requirements
924 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
925 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
926 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
927 // "the type returned by a _BinaryOperation"
928 __typeof__(__binary_op(*__first1,*__first2))>)
929 __glibcxx_requires_valid_range(__first1, __last1);
931 for ( ; __first1 != __last1; ++__first1, ++__first2, ++__result)
932 *__result = __binary_op(*__first1, *__first2);
937 * @brief Replace each occurrence of one value in a sequence with another
939 * @param first A forward iterator.
940 * @param last A forward iterator.
941 * @param old_value The value to be replaced.
942 * @param new_value The replacement value.
943 * @return replace() returns no value.
945 * For each iterator @c i in the range @p [first,last) if @c *i ==
946 * @p old_value then the assignment @c *i = @p new_value is performed.
948 template<typename _ForwardIterator, typename _Tp>
950 replace(_ForwardIterator __first, _ForwardIterator __last,
951 const _Tp& __old_value, const _Tp& __new_value)
953 // concept requirements
954 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
956 __glibcxx_function_requires(_EqualOpConcept<
957 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
958 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
959 typename iterator_traits<_ForwardIterator>::value_type>)
960 __glibcxx_requires_valid_range(__first, __last);
962 for ( ; __first != __last; ++__first)
963 if (*__first == __old_value)
964 *__first = __new_value;
968 * @brief Replace each value in a sequence for which a predicate returns
969 * true with another value.
970 * @param first A forward iterator.
971 * @param last A forward iterator.
972 * @param pred A predicate.
973 * @param new_value The replacement value.
974 * @return replace_if() returns no value.
976 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
977 * is true then the assignment @c *i = @p new_value is performed.
979 template<typename _ForwardIterator, typename _Predicate, typename _Tp>
981 replace_if(_ForwardIterator __first, _ForwardIterator __last,
982 _Predicate __pred, const _Tp& __new_value)
984 // concept requirements
985 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
987 __glibcxx_function_requires(_ConvertibleConcept<_Tp,
988 typename iterator_traits<_ForwardIterator>::value_type>)
989 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
990 typename iterator_traits<_ForwardIterator>::value_type>)
991 __glibcxx_requires_valid_range(__first, __last);
993 for ( ; __first != __last; ++__first)
994 if (__pred(*__first))
995 *__first = __new_value;
999 * @brief Copy a sequence, replacing each element of one value with another
1001 * @param first An input iterator.
1002 * @param last An input iterator.
1003 * @param result An output iterator.
1004 * @param old_value The value to be replaced.
1005 * @param new_value The replacement value.
1006 * @return The end of the output sequence, @p result+(last-first).
1008 * Copies each element in the input range @p [first,last) to the
1009 * output range @p [result,result+(last-first)) replacing elements
1010 * equal to @p old_value with @p new_value.
1012 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
1014 replace_copy(_InputIterator __first, _InputIterator __last,
1015 _OutputIterator __result,
1016 const _Tp& __old_value, const _Tp& __new_value)
1018 // concept requirements
1019 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1020 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1021 typename iterator_traits<_InputIterator>::value_type>)
1022 __glibcxx_function_requires(_EqualOpConcept<
1023 typename iterator_traits<_InputIterator>::value_type, _Tp>)
1024 __glibcxx_requires_valid_range(__first, __last);
1026 for ( ; __first != __last; ++__first, ++__result)
1027 if (*__first == __old_value)
1028 *__result = __new_value;
1030 *__result = *__first;
1035 * @brief Copy a sequence, replacing each value for which a predicate
1036 * returns true with another value.
1037 * @param first An input iterator.
1038 * @param last An input iterator.
1039 * @param result An output iterator.
1040 * @param pred A predicate.
1041 * @param new_value The replacement value.
1042 * @return The end of the output sequence, @p result+(last-first).
1044 * Copies each element in the range @p [first,last) to the range
1045 * @p [result,result+(last-first)) replacing elements for which
1046 * @p pred returns true with @p new_value.
1048 template<typename _InputIterator, typename _OutputIterator,
1049 typename _Predicate, typename _Tp>
1051 replace_copy_if(_InputIterator __first, _InputIterator __last,
1052 _OutputIterator __result,
1053 _Predicate __pred, const _Tp& __new_value)
1055 // concept requirements
1056 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1057 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1058 typename iterator_traits<_InputIterator>::value_type>)
1059 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1060 typename iterator_traits<_InputIterator>::value_type>)
1061 __glibcxx_requires_valid_range(__first, __last);
1063 for ( ; __first != __last; ++__first, ++__result)
1064 if (__pred(*__first))
1065 *__result = __new_value;
1067 *__result = *__first;
1072 * @brief Assign the result of a function object to each value in a
1074 * @param first A forward iterator.
1075 * @param last A forward iterator.
1076 * @param gen A function object taking no arguments.
1077 * @return generate() returns no value.
1079 * Performs the assignment @c *i = @p gen() for each @c i in the range
1082 template<typename _ForwardIterator, typename _Generator>
1084 generate(_ForwardIterator __first, _ForwardIterator __last,
1087 // concept requirements
1088 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1089 __glibcxx_function_requires(_GeneratorConcept<_Generator,
1090 typename iterator_traits<_ForwardIterator>::value_type>)
1091 __glibcxx_requires_valid_range(__first, __last);
1093 for ( ; __first != __last; ++__first)
1098 * @brief Assign the result of a function object to each value in a
1100 * @param first A forward iterator.
1101 * @param n The length of the sequence.
1102 * @param gen A function object taking no arguments.
1103 * @return The end of the sequence, @p first+n
1105 * Performs the assignment @c *i = @p gen() for each @c i in the range
1106 * @p [first,first+n).
1108 template<typename _OutputIterator, typename _Size, typename _Generator>
1110 generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
1112 // concept requirements
1113 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1114 // "the type returned by a _Generator"
1115 __typeof__(__gen())>)
1117 for ( ; __n > 0; --__n, ++__first)
1123 * @brief Copy a sequence, removing elements of a given value.
1124 * @param first An input iterator.
1125 * @param last An input iterator.
1126 * @param result An output iterator.
1127 * @param value The value to be removed.
1128 * @return An iterator designating the end of the resulting sequence.
1130 * Copies each element in the range @p [first,last) not equal to @p value
1131 * to the range beginning at @p result.
1132 * remove_copy() is stable, so the relative order of elements that are
1133 * copied is unchanged.
1135 template<typename _InputIterator, typename _OutputIterator, typename _Tp>
1137 remove_copy(_InputIterator __first, _InputIterator __last,
1138 _OutputIterator __result, const _Tp& __value)
1140 // concept requirements
1141 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1142 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1143 typename iterator_traits<_InputIterator>::value_type>)
1144 __glibcxx_function_requires(_EqualOpConcept<
1145 typename iterator_traits<_InputIterator>::value_type, _Tp>)
1146 __glibcxx_requires_valid_range(__first, __last);
1148 for ( ; __first != __last; ++__first)
1149 if (!(*__first == __value))
1151 *__result = *__first;
1158 * @brief Copy a sequence, removing elements for which a predicate is true.
1159 * @param first An input iterator.
1160 * @param last An input iterator.
1161 * @param result An output iterator.
1162 * @param pred A predicate.
1163 * @return An iterator designating the end of the resulting sequence.
1165 * Copies each element in the range @p [first,last) for which
1166 * @p pred returns true to the range beginning at @p result.
1168 * remove_copy_if() is stable, so the relative order of elements that are
1169 * copied is unchanged.
1171 template<typename _InputIterator, typename _OutputIterator,
1172 typename _Predicate>
1174 remove_copy_if(_InputIterator __first, _InputIterator __last,
1175 _OutputIterator __result, _Predicate __pred)
1177 // concept requirements
1178 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1179 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1180 typename iterator_traits<_InputIterator>::value_type>)
1181 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1182 typename iterator_traits<_InputIterator>::value_type>)
1183 __glibcxx_requires_valid_range(__first, __last);
1185 for ( ; __first != __last; ++__first)
1186 if (!bool(__pred(*__first)))
1188 *__result = *__first;
1195 * @brief Remove elements from a sequence.
1196 * @param first An input iterator.
1197 * @param last An input iterator.
1198 * @param value The value to be removed.
1199 * @return An iterator designating the end of the resulting sequence.
1201 * All elements equal to @p value are removed from the range
1204 * remove() is stable, so the relative order of elements that are
1205 * not removed is unchanged.
1207 * Elements between the end of the resulting sequence and @p last
1208 * are still present, but their value is unspecified.
1210 template<typename _ForwardIterator, typename _Tp>
1212 remove(_ForwardIterator __first, _ForwardIterator __last,
1215 // concept requirements
1216 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1218 __glibcxx_function_requires(_EqualOpConcept<
1219 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
1220 __glibcxx_requires_valid_range(__first, __last);
1222 __first = std::find(__first, __last, __value);
1223 _ForwardIterator __i = __first;
1224 return __first == __last ? __first
1225 : std::remove_copy(++__i, __last,
1230 * @brief Remove elements from a sequence using a predicate.
1231 * @param first A forward iterator.
1232 * @param last A forward iterator.
1233 * @param pred A predicate.
1234 * @return An iterator designating the end of the resulting sequence.
1236 * All elements for which @p pred returns true are removed from the range
1239 * remove_if() is stable, so the relative order of elements that are
1240 * not removed is unchanged.
1242 * Elements between the end of the resulting sequence and @p last
1243 * are still present, but their value is unspecified.
1245 template<typename _ForwardIterator, typename _Predicate>
1247 remove_if(_ForwardIterator __first, _ForwardIterator __last,
1250 // concept requirements
1251 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1253 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
1254 typename iterator_traits<_ForwardIterator>::value_type>)
1255 __glibcxx_requires_valid_range(__first, __last);
1257 __first = std::find_if(__first, __last, __pred);
1258 _ForwardIterator __i = __first;
1259 return __first == __last ? __first
1260 : std::remove_copy_if(++__i, __last,
1266 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1268 * overloaded for forward iterators and output iterator as result.
1271 template<typename _ForwardIterator, typename _OutputIterator>
1273 __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
1274 _OutputIterator __result,
1275 forward_iterator_tag, output_iterator_tag)
1277 // concept requirements -- taken care of in dispatching function
1278 _ForwardIterator __next = __first;
1279 *__result = *__first;
1280 while (++__next != __last)
1281 if (!(*__first == *__next))
1284 *++__result = *__first;
1291 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1293 * overloaded for input iterators and output iterator as result.
1296 template<typename _InputIterator, typename _OutputIterator>
1298 __unique_copy(_InputIterator __first, _InputIterator __last,
1299 _OutputIterator __result,
1300 input_iterator_tag, output_iterator_tag)
1302 // concept requirements -- taken care of in dispatching function
1303 typename iterator_traits<_InputIterator>::value_type __value = *__first;
1304 *__result = __value;
1305 while (++__first != __last)
1306 if (!(__value == *__first))
1309 *++__result = __value;
1316 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1318 * overloaded for input iterators and forward iterator as result.
1321 template<typename _InputIterator, typename _ForwardIterator>
1323 __unique_copy(_InputIterator __first, _InputIterator __last,
1324 _ForwardIterator __result,
1325 input_iterator_tag, forward_iterator_tag)
1327 // concept requirements -- taken care of in dispatching function
1328 *__result = *__first;
1329 while (++__first != __last)
1330 if (!(*__result == *__first))
1331 *++__result = *__first;
1337 * This is an uglified
1338 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1340 * overloaded for forward iterators and output iterator as result.
1343 template<typename _ForwardIterator, typename _OutputIterator,
1344 typename _BinaryPredicate>
1346 __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
1347 _OutputIterator __result, _BinaryPredicate __binary_pred,
1348 forward_iterator_tag, output_iterator_tag)
1350 // concept requirements -- iterators already checked
1351 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1352 typename iterator_traits<_ForwardIterator>::value_type,
1353 typename iterator_traits<_ForwardIterator>::value_type>)
1355 _ForwardIterator __next = __first;
1356 *__result = *__first;
1357 while (++__next != __last)
1358 if (!bool(__binary_pred(*__first, *__next)))
1361 *++__result = *__first;
1368 * This is an uglified
1369 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1371 * overloaded for input iterators and output iterator as result.
1374 template<typename _InputIterator, typename _OutputIterator,
1375 typename _BinaryPredicate>
1377 __unique_copy(_InputIterator __first, _InputIterator __last,
1378 _OutputIterator __result, _BinaryPredicate __binary_pred,
1379 input_iterator_tag, output_iterator_tag)
1381 // concept requirements -- iterators already checked
1382 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1383 typename iterator_traits<_InputIterator>::value_type,
1384 typename iterator_traits<_InputIterator>::value_type>)
1386 typename iterator_traits<_InputIterator>::value_type __value = *__first;
1387 *__result = __value;
1388 while (++__first != __last)
1389 if (!bool(__binary_pred(__value, *__first)))
1392 *++__result = __value;
1399 * This is an uglified
1400 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1402 * overloaded for input iterators and forward iterator as result.
1405 template<typename _InputIterator, typename _ForwardIterator,
1406 typename _BinaryPredicate>
1408 __unique_copy(_InputIterator __first, _InputIterator __last,
1409 _ForwardIterator __result, _BinaryPredicate __binary_pred,
1410 input_iterator_tag, forward_iterator_tag)
1412 // concept requirements -- iterators already checked
1413 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1414 typename iterator_traits<_ForwardIterator>::value_type,
1415 typename iterator_traits<_InputIterator>::value_type>)
1417 *__result = *__first;
1418 while (++__first != __last)
1419 if (!bool(__binary_pred(*__result, *__first)))
1420 *++__result = *__first;
1425 * @brief Copy a sequence, removing consecutive duplicate values.
1426 * @param first An input iterator.
1427 * @param last An input iterator.
1428 * @param result An output iterator.
1429 * @return An iterator designating the end of the resulting sequence.
1431 * Copies each element in the range @p [first,last) to the range
1432 * beginning at @p result, except that only the first element is copied
1433 * from groups of consecutive elements that compare equal.
1434 * unique_copy() is stable, so the relative order of elements that are
1435 * copied is unchanged.
1438 * _GLIBCXX_RESOLVE_LIB_DEFECTS
1439 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
1441 * _GLIBCXX_RESOLVE_LIB_DEFECTS
1442 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
1446 template<typename _InputIterator, typename _OutputIterator>
1447 inline _OutputIterator
1448 unique_copy(_InputIterator __first, _InputIterator __last,
1449 _OutputIterator __result)
1451 // concept requirements
1452 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1453 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1454 typename iterator_traits<_InputIterator>::value_type>)
1455 __glibcxx_function_requires(_EqualityComparableConcept<
1456 typename iterator_traits<_InputIterator>::value_type>)
1457 __glibcxx_requires_valid_range(__first, __last);
1459 if (__first == __last)
1461 return std::__unique_copy(__first, __last, __result,
1462 std::__iterator_category(__first),
1463 std::__iterator_category(__result));
1467 * @brief Copy a sequence, removing consecutive values using a predicate.
1468 * @param first An input iterator.
1469 * @param last An input iterator.
1470 * @param result An output iterator.
1471 * @param binary_pred A binary predicate.
1472 * @return An iterator designating the end of the resulting sequence.
1474 * Copies each element in the range @p [first,last) to the range
1475 * beginning at @p result, except that only the first element is copied
1476 * from groups of consecutive elements for which @p binary_pred returns
1478 * unique_copy() is stable, so the relative order of elements that are
1479 * copied is unchanged.
1482 * _GLIBCXX_RESOLVE_LIB_DEFECTS
1483 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
1486 template<typename _InputIterator, typename _OutputIterator,
1487 typename _BinaryPredicate>
1488 inline _OutputIterator
1489 unique_copy(_InputIterator __first, _InputIterator __last,
1490 _OutputIterator __result,
1491 _BinaryPredicate __binary_pred)
1493 // concept requirements -- predicates checked later
1494 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
1495 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1496 typename iterator_traits<_InputIterator>::value_type>)
1497 __glibcxx_requires_valid_range(__first, __last);
1499 if (__first == __last)
1501 return std::__unique_copy(__first, __last, __result, __binary_pred,
1502 std::__iterator_category(__first),
1503 std::__iterator_category(__result));
1507 * @brief Remove consecutive duplicate values from a sequence.
1508 * @param first A forward iterator.
1509 * @param last A forward iterator.
1510 * @return An iterator designating the end of the resulting sequence.
1512 * Removes all but the first element from each group of consecutive
1513 * values that compare equal.
1514 * unique() is stable, so the relative order of elements that are
1515 * not removed is unchanged.
1516 * Elements between the end of the resulting sequence and @p last
1517 * are still present, but their value is unspecified.
1519 template<typename _ForwardIterator>
1521 unique(_ForwardIterator __first, _ForwardIterator __last)
1523 // concept requirements
1524 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1526 __glibcxx_function_requires(_EqualityComparableConcept<
1527 typename iterator_traits<_ForwardIterator>::value_type>)
1528 __glibcxx_requires_valid_range(__first, __last);
1530 // Skip the beginning, if already unique.
1531 __first = std::adjacent_find(__first, __last);
1532 if (__first == __last)
1535 // Do the real copy work.
1536 _ForwardIterator __dest = __first;
1538 while (++__first != __last)
1539 if (!(*__dest == *__first))
1540 *++__dest = *__first;
1545 * @brief Remove consecutive values from a sequence using a predicate.
1546 * @param first A forward iterator.
1547 * @param last A forward iterator.
1548 * @param binary_pred A binary predicate.
1549 * @return An iterator designating the end of the resulting sequence.
1551 * Removes all but the first element from each group of consecutive
1552 * values for which @p binary_pred returns true.
1553 * unique() is stable, so the relative order of elements that are
1554 * not removed is unchanged.
1555 * Elements between the end of the resulting sequence and @p last
1556 * are still present, but their value is unspecified.
1558 template<typename _ForwardIterator, typename _BinaryPredicate>
1560 unique(_ForwardIterator __first, _ForwardIterator __last,
1561 _BinaryPredicate __binary_pred)
1563 // concept requirements
1564 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1566 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
1567 typename iterator_traits<_ForwardIterator>::value_type,
1568 typename iterator_traits<_ForwardIterator>::value_type>)
1569 __glibcxx_requires_valid_range(__first, __last);
1571 // Skip the beginning, if already unique.
1572 __first = std::adjacent_find(__first, __last, __binary_pred);
1573 if (__first == __last)
1576 // Do the real copy work.
1577 _ForwardIterator __dest = __first;
1579 while (++__first != __last)
1580 if (!bool(__binary_pred(*__dest, *__first)))
1581 *++__dest = *__first;
1587 * This is an uglified reverse(_BidirectionalIterator,
1588 * _BidirectionalIterator)
1589 * overloaded for bidirectional iterators.
1592 template<typename _BidirectionalIterator>
1594 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last,
1595 bidirectional_iterator_tag)
1598 if (__first == __last || __first == --__last)
1602 std::iter_swap(__first, __last);
1609 * This is an uglified reverse(_BidirectionalIterator,
1610 * _BidirectionalIterator)
1611 * overloaded for random access iterators.
1614 template<typename _RandomAccessIterator>
1616 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last,
1617 random_access_iterator_tag)
1619 if (__first == __last)
1622 while (__first < __last)
1624 std::iter_swap(__first, __last);
1631 * @brief Reverse a sequence.
1632 * @param first A bidirectional iterator.
1633 * @param last A bidirectional iterator.
1634 * @return reverse() returns no value.
1636 * Reverses the order of the elements in the range @p [first,last),
1637 * so that the first element becomes the last etc.
1638 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1639 * swaps @p *(first+i) and @p *(last-(i+1))
1641 template<typename _BidirectionalIterator>
1643 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last)
1645 // concept requirements
1646 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
1647 _BidirectionalIterator>)
1648 __glibcxx_requires_valid_range(__first, __last);
1649 std::__reverse(__first, __last, std::__iterator_category(__first));
1653 * @brief Copy a sequence, reversing its elements.
1654 * @param first A bidirectional iterator.
1655 * @param last A bidirectional iterator.
1656 * @param result An output iterator.
1657 * @return An iterator designating the end of the resulting sequence.
1659 * Copies the elements in the range @p [first,last) to the range
1660 * @p [result,result+(last-first)) such that the order of the
1661 * elements is reversed.
1662 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1663 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1664 * The ranges @p [first,last) and @p [result,result+(last-first))
1667 template<typename _BidirectionalIterator, typename _OutputIterator>
1669 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last,
1670 _OutputIterator __result)
1672 // concept requirements
1673 __glibcxx_function_requires(_BidirectionalIteratorConcept<
1674 _BidirectionalIterator>)
1675 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1676 typename iterator_traits<_BidirectionalIterator>::value_type>)
1677 __glibcxx_requires_valid_range(__first, __last);
1679 while (__first != __last)
1682 *__result = *__last;
1691 * This is a helper function for the rotate algorithm specialized on RAIs.
1692 * It returns the greatest common divisor of two integer values.
1695 template<typename _EuclideanRingElement>
1696 _EuclideanRingElement
1697 __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n)
1701 _EuclideanRingElement __t = __m % __n;
1710 * This is a helper function for the rotate algorithm.
1713 template<typename _ForwardIterator>
1715 __rotate(_ForwardIterator __first,
1716 _ForwardIterator __middle,
1717 _ForwardIterator __last,
1718 forward_iterator_tag)
1720 if (__first == __middle || __last == __middle)
1723 _ForwardIterator __first2 = __middle;
1726 swap(*__first, *__first2);
1729 if (__first == __middle)
1730 __middle = __first2;
1732 while (__first2 != __last);
1734 __first2 = __middle;
1736 while (__first2 != __last)
1738 swap(*__first, *__first2);
1741 if (__first == __middle)
1742 __middle = __first2;
1743 else if (__first2 == __last)
1744 __first2 = __middle;
1750 * This is a helper function for the rotate algorithm.
1753 template<typename _BidirectionalIterator>
1755 __rotate(_BidirectionalIterator __first,
1756 _BidirectionalIterator __middle,
1757 _BidirectionalIterator __last,
1758 bidirectional_iterator_tag)
1760 // concept requirements
1761 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
1762 _BidirectionalIterator>)
1764 if (__first == __middle || __last == __middle)
1767 std::__reverse(__first, __middle, bidirectional_iterator_tag());
1768 std::__reverse(__middle, __last, bidirectional_iterator_tag());
1770 while (__first != __middle && __middle != __last)
1772 swap(*__first, *--__last);
1776 if (__first == __middle)
1777 std::__reverse(__middle, __last, bidirectional_iterator_tag());
1779 std::__reverse(__first, __middle, bidirectional_iterator_tag());
1784 * This is a helper function for the rotate algorithm.
1787 template<typename _RandomAccessIterator>
1789 __rotate(_RandomAccessIterator __first,
1790 _RandomAccessIterator __middle,
1791 _RandomAccessIterator __last,
1792 random_access_iterator_tag)
1794 // concept requirements
1795 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
1796 _RandomAccessIterator>)
1798 if (__first == __middle || __last == __middle)
1801 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
1803 typedef typename iterator_traits<_RandomAccessIterator>::value_type
1806 const _Distance __n = __last - __first;
1807 const _Distance __k = __middle - __first;
1808 const _Distance __l = __n - __k;
1812 std::swap_ranges(__first, __middle, __middle);
1816 const _Distance __d = __gcd(__n, __k);
1818 for (_Distance __i = 0; __i < __d; __i++)
1820 _ValueType __tmp = *__first;
1821 _RandomAccessIterator __p = __first;
1825 for (_Distance __j = 0; __j < __l / __d; __j++)
1827 if (__p > __first + __l)
1829 *__p = *(__p - __l);
1833 *__p = *(__p + __k);
1839 for (_Distance __j = 0; __j < __k / __d - 1; __j ++)
1841 if (__p < __last - __k)
1843 *__p = *(__p + __k);
1846 *__p = * (__p - __l);
1857 * @brief Rotate the elements of a sequence.
1858 * @param first A forward iterator.
1859 * @param middle A forward iterator.
1860 * @param last A forward iterator.
1863 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1864 * positions so that the element at @p middle is moved to @p first, the
1865 * element at @p middle+1 is moved to @first+1 and so on for each element
1866 * in the range @p [first,last).
1868 * This effectively swaps the ranges @p [first,middle) and
1871 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1872 * each @p n in the range @p [0,last-first).
1874 template<typename _ForwardIterator>
1876 rotate(_ForwardIterator __first, _ForwardIterator __middle,
1877 _ForwardIterator __last)
1879 // concept requirements
1880 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
1882 __glibcxx_requires_valid_range(__first, __middle);
1883 __glibcxx_requires_valid_range(__middle, __last);
1885 typedef typename iterator_traits<_ForwardIterator>::iterator_category
1887 std::__rotate(__first, __middle, __last, _IterType());
1891 * @brief Copy a sequence, rotating its elements.
1892 * @param first A forward iterator.
1893 * @param middle A forward iterator.
1894 * @param last A forward iterator.
1895 * @param result An output iterator.
1896 * @return An iterator designating the end of the resulting sequence.
1898 * Copies the elements of the range @p [first,last) to the range
1899 * beginning at @result, rotating the copied elements by @p (middle-first)
1900 * positions so that the element at @p middle is moved to @p result, the
1901 * element at @p middle+1 is moved to @result+1 and so on for each element
1902 * in the range @p [first,last).
1904 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1905 * each @p n in the range @p [0,last-first).
1907 template<typename _ForwardIterator, typename _OutputIterator>
1909 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle,
1910 _ForwardIterator __last, _OutputIterator __result)
1912 // concept requirements
1913 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1914 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
1915 typename iterator_traits<_ForwardIterator>::value_type>)
1916 __glibcxx_requires_valid_range(__first, __middle);
1917 __glibcxx_requires_valid_range(__middle, __last);
1919 return std::copy(__first, __middle,
1920 std::copy(__middle, __last, __result));
1924 * @brief Randomly shuffle the elements of a sequence.
1925 * @param first A forward iterator.
1926 * @param last A forward iterator.
1929 * Reorder the elements in the range @p [first,last) using a random
1930 * distribution, so that every possible ordering of the sequence is
1933 template<typename _RandomAccessIterator>
1935 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
1937 // concept requirements
1938 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
1939 _RandomAccessIterator>)
1940 __glibcxx_requires_valid_range(__first, __last);
1942 if (__first != __last)
1943 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
1944 std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1)));
1948 * @brief Shuffle the elements of a sequence using a random number
1950 * @param first A forward iterator.
1951 * @param last A forward iterator.
1952 * @param rand The RNG functor or function.
1955 * Reorders the elements in the range @p [first,last) using @p rand to
1956 * provide a random distribution. Calling @p rand(N) for a positive
1957 * integer @p N should return a randomly chosen integer from the
1960 template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
1962 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
1963 _RandomNumberGenerator& __rand)
1965 // concept requirements
1966 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
1967 _RandomAccessIterator>)
1968 __glibcxx_requires_valid_range(__first, __last);
1970 if (__first == __last)
1972 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
1973 std::iter_swap(__i, __first + __rand((__i - __first) + 1));
1979 * This is a helper function...
1982 template<typename _ForwardIterator, typename _Predicate>
1984 __partition(_ForwardIterator __first, _ForwardIterator __last,
1986 forward_iterator_tag)
1988 if (__first == __last)
1991 while (__pred(*__first))
1992 if (++__first == __last)
1995 _ForwardIterator __next = __first;
1997 while (++__next != __last)
1998 if (__pred(*__next))
2000 swap(*__first, *__next);
2009 * This is a helper function...
2012 template<typename _BidirectionalIterator, typename _Predicate>
2013 _BidirectionalIterator
2014 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last,
2016 bidirectional_iterator_tag)
2021 if (__first == __last)
2023 else if (__pred(*__first))
2029 if (__first == __last)
2031 else if (!bool(__pred(*__last)))
2035 std::iter_swap(__first, __last);
2041 * @brief Move elements for which a predicate is true to the beginning
2043 * @param first A forward iterator.
2044 * @param last A forward iterator.
2045 * @param pred A predicate functor.
2046 * @return An iterator @p middle such that @p pred(i) is true for each
2047 * iterator @p i in the range @p [first,middle) and false for each @p i
2048 * in the range @p [middle,last).
2050 * @p pred must not modify its operand. @p partition() does not preserve
2051 * the relative ordering of elements in each group, use
2052 * @p stable_partition() if this is needed.
2054 template<typename _ForwardIterator, typename _Predicate>
2055 inline _ForwardIterator
2056 partition(_ForwardIterator __first, _ForwardIterator __last,
2059 // concept requirements
2060 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
2062 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
2063 typename iterator_traits<_ForwardIterator>::value_type>)
2064 __glibcxx_requires_valid_range(__first, __last);
2066 return std::__partition(__first, __last, __pred,
2067 std::__iterator_category(__first));
2073 * This is a helper function...
2076 template<typename _ForwardIterator, typename _Predicate, typename _Distance>
2078 __inplace_stable_partition(_ForwardIterator __first,
2079 _ForwardIterator __last,
2080 _Predicate __pred, _Distance __len)
2083 return __pred(*__first) ? __last : __first;
2084 _ForwardIterator __middle = __first;
2085 std::advance(__middle, __len / 2);
2086 _ForwardIterator __begin = std::__inplace_stable_partition(__first,
2090 _ForwardIterator __end = std::__inplace_stable_partition(__middle, __last,
2094 std::rotate(__begin, __middle, __end);
2095 std::advance(__begin, std::distance(__middle, __end));
2101 * This is a helper function...
2104 template<typename _ForwardIterator, typename _Pointer, typename _Predicate,
2107 __stable_partition_adaptive(_ForwardIterator __first,
2108 _ForwardIterator __last,
2109 _Predicate __pred, _Distance __len,
2111 _Distance __buffer_size)
2113 if (__len <= __buffer_size)
2115 _ForwardIterator __result1 = __first;
2116 _Pointer __result2 = __buffer;
2117 for ( ; __first != __last ; ++__first)
2118 if (__pred(*__first))
2120 *__result1 = *__first;
2125 *__result2 = *__first;
2128 std::copy(__buffer, __result2, __result1);
2133 _ForwardIterator __middle = __first;
2134 std::advance(__middle, __len / 2);
2135 _ForwardIterator __begin =
2136 std::__stable_partition_adaptive(__first, __middle, __pred,
2137 __len / 2, __buffer,
2139 _ForwardIterator __end =
2140 std::__stable_partition_adaptive(__middle, __last, __pred,
2142 __buffer, __buffer_size);
2143 std::rotate(__begin, __middle, __end);
2144 std::advance(__begin, std::distance(__middle, __end));
2150 * @brief Move elements for which a predicate is true to the beginning
2151 * of a sequence, preserving relative ordering.
2152 * @param first A forward iterator.
2153 * @param last A forward iterator.
2154 * @param pred A predicate functor.
2155 * @return An iterator @p middle such that @p pred(i) is true for each
2156 * iterator @p i in the range @p [first,middle) and false for each @p i
2157 * in the range @p [middle,last).
2159 * Performs the same function as @p partition() with the additional
2160 * guarantee that the relative ordering of elements in each group is
2161 * preserved, so any two elements @p x and @p y in the range
2162 * @p [first,last) such that @p pred(x)==pred(y) will have the same
2163 * relative ordering after calling @p stable_partition().
2165 template<typename _ForwardIterator, typename _Predicate>
2167 stable_partition(_ForwardIterator __first, _ForwardIterator __last,
2170 // concept requirements
2171 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
2173 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
2174 typename iterator_traits<_ForwardIterator>::value_type>)
2175 __glibcxx_requires_valid_range(__first, __last);
2177 if (__first == __last)
2181 typedef typename iterator_traits<_ForwardIterator>::value_type
2183 typedef typename iterator_traits<_ForwardIterator>::difference_type
2186 _Temporary_buffer<_ForwardIterator, _ValueType> __buf(__first,
2188 if (__buf.size() > 0)
2190 std::__stable_partition_adaptive(__first, __last, __pred,
2191 _DistanceType(__buf.requested_size()),
2193 _DistanceType(__buf.size()));
2196 std::__inplace_stable_partition(__first, __last, __pred,
2197 _DistanceType(__buf.requested_size()));
2203 * This is a helper function...
2206 template<typename _RandomAccessIterator, typename _Tp>
2207 _RandomAccessIterator
2208 __unguarded_partition(_RandomAccessIterator __first,
2209 _RandomAccessIterator __last, _Tp __pivot)
2213 while (*__first < __pivot)
2216 while (__pivot < *__last)
2218 if (!(__first < __last))
2220 std::iter_swap(__first, __last);
2227 * This is a helper function...
2230 template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
2231 _RandomAccessIterator
2232 __unguarded_partition(_RandomAccessIterator __first,
2233 _RandomAccessIterator __last,
2234 _Tp __pivot, _Compare __comp)
2238 while (__comp(*__first, __pivot))
2241 while (__comp(__pivot, *__last))
2243 if (!(__first < __last))
2245 std::iter_swap(__first, __last);
2253 * This controls some aspect of the sort routines.
2256 enum { _S_threshold = 16 };
2260 * This is a helper function for the sort routine.
2263 template<typename _RandomAccessIterator, typename _Tp>
2265 __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val)
2267 _RandomAccessIterator __next = __last;
2269 while (__val < *__next)
2280 * This is a helper function for the sort routine.
2283 template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
2285 __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val,
2288 _RandomAccessIterator __next = __last;
2290 while (__comp(__val, *__next))
2301 * This is a helper function for the sort routine.
2304 template<typename _RandomAccessIterator>
2306 __insertion_sort(_RandomAccessIterator __first,
2307 _RandomAccessIterator __last)
2309 if (__first == __last)
2312 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
2314 typename iterator_traits<_RandomAccessIterator>::value_type
2316 if (__val < *__first)
2318 std::copy_backward(__first, __i, __i + 1);
2322 std::__unguarded_linear_insert(__i, __val);
2328 * This is a helper function for the sort routine.
2331 template<typename _RandomAccessIterator, typename _Compare>
2333 __insertion_sort(_RandomAccessIterator __first,
2334 _RandomAccessIterator __last, _Compare __comp)
2336 if (__first == __last) return;
2338 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
2340 typename iterator_traits<_RandomAccessIterator>::value_type
2342 if (__comp(__val, *__first))
2344 std::copy_backward(__first, __i, __i + 1);
2348 std::__unguarded_linear_insert(__i, __val, __comp);
2354 * This is a helper function for the sort routine.
2357 template<typename _RandomAccessIterator>
2359 __unguarded_insertion_sort(_RandomAccessIterator __first,
2360 _RandomAccessIterator __last)
2362 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2365 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
2366 std::__unguarded_linear_insert(__i, _ValueType(*__i));
2371 * This is a helper function for the sort routine.
2374 template<typename _RandomAccessIterator, typename _Compare>
2376 __unguarded_insertion_sort(_RandomAccessIterator __first,
2377 _RandomAccessIterator __last, _Compare __comp)
2379 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2382 for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
2383 std::__unguarded_linear_insert(__i, _ValueType(*__i), __comp);
2388 * This is a helper function for the sort routine.
2391 template<typename _RandomAccessIterator>
2393 __final_insertion_sort(_RandomAccessIterator __first,
2394 _RandomAccessIterator __last)
2396 if (__last - __first > int(_S_threshold))
2398 std::__insertion_sort(__first, __first + int(_S_threshold));
2399 std::__unguarded_insertion_sort(__first + int(_S_threshold), __last);
2402 std::__insertion_sort(__first, __last);
2407 * This is a helper function for the sort routine.
2410 template<typename _RandomAccessIterator, typename _Compare>
2412 __final_insertion_sort(_RandomAccessIterator __first,
2413 _RandomAccessIterator __last, _Compare __comp)
2415 if (__last - __first > int(_S_threshold))
2417 std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
2418 std::__unguarded_insertion_sort(__first + int(_S_threshold), __last,
2422 std::__insertion_sort(__first, __last, __comp);
2427 * This is a helper function for the sort routines.
2430 template<typename _RandomAccessIterator>
2432 __heap_select(_RandomAccessIterator __first,
2433 _RandomAccessIterator __middle,
2434 _RandomAccessIterator __last)
2436 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2439 std::make_heap(__first, __middle);
2440 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
2441 if (*__i < *__first)
2442 std::__pop_heap(__first, __middle, __i, _ValueType(*__i));
2447 * This is a helper function for the sort routines.
2450 template<typename _RandomAccessIterator, typename _Compare>
2452 __heap_select(_RandomAccessIterator __first,
2453 _RandomAccessIterator __middle,
2454 _RandomAccessIterator __last, _Compare __comp)
2456 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2459 std::make_heap(__first, __middle, __comp);
2460 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
2461 if (__comp(*__i, *__first))
2462 std::__pop_heap(__first, __middle, __i, _ValueType(*__i), __comp);
2467 * This is a helper function for the sort routines.
2470 template<typename _Size>
2475 for (__k = 0; __n != 1; __n >>= 1)
2481 * @brief Sort the smallest elements of a sequence.
2482 * @param first An iterator.
2483 * @param middle Another iterator.
2484 * @param last Another iterator.
2487 * Sorts the smallest @p (middle-first) elements in the range
2488 * @p [first,last) and moves them to the range @p [first,middle). The
2489 * order of the remaining elements in the range @p [middle,last) is
2491 * After the sort if @p i and @j are iterators in the range
2492 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2493 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
2495 template<typename _RandomAccessIterator>
2497 partial_sort(_RandomAccessIterator __first,
2498 _RandomAccessIterator __middle,
2499 _RandomAccessIterator __last)
2501 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2504 // concept requirements
2505 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
2506 _RandomAccessIterator>)
2507 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
2508 __glibcxx_requires_valid_range(__first, __middle);
2509 __glibcxx_requires_valid_range(__middle, __last);
2511 std::__heap_select(__first, __middle, __last);
2512 std::sort_heap(__first, __middle);
2516 * @brief Sort the smallest elements of a sequence using a predicate
2518 * @param first An iterator.
2519 * @param middle Another iterator.
2520 * @param last Another iterator.
2521 * @param comp A comparison functor.
2524 * Sorts the smallest @p (middle-first) elements in the range
2525 * @p [first,last) and moves them to the range @p [first,middle). The
2526 * order of the remaining elements in the range @p [middle,last) is
2528 * After the sort if @p i and @j are iterators in the range
2529 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2530 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
2533 template<typename _RandomAccessIterator, typename _Compare>
2535 partial_sort(_RandomAccessIterator __first,
2536 _RandomAccessIterator __middle,
2537 _RandomAccessIterator __last,
2540 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2543 // concept requirements
2544 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
2545 _RandomAccessIterator>)
2546 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2547 _ValueType, _ValueType>)
2548 __glibcxx_requires_valid_range(__first, __middle);
2549 __glibcxx_requires_valid_range(__middle, __last);
2551 std::__heap_select(__first, __middle, __last, __comp);
2552 std::sort_heap(__first, __middle, __comp);
2556 * @brief Copy the smallest elements of a sequence.
2557 * @param first An iterator.
2558 * @param last Another iterator.
2559 * @param result_first A random-access iterator.
2560 * @param result_last Another random-access iterator.
2561 * @return An iterator indicating the end of the resulting sequence.
2563 * Copies and sorts the smallest N values from the range @p [first,last)
2564 * to the range beginning at @p result_first, where the number of
2565 * elements to be copied, @p N, is the smaller of @p (last-first) and
2566 * @p (result_last-result_first).
2567 * After the sort if @p i and @j are iterators in the range
2568 * @p [result_first,result_first+N) such that @i precedes @j then
2569 * @p *j<*i is false.
2570 * The value returned is @p result_first+N.
2572 template<typename _InputIterator, typename _RandomAccessIterator>
2573 _RandomAccessIterator
2574 partial_sort_copy(_InputIterator __first, _InputIterator __last,
2575 _RandomAccessIterator __result_first,
2576 _RandomAccessIterator __result_last)
2578 typedef typename iterator_traits<_InputIterator>::value_type
2580 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2582 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
2585 // concept requirements
2586 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
2587 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
2589 __glibcxx_function_requires(_LessThanOpConcept<_InputValueType,
2591 __glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>)
2592 __glibcxx_requires_valid_range(__first, __last);
2593 __glibcxx_requires_valid_range(__result_first, __result_last);
2595 if (__result_first == __result_last)
2596 return __result_last;
2597 _RandomAccessIterator __result_real_last = __result_first;
2598 while(__first != __last && __result_real_last != __result_last)
2600 *__result_real_last = *__first;
2601 ++__result_real_last;
2604 std::make_heap(__result_first, __result_real_last);
2605 while (__first != __last)
2607 if (*__first < *__result_first)
2608 std::__adjust_heap(__result_first, _DistanceType(0),
2609 _DistanceType(__result_real_last
2611 _InputValueType(*__first));
2614 std::sort_heap(__result_first, __result_real_last);
2615 return __result_real_last;
2619 * @brief Copy the smallest elements of a sequence using a predicate for
2621 * @param first An input iterator.
2622 * @param last Another input iterator.
2623 * @param result_first A random-access iterator.
2624 * @param result_last Another random-access iterator.
2625 * @param comp A comparison functor.
2626 * @return An iterator indicating the end of the resulting sequence.
2628 * Copies and sorts the smallest N values from the range @p [first,last)
2629 * to the range beginning at @p result_first, where the number of
2630 * elements to be copied, @p N, is the smaller of @p (last-first) and
2631 * @p (result_last-result_first).
2632 * After the sort if @p i and @j are iterators in the range
2633 * @p [result_first,result_first+N) such that @i precedes @j then
2634 * @p comp(*j,*i) is false.
2635 * The value returned is @p result_first+N.
2637 template<typename _InputIterator, typename _RandomAccessIterator, typename _Compare>
2638 _RandomAccessIterator
2639 partial_sort_copy(_InputIterator __first, _InputIterator __last,
2640 _RandomAccessIterator __result_first,
2641 _RandomAccessIterator __result_last,
2644 typedef typename iterator_traits<_InputIterator>::value_type
2646 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2648 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
2651 // concept requirements
2652 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
2653 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
2654 _RandomAccessIterator>)
2655 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
2657 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2658 _InputValueType, _OutputValueType>)
2659 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2660 _OutputValueType, _OutputValueType>)
2661 __glibcxx_requires_valid_range(__first, __last);
2662 __glibcxx_requires_valid_range(__result_first, __result_last);
2664 if (__result_first == __result_last)
2665 return __result_last;
2666 _RandomAccessIterator __result_real_last = __result_first;
2667 while(__first != __last && __result_real_last != __result_last)
2669 *__result_real_last = *__first;
2670 ++__result_real_last;
2673 std::make_heap(__result_first, __result_real_last, __comp);
2674 while (__first != __last)
2676 if (__comp(*__first, *__result_first))
2677 std::__adjust_heap(__result_first, _DistanceType(0),
2678 _DistanceType(__result_real_last
2680 _InputValueType(*__first),
2684 std::sort_heap(__result_first, __result_real_last, __comp);
2685 return __result_real_last;
2690 * This is a helper function for the sort routine.
2693 template<typename _RandomAccessIterator, typename _Size>
2695 __introsort_loop(_RandomAccessIterator __first,
2696 _RandomAccessIterator __last,
2697 _Size __depth_limit)
2699 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2702 while (__last - __first > int(_S_threshold))
2704 if (__depth_limit == 0)
2706 std::partial_sort(__first, __last, __last);
2710 _RandomAccessIterator __cut =
2711 std::__unguarded_partition(__first, __last,
2712 _ValueType(std::__median(*__first,
2719 std::__introsort_loop(__cut, __last, __depth_limit);
2726 * This is a helper function for the sort routine.
2729 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
2731 __introsort_loop(_RandomAccessIterator __first,
2732 _RandomAccessIterator __last,
2733 _Size __depth_limit, _Compare __comp)
2735 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2738 while (__last - __first > int(_S_threshold))
2740 if (__depth_limit == 0)
2742 std::partial_sort(__first, __last, __last, __comp);
2746 _RandomAccessIterator __cut =
2747 std::__unguarded_partition(__first, __last,
2748 _ValueType(std::__median(*__first,
2756 std::__introsort_loop(__cut, __last, __depth_limit, __comp);
2762 * @brief Sort the elements of a sequence.
2763 * @param first An iterator.
2764 * @param last Another iterator.
2767 * Sorts the elements in the range @p [first,last) in ascending order,
2768 * such that @p *(i+1)<*i is false for each iterator @p i in the range
2769 * @p [first,last-1).
2771 * The relative ordering of equivalent elements is not preserved, use
2772 * @p stable_sort() if this is needed.
2774 template<typename _RandomAccessIterator>
2776 sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
2778 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2781 // concept requirements
2782 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
2783 _RandomAccessIterator>)
2784 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
2785 __glibcxx_requires_valid_range(__first, __last);
2787 if (__first != __last)
2789 std::__introsort_loop(__first, __last,
2790 std::__lg(__last - __first) * 2);
2791 std::__final_insertion_sort(__first, __last);
2796 * @brief Sort the elements of a sequence using a predicate for comparison.
2797 * @param first An iterator.
2798 * @param last Another iterator.
2799 * @param comp A comparison functor.
2802 * Sorts the elements in the range @p [first,last) in ascending order,
2803 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
2804 * range @p [first,last-1).
2806 * The relative ordering of equivalent elements is not preserved, use
2807 * @p stable_sort() if this is needed.
2809 template<typename _RandomAccessIterator, typename _Compare>
2811 sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
2814 typedef typename iterator_traits<_RandomAccessIterator>::value_type
2817 // concept requirements
2818 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
2819 _RandomAccessIterator>)
2820 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType,
2822 __glibcxx_requires_valid_range(__first, __last);
2824 if (__first != __last)
2826 std::__introsort_loop(__first, __last,
2827 std::__lg(__last - __first) * 2, __comp);
2828 std::__final_insertion_sort(__first, __last, __comp);
2833 * @brief Finds the first position in which @a val could be inserted
2834 * without changing the ordering.
2835 * @param first An iterator.
2836 * @param last Another iterator.
2837 * @param val The search term.
2838 * @return An iterator pointing to the first element "not less than" @a val,
2839 * or end() if every element is less than @a val.
2840 * @ingroup binarysearch
2842 template<typename _ForwardIterator, typename _Tp>
2844 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
2847 typedef typename iterator_traits<_ForwardIterator>::value_type
2849 typedef typename iterator_traits<_ForwardIterator>::difference_type
2852 // concept requirements
2853 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2854 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
2855 __glibcxx_requires_partitioned(__first, __last, __val);
2857 _DistanceType __len = std::distance(__first, __last);
2858 _DistanceType __half;
2859 _ForwardIterator __middle;
2863 __half = __len >> 1;
2865 std::advance(__middle, __half);
2866 if (*__middle < __val)
2870 __len = __len - __half - 1;
2879 * @brief Finds the first position in which @a val could be inserted
2880 * without changing the ordering.
2881 * @param first An iterator.
2882 * @param last Another iterator.
2883 * @param val The search term.
2884 * @param comp A functor to use for comparisons.
2885 * @return An iterator pointing to the first element "not less than" @a val,
2886 * or end() if every element is less than @a val.
2887 * @ingroup binarysearch
2889 * The comparison function should have the same effects on ordering as
2890 * the function used for the initial sort.
2892 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2894 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
2895 const _Tp& __val, _Compare __comp)
2897 typedef typename iterator_traits<_ForwardIterator>::value_type
2899 typedef typename iterator_traits<_ForwardIterator>::difference_type
2902 // concept requirements
2903 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2904 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2906 __glibcxx_requires_partitioned_pred(__first, __last, __val, __comp);
2908 _DistanceType __len = std::distance(__first, __last);
2909 _DistanceType __half;
2910 _ForwardIterator __middle;
2914 __half = __len >> 1;
2916 std::advance(__middle, __half);
2917 if (__comp(*__middle, __val))
2921 __len = __len - __half - 1;
2930 * @brief Finds the last position in which @a val could be inserted
2931 * without changing the ordering.
2932 * @param first An iterator.
2933 * @param last Another iterator.
2934 * @param val The search term.
2935 * @return An iterator pointing to the first element greater than @a val,
2936 * or end() if no elements are greater than @a val.
2937 * @ingroup binarysearch
2939 template<typename _ForwardIterator, typename _Tp>
2941 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2944 typedef typename iterator_traits<_ForwardIterator>::value_type
2946 typedef typename iterator_traits<_ForwardIterator>::difference_type
2949 // concept requirements
2950 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
2951 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
2952 __glibcxx_requires_partitioned(__first, __last, __val);
2954 _DistanceType __len = std::distance(__first, __last);
2955 _DistanceType __half;
2956 _ForwardIterator __middle;
2960 __half = __len >> 1;
2962 std::advance(__middle, __half);
2963 if (__val < *__middle)
2969 __len = __len - __half - 1;
2976 * @brief Finds the last position in which @a val could be inserted
2977 * without changing the ordering.
2978 * @param first An iterator.
2979 * @param last Another iterator.
2980 * @param val The search term.
2981 * @param comp A functor to use for comparisons.
2982 * @return An iterator pointing to the first element greater than @a val,
2983 * or end() if no elements are greater than @a val.
2984 * @ingroup binarysearch
2986 * The comparison function should have the same effects on ordering as
2987 * the function used for the initial sort.
2989 template<typename _ForwardIterator, typename _Tp, typename _Compare>
2991 upper_bound(_ForwardIterator __first, _ForwardIterator __last,
2992 const _Tp& __val, _Compare __comp)
2994 typedef typename iterator_traits<_ForwardIterator>::value_type
2996 typedef typename iterator_traits<_ForwardIterator>::difference_type
2999 // concept requirements
3000 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
3001 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3003 __glibcxx_requires_partitioned_pred(__first, __last, __val, __comp);
3005 _DistanceType __len = std::distance(__first, __last);
3006 _DistanceType __half;
3007 _ForwardIterator __middle;
3011 __half = __len >> 1;
3013 std::advance(__middle, __half);
3014 if (__comp(__val, *__middle))
3020 __len = __len - __half - 1;
3028 * This is a helper function for the merge routines.
3031 template<typename _BidirectionalIterator, typename _Distance>
3033 __merge_without_buffer(_BidirectionalIterator __first,
3034 _BidirectionalIterator __middle,
3035 _BidirectionalIterator __last,
3036 _Distance __len1, _Distance __len2)
3038 if (__len1 == 0 || __len2 == 0)
3040 if (__len1 + __len2 == 2)
3042 if (*__middle < *__first)
3043 std::iter_swap(__first, __middle);
3046 _BidirectionalIterator __first_cut = __first;
3047 _BidirectionalIterator __second_cut = __middle;
3048 _Distance __len11 = 0;
3049 _Distance __len22 = 0;
3050 if (__len1 > __len2)
3052 __len11 = __len1 / 2;
3053 std::advance(__first_cut, __len11);
3054 __second_cut = std::lower_bound(__middle, __last, *__first_cut);
3055 __len22 = std::distance(__middle, __second_cut);
3059 __len22 = __len2 / 2;
3060 std::advance(__second_cut, __len22);
3061 __first_cut = std::upper_bound(__first, __middle, *__second_cut);
3062 __len11 = std::distance(__first, __first_cut);
3064 std::rotate(__first_cut, __middle, __second_cut);
3065 _BidirectionalIterator __new_middle = __first_cut;
3066 std::advance(__new_middle, std::distance(__middle, __second_cut));
3067 std::__merge_without_buffer(__first, __first_cut, __new_middle,
3069 std::__merge_without_buffer(__new_middle, __second_cut, __last,
3070 __len1 - __len11, __len2 - __len22);
3075 * This is a helper function for the merge routines.
3078 template<typename _BidirectionalIterator, typename _Distance,
3081 __merge_without_buffer(_BidirectionalIterator __first,
3082 _BidirectionalIterator __middle,
3083 _BidirectionalIterator __last,
3084 _Distance __len1, _Distance __len2,
3087 if (__len1 == 0 || __len2 == 0)
3089 if (__len1 + __len2 == 2)
3091 if (__comp(*__middle, *__first))
3092 std::iter_swap(__first, __middle);
3095 _BidirectionalIterator __first_cut = __first;
3096 _BidirectionalIterator __second_cut = __middle;
3097 _Distance __len11 = 0;
3098 _Distance __len22 = 0;
3099 if (__len1 > __len2)
3101 __len11 = __len1 / 2;
3102 std::advance(__first_cut, __len11);
3103 __second_cut = std::lower_bound(__middle, __last, *__first_cut,
3105 __len22 = std::distance(__middle, __second_cut);
3109 __len22 = __len2 / 2;
3110 std::advance(__second_cut, __len22);
3111 __first_cut = std::upper_bound(__first, __middle, *__second_cut,
3113 __len11 = std::distance(__first, __first_cut);
3115 std::rotate(__first_cut, __middle, __second_cut);
3116 _BidirectionalIterator __new_middle = __first_cut;
3117 std::advance(__new_middle, std::distance(__middle, __second_cut));
3118 std::__merge_without_buffer(__first, __first_cut, __new_middle,
3119 __len11, __len22, __comp);
3120 std::__merge_without_buffer(__new_middle, __second_cut, __last,
3121 __len1 - __len11, __len2 - __len22, __comp);
3126 * This is a helper function for the stable sorting routines.
3129 template<typename _RandomAccessIterator>
3131 __inplace_stable_sort(_RandomAccessIterator __first,
3132 _RandomAccessIterator __last)
3134 if (__last - __first < 15)
3136 std::__insertion_sort(__first, __last);
3139 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
3140 std::__inplace_stable_sort(__first, __middle);
3141 std::__inplace_stable_sort(__middle, __last);
3142 std::__merge_without_buffer(__first, __middle, __last,
3149 * This is a helper function for the stable sorting routines.
3152 template<typename _RandomAccessIterator, typename _Compare>
3154 __inplace_stable_sort(_RandomAccessIterator __first,
3155 _RandomAccessIterator __last, _Compare __comp)
3157 if (__last - __first < 15)
3159 std::__insertion_sort(__first, __last, __comp);
3162 _RandomAccessIterator __middle = __first + (__last - __first) / 2;
3163 std::__inplace_stable_sort(__first, __middle, __comp);
3164 std::__inplace_stable_sort(__middle, __last, __comp);
3165 std::__merge_without_buffer(__first, __middle, __last,
3172 * @brief Merges two sorted ranges.
3173 * @param first1 An iterator.
3174 * @param first2 Another iterator.
3175 * @param last1 Another iterator.
3176 * @param last2 Another iterator.
3177 * @param result An iterator pointing to the end of the merged range.
3178 * @return An iterator pointing to the first element "not less than" @a val.
3180 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
3181 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
3182 * must be sorted, and the output range must not overlap with either of
3183 * the input ranges. The sort is @e stable, that is, for equivalent
3184 * elements in the two ranges, elements from the first range will always
3185 * come before elements from the second.
3187 template<typename _InputIterator1, typename _InputIterator2,
3188 typename _OutputIterator>
3190 merge(_InputIterator1 __first1, _InputIterator1 __last1,
3191 _InputIterator2 __first2, _InputIterator2 __last2,
3192 _OutputIterator __result)
3194 typedef typename iterator_traits<_InputIterator1>::value_type
3196 typedef typename iterator_traits<_InputIterator2>::value_type
3199 // concept requirements
3200 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
3201 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
3202 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3204 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3206 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
3207 __glibcxx_requires_sorted(__first1, __last1);
3208 __glibcxx_requires_sorted(__first2, __last2);
3210 while (__first1 != __last1 && __first2 != __last2)
3212 if (*__first2 < *__first1)
3214 *__result = *__first2;
3219 *__result = *__first1;
3224 return std::copy(__first2, __last2, std::copy(__first1, __last1,
3229 * @brief Merges two sorted ranges.
3230 * @param first1 An iterator.
3231 * @param first2 Another iterator.
3232 * @param last1 Another iterator.
3233 * @param last2 Another iterator.
3234 * @param result An iterator pointing to the end of the merged range.
3235 * @param comp A functor to use for comparisons.
3236 * @return An iterator pointing to the first element "not less than" @a val.
3238 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
3239 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
3240 * must be sorted, and the output range must not overlap with either of
3241 * the input ranges. The sort is @e stable, that is, for equivalent
3242 * elements in the two ranges, elements from the first range will always
3243 * come before elements from the second.
3245 * The comparison function should have the same effects on ordering as
3246 * the function used for the initial sort.
3248 template<typename _InputIterator1, typename _InputIterator2,
3249 typename _OutputIterator, typename _Compare>
3251 merge(_InputIterator1 __first1, _InputIterator1 __last1,
3252 _InputIterator2 __first2, _InputIterator2 __last2,
3253 _OutputIterator __result, _Compare __comp)
3255 typedef typename iterator_traits<_InputIterator1>::value_type
3257 typedef typename iterator_traits<_InputIterator2>::value_type
3260 // concept requirements
3261 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
3262 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
3263 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3265 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
3267 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3268 _ValueType2, _ValueType1>)
3269 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
3270 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
3272 while (__first1 != __last1 && __first2 != __last2)
3274 if (__comp(*__first2, *__first1))
3276 *__result = *__first2;
3281 *__result = *__first1;
3286 return std::copy(__first2, __last2, std::copy(__first1, __last1,
3290 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
3293 __merge_sort_loop(_RandomAccessIterator1 __first,
3294 _RandomAccessIterator1 __last,
3295 _RandomAccessIterator2 __result,
3296 _Distance __step_size)
3298 const _Distance __two_step = 2 * __step_size;
3300 while (__last - __first >= __two_step)
3302 __result = std::merge(__first, __first + __step_size,
3303 __first + __step_size, __first + __two_step,
3305 __first += __two_step;
3308 __step_size = std::min(_Distance(__last - __first), __step_size);
3309 std::merge(__first, __first + __step_size, __first + __step_size, __last,
3313 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
3314 typename _Distance, typename _Compare>
3316 __merge_sort_loop(_RandomAccessIterator1 __first,
3317 _RandomAccessIterator1 __last,
3318 _RandomAccessIterator2 __result, _Distance __step_size,
3321 const _Distance __two_step = 2 * __step_size;
3323 while (__last - __first >= __two_step)
3325 __result = std::merge(__first, __first + __step_size,
3326 __first + __step_size, __first + __two_step,
3329 __first += __two_step;
3331 __step_size = std::min(_Distance(__last - __first), __step_size);
3333 std::merge(__first, __first + __step_size,
3334 __first + __step_size, __last,
3339 enum { _S_chunk_size = 7 };
3341 template<typename _RandomAccessIterator, typename _Distance>
3343 __chunk_insertion_sort(_RandomAccessIterator __first,
3344 _RandomAccessIterator __last,
3345 _Distance __chunk_size)
3347 while (__last - __first >= __chunk_size)
3349 std::__insertion_sort(__first, __first + __chunk_size);
3350 __first += __chunk_size;
3352 std::__insertion_sort(__first, __last);
3355 template<typename _RandomAccessIterator, typename _Distance, typename _Compare>
3357 __chunk_insertion_sort(_RandomAccessIterator __first,
3358 _RandomAccessIterator __last,
3359 _Distance __chunk_size, _Compare __comp)
3361 while (__last - __first >= __chunk_size)
3363 std::__insertion_sort(__first, __first + __chunk_size, __comp);
3364 __first += __chunk_size;
3366 std::__insertion_sort(__first, __last, __comp);
3369 template<typename _RandomAccessIterator, typename _Pointer>
3371 __merge_sort_with_buffer(_RandomAccessIterator __first,
3372 _RandomAccessIterator __last,
3375 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3378 const _Distance __len = __last - __first;
3379 const _Pointer __buffer_last = __buffer + __len;
3381 _Distance __step_size = _S_chunk_size;
3382 std::__chunk_insertion_sort(__first, __last, __step_size);
3384 while (__step_size < __len)
3386 std::__merge_sort_loop(__first, __last, __buffer, __step_size);
3388 std::__merge_sort_loop(__buffer, __buffer_last, __first, __step_size);
3393 template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
3395 __merge_sort_with_buffer(_RandomAccessIterator __first,
3396 _RandomAccessIterator __last,
3397 _Pointer __buffer, _Compare __comp)
3399 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3402 const _Distance __len = __last - __first;
3403 const _Pointer __buffer_last = __buffer + __len;
3405 _Distance __step_size = _S_chunk_size;
3406 std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
3408 while (__step_size < __len)
3410 std::__merge_sort_loop(__first, __last, __buffer,
3411 __step_size, __comp);
3413 std::__merge_sort_loop(__buffer, __buffer_last, __first,
3414 __step_size, __comp);
3421 * This is a helper function for the merge routines.
3424 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
3425 typename _BidirectionalIterator3>
3426 _BidirectionalIterator3
3427 __merge_backward(_BidirectionalIterator1 __first1,
3428 _BidirectionalIterator1 __last1,
3429 _BidirectionalIterator2 __first2,
3430 _BidirectionalIterator2 __last2,
3431 _BidirectionalIterator3 __result)
3433 if (__first1 == __last1)
3434 return std::copy_backward(__first2, __last2, __result);
3435 if (__first2 == __last2)
3436 return std::copy_backward(__first1, __last1, __result);
3441 if (*__last2 < *__last1)
3443 *--__result = *__last1;
3444 if (__first1 == __last1)
3445 return std::copy_backward(__first2, ++__last2, __result);
3450 *--__result = *__last2;
3451 if (__first2 == __last2)
3452 return std::copy_backward(__first1, ++__last1, __result);
3460 * This is a helper function for the merge routines.
3463 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
3464 typename _BidirectionalIterator3, typename _Compare>
3465 _BidirectionalIterator3
3466 __merge_backward(_BidirectionalIterator1 __first1,
3467 _BidirectionalIterator1 __last1,
3468 _BidirectionalIterator2 __first2,
3469 _BidirectionalIterator2 __last2,
3470 _BidirectionalIterator3 __result,
3473 if (__first1 == __last1)
3474 return std::copy_backward(__first2, __last2, __result);
3475 if (__first2 == __last2)
3476 return std::copy_backward(__first1, __last1, __result);
3481 if (__comp(*__last2, *__last1))
3483 *--__result = *__last1;
3484 if (__first1 == __last1)
3485 return std::copy_backward(__first2, ++__last2, __result);
3490 *--__result = *__last2;
3491 if (__first2 == __last2)
3492 return std::copy_backward(__first1, ++__last1, __result);
3500 * This is a helper function for the merge routines.
3503 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
3505 _BidirectionalIterator1
3506 __rotate_adaptive(_BidirectionalIterator1 __first,
3507 _BidirectionalIterator1 __middle,
3508 _BidirectionalIterator1 __last,
3509 _Distance __len1, _Distance __len2,
3510 _BidirectionalIterator2 __buffer,
3511 _Distance __buffer_size)
3513 _BidirectionalIterator2 __buffer_end;
3514 if (__len1 > __len2 && __len2 <= __buffer_size)
3516 __buffer_end = std::copy(__middle, __last, __buffer);
3517 std::copy_backward(__first, __middle, __last);
3518 return std::copy(__buffer, __buffer_end, __first);
3520 else if (__len1 <= __buffer_size)
3522 __buffer_end = std::copy(__first, __middle, __buffer);
3523 std::copy(__middle, __last, __first);
3524 return std::copy_backward(__buffer, __buffer_end, __last);
3528 std::rotate(__first, __middle, __last);
3529 std::advance(__first, std::distance(__middle, __last));
3536 * This is a helper function for the merge routines.
3539 template<typename _BidirectionalIterator, typename _Distance,
3542 __merge_adaptive(_BidirectionalIterator __first,
3543 _BidirectionalIterator __middle,
3544 _BidirectionalIterator __last,
3545 _Distance __len1, _Distance __len2,
3546 _Pointer __buffer, _Distance __buffer_size)
3548 if (__len1 <= __len2 && __len1 <= __buffer_size)
3550 _Pointer __buffer_end = std::copy(__first, __middle, __buffer);
3551 std::merge(__buffer, __buffer_end, __middle, __last, __first);
3553 else if (__len2 <= __buffer_size)
3555 _Pointer __buffer_end = std::copy(__middle, __last, __buffer);
3556 std::__merge_backward(__first, __middle, __buffer,
3557 __buffer_end, __last);
3561 _BidirectionalIterator __first_cut = __first;
3562 _BidirectionalIterator __second_cut = __middle;
3563 _Distance __len11 = 0;
3564 _Distance __len22 = 0;
3565 if (__len1 > __len2)
3567 __len11 = __len1 / 2;
3568 std::advance(__first_cut, __len11);
3569 __second_cut = std::lower_bound(__middle, __last,
3571 __len22 = std::distance(__middle, __second_cut);
3575 __len22 = __len2 / 2;
3576 std::advance(__second_cut, __len22);
3577 __first_cut = std::upper_bound(__first, __middle,
3579 __len11 = std::distance(__first, __first_cut);
3581 _BidirectionalIterator __new_middle =
3582 std::__rotate_adaptive(__first_cut, __middle, __second_cut,
3583 __len1 - __len11, __len22, __buffer,
3585 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
3586 __len22, __buffer, __buffer_size);
3587 std::__merge_adaptive(__new_middle, __second_cut, __last,
3589 __len2 - __len22, __buffer, __buffer_size);
3595 * This is a helper function for the merge routines.
3598 template<typename _BidirectionalIterator, typename _Distance, typename _Pointer,
3601 __merge_adaptive(_BidirectionalIterator __first,
3602 _BidirectionalIterator __middle,
3603 _BidirectionalIterator __last,
3604 _Distance __len1, _Distance __len2,
3605 _Pointer __buffer, _Distance __buffer_size,
3608 if (__len1 <= __len2 && __len1 <= __buffer_size)
3610 _Pointer __buffer_end = std::copy(__first, __middle, __buffer);
3611 std::merge(__buffer, __buffer_end, __middle, __last, __first, __comp);
3613 else if (__len2 <= __buffer_size)
3615 _Pointer __buffer_end = std::copy(__middle, __last, __buffer);
3616 std::__merge_backward(__first, __middle, __buffer, __buffer_end,
3621 _BidirectionalIterator __first_cut = __first;
3622 _BidirectionalIterator __second_cut = __middle;
3623 _Distance __len11 = 0;
3624 _Distance __len22 = 0;
3625 if (__len1 > __len2)
3627 __len11 = __len1 / 2;
3628 std::advance(__first_cut, __len11);
3629 __second_cut = std::lower_bound(__middle, __last, *__first_cut,
3631 __len22 = std::distance(__middle, __second_cut);
3635 __len22 = __len2 / 2;
3636 std::advance(__second_cut, __len22);
3637 __first_cut = std::upper_bound(__first, __middle, *__second_cut,
3639 __len11 = std::distance(__first, __first_cut);
3641 _BidirectionalIterator __new_middle =
3642 std::__rotate_adaptive(__first_cut, __middle, __second_cut,
3643 __len1 - __len11, __len22, __buffer,
3645 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11,
3646 __len22, __buffer, __buffer_size, __comp);
3647 std::__merge_adaptive(__new_middle, __second_cut, __last,
3649 __len2 - __len22, __buffer,
3650 __buffer_size, __comp);
3655 * @brief Merges two sorted ranges in place.
3656 * @param first An iterator.
3657 * @param middle Another iterator.
3658 * @param last Another iterator.
3661 * Merges two sorted and consecutive ranges, [first,middle) and
3662 * [middle,last), and puts the result in [first,last). The output will
3663 * be sorted. The sort is @e stable, that is, for equivalent
3664 * elements in the two ranges, elements from the first range will always
3665 * come before elements from the second.
3667 * If enough additional memory is available, this takes (last-first)-1
3668 * comparisons. Otherwise an NlogN algorithm is used, where N is
3669 * distance(first,last).
3671 template<typename _BidirectionalIterator>
3673 inplace_merge(_BidirectionalIterator __first,
3674 _BidirectionalIterator __middle,
3675 _BidirectionalIterator __last)
3677 typedef typename iterator_traits<_BidirectionalIterator>::value_type
3679 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
3682 // concept requirements
3683 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
3684 _BidirectionalIterator>)
3685 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
3686 __glibcxx_requires_sorted(__first, __middle);
3687 __glibcxx_requires_sorted(__middle, __last);
3689 if (__first == __middle || __middle == __last)
3692 _DistanceType __len1 = std::distance(__first, __middle);
3693 _DistanceType __len2 = std::distance(__middle, __last);
3695 _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
3697 if (__buf.begin() == 0)
3698 std::__merge_without_buffer(__first, __middle, __last, __len1, __len2);
3700 std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
3701 __buf.begin(), _DistanceType(__buf.size()));
3705 * @brief Merges two sorted ranges in place.
3706 * @param first An iterator.
3707 * @param middle Another iterator.
3708 * @param last Another iterator.
3709 * @param comp A functor to use for comparisons.
3712 * Merges two sorted and consecutive ranges, [first,middle) and
3713 * [middle,last), and puts the result in [first,last). The output will
3714 * be sorted. The sort is @e stable, that is, for equivalent
3715 * elements in the two ranges, elements from the first range will always
3716 * come before elements from the second.
3718 * If enough additional memory is available, this takes (last-first)-1
3719 * comparisons. Otherwise an NlogN algorithm is used, where N is
3720 * distance(first,last).
3722 * The comparison function should have the same effects on ordering as
3723 * the function used for the initial sort.
3725 template<typename _BidirectionalIterator, typename _Compare>
3727 inplace_merge(_BidirectionalIterator __first,
3728 _BidirectionalIterator __middle,
3729 _BidirectionalIterator __last,
3732 typedef typename iterator_traits<_BidirectionalIterator>::value_type
3734 typedef typename iterator_traits<_BidirectionalIterator>::difference_type
3737 // concept requirements
3738 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
3739 _BidirectionalIterator>)
3740 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3741 _ValueType, _ValueType>)
3742 __glibcxx_requires_sorted_pred(__first, __middle, __comp);
3743 __glibcxx_requires_sorted_pred(__middle, __last, __comp);
3745 if (__first == __middle || __middle == __last)
3748 const _DistanceType __len1 = std::distance(__first, __middle);
3749 const _DistanceType __len2 = std::distance(__middle, __last);
3751 _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
3753 if (__buf.begin() == 0)
3754 std::__merge_without_buffer(__first, __middle, __last, __len1,
3757 std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
3758 __buf.begin(), _DistanceType(__buf.size()),
3762 template<typename _RandomAccessIterator, typename _Pointer,
3765 __stable_sort_adaptive(_RandomAccessIterator __first,
3766 _RandomAccessIterator __last,
3767 _Pointer __buffer, _Distance __buffer_size)
3769 const _Distance __len = (__last - __first + 1) / 2;
3770 const _RandomAccessIterator __middle = __first + __len;
3771 if (__len > __buffer_size)
3773 std::__stable_sort_adaptive(__first, __middle,
3774 __buffer, __buffer_size);
3775 std::__stable_sort_adaptive(__middle, __last,
3776 __buffer, __buffer_size);
3780 std::__merge_sort_with_buffer(__first, __middle, __buffer);
3781 std::__merge_sort_with_buffer(__middle, __last, __buffer);
3783 std::__merge_adaptive(__first, __middle, __last,
3784 _Distance(__middle - __first),
3785 _Distance(__last - __middle),
3786 __buffer, __buffer_size);
3789 template<typename _RandomAccessIterator, typename _Pointer,
3790 typename _Distance, typename _Compare>
3792 __stable_sort_adaptive(_RandomAccessIterator __first,
3793 _RandomAccessIterator __last,
3794 _Pointer __buffer, _Distance __buffer_size,
3797 const _Distance __len = (__last - __first + 1) / 2;
3798 const _RandomAccessIterator __middle = __first + __len;
3799 if (__len > __buffer_size)
3801 std::__stable_sort_adaptive(__first, __middle, __buffer,
3802 __buffer_size, __comp);
3803 std::__stable_sort_adaptive(__middle, __last, __buffer,
3804 __buffer_size, __comp);
3808 std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp);
3809 std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp);
3811 std::__merge_adaptive(__first, __middle, __last,
3812 _Distance(__middle - __first),
3813 _Distance(__last - __middle),
3814 __buffer, __buffer_size,
3819 * @brief Sort the elements of a sequence, preserving the relative order
3820 * of equivalent elements.
3821 * @param first An iterator.
3822 * @param last Another iterator.
3825 * Sorts the elements in the range @p [first,last) in ascending order,
3826 * such that @p *(i+1)<*i is false for each iterator @p i in the range
3827 * @p [first,last-1).
3829 * The relative ordering of equivalent elements is preserved, so any two
3830 * elements @p x and @p y in the range @p [first,last) such that
3831 * @p x<y is false and @p y<x is false will have the same relative
3832 * ordering after calling @p stable_sort().
3834 template<typename _RandomAccessIterator>
3836 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
3838 typedef typename iterator_traits<_RandomAccessIterator>::value_type
3840 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3843 // concept requirements
3844 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
3845 _RandomAccessIterator>)
3846 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
3847 __glibcxx_requires_valid_range(__first, __last);
3849 _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
3851 if (__buf.begin() == 0)
3852 std::__inplace_stable_sort(__first, __last);
3854 std::__stable_sort_adaptive(__first, __last, __buf.begin(),
3855 _DistanceType(__buf.size()));
3859 * @brief Sort the elements of a sequence using a predicate for comparison,
3860 * preserving the relative order of equivalent elements.
3861 * @param first An iterator.
3862 * @param last Another iterator.
3863 * @param comp A comparison functor.
3866 * Sorts the elements in the range @p [first,last) in ascending order,
3867 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
3868 * range @p [first,last-1).
3870 * The relative ordering of equivalent elements is preserved, so any two
3871 * elements @p x and @p y in the range @p [first,last) such that
3872 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
3873 * relative ordering after calling @p stable_sort().
3875 template<typename _RandomAccessIterator, typename _Compare>
3877 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
3880 typedef typename iterator_traits<_RandomAccessIterator>::value_type
3882 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
3885 // concept requirements
3886 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
3887 _RandomAccessIterator>)
3888 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
3891 __glibcxx_requires_valid_range(__first, __last);
3893 _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
3895 if (__buf.begin() == 0)
3896 std::__inplace_stable_sort(__first, __last, __comp);
3898 std::__stable_sort_adaptive(__first, __last, __buf.begin(),
3899 _DistanceType(__buf.size()), __comp);
3903 template<typename _RandomAccessIterator, typename _Size>
3905 __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
3906 _RandomAccessIterator __last, _Size __depth_limit)
3908 typedef typename iterator_traits<_RandomAccessIterator>::value_type
3911 while (__last - __first > 3)
3913 if (__depth_limit == 0)
3915 std::__heap_select(__first, __nth + 1, __last);
3916 // Place the nth largest element in its final position.
3917 std::iter_swap(__first, __nth);
3921 _RandomAccessIterator __cut =
3922 std::__unguarded_partition(__first, __last,
3923 _ValueType(std::__median(*__first,
3935 std::__insertion_sort(__first, __last);
3938 template<typename _RandomAccessIterator, typename _Size, typename _Compare>
3940 __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
3941 _RandomAccessIterator __last, _Size __depth_limit,
3944 typedef typename iterator_traits<_RandomAccessIterator>::value_type
3947 while (__last - __first > 3)
3949 if (__depth_limit == 0)
3951 std::__heap_select(__first, __nth + 1, __last, __comp);
3952 // Place the nth largest element in its final position.
3953 std::iter_swap(__first, __nth);
3957 _RandomAccessIterator __cut =
3958 std::__unguarded_partition(__first, __last,
3959 _ValueType(std::__median(*__first,
3972 std::__insertion_sort(__first, __last, __comp);
3976 * @brief Sort a sequence just enough to find a particular position.
3977 * @param first An iterator.
3978 * @param nth Another iterator.
3979 * @param last Another iterator.
3982 * Rearranges the elements in the range @p [first,last) so that @p *nth
3983 * is the same element that would have been in that position had the
3984 * whole sequence been sorted.
3985 * whole sequence been sorted. The elements either side of @p *nth are
3986 * not completely sorted, but for any iterator @i in the range
3987 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3988 * holds that @p *j<*i is false.
3990 template<typename _RandomAccessIterator>
3992 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
3993 _RandomAccessIterator __last)
3995 typedef typename iterator_traits<_RandomAccessIterator>::value_type
3998 // concept requirements
3999 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4000 _RandomAccessIterator>)
4001 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
4002 __glibcxx_requires_valid_range(__first, __nth);
4003 __glibcxx_requires_valid_range(__nth, __last);
4005 if (__first == __last || __nth == __last)
4008 std::__introselect(__first, __nth, __last,
4009 std::__lg(__last - __first) * 2);
4013 * @brief Sort a sequence just enough to find a particular position
4014 * using a predicate for comparison.
4015 * @param first An iterator.
4016 * @param nth Another iterator.
4017 * @param last Another iterator.
4018 * @param comp A comparison functor.
4021 * Rearranges the elements in the range @p [first,last) so that @p *nth
4022 * is the same element that would have been in that position had the
4023 * whole sequence been sorted. The elements either side of @p *nth are
4024 * not completely sorted, but for any iterator @i in the range
4025 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4026 * holds that @p comp(*j,*i) is false.
4028 template<typename _RandomAccessIterator, typename _Compare>
4030 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
4031 _RandomAccessIterator __last, _Compare __comp)
4033 typedef typename iterator_traits<_RandomAccessIterator>::value_type
4036 // concept requirements
4037 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
4038 _RandomAccessIterator>)
4039 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4040 _ValueType, _ValueType>)
4041 __glibcxx_requires_valid_range(__first, __nth);
4042 __glibcxx_requires_valid_range(__nth, __last);
4044 if (__first == __last || __nth == __last)
4047 std::__introselect(__first, __nth, __last,
4048 std::__lg(__last - __first) * 2, __comp);
4052 * @brief Finds the largest subrange in which @a val could be inserted
4053 * at any place in it without changing the ordering.
4054 * @param first An iterator.
4055 * @param last Another iterator.
4056 * @param val The search term.
4057 * @return An pair of iterators defining the subrange.
4058 * @ingroup binarysearch
4060 * This is equivalent to
4062 * std::make_pair(lower_bound(first, last, val),
4063 * upper_bound(first, last, val))
4065 * but does not actually call those functions.
4067 template<typename _ForwardIterator, typename _Tp>
4068 pair<_ForwardIterator, _ForwardIterator>
4069 equal_range(_ForwardIterator __first, _ForwardIterator __last,
4072 typedef typename iterator_traits<_ForwardIterator>::value_type
4074 typedef typename iterator_traits<_ForwardIterator>::difference_type
4077 // concept requirements
4078 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4079 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
4080 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
4081 __glibcxx_requires_partitioned(__first, __last, __val);
4083 _DistanceType __len = std::distance(__first, __last);
4084 _DistanceType __half;
4085 _ForwardIterator __middle, __left, __right;
4089 __half = __len >> 1;
4091 std::advance(__middle, __half);
4092 if (*__middle < __val)
4096 __len = __len - __half - 1;
4098 else if (__val < *__middle)
4102 __left = std::lower_bound(__first, __middle, __val);
4103 std::advance(__first, __len);
4104 __right = std::upper_bound(++__middle, __first, __val);
4105 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
4108 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4112 * @brief Finds the largest subrange in which @a val could be inserted
4113 * at any place in it without changing the ordering.
4114 * @param first An iterator.
4115 * @param last Another iterator.
4116 * @param val The search term.
4117 * @param comp A functor to use for comparisons.
4118 * @return An pair of iterators defining the subrange.
4119 * @ingroup binarysearch
4121 * This is equivalent to
4123 * std::make_pair(lower_bound(first, last, val, comp),
4124 * upper_bound(first, last, val, comp))
4126 * but does not actually call those functions.
4128 template<typename _ForwardIterator, typename _Tp, typename _Compare>
4129 pair<_ForwardIterator, _ForwardIterator>
4130 equal_range(_ForwardIterator __first, _ForwardIterator __last,
4134 typedef typename iterator_traits<_ForwardIterator>::value_type
4136 typedef typename iterator_traits<_ForwardIterator>::difference_type
4139 // concept requirements
4140 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4141 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4143 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4145 __glibcxx_requires_partitioned_pred(__first, __last, __val, __comp);
4147 _DistanceType __len = std::distance(__first, __last);
4148 _DistanceType __half;
4149 _ForwardIterator __middle, __left, __right;
4153 __half = __len >> 1;
4155 std::advance(__middle, __half);
4156 if (__comp(*__middle, __val))
4160 __len = __len - __half - 1;
4162 else if (__comp(__val, *__middle))
4166 __left = std::lower_bound(__first, __middle, __val, __comp);
4167 std::advance(__first, __len);
4168 __right = std::upper_bound(++__middle, __first, __val, __comp);
4169 return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
4172 return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
4176 * @brief Determines whether an element exists in a range.
4177 * @param first An iterator.
4178 * @param last Another iterator.
4179 * @param val The search term.
4180 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
4181 * @ingroup binarysearch
4183 * Note that this does not actually return an iterator to @a val. For
4184 * that, use std::find or a container's specialized find member functions.
4186 template<typename _ForwardIterator, typename _Tp>
4188 binary_search(_ForwardIterator __first, _ForwardIterator __last,
4191 typedef typename iterator_traits<_ForwardIterator>::value_type
4194 // concept requirements
4195 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4196 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
4197 __glibcxx_requires_partitioned(__first, __last, __val);
4199 _ForwardIterator __i = std::lower_bound(__first, __last, __val);
4200 return __i != __last && !(__val < *__i);
4204 * @brief Determines whether an element exists in a range.
4205 * @param first An iterator.
4206 * @param last Another iterator.
4207 * @param val The search term.
4208 * @param comp A functor to use for comparisons.
4209 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
4210 * @ingroup binarysearch
4212 * Note that this does not actually return an iterator to @a val. For
4213 * that, use std::find or a container's specialized find member functions.
4215 * The comparison function should have the same effects on ordering as
4216 * the function used for the initial sort.
4218 template<typename _ForwardIterator, typename _Tp, typename _Compare>
4220 binary_search(_ForwardIterator __first, _ForwardIterator __last,
4221 const _Tp& __val, _Compare __comp)
4223 typedef typename iterator_traits<_ForwardIterator>::value_type
4226 // concept requirements
4227 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4228 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4230 __glibcxx_requires_partitioned_pred(__first, __last, __val, __comp);
4232 _ForwardIterator __i = std::lower_bound(__first, __last, __val, __comp);
4233 return __i != __last && !bool(__comp(__val, *__i));
4236 // Set algorithms: includes, set_union, set_intersection, set_difference,
4237 // set_symmetric_difference. All of these algorithms have the precondition
4238 // that their input ranges are sorted and the postcondition that their output
4239 // ranges are sorted.
4242 * @brief Determines whether all elements of a sequence exists in a range.
4243 * @param first1 Start of search range.
4244 * @param last1 End of search range.
4245 * @param first2 Start of sequence
4246 * @param last2 End of sequence.
4247 * @return True if each element in [first2,last2) is contained in order
4248 * within [first1,last1). False otherwise.
4249 * @ingroup setoperations
4251 * This operation expects both [first1,last1) and [first2,last2) to be
4252 * sorted. Searches for the presence of each element in [first2,last2)
4253 * within [first1,last1). The iterators over each range only move forward,
4254 * so this is a linear algorithm. If an element in [first2,last2) is not
4255 * found before the search iterator reaches @a last2, false is returned.
4257 template<typename _InputIterator1, typename _InputIterator2>
4259 includes(_InputIterator1 __first1, _InputIterator1 __last1,
4260 _InputIterator2 __first2, _InputIterator2 __last2)
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(_LessThanOpConcept<_ValueType1, _ValueType2>)
4271 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
4272 __glibcxx_requires_sorted(__first1, __last1);
4273 __glibcxx_requires_sorted(__first2, __last2);
4275 while (__first1 != __last1 && __first2 != __last2)
4276 if (*__first2 < *__first1)
4278 else if(*__first1 < *__first2)
4281 ++__first1, ++__first2;
4283 return __first2 == __last2;
4287 * @brief Determines whether all elements of a sequence exists in a range
4289 * @param first1 Start of search range.
4290 * @param last1 End of search range.
4291 * @param first2 Start of sequence
4292 * @param last2 End of sequence.
4293 * @param comp Comparison function to use.
4294 * @return True if each element in [first2,last2) is contained in order
4295 * within [first1,last1) according to comp. False otherwise.
4296 * @ingroup setoperations
4298 * This operation expects both [first1,last1) and [first2,last2) to be
4299 * sorted. Searches for the presence of each element in [first2,last2)
4300 * within [first1,last1), using comp to decide. The iterators over each
4301 * range only move forward, so this is a linear algorithm. If an element
4302 * in [first2,last2) is not found before the search iterator reaches @a
4303 * last2, false is returned.
4305 template<typename _InputIterator1, typename _InputIterator2,
4308 includes(_InputIterator1 __first1, _InputIterator1 __last1,
4309 _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
4311 typedef typename iterator_traits<_InputIterator1>::value_type
4313 typedef typename iterator_traits<_InputIterator2>::value_type
4316 // concept requirements
4317 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4318 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4319 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4320 _ValueType1, _ValueType2>)
4321 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4322 _ValueType2, _ValueType1>)
4323 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
4324 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
4326 while (__first1 != __last1 && __first2 != __last2)
4327 if (__comp(*__first2, *__first1))
4329 else if(__comp(*__first1, *__first2))
4332 ++__first1, ++__first2;
4334 return __first2 == __last2;
4338 * @brief Return the union of two sorted ranges.
4339 * @param first1 Start of first range.
4340 * @param last1 End of first range.
4341 * @param first2 Start of second range.
4342 * @param last2 End of second range.
4343 * @return End of the output range.
4344 * @ingroup setoperations
4346 * This operation iterates over both ranges, copying elements present in
4347 * each range in order to the output range. Iterators increment for each
4348 * range. When the current element of one range is less than the other,
4349 * that element is copied and the iterator advanced. If an element is
4350 * contained in both ranges, the element from the first range is copied and
4351 * both ranges advance. The output range may not overlap either input
4354 template<typename _InputIterator1, typename _InputIterator2,
4355 typename _OutputIterator>
4357 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
4358 _InputIterator2 __first2, _InputIterator2 __last2,
4359 _OutputIterator __result)
4361 typedef typename iterator_traits<_InputIterator1>::value_type
4363 typedef typename iterator_traits<_InputIterator2>::value_type
4366 // concept requirements
4367 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4368 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4369 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4371 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4373 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
4374 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
4375 __glibcxx_requires_sorted(__first1, __last1);
4376 __glibcxx_requires_sorted(__first2, __last2);
4378 while (__first1 != __last1 && __first2 != __last2)
4380 if (*__first1 < *__first2)
4382 *__result = *__first1;
4385 else if (*__first2 < *__first1)
4387 *__result = *__first2;
4392 *__result = *__first1;
4398 return std::copy(__first2, __last2, std::copy(__first1, __last1,
4403 * @brief Return the union of two sorted ranges using a comparison functor.
4404 * @param first1 Start of first range.
4405 * @param last1 End of first range.
4406 * @param first2 Start of second range.
4407 * @param last2 End of second range.
4408 * @param comp The comparison functor.
4409 * @return End of the output range.
4410 * @ingroup setoperations
4412 * This operation iterates over both ranges, copying elements present in
4413 * each range in order to the output range. Iterators increment for each
4414 * range. When the current element of one range is less than the other
4415 * according to @a comp, that element is copied and the iterator advanced.
4416 * If an equivalent element according to @a comp is contained in both
4417 * ranges, the element from the first range is copied and both ranges
4418 * advance. The output range may not overlap either input range.
4420 template<typename _InputIterator1, typename _InputIterator2,
4421 typename _OutputIterator, typename _Compare>
4423 set_union(_InputIterator1 __first1, _InputIterator1 __last1,
4424 _InputIterator2 __first2, _InputIterator2 __last2,
4425 _OutputIterator __result, _Compare __comp)
4427 typedef typename iterator_traits<_InputIterator1>::value_type
4429 typedef typename iterator_traits<_InputIterator2>::value_type
4432 // concept requirements
4433 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4434 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4435 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4437 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4439 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4440 _ValueType1, _ValueType2>)
4441 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4442 _ValueType2, _ValueType1>)
4443 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
4444 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
4446 while (__first1 != __last1 && __first2 != __last2)
4448 if (__comp(*__first1, *__first2))
4450 *__result = *__first1;
4453 else if (__comp(*__first2, *__first1))
4455 *__result = *__first2;
4460 *__result = *__first1;
4466 return std::copy(__first2, __last2, std::copy(__first1, __last1,
4471 * @brief Return the intersection of two sorted ranges.
4472 * @param first1 Start of first range.
4473 * @param last1 End of first range.
4474 * @param first2 Start of second range.
4475 * @param last2 End of second range.
4476 * @return End of the output range.
4477 * @ingroup setoperations
4479 * This operation iterates over both ranges, copying elements present in
4480 * both ranges in order to the output range. Iterators increment for each
4481 * range. When the current element of one range is less than the other,
4482 * that iterator advances. If an element is contained in both ranges, the
4483 * element from the first range is copied and both ranges advance. The
4484 * output range may not overlap either input range.
4486 template<typename _InputIterator1, typename _InputIterator2,
4487 typename _OutputIterator>
4489 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
4490 _InputIterator2 __first2, _InputIterator2 __last2,
4491 _OutputIterator __result)
4493 typedef typename iterator_traits<_InputIterator1>::value_type
4495 typedef typename iterator_traits<_InputIterator2>::value_type
4498 // concept requirements
4499 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4500 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4501 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4503 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
4504 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
4505 __glibcxx_requires_sorted(__first1, __last1);
4506 __glibcxx_requires_sorted(__first2, __last2);
4508 while (__first1 != __last1 && __first2 != __last2)
4509 if (*__first1 < *__first2)
4511 else if (*__first2 < *__first1)
4515 *__result = *__first1;
4524 * @brief Return the intersection of two sorted ranges using comparison
4526 * @param first1 Start of first range.
4527 * @param last1 End of first range.
4528 * @param first2 Start of second range.
4529 * @param last2 End of second range.
4530 * @param comp The comparison functor.
4531 * @return End of the output range.
4532 * @ingroup setoperations
4534 * This operation iterates over both ranges, copying elements present in
4535 * both ranges in order to the output range. Iterators increment for each
4536 * range. When the current element of one range is less than the other
4537 * according to @a comp, that iterator advances. If an element is
4538 * contained in both ranges according to @a comp, the element from the
4539 * first range is copied and both ranges advance. The output range may not
4540 * overlap either input range.
4542 template<typename _InputIterator1, typename _InputIterator2,
4543 typename _OutputIterator, typename _Compare>
4545 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
4546 _InputIterator2 __first2, _InputIterator2 __last2,
4547 _OutputIterator __result, _Compare __comp)
4549 typedef typename iterator_traits<_InputIterator1>::value_type
4551 typedef typename iterator_traits<_InputIterator2>::value_type
4554 // concept requirements
4555 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4556 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4557 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4559 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4560 _ValueType1, _ValueType2>)
4561 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4562 _ValueType2, _ValueType1>)
4563 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
4564 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
4566 while (__first1 != __last1 && __first2 != __last2)
4567 if (__comp(*__first1, *__first2))
4569 else if (__comp(*__first2, *__first1))
4573 *__result = *__first1;
4582 * @brief Return the difference of two sorted ranges.
4583 * @param first1 Start of first range.
4584 * @param last1 End of first range.
4585 * @param first2 Start of second range.
4586 * @param last2 End of second range.
4587 * @return End of the output range.
4588 * @ingroup setoperations
4590 * This operation iterates over both ranges, copying elements present in
4591 * the first range but not the second in order to the output range.
4592 * Iterators increment for each range. When the current element of the
4593 * first range is less than the second, that element is copied and the
4594 * iterator advances. If the current element of the second range is less,
4595 * the iterator advances, but no element is copied. If an element is
4596 * contained in both ranges, no elements are copied and both ranges
4597 * advance. The output range may not overlap either input range.
4599 template<typename _InputIterator1, typename _InputIterator2,
4600 typename _OutputIterator>
4602 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
4603 _InputIterator2 __first2, _InputIterator2 __last2,
4604 _OutputIterator __result)
4606 typedef typename iterator_traits<_InputIterator1>::value_type
4608 typedef typename iterator_traits<_InputIterator2>::value_type
4611 // concept requirements
4612 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4613 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4614 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4616 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
4617 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
4618 __glibcxx_requires_sorted(__first1, __last1);
4619 __glibcxx_requires_sorted(__first2, __last2);
4621 while (__first1 != __last1 && __first2 != __last2)
4622 if (*__first1 < *__first2)
4624 *__result = *__first1;
4628 else if (*__first2 < *__first1)
4635 return std::copy(__first1, __last1, __result);
4639 * @brief Return the difference of two sorted ranges using comparison
4641 * @param first1 Start of first range.
4642 * @param last1 End of first range.
4643 * @param first2 Start of second range.
4644 * @param last2 End of second range.
4645 * @param comp The comparison functor.
4646 * @return End of the output range.
4647 * @ingroup setoperations
4649 * This operation iterates over both ranges, copying elements present in
4650 * the first range but not the second in order to the output range.
4651 * Iterators increment for each range. When the current element of the
4652 * first range is less than the second according to @a comp, that element
4653 * is copied and the iterator advances. If the current element of the
4654 * second range is less, no element is copied and the iterator advances.
4655 * If an element is contained in both ranges according to @a comp, no
4656 * elements are copied and both ranges advance. The output range may not
4657 * overlap either input range.
4659 template<typename _InputIterator1, typename _InputIterator2,
4660 typename _OutputIterator, typename _Compare>
4662 set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
4663 _InputIterator2 __first2, _InputIterator2 __last2,
4664 _OutputIterator __result, _Compare __comp)
4666 typedef typename iterator_traits<_InputIterator1>::value_type
4668 typedef typename iterator_traits<_InputIterator2>::value_type
4671 // concept requirements
4672 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4673 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4674 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4676 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4677 _ValueType1, _ValueType2>)
4678 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4679 _ValueType2, _ValueType1>)
4680 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
4681 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
4683 while (__first1 != __last1 && __first2 != __last2)
4684 if (__comp(*__first1, *__first2))
4686 *__result = *__first1;
4690 else if (__comp(*__first2, *__first1))
4697 return std::copy(__first1, __last1, __result);
4701 * @brief Return the symmetric difference of two sorted ranges.
4702 * @param first1 Start of first range.
4703 * @param last1 End of first range.
4704 * @param first2 Start of second range.
4705 * @param last2 End of second range.
4706 * @return End of the output range.
4707 * @ingroup setoperations
4709 * This operation iterates over both ranges, copying elements present in
4710 * one range but not the other in order to the output range. Iterators
4711 * increment for each range. When the current element of one range is less
4712 * than the other, that element is copied and the iterator advances. If an
4713 * element is contained in both ranges, no elements are copied and both
4714 * ranges advance. The output range may not overlap either input range.
4716 template<typename _InputIterator1, typename _InputIterator2,
4717 typename _OutputIterator>
4719 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
4720 _InputIterator2 __first2, _InputIterator2 __last2,
4721 _OutputIterator __result)
4723 typedef typename iterator_traits<_InputIterator1>::value_type
4725 typedef typename iterator_traits<_InputIterator2>::value_type
4728 // concept requirements
4729 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4730 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4731 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4733 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4735 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
4736 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
4737 __glibcxx_requires_sorted(__first1, __last1);
4738 __glibcxx_requires_sorted(__first2, __last2);
4740 while (__first1 != __last1 && __first2 != __last2)
4741 if (*__first1 < *__first2)
4743 *__result = *__first1;
4747 else if (*__first2 < *__first1)
4749 *__result = *__first2;
4758 return std::copy(__first2, __last2, std::copy(__first1,
4759 __last1, __result));
4763 * @brief Return the symmetric difference of two sorted ranges using
4764 * comparison functor.
4765 * @param first1 Start of first range.
4766 * @param last1 End of first range.
4767 * @param first2 Start of second range.
4768 * @param last2 End of second range.
4769 * @param comp The comparison functor.
4770 * @return End of the output range.
4771 * @ingroup setoperations
4773 * This operation iterates over both ranges, copying elements present in
4774 * one range but not the other in order to the output range. Iterators
4775 * increment for each range. When the current element of one range is less
4776 * than the other according to @a comp, that element is copied and the
4777 * iterator advances. If an element is contained in both ranges according
4778 * to @a comp, no elements are copied and both ranges advance. The output
4779 * range may not overlap either input range.
4781 template<typename _InputIterator1, typename _InputIterator2,
4782 typename _OutputIterator, typename _Compare>
4784 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
4785 _InputIterator2 __first2, _InputIterator2 __last2,
4786 _OutputIterator __result,
4789 typedef typename iterator_traits<_InputIterator1>::value_type
4791 typedef typename iterator_traits<_InputIterator2>::value_type
4794 // concept requirements
4795 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
4796 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
4797 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4799 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
4801 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4802 _ValueType1, _ValueType2>)
4803 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4804 _ValueType2, _ValueType1>)
4805 __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
4806 __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
4808 while (__first1 != __last1 && __first2 != __last2)
4809 if (__comp(*__first1, *__first2))
4811 *__result = *__first1;
4815 else if (__comp(*__first2, *__first1))
4817 *__result = *__first2;
4826 return std::copy(__first2, __last2, std::copy(__first1,
4827 __last1, __result));
4830 // min_element and max_element, with and without an explicitly supplied
4831 // comparison function.
4834 * @brief Return the maximum element in a range.
4835 * @param first Start of range.
4836 * @param last End of range.
4837 * @return Iterator referencing the first instance of the largest value.
4839 template<typename _ForwardIterator>
4841 max_element(_ForwardIterator __first, _ForwardIterator __last)
4843 // concept requirements
4844 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4845 __glibcxx_function_requires(_LessThanComparableConcept<
4846 typename iterator_traits<_ForwardIterator>::value_type>)
4847 __glibcxx_requires_valid_range(__first, __last);
4849 if (__first == __last)
4851 _ForwardIterator __result = __first;
4852 while (++__first != __last)
4853 if (*__result < *__first)
4859 * @brief Return the maximum element in a range using comparison functor.
4860 * @param first Start of range.
4861 * @param last End of range.
4862 * @param comp Comparison functor.
4863 * @return Iterator referencing the first instance of the largest value
4864 * according to comp.
4866 template<typename _ForwardIterator, typename _Compare>
4868 max_element(_ForwardIterator __first, _ForwardIterator __last,
4871 // concept requirements
4872 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4873 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4874 typename iterator_traits<_ForwardIterator>::value_type,
4875 typename iterator_traits<_ForwardIterator>::value_type>)
4876 __glibcxx_requires_valid_range(__first, __last);
4878 if (__first == __last) return __first;
4879 _ForwardIterator __result = __first;
4880 while (++__first != __last)
4881 if (__comp(*__result, *__first))
4887 * @brief Return the minimum element in a range.
4888 * @param first Start of range.
4889 * @param last End of range.
4890 * @return Iterator referencing the first instance of the smallest value.
4892 template<typename _ForwardIterator>
4894 min_element(_ForwardIterator __first, _ForwardIterator __last)
4896 // concept requirements
4897 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4898 __glibcxx_function_requires(_LessThanComparableConcept<
4899 typename iterator_traits<_ForwardIterator>::value_type>)
4900 __glibcxx_requires_valid_range(__first, __last);
4902 if (__first == __last)
4904 _ForwardIterator __result = __first;
4905 while (++__first != __last)
4906 if (*__first < *__result)
4912 * @brief Return the minimum element in a range using comparison functor.
4913 * @param first Start of range.
4914 * @param last End of range.
4915 * @param comp Comparison functor.
4916 * @return Iterator referencing the first instance of the smallest value
4917 * according to comp.
4919 template<typename _ForwardIterator, typename _Compare>
4921 min_element(_ForwardIterator __first, _ForwardIterator __last,
4924 // concept requirements
4925 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
4926 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
4927 typename iterator_traits<_ForwardIterator>::value_type,
4928 typename iterator_traits<_ForwardIterator>::value_type>)
4929 __glibcxx_requires_valid_range(__first, __last);
4931 if (__first == __last)
4933 _ForwardIterator __result = __first;
4934 while (++__first != __last)
4935 if (__comp(*__first, *__result))
4940 // next_permutation and prev_permutation, with and without an explicitly
4941 // supplied comparison function.
4944 * @brief Permute range into the next "dictionary" ordering.
4945 * @param first Start of range.
4946 * @param last End of range.
4947 * @return False if wrapped to first permutation, true otherwise.
4949 * Treats all permutations of the range as a set of "dictionary" sorted
4950 * sequences. Permutes the current sequence into the next one of this set.
4951 * Returns true if there are more sequences to generate. If the sequence
4952 * is the largest of the set, the smallest is generated and false returned.
4954 template<typename _BidirectionalIterator>
4956 next_permutation(_BidirectionalIterator __first,
4957 _BidirectionalIterator __last)
4959 // concept requirements
4960 __glibcxx_function_requires(_BidirectionalIteratorConcept<
4961 _BidirectionalIterator>)
4962 __glibcxx_function_requires(_LessThanComparableConcept<
4963 typename iterator_traits<_BidirectionalIterator>::value_type>)
4964 __glibcxx_requires_valid_range(__first, __last);
4966 if (__first == __last)
4968 _BidirectionalIterator __i = __first;
4977 _BidirectionalIterator __ii = __i;
4981 _BidirectionalIterator __j = __last;
4982 while (!(*__i < *--__j))
4984 std::iter_swap(__i, __j);
4985 std::reverse(__ii, __last);
4990 std::reverse(__first, __last);
4997 * @brief Permute range into the next "dictionary" ordering using
4998 * comparison functor.
4999 * @param first Start of range.
5000 * @param last End of range.
5002 * @return False if wrapped to first permutation, true otherwise.
5004 * Treats all permutations of the range [first,last) as a set of
5005 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
5006 * sequence into the next one of this set. Returns true if there are more
5007 * sequences to generate. If the sequence is the largest of the set, the
5008 * smallest is generated and false returned.
5010 template<typename _BidirectionalIterator, typename _Compare>
5012 next_permutation(_BidirectionalIterator __first,
5013 _BidirectionalIterator __last, _Compare __comp)
5015 // concept requirements
5016 __glibcxx_function_requires(_BidirectionalIteratorConcept<
5017 _BidirectionalIterator>)
5018 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5019 typename iterator_traits<_BidirectionalIterator>::value_type,
5020 typename iterator_traits<_BidirectionalIterator>::value_type>)
5021 __glibcxx_requires_valid_range(__first, __last);
5023 if (__first == __last)
5025 _BidirectionalIterator __i = __first;
5034 _BidirectionalIterator __ii = __i;
5036 if (__comp(*__i, *__ii))
5038 _BidirectionalIterator __j = __last;
5039 while (!bool(__comp(*__i, *--__j)))
5041 std::iter_swap(__i, __j);
5042 std::reverse(__ii, __last);
5047 std::reverse(__first, __last);
5054 * @brief Permute range into the previous "dictionary" ordering.
5055 * @param first Start of range.
5056 * @param last End of range.
5057 * @return False if wrapped to last permutation, true otherwise.
5059 * Treats all permutations of the range as a set of "dictionary" sorted
5060 * sequences. Permutes the current sequence into the previous one of this
5061 * set. Returns true if there are more sequences to generate. If the
5062 * sequence is the smallest of the set, the largest is generated and false
5065 template<typename _BidirectionalIterator>
5067 prev_permutation(_BidirectionalIterator __first,
5068 _BidirectionalIterator __last)
5070 // concept requirements
5071 __glibcxx_function_requires(_BidirectionalIteratorConcept<
5072 _BidirectionalIterator>)
5073 __glibcxx_function_requires(_LessThanComparableConcept<
5074 typename iterator_traits<_BidirectionalIterator>::value_type>)
5075 __glibcxx_requires_valid_range(__first, __last);
5077 if (__first == __last)
5079 _BidirectionalIterator __i = __first;
5088 _BidirectionalIterator __ii = __i;
5092 _BidirectionalIterator __j = __last;
5093 while (!(*--__j < *__i))
5095 std::iter_swap(__i, __j);
5096 std::reverse(__ii, __last);
5101 std::reverse(__first, __last);
5108 * @brief Permute range into the previous "dictionary" ordering using
5109 * comparison functor.
5110 * @param first Start of range.
5111 * @param last End of range.
5113 * @return False if wrapped to last permutation, true otherwise.
5115 * Treats all permutations of the range [first,last) as a set of
5116 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
5117 * sequence into the previous one of this set. Returns true if there are
5118 * more sequences to generate. If the sequence is the smallest of the set,
5119 * the largest is generated and false returned.
5121 template<typename _BidirectionalIterator, typename _Compare>
5123 prev_permutation(_BidirectionalIterator __first,
5124 _BidirectionalIterator __last, _Compare __comp)
5126 // concept requirements
5127 __glibcxx_function_requires(_BidirectionalIteratorConcept<
5128 _BidirectionalIterator>)
5129 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
5130 typename iterator_traits<_BidirectionalIterator>::value_type,
5131 typename iterator_traits<_BidirectionalIterator>::value_type>)
5132 __glibcxx_requires_valid_range(__first, __last);
5134 if (__first == __last)
5136 _BidirectionalIterator __i = __first;
5145 _BidirectionalIterator __ii = __i;
5147 if (__comp(*__ii, *__i))
5149 _BidirectionalIterator __j = __last;
5150 while (!bool(__comp(*--__j, *__i)))
5152 std::iter_swap(__i, __j);
5153 std::reverse(__ii, __last);
5158 std::reverse(__first, __last);
5164 // find_first_of, with and without an explicitly supplied comparison function.
5167 * @brief Find element from a set in a sequence.
5168 * @param first1 Start of range to search.
5169 * @param last1 End of range to search.
5170 * @param first2 Start of match candidates.
5171 * @param last2 End of match candidates.
5172 * @return The first iterator @c i in the range
5173 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
5174 * interator in [first2,last2), or @p last1 if no such iterator exists.
5176 * Searches the range @p [first1,last1) for an element that is equal to
5177 * some element in the range [first2,last2). If found, returns an iterator
5178 * in the range [first1,last1), otherwise returns @p last1.
5180 template<typename _InputIterator, typename _ForwardIterator>
5182 find_first_of(_InputIterator __first1, _InputIterator __last1,
5183 _ForwardIterator __first2, _ForwardIterator __last2)
5185 // concept requirements
5186 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
5187 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5188 __glibcxx_function_requires(_EqualOpConcept<
5189 typename iterator_traits<_InputIterator>::value_type,
5190 typename iterator_traits<_ForwardIterator>::value_type>)
5191 __glibcxx_requires_valid_range(__first1, __last1);
5192 __glibcxx_requires_valid_range(__first2, __last2);
5194 for ( ; __first1 != __last1; ++__first1)
5195 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
5196 if (*__first1 == *__iter)
5202 * @brief Find element from a set in a sequence using a predicate.
5203 * @param first1 Start of range to search.
5204 * @param last1 End of range to search.
5205 * @param first2 Start of match candidates.
5206 * @param last2 End of match candidates.
5207 * @param comp Predicate to use.
5208 * @return The first iterator @c i in the range
5209 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
5210 * interator in [first2,last2), or @p last1 if no such iterator exists.
5212 * Searches the range @p [first1,last1) for an element that is equal to
5213 * some element in the range [first2,last2). If found, returns an iterator in
5214 * the range [first1,last1), otherwise returns @p last1.
5216 template<typename _InputIterator, typename _ForwardIterator,
5217 typename _BinaryPredicate>
5219 find_first_of(_InputIterator __first1, _InputIterator __last1,
5220 _ForwardIterator __first2, _ForwardIterator __last2,
5221 _BinaryPredicate __comp)
5223 // concept requirements
5224 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
5225 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
5226 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
5227 typename iterator_traits<_InputIterator>::value_type,
5228 typename iterator_traits<_ForwardIterator>::value_type>)
5229 __glibcxx_requires_valid_range(__first1, __last1);
5230 __glibcxx_requires_valid_range(__first2, __last2);
5232 for ( ; __first1 != __last1; ++__first1)
5233 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
5234 if (__comp(*__first1, *__iter))
5240 // find_end, with and without an explicitly supplied comparison function.
5241 // Search [first2, last2) as a subsequence in [first1, last1), and return
5242 // the *last* possible match. Note that find_end for bidirectional iterators
5243 // is much faster than for forward iterators.
5245 // find_end for forward iterators.
5246 template<typename _ForwardIterator1, typename _ForwardIterator2>
5248 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
5249 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
5250 forward_iterator_tag, forward_iterator_tag)
5252 if (__first2 == __last2)
5256 _ForwardIterator1 __result = __last1;
5259 _ForwardIterator1 __new_result
5260 = std::search(__first1, __last1, __first2, __last2);
5261 if (__new_result == __last1)
5265 __result = __new_result;
5266 __first1 = __new_result;
5273 template<typename _ForwardIterator1, typename _ForwardIterator2,
5274 typename _BinaryPredicate>
5276 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
5277 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
5278 forward_iterator_tag, forward_iterator_tag,
5279 _BinaryPredicate __comp)
5281 if (__first2 == __last2)
5285 _ForwardIterator1 __result = __last1;
5288 _ForwardIterator1 __new_result
5289 = std::search(__first1, __last1, __first2, __last2, __comp);
5290 if (__new_result == __last1)
5294 __result = __new_result;
5295 __first1 = __new_result;
5302 // find_end for bidirectional iterators. Requires partial specialization.
5303 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2>
5304 _BidirectionalIterator1
5305 __find_end(_BidirectionalIterator1 __first1,
5306 _BidirectionalIterator1 __last1,
5307 _BidirectionalIterator2 __first2,
5308 _BidirectionalIterator2 __last2,
5309 bidirectional_iterator_tag, bidirectional_iterator_tag)
5311 // concept requirements
5312 __glibcxx_function_requires(_BidirectionalIteratorConcept<
5313 _BidirectionalIterator1>)
5314 __glibcxx_function_requires(_BidirectionalIteratorConcept<
5315 _BidirectionalIterator2>)
5317 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
5318 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
5320 _RevIterator1 __rlast1(__first1);
5321 _RevIterator2 __rlast2(__first2);
5322 _RevIterator1 __rresult = std::search(_RevIterator1(__last1), __rlast1,
5323 _RevIterator2(__last2), __rlast2);
5325 if (__rresult == __rlast1)
5329 _BidirectionalIterator1 __result = __rresult.base();
5330 std::advance(__result, -std::distance(__first2, __last2));
5335 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
5336 typename _BinaryPredicate>
5337 _BidirectionalIterator1
5338 __find_end(_BidirectionalIterator1 __first1,
5339 _BidirectionalIterator1 __last1,
5340 _BidirectionalIterator2 __first2,
5341 _BidirectionalIterator2 __last2,
5342 bidirectional_iterator_tag, bidirectional_iterator_tag,
5343 _BinaryPredicate __comp)
5345 // concept requirements
5346 __glibcxx_function_requires(_BidirectionalIteratorConcept<
5347 _BidirectionalIterator1>)
5348 __glibcxx_function_requires(_BidirectionalIteratorConcept<
5349 _BidirectionalIterator2>)
5351 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
5352 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
5354 _RevIterator1 __rlast1(__first1);
5355 _RevIterator2 __rlast2(__first2);
5356 _RevIterator1 __rresult = std::search(_RevIterator1(__last1), __rlast1,
5357 _RevIterator2(__last2), __rlast2,
5360 if (__rresult == __rlast1)
5364 _BidirectionalIterator1 __result = __rresult.base();
5365 std::advance(__result, -std::distance(__first2, __last2));
5370 // Dispatching functions for find_end.
5373 * @brief Find last matching subsequence in a sequence.
5374 * @param first1 Start of range to search.
5375 * @param last1 End of range to search.
5376 * @param first2 Start of sequence to match.
5377 * @param last2 End of sequence to match.
5378 * @return The last iterator @c i in the range
5379 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
5380 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
5381 * such iterator exists.
5383 * Searches the range @p [first1,last1) for a sub-sequence that compares
5384 * equal value-by-value with the sequence given by @p [first2,last2) and
5385 * returns an iterator to the first element of the sub-sequence, or
5386 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
5387 * last such subsequence contained in [first,last1).
5389 * Because the sub-sequence must lie completely within the range
5390 * @p [first1,last1) it must start at a position less than
5391 * @p last1-(last2-first2) where @p last2-first2 is the length of the
5393 * This means that the returned iterator @c i will be in the range
5394 * @p [first1,last1-(last2-first2))
5396 template<typename _ForwardIterator1, typename _ForwardIterator2>
5397 inline _ForwardIterator1
5398 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
5399 _ForwardIterator2 __first2, _ForwardIterator2 __last2)
5401 // concept requirements
5402 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
5403 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
5404 __glibcxx_function_requires(_EqualOpConcept<
5405 typename iterator_traits<_ForwardIterator1>::value_type,
5406 typename iterator_traits<_ForwardIterator2>::value_type>)
5407 __glibcxx_requires_valid_range(__first1, __last1);
5408 __glibcxx_requires_valid_range(__first2, __last2);
5410 return std::__find_end(__first1, __last1, __first2, __last2,
5411 std::__iterator_category(__first1),
5412 std::__iterator_category(__first2));
5416 * @brief Find last matching subsequence in a sequence using a predicate.
5417 * @param first1 Start of range to search.
5418 * @param last1 End of range to search.
5419 * @param first2 Start of sequence to match.
5420 * @param last2 End of sequence to match.
5421 * @param comp The predicate to use.
5422 * @return The last iterator @c i in the range
5423 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
5424 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
5425 * @p last1 if no such iterator exists.
5427 * Searches the range @p [first1,last1) for a sub-sequence that compares
5428 * equal value-by-value with the sequence given by @p [first2,last2) using
5429 * comp as a predicate and returns an iterator to the first element of the
5430 * sub-sequence, or @p last1 if the sub-sequence is not found. The
5431 * sub-sequence will be the last such subsequence contained in
5434 * Because the sub-sequence must lie completely within the range
5435 * @p [first1,last1) it must start at a position less than
5436 * @p last1-(last2-first2) where @p last2-first2 is the length of the
5438 * This means that the returned iterator @c i will be in the range
5439 * @p [first1,last1-(last2-first2))
5441 template<typename _ForwardIterator1, typename _ForwardIterator2,
5442 typename _BinaryPredicate>
5443 inline _ForwardIterator1
5444 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
5445 _ForwardIterator2 __first2, _ForwardIterator2 __last2,
5446 _BinaryPredicate __comp)
5448 // concept requirements
5449 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
5450 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
5451 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
5452 typename iterator_traits<_ForwardIterator1>::value_type,
5453 typename iterator_traits<_ForwardIterator2>::value_type>)
5454 __glibcxx_requires_valid_range(__first1, __last1);
5455 __glibcxx_requires_valid_range(__first2, __last2);
5457 return std::__find_end(__first1, __last1, __first2, __last2,
5458 std::__iterator_category(__first1),
5459 std::__iterator_category(__first2),
5463 _GLIBCXX_END_NAMESPACE
5465 #endif /* _ALGO_H */