1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
64 #include <bits/stl_heap.h>
65 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
67 // See concept_check.h for the __glibcxx_*_requires macros.
73 * @brief Find the median of three values.
77 * @return One of @p a, @p b or @p c.
79 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
80 * then the value returned will be @c m.
81 * This is an SGI extension.
82 * @ingroup SGIextensions
84 template<typename _Tp
>
86 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
)
88 // concept requirements
89 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
106 * @brief Find the median of three values using a predicate for comparison.
110 * @param comp A binary predicate.
111 * @return One of @p a, @p b or @p c.
113 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
114 * and @p comp(m,n) are both true then the value returned will be @c m.
115 * This is an SGI extension.
116 * @ingroup SGIextensions
118 template<typename _Tp
, typename _Compare
>
120 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
, _Compare __comp
)
122 // concept requirements
123 __glibcxx_function_requires(_BinaryFunctionConcept
<_Compare
,bool,_Tp
,_Tp
>)
124 if (__comp(__a
, __b
))
125 if (__comp(__b
, __c
))
127 else if (__comp(__a
, __c
))
131 else if (__comp(__a
, __c
))
133 else if (__comp(__b
, __c
))
140 * @brief Apply a function to every element of a sequence.
141 * @param first An input iterator.
142 * @param last An input iterator.
143 * @param f A unary function object.
146 * Applies the function object @p f to each element in the range
147 * @p [first,last). @p f must not modify the order of the sequence.
148 * If @p f has a return value it is ignored.
150 template<typename _InputIterator
, typename _Function
>
152 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
154 // concept requirements
155 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
156 for ( ; __first
!= __last
; ++__first
)
163 * This is an overload used by find() for the Input Iterator case.
166 template<typename _InputIterator
, typename _Tp
>
167 inline _InputIterator
168 find(_InputIterator __first
, _InputIterator __last
,
172 while (__first
!= __last
&& !(*__first
== __val
))
179 * This is an overload used by find_if() for the Input Iterator case.
182 template<typename _InputIterator
, typename _Predicate
>
183 inline _InputIterator
184 find_if(_InputIterator __first
, _InputIterator __last
,
188 while (__first
!= __last
&& !__pred(*__first
))
195 * This is an overload used by find() for the RAI case.
198 template<typename _RandomAccessIterator
, typename _Tp
>
199 _RandomAccessIterator
200 find(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
202 random_access_iterator_tag
)
204 typename iterator_traits
<_RandomAccessIterator
>::difference_type __trip_count
205 = (__last
- __first
) >> 2;
207 for ( ; __trip_count
> 0 ; --__trip_count
) {
208 if (*__first
== __val
) return __first
;
211 if (*__first
== __val
) return __first
;
214 if (*__first
== __val
) return __first
;
217 if (*__first
== __val
) return __first
;
221 switch(__last
- __first
) {
223 if (*__first
== __val
) return __first
;
226 if (*__first
== __val
) return __first
;
229 if (*__first
== __val
) return __first
;
239 * This is an overload used by find_if() for the RAI case.
242 template<typename _RandomAccessIterator
, typename _Predicate
>
243 _RandomAccessIterator
244 find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
246 random_access_iterator_tag
)
248 typename iterator_traits
<_RandomAccessIterator
>::difference_type __trip_count
249 = (__last
- __first
) >> 2;
251 for ( ; __trip_count
> 0 ; --__trip_count
) {
252 if (__pred(*__first
)) return __first
;
255 if (__pred(*__first
)) return __first
;
258 if (__pred(*__first
)) return __first
;
261 if (__pred(*__first
)) return __first
;
265 switch(__last
- __first
) {
267 if (__pred(*__first
)) return __first
;
270 if (__pred(*__first
)) return __first
;
273 if (__pred(*__first
)) return __first
;
282 * @brief Find the first occurrence of a value in a sequence.
283 * @param first An input iterator.
284 * @param last An input iterator.
285 * @param val The value to find.
286 * @return The first iterator @c i in the range @p [first,last)
287 * such that @c *i == @p val, or @p last if no such iterator exists.
289 template<typename _InputIterator
, typename _Tp
>
290 inline _InputIterator
291 find(_InputIterator __first
, _InputIterator __last
,
294 // concept requirements
295 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
296 __glibcxx_function_requires(_EqualOpConcept
<
297 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
298 return std::find(__first
, __last
, __val
, std::__iterator_category(__first
));
302 * @brief Find the first element in a sequence for which a predicate is true.
303 * @param first An input iterator.
304 * @param last An input iterator.
305 * @param pred A predicate.
306 * @return The first iterator @c i in the range @p [first,last)
307 * such that @p pred(*i) is true, or @p last if no such iterator exists.
309 template<typename _InputIterator
, typename _Predicate
>
310 inline _InputIterator
311 find_if(_InputIterator __first
, _InputIterator __last
,
314 // concept requirements
315 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
316 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
317 typename iterator_traits
<_InputIterator
>::value_type
>)
318 return std::find_if(__first
, __last
, __pred
, std::__iterator_category(__first
));
322 * @brief Find two adjacent values in a sequence that are equal.
323 * @param first A forward iterator.
324 * @param last A forward iterator.
325 * @return The first iterator @c i such that @c i and @c i+1 are both
326 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
327 * or @p last if no such iterator exists.
329 template<typename _ForwardIterator
>
331 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
333 // concept requirements
334 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
335 __glibcxx_function_requires(_EqualityComparableConcept
<
336 typename iterator_traits
<_ForwardIterator
>::value_type
>)
337 if (__first
== __last
)
339 _ForwardIterator __next
= __first
;
340 while(++__next
!= __last
) {
341 if (*__first
== *__next
)
349 * @brief Find two adjacent values in a sequence using a predicate.
350 * @param first A forward iterator.
351 * @param last A forward iterator.
352 * @param binary_pred A binary predicate.
353 * @return The first iterator @c i such that @c i and @c i+1 are both
354 * valid iterators in @p [first,last) and such that
355 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
358 template<typename _ForwardIterator
, typename _BinaryPredicate
>
360 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
361 _BinaryPredicate __binary_pred
)
363 // concept requirements
364 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
365 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
366 typename iterator_traits
<_ForwardIterator
>::value_type
,
367 typename iterator_traits
<_ForwardIterator
>::value_type
>)
368 if (__first
== __last
)
370 _ForwardIterator __next
= __first
;
371 while(++__next
!= __last
) {
372 if (__binary_pred(*__first
, *__next
))
380 * @brief Count the number of copies of a value in a sequence.
381 * @param first An input iterator.
382 * @param last An input iterator.
383 * @param value The value to be counted.
384 * @return The number of iterators @c i in the range @p [first,last)
385 * for which @c *i == @p value
387 template<typename _InputIterator
, typename _Tp
>
388 typename iterator_traits
<_InputIterator
>::difference_type
389 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
391 // concept requirements
392 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
393 __glibcxx_function_requires(_EqualityComparableConcept
<
394 typename iterator_traits
<_InputIterator
>::value_type
>)
395 __glibcxx_function_requires(_EqualityComparableConcept
<_Tp
>)
396 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
397 for ( ; __first
!= __last
; ++__first
)
398 if (*__first
== __value
)
404 * @brief Count the elements of a sequence for which a predicate is true.
405 * @param first An input iterator.
406 * @param last An input iterator.
407 * @param pred A predicate.
408 * @return The number of iterators @c i in the range @p [first,last)
409 * for which @p pred(*i) is true.
411 template<typename _InputIterator
, typename _Predicate
>
412 typename iterator_traits
<_InputIterator
>::difference_type
413 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
415 // concept requirements
416 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
417 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
418 typename iterator_traits
<_InputIterator
>::value_type
>)
419 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
420 for ( ; __first
!= __last
; ++__first
)
421 if (__pred(*__first
))
428 * @brief Search a sequence for a matching sub-sequence.
429 * @param first1 A forward iterator.
430 * @param last1 A forward iterator.
431 * @param first2 A forward iterator.
432 * @param last2 A forward iterator.
433 * @return The first iterator @c i in the range
434 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
435 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
436 * such iterator exists.
438 * Searches the range @p [first1,last1) for a sub-sequence that compares
439 * equal value-by-value with the sequence given by @p [first2,last2) and
440 * returns an iterator to the first element of the sub-sequence, or
441 * @p last1 if the sub-sequence is not found.
443 * Because the sub-sequence must lie completely within the range
444 * @p [first1,last1) it must start at a position less than
445 * @p last1-(last2-first2) where @p last2-first2 is the length of the
447 * This means that the returned iterator @c i will be in the range
448 * @p [first1,last1-(last2-first2))
450 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
452 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
453 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
455 // concept requirements
456 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
457 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
458 __glibcxx_function_requires(_EqualOpConcept
<
459 typename iterator_traits
<_ForwardIterator1
>::value_type
,
460 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
462 // Test for empty ranges
463 if (__first1
== __last1
|| __first2
== __last2
)
466 // Test for a pattern of length 1.
467 _ForwardIterator2
__tmp(__first2
);
469 if (__tmp
== __last2
)
470 return std::find(__first1
, __last1
, *__first2
);
474 _ForwardIterator2 __p1
, __p
;
476 __p1
= __first2
; ++__p1
;
478 _ForwardIterator1 __current
= __first1
;
480 while (__first1
!= __last1
) {
481 __first1
= std::find(__first1
, __last1
, *__first2
);
482 if (__first1
== __last1
)
486 __current
= __first1
;
487 if (++__current
== __last1
)
490 while (*__current
== *__p
) {
491 if (++__p
== __last2
)
493 if (++__current
== __last1
)
503 * @brief Search a sequence for a matching sub-sequence using a predicate.
504 * @param first1 A forward iterator.
505 * @param last1 A forward iterator.
506 * @param first2 A forward iterator.
507 * @param last2 A forward iterator.
508 * @param predicate A binary predicate.
509 * @return The first iterator @c i in the range
510 * @p [first1,last1-(last2-first2)) such that
511 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
512 * @p [0,last2-first2), or @p last1 if no such iterator exists.
514 * Searches the range @p [first1,last1) for a sub-sequence that compares
515 * equal value-by-value with the sequence given by @p [first2,last2),
516 * using @p predicate to determine equality, and returns an iterator
517 * to the first element of the sub-sequence, or @p last1 if no such
520 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
522 template<typename _ForwardIterator1
, typename _ForwardIterator2
, typename _BinaryPredicate
>
524 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
525 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
526 _BinaryPredicate __predicate
)
528 // concept requirements
529 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
530 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
531 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
532 typename iterator_traits
<_ForwardIterator1
>::value_type
,
533 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
535 // Test for empty ranges
536 if (__first1
== __last1
|| __first2
== __last2
)
539 // Test for a pattern of length 1.
540 _ForwardIterator2
__tmp(__first2
);
542 if (__tmp
== __last2
) {
543 while (__first1
!= __last1
&& !__predicate(*__first1
, *__first2
))
550 _ForwardIterator2 __p1
, __p
;
552 __p1
= __first2
; ++__p1
;
554 _ForwardIterator1 __current
= __first1
;
556 while (__first1
!= __last1
) {
557 while (__first1
!= __last1
) {
558 if (__predicate(*__first1
, *__first2
))
562 while (__first1
!= __last1
&& !__predicate(*__first1
, *__first2
))
564 if (__first1
== __last1
)
568 __current
= __first1
;
569 if (++__current
== __last1
) return __last1
;
571 while (__predicate(*__current
, *__p
)) {
572 if (++__p
== __last2
)
574 if (++__current
== __last1
)
584 * @brief Search a sequence for a number of consecutive values.
585 * @param first A forward iterator.
586 * @param last A forward iterator.
587 * @param count The number of consecutive values.
588 * @param val The value to find.
589 * @return The first iterator @c i in the range @p [first,last-count)
590 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
591 * or @p last if no such iterator exists.
593 * Searches the range @p [first,last) for @p count consecutive elements
596 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
598 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
599 _Integer __count
, const _Tp
& __val
)
601 // concept requirements
602 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
603 __glibcxx_function_requires(_EqualityComparableConcept
<
604 typename iterator_traits
<_ForwardIterator
>::value_type
>)
605 __glibcxx_function_requires(_EqualityComparableConcept
<_Tp
>)
610 __first
= std::find(__first
, __last
, __val
);
611 while (__first
!= __last
) {
612 _Integer __n
= __count
- 1;
613 _ForwardIterator __i
= __first
;
615 while (__i
!= __last
&& __n
!= 0 && *__i
== __val
) {
622 __first
= std::find(__i
, __last
, __val
);
629 * @brief Search a sequence for a number of consecutive values using a
631 * @param first A forward iterator.
632 * @param last A forward iterator.
633 * @param count The number of consecutive values.
634 * @param val The value to find.
635 * @param binary_pred A binary predicate.
636 * @return The first iterator @c i in the range @p [first,last-count)
637 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
638 * range @p [0,count), or @p last if no such iterator exists.
640 * Searches the range @p [first,last) for @p count consecutive elements
641 * for which the predicate returns true.
643 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
644 typename _BinaryPredicate
>
646 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
647 _Integer __count
, const _Tp
& __val
,
648 _BinaryPredicate __binary_pred
)
650 // concept requirements
651 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
652 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
653 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
658 while (__first
!= __last
) {
659 if (__binary_pred(*__first
, __val
))
663 while (__first
!= __last
) {
664 _Integer __n
= __count
- 1;
665 _ForwardIterator __i
= __first
;
667 while (__i
!= __last
&& __n
!= 0 && __binary_pred(*__i
, __val
)) {
674 while (__i
!= __last
) {
675 if (__binary_pred(*__i
, __val
))
687 * @brief Swap the elements of two sequences.
688 * @param first1 A forward iterator.
689 * @param last1 A forward iterator.
690 * @param first2 A forward iterator.
691 * @return An iterator equal to @p first2+(last1-first1).
693 * Swaps each element in the range @p [first1,last1) with the
694 * corresponding element in the range @p [first2,(last1-first1)).
695 * The ranges must not overlap.
697 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
699 swap_ranges(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
700 _ForwardIterator2 __first2
)
702 // concept requirements
703 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<_ForwardIterator1
>)
704 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<_ForwardIterator2
>)
705 __glibcxx_function_requires(_ConvertibleConcept
<
706 typename iterator_traits
<_ForwardIterator1
>::value_type
,
707 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
708 __glibcxx_function_requires(_ConvertibleConcept
<
709 typename iterator_traits
<_ForwardIterator2
>::value_type
,
710 typename iterator_traits
<_ForwardIterator1
>::value_type
>)
712 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
)
713 std::iter_swap(__first1
, __first2
);
718 * @brief Perform an operation on a sequence.
719 * @param first An input iterator.
720 * @param last An input iterator.
721 * @param result An output iterator.
722 * @param unary_op A unary operator.
723 * @return An output iterator equal to @p result+(last-first).
725 * Applies the operator to each element in the input range and assigns
726 * the results to successive elements of the output sequence.
727 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
728 * range @p [0,last-first).
730 * @p unary_op must not alter its argument.
732 template<typename _InputIterator
, typename _OutputIterator
, typename _UnaryOperation
>
734 transform(_InputIterator __first
, _InputIterator __last
,
735 _OutputIterator __result
, _UnaryOperation __unary_op
)
737 // concept requirements
738 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
739 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
740 // "the type returned by a _UnaryOperation"
741 __typeof__(__unary_op(*__first
))>)
743 for ( ; __first
!= __last
; ++__first
, ++__result
)
744 *__result
= __unary_op(*__first
);
749 * @brief Perform an operation on corresponding elements of two sequences.
750 * @param first1 An input iterator.
751 * @param last1 An input iterator.
752 * @param first2 An input iterator.
753 * @param result An output iterator.
754 * @param binary_op A binary operator.
755 * @return An output iterator equal to @p result+(last-first).
757 * Applies the operator to the corresponding elements in the two
758 * input ranges and assigns the results to successive elements of the
760 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
761 * @c N in the range @p [0,last1-first1).
763 * @p binary_op must not alter either of its arguments.
765 template<typename _InputIterator1
, typename _InputIterator2
, typename _OutputIterator
,
766 typename _BinaryOperation
>
768 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
769 _InputIterator2 __first2
, _OutputIterator __result
,
770 _BinaryOperation __binary_op
)
772 // concept requirements
773 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
774 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
775 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
776 // "the type returned by a _BinaryOperation"
777 __typeof__(__binary_op(*__first1
,*__first2
))>)
779 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
780 *__result
= __binary_op(*__first1
, *__first2
);
785 * @brief Replace each occurrence of one value in a sequence with another
787 * @param first A forward iterator.
788 * @param last A forward iterator.
789 * @param old_value The value to be replaced.
790 * @param new_value The replacement value.
791 * @return replace() returns no value.
793 * For each iterator @c i in the range @p [first,last) if @c *i ==
794 * @p old_value then the assignment @c *i = @p new_value is performed.
796 template<typename _ForwardIterator
, typename _Tp
>
798 replace(_ForwardIterator __first
, _ForwardIterator __last
,
799 const _Tp
& __old_value
, const _Tp
& __new_value
)
801 // concept requirements
802 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<_ForwardIterator
>)
803 __glibcxx_function_requires(_EqualOpConcept
<
804 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
805 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
806 typename iterator_traits
<_ForwardIterator
>::value_type
>)
808 for ( ; __first
!= __last
; ++__first
)
809 if (*__first
== __old_value
)
810 *__first
= __new_value
;
814 * @brief Replace each value in a sequence for which a predicate returns
815 * true with another value.
816 * @param first A forward iterator.
817 * @param last A forward iterator.
818 * @param pred A predicate.
819 * @param new_value The replacement value.
820 * @return replace_if() returns no value.
822 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
823 * is true then the assignment @c *i = @p new_value is performed.
825 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
827 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
828 _Predicate __pred
, const _Tp
& __new_value
)
830 // concept requirements
831 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<_ForwardIterator
>)
832 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
833 typename iterator_traits
<_ForwardIterator
>::value_type
>)
834 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
835 typename iterator_traits
<_ForwardIterator
>::value_type
>)
837 for ( ; __first
!= __last
; ++__first
)
838 if (__pred(*__first
))
839 *__first
= __new_value
;
843 * @brief Copy a sequence, replacing each element of one value with another
845 * @param first An input iterator.
846 * @param last An input iterator.
847 * @param result An output iterator.
848 * @param old_value The value to be replaced.
849 * @param new_value The replacement value.
850 * @return The end of the output sequence, @p result+(last-first).
852 * Copies each element in the input range @p [first,last) to the
853 * output range @p [result,result+(last-first)) replacing elements
854 * equal to @p old_value with @p new_value.
856 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
858 replace_copy(_InputIterator __first
, _InputIterator __last
,
859 _OutputIterator __result
,
860 const _Tp
& __old_value
, const _Tp
& __new_value
)
862 // concept requirements
863 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
864 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
865 typename iterator_traits
<_InputIterator
>::value_type
>)
866 __glibcxx_function_requires(_EqualOpConcept
<
867 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
869 for ( ; __first
!= __last
; ++__first
, ++__result
)
870 *__result
= *__first
== __old_value
? __new_value
: *__first
;
875 * @brief Copy a sequence, replacing each value for which a predicate
876 * returns true with another value.
877 * @param first An input iterator.
878 * @param last An input iterator.
879 * @param result An output iterator.
880 * @param pred A predicate.
881 * @param new_value The replacement value.
882 * @return The end of the output sequence, @p result+(last-first).
884 * Copies each element in the range @p [first,last) to the range
885 * @p [result,result+(last-first)) replacing elements for which
886 * @p pred returns true with @p new_value.
888 template<typename _InputIterator
, typename _OutputIterator
, typename _Predicate
,
891 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
892 _OutputIterator __result
,
893 _Predicate __pred
, const _Tp
& __new_value
)
895 // concept requirements
896 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
897 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
898 typename iterator_traits
<_InputIterator
>::value_type
>)
899 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
900 typename iterator_traits
<_InputIterator
>::value_type
>)
902 for ( ; __first
!= __last
; ++__first
, ++__result
)
903 *__result
= __pred(*__first
) ? __new_value
: *__first
;
908 * @brief Assign the result of a function object to each value in a
910 * @param first A forward iterator.
911 * @param last A forward iterator.
912 * @param gen A function object taking no arguments.
913 * @return generate() returns no value.
915 * Performs the assignment @c *i = @p gen() for each @c i in the range
918 template<typename _ForwardIterator
, typename _Generator
>
920 generate(_ForwardIterator __first
, _ForwardIterator __last
, _Generator __gen
)
922 // concept requirements
923 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
924 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
925 typename iterator_traits
<_ForwardIterator
>::value_type
>)
927 for ( ; __first
!= __last
; ++__first
)
932 * @brief Assign the result of a function object to each value in a
934 * @param first A forward iterator.
935 * @param n The length of the sequence.
936 * @param gen A function object taking no arguments.
937 * @return The end of the sequence, @p first+n
939 * Performs the assignment @c *i = @p gen() for each @c i in the range
940 * @p [first,first+n).
942 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
944 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
946 // concept requirements
947 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
948 // "the type returned by a _Generator"
949 __typeof__(__gen())>)
951 for ( ; __n
> 0; --__n
, ++__first
)
957 * @brief Copy a sequence, removing elements of a given value.
958 * @param first An input iterator.
959 * @param last An input iterator.
960 * @param result An output iterator.
961 * @param value The value to be removed.
962 * @return An iterator designating the end of the resulting sequence.
964 * Copies each element in the range @p [first,last) not equal to @p value
965 * to the range beginning at @p result.
966 * remove_copy() is stable, so the relative order of elements that are
967 * copied is unchanged.
969 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
971 remove_copy(_InputIterator __first
, _InputIterator __last
,
972 _OutputIterator __result
, const _Tp
& __value
)
974 // concept requirements
975 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
976 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
977 typename iterator_traits
<_InputIterator
>::value_type
>)
978 __glibcxx_function_requires(_EqualOpConcept
<
979 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
981 for ( ; __first
!= __last
; ++__first
)
982 if (!(*__first
== __value
)) {
983 *__result
= *__first
;
990 * @brief Copy a sequence, removing elements for which a predicate is true.
991 * @param first An input iterator.
992 * @param last An input iterator.
993 * @param result An output iterator.
994 * @param pred A predicate.
995 * @return An iterator designating the end of the resulting sequence.
997 * Copies each element in the range @p [first,last) for which
998 * @p pred returns true to the range beginning at @p result.
1000 * remove_copy_if() is stable, so the relative order of elements that are
1001 * copied is unchanged.
1003 template<typename _InputIterator
, typename _OutputIterator
, typename _Predicate
>
1005 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
1006 _OutputIterator __result
, _Predicate __pred
)
1008 // concept requirements
1009 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1010 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1011 typename iterator_traits
<_InputIterator
>::value_type
>)
1012 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1013 typename iterator_traits
<_InputIterator
>::value_type
>)
1015 for ( ; __first
!= __last
; ++__first
)
1016 if (!__pred(*__first
)) {
1017 *__result
= *__first
;
1024 * @brief Remove elements from a sequence.
1025 * @param first An input iterator.
1026 * @param last An input iterator.
1027 * @param value The value to be removed.
1028 * @return An iterator designating the end of the resulting sequence.
1030 * All elements equal to @p value are removed from the range
1033 * remove() is stable, so the relative order of elements that are
1034 * not removed is unchanged.
1036 * Elements between the end of the resulting sequence and @p last
1037 * are still present, but their value is unspecified.
1039 template<typename _ForwardIterator
, typename _Tp
>
1041 remove(_ForwardIterator __first
, _ForwardIterator __last
,
1044 // concept requirements
1045 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<_ForwardIterator
>)
1046 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
1047 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1048 __glibcxx_function_requires(_EqualOpConcept
<
1049 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1051 __first
= std::find(__first
, __last
, __value
);
1052 _ForwardIterator __i
= __first
;
1053 return __first
== __last
? __first
1054 : std::remove_copy(++__i
, __last
, __first
, __value
);
1058 * @brief Remove elements from a sequence using a predicate.
1059 * @param first A forward iterator.
1060 * @param last A forward iterator.
1061 * @param pred A predicate.
1062 * @return An iterator designating the end of the resulting sequence.
1064 * All elements for which @p pred returns true are removed from the range
1067 * remove_if() is stable, so the relative order of elements that are
1068 * not removed is unchanged.
1070 * Elements between the end of the resulting sequence and @p last
1071 * are still present, but their value is unspecified.
1073 template<typename _ForwardIterator
, typename _Predicate
>
1075 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1078 // concept requirements
1079 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<_ForwardIterator
>)
1080 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1081 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1083 __first
= std::find_if(__first
, __last
, __pred
);
1084 _ForwardIterator __i
= __first
;
1085 return __first
== __last
? __first
1086 : std::remove_copy_if(++__i
, __last
, __first
, __pred
);
1091 * This is an uglified unique_copy(_InputIterator, _InputIterator, _OutputIterator)
1092 * overloaded for output iterators.
1095 template<typename _InputIterator
, typename _OutputIterator
>
1097 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1098 _OutputIterator __result
,
1099 output_iterator_tag
)
1101 // concept requirements -- taken care of in dispatching function
1102 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1103 *__result
= __value
;
1104 while (++__first
!= __last
)
1105 if (!(__value
== *__first
)) {
1107 *++__result
= __value
;
1114 * This is an uglified unique_copy(_InputIterator, _InputIterator, _OutputIterator)
1115 * overloaded for forward iterators.
1118 template<typename _InputIterator
, typename _ForwardIterator
>
1120 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1121 _ForwardIterator __result
,
1122 forward_iterator_tag
)
1124 // concept requirements -- taken care of in dispatching function
1125 *__result
= *__first
;
1126 while (++__first
!= __last
)
1127 if (!(*__result
== *__first
))
1128 *++__result
= *__first
;
1134 * This is an uglified
1135 * unique_copy(_InputIterator, _InputIterator, _OutputIterator, _BinaryPredicate)
1136 * overloaded for output iterators.
1139 template<typename _InputIterator
, typename _OutputIterator
, typename _BinaryPredicate
>
1141 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1142 _OutputIterator __result
,
1143 _BinaryPredicate __binary_pred
,
1144 output_iterator_tag
)
1146 // concept requirements -- iterators already checked
1147 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1148 typename iterator_traits
<_InputIterator
>::value_type
,
1149 typename iterator_traits
<_InputIterator
>::value_type
>)
1151 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1152 *__result
= __value
;
1153 while (++__first
!= __last
)
1154 if (!__binary_pred(__value
, *__first
)) {
1156 *++__result
= __value
;
1163 * This is an uglified
1164 * unique_copy(_InputIterator, _InputIterator, _OutputIterator, _BinaryPredicate)
1165 * overloaded for forward iterators.
1168 template<typename _InputIterator
, typename _ForwardIterator
, typename _BinaryPredicate
>
1170 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1171 _ForwardIterator __result
,
1172 _BinaryPredicate __binary_pred
,
1173 forward_iterator_tag
)
1175 // concept requirements -- iterators already checked
1176 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1177 typename iterator_traits
<_ForwardIterator
>::value_type
,
1178 typename iterator_traits
<_InputIterator
>::value_type
>)
1180 *__result
= *__first
;
1181 while (++__first
!= __last
)
1182 if (!__binary_pred(*__result
, *__first
)) *++__result
= *__first
;
1187 * @brief Copy a sequence, removing consecutive duplicate values.
1188 * @param first An input iterator.
1189 * @param last An input iterator.
1190 * @param result An output iterator.
1191 * @return An iterator designating the end of the resulting sequence.
1193 * Copies each element in the range @p [first,last) to the range
1194 * beginning at @p result, except that only the first element is copied
1195 * from groups of consecutive elements that compare equal.
1196 * unique_copy() is stable, so the relative order of elements that are
1197 * copied is unchanged.
1199 template<typename _InputIterator
, typename _OutputIterator
>
1200 inline _OutputIterator
1201 unique_copy(_InputIterator __first
, _InputIterator __last
,
1202 _OutputIterator __result
)
1204 // concept requirements
1205 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1206 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1207 typename iterator_traits
<_InputIterator
>::value_type
>)
1208 __glibcxx_function_requires(_EqualityComparableConcept
<
1209 typename iterator_traits
<_InputIterator
>::value_type
>)
1211 typedef typename iterator_traits
<_OutputIterator
>::iterator_category _IterType
;
1213 if (__first
== __last
) return __result
;
1214 return std::__unique_copy(__first
, __last
, __result
, _IterType());
1218 * @brief Copy a sequence, removing consecutive values using a predicate.
1219 * @param first An input iterator.
1220 * @param last An input iterator.
1221 * @param result An output iterator.
1222 * @param binary_pred A binary predicate.
1223 * @return An iterator designating the end of the resulting sequence.
1225 * Copies each element in the range @p [first,last) to the range
1226 * beginning at @p result, except that only the first element is copied
1227 * from groups of consecutive elements for which @p binary_pred returns
1229 * unique_copy() is stable, so the relative order of elements that are
1230 * copied is unchanged.
1232 template<typename _InputIterator
, typename _OutputIterator
, typename _BinaryPredicate
>
1233 inline _OutputIterator
1234 unique_copy(_InputIterator __first
, _InputIterator __last
,
1235 _OutputIterator __result
,
1236 _BinaryPredicate __binary_pred
)
1238 // concept requirements -- predicates checked later
1239 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1240 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1241 typename iterator_traits
<_InputIterator
>::value_type
>)
1243 typedef typename iterator_traits
<_OutputIterator
>::iterator_category _IterType
;
1245 if (__first
== __last
) return __result
;
1246 return std::__unique_copy(__first
, __last
, __result
, __binary_pred
, _IterType());
1250 * @brief Remove consecutive duplicate values from a sequence.
1251 * @param first A forward iterator.
1252 * @param last A forward iterator.
1253 * @return An iterator designating the end of the resulting sequence.
1255 * Removes all but the first element from each group of consecutive
1256 * values that compare equal.
1257 * unique() is stable, so the relative order of elements that are
1258 * not removed is unchanged.
1259 * Elements between the end of the resulting sequence and @p last
1260 * are still present, but their value is unspecified.
1262 template<typename _ForwardIterator
>
1264 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1266 // concept requirements
1267 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<_ForwardIterator
>)
1268 __glibcxx_function_requires(_EqualityComparableConcept
<
1269 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1271 __first
= std::adjacent_find(__first
, __last
);
1272 return std::unique_copy(__first
, __last
, __first
);
1276 * @brief Remove consecutive values from a sequence using a predicate.
1277 * @param first A forward iterator.
1278 * @param last A forward iterator.
1279 * @param binary_pred A binary predicate.
1280 * @return An iterator designating the end of the resulting sequence.
1282 * Removes all but the first element from each group of consecutive
1283 * values for which @p binary_pred returns true.
1284 * unique() is stable, so the relative order of elements that are
1285 * not removed is unchanged.
1286 * Elements between the end of the resulting sequence and @p last
1287 * are still present, but their value is unspecified.
1289 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1291 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1292 _BinaryPredicate __binary_pred
)
1294 // concept requirements
1295 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<_ForwardIterator
>)
1296 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1297 typename iterator_traits
<_ForwardIterator
>::value_type
,
1298 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1300 __first
= std::adjacent_find(__first
, __last
, __binary_pred
);
1301 return std::unique_copy(__first
, __last
, __first
, __binary_pred
);
1306 * This is an uglified reverse(_BidirectionalIterator, _BidirectionalIterator)
1307 * overloaded for bidirectional iterators.
1310 template<typename _BidirectionalIterator
>
1312 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1313 bidirectional_iterator_tag
)
1316 if (__first
== __last
|| __first
== --__last
)
1319 std::iter_swap(__first
++, __last
);
1324 * This is an uglified reverse(_BidirectionalIterator, _BidirectionalIterator)
1325 * overloaded for bidirectional iterators.
1328 template<typename _RandomAccessIterator
>
1330 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1331 random_access_iterator_tag
)
1333 while (__first
< __last
)
1334 std::iter_swap(__first
++, --__last
);
1338 * @brief Reverse a sequence.
1339 * @param first A bidirectional iterator.
1340 * @param last A bidirectional iterator.
1341 * @return reverse() returns no value.
1343 * Reverses the order of the elements in the range @p [first,last),
1344 * so that the first element becomes the last etc.
1345 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1346 * swaps @p *(first+i) and @p *(last-(i+1))
1348 template<typename _BidirectionalIterator
>
1350 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1352 // concept requirements
1353 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1354 _BidirectionalIterator
>)
1355 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1359 * @brief Copy a sequence, reversing its elements.
1360 * @param first A bidirectional iterator.
1361 * @param last A bidirectional iterator.
1362 * @param result An output iterator.
1363 * @return An iterator designating the end of the resulting sequence.
1365 * Copies the elements in the range @p [first,last) to the range
1366 * @p [result,result+(last-first)) such that the order of the
1367 * elements is reversed.
1368 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1369 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1370 * The ranges @p [first,last) and @p [result,result+(last-first))
1373 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1375 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1376 _OutputIterator __result
)
1378 // concept requirements
1379 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BidirectionalIterator
>)
1380 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1381 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1383 while (__first
!= __last
) {
1385 *__result
= *__last
;
1394 * This is a helper function for the rotate algorithm specialized on RAIs.
1395 * It returns the greatest common divisor of two integer values.
1398 template<typename _EuclideanRingElement
>
1399 _EuclideanRingElement
1400 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1403 _EuclideanRingElement __t
= __m
% __n
;
1412 * This is a helper function for the rotate algorithm.
1415 template<typename _ForwardIterator
>
1417 __rotate(_ForwardIterator __first
,
1418 _ForwardIterator __middle
,
1419 _ForwardIterator __last
,
1420 forward_iterator_tag
)
1422 if ((__first
== __middle
) || (__last
== __middle
))
1425 _ForwardIterator __first2
= __middle
;
1427 swap(*__first
++, *__first2
++);
1428 if (__first
== __middle
)
1429 __middle
= __first2
;
1430 } while (__first2
!= __last
);
1432 __first2
= __middle
;
1434 while (__first2
!= __last
) {
1435 swap(*__first
++, *__first2
++);
1436 if (__first
== __middle
)
1437 __middle
= __first2
;
1438 else if (__first2
== __last
)
1439 __first2
= __middle
;
1445 * This is a helper function for the rotate algorithm.
1448 template<typename _BidirectionalIterator
>
1450 __rotate(_BidirectionalIterator __first
,
1451 _BidirectionalIterator __middle
,
1452 _BidirectionalIterator __last
,
1453 bidirectional_iterator_tag
)
1455 // concept requirements
1456 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1457 _BidirectionalIterator
>)
1459 if ((__first
== __middle
) || (__last
== __middle
))
1462 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1463 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1465 while (__first
!= __middle
&& __middle
!= __last
)
1466 swap(*__first
++, *--__last
);
1468 if (__first
== __middle
) {
1469 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1472 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1478 * This is a helper function for the rotate algorithm.
1481 template<typename _RandomAccessIterator
>
1483 __rotate(_RandomAccessIterator __first
,
1484 _RandomAccessIterator __middle
,
1485 _RandomAccessIterator __last
,
1486 random_access_iterator_tag
)
1488 // concept requirements
1489 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1490 _RandomAccessIterator
>)
1492 if ((__first
== __middle
) || (__last
== __middle
))
1495 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type _Distance
;
1496 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type _ValueType
;
1498 _Distance __n
= __last
- __first
;
1499 _Distance __k
= __middle
- __first
;
1500 _Distance __l
= __n
- __k
;
1503 std::swap_ranges(__first
, __middle
, __middle
);
1507 _Distance __d
= __gcd(__n
, __k
);
1509 for (_Distance __i
= 0; __i
< __d
; __i
++) {
1510 _ValueType __tmp
= *__first
;
1511 _RandomAccessIterator __p
= __first
;
1514 for (_Distance __j
= 0; __j
< __l
/__d
; __j
++) {
1515 if (__p
> __first
+ __l
) {
1516 *__p
= *(__p
- __l
);
1520 *__p
= *(__p
+ __k
);
1526 for (_Distance __j
= 0; __j
< __k
/__d
- 1; __j
++) {
1527 if (__p
< __last
- __k
) {
1528 *__p
= *(__p
+ __k
);
1532 *__p
= * (__p
- __l
);
1543 * @brief Rotate the elements of a sequence.
1544 * @param first A forward iterator.
1545 * @param middle A forward iterator.
1546 * @param last A forward iterator.
1549 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1550 * positions so that the element at @p middle is moved to @p first, the
1551 * element at @p middle+1 is moved to @first+1 and so on for each element
1552 * in the range @p [first,last).
1554 * This effectively swaps the ranges @p [first,middle) and
1557 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1558 * each @p n in the range @p [0,last-first).
1560 template<typename _ForwardIterator
>
1562 rotate(_ForwardIterator __first
, _ForwardIterator __middle
, _ForwardIterator __last
)
1564 // concept requirements
1565 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<_ForwardIterator
>)
1567 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category _IterType
;
1568 std::__rotate(__first
, __middle
, __last
, _IterType());
1572 * @brief Copy a sequence, rotating its elements.
1573 * @param first A forward iterator.
1574 * @param middle A forward iterator.
1575 * @param last A forward iterator.
1576 * @param result An output iterator.
1577 * @return An iterator designating the end of the resulting sequence.
1579 * Copies the elements of the range @p [first,last) to the range
1580 * beginning at @result, rotating the copied elements by @p (middle-first)
1581 * positions so that the element at @p middle is moved to @p result, the
1582 * element at @p middle+1 is moved to @result+1 and so on for each element
1583 * in the range @p [first,last).
1585 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1586 * each @p n in the range @p [0,last-first).
1588 template<typename _ForwardIterator
, typename _OutputIterator
>
1590 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1591 _ForwardIterator __last
, _OutputIterator __result
)
1593 // concept requirements
1594 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1595 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1596 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1598 return std::copy(__first
, __middle
, copy(__middle
, __last
, __result
));
1604 * Return a random number in the range [0, __n). This function encapsulates
1605 * whether we're using rand (part of the standard C library) or lrand48
1606 * (not standard, but a much better choice whenever it's available).
1608 * XXX There is no corresponding encapsulation fn to seed the generator.
1611 template<typename _Distance
>
1613 __random_number(_Distance __n
)
1615 #ifdef _GLIBCXX_HAVE_DRAND48
1616 return lrand48() % __n
;
1618 return rand() % __n
;
1624 * @brief Randomly shuffle the elements of a sequence.
1625 * @param first A forward iterator.
1626 * @param last A forward iterator.
1629 * Reorder the elements in the range @p [first,last) using a random
1630 * distribution, so that every possible ordering of the sequence is
1633 template<typename _RandomAccessIterator
>
1635 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
1637 // concept requirements
1638 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1639 _RandomAccessIterator
>)
1641 if (__first
== __last
) return;
1642 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1643 std::iter_swap(__i
, __first
+ std::__random_number((__i
- __first
) + 1));
1647 * @brief Shuffle the elements of a sequence using a random number
1649 * @param first A forward iterator.
1650 * @param last A forward iterator.
1651 * @param rand The RNG functor or function.
1654 * Reorders the elements in the range @p [first,last) using @p rand to
1655 * provide a random distribution. Calling @p rand(N) for a positive
1656 * integer @p N should return a randomly chosen integer from the
1659 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
1661 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1662 _RandomNumberGenerator
& __rand
)
1664 // concept requirements
1665 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1666 _RandomAccessIterator
>)
1668 if (__first
== __last
) return;
1669 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1670 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
1676 * This is a helper function...
1679 template<typename _ForwardIterator
, typename _Predicate
>
1681 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1683 forward_iterator_tag
)
1685 if (__first
== __last
) return __first
;
1687 while (__pred(*__first
))
1688 if (++__first
== __last
) return __first
;
1690 _ForwardIterator __next
= __first
;
1692 while (++__next
!= __last
)
1693 if (__pred(*__next
)) {
1694 swap(*__first
, *__next
);
1703 * This is a helper function...
1706 template<typename _BidirectionalIterator
, typename _Predicate
>
1707 _BidirectionalIterator
1708 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1710 bidirectional_iterator_tag
)
1714 if (__first
== __last
)
1716 else if (__pred(*__first
))
1722 if (__first
== __last
)
1724 else if (!__pred(*__last
))
1728 std::iter_swap(__first
, __last
);
1734 * @brief Move elements for which a predicate is true to the beginning
1736 * @param first A forward iterator.
1737 * @param last A forward iterator.
1738 * @param pred A predicate functor.
1739 * @return An iterator @p middle such that @p pred(i) is true for each
1740 * iterator @p i in the range @p [first,middle) and false for each @p i
1741 * in the range @p [middle,last).
1743 * @p pred must not modify its operand. @p partition() does not preserve
1744 * the relative ordering of elements in each group, use
1745 * @p stable_partition() if this is needed.
1747 template<typename _ForwardIterator
, typename _Predicate
>
1748 inline _ForwardIterator
1749 partition(_ForwardIterator __first
, _ForwardIterator __last
,
1752 // concept requirements
1753 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<_ForwardIterator
>)
1754 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1755 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1757 return std::__partition(__first
, __last
, __pred
, std::__iterator_category(__first
));
1763 * This is a helper function...
1766 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1768 __inplace_stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1769 _Predicate __pred
, _Distance __len
)
1772 return __pred(*__first
) ? __last
: __first
;
1773 _ForwardIterator __middle
= __first
;
1774 std::advance(__middle
, __len
/ 2);
1775 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
, __middle
,
1778 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1781 std::rotate(__begin
, __middle
, __end
);
1782 std::advance(__begin
, std::distance(__middle
, __end
));
1788 * This is a helper function...
1791 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1794 __stable_partition_adaptive(_ForwardIterator __first
, _ForwardIterator __last
,
1795 _Predicate __pred
, _Distance __len
,
1797 _Distance __buffer_size
)
1799 if (__len
<= __buffer_size
) {
1800 _ForwardIterator __result1
= __first
;
1801 _Pointer __result2
= __buffer
;
1802 for ( ; __first
!= __last
; ++__first
)
1803 if (__pred(*__first
)) {
1804 *__result1
= *__first
;
1808 *__result2
= *__first
;
1811 std::copy(__buffer
, __result2
, __result1
);
1815 _ForwardIterator __middle
= __first
;
1816 std::advance(__middle
, __len
/ 2);
1817 _ForwardIterator __begin
= std::__stable_partition_adaptive(__first
, __middle
,
1820 __buffer
, __buffer_size
);
1821 _ForwardIterator __end
= std::__stable_partition_adaptive( __middle
, __last
,
1824 __buffer
, __buffer_size
);
1825 std::rotate(__begin
, __middle
, __end
);
1826 std::advance(__begin
, std::distance(__middle
, __end
));
1832 * @brief Move elements for which a predicate is true to the beginning
1833 * of a sequence, preserving relative ordering.
1834 * @param first A forward iterator.
1835 * @param last A forward iterator.
1836 * @param pred A predicate functor.
1837 * @return An iterator @p middle such that @p pred(i) is true for each
1838 * iterator @p i in the range @p [first,middle) and false for each @p i
1839 * in the range @p [middle,last).
1841 * Performs the same function as @p partition() with the additional
1842 * guarantee that the relative ordering of elements in each group is
1843 * preserved, so any two elements @p x and @p y in the range
1844 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1845 * relative ordering after calling @p stable_partition().
1847 template<typename _ForwardIterator
, typename _Predicate
>
1849 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1852 // concept requirements
1853 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<_ForwardIterator
>)
1854 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1855 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1857 if (__first
== __last
)
1861 typedef typename iterator_traits
<_ForwardIterator
>::value_type _ValueType
;
1862 typedef typename iterator_traits
<_ForwardIterator
>::difference_type _DistanceType
;
1864 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
, __last
);
1865 if (__buf
.size() > 0)
1866 return std::__stable_partition_adaptive(__first
, __last
, __pred
,
1867 _DistanceType(__buf
.requested_size()),
1868 __buf
.begin(), __buf
.size());
1870 return std::__inplace_stable_partition(__first
, __last
, __pred
,
1871 _DistanceType(__buf
.requested_size()));
1877 * This is a helper function...
1880 template<typename _RandomAccessIterator
, typename _Tp
>
1881 _RandomAccessIterator
1882 __unguarded_partition(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1886 while (*__first
< __pivot
)
1889 while (__pivot
< *__last
)
1891 if (!(__first
< __last
))
1893 std::iter_swap(__first
, __last
);
1900 * This is a helper function...
1903 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
1904 _RandomAccessIterator
1905 __unguarded_partition(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1906 _Tp __pivot
, _Compare __comp
)
1909 while (__comp(*__first
, __pivot
))
1912 while (__comp(__pivot
, *__last
))
1914 if (!(__first
< __last
))
1916 std::iter_swap(__first
, __last
);
1925 * This controls some aspect of the sort routines.
1928 enum { _S_threshold
= 16 };
1932 * This is a helper function for the sort routine.
1935 template<typename _RandomAccessIterator
, typename _Tp
>
1937 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
1939 _RandomAccessIterator __next
= __last
;
1941 while (__val
< *__next
) {
1951 * This is a helper function for the sort routine.
1954 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
1956 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
, _Compare __comp
)
1958 _RandomAccessIterator __next
= __last
;
1960 while (__comp(__val
, *__next
)) {
1970 * This is a helper function for the sort routine.
1973 template<typename _RandomAccessIterator
>
1975 __insertion_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
1977 if (__first
== __last
) return;
1979 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1981 typename iterator_traits
<_RandomAccessIterator
>::value_type __val
= *__i
;
1982 if (__val
< *__first
) {
1983 std::copy_backward(__first
, __i
, __i
+ 1);
1987 std::__unguarded_linear_insert(__i
, __val
);
1993 * This is a helper function for the sort routine.
1996 template<typename _RandomAccessIterator
, typename _Compare
>
1998 __insertion_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2001 if (__first
== __last
) return;
2003 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2005 typename iterator_traits
<_RandomAccessIterator
>::value_type __val
= *__i
;
2006 if (__comp(__val
, *__first
)) {
2007 std::copy_backward(__first
, __i
, __i
+ 1);
2011 std::__unguarded_linear_insert(__i
, __val
, __comp
);
2017 * This is a helper function for the sort routine.
2020 template<typename _RandomAccessIterator
>
2022 __unguarded_insertion_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
2024 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type _ValueType
;
2026 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2027 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
2032 * This is a helper function for the sort routine.
2035 template<typename _RandomAccessIterator
, typename _Compare
>
2037 __unguarded_insertion_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2040 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type _ValueType
;
2042 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2043 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
2048 * This is a helper function for the sort routine.
2051 template<typename _RandomAccessIterator
>
2053 __final_insertion_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
2055 if (__last
- __first
> _S_threshold
) {
2056 std::__insertion_sort(__first
, __first
+ _S_threshold
);
2057 std::__unguarded_insertion_sort(__first
+ _S_threshold
, __last
);
2060 std::__insertion_sort(__first
, __last
);
2065 * This is a helper function for the sort routine.
2068 template<typename _RandomAccessIterator
, typename _Compare
>
2070 __final_insertion_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2073 if (__last
- __first
> _S_threshold
) {
2074 std::__insertion_sort(__first
, __first
+ _S_threshold
, __comp
);
2075 std::__unguarded_insertion_sort(__first
+ _S_threshold
, __last
, __comp
);
2078 std::__insertion_sort(__first
, __last
, __comp
);
2083 * This is a helper function for the sort routine.
2086 template<typename _Size
>
2091 for (__k
= 0; __n
!= 1; __n
>>= 1) ++__k
;
2096 * @brief Sort the smallest elements of a sequence.
2097 * @param first An iterator.
2098 * @param middle Another iterator.
2099 * @param last Another iterator.
2102 * Sorts the smallest @p (middle-first) elements in the range
2103 * @p [first,last) and moves them to the range @p [first,middle). The
2104 * order of the remaining elements in the range @p [middle,last) is
2106 * After the sort if @p i and @j are iterators in the range
2107 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2108 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
2110 template<typename _RandomAccessIterator
>
2112 partial_sort(_RandomAccessIterator __first
,
2113 _RandomAccessIterator __middle
,
2114 _RandomAccessIterator __last
)
2116 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type _ValueType
;
2118 // concept requirements
2119 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2120 _RandomAccessIterator
>)
2121 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2123 std::make_heap(__first
, __middle
);
2124 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
2125 if (*__i
< *__first
)
2126 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
));
2127 std::sort_heap(__first
, __middle
);
2131 * @brief Sort the smallest elements of a sequence using a predicate
2133 * @param first An iterator.
2134 * @param middle Another iterator.
2135 * @param last Another iterator.
2136 * @param comp A comparison functor.
2139 * Sorts the smallest @p (middle-first) elements in the range
2140 * @p [first,last) and moves them to the range @p [first,middle). The
2141 * order of the remaining elements in the range @p [middle,last) is
2143 * After the sort if @p i and @j are iterators in the range
2144 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2145 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
2148 template<typename _RandomAccessIterator
, typename _Compare
>
2150 partial_sort(_RandomAccessIterator __first
,
2151 _RandomAccessIterator __middle
,
2152 _RandomAccessIterator __last
,
2155 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type _ValueType
;
2157 // concept requirements
2158 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2159 _RandomAccessIterator
>)
2160 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2161 _ValueType
, _ValueType
>)
2163 std::make_heap(__first
, __middle
, __comp
);
2164 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
2165 if (__comp(*__i
, *__first
))
2166 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
), __comp
);
2167 std::sort_heap(__first
, __middle
, __comp
);
2171 * @brief Copy the smallest elements of a sequence.
2172 * @param first An iterator.
2173 * @param last Another iterator.
2174 * @param result_first A random-access iterator.
2175 * @param result_last Another random-access iterator.
2176 * @return An iterator indicating the end of the resulting sequence.
2178 * Copies and sorts the smallest N values from the range @p [first,last)
2179 * to the range beginning at @p result_first, where the number of
2180 * elements to be copied, @p N, is the smaller of @p (last-first) and
2181 * @p (result_last-result_first).
2182 * After the sort if @p i and @j are iterators in the range
2183 * @p [result_first,result_first+N) such that @i precedes @j then
2184 * @p *j<*i is false.
2185 * The value returned is @p result_first+N.
2187 template<typename _InputIterator
, typename _RandomAccessIterator
>
2188 _RandomAccessIterator
2189 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2190 _RandomAccessIterator __result_first
,
2191 _RandomAccessIterator __result_last
)
2193 typedef typename iterator_traits
<_InputIterator
>::value_type _InputValueType
;
2194 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type _OutputValueType
;
2195 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type _DistanceType
;
2197 // concept requirements
2198 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2199 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
, _OutputValueType
>)
2200 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
2201 __glibcxx_function_requires(_LessThanComparableConcept
<_InputValueType
>)
2203 if (__result_first
== __result_last
) return __result_last
;
2204 _RandomAccessIterator __result_real_last
= __result_first
;
2205 while(__first
!= __last
&& __result_real_last
!= __result_last
) {
2206 *__result_real_last
= *__first
;
2207 ++__result_real_last
;
2210 std::make_heap(__result_first
, __result_real_last
);
2211 while (__first
!= __last
) {
2212 if (*__first
< *__result_first
)
2213 std::__adjust_heap(__result_first
, _DistanceType(0),
2214 _DistanceType(__result_real_last
- __result_first
),
2215 _InputValueType(*__first
));
2218 std::sort_heap(__result_first
, __result_real_last
);
2219 return __result_real_last
;
2223 * @brief Copy the smallest elements of a sequence using a predicate for
2225 * @param first An input iterator.
2226 * @param last Another input iterator.
2227 * @param result_first A random-access iterator.
2228 * @param result_last Another random-access iterator.
2229 * @param comp A comparison functor.
2230 * @return An iterator indicating the end of the resulting sequence.
2232 * Copies and sorts the smallest N values from the range @p [first,last)
2233 * to the range beginning at @p result_first, where the number of
2234 * elements to be copied, @p N, is the smaller of @p (last-first) and
2235 * @p (result_last-result_first).
2236 * After the sort if @p i and @j are iterators in the range
2237 * @p [result_first,result_first+N) such that @i precedes @j then
2238 * @p comp(*j,*i) is false.
2239 * The value returned is @p result_first+N.
2241 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
2242 _RandomAccessIterator
2243 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2244 _RandomAccessIterator __result_first
,
2245 _RandomAccessIterator __result_last
,
2248 typedef typename iterator_traits
<_InputIterator
>::value_type _InputValueType
;
2249 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type _OutputValueType
;
2250 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type _DistanceType
;
2252 // concept requirements
2253 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2254 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<_RandomAccessIterator
>)
2255 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
, _OutputValueType
>)
2256 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2257 _OutputValueType
, _OutputValueType
>)
2259 if (__result_first
== __result_last
) return __result_last
;
2260 _RandomAccessIterator __result_real_last
= __result_first
;
2261 while(__first
!= __last
&& __result_real_last
!= __result_last
) {
2262 *__result_real_last
= *__first
;
2263 ++__result_real_last
;
2266 std::make_heap(__result_first
, __result_real_last
, __comp
);
2267 while (__first
!= __last
) {
2268 if (__comp(*__first
, *__result_first
))
2269 std::__adjust_heap(__result_first
, _DistanceType(0),
2270 _DistanceType(__result_real_last
- __result_first
),
2271 _InputValueType(*__first
),
2275 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2276 return __result_real_last
;
2281 * This is a helper function for the sort routine.
2284 template<typename _RandomAccessIterator
, typename _Size
>
2286 __introsort_loop(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2287 _Size __depth_limit
)
2289 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type _ValueType
;
2291 while (__last
- __first
> _S_threshold
) {
2292 if (__depth_limit
== 0) {
2293 std::partial_sort(__first
, __last
, __last
);
2297 _RandomAccessIterator __cut
=
2298 std::__unguarded_partition(__first
, __last
,
2299 _ValueType(std::__median(*__first
,
2300 *(__first
+ (__last
- __first
)/2),
2302 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2309 * This is a helper function for the sort routine.
2312 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2314 __introsort_loop(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2315 _Size __depth_limit
, _Compare __comp
)
2317 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type _ValueType
;
2319 while (__last
- __first
> _S_threshold
) {
2320 if (__depth_limit
== 0) {
2321 std::partial_sort(__first
, __last
, __last
, __comp
);
2325 _RandomAccessIterator __cut
=
2326 std::__unguarded_partition(__first
, __last
,
2327 _ValueType(std::__median(*__first
,
2328 *(__first
+ (__last
- __first
)/2),
2329 *(__last
- 1), __comp
)), __comp
);
2330 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2336 * @brief Sort the elements of a sequence.
2337 * @param first An iterator.
2338 * @param last Another iterator.
2341 * Sorts the elements in the range @p [first,last) in ascending order,
2342 * such that @p *(i+1)<*i is false for each iterator @p i in the range
2343 * @p [first,last-1).
2345 * The relative ordering of equivalent elements is not preserved, use
2346 * @p stable_sort() if this is needed.
2348 template<typename _RandomAccessIterator
>
2350 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
2352 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type _ValueType
;
2354 // concept requirements
2355 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2356 _RandomAccessIterator
>)
2357 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2359 if (__first
!= __last
) {
2360 std::__introsort_loop(__first
, __last
, __lg(__last
- __first
) * 2);
2361 std::__final_insertion_sort(__first
, __last
);
2366 * @brief Sort the elements of a sequence using a predicate for comparison.
2367 * @param first An iterator.
2368 * @param last Another iterator.
2369 * @param comp A comparison functor.
2372 * Sorts the elements in the range @p [first,last) in ascending order,
2373 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
2374 * range @p [first,last-1).
2376 * The relative ordering of equivalent elements is not preserved, use
2377 * @p stable_sort() if this is needed.
2379 template<typename _RandomAccessIterator
, typename _Compare
>
2381 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
, _Compare __comp
)
2383 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type _ValueType
;
2385 // concept requirements
2386 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2387 _RandomAccessIterator
>)
2388 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
, _ValueType
>)
2390 if (__first
!= __last
) {
2391 std::__introsort_loop(__first
, __last
, __lg(__last
- __first
) * 2, __comp
);
2392 std::__final_insertion_sort(__first
, __last
, __comp
);
2397 * @brief Finds the first position in which @a val could be inserted
2398 * without changing the ordering.
2399 * @param first An iterator.
2400 * @param last Another iterator.
2401 * @param val The search term.
2402 * @return An iterator pointing to the first element "not less than" @a val,
2403 * or end() if every element is less than @a val.
2404 * @ingroup binarysearch
2406 template<typename _ForwardIterator
, typename _Tp
>
2408 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __val
)
2410 typedef typename iterator_traits
<_ForwardIterator
>::value_type _ValueType
;
2411 typedef typename iterator_traits
<_ForwardIterator
>::difference_type _DistanceType
;
2413 // concept requirements
2414 // Note that these are slightly stricter than those of the 4-argument
2415 // version, defined next. The difference is in the strictness of the
2416 // comparison operations... so for looser checking, define your own
2417 // comparison function, as was intended.
2418 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2419 __glibcxx_function_requires(_SameTypeConcept
<_Tp
, _ValueType
>)
2420 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
2422 _DistanceType __len
= std::distance(__first
, __last
);
2423 _DistanceType __half
;
2424 _ForwardIterator __middle
;
2427 __half
= __len
>> 1;
2429 std::advance(__middle
, __half
);
2430 if (*__middle
< __val
) {
2433 __len
= __len
- __half
- 1;
2442 * @brief Finds the first position in which @a val could be inserted
2443 * without changing the ordering.
2444 * @param first An iterator.
2445 * @param last Another iterator.
2446 * @param val The search term.
2447 * @param comp A functor to use for comparisons.
2448 * @return An iterator pointing to the first element "not less than" @a val,
2449 * or end() if every element is less than @a val.
2450 * @ingroup binarysearch
2452 * The comparison function should have the same effects on ordering as
2453 * the function used for the initial sort.
2455 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2457 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2458 const _Tp
& __val
, _Compare __comp
)
2460 typedef typename iterator_traits
<_ForwardIterator
>::value_type _ValueType
;
2461 typedef typename iterator_traits
<_ForwardIterator
>::difference_type _DistanceType
;
2463 // concept requirements
2464 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2465 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
, _Tp
>)
2467 _DistanceType __len
= std::distance(__first
, __last
);
2468 _DistanceType __half
;
2469 _ForwardIterator __middle
;
2472 __half
= __len
>> 1;
2474 std::advance(__middle
, __half
);
2475 if (__comp(*__middle
, __val
)) {
2478 __len
= __len
- __half
- 1;
2487 * @brief Finds the last position in which @a val could be inserted
2488 * without changing the ordering.
2489 * @param first An iterator.
2490 * @param last Another iterator.
2491 * @param val The search term.
2492 * @return An iterator pointing to the first element greater than @a val,
2493 * or end() if no elements are greater than @a val.
2494 * @ingroup binarysearch
2496 template<typename _ForwardIterator
, typename _Tp
>
2498 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __val
)
2500 typedef typename iterator_traits
<_ForwardIterator
>::value_type _ValueType
;
2501 typedef typename iterator_traits
<_ForwardIterator
>::difference_type _DistanceType
;
2503 // concept requirements
2504 // See comments on lower_bound.
2505 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2506 __glibcxx_function_requires(_SameTypeConcept
<_Tp
, _ValueType
>)
2507 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
2509 _DistanceType __len
= std::distance(__first
, __last
);
2510 _DistanceType __half
;
2511 _ForwardIterator __middle
;
2514 __half
= __len
>> 1;
2516 std::advance(__middle
, __half
);
2517 if (__val
< *__middle
)
2522 __len
= __len
- __half
- 1;
2529 * @brief Finds the last position in which @a val could be inserted
2530 * without changing the ordering.
2531 * @param first An iterator.
2532 * @param last Another iterator.
2533 * @param val The search term.
2534 * @param comp A functor to use for comparisons.
2535 * @return An iterator pointing to the first element greater than @a val,
2536 * or end() if no elements are greater than @a val.
2537 * @ingroup binarysearch
2539 * The comparison function should have the same effects on ordering as
2540 * the function used for the initial sort.
2542 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2544 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2545 const _Tp
& __val
, _Compare __comp
)
2547 typedef typename iterator_traits
<_ForwardIterator
>::value_type _ValueType
;
2548 typedef typename iterator_traits
<_ForwardIterator
>::difference_type _DistanceType
;
2550 // concept requirements
2551 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2552 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _Tp
, _ValueType
>)
2554 _DistanceType __len
= std::distance(__first
, __last
);
2555 _DistanceType __half
;
2556 _ForwardIterator __middle
;
2559 __half
= __len
>> 1;
2561 std::advance(__middle
, __half
);
2562 if (__comp(__val
, *__middle
))
2567 __len
= __len
- __half
- 1;
2575 * This is a helper function for the merge routines.
2578 template<typename _BidirectionalIterator
, typename _Distance
>
2580 __merge_without_buffer(_BidirectionalIterator __first
,
2581 _BidirectionalIterator __middle
,
2582 _BidirectionalIterator __last
,
2583 _Distance __len1
, _Distance __len2
)
2585 if (__len1
== 0 || __len2
== 0)
2587 if (__len1
+ __len2
== 2) {
2588 if (*__middle
< *__first
)
2589 std::iter_swap(__first
, __middle
);
2592 _BidirectionalIterator __first_cut
= __first
;
2593 _BidirectionalIterator __second_cut
= __middle
;
2594 _Distance __len11
= 0;
2595 _Distance __len22
= 0;
2596 if (__len1
> __len2
) {
2597 __len11
= __len1
/ 2;
2598 std::advance(__first_cut
, __len11
);
2599 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
2600 __len22
= std::distance(__middle
, __second_cut
);
2603 __len22
= __len2
/ 2;
2604 std::advance(__second_cut
, __len22
);
2605 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
2606 __len11
= std::distance(__first
, __first_cut
);
2608 std::rotate(__first_cut
, __middle
, __second_cut
);
2609 _BidirectionalIterator __new_middle
= __first_cut
;
2610 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2611 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2613 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2614 __len1
- __len11
, __len2
- __len22
);
2619 * This is a helper function for the merge routines.
2622 template<typename _BidirectionalIterator
, typename _Distance
, typename _Compare
>
2624 __merge_without_buffer(_BidirectionalIterator __first
,
2625 _BidirectionalIterator __middle
,
2626 _BidirectionalIterator __last
,
2627 _Distance __len1
, _Distance __len2
,
2630 if (__len1
== 0 || __len2
== 0)
2632 if (__len1
+ __len2
== 2) {
2633 if (__comp(*__middle
, *__first
))
2634 std::iter_swap(__first
, __middle
);
2637 _BidirectionalIterator __first_cut
= __first
;
2638 _BidirectionalIterator __second_cut
= __middle
;
2639 _Distance __len11
= 0;
2640 _Distance __len22
= 0;
2641 if (__len1
> __len2
) {
2642 __len11
= __len1
/ 2;
2643 std::advance(__first_cut
, __len11
);
2644 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
, __comp
);
2645 __len22
= std::distance(__middle
, __second_cut
);
2648 __len22
= __len2
/ 2;
2649 std::advance(__second_cut
, __len22
);
2650 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
, __comp
);
2651 __len11
= std::distance(__first
, __first_cut
);
2653 std::rotate(__first_cut
, __middle
, __second_cut
);
2654 _BidirectionalIterator __new_middle
= __first_cut
;
2655 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2656 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2657 __len11
, __len22
, __comp
);
2658 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2659 __len1
- __len11
, __len2
- __len22
, __comp
);
2664 * This is a helper function for the stable sorting routines.
2667 template<typename _RandomAccessIterator
>
2669 __inplace_stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
2671 if (__last
- __first
< 15) {
2672 std::__insertion_sort(__first
, __last
);
2675 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2676 std::__inplace_stable_sort(__first
, __middle
);
2677 std::__inplace_stable_sort(__middle
, __last
);
2678 std::__merge_without_buffer(__first
, __middle
, __last
,
2685 * This is a helper function for the stable sorting routines.
2688 template<typename _RandomAccessIterator
, typename _Compare
>
2690 __inplace_stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2693 if (__last
- __first
< 15) {
2694 std::__insertion_sort(__first
, __last
, __comp
);
2697 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2698 std::__inplace_stable_sort(__first
, __middle
, __comp
);
2699 std::__inplace_stable_sort(__middle
, __last
, __comp
);
2700 std::__merge_without_buffer(__first
, __middle
, __last
,
2707 * @brief Merges two sorted ranges.
2708 * @param first1 An iterator.
2709 * @param first2 Another iterator.
2710 * @param last1 Another iterator.
2711 * @param last2 Another iterator.
2712 * @param result An iterator pointing to the end of the merged range.
2713 * @return An iterator pointing to the first element "not less than" @a val.
2715 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
2716 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
2717 * must be sorted, and the output range must not overlap with either of
2718 * the input ranges. The sort is @e stable, that is, for equivalent
2719 * elements in the two ranges, elements from the first range will always
2720 * come before elements from the second.
2722 template<typename _InputIterator1
, typename _InputIterator2
, typename _OutputIterator
>
2724 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
2725 _InputIterator2 __first2
, _InputIterator2 __last2
,
2726 _OutputIterator __result
)
2728 // concept requirements
2729 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2730 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2731 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
2732 typename iterator_traits
<_InputIterator1
>::value_type
>)
2733 __glibcxx_function_requires(_SameTypeConcept
<
2734 typename iterator_traits
<_InputIterator1
>::value_type
,
2735 typename iterator_traits
<_InputIterator2
>::value_type
>)
2736 __glibcxx_function_requires(_LessThanComparableConcept
<
2737 typename iterator_traits
<_InputIterator1
>::value_type
>)
2739 while (__first1
!= __last1
&& __first2
!= __last2
) {
2740 if (*__first2
< *__first1
) {
2741 *__result
= *__first2
;
2745 *__result
= *__first1
;
2750 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
, __result
));
2754 * @brief Merges two sorted ranges.
2755 * @param first1 An iterator.
2756 * @param first2 Another iterator.
2757 * @param last1 Another iterator.
2758 * @param last2 Another iterator.
2759 * @param result An iterator pointing to the end of the merged range.
2760 * @param comp A functor to use for comparisons.
2761 * @return An iterator pointing to the first element "not less than" @a val.
2763 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
2764 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
2765 * must be sorted, and the output range must not overlap with either of
2766 * the input ranges. The sort is @e stable, that is, for equivalent
2767 * elements in the two ranges, elements from the first range will always
2768 * come before elements from the second.
2770 * The comparison function should have the same effects on ordering as
2771 * the function used for the initial sort.
2773 template<typename _InputIterator1
, typename _InputIterator2
, typename _OutputIterator
,
2776 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
2777 _InputIterator2 __first2
, _InputIterator2 __last2
,
2778 _OutputIterator __result
, _Compare __comp
)
2780 // concept requirements
2781 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2782 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2783 __glibcxx_function_requires(_SameTypeConcept
<
2784 typename iterator_traits
<_InputIterator1
>::value_type
,
2785 typename iterator_traits
<_InputIterator2
>::value_type
>)
2786 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
2787 typename iterator_traits
<_InputIterator1
>::value_type
>)
2788 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2789 typename iterator_traits
<_InputIterator1
>::value_type
,
2790 typename iterator_traits
<_InputIterator2
>::value_type
>)
2792 while (__first1
!= __last1
&& __first2
!= __last2
) {
2793 if (__comp(*__first2
, *__first1
)) {
2794 *__result
= *__first2
;
2798 *__result
= *__first1
;
2803 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
, __result
));
2806 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
2809 __merge_sort_loop(_RandomAccessIterator1 __first
, _RandomAccessIterator1 __last
,
2810 _RandomAccessIterator2 __result
, _Distance __step_size
)
2812 _Distance __two_step
= 2 * __step_size
;
2814 while (__last
- __first
>= __two_step
) {
2815 __result
= std::merge(__first
, __first
+ __step_size
,
2816 __first
+ __step_size
, __first
+ __two_step
,
2818 __first
+= __two_step
;
2821 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
2822 std::merge(__first
, __first
+ __step_size
, __first
+ __step_size
, __last
,
2826 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
2827 typename _Distance
, typename _Compare
>
2829 __merge_sort_loop(_RandomAccessIterator1 __first
, _RandomAccessIterator1 __last
,
2830 _RandomAccessIterator2 __result
, _Distance __step_size
,
2833 _Distance __two_step
= 2 * __step_size
;
2835 while (__last
- __first
>= __two_step
) {
2836 __result
= std::merge(__first
, __first
+ __step_size
,
2837 __first
+ __step_size
, __first
+ __two_step
,
2840 __first
+= __two_step
;
2842 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
2844 std::merge(__first
, __first
+ __step_size
,
2845 __first
+ __step_size
, __last
,
2850 enum { _S_chunk_size
= 7 };
2852 template<typename _RandomAccessIterator
, typename _Distance
>
2854 __chunk_insertion_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2855 _Distance __chunk_size
)
2857 while (__last
- __first
>= __chunk_size
) {
2858 std::__insertion_sort(__first
, __first
+ __chunk_size
);
2859 __first
+= __chunk_size
;
2861 std::__insertion_sort(__first
, __last
);
2864 template<typename _RandomAccessIterator
, typename _Distance
, typename _Compare
>
2866 __chunk_insertion_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2867 _Distance __chunk_size
, _Compare __comp
)
2869 while (__last
- __first
>= __chunk_size
) {
2870 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
2871 __first
+= __chunk_size
;
2873 std::__insertion_sort(__first
, __last
, __comp
);
2876 template<typename _RandomAccessIterator
, typename _Pointer
>
2878 __merge_sort_with_buffer(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2881 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type _Distance
;
2883 _Distance __len
= __last
- __first
;
2884 _Pointer __buffer_last
= __buffer
+ __len
;
2886 _Distance __step_size
= _S_chunk_size
;
2887 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
2889 while (__step_size
< __len
) {
2890 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
2892 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
2897 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
2899 __merge_sort_with_buffer(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2900 _Pointer __buffer
, _Compare __comp
)
2902 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type _Distance
;
2904 _Distance __len
= __last
- __first
;
2905 _Pointer __buffer_last
= __buffer
+ __len
;
2907 _Distance __step_size
= _S_chunk_size
;
2908 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
2910 while (__step_size
< __len
) {
2911 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
, __comp
);
2913 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
, __comp
);
2920 * This is a helper function for the merge routines.
2923 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2924 typename _BidirectionalIterator3
>
2925 _BidirectionalIterator3
2926 __merge_backward(_BidirectionalIterator1 __first1
, _BidirectionalIterator1 __last1
,
2927 _BidirectionalIterator2 __first2
, _BidirectionalIterator2 __last2
,
2928 _BidirectionalIterator3 __result
)
2930 if (__first1
== __last1
)
2931 return std::copy_backward(__first2
, __last2
, __result
);
2932 if (__first2
== __last2
)
2933 return std::copy_backward(__first1
, __last1
, __result
);
2937 if (*__last2
< *__last1
) {
2938 *--__result
= *__last1
;
2939 if (__first1
== __last1
)
2940 return std::copy_backward(__first2
, ++__last2
, __result
);
2944 *--__result
= *__last2
;
2945 if (__first2
== __last2
)
2946 return std::copy_backward(__first1
, ++__last1
, __result
);
2954 * This is a helper function for the merge routines.
2957 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2958 typename _BidirectionalIterator3
, typename _Compare
>
2959 _BidirectionalIterator3
2960 __merge_backward(_BidirectionalIterator1 __first1
, _BidirectionalIterator1 __last1
,
2961 _BidirectionalIterator2 __first2
, _BidirectionalIterator2 __last2
,
2962 _BidirectionalIterator3 __result
,
2965 if (__first1
== __last1
)
2966 return std::copy_backward(__first2
, __last2
, __result
);
2967 if (__first2
== __last2
)
2968 return std::copy_backward(__first1
, __last1
, __result
);
2972 if (__comp(*__last2
, *__last1
)) {
2973 *--__result
= *__last1
;
2974 if (__first1
== __last1
)
2975 return std::copy_backward(__first2
, ++__last2
, __result
);
2979 *--__result
= *__last2
;
2980 if (__first2
== __last2
)
2981 return std::copy_backward(__first1
, ++__last1
, __result
);
2989 * This is a helper function for the merge routines.
2992 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
2994 _BidirectionalIterator1
2995 __rotate_adaptive(_BidirectionalIterator1 __first
,
2996 _BidirectionalIterator1 __middle
,
2997 _BidirectionalIterator1 __last
,
2998 _Distance __len1
, _Distance __len2
,
2999 _BidirectionalIterator2 __buffer
,
3000 _Distance __buffer_size
)
3002 _BidirectionalIterator2 __buffer_end
;
3003 if (__len1
> __len2
&& __len2
<= __buffer_size
) {
3004 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3005 std::copy_backward(__first
, __middle
, __last
);
3006 return std::copy(__buffer
, __buffer_end
, __first
);
3008 else if (__len1
<= __buffer_size
) {
3009 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3010 std::copy(__middle
, __last
, __first
);
3011 return std::copy_backward(__buffer
, __buffer_end
, __last
);
3014 std::rotate(__first
, __middle
, __last
);
3015 std::advance(__first
, std::distance(__middle
, __last
));
3022 * This is a helper function for the merge routines.
3025 template<typename _BidirectionalIterator
, typename _Distance
, typename _Pointer
>
3027 __merge_adaptive(_BidirectionalIterator __first
,
3028 _BidirectionalIterator __middle
,
3029 _BidirectionalIterator __last
,
3030 _Distance __len1
, _Distance __len2
,
3031 _Pointer __buffer
, _Distance __buffer_size
)
3033 if (__len1
<= __len2
&& __len1
<= __buffer_size
) {
3034 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3035 std::merge(__buffer
, __buffer_end
, __middle
, __last
, __first
);
3037 else if (__len2
<= __buffer_size
) {
3038 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3039 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
, __last
);
3042 _BidirectionalIterator __first_cut
= __first
;
3043 _BidirectionalIterator __second_cut
= __middle
;
3044 _Distance __len11
= 0;
3045 _Distance __len22
= 0;
3046 if (__len1
> __len2
) {
3047 __len11
= __len1
/ 2;
3048 std::advance(__first_cut
, __len11
);
3049 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
3050 __len22
= std::distance(__middle
, __second_cut
);
3053 __len22
= __len2
/ 2;
3054 std::advance(__second_cut
, __len22
);
3055 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
3056 __len11
= std::distance(__first
, __first_cut
);
3058 _BidirectionalIterator __new_middle
=
3059 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3060 __len1
- __len11
, __len22
, __buffer
,
3062 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3063 __len22
, __buffer
, __buffer_size
);
3064 std::__merge_adaptive(__new_middle
, __second_cut
, __last
, __len1
- __len11
,
3065 __len2
- __len22
, __buffer
, __buffer_size
);
3071 * This is a helper function for the merge routines.
3074 template<typename _BidirectionalIterator
, typename _Distance
, typename _Pointer
,
3077 __merge_adaptive(_BidirectionalIterator __first
,
3078 _BidirectionalIterator __middle
,
3079 _BidirectionalIterator __last
,
3080 _Distance __len1
, _Distance __len2
,
3081 _Pointer __buffer
, _Distance __buffer_size
,
3084 if (__len1
<= __len2
&& __len1
<= __buffer_size
) {
3085 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3086 std::merge(__buffer
, __buffer_end
, __middle
, __last
, __first
, __comp
);
3088 else if (__len2
<= __buffer_size
) {
3089 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3090 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
, __last
,
3094 _BidirectionalIterator __first_cut
= __first
;
3095 _BidirectionalIterator __second_cut
= __middle
;
3096 _Distance __len11
= 0;
3097 _Distance __len22
= 0;
3098 if (__len1
> __len2
) {
3099 __len11
= __len1
/ 2;
3100 std::advance(__first_cut
, __len11
);
3101 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
, __comp
);
3102 __len22
= std::distance(__middle
, __second_cut
);
3105 __len22
= __len2
/ 2;
3106 std::advance(__second_cut
, __len22
);
3107 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
, __comp
);
3108 __len11
= std::distance(__first
, __first_cut
);
3110 _BidirectionalIterator __new_middle
=
3111 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3112 __len1
- __len11
, __len22
, __buffer
,
3114 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3115 __len22
, __buffer
, __buffer_size
, __comp
);
3116 std::__merge_adaptive(__new_middle
, __second_cut
, __last
, __len1
- __len11
,
3117 __len2
- __len22
, __buffer
, __buffer_size
, __comp
);
3122 * @brief Merges two sorted ranges in place.
3123 * @param first An iterator.
3124 * @param middle Another iterator.
3125 * @param last Another iterator.
3128 * Merges two sorted and consecutive ranges, [first,middle) and
3129 * [middle,last), and puts the result in [first,last). The output will
3130 * be sorted. The sort is @e stable, that is, for equivalent
3131 * elements in the two ranges, elements from the first range will always
3132 * come before elements from the second.
3134 * If enough additional memory is available, this takes (last-first)-1
3135 * comparisons. Otherwise an NlogN algorithm is used, where N is
3136 * distance(first,last).
3138 template<typename _BidirectionalIterator
>
3140 inplace_merge(_BidirectionalIterator __first
,
3141 _BidirectionalIterator __middle
,
3142 _BidirectionalIterator __last
)
3144 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3146 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3149 // concept requirements
3150 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3151 _BidirectionalIterator
>)
3152 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3154 if (__first
== __middle
|| __middle
== __last
)
3157 _DistanceType __len1
= std::distance(__first
, __middle
);
3158 _DistanceType __len2
= std::distance(__middle
, __last
);
3160 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
, __last
);
3161 if (__buf
.begin() == 0)
3162 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3164 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3165 __buf
.begin(), _DistanceType(__buf
.size()));
3169 * @brief Merges two sorted ranges in place.
3170 * @param first An iterator.
3171 * @param middle Another iterator.
3172 * @param last Another iterator.
3173 * @param comp A functor to use for comparisons.
3176 * Merges two sorted and consecutive ranges, [first,middle) and
3177 * [middle,last), and puts the result in [first,last). The output will
3178 * be sorted. The sort is @e stable, that is, for equivalent
3179 * elements in the two ranges, elements from the first range will always
3180 * come before elements from the second.
3182 * If enough additional memory is available, this takes (last-first)-1
3183 * comparisons. Otherwise an NlogN algorithm is used, where N is
3184 * distance(first,last).
3186 * The comparison function should have the same effects on ordering as
3187 * the function used for the initial sort.
3189 template<typename _BidirectionalIterator
, typename _Compare
>
3191 inplace_merge(_BidirectionalIterator __first
,
3192 _BidirectionalIterator __middle
,
3193 _BidirectionalIterator __last
,
3196 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3198 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3201 // concept requirements
3202 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3203 _BidirectionalIterator
>)
3204 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3205 _ValueType
, _ValueType
>)
3207 if (__first
== __middle
|| __middle
== __last
)
3210 _DistanceType __len1
= std::distance(__first
, __middle
);
3211 _DistanceType __len2
= std::distance(__middle
, __last
);
3213 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
, __last
);
3214 if (__buf
.begin() == 0)
3215 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
, __comp
);
3217 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3218 __buf
.begin(), _DistanceType(__buf
.size()),
3222 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Distance
>
3224 __stable_sort_adaptive(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3225 _Pointer __buffer
, _Distance __buffer_size
)
3227 _Distance __len
= (__last
- __first
+ 1) / 2;
3228 _RandomAccessIterator __middle
= __first
+ __len
;
3229 if (__len
> __buffer_size
) {
3230 std::__stable_sort_adaptive(__first
, __middle
, __buffer
, __buffer_size
);
3231 std::__stable_sort_adaptive(__middle
, __last
, __buffer
, __buffer_size
);
3234 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3235 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3237 std::__merge_adaptive(__first
, __middle
, __last
, _Distance(__middle
- __first
),
3238 _Distance(__last
- __middle
), __buffer
, __buffer_size
);
3241 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Distance
,
3244 __stable_sort_adaptive(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3245 _Pointer __buffer
, _Distance __buffer_size
,
3248 _Distance __len
= (__last
- __first
+ 1) / 2;
3249 _RandomAccessIterator __middle
= __first
+ __len
;
3250 if (__len
> __buffer_size
) {
3251 std::__stable_sort_adaptive(__first
, __middle
, __buffer
, __buffer_size
,
3253 std::__stable_sort_adaptive(__middle
, __last
, __buffer
, __buffer_size
,
3257 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3258 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3260 std::__merge_adaptive(__first
, __middle
, __last
, _Distance(__middle
- __first
),
3261 _Distance(__last
- __middle
), __buffer
, __buffer_size
,
3266 * @brief Sort the elements of a sequence, preserving the relative order
3267 * of equivalent elements.
3268 * @param first An iterator.
3269 * @param last Another iterator.
3272 * Sorts the elements in the range @p [first,last) in ascending order,
3273 * such that @p *(i+1)<*i is false for each iterator @p i in the range
3274 * @p [first,last-1).
3276 * The relative ordering of equivalent elements is preserved, so any two
3277 * elements @p x and @p y in the range @p [first,last) such that
3278 * @p x<y is false and @p y<x is false will have the same relative
3279 * ordering after calling @p stable_sort().
3281 template<typename _RandomAccessIterator
>
3283 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
3285 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type _ValueType
;
3286 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type _DistanceType
;
3288 // concept requirements
3289 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3290 _RandomAccessIterator
>)
3291 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3293 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> buf(__first
, __last
);
3294 if (buf
.begin() == 0)
3295 std::__inplace_stable_sort(__first
, __last
);
3297 std::__stable_sort_adaptive(__first
, __last
, buf
.begin(), _DistanceType(buf
.size()));
3301 * @brief Sort the elements of a sequence using a predicate for comparison,
3302 * preserving the relative order of equivalent elements.
3303 * @param first An iterator.
3304 * @param last Another iterator.
3305 * @param comp A comparison functor.
3308 * Sorts the elements in the range @p [first,last) in ascending order,
3309 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
3310 * range @p [first,last-1).
3312 * The relative ordering of equivalent elements is preserved, so any two
3313 * elements @p x and @p y in the range @p [first,last) such that
3314 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
3315 * relative ordering after calling @p stable_sort().
3317 template<typename _RandomAccessIterator
, typename _Compare
>
3319 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
, _Compare __comp
)
3321 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type _ValueType
;
3322 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type _DistanceType
;
3324 // concept requirements
3325 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3326 _RandomAccessIterator
>)
3327 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3328 _ValueType
, _ValueType
>)
3330 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> buf(__first
, __last
);
3331 if (buf
.begin() == 0)
3332 std::__inplace_stable_sort(__first
, __last
, __comp
);
3334 std::__stable_sort_adaptive(__first
, __last
, buf
.begin(), _DistanceType(buf
.size()),
3339 * @brief Sort a sequence just enough to find a particular position.
3340 * @param first An iterator.
3341 * @param nth Another iterator.
3342 * @param last Another iterator.
3345 * Rearranges the elements in the range @p [first,last) so that @p *nth
3346 * is the same element that would have been in that position had the
3347 * whole sequence been sorted.
3348 * whole sequence been sorted. The elements either side of @p *nth are
3349 * not completely sorted, but for any iterator @i in the range
3350 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3351 * holds that @p *j<*i is false.
3353 template<typename _RandomAccessIterator
>
3355 nth_element(_RandomAccessIterator __first
,
3356 _RandomAccessIterator __nth
,
3357 _RandomAccessIterator __last
)
3359 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type _ValueType
;
3361 // concept requirements
3362 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<_RandomAccessIterator
>)
3363 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3365 while (__last
- __first
> 3) {
3366 _RandomAccessIterator __cut
=
3367 std::__unguarded_partition(__first
, __last
,
3368 _ValueType(std::__median(*__first
,
3369 *(__first
+ (__last
- __first
)/2),
3376 std::__insertion_sort(__first
, __last
);
3380 * @brief Sort a sequence just enough to find a particular position
3381 * using a predicate for comparison.
3382 * @param first An iterator.
3383 * @param nth Another iterator.
3384 * @param last Another iterator.
3385 * @param comp A comparison functor.
3388 * Rearranges the elements in the range @p [first,last) so that @p *nth
3389 * is the same element that would have been in that position had the
3390 * whole sequence been sorted. The elements either side of @p *nth are
3391 * not completely sorted, but for any iterator @i in the range
3392 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3393 * holds that @p comp(*j,*i) is false.
3395 template<typename _RandomAccessIterator
, typename _Compare
>
3397 nth_element(_RandomAccessIterator __first
,
3398 _RandomAccessIterator __nth
,
3399 _RandomAccessIterator __last
,
3402 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type _ValueType
;
3404 // concept requirements
3405 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<_RandomAccessIterator
>)
3406 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3407 _ValueType
, _ValueType
>)
3409 while (__last
- __first
> 3) {
3410 _RandomAccessIterator __cut
=
3411 std::__unguarded_partition(__first
, __last
,
3412 _ValueType(std::__median(*__first
,
3413 *(__first
+ (__last
- __first
)/2),
3421 std::__insertion_sort(__first
, __last
, __comp
);
3425 * @brief Finds the largest subrange in which @a val could be inserted
3426 * at any place in it without changing the ordering.
3427 * @param first An iterator.
3428 * @param last Another iterator.
3429 * @param val The search term.
3430 * @return An pair of iterators defining the subrange.
3431 * @ingroup binarysearch
3433 * This is equivalent to
3435 * std::make_pair(lower_bound(first, last, val),
3436 * upper_bound(first, last, val))
3438 * but does not actually call those functions.
3440 template<typename _ForwardIterator
, typename _Tp
>
3441 pair
<_ForwardIterator
, _ForwardIterator
>
3442 equal_range(_ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __val
)
3444 typedef typename iterator_traits
<_ForwardIterator
>::value_type _ValueType
;
3445 typedef typename iterator_traits
<_ForwardIterator
>::difference_type _DistanceType
;
3447 // concept requirements
3448 // See comments on lower_bound.
3449 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3450 __glibcxx_function_requires(_SameTypeConcept
<_Tp
, _ValueType
>)
3451 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3453 _DistanceType __len
= std::distance(__first
, __last
);
3454 _DistanceType __half
;
3455 _ForwardIterator __middle
, __left
, __right
;
3458 __half
= __len
>> 1;
3460 std::advance(__middle
, __half
);
3461 if (*__middle
< __val
) {
3464 __len
= __len
- __half
- 1;
3466 else if (__val
< *__middle
)
3469 __left
= std::lower_bound(__first
, __middle
, __val
);
3470 std::advance(__first
, __len
);
3471 __right
= std::upper_bound(++__middle
, __first
, __val
);
3472 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
3475 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
3479 * @brief Finds the largest subrange in which @a val could be inserted
3480 * at any place in it without changing the ordering.
3481 * @param first An iterator.
3482 * @param last Another iterator.
3483 * @param val The search term.
3484 * @param comp A functor to use for comparisons.
3485 * @return An pair of iterators defining the subrange.
3486 * @ingroup binarysearch
3488 * This is equivalent to
3490 * std::make_pair(lower_bound(first, last, val, comp),
3491 * upper_bound(first, last, val, comp))
3493 * but does not actually call those functions.
3495 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3496 pair
<_ForwardIterator
, _ForwardIterator
>
3497 equal_range(_ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __val
,
3500 typedef typename iterator_traits
<_ForwardIterator
>::value_type _ValueType
;
3501 typedef typename iterator_traits
<_ForwardIterator
>::difference_type _DistanceType
;
3503 // concept requirements
3504 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3505 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
, _Tp
>)
3506 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _Tp
, _ValueType
>)
3508 _DistanceType __len
= std::distance(__first
, __last
);
3509 _DistanceType __half
;
3510 _ForwardIterator __middle
, __left
, __right
;
3513 __half
= __len
>> 1;
3515 std::advance(__middle
, __half
);
3516 if (__comp(*__middle
, __val
)) {
3519 __len
= __len
- __half
- 1;
3521 else if (__comp(__val
, *__middle
))
3524 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
3525 std::advance(__first
, __len
);
3526 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
3527 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
3530 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
3534 * @brief Determines whether an element exists in a range.
3535 * @param first An iterator.
3536 * @param last Another iterator.
3537 * @param val The search term.
3538 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
3539 * @ingroup binarysearch
3541 * Note that this does not actually return an iterator to @a val. For
3542 * that, use std::find or a container's specialized find member functions.
3544 template<typename _ForwardIterator
, typename _Tp
>
3546 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
3549 // concept requirements
3550 // See comments on lower_bound.
3551 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3552 __glibcxx_function_requires(_SameTypeConcept
<_Tp
,
3553 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3554 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3556 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
3557 return __i
!= __last
&& !(__val
< *__i
);
3561 * @brief Determines whether an element exists in a range.
3562 * @param first An iterator.
3563 * @param last Another iterator.
3564 * @param val The search term.
3565 * @param comp A functor to use for comparisons.
3566 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
3567 * @ingroup binarysearch
3569 * Note that this does not actually return an iterator to @a val. For
3570 * that, use std::find or a container's specialized find member functions.
3572 * The comparison function should have the same effects on ordering as
3573 * the function used for the initial sort.
3575 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3577 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
3578 const _Tp
& __val
, _Compare __comp
)
3580 // concept requirements
3581 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3582 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3583 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
3584 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _Tp
,
3585 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3587 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
3588 return __i
!= __last
&& !__comp(__val
, *__i
);
3591 // Set algorithms: includes, set_union, set_intersection, set_difference,
3592 // set_symmetric_difference. All of these algorithms have the precondition
3593 // that their input ranges are sorted and the postcondition that their output
3594 // ranges are sorted.
3597 * @brief Determines whether all elements of a sequence exists in a range.
3598 * @param first1 Start of search range.
3599 * @param last1 End of search range.
3600 * @param first2 Start of sequence
3601 * @param last2 End of sequence.
3602 * @return True if each element in [first2,last2) is contained in order
3603 * within [first1,last1). False otherwise.
3604 * @ingroup setoperations
3606 * This operation expects both [first1,last1) and [first2,last2) to be
3607 * sorted. Searches for the presence of each element in [first2,last2)
3608 * within [first1,last1). The iterators over each range only move forward,
3609 * so this is a linear algorithm. If an element in [first2,last2) is not
3610 * found before the search iterator reaches @a last2, false is returned.
3612 template<typename _InputIterator1
, typename _InputIterator2
>
3614 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3615 _InputIterator2 __first2
, _InputIterator2 __last2
)
3617 // concept requirements
3618 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3619 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3620 __glibcxx_function_requires(_SameTypeConcept
<
3621 typename iterator_traits
<_InputIterator1
>::value_type
,
3622 typename iterator_traits
<_InputIterator2
>::value_type
>)
3623 __glibcxx_function_requires(_LessThanComparableConcept
<
3624 typename iterator_traits
<_InputIterator1
>::value_type
>)
3626 while (__first1
!= __last1
&& __first2
!= __last2
)
3627 if (*__first2
< *__first1
)
3629 else if(*__first1
< *__first2
)
3632 ++__first1
, ++__first2
;
3634 return __first2
== __last2
;
3638 * @brief Determines whether all elements of a sequence exists in a range
3640 * @param first1 Start of search range.
3641 * @param last1 End of search range.
3642 * @param first2 Start of sequence
3643 * @param last2 End of sequence.
3644 * @param comp Comparison function to use.
3645 * @return True if each element in [first2,last2) is contained in order
3646 * within [first1,last1) according to comp. False otherwise.
3647 * @ingroup setoperations
3649 * This operation expects both [first1,last1) and [first2,last2) to be
3650 * sorted. Searches for the presence of each element in [first2,last2)
3651 * within [first1,last1), using comp to decide. The iterators over each
3652 * range only move forward, so this is a linear algorithm. If an element
3653 * in [first2,last2) is not found before the search iterator reaches @a
3654 * last2, false is returned.
3656 template<typename _InputIterator1
, typename _InputIterator2
, typename _Compare
>
3658 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3659 _InputIterator2 __first2
, _InputIterator2 __last2
, _Compare __comp
)
3661 // concept requirements
3662 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3663 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3664 __glibcxx_function_requires(_SameTypeConcept
<
3665 typename iterator_traits
<_InputIterator1
>::value_type
,
3666 typename iterator_traits
<_InputIterator2
>::value_type
>)
3667 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3668 typename iterator_traits
<_InputIterator1
>::value_type
,
3669 typename iterator_traits
<_InputIterator2
>::value_type
>)
3671 while (__first1
!= __last1
&& __first2
!= __last2
)
3672 if (__comp(*__first2
, *__first1
))
3674 else if(__comp(*__first1
, *__first2
))
3677 ++__first1
, ++__first2
;
3679 return __first2
== __last2
;
3683 * @brief Return the union of two sorted ranges.
3684 * @param first1 Start of first range.
3685 * @param last1 End of first range.
3686 * @param first2 Start of second range.
3687 * @param last2 End of second range.
3688 * @return End of the output range.
3689 * @ingroup setoperations
3691 * This operation iterates over both ranges, copying elements present in
3692 * each range in order to the output range. Iterators increment for each
3693 * range. When the current element of one range is less than the other,
3694 * that element is copied and the iterator advanced. If an element is
3695 * contained in both ranges, the element from the first range is copied and
3696 * both ranges advance. The output range may not overlap either input
3699 template<typename _InputIterator1
, typename _InputIterator2
, typename _OutputIterator
>
3701 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
3702 _InputIterator2 __first2
, _InputIterator2 __last2
,
3703 _OutputIterator __result
)
3705 // concept requirements
3706 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3707 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3708 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3709 typename iterator_traits
<_InputIterator1
>::value_type
>)
3710 __glibcxx_function_requires(_SameTypeConcept
<
3711 typename iterator_traits
<_InputIterator1
>::value_type
,
3712 typename iterator_traits
<_InputIterator2
>::value_type
>)
3713 __glibcxx_function_requires(_LessThanComparableConcept
<
3714 typename iterator_traits
<_InputIterator1
>::value_type
>)
3716 while (__first1
!= __last1
&& __first2
!= __last2
) {
3717 if (*__first1
< *__first2
) {
3718 *__result
= *__first1
;
3721 else if (*__first2
< *__first1
) {
3722 *__result
= *__first2
;
3726 *__result
= *__first1
;
3732 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
, __result
));
3736 * @brief Return the union of two sorted ranges using a comparison functor.
3737 * @param first1 Start of first range.
3738 * @param last1 End of first range.
3739 * @param first2 Start of second range.
3740 * @param last2 End of second range.
3741 * @param comp The comparison functor.
3742 * @return End of the output range.
3743 * @ingroup setoperations
3745 * This operation iterates over both ranges, copying elements present in
3746 * each range in order to the output range. Iterators increment for each
3747 * range. When the current element of one range is less than the other
3748 * according to @a comp, that element is copied and the iterator advanced.
3749 * If an equivalent element according to @a comp is contained in both
3750 * ranges, the element from the first range is copied and both ranges
3751 * advance. The output range may not overlap either input range.
3753 template<typename _InputIterator1
, typename _InputIterator2
, typename _OutputIterator
,
3756 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
3757 _InputIterator2 __first2
, _InputIterator2 __last2
,
3758 _OutputIterator __result
, _Compare __comp
)
3760 // concept requirements
3761 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3762 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3763 __glibcxx_function_requires(_SameTypeConcept
<
3764 typename iterator_traits
<_InputIterator1
>::value_type
,
3765 typename iterator_traits
<_InputIterator2
>::value_type
>)
3766 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3767 typename iterator_traits
<_InputIterator1
>::value_type
>)
3768 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3769 typename iterator_traits
<_InputIterator1
>::value_type
,
3770 typename iterator_traits
<_InputIterator2
>::value_type
>)
3772 while (__first1
!= __last1
&& __first2
!= __last2
) {
3773 if (__comp(*__first1
, *__first2
)) {
3774 *__result
= *__first1
;
3777 else if (__comp(*__first2
, *__first1
)) {
3778 *__result
= *__first2
;
3782 *__result
= *__first1
;
3788 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
, __result
));
3792 * @brief Return the intersection of two sorted ranges.
3793 * @param first1 Start of first range.
3794 * @param last1 End of first range.
3795 * @param first2 Start of second range.
3796 * @param last2 End of second range.
3797 * @return End of the output range.
3798 * @ingroup setoperations
3800 * This operation iterates over both ranges, copying elements present in
3801 * both ranges in order to the output range. Iterators increment for each
3802 * range. When the current element of one range is less than the other,
3803 * that iterator advances. If an element is contained in both ranges, the
3804 * element from the first range is copied and both ranges advance. The
3805 * output range may not overlap either input range.
3807 template<typename _InputIterator1
, typename _InputIterator2
, typename _OutputIterator
>
3809 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
3810 _InputIterator2 __first2
, _InputIterator2 __last2
,
3811 _OutputIterator __result
)
3813 // concept requirements
3814 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3815 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3816 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3817 typename iterator_traits
<_InputIterator1
>::value_type
>)
3818 __glibcxx_function_requires(_SameTypeConcept
<
3819 typename iterator_traits
<_InputIterator1
>::value_type
,
3820 typename iterator_traits
<_InputIterator2
>::value_type
>)
3821 __glibcxx_function_requires(_LessThanComparableConcept
<
3822 typename iterator_traits
<_InputIterator1
>::value_type
>)
3824 while (__first1
!= __last1
&& __first2
!= __last2
)
3825 if (*__first1
< *__first2
)
3827 else if (*__first2
< *__first1
)
3830 *__result
= *__first1
;
3839 * @brief Return the intersection of two sorted ranges using comparison
3841 * @param first1 Start of first range.
3842 * @param last1 End of first range.
3843 * @param first2 Start of second range.
3844 * @param last2 End of second range.
3845 * @param comp The comparison functor.
3846 * @return End of the output range.
3847 * @ingroup setoperations
3849 * This operation iterates over both ranges, copying elements present in
3850 * both ranges in order to the output range. Iterators increment for each
3851 * range. When the current element of one range is less than the other
3852 * according to @a comp, that iterator advances. If an element is
3853 * contained in both ranges according to @a comp, the element from the
3854 * first range is copied and both ranges advance. The output range may not
3855 * overlap either input range.
3857 template<typename _InputIterator1
, typename _InputIterator2
, typename _OutputIterator
,
3860 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
3861 _InputIterator2 __first2
, _InputIterator2 __last2
,
3862 _OutputIterator __result
, _Compare __comp
)
3864 // concept requirements
3865 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3866 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3867 __glibcxx_function_requires(_SameTypeConcept
<
3868 typename iterator_traits
<_InputIterator1
>::value_type
,
3869 typename iterator_traits
<_InputIterator2
>::value_type
>)
3870 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3871 typename iterator_traits
<_InputIterator1
>::value_type
>)
3872 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3873 typename iterator_traits
<_InputIterator1
>::value_type
,
3874 typename iterator_traits
<_InputIterator2
>::value_type
>)
3876 while (__first1
!= __last1
&& __first2
!= __last2
)
3877 if (__comp(*__first1
, *__first2
))
3879 else if (__comp(*__first2
, *__first1
))
3882 *__result
= *__first1
;
3891 * @brief Return the difference of two sorted ranges.
3892 * @param first1 Start of first range.
3893 * @param last1 End of first range.
3894 * @param first2 Start of second range.
3895 * @param last2 End of second range.
3896 * @return End of the output range.
3897 * @ingroup setoperations
3899 * This operation iterates over both ranges, copying elements present in
3900 * the first range but not the second in order to the output range.
3901 * Iterators increment for each range. When the current element of the
3902 * first range is less than the second, that element is copied and the
3903 * iterator advances. If the current element of the second range is less,
3904 * the iterator advances, but no element is copied. If an element is
3905 * contained in both ranges, no elements are copied and both ranges
3906 * advance. The output range may not overlap either input range.
3908 template<typename _InputIterator1
, typename _InputIterator2
, typename _OutputIterator
>
3910 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
3911 _InputIterator2 __first2
, _InputIterator2 __last2
,
3912 _OutputIterator __result
)
3914 // concept requirements
3915 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3916 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3917 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3918 typename iterator_traits
<_InputIterator1
>::value_type
>)
3919 __glibcxx_function_requires(_SameTypeConcept
<
3920 typename iterator_traits
<_InputIterator1
>::value_type
,
3921 typename iterator_traits
<_InputIterator2
>::value_type
>)
3922 __glibcxx_function_requires(_LessThanComparableConcept
<
3923 typename iterator_traits
<_InputIterator1
>::value_type
>)
3925 while (__first1
!= __last1
&& __first2
!= __last2
)
3926 if (*__first1
< *__first2
) {
3927 *__result
= *__first1
;
3931 else if (*__first2
< *__first1
)
3937 return std::copy(__first1
, __last1
, __result
);
3941 * @brief Return the difference of two sorted ranges using comparison
3943 * @param first1 Start of first range.
3944 * @param last1 End of first range.
3945 * @param first2 Start of second range.
3946 * @param last2 End of second range.
3947 * @param comp The comparison functor.
3948 * @return End of the output range.
3949 * @ingroup setoperations
3951 * This operation iterates over both ranges, copying elements present in
3952 * the first range but not the second in order to the output range.
3953 * Iterators increment for each range. When the current element of the
3954 * first range is less than the second according to @a comp, that element
3955 * is copied and the iterator advances. If the current element of the
3956 * second range is less, no element is copied and the iterator advances.
3957 * If an element is contained in both ranges according to @a comp, no
3958 * elements are copied and both ranges advance. The output range may not
3959 * overlap either input range.
3961 template<typename _InputIterator1
, typename _InputIterator2
, typename _OutputIterator
,
3964 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
3965 _InputIterator2 __first2
, _InputIterator2 __last2
,
3966 _OutputIterator __result
, _Compare __comp
)
3968 // concept requirements
3969 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3970 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3971 __glibcxx_function_requires(_SameTypeConcept
<
3972 typename iterator_traits
<_InputIterator1
>::value_type
,
3973 typename iterator_traits
<_InputIterator2
>::value_type
>)
3974 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3975 typename iterator_traits
<_InputIterator1
>::value_type
>)
3976 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3977 typename iterator_traits
<_InputIterator1
>::value_type
,
3978 typename iterator_traits
<_InputIterator2
>::value_type
>)
3980 while (__first1
!= __last1
&& __first2
!= __last2
)
3981 if (__comp(*__first1
, *__first2
)) {
3982 *__result
= *__first1
;
3986 else if (__comp(*__first2
, *__first1
))
3992 return std::copy(__first1
, __last1
, __result
);
3996 * @brief Return the symmetric difference of two sorted ranges.
3997 * @param first1 Start of first range.
3998 * @param last1 End of first range.
3999 * @param first2 Start of second range.
4000 * @param last2 End of second range.
4001 * @return End of the output range.
4002 * @ingroup setoperations
4004 * This operation iterates over both ranges, copying elements present in
4005 * one range but not the other in order to the output range. Iterators
4006 * increment for each range. When the current element of one range is less
4007 * than the other, that element is copied and the iterator advances. If an
4008 * element is contained in both ranges, no elements are copied and both
4009 * ranges advance. The output range may not overlap either input range.
4011 template<typename _InputIterator1
, typename _InputIterator2
, typename _OutputIterator
>
4013 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4014 _InputIterator2 __first2
, _InputIterator2 __last2
,
4015 _OutputIterator __result
)
4017 // concept requirements
4018 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4019 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4020 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4021 typename iterator_traits
<_InputIterator1
>::value_type
>)
4022 __glibcxx_function_requires(_SameTypeConcept
<
4023 typename iterator_traits
<_InputIterator1
>::value_type
,
4024 typename iterator_traits
<_InputIterator2
>::value_type
>)
4025 __glibcxx_function_requires(_LessThanComparableConcept
<
4026 typename iterator_traits
<_InputIterator1
>::value_type
>)
4028 while (__first1
!= __last1
&& __first2
!= __last2
)
4029 if (*__first1
< *__first2
) {
4030 *__result
= *__first1
;
4034 else if (*__first2
< *__first1
) {
4035 *__result
= *__first2
;
4043 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
, __result
));
4047 * @brief Return the symmetric difference of two sorted ranges using
4048 * comparison functor.
4049 * @param first1 Start of first range.
4050 * @param last1 End of first range.
4051 * @param first2 Start of second range.
4052 * @param last2 End of second range.
4053 * @param comp The comparison functor.
4054 * @return End of the output range.
4055 * @ingroup setoperations
4057 * This operation iterates over both ranges, copying elements present in
4058 * one range but not the other in order to the output range. Iterators
4059 * increment for each range. When the current element of one range is less
4060 * than the other according to @a comp, that element is copied and the
4061 * iterator advances. If an element is contained in both ranges according
4062 * to @a comp, no elements are copied and both ranges advance. The output
4063 * range may not overlap either input range.
4065 template<typename _InputIterator1
, typename _InputIterator2
, typename _OutputIterator
,
4068 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4069 _InputIterator2 __first2
, _InputIterator2 __last2
,
4070 _OutputIterator __result
,
4073 // concept requirements
4074 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4075 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4076 __glibcxx_function_requires(_SameTypeConcept
<
4077 typename iterator_traits
<_InputIterator1
>::value_type
,
4078 typename iterator_traits
<_InputIterator2
>::value_type
>)
4079 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4080 typename iterator_traits
<_InputIterator1
>::value_type
>)
4081 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4082 typename iterator_traits
<_InputIterator1
>::value_type
,
4083 typename iterator_traits
<_InputIterator2
>::value_type
>)
4085 while (__first1
!= __last1
&& __first2
!= __last2
)
4086 if (__comp(*__first1
, *__first2
)) {
4087 *__result
= *__first1
;
4091 else if (__comp(*__first2
, *__first1
)) {
4092 *__result
= *__first2
;
4100 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
, __result
));
4103 // min_element and max_element, with and without an explicitly supplied
4104 // comparison function.
4107 * @brief Return the maximum element in a range.
4108 * @param first Start of range.
4109 * @param last End of range.
4110 * @return Iterator referencing the first instance of the largest value.
4112 template<typename _ForwardIterator
>
4114 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
4116 // concept requirements
4117 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4118 __glibcxx_function_requires(_LessThanComparableConcept
<
4119 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4121 if (__first
== __last
) return __first
;
4122 _ForwardIterator __result
= __first
;
4123 while (++__first
!= __last
)
4124 if (*__result
< *__first
)
4130 * @brief Return the maximum element in a range using comparison functor.
4131 * @param first Start of range.
4132 * @param last End of range.
4133 * @param comp Comparison functor.
4134 * @return Iterator referencing the first instance of the largest value
4135 * according to comp.
4137 template<typename _ForwardIterator
, typename _Compare
>
4139 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
4142 // concept requirements
4143 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4144 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4145 typename iterator_traits
<_ForwardIterator
>::value_type
,
4146 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4148 if (__first
== __last
) return __first
;
4149 _ForwardIterator __result
= __first
;
4150 while (++__first
!= __last
)
4151 if (__comp(*__result
, *__first
)) __result
= __first
;
4156 * @brief Return the minimum element in a range.
4157 * @param first Start of range.
4158 * @param last End of range.
4159 * @return Iterator referencing the first instance of the smallest value.
4161 template<typename _ForwardIterator
>
4163 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
4165 // concept requirements
4166 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4167 __glibcxx_function_requires(_LessThanComparableConcept
<
4168 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4170 if (__first
== __last
) return __first
;
4171 _ForwardIterator __result
= __first
;
4172 while (++__first
!= __last
)
4173 if (*__first
< *__result
)
4179 * @brief Return the minimum element in a range using comparison functor.
4180 * @param first Start of range.
4181 * @param last End of range.
4182 * @param comp Comparison functor.
4183 * @return Iterator referencing the first instance of the smallest value
4184 * according to comp.
4186 template<typename _ForwardIterator
, typename _Compare
>
4188 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
4191 // concept requirements
4192 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4193 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4194 typename iterator_traits
<_ForwardIterator
>::value_type
,
4195 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4197 if (__first
== __last
) return __first
;
4198 _ForwardIterator __result
= __first
;
4199 while (++__first
!= __last
)
4200 if (__comp(*__first
, *__result
))
4205 // next_permutation and prev_permutation, with and without an explicitly
4206 // supplied comparison function.
4209 * @brief Permute range into the next "dictionary" ordering.
4210 * @param first Start of range.
4211 * @param last End of range.
4212 * @return False if wrapped to first permutation, true otherwise.
4214 * Treats all permutations of the range as a set of "dictionary" sorted
4215 * sequences. Permutes the current sequence into the next one of this set.
4216 * Returns true if there are more sequences to generate. If the sequence
4217 * is the largest of the set, the smallest is generated and false returned.
4219 template<typename _BidirectionalIterator
>
4221 next_permutation(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
4223 // concept requirements
4224 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BidirectionalIterator
>)
4225 __glibcxx_function_requires(_LessThanComparableConcept
<
4226 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4228 if (__first
== __last
)
4230 _BidirectionalIterator __i
= __first
;
4238 _BidirectionalIterator __ii
= __i
;
4241 _BidirectionalIterator __j
= __last
;
4242 while (!(*__i
< *--__j
))
4244 std::iter_swap(__i
, __j
);
4245 std::reverse(__ii
, __last
);
4248 if (__i
== __first
) {
4249 std::reverse(__first
, __last
);
4256 * @brief Permute range into the next "dictionary" ordering using
4257 * comparison functor.
4258 * @param first Start of range.
4259 * @param last End of range.
4261 * @return False if wrapped to first permutation, true otherwise.
4263 * Treats all permutations of the range [first,last) as a set of
4264 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
4265 * sequence into the next one of this set. Returns true if there are more
4266 * sequences to generate. If the sequence is the largest of the set, the
4267 * smallest is generated and false returned.
4269 template<typename _BidirectionalIterator
, typename _Compare
>
4271 next_permutation(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
4274 // concept requirements
4275 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BidirectionalIterator
>)
4276 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4277 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
4278 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4280 if (__first
== __last
)
4282 _BidirectionalIterator __i
= __first
;
4290 _BidirectionalIterator __ii
= __i
;
4292 if (__comp(*__i
, *__ii
)) {
4293 _BidirectionalIterator __j
= __last
;
4294 while (!__comp(*__i
, *--__j
))
4296 std::iter_swap(__i
, __j
);
4297 std::reverse(__ii
, __last
);
4300 if (__i
== __first
) {
4301 std::reverse(__first
, __last
);
4308 * @brief Permute range into the previous "dictionary" ordering.
4309 * @param first Start of range.
4310 * @param last End of range.
4311 * @return False if wrapped to last permutation, true otherwise.
4313 * Treats all permutations of the range as a set of "dictionary" sorted
4314 * sequences. Permutes the current sequence into the previous one of this
4315 * set. Returns true if there are more sequences to generate. If the
4316 * sequence is the smallest of the set, the largest is generated and false
4319 template<typename _BidirectionalIterator
>
4321 prev_permutation(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
4323 // concept requirements
4324 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BidirectionalIterator
>)
4325 __glibcxx_function_requires(_LessThanComparableConcept
<
4326 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4328 if (__first
== __last
)
4330 _BidirectionalIterator __i
= __first
;
4338 _BidirectionalIterator __ii
= __i
;
4341 _BidirectionalIterator __j
= __last
;
4342 while (!(*--__j
< *__i
))
4344 std::iter_swap(__i
, __j
);
4345 std::reverse(__ii
, __last
);
4348 if (__i
== __first
) {
4349 std::reverse(__first
, __last
);
4356 * @brief Permute range into the previous "dictionary" ordering using
4357 * comparison functor.
4358 * @param first Start of range.
4359 * @param last End of range.
4361 * @return False if wrapped to last permutation, true otherwise.
4363 * Treats all permutations of the range [first,last) as a set of
4364 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
4365 * sequence into the previous one of this set. Returns true if there are
4366 * more sequences to generate. If the sequence is the smallest of the set,
4367 * the largest is generated and false returned.
4369 template<typename _BidirectionalIterator
, typename _Compare
>
4371 prev_permutation(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
4374 // concept requirements
4375 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BidirectionalIterator
>)
4376 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4377 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
4378 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4380 if (__first
== __last
)
4382 _BidirectionalIterator __i
= __first
;
4390 _BidirectionalIterator __ii
= __i
;
4392 if (__comp(*__ii
, *__i
)) {
4393 _BidirectionalIterator __j
= __last
;
4394 while (!__comp(*--__j
, *__i
))
4396 std::iter_swap(__i
, __j
);
4397 std::reverse(__ii
, __last
);
4400 if (__i
== __first
) {
4401 std::reverse(__first
, __last
);
4407 // find_first_of, with and without an explicitly supplied comparison function.
4410 * @brief Find element from a set in a sequence.
4411 * @param first1 Start of range to search.
4412 * @param last1 End of range to search.
4413 * @param first2 Start of match candidates.
4414 * @param last2 End of match candidates.
4415 * @return The first iterator @c i in the range
4416 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4417 * interator in [first2,last2), or @p last1 if no such iterator exists.
4419 * Searches the range @p [first1,last1) for an element that is equal to
4420 * some element in the range [first2,last2). If found, returns an iterator
4421 * in the range [first1,last1), otherwise returns @p last1.
4423 template<typename _InputIterator
, typename _ForwardIterator
>
4425 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4426 _ForwardIterator __first2
, _ForwardIterator __last2
)
4428 // concept requirements
4429 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4430 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4431 __glibcxx_function_requires(_EqualOpConcept
<
4432 typename iterator_traits
<_InputIterator
>::value_type
,
4433 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4435 for ( ; __first1
!= __last1
; ++__first1
)
4436 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4437 if (*__first1
== *__iter
)
4443 * @brief Find element from a set in a sequence using a predicate.
4444 * @param first1 Start of range to search.
4445 * @param last1 End of range to search.
4446 * @param first2 Start of match candidates.
4447 * @param last2 End of match candidates.
4448 * @param comp Predicate to use.
4449 * @return The first iterator @c i in the range
4450 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4451 * interator in [first2,last2), or @p last1 if no such iterator exists.
4453 * Searches the range @p [first1,last1) for an element that is equal to
4454 * some element in the range [first2,last2). If found, returns an iterator in
4455 * the range [first1,last1), otherwise returns @p last1.
4457 template<typename _InputIterator
, typename _ForwardIterator
, typename _BinaryPredicate
>
4459 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4460 _ForwardIterator __first2
, _ForwardIterator __last2
,
4461 _BinaryPredicate __comp
)
4463 // concept requirements
4464 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4465 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4466 __glibcxx_function_requires(_EqualOpConcept
<
4467 typename iterator_traits
<_InputIterator
>::value_type
,
4468 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4469 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4470 typename iterator_traits
<_InputIterator
>::value_type
,
4471 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4473 for ( ; __first1
!= __last1
; ++__first1
)
4474 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4475 if (__comp(*__first1
, *__iter
))
4481 // find_end, with and without an explicitly supplied comparison function.
4482 // Search [first2, last2) as a subsequence in [first1, last1), and return
4483 // the *last* possible match. Note that find_end for bidirectional iterators
4484 // is much faster than for forward iterators.
4486 // find_end for forward iterators.
4487 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4489 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4490 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4491 forward_iterator_tag
, forward_iterator_tag
)
4493 if (__first2
== __last2
)
4496 _ForwardIterator1 __result
= __last1
;
4498 _ForwardIterator1 __new_result
4499 = std::search(__first1
, __last1
, __first2
, __last2
);
4500 if (__new_result
== __last1
)
4503 __result
= __new_result
;
4504 __first1
= __new_result
;
4511 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4512 typename _BinaryPredicate
>
4514 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4515 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4516 forward_iterator_tag
, forward_iterator_tag
,
4517 _BinaryPredicate __comp
)
4519 if (__first2
== __last2
)
4522 _ForwardIterator1 __result
= __last1
;
4524 _ForwardIterator1 __new_result
4525 = std::search(__first1
, __last1
, __first2
, __last2
, __comp
);
4526 if (__new_result
== __last1
)
4529 __result
= __new_result
;
4530 __first1
= __new_result
;
4537 // find_end for bidirectional iterators. Requires partial specialization.
4538 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
4539 _BidirectionalIterator1
4540 __find_end(_BidirectionalIterator1 __first1
, _BidirectionalIterator1 __last1
,
4541 _BidirectionalIterator2 __first2
, _BidirectionalIterator2 __last2
,
4542 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
4544 // concept requirements
4545 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BidirectionalIterator1
>)
4546 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BidirectionalIterator2
>)
4548 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
4549 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
4551 _RevIterator1
__rlast1(__first1
);
4552 _RevIterator2
__rlast2(__first2
);
4553 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
4554 _RevIterator2(__last2
), __rlast2
);
4556 if (__rresult
== __rlast1
)
4559 _BidirectionalIterator1 __result
= __rresult
.base();
4560 std::advance(__result
, -std::distance(__first2
, __last2
));
4565 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
4566 typename _BinaryPredicate
>
4567 _BidirectionalIterator1
4568 __find_end(_BidirectionalIterator1 __first1
, _BidirectionalIterator1 __last1
,
4569 _BidirectionalIterator2 __first2
, _BidirectionalIterator2 __last2
,
4570 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
4571 _BinaryPredicate __comp
)
4573 // concept requirements
4574 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BidirectionalIterator1
>)
4575 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BidirectionalIterator2
>)
4577 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
4578 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
4580 _RevIterator1
__rlast1(__first1
);
4581 _RevIterator2
__rlast2(__first2
);
4582 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
4583 _RevIterator2(__last2
), __rlast2
,
4586 if (__rresult
== __rlast1
)
4589 _BidirectionalIterator1 __result
= __rresult
.base();
4590 std::advance(__result
, -std::distance(__first2
, __last2
));
4595 // Dispatching functions for find_end.
4598 * @brief Find last matching subsequence in a sequence.
4599 * @param first1 Start of range to search.
4600 * @param last1 End of range to search.
4601 * @param first2 Start of sequence to match.
4602 * @param last2 End of sequence to match.
4603 * @return The last iterator @c i in the range
4604 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
4605 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
4606 * such iterator exists.
4608 * Searches the range @p [first1,last1) for a sub-sequence that compares
4609 * equal value-by-value with the sequence given by @p [first2,last2) and
4610 * returns an iterator to the first element of the sub-sequence, or
4611 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
4612 * last such subsequence contained in [first,last1).
4614 * Because the sub-sequence must lie completely within the range
4615 * @p [first1,last1) it must start at a position less than
4616 * @p last1-(last2-first2) where @p last2-first2 is the length of the
4618 * This means that the returned iterator @c i will be in the range
4619 * @p [first1,last1-(last2-first2))
4621 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4622 inline _ForwardIterator1
4623 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4624 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
4626 // concept requirements
4627 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4628 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4629 __glibcxx_function_requires(_EqualOpConcept
<
4630 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4631 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4633 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
4634 std::__iterator_category(__first1
),
4635 std::__iterator_category(__first2
));
4639 * @brief Find last matching subsequence in a sequence using a predicate.
4640 * @param first1 Start of range to search.
4641 * @param last1 End of range to search.
4642 * @param first2 Start of sequence to match.
4643 * @param last2 End of sequence to match.
4644 * @param comp The predicate to use.
4645 * @return The last iterator @c i in the range
4646 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
4647 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
4648 * @p last1 if no such iterator exists.
4650 * Searches the range @p [first1,last1) for a sub-sequence that compares
4651 * equal value-by-value with the sequence given by @p [first2,last2) using
4652 * comp as a predicate and returns an iterator to the first element of the
4653 * sub-sequence, or @p last1 if the sub-sequence is not found. The
4654 * sub-sequence will be the last such subsequence contained in
4657 * Because the sub-sequence must lie completely within the range
4658 * @p [first1,last1) it must start at a position less than
4659 * @p last1-(last2-first2) where @p last2-first2 is the length of the
4661 * This means that the returned iterator @c i will be in the range
4662 * @p [first1,last1-(last2-first2))
4664 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4665 typename _BinaryPredicate
>
4666 inline _ForwardIterator1
4667 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4668 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4669 _BinaryPredicate __comp
)
4671 // concept requirements
4672 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
4673 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
4674 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4675 typename iterator_traits
<_ForwardIterator1
>::value_type
,
4676 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
4678 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
4679 std::__iterator_category(__first1
),
4680 std::__iterator_category(__first2
),
4686 #endif /* _ALGO_H */