1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
65 #include <bits/stl_heap.h>
66 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
67 #include <debug/debug.h>
69 // See concept_check.h for the __glibcxx_*_requires macros.
71 _GLIBCXX_BEGIN_NAMESPACE(std
)
74 * @brief Find the median of three values.
78 * @return One of @p a, @p b or @p c.
80 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
81 * then the value returned will be @c m.
82 * This is an SGI extension.
83 * @ingroup SGIextensions
85 template<typename _Tp
>
87 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
)
89 // concept requirements
90 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
107 * @brief Find the median of three values using a predicate for comparison.
111 * @param comp A binary predicate.
112 * @return One of @p a, @p b or @p c.
114 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
115 * and @p comp(m,n) are both true then the value returned will be @c m.
116 * This is an SGI extension.
117 * @ingroup SGIextensions
119 template<typename _Tp
, typename _Compare
>
121 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
, _Compare __comp
)
123 // concept requirements
124 __glibcxx_function_requires(_BinaryFunctionConcept
<_Compare
,bool,_Tp
,_Tp
>)
125 if (__comp(__a
, __b
))
126 if (__comp(__b
, __c
))
128 else if (__comp(__a
, __c
))
132 else if (__comp(__a
, __c
))
134 else if (__comp(__b
, __c
))
141 * @brief Apply a function to every element of a sequence.
142 * @param first An input iterator.
143 * @param last An input iterator.
144 * @param f A unary function object.
147 * Applies the function object @p f to each element in the range
148 * @p [first,last). @p f must not modify the order of the sequence.
149 * If @p f has a return value it is ignored.
151 template<typename _InputIterator
, typename _Function
>
153 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
155 // concept requirements
156 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
157 __glibcxx_requires_valid_range(__first
, __last
);
158 for ( ; __first
!= __last
; ++__first
)
165 * This is an overload used by find() for the Input Iterator case.
168 template<typename _InputIterator
, typename _Tp
>
169 inline _InputIterator
170 __find(_InputIterator __first
, _InputIterator __last
,
171 const _Tp
& __val
, input_iterator_tag
)
173 while (__first
!= __last
&& !(*__first
== __val
))
180 * This is an overload used by find_if() for the Input Iterator case.
183 template<typename _InputIterator
, typename _Predicate
>
184 inline _InputIterator
185 __find_if(_InputIterator __first
, _InputIterator __last
,
186 _Predicate __pred
, input_iterator_tag
)
188 while (__first
!= __last
&& !__pred(*__first
))
195 * This is an overload used by find() for the RAI case.
198 template<typename _RandomAccessIterator
, typename _Tp
>
199 _RandomAccessIterator
200 __find(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
201 const _Tp
& __val
, random_access_iterator_tag
)
203 typename iterator_traits
<_RandomAccessIterator
>::difference_type
204 __trip_count
= (__last
- __first
) >> 2;
206 for ( ; __trip_count
> 0 ; --__trip_count
)
208 if (*__first
== __val
)
212 if (*__first
== __val
)
216 if (*__first
== __val
)
220 if (*__first
== __val
)
225 switch (__last
- __first
)
228 if (*__first
== __val
)
232 if (*__first
== __val
)
236 if (*__first
== __val
)
247 * This is an overload used by find_if() for the RAI case.
250 template<typename _RandomAccessIterator
, typename _Predicate
>
251 _RandomAccessIterator
252 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
253 _Predicate __pred
, random_access_iterator_tag
)
255 typename iterator_traits
<_RandomAccessIterator
>::difference_type
256 __trip_count
= (__last
- __first
) >> 2;
258 for ( ; __trip_count
> 0 ; --__trip_count
)
260 if (__pred(*__first
))
264 if (__pred(*__first
))
268 if (__pred(*__first
))
272 if (__pred(*__first
))
277 switch (__last
- __first
)
280 if (__pred(*__first
))
284 if (__pred(*__first
))
288 if (__pred(*__first
))
299 * This is an overload of find() for streambuf iterators.
302 template<typename _CharT
>
303 typename __enable_if
<istreambuf_iterator
<_CharT
>,
304 __is_char
<_CharT
>::__value
>::__type
305 find(istreambuf_iterator
<_CharT
>, istreambuf_iterator
<_CharT
>, _CharT
);
308 * @brief Find the first occurrence of a value in a sequence.
309 * @param first An input iterator.
310 * @param last An input iterator.
311 * @param val The value to find.
312 * @return The first iterator @c i in the range @p [first,last)
313 * such that @c *i == @p val, or @p last if no such iterator exists.
315 template<typename _InputIterator
, typename _Tp
>
316 inline _InputIterator
317 find(_InputIterator __first
, _InputIterator __last
,
320 // concept requirements
321 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
322 __glibcxx_function_requires(_EqualOpConcept
<
323 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
324 __glibcxx_requires_valid_range(__first
, __last
);
325 return std::__find(__first
, __last
, __val
,
326 std::__iterator_category(__first
));
330 * @brief Find the first element in a sequence for which a predicate is true.
331 * @param first An input iterator.
332 * @param last An input iterator.
333 * @param pred A predicate.
334 * @return The first iterator @c i in the range @p [first,last)
335 * such that @p pred(*i) is true, or @p last if no such iterator exists.
337 template<typename _InputIterator
, typename _Predicate
>
338 inline _InputIterator
339 find_if(_InputIterator __first
, _InputIterator __last
,
342 // concept requirements
343 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
344 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
345 typename iterator_traits
<_InputIterator
>::value_type
>)
346 __glibcxx_requires_valid_range(__first
, __last
);
347 return std::__find_if(__first
, __last
, __pred
,
348 std::__iterator_category(__first
));
352 * @brief Find two adjacent values in a sequence that are equal.
353 * @param first A forward iterator.
354 * @param last A forward iterator.
355 * @return The first iterator @c i such that @c i and @c i+1 are both
356 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
357 * or @p last if no such iterator exists.
359 template<typename _ForwardIterator
>
361 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
363 // concept requirements
364 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
365 __glibcxx_function_requires(_EqualityComparableConcept
<
366 typename iterator_traits
<_ForwardIterator
>::value_type
>)
367 __glibcxx_requires_valid_range(__first
, __last
);
368 if (__first
== __last
)
370 _ForwardIterator __next
= __first
;
371 while(++__next
!= __last
)
373 if (*__first
== *__next
)
381 * @brief Find two adjacent values in a sequence using a predicate.
382 * @param first A forward iterator.
383 * @param last A forward iterator.
384 * @param binary_pred A binary predicate.
385 * @return The first iterator @c i such that @c i and @c i+1 are both
386 * valid iterators in @p [first,last) and such that
387 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
390 template<typename _ForwardIterator
, typename _BinaryPredicate
>
392 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
393 _BinaryPredicate __binary_pred
)
395 // concept requirements
396 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
397 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
398 typename iterator_traits
<_ForwardIterator
>::value_type
,
399 typename iterator_traits
<_ForwardIterator
>::value_type
>)
400 __glibcxx_requires_valid_range(__first
, __last
);
401 if (__first
== __last
)
403 _ForwardIterator __next
= __first
;
404 while(++__next
!= __last
)
406 if (__binary_pred(*__first
, *__next
))
414 * @brief Count the number of copies of a value in a sequence.
415 * @param first An input iterator.
416 * @param last An input iterator.
417 * @param value The value to be counted.
418 * @return The number of iterators @c i in the range @p [first,last)
419 * for which @c *i == @p value
421 template<typename _InputIterator
, typename _Tp
>
422 typename iterator_traits
<_InputIterator
>::difference_type
423 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
425 // concept requirements
426 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
427 __glibcxx_function_requires(_EqualOpConcept
<
428 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
429 __glibcxx_requires_valid_range(__first
, __last
);
430 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
431 for ( ; __first
!= __last
; ++__first
)
432 if (*__first
== __value
)
438 * @brief Count the elements of a sequence for which a predicate is true.
439 * @param first An input iterator.
440 * @param last An input iterator.
441 * @param pred A predicate.
442 * @return The number of iterators @c i in the range @p [first,last)
443 * for which @p pred(*i) is true.
445 template<typename _InputIterator
, typename _Predicate
>
446 typename iterator_traits
<_InputIterator
>::difference_type
447 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
449 // concept requirements
450 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
451 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
452 typename iterator_traits
<_InputIterator
>::value_type
>)
453 __glibcxx_requires_valid_range(__first
, __last
);
454 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
455 for ( ; __first
!= __last
; ++__first
)
456 if (__pred(*__first
))
462 * @brief Search a sequence for a matching sub-sequence.
463 * @param first1 A forward iterator.
464 * @param last1 A forward iterator.
465 * @param first2 A forward iterator.
466 * @param last2 A forward iterator.
467 * @return The first iterator @c i in the range
468 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
469 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
470 * such iterator exists.
472 * Searches the range @p [first1,last1) for a sub-sequence that compares
473 * equal value-by-value with the sequence given by @p [first2,last2) and
474 * returns an iterator to the first element of the sub-sequence, or
475 * @p last1 if the sub-sequence is not found.
477 * Because the sub-sequence must lie completely within the range
478 * @p [first1,last1) it must start at a position less than
479 * @p last1-(last2-first2) where @p last2-first2 is the length of the
481 * This means that the returned iterator @c i will be in the range
482 * @p [first1,last1-(last2-first2))
484 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
486 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
487 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
489 // concept requirements
490 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
491 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
492 __glibcxx_function_requires(_EqualOpConcept
<
493 typename iterator_traits
<_ForwardIterator1
>::value_type
,
494 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
495 __glibcxx_requires_valid_range(__first1
, __last1
);
496 __glibcxx_requires_valid_range(__first2
, __last2
);
497 // Test for empty ranges
498 if (__first1
== __last1
|| __first2
== __last2
)
501 // Test for a pattern of length 1.
502 _ForwardIterator2
__tmp(__first2
);
504 if (__tmp
== __last2
)
505 return std::find(__first1
, __last1
, *__first2
);
508 _ForwardIterator2 __p1
, __p
;
509 __p1
= __first2
; ++__p1
;
510 _ForwardIterator1 __current
= __first1
;
512 while (__first1
!= __last1
)
514 __first1
= std::find(__first1
, __last1
, *__first2
);
515 if (__first1
== __last1
)
519 __current
= __first1
;
520 if (++__current
== __last1
)
523 while (*__current
== *__p
)
525 if (++__p
== __last2
)
527 if (++__current
== __last1
)
536 * @brief Search a sequence for a matching sub-sequence using a predicate.
537 * @param first1 A forward iterator.
538 * @param last1 A forward iterator.
539 * @param first2 A forward iterator.
540 * @param last2 A forward iterator.
541 * @param predicate A binary predicate.
542 * @return The first iterator @c i in the range
543 * @p [first1,last1-(last2-first2)) such that
544 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
545 * @p [0,last2-first2), or @p last1 if no such iterator exists.
547 * Searches the range @p [first1,last1) for a sub-sequence that compares
548 * equal value-by-value with the sequence given by @p [first2,last2),
549 * using @p predicate to determine equality, and returns an iterator
550 * to the first element of the sub-sequence, or @p last1 if no such
553 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
555 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
556 typename _BinaryPredicate
>
558 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
559 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
560 _BinaryPredicate __predicate
)
562 // concept requirements
563 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
564 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
565 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
566 typename iterator_traits
<_ForwardIterator1
>::value_type
,
567 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
568 __glibcxx_requires_valid_range(__first1
, __last1
);
569 __glibcxx_requires_valid_range(__first2
, __last2
);
571 // Test for empty ranges
572 if (__first1
== __last1
|| __first2
== __last2
)
575 // Test for a pattern of length 1.
576 _ForwardIterator2
__tmp(__first2
);
578 if (__tmp
== __last2
)
580 while (__first1
!= __last1
&& !__predicate(*__first1
, *__first2
))
586 _ForwardIterator2 __p1
, __p
;
587 __p1
= __first2
; ++__p1
;
588 _ForwardIterator1 __current
= __first1
;
590 while (__first1
!= __last1
)
592 while (__first1
!= __last1
)
594 if (__predicate(*__first1
, *__first2
))
598 while (__first1
!= __last1
&& !__predicate(*__first1
, *__first2
))
600 if (__first1
== __last1
)
604 __current
= __first1
;
605 if (++__current
== __last1
)
608 while (__predicate(*__current
, *__p
))
610 if (++__p
== __last2
)
612 if (++__current
== __last1
)
622 * This is an uglified
623 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
624 * overloaded for forward iterators.
627 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
629 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
630 _Integer __count
, const _Tp
& __val
,
631 std::forward_iterator_tag
)
633 __first
= std::find(__first
, __last
, __val
);
634 while (__first
!= __last
)
636 typename iterator_traits
<_ForwardIterator
>::difference_type
638 _ForwardIterator __i
= __first
;
640 while (__i
!= __last
&& __n
!= 1 && *__i
== __val
)
649 __first
= std::find(++__i
, __last
, __val
);
656 * This is an uglified
657 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
658 * overloaded for random access iterators.
661 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
>
663 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
664 _Integer __count
, const _Tp
& __val
,
665 std::random_access_iterator_tag
)
668 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
671 _DistanceType __tailSize
= __last
- __first
;
672 const _DistanceType __pattSize
= __count
;
674 if (__tailSize
< __pattSize
)
677 const _DistanceType __skipOffset
= __pattSize
- 1;
678 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
679 __tailSize
-= __pattSize
;
681 while (1) // the main loop...
683 // __lookAhead here is always pointing to the last element of next
685 while (!(*__lookAhead
== __val
)) // the skip loop...
687 if (__tailSize
< __pattSize
)
688 return __last
; // Failure
689 __lookAhead
+= __pattSize
;
690 __tailSize
-= __pattSize
;
692 _DistanceType __remainder
= __skipOffset
;
693 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
694 *__backTrack
== __val
; --__backTrack
)
696 if (--__remainder
== 0)
697 return (__lookAhead
- __skipOffset
); // Success
699 if (__remainder
> __tailSize
)
700 return __last
; // Failure
701 __lookAhead
+= __remainder
;
702 __tailSize
-= __remainder
;
707 * @brief Search a sequence for a number of consecutive values.
708 * @param first A forward iterator.
709 * @param last A forward iterator.
710 * @param count The number of consecutive values.
711 * @param val The value to find.
712 * @return The first iterator @c i in the range @p [first,last-count)
713 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
714 * or @p last if no such iterator exists.
716 * Searches the range @p [first,last) for @p count consecutive elements
719 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
721 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
722 _Integer __count
, const _Tp
& __val
)
724 // concept requirements
725 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
726 __glibcxx_function_requires(_EqualOpConcept
<
727 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
728 __glibcxx_requires_valid_range(__first
, __last
);
733 return std::find(__first
, __last
, __val
);
734 return std::__search_n(__first
, __last
, __count
, __val
,
735 std::__iterator_category(__first
));
740 * This is an uglified
741 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
743 * overloaded for forward iterators.
746 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
747 typename _BinaryPredicate
>
749 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
750 _Integer __count
, const _Tp
& __val
,
751 _BinaryPredicate __binary_pred
, std::forward_iterator_tag
)
753 while (__first
!= __last
&& !__binary_pred(*__first
, __val
))
756 while (__first
!= __last
)
758 typename iterator_traits
<_ForwardIterator
>::difference_type
760 _ForwardIterator __i
= __first
;
762 while (__i
!= __last
&& __n
!= 1 && __binary_pred(*__i
, __val
))
772 while (__first
!= __last
&& !__binary_pred(*__first
, __val
))
780 * This is an uglified
781 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
783 * overloaded for random access iterators.
786 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
,
787 typename _BinaryPredicate
>
789 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
790 _Integer __count
, const _Tp
& __val
,
791 _BinaryPredicate __binary_pred
, std::random_access_iterator_tag
)
794 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
797 _DistanceType __tailSize
= __last
- __first
;
798 const _DistanceType __pattSize
= __count
;
800 if (__tailSize
< __pattSize
)
803 const _DistanceType __skipOffset
= __pattSize
- 1;
804 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
805 __tailSize
-= __pattSize
;
807 while (1) // the main loop...
809 // __lookAhead here is always pointing to the last element of next
811 while (!__binary_pred(*__lookAhead
, __val
)) // the skip loop...
813 if (__tailSize
< __pattSize
)
814 return __last
; // Failure
815 __lookAhead
+= __pattSize
;
816 __tailSize
-= __pattSize
;
818 _DistanceType __remainder
= __skipOffset
;
819 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
820 __binary_pred(*__backTrack
, __val
); --__backTrack
)
822 if (--__remainder
== 0)
823 return (__lookAhead
- __skipOffset
); // Success
825 if (__remainder
> __tailSize
)
826 return __last
; // Failure
827 __lookAhead
+= __remainder
;
828 __tailSize
-= __remainder
;
833 * @brief Search a sequence for a number of consecutive values using a
835 * @param first A forward iterator.
836 * @param last A forward iterator.
837 * @param count The number of consecutive values.
838 * @param val The value to find.
839 * @param binary_pred A binary predicate.
840 * @return The first iterator @c i in the range @p [first,last-count)
841 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
842 * range @p [0,count), or @p last if no such iterator exists.
844 * Searches the range @p [first,last) for @p count consecutive elements
845 * for which the predicate returns true.
847 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
848 typename _BinaryPredicate
>
850 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
851 _Integer __count
, const _Tp
& __val
,
852 _BinaryPredicate __binary_pred
)
854 // concept requirements
855 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
856 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
857 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
858 __glibcxx_requires_valid_range(__first
, __last
);
864 while (__first
!= __last
&& !__binary_pred(*__first
, __val
))
868 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
869 std::__iterator_category(__first
));
873 * @brief Swap the elements of two sequences.
874 * @param first1 A forward iterator.
875 * @param last1 A forward iterator.
876 * @param first2 A forward iterator.
877 * @return An iterator equal to @p first2+(last1-first1).
879 * Swaps each element in the range @p [first1,last1) with the
880 * corresponding element in the range @p [first2,(last1-first1)).
881 * The ranges must not overlap.
883 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
885 swap_ranges(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
886 _ForwardIterator2 __first2
)
888 // concept requirements
889 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
891 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
893 __glibcxx_function_requires(_ConvertibleConcept
<
894 typename iterator_traits
<_ForwardIterator1
>::value_type
,
895 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
896 __glibcxx_function_requires(_ConvertibleConcept
<
897 typename iterator_traits
<_ForwardIterator2
>::value_type
,
898 typename iterator_traits
<_ForwardIterator1
>::value_type
>)
899 __glibcxx_requires_valid_range(__first1
, __last1
);
901 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
)
902 std::iter_swap(__first1
, __first2
);
907 * @brief Perform an operation on a sequence.
908 * @param first An input iterator.
909 * @param last An input iterator.
910 * @param result An output iterator.
911 * @param unary_op A unary operator.
912 * @return An output iterator equal to @p result+(last-first).
914 * Applies the operator to each element in the input range and assigns
915 * the results to successive elements of the output sequence.
916 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
917 * range @p [0,last-first).
919 * @p unary_op must not alter its argument.
921 template<typename _InputIterator
, typename _OutputIterator
,
922 typename _UnaryOperation
>
924 transform(_InputIterator __first
, _InputIterator __last
,
925 _OutputIterator __result
, _UnaryOperation __unary_op
)
927 // concept requirements
928 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
929 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
930 // "the type returned by a _UnaryOperation"
931 __typeof__(__unary_op(*__first
))>)
932 __glibcxx_requires_valid_range(__first
, __last
);
934 for ( ; __first
!= __last
; ++__first
, ++__result
)
935 *__result
= __unary_op(*__first
);
940 * @brief Perform an operation on corresponding elements of two sequences.
941 * @param first1 An input iterator.
942 * @param last1 An input iterator.
943 * @param first2 An input iterator.
944 * @param result An output iterator.
945 * @param binary_op A binary operator.
946 * @return An output iterator equal to @p result+(last-first).
948 * Applies the operator to the corresponding elements in the two
949 * input ranges and assigns the results to successive elements of the
951 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
952 * @c N in the range @p [0,last1-first1).
954 * @p binary_op must not alter either of its arguments.
956 template<typename _InputIterator1
, typename _InputIterator2
,
957 typename _OutputIterator
, typename _BinaryOperation
>
959 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
960 _InputIterator2 __first2
, _OutputIterator __result
,
961 _BinaryOperation __binary_op
)
963 // concept requirements
964 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
965 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
966 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
967 // "the type returned by a _BinaryOperation"
968 __typeof__(__binary_op(*__first1
,*__first2
))>)
969 __glibcxx_requires_valid_range(__first1
, __last1
);
971 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
972 *__result
= __binary_op(*__first1
, *__first2
);
977 * @brief Replace each occurrence of one value in a sequence with another
979 * @param first A forward iterator.
980 * @param last A forward iterator.
981 * @param old_value The value to be replaced.
982 * @param new_value The replacement value.
983 * @return replace() returns no value.
985 * For each iterator @c i in the range @p [first,last) if @c *i ==
986 * @p old_value then the assignment @c *i = @p new_value is performed.
988 template<typename _ForwardIterator
, typename _Tp
>
990 replace(_ForwardIterator __first
, _ForwardIterator __last
,
991 const _Tp
& __old_value
, const _Tp
& __new_value
)
993 // concept requirements
994 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
996 __glibcxx_function_requires(_EqualOpConcept
<
997 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
998 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
999 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1000 __glibcxx_requires_valid_range(__first
, __last
);
1002 for ( ; __first
!= __last
; ++__first
)
1003 if (*__first
== __old_value
)
1004 *__first
= __new_value
;
1008 * @brief Replace each value in a sequence for which a predicate returns
1009 * true with another value.
1010 * @param first A forward iterator.
1011 * @param last A forward iterator.
1012 * @param pred A predicate.
1013 * @param new_value The replacement value.
1014 * @return replace_if() returns no value.
1016 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
1017 * is true then the assignment @c *i = @p new_value is performed.
1019 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
1021 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
1022 _Predicate __pred
, const _Tp
& __new_value
)
1024 // concept requirements
1025 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1027 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
1028 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1029 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1030 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1031 __glibcxx_requires_valid_range(__first
, __last
);
1033 for ( ; __first
!= __last
; ++__first
)
1034 if (__pred(*__first
))
1035 *__first
= __new_value
;
1039 * @brief Copy a sequence, replacing each element of one value with another
1041 * @param first An input iterator.
1042 * @param last An input iterator.
1043 * @param result An output iterator.
1044 * @param old_value The value to be replaced.
1045 * @param new_value The replacement value.
1046 * @return The end of the output sequence, @p result+(last-first).
1048 * Copies each element in the input range @p [first,last) to the
1049 * output range @p [result,result+(last-first)) replacing elements
1050 * equal to @p old_value with @p new_value.
1052 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
1054 replace_copy(_InputIterator __first
, _InputIterator __last
,
1055 _OutputIterator __result
,
1056 const _Tp
& __old_value
, const _Tp
& __new_value
)
1058 // concept requirements
1059 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1060 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1061 typename iterator_traits
<_InputIterator
>::value_type
>)
1062 __glibcxx_function_requires(_EqualOpConcept
<
1063 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
1064 __glibcxx_requires_valid_range(__first
, __last
);
1066 for ( ; __first
!= __last
; ++__first
, ++__result
)
1067 if (*__first
== __old_value
)
1068 *__result
= __new_value
;
1070 *__result
= *__first
;
1075 * @brief Copy a sequence, replacing each value for which a predicate
1076 * returns true with another value.
1077 * @param first An input iterator.
1078 * @param last An input iterator.
1079 * @param result An output iterator.
1080 * @param pred A predicate.
1081 * @param new_value The replacement value.
1082 * @return The end of the output sequence, @p result+(last-first).
1084 * Copies each element in the range @p [first,last) to the range
1085 * @p [result,result+(last-first)) replacing elements for which
1086 * @p pred returns true with @p new_value.
1088 template<typename _InputIterator
, typename _OutputIterator
,
1089 typename _Predicate
, typename _Tp
>
1091 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
1092 _OutputIterator __result
,
1093 _Predicate __pred
, const _Tp
& __new_value
)
1095 // concept requirements
1096 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1097 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1098 typename iterator_traits
<_InputIterator
>::value_type
>)
1099 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1100 typename iterator_traits
<_InputIterator
>::value_type
>)
1101 __glibcxx_requires_valid_range(__first
, __last
);
1103 for ( ; __first
!= __last
; ++__first
, ++__result
)
1104 if (__pred(*__first
))
1105 *__result
= __new_value
;
1107 *__result
= *__first
;
1112 * @brief Assign the result of a function object to each value in a
1114 * @param first A forward iterator.
1115 * @param last A forward iterator.
1116 * @param gen A function object taking no arguments.
1117 * @return generate() returns no value.
1119 * Performs the assignment @c *i = @p gen() for each @c i in the range
1122 template<typename _ForwardIterator
, typename _Generator
>
1124 generate(_ForwardIterator __first
, _ForwardIterator __last
,
1127 // concept requirements
1128 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1129 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
1130 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1131 __glibcxx_requires_valid_range(__first
, __last
);
1133 for ( ; __first
!= __last
; ++__first
)
1138 * @brief Assign the result of a function object to each value in a
1140 * @param first A forward iterator.
1141 * @param n The length of the sequence.
1142 * @param gen A function object taking no arguments.
1143 * @return The end of the sequence, @p first+n
1145 * Performs the assignment @c *i = @p gen() for each @c i in the range
1146 * @p [first,first+n).
1148 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
1150 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
1152 // concept requirements
1153 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1154 // "the type returned by a _Generator"
1155 __typeof__(__gen())>)
1157 for ( ; __n
> 0; --__n
, ++__first
)
1163 * @brief Copy a sequence, removing elements of a given value.
1164 * @param first An input iterator.
1165 * @param last An input iterator.
1166 * @param result An output iterator.
1167 * @param value The value to be removed.
1168 * @return An iterator designating the end of the resulting sequence.
1170 * Copies each element in the range @p [first,last) not equal to @p value
1171 * to the range beginning at @p result.
1172 * remove_copy() is stable, so the relative order of elements that are
1173 * copied is unchanged.
1175 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
1177 remove_copy(_InputIterator __first
, _InputIterator __last
,
1178 _OutputIterator __result
, const _Tp
& __value
)
1180 // concept requirements
1181 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1182 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1183 typename iterator_traits
<_InputIterator
>::value_type
>)
1184 __glibcxx_function_requires(_EqualOpConcept
<
1185 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
1186 __glibcxx_requires_valid_range(__first
, __last
);
1188 for ( ; __first
!= __last
; ++__first
)
1189 if (!(*__first
== __value
))
1191 *__result
= *__first
;
1198 * @brief Copy a sequence, removing elements for which a predicate is true.
1199 * @param first An input iterator.
1200 * @param last An input iterator.
1201 * @param result An output iterator.
1202 * @param pred A predicate.
1203 * @return An iterator designating the end of the resulting sequence.
1205 * Copies each element in the range @p [first,last) for which
1206 * @p pred returns true to the range beginning at @p result.
1208 * remove_copy_if() is stable, so the relative order of elements that are
1209 * copied is unchanged.
1211 template<typename _InputIterator
, typename _OutputIterator
,
1212 typename _Predicate
>
1214 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
1215 _OutputIterator __result
, _Predicate __pred
)
1217 // concept requirements
1218 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1219 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1220 typename iterator_traits
<_InputIterator
>::value_type
>)
1221 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1222 typename iterator_traits
<_InputIterator
>::value_type
>)
1223 __glibcxx_requires_valid_range(__first
, __last
);
1225 for ( ; __first
!= __last
; ++__first
)
1226 if (!__pred(*__first
))
1228 *__result
= *__first
;
1235 * @brief Remove elements from a sequence.
1236 * @param first An input iterator.
1237 * @param last An input iterator.
1238 * @param value The value to be removed.
1239 * @return An iterator designating the end of the resulting sequence.
1241 * All elements equal to @p value are removed from the range
1244 * remove() is stable, so the relative order of elements that are
1245 * not removed is unchanged.
1247 * Elements between the end of the resulting sequence and @p last
1248 * are still present, but their value is unspecified.
1250 template<typename _ForwardIterator
, typename _Tp
>
1252 remove(_ForwardIterator __first
, _ForwardIterator __last
,
1255 // concept requirements
1256 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1258 __glibcxx_function_requires(_EqualOpConcept
<
1259 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1260 __glibcxx_requires_valid_range(__first
, __last
);
1262 __first
= std::find(__first
, __last
, __value
);
1263 _ForwardIterator __i
= __first
;
1264 return __first
== __last
? __first
1265 : std::remove_copy(++__i
, __last
,
1270 * @brief Remove elements from a sequence using a predicate.
1271 * @param first A forward iterator.
1272 * @param last A forward iterator.
1273 * @param pred A predicate.
1274 * @return An iterator designating the end of the resulting sequence.
1276 * All elements for which @p pred returns true are removed from the range
1279 * remove_if() is stable, so the relative order of elements that are
1280 * not removed is unchanged.
1282 * Elements between the end of the resulting sequence and @p last
1283 * are still present, but their value is unspecified.
1285 template<typename _ForwardIterator
, typename _Predicate
>
1287 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1290 // concept requirements
1291 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1293 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1294 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1295 __glibcxx_requires_valid_range(__first
, __last
);
1297 __first
= std::find_if(__first
, __last
, __pred
);
1298 _ForwardIterator __i
= __first
;
1299 return __first
== __last
? __first
1300 : std::remove_copy_if(++__i
, __last
,
1306 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1308 * overloaded for forward iterators and output iterator as result.
1311 template<typename _ForwardIterator
, typename _OutputIterator
>
1313 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1314 _OutputIterator __result
,
1315 forward_iterator_tag
, output_iterator_tag
)
1317 // concept requirements -- taken care of in dispatching function
1318 _ForwardIterator __next
= __first
;
1319 *__result
= *__first
;
1320 while (++__next
!= __last
)
1321 if (!(*__first
== *__next
))
1324 *++__result
= *__first
;
1331 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1333 * overloaded for input iterators and output iterator as result.
1336 template<typename _InputIterator
, typename _OutputIterator
>
1338 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1339 _OutputIterator __result
,
1340 input_iterator_tag
, output_iterator_tag
)
1342 // concept requirements -- taken care of in dispatching function
1343 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1344 *__result
= __value
;
1345 while (++__first
!= __last
)
1346 if (!(__value
== *__first
))
1349 *++__result
= __value
;
1356 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1358 * overloaded for input iterators and forward iterator as result.
1361 template<typename _InputIterator
, typename _ForwardIterator
>
1363 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1364 _ForwardIterator __result
,
1365 input_iterator_tag
, forward_iterator_tag
)
1367 // concept requirements -- taken care of in dispatching function
1368 *__result
= *__first
;
1369 while (++__first
!= __last
)
1370 if (!(*__result
== *__first
))
1371 *++__result
= *__first
;
1377 * This is an uglified
1378 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1380 * overloaded for forward iterators and output iterator as result.
1383 template<typename _ForwardIterator
, typename _OutputIterator
,
1384 typename _BinaryPredicate
>
1386 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1387 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1388 forward_iterator_tag
, output_iterator_tag
)
1390 // concept requirements -- iterators already checked
1391 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1392 typename iterator_traits
<_ForwardIterator
>::value_type
,
1393 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1395 _ForwardIterator __next
= __first
;
1396 *__result
= *__first
;
1397 while (++__next
!= __last
)
1398 if (!__binary_pred(*__first
, *__next
))
1401 *++__result
= *__first
;
1408 * This is an uglified
1409 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1411 * overloaded for input iterators and output iterator as result.
1414 template<typename _InputIterator
, typename _OutputIterator
,
1415 typename _BinaryPredicate
>
1417 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1418 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1419 input_iterator_tag
, output_iterator_tag
)
1421 // concept requirements -- iterators already checked
1422 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1423 typename iterator_traits
<_InputIterator
>::value_type
,
1424 typename iterator_traits
<_InputIterator
>::value_type
>)
1426 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1427 *__result
= __value
;
1428 while (++__first
!= __last
)
1429 if (!__binary_pred(__value
, *__first
))
1432 *++__result
= __value
;
1439 * This is an uglified
1440 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1442 * overloaded for input iterators and forward iterator as result.
1445 template<typename _InputIterator
, typename _ForwardIterator
,
1446 typename _BinaryPredicate
>
1448 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1449 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1450 input_iterator_tag
, forward_iterator_tag
)
1452 // concept requirements -- iterators already checked
1453 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1454 typename iterator_traits
<_ForwardIterator
>::value_type
,
1455 typename iterator_traits
<_InputIterator
>::value_type
>)
1457 *__result
= *__first
;
1458 while (++__first
!= __last
)
1459 if (!__binary_pred(*__result
, *__first
))
1460 *++__result
= *__first
;
1465 * @brief Copy a sequence, removing consecutive duplicate values.
1466 * @param first An input iterator.
1467 * @param last An input iterator.
1468 * @param result An output iterator.
1469 * @return An iterator designating the end of the resulting sequence.
1471 * Copies each element in the range @p [first,last) to the range
1472 * beginning at @p result, except that only the first element is copied
1473 * from groups of consecutive elements that compare equal.
1474 * unique_copy() is stable, so the relative order of elements that are
1475 * copied is unchanged.
1478 * _GLIBCXX_RESOLVE_LIB_DEFECTS
1479 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
1481 * _GLIBCXX_RESOLVE_LIB_DEFECTS
1482 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
1486 template<typename _InputIterator
, typename _OutputIterator
>
1487 inline _OutputIterator
1488 unique_copy(_InputIterator __first
, _InputIterator __last
,
1489 _OutputIterator __result
)
1491 // concept requirements
1492 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1493 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1494 typename iterator_traits
<_InputIterator
>::value_type
>)
1495 __glibcxx_function_requires(_EqualityComparableConcept
<
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
,
1502 std::__iterator_category(__first
),
1503 std::__iterator_category(__result
));
1507 * @brief Copy a sequence, removing consecutive values using a predicate.
1508 * @param first An input iterator.
1509 * @param last An input iterator.
1510 * @param result An output iterator.
1511 * @param binary_pred A binary predicate.
1512 * @return An iterator designating the end of the resulting sequence.
1514 * Copies each element in the range @p [first,last) to the range
1515 * beginning at @p result, except that only the first element is copied
1516 * from groups of consecutive elements for which @p binary_pred returns
1518 * unique_copy() is stable, so the relative order of elements that are
1519 * copied is unchanged.
1522 * _GLIBCXX_RESOLVE_LIB_DEFECTS
1523 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
1526 template<typename _InputIterator
, typename _OutputIterator
,
1527 typename _BinaryPredicate
>
1528 inline _OutputIterator
1529 unique_copy(_InputIterator __first
, _InputIterator __last
,
1530 _OutputIterator __result
,
1531 _BinaryPredicate __binary_pred
)
1533 // concept requirements -- predicates checked later
1534 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1535 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1536 typename iterator_traits
<_InputIterator
>::value_type
>)
1537 __glibcxx_requires_valid_range(__first
, __last
);
1539 if (__first
== __last
)
1541 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
1542 std::__iterator_category(__first
),
1543 std::__iterator_category(__result
));
1547 * @brief Remove consecutive duplicate values from a sequence.
1548 * @param first A forward iterator.
1549 * @param last A forward iterator.
1550 * @return An iterator designating the end of the resulting sequence.
1552 * Removes all but the first element from each group of consecutive
1553 * values that compare equal.
1554 * unique() is stable, so the relative order of elements that are
1555 * not removed is unchanged.
1556 * Elements between the end of the resulting sequence and @p last
1557 * are still present, but their value is unspecified.
1559 template<typename _ForwardIterator
>
1561 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1563 // concept requirements
1564 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1566 __glibcxx_function_requires(_EqualityComparableConcept
<
1567 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1568 __glibcxx_requires_valid_range(__first
, __last
);
1570 // Skip the beginning, if already unique.
1571 __first
= std::adjacent_find(__first
, __last
);
1572 if (__first
== __last
)
1575 // Do the real copy work.
1576 _ForwardIterator __dest
= __first
;
1578 while (++__first
!= __last
)
1579 if (!(*__dest
== *__first
))
1580 *++__dest
= *__first
;
1585 * @brief Remove consecutive values from a sequence using a predicate.
1586 * @param first A forward iterator.
1587 * @param last A forward iterator.
1588 * @param binary_pred A binary predicate.
1589 * @return An iterator designating the end of the resulting sequence.
1591 * Removes all but the first element from each group of consecutive
1592 * values for which @p binary_pred returns true.
1593 * unique() is stable, so the relative order of elements that are
1594 * not removed is unchanged.
1595 * Elements between the end of the resulting sequence and @p last
1596 * are still present, but their value is unspecified.
1598 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1600 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1601 _BinaryPredicate __binary_pred
)
1603 // concept requirements
1604 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1606 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1607 typename iterator_traits
<_ForwardIterator
>::value_type
,
1608 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1609 __glibcxx_requires_valid_range(__first
, __last
);
1611 // Skip the beginning, if already unique.
1612 __first
= std::adjacent_find(__first
, __last
, __binary_pred
);
1613 if (__first
== __last
)
1616 // Do the real copy work.
1617 _ForwardIterator __dest
= __first
;
1619 while (++__first
!= __last
)
1620 if (!__binary_pred(*__dest
, *__first
))
1621 *++__dest
= *__first
;
1627 * This is an uglified reverse(_BidirectionalIterator,
1628 * _BidirectionalIterator)
1629 * overloaded for bidirectional iterators.
1632 template<typename _BidirectionalIterator
>
1634 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1635 bidirectional_iterator_tag
)
1638 if (__first
== __last
|| __first
== --__last
)
1642 std::iter_swap(__first
, __last
);
1649 * This is an uglified reverse(_BidirectionalIterator,
1650 * _BidirectionalIterator)
1651 * overloaded for random access iterators.
1654 template<typename _RandomAccessIterator
>
1656 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1657 random_access_iterator_tag
)
1659 if (__first
== __last
)
1662 while (__first
< __last
)
1664 std::iter_swap(__first
, __last
);
1671 * @brief Reverse a sequence.
1672 * @param first A bidirectional iterator.
1673 * @param last A bidirectional iterator.
1674 * @return reverse() returns no value.
1676 * Reverses the order of the elements in the range @p [first,last),
1677 * so that the first element becomes the last etc.
1678 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1679 * swaps @p *(first+i) and @p *(last-(i+1))
1681 template<typename _BidirectionalIterator
>
1683 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1685 // concept requirements
1686 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1687 _BidirectionalIterator
>)
1688 __glibcxx_requires_valid_range(__first
, __last
);
1689 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1693 * @brief Copy a sequence, reversing its elements.
1694 * @param first A bidirectional iterator.
1695 * @param last A bidirectional iterator.
1696 * @param result An output iterator.
1697 * @return An iterator designating the end of the resulting sequence.
1699 * Copies the elements in the range @p [first,last) to the range
1700 * @p [result,result+(last-first)) such that the order of the
1701 * elements is reversed.
1702 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1703 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1704 * The ranges @p [first,last) and @p [result,result+(last-first))
1707 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1709 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1710 _OutputIterator __result
)
1712 // concept requirements
1713 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1714 _BidirectionalIterator
>)
1715 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1716 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1717 __glibcxx_requires_valid_range(__first
, __last
);
1719 while (__first
!= __last
)
1722 *__result
= *__last
;
1731 * This is a helper function for the rotate algorithm specialized on RAIs.
1732 * It returns the greatest common divisor of two integer values.
1735 template<typename _EuclideanRingElement
>
1736 _EuclideanRingElement
1737 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1741 _EuclideanRingElement __t
= __m
% __n
;
1750 * This is a helper function for the rotate algorithm.
1753 template<typename _ForwardIterator
>
1755 __rotate(_ForwardIterator __first
,
1756 _ForwardIterator __middle
,
1757 _ForwardIterator __last
,
1758 forward_iterator_tag
)
1760 if (__first
== __middle
|| __last
== __middle
)
1763 _ForwardIterator __first2
= __middle
;
1766 swap(*__first
, *__first2
);
1769 if (__first
== __middle
)
1770 __middle
= __first2
;
1772 while (__first2
!= __last
);
1774 __first2
= __middle
;
1776 while (__first2
!= __last
)
1778 swap(*__first
, *__first2
);
1781 if (__first
== __middle
)
1782 __middle
= __first2
;
1783 else if (__first2
== __last
)
1784 __first2
= __middle
;
1790 * This is a helper function for the rotate algorithm.
1793 template<typename _BidirectionalIterator
>
1795 __rotate(_BidirectionalIterator __first
,
1796 _BidirectionalIterator __middle
,
1797 _BidirectionalIterator __last
,
1798 bidirectional_iterator_tag
)
1800 // concept requirements
1801 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1802 _BidirectionalIterator
>)
1804 if (__first
== __middle
|| __last
== __middle
)
1807 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1808 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1810 while (__first
!= __middle
&& __middle
!= __last
)
1812 swap(*__first
, *--__last
);
1816 if (__first
== __middle
)
1817 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1819 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1824 * This is a helper function for the rotate algorithm.
1827 template<typename _RandomAccessIterator
>
1829 __rotate(_RandomAccessIterator __first
,
1830 _RandomAccessIterator __middle
,
1831 _RandomAccessIterator __last
,
1832 random_access_iterator_tag
)
1834 // concept requirements
1835 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1836 _RandomAccessIterator
>)
1838 if (__first
== __middle
|| __last
== __middle
)
1841 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1843 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1846 const _Distance __n
= __last
- __first
;
1847 const _Distance __k
= __middle
- __first
;
1848 const _Distance __l
= __n
- __k
;
1852 std::swap_ranges(__first
, __middle
, __middle
);
1856 const _Distance __d
= __gcd(__n
, __k
);
1858 for (_Distance __i
= 0; __i
< __d
; __i
++)
1860 _ValueType __tmp
= *__first
;
1861 _RandomAccessIterator __p
= __first
;
1865 for (_Distance __j
= 0; __j
< __l
/ __d
; __j
++)
1867 if (__p
> __first
+ __l
)
1869 *__p
= *(__p
- __l
);
1873 *__p
= *(__p
+ __k
);
1879 for (_Distance __j
= 0; __j
< __k
/ __d
- 1; __j
++)
1881 if (__p
< __last
- __k
)
1883 *__p
= *(__p
+ __k
);
1886 *__p
= * (__p
- __l
);
1897 * @brief Rotate the elements of a sequence.
1898 * @param first A forward iterator.
1899 * @param middle A forward iterator.
1900 * @param last A forward iterator.
1903 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1904 * positions so that the element at @p middle is moved to @p first, the
1905 * element at @p middle+1 is moved to @first+1 and so on for each element
1906 * in the range @p [first,last).
1908 * This effectively swaps the ranges @p [first,middle) and
1911 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1912 * each @p n in the range @p [0,last-first).
1914 template<typename _ForwardIterator
>
1916 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1917 _ForwardIterator __last
)
1919 // concept requirements
1920 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1922 __glibcxx_requires_valid_range(__first
, __middle
);
1923 __glibcxx_requires_valid_range(__middle
, __last
);
1925 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1927 std::__rotate(__first
, __middle
, __last
, _IterType());
1931 * @brief Copy a sequence, rotating its elements.
1932 * @param first A forward iterator.
1933 * @param middle A forward iterator.
1934 * @param last A forward iterator.
1935 * @param result An output iterator.
1936 * @return An iterator designating the end of the resulting sequence.
1938 * Copies the elements of the range @p [first,last) to the range
1939 * beginning at @result, rotating the copied elements by @p (middle-first)
1940 * positions so that the element at @p middle is moved to @p result, the
1941 * element at @p middle+1 is moved to @result+1 and so on for each element
1942 * in the range @p [first,last).
1944 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1945 * each @p n in the range @p [0,last-first).
1947 template<typename _ForwardIterator
, typename _OutputIterator
>
1949 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1950 _ForwardIterator __last
, _OutputIterator __result
)
1952 // concept requirements
1953 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1954 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1955 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1956 __glibcxx_requires_valid_range(__first
, __middle
);
1957 __glibcxx_requires_valid_range(__middle
, __last
);
1959 return std::copy(__first
, __middle
,
1960 std::copy(__middle
, __last
, __result
));
1964 * @brief Randomly shuffle the elements of a sequence.
1965 * @param first A forward iterator.
1966 * @param last A forward iterator.
1969 * Reorder the elements in the range @p [first,last) using a random
1970 * distribution, so that every possible ordering of the sequence is
1973 template<typename _RandomAccessIterator
>
1975 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
1977 // concept requirements
1978 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1979 _RandomAccessIterator
>)
1980 __glibcxx_requires_valid_range(__first
, __last
);
1982 if (__first
!= __last
)
1983 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1984 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
1988 * @brief Shuffle the elements of a sequence using a random number
1990 * @param first A forward iterator.
1991 * @param last A forward iterator.
1992 * @param rand The RNG functor or function.
1995 * Reorders the elements in the range @p [first,last) using @p rand to
1996 * provide a random distribution. Calling @p rand(N) for a positive
1997 * integer @p N should return a randomly chosen integer from the
2000 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
2002 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2003 _RandomNumberGenerator
& __rand
)
2005 // concept requirements
2006 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2007 _RandomAccessIterator
>)
2008 __glibcxx_requires_valid_range(__first
, __last
);
2010 if (__first
== __last
)
2012 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2013 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
2019 * This is a helper function...
2022 template<typename _ForwardIterator
, typename _Predicate
>
2024 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
2026 forward_iterator_tag
)
2028 if (__first
== __last
)
2031 while (__pred(*__first
))
2032 if (++__first
== __last
)
2035 _ForwardIterator __next
= __first
;
2037 while (++__next
!= __last
)
2038 if (__pred(*__next
))
2040 swap(*__first
, *__next
);
2049 * This is a helper function...
2052 template<typename _BidirectionalIterator
, typename _Predicate
>
2053 _BidirectionalIterator
2054 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
2056 bidirectional_iterator_tag
)
2061 if (__first
== __last
)
2063 else if (__pred(*__first
))
2069 if (__first
== __last
)
2071 else if (!__pred(*__last
))
2075 std::iter_swap(__first
, __last
);
2081 * @brief Move elements for which a predicate is true to the beginning
2083 * @param first A forward iterator.
2084 * @param last A forward iterator.
2085 * @param pred A predicate functor.
2086 * @return An iterator @p middle such that @p pred(i) is true for each
2087 * iterator @p i in the range @p [first,middle) and false for each @p i
2088 * in the range @p [middle,last).
2090 * @p pred must not modify its operand. @p partition() does not preserve
2091 * the relative ordering of elements in each group, use
2092 * @p stable_partition() if this is needed.
2094 template<typename _ForwardIterator
, typename _Predicate
>
2095 inline _ForwardIterator
2096 partition(_ForwardIterator __first
, _ForwardIterator __last
,
2099 // concept requirements
2100 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
2102 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
2103 typename iterator_traits
<_ForwardIterator
>::value_type
>)
2104 __glibcxx_requires_valid_range(__first
, __last
);
2106 return std::__partition(__first
, __last
, __pred
,
2107 std::__iterator_category(__first
));
2113 * This is a helper function...
2116 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
2118 __inplace_stable_partition(_ForwardIterator __first
,
2119 _ForwardIterator __last
,
2120 _Predicate __pred
, _Distance __len
)
2123 return __pred(*__first
) ? __last
: __first
;
2124 _ForwardIterator __middle
= __first
;
2125 std::advance(__middle
, __len
/ 2);
2126 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
2130 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
2134 std::rotate(__begin
, __middle
, __end
);
2135 std::advance(__begin
, std::distance(__middle
, __end
));
2141 * This is a helper function...
2144 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
2147 __stable_partition_adaptive(_ForwardIterator __first
,
2148 _ForwardIterator __last
,
2149 _Predicate __pred
, _Distance __len
,
2151 _Distance __buffer_size
)
2153 if (__len
<= __buffer_size
)
2155 _ForwardIterator __result1
= __first
;
2156 _Pointer __result2
= __buffer
;
2157 for ( ; __first
!= __last
; ++__first
)
2158 if (__pred(*__first
))
2160 *__result1
= *__first
;
2165 *__result2
= *__first
;
2168 std::copy(__buffer
, __result2
, __result1
);
2173 _ForwardIterator __middle
= __first
;
2174 std::advance(__middle
, __len
/ 2);
2175 _ForwardIterator __begin
=
2176 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
2177 __len
/ 2, __buffer
,
2179 _ForwardIterator __end
=
2180 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
2182 __buffer
, __buffer_size
);
2183 std::rotate(__begin
, __middle
, __end
);
2184 std::advance(__begin
, std::distance(__middle
, __end
));
2190 * @brief Move elements for which a predicate is true to the beginning
2191 * of a sequence, preserving relative ordering.
2192 * @param first A forward iterator.
2193 * @param last A forward iterator.
2194 * @param pred A predicate functor.
2195 * @return An iterator @p middle such that @p pred(i) is true for each
2196 * iterator @p i in the range @p [first,middle) and false for each @p i
2197 * in the range @p [middle,last).
2199 * Performs the same function as @p partition() with the additional
2200 * guarantee that the relative ordering of elements in each group is
2201 * preserved, so any two elements @p x and @p y in the range
2202 * @p [first,last) such that @p pred(x)==pred(y) will have the same
2203 * relative ordering after calling @p stable_partition().
2205 template<typename _ForwardIterator
, typename _Predicate
>
2207 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
2210 // concept requirements
2211 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
2213 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
2214 typename iterator_traits
<_ForwardIterator
>::value_type
>)
2215 __glibcxx_requires_valid_range(__first
, __last
);
2217 if (__first
== __last
)
2221 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2223 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2226 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
2228 if (__buf
.size() > 0)
2230 std::__stable_partition_adaptive(__first
, __last
, __pred
,
2231 _DistanceType(__buf
.requested_size()),
2232 __buf
.begin(), __buf
.size());
2235 std::__inplace_stable_partition(__first
, __last
, __pred
,
2236 _DistanceType(__buf
.requested_size()));
2242 * This is a helper function...
2245 template<typename _RandomAccessIterator
, typename _Tp
>
2246 _RandomAccessIterator
2247 __unguarded_partition(_RandomAccessIterator __first
,
2248 _RandomAccessIterator __last
, _Tp __pivot
)
2252 while (*__first
< __pivot
)
2255 while (__pivot
< *__last
)
2257 if (!(__first
< __last
))
2259 std::iter_swap(__first
, __last
);
2266 * This is a helper function...
2269 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2270 _RandomAccessIterator
2271 __unguarded_partition(_RandomAccessIterator __first
,
2272 _RandomAccessIterator __last
,
2273 _Tp __pivot
, _Compare __comp
)
2277 while (__comp(*__first
, __pivot
))
2280 while (__comp(__pivot
, *__last
))
2282 if (!(__first
< __last
))
2284 std::iter_swap(__first
, __last
);
2292 * This controls some aspect of the sort routines.
2295 enum { _S_threshold
= 16 };
2299 * This is a helper function for the sort routine.
2302 template<typename _RandomAccessIterator
, typename _Tp
>
2304 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
2306 _RandomAccessIterator __next
= __last
;
2308 while (__val
< *__next
)
2319 * This is a helper function for the sort routine.
2322 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2324 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
2327 _RandomAccessIterator __next
= __last
;
2329 while (__comp(__val
, *__next
))
2340 * This is a helper function for the sort routine.
2343 template<typename _RandomAccessIterator
>
2345 __insertion_sort(_RandomAccessIterator __first
,
2346 _RandomAccessIterator __last
)
2348 if (__first
== __last
)
2351 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2353 typename iterator_traits
<_RandomAccessIterator
>::value_type
2355 if (__val
< *__first
)
2357 std::copy_backward(__first
, __i
, __i
+ 1);
2361 std::__unguarded_linear_insert(__i
, __val
);
2367 * This is a helper function for the sort routine.
2370 template<typename _RandomAccessIterator
, typename _Compare
>
2372 __insertion_sort(_RandomAccessIterator __first
,
2373 _RandomAccessIterator __last
, _Compare __comp
)
2375 if (__first
== __last
) return;
2377 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2379 typename iterator_traits
<_RandomAccessIterator
>::value_type
2381 if (__comp(__val
, *__first
))
2383 std::copy_backward(__first
, __i
, __i
+ 1);
2387 std::__unguarded_linear_insert(__i
, __val
, __comp
);
2393 * This is a helper function for the sort routine.
2396 template<typename _RandomAccessIterator
>
2398 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2399 _RandomAccessIterator __last
)
2401 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2404 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2405 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
2410 * This is a helper function for the sort routine.
2413 template<typename _RandomAccessIterator
, typename _Compare
>
2415 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2416 _RandomAccessIterator __last
, _Compare __comp
)
2418 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2421 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2422 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
2427 * This is a helper function for the sort routine.
2430 template<typename _RandomAccessIterator
>
2432 __final_insertion_sort(_RandomAccessIterator __first
,
2433 _RandomAccessIterator __last
)
2435 if (__last
- __first
> int(_S_threshold
))
2437 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
2438 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
2441 std::__insertion_sort(__first
, __last
);
2446 * This is a helper function for the sort routine.
2449 template<typename _RandomAccessIterator
, typename _Compare
>
2451 __final_insertion_sort(_RandomAccessIterator __first
,
2452 _RandomAccessIterator __last
, _Compare __comp
)
2454 if (__last
- __first
> int(_S_threshold
))
2456 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
2457 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
2461 std::__insertion_sort(__first
, __last
, __comp
);
2466 * This is a helper function for the sort routine.
2469 template<typename _Size
>
2474 for (__k
= 0; __n
!= 1; __n
>>= 1)
2480 * @brief Sort the smallest elements of a sequence.
2481 * @param first An iterator.
2482 * @param middle Another iterator.
2483 * @param last Another iterator.
2486 * Sorts the smallest @p (middle-first) elements in the range
2487 * @p [first,last) and moves them to the range @p [first,middle). The
2488 * order of the remaining elements in the range @p [middle,last) is
2490 * After the sort if @p i and @j are iterators in the range
2491 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2492 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
2494 template<typename _RandomAccessIterator
>
2496 partial_sort(_RandomAccessIterator __first
,
2497 _RandomAccessIterator __middle
,
2498 _RandomAccessIterator __last
)
2500 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2503 // concept requirements
2504 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2505 _RandomAccessIterator
>)
2506 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2507 __glibcxx_requires_valid_range(__first
, __middle
);
2508 __glibcxx_requires_valid_range(__middle
, __last
);
2510 std::make_heap(__first
, __middle
);
2511 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
2512 if (*__i
< *__first
)
2513 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
));
2514 std::sort_heap(__first
, __middle
);
2518 * @brief Sort the smallest elements of a sequence using a predicate
2520 * @param first An iterator.
2521 * @param middle Another iterator.
2522 * @param last Another iterator.
2523 * @param comp A comparison functor.
2526 * Sorts the smallest @p (middle-first) elements in the range
2527 * @p [first,last) and moves them to the range @p [first,middle). The
2528 * order of the remaining elements in the range @p [middle,last) is
2530 * After the sort if @p i and @j are iterators in the range
2531 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2532 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
2535 template<typename _RandomAccessIterator
, typename _Compare
>
2537 partial_sort(_RandomAccessIterator __first
,
2538 _RandomAccessIterator __middle
,
2539 _RandomAccessIterator __last
,
2542 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2545 // concept requirements
2546 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2547 _RandomAccessIterator
>)
2548 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2549 _ValueType
, _ValueType
>)
2550 __glibcxx_requires_valid_range(__first
, __middle
);
2551 __glibcxx_requires_valid_range(__middle
, __last
);
2553 std::make_heap(__first
, __middle
, __comp
);
2554 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
2555 if (__comp(*__i
, *__first
))
2556 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
), __comp
);
2557 std::sort_heap(__first
, __middle
, __comp
);
2561 * @brief Copy the smallest elements of a sequence.
2562 * @param first An iterator.
2563 * @param last Another iterator.
2564 * @param result_first A random-access iterator.
2565 * @param result_last Another random-access iterator.
2566 * @return An iterator indicating the end of the resulting sequence.
2568 * Copies and sorts the smallest N values from the range @p [first,last)
2569 * to the range beginning at @p result_first, where the number of
2570 * elements to be copied, @p N, is the smaller of @p (last-first) and
2571 * @p (result_last-result_first).
2572 * After the sort if @p i and @j are iterators in the range
2573 * @p [result_first,result_first+N) such that @i precedes @j then
2574 * @p *j<*i is false.
2575 * The value returned is @p result_first+N.
2577 template<typename _InputIterator
, typename _RandomAccessIterator
>
2578 _RandomAccessIterator
2579 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2580 _RandomAccessIterator __result_first
,
2581 _RandomAccessIterator __result_last
)
2583 typedef typename iterator_traits
<_InputIterator
>::value_type
2585 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2587 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2590 // concept requirements
2591 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2592 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2594 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
2596 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
2597 __glibcxx_requires_valid_range(__first
, __last
);
2598 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2600 if (__result_first
== __result_last
)
2601 return __result_last
;
2602 _RandomAccessIterator __result_real_last
= __result_first
;
2603 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2605 *__result_real_last
= *__first
;
2606 ++__result_real_last
;
2609 std::make_heap(__result_first
, __result_real_last
);
2610 while (__first
!= __last
)
2612 if (*__first
< *__result_first
)
2613 std::__adjust_heap(__result_first
, _DistanceType(0),
2614 _DistanceType(__result_real_last
2616 _InputValueType(*__first
));
2619 std::sort_heap(__result_first
, __result_real_last
);
2620 return __result_real_last
;
2624 * @brief Copy the smallest elements of a sequence using a predicate for
2626 * @param first An input iterator.
2627 * @param last Another input iterator.
2628 * @param result_first A random-access iterator.
2629 * @param result_last Another random-access iterator.
2630 * @param comp A comparison functor.
2631 * @return An iterator indicating the end of the resulting sequence.
2633 * Copies and sorts the smallest N values from the range @p [first,last)
2634 * to the range beginning at @p result_first, where the number of
2635 * elements to be copied, @p N, is the smaller of @p (last-first) and
2636 * @p (result_last-result_first).
2637 * After the sort if @p i and @j are iterators in the range
2638 * @p [result_first,result_first+N) such that @i precedes @j then
2639 * @p comp(*j,*i) is false.
2640 * The value returned is @p result_first+N.
2642 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
2643 _RandomAccessIterator
2644 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2645 _RandomAccessIterator __result_first
,
2646 _RandomAccessIterator __result_last
,
2649 typedef typename iterator_traits
<_InputIterator
>::value_type
2651 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2653 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2656 // concept requirements
2657 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2658 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2659 _RandomAccessIterator
>)
2660 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2662 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2663 _InputValueType
, _OutputValueType
>)
2664 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2665 _OutputValueType
, _OutputValueType
>)
2666 __glibcxx_requires_valid_range(__first
, __last
);
2667 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2669 if (__result_first
== __result_last
)
2670 return __result_last
;
2671 _RandomAccessIterator __result_real_last
= __result_first
;
2672 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2674 *__result_real_last
= *__first
;
2675 ++__result_real_last
;
2678 std::make_heap(__result_first
, __result_real_last
, __comp
);
2679 while (__first
!= __last
)
2681 if (__comp(*__first
, *__result_first
))
2682 std::__adjust_heap(__result_first
, _DistanceType(0),
2683 _DistanceType(__result_real_last
2685 _InputValueType(*__first
),
2689 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2690 return __result_real_last
;
2695 * This is a helper function for the sort routine.
2698 template<typename _RandomAccessIterator
, typename _Size
>
2700 __introsort_loop(_RandomAccessIterator __first
,
2701 _RandomAccessIterator __last
,
2702 _Size __depth_limit
)
2704 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2707 while (__last
- __first
> int(_S_threshold
))
2709 if (__depth_limit
== 0)
2711 std::partial_sort(__first
, __last
, __last
);
2715 _RandomAccessIterator __cut
=
2716 std::__unguarded_partition(__first
, __last
,
2717 _ValueType(std::__median(*__first
,
2724 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2731 * This is a helper function for the sort routine.
2734 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2736 __introsort_loop(_RandomAccessIterator __first
,
2737 _RandomAccessIterator __last
,
2738 _Size __depth_limit
, _Compare __comp
)
2740 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2743 while (__last
- __first
> int(_S_threshold
))
2745 if (__depth_limit
== 0)
2747 std::partial_sort(__first
, __last
, __last
, __comp
);
2751 _RandomAccessIterator __cut
=
2752 std::__unguarded_partition(__first
, __last
,
2753 _ValueType(std::__median(*__first
,
2761 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2767 * @brief Sort the elements of a sequence.
2768 * @param first An iterator.
2769 * @param last Another iterator.
2772 * Sorts the elements in the range @p [first,last) in ascending order,
2773 * such that @p *(i+1)<*i is false for each iterator @p i in the range
2774 * @p [first,last-1).
2776 * The relative ordering of equivalent elements is not preserved, use
2777 * @p stable_sort() if this is needed.
2779 template<typename _RandomAccessIterator
>
2781 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
2783 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2786 // concept requirements
2787 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2788 _RandomAccessIterator
>)
2789 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2790 __glibcxx_requires_valid_range(__first
, __last
);
2792 if (__first
!= __last
)
2794 std::__introsort_loop(__first
, __last
, __lg(__last
- __first
) * 2);
2795 std::__final_insertion_sort(__first
, __last
);
2800 * @brief Sort the elements of a sequence using a predicate for comparison.
2801 * @param first An iterator.
2802 * @param last Another iterator.
2803 * @param comp A comparison functor.
2806 * Sorts the elements in the range @p [first,last) in ascending order,
2807 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
2808 * range @p [first,last-1).
2810 * The relative ordering of equivalent elements is not preserved, use
2811 * @p stable_sort() if this is needed.
2813 template<typename _RandomAccessIterator
, typename _Compare
>
2815 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2818 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2821 // concept requirements
2822 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2823 _RandomAccessIterator
>)
2824 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
2826 __glibcxx_requires_valid_range(__first
, __last
);
2828 if (__first
!= __last
)
2830 std::__introsort_loop(__first
, __last
, __lg(__last
- __first
) * 2,
2832 std::__final_insertion_sort(__first
, __last
, __comp
);
2837 * @brief Finds the first position in which @a val could be inserted
2838 * without changing the ordering.
2839 * @param first An iterator.
2840 * @param last Another iterator.
2841 * @param val The search term.
2842 * @return An iterator pointing to the first element "not less than" @a val,
2843 * or end() if every element is less than @a val.
2844 * @ingroup binarysearch
2846 template<typename _ForwardIterator
, typename _Tp
>
2848 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2851 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2853 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2856 // concept requirements
2857 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2858 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2859 __glibcxx_requires_partitioned(__first
, __last
, __val
);
2861 _DistanceType __len
= std::distance(__first
, __last
);
2862 _DistanceType __half
;
2863 _ForwardIterator __middle
;
2867 __half
= __len
>> 1;
2869 std::advance(__middle
, __half
);
2870 if (*__middle
< __val
)
2874 __len
= __len
- __half
- 1;
2883 * @brief Finds the first position in which @a val could be inserted
2884 * without changing the ordering.
2885 * @param first An iterator.
2886 * @param last Another iterator.
2887 * @param val The search term.
2888 * @param comp A functor to use for comparisons.
2889 * @return An iterator pointing to the first element "not less than" @a val,
2890 * or end() if every element is less than @a val.
2891 * @ingroup binarysearch
2893 * The comparison function should have the same effects on ordering as
2894 * the function used for the initial sort.
2896 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2898 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2899 const _Tp
& __val
, _Compare __comp
)
2901 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2903 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2906 // concept requirements
2907 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2908 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2910 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
2912 _DistanceType __len
= std::distance(__first
, __last
);
2913 _DistanceType __half
;
2914 _ForwardIterator __middle
;
2918 __half
= __len
>> 1;
2920 std::advance(__middle
, __half
);
2921 if (__comp(*__middle
, __val
))
2925 __len
= __len
- __half
- 1;
2934 * @brief Finds the last position in which @a val could be inserted
2935 * without changing the ordering.
2936 * @param first An iterator.
2937 * @param last Another iterator.
2938 * @param val The search term.
2939 * @return An iterator pointing to the first element greater than @a val,
2940 * or end() if no elements are greater than @a val.
2941 * @ingroup binarysearch
2943 template<typename _ForwardIterator
, typename _Tp
>
2945 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2948 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2950 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2953 // concept requirements
2954 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2955 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2956 __glibcxx_requires_partitioned(__first
, __last
, __val
);
2958 _DistanceType __len
= std::distance(__first
, __last
);
2959 _DistanceType __half
;
2960 _ForwardIterator __middle
;
2964 __half
= __len
>> 1;
2966 std::advance(__middle
, __half
);
2967 if (__val
< *__middle
)
2973 __len
= __len
- __half
- 1;
2980 * @brief Finds the last position in which @a val could be inserted
2981 * without changing the ordering.
2982 * @param first An iterator.
2983 * @param last Another iterator.
2984 * @param val The search term.
2985 * @param comp A functor to use for comparisons.
2986 * @return An iterator pointing to the first element greater than @a val,
2987 * or end() if no elements are greater than @a val.
2988 * @ingroup binarysearch
2990 * The comparison function should have the same effects on ordering as
2991 * the function used for the initial sort.
2993 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2995 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2996 const _Tp
& __val
, _Compare __comp
)
2998 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3000 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3003 // concept requirements
3004 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3005 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3007 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
3009 _DistanceType __len
= std::distance(__first
, __last
);
3010 _DistanceType __half
;
3011 _ForwardIterator __middle
;
3015 __half
= __len
>> 1;
3017 std::advance(__middle
, __half
);
3018 if (__comp(__val
, *__middle
))
3024 __len
= __len
- __half
- 1;
3032 * This is a helper function for the merge routines.
3035 template<typename _BidirectionalIterator
, typename _Distance
>
3037 __merge_without_buffer(_BidirectionalIterator __first
,
3038 _BidirectionalIterator __middle
,
3039 _BidirectionalIterator __last
,
3040 _Distance __len1
, _Distance __len2
)
3042 if (__len1
== 0 || __len2
== 0)
3044 if (__len1
+ __len2
== 2)
3046 if (*__middle
< *__first
)
3047 std::iter_swap(__first
, __middle
);
3050 _BidirectionalIterator __first_cut
= __first
;
3051 _BidirectionalIterator __second_cut
= __middle
;
3052 _Distance __len11
= 0;
3053 _Distance __len22
= 0;
3054 if (__len1
> __len2
)
3056 __len11
= __len1
/ 2;
3057 std::advance(__first_cut
, __len11
);
3058 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
3059 __len22
= std::distance(__middle
, __second_cut
);
3063 __len22
= __len2
/ 2;
3064 std::advance(__second_cut
, __len22
);
3065 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
3066 __len11
= std::distance(__first
, __first_cut
);
3068 std::rotate(__first_cut
, __middle
, __second_cut
);
3069 _BidirectionalIterator __new_middle
= __first_cut
;
3070 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3071 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3073 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3074 __len1
- __len11
, __len2
- __len22
);
3079 * This is a helper function for the merge routines.
3082 template<typename _BidirectionalIterator
, typename _Distance
,
3085 __merge_without_buffer(_BidirectionalIterator __first
,
3086 _BidirectionalIterator __middle
,
3087 _BidirectionalIterator __last
,
3088 _Distance __len1
, _Distance __len2
,
3091 if (__len1
== 0 || __len2
== 0)
3093 if (__len1
+ __len2
== 2)
3095 if (__comp(*__middle
, *__first
))
3096 std::iter_swap(__first
, __middle
);
3099 _BidirectionalIterator __first_cut
= __first
;
3100 _BidirectionalIterator __second_cut
= __middle
;
3101 _Distance __len11
= 0;
3102 _Distance __len22
= 0;
3103 if (__len1
> __len2
)
3105 __len11
= __len1
/ 2;
3106 std::advance(__first_cut
, __len11
);
3107 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3109 __len22
= std::distance(__middle
, __second_cut
);
3113 __len22
= __len2
/ 2;
3114 std::advance(__second_cut
, __len22
);
3115 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3117 __len11
= std::distance(__first
, __first_cut
);
3119 std::rotate(__first_cut
, __middle
, __second_cut
);
3120 _BidirectionalIterator __new_middle
= __first_cut
;
3121 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3122 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3123 __len11
, __len22
, __comp
);
3124 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3125 __len1
- __len11
, __len2
- __len22
, __comp
);
3130 * This is a helper function for the stable sorting routines.
3133 template<typename _RandomAccessIterator
>
3135 __inplace_stable_sort(_RandomAccessIterator __first
,
3136 _RandomAccessIterator __last
)
3138 if (__last
- __first
< 15)
3140 std::__insertion_sort(__first
, __last
);
3143 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3144 std::__inplace_stable_sort(__first
, __middle
);
3145 std::__inplace_stable_sort(__middle
, __last
);
3146 std::__merge_without_buffer(__first
, __middle
, __last
,
3153 * This is a helper function for the stable sorting routines.
3156 template<typename _RandomAccessIterator
, typename _Compare
>
3158 __inplace_stable_sort(_RandomAccessIterator __first
,
3159 _RandomAccessIterator __last
, _Compare __comp
)
3161 if (__last
- __first
< 15)
3163 std::__insertion_sort(__first
, __last
, __comp
);
3166 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3167 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3168 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3169 std::__merge_without_buffer(__first
, __middle
, __last
,
3176 * @brief Merges two sorted ranges.
3177 * @param first1 An iterator.
3178 * @param first2 Another iterator.
3179 * @param last1 Another iterator.
3180 * @param last2 Another iterator.
3181 * @param result An iterator pointing to the end of the merged range.
3182 * @return An iterator pointing to the first element "not less than" @a val.
3184 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
3185 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
3186 * must be sorted, and the output range must not overlap with either of
3187 * the input ranges. The sort is @e stable, that is, for equivalent
3188 * elements in the two ranges, elements from the first range will always
3189 * come before elements from the second.
3191 template<typename _InputIterator1
, typename _InputIterator2
,
3192 typename _OutputIterator
>
3194 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3195 _InputIterator2 __first2
, _InputIterator2 __last2
,
3196 _OutputIterator __result
)
3198 typedef typename iterator_traits
<_InputIterator1
>::value_type
3200 typedef typename iterator_traits
<_InputIterator2
>::value_type
3203 // concept requirements
3204 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3205 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3206 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3208 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3210 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3211 __glibcxx_requires_sorted(__first1
, __last1
);
3212 __glibcxx_requires_sorted(__first2
, __last2
);
3214 while (__first1
!= __last1
&& __first2
!= __last2
)
3216 if (*__first2
< *__first1
)
3218 *__result
= *__first2
;
3223 *__result
= *__first1
;
3228 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
3233 * @brief Merges two sorted ranges.
3234 * @param first1 An iterator.
3235 * @param first2 Another iterator.
3236 * @param last1 Another iterator.
3237 * @param last2 Another iterator.
3238 * @param result An iterator pointing to the end of the merged range.
3239 * @param comp A functor to use for comparisons.
3240 * @return An iterator pointing to the first element "not less than" @a val.
3242 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
3243 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
3244 * must be sorted, and the output range must not overlap with either of
3245 * the input ranges. The sort is @e stable, that is, for equivalent
3246 * elements in the two ranges, elements from the first range will always
3247 * come before elements from the second.
3249 * The comparison function should have the same effects on ordering as
3250 * the function used for the initial sort.
3252 template<typename _InputIterator1
, typename _InputIterator2
,
3253 typename _OutputIterator
, typename _Compare
>
3255 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3256 _InputIterator2 __first2
, _InputIterator2 __last2
,
3257 _OutputIterator __result
, _Compare __comp
)
3259 typedef typename iterator_traits
<_InputIterator1
>::value_type
3261 typedef typename iterator_traits
<_InputIterator2
>::value_type
3264 // concept requirements
3265 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3266 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3267 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3269 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3271 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3272 _ValueType2
, _ValueType1
>)
3273 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
3274 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
3276 while (__first1
!= __last1
&& __first2
!= __last2
)
3278 if (__comp(*__first2
, *__first1
))
3280 *__result
= *__first2
;
3285 *__result
= *__first1
;
3290 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
3294 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3297 __merge_sort_loop(_RandomAccessIterator1 __first
,
3298 _RandomAccessIterator1 __last
,
3299 _RandomAccessIterator2 __result
,
3300 _Distance __step_size
)
3302 const _Distance __two_step
= 2 * __step_size
;
3304 while (__last
- __first
>= __two_step
)
3306 __result
= std::merge(__first
, __first
+ __step_size
,
3307 __first
+ __step_size
, __first
+ __two_step
,
3309 __first
+= __two_step
;
3312 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3313 std::merge(__first
, __first
+ __step_size
, __first
+ __step_size
, __last
,
3317 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3318 typename _Distance
, typename _Compare
>
3320 __merge_sort_loop(_RandomAccessIterator1 __first
,
3321 _RandomAccessIterator1 __last
,
3322 _RandomAccessIterator2 __result
, _Distance __step_size
,
3325 const _Distance __two_step
= 2 * __step_size
;
3327 while (__last
- __first
>= __two_step
)
3329 __result
= std::merge(__first
, __first
+ __step_size
,
3330 __first
+ __step_size
, __first
+ __two_step
,
3333 __first
+= __two_step
;
3335 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3337 std::merge(__first
, __first
+ __step_size
,
3338 __first
+ __step_size
, __last
,
3343 enum { _S_chunk_size
= 7 };
3345 template<typename _RandomAccessIterator
, typename _Distance
>
3347 __chunk_insertion_sort(_RandomAccessIterator __first
,
3348 _RandomAccessIterator __last
,
3349 _Distance __chunk_size
)
3351 while (__last
- __first
>= __chunk_size
)
3353 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3354 __first
+= __chunk_size
;
3356 std::__insertion_sort(__first
, __last
);
3359 template<typename _RandomAccessIterator
, typename _Distance
, typename _Compare
>
3361 __chunk_insertion_sort(_RandomAccessIterator __first
,
3362 _RandomAccessIterator __last
,
3363 _Distance __chunk_size
, _Compare __comp
)
3365 while (__last
- __first
>= __chunk_size
)
3367 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3368 __first
+= __chunk_size
;
3370 std::__insertion_sort(__first
, __last
, __comp
);
3373 template<typename _RandomAccessIterator
, typename _Pointer
>
3375 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3376 _RandomAccessIterator __last
,
3379 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3382 const _Distance __len
= __last
- __first
;
3383 const _Pointer __buffer_last
= __buffer
+ __len
;
3385 _Distance __step_size
= _S_chunk_size
;
3386 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3388 while (__step_size
< __len
)
3390 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3392 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3397 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3399 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3400 _RandomAccessIterator __last
,
3401 _Pointer __buffer
, _Compare __comp
)
3403 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3406 const _Distance __len
= __last
- __first
;
3407 const _Pointer __buffer_last
= __buffer
+ __len
;
3409 _Distance __step_size
= _S_chunk_size
;
3410 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3412 while (__step_size
< __len
)
3414 std::__merge_sort_loop(__first
, __last
, __buffer
,
3415 __step_size
, __comp
);
3417 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3418 __step_size
, __comp
);
3425 * This is a helper function for the merge routines.
3428 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3429 typename _BidirectionalIterator3
>
3430 _BidirectionalIterator3
3431 __merge_backward(_BidirectionalIterator1 __first1
,
3432 _BidirectionalIterator1 __last1
,
3433 _BidirectionalIterator2 __first2
,
3434 _BidirectionalIterator2 __last2
,
3435 _BidirectionalIterator3 __result
)
3437 if (__first1
== __last1
)
3438 return std::copy_backward(__first2
, __last2
, __result
);
3439 if (__first2
== __last2
)
3440 return std::copy_backward(__first1
, __last1
, __result
);
3445 if (*__last2
< *__last1
)
3447 *--__result
= *__last1
;
3448 if (__first1
== __last1
)
3449 return std::copy_backward(__first2
, ++__last2
, __result
);
3454 *--__result
= *__last2
;
3455 if (__first2
== __last2
)
3456 return std::copy_backward(__first1
, ++__last1
, __result
);
3464 * This is a helper function for the merge routines.
3467 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3468 typename _BidirectionalIterator3
, typename _Compare
>
3469 _BidirectionalIterator3
3470 __merge_backward(_BidirectionalIterator1 __first1
,
3471 _BidirectionalIterator1 __last1
,
3472 _BidirectionalIterator2 __first2
,
3473 _BidirectionalIterator2 __last2
,
3474 _BidirectionalIterator3 __result
,
3477 if (__first1
== __last1
)
3478 return std::copy_backward(__first2
, __last2
, __result
);
3479 if (__first2
== __last2
)
3480 return std::copy_backward(__first1
, __last1
, __result
);
3485 if (__comp(*__last2
, *__last1
))
3487 *--__result
= *__last1
;
3488 if (__first1
== __last1
)
3489 return std::copy_backward(__first2
, ++__last2
, __result
);
3494 *--__result
= *__last2
;
3495 if (__first2
== __last2
)
3496 return std::copy_backward(__first1
, ++__last1
, __result
);
3504 * This is a helper function for the merge routines.
3507 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3509 _BidirectionalIterator1
3510 __rotate_adaptive(_BidirectionalIterator1 __first
,
3511 _BidirectionalIterator1 __middle
,
3512 _BidirectionalIterator1 __last
,
3513 _Distance __len1
, _Distance __len2
,
3514 _BidirectionalIterator2 __buffer
,
3515 _Distance __buffer_size
)
3517 _BidirectionalIterator2 __buffer_end
;
3518 if (__len1
> __len2
&& __len2
<= __buffer_size
)
3520 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3521 std::copy_backward(__first
, __middle
, __last
);
3522 return std::copy(__buffer
, __buffer_end
, __first
);
3524 else if (__len1
<= __buffer_size
)
3526 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3527 std::copy(__middle
, __last
, __first
);
3528 return std::copy_backward(__buffer
, __buffer_end
, __last
);
3532 std::rotate(__first
, __middle
, __last
);
3533 std::advance(__first
, std::distance(__middle
, __last
));
3540 * This is a helper function for the merge routines.
3543 template<typename _BidirectionalIterator
, typename _Distance
,
3546 __merge_adaptive(_BidirectionalIterator __first
,
3547 _BidirectionalIterator __middle
,
3548 _BidirectionalIterator __last
,
3549 _Distance __len1
, _Distance __len2
,
3550 _Pointer __buffer
, _Distance __buffer_size
)
3552 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
3554 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3555 std::merge(__buffer
, __buffer_end
, __middle
, __last
, __first
);
3557 else if (__len2
<= __buffer_size
)
3559 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3560 std::__merge_backward(__first
, __middle
, __buffer
,
3561 __buffer_end
, __last
);
3565 _BidirectionalIterator __first_cut
= __first
;
3566 _BidirectionalIterator __second_cut
= __middle
;
3567 _Distance __len11
= 0;
3568 _Distance __len22
= 0;
3569 if (__len1
> __len2
)
3571 __len11
= __len1
/ 2;
3572 std::advance(__first_cut
, __len11
);
3573 __second_cut
= std::lower_bound(__middle
, __last
,
3575 __len22
= std::distance(__middle
, __second_cut
);
3579 __len22
= __len2
/ 2;
3580 std::advance(__second_cut
, __len22
);
3581 __first_cut
= std::upper_bound(__first
, __middle
,
3583 __len11
= std::distance(__first
, __first_cut
);
3585 _BidirectionalIterator __new_middle
=
3586 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3587 __len1
- __len11
, __len22
, __buffer
,
3589 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3590 __len22
, __buffer
, __buffer_size
);
3591 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3593 __len2
- __len22
, __buffer
, __buffer_size
);
3599 * This is a helper function for the merge routines.
3602 template<typename _BidirectionalIterator
, typename _Distance
, typename _Pointer
,
3605 __merge_adaptive(_BidirectionalIterator __first
,
3606 _BidirectionalIterator __middle
,
3607 _BidirectionalIterator __last
,
3608 _Distance __len1
, _Distance __len2
,
3609 _Pointer __buffer
, _Distance __buffer_size
,
3612 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
3614 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3615 std::merge(__buffer
, __buffer_end
, __middle
, __last
, __first
, __comp
);
3617 else if (__len2
<= __buffer_size
)
3619 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3620 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
3625 _BidirectionalIterator __first_cut
= __first
;
3626 _BidirectionalIterator __second_cut
= __middle
;
3627 _Distance __len11
= 0;
3628 _Distance __len22
= 0;
3629 if (__len1
> __len2
)
3631 __len11
= __len1
/ 2;
3632 std::advance(__first_cut
, __len11
);
3633 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3635 __len22
= std::distance(__middle
, __second_cut
);
3639 __len22
= __len2
/ 2;
3640 std::advance(__second_cut
, __len22
);
3641 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3643 __len11
= std::distance(__first
, __first_cut
);
3645 _BidirectionalIterator __new_middle
=
3646 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3647 __len1
- __len11
, __len22
, __buffer
,
3649 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3650 __len22
, __buffer
, __buffer_size
, __comp
);
3651 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3653 __len2
- __len22
, __buffer
,
3654 __buffer_size
, __comp
);
3659 * @brief Merges two sorted ranges in place.
3660 * @param first An iterator.
3661 * @param middle Another iterator.
3662 * @param last Another iterator.
3665 * Merges two sorted and consecutive ranges, [first,middle) and
3666 * [middle,last), and puts the result in [first,last). The output will
3667 * be sorted. The sort is @e stable, that is, for equivalent
3668 * elements in the two ranges, elements from the first range will always
3669 * come before elements from the second.
3671 * If enough additional memory is available, this takes (last-first)-1
3672 * comparisons. Otherwise an NlogN algorithm is used, where N is
3673 * distance(first,last).
3675 template<typename _BidirectionalIterator
>
3677 inplace_merge(_BidirectionalIterator __first
,
3678 _BidirectionalIterator __middle
,
3679 _BidirectionalIterator __last
)
3681 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3683 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3686 // concept requirements
3687 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3688 _BidirectionalIterator
>)
3689 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3690 __glibcxx_requires_sorted(__first
, __middle
);
3691 __glibcxx_requires_sorted(__middle
, __last
);
3693 if (__first
== __middle
|| __middle
== __last
)
3696 _DistanceType __len1
= std::distance(__first
, __middle
);
3697 _DistanceType __len2
= std::distance(__middle
, __last
);
3699 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3701 if (__buf
.begin() == 0)
3702 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3704 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3705 __buf
.begin(), _DistanceType(__buf
.size()));
3709 * @brief Merges two sorted ranges in place.
3710 * @param first An iterator.
3711 * @param middle Another iterator.
3712 * @param last Another iterator.
3713 * @param comp A functor to use for comparisons.
3716 * Merges two sorted and consecutive ranges, [first,middle) and
3717 * [middle,last), and puts the result in [first,last). The output will
3718 * be sorted. The sort is @e stable, that is, for equivalent
3719 * elements in the two ranges, elements from the first range will always
3720 * come before elements from the second.
3722 * If enough additional memory is available, this takes (last-first)-1
3723 * comparisons. Otherwise an NlogN algorithm is used, where N is
3724 * distance(first,last).
3726 * The comparison function should have the same effects on ordering as
3727 * the function used for the initial sort.
3729 template<typename _BidirectionalIterator
, typename _Compare
>
3731 inplace_merge(_BidirectionalIterator __first
,
3732 _BidirectionalIterator __middle
,
3733 _BidirectionalIterator __last
,
3736 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3738 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3741 // concept requirements
3742 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3743 _BidirectionalIterator
>)
3744 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3745 _ValueType
, _ValueType
>)
3746 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3747 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3749 if (__first
== __middle
|| __middle
== __last
)
3752 const _DistanceType __len1
= std::distance(__first
, __middle
);
3753 const _DistanceType __len2
= std::distance(__middle
, __last
);
3755 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3757 if (__buf
.begin() == 0)
3758 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3761 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3762 __buf
.begin(), _DistanceType(__buf
.size()),
3766 template<typename _RandomAccessIterator
, typename _Pointer
,
3769 __stable_sort_adaptive(_RandomAccessIterator __first
,
3770 _RandomAccessIterator __last
,
3771 _Pointer __buffer
, _Distance __buffer_size
)
3773 const _Distance __len
= (__last
- __first
+ 1) / 2;
3774 const _RandomAccessIterator __middle
= __first
+ __len
;
3775 if (__len
> __buffer_size
)
3777 std::__stable_sort_adaptive(__first
, __middle
,
3778 __buffer
, __buffer_size
);
3779 std::__stable_sort_adaptive(__middle
, __last
,
3780 __buffer
, __buffer_size
);
3784 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3785 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3787 std::__merge_adaptive(__first
, __middle
, __last
,
3788 _Distance(__middle
- __first
),
3789 _Distance(__last
- __middle
),
3790 __buffer
, __buffer_size
);
3793 template<typename _RandomAccessIterator
, typename _Pointer
,
3794 typename _Distance
, typename _Compare
>
3796 __stable_sort_adaptive(_RandomAccessIterator __first
,
3797 _RandomAccessIterator __last
,
3798 _Pointer __buffer
, _Distance __buffer_size
,
3801 const _Distance __len
= (__last
- __first
+ 1) / 2;
3802 const _RandomAccessIterator __middle
= __first
+ __len
;
3803 if (__len
> __buffer_size
)
3805 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3806 __buffer_size
, __comp
);
3807 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3808 __buffer_size
, __comp
);
3812 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3813 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3815 std::__merge_adaptive(__first
, __middle
, __last
,
3816 _Distance(__middle
- __first
),
3817 _Distance(__last
- __middle
),
3818 __buffer
, __buffer_size
,
3823 * @brief Sort the elements of a sequence, preserving the relative order
3824 * of equivalent elements.
3825 * @param first An iterator.
3826 * @param last Another iterator.
3829 * Sorts the elements in the range @p [first,last) in ascending order,
3830 * such that @p *(i+1)<*i is false for each iterator @p i in the range
3831 * @p [first,last-1).
3833 * The relative ordering of equivalent elements is preserved, so any two
3834 * elements @p x and @p y in the range @p [first,last) such that
3835 * @p x<y is false and @p y<x is false will have the same relative
3836 * ordering after calling @p stable_sort().
3838 template<typename _RandomAccessIterator
>
3840 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
3842 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3844 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3847 // concept requirements
3848 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3849 _RandomAccessIterator
>)
3850 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3851 __glibcxx_requires_valid_range(__first
, __last
);
3853 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
3855 if (__buf
.begin() == 0)
3856 std::__inplace_stable_sort(__first
, __last
);
3858 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
3859 _DistanceType(__buf
.size()));
3863 * @brief Sort the elements of a sequence using a predicate for comparison,
3864 * preserving the relative order of equivalent elements.
3865 * @param first An iterator.
3866 * @param last Another iterator.
3867 * @param comp A comparison functor.
3870 * Sorts the elements in the range @p [first,last) in ascending order,
3871 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
3872 * range @p [first,last-1).
3874 * The relative ordering of equivalent elements is preserved, so any two
3875 * elements @p x and @p y in the range @p [first,last) such that
3876 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
3877 * relative ordering after calling @p stable_sort().
3879 template<typename _RandomAccessIterator
, typename _Compare
>
3881 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3884 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3886 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3889 // concept requirements
3890 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3891 _RandomAccessIterator
>)
3892 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3895 __glibcxx_requires_valid_range(__first
, __last
);
3897 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
3899 if (__buf
.begin() == 0)
3900 std::__inplace_stable_sort(__first
, __last
, __comp
);
3902 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
3903 _DistanceType(__buf
.size()), __comp
);
3907 * @brief Sort a sequence just enough to find a particular position.
3908 * @param first An iterator.
3909 * @param nth Another iterator.
3910 * @param last Another iterator.
3913 * Rearranges the elements in the range @p [first,last) so that @p *nth
3914 * is the same element that would have been in that position had the
3915 * whole sequence been sorted.
3916 * whole sequence been sorted. The elements either side of @p *nth are
3917 * not completely sorted, but for any iterator @i in the range
3918 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3919 * holds that @p *j<*i is false.
3921 template<typename _RandomAccessIterator
>
3923 nth_element(_RandomAccessIterator __first
,
3924 _RandomAccessIterator __nth
,
3925 _RandomAccessIterator __last
)
3927 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3930 // concept requirements
3931 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3932 _RandomAccessIterator
>)
3933 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3934 __glibcxx_requires_valid_range(__first
, __nth
);
3935 __glibcxx_requires_valid_range(__nth
, __last
);
3937 while (__last
- __first
> 3)
3939 _RandomAccessIterator __cut
=
3940 std::__unguarded_partition(__first
, __last
,
3941 _ValueType(std::__median(*__first
,
3953 std::__insertion_sort(__first
, __last
);
3957 * @brief Sort a sequence just enough to find a particular position
3958 * using a predicate for comparison.
3959 * @param first An iterator.
3960 * @param nth Another iterator.
3961 * @param last Another iterator.
3962 * @param comp A comparison functor.
3965 * Rearranges the elements in the range @p [first,last) so that @p *nth
3966 * is the same element that would have been in that position had the
3967 * whole sequence been sorted. The elements either side of @p *nth are
3968 * not completely sorted, but for any iterator @i in the range
3969 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3970 * holds that @p comp(*j,*i) is false.
3972 template<typename _RandomAccessIterator
, typename _Compare
>
3974 nth_element(_RandomAccessIterator __first
,
3975 _RandomAccessIterator __nth
,
3976 _RandomAccessIterator __last
,
3979 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3982 // concept requirements
3983 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3984 _RandomAccessIterator
>)
3985 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3986 _ValueType
, _ValueType
>)
3987 __glibcxx_requires_valid_range(__first
, __nth
);
3988 __glibcxx_requires_valid_range(__nth
, __last
);
3990 while (__last
- __first
> 3)
3992 _RandomAccessIterator __cut
=
3993 std::__unguarded_partition(__first
, __last
,
3994 _ValueType(std::__median(*__first
,
4006 std::__insertion_sort(__first
, __last
, __comp
);
4010 * @brief Finds the largest subrange in which @a val could be inserted
4011 * at any place in it without changing the ordering.
4012 * @param first An iterator.
4013 * @param last Another iterator.
4014 * @param val The search term.
4015 * @return An pair of iterators defining the subrange.
4016 * @ingroup binarysearch
4018 * This is equivalent to
4020 * std::make_pair(lower_bound(first, last, val),
4021 * upper_bound(first, last, val))
4023 * but does not actually call those functions.
4025 template<typename _ForwardIterator
, typename _Tp
>
4026 pair
<_ForwardIterator
, _ForwardIterator
>
4027 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
4030 typedef typename iterator_traits
<_ForwardIterator
>::value_type
4032 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
4035 // concept requirements
4036 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4037 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
4038 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
4039 __glibcxx_requires_partitioned(__first
, __last
, __val
);
4041 _DistanceType __len
= std::distance(__first
, __last
);
4042 _DistanceType __half
;
4043 _ForwardIterator __middle
, __left
, __right
;
4047 __half
= __len
>> 1;
4049 std::advance(__middle
, __half
);
4050 if (*__middle
< __val
)
4054 __len
= __len
- __half
- 1;
4056 else if (__val
< *__middle
)
4060 __left
= std::lower_bound(__first
, __middle
, __val
);
4061 std::advance(__first
, __len
);
4062 __right
= std::upper_bound(++__middle
, __first
, __val
);
4063 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
4066 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
4070 * @brief Finds the largest subrange in which @a val could be inserted
4071 * at any place in it without changing the ordering.
4072 * @param first An iterator.
4073 * @param last Another iterator.
4074 * @param val The search term.
4075 * @param comp A functor to use for comparisons.
4076 * @return An pair of iterators defining the subrange.
4077 * @ingroup binarysearch
4079 * This is equivalent to
4081 * std::make_pair(lower_bound(first, last, val, comp),
4082 * upper_bound(first, last, val, comp))
4084 * but does not actually call those functions.
4086 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
4087 pair
<_ForwardIterator
, _ForwardIterator
>
4088 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
4092 typedef typename iterator_traits
<_ForwardIterator
>::value_type
4094 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
4097 // concept requirements
4098 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4099 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4101 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4103 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
4105 _DistanceType __len
= std::distance(__first
, __last
);
4106 _DistanceType __half
;
4107 _ForwardIterator __middle
, __left
, __right
;
4111 __half
= __len
>> 1;
4113 std::advance(__middle
, __half
);
4114 if (__comp(*__middle
, __val
))
4118 __len
= __len
- __half
- 1;
4120 else if (__comp(__val
, *__middle
))
4124 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
4125 std::advance(__first
, __len
);
4126 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
4127 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
4130 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
4134 * @brief Determines whether an element exists in a range.
4135 * @param first An iterator.
4136 * @param last Another iterator.
4137 * @param val The search term.
4138 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
4139 * @ingroup binarysearch
4141 * Note that this does not actually return an iterator to @a val. For
4142 * that, use std::find or a container's specialized find member functions.
4144 template<typename _ForwardIterator
, typename _Tp
>
4146 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
4149 typedef typename iterator_traits
<_ForwardIterator
>::value_type
4152 // concept requirements
4153 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4154 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
4155 __glibcxx_requires_partitioned(__first
, __last
, __val
);
4157 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
4158 return __i
!= __last
&& !(__val
< *__i
);
4162 * @brief Determines whether an element exists in a range.
4163 * @param first An iterator.
4164 * @param last Another iterator.
4165 * @param val The search term.
4166 * @param comp A functor to use for comparisons.
4167 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
4168 * @ingroup binarysearch
4170 * Note that this does not actually return an iterator to @a val. For
4171 * that, use std::find or a container's specialized find member functions.
4173 * The comparison function should have the same effects on ordering as
4174 * the function used for the initial sort.
4176 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
4178 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
4179 const _Tp
& __val
, _Compare __comp
)
4181 typedef typename iterator_traits
<_ForwardIterator
>::value_type
4184 // concept requirements
4185 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4186 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4188 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
4190 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
4191 return __i
!= __last
&& !__comp(__val
, *__i
);
4194 // Set algorithms: includes, set_union, set_intersection, set_difference,
4195 // set_symmetric_difference. All of these algorithms have the precondition
4196 // that their input ranges are sorted and the postcondition that their output
4197 // ranges are sorted.
4200 * @brief Determines whether all elements of a sequence exists in a range.
4201 * @param first1 Start of search range.
4202 * @param last1 End of search range.
4203 * @param first2 Start of sequence
4204 * @param last2 End of sequence.
4205 * @return True if each element in [first2,last2) is contained in order
4206 * within [first1,last1). False otherwise.
4207 * @ingroup setoperations
4209 * This operation expects both [first1,last1) and [first2,last2) to be
4210 * sorted. Searches for the presence of each element in [first2,last2)
4211 * within [first1,last1). The iterators over each range only move forward,
4212 * so this is a linear algorithm. If an element in [first2,last2) is not
4213 * found before the search iterator reaches @a last2, false is returned.
4215 template<typename _InputIterator1
, typename _InputIterator2
>
4217 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
4218 _InputIterator2 __first2
, _InputIterator2 __last2
)
4220 typedef typename iterator_traits
<_InputIterator1
>::value_type
4222 typedef typename iterator_traits
<_InputIterator2
>::value_type
4225 // concept requirements
4226 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4227 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4228 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
4229 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4230 __glibcxx_requires_sorted(__first1
, __last1
);
4231 __glibcxx_requires_sorted(__first2
, __last2
);
4233 while (__first1
!= __last1
&& __first2
!= __last2
)
4234 if (*__first2
< *__first1
)
4236 else if(*__first1
< *__first2
)
4239 ++__first1
, ++__first2
;
4241 return __first2
== __last2
;
4245 * @brief Determines whether all elements of a sequence exists in a range
4247 * @param first1 Start of search range.
4248 * @param last1 End of search range.
4249 * @param first2 Start of sequence
4250 * @param last2 End of sequence.
4251 * @param comp Comparison function to use.
4252 * @return True if each element in [first2,last2) is contained in order
4253 * within [first1,last1) according to comp. False otherwise.
4254 * @ingroup setoperations
4256 * This operation expects both [first1,last1) and [first2,last2) to be
4257 * sorted. Searches for the presence of each element in [first2,last2)
4258 * within [first1,last1), using comp to decide. The iterators over each
4259 * range only move forward, so this is a linear algorithm. If an element
4260 * in [first2,last2) is not found before the search iterator reaches @a
4261 * last2, false is returned.
4263 template<typename _InputIterator1
, typename _InputIterator2
,
4266 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
4267 _InputIterator2 __first2
, _InputIterator2 __last2
, _Compare __comp
)
4269 typedef typename iterator_traits
<_InputIterator1
>::value_type
4271 typedef typename iterator_traits
<_InputIterator2
>::value_type
4274 // concept requirements
4275 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4276 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4277 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4278 _ValueType1
, _ValueType2
>)
4279 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4280 _ValueType2
, _ValueType1
>)
4281 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4282 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4284 while (__first1
!= __last1
&& __first2
!= __last2
)
4285 if (__comp(*__first2
, *__first1
))
4287 else if(__comp(*__first1
, *__first2
))
4290 ++__first1
, ++__first2
;
4292 return __first2
== __last2
;
4296 * @brief Return the union of two sorted ranges.
4297 * @param first1 Start of first range.
4298 * @param last1 End of first range.
4299 * @param first2 Start of second range.
4300 * @param last2 End of second range.
4301 * @return End of the output range.
4302 * @ingroup setoperations
4304 * This operation iterates over both ranges, copying elements present in
4305 * each range in order to the output range. Iterators increment for each
4306 * range. When the current element of one range is less than the other,
4307 * that element is copied and the iterator advanced. If an element is
4308 * contained in both ranges, the element from the first range is copied and
4309 * both ranges advance. The output range may not overlap either input
4312 template<typename _InputIterator1
, typename _InputIterator2
,
4313 typename _OutputIterator
>
4315 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4316 _InputIterator2 __first2
, _InputIterator2 __last2
,
4317 _OutputIterator __result
)
4319 typedef typename iterator_traits
<_InputIterator1
>::value_type
4321 typedef typename iterator_traits
<_InputIterator2
>::value_type
4324 // concept requirements
4325 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4326 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4327 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4329 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4331 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
4332 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4333 __glibcxx_requires_sorted(__first1
, __last1
);
4334 __glibcxx_requires_sorted(__first2
, __last2
);
4336 while (__first1
!= __last1
&& __first2
!= __last2
)
4338 if (*__first1
< *__first2
)
4340 *__result
= *__first1
;
4343 else if (*__first2
< *__first1
)
4345 *__result
= *__first2
;
4350 *__result
= *__first1
;
4356 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4361 * @brief Return the union of two sorted ranges using a comparison functor.
4362 * @param first1 Start of first range.
4363 * @param last1 End of first range.
4364 * @param first2 Start of second range.
4365 * @param last2 End of second range.
4366 * @param comp The comparison functor.
4367 * @return End of the output range.
4368 * @ingroup setoperations
4370 * This operation iterates over both ranges, copying elements present in
4371 * each range in order to the output range. Iterators increment for each
4372 * range. When the current element of one range is less than the other
4373 * according to @a comp, that element is copied and the iterator advanced.
4374 * If an equivalent element according to @a comp is contained in both
4375 * ranges, the element from the first range is copied and both ranges
4376 * advance. The output range may not overlap either input range.
4378 template<typename _InputIterator1
, typename _InputIterator2
,
4379 typename _OutputIterator
, typename _Compare
>
4381 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4382 _InputIterator2 __first2
, _InputIterator2 __last2
,
4383 _OutputIterator __result
, _Compare __comp
)
4385 typedef typename iterator_traits
<_InputIterator1
>::value_type
4387 typedef typename iterator_traits
<_InputIterator2
>::value_type
4390 // concept requirements
4391 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4392 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4393 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4395 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4397 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4398 _ValueType1
, _ValueType2
>)
4399 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4400 _ValueType2
, _ValueType1
>)
4401 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4402 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4404 while (__first1
!= __last1
&& __first2
!= __last2
)
4406 if (__comp(*__first1
, *__first2
))
4408 *__result
= *__first1
;
4411 else if (__comp(*__first2
, *__first1
))
4413 *__result
= *__first2
;
4418 *__result
= *__first1
;
4424 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4429 * @brief Return the intersection of two sorted ranges.
4430 * @param first1 Start of first range.
4431 * @param last1 End of first range.
4432 * @param first2 Start of second range.
4433 * @param last2 End of second range.
4434 * @return End of the output range.
4435 * @ingroup setoperations
4437 * This operation iterates over both ranges, copying elements present in
4438 * both ranges in order to the output range. Iterators increment for each
4439 * range. When the current element of one range is less than the other,
4440 * that iterator advances. If an element is contained in both ranges, the
4441 * element from the first range is copied and both ranges advance. The
4442 * output range may not overlap either input range.
4444 template<typename _InputIterator1
, typename _InputIterator2
,
4445 typename _OutputIterator
>
4447 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
4448 _InputIterator2 __first2
, _InputIterator2 __last2
,
4449 _OutputIterator __result
)
4451 typedef typename iterator_traits
<_InputIterator1
>::value_type
4453 typedef typename iterator_traits
<_InputIterator2
>::value_type
4456 // concept requirements
4457 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4458 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4459 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4461 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
4462 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4463 __glibcxx_requires_sorted(__first1
, __last1
);
4464 __glibcxx_requires_sorted(__first2
, __last2
);
4466 while (__first1
!= __last1
&& __first2
!= __last2
)
4467 if (*__first1
< *__first2
)
4469 else if (*__first2
< *__first1
)
4473 *__result
= *__first1
;
4482 * @brief Return the intersection of two sorted ranges using comparison
4484 * @param first1 Start of first range.
4485 * @param last1 End of first range.
4486 * @param first2 Start of second range.
4487 * @param last2 End of second range.
4488 * @param comp The comparison functor.
4489 * @return End of the output range.
4490 * @ingroup setoperations
4492 * This operation iterates over both ranges, copying elements present in
4493 * both ranges in order to the output range. Iterators increment for each
4494 * range. When the current element of one range is less than the other
4495 * according to @a comp, that iterator advances. If an element is
4496 * contained in both ranges according to @a comp, the element from the
4497 * first range is copied and both ranges advance. The output range may not
4498 * overlap either input range.
4500 template<typename _InputIterator1
, typename _InputIterator2
,
4501 typename _OutputIterator
, typename _Compare
>
4503 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
4504 _InputIterator2 __first2
, _InputIterator2 __last2
,
4505 _OutputIterator __result
, _Compare __comp
)
4507 typedef typename iterator_traits
<_InputIterator1
>::value_type
4509 typedef typename iterator_traits
<_InputIterator2
>::value_type
4512 // concept requirements
4513 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4514 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4515 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4517 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4518 _ValueType1
, _ValueType2
>)
4519 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4520 _ValueType2
, _ValueType1
>)
4521 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4522 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4524 while (__first1
!= __last1
&& __first2
!= __last2
)
4525 if (__comp(*__first1
, *__first2
))
4527 else if (__comp(*__first2
, *__first1
))
4531 *__result
= *__first1
;
4540 * @brief Return the difference of two sorted ranges.
4541 * @param first1 Start of first range.
4542 * @param last1 End of first range.
4543 * @param first2 Start of second range.
4544 * @param last2 End of second range.
4545 * @return End of the output range.
4546 * @ingroup setoperations
4548 * This operation iterates over both ranges, copying elements present in
4549 * the first range but not the second in order to the output range.
4550 * Iterators increment for each range. When the current element of the
4551 * first range is less than the second, that element is copied and the
4552 * iterator advances. If the current element of the second range is less,
4553 * the iterator advances, but no element is copied. If an element is
4554 * contained in both ranges, no elements are copied and both ranges
4555 * advance. The output range may not overlap either input range.
4557 template<typename _InputIterator1
, typename _InputIterator2
,
4558 typename _OutputIterator
>
4560 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4561 _InputIterator2 __first2
, _InputIterator2 __last2
,
4562 _OutputIterator __result
)
4564 typedef typename iterator_traits
<_InputIterator1
>::value_type
4566 typedef typename iterator_traits
<_InputIterator2
>::value_type
4569 // concept requirements
4570 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4571 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4572 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4574 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
4575 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4576 __glibcxx_requires_sorted(__first1
, __last1
);
4577 __glibcxx_requires_sorted(__first2
, __last2
);
4579 while (__first1
!= __last1
&& __first2
!= __last2
)
4580 if (*__first1
< *__first2
)
4582 *__result
= *__first1
;
4586 else if (*__first2
< *__first1
)
4593 return std::copy(__first1
, __last1
, __result
);
4597 * @brief Return the difference of two sorted ranges using comparison
4599 * @param first1 Start of first range.
4600 * @param last1 End of first range.
4601 * @param first2 Start of second range.
4602 * @param last2 End of second range.
4603 * @param comp The comparison functor.
4604 * @return End of the output range.
4605 * @ingroup setoperations
4607 * This operation iterates over both ranges, copying elements present in
4608 * the first range but not the second in order to the output range.
4609 * Iterators increment for each range. When the current element of the
4610 * first range is less than the second according to @a comp, that element
4611 * is copied and the iterator advances. If the current element of the
4612 * second range is less, no element is copied and the iterator advances.
4613 * If an element is contained in both ranges according to @a comp, no
4614 * elements are copied and both ranges advance. The output range may not
4615 * overlap either input range.
4617 template<typename _InputIterator1
, typename _InputIterator2
,
4618 typename _OutputIterator
, typename _Compare
>
4620 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4621 _InputIterator2 __first2
, _InputIterator2 __last2
,
4622 _OutputIterator __result
, _Compare __comp
)
4624 typedef typename iterator_traits
<_InputIterator1
>::value_type
4626 typedef typename iterator_traits
<_InputIterator2
>::value_type
4629 // concept requirements
4630 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4631 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4632 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4634 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4635 _ValueType1
, _ValueType2
>)
4636 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4637 _ValueType2
, _ValueType1
>)
4638 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4639 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4641 while (__first1
!= __last1
&& __first2
!= __last2
)
4642 if (__comp(*__first1
, *__first2
))
4644 *__result
= *__first1
;
4648 else if (__comp(*__first2
, *__first1
))
4655 return std::copy(__first1
, __last1
, __result
);
4659 * @brief Return the symmetric difference of two sorted ranges.
4660 * @param first1 Start of first range.
4661 * @param last1 End of first range.
4662 * @param first2 Start of second range.
4663 * @param last2 End of second range.
4664 * @return End of the output range.
4665 * @ingroup setoperations
4667 * This operation iterates over both ranges, copying elements present in
4668 * one range but not the other in order to the output range. Iterators
4669 * increment for each range. When the current element of one range is less
4670 * than the other, that element is copied and the iterator advances. If an
4671 * element is contained in both ranges, no elements are copied and both
4672 * ranges advance. The output range may not overlap either input range.
4674 template<typename _InputIterator1
, typename _InputIterator2
,
4675 typename _OutputIterator
>
4677 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4678 _InputIterator2 __first2
, _InputIterator2 __last2
,
4679 _OutputIterator __result
)
4681 typedef typename iterator_traits
<_InputIterator1
>::value_type
4683 typedef typename iterator_traits
<_InputIterator2
>::value_type
4686 // concept requirements
4687 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4688 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4689 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4691 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4693 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
4694 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4695 __glibcxx_requires_sorted(__first1
, __last1
);
4696 __glibcxx_requires_sorted(__first2
, __last2
);
4698 while (__first1
!= __last1
&& __first2
!= __last2
)
4699 if (*__first1
< *__first2
)
4701 *__result
= *__first1
;
4705 else if (*__first2
< *__first1
)
4707 *__result
= *__first2
;
4716 return std::copy(__first2
, __last2
, std::copy(__first1
,
4717 __last1
, __result
));
4721 * @brief Return the symmetric difference of two sorted ranges using
4722 * comparison functor.
4723 * @param first1 Start of first range.
4724 * @param last1 End of first range.
4725 * @param first2 Start of second range.
4726 * @param last2 End of second range.
4727 * @param comp The comparison functor.
4728 * @return End of the output range.
4729 * @ingroup setoperations
4731 * This operation iterates over both ranges, copying elements present in
4732 * one range but not the other in order to the output range. Iterators
4733 * increment for each range. When the current element of one range is less
4734 * than the other according to @a comp, that element is copied and the
4735 * iterator advances. If an element is contained in both ranges according
4736 * to @a comp, no elements are copied and both ranges advance. The output
4737 * range may not overlap either input range.
4739 template<typename _InputIterator1
, typename _InputIterator2
,
4740 typename _OutputIterator
, typename _Compare
>
4742 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4743 _InputIterator2 __first2
, _InputIterator2 __last2
,
4744 _OutputIterator __result
,
4747 typedef typename iterator_traits
<_InputIterator1
>::value_type
4749 typedef typename iterator_traits
<_InputIterator2
>::value_type
4752 // concept requirements
4753 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4754 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4755 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4757 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4759 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4760 _ValueType1
, _ValueType2
>)
4761 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4762 _ValueType2
, _ValueType1
>)
4763 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4764 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4766 while (__first1
!= __last1
&& __first2
!= __last2
)
4767 if (__comp(*__first1
, *__first2
))
4769 *__result
= *__first1
;
4773 else if (__comp(*__first2
, *__first1
))
4775 *__result
= *__first2
;
4784 return std::copy(__first2
, __last2
, std::copy(__first1
,
4785 __last1
, __result
));
4788 // min_element and max_element, with and without an explicitly supplied
4789 // comparison function.
4792 * @brief Return the maximum element in a range.
4793 * @param first Start of range.
4794 * @param last End of range.
4795 * @return Iterator referencing the first instance of the largest value.
4797 template<typename _ForwardIterator
>
4799 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
4801 // concept requirements
4802 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4803 __glibcxx_function_requires(_LessThanComparableConcept
<
4804 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4805 __glibcxx_requires_valid_range(__first
, __last
);
4807 if (__first
== __last
)
4809 _ForwardIterator __result
= __first
;
4810 while (++__first
!= __last
)
4811 if (*__result
< *__first
)
4817 * @brief Return the maximum element in a range using comparison functor.
4818 * @param first Start of range.
4819 * @param last End of range.
4820 * @param comp Comparison functor.
4821 * @return Iterator referencing the first instance of the largest value
4822 * according to comp.
4824 template<typename _ForwardIterator
, typename _Compare
>
4826 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
4829 // concept requirements
4830 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4831 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4832 typename iterator_traits
<_ForwardIterator
>::value_type
,
4833 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4834 __glibcxx_requires_valid_range(__first
, __last
);
4836 if (__first
== __last
) return __first
;
4837 _ForwardIterator __result
= __first
;
4838 while (++__first
!= __last
)
4839 if (__comp(*__result
, *__first
)) __result
= __first
;
4844 * @brief Return the minimum element in a range.
4845 * @param first Start of range.
4846 * @param last End of range.
4847 * @return Iterator referencing the first instance of the smallest value.
4849 template<typename _ForwardIterator
>
4851 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
4853 // concept requirements
4854 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4855 __glibcxx_function_requires(_LessThanComparableConcept
<
4856 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4857 __glibcxx_requires_valid_range(__first
, __last
);
4859 if (__first
== __last
)
4861 _ForwardIterator __result
= __first
;
4862 while (++__first
!= __last
)
4863 if (*__first
< *__result
)
4869 * @brief Return the minimum element in a range using comparison functor.
4870 * @param first Start of range.
4871 * @param last End of range.
4872 * @param comp Comparison functor.
4873 * @return Iterator referencing the first instance of the smallest value
4874 * according to comp.
4876 template<typename _ForwardIterator
, typename _Compare
>
4878 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
4881 // concept requirements
4882 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4883 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4884 typename iterator_traits
<_ForwardIterator
>::value_type
,
4885 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4886 __glibcxx_requires_valid_range(__first
, __last
);
4888 if (__first
== __last
)
4890 _ForwardIterator __result
= __first
;
4891 while (++__first
!= __last
)
4892 if (__comp(*__first
, *__result
))
4897 // next_permutation and prev_permutation, with and without an explicitly
4898 // supplied comparison function.
4901 * @brief Permute range into the next "dictionary" ordering.
4902 * @param first Start of range.
4903 * @param last End of range.
4904 * @return False if wrapped to first permutation, true otherwise.
4906 * Treats all permutations of the range as a set of "dictionary" sorted
4907 * sequences. Permutes the current sequence into the next one of this set.
4908 * Returns true if there are more sequences to generate. If the sequence
4909 * is the largest of the set, the smallest is generated and false returned.
4911 template<typename _BidirectionalIterator
>
4913 next_permutation(_BidirectionalIterator __first
,
4914 _BidirectionalIterator __last
)
4916 // concept requirements
4917 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4918 _BidirectionalIterator
>)
4919 __glibcxx_function_requires(_LessThanComparableConcept
<
4920 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4921 __glibcxx_requires_valid_range(__first
, __last
);
4923 if (__first
== __last
)
4925 _BidirectionalIterator __i
= __first
;
4934 _BidirectionalIterator __ii
= __i
;
4938 _BidirectionalIterator __j
= __last
;
4939 while (!(*__i
< *--__j
))
4941 std::iter_swap(__i
, __j
);
4942 std::reverse(__ii
, __last
);
4947 std::reverse(__first
, __last
);
4954 * @brief Permute range into the next "dictionary" ordering using
4955 * comparison functor.
4956 * @param first Start of range.
4957 * @param last End of range.
4959 * @return False if wrapped to first permutation, true otherwise.
4961 * Treats all permutations of the range [first,last) as a set of
4962 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
4963 * sequence into the next one of this set. Returns true if there are more
4964 * sequences to generate. If the sequence is the largest of the set, the
4965 * smallest is generated and false returned.
4967 template<typename _BidirectionalIterator
, typename _Compare
>
4969 next_permutation(_BidirectionalIterator __first
,
4970 _BidirectionalIterator __last
, _Compare __comp
)
4972 // concept requirements
4973 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4974 _BidirectionalIterator
>)
4975 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4976 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
4977 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4978 __glibcxx_requires_valid_range(__first
, __last
);
4980 if (__first
== __last
)
4982 _BidirectionalIterator __i
= __first
;
4991 _BidirectionalIterator __ii
= __i
;
4993 if (__comp(*__i
, *__ii
))
4995 _BidirectionalIterator __j
= __last
;
4996 while (!__comp(*__i
, *--__j
))
4998 std::iter_swap(__i
, __j
);
4999 std::reverse(__ii
, __last
);
5004 std::reverse(__first
, __last
);
5011 * @brief Permute range into the previous "dictionary" ordering.
5012 * @param first Start of range.
5013 * @param last End of range.
5014 * @return False if wrapped to last permutation, true otherwise.
5016 * Treats all permutations of the range as a set of "dictionary" sorted
5017 * sequences. Permutes the current sequence into the previous one of this
5018 * set. Returns true if there are more sequences to generate. If the
5019 * sequence is the smallest of the set, the largest is generated and false
5022 template<typename _BidirectionalIterator
>
5024 prev_permutation(_BidirectionalIterator __first
,
5025 _BidirectionalIterator __last
)
5027 // concept requirements
5028 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5029 _BidirectionalIterator
>)
5030 __glibcxx_function_requires(_LessThanComparableConcept
<
5031 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
5032 __glibcxx_requires_valid_range(__first
, __last
);
5034 if (__first
== __last
)
5036 _BidirectionalIterator __i
= __first
;
5045 _BidirectionalIterator __ii
= __i
;
5049 _BidirectionalIterator __j
= __last
;
5050 while (!(*--__j
< *__i
))
5052 std::iter_swap(__i
, __j
);
5053 std::reverse(__ii
, __last
);
5058 std::reverse(__first
, __last
);
5065 * @brief Permute range into the previous "dictionary" ordering using
5066 * comparison functor.
5067 * @param first Start of range.
5068 * @param last End of range.
5070 * @return False if wrapped to last permutation, true otherwise.
5072 * Treats all permutations of the range [first,last) as a set of
5073 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
5074 * sequence into the previous one of this set. Returns true if there are
5075 * more sequences to generate. If the sequence is the smallest of the set,
5076 * the largest is generated and false returned.
5078 template<typename _BidirectionalIterator
, typename _Compare
>
5080 prev_permutation(_BidirectionalIterator __first
,
5081 _BidirectionalIterator __last
, _Compare __comp
)
5083 // concept requirements
5084 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5085 _BidirectionalIterator
>)
5086 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5087 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
5088 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
5089 __glibcxx_requires_valid_range(__first
, __last
);
5091 if (__first
== __last
)
5093 _BidirectionalIterator __i
= __first
;
5102 _BidirectionalIterator __ii
= __i
;
5104 if (__comp(*__ii
, *__i
))
5106 _BidirectionalIterator __j
= __last
;
5107 while (!__comp(*--__j
, *__i
))
5109 std::iter_swap(__i
, __j
);
5110 std::reverse(__ii
, __last
);
5115 std::reverse(__first
, __last
);
5121 // find_first_of, with and without an explicitly supplied comparison function.
5124 * @brief Find element from a set in a sequence.
5125 * @param first1 Start of range to search.
5126 * @param last1 End of range to search.
5127 * @param first2 Start of match candidates.
5128 * @param last2 End of match candidates.
5129 * @return The first iterator @c i in the range
5130 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
5131 * interator in [first2,last2), or @p last1 if no such iterator exists.
5133 * Searches the range @p [first1,last1) for an element that is equal to
5134 * some element in the range [first2,last2). If found, returns an iterator
5135 * in the range [first1,last1), otherwise returns @p last1.
5137 template<typename _InputIterator
, typename _ForwardIterator
>
5139 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
5140 _ForwardIterator __first2
, _ForwardIterator __last2
)
5142 // concept requirements
5143 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
5144 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5145 __glibcxx_function_requires(_EqualOpConcept
<
5146 typename iterator_traits
<_InputIterator
>::value_type
,
5147 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5148 __glibcxx_requires_valid_range(__first1
, __last1
);
5149 __glibcxx_requires_valid_range(__first2
, __last2
);
5151 for ( ; __first1
!= __last1
; ++__first1
)
5152 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
5153 if (*__first1
== *__iter
)
5159 * @brief Find element from a set in a sequence using a predicate.
5160 * @param first1 Start of range to search.
5161 * @param last1 End of range to search.
5162 * @param first2 Start of match candidates.
5163 * @param last2 End of match candidates.
5164 * @param comp Predicate to use.
5165 * @return The first iterator @c i in the range
5166 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
5167 * interator in [first2,last2), or @p last1 if no such iterator exists.
5169 * Searches the range @p [first1,last1) for an element that is equal to
5170 * some element in the range [first2,last2). If found, returns an iterator in
5171 * the range [first1,last1), otherwise returns @p last1.
5173 template<typename _InputIterator
, typename _ForwardIterator
,
5174 typename _BinaryPredicate
>
5176 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
5177 _ForwardIterator __first2
, _ForwardIterator __last2
,
5178 _BinaryPredicate __comp
)
5180 // concept requirements
5181 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
5182 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5183 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
5184 typename iterator_traits
<_InputIterator
>::value_type
,
5185 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5186 __glibcxx_requires_valid_range(__first1
, __last1
);
5187 __glibcxx_requires_valid_range(__first2
, __last2
);
5189 for ( ; __first1
!= __last1
; ++__first1
)
5190 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
5191 if (__comp(*__first1
, *__iter
))
5197 // find_end, with and without an explicitly supplied comparison function.
5198 // Search [first2, last2) as a subsequence in [first1, last1), and return
5199 // the *last* possible match. Note that find_end for bidirectional iterators
5200 // is much faster than for forward iterators.
5202 // find_end for forward iterators.
5203 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
5205 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
5206 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
5207 forward_iterator_tag
, forward_iterator_tag
)
5209 if (__first2
== __last2
)
5213 _ForwardIterator1 __result
= __last1
;
5216 _ForwardIterator1 __new_result
5217 = std::search(__first1
, __last1
, __first2
, __last2
);
5218 if (__new_result
== __last1
)
5222 __result
= __new_result
;
5223 __first1
= __new_result
;
5230 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
5231 typename _BinaryPredicate
>
5233 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
5234 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
5235 forward_iterator_tag
, forward_iterator_tag
,
5236 _BinaryPredicate __comp
)
5238 if (__first2
== __last2
)
5242 _ForwardIterator1 __result
= __last1
;
5245 _ForwardIterator1 __new_result
5246 = std::search(__first1
, __last1
, __first2
, __last2
, __comp
);
5247 if (__new_result
== __last1
)
5251 __result
= __new_result
;
5252 __first1
= __new_result
;
5259 // find_end for bidirectional iterators. Requires partial specialization.
5260 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
5261 _BidirectionalIterator1
5262 __find_end(_BidirectionalIterator1 __first1
,
5263 _BidirectionalIterator1 __last1
,
5264 _BidirectionalIterator2 __first2
,
5265 _BidirectionalIterator2 __last2
,
5266 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
5268 // concept requirements
5269 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5270 _BidirectionalIterator1
>)
5271 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5272 _BidirectionalIterator2
>)
5274 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
5275 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
5277 _RevIterator1
__rlast1(__first1
);
5278 _RevIterator2
__rlast2(__first2
);
5279 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
5280 _RevIterator2(__last2
), __rlast2
);
5282 if (__rresult
== __rlast1
)
5286 _BidirectionalIterator1 __result
= __rresult
.base();
5287 std::advance(__result
, -std::distance(__first2
, __last2
));
5292 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
5293 typename _BinaryPredicate
>
5294 _BidirectionalIterator1
5295 __find_end(_BidirectionalIterator1 __first1
,
5296 _BidirectionalIterator1 __last1
,
5297 _BidirectionalIterator2 __first2
,
5298 _BidirectionalIterator2 __last2
,
5299 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
5300 _BinaryPredicate __comp
)
5302 // concept requirements
5303 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5304 _BidirectionalIterator1
>)
5305 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5306 _BidirectionalIterator2
>)
5308 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
5309 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
5311 _RevIterator1
__rlast1(__first1
);
5312 _RevIterator2
__rlast2(__first2
);
5313 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
5314 _RevIterator2(__last2
), __rlast2
,
5317 if (__rresult
== __rlast1
)
5321 _BidirectionalIterator1 __result
= __rresult
.base();
5322 std::advance(__result
, -std::distance(__first2
, __last2
));
5327 // Dispatching functions for find_end.
5330 * @brief Find last matching subsequence in a sequence.
5331 * @param first1 Start of range to search.
5332 * @param last1 End of range to search.
5333 * @param first2 Start of sequence to match.
5334 * @param last2 End of sequence to match.
5335 * @return The last iterator @c i in the range
5336 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
5337 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
5338 * such iterator exists.
5340 * Searches the range @p [first1,last1) for a sub-sequence that compares
5341 * equal value-by-value with the sequence given by @p [first2,last2) and
5342 * returns an iterator to the first element of the sub-sequence, or
5343 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
5344 * last such subsequence contained in [first,last1).
5346 * Because the sub-sequence must lie completely within the range
5347 * @p [first1,last1) it must start at a position less than
5348 * @p last1-(last2-first2) where @p last2-first2 is the length of the
5350 * This means that the returned iterator @c i will be in the range
5351 * @p [first1,last1-(last2-first2))
5353 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
5354 inline _ForwardIterator1
5355 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
5356 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
5358 // concept requirements
5359 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
5360 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
5361 __glibcxx_function_requires(_EqualOpConcept
<
5362 typename iterator_traits
<_ForwardIterator1
>::value_type
,
5363 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
5364 __glibcxx_requires_valid_range(__first1
, __last1
);
5365 __glibcxx_requires_valid_range(__first2
, __last2
);
5367 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
5368 std::__iterator_category(__first1
),
5369 std::__iterator_category(__first2
));
5373 * @brief Find last matching subsequence in a sequence using a predicate.
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 * @param comp The predicate to use.
5379 * @return The last iterator @c i in the range
5380 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
5381 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
5382 * @p last1 if no such iterator exists.
5384 * Searches the range @p [first1,last1) for a sub-sequence that compares
5385 * equal value-by-value with the sequence given by @p [first2,last2) using
5386 * comp as a predicate and returns an iterator to the first element of the
5387 * sub-sequence, or @p last1 if the sub-sequence is not found. The
5388 * sub-sequence will be the last such subsequence contained in
5391 * Because the sub-sequence must lie completely within the range
5392 * @p [first1,last1) it must start at a position less than
5393 * @p last1-(last2-first2) where @p last2-first2 is the length of the
5395 * This means that the returned iterator @c i will be in the range
5396 * @p [first1,last1-(last2-first2))
5398 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
5399 typename _BinaryPredicate
>
5400 inline _ForwardIterator1
5401 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
5402 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
5403 _BinaryPredicate __comp
)
5405 // concept requirements
5406 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
5407 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
5408 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
5409 typename iterator_traits
<_ForwardIterator1
>::value_type
,
5410 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
5411 __glibcxx_requires_valid_range(__first1
, __last1
);
5412 __glibcxx_requires_valid_range(__first2
, __last2
);
5414 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
5415 std::__iterator_category(__first1
),
5416 std::__iterator_category(__first2
),
5420 _GLIBCXX_END_NAMESPACE
5422 #endif /* _ALGO_H */