1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
65 #include <bits/stl_heap.h>
66 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
67 #include <cstdlib> // for rand
68 #include <debug/debug.h>
70 // See concept_check.h for the __glibcxx_*_requires macros.
72 _GLIBCXX_BEGIN_NAMESPACE(std
)
75 * @brief Find the median of three values.
79 * @return One of @p a, @p b or @p c.
81 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
82 * then the value returned will be @c m.
83 * This is an SGI extension.
84 * @ingroup SGIextensions
86 template<typename _Tp
>
88 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
)
90 // concept requirements
91 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
108 * @brief Find the median of three values using a predicate for comparison.
112 * @param comp A binary predicate.
113 * @return One of @p a, @p b or @p c.
115 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
116 * and @p comp(m,n) are both true then the value returned will be @c m.
117 * This is an SGI extension.
118 * @ingroup SGIextensions
120 template<typename _Tp
, typename _Compare
>
122 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
, _Compare __comp
)
124 // concept requirements
125 __glibcxx_function_requires(_BinaryFunctionConcept
<_Compare
,bool,_Tp
,_Tp
>)
126 if (__comp(__a
, __b
))
127 if (__comp(__b
, __c
))
129 else if (__comp(__a
, __c
))
133 else if (__comp(__a
, __c
))
135 else if (__comp(__b
, __c
))
142 * @brief Apply a function to every element of a sequence.
143 * @param first An input iterator.
144 * @param last An input iterator.
145 * @param f A unary function object.
148 * Applies the function object @p f to each element in the range
149 * @p [first,last). @p f must not modify the order of the sequence.
150 * If @p f has a return value it is ignored.
152 template<typename _InputIterator
, typename _Function
>
154 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
156 // concept requirements
157 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
158 __glibcxx_requires_valid_range(__first
, __last
);
159 for ( ; __first
!= __last
; ++__first
)
166 * This is an overload used by find() for the Input Iterator case.
169 template<typename _InputIterator
, typename _Tp
>
170 inline _InputIterator
171 __find(_InputIterator __first
, _InputIterator __last
,
172 const _Tp
& __val
, input_iterator_tag
)
174 while (__first
!= __last
&& !(*__first
== __val
))
181 * This is an overload used by find_if() for the Input Iterator case.
184 template<typename _InputIterator
, typename _Predicate
>
185 inline _InputIterator
186 __find_if(_InputIterator __first
, _InputIterator __last
,
187 _Predicate __pred
, input_iterator_tag
)
189 while (__first
!= __last
&& !__pred(*__first
))
196 * This is an overload used by find() for the RAI case.
199 template<typename _RandomAccessIterator
, typename _Tp
>
200 _RandomAccessIterator
201 __find(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
202 const _Tp
& __val
, random_access_iterator_tag
)
204 typename iterator_traits
<_RandomAccessIterator
>::difference_type
205 __trip_count
= (__last
- __first
) >> 2;
207 for ( ; __trip_count
> 0 ; --__trip_count
)
209 if (*__first
== __val
)
213 if (*__first
== __val
)
217 if (*__first
== __val
)
221 if (*__first
== __val
)
226 switch (__last
- __first
)
229 if (*__first
== __val
)
233 if (*__first
== __val
)
237 if (*__first
== __val
)
248 * This is an overload used by find_if() for the RAI case.
251 template<typename _RandomAccessIterator
, typename _Predicate
>
252 _RandomAccessIterator
253 __find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
254 _Predicate __pred
, random_access_iterator_tag
)
256 typename iterator_traits
<_RandomAccessIterator
>::difference_type
257 __trip_count
= (__last
- __first
) >> 2;
259 for ( ; __trip_count
> 0 ; --__trip_count
)
261 if (__pred(*__first
))
265 if (__pred(*__first
))
269 if (__pred(*__first
))
273 if (__pred(*__first
))
278 switch (__last
- __first
)
281 if (__pred(*__first
))
285 if (__pred(*__first
))
289 if (__pred(*__first
))
299 * @brief Find the first occurrence of a value in a sequence.
300 * @param first An input iterator.
301 * @param last An input iterator.
302 * @param val The value to find.
303 * @return The first iterator @c i in the range @p [first,last)
304 * such that @c *i == @p val, or @p last if no such iterator exists.
306 template<typename _InputIterator
, typename _Tp
>
307 inline _InputIterator
308 find(_InputIterator __first
, _InputIterator __last
,
311 // concept requirements
312 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
313 __glibcxx_function_requires(_EqualOpConcept
<
314 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
315 __glibcxx_requires_valid_range(__first
, __last
);
316 return std::__find(__first
, __last
, __val
,
317 std::__iterator_category(__first
));
321 * @brief Find the first element in a sequence for which a predicate is true.
322 * @param first An input iterator.
323 * @param last An input iterator.
324 * @param pred A predicate.
325 * @return The first iterator @c i in the range @p [first,last)
326 * such that @p pred(*i) is true, or @p last if no such iterator exists.
328 template<typename _InputIterator
, typename _Predicate
>
329 inline _InputIterator
330 find_if(_InputIterator __first
, _InputIterator __last
,
333 // concept requirements
334 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
335 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
336 typename iterator_traits
<_InputIterator
>::value_type
>)
337 __glibcxx_requires_valid_range(__first
, __last
);
338 return std::__find_if(__first
, __last
, __pred
,
339 std::__iterator_category(__first
));
343 * @brief Find two adjacent values in a sequence that are equal.
344 * @param first A forward iterator.
345 * @param last A forward iterator.
346 * @return The first iterator @c i such that @c i and @c i+1 are both
347 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
348 * or @p last if no such iterator exists.
350 template<typename _ForwardIterator
>
352 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
354 // concept requirements
355 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
356 __glibcxx_function_requires(_EqualityComparableConcept
<
357 typename iterator_traits
<_ForwardIterator
>::value_type
>)
358 __glibcxx_requires_valid_range(__first
, __last
);
359 if (__first
== __last
)
361 _ForwardIterator __next
= __first
;
362 while(++__next
!= __last
)
364 if (*__first
== *__next
)
372 * @brief Find two adjacent values in a sequence using a predicate.
373 * @param first A forward iterator.
374 * @param last A forward iterator.
375 * @param binary_pred A binary predicate.
376 * @return The first iterator @c i such that @c i and @c i+1 are both
377 * valid iterators in @p [first,last) and such that
378 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
381 template<typename _ForwardIterator
, typename _BinaryPredicate
>
383 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
384 _BinaryPredicate __binary_pred
)
386 // concept requirements
387 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
388 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
389 typename iterator_traits
<_ForwardIterator
>::value_type
,
390 typename iterator_traits
<_ForwardIterator
>::value_type
>)
391 __glibcxx_requires_valid_range(__first
, __last
);
392 if (__first
== __last
)
394 _ForwardIterator __next
= __first
;
395 while(++__next
!= __last
)
397 if (__binary_pred(*__first
, *__next
))
405 * @brief Count the number of copies of a value in a sequence.
406 * @param first An input iterator.
407 * @param last An input iterator.
408 * @param value The value to be counted.
409 * @return The number of iterators @c i in the range @p [first,last)
410 * for which @c *i == @p value
412 template<typename _InputIterator
, typename _Tp
>
413 typename iterator_traits
<_InputIterator
>::difference_type
414 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
416 // concept requirements
417 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
418 __glibcxx_function_requires(_EqualOpConcept
<
419 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
420 __glibcxx_requires_valid_range(__first
, __last
);
421 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
422 for ( ; __first
!= __last
; ++__first
)
423 if (*__first
== __value
)
429 * @brief Count the elements of a sequence for which a predicate is true.
430 * @param first An input iterator.
431 * @param last An input iterator.
432 * @param pred A predicate.
433 * @return The number of iterators @c i in the range @p [first,last)
434 * for which @p pred(*i) is true.
436 template<typename _InputIterator
, typename _Predicate
>
437 typename iterator_traits
<_InputIterator
>::difference_type
438 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
440 // concept requirements
441 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
442 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
443 typename iterator_traits
<_InputIterator
>::value_type
>)
444 __glibcxx_requires_valid_range(__first
, __last
);
445 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
446 for ( ; __first
!= __last
; ++__first
)
447 if (__pred(*__first
))
453 * @brief Search a sequence for a matching sub-sequence.
454 * @param first1 A forward iterator.
455 * @param last1 A forward iterator.
456 * @param first2 A forward iterator.
457 * @param last2 A forward iterator.
458 * @return The first iterator @c i in the range
459 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
460 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
461 * such iterator exists.
463 * Searches the range @p [first1,last1) for a sub-sequence that compares
464 * equal value-by-value with the sequence given by @p [first2,last2) and
465 * returns an iterator to the first element of the sub-sequence, or
466 * @p last1 if the sub-sequence is not found.
468 * Because the sub-sequence must lie completely within the range
469 * @p [first1,last1) it must start at a position less than
470 * @p last1-(last2-first2) where @p last2-first2 is the length of the
472 * This means that the returned iterator @c i will be in the range
473 * @p [first1,last1-(last2-first2))
475 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
477 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
478 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
480 // concept requirements
481 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
482 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
483 __glibcxx_function_requires(_EqualOpConcept
<
484 typename iterator_traits
<_ForwardIterator1
>::value_type
,
485 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
486 __glibcxx_requires_valid_range(__first1
, __last1
);
487 __glibcxx_requires_valid_range(__first2
, __last2
);
488 // Test for empty ranges
489 if (__first1
== __last1
|| __first2
== __last2
)
492 // Test for a pattern of length 1.
493 _ForwardIterator2
__tmp(__first2
);
495 if (__tmp
== __last2
)
496 return std::find(__first1
, __last1
, *__first2
);
499 _ForwardIterator2 __p1
, __p
;
500 __p1
= __first2
; ++__p1
;
501 _ForwardIterator1 __current
= __first1
;
503 while (__first1
!= __last1
)
505 __first1
= std::find(__first1
, __last1
, *__first2
);
506 if (__first1
== __last1
)
510 __current
= __first1
;
511 if (++__current
== __last1
)
514 while (*__current
== *__p
)
516 if (++__p
== __last2
)
518 if (++__current
== __last1
)
527 * @brief Search a sequence for a matching sub-sequence using a predicate.
528 * @param first1 A forward iterator.
529 * @param last1 A forward iterator.
530 * @param first2 A forward iterator.
531 * @param last2 A forward iterator.
532 * @param predicate A binary predicate.
533 * @return The first iterator @c i in the range
534 * @p [first1,last1-(last2-first2)) such that
535 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
536 * @p [0,last2-first2), or @p last1 if no such iterator exists.
538 * Searches the range @p [first1,last1) for a sub-sequence that compares
539 * equal value-by-value with the sequence given by @p [first2,last2),
540 * using @p predicate to determine equality, and returns an iterator
541 * to the first element of the sub-sequence, or @p last1 if no such
544 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
546 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
547 typename _BinaryPredicate
>
549 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
550 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
551 _BinaryPredicate __predicate
)
553 // concept requirements
554 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
555 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
556 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
557 typename iterator_traits
<_ForwardIterator1
>::value_type
,
558 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
559 __glibcxx_requires_valid_range(__first1
, __last1
);
560 __glibcxx_requires_valid_range(__first2
, __last2
);
562 // Test for empty ranges
563 if (__first1
== __last1
|| __first2
== __last2
)
566 // Test for a pattern of length 1.
567 _ForwardIterator2
__tmp(__first2
);
569 if (__tmp
== __last2
)
571 while (__first1
!= __last1
&& !__predicate(*__first1
, *__first2
))
577 _ForwardIterator2 __p1
, __p
;
578 __p1
= __first2
; ++__p1
;
579 _ForwardIterator1 __current
= __first1
;
581 while (__first1
!= __last1
)
583 while (__first1
!= __last1
)
585 if (__predicate(*__first1
, *__first2
))
589 while (__first1
!= __last1
&& !__predicate(*__first1
, *__first2
))
591 if (__first1
== __last1
)
595 __current
= __first1
;
596 if (++__current
== __last1
)
599 while (__predicate(*__current
, *__p
))
601 if (++__p
== __last2
)
603 if (++__current
== __last1
)
613 * This is an uglified
614 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
615 * overloaded for forward iterators.
618 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
620 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
621 _Integer __count
, const _Tp
& __val
,
622 std::forward_iterator_tag
)
624 __first
= std::find(__first
, __last
, __val
);
625 while (__first
!= __last
)
627 typename iterator_traits
<_ForwardIterator
>::difference_type
629 _ForwardIterator __i
= __first
;
631 while (__i
!= __last
&& __n
!= 1 && *__i
== __val
)
640 __first
= std::find(++__i
, __last
, __val
);
647 * This is an uglified
648 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
649 * overloaded for random access iterators.
652 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
>
654 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
655 _Integer __count
, const _Tp
& __val
,
656 std::random_access_iterator_tag
)
659 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
662 _DistanceType __tailSize
= __last
- __first
;
663 const _DistanceType __pattSize
= __count
;
665 if (__tailSize
< __pattSize
)
668 const _DistanceType __skipOffset
= __pattSize
- 1;
669 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
670 __tailSize
-= __pattSize
;
672 while (1) // the main loop...
674 // __lookAhead here is always pointing to the last element of next
676 while (!(*__lookAhead
== __val
)) // the skip loop...
678 if (__tailSize
< __pattSize
)
679 return __last
; // Failure
680 __lookAhead
+= __pattSize
;
681 __tailSize
-= __pattSize
;
683 _DistanceType __remainder
= __skipOffset
;
684 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
685 *__backTrack
== __val
; --__backTrack
)
687 if (--__remainder
== 0)
688 return (__lookAhead
- __skipOffset
); // Success
690 if (__remainder
> __tailSize
)
691 return __last
; // Failure
692 __lookAhead
+= __remainder
;
693 __tailSize
-= __remainder
;
698 * @brief Search a sequence for a number of consecutive values.
699 * @param first A forward iterator.
700 * @param last A forward iterator.
701 * @param count The number of consecutive values.
702 * @param val The value to find.
703 * @return The first iterator @c i in the range @p [first,last-count)
704 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
705 * or @p last if no such iterator exists.
707 * Searches the range @p [first,last) for @p count consecutive elements
710 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
712 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
713 _Integer __count
, const _Tp
& __val
)
715 // concept requirements
716 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
717 __glibcxx_function_requires(_EqualOpConcept
<
718 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
719 __glibcxx_requires_valid_range(__first
, __last
);
724 return std::find(__first
, __last
, __val
);
725 return std::__search_n(__first
, __last
, __count
, __val
,
726 std::__iterator_category(__first
));
731 * This is an uglified
732 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
734 * overloaded for forward iterators.
737 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
738 typename _BinaryPredicate
>
740 __search_n(_ForwardIterator __first
, _ForwardIterator __last
,
741 _Integer __count
, const _Tp
& __val
,
742 _BinaryPredicate __binary_pred
, std::forward_iterator_tag
)
744 while (__first
!= __last
&& !__binary_pred(*__first
, __val
))
747 while (__first
!= __last
)
749 typename iterator_traits
<_ForwardIterator
>::difference_type
751 _ForwardIterator __i
= __first
;
753 while (__i
!= __last
&& __n
!= 1 && __binary_pred(*__i
, __val
))
763 while (__first
!= __last
&& !__binary_pred(*__first
, __val
))
771 * This is an uglified
772 * search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
774 * overloaded for random access iterators.
777 template<typename _RandomAccessIter
, typename _Integer
, typename _Tp
,
778 typename _BinaryPredicate
>
780 __search_n(_RandomAccessIter __first
, _RandomAccessIter __last
,
781 _Integer __count
, const _Tp
& __val
,
782 _BinaryPredicate __binary_pred
, std::random_access_iterator_tag
)
785 typedef typename
std::iterator_traits
<_RandomAccessIter
>::difference_type
788 _DistanceType __tailSize
= __last
- __first
;
789 const _DistanceType __pattSize
= __count
;
791 if (__tailSize
< __pattSize
)
794 const _DistanceType __skipOffset
= __pattSize
- 1;
795 _RandomAccessIter __lookAhead
= __first
+ __skipOffset
;
796 __tailSize
-= __pattSize
;
798 while (1) // the main loop...
800 // __lookAhead here is always pointing to the last element of next
802 while (!__binary_pred(*__lookAhead
, __val
)) // the skip loop...
804 if (__tailSize
< __pattSize
)
805 return __last
; // Failure
806 __lookAhead
+= __pattSize
;
807 __tailSize
-= __pattSize
;
809 _DistanceType __remainder
= __skipOffset
;
810 for (_RandomAccessIter __backTrack
= __lookAhead
- 1;
811 __binary_pred(*__backTrack
, __val
); --__backTrack
)
813 if (--__remainder
== 0)
814 return (__lookAhead
- __skipOffset
); // Success
816 if (__remainder
> __tailSize
)
817 return __last
; // Failure
818 __lookAhead
+= __remainder
;
819 __tailSize
-= __remainder
;
824 * @brief Search a sequence for a number of consecutive values using a
826 * @param first A forward iterator.
827 * @param last A forward iterator.
828 * @param count The number of consecutive values.
829 * @param val The value to find.
830 * @param binary_pred A binary predicate.
831 * @return The first iterator @c i in the range @p [first,last-count)
832 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
833 * range @p [0,count), or @p last if no such iterator exists.
835 * Searches the range @p [first,last) for @p count consecutive elements
836 * for which the predicate returns true.
838 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
839 typename _BinaryPredicate
>
841 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
842 _Integer __count
, const _Tp
& __val
,
843 _BinaryPredicate __binary_pred
)
845 // concept requirements
846 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
847 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
848 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
849 __glibcxx_requires_valid_range(__first
, __last
);
855 while (__first
!= __last
&& !__binary_pred(*__first
, __val
))
859 return std::__search_n(__first
, __last
, __count
, __val
, __binary_pred
,
860 std::__iterator_category(__first
));
864 * @brief Swap the elements of two sequences.
865 * @param first1 A forward iterator.
866 * @param last1 A forward iterator.
867 * @param first2 A forward iterator.
868 * @return An iterator equal to @p first2+(last1-first1).
870 * Swaps each element in the range @p [first1,last1) with the
871 * corresponding element in the range @p [first2,(last1-first1)).
872 * The ranges must not overlap.
874 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
876 swap_ranges(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
877 _ForwardIterator2 __first2
)
879 // concept requirements
880 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
882 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
884 __glibcxx_function_requires(_ConvertibleConcept
<
885 typename iterator_traits
<_ForwardIterator1
>::value_type
,
886 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
887 __glibcxx_function_requires(_ConvertibleConcept
<
888 typename iterator_traits
<_ForwardIterator2
>::value_type
,
889 typename iterator_traits
<_ForwardIterator1
>::value_type
>)
890 __glibcxx_requires_valid_range(__first1
, __last1
);
892 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
)
893 std::iter_swap(__first1
, __first2
);
898 * @brief Perform an operation on a sequence.
899 * @param first An input iterator.
900 * @param last An input iterator.
901 * @param result An output iterator.
902 * @param unary_op A unary operator.
903 * @return An output iterator equal to @p result+(last-first).
905 * Applies the operator to each element in the input range and assigns
906 * the results to successive elements of the output sequence.
907 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
908 * range @p [0,last-first).
910 * @p unary_op must not alter its argument.
912 template<typename _InputIterator
, typename _OutputIterator
,
913 typename _UnaryOperation
>
915 transform(_InputIterator __first
, _InputIterator __last
,
916 _OutputIterator __result
, _UnaryOperation __unary_op
)
918 // concept requirements
919 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
920 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
921 // "the type returned by a _UnaryOperation"
922 __typeof__(__unary_op(*__first
))>)
923 __glibcxx_requires_valid_range(__first
, __last
);
925 for ( ; __first
!= __last
; ++__first
, ++__result
)
926 *__result
= __unary_op(*__first
);
931 * @brief Perform an operation on corresponding elements of two sequences.
932 * @param first1 An input iterator.
933 * @param last1 An input iterator.
934 * @param first2 An input iterator.
935 * @param result An output iterator.
936 * @param binary_op A binary operator.
937 * @return An output iterator equal to @p result+(last-first).
939 * Applies the operator to the corresponding elements in the two
940 * input ranges and assigns the results to successive elements of the
942 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
943 * @c N in the range @p [0,last1-first1).
945 * @p binary_op must not alter either of its arguments.
947 template<typename _InputIterator1
, typename _InputIterator2
,
948 typename _OutputIterator
, typename _BinaryOperation
>
950 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
951 _InputIterator2 __first2
, _OutputIterator __result
,
952 _BinaryOperation __binary_op
)
954 // concept requirements
955 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
956 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
957 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
958 // "the type returned by a _BinaryOperation"
959 __typeof__(__binary_op(*__first1
,*__first2
))>)
960 __glibcxx_requires_valid_range(__first1
, __last1
);
962 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
963 *__result
= __binary_op(*__first1
, *__first2
);
968 * @brief Replace each occurrence of one value in a sequence with another
970 * @param first A forward iterator.
971 * @param last A forward iterator.
972 * @param old_value The value to be replaced.
973 * @param new_value The replacement value.
974 * @return replace() returns no value.
976 * For each iterator @c i in the range @p [first,last) if @c *i ==
977 * @p old_value then the assignment @c *i = @p new_value is performed.
979 template<typename _ForwardIterator
, typename _Tp
>
981 replace(_ForwardIterator __first
, _ForwardIterator __last
,
982 const _Tp
& __old_value
, const _Tp
& __new_value
)
984 // concept requirements
985 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
987 __glibcxx_function_requires(_EqualOpConcept
<
988 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
989 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
990 typename iterator_traits
<_ForwardIterator
>::value_type
>)
991 __glibcxx_requires_valid_range(__first
, __last
);
993 for ( ; __first
!= __last
; ++__first
)
994 if (*__first
== __old_value
)
995 *__first
= __new_value
;
999 * @brief Replace each value in a sequence for which a predicate returns
1000 * true with another value.
1001 * @param first A forward iterator.
1002 * @param last A forward iterator.
1003 * @param pred A predicate.
1004 * @param new_value The replacement value.
1005 * @return replace_if() returns no value.
1007 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
1008 * is true then the assignment @c *i = @p new_value is performed.
1010 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
1012 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
1013 _Predicate __pred
, const _Tp
& __new_value
)
1015 // concept requirements
1016 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1018 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
1019 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1020 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1021 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1022 __glibcxx_requires_valid_range(__first
, __last
);
1024 for ( ; __first
!= __last
; ++__first
)
1025 if (__pred(*__first
))
1026 *__first
= __new_value
;
1030 * @brief Copy a sequence, replacing each element of one value with another
1032 * @param first An input iterator.
1033 * @param last An input iterator.
1034 * @param result An output iterator.
1035 * @param old_value The value to be replaced.
1036 * @param new_value The replacement value.
1037 * @return The end of the output sequence, @p result+(last-first).
1039 * Copies each element in the input range @p [first,last) to the
1040 * output range @p [result,result+(last-first)) replacing elements
1041 * equal to @p old_value with @p new_value.
1043 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
1045 replace_copy(_InputIterator __first
, _InputIterator __last
,
1046 _OutputIterator __result
,
1047 const _Tp
& __old_value
, const _Tp
& __new_value
)
1049 // concept requirements
1050 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1051 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1052 typename iterator_traits
<_InputIterator
>::value_type
>)
1053 __glibcxx_function_requires(_EqualOpConcept
<
1054 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
1055 __glibcxx_requires_valid_range(__first
, __last
);
1057 for ( ; __first
!= __last
; ++__first
, ++__result
)
1058 if (*__first
== __old_value
)
1059 *__result
= __new_value
;
1061 *__result
= *__first
;
1066 * @brief Copy a sequence, replacing each value for which a predicate
1067 * returns true with another value.
1068 * @param first An input iterator.
1069 * @param last An input iterator.
1070 * @param result An output iterator.
1071 * @param pred A predicate.
1072 * @param new_value The replacement value.
1073 * @return The end of the output sequence, @p result+(last-first).
1075 * Copies each element in the range @p [first,last) to the range
1076 * @p [result,result+(last-first)) replacing elements for which
1077 * @p pred returns true with @p new_value.
1079 template<typename _InputIterator
, typename _OutputIterator
,
1080 typename _Predicate
, typename _Tp
>
1082 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
1083 _OutputIterator __result
,
1084 _Predicate __pred
, const _Tp
& __new_value
)
1086 // concept requirements
1087 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1088 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1089 typename iterator_traits
<_InputIterator
>::value_type
>)
1090 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1091 typename iterator_traits
<_InputIterator
>::value_type
>)
1092 __glibcxx_requires_valid_range(__first
, __last
);
1094 for ( ; __first
!= __last
; ++__first
, ++__result
)
1095 if (__pred(*__first
))
1096 *__result
= __new_value
;
1098 *__result
= *__first
;
1103 * @brief Assign the result of a function object to each value in a
1105 * @param first A forward iterator.
1106 * @param last A forward iterator.
1107 * @param gen A function object taking no arguments.
1108 * @return generate() returns no value.
1110 * Performs the assignment @c *i = @p gen() for each @c i in the range
1113 template<typename _ForwardIterator
, typename _Generator
>
1115 generate(_ForwardIterator __first
, _ForwardIterator __last
,
1118 // concept requirements
1119 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1120 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
1121 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1122 __glibcxx_requires_valid_range(__first
, __last
);
1124 for ( ; __first
!= __last
; ++__first
)
1129 * @brief Assign the result of a function object to each value in a
1131 * @param first A forward iterator.
1132 * @param n The length of the sequence.
1133 * @param gen A function object taking no arguments.
1134 * @return The end of the sequence, @p first+n
1136 * Performs the assignment @c *i = @p gen() for each @c i in the range
1137 * @p [first,first+n).
1139 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
1141 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
1143 // concept requirements
1144 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1145 // "the type returned by a _Generator"
1146 __typeof__(__gen())>)
1148 for ( ; __n
> 0; --__n
, ++__first
)
1154 * @brief Copy a sequence, removing elements of a given value.
1155 * @param first An input iterator.
1156 * @param last An input iterator.
1157 * @param result An output iterator.
1158 * @param value The value to be removed.
1159 * @return An iterator designating the end of the resulting sequence.
1161 * Copies each element in the range @p [first,last) not equal to @p value
1162 * to the range beginning at @p result.
1163 * remove_copy() is stable, so the relative order of elements that are
1164 * copied is unchanged.
1166 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
1168 remove_copy(_InputIterator __first
, _InputIterator __last
,
1169 _OutputIterator __result
, const _Tp
& __value
)
1171 // concept requirements
1172 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1173 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1174 typename iterator_traits
<_InputIterator
>::value_type
>)
1175 __glibcxx_function_requires(_EqualOpConcept
<
1176 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
1177 __glibcxx_requires_valid_range(__first
, __last
);
1179 for ( ; __first
!= __last
; ++__first
)
1180 if (!(*__first
== __value
))
1182 *__result
= *__first
;
1189 * @brief Copy a sequence, removing elements for which a predicate is true.
1190 * @param first An input iterator.
1191 * @param last An input iterator.
1192 * @param result An output iterator.
1193 * @param pred A predicate.
1194 * @return An iterator designating the end of the resulting sequence.
1196 * Copies each element in the range @p [first,last) for which
1197 * @p pred returns true to the range beginning at @p result.
1199 * remove_copy_if() is stable, so the relative order of elements that are
1200 * copied is unchanged.
1202 template<typename _InputIterator
, typename _OutputIterator
,
1203 typename _Predicate
>
1205 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
1206 _OutputIterator __result
, _Predicate __pred
)
1208 // concept requirements
1209 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1210 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1211 typename iterator_traits
<_InputIterator
>::value_type
>)
1212 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1213 typename iterator_traits
<_InputIterator
>::value_type
>)
1214 __glibcxx_requires_valid_range(__first
, __last
);
1216 for ( ; __first
!= __last
; ++__first
)
1217 if (!__pred(*__first
))
1219 *__result
= *__first
;
1226 * @brief Remove elements from a sequence.
1227 * @param first An input iterator.
1228 * @param last An input iterator.
1229 * @param value The value to be removed.
1230 * @return An iterator designating the end of the resulting sequence.
1232 * All elements equal to @p value are removed from the range
1235 * remove() is stable, so the relative order of elements that are
1236 * not removed is unchanged.
1238 * Elements between the end of the resulting sequence and @p last
1239 * are still present, but their value is unspecified.
1241 template<typename _ForwardIterator
, typename _Tp
>
1243 remove(_ForwardIterator __first
, _ForwardIterator __last
,
1246 // concept requirements
1247 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1249 __glibcxx_function_requires(_EqualOpConcept
<
1250 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1251 __glibcxx_requires_valid_range(__first
, __last
);
1253 __first
= std::find(__first
, __last
, __value
);
1254 _ForwardIterator __i
= __first
;
1255 return __first
== __last
? __first
1256 : std::remove_copy(++__i
, __last
,
1261 * @brief Remove elements from a sequence using a predicate.
1262 * @param first A forward iterator.
1263 * @param last A forward iterator.
1264 * @param pred A predicate.
1265 * @return An iterator designating the end of the resulting sequence.
1267 * All elements for which @p pred returns true are removed from the range
1270 * remove_if() is stable, so the relative order of elements that are
1271 * not removed is unchanged.
1273 * Elements between the end of the resulting sequence and @p last
1274 * are still present, but their value is unspecified.
1276 template<typename _ForwardIterator
, typename _Predicate
>
1278 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1281 // concept requirements
1282 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1284 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1285 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1286 __glibcxx_requires_valid_range(__first
, __last
);
1288 __first
= std::find_if(__first
, __last
, __pred
);
1289 _ForwardIterator __i
= __first
;
1290 return __first
== __last
? __first
1291 : std::remove_copy_if(++__i
, __last
,
1297 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1299 * overloaded for forward iterators and output iterator as result.
1302 template<typename _ForwardIterator
, typename _OutputIterator
>
1304 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1305 _OutputIterator __result
,
1306 forward_iterator_tag
, output_iterator_tag
)
1308 // concept requirements -- taken care of in dispatching function
1309 _ForwardIterator __next
= __first
;
1310 *__result
= *__first
;
1311 while (++__next
!= __last
)
1312 if (!(*__first
== *__next
))
1315 *++__result
= *__first
;
1322 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1324 * overloaded for input iterators and output iterator as result.
1327 template<typename _InputIterator
, typename _OutputIterator
>
1329 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1330 _OutputIterator __result
,
1331 input_iterator_tag
, output_iterator_tag
)
1333 // concept requirements -- taken care of in dispatching function
1334 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1335 *__result
= __value
;
1336 while (++__first
!= __last
)
1337 if (!(__value
== *__first
))
1340 *++__result
= __value
;
1347 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1349 * overloaded for input iterators and forward iterator as result.
1352 template<typename _InputIterator
, typename _ForwardIterator
>
1354 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1355 _ForwardIterator __result
,
1356 input_iterator_tag
, forward_iterator_tag
)
1358 // concept requirements -- taken care of in dispatching function
1359 *__result
= *__first
;
1360 while (++__first
!= __last
)
1361 if (!(*__result
== *__first
))
1362 *++__result
= *__first
;
1368 * This is an uglified
1369 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1371 * overloaded for forward iterators and output iterator as result.
1374 template<typename _ForwardIterator
, typename _OutputIterator
,
1375 typename _BinaryPredicate
>
1377 __unique_copy(_ForwardIterator __first
, _ForwardIterator __last
,
1378 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1379 forward_iterator_tag
, output_iterator_tag
)
1381 // concept requirements -- iterators already checked
1382 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1383 typename iterator_traits
<_ForwardIterator
>::value_type
,
1384 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1386 _ForwardIterator __next
= __first
;
1387 *__result
= *__first
;
1388 while (++__next
!= __last
)
1389 if (!__binary_pred(*__first
, *__next
))
1392 *++__result
= *__first
;
1399 * This is an uglified
1400 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1402 * overloaded for input iterators and output iterator as result.
1405 template<typename _InputIterator
, typename _OutputIterator
,
1406 typename _BinaryPredicate
>
1408 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1409 _OutputIterator __result
, _BinaryPredicate __binary_pred
,
1410 input_iterator_tag
, output_iterator_tag
)
1412 // concept requirements -- iterators already checked
1413 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1414 typename iterator_traits
<_InputIterator
>::value_type
,
1415 typename iterator_traits
<_InputIterator
>::value_type
>)
1417 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1418 *__result
= __value
;
1419 while (++__first
!= __last
)
1420 if (!__binary_pred(__value
, *__first
))
1423 *++__result
= __value
;
1430 * This is an uglified
1431 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1433 * overloaded for input iterators and forward iterator as result.
1436 template<typename _InputIterator
, typename _ForwardIterator
,
1437 typename _BinaryPredicate
>
1439 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1440 _ForwardIterator __result
, _BinaryPredicate __binary_pred
,
1441 input_iterator_tag
, forward_iterator_tag
)
1443 // concept requirements -- iterators already checked
1444 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1445 typename iterator_traits
<_ForwardIterator
>::value_type
,
1446 typename iterator_traits
<_InputIterator
>::value_type
>)
1448 *__result
= *__first
;
1449 while (++__first
!= __last
)
1450 if (!__binary_pred(*__result
, *__first
))
1451 *++__result
= *__first
;
1456 * @brief Copy a sequence, removing consecutive duplicate values.
1457 * @param first An input iterator.
1458 * @param last An input iterator.
1459 * @param result An output iterator.
1460 * @return An iterator designating the end of the resulting sequence.
1462 * Copies each element in the range @p [first,last) to the range
1463 * beginning at @p result, except that only the first element is copied
1464 * from groups of consecutive elements that compare equal.
1465 * unique_copy() is stable, so the relative order of elements that are
1466 * copied is unchanged.
1469 * _GLIBCXX_RESOLVE_LIB_DEFECTS
1470 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
1472 * _GLIBCXX_RESOLVE_LIB_DEFECTS
1473 * DR 538. 241 again: Does unique_copy() require CopyConstructible and
1477 template<typename _InputIterator
, typename _OutputIterator
>
1478 inline _OutputIterator
1479 unique_copy(_InputIterator __first
, _InputIterator __last
,
1480 _OutputIterator __result
)
1482 // concept requirements
1483 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1484 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1485 typename iterator_traits
<_InputIterator
>::value_type
>)
1486 __glibcxx_function_requires(_EqualityComparableConcept
<
1487 typename iterator_traits
<_InputIterator
>::value_type
>)
1488 __glibcxx_requires_valid_range(__first
, __last
);
1490 if (__first
== __last
)
1492 return std::__unique_copy(__first
, __last
, __result
,
1493 std::__iterator_category(__first
),
1494 std::__iterator_category(__result
));
1498 * @brief Copy a sequence, removing consecutive values using a predicate.
1499 * @param first An input iterator.
1500 * @param last An input iterator.
1501 * @param result An output iterator.
1502 * @param binary_pred A binary predicate.
1503 * @return An iterator designating the end of the resulting sequence.
1505 * Copies each element in the range @p [first,last) to the range
1506 * beginning at @p result, except that only the first element is copied
1507 * from groups of consecutive elements for which @p binary_pred returns
1509 * unique_copy() is stable, so the relative order of elements that are
1510 * copied is unchanged.
1513 * _GLIBCXX_RESOLVE_LIB_DEFECTS
1514 * DR 241. Does unique_copy() require CopyConstructible and Assignable?
1517 template<typename _InputIterator
, typename _OutputIterator
,
1518 typename _BinaryPredicate
>
1519 inline _OutputIterator
1520 unique_copy(_InputIterator __first
, _InputIterator __last
,
1521 _OutputIterator __result
,
1522 _BinaryPredicate __binary_pred
)
1524 // concept requirements -- predicates checked later
1525 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1526 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1527 typename iterator_traits
<_InputIterator
>::value_type
>)
1528 __glibcxx_requires_valid_range(__first
, __last
);
1530 if (__first
== __last
)
1532 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
,
1533 std::__iterator_category(__first
),
1534 std::__iterator_category(__result
));
1538 * @brief Remove consecutive duplicate values from a sequence.
1539 * @param first A forward iterator.
1540 * @param last A forward iterator.
1541 * @return An iterator designating the end of the resulting sequence.
1543 * Removes all but the first element from each group of consecutive
1544 * values that compare equal.
1545 * unique() is stable, so the relative order of elements that are
1546 * not removed is unchanged.
1547 * Elements between the end of the resulting sequence and @p last
1548 * are still present, but their value is unspecified.
1550 template<typename _ForwardIterator
>
1552 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1554 // concept requirements
1555 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1557 __glibcxx_function_requires(_EqualityComparableConcept
<
1558 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1559 __glibcxx_requires_valid_range(__first
, __last
);
1561 // Skip the beginning, if already unique.
1562 __first
= std::adjacent_find(__first
, __last
);
1563 if (__first
== __last
)
1566 // Do the real copy work.
1567 _ForwardIterator __dest
= __first
;
1569 while (++__first
!= __last
)
1570 if (!(*__dest
== *__first
))
1571 *++__dest
= *__first
;
1576 * @brief Remove consecutive values from a sequence using a predicate.
1577 * @param first A forward iterator.
1578 * @param last A forward iterator.
1579 * @param binary_pred A binary predicate.
1580 * @return An iterator designating the end of the resulting sequence.
1582 * Removes all but the first element from each group of consecutive
1583 * values for which @p binary_pred returns true.
1584 * unique() is stable, so the relative order of elements that are
1585 * not removed is unchanged.
1586 * Elements between the end of the resulting sequence and @p last
1587 * are still present, but their value is unspecified.
1589 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1591 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1592 _BinaryPredicate __binary_pred
)
1594 // concept requirements
1595 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1597 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1598 typename iterator_traits
<_ForwardIterator
>::value_type
,
1599 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1600 __glibcxx_requires_valid_range(__first
, __last
);
1602 // Skip the beginning, if already unique.
1603 __first
= std::adjacent_find(__first
, __last
, __binary_pred
);
1604 if (__first
== __last
)
1607 // Do the real copy work.
1608 _ForwardIterator __dest
= __first
;
1610 while (++__first
!= __last
)
1611 if (!__binary_pred(*__dest
, *__first
))
1612 *++__dest
= *__first
;
1618 * This is an uglified reverse(_BidirectionalIterator,
1619 * _BidirectionalIterator)
1620 * overloaded for bidirectional iterators.
1623 template<typename _BidirectionalIterator
>
1625 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1626 bidirectional_iterator_tag
)
1629 if (__first
== __last
|| __first
== --__last
)
1633 std::iter_swap(__first
, __last
);
1640 * This is an uglified reverse(_BidirectionalIterator,
1641 * _BidirectionalIterator)
1642 * overloaded for random access iterators.
1645 template<typename _RandomAccessIterator
>
1647 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1648 random_access_iterator_tag
)
1650 if (__first
== __last
)
1653 while (__first
< __last
)
1655 std::iter_swap(__first
, __last
);
1662 * @brief Reverse a sequence.
1663 * @param first A bidirectional iterator.
1664 * @param last A bidirectional iterator.
1665 * @return reverse() returns no value.
1667 * Reverses the order of the elements in the range @p [first,last),
1668 * so that the first element becomes the last etc.
1669 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1670 * swaps @p *(first+i) and @p *(last-(i+1))
1672 template<typename _BidirectionalIterator
>
1674 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1676 // concept requirements
1677 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1678 _BidirectionalIterator
>)
1679 __glibcxx_requires_valid_range(__first
, __last
);
1680 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1684 * @brief Copy a sequence, reversing its elements.
1685 * @param first A bidirectional iterator.
1686 * @param last A bidirectional iterator.
1687 * @param result An output iterator.
1688 * @return An iterator designating the end of the resulting sequence.
1690 * Copies the elements in the range @p [first,last) to the range
1691 * @p [result,result+(last-first)) such that the order of the
1692 * elements is reversed.
1693 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1694 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1695 * The ranges @p [first,last) and @p [result,result+(last-first))
1698 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1700 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1701 _OutputIterator __result
)
1703 // concept requirements
1704 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1705 _BidirectionalIterator
>)
1706 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1707 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1708 __glibcxx_requires_valid_range(__first
, __last
);
1710 while (__first
!= __last
)
1713 *__result
= *__last
;
1722 * This is a helper function for the rotate algorithm specialized on RAIs.
1723 * It returns the greatest common divisor of two integer values.
1726 template<typename _EuclideanRingElement
>
1727 _EuclideanRingElement
1728 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1732 _EuclideanRingElement __t
= __m
% __n
;
1741 * This is a helper function for the rotate algorithm.
1744 template<typename _ForwardIterator
>
1746 __rotate(_ForwardIterator __first
,
1747 _ForwardIterator __middle
,
1748 _ForwardIterator __last
,
1749 forward_iterator_tag
)
1751 if (__first
== __middle
|| __last
== __middle
)
1754 _ForwardIterator __first2
= __middle
;
1757 swap(*__first
, *__first2
);
1760 if (__first
== __middle
)
1761 __middle
= __first2
;
1763 while (__first2
!= __last
);
1765 __first2
= __middle
;
1767 while (__first2
!= __last
)
1769 swap(*__first
, *__first2
);
1772 if (__first
== __middle
)
1773 __middle
= __first2
;
1774 else if (__first2
== __last
)
1775 __first2
= __middle
;
1781 * This is a helper function for the rotate algorithm.
1784 template<typename _BidirectionalIterator
>
1786 __rotate(_BidirectionalIterator __first
,
1787 _BidirectionalIterator __middle
,
1788 _BidirectionalIterator __last
,
1789 bidirectional_iterator_tag
)
1791 // concept requirements
1792 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1793 _BidirectionalIterator
>)
1795 if (__first
== __middle
|| __last
== __middle
)
1798 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1799 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1801 while (__first
!= __middle
&& __middle
!= __last
)
1803 swap(*__first
, *--__last
);
1807 if (__first
== __middle
)
1808 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1810 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1815 * This is a helper function for the rotate algorithm.
1818 template<typename _RandomAccessIterator
>
1820 __rotate(_RandomAccessIterator __first
,
1821 _RandomAccessIterator __middle
,
1822 _RandomAccessIterator __last
,
1823 random_access_iterator_tag
)
1825 // concept requirements
1826 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1827 _RandomAccessIterator
>)
1829 if (__first
== __middle
|| __last
== __middle
)
1832 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1834 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1837 const _Distance __n
= __last
- __first
;
1838 const _Distance __k
= __middle
- __first
;
1839 const _Distance __l
= __n
- __k
;
1843 std::swap_ranges(__first
, __middle
, __middle
);
1847 const _Distance __d
= __gcd(__n
, __k
);
1849 for (_Distance __i
= 0; __i
< __d
; __i
++)
1851 _ValueType __tmp
= *__first
;
1852 _RandomAccessIterator __p
= __first
;
1856 for (_Distance __j
= 0; __j
< __l
/ __d
; __j
++)
1858 if (__p
> __first
+ __l
)
1860 *__p
= *(__p
- __l
);
1864 *__p
= *(__p
+ __k
);
1870 for (_Distance __j
= 0; __j
< __k
/ __d
- 1; __j
++)
1872 if (__p
< __last
- __k
)
1874 *__p
= *(__p
+ __k
);
1877 *__p
= * (__p
- __l
);
1888 * @brief Rotate the elements of a sequence.
1889 * @param first A forward iterator.
1890 * @param middle A forward iterator.
1891 * @param last A forward iterator.
1894 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1895 * positions so that the element at @p middle is moved to @p first, the
1896 * element at @p middle+1 is moved to @first+1 and so on for each element
1897 * in the range @p [first,last).
1899 * This effectively swaps the ranges @p [first,middle) and
1902 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1903 * each @p n in the range @p [0,last-first).
1905 template<typename _ForwardIterator
>
1907 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1908 _ForwardIterator __last
)
1910 // concept requirements
1911 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1913 __glibcxx_requires_valid_range(__first
, __middle
);
1914 __glibcxx_requires_valid_range(__middle
, __last
);
1916 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1918 std::__rotate(__first
, __middle
, __last
, _IterType());
1922 * @brief Copy a sequence, rotating its elements.
1923 * @param first A forward iterator.
1924 * @param middle A forward iterator.
1925 * @param last A forward iterator.
1926 * @param result An output iterator.
1927 * @return An iterator designating the end of the resulting sequence.
1929 * Copies the elements of the range @p [first,last) to the range
1930 * beginning at @result, rotating the copied elements by @p (middle-first)
1931 * positions so that the element at @p middle is moved to @p result, the
1932 * element at @p middle+1 is moved to @result+1 and so on for each element
1933 * in the range @p [first,last).
1935 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1936 * each @p n in the range @p [0,last-first).
1938 template<typename _ForwardIterator
, typename _OutputIterator
>
1940 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1941 _ForwardIterator __last
, _OutputIterator __result
)
1943 // concept requirements
1944 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1945 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1946 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1947 __glibcxx_requires_valid_range(__first
, __middle
);
1948 __glibcxx_requires_valid_range(__middle
, __last
);
1950 return std::copy(__first
, __middle
,
1951 std::copy(__middle
, __last
, __result
));
1955 * @brief Randomly shuffle the elements of a sequence.
1956 * @param first A forward iterator.
1957 * @param last A forward iterator.
1960 * Reorder the elements in the range @p [first,last) using a random
1961 * distribution, so that every possible ordering of the sequence is
1964 template<typename _RandomAccessIterator
>
1966 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
1968 // concept requirements
1969 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1970 _RandomAccessIterator
>)
1971 __glibcxx_requires_valid_range(__first
, __last
);
1973 if (__first
!= __last
)
1974 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1975 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
1979 * @brief Shuffle the elements of a sequence using a random number
1981 * @param first A forward iterator.
1982 * @param last A forward iterator.
1983 * @param rand The RNG functor or function.
1986 * Reorders the elements in the range @p [first,last) using @p rand to
1987 * provide a random distribution. Calling @p rand(N) for a positive
1988 * integer @p N should return a randomly chosen integer from the
1991 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
1993 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1994 _RandomNumberGenerator
& __rand
)
1996 // concept requirements
1997 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1998 _RandomAccessIterator
>)
1999 __glibcxx_requires_valid_range(__first
, __last
);
2001 if (__first
== __last
)
2003 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2004 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
2010 * This is a helper function...
2013 template<typename _ForwardIterator
, typename _Predicate
>
2015 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
2017 forward_iterator_tag
)
2019 if (__first
== __last
)
2022 while (__pred(*__first
))
2023 if (++__first
== __last
)
2026 _ForwardIterator __next
= __first
;
2028 while (++__next
!= __last
)
2029 if (__pred(*__next
))
2031 swap(*__first
, *__next
);
2040 * This is a helper function...
2043 template<typename _BidirectionalIterator
, typename _Predicate
>
2044 _BidirectionalIterator
2045 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
2047 bidirectional_iterator_tag
)
2052 if (__first
== __last
)
2054 else if (__pred(*__first
))
2060 if (__first
== __last
)
2062 else if (!__pred(*__last
))
2066 std::iter_swap(__first
, __last
);
2072 * @brief Move elements for which a predicate is true to the beginning
2074 * @param first A forward iterator.
2075 * @param last A forward iterator.
2076 * @param pred A predicate functor.
2077 * @return An iterator @p middle such that @p pred(i) is true for each
2078 * iterator @p i in the range @p [first,middle) and false for each @p i
2079 * in the range @p [middle,last).
2081 * @p pred must not modify its operand. @p partition() does not preserve
2082 * the relative ordering of elements in each group, use
2083 * @p stable_partition() if this is needed.
2085 template<typename _ForwardIterator
, typename _Predicate
>
2086 inline _ForwardIterator
2087 partition(_ForwardIterator __first
, _ForwardIterator __last
,
2090 // concept requirements
2091 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
2093 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
2094 typename iterator_traits
<_ForwardIterator
>::value_type
>)
2095 __glibcxx_requires_valid_range(__first
, __last
);
2097 return std::__partition(__first
, __last
, __pred
,
2098 std::__iterator_category(__first
));
2104 * This is a helper function...
2107 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
2109 __inplace_stable_partition(_ForwardIterator __first
,
2110 _ForwardIterator __last
,
2111 _Predicate __pred
, _Distance __len
)
2114 return __pred(*__first
) ? __last
: __first
;
2115 _ForwardIterator __middle
= __first
;
2116 std::advance(__middle
, __len
/ 2);
2117 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
2121 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
2125 std::rotate(__begin
, __middle
, __end
);
2126 std::advance(__begin
, std::distance(__middle
, __end
));
2132 * This is a helper function...
2135 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
2138 __stable_partition_adaptive(_ForwardIterator __first
,
2139 _ForwardIterator __last
,
2140 _Predicate __pred
, _Distance __len
,
2142 _Distance __buffer_size
)
2144 if (__len
<= __buffer_size
)
2146 _ForwardIterator __result1
= __first
;
2147 _Pointer __result2
= __buffer
;
2148 for ( ; __first
!= __last
; ++__first
)
2149 if (__pred(*__first
))
2151 *__result1
= *__first
;
2156 *__result2
= *__first
;
2159 std::copy(__buffer
, __result2
, __result1
);
2164 _ForwardIterator __middle
= __first
;
2165 std::advance(__middle
, __len
/ 2);
2166 _ForwardIterator __begin
=
2167 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
2168 __len
/ 2, __buffer
,
2170 _ForwardIterator __end
=
2171 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
2173 __buffer
, __buffer_size
);
2174 std::rotate(__begin
, __middle
, __end
);
2175 std::advance(__begin
, std::distance(__middle
, __end
));
2181 * @brief Move elements for which a predicate is true to the beginning
2182 * of a sequence, preserving relative ordering.
2183 * @param first A forward iterator.
2184 * @param last A forward iterator.
2185 * @param pred A predicate functor.
2186 * @return An iterator @p middle such that @p pred(i) is true for each
2187 * iterator @p i in the range @p [first,middle) and false for each @p i
2188 * in the range @p [middle,last).
2190 * Performs the same function as @p partition() with the additional
2191 * guarantee that the relative ordering of elements in each group is
2192 * preserved, so any two elements @p x and @p y in the range
2193 * @p [first,last) such that @p pred(x)==pred(y) will have the same
2194 * relative ordering after calling @p stable_partition().
2196 template<typename _ForwardIterator
, typename _Predicate
>
2198 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
2201 // concept requirements
2202 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
2204 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
2205 typename iterator_traits
<_ForwardIterator
>::value_type
>)
2206 __glibcxx_requires_valid_range(__first
, __last
);
2208 if (__first
== __last
)
2212 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2214 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2217 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
2219 if (__buf
.size() > 0)
2221 std::__stable_partition_adaptive(__first
, __last
, __pred
,
2222 _DistanceType(__buf
.requested_size()),
2223 __buf
.begin(), __buf
.size());
2226 std::__inplace_stable_partition(__first
, __last
, __pred
,
2227 _DistanceType(__buf
.requested_size()));
2233 * This is a helper function...
2236 template<typename _RandomAccessIterator
, typename _Tp
>
2237 _RandomAccessIterator
2238 __unguarded_partition(_RandomAccessIterator __first
,
2239 _RandomAccessIterator __last
, _Tp __pivot
)
2243 while (*__first
< __pivot
)
2246 while (__pivot
< *__last
)
2248 if (!(__first
< __last
))
2250 std::iter_swap(__first
, __last
);
2257 * This is a helper function...
2260 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2261 _RandomAccessIterator
2262 __unguarded_partition(_RandomAccessIterator __first
,
2263 _RandomAccessIterator __last
,
2264 _Tp __pivot
, _Compare __comp
)
2268 while (__comp(*__first
, __pivot
))
2271 while (__comp(__pivot
, *__last
))
2273 if (!(__first
< __last
))
2275 std::iter_swap(__first
, __last
);
2283 * This controls some aspect of the sort routines.
2286 enum { _S_threshold
= 16 };
2290 * This is a helper function for the sort routine.
2293 template<typename _RandomAccessIterator
, typename _Tp
>
2295 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
2297 _RandomAccessIterator __next
= __last
;
2299 while (__val
< *__next
)
2310 * This is a helper function for the sort routine.
2313 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2315 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
2318 _RandomAccessIterator __next
= __last
;
2320 while (__comp(__val
, *__next
))
2331 * This is a helper function for the sort routine.
2334 template<typename _RandomAccessIterator
>
2336 __insertion_sort(_RandomAccessIterator __first
,
2337 _RandomAccessIterator __last
)
2339 if (__first
== __last
)
2342 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2344 typename iterator_traits
<_RandomAccessIterator
>::value_type
2346 if (__val
< *__first
)
2348 std::copy_backward(__first
, __i
, __i
+ 1);
2352 std::__unguarded_linear_insert(__i
, __val
);
2358 * This is a helper function for the sort routine.
2361 template<typename _RandomAccessIterator
, typename _Compare
>
2363 __insertion_sort(_RandomAccessIterator __first
,
2364 _RandomAccessIterator __last
, _Compare __comp
)
2366 if (__first
== __last
) return;
2368 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2370 typename iterator_traits
<_RandomAccessIterator
>::value_type
2372 if (__comp(__val
, *__first
))
2374 std::copy_backward(__first
, __i
, __i
+ 1);
2378 std::__unguarded_linear_insert(__i
, __val
, __comp
);
2384 * This is a helper function for the sort routine.
2387 template<typename _RandomAccessIterator
>
2389 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2390 _RandomAccessIterator __last
)
2392 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2395 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2396 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
2401 * This is a helper function for the sort routine.
2404 template<typename _RandomAccessIterator
, typename _Compare
>
2406 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2407 _RandomAccessIterator __last
, _Compare __comp
)
2409 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2412 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2413 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
2418 * This is a helper function for the sort routine.
2421 template<typename _RandomAccessIterator
>
2423 __final_insertion_sort(_RandomAccessIterator __first
,
2424 _RandomAccessIterator __last
)
2426 if (__last
- __first
> int(_S_threshold
))
2428 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
2429 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
2432 std::__insertion_sort(__first
, __last
);
2437 * This is a helper function for the sort routine.
2440 template<typename _RandomAccessIterator
, typename _Compare
>
2442 __final_insertion_sort(_RandomAccessIterator __first
,
2443 _RandomAccessIterator __last
, _Compare __comp
)
2445 if (__last
- __first
> int(_S_threshold
))
2447 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
2448 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
2452 std::__insertion_sort(__first
, __last
, __comp
);
2457 * This is a helper function for the sort routines.
2460 template<typename _RandomAccessIterator
>
2462 __heap_select(_RandomAccessIterator __first
,
2463 _RandomAccessIterator __middle
,
2464 _RandomAccessIterator __last
)
2466 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2469 std::make_heap(__first
, __middle
);
2470 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
2471 if (*__i
< *__first
)
2472 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
));
2477 * This is a helper function for the sort routines.
2480 template<typename _RandomAccessIterator
, typename _Compare
>
2482 __heap_select(_RandomAccessIterator __first
,
2483 _RandomAccessIterator __middle
,
2484 _RandomAccessIterator __last
, _Compare __comp
)
2486 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2489 std::make_heap(__first
, __middle
, __comp
);
2490 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
2491 if (__comp(*__i
, *__first
))
2492 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
), __comp
);
2497 * This is a helper function for the sort routines.
2500 template<typename _Size
>
2505 for (__k
= 0; __n
!= 1; __n
>>= 1)
2511 * @brief Sort the smallest elements of a sequence.
2512 * @param first An iterator.
2513 * @param middle Another iterator.
2514 * @param last Another iterator.
2517 * Sorts the smallest @p (middle-first) elements in the range
2518 * @p [first,last) and moves them to the range @p [first,middle). The
2519 * order of the remaining elements in the range @p [middle,last) is
2521 * After the sort if @p i and @j are iterators in the range
2522 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2523 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
2525 template<typename _RandomAccessIterator
>
2527 partial_sort(_RandomAccessIterator __first
,
2528 _RandomAccessIterator __middle
,
2529 _RandomAccessIterator __last
)
2531 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2534 // concept requirements
2535 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2536 _RandomAccessIterator
>)
2537 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2538 __glibcxx_requires_valid_range(__first
, __middle
);
2539 __glibcxx_requires_valid_range(__middle
, __last
);
2541 std::__heap_select(__first
, __middle
, __last
);
2542 std::sort_heap(__first
, __middle
);
2546 * @brief Sort the smallest elements of a sequence using a predicate
2548 * @param first An iterator.
2549 * @param middle Another iterator.
2550 * @param last Another iterator.
2551 * @param comp A comparison functor.
2554 * Sorts the smallest @p (middle-first) elements in the range
2555 * @p [first,last) and moves them to the range @p [first,middle). The
2556 * order of the remaining elements in the range @p [middle,last) is
2558 * After the sort if @p i and @j are iterators in the range
2559 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2560 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
2563 template<typename _RandomAccessIterator
, typename _Compare
>
2565 partial_sort(_RandomAccessIterator __first
,
2566 _RandomAccessIterator __middle
,
2567 _RandomAccessIterator __last
,
2570 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2573 // concept requirements
2574 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2575 _RandomAccessIterator
>)
2576 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2577 _ValueType
, _ValueType
>)
2578 __glibcxx_requires_valid_range(__first
, __middle
);
2579 __glibcxx_requires_valid_range(__middle
, __last
);
2581 std::__heap_select(__first
, __middle
, __last
, __comp
);
2582 std::sort_heap(__first
, __middle
, __comp
);
2586 * @brief Copy the smallest elements of a sequence.
2587 * @param first An iterator.
2588 * @param last Another iterator.
2589 * @param result_first A random-access iterator.
2590 * @param result_last Another random-access iterator.
2591 * @return An iterator indicating the end of the resulting sequence.
2593 * Copies and sorts the smallest N values from the range @p [first,last)
2594 * to the range beginning at @p result_first, where the number of
2595 * elements to be copied, @p N, is the smaller of @p (last-first) and
2596 * @p (result_last-result_first).
2597 * After the sort if @p i and @j are iterators in the range
2598 * @p [result_first,result_first+N) such that @i precedes @j then
2599 * @p *j<*i is false.
2600 * The value returned is @p result_first+N.
2602 template<typename _InputIterator
, typename _RandomAccessIterator
>
2603 _RandomAccessIterator
2604 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2605 _RandomAccessIterator __result_first
,
2606 _RandomAccessIterator __result_last
)
2608 typedef typename iterator_traits
<_InputIterator
>::value_type
2610 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2612 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2615 // concept requirements
2616 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2617 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2619 __glibcxx_function_requires(_LessThanOpConcept
<_InputValueType
,
2621 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
2622 __glibcxx_requires_valid_range(__first
, __last
);
2623 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2625 if (__result_first
== __result_last
)
2626 return __result_last
;
2627 _RandomAccessIterator __result_real_last
= __result_first
;
2628 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2630 *__result_real_last
= *__first
;
2631 ++__result_real_last
;
2634 std::make_heap(__result_first
, __result_real_last
);
2635 while (__first
!= __last
)
2637 if (*__first
< *__result_first
)
2638 std::__adjust_heap(__result_first
, _DistanceType(0),
2639 _DistanceType(__result_real_last
2641 _InputValueType(*__first
));
2644 std::sort_heap(__result_first
, __result_real_last
);
2645 return __result_real_last
;
2649 * @brief Copy the smallest elements of a sequence using a predicate for
2651 * @param first An input iterator.
2652 * @param last Another input iterator.
2653 * @param result_first A random-access iterator.
2654 * @param result_last Another random-access iterator.
2655 * @param comp A comparison functor.
2656 * @return An iterator indicating the end of the resulting sequence.
2658 * Copies and sorts the smallest N values from the range @p [first,last)
2659 * to the range beginning at @p result_first, where the number of
2660 * elements to be copied, @p N, is the smaller of @p (last-first) and
2661 * @p (result_last-result_first).
2662 * After the sort if @p i and @j are iterators in the range
2663 * @p [result_first,result_first+N) such that @i precedes @j then
2664 * @p comp(*j,*i) is false.
2665 * The value returned is @p result_first+N.
2667 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
2668 _RandomAccessIterator
2669 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2670 _RandomAccessIterator __result_first
,
2671 _RandomAccessIterator __result_last
,
2674 typedef typename iterator_traits
<_InputIterator
>::value_type
2676 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2678 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2681 // concept requirements
2682 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2683 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2684 _RandomAccessIterator
>)
2685 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2687 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2688 _InputValueType
, _OutputValueType
>)
2689 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2690 _OutputValueType
, _OutputValueType
>)
2691 __glibcxx_requires_valid_range(__first
, __last
);
2692 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2694 if (__result_first
== __result_last
)
2695 return __result_last
;
2696 _RandomAccessIterator __result_real_last
= __result_first
;
2697 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2699 *__result_real_last
= *__first
;
2700 ++__result_real_last
;
2703 std::make_heap(__result_first
, __result_real_last
, __comp
);
2704 while (__first
!= __last
)
2706 if (__comp(*__first
, *__result_first
))
2707 std::__adjust_heap(__result_first
, _DistanceType(0),
2708 _DistanceType(__result_real_last
2710 _InputValueType(*__first
),
2714 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2715 return __result_real_last
;
2720 * This is a helper function for the sort routine.
2723 template<typename _RandomAccessIterator
, typename _Size
>
2725 __introsort_loop(_RandomAccessIterator __first
,
2726 _RandomAccessIterator __last
,
2727 _Size __depth_limit
)
2729 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2732 while (__last
- __first
> int(_S_threshold
))
2734 if (__depth_limit
== 0)
2736 std::partial_sort(__first
, __last
, __last
);
2740 _RandomAccessIterator __cut
=
2741 std::__unguarded_partition(__first
, __last
,
2742 _ValueType(std::__median(*__first
,
2749 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2756 * This is a helper function for the sort routine.
2759 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2761 __introsort_loop(_RandomAccessIterator __first
,
2762 _RandomAccessIterator __last
,
2763 _Size __depth_limit
, _Compare __comp
)
2765 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2768 while (__last
- __first
> int(_S_threshold
))
2770 if (__depth_limit
== 0)
2772 std::partial_sort(__first
, __last
, __last
, __comp
);
2776 _RandomAccessIterator __cut
=
2777 std::__unguarded_partition(__first
, __last
,
2778 _ValueType(std::__median(*__first
,
2786 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2792 * @brief Sort the elements of a sequence.
2793 * @param first An iterator.
2794 * @param last Another iterator.
2797 * Sorts the elements in the range @p [first,last) in ascending order,
2798 * such that @p *(i+1)<*i is false for each iterator @p i in the range
2799 * @p [first,last-1).
2801 * The relative ordering of equivalent elements is not preserved, use
2802 * @p stable_sort() if this is needed.
2804 template<typename _RandomAccessIterator
>
2806 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
2808 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2811 // concept requirements
2812 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2813 _RandomAccessIterator
>)
2814 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2815 __glibcxx_requires_valid_range(__first
, __last
);
2817 if (__first
!= __last
)
2819 std::__introsort_loop(__first
, __last
,
2820 std::__lg(__last
- __first
) * 2);
2821 std::__final_insertion_sort(__first
, __last
);
2826 * @brief Sort the elements of a sequence using a predicate for comparison.
2827 * @param first An iterator.
2828 * @param last Another iterator.
2829 * @param comp A comparison functor.
2832 * Sorts the elements in the range @p [first,last) in ascending order,
2833 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
2834 * range @p [first,last-1).
2836 * The relative ordering of equivalent elements is not preserved, use
2837 * @p stable_sort() if this is needed.
2839 template<typename _RandomAccessIterator
, typename _Compare
>
2841 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2844 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2847 // concept requirements
2848 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2849 _RandomAccessIterator
>)
2850 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
2852 __glibcxx_requires_valid_range(__first
, __last
);
2854 if (__first
!= __last
)
2856 std::__introsort_loop(__first
, __last
,
2857 std::__lg(__last
- __first
) * 2, __comp
);
2858 std::__final_insertion_sort(__first
, __last
, __comp
);
2863 * @brief Finds the first position in which @a val could be inserted
2864 * without changing the ordering.
2865 * @param first An iterator.
2866 * @param last Another iterator.
2867 * @param val The search term.
2868 * @return An iterator pointing to the first element "not less than" @a val,
2869 * or end() if every element is less than @a val.
2870 * @ingroup binarysearch
2872 template<typename _ForwardIterator
, typename _Tp
>
2874 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2877 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2879 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2882 // concept requirements
2883 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2884 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
2885 __glibcxx_requires_partitioned(__first
, __last
, __val
);
2887 _DistanceType __len
= std::distance(__first
, __last
);
2888 _DistanceType __half
;
2889 _ForwardIterator __middle
;
2893 __half
= __len
>> 1;
2895 std::advance(__middle
, __half
);
2896 if (*__middle
< __val
)
2900 __len
= __len
- __half
- 1;
2909 * @brief Finds the first position in which @a val could be inserted
2910 * without changing the ordering.
2911 * @param first An iterator.
2912 * @param last Another iterator.
2913 * @param val The search term.
2914 * @param comp A functor to use for comparisons.
2915 * @return An iterator pointing to the first element "not less than" @a val,
2916 * or end() if every element is less than @a val.
2917 * @ingroup binarysearch
2919 * The comparison function should have the same effects on ordering as
2920 * the function used for the initial sort.
2922 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2924 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2925 const _Tp
& __val
, _Compare __comp
)
2927 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2929 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2932 // concept requirements
2933 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2934 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2936 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
2938 _DistanceType __len
= std::distance(__first
, __last
);
2939 _DistanceType __half
;
2940 _ForwardIterator __middle
;
2944 __half
= __len
>> 1;
2946 std::advance(__middle
, __half
);
2947 if (__comp(*__middle
, __val
))
2951 __len
= __len
- __half
- 1;
2960 * @brief Finds the last position in which @a val could be inserted
2961 * without changing the ordering.
2962 * @param first An iterator.
2963 * @param last Another iterator.
2964 * @param val The search term.
2965 * @return An iterator pointing to the first element greater than @a val,
2966 * or end() if no elements are greater than @a val.
2967 * @ingroup binarysearch
2969 template<typename _ForwardIterator
, typename _Tp
>
2971 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2974 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2976 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2979 // concept requirements
2980 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2981 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
2982 __glibcxx_requires_partitioned(__first
, __last
, __val
);
2984 _DistanceType __len
= std::distance(__first
, __last
);
2985 _DistanceType __half
;
2986 _ForwardIterator __middle
;
2990 __half
= __len
>> 1;
2992 std::advance(__middle
, __half
);
2993 if (__val
< *__middle
)
2999 __len
= __len
- __half
- 1;
3006 * @brief Finds the last position in which @a val could be inserted
3007 * without changing the ordering.
3008 * @param first An iterator.
3009 * @param last Another iterator.
3010 * @param val The search term.
3011 * @param comp A functor to use for comparisons.
3012 * @return An iterator pointing to the first element greater than @a val,
3013 * or end() if no elements are greater than @a val.
3014 * @ingroup binarysearch
3016 * The comparison function should have the same effects on ordering as
3017 * the function used for the initial sort.
3019 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3021 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
3022 const _Tp
& __val
, _Compare __comp
)
3024 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3026 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3029 // concept requirements
3030 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3031 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3033 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
3035 _DistanceType __len
= std::distance(__first
, __last
);
3036 _DistanceType __half
;
3037 _ForwardIterator __middle
;
3041 __half
= __len
>> 1;
3043 std::advance(__middle
, __half
);
3044 if (__comp(__val
, *__middle
))
3050 __len
= __len
- __half
- 1;
3058 * This is a helper function for the merge routines.
3061 template<typename _BidirectionalIterator
, typename _Distance
>
3063 __merge_without_buffer(_BidirectionalIterator __first
,
3064 _BidirectionalIterator __middle
,
3065 _BidirectionalIterator __last
,
3066 _Distance __len1
, _Distance __len2
)
3068 if (__len1
== 0 || __len2
== 0)
3070 if (__len1
+ __len2
== 2)
3072 if (*__middle
< *__first
)
3073 std::iter_swap(__first
, __middle
);
3076 _BidirectionalIterator __first_cut
= __first
;
3077 _BidirectionalIterator __second_cut
= __middle
;
3078 _Distance __len11
= 0;
3079 _Distance __len22
= 0;
3080 if (__len1
> __len2
)
3082 __len11
= __len1
/ 2;
3083 std::advance(__first_cut
, __len11
);
3084 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
3085 __len22
= std::distance(__middle
, __second_cut
);
3089 __len22
= __len2
/ 2;
3090 std::advance(__second_cut
, __len22
);
3091 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
3092 __len11
= std::distance(__first
, __first_cut
);
3094 std::rotate(__first_cut
, __middle
, __second_cut
);
3095 _BidirectionalIterator __new_middle
= __first_cut
;
3096 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3097 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3099 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3100 __len1
- __len11
, __len2
- __len22
);
3105 * This is a helper function for the merge routines.
3108 template<typename _BidirectionalIterator
, typename _Distance
,
3111 __merge_without_buffer(_BidirectionalIterator __first
,
3112 _BidirectionalIterator __middle
,
3113 _BidirectionalIterator __last
,
3114 _Distance __len1
, _Distance __len2
,
3117 if (__len1
== 0 || __len2
== 0)
3119 if (__len1
+ __len2
== 2)
3121 if (__comp(*__middle
, *__first
))
3122 std::iter_swap(__first
, __middle
);
3125 _BidirectionalIterator __first_cut
= __first
;
3126 _BidirectionalIterator __second_cut
= __middle
;
3127 _Distance __len11
= 0;
3128 _Distance __len22
= 0;
3129 if (__len1
> __len2
)
3131 __len11
= __len1
/ 2;
3132 std::advance(__first_cut
, __len11
);
3133 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3135 __len22
= std::distance(__middle
, __second_cut
);
3139 __len22
= __len2
/ 2;
3140 std::advance(__second_cut
, __len22
);
3141 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3143 __len11
= std::distance(__first
, __first_cut
);
3145 std::rotate(__first_cut
, __middle
, __second_cut
);
3146 _BidirectionalIterator __new_middle
= __first_cut
;
3147 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
3148 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
3149 __len11
, __len22
, __comp
);
3150 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
3151 __len1
- __len11
, __len2
- __len22
, __comp
);
3156 * This is a helper function for the stable sorting routines.
3159 template<typename _RandomAccessIterator
>
3161 __inplace_stable_sort(_RandomAccessIterator __first
,
3162 _RandomAccessIterator __last
)
3164 if (__last
- __first
< 15)
3166 std::__insertion_sort(__first
, __last
);
3169 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3170 std::__inplace_stable_sort(__first
, __middle
);
3171 std::__inplace_stable_sort(__middle
, __last
);
3172 std::__merge_without_buffer(__first
, __middle
, __last
,
3179 * This is a helper function for the stable sorting routines.
3182 template<typename _RandomAccessIterator
, typename _Compare
>
3184 __inplace_stable_sort(_RandomAccessIterator __first
,
3185 _RandomAccessIterator __last
, _Compare __comp
)
3187 if (__last
- __first
< 15)
3189 std::__insertion_sort(__first
, __last
, __comp
);
3192 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
3193 std::__inplace_stable_sort(__first
, __middle
, __comp
);
3194 std::__inplace_stable_sort(__middle
, __last
, __comp
);
3195 std::__merge_without_buffer(__first
, __middle
, __last
,
3202 * @brief Merges two sorted ranges.
3203 * @param first1 An iterator.
3204 * @param first2 Another iterator.
3205 * @param last1 Another iterator.
3206 * @param last2 Another iterator.
3207 * @param result An iterator pointing to the end of the merged range.
3208 * @return An iterator pointing to the first element "not less than" @a val.
3210 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
3211 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
3212 * must be sorted, and the output range must not overlap with either of
3213 * the input ranges. The sort is @e stable, that is, for equivalent
3214 * elements in the two ranges, elements from the first range will always
3215 * come before elements from the second.
3217 template<typename _InputIterator1
, typename _InputIterator2
,
3218 typename _OutputIterator
>
3220 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3221 _InputIterator2 __first2
, _InputIterator2 __last2
,
3222 _OutputIterator __result
)
3224 typedef typename iterator_traits
<_InputIterator1
>::value_type
3226 typedef typename iterator_traits
<_InputIterator2
>::value_type
3229 // concept requirements
3230 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3231 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3232 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3234 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3236 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
3237 __glibcxx_requires_sorted(__first1
, __last1
);
3238 __glibcxx_requires_sorted(__first2
, __last2
);
3240 while (__first1
!= __last1
&& __first2
!= __last2
)
3242 if (*__first2
< *__first1
)
3244 *__result
= *__first2
;
3249 *__result
= *__first1
;
3254 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
3259 * @brief Merges two sorted ranges.
3260 * @param first1 An iterator.
3261 * @param first2 Another iterator.
3262 * @param last1 Another iterator.
3263 * @param last2 Another iterator.
3264 * @param result An iterator pointing to the end of the merged range.
3265 * @param comp A functor to use for comparisons.
3266 * @return An iterator pointing to the first element "not less than" @a val.
3268 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
3269 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
3270 * must be sorted, and the output range must not overlap with either of
3271 * the input ranges. The sort is @e stable, that is, for equivalent
3272 * elements in the two ranges, elements from the first range will always
3273 * come before elements from the second.
3275 * The comparison function should have the same effects on ordering as
3276 * the function used for the initial sort.
3278 template<typename _InputIterator1
, typename _InputIterator2
,
3279 typename _OutputIterator
, typename _Compare
>
3281 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3282 _InputIterator2 __first2
, _InputIterator2 __last2
,
3283 _OutputIterator __result
, _Compare __comp
)
3285 typedef typename iterator_traits
<_InputIterator1
>::value_type
3287 typedef typename iterator_traits
<_InputIterator2
>::value_type
3290 // concept requirements
3291 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3292 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3293 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3295 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3297 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3298 _ValueType2
, _ValueType1
>)
3299 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
3300 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
3302 while (__first1
!= __last1
&& __first2
!= __last2
)
3304 if (__comp(*__first2
, *__first1
))
3306 *__result
= *__first2
;
3311 *__result
= *__first1
;
3316 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
3320 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3323 __merge_sort_loop(_RandomAccessIterator1 __first
,
3324 _RandomAccessIterator1 __last
,
3325 _RandomAccessIterator2 __result
,
3326 _Distance __step_size
)
3328 const _Distance __two_step
= 2 * __step_size
;
3330 while (__last
- __first
>= __two_step
)
3332 __result
= std::merge(__first
, __first
+ __step_size
,
3333 __first
+ __step_size
, __first
+ __two_step
,
3335 __first
+= __two_step
;
3338 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3339 std::merge(__first
, __first
+ __step_size
, __first
+ __step_size
, __last
,
3343 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3344 typename _Distance
, typename _Compare
>
3346 __merge_sort_loop(_RandomAccessIterator1 __first
,
3347 _RandomAccessIterator1 __last
,
3348 _RandomAccessIterator2 __result
, _Distance __step_size
,
3351 const _Distance __two_step
= 2 * __step_size
;
3353 while (__last
- __first
>= __two_step
)
3355 __result
= std::merge(__first
, __first
+ __step_size
,
3356 __first
+ __step_size
, __first
+ __two_step
,
3359 __first
+= __two_step
;
3361 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3363 std::merge(__first
, __first
+ __step_size
,
3364 __first
+ __step_size
, __last
,
3369 enum { _S_chunk_size
= 7 };
3371 template<typename _RandomAccessIterator
, typename _Distance
>
3373 __chunk_insertion_sort(_RandomAccessIterator __first
,
3374 _RandomAccessIterator __last
,
3375 _Distance __chunk_size
)
3377 while (__last
- __first
>= __chunk_size
)
3379 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3380 __first
+= __chunk_size
;
3382 std::__insertion_sort(__first
, __last
);
3385 template<typename _RandomAccessIterator
, typename _Distance
, typename _Compare
>
3387 __chunk_insertion_sort(_RandomAccessIterator __first
,
3388 _RandomAccessIterator __last
,
3389 _Distance __chunk_size
, _Compare __comp
)
3391 while (__last
- __first
>= __chunk_size
)
3393 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3394 __first
+= __chunk_size
;
3396 std::__insertion_sort(__first
, __last
, __comp
);
3399 template<typename _RandomAccessIterator
, typename _Pointer
>
3401 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3402 _RandomAccessIterator __last
,
3405 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3408 const _Distance __len
= __last
- __first
;
3409 const _Pointer __buffer_last
= __buffer
+ __len
;
3411 _Distance __step_size
= _S_chunk_size
;
3412 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3414 while (__step_size
< __len
)
3416 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3418 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3423 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3425 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3426 _RandomAccessIterator __last
,
3427 _Pointer __buffer
, _Compare __comp
)
3429 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3432 const _Distance __len
= __last
- __first
;
3433 const _Pointer __buffer_last
= __buffer
+ __len
;
3435 _Distance __step_size
= _S_chunk_size
;
3436 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3438 while (__step_size
< __len
)
3440 std::__merge_sort_loop(__first
, __last
, __buffer
,
3441 __step_size
, __comp
);
3443 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3444 __step_size
, __comp
);
3451 * This is a helper function for the merge routines.
3454 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3455 typename _BidirectionalIterator3
>
3456 _BidirectionalIterator3
3457 __merge_backward(_BidirectionalIterator1 __first1
,
3458 _BidirectionalIterator1 __last1
,
3459 _BidirectionalIterator2 __first2
,
3460 _BidirectionalIterator2 __last2
,
3461 _BidirectionalIterator3 __result
)
3463 if (__first1
== __last1
)
3464 return std::copy_backward(__first2
, __last2
, __result
);
3465 if (__first2
== __last2
)
3466 return std::copy_backward(__first1
, __last1
, __result
);
3471 if (*__last2
< *__last1
)
3473 *--__result
= *__last1
;
3474 if (__first1
== __last1
)
3475 return std::copy_backward(__first2
, ++__last2
, __result
);
3480 *--__result
= *__last2
;
3481 if (__first2
== __last2
)
3482 return std::copy_backward(__first1
, ++__last1
, __result
);
3490 * This is a helper function for the merge routines.
3493 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3494 typename _BidirectionalIterator3
, typename _Compare
>
3495 _BidirectionalIterator3
3496 __merge_backward(_BidirectionalIterator1 __first1
,
3497 _BidirectionalIterator1 __last1
,
3498 _BidirectionalIterator2 __first2
,
3499 _BidirectionalIterator2 __last2
,
3500 _BidirectionalIterator3 __result
,
3503 if (__first1
== __last1
)
3504 return std::copy_backward(__first2
, __last2
, __result
);
3505 if (__first2
== __last2
)
3506 return std::copy_backward(__first1
, __last1
, __result
);
3511 if (__comp(*__last2
, *__last1
))
3513 *--__result
= *__last1
;
3514 if (__first1
== __last1
)
3515 return std::copy_backward(__first2
, ++__last2
, __result
);
3520 *--__result
= *__last2
;
3521 if (__first2
== __last2
)
3522 return std::copy_backward(__first1
, ++__last1
, __result
);
3530 * This is a helper function for the merge routines.
3533 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3535 _BidirectionalIterator1
3536 __rotate_adaptive(_BidirectionalIterator1 __first
,
3537 _BidirectionalIterator1 __middle
,
3538 _BidirectionalIterator1 __last
,
3539 _Distance __len1
, _Distance __len2
,
3540 _BidirectionalIterator2 __buffer
,
3541 _Distance __buffer_size
)
3543 _BidirectionalIterator2 __buffer_end
;
3544 if (__len1
> __len2
&& __len2
<= __buffer_size
)
3546 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3547 std::copy_backward(__first
, __middle
, __last
);
3548 return std::copy(__buffer
, __buffer_end
, __first
);
3550 else if (__len1
<= __buffer_size
)
3552 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3553 std::copy(__middle
, __last
, __first
);
3554 return std::copy_backward(__buffer
, __buffer_end
, __last
);
3558 std::rotate(__first
, __middle
, __last
);
3559 std::advance(__first
, std::distance(__middle
, __last
));
3566 * This is a helper function for the merge routines.
3569 template<typename _BidirectionalIterator
, typename _Distance
,
3572 __merge_adaptive(_BidirectionalIterator __first
,
3573 _BidirectionalIterator __middle
,
3574 _BidirectionalIterator __last
,
3575 _Distance __len1
, _Distance __len2
,
3576 _Pointer __buffer
, _Distance __buffer_size
)
3578 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
3580 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3581 std::merge(__buffer
, __buffer_end
, __middle
, __last
, __first
);
3583 else if (__len2
<= __buffer_size
)
3585 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3586 std::__merge_backward(__first
, __middle
, __buffer
,
3587 __buffer_end
, __last
);
3591 _BidirectionalIterator __first_cut
= __first
;
3592 _BidirectionalIterator __second_cut
= __middle
;
3593 _Distance __len11
= 0;
3594 _Distance __len22
= 0;
3595 if (__len1
> __len2
)
3597 __len11
= __len1
/ 2;
3598 std::advance(__first_cut
, __len11
);
3599 __second_cut
= std::lower_bound(__middle
, __last
,
3601 __len22
= std::distance(__middle
, __second_cut
);
3605 __len22
= __len2
/ 2;
3606 std::advance(__second_cut
, __len22
);
3607 __first_cut
= std::upper_bound(__first
, __middle
,
3609 __len11
= std::distance(__first
, __first_cut
);
3611 _BidirectionalIterator __new_middle
=
3612 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3613 __len1
- __len11
, __len22
, __buffer
,
3615 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3616 __len22
, __buffer
, __buffer_size
);
3617 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3619 __len2
- __len22
, __buffer
, __buffer_size
);
3625 * This is a helper function for the merge routines.
3628 template<typename _BidirectionalIterator
, typename _Distance
, typename _Pointer
,
3631 __merge_adaptive(_BidirectionalIterator __first
,
3632 _BidirectionalIterator __middle
,
3633 _BidirectionalIterator __last
,
3634 _Distance __len1
, _Distance __len2
,
3635 _Pointer __buffer
, _Distance __buffer_size
,
3638 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
3640 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3641 std::merge(__buffer
, __buffer_end
, __middle
, __last
, __first
, __comp
);
3643 else if (__len2
<= __buffer_size
)
3645 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3646 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
3651 _BidirectionalIterator __first_cut
= __first
;
3652 _BidirectionalIterator __second_cut
= __middle
;
3653 _Distance __len11
= 0;
3654 _Distance __len22
= 0;
3655 if (__len1
> __len2
)
3657 __len11
= __len1
/ 2;
3658 std::advance(__first_cut
, __len11
);
3659 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3661 __len22
= std::distance(__middle
, __second_cut
);
3665 __len22
= __len2
/ 2;
3666 std::advance(__second_cut
, __len22
);
3667 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3669 __len11
= std::distance(__first
, __first_cut
);
3671 _BidirectionalIterator __new_middle
=
3672 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3673 __len1
- __len11
, __len22
, __buffer
,
3675 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3676 __len22
, __buffer
, __buffer_size
, __comp
);
3677 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3679 __len2
- __len22
, __buffer
,
3680 __buffer_size
, __comp
);
3685 * @brief Merges two sorted ranges in place.
3686 * @param first An iterator.
3687 * @param middle Another iterator.
3688 * @param last Another iterator.
3691 * Merges two sorted and consecutive ranges, [first,middle) and
3692 * [middle,last), and puts the result in [first,last). The output will
3693 * be sorted. The sort is @e stable, that is, for equivalent
3694 * elements in the two ranges, elements from the first range will always
3695 * come before elements from the second.
3697 * If enough additional memory is available, this takes (last-first)-1
3698 * comparisons. Otherwise an NlogN algorithm is used, where N is
3699 * distance(first,last).
3701 template<typename _BidirectionalIterator
>
3703 inplace_merge(_BidirectionalIterator __first
,
3704 _BidirectionalIterator __middle
,
3705 _BidirectionalIterator __last
)
3707 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3709 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3712 // concept requirements
3713 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3714 _BidirectionalIterator
>)
3715 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3716 __glibcxx_requires_sorted(__first
, __middle
);
3717 __glibcxx_requires_sorted(__middle
, __last
);
3719 if (__first
== __middle
|| __middle
== __last
)
3722 _DistanceType __len1
= std::distance(__first
, __middle
);
3723 _DistanceType __len2
= std::distance(__middle
, __last
);
3725 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3727 if (__buf
.begin() == 0)
3728 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3730 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3731 __buf
.begin(), _DistanceType(__buf
.size()));
3735 * @brief Merges two sorted ranges in place.
3736 * @param first An iterator.
3737 * @param middle Another iterator.
3738 * @param last Another iterator.
3739 * @param comp A functor to use for comparisons.
3742 * Merges two sorted and consecutive ranges, [first,middle) and
3743 * [middle,last), and puts the result in [first,last). The output will
3744 * be sorted. The sort is @e stable, that is, for equivalent
3745 * elements in the two ranges, elements from the first range will always
3746 * come before elements from the second.
3748 * If enough additional memory is available, this takes (last-first)-1
3749 * comparisons. Otherwise an NlogN algorithm is used, where N is
3750 * distance(first,last).
3752 * The comparison function should have the same effects on ordering as
3753 * the function used for the initial sort.
3755 template<typename _BidirectionalIterator
, typename _Compare
>
3757 inplace_merge(_BidirectionalIterator __first
,
3758 _BidirectionalIterator __middle
,
3759 _BidirectionalIterator __last
,
3762 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3764 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3767 // concept requirements
3768 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3769 _BidirectionalIterator
>)
3770 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3771 _ValueType
, _ValueType
>)
3772 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3773 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3775 if (__first
== __middle
|| __middle
== __last
)
3778 const _DistanceType __len1
= std::distance(__first
, __middle
);
3779 const _DistanceType __len2
= std::distance(__middle
, __last
);
3781 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3783 if (__buf
.begin() == 0)
3784 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3787 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3788 __buf
.begin(), _DistanceType(__buf
.size()),
3792 template<typename _RandomAccessIterator
, typename _Pointer
,
3795 __stable_sort_adaptive(_RandomAccessIterator __first
,
3796 _RandomAccessIterator __last
,
3797 _Pointer __buffer
, _Distance __buffer_size
)
3799 const _Distance __len
= (__last
- __first
+ 1) / 2;
3800 const _RandomAccessIterator __middle
= __first
+ __len
;
3801 if (__len
> __buffer_size
)
3803 std::__stable_sort_adaptive(__first
, __middle
,
3804 __buffer
, __buffer_size
);
3805 std::__stable_sort_adaptive(__middle
, __last
,
3806 __buffer
, __buffer_size
);
3810 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3811 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3813 std::__merge_adaptive(__first
, __middle
, __last
,
3814 _Distance(__middle
- __first
),
3815 _Distance(__last
- __middle
),
3816 __buffer
, __buffer_size
);
3819 template<typename _RandomAccessIterator
, typename _Pointer
,
3820 typename _Distance
, typename _Compare
>
3822 __stable_sort_adaptive(_RandomAccessIterator __first
,
3823 _RandomAccessIterator __last
,
3824 _Pointer __buffer
, _Distance __buffer_size
,
3827 const _Distance __len
= (__last
- __first
+ 1) / 2;
3828 const _RandomAccessIterator __middle
= __first
+ __len
;
3829 if (__len
> __buffer_size
)
3831 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3832 __buffer_size
, __comp
);
3833 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3834 __buffer_size
, __comp
);
3838 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3839 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3841 std::__merge_adaptive(__first
, __middle
, __last
,
3842 _Distance(__middle
- __first
),
3843 _Distance(__last
- __middle
),
3844 __buffer
, __buffer_size
,
3849 * @brief Sort the elements of a sequence, preserving the relative order
3850 * of equivalent elements.
3851 * @param first An iterator.
3852 * @param last Another iterator.
3855 * Sorts the elements in the range @p [first,last) in ascending order,
3856 * such that @p *(i+1)<*i is false for each iterator @p i in the range
3857 * @p [first,last-1).
3859 * The relative ordering of equivalent elements is preserved, so any two
3860 * elements @p x and @p y in the range @p [first,last) such that
3861 * @p x<y is false and @p y<x is false will have the same relative
3862 * ordering after calling @p stable_sort().
3864 template<typename _RandomAccessIterator
>
3866 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
3868 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3870 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3873 // concept requirements
3874 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3875 _RandomAccessIterator
>)
3876 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3877 __glibcxx_requires_valid_range(__first
, __last
);
3879 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
3881 if (__buf
.begin() == 0)
3882 std::__inplace_stable_sort(__first
, __last
);
3884 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
3885 _DistanceType(__buf
.size()));
3889 * @brief Sort the elements of a sequence using a predicate for comparison,
3890 * preserving the relative order of equivalent elements.
3891 * @param first An iterator.
3892 * @param last Another iterator.
3893 * @param comp A comparison functor.
3896 * Sorts the elements in the range @p [first,last) in ascending order,
3897 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
3898 * range @p [first,last-1).
3900 * The relative ordering of equivalent elements is preserved, so any two
3901 * elements @p x and @p y in the range @p [first,last) such that
3902 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
3903 * relative ordering after calling @p stable_sort().
3905 template<typename _RandomAccessIterator
, typename _Compare
>
3907 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3910 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3912 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3915 // concept requirements
3916 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3917 _RandomAccessIterator
>)
3918 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3921 __glibcxx_requires_valid_range(__first
, __last
);
3923 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> __buf(__first
,
3925 if (__buf
.begin() == 0)
3926 std::__inplace_stable_sort(__first
, __last
, __comp
);
3928 std::__stable_sort_adaptive(__first
, __last
, __buf
.begin(),
3929 _DistanceType(__buf
.size()), __comp
);
3933 template<typename _RandomAccessIterator
, typename _Size
>
3935 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
3936 _RandomAccessIterator __last
, _Size __depth_limit
)
3938 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3941 while (__last
- __first
> 3)
3943 if (__depth_limit
== 0)
3945 std::__heap_select(__first
, __nth
+ 1, __last
);
3946 // Place the nth largest element in its final position.
3947 std::iter_swap(__first
, __nth
);
3951 _RandomAccessIterator __cut
=
3952 std::__unguarded_partition(__first
, __last
,
3953 _ValueType(std::__median(*__first
,
3965 std::__insertion_sort(__first
, __last
);
3968 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
3970 __introselect(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
3971 _RandomAccessIterator __last
, _Size __depth_limit
,
3974 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3977 while (__last
- __first
> 3)
3979 if (__depth_limit
== 0)
3981 std::__heap_select(__first
, __nth
+ 1, __last
, __comp
);
3982 // Place the nth largest element in its final position.
3983 std::iter_swap(__first
, __nth
);
3987 _RandomAccessIterator __cut
=
3988 std::__unguarded_partition(__first
, __last
,
3989 _ValueType(std::__median(*__first
,
4002 std::__insertion_sort(__first
, __last
, __comp
);
4006 * @brief Sort a sequence just enough to find a particular position.
4007 * @param first An iterator.
4008 * @param nth Another iterator.
4009 * @param last Another iterator.
4012 * Rearranges the elements in the range @p [first,last) so that @p *nth
4013 * is the same element that would have been in that position had the
4014 * whole sequence been sorted.
4015 * whole sequence been sorted. The elements either side of @p *nth are
4016 * not completely sorted, but for any iterator @i in the range
4017 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4018 * holds that @p *j<*i is false.
4020 template<typename _RandomAccessIterator
>
4022 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4023 _RandomAccessIterator __last
)
4025 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4028 // concept requirements
4029 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4030 _RandomAccessIterator
>)
4031 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
4032 __glibcxx_requires_valid_range(__first
, __nth
);
4033 __glibcxx_requires_valid_range(__nth
, __last
);
4035 if (__first
== __last
|| __nth
== __last
)
4038 std::__introselect(__first
, __nth
, __last
,
4039 std::__lg(__last
- __first
) * 2);
4043 * @brief Sort a sequence just enough to find a particular position
4044 * using a predicate for comparison.
4045 * @param first An iterator.
4046 * @param nth Another iterator.
4047 * @param last Another iterator.
4048 * @param comp A comparison functor.
4051 * Rearranges the elements in the range @p [first,last) so that @p *nth
4052 * is the same element that would have been in that position had the
4053 * whole sequence been sorted. The elements either side of @p *nth are
4054 * not completely sorted, but for any iterator @i in the range
4055 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
4056 * holds that @p comp(*j,*i) is false.
4058 template<typename _RandomAccessIterator
, typename _Compare
>
4060 nth_element(_RandomAccessIterator __first
, _RandomAccessIterator __nth
,
4061 _RandomAccessIterator __last
, _Compare __comp
)
4063 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
4066 // concept requirements
4067 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
4068 _RandomAccessIterator
>)
4069 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4070 _ValueType
, _ValueType
>)
4071 __glibcxx_requires_valid_range(__first
, __nth
);
4072 __glibcxx_requires_valid_range(__nth
, __last
);
4074 if (__first
== __last
|| __nth
== __last
)
4077 std::__introselect(__first
, __nth
, __last
,
4078 std::__lg(__last
- __first
) * 2, __comp
);
4082 * @brief Finds the largest subrange in which @a val could be inserted
4083 * at any place in it without changing the ordering.
4084 * @param first An iterator.
4085 * @param last Another iterator.
4086 * @param val The search term.
4087 * @return An pair of iterators defining the subrange.
4088 * @ingroup binarysearch
4090 * This is equivalent to
4092 * std::make_pair(lower_bound(first, last, val),
4093 * upper_bound(first, last, val))
4095 * but does not actually call those functions.
4097 template<typename _ForwardIterator
, typename _Tp
>
4098 pair
<_ForwardIterator
, _ForwardIterator
>
4099 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
4102 typedef typename iterator_traits
<_ForwardIterator
>::value_type
4104 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
4107 // concept requirements
4108 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4109 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
4110 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
4111 __glibcxx_requires_partitioned(__first
, __last
, __val
);
4113 _DistanceType __len
= std::distance(__first
, __last
);
4114 _DistanceType __half
;
4115 _ForwardIterator __middle
, __left
, __right
;
4119 __half
= __len
>> 1;
4121 std::advance(__middle
, __half
);
4122 if (*__middle
< __val
)
4126 __len
= __len
- __half
- 1;
4128 else if (__val
< *__middle
)
4132 __left
= std::lower_bound(__first
, __middle
, __val
);
4133 std::advance(__first
, __len
);
4134 __right
= std::upper_bound(++__middle
, __first
, __val
);
4135 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
4138 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
4142 * @brief Finds the largest subrange in which @a val could be inserted
4143 * at any place in it without changing the ordering.
4144 * @param first An iterator.
4145 * @param last Another iterator.
4146 * @param val The search term.
4147 * @param comp A functor to use for comparisons.
4148 * @return An pair of iterators defining the subrange.
4149 * @ingroup binarysearch
4151 * This is equivalent to
4153 * std::make_pair(lower_bound(first, last, val, comp),
4154 * upper_bound(first, last, val, comp))
4156 * but does not actually call those functions.
4158 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
4159 pair
<_ForwardIterator
, _ForwardIterator
>
4160 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
4164 typedef typename iterator_traits
<_ForwardIterator
>::value_type
4166 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
4169 // concept requirements
4170 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4171 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4173 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4175 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
4177 _DistanceType __len
= std::distance(__first
, __last
);
4178 _DistanceType __half
;
4179 _ForwardIterator __middle
, __left
, __right
;
4183 __half
= __len
>> 1;
4185 std::advance(__middle
, __half
);
4186 if (__comp(*__middle
, __val
))
4190 __len
= __len
- __half
- 1;
4192 else if (__comp(__val
, *__middle
))
4196 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
4197 std::advance(__first
, __len
);
4198 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
4199 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
4202 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
4206 * @brief Determines whether an element exists in a range.
4207 * @param first An iterator.
4208 * @param last Another iterator.
4209 * @param val The search term.
4210 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
4211 * @ingroup binarysearch
4213 * Note that this does not actually return an iterator to @a val. For
4214 * that, use std::find or a container's specialized find member functions.
4216 template<typename _ForwardIterator
, typename _Tp
>
4218 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
4221 typedef typename iterator_traits
<_ForwardIterator
>::value_type
4224 // concept requirements
4225 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4226 __glibcxx_function_requires(_LessThanOpConcept
<_Tp
, _ValueType
>)
4227 __glibcxx_requires_partitioned(__first
, __last
, __val
);
4229 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
4230 return __i
!= __last
&& !(__val
< *__i
);
4234 * @brief Determines whether an element exists in a range.
4235 * @param first An iterator.
4236 * @param last Another iterator.
4237 * @param val The search term.
4238 * @param comp A functor to use for comparisons.
4239 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
4240 * @ingroup binarysearch
4242 * Note that this does not actually return an iterator to @a val. For
4243 * that, use std::find or a container's specialized find member functions.
4245 * The comparison function should have the same effects on ordering as
4246 * the function used for the initial sort.
4248 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
4250 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
4251 const _Tp
& __val
, _Compare __comp
)
4253 typedef typename iterator_traits
<_ForwardIterator
>::value_type
4256 // concept requirements
4257 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4258 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4260 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
4262 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
4263 return __i
!= __last
&& !__comp(__val
, *__i
);
4266 // Set algorithms: includes, set_union, set_intersection, set_difference,
4267 // set_symmetric_difference. All of these algorithms have the precondition
4268 // that their input ranges are sorted and the postcondition that their output
4269 // ranges are sorted.
4272 * @brief Determines whether all elements of a sequence exists in a range.
4273 * @param first1 Start of search range.
4274 * @param last1 End of search range.
4275 * @param first2 Start of sequence
4276 * @param last2 End of sequence.
4277 * @return True if each element in [first2,last2) is contained in order
4278 * within [first1,last1). False otherwise.
4279 * @ingroup setoperations
4281 * This operation expects both [first1,last1) and [first2,last2) to be
4282 * sorted. Searches for the presence of each element in [first2,last2)
4283 * within [first1,last1). The iterators over each range only move forward,
4284 * so this is a linear algorithm. If an element in [first2,last2) is not
4285 * found before the search iterator reaches @a last2, false is returned.
4287 template<typename _InputIterator1
, typename _InputIterator2
>
4289 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
4290 _InputIterator2 __first2
, _InputIterator2 __last2
)
4292 typedef typename iterator_traits
<_InputIterator1
>::value_type
4294 typedef typename iterator_traits
<_InputIterator2
>::value_type
4297 // concept requirements
4298 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4299 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4300 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
4301 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4302 __glibcxx_requires_sorted(__first1
, __last1
);
4303 __glibcxx_requires_sorted(__first2
, __last2
);
4305 while (__first1
!= __last1
&& __first2
!= __last2
)
4306 if (*__first2
< *__first1
)
4308 else if(*__first1
< *__first2
)
4311 ++__first1
, ++__first2
;
4313 return __first2
== __last2
;
4317 * @brief Determines whether all elements of a sequence exists in a range
4319 * @param first1 Start of search range.
4320 * @param last1 End of search range.
4321 * @param first2 Start of sequence
4322 * @param last2 End of sequence.
4323 * @param comp Comparison function to use.
4324 * @return True if each element in [first2,last2) is contained in order
4325 * within [first1,last1) according to comp. False otherwise.
4326 * @ingroup setoperations
4328 * This operation expects both [first1,last1) and [first2,last2) to be
4329 * sorted. Searches for the presence of each element in [first2,last2)
4330 * within [first1,last1), using comp to decide. The iterators over each
4331 * range only move forward, so this is a linear algorithm. If an element
4332 * in [first2,last2) is not found before the search iterator reaches @a
4333 * last2, false is returned.
4335 template<typename _InputIterator1
, typename _InputIterator2
,
4338 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
4339 _InputIterator2 __first2
, _InputIterator2 __last2
, _Compare __comp
)
4341 typedef typename iterator_traits
<_InputIterator1
>::value_type
4343 typedef typename iterator_traits
<_InputIterator2
>::value_type
4346 // concept requirements
4347 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4348 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4349 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4350 _ValueType1
, _ValueType2
>)
4351 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4352 _ValueType2
, _ValueType1
>)
4353 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4354 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4356 while (__first1
!= __last1
&& __first2
!= __last2
)
4357 if (__comp(*__first2
, *__first1
))
4359 else if(__comp(*__first1
, *__first2
))
4362 ++__first1
, ++__first2
;
4364 return __first2
== __last2
;
4368 * @brief Return the union of two sorted ranges.
4369 * @param first1 Start of first range.
4370 * @param last1 End of first range.
4371 * @param first2 Start of second range.
4372 * @param last2 End of second range.
4373 * @return End of the output range.
4374 * @ingroup setoperations
4376 * This operation iterates over both ranges, copying elements present in
4377 * each range in order to the output range. Iterators increment for each
4378 * range. When the current element of one range is less than the other,
4379 * that element is copied and the iterator advanced. If an element is
4380 * contained in both ranges, the element from the first range is copied and
4381 * both ranges advance. The output range may not overlap either input
4384 template<typename _InputIterator1
, typename _InputIterator2
,
4385 typename _OutputIterator
>
4387 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4388 _InputIterator2 __first2
, _InputIterator2 __last2
,
4389 _OutputIterator __result
)
4391 typedef typename iterator_traits
<_InputIterator1
>::value_type
4393 typedef typename iterator_traits
<_InputIterator2
>::value_type
4396 // concept requirements
4397 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4398 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4399 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4401 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4403 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
4404 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4405 __glibcxx_requires_sorted(__first1
, __last1
);
4406 __glibcxx_requires_sorted(__first2
, __last2
);
4408 while (__first1
!= __last1
&& __first2
!= __last2
)
4410 if (*__first1
< *__first2
)
4412 *__result
= *__first1
;
4415 else if (*__first2
< *__first1
)
4417 *__result
= *__first2
;
4422 *__result
= *__first1
;
4428 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4433 * @brief Return the union of two sorted ranges using a comparison functor.
4434 * @param first1 Start of first range.
4435 * @param last1 End of first range.
4436 * @param first2 Start of second range.
4437 * @param last2 End of second range.
4438 * @param comp The comparison functor.
4439 * @return End of the output range.
4440 * @ingroup setoperations
4442 * This operation iterates over both ranges, copying elements present in
4443 * each range in order to the output range. Iterators increment for each
4444 * range. When the current element of one range is less than the other
4445 * according to @a comp, that element is copied and the iterator advanced.
4446 * If an equivalent element according to @a comp is contained in both
4447 * ranges, the element from the first range is copied and both ranges
4448 * advance. The output range may not overlap either input range.
4450 template<typename _InputIterator1
, typename _InputIterator2
,
4451 typename _OutputIterator
, typename _Compare
>
4453 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4454 _InputIterator2 __first2
, _InputIterator2 __last2
,
4455 _OutputIterator __result
, _Compare __comp
)
4457 typedef typename iterator_traits
<_InputIterator1
>::value_type
4459 typedef typename iterator_traits
<_InputIterator2
>::value_type
4462 // concept requirements
4463 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4464 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4465 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4467 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4469 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4470 _ValueType1
, _ValueType2
>)
4471 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4472 _ValueType2
, _ValueType1
>)
4473 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4474 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4476 while (__first1
!= __last1
&& __first2
!= __last2
)
4478 if (__comp(*__first1
, *__first2
))
4480 *__result
= *__first1
;
4483 else if (__comp(*__first2
, *__first1
))
4485 *__result
= *__first2
;
4490 *__result
= *__first1
;
4496 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4501 * @brief Return the intersection of two sorted ranges.
4502 * @param first1 Start of first range.
4503 * @param last1 End of first range.
4504 * @param first2 Start of second range.
4505 * @param last2 End of second range.
4506 * @return End of the output range.
4507 * @ingroup setoperations
4509 * This operation iterates over both ranges, copying elements present in
4510 * both ranges in order to the output range. Iterators increment for each
4511 * range. When the current element of one range is less than the other,
4512 * that iterator advances. If an element is contained in both ranges, the
4513 * element from the first range is copied and both ranges advance. The
4514 * output range may not overlap either input range.
4516 template<typename _InputIterator1
, typename _InputIterator2
,
4517 typename _OutputIterator
>
4519 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
4520 _InputIterator2 __first2
, _InputIterator2 __last2
,
4521 _OutputIterator __result
)
4523 typedef typename iterator_traits
<_InputIterator1
>::value_type
4525 typedef typename iterator_traits
<_InputIterator2
>::value_type
4528 // concept requirements
4529 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4530 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4531 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4533 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
4534 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4535 __glibcxx_requires_sorted(__first1
, __last1
);
4536 __glibcxx_requires_sorted(__first2
, __last2
);
4538 while (__first1
!= __last1
&& __first2
!= __last2
)
4539 if (*__first1
< *__first2
)
4541 else if (*__first2
< *__first1
)
4545 *__result
= *__first1
;
4554 * @brief Return the intersection of two sorted ranges using comparison
4556 * @param first1 Start of first range.
4557 * @param last1 End of first range.
4558 * @param first2 Start of second range.
4559 * @param last2 End of second range.
4560 * @param comp The comparison functor.
4561 * @return End of the output range.
4562 * @ingroup setoperations
4564 * This operation iterates over both ranges, copying elements present in
4565 * both ranges in order to the output range. Iterators increment for each
4566 * range. When the current element of one range is less than the other
4567 * according to @a comp, that iterator advances. If an element is
4568 * contained in both ranges according to @a comp, the element from the
4569 * first range is copied and both ranges advance. The output range may not
4570 * overlap either input range.
4572 template<typename _InputIterator1
, typename _InputIterator2
,
4573 typename _OutputIterator
, typename _Compare
>
4575 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
4576 _InputIterator2 __first2
, _InputIterator2 __last2
,
4577 _OutputIterator __result
, _Compare __comp
)
4579 typedef typename iterator_traits
<_InputIterator1
>::value_type
4581 typedef typename iterator_traits
<_InputIterator2
>::value_type
4584 // concept requirements
4585 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4586 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4587 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4589 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4590 _ValueType1
, _ValueType2
>)
4591 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4592 _ValueType2
, _ValueType1
>)
4593 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4594 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4596 while (__first1
!= __last1
&& __first2
!= __last2
)
4597 if (__comp(*__first1
, *__first2
))
4599 else if (__comp(*__first2
, *__first1
))
4603 *__result
= *__first1
;
4612 * @brief Return the difference of two sorted ranges.
4613 * @param first1 Start of first range.
4614 * @param last1 End of first range.
4615 * @param first2 Start of second range.
4616 * @param last2 End of second range.
4617 * @return End of the output range.
4618 * @ingroup setoperations
4620 * This operation iterates over both ranges, copying elements present in
4621 * the first range but not the second in order to the output range.
4622 * Iterators increment for each range. When the current element of the
4623 * first range is less than the second, that element is copied and the
4624 * iterator advances. If the current element of the second range is less,
4625 * the iterator advances, but no element is copied. If an element is
4626 * contained in both ranges, no elements are copied and both ranges
4627 * advance. The output range may not overlap either input range.
4629 template<typename _InputIterator1
, typename _InputIterator2
,
4630 typename _OutputIterator
>
4632 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4633 _InputIterator2 __first2
, _InputIterator2 __last2
,
4634 _OutputIterator __result
)
4636 typedef typename iterator_traits
<_InputIterator1
>::value_type
4638 typedef typename iterator_traits
<_InputIterator2
>::value_type
4641 // concept requirements
4642 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4643 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4644 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4646 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
4647 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4648 __glibcxx_requires_sorted(__first1
, __last1
);
4649 __glibcxx_requires_sorted(__first2
, __last2
);
4651 while (__first1
!= __last1
&& __first2
!= __last2
)
4652 if (*__first1
< *__first2
)
4654 *__result
= *__first1
;
4658 else if (*__first2
< *__first1
)
4665 return std::copy(__first1
, __last1
, __result
);
4669 * @brief Return the difference of two sorted ranges using comparison
4671 * @param first1 Start of first range.
4672 * @param last1 End of first range.
4673 * @param first2 Start of second range.
4674 * @param last2 End of second range.
4675 * @param comp The comparison functor.
4676 * @return End of the output range.
4677 * @ingroup setoperations
4679 * This operation iterates over both ranges, copying elements present in
4680 * the first range but not the second in order to the output range.
4681 * Iterators increment for each range. When the current element of the
4682 * first range is less than the second according to @a comp, that element
4683 * is copied and the iterator advances. If the current element of the
4684 * second range is less, no element is copied and the iterator advances.
4685 * If an element is contained in both ranges according to @a comp, no
4686 * elements are copied and both ranges advance. The output range may not
4687 * overlap either input range.
4689 template<typename _InputIterator1
, typename _InputIterator2
,
4690 typename _OutputIterator
, typename _Compare
>
4692 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4693 _InputIterator2 __first2
, _InputIterator2 __last2
,
4694 _OutputIterator __result
, _Compare __comp
)
4696 typedef typename iterator_traits
<_InputIterator1
>::value_type
4698 typedef typename iterator_traits
<_InputIterator2
>::value_type
4701 // concept requirements
4702 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4703 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4704 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4706 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4707 _ValueType1
, _ValueType2
>)
4708 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4709 _ValueType2
, _ValueType1
>)
4710 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4711 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4713 while (__first1
!= __last1
&& __first2
!= __last2
)
4714 if (__comp(*__first1
, *__first2
))
4716 *__result
= *__first1
;
4720 else if (__comp(*__first2
, *__first1
))
4727 return std::copy(__first1
, __last1
, __result
);
4731 * @brief Return the symmetric difference of two sorted ranges.
4732 * @param first1 Start of first range.
4733 * @param last1 End of first range.
4734 * @param first2 Start of second range.
4735 * @param last2 End of second range.
4736 * @return End of the output range.
4737 * @ingroup setoperations
4739 * This operation iterates over both ranges, copying elements present in
4740 * one range but not the other in order to the output range. Iterators
4741 * increment for each range. When the current element of one range is less
4742 * than the other, that element is copied and the iterator advances. If an
4743 * element is contained in both ranges, no elements are copied and both
4744 * ranges advance. The output range may not overlap either input range.
4746 template<typename _InputIterator1
, typename _InputIterator2
,
4747 typename _OutputIterator
>
4749 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4750 _InputIterator2 __first2
, _InputIterator2 __last2
,
4751 _OutputIterator __result
)
4753 typedef typename iterator_traits
<_InputIterator1
>::value_type
4755 typedef typename iterator_traits
<_InputIterator2
>::value_type
4758 // concept requirements
4759 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4760 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4761 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4763 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4765 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
4766 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
4767 __glibcxx_requires_sorted(__first1
, __last1
);
4768 __glibcxx_requires_sorted(__first2
, __last2
);
4770 while (__first1
!= __last1
&& __first2
!= __last2
)
4771 if (*__first1
< *__first2
)
4773 *__result
= *__first1
;
4777 else if (*__first2
< *__first1
)
4779 *__result
= *__first2
;
4788 return std::copy(__first2
, __last2
, std::copy(__first1
,
4789 __last1
, __result
));
4793 * @brief Return the symmetric difference of two sorted ranges using
4794 * comparison functor.
4795 * @param first1 Start of first range.
4796 * @param last1 End of first range.
4797 * @param first2 Start of second range.
4798 * @param last2 End of second range.
4799 * @param comp The comparison functor.
4800 * @return End of the output range.
4801 * @ingroup setoperations
4803 * This operation iterates over both ranges, copying elements present in
4804 * one range but not the other in order to the output range. Iterators
4805 * increment for each range. When the current element of one range is less
4806 * than the other according to @a comp, that element is copied and the
4807 * iterator advances. If an element is contained in both ranges according
4808 * to @a comp, no elements are copied and both ranges advance. The output
4809 * range may not overlap either input range.
4811 template<typename _InputIterator1
, typename _InputIterator2
,
4812 typename _OutputIterator
, typename _Compare
>
4814 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4815 _InputIterator2 __first2
, _InputIterator2 __last2
,
4816 _OutputIterator __result
,
4819 typedef typename iterator_traits
<_InputIterator1
>::value_type
4821 typedef typename iterator_traits
<_InputIterator2
>::value_type
4824 // concept requirements
4825 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4826 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4827 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4829 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4831 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4832 _ValueType1
, _ValueType2
>)
4833 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4834 _ValueType2
, _ValueType1
>)
4835 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4836 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4838 while (__first1
!= __last1
&& __first2
!= __last2
)
4839 if (__comp(*__first1
, *__first2
))
4841 *__result
= *__first1
;
4845 else if (__comp(*__first2
, *__first1
))
4847 *__result
= *__first2
;
4856 return std::copy(__first2
, __last2
, std::copy(__first1
,
4857 __last1
, __result
));
4860 // min_element and max_element, with and without an explicitly supplied
4861 // comparison function.
4864 * @brief Return the maximum element in a range.
4865 * @param first Start of range.
4866 * @param last End of range.
4867 * @return Iterator referencing the first instance of the largest value.
4869 template<typename _ForwardIterator
>
4871 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
4873 // concept requirements
4874 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4875 __glibcxx_function_requires(_LessThanComparableConcept
<
4876 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4877 __glibcxx_requires_valid_range(__first
, __last
);
4879 if (__first
== __last
)
4881 _ForwardIterator __result
= __first
;
4882 while (++__first
!= __last
)
4883 if (*__result
< *__first
)
4889 * @brief Return the maximum element in a range using comparison functor.
4890 * @param first Start of range.
4891 * @param last End of range.
4892 * @param comp Comparison functor.
4893 * @return Iterator referencing the first instance of the largest value
4894 * according to comp.
4896 template<typename _ForwardIterator
, typename _Compare
>
4898 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
4901 // concept requirements
4902 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4903 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4904 typename iterator_traits
<_ForwardIterator
>::value_type
,
4905 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4906 __glibcxx_requires_valid_range(__first
, __last
);
4908 if (__first
== __last
) return __first
;
4909 _ForwardIterator __result
= __first
;
4910 while (++__first
!= __last
)
4911 if (__comp(*__result
, *__first
)) __result
= __first
;
4916 * @brief Return the minimum element in a range.
4917 * @param first Start of range.
4918 * @param last End of range.
4919 * @return Iterator referencing the first instance of the smallest value.
4921 template<typename _ForwardIterator
>
4923 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
4925 // concept requirements
4926 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4927 __glibcxx_function_requires(_LessThanComparableConcept
<
4928 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4929 __glibcxx_requires_valid_range(__first
, __last
);
4931 if (__first
== __last
)
4933 _ForwardIterator __result
= __first
;
4934 while (++__first
!= __last
)
4935 if (*__first
< *__result
)
4941 * @brief Return the minimum element in a range using comparison functor.
4942 * @param first Start of range.
4943 * @param last End of range.
4944 * @param comp Comparison functor.
4945 * @return Iterator referencing the first instance of the smallest value
4946 * according to comp.
4948 template<typename _ForwardIterator
, typename _Compare
>
4950 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
4953 // concept requirements
4954 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4955 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4956 typename iterator_traits
<_ForwardIterator
>::value_type
,
4957 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4958 __glibcxx_requires_valid_range(__first
, __last
);
4960 if (__first
== __last
)
4962 _ForwardIterator __result
= __first
;
4963 while (++__first
!= __last
)
4964 if (__comp(*__first
, *__result
))
4969 // next_permutation and prev_permutation, with and without an explicitly
4970 // supplied comparison function.
4973 * @brief Permute range into the next "dictionary" ordering.
4974 * @param first Start of range.
4975 * @param last End of range.
4976 * @return False if wrapped to first permutation, true otherwise.
4978 * Treats all permutations of the range as a set of "dictionary" sorted
4979 * sequences. Permutes the current sequence into the next one of this set.
4980 * Returns true if there are more sequences to generate. If the sequence
4981 * is the largest of the set, the smallest is generated and false returned.
4983 template<typename _BidirectionalIterator
>
4985 next_permutation(_BidirectionalIterator __first
,
4986 _BidirectionalIterator __last
)
4988 // concept requirements
4989 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4990 _BidirectionalIterator
>)
4991 __glibcxx_function_requires(_LessThanComparableConcept
<
4992 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4993 __glibcxx_requires_valid_range(__first
, __last
);
4995 if (__first
== __last
)
4997 _BidirectionalIterator __i
= __first
;
5006 _BidirectionalIterator __ii
= __i
;
5010 _BidirectionalIterator __j
= __last
;
5011 while (!(*__i
< *--__j
))
5013 std::iter_swap(__i
, __j
);
5014 std::reverse(__ii
, __last
);
5019 std::reverse(__first
, __last
);
5026 * @brief Permute range into the next "dictionary" ordering using
5027 * comparison functor.
5028 * @param first Start of range.
5029 * @param last End of range.
5031 * @return False if wrapped to first permutation, true otherwise.
5033 * Treats all permutations of the range [first,last) as a set of
5034 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
5035 * sequence into the next one of this set. Returns true if there are more
5036 * sequences to generate. If the sequence is the largest of the set, the
5037 * smallest is generated and false returned.
5039 template<typename _BidirectionalIterator
, typename _Compare
>
5041 next_permutation(_BidirectionalIterator __first
,
5042 _BidirectionalIterator __last
, _Compare __comp
)
5044 // concept requirements
5045 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5046 _BidirectionalIterator
>)
5047 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5048 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
5049 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
5050 __glibcxx_requires_valid_range(__first
, __last
);
5052 if (__first
== __last
)
5054 _BidirectionalIterator __i
= __first
;
5063 _BidirectionalIterator __ii
= __i
;
5065 if (__comp(*__i
, *__ii
))
5067 _BidirectionalIterator __j
= __last
;
5068 while (!__comp(*__i
, *--__j
))
5070 std::iter_swap(__i
, __j
);
5071 std::reverse(__ii
, __last
);
5076 std::reverse(__first
, __last
);
5083 * @brief Permute range into the previous "dictionary" ordering.
5084 * @param first Start of range.
5085 * @param last End of range.
5086 * @return False if wrapped to last permutation, true otherwise.
5088 * Treats all permutations of the range as a set of "dictionary" sorted
5089 * sequences. Permutes the current sequence into the previous one of this
5090 * set. Returns true if there are more sequences to generate. If the
5091 * sequence is the smallest of the set, the largest is generated and false
5094 template<typename _BidirectionalIterator
>
5096 prev_permutation(_BidirectionalIterator __first
,
5097 _BidirectionalIterator __last
)
5099 // concept requirements
5100 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5101 _BidirectionalIterator
>)
5102 __glibcxx_function_requires(_LessThanComparableConcept
<
5103 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
5104 __glibcxx_requires_valid_range(__first
, __last
);
5106 if (__first
== __last
)
5108 _BidirectionalIterator __i
= __first
;
5117 _BidirectionalIterator __ii
= __i
;
5121 _BidirectionalIterator __j
= __last
;
5122 while (!(*--__j
< *__i
))
5124 std::iter_swap(__i
, __j
);
5125 std::reverse(__ii
, __last
);
5130 std::reverse(__first
, __last
);
5137 * @brief Permute range into the previous "dictionary" ordering using
5138 * comparison functor.
5139 * @param first Start of range.
5140 * @param last End of range.
5142 * @return False if wrapped to last permutation, true otherwise.
5144 * Treats all permutations of the range [first,last) as a set of
5145 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
5146 * sequence into the previous one of this set. Returns true if there are
5147 * more sequences to generate. If the sequence is the smallest of the set,
5148 * the largest is generated and false returned.
5150 template<typename _BidirectionalIterator
, typename _Compare
>
5152 prev_permutation(_BidirectionalIterator __first
,
5153 _BidirectionalIterator __last
, _Compare __comp
)
5155 // concept requirements
5156 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5157 _BidirectionalIterator
>)
5158 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
5159 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
5160 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
5161 __glibcxx_requires_valid_range(__first
, __last
);
5163 if (__first
== __last
)
5165 _BidirectionalIterator __i
= __first
;
5174 _BidirectionalIterator __ii
= __i
;
5176 if (__comp(*__ii
, *__i
))
5178 _BidirectionalIterator __j
= __last
;
5179 while (!__comp(*--__j
, *__i
))
5181 std::iter_swap(__i
, __j
);
5182 std::reverse(__ii
, __last
);
5187 std::reverse(__first
, __last
);
5193 // find_first_of, with and without an explicitly supplied comparison function.
5196 * @brief Find element from a set in a sequence.
5197 * @param first1 Start of range to search.
5198 * @param last1 End of range to search.
5199 * @param first2 Start of match candidates.
5200 * @param last2 End of match candidates.
5201 * @return The first iterator @c i in the range
5202 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
5203 * interator in [first2,last2), or @p last1 if no such iterator exists.
5205 * Searches the range @p [first1,last1) for an element that is equal to
5206 * some element in the range [first2,last2). If found, returns an iterator
5207 * in the range [first1,last1), otherwise returns @p last1.
5209 template<typename _InputIterator
, typename _ForwardIterator
>
5211 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
5212 _ForwardIterator __first2
, _ForwardIterator __last2
)
5214 // concept requirements
5215 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
5216 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5217 __glibcxx_function_requires(_EqualOpConcept
<
5218 typename iterator_traits
<_InputIterator
>::value_type
,
5219 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5220 __glibcxx_requires_valid_range(__first1
, __last1
);
5221 __glibcxx_requires_valid_range(__first2
, __last2
);
5223 for ( ; __first1
!= __last1
; ++__first1
)
5224 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
5225 if (*__first1
== *__iter
)
5231 * @brief Find element from a set in a sequence using a predicate.
5232 * @param first1 Start of range to search.
5233 * @param last1 End of range to search.
5234 * @param first2 Start of match candidates.
5235 * @param last2 End of match candidates.
5236 * @param comp Predicate to use.
5237 * @return The first iterator @c i in the range
5238 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
5239 * interator in [first2,last2), or @p last1 if no such iterator exists.
5241 * Searches the range @p [first1,last1) for an element that is equal to
5242 * some element in the range [first2,last2). If found, returns an iterator in
5243 * the range [first1,last1), otherwise returns @p last1.
5245 template<typename _InputIterator
, typename _ForwardIterator
,
5246 typename _BinaryPredicate
>
5248 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
5249 _ForwardIterator __first2
, _ForwardIterator __last2
,
5250 _BinaryPredicate __comp
)
5252 // concept requirements
5253 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
5254 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
5255 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
5256 typename iterator_traits
<_InputIterator
>::value_type
,
5257 typename iterator_traits
<_ForwardIterator
>::value_type
>)
5258 __glibcxx_requires_valid_range(__first1
, __last1
);
5259 __glibcxx_requires_valid_range(__first2
, __last2
);
5261 for ( ; __first1
!= __last1
; ++__first1
)
5262 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
5263 if (__comp(*__first1
, *__iter
))
5269 // find_end, with and without an explicitly supplied comparison function.
5270 // Search [first2, last2) as a subsequence in [first1, last1), and return
5271 // the *last* possible match. Note that find_end for bidirectional iterators
5272 // is much faster than for forward iterators.
5274 // find_end for forward iterators.
5275 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
5277 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
5278 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
5279 forward_iterator_tag
, forward_iterator_tag
)
5281 if (__first2
== __last2
)
5285 _ForwardIterator1 __result
= __last1
;
5288 _ForwardIterator1 __new_result
5289 = std::search(__first1
, __last1
, __first2
, __last2
);
5290 if (__new_result
== __last1
)
5294 __result
= __new_result
;
5295 __first1
= __new_result
;
5302 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
5303 typename _BinaryPredicate
>
5305 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
5306 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
5307 forward_iterator_tag
, forward_iterator_tag
,
5308 _BinaryPredicate __comp
)
5310 if (__first2
== __last2
)
5314 _ForwardIterator1 __result
= __last1
;
5317 _ForwardIterator1 __new_result
5318 = std::search(__first1
, __last1
, __first2
, __last2
, __comp
);
5319 if (__new_result
== __last1
)
5323 __result
= __new_result
;
5324 __first1
= __new_result
;
5331 // find_end for bidirectional iterators. Requires partial specialization.
5332 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
5333 _BidirectionalIterator1
5334 __find_end(_BidirectionalIterator1 __first1
,
5335 _BidirectionalIterator1 __last1
,
5336 _BidirectionalIterator2 __first2
,
5337 _BidirectionalIterator2 __last2
,
5338 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
5340 // concept requirements
5341 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5342 _BidirectionalIterator1
>)
5343 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5344 _BidirectionalIterator2
>)
5346 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
5347 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
5349 _RevIterator1
__rlast1(__first1
);
5350 _RevIterator2
__rlast2(__first2
);
5351 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
5352 _RevIterator2(__last2
), __rlast2
);
5354 if (__rresult
== __rlast1
)
5358 _BidirectionalIterator1 __result
= __rresult
.base();
5359 std::advance(__result
, -std::distance(__first2
, __last2
));
5364 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
5365 typename _BinaryPredicate
>
5366 _BidirectionalIterator1
5367 __find_end(_BidirectionalIterator1 __first1
,
5368 _BidirectionalIterator1 __last1
,
5369 _BidirectionalIterator2 __first2
,
5370 _BidirectionalIterator2 __last2
,
5371 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
5372 _BinaryPredicate __comp
)
5374 // concept requirements
5375 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5376 _BidirectionalIterator1
>)
5377 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5378 _BidirectionalIterator2
>)
5380 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
5381 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
5383 _RevIterator1
__rlast1(__first1
);
5384 _RevIterator2
__rlast2(__first2
);
5385 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
5386 _RevIterator2(__last2
), __rlast2
,
5389 if (__rresult
== __rlast1
)
5393 _BidirectionalIterator1 __result
= __rresult
.base();
5394 std::advance(__result
, -std::distance(__first2
, __last2
));
5399 // Dispatching functions for find_end.
5402 * @brief Find last matching subsequence in a sequence.
5403 * @param first1 Start of range to search.
5404 * @param last1 End of range to search.
5405 * @param first2 Start of sequence to match.
5406 * @param last2 End of sequence to match.
5407 * @return The last iterator @c i in the range
5408 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
5409 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
5410 * such iterator exists.
5412 * Searches the range @p [first1,last1) for a sub-sequence that compares
5413 * equal value-by-value with the sequence given by @p [first2,last2) and
5414 * returns an iterator to the first element of the sub-sequence, or
5415 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
5416 * last such subsequence contained in [first,last1).
5418 * Because the sub-sequence must lie completely within the range
5419 * @p [first1,last1) it must start at a position less than
5420 * @p last1-(last2-first2) where @p last2-first2 is the length of the
5422 * This means that the returned iterator @c i will be in the range
5423 * @p [first1,last1-(last2-first2))
5425 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
5426 inline _ForwardIterator1
5427 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
5428 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
5430 // concept requirements
5431 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
5432 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
5433 __glibcxx_function_requires(_EqualOpConcept
<
5434 typename iterator_traits
<_ForwardIterator1
>::value_type
,
5435 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
5436 __glibcxx_requires_valid_range(__first1
, __last1
);
5437 __glibcxx_requires_valid_range(__first2
, __last2
);
5439 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
5440 std::__iterator_category(__first1
),
5441 std::__iterator_category(__first2
));
5445 * @brief Find last matching subsequence in a sequence using a predicate.
5446 * @param first1 Start of range to search.
5447 * @param last1 End of range to search.
5448 * @param first2 Start of sequence to match.
5449 * @param last2 End of sequence to match.
5450 * @param comp The predicate to use.
5451 * @return The last iterator @c i in the range
5452 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
5453 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
5454 * @p last1 if no such iterator exists.
5456 * Searches the range @p [first1,last1) for a sub-sequence that compares
5457 * equal value-by-value with the sequence given by @p [first2,last2) using
5458 * comp as a predicate and returns an iterator to the first element of the
5459 * sub-sequence, or @p last1 if the sub-sequence is not found. The
5460 * sub-sequence will be the last such subsequence contained in
5463 * Because the sub-sequence must lie completely within the range
5464 * @p [first1,last1) it must start at a position less than
5465 * @p last1-(last2-first2) where @p last2-first2 is the length of the
5467 * This means that the returned iterator @c i will be in the range
5468 * @p [first1,last1-(last2-first2))
5470 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
5471 typename _BinaryPredicate
>
5472 inline _ForwardIterator1
5473 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
5474 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
5475 _BinaryPredicate __comp
)
5477 // concept requirements
5478 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
5479 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
5480 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
5481 typename iterator_traits
<_ForwardIterator1
>::value_type
,
5482 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
5483 __glibcxx_requires_valid_range(__first1
, __last1
);
5484 __glibcxx_requires_valid_range(__first2
, __last2
);
5486 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
5487 std::__iterator_category(__first1
),
5488 std::__iterator_category(__first2
),
5492 _GLIBCXX_END_NAMESPACE
5494 #endif /* _ALGO_H */