1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004 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
66 #include <debug/debug.h>
68 // 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 __glibcxx_requires_valid_range(__first
, __last
);
157 for ( ; __first
!= __last
; ++__first
)
164 * This is an overload used by find() for the Input Iterator case.
167 template<typename _InputIterator
, typename _Tp
>
168 inline _InputIterator
169 find(_InputIterator __first
, _InputIterator __last
,
170 const _Tp
& __val
, input_iterator_tag
)
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
,
185 _Predicate __pred
, input_iterator_tag
)
187 while (__first
!= __last
&& !__pred(*__first
))
194 * This is an overload used by find() for the RAI case.
197 template<typename _RandomAccessIterator
, typename _Tp
>
198 _RandomAccessIterator
199 find(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
200 const _Tp
& __val
, random_access_iterator_tag
)
202 typename iterator_traits
<_RandomAccessIterator
>::difference_type
203 __trip_count
= (__last
- __first
) >> 2;
205 for ( ; __trip_count
> 0 ; --__trip_count
)
207 if (*__first
== __val
)
211 if (*__first
== __val
)
215 if (*__first
== __val
)
219 if (*__first
== __val
)
224 switch (__last
- __first
)
227 if (*__first
== __val
)
231 if (*__first
== __val
)
235 if (*__first
== __val
)
246 * This is an overload used by find_if() for the RAI case.
249 template<typename _RandomAccessIterator
, typename _Predicate
>
250 _RandomAccessIterator
251 find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
252 _Predicate __pred
, random_access_iterator_tag
)
254 typename iterator_traits
<_RandomAccessIterator
>::difference_type
255 __trip_count
= (__last
- __first
) >> 2;
257 for ( ; __trip_count
> 0 ; --__trip_count
)
259 if (__pred(*__first
))
263 if (__pred(*__first
))
267 if (__pred(*__first
))
271 if (__pred(*__first
))
276 switch (__last
- __first
)
279 if (__pred(*__first
))
283 if (__pred(*__first
))
287 if (__pred(*__first
))
297 * @brief Find the first occurrence of a value in a sequence.
298 * @param first An input iterator.
299 * @param last An input iterator.
300 * @param val The value to find.
301 * @return The first iterator @c i in the range @p [first,last)
302 * such that @c *i == @p val, or @p last if no such iterator exists.
304 template<typename _InputIterator
, typename _Tp
>
305 inline _InputIterator
306 find(_InputIterator __first
, _InputIterator __last
,
309 // concept requirements
310 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
311 __glibcxx_function_requires(_EqualOpConcept
<
312 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
313 __glibcxx_requires_valid_range(__first
, __last
);
314 return std::find(__first
, __last
, __val
,
315 std::__iterator_category(__first
));
319 * @brief Find the first element in a sequence for which a predicate is true.
320 * @param first An input iterator.
321 * @param last An input iterator.
322 * @param pred A predicate.
323 * @return The first iterator @c i in the range @p [first,last)
324 * such that @p pred(*i) is true, or @p last if no such iterator exists.
326 template<typename _InputIterator
, typename _Predicate
>
327 inline _InputIterator
328 find_if(_InputIterator __first
, _InputIterator __last
,
331 // concept requirements
332 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
333 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
334 typename iterator_traits
<_InputIterator
>::value_type
>)
335 __glibcxx_requires_valid_range(__first
, __last
);
336 return std::find_if(__first
, __last
, __pred
,
337 std::__iterator_category(__first
));
341 * @brief Find two adjacent values in a sequence that are equal.
342 * @param first A forward iterator.
343 * @param last A forward iterator.
344 * @return The first iterator @c i such that @c i and @c i+1 are both
345 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
346 * or @p last if no such iterator exists.
348 template<typename _ForwardIterator
>
350 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
352 // concept requirements
353 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
354 __glibcxx_function_requires(_EqualityComparableConcept
<
355 typename iterator_traits
<_ForwardIterator
>::value_type
>)
356 __glibcxx_requires_valid_range(__first
, __last
);
357 if (__first
== __last
)
359 _ForwardIterator __next
= __first
;
360 while(++__next
!= __last
)
362 if (*__first
== *__next
)
370 * @brief Find two adjacent values in a sequence using a predicate.
371 * @param first A forward iterator.
372 * @param last A forward iterator.
373 * @param binary_pred A binary predicate.
374 * @return The first iterator @c i such that @c i and @c i+1 are both
375 * valid iterators in @p [first,last) and such that
376 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
379 template<typename _ForwardIterator
, typename _BinaryPredicate
>
381 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
382 _BinaryPredicate __binary_pred
)
384 // concept requirements
385 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
386 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
387 typename iterator_traits
<_ForwardIterator
>::value_type
,
388 typename iterator_traits
<_ForwardIterator
>::value_type
>)
389 __glibcxx_requires_valid_range(__first
, __last
);
390 if (__first
== __last
)
392 _ForwardIterator __next
= __first
;
393 while(++__next
!= __last
)
395 if (__binary_pred(*__first
, *__next
))
403 * @brief Count the number of copies of a value in a sequence.
404 * @param first An input iterator.
405 * @param last An input iterator.
406 * @param value The value to be counted.
407 * @return The number of iterators @c i in the range @p [first,last)
408 * for which @c *i == @p value
410 template<typename _InputIterator
, typename _Tp
>
411 typename iterator_traits
<_InputIterator
>::difference_type
412 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
414 // concept requirements
415 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
416 __glibcxx_function_requires(_EqualityComparableConcept
<
417 typename iterator_traits
<_InputIterator
>::value_type
>)
418 __glibcxx_function_requires(_EqualityComparableConcept
<_Tp
>)
419 __glibcxx_requires_valid_range(__first
, __last
);
420 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
421 for ( ; __first
!= __last
; ++__first
)
422 if (*__first
== __value
)
428 * @brief Count the elements of a sequence for which a predicate is true.
429 * @param first An input iterator.
430 * @param last An input iterator.
431 * @param pred A predicate.
432 * @return The number of iterators @c i in the range @p [first,last)
433 * for which @p pred(*i) is true.
435 template<typename _InputIterator
, typename _Predicate
>
436 typename iterator_traits
<_InputIterator
>::difference_type
437 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
439 // concept requirements
440 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
441 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
442 typename iterator_traits
<_InputIterator
>::value_type
>)
443 __glibcxx_requires_valid_range(__first
, __last
);
444 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
445 for ( ; __first
!= __last
; ++__first
)
446 if (__pred(*__first
))
452 * @brief Search a sequence for a matching sub-sequence.
453 * @param first1 A forward iterator.
454 * @param last1 A forward iterator.
455 * @param first2 A forward iterator.
456 * @param last2 A forward iterator.
457 * @return The first iterator @c i in the range
458 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
459 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
460 * such iterator exists.
462 * Searches the range @p [first1,last1) for a sub-sequence that compares
463 * equal value-by-value with the sequence given by @p [first2,last2) and
464 * returns an iterator to the first element of the sub-sequence, or
465 * @p last1 if the sub-sequence is not found.
467 * Because the sub-sequence must lie completely within the range
468 * @p [first1,last1) it must start at a position less than
469 * @p last1-(last2-first2) where @p last2-first2 is the length of the
471 * This means that the returned iterator @c i will be in the range
472 * @p [first1,last1-(last2-first2))
474 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
476 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
477 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
479 // concept requirements
480 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
481 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
482 __glibcxx_function_requires(_EqualOpConcept
<
483 typename iterator_traits
<_ForwardIterator1
>::value_type
,
484 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
485 __glibcxx_requires_valid_range(__first1
, __last1
);
486 __glibcxx_requires_valid_range(__first2
, __last2
);
487 // Test for empty ranges
488 if (__first1
== __last1
|| __first2
== __last2
)
491 // Test for a pattern of length 1.
492 _ForwardIterator2
__tmp(__first2
);
494 if (__tmp
== __last2
)
495 return std::find(__first1
, __last1
, *__first2
);
498 _ForwardIterator2 __p1
, __p
;
499 __p1
= __first2
; ++__p1
;
500 _ForwardIterator1 __current
= __first1
;
502 while (__first1
!= __last1
)
504 __first1
= std::find(__first1
, __last1
, *__first2
);
505 if (__first1
== __last1
)
509 __current
= __first1
;
510 if (++__current
== __last1
)
513 while (*__current
== *__p
)
515 if (++__p
== __last2
)
517 if (++__current
== __last1
)
526 * @brief Search a sequence for a matching sub-sequence using a predicate.
527 * @param first1 A forward iterator.
528 * @param last1 A forward iterator.
529 * @param first2 A forward iterator.
530 * @param last2 A forward iterator.
531 * @param predicate A binary predicate.
532 * @return The first iterator @c i in the range
533 * @p [first1,last1-(last2-first2)) such that
534 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
535 * @p [0,last2-first2), or @p last1 if no such iterator exists.
537 * Searches the range @p [first1,last1) for a sub-sequence that compares
538 * equal value-by-value with the sequence given by @p [first2,last2),
539 * using @p predicate to determine equality, and returns an iterator
540 * to the first element of the sub-sequence, or @p last1 if no such
543 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
545 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
546 typename _BinaryPredicate
>
548 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
549 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
550 _BinaryPredicate __predicate
)
552 // concept requirements
553 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
554 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
555 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
556 typename iterator_traits
<_ForwardIterator1
>::value_type
,
557 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
558 __glibcxx_requires_valid_range(__first1
, __last1
);
559 __glibcxx_requires_valid_range(__first2
, __last2
);
561 // Test for empty ranges
562 if (__first1
== __last1
|| __first2
== __last2
)
565 // Test for a pattern of length 1.
566 _ForwardIterator2
__tmp(__first2
);
568 if (__tmp
== __last2
)
570 while (__first1
!= __last1
&& !__predicate(*__first1
, *__first2
))
576 _ForwardIterator2 __p1
, __p
;
577 __p1
= __first2
; ++__p1
;
578 _ForwardIterator1 __current
= __first1
;
580 while (__first1
!= __last1
)
582 while (__first1
!= __last1
)
584 if (__predicate(*__first1
, *__first2
))
588 while (__first1
!= __last1
&& !__predicate(*__first1
, *__first2
))
590 if (__first1
== __last1
)
594 __current
= __first1
;
595 if (++__current
== __last1
)
598 while (__predicate(*__current
, *__p
))
600 if (++__p
== __last2
)
602 if (++__current
== __last1
)
611 * @brief Search a sequence for a number of consecutive values.
612 * @param first A forward iterator.
613 * @param last A forward iterator.
614 * @param count The number of consecutive values.
615 * @param val The value to find.
616 * @return The first iterator @c i in the range @p [first,last-count)
617 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
618 * or @p last if no such iterator exists.
620 * Searches the range @p [first,last) for @p count consecutive elements
623 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
625 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
626 _Integer __count
, const _Tp
& __val
)
628 // concept requirements
629 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
630 __glibcxx_function_requires(_EqualityComparableConcept
<
631 typename iterator_traits
<_ForwardIterator
>::value_type
>)
632 __glibcxx_function_requires(_EqualityComparableConcept
<_Tp
>)
633 __glibcxx_requires_valid_range(__first
, __last
);
639 __first
= std::find(__first
, __last
, __val
);
640 while (__first
!= __last
)
642 typename iterator_traits
<_ForwardIterator
>::difference_type
644 _ForwardIterator __i
= __first
;
646 while (__i
!= __last
&& __n
!= 1 && *__i
== __val
)
654 __first
= std::find(__i
, __last
, __val
);
661 * @brief Search a sequence for a number of consecutive values using a
663 * @param first A forward iterator.
664 * @param last A forward iterator.
665 * @param count The number of consecutive values.
666 * @param val The value to find.
667 * @param binary_pred A binary predicate.
668 * @return The first iterator @c i in the range @p [first,last-count)
669 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
670 * range @p [0,count), or @p last if no such iterator exists.
672 * Searches the range @p [first,last) for @p count consecutive elements
673 * for which the predicate returns true.
675 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
676 typename _BinaryPredicate
>
678 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
679 _Integer __count
, const _Tp
& __val
,
680 _BinaryPredicate __binary_pred
)
682 // concept requirements
683 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
684 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
685 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
686 __glibcxx_requires_valid_range(__first
, __last
);
692 while (__first
!= __last
)
694 if (__binary_pred(*__first
, __val
))
698 while (__first
!= __last
)
700 typename iterator_traits
<_ForwardIterator
>::difference_type
702 _ForwardIterator __i
= __first
;
704 while (__i
!= __last
&& __n
!= 1 && __binary_pred(*__i
, __val
))
713 while (__i
!= __last
)
715 if (__binary_pred(*__i
, __val
))
727 * @brief Swap the elements of two sequences.
728 * @param first1 A forward iterator.
729 * @param last1 A forward iterator.
730 * @param first2 A forward iterator.
731 * @return An iterator equal to @p first2+(last1-first1).
733 * Swaps each element in the range @p [first1,last1) with the
734 * corresponding element in the range @p [first2,(last1-first1)).
735 * The ranges must not overlap.
737 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
739 swap_ranges(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
740 _ForwardIterator2 __first2
)
742 // concept requirements
743 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
745 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
747 __glibcxx_function_requires(_ConvertibleConcept
<
748 typename iterator_traits
<_ForwardIterator1
>::value_type
,
749 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
750 __glibcxx_function_requires(_ConvertibleConcept
<
751 typename iterator_traits
<_ForwardIterator2
>::value_type
,
752 typename iterator_traits
<_ForwardIterator1
>::value_type
>)
753 __glibcxx_requires_valid_range(__first1
, __last1
);
755 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
)
756 std::iter_swap(__first1
, __first2
);
761 * @brief Perform an operation on a sequence.
762 * @param first An input iterator.
763 * @param last An input iterator.
764 * @param result An output iterator.
765 * @param unary_op A unary operator.
766 * @return An output iterator equal to @p result+(last-first).
768 * Applies the operator to each element in the input range and assigns
769 * the results to successive elements of the output sequence.
770 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
771 * range @p [0,last-first).
773 * @p unary_op must not alter its argument.
775 template<typename _InputIterator
, typename _OutputIterator
,
776 typename _UnaryOperation
>
778 transform(_InputIterator __first
, _InputIterator __last
,
779 _OutputIterator __result
, _UnaryOperation __unary_op
)
781 // concept requirements
782 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
783 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
784 // "the type returned by a _UnaryOperation"
785 __typeof__(__unary_op(*__first
))>)
786 __glibcxx_requires_valid_range(__first
, __last
);
788 for ( ; __first
!= __last
; ++__first
, ++__result
)
789 *__result
= __unary_op(*__first
);
794 * @brief Perform an operation on corresponding elements of two sequences.
795 * @param first1 An input iterator.
796 * @param last1 An input iterator.
797 * @param first2 An input iterator.
798 * @param result An output iterator.
799 * @param binary_op A binary operator.
800 * @return An output iterator equal to @p result+(last-first).
802 * Applies the operator to the corresponding elements in the two
803 * input ranges and assigns the results to successive elements of the
805 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
806 * @c N in the range @p [0,last1-first1).
808 * @p binary_op must not alter either of its arguments.
810 template<typename _InputIterator1
, typename _InputIterator2
,
811 typename _OutputIterator
, typename _BinaryOperation
>
813 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
814 _InputIterator2 __first2
, _OutputIterator __result
,
815 _BinaryOperation __binary_op
)
817 // concept requirements
818 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
819 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
820 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
821 // "the type returned by a _BinaryOperation"
822 __typeof__(__binary_op(*__first1
,*__first2
))>)
823 __glibcxx_requires_valid_range(__first1
, __last1
);
825 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
826 *__result
= __binary_op(*__first1
, *__first2
);
831 * @brief Replace each occurrence of one value in a sequence with another
833 * @param first A forward iterator.
834 * @param last A forward iterator.
835 * @param old_value The value to be replaced.
836 * @param new_value The replacement value.
837 * @return replace() returns no value.
839 * For each iterator @c i in the range @p [first,last) if @c *i ==
840 * @p old_value then the assignment @c *i = @p new_value is performed.
842 template<typename _ForwardIterator
, typename _Tp
>
844 replace(_ForwardIterator __first
, _ForwardIterator __last
,
845 const _Tp
& __old_value
, const _Tp
& __new_value
)
847 // concept requirements
848 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
850 __glibcxx_function_requires(_EqualOpConcept
<
851 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
852 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
853 typename iterator_traits
<_ForwardIterator
>::value_type
>)
854 __glibcxx_requires_valid_range(__first
, __last
);
856 for ( ; __first
!= __last
; ++__first
)
857 if (*__first
== __old_value
)
858 *__first
= __new_value
;
862 * @brief Replace each value in a sequence for which a predicate returns
863 * true with another value.
864 * @param first A forward iterator.
865 * @param last A forward iterator.
866 * @param pred A predicate.
867 * @param new_value The replacement value.
868 * @return replace_if() returns no value.
870 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
871 * is true then the assignment @c *i = @p new_value is performed.
873 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
875 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
876 _Predicate __pred
, const _Tp
& __new_value
)
878 // concept requirements
879 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
881 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
882 typename iterator_traits
<_ForwardIterator
>::value_type
>)
883 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
884 typename iterator_traits
<_ForwardIterator
>::value_type
>)
885 __glibcxx_requires_valid_range(__first
, __last
);
887 for ( ; __first
!= __last
; ++__first
)
888 if (__pred(*__first
))
889 *__first
= __new_value
;
893 * @brief Copy a sequence, replacing each element of one value with another
895 * @param first An input iterator.
896 * @param last An input iterator.
897 * @param result An output iterator.
898 * @param old_value The value to be replaced.
899 * @param new_value The replacement value.
900 * @return The end of the output sequence, @p result+(last-first).
902 * Copies each element in the input range @p [first,last) to the
903 * output range @p [result,result+(last-first)) replacing elements
904 * equal to @p old_value with @p new_value.
906 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
908 replace_copy(_InputIterator __first
, _InputIterator __last
,
909 _OutputIterator __result
,
910 const _Tp
& __old_value
, const _Tp
& __new_value
)
912 // concept requirements
913 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
914 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
915 typename iterator_traits
<_InputIterator
>::value_type
>)
916 __glibcxx_function_requires(_EqualOpConcept
<
917 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
918 __glibcxx_requires_valid_range(__first
, __last
);
920 for ( ; __first
!= __last
; ++__first
, ++__result
)
921 *__result
= *__first
== __old_value
? __new_value
: *__first
;
926 * @brief Copy a sequence, replacing each value for which a predicate
927 * returns true with another value.
928 * @param first An input iterator.
929 * @param last An input iterator.
930 * @param result An output iterator.
931 * @param pred A predicate.
932 * @param new_value The replacement value.
933 * @return The end of the output sequence, @p result+(last-first).
935 * Copies each element in the range @p [first,last) to the range
936 * @p [result,result+(last-first)) replacing elements for which
937 * @p pred returns true with @p new_value.
939 template<typename _InputIterator
, typename _OutputIterator
,
940 typename _Predicate
, typename _Tp
>
942 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
943 _OutputIterator __result
,
944 _Predicate __pred
, const _Tp
& __new_value
)
946 // concept requirements
947 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
948 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
949 typename iterator_traits
<_InputIterator
>::value_type
>)
950 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
951 typename iterator_traits
<_InputIterator
>::value_type
>)
952 __glibcxx_requires_valid_range(__first
, __last
);
954 for ( ; __first
!= __last
; ++__first
, ++__result
)
955 *__result
= __pred(*__first
) ? __new_value
: *__first
;
960 * @brief Assign the result of a function object to each value in a
962 * @param first A forward iterator.
963 * @param last A forward iterator.
964 * @param gen A function object taking no arguments.
965 * @return generate() returns no value.
967 * Performs the assignment @c *i = @p gen() for each @c i in the range
970 template<typename _ForwardIterator
, typename _Generator
>
972 generate(_ForwardIterator __first
, _ForwardIterator __last
,
975 // concept requirements
976 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
977 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
978 typename iterator_traits
<_ForwardIterator
>::value_type
>)
979 __glibcxx_requires_valid_range(__first
, __last
);
981 for ( ; __first
!= __last
; ++__first
)
986 * @brief Assign the result of a function object to each value in a
988 * @param first A forward iterator.
989 * @param n The length of the sequence.
990 * @param gen A function object taking no arguments.
991 * @return The end of the sequence, @p first+n
993 * Performs the assignment @c *i = @p gen() for each @c i in the range
994 * @p [first,first+n).
996 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
998 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
1000 // concept requirements
1001 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1002 // "the type returned by a _Generator"
1003 __typeof__(__gen())>)
1005 for ( ; __n
> 0; --__n
, ++__first
)
1011 * @brief Copy a sequence, removing elements of a given value.
1012 * @param first An input iterator.
1013 * @param last An input iterator.
1014 * @param result An output iterator.
1015 * @param value The value to be removed.
1016 * @return An iterator designating the end of the resulting sequence.
1018 * Copies each element in the range @p [first,last) not equal to @p value
1019 * to the range beginning at @p result.
1020 * remove_copy() is stable, so the relative order of elements that are
1021 * copied is unchanged.
1023 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
1025 remove_copy(_InputIterator __first
, _InputIterator __last
,
1026 _OutputIterator __result
, const _Tp
& __value
)
1028 // concept requirements
1029 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1030 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1031 typename iterator_traits
<_InputIterator
>::value_type
>)
1032 __glibcxx_function_requires(_EqualOpConcept
<
1033 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
1034 __glibcxx_requires_valid_range(__first
, __last
);
1036 for ( ; __first
!= __last
; ++__first
)
1037 if (!(*__first
== __value
))
1039 *__result
= *__first
;
1046 * @brief Copy a sequence, removing elements for which a predicate is true.
1047 * @param first An input iterator.
1048 * @param last An input iterator.
1049 * @param result An output iterator.
1050 * @param pred A predicate.
1051 * @return An iterator designating the end of the resulting sequence.
1053 * Copies each element in the range @p [first,last) for which
1054 * @p pred returns true to the range beginning at @p result.
1056 * remove_copy_if() is stable, so the relative order of elements that are
1057 * copied is unchanged.
1059 template<typename _InputIterator
, typename _OutputIterator
,
1060 typename _Predicate
>
1062 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
1063 _OutputIterator __result
, _Predicate __pred
)
1065 // concept requirements
1066 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1067 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1068 typename iterator_traits
<_InputIterator
>::value_type
>)
1069 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1070 typename iterator_traits
<_InputIterator
>::value_type
>)
1071 __glibcxx_requires_valid_range(__first
, __last
);
1073 for ( ; __first
!= __last
; ++__first
)
1074 if (!__pred(*__first
))
1076 *__result
= *__first
;
1083 * @brief Remove elements from a sequence.
1084 * @param first An input iterator.
1085 * @param last An input iterator.
1086 * @param value The value to be removed.
1087 * @return An iterator designating the end of the resulting sequence.
1089 * All elements equal to @p value are removed from the range
1092 * remove() is stable, so the relative order of elements that are
1093 * not removed is unchanged.
1095 * Elements between the end of the resulting sequence and @p last
1096 * are still present, but their value is unspecified.
1098 template<typename _ForwardIterator
, typename _Tp
>
1100 remove(_ForwardIterator __first
, _ForwardIterator __last
,
1103 // concept requirements
1104 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1106 __glibcxx_function_requires(_EqualOpConcept
<
1107 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1108 __glibcxx_requires_valid_range(__first
, __last
);
1110 __first
= std::find(__first
, __last
, __value
);
1111 _ForwardIterator __i
= __first
;
1112 return __first
== __last
? __first
1113 : std::remove_copy(++__i
, __last
,
1118 * @brief Remove elements from a sequence using a predicate.
1119 * @param first A forward iterator.
1120 * @param last A forward iterator.
1121 * @param pred A predicate.
1122 * @return An iterator designating the end of the resulting sequence.
1124 * All elements for which @p pred returns true are removed from the range
1127 * remove_if() is stable, so the relative order of elements that are
1128 * not removed is unchanged.
1130 * Elements between the end of the resulting sequence and @p last
1131 * are still present, but their value is unspecified.
1133 template<typename _ForwardIterator
, typename _Predicate
>
1135 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1138 // concept requirements
1139 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1141 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1142 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1143 __glibcxx_requires_valid_range(__first
, __last
);
1145 __first
= std::find_if(__first
, __last
, __pred
);
1146 _ForwardIterator __i
= __first
;
1147 return __first
== __last
? __first
1148 : std::remove_copy_if(++__i
, __last
,
1154 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1156 * overloaded for output iterators.
1159 template<typename _InputIterator
, typename _OutputIterator
>
1161 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1162 _OutputIterator __result
,
1163 output_iterator_tag
)
1165 // concept requirements -- taken care of in dispatching function
1166 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1167 *__result
= __value
;
1168 while (++__first
!= __last
)
1169 if (!(__value
== *__first
))
1172 *++__result
= __value
;
1179 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1181 * overloaded for forward iterators.
1184 template<typename _InputIterator
, typename _ForwardIterator
>
1186 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1187 _ForwardIterator __result
,
1188 forward_iterator_tag
)
1190 // concept requirements -- taken care of in dispatching function
1191 *__result
= *__first
;
1192 while (++__first
!= __last
)
1193 if (!(*__result
== *__first
))
1194 *++__result
= *__first
;
1200 * This is an uglified
1201 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1203 * overloaded for output iterators.
1206 template<typename _InputIterator
, typename _OutputIterator
,
1207 typename _BinaryPredicate
>
1209 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1210 _OutputIterator __result
,
1211 _BinaryPredicate __binary_pred
,
1212 output_iterator_tag
)
1214 // concept requirements -- iterators already checked
1215 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1216 typename iterator_traits
<_InputIterator
>::value_type
,
1217 typename iterator_traits
<_InputIterator
>::value_type
>)
1219 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1220 *__result
= __value
;
1221 while (++__first
!= __last
)
1222 if (!__binary_pred(__value
, *__first
))
1225 *++__result
= __value
;
1232 * This is an uglified
1233 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1235 * overloaded for forward iterators.
1238 template<typename _InputIterator
, typename _ForwardIterator
,
1239 typename _BinaryPredicate
>
1241 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1242 _ForwardIterator __result
,
1243 _BinaryPredicate __binary_pred
,
1244 forward_iterator_tag
)
1246 // concept requirements -- iterators already checked
1247 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1248 typename iterator_traits
<_ForwardIterator
>::value_type
,
1249 typename iterator_traits
<_InputIterator
>::value_type
>)
1251 *__result
= *__first
;
1252 while (++__first
!= __last
)
1253 if (!__binary_pred(*__result
, *__first
)) *++__result
= *__first
;
1258 * @brief Copy a sequence, removing consecutive duplicate values.
1259 * @param first An input iterator.
1260 * @param last An input iterator.
1261 * @param result An output iterator.
1262 * @return An iterator designating the end of the resulting sequence.
1264 * Copies each element in the range @p [first,last) to the range
1265 * beginning at @p result, except that only the first element is copied
1266 * from groups of consecutive elements that compare equal.
1267 * unique_copy() is stable, so the relative order of elements that are
1268 * copied is unchanged.
1270 template<typename _InputIterator
, typename _OutputIterator
>
1271 inline _OutputIterator
1272 unique_copy(_InputIterator __first
, _InputIterator __last
,
1273 _OutputIterator __result
)
1275 // concept requirements
1276 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1277 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1278 typename iterator_traits
<_InputIterator
>::value_type
>)
1279 __glibcxx_function_requires(_EqualityComparableConcept
<
1280 typename iterator_traits
<_InputIterator
>::value_type
>)
1281 __glibcxx_requires_valid_range(__first
, __last
);
1283 typedef typename iterator_traits
<_OutputIterator
>::iterator_category
1286 if (__first
== __last
) return __result
;
1287 return std::__unique_copy(__first
, __last
, __result
, _IterType());
1291 * @brief Copy a sequence, removing consecutive values using a predicate.
1292 * @param first An input iterator.
1293 * @param last An input iterator.
1294 * @param result An output iterator.
1295 * @param binary_pred A binary predicate.
1296 * @return An iterator designating the end of the resulting sequence.
1298 * Copies each element in the range @p [first,last) to the range
1299 * beginning at @p result, except that only the first element is copied
1300 * from groups of consecutive elements for which @p binary_pred returns
1302 * unique_copy() is stable, so the relative order of elements that are
1303 * copied is unchanged.
1305 template<typename _InputIterator
, typename _OutputIterator
,
1306 typename _BinaryPredicate
>
1307 inline _OutputIterator
1308 unique_copy(_InputIterator __first
, _InputIterator __last
,
1309 _OutputIterator __result
,
1310 _BinaryPredicate __binary_pred
)
1312 // concept requirements -- predicates checked later
1313 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1314 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1315 typename iterator_traits
<_InputIterator
>::value_type
>)
1316 __glibcxx_requires_valid_range(__first
, __last
);
1318 typedef typename iterator_traits
<_OutputIterator
>::iterator_category
1321 if (__first
== __last
) return __result
;
1322 return std::__unique_copy(__first
, __last
, __result
,
1323 __binary_pred
, _IterType());
1327 * @brief Remove consecutive duplicate values from a sequence.
1328 * @param first A forward iterator.
1329 * @param last A forward iterator.
1330 * @return An iterator designating the end of the resulting sequence.
1332 * Removes all but the first element from each group of consecutive
1333 * values that compare equal.
1334 * unique() is stable, so the relative order of elements that are
1335 * not removed is unchanged.
1336 * Elements between the end of the resulting sequence and @p last
1337 * are still present, but their value is unspecified.
1339 template<typename _ForwardIterator
>
1341 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1343 // concept requirements
1344 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1346 __glibcxx_function_requires(_EqualityComparableConcept
<
1347 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1348 __glibcxx_requires_valid_range(__first
, __last
);
1350 // Skip the beginning, if already unique.
1351 __first
= std::adjacent_find(__first
, __last
);
1352 if (__first
== __last
)
1355 // Do the real copy work.
1356 _ForwardIterator __dest
= __first
;
1358 while (++__first
!= __last
)
1359 if (!(*__dest
== *__first
))
1360 *++__dest
= *__first
;
1365 * @brief Remove consecutive values from a sequence using a predicate.
1366 * @param first A forward iterator.
1367 * @param last A forward iterator.
1368 * @param binary_pred A binary predicate.
1369 * @return An iterator designating the end of the resulting sequence.
1371 * Removes all but the first element from each group of consecutive
1372 * values for which @p binary_pred returns true.
1373 * unique() is stable, so the relative order of elements that are
1374 * not removed is unchanged.
1375 * Elements between the end of the resulting sequence and @p last
1376 * are still present, but their value is unspecified.
1378 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1380 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1381 _BinaryPredicate __binary_pred
)
1383 // concept requirements
1384 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1386 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1387 typename iterator_traits
<_ForwardIterator
>::value_type
,
1388 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1389 __glibcxx_requires_valid_range(__first
, __last
);
1391 // Skip the beginning, if already unique.
1392 __first
= std::adjacent_find(__first
, __last
, __binary_pred
);
1393 if (__first
== __last
)
1396 // Do the real copy work.
1397 _ForwardIterator __dest
= __first
;
1399 while (++__first
!= __last
)
1400 if (!__binary_pred(*__dest
, *__first
))
1401 *++__dest
= *__first
;
1407 * This is an uglified reverse(_BidirectionalIterator,
1408 * _BidirectionalIterator)
1409 * overloaded for bidirectional iterators.
1412 template<typename _BidirectionalIterator
>
1414 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1415 bidirectional_iterator_tag
)
1418 if (__first
== __last
|| __first
== --__last
)
1421 std::iter_swap(__first
++, __last
);
1426 * This is an uglified reverse(_BidirectionalIterator,
1427 * _BidirectionalIterator)
1428 * overloaded for bidirectional iterators.
1431 template<typename _RandomAccessIterator
>
1433 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1434 random_access_iterator_tag
)
1436 while (__first
< __last
)
1437 std::iter_swap(__first
++, --__last
);
1441 * @brief Reverse a sequence.
1442 * @param first A bidirectional iterator.
1443 * @param last A bidirectional iterator.
1444 * @return reverse() returns no value.
1446 * Reverses the order of the elements in the range @p [first,last),
1447 * so that the first element becomes the last etc.
1448 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1449 * swaps @p *(first+i) and @p *(last-(i+1))
1451 template<typename _BidirectionalIterator
>
1453 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1455 // concept requirements
1456 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1457 _BidirectionalIterator
>)
1458 __glibcxx_requires_valid_range(__first
, __last
);
1459 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1463 * @brief Copy a sequence, reversing its elements.
1464 * @param first A bidirectional iterator.
1465 * @param last A bidirectional iterator.
1466 * @param result An output iterator.
1467 * @return An iterator designating the end of the resulting sequence.
1469 * Copies the elements in the range @p [first,last) to the range
1470 * @p [result,result+(last-first)) such that the order of the
1471 * elements is reversed.
1472 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1473 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1474 * The ranges @p [first,last) and @p [result,result+(last-first))
1477 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1479 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1480 _OutputIterator __result
)
1482 // concept requirements
1483 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1484 _BidirectionalIterator
>)
1485 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1486 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1487 __glibcxx_requires_valid_range(__first
, __last
);
1489 while (__first
!= __last
)
1492 *__result
= *__last
;
1501 * This is a helper function for the rotate algorithm specialized on RAIs.
1502 * It returns the greatest common divisor of two integer values.
1505 template<typename _EuclideanRingElement
>
1506 _EuclideanRingElement
1507 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1511 _EuclideanRingElement __t
= __m
% __n
;
1520 * This is a helper function for the rotate algorithm.
1523 template<typename _ForwardIterator
>
1525 __rotate(_ForwardIterator __first
,
1526 _ForwardIterator __middle
,
1527 _ForwardIterator __last
,
1528 forward_iterator_tag
)
1530 if ((__first
== __middle
) || (__last
== __middle
))
1533 _ForwardIterator __first2
= __middle
;
1536 swap(*__first
++, *__first2
++);
1537 if (__first
== __middle
)
1538 __middle
= __first2
;
1540 while (__first2
!= __last
);
1542 __first2
= __middle
;
1544 while (__first2
!= __last
)
1546 swap(*__first
++, *__first2
++);
1547 if (__first
== __middle
)
1548 __middle
= __first2
;
1549 else if (__first2
== __last
)
1550 __first2
= __middle
;
1556 * This is a helper function for the rotate algorithm.
1559 template<typename _BidirectionalIterator
>
1561 __rotate(_BidirectionalIterator __first
,
1562 _BidirectionalIterator __middle
,
1563 _BidirectionalIterator __last
,
1564 bidirectional_iterator_tag
)
1566 // concept requirements
1567 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1568 _BidirectionalIterator
>)
1570 if ((__first
== __middle
) || (__last
== __middle
))
1573 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1574 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1576 while (__first
!= __middle
&& __middle
!= __last
)
1577 swap(*__first
++, *--__last
);
1579 if (__first
== __middle
)
1580 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1582 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1587 * This is a helper function for the rotate algorithm.
1590 template<typename _RandomAccessIterator
>
1592 __rotate(_RandomAccessIterator __first
,
1593 _RandomAccessIterator __middle
,
1594 _RandomAccessIterator __last
,
1595 random_access_iterator_tag
)
1597 // concept requirements
1598 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1599 _RandomAccessIterator
>)
1601 if ((__first
== __middle
) || (__last
== __middle
))
1604 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1606 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1609 const _Distance __n
= __last
- __first
;
1610 const _Distance __k
= __middle
- __first
;
1611 const _Distance __l
= __n
- __k
;
1615 std::swap_ranges(__first
, __middle
, __middle
);
1619 const _Distance __d
= __gcd(__n
, __k
);
1621 for (_Distance __i
= 0; __i
< __d
; __i
++)
1623 const _ValueType __tmp
= *__first
;
1624 _RandomAccessIterator __p
= __first
;
1628 for (_Distance __j
= 0; __j
< __l
/ __d
; __j
++)
1630 if (__p
> __first
+ __l
)
1632 *__p
= *(__p
- __l
);
1636 *__p
= *(__p
+ __k
);
1642 for (_Distance __j
= 0; __j
< __k
/ __d
- 1; __j
++)
1644 if (__p
< __last
- __k
)
1646 *__p
= *(__p
+ __k
);
1649 *__p
= * (__p
- __l
);
1660 * @brief Rotate the elements of a sequence.
1661 * @param first A forward iterator.
1662 * @param middle A forward iterator.
1663 * @param last A forward iterator.
1666 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1667 * positions so that the element at @p middle is moved to @p first, the
1668 * element at @p middle+1 is moved to @first+1 and so on for each element
1669 * in the range @p [first,last).
1671 * This effectively swaps the ranges @p [first,middle) and
1674 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1675 * each @p n in the range @p [0,last-first).
1677 template<typename _ForwardIterator
>
1679 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1680 _ForwardIterator __last
)
1682 // concept requirements
1683 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1685 __glibcxx_requires_valid_range(__first
, __middle
);
1686 __glibcxx_requires_valid_range(__middle
, __last
);
1688 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1690 std::__rotate(__first
, __middle
, __last
, _IterType());
1694 * @brief Copy a sequence, rotating its elements.
1695 * @param first A forward iterator.
1696 * @param middle A forward iterator.
1697 * @param last A forward iterator.
1698 * @param result An output iterator.
1699 * @return An iterator designating the end of the resulting sequence.
1701 * Copies the elements of the range @p [first,last) to the range
1702 * beginning at @result, rotating the copied elements by @p (middle-first)
1703 * positions so that the element at @p middle is moved to @p result, the
1704 * element at @p middle+1 is moved to @result+1 and so on for each element
1705 * in the range @p [first,last).
1707 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1708 * each @p n in the range @p [0,last-first).
1710 template<typename _ForwardIterator
, typename _OutputIterator
>
1712 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1713 _ForwardIterator __last
, _OutputIterator __result
)
1715 // concept requirements
1716 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1717 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1718 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1719 __glibcxx_requires_valid_range(__first
, __middle
);
1720 __glibcxx_requires_valid_range(__middle
, __last
);
1722 return std::copy(__first
, __middle
, copy(__middle
, __last
, __result
));
1726 * @brief Randomly shuffle the elements of a sequence.
1727 * @param first A forward iterator.
1728 * @param last A forward iterator.
1731 * Reorder the elements in the range @p [first,last) using a random
1732 * distribution, so that every possible ordering of the sequence is
1735 template<typename _RandomAccessIterator
>
1737 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
1739 // concept requirements
1740 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1741 _RandomAccessIterator
>)
1742 __glibcxx_requires_valid_range(__first
, __last
);
1744 if (__first
!= __last
)
1745 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1746 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
1750 * @brief Shuffle the elements of a sequence using a random number
1752 * @param first A forward iterator.
1753 * @param last A forward iterator.
1754 * @param rand The RNG functor or function.
1757 * Reorders the elements in the range @p [first,last) using @p rand to
1758 * provide a random distribution. Calling @p rand(N) for a positive
1759 * integer @p N should return a randomly chosen integer from the
1762 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
1764 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1765 _RandomNumberGenerator
& __rand
)
1767 // concept requirements
1768 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1769 _RandomAccessIterator
>)
1770 __glibcxx_requires_valid_range(__first
, __last
);
1772 if (__first
== __last
)
1774 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1775 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
1781 * This is a helper function...
1784 template<typename _ForwardIterator
, typename _Predicate
>
1786 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1788 forward_iterator_tag
)
1790 if (__first
== __last
)
1793 while (__pred(*__first
))
1794 if (++__first
== __last
)
1797 _ForwardIterator __next
= __first
;
1799 while (++__next
!= __last
)
1800 if (__pred(*__next
))
1802 swap(*__first
, *__next
);
1811 * This is a helper function...
1814 template<typename _BidirectionalIterator
, typename _Predicate
>
1815 _BidirectionalIterator
1816 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1818 bidirectional_iterator_tag
)
1823 if (__first
== __last
)
1825 else if (__pred(*__first
))
1831 if (__first
== __last
)
1833 else if (!__pred(*__last
))
1837 std::iter_swap(__first
, __last
);
1843 * @brief Move elements for which a predicate is true to the beginning
1845 * @param first A forward iterator.
1846 * @param last A forward iterator.
1847 * @param pred A predicate functor.
1848 * @return An iterator @p middle such that @p pred(i) is true for each
1849 * iterator @p i in the range @p [first,middle) and false for each @p i
1850 * in the range @p [middle,last).
1852 * @p pred must not modify its operand. @p partition() does not preserve
1853 * the relative ordering of elements in each group, use
1854 * @p stable_partition() if this is needed.
1856 template<typename _ForwardIterator
, typename _Predicate
>
1857 inline _ForwardIterator
1858 partition(_ForwardIterator __first
, _ForwardIterator __last
,
1861 // concept requirements
1862 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1864 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1865 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1866 __glibcxx_requires_valid_range(__first
, __last
);
1868 return std::__partition(__first
, __last
, __pred
,
1869 std::__iterator_category(__first
));
1875 * This is a helper function...
1878 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1880 __inplace_stable_partition(_ForwardIterator __first
,
1881 _ForwardIterator __last
,
1882 _Predicate __pred
, _Distance __len
)
1885 return __pred(*__first
) ? __last
: __first
;
1886 _ForwardIterator __middle
= __first
;
1887 std::advance(__middle
, __len
/ 2);
1888 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1892 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1896 std::rotate(__begin
, __middle
, __end
);
1897 std::advance(__begin
, std::distance(__middle
, __end
));
1903 * This is a helper function...
1906 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1909 __stable_partition_adaptive(_ForwardIterator __first
,
1910 _ForwardIterator __last
,
1911 _Predicate __pred
, _Distance __len
,
1913 _Distance __buffer_size
)
1915 if (__len
<= __buffer_size
)
1917 _ForwardIterator __result1
= __first
;
1918 _Pointer __result2
= __buffer
;
1919 for ( ; __first
!= __last
; ++__first
)
1920 if (__pred(*__first
))
1922 *__result1
= *__first
;
1927 *__result2
= *__first
;
1930 std::copy(__buffer
, __result2
, __result1
);
1935 _ForwardIterator __middle
= __first
;
1936 std::advance(__middle
, __len
/ 2);
1937 _ForwardIterator __begin
=
1938 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1939 __len
/ 2, __buffer
,
1941 _ForwardIterator __end
=
1942 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1944 __buffer
, __buffer_size
);
1945 std::rotate(__begin
, __middle
, __end
);
1946 std::advance(__begin
, std::distance(__middle
, __end
));
1952 * @brief Move elements for which a predicate is true to the beginning
1953 * of a sequence, preserving relative ordering.
1954 * @param first A forward iterator.
1955 * @param last A forward iterator.
1956 * @param pred A predicate functor.
1957 * @return An iterator @p middle such that @p pred(i) is true for each
1958 * iterator @p i in the range @p [first,middle) and false for each @p i
1959 * in the range @p [middle,last).
1961 * Performs the same function as @p partition() with the additional
1962 * guarantee that the relative ordering of elements in each group is
1963 * preserved, so any two elements @p x and @p y in the range
1964 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1965 * relative ordering after calling @p stable_partition().
1967 template<typename _ForwardIterator
, typename _Predicate
>
1969 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1972 // concept requirements
1973 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1975 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1976 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1977 __glibcxx_requires_valid_range(__first
, __last
);
1979 if (__first
== __last
)
1983 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1985 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1988 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
1990 if (__buf
.size() > 0)
1992 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1993 _DistanceType(__buf
.requested_size()),
1994 __buf
.begin(), __buf
.size());
1997 std::__inplace_stable_partition(__first
, __last
, __pred
,
1998 _DistanceType(__buf
.requested_size()));
2004 * This is a helper function...
2007 template<typename _RandomAccessIterator
, typename _Tp
>
2008 _RandomAccessIterator
2009 __unguarded_partition(_RandomAccessIterator __first
,
2010 _RandomAccessIterator __last
, _Tp __pivot
)
2014 while (*__first
< __pivot
)
2017 while (__pivot
< *__last
)
2019 if (!(__first
< __last
))
2021 std::iter_swap(__first
, __last
);
2028 * This is a helper function...
2031 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2032 _RandomAccessIterator
2033 __unguarded_partition(_RandomAccessIterator __first
,
2034 _RandomAccessIterator __last
,
2035 _Tp __pivot
, _Compare __comp
)
2039 while (__comp(*__first
, __pivot
))
2042 while (__comp(__pivot
, *__last
))
2044 if (!(__first
< __last
))
2046 std::iter_swap(__first
, __last
);
2054 * This controls some aspect of the sort routines.
2057 enum { _S_threshold
= 16 };
2061 * This is a helper function for the sort routine.
2064 template<typename _RandomAccessIterator
, typename _Tp
>
2066 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
2068 _RandomAccessIterator __next
= __last
;
2070 while (__val
< *__next
)
2081 * This is a helper function for the sort routine.
2084 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2086 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
2089 _RandomAccessIterator __next
= __last
;
2091 while (__comp(__val
, *__next
))
2102 * This is a helper function for the sort routine.
2105 template<typename _RandomAccessIterator
>
2107 __insertion_sort(_RandomAccessIterator __first
,
2108 _RandomAccessIterator __last
)
2110 if (__first
== __last
)
2113 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2115 typename iterator_traits
<_RandomAccessIterator
>::value_type
2117 if (__val
< *__first
)
2119 std::copy_backward(__first
, __i
, __i
+ 1);
2123 std::__unguarded_linear_insert(__i
, __val
);
2129 * This is a helper function for the sort routine.
2132 template<typename _RandomAccessIterator
, typename _Compare
>
2134 __insertion_sort(_RandomAccessIterator __first
,
2135 _RandomAccessIterator __last
, _Compare __comp
)
2137 if (__first
== __last
) return;
2139 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2141 typename iterator_traits
<_RandomAccessIterator
>::value_type
2143 if (__comp(__val
, *__first
))
2145 std::copy_backward(__first
, __i
, __i
+ 1);
2149 std::__unguarded_linear_insert(__i
, __val
, __comp
);
2155 * This is a helper function for the sort routine.
2158 template<typename _RandomAccessIterator
>
2160 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2161 _RandomAccessIterator __last
)
2163 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2166 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2167 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
2172 * This is a helper function for the sort routine.
2175 template<typename _RandomAccessIterator
, typename _Compare
>
2177 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2178 _RandomAccessIterator __last
, _Compare __comp
)
2180 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2183 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2184 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
2189 * This is a helper function for the sort routine.
2192 template<typename _RandomAccessIterator
>
2194 __final_insertion_sort(_RandomAccessIterator __first
,
2195 _RandomAccessIterator __last
)
2197 if (__last
- __first
> _S_threshold
)
2199 std::__insertion_sort(__first
, __first
+ _S_threshold
);
2200 std::__unguarded_insertion_sort(__first
+ _S_threshold
, __last
);
2203 std::__insertion_sort(__first
, __last
);
2208 * This is a helper function for the sort routine.
2211 template<typename _RandomAccessIterator
, typename _Compare
>
2213 __final_insertion_sort(_RandomAccessIterator __first
,
2214 _RandomAccessIterator __last
, _Compare __comp
)
2216 if (__last
- __first
> _S_threshold
)
2218 std::__insertion_sort(__first
, __first
+ _S_threshold
, __comp
);
2219 std::__unguarded_insertion_sort(__first
+ _S_threshold
, __last
,
2223 std::__insertion_sort(__first
, __last
, __comp
);
2228 * This is a helper function for the sort routine.
2231 template<typename _Size
>
2236 for (__k
= 0; __n
!= 1; __n
>>= 1)
2242 * @brief Sort the smallest elements of a sequence.
2243 * @param first An iterator.
2244 * @param middle Another iterator.
2245 * @param last Another iterator.
2248 * Sorts the smallest @p (middle-first) elements in the range
2249 * @p [first,last) and moves them to the range @p [first,middle). The
2250 * order of the remaining elements in the range @p [middle,last) is
2252 * After the sort if @p i and @j are iterators in the range
2253 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2254 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
2256 template<typename _RandomAccessIterator
>
2258 partial_sort(_RandomAccessIterator __first
,
2259 _RandomAccessIterator __middle
,
2260 _RandomAccessIterator __last
)
2262 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2265 // concept requirements
2266 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2267 _RandomAccessIterator
>)
2268 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2269 __glibcxx_requires_valid_range(__first
, __middle
);
2270 __glibcxx_requires_valid_range(__middle
, __last
);
2272 std::make_heap(__first
, __middle
);
2273 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
2274 if (*__i
< *__first
)
2275 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
));
2276 std::sort_heap(__first
, __middle
);
2280 * @brief Sort the smallest elements of a sequence using a predicate
2282 * @param first An iterator.
2283 * @param middle Another iterator.
2284 * @param last Another iterator.
2285 * @param comp A comparison functor.
2288 * Sorts the smallest @p (middle-first) elements in the range
2289 * @p [first,last) and moves them to the range @p [first,middle). The
2290 * order of the remaining elements in the range @p [middle,last) is
2292 * After the sort if @p i and @j are iterators in the range
2293 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2294 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
2297 template<typename _RandomAccessIterator
, typename _Compare
>
2299 partial_sort(_RandomAccessIterator __first
,
2300 _RandomAccessIterator __middle
,
2301 _RandomAccessIterator __last
,
2304 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2307 // concept requirements
2308 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2309 _RandomAccessIterator
>)
2310 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2311 _ValueType
, _ValueType
>)
2312 __glibcxx_requires_valid_range(__first
, __middle
);
2313 __glibcxx_requires_valid_range(__middle
, __last
);
2315 std::make_heap(__first
, __middle
, __comp
);
2316 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
2317 if (__comp(*__i
, *__first
))
2318 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
), __comp
);
2319 std::sort_heap(__first
, __middle
, __comp
);
2323 * @brief Copy the smallest elements of a sequence.
2324 * @param first An iterator.
2325 * @param last Another iterator.
2326 * @param result_first A random-access iterator.
2327 * @param result_last Another random-access iterator.
2328 * @return An iterator indicating the end of the resulting sequence.
2330 * Copies and sorts the smallest N values from the range @p [first,last)
2331 * to the range beginning at @p result_first, where the number of
2332 * elements to be copied, @p N, is the smaller of @p (last-first) and
2333 * @p (result_last-result_first).
2334 * After the sort if @p i and @j are iterators in the range
2335 * @p [result_first,result_first+N) such that @i precedes @j then
2336 * @p *j<*i is false.
2337 * The value returned is @p result_first+N.
2339 template<typename _InputIterator
, typename _RandomAccessIterator
>
2340 _RandomAccessIterator
2341 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2342 _RandomAccessIterator __result_first
,
2343 _RandomAccessIterator __result_last
)
2345 typedef typename iterator_traits
<_InputIterator
>::value_type
2347 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2349 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2352 // concept requirements
2353 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2354 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2356 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
2357 __glibcxx_function_requires(_LessThanComparableConcept
<_InputValueType
>)
2358 __glibcxx_requires_valid_range(__first
, __last
);
2359 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2361 if (__result_first
== __result_last
)
2362 return __result_last
;
2363 _RandomAccessIterator __result_real_last
= __result_first
;
2364 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2366 *__result_real_last
= *__first
;
2367 ++__result_real_last
;
2370 std::make_heap(__result_first
, __result_real_last
);
2371 while (__first
!= __last
)
2373 if (*__first
< *__result_first
)
2374 std::__adjust_heap(__result_first
, _DistanceType(0),
2375 _DistanceType(__result_real_last
2377 _InputValueType(*__first
));
2380 std::sort_heap(__result_first
, __result_real_last
);
2381 return __result_real_last
;
2385 * @brief Copy the smallest elements of a sequence using a predicate for
2387 * @param first An input iterator.
2388 * @param last Another input iterator.
2389 * @param result_first A random-access iterator.
2390 * @param result_last Another random-access iterator.
2391 * @param comp A comparison functor.
2392 * @return An iterator indicating the end of the resulting sequence.
2394 * Copies and sorts the smallest N values from the range @p [first,last)
2395 * to the range beginning at @p result_first, where the number of
2396 * elements to be copied, @p N, is the smaller of @p (last-first) and
2397 * @p (result_last-result_first).
2398 * After the sort if @p i and @j are iterators in the range
2399 * @p [result_first,result_first+N) such that @i precedes @j then
2400 * @p comp(*j,*i) is false.
2401 * The value returned is @p result_first+N.
2403 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
2404 _RandomAccessIterator
2405 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2406 _RandomAccessIterator __result_first
,
2407 _RandomAccessIterator __result_last
,
2410 typedef typename iterator_traits
<_InputIterator
>::value_type
2412 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2414 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2417 // concept requirements
2418 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2419 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2420 _RandomAccessIterator
>)
2421 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2423 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2424 _OutputValueType
, _OutputValueType
>)
2425 __glibcxx_requires_valid_range(__first
, __last
);
2426 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2428 if (__result_first
== __result_last
)
2429 return __result_last
;
2430 _RandomAccessIterator __result_real_last
= __result_first
;
2431 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2433 *__result_real_last
= *__first
;
2434 ++__result_real_last
;
2437 std::make_heap(__result_first
, __result_real_last
, __comp
);
2438 while (__first
!= __last
)
2440 if (__comp(*__first
, *__result_first
))
2441 std::__adjust_heap(__result_first
, _DistanceType(0),
2442 _DistanceType(__result_real_last
2444 _InputValueType(*__first
),
2448 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2449 return __result_real_last
;
2454 * This is a helper function for the sort routine.
2457 template<typename _RandomAccessIterator
, typename _Size
>
2459 __introsort_loop(_RandomAccessIterator __first
,
2460 _RandomAccessIterator __last
,
2461 _Size __depth_limit
)
2463 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2466 while (__last
- __first
> _S_threshold
)
2468 if (__depth_limit
== 0)
2470 std::partial_sort(__first
, __last
, __last
);
2474 _RandomAccessIterator __cut
=
2475 std::__unguarded_partition(__first
, __last
,
2476 _ValueType(std::__median(*__first
,
2483 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2490 * This is a helper function for the sort routine.
2493 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2495 __introsort_loop(_RandomAccessIterator __first
,
2496 _RandomAccessIterator __last
,
2497 _Size __depth_limit
, _Compare __comp
)
2499 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2502 while (__last
- __first
> _S_threshold
)
2504 if (__depth_limit
== 0)
2506 std::partial_sort(__first
, __last
, __last
, __comp
);
2510 _RandomAccessIterator __cut
=
2511 std::__unguarded_partition(__first
, __last
,
2512 _ValueType(std::__median(*__first
,
2520 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2526 * @brief Sort the elements of a sequence.
2527 * @param first An iterator.
2528 * @param last Another iterator.
2531 * Sorts the elements in the range @p [first,last) in ascending order,
2532 * such that @p *(i+1)<*i is false for each iterator @p i in the range
2533 * @p [first,last-1).
2535 * The relative ordering of equivalent elements is not preserved, use
2536 * @p stable_sort() if this is needed.
2538 template<typename _RandomAccessIterator
>
2540 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
2542 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2545 // concept requirements
2546 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2547 _RandomAccessIterator
>)
2548 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2549 __glibcxx_requires_valid_range(__first
, __last
);
2551 if (__first
!= __last
)
2553 std::__introsort_loop(__first
, __last
, __lg(__last
- __first
) * 2);
2554 std::__final_insertion_sort(__first
, __last
);
2559 * @brief Sort the elements of a sequence using a predicate for comparison.
2560 * @param first An iterator.
2561 * @param last Another iterator.
2562 * @param comp A comparison functor.
2565 * Sorts the elements in the range @p [first,last) in ascending order,
2566 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
2567 * range @p [first,last-1).
2569 * The relative ordering of equivalent elements is not preserved, use
2570 * @p stable_sort() if this is needed.
2572 template<typename _RandomAccessIterator
, typename _Compare
>
2574 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2577 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2580 // concept requirements
2581 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2582 _RandomAccessIterator
>)
2583 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
2585 __glibcxx_requires_valid_range(__first
, __last
);
2587 if (__first
!= __last
)
2589 std::__introsort_loop(__first
, __last
, __lg(__last
- __first
) * 2,
2591 std::__final_insertion_sort(__first
, __last
, __comp
);
2596 * @brief Finds the first position in which @a val could be inserted
2597 * without changing the ordering.
2598 * @param first An iterator.
2599 * @param last Another iterator.
2600 * @param val The search term.
2601 * @return An iterator pointing to the first element "not less than" @a val,
2602 * or end() if every element is less than @a val.
2603 * @ingroup binarysearch
2605 template<typename _ForwardIterator
, typename _Tp
>
2607 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2610 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2612 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2615 // concept requirements
2616 // Note that these are slightly stricter than those of the 4-argument
2617 // version, defined next. The difference is in the strictness of the
2618 // comparison operations... so for looser checking, define your own
2619 // comparison function, as was intended.
2620 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2621 __glibcxx_function_requires(_SameTypeConcept
<_Tp
, _ValueType
>)
2622 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
2623 __glibcxx_requires_partitioned(__first
, __last
, __val
);
2625 _DistanceType __len
= std::distance(__first
, __last
);
2626 _DistanceType __half
;
2627 _ForwardIterator __middle
;
2631 __half
= __len
>> 1;
2633 std::advance(__middle
, __half
);
2634 if (*__middle
< __val
)
2638 __len
= __len
- __half
- 1;
2647 * @brief Finds the first position in which @a val could be inserted
2648 * without changing the ordering.
2649 * @param first An iterator.
2650 * @param last Another iterator.
2651 * @param val The search term.
2652 * @param comp A functor to use for comparisons.
2653 * @return An iterator pointing to the first element "not less than" @a val,
2654 * or end() if every element is less than @a val.
2655 * @ingroup binarysearch
2657 * The comparison function should have the same effects on ordering as
2658 * the function used for the initial sort.
2660 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2662 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2663 const _Tp
& __val
, _Compare __comp
)
2665 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2667 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2670 // concept requirements
2671 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2672 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2674 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
2676 _DistanceType __len
= std::distance(__first
, __last
);
2677 _DistanceType __half
;
2678 _ForwardIterator __middle
;
2682 __half
= __len
>> 1;
2684 std::advance(__middle
, __half
);
2685 if (__comp(*__middle
, __val
))
2689 __len
= __len
- __half
- 1;
2698 * @brief Finds the last position in which @a val could be inserted
2699 * without changing the ordering.
2700 * @param first An iterator.
2701 * @param last Another iterator.
2702 * @param val The search term.
2703 * @return An iterator pointing to the first element greater than @a val,
2704 * or end() if no elements are greater than @a val.
2705 * @ingroup binarysearch
2707 template<typename _ForwardIterator
, typename _Tp
>
2709 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2712 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2714 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2717 // concept requirements
2718 // See comments on lower_bound.
2719 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2720 __glibcxx_function_requires(_SameTypeConcept
<_Tp
, _ValueType
>)
2721 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
2722 __glibcxx_requires_partitioned(__first
, __last
, __val
);
2724 _DistanceType __len
= std::distance(__first
, __last
);
2725 _DistanceType __half
;
2726 _ForwardIterator __middle
;
2730 __half
= __len
>> 1;
2732 std::advance(__middle
, __half
);
2733 if (__val
< *__middle
)
2739 __len
= __len
- __half
- 1;
2746 * @brief Finds the last position in which @a val could be inserted
2747 * without changing the ordering.
2748 * @param first An iterator.
2749 * @param last Another iterator.
2750 * @param val The search term.
2751 * @param comp A functor to use for comparisons.
2752 * @return An iterator pointing to the first element greater than @a val,
2753 * or end() if no elements are greater than @a val.
2754 * @ingroup binarysearch
2756 * The comparison function should have the same effects on ordering as
2757 * the function used for the initial sort.
2759 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2761 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2762 const _Tp
& __val
, _Compare __comp
)
2764 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2766 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2769 // concept requirements
2770 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2771 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2773 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
2775 _DistanceType __len
= std::distance(__first
, __last
);
2776 _DistanceType __half
;
2777 _ForwardIterator __middle
;
2781 __half
= __len
>> 1;
2783 std::advance(__middle
, __half
);
2784 if (__comp(__val
, *__middle
))
2790 __len
= __len
- __half
- 1;
2798 * This is a helper function for the merge routines.
2801 template<typename _BidirectionalIterator
, typename _Distance
>
2803 __merge_without_buffer(_BidirectionalIterator __first
,
2804 _BidirectionalIterator __middle
,
2805 _BidirectionalIterator __last
,
2806 _Distance __len1
, _Distance __len2
)
2808 if (__len1
== 0 || __len2
== 0)
2810 if (__len1
+ __len2
== 2)
2812 if (*__middle
< *__first
)
2813 std::iter_swap(__first
, __middle
);
2816 _BidirectionalIterator __first_cut
= __first
;
2817 _BidirectionalIterator __second_cut
= __middle
;
2818 _Distance __len11
= 0;
2819 _Distance __len22
= 0;
2820 if (__len1
> __len2
)
2822 __len11
= __len1
/ 2;
2823 std::advance(__first_cut
, __len11
);
2824 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
2825 __len22
= std::distance(__middle
, __second_cut
);
2829 __len22
= __len2
/ 2;
2830 std::advance(__second_cut
, __len22
);
2831 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
2832 __len11
= std::distance(__first
, __first_cut
);
2834 std::rotate(__first_cut
, __middle
, __second_cut
);
2835 _BidirectionalIterator __new_middle
= __first_cut
;
2836 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2837 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2839 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2840 __len1
- __len11
, __len2
- __len22
);
2845 * This is a helper function for the merge routines.
2848 template<typename _BidirectionalIterator
, typename _Distance
,
2851 __merge_without_buffer(_BidirectionalIterator __first
,
2852 _BidirectionalIterator __middle
,
2853 _BidirectionalIterator __last
,
2854 _Distance __len1
, _Distance __len2
,
2857 if (__len1
== 0 || __len2
== 0)
2859 if (__len1
+ __len2
== 2)
2861 if (__comp(*__middle
, *__first
))
2862 std::iter_swap(__first
, __middle
);
2865 _BidirectionalIterator __first_cut
= __first
;
2866 _BidirectionalIterator __second_cut
= __middle
;
2867 _Distance __len11
= 0;
2868 _Distance __len22
= 0;
2869 if (__len1
> __len2
)
2871 __len11
= __len1
/ 2;
2872 std::advance(__first_cut
, __len11
);
2873 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2875 __len22
= std::distance(__middle
, __second_cut
);
2879 __len22
= __len2
/ 2;
2880 std::advance(__second_cut
, __len22
);
2881 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2883 __len11
= std::distance(__first
, __first_cut
);
2885 std::rotate(__first_cut
, __middle
, __second_cut
);
2886 _BidirectionalIterator __new_middle
= __first_cut
;
2887 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2888 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2889 __len11
, __len22
, __comp
);
2890 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2891 __len1
- __len11
, __len2
- __len22
, __comp
);
2896 * This is a helper function for the stable sorting routines.
2899 template<typename _RandomAccessIterator
>
2901 __inplace_stable_sort(_RandomAccessIterator __first
,
2902 _RandomAccessIterator __last
)
2904 if (__last
- __first
< 15)
2906 std::__insertion_sort(__first
, __last
);
2909 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2910 std::__inplace_stable_sort(__first
, __middle
);
2911 std::__inplace_stable_sort(__middle
, __last
);
2912 std::__merge_without_buffer(__first
, __middle
, __last
,
2919 * This is a helper function for the stable sorting routines.
2922 template<typename _RandomAccessIterator
, typename _Compare
>
2924 __inplace_stable_sort(_RandomAccessIterator __first
,
2925 _RandomAccessIterator __last
, _Compare __comp
)
2927 if (__last
- __first
< 15)
2929 std::__insertion_sort(__first
, __last
, __comp
);
2932 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2933 std::__inplace_stable_sort(__first
, __middle
, __comp
);
2934 std::__inplace_stable_sort(__middle
, __last
, __comp
);
2935 std::__merge_without_buffer(__first
, __middle
, __last
,
2942 * @brief Merges two sorted ranges.
2943 * @param first1 An iterator.
2944 * @param first2 Another iterator.
2945 * @param last1 Another iterator.
2946 * @param last2 Another iterator.
2947 * @param result An iterator pointing to the end of the merged range.
2948 * @return An iterator pointing to the first element "not less than" @a val.
2950 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
2951 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
2952 * must be sorted, and the output range must not overlap with either of
2953 * the input ranges. The sort is @e stable, that is, for equivalent
2954 * elements in the two ranges, elements from the first range will always
2955 * come before elements from the second.
2957 template<typename _InputIterator1
, typename _InputIterator2
,
2958 typename _OutputIterator
>
2960 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
2961 _InputIterator2 __first2
, _InputIterator2 __last2
,
2962 _OutputIterator __result
)
2964 // concept requirements
2965 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2966 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2967 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
2968 typename iterator_traits
<_InputIterator1
>::value_type
>)
2969 __glibcxx_function_requires(_SameTypeConcept
<
2970 typename iterator_traits
<_InputIterator1
>::value_type
,
2971 typename iterator_traits
<_InputIterator2
>::value_type
>)
2972 __glibcxx_function_requires(_LessThanComparableConcept
<
2973 typename iterator_traits
<_InputIterator1
>::value_type
>)
2974 __glibcxx_requires_sorted(__first1
, __last1
);
2975 __glibcxx_requires_sorted(__first2
, __last2
);
2977 while (__first1
!= __last1
&& __first2
!= __last2
)
2979 if (*__first2
< *__first1
)
2981 *__result
= *__first2
;
2986 *__result
= *__first1
;
2991 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
2996 * @brief Merges two sorted ranges.
2997 * @param first1 An iterator.
2998 * @param first2 Another iterator.
2999 * @param last1 Another iterator.
3000 * @param last2 Another iterator.
3001 * @param result An iterator pointing to the end of the merged range.
3002 * @param comp A functor to use for comparisons.
3003 * @return An iterator pointing to the first element "not less than" @a val.
3005 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
3006 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
3007 * must be sorted, and the output range must not overlap with either of
3008 * the input ranges. The sort is @e stable, that is, for equivalent
3009 * elements in the two ranges, elements from the first range will always
3010 * come before elements from the second.
3012 * The comparison function should have the same effects on ordering as
3013 * the function used for the initial sort.
3015 template<typename _InputIterator1
, typename _InputIterator2
,
3016 typename _OutputIterator
, typename _Compare
>
3018 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3019 _InputIterator2 __first2
, _InputIterator2 __last2
,
3020 _OutputIterator __result
, _Compare __comp
)
3022 // concept requirements
3023 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3024 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3025 __glibcxx_function_requires(_SameTypeConcept
<
3026 typename iterator_traits
<_InputIterator1
>::value_type
,
3027 typename iterator_traits
<_InputIterator2
>::value_type
>)
3028 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3029 typename iterator_traits
<_InputIterator1
>::value_type
>)
3030 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3031 typename iterator_traits
<_InputIterator1
>::value_type
,
3032 typename iterator_traits
<_InputIterator2
>::value_type
>)
3033 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
3034 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
3036 while (__first1
!= __last1
&& __first2
!= __last2
)
3038 if (__comp(*__first2
, *__first1
))
3040 *__result
= *__first2
;
3045 *__result
= *__first1
;
3050 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
3054 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3057 __merge_sort_loop(_RandomAccessIterator1 __first
,
3058 _RandomAccessIterator1 __last
,
3059 _RandomAccessIterator2 __result
,
3060 _Distance __step_size
)
3062 const _Distance __two_step
= 2 * __step_size
;
3064 while (__last
- __first
>= __two_step
)
3066 __result
= std::merge(__first
, __first
+ __step_size
,
3067 __first
+ __step_size
, __first
+ __two_step
,
3069 __first
+= __two_step
;
3072 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3073 std::merge(__first
, __first
+ __step_size
, __first
+ __step_size
, __last
,
3077 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3078 typename _Distance
, typename _Compare
>
3080 __merge_sort_loop(_RandomAccessIterator1 __first
,
3081 _RandomAccessIterator1 __last
,
3082 _RandomAccessIterator2 __result
, _Distance __step_size
,
3085 const _Distance __two_step
= 2 * __step_size
;
3087 while (__last
- __first
>= __two_step
)
3089 __result
= std::merge(__first
, __first
+ __step_size
,
3090 __first
+ __step_size
, __first
+ __two_step
,
3093 __first
+= __two_step
;
3095 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3097 std::merge(__first
, __first
+ __step_size
,
3098 __first
+ __step_size
, __last
,
3103 enum { _S_chunk_size
= 7 };
3105 template<typename _RandomAccessIterator
, typename _Distance
>
3107 __chunk_insertion_sort(_RandomAccessIterator __first
,
3108 _RandomAccessIterator __last
,
3109 _Distance __chunk_size
)
3111 while (__last
- __first
>= __chunk_size
)
3113 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3114 __first
+= __chunk_size
;
3116 std::__insertion_sort(__first
, __last
);
3119 template<typename _RandomAccessIterator
, typename _Distance
, typename _Compare
>
3121 __chunk_insertion_sort(_RandomAccessIterator __first
,
3122 _RandomAccessIterator __last
,
3123 _Distance __chunk_size
, _Compare __comp
)
3125 while (__last
- __first
>= __chunk_size
)
3127 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3128 __first
+= __chunk_size
;
3130 std::__insertion_sort(__first
, __last
, __comp
);
3133 template<typename _RandomAccessIterator
, typename _Pointer
>
3135 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3136 _RandomAccessIterator __last
,
3139 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3142 const _Distance __len
= __last
- __first
;
3143 const _Pointer __buffer_last
= __buffer
+ __len
;
3145 _Distance __step_size
= _S_chunk_size
;
3146 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3148 while (__step_size
< __len
)
3150 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3152 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3157 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3159 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3160 _RandomAccessIterator __last
,
3161 _Pointer __buffer
, _Compare __comp
)
3163 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3166 const _Distance __len
= __last
- __first
;
3167 const _Pointer __buffer_last
= __buffer
+ __len
;
3169 _Distance __step_size
= _S_chunk_size
;
3170 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3172 while (__step_size
< __len
)
3174 std::__merge_sort_loop(__first
, __last
, __buffer
,
3175 __step_size
, __comp
);
3177 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3178 __step_size
, __comp
);
3185 * This is a helper function for the merge routines.
3188 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3189 typename _BidirectionalIterator3
>
3190 _BidirectionalIterator3
3191 __merge_backward(_BidirectionalIterator1 __first1
,
3192 _BidirectionalIterator1 __last1
,
3193 _BidirectionalIterator2 __first2
,
3194 _BidirectionalIterator2 __last2
,
3195 _BidirectionalIterator3 __result
)
3197 if (__first1
== __last1
)
3198 return std::copy_backward(__first2
, __last2
, __result
);
3199 if (__first2
== __last2
)
3200 return std::copy_backward(__first1
, __last1
, __result
);
3205 if (*__last2
< *__last1
)
3207 *--__result
= *__last1
;
3208 if (__first1
== __last1
)
3209 return std::copy_backward(__first2
, ++__last2
, __result
);
3214 *--__result
= *__last2
;
3215 if (__first2
== __last2
)
3216 return std::copy_backward(__first1
, ++__last1
, __result
);
3224 * This is a helper function for the merge routines.
3227 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3228 typename _BidirectionalIterator3
, typename _Compare
>
3229 _BidirectionalIterator3
3230 __merge_backward(_BidirectionalIterator1 __first1
,
3231 _BidirectionalIterator1 __last1
,
3232 _BidirectionalIterator2 __first2
,
3233 _BidirectionalIterator2 __last2
,
3234 _BidirectionalIterator3 __result
,
3237 if (__first1
== __last1
)
3238 return std::copy_backward(__first2
, __last2
, __result
);
3239 if (__first2
== __last2
)
3240 return std::copy_backward(__first1
, __last1
, __result
);
3245 if (__comp(*__last2
, *__last1
))
3247 *--__result
= *__last1
;
3248 if (__first1
== __last1
)
3249 return std::copy_backward(__first2
, ++__last2
, __result
);
3254 *--__result
= *__last2
;
3255 if (__first2
== __last2
)
3256 return std::copy_backward(__first1
, ++__last1
, __result
);
3264 * This is a helper function for the merge routines.
3267 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3269 _BidirectionalIterator1
3270 __rotate_adaptive(_BidirectionalIterator1 __first
,
3271 _BidirectionalIterator1 __middle
,
3272 _BidirectionalIterator1 __last
,
3273 _Distance __len1
, _Distance __len2
,
3274 _BidirectionalIterator2 __buffer
,
3275 _Distance __buffer_size
)
3277 _BidirectionalIterator2 __buffer_end
;
3278 if (__len1
> __len2
&& __len2
<= __buffer_size
)
3280 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3281 std::copy_backward(__first
, __middle
, __last
);
3282 return std::copy(__buffer
, __buffer_end
, __first
);
3284 else if (__len1
<= __buffer_size
)
3286 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3287 std::copy(__middle
, __last
, __first
);
3288 return std::copy_backward(__buffer
, __buffer_end
, __last
);
3292 std::rotate(__first
, __middle
, __last
);
3293 std::advance(__first
, std::distance(__middle
, __last
));
3300 * This is a helper function for the merge routines.
3303 template<typename _BidirectionalIterator
, typename _Distance
,
3306 __merge_adaptive(_BidirectionalIterator __first
,
3307 _BidirectionalIterator __middle
,
3308 _BidirectionalIterator __last
,
3309 _Distance __len1
, _Distance __len2
,
3310 _Pointer __buffer
, _Distance __buffer_size
)
3312 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
3314 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3315 std::merge(__buffer
, __buffer_end
, __middle
, __last
, __first
);
3317 else if (__len2
<= __buffer_size
)
3319 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3320 std::__merge_backward(__first
, __middle
, __buffer
,
3321 __buffer_end
, __last
);
3325 _BidirectionalIterator __first_cut
= __first
;
3326 _BidirectionalIterator __second_cut
= __middle
;
3327 _Distance __len11
= 0;
3328 _Distance __len22
= 0;
3329 if (__len1
> __len2
)
3331 __len11
= __len1
/ 2;
3332 std::advance(__first_cut
, __len11
);
3333 __second_cut
= std::lower_bound(__middle
, __last
,
3335 __len22
= std::distance(__middle
, __second_cut
);
3339 __len22
= __len2
/ 2;
3340 std::advance(__second_cut
, __len22
);
3341 __first_cut
= std::upper_bound(__first
, __middle
,
3343 __len11
= std::distance(__first
, __first_cut
);
3345 _BidirectionalIterator __new_middle
=
3346 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3347 __len1
- __len11
, __len22
, __buffer
,
3349 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3350 __len22
, __buffer
, __buffer_size
);
3351 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3353 __len2
- __len22
, __buffer
, __buffer_size
);
3359 * This is a helper function for the merge routines.
3362 template<typename _BidirectionalIterator
, typename _Distance
, typename _Pointer
,
3365 __merge_adaptive(_BidirectionalIterator __first
,
3366 _BidirectionalIterator __middle
,
3367 _BidirectionalIterator __last
,
3368 _Distance __len1
, _Distance __len2
,
3369 _Pointer __buffer
, _Distance __buffer_size
,
3372 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
3374 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3375 std::merge(__buffer
, __buffer_end
, __middle
, __last
, __first
, __comp
);
3377 else if (__len2
<= __buffer_size
)
3379 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3380 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
3385 _BidirectionalIterator __first_cut
= __first
;
3386 _BidirectionalIterator __second_cut
= __middle
;
3387 _Distance __len11
= 0;
3388 _Distance __len22
= 0;
3389 if (__len1
> __len2
)
3391 __len11
= __len1
/ 2;
3392 std::advance(__first_cut
, __len11
);
3393 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3395 __len22
= std::distance(__middle
, __second_cut
);
3399 __len22
= __len2
/ 2;
3400 std::advance(__second_cut
, __len22
);
3401 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3403 __len11
= std::distance(__first
, __first_cut
);
3405 _BidirectionalIterator __new_middle
=
3406 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3407 __len1
- __len11
, __len22
, __buffer
,
3409 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3410 __len22
, __buffer
, __buffer_size
, __comp
);
3411 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3413 __len2
- __len22
, __buffer
,
3414 __buffer_size
, __comp
);
3419 * @brief Merges two sorted ranges in place.
3420 * @param first An iterator.
3421 * @param middle Another iterator.
3422 * @param last Another iterator.
3425 * Merges two sorted and consecutive ranges, [first,middle) and
3426 * [middle,last), and puts the result in [first,last). The output will
3427 * be sorted. The sort is @e stable, that is, for equivalent
3428 * elements in the two ranges, elements from the first range will always
3429 * come before elements from the second.
3431 * If enough additional memory is available, this takes (last-first)-1
3432 * comparisons. Otherwise an NlogN algorithm is used, where N is
3433 * distance(first,last).
3435 template<typename _BidirectionalIterator
>
3437 inplace_merge(_BidirectionalIterator __first
,
3438 _BidirectionalIterator __middle
,
3439 _BidirectionalIterator __last
)
3441 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3443 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3446 // concept requirements
3447 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3448 _BidirectionalIterator
>)
3449 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3450 __glibcxx_requires_sorted(__first
, __middle
);
3451 __glibcxx_requires_sorted(__middle
, __last
);
3453 if (__first
== __middle
|| __middle
== __last
)
3456 _DistanceType __len1
= std::distance(__first
, __middle
);
3457 _DistanceType __len2
= std::distance(__middle
, __last
);
3459 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3461 if (__buf
.begin() == 0)
3462 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3464 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3465 __buf
.begin(), _DistanceType(__buf
.size()));
3469 * @brief Merges two sorted ranges in place.
3470 * @param first An iterator.
3471 * @param middle Another iterator.
3472 * @param last Another iterator.
3473 * @param comp A functor to use for comparisons.
3476 * Merges two sorted and consecutive ranges, [first,middle) and
3477 * [middle,last), and puts the result in [first,last). The output will
3478 * be sorted. The sort is @e stable, that is, for equivalent
3479 * elements in the two ranges, elements from the first range will always
3480 * come before elements from the second.
3482 * If enough additional memory is available, this takes (last-first)-1
3483 * comparisons. Otherwise an NlogN algorithm is used, where N is
3484 * distance(first,last).
3486 * The comparison function should have the same effects on ordering as
3487 * the function used for the initial sort.
3489 template<typename _BidirectionalIterator
, typename _Compare
>
3491 inplace_merge(_BidirectionalIterator __first
,
3492 _BidirectionalIterator __middle
,
3493 _BidirectionalIterator __last
,
3496 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3498 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3501 // concept requirements
3502 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3503 _BidirectionalIterator
>)
3504 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3505 _ValueType
, _ValueType
>)
3506 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3507 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3509 if (__first
== __middle
|| __middle
== __last
)
3512 const _DistanceType __len1
= std::distance(__first
, __middle
);
3513 const _DistanceType __len2
= std::distance(__middle
, __last
);
3515 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3517 if (__buf
.begin() == 0)
3518 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3521 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3522 __buf
.begin(), _DistanceType(__buf
.size()),
3526 template<typename _RandomAccessIterator
, typename _Pointer
,
3529 __stable_sort_adaptive(_RandomAccessIterator __first
,
3530 _RandomAccessIterator __last
,
3531 _Pointer __buffer
, _Distance __buffer_size
)
3533 const _Distance __len
= (__last
- __first
+ 1) / 2;
3534 const _RandomAccessIterator __middle
= __first
+ __len
;
3535 if (__len
> __buffer_size
)
3537 std::__stable_sort_adaptive(__first
, __middle
,
3538 __buffer
, __buffer_size
);
3539 std::__stable_sort_adaptive(__middle
, __last
,
3540 __buffer
, __buffer_size
);
3544 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3545 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3547 std::__merge_adaptive(__first
, __middle
, __last
,
3548 _Distance(__middle
- __first
),
3549 _Distance(__last
- __middle
),
3550 __buffer
, __buffer_size
);
3553 template<typename _RandomAccessIterator
, typename _Pointer
,
3554 typename _Distance
, typename _Compare
>
3556 __stable_sort_adaptive(_RandomAccessIterator __first
,
3557 _RandomAccessIterator __last
,
3558 _Pointer __buffer
, _Distance __buffer_size
,
3561 const _Distance __len
= (__last
- __first
+ 1) / 2;
3562 const _RandomAccessIterator __middle
= __first
+ __len
;
3563 if (__len
> __buffer_size
)
3565 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3566 __buffer_size
, __comp
);
3567 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3568 __buffer_size
, __comp
);
3572 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3573 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3575 std::__merge_adaptive(__first
, __middle
, __last
,
3576 _Distance(__middle
- __first
),
3577 _Distance(__last
- __middle
),
3578 __buffer
, __buffer_size
,
3583 * @brief Sort the elements of a sequence, preserving the relative order
3584 * of equivalent elements.
3585 * @param first An iterator.
3586 * @param last Another iterator.
3589 * Sorts the elements in the range @p [first,last) in ascending order,
3590 * such that @p *(i+1)<*i is false for each iterator @p i in the range
3591 * @p [first,last-1).
3593 * The relative ordering of equivalent elements is preserved, so any two
3594 * elements @p x and @p y in the range @p [first,last) such that
3595 * @p x<y is false and @p y<x is false will have the same relative
3596 * ordering after calling @p stable_sort().
3598 template<typename _RandomAccessIterator
>
3600 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
3602 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3604 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3607 // concept requirements
3608 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3609 _RandomAccessIterator
>)
3610 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3611 __glibcxx_requires_valid_range(__first
, __last
);
3613 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
>
3614 buf(__first
, __last
);
3615 if (buf
.begin() == 0)
3616 std::__inplace_stable_sort(__first
, __last
);
3618 std::__stable_sort_adaptive(__first
, __last
, buf
.begin(),
3619 _DistanceType(buf
.size()));
3623 * @brief Sort the elements of a sequence using a predicate for comparison,
3624 * preserving the relative order of equivalent elements.
3625 * @param first An iterator.
3626 * @param last Another iterator.
3627 * @param comp A comparison functor.
3630 * Sorts the elements in the range @p [first,last) in ascending order,
3631 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
3632 * range @p [first,last-1).
3634 * The relative ordering of equivalent elements is preserved, so any two
3635 * elements @p x and @p y in the range @p [first,last) such that
3636 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
3637 * relative ordering after calling @p stable_sort().
3639 template<typename _RandomAccessIterator
, typename _Compare
>
3641 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3644 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3646 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3649 // concept requirements
3650 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3651 _RandomAccessIterator
>)
3652 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3655 __glibcxx_requires_valid_range(__first
, __last
);
3657 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> buf(__first
, __last
);
3658 if (buf
.begin() == 0)
3659 std::__inplace_stable_sort(__first
, __last
, __comp
);
3661 std::__stable_sort_adaptive(__first
, __last
, buf
.begin(),
3662 _DistanceType(buf
.size()), __comp
);
3666 * @brief Sort a sequence just enough to find a particular position.
3667 * @param first An iterator.
3668 * @param nth Another iterator.
3669 * @param last Another iterator.
3672 * Rearranges the elements in the range @p [first,last) so that @p *nth
3673 * is the same element that would have been in that position had the
3674 * whole sequence been sorted.
3675 * whole sequence been sorted. The elements either side of @p *nth are
3676 * not completely sorted, but for any iterator @i in the range
3677 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3678 * holds that @p *j<*i is false.
3680 template<typename _RandomAccessIterator
>
3682 nth_element(_RandomAccessIterator __first
,
3683 _RandomAccessIterator __nth
,
3684 _RandomAccessIterator __last
)
3686 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3689 // concept requirements
3690 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3691 _RandomAccessIterator
>)
3692 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3693 __glibcxx_requires_valid_range(__first
, __nth
);
3694 __glibcxx_requires_valid_range(__nth
, __last
);
3696 while (__last
- __first
> 3)
3698 _RandomAccessIterator __cut
=
3699 std::__unguarded_partition(__first
, __last
,
3700 _ValueType(std::__median(*__first
,
3712 std::__insertion_sort(__first
, __last
);
3716 * @brief Sort a sequence just enough to find a particular position
3717 * using a predicate for comparison.
3718 * @param first An iterator.
3719 * @param nth Another iterator.
3720 * @param last Another iterator.
3721 * @param comp A comparison functor.
3724 * Rearranges the elements in the range @p [first,last) so that @p *nth
3725 * is the same element that would have been in that position had the
3726 * whole sequence been sorted. The elements either side of @p *nth are
3727 * not completely sorted, but for any iterator @i in the range
3728 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3729 * holds that @p comp(*j,*i) is false.
3731 template<typename _RandomAccessIterator
, typename _Compare
>
3733 nth_element(_RandomAccessIterator __first
,
3734 _RandomAccessIterator __nth
,
3735 _RandomAccessIterator __last
,
3738 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3741 // concept requirements
3742 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3743 _RandomAccessIterator
>)
3744 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3745 _ValueType
, _ValueType
>)
3746 __glibcxx_requires_valid_range(__first
, __nth
);
3747 __glibcxx_requires_valid_range(__nth
, __last
);
3749 while (__last
- __first
> 3)
3751 _RandomAccessIterator __cut
=
3752 std::__unguarded_partition(__first
, __last
,
3753 _ValueType(std::__median(*__first
,
3765 std::__insertion_sort(__first
, __last
, __comp
);
3769 * @brief Finds the largest subrange in which @a val could be inserted
3770 * at any place in it without changing the ordering.
3771 * @param first An iterator.
3772 * @param last Another iterator.
3773 * @param val The search term.
3774 * @return An pair of iterators defining the subrange.
3775 * @ingroup binarysearch
3777 * This is equivalent to
3779 * std::make_pair(lower_bound(first, last, val),
3780 * upper_bound(first, last, val))
3782 * but does not actually call those functions.
3784 template<typename _ForwardIterator
, typename _Tp
>
3785 pair
<_ForwardIterator
, _ForwardIterator
>
3786 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
3789 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3791 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3794 // concept requirements
3795 // See comments on lower_bound.
3796 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3797 __glibcxx_function_requires(_SameTypeConcept
<_Tp
, _ValueType
>)
3798 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3799 __glibcxx_requires_partitioned(__first
, __last
, __val
);
3801 _DistanceType __len
= std::distance(__first
, __last
);
3802 _DistanceType __half
;
3803 _ForwardIterator __middle
, __left
, __right
;
3807 __half
= __len
>> 1;
3809 std::advance(__middle
, __half
);
3810 if (*__middle
< __val
)
3814 __len
= __len
- __half
- 1;
3816 else if (__val
< *__middle
)
3820 __left
= std::lower_bound(__first
, __middle
, __val
);
3821 std::advance(__first
, __len
);
3822 __right
= std::upper_bound(++__middle
, __first
, __val
);
3823 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
3826 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
3830 * @brief Finds the largest subrange in which @a val could be inserted
3831 * at any place in it without changing the ordering.
3832 * @param first An iterator.
3833 * @param last Another iterator.
3834 * @param val The search term.
3835 * @param comp A functor to use for comparisons.
3836 * @return An pair of iterators defining the subrange.
3837 * @ingroup binarysearch
3839 * This is equivalent to
3841 * std::make_pair(lower_bound(first, last, val, comp),
3842 * upper_bound(first, last, val, comp))
3844 * but does not actually call those functions.
3846 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3847 pair
<_ForwardIterator
, _ForwardIterator
>
3848 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
3852 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3854 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3857 // concept requirements
3858 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3859 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3861 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3863 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
3865 _DistanceType __len
= std::distance(__first
, __last
);
3866 _DistanceType __half
;
3867 _ForwardIterator __middle
, __left
, __right
;
3871 __half
= __len
>> 1;
3873 std::advance(__middle
, __half
);
3874 if (__comp(*__middle
, __val
))
3878 __len
= __len
- __half
- 1;
3880 else if (__comp(__val
, *__middle
))
3884 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
3885 std::advance(__first
, __len
);
3886 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
3887 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
3890 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
3894 * @brief Determines whether an element exists in a range.
3895 * @param first An iterator.
3896 * @param last Another iterator.
3897 * @param val The search term.
3898 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
3899 * @ingroup binarysearch
3901 * Note that this does not actually return an iterator to @a val. For
3902 * that, use std::find or a container's specialized find member functions.
3904 template<typename _ForwardIterator
, typename _Tp
>
3906 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
3909 // concept requirements
3910 // See comments on lower_bound.
3911 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3912 __glibcxx_function_requires(_SameTypeConcept
<_Tp
,
3913 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3914 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3915 __glibcxx_requires_partitioned(__first
, __last
, __val
);
3917 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
3918 return __i
!= __last
&& !(__val
< *__i
);
3922 * @brief Determines whether an element exists in a range.
3923 * @param first An iterator.
3924 * @param last Another iterator.
3925 * @param val The search term.
3926 * @param comp A functor to use for comparisons.
3927 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
3928 * @ingroup binarysearch
3930 * Note that this does not actually return an iterator to @a val. For
3931 * that, use std::find or a container's specialized find member functions.
3933 * The comparison function should have the same effects on ordering as
3934 * the function used for the initial sort.
3936 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3938 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
3939 const _Tp
& __val
, _Compare __comp
)
3941 // concept requirements
3942 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3943 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3944 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
3945 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _Tp
,
3946 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3947 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
3949 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
3950 return __i
!= __last
&& !__comp(__val
, *__i
);
3953 // Set algorithms: includes, set_union, set_intersection, set_difference,
3954 // set_symmetric_difference. All of these algorithms have the precondition
3955 // that their input ranges are sorted and the postcondition that their output
3956 // ranges are sorted.
3959 * @brief Determines whether all elements of a sequence exists in a range.
3960 * @param first1 Start of search range.
3961 * @param last1 End of search range.
3962 * @param first2 Start of sequence
3963 * @param last2 End of sequence.
3964 * @return True if each element in [first2,last2) is contained in order
3965 * within [first1,last1). False otherwise.
3966 * @ingroup setoperations
3968 * This operation expects both [first1,last1) and [first2,last2) to be
3969 * sorted. Searches for the presence of each element in [first2,last2)
3970 * within [first1,last1). The iterators over each range only move forward,
3971 * so this is a linear algorithm. If an element in [first2,last2) is not
3972 * found before the search iterator reaches @a last2, false is returned.
3974 template<typename _InputIterator1
, typename _InputIterator2
>
3976 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3977 _InputIterator2 __first2
, _InputIterator2 __last2
)
3979 // concept requirements
3980 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3981 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3982 __glibcxx_function_requires(_SameTypeConcept
<
3983 typename iterator_traits
<_InputIterator1
>::value_type
,
3984 typename iterator_traits
<_InputIterator2
>::value_type
>)
3985 __glibcxx_function_requires(_LessThanComparableConcept
<
3986 typename iterator_traits
<_InputIterator1
>::value_type
>)
3987 __glibcxx_requires_sorted(__first1
, __last1
);
3988 __glibcxx_requires_sorted(__first2
, __last2
);
3990 while (__first1
!= __last1
&& __first2
!= __last2
)
3991 if (*__first2
< *__first1
)
3993 else if(*__first1
< *__first2
)
3996 ++__first1
, ++__first2
;
3998 return __first2
== __last2
;
4002 * @brief Determines whether all elements of a sequence exists in a range
4004 * @param first1 Start of search range.
4005 * @param last1 End of search range.
4006 * @param first2 Start of sequence
4007 * @param last2 End of sequence.
4008 * @param comp Comparison function to use.
4009 * @return True if each element in [first2,last2) is contained in order
4010 * within [first1,last1) according to comp. False otherwise.
4011 * @ingroup setoperations
4013 * This operation expects both [first1,last1) and [first2,last2) to be
4014 * sorted. Searches for the presence of each element in [first2,last2)
4015 * within [first1,last1), using comp to decide. The iterators over each
4016 * range only move forward, so this is a linear algorithm. If an element
4017 * in [first2,last2) is not found before the search iterator reaches @a
4018 * last2, false is returned.
4020 template<typename _InputIterator1
, typename _InputIterator2
,
4023 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
4024 _InputIterator2 __first2
, _InputIterator2 __last2
, _Compare __comp
)
4026 // concept requirements
4027 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4028 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4029 __glibcxx_function_requires(_SameTypeConcept
<
4030 typename iterator_traits
<_InputIterator1
>::value_type
,
4031 typename iterator_traits
<_InputIterator2
>::value_type
>)
4032 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4033 typename iterator_traits
<_InputIterator1
>::value_type
,
4034 typename iterator_traits
<_InputIterator2
>::value_type
>)
4035 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4036 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4038 while (__first1
!= __last1
&& __first2
!= __last2
)
4039 if (__comp(*__first2
, *__first1
))
4041 else if(__comp(*__first1
, *__first2
))
4044 ++__first1
, ++__first2
;
4046 return __first2
== __last2
;
4050 * @brief Return the union of two sorted ranges.
4051 * @param first1 Start of first range.
4052 * @param last1 End of first range.
4053 * @param first2 Start of second range.
4054 * @param last2 End of second range.
4055 * @return End of the output range.
4056 * @ingroup setoperations
4058 * This operation iterates over both ranges, copying elements present in
4059 * each range in order to the output range. Iterators increment for each
4060 * range. When the current element of one range is less than the other,
4061 * that element is copied and the iterator advanced. If an element is
4062 * contained in both ranges, the element from the first range is copied and
4063 * both ranges advance. The output range may not overlap either input
4066 template<typename _InputIterator1
, typename _InputIterator2
,
4067 typename _OutputIterator
>
4069 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4070 _InputIterator2 __first2
, _InputIterator2 __last2
,
4071 _OutputIterator __result
)
4073 // concept requirements
4074 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4075 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4076 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4077 typename iterator_traits
<_InputIterator1
>::value_type
>)
4078 __glibcxx_function_requires(_SameTypeConcept
<
4079 typename iterator_traits
<_InputIterator1
>::value_type
,
4080 typename iterator_traits
<_InputIterator2
>::value_type
>)
4081 __glibcxx_function_requires(_LessThanComparableConcept
<
4082 typename iterator_traits
<_InputIterator1
>::value_type
>)
4083 __glibcxx_requires_sorted(__first1
, __last1
);
4084 __glibcxx_requires_sorted(__first2
, __last2
);
4086 while (__first1
!= __last1
&& __first2
!= __last2
)
4088 if (*__first1
< *__first2
)
4090 *__result
= *__first1
;
4093 else if (*__first2
< *__first1
)
4095 *__result
= *__first2
;
4100 *__result
= *__first1
;
4106 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4111 * @brief Return the union of two sorted ranges using a comparison functor.
4112 * @param first1 Start of first range.
4113 * @param last1 End of first range.
4114 * @param first2 Start of second range.
4115 * @param last2 End of second range.
4116 * @param comp The comparison functor.
4117 * @return End of the output range.
4118 * @ingroup setoperations
4120 * This operation iterates over both ranges, copying elements present in
4121 * each range in order to the output range. Iterators increment for each
4122 * range. When the current element of one range is less than the other
4123 * according to @a comp, that element is copied and the iterator advanced.
4124 * If an equivalent element according to @a comp is contained in both
4125 * ranges, the element from the first range is copied and both ranges
4126 * advance. The output range may not overlap either input range.
4128 template<typename _InputIterator1
, typename _InputIterator2
,
4129 typename _OutputIterator
, typename _Compare
>
4131 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4132 _InputIterator2 __first2
, _InputIterator2 __last2
,
4133 _OutputIterator __result
, _Compare __comp
)
4135 // concept requirements
4136 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4137 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4138 __glibcxx_function_requires(_SameTypeConcept
<
4139 typename iterator_traits
<_InputIterator1
>::value_type
,
4140 typename iterator_traits
<_InputIterator2
>::value_type
>)
4141 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4142 typename iterator_traits
<_InputIterator1
>::value_type
>)
4143 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4144 typename iterator_traits
<_InputIterator1
>::value_type
,
4145 typename iterator_traits
<_InputIterator2
>::value_type
>)
4146 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4147 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4149 while (__first1
!= __last1
&& __first2
!= __last2
)
4151 if (__comp(*__first1
, *__first2
))
4153 *__result
= *__first1
;
4156 else if (__comp(*__first2
, *__first1
))
4158 *__result
= *__first2
;
4163 *__result
= *__first1
;
4169 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4174 * @brief Return the intersection of two sorted ranges.
4175 * @param first1 Start of first range.
4176 * @param last1 End of first range.
4177 * @param first2 Start of second range.
4178 * @param last2 End of second range.
4179 * @return End of the output range.
4180 * @ingroup setoperations
4182 * This operation iterates over both ranges, copying elements present in
4183 * both ranges in order to the output range. Iterators increment for each
4184 * range. When the current element of one range is less than the other,
4185 * that iterator advances. If an element is contained in both ranges, the
4186 * element from the first range is copied and both ranges advance. The
4187 * output range may not overlap either input range.
4189 template<typename _InputIterator1
, typename _InputIterator2
,
4190 typename _OutputIterator
>
4192 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
4193 _InputIterator2 __first2
, _InputIterator2 __last2
,
4194 _OutputIterator __result
)
4196 // concept requirements
4197 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4198 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4199 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4200 typename iterator_traits
<_InputIterator1
>::value_type
>)
4201 __glibcxx_function_requires(_SameTypeConcept
<
4202 typename iterator_traits
<_InputIterator1
>::value_type
,
4203 typename iterator_traits
<_InputIterator2
>::value_type
>)
4204 __glibcxx_function_requires(_LessThanComparableConcept
<
4205 typename iterator_traits
<_InputIterator1
>::value_type
>)
4206 __glibcxx_requires_sorted(__first1
, __last1
);
4207 __glibcxx_requires_sorted(__first2
, __last2
);
4209 while (__first1
!= __last1
&& __first2
!= __last2
)
4210 if (*__first1
< *__first2
)
4212 else if (*__first2
< *__first1
)
4216 *__result
= *__first1
;
4225 * @brief Return the intersection of two sorted ranges using comparison
4227 * @param first1 Start of first range.
4228 * @param last1 End of first range.
4229 * @param first2 Start of second range.
4230 * @param last2 End of second range.
4231 * @param comp The comparison functor.
4232 * @return End of the output range.
4233 * @ingroup setoperations
4235 * This operation iterates over both ranges, copying elements present in
4236 * both ranges in order to the output range. Iterators increment for each
4237 * range. When the current element of one range is less than the other
4238 * according to @a comp, that iterator advances. If an element is
4239 * contained in both ranges according to @a comp, the element from the
4240 * first range is copied and both ranges advance. The output range may not
4241 * overlap either input range.
4243 template<typename _InputIterator1
, typename _InputIterator2
,
4244 typename _OutputIterator
, typename _Compare
>
4246 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
4247 _InputIterator2 __first2
, _InputIterator2 __last2
,
4248 _OutputIterator __result
, _Compare __comp
)
4250 // concept requirements
4251 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4252 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4253 __glibcxx_function_requires(_SameTypeConcept
<
4254 typename iterator_traits
<_InputIterator1
>::value_type
,
4255 typename iterator_traits
<_InputIterator2
>::value_type
>)
4256 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4257 typename iterator_traits
<_InputIterator1
>::value_type
>)
4258 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4259 typename iterator_traits
<_InputIterator1
>::value_type
,
4260 typename iterator_traits
<_InputIterator2
>::value_type
>)
4261 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4262 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4264 while (__first1
!= __last1
&& __first2
!= __last2
)
4265 if (__comp(*__first1
, *__first2
))
4267 else if (__comp(*__first2
, *__first1
))
4271 *__result
= *__first1
;
4280 * @brief Return the difference of two sorted ranges.
4281 * @param first1 Start of first range.
4282 * @param last1 End of first range.
4283 * @param first2 Start of second range.
4284 * @param last2 End of second range.
4285 * @return End of the output range.
4286 * @ingroup setoperations
4288 * This operation iterates over both ranges, copying elements present in
4289 * the first range but not the second in order to the output range.
4290 * Iterators increment for each range. When the current element of the
4291 * first range is less than the second, that element is copied and the
4292 * iterator advances. If the current element of the second range is less,
4293 * the iterator advances, but no element is copied. If an element is
4294 * contained in both ranges, no elements are copied and both ranges
4295 * advance. The output range may not overlap either input range.
4297 template<typename _InputIterator1
, typename _InputIterator2
,
4298 typename _OutputIterator
>
4300 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4301 _InputIterator2 __first2
, _InputIterator2 __last2
,
4302 _OutputIterator __result
)
4304 // concept requirements
4305 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4306 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4307 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4308 typename iterator_traits
<_InputIterator1
>::value_type
>)
4309 __glibcxx_function_requires(_SameTypeConcept
<
4310 typename iterator_traits
<_InputIterator1
>::value_type
,
4311 typename iterator_traits
<_InputIterator2
>::value_type
>)
4312 __glibcxx_function_requires(_LessThanComparableConcept
<
4313 typename iterator_traits
<_InputIterator1
>::value_type
>)
4314 __glibcxx_requires_sorted(__first1
, __last1
);
4315 __glibcxx_requires_sorted(__first2
, __last2
);
4317 while (__first1
!= __last1
&& __first2
!= __last2
)
4318 if (*__first1
< *__first2
)
4320 *__result
= *__first1
;
4324 else if (*__first2
< *__first1
)
4331 return std::copy(__first1
, __last1
, __result
);
4335 * @brief Return the difference of two sorted ranges using comparison
4337 * @param first1 Start of first range.
4338 * @param last1 End of first range.
4339 * @param first2 Start of second range.
4340 * @param last2 End of second range.
4341 * @param comp The comparison functor.
4342 * @return End of the output range.
4343 * @ingroup setoperations
4345 * This operation iterates over both ranges, copying elements present in
4346 * the first range but not the second in order to the output range.
4347 * Iterators increment for each range. When the current element of the
4348 * first range is less than the second according to @a comp, that element
4349 * is copied and the iterator advances. If the current element of the
4350 * second range is less, no element is copied and the iterator advances.
4351 * If an element is contained in both ranges according to @a comp, no
4352 * elements are copied and both ranges advance. The output range may not
4353 * overlap either input range.
4355 template<typename _InputIterator1
, typename _InputIterator2
,
4356 typename _OutputIterator
, typename _Compare
>
4358 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4359 _InputIterator2 __first2
, _InputIterator2 __last2
,
4360 _OutputIterator __result
, _Compare __comp
)
4362 // concept requirements
4363 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4364 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4365 __glibcxx_function_requires(_SameTypeConcept
<
4366 typename iterator_traits
<_InputIterator1
>::value_type
,
4367 typename iterator_traits
<_InputIterator2
>::value_type
>)
4368 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4369 typename iterator_traits
<_InputIterator1
>::value_type
>)
4370 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4371 typename iterator_traits
<_InputIterator1
>::value_type
,
4372 typename iterator_traits
<_InputIterator2
>::value_type
>)
4373 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4374 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4376 while (__first1
!= __last1
&& __first2
!= __last2
)
4377 if (__comp(*__first1
, *__first2
))
4379 *__result
= *__first1
;
4383 else if (__comp(*__first2
, *__first1
))
4390 return std::copy(__first1
, __last1
, __result
);
4394 * @brief Return the symmetric difference of two sorted ranges.
4395 * @param first1 Start of first range.
4396 * @param last1 End of first range.
4397 * @param first2 Start of second range.
4398 * @param last2 End of second range.
4399 * @return End of the output range.
4400 * @ingroup setoperations
4402 * This operation iterates over both ranges, copying elements present in
4403 * one range but not the other in order to the output range. Iterators
4404 * increment for each range. When the current element of one range is less
4405 * than the other, that element is copied and the iterator advances. If an
4406 * element is contained in both ranges, no elements are copied and both
4407 * ranges advance. The output range may not overlap either input range.
4409 template<typename _InputIterator1
, typename _InputIterator2
,
4410 typename _OutputIterator
>
4412 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4413 _InputIterator2 __first2
, _InputIterator2 __last2
,
4414 _OutputIterator __result
)
4416 // concept requirements
4417 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4418 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4419 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4420 typename iterator_traits
<_InputIterator1
>::value_type
>)
4421 __glibcxx_function_requires(_SameTypeConcept
<
4422 typename iterator_traits
<_InputIterator1
>::value_type
,
4423 typename iterator_traits
<_InputIterator2
>::value_type
>)
4424 __glibcxx_function_requires(_LessThanComparableConcept
<
4425 typename iterator_traits
<_InputIterator1
>::value_type
>)
4426 __glibcxx_requires_sorted(__first1
, __last1
);
4427 __glibcxx_requires_sorted(__first2
, __last2
);
4429 while (__first1
!= __last1
&& __first2
!= __last2
)
4430 if (*__first1
< *__first2
)
4432 *__result
= *__first1
;
4436 else if (*__first2
< *__first1
)
4438 *__result
= *__first2
;
4447 return std::copy(__first2
, __last2
, std::copy(__first1
,
4448 __last1
, __result
));
4452 * @brief Return the symmetric difference of two sorted ranges using
4453 * comparison functor.
4454 * @param first1 Start of first range.
4455 * @param last1 End of first range.
4456 * @param first2 Start of second range.
4457 * @param last2 End of second range.
4458 * @param comp The comparison functor.
4459 * @return End of the output range.
4460 * @ingroup setoperations
4462 * This operation iterates over both ranges, copying elements present in
4463 * one range but not the other in order to the output range. Iterators
4464 * increment for each range. When the current element of one range is less
4465 * than the other according to @a comp, that element is copied and the
4466 * iterator advances. If an element is contained in both ranges according
4467 * to @a comp, no elements are copied and both ranges advance. The output
4468 * range may not overlap either input range.
4470 template<typename _InputIterator1
, typename _InputIterator2
,
4471 typename _OutputIterator
, typename _Compare
>
4473 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4474 _InputIterator2 __first2
, _InputIterator2 __last2
,
4475 _OutputIterator __result
,
4478 // concept requirements
4479 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4480 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4481 __glibcxx_function_requires(_SameTypeConcept
<
4482 typename iterator_traits
<_InputIterator1
>::value_type
,
4483 typename iterator_traits
<_InputIterator2
>::value_type
>)
4484 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4485 typename iterator_traits
<_InputIterator1
>::value_type
>)
4486 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4487 typename iterator_traits
<_InputIterator1
>::value_type
,
4488 typename iterator_traits
<_InputIterator2
>::value_type
>)
4489 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4490 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4492 while (__first1
!= __last1
&& __first2
!= __last2
)
4493 if (__comp(*__first1
, *__first2
))
4495 *__result
= *__first1
;
4499 else if (__comp(*__first2
, *__first1
))
4501 *__result
= *__first2
;
4510 return std::copy(__first2
, __last2
, std::copy(__first1
,
4511 __last1
, __result
));
4514 // min_element and max_element, with and without an explicitly supplied
4515 // comparison function.
4518 * @brief Return the maximum element in a range.
4519 * @param first Start of range.
4520 * @param last End of range.
4521 * @return Iterator referencing the first instance of the largest value.
4523 template<typename _ForwardIterator
>
4525 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
4527 // concept requirements
4528 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4529 __glibcxx_function_requires(_LessThanComparableConcept
<
4530 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4531 __glibcxx_requires_valid_range(__first
, __last
);
4533 if (__first
== __last
)
4535 _ForwardIterator __result
= __first
;
4536 while (++__first
!= __last
)
4537 if (*__result
< *__first
)
4543 * @brief Return the maximum element in a range using comparison functor.
4544 * @param first Start of range.
4545 * @param last End of range.
4546 * @param comp Comparison functor.
4547 * @return Iterator referencing the first instance of the largest value
4548 * according to comp.
4550 template<typename _ForwardIterator
, typename _Compare
>
4552 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
4555 // concept requirements
4556 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4557 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4558 typename iterator_traits
<_ForwardIterator
>::value_type
,
4559 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4560 __glibcxx_requires_valid_range(__first
, __last
);
4562 if (__first
== __last
) return __first
;
4563 _ForwardIterator __result
= __first
;
4564 while (++__first
!= __last
)
4565 if (__comp(*__result
, *__first
)) __result
= __first
;
4570 * @brief Return the minimum element in a range.
4571 * @param first Start of range.
4572 * @param last End of range.
4573 * @return Iterator referencing the first instance of the smallest value.
4575 template<typename _ForwardIterator
>
4577 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
4579 // concept requirements
4580 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4581 __glibcxx_function_requires(_LessThanComparableConcept
<
4582 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4583 __glibcxx_requires_valid_range(__first
, __last
);
4585 if (__first
== __last
)
4587 _ForwardIterator __result
= __first
;
4588 while (++__first
!= __last
)
4589 if (*__first
< *__result
)
4595 * @brief Return the minimum element in a range using comparison functor.
4596 * @param first Start of range.
4597 * @param last End of range.
4598 * @param comp Comparison functor.
4599 * @return Iterator referencing the first instance of the smallest value
4600 * according to comp.
4602 template<typename _ForwardIterator
, typename _Compare
>
4604 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
4607 // concept requirements
4608 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4609 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4610 typename iterator_traits
<_ForwardIterator
>::value_type
,
4611 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4612 __glibcxx_requires_valid_range(__first
, __last
);
4614 if (__first
== __last
)
4616 _ForwardIterator __result
= __first
;
4617 while (++__first
!= __last
)
4618 if (__comp(*__first
, *__result
))
4623 // next_permutation and prev_permutation, with and without an explicitly
4624 // supplied comparison function.
4627 * @brief Permute range into the next "dictionary" ordering.
4628 * @param first Start of range.
4629 * @param last End of range.
4630 * @return False if wrapped to first permutation, true otherwise.
4632 * Treats all permutations of the range as a set of "dictionary" sorted
4633 * sequences. Permutes the current sequence into the next one of this set.
4634 * Returns true if there are more sequences to generate. If the sequence
4635 * is the largest of the set, the smallest is generated and false returned.
4637 template<typename _BidirectionalIterator
>
4639 next_permutation(_BidirectionalIterator __first
,
4640 _BidirectionalIterator __last
)
4642 // concept requirements
4643 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4644 _BidirectionalIterator
>)
4645 __glibcxx_function_requires(_LessThanComparableConcept
<
4646 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4647 __glibcxx_requires_valid_range(__first
, __last
);
4649 if (__first
== __last
)
4651 _BidirectionalIterator __i
= __first
;
4660 _BidirectionalIterator __ii
= __i
;
4664 _BidirectionalIterator __j
= __last
;
4665 while (!(*__i
< *--__j
))
4667 std::iter_swap(__i
, __j
);
4668 std::reverse(__ii
, __last
);
4673 std::reverse(__first
, __last
);
4680 * @brief Permute range into the next "dictionary" ordering using
4681 * comparison functor.
4682 * @param first Start of range.
4683 * @param last End of range.
4685 * @return False if wrapped to first permutation, true otherwise.
4687 * Treats all permutations of the range [first,last) as a set of
4688 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
4689 * sequence into the next one of this set. Returns true if there are more
4690 * sequences to generate. If the sequence is the largest of the set, the
4691 * smallest is generated and false returned.
4693 template<typename _BidirectionalIterator
, typename _Compare
>
4695 next_permutation(_BidirectionalIterator __first
,
4696 _BidirectionalIterator __last
, _Compare __comp
)
4698 // concept requirements
4699 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4700 _BidirectionalIterator
>)
4701 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4702 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
4703 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4704 __glibcxx_requires_valid_range(__first
, __last
);
4706 if (__first
== __last
)
4708 _BidirectionalIterator __i
= __first
;
4717 _BidirectionalIterator __ii
= __i
;
4719 if (__comp(*__i
, *__ii
))
4721 _BidirectionalIterator __j
= __last
;
4722 while (!__comp(*__i
, *--__j
))
4724 std::iter_swap(__i
, __j
);
4725 std::reverse(__ii
, __last
);
4730 std::reverse(__first
, __last
);
4737 * @brief Permute range into the previous "dictionary" ordering.
4738 * @param first Start of range.
4739 * @param last End of range.
4740 * @return False if wrapped to last permutation, true otherwise.
4742 * Treats all permutations of the range as a set of "dictionary" sorted
4743 * sequences. Permutes the current sequence into the previous one of this
4744 * set. Returns true if there are more sequences to generate. If the
4745 * sequence is the smallest of the set, the largest is generated and false
4748 template<typename _BidirectionalIterator
>
4750 prev_permutation(_BidirectionalIterator __first
,
4751 _BidirectionalIterator __last
)
4753 // concept requirements
4754 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4755 _BidirectionalIterator
>)
4756 __glibcxx_function_requires(_LessThanComparableConcept
<
4757 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4758 __glibcxx_requires_valid_range(__first
, __last
);
4760 if (__first
== __last
)
4762 _BidirectionalIterator __i
= __first
;
4771 _BidirectionalIterator __ii
= __i
;
4775 _BidirectionalIterator __j
= __last
;
4776 while (!(*--__j
< *__i
))
4778 std::iter_swap(__i
, __j
);
4779 std::reverse(__ii
, __last
);
4784 std::reverse(__first
, __last
);
4791 * @brief Permute range into the previous "dictionary" ordering using
4792 * comparison functor.
4793 * @param first Start of range.
4794 * @param last End of range.
4796 * @return False if wrapped to last permutation, true otherwise.
4798 * Treats all permutations of the range [first,last) as a set of
4799 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
4800 * sequence into the previous one of this set. Returns true if there are
4801 * more sequences to generate. If the sequence is the smallest of the set,
4802 * the largest is generated and false returned.
4804 template<typename _BidirectionalIterator
, typename _Compare
>
4806 prev_permutation(_BidirectionalIterator __first
,
4807 _BidirectionalIterator __last
, _Compare __comp
)
4809 // concept requirements
4810 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4811 _BidirectionalIterator
>)
4812 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4813 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
4814 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4815 __glibcxx_requires_valid_range(__first
, __last
);
4817 if (__first
== __last
)
4819 _BidirectionalIterator __i
= __first
;
4828 _BidirectionalIterator __ii
= __i
;
4830 if (__comp(*__ii
, *__i
))
4832 _BidirectionalIterator __j
= __last
;
4833 while (!__comp(*--__j
, *__i
))
4835 std::iter_swap(__i
, __j
);
4836 std::reverse(__ii
, __last
);
4841 std::reverse(__first
, __last
);
4847 // find_first_of, with and without an explicitly supplied comparison function.
4850 * @brief Find element from a set in a sequence.
4851 * @param first1 Start of range to search.
4852 * @param last1 End of range to search.
4853 * @param first2 Start of match candidates.
4854 * @param last2 End of match candidates.
4855 * @return The first iterator @c i in the range
4856 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4857 * interator in [first2,last2), or @p last1 if no such iterator exists.
4859 * Searches the range @p [first1,last1) for an element that is equal to
4860 * some element in the range [first2,last2). If found, returns an iterator
4861 * in the range [first1,last1), otherwise returns @p last1.
4863 template<typename _InputIterator
, typename _ForwardIterator
>
4865 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4866 _ForwardIterator __first2
, _ForwardIterator __last2
)
4868 // concept requirements
4869 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4870 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4871 __glibcxx_function_requires(_EqualOpConcept
<
4872 typename iterator_traits
<_InputIterator
>::value_type
,
4873 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4874 __glibcxx_requires_valid_range(__first1
, __last1
);
4875 __glibcxx_requires_valid_range(__first2
, __last2
);
4877 for ( ; __first1
!= __last1
; ++__first1
)
4878 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4879 if (*__first1
== *__iter
)
4885 * @brief Find element from a set in a sequence using a predicate.
4886 * @param first1 Start of range to search.
4887 * @param last1 End of range to search.
4888 * @param first2 Start of match candidates.
4889 * @param last2 End of match candidates.
4890 * @param comp Predicate to use.
4891 * @return The first iterator @c i in the range
4892 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4893 * interator in [first2,last2), or @p last1 if no such iterator exists.
4895 * Searches the range @p [first1,last1) for an element that is equal to
4896 * some element in the range [first2,last2). If found, returns an iterator in
4897 * the range [first1,last1), otherwise returns @p last1.
4899 template<typename _InputIterator
, typename _ForwardIterator
,
4900 typename _BinaryPredicate
>
4902 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4903 _ForwardIterator __first2
, _ForwardIterator __last2
,
4904 _BinaryPredicate __comp
)
4906 // concept requirements
4907 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4908 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4909 __glibcxx_function_requires(_EqualOpConcept
<
4910 typename iterator_traits
<_InputIterator
>::value_type
,
4911 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4912 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4913 typename iterator_traits
<_InputIterator
>::value_type
,
4914 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4915 __glibcxx_requires_valid_range(__first1
, __last1
);
4916 __glibcxx_requires_valid_range(__first2
, __last2
);
4918 for ( ; __first1
!= __last1
; ++__first1
)
4919 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4920 if (__comp(*__first1
, *__iter
))
4926 // find_end, with and without an explicitly supplied comparison function.
4927 // Search [first2, last2) as a subsequence in [first1, last1), and return
4928 // the *last* possible match. Note that find_end for bidirectional iterators
4929 // is much faster than for forward iterators.
4931 // find_end for forward iterators.
4932 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4934 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4935 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4936 forward_iterator_tag
, forward_iterator_tag
)
4938 if (__first2
== __last2
)
4942 _ForwardIterator1 __result
= __last1
;
4945 _ForwardIterator1 __new_result
4946 = std::search(__first1
, __last1
, __first2
, __last2
);
4947 if (__new_result
== __last1
)
4951 __result
= __new_result
;
4952 __first1
= __new_result
;
4959 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4960 typename _BinaryPredicate
>
4962 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4963 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4964 forward_iterator_tag
, forward_iterator_tag
,
4965 _BinaryPredicate __comp
)
4967 if (__first2
== __last2
)
4971 _ForwardIterator1 __result
= __last1
;
4974 _ForwardIterator1 __new_result
4975 = std::search(__first1
, __last1
, __first2
, __last2
, __comp
);
4976 if (__new_result
== __last1
)
4980 __result
= __new_result
;
4981 __first1
= __new_result
;
4988 // find_end for bidirectional iterators. Requires partial specialization.
4989 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
4990 _BidirectionalIterator1
4991 __find_end(_BidirectionalIterator1 __first1
,
4992 _BidirectionalIterator1 __last1
,
4993 _BidirectionalIterator2 __first2
,
4994 _BidirectionalIterator2 __last2
,
4995 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
4997 // concept requirements
4998 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4999 _BidirectionalIterator1
>)
5000 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5001 _BidirectionalIterator2
>)
5003 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
5004 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
5006 _RevIterator1
__rlast1(__first1
);
5007 _RevIterator2
__rlast2(__first2
);
5008 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
5009 _RevIterator2(__last2
), __rlast2
);
5011 if (__rresult
== __rlast1
)
5015 _BidirectionalIterator1 __result
= __rresult
.base();
5016 std::advance(__result
, -std::distance(__first2
, __last2
));
5021 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
5022 typename _BinaryPredicate
>
5023 _BidirectionalIterator1
5024 __find_end(_BidirectionalIterator1 __first1
,
5025 _BidirectionalIterator1 __last1
,
5026 _BidirectionalIterator2 __first2
,
5027 _BidirectionalIterator2 __last2
,
5028 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
5029 _BinaryPredicate __comp
)
5031 // concept requirements
5032 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5033 _BidirectionalIterator1
>)
5034 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5035 _BidirectionalIterator2
>)
5037 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
5038 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
5040 _RevIterator1
__rlast1(__first1
);
5041 _RevIterator2
__rlast2(__first2
);
5042 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
5043 _RevIterator2(__last2
), __rlast2
,
5046 if (__rresult
== __rlast1
)
5050 _BidirectionalIterator1 __result
= __rresult
.base();
5051 std::advance(__result
, -std::distance(__first2
, __last2
));
5056 // Dispatching functions for find_end.
5059 * @brief Find last matching subsequence in a sequence.
5060 * @param first1 Start of range to search.
5061 * @param last1 End of range to search.
5062 * @param first2 Start of sequence to match.
5063 * @param last2 End of sequence to match.
5064 * @return The last iterator @c i in the range
5065 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
5066 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
5067 * such iterator exists.
5069 * Searches the range @p [first1,last1) for a sub-sequence that compares
5070 * equal value-by-value with the sequence given by @p [first2,last2) and
5071 * returns an iterator to the first element of the sub-sequence, or
5072 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
5073 * last such subsequence contained in [first,last1).
5075 * Because the sub-sequence must lie completely within the range
5076 * @p [first1,last1) it must start at a position less than
5077 * @p last1-(last2-first2) where @p last2-first2 is the length of the
5079 * This means that the returned iterator @c i will be in the range
5080 * @p [first1,last1-(last2-first2))
5082 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
5083 inline _ForwardIterator1
5084 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
5085 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
5087 // concept requirements
5088 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
5089 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
5090 __glibcxx_function_requires(_EqualOpConcept
<
5091 typename iterator_traits
<_ForwardIterator1
>::value_type
,
5092 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
5093 __glibcxx_requires_valid_range(__first1
, __last1
);
5094 __glibcxx_requires_valid_range(__first2
, __last2
);
5096 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
5097 std::__iterator_category(__first1
),
5098 std::__iterator_category(__first2
));
5102 * @brief Find last matching subsequence in a sequence using a predicate.
5103 * @param first1 Start of range to search.
5104 * @param last1 End of range to search.
5105 * @param first2 Start of sequence to match.
5106 * @param last2 End of sequence to match.
5107 * @param comp The predicate to use.
5108 * @return The last iterator @c i in the range
5109 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
5110 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
5111 * @p last1 if no such iterator exists.
5113 * Searches the range @p [first1,last1) for a sub-sequence that compares
5114 * equal value-by-value with the sequence given by @p [first2,last2) using
5115 * comp as a predicate and returns an iterator to the first element of the
5116 * sub-sequence, or @p last1 if the sub-sequence is not found. The
5117 * sub-sequence will be the last such subsequence contained in
5120 * Because the sub-sequence must lie completely within the range
5121 * @p [first1,last1) it must start at a position less than
5122 * @p last1-(last2-first2) where @p last2-first2 is the length of the
5124 * This means that the returned iterator @c i will be in the range
5125 * @p [first1,last1-(last2-first2))
5127 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
5128 typename _BinaryPredicate
>
5129 inline _ForwardIterator1
5130 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
5131 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
5132 _BinaryPredicate __comp
)
5134 // concept requirements
5135 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
5136 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
5137 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
5138 typename iterator_traits
<_ForwardIterator1
>::value_type
,
5139 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
5140 __glibcxx_requires_valid_range(__first1
, __last1
);
5141 __glibcxx_requires_valid_range(__first2
, __last2
);
5143 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
5144 std::__iterator_category(__first1
),
5145 std::__iterator_category(__first2
),
5151 #endif /* _ALGO_H */