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(_ConvertibleConcept
<_Tp
,
1107 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1108 __glibcxx_function_requires(_EqualOpConcept
<
1109 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1110 __glibcxx_requires_valid_range(__first
, __last
);
1112 __first
= std::find(__first
, __last
, __value
);
1113 _ForwardIterator __i
= __first
;
1114 return __first
== __last
? __first
1115 : std::remove_copy(++__i
, __last
,
1120 * @brief Remove elements from a sequence using a predicate.
1121 * @param first A forward iterator.
1122 * @param last A forward iterator.
1123 * @param pred A predicate.
1124 * @return An iterator designating the end of the resulting sequence.
1126 * All elements for which @p pred returns true are removed from the range
1129 * remove_if() is stable, so the relative order of elements that are
1130 * not removed is unchanged.
1132 * Elements between the end of the resulting sequence and @p last
1133 * are still present, but their value is unspecified.
1135 template<typename _ForwardIterator
, typename _Predicate
>
1137 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1140 // concept requirements
1141 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1143 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1144 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1145 __glibcxx_requires_valid_range(__first
, __last
);
1147 __first
= std::find_if(__first
, __last
, __pred
);
1148 _ForwardIterator __i
= __first
;
1149 return __first
== __last
? __first
1150 : std::remove_copy_if(++__i
, __last
,
1156 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1158 * overloaded for output iterators.
1161 template<typename _InputIterator
, typename _OutputIterator
>
1163 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1164 _OutputIterator __result
,
1165 output_iterator_tag
)
1167 // concept requirements -- taken care of in dispatching function
1168 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1169 *__result
= __value
;
1170 while (++__first
!= __last
)
1171 if (!(__value
== *__first
))
1174 *++__result
= __value
;
1181 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1183 * overloaded for forward iterators.
1186 template<typename _InputIterator
, typename _ForwardIterator
>
1188 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1189 _ForwardIterator __result
,
1190 forward_iterator_tag
)
1192 // concept requirements -- taken care of in dispatching function
1193 *__result
= *__first
;
1194 while (++__first
!= __last
)
1195 if (!(*__result
== *__first
))
1196 *++__result
= *__first
;
1202 * This is an uglified
1203 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1205 * overloaded for output iterators.
1208 template<typename _InputIterator
, typename _OutputIterator
,
1209 typename _BinaryPredicate
>
1211 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1212 _OutputIterator __result
,
1213 _BinaryPredicate __binary_pred
,
1214 output_iterator_tag
)
1216 // concept requirements -- iterators already checked
1217 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1218 typename iterator_traits
<_InputIterator
>::value_type
,
1219 typename iterator_traits
<_InputIterator
>::value_type
>)
1221 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1222 *__result
= __value
;
1223 while (++__first
!= __last
)
1224 if (!__binary_pred(__value
, *__first
))
1227 *++__result
= __value
;
1234 * This is an uglified
1235 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1237 * overloaded for forward iterators.
1240 template<typename _InputIterator
, typename _ForwardIterator
,
1241 typename _BinaryPredicate
>
1243 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1244 _ForwardIterator __result
,
1245 _BinaryPredicate __binary_pred
,
1246 forward_iterator_tag
)
1248 // concept requirements -- iterators already checked
1249 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1250 typename iterator_traits
<_ForwardIterator
>::value_type
,
1251 typename iterator_traits
<_InputIterator
>::value_type
>)
1253 *__result
= *__first
;
1254 while (++__first
!= __last
)
1255 if (!__binary_pred(*__result
, *__first
)) *++__result
= *__first
;
1260 * @brief Copy a sequence, removing consecutive duplicate values.
1261 * @param first An input iterator.
1262 * @param last An input iterator.
1263 * @param result An output iterator.
1264 * @return An iterator designating the end of the resulting sequence.
1266 * Copies each element in the range @p [first,last) to the range
1267 * beginning at @p result, except that only the first element is copied
1268 * from groups of consecutive elements that compare equal.
1269 * unique_copy() is stable, so the relative order of elements that are
1270 * copied is unchanged.
1272 template<typename _InputIterator
, typename _OutputIterator
>
1273 inline _OutputIterator
1274 unique_copy(_InputIterator __first
, _InputIterator __last
,
1275 _OutputIterator __result
)
1277 // concept requirements
1278 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1279 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1280 typename iterator_traits
<_InputIterator
>::value_type
>)
1281 __glibcxx_function_requires(_EqualityComparableConcept
<
1282 typename iterator_traits
<_InputIterator
>::value_type
>)
1283 __glibcxx_requires_valid_range(__first
, __last
);
1285 typedef typename iterator_traits
<_OutputIterator
>::iterator_category
1288 if (__first
== __last
) return __result
;
1289 return std::__unique_copy(__first
, __last
, __result
, _IterType());
1293 * @brief Copy a sequence, removing consecutive values using a predicate.
1294 * @param first An input iterator.
1295 * @param last An input iterator.
1296 * @param result An output iterator.
1297 * @param binary_pred A binary predicate.
1298 * @return An iterator designating the end of the resulting sequence.
1300 * Copies each element in the range @p [first,last) to the range
1301 * beginning at @p result, except that only the first element is copied
1302 * from groups of consecutive elements for which @p binary_pred returns
1304 * unique_copy() is stable, so the relative order of elements that are
1305 * copied is unchanged.
1307 template<typename _InputIterator
, typename _OutputIterator
,
1308 typename _BinaryPredicate
>
1309 inline _OutputIterator
1310 unique_copy(_InputIterator __first
, _InputIterator __last
,
1311 _OutputIterator __result
,
1312 _BinaryPredicate __binary_pred
)
1314 // concept requirements -- predicates checked later
1315 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1316 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1317 typename iterator_traits
<_InputIterator
>::value_type
>)
1318 __glibcxx_requires_valid_range(__first
, __last
);
1320 typedef typename iterator_traits
<_OutputIterator
>::iterator_category
1323 if (__first
== __last
) return __result
;
1324 return std::__unique_copy(__first
, __last
, __result
,
1325 __binary_pred
, _IterType());
1329 * @brief Remove consecutive duplicate values from a sequence.
1330 * @param first A forward iterator.
1331 * @param last A forward iterator.
1332 * @return An iterator designating the end of the resulting sequence.
1334 * Removes all but the first element from each group of consecutive
1335 * values that compare equal.
1336 * unique() is stable, so the relative order of elements that are
1337 * not removed is unchanged.
1338 * Elements between the end of the resulting sequence and @p last
1339 * are still present, but their value is unspecified.
1341 template<typename _ForwardIterator
>
1343 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1345 // concept requirements
1346 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1348 __glibcxx_function_requires(_EqualityComparableConcept
<
1349 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1350 __glibcxx_requires_valid_range(__first
, __last
);
1352 // Skip the beginning, if already unique.
1353 __first
= std::adjacent_find(__first
, __last
);
1354 if (__first
== __last
)
1357 // Do the real copy work.
1358 _ForwardIterator __dest
= __first
;
1360 while (++__first
!= __last
)
1361 if (!(*__dest
== *__first
))
1362 *++__dest
= *__first
;
1367 * @brief Remove consecutive values from a sequence using a predicate.
1368 * @param first A forward iterator.
1369 * @param last A forward iterator.
1370 * @param binary_pred A binary predicate.
1371 * @return An iterator designating the end of the resulting sequence.
1373 * Removes all but the first element from each group of consecutive
1374 * values for which @p binary_pred returns true.
1375 * unique() is stable, so the relative order of elements that are
1376 * not removed is unchanged.
1377 * Elements between the end of the resulting sequence and @p last
1378 * are still present, but their value is unspecified.
1380 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1382 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1383 _BinaryPredicate __binary_pred
)
1385 // concept requirements
1386 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1388 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1389 typename iterator_traits
<_ForwardIterator
>::value_type
,
1390 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1391 __glibcxx_requires_valid_range(__first
, __last
);
1393 // Skip the beginning, if already unique.
1394 __first
= std::adjacent_find(__first
, __last
, __binary_pred
);
1395 if (__first
== __last
)
1398 // Do the real copy work.
1399 _ForwardIterator __dest
= __first
;
1401 while (++__first
!= __last
)
1402 if (!__binary_pred(*__dest
, *__first
))
1403 *++__dest
= *__first
;
1409 * This is an uglified reverse(_BidirectionalIterator,
1410 * _BidirectionalIterator)
1411 * overloaded for bidirectional iterators.
1414 template<typename _BidirectionalIterator
>
1416 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1417 bidirectional_iterator_tag
)
1420 if (__first
== __last
|| __first
== --__last
)
1423 std::iter_swap(__first
++, __last
);
1428 * This is an uglified reverse(_BidirectionalIterator,
1429 * _BidirectionalIterator)
1430 * overloaded for bidirectional iterators.
1433 template<typename _RandomAccessIterator
>
1435 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1436 random_access_iterator_tag
)
1438 while (__first
< __last
)
1439 std::iter_swap(__first
++, --__last
);
1443 * @brief Reverse a sequence.
1444 * @param first A bidirectional iterator.
1445 * @param last A bidirectional iterator.
1446 * @return reverse() returns no value.
1448 * Reverses the order of the elements in the range @p [first,last),
1449 * so that the first element becomes the last etc.
1450 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1451 * swaps @p *(first+i) and @p *(last-(i+1))
1453 template<typename _BidirectionalIterator
>
1455 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1457 // concept requirements
1458 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1459 _BidirectionalIterator
>)
1460 __glibcxx_requires_valid_range(__first
, __last
);
1461 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1465 * @brief Copy a sequence, reversing its elements.
1466 * @param first A bidirectional iterator.
1467 * @param last A bidirectional iterator.
1468 * @param result An output iterator.
1469 * @return An iterator designating the end of the resulting sequence.
1471 * Copies the elements in the range @p [first,last) to the range
1472 * @p [result,result+(last-first)) such that the order of the
1473 * elements is reversed.
1474 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1475 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1476 * The ranges @p [first,last) and @p [result,result+(last-first))
1479 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1481 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1482 _OutputIterator __result
)
1484 // concept requirements
1485 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1486 _BidirectionalIterator
>)
1487 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1488 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1489 __glibcxx_requires_valid_range(__first
, __last
);
1491 while (__first
!= __last
)
1494 *__result
= *__last
;
1503 * This is a helper function for the rotate algorithm specialized on RAIs.
1504 * It returns the greatest common divisor of two integer values.
1507 template<typename _EuclideanRingElement
>
1508 _EuclideanRingElement
1509 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1513 _EuclideanRingElement __t
= __m
% __n
;
1522 * This is a helper function for the rotate algorithm.
1525 template<typename _ForwardIterator
>
1527 __rotate(_ForwardIterator __first
,
1528 _ForwardIterator __middle
,
1529 _ForwardIterator __last
,
1530 forward_iterator_tag
)
1532 if ((__first
== __middle
) || (__last
== __middle
))
1535 _ForwardIterator __first2
= __middle
;
1538 swap(*__first
++, *__first2
++);
1539 if (__first
== __middle
)
1540 __middle
= __first2
;
1542 while (__first2
!= __last
);
1544 __first2
= __middle
;
1546 while (__first2
!= __last
)
1548 swap(*__first
++, *__first2
++);
1549 if (__first
== __middle
)
1550 __middle
= __first2
;
1551 else if (__first2
== __last
)
1552 __first2
= __middle
;
1558 * This is a helper function for the rotate algorithm.
1561 template<typename _BidirectionalIterator
>
1563 __rotate(_BidirectionalIterator __first
,
1564 _BidirectionalIterator __middle
,
1565 _BidirectionalIterator __last
,
1566 bidirectional_iterator_tag
)
1568 // concept requirements
1569 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1570 _BidirectionalIterator
>)
1572 if ((__first
== __middle
) || (__last
== __middle
))
1575 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1576 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1578 while (__first
!= __middle
&& __middle
!= __last
)
1579 swap(*__first
++, *--__last
);
1581 if (__first
== __middle
)
1582 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1584 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1589 * This is a helper function for the rotate algorithm.
1592 template<typename _RandomAccessIterator
>
1594 __rotate(_RandomAccessIterator __first
,
1595 _RandomAccessIterator __middle
,
1596 _RandomAccessIterator __last
,
1597 random_access_iterator_tag
)
1599 // concept requirements
1600 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1601 _RandomAccessIterator
>)
1603 if ((__first
== __middle
) || (__last
== __middle
))
1606 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1608 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1611 const _Distance __n
= __last
- __first
;
1612 const _Distance __k
= __middle
- __first
;
1613 const _Distance __l
= __n
- __k
;
1617 std::swap_ranges(__first
, __middle
, __middle
);
1621 const _Distance __d
= __gcd(__n
, __k
);
1623 for (_Distance __i
= 0; __i
< __d
; __i
++)
1625 const _ValueType __tmp
= *__first
;
1626 _RandomAccessIterator __p
= __first
;
1630 for (_Distance __j
= 0; __j
< __l
/ __d
; __j
++)
1632 if (__p
> __first
+ __l
)
1634 *__p
= *(__p
- __l
);
1638 *__p
= *(__p
+ __k
);
1644 for (_Distance __j
= 0; __j
< __k
/ __d
- 1; __j
++)
1646 if (__p
< __last
- __k
)
1648 *__p
= *(__p
+ __k
);
1651 *__p
= * (__p
- __l
);
1662 * @brief Rotate the elements of a sequence.
1663 * @param first A forward iterator.
1664 * @param middle A forward iterator.
1665 * @param last A forward iterator.
1668 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1669 * positions so that the element at @p middle is moved to @p first, the
1670 * element at @p middle+1 is moved to @first+1 and so on for each element
1671 * in the range @p [first,last).
1673 * This effectively swaps the ranges @p [first,middle) and
1676 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1677 * each @p n in the range @p [0,last-first).
1679 template<typename _ForwardIterator
>
1681 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1682 _ForwardIterator __last
)
1684 // concept requirements
1685 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1687 __glibcxx_requires_valid_range(__first
, __middle
);
1688 __glibcxx_requires_valid_range(__middle
, __last
);
1690 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1692 std::__rotate(__first
, __middle
, __last
, _IterType());
1696 * @brief Copy a sequence, rotating its elements.
1697 * @param first A forward iterator.
1698 * @param middle A forward iterator.
1699 * @param last A forward iterator.
1700 * @param result An output iterator.
1701 * @return An iterator designating the end of the resulting sequence.
1703 * Copies the elements of the range @p [first,last) to the range
1704 * beginning at @result, rotating the copied elements by @p (middle-first)
1705 * positions so that the element at @p middle is moved to @p result, the
1706 * element at @p middle+1 is moved to @result+1 and so on for each element
1707 * in the range @p [first,last).
1709 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1710 * each @p n in the range @p [0,last-first).
1712 template<typename _ForwardIterator
, typename _OutputIterator
>
1714 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1715 _ForwardIterator __last
, _OutputIterator __result
)
1717 // concept requirements
1718 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1719 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1720 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1721 __glibcxx_requires_valid_range(__first
, __middle
);
1722 __glibcxx_requires_valid_range(__middle
, __last
);
1724 return std::copy(__first
, __middle
, copy(__middle
, __last
, __result
));
1728 * @brief Randomly shuffle the elements of a sequence.
1729 * @param first A forward iterator.
1730 * @param last A forward iterator.
1733 * Reorder the elements in the range @p [first,last) using a random
1734 * distribution, so that every possible ordering of the sequence is
1737 template<typename _RandomAccessIterator
>
1739 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
1741 // concept requirements
1742 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1743 _RandomAccessIterator
>)
1744 __glibcxx_requires_valid_range(__first
, __last
);
1746 if (__first
!= __last
)
1747 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1748 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
1752 * @brief Shuffle the elements of a sequence using a random number
1754 * @param first A forward iterator.
1755 * @param last A forward iterator.
1756 * @param rand The RNG functor or function.
1759 * Reorders the elements in the range @p [first,last) using @p rand to
1760 * provide a random distribution. Calling @p rand(N) for a positive
1761 * integer @p N should return a randomly chosen integer from the
1764 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
1766 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1767 _RandomNumberGenerator
& __rand
)
1769 // concept requirements
1770 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1771 _RandomAccessIterator
>)
1772 __glibcxx_requires_valid_range(__first
, __last
);
1774 if (__first
== __last
)
1776 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1777 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
1783 * This is a helper function...
1786 template<typename _ForwardIterator
, typename _Predicate
>
1788 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1790 forward_iterator_tag
)
1792 if (__first
== __last
)
1795 while (__pred(*__first
))
1796 if (++__first
== __last
)
1799 _ForwardIterator __next
= __first
;
1801 while (++__next
!= __last
)
1802 if (__pred(*__next
))
1804 swap(*__first
, *__next
);
1813 * This is a helper function...
1816 template<typename _BidirectionalIterator
, typename _Predicate
>
1817 _BidirectionalIterator
1818 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1820 bidirectional_iterator_tag
)
1825 if (__first
== __last
)
1827 else if (__pred(*__first
))
1833 if (__first
== __last
)
1835 else if (!__pred(*__last
))
1839 std::iter_swap(__first
, __last
);
1845 * @brief Move elements for which a predicate is true to the beginning
1847 * @param first A forward iterator.
1848 * @param last A forward iterator.
1849 * @param pred A predicate functor.
1850 * @return An iterator @p middle such that @p pred(i) is true for each
1851 * iterator @p i in the range @p [first,middle) and false for each @p i
1852 * in the range @p [middle,last).
1854 * @p pred must not modify its operand. @p partition() does not preserve
1855 * the relative ordering of elements in each group, use
1856 * @p stable_partition() if this is needed.
1858 template<typename _ForwardIterator
, typename _Predicate
>
1859 inline _ForwardIterator
1860 partition(_ForwardIterator __first
, _ForwardIterator __last
,
1863 // concept requirements
1864 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1866 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1867 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1868 __glibcxx_requires_valid_range(__first
, __last
);
1870 return std::__partition(__first
, __last
, __pred
,
1871 std::__iterator_category(__first
));
1877 * This is a helper function...
1880 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1882 __inplace_stable_partition(_ForwardIterator __first
,
1883 _ForwardIterator __last
,
1884 _Predicate __pred
, _Distance __len
)
1887 return __pred(*__first
) ? __last
: __first
;
1888 _ForwardIterator __middle
= __first
;
1889 std::advance(__middle
, __len
/ 2);
1890 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1894 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1898 std::rotate(__begin
, __middle
, __end
);
1899 std::advance(__begin
, std::distance(__middle
, __end
));
1905 * This is a helper function...
1908 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1911 __stable_partition_adaptive(_ForwardIterator __first
,
1912 _ForwardIterator __last
,
1913 _Predicate __pred
, _Distance __len
,
1915 _Distance __buffer_size
)
1917 if (__len
<= __buffer_size
)
1919 _ForwardIterator __result1
= __first
;
1920 _Pointer __result2
= __buffer
;
1921 for ( ; __first
!= __last
; ++__first
)
1922 if (__pred(*__first
))
1924 *__result1
= *__first
;
1929 *__result2
= *__first
;
1932 std::copy(__buffer
, __result2
, __result1
);
1937 _ForwardIterator __middle
= __first
;
1938 std::advance(__middle
, __len
/ 2);
1939 _ForwardIterator __begin
=
1940 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1941 __len
/ 2, __buffer
,
1943 _ForwardIterator __end
=
1944 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1946 __buffer
, __buffer_size
);
1947 std::rotate(__begin
, __middle
, __end
);
1948 std::advance(__begin
, std::distance(__middle
, __end
));
1954 * @brief Move elements for which a predicate is true to the beginning
1955 * of a sequence, preserving relative ordering.
1956 * @param first A forward iterator.
1957 * @param last A forward iterator.
1958 * @param pred A predicate functor.
1959 * @return An iterator @p middle such that @p pred(i) is true for each
1960 * iterator @p i in the range @p [first,middle) and false for each @p i
1961 * in the range @p [middle,last).
1963 * Performs the same function as @p partition() with the additional
1964 * guarantee that the relative ordering of elements in each group is
1965 * preserved, so any two elements @p x and @p y in the range
1966 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1967 * relative ordering after calling @p stable_partition().
1969 template<typename _ForwardIterator
, typename _Predicate
>
1971 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1974 // concept requirements
1975 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1977 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1978 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1979 __glibcxx_requires_valid_range(__first
, __last
);
1981 if (__first
== __last
)
1985 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1987 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1990 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
1992 if (__buf
.size() > 0)
1994 std::__stable_partition_adaptive(__first
, __last
, __pred
,
1995 _DistanceType(__buf
.requested_size()),
1996 __buf
.begin(), __buf
.size());
1999 std::__inplace_stable_partition(__first
, __last
, __pred
,
2000 _DistanceType(__buf
.requested_size()));
2006 * This is a helper function...
2009 template<typename _RandomAccessIterator
, typename _Tp
>
2010 _RandomAccessIterator
2011 __unguarded_partition(_RandomAccessIterator __first
,
2012 _RandomAccessIterator __last
, _Tp __pivot
)
2016 while (*__first
< __pivot
)
2019 while (__pivot
< *__last
)
2021 if (!(__first
< __last
))
2023 std::iter_swap(__first
, __last
);
2030 * This is a helper function...
2033 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2034 _RandomAccessIterator
2035 __unguarded_partition(_RandomAccessIterator __first
,
2036 _RandomAccessIterator __last
,
2037 _Tp __pivot
, _Compare __comp
)
2041 while (__comp(*__first
, __pivot
))
2044 while (__comp(__pivot
, *__last
))
2046 if (!(__first
< __last
))
2048 std::iter_swap(__first
, __last
);
2056 * This controls some aspect of the sort routines.
2059 enum { _S_threshold
= 16 };
2063 * This is a helper function for the sort routine.
2066 template<typename _RandomAccessIterator
, typename _Tp
>
2068 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
2070 _RandomAccessIterator __next
= __last
;
2072 while (__val
< *__next
)
2083 * This is a helper function for the sort routine.
2086 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2088 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
2091 _RandomAccessIterator __next
= __last
;
2093 while (__comp(__val
, *__next
))
2104 * This is a helper function for the sort routine.
2107 template<typename _RandomAccessIterator
>
2109 __insertion_sort(_RandomAccessIterator __first
,
2110 _RandomAccessIterator __last
)
2112 if (__first
== __last
)
2115 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2117 typename iterator_traits
<_RandomAccessIterator
>::value_type
2119 if (__val
< *__first
)
2121 std::copy_backward(__first
, __i
, __i
+ 1);
2125 std::__unguarded_linear_insert(__i
, __val
);
2131 * This is a helper function for the sort routine.
2134 template<typename _RandomAccessIterator
, typename _Compare
>
2136 __insertion_sort(_RandomAccessIterator __first
,
2137 _RandomAccessIterator __last
, _Compare __comp
)
2139 if (__first
== __last
) return;
2141 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2143 typename iterator_traits
<_RandomAccessIterator
>::value_type
2145 if (__comp(__val
, *__first
))
2147 std::copy_backward(__first
, __i
, __i
+ 1);
2151 std::__unguarded_linear_insert(__i
, __val
, __comp
);
2157 * This is a helper function for the sort routine.
2160 template<typename _RandomAccessIterator
>
2162 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2163 _RandomAccessIterator __last
)
2165 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2168 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2169 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
2174 * This is a helper function for the sort routine.
2177 template<typename _RandomAccessIterator
, typename _Compare
>
2179 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2180 _RandomAccessIterator __last
, _Compare __comp
)
2182 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2185 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2186 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
2191 * This is a helper function for the sort routine.
2194 template<typename _RandomAccessIterator
>
2196 __final_insertion_sort(_RandomAccessIterator __first
,
2197 _RandomAccessIterator __last
)
2199 if (__last
- __first
> _S_threshold
)
2201 std::__insertion_sort(__first
, __first
+ _S_threshold
);
2202 std::__unguarded_insertion_sort(__first
+ _S_threshold
, __last
);
2205 std::__insertion_sort(__first
, __last
);
2210 * This is a helper function for the sort routine.
2213 template<typename _RandomAccessIterator
, typename _Compare
>
2215 __final_insertion_sort(_RandomAccessIterator __first
,
2216 _RandomAccessIterator __last
, _Compare __comp
)
2218 if (__last
- __first
> _S_threshold
)
2220 std::__insertion_sort(__first
, __first
+ _S_threshold
, __comp
);
2221 std::__unguarded_insertion_sort(__first
+ _S_threshold
, __last
,
2225 std::__insertion_sort(__first
, __last
, __comp
);
2230 * This is a helper function for the sort routine.
2233 template<typename _Size
>
2238 for (__k
= 0; __n
!= 1; __n
>>= 1)
2244 * @brief Sort the smallest elements of a sequence.
2245 * @param first An iterator.
2246 * @param middle Another iterator.
2247 * @param last Another iterator.
2250 * Sorts the smallest @p (middle-first) elements in the range
2251 * @p [first,last) and moves them to the range @p [first,middle). The
2252 * order of the remaining elements in the range @p [middle,last) is
2254 * After the sort if @p i and @j are iterators in the range
2255 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2256 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
2258 template<typename _RandomAccessIterator
>
2260 partial_sort(_RandomAccessIterator __first
,
2261 _RandomAccessIterator __middle
,
2262 _RandomAccessIterator __last
)
2264 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2267 // concept requirements
2268 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2269 _RandomAccessIterator
>)
2270 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2271 __glibcxx_requires_valid_range(__first
, __middle
);
2272 __glibcxx_requires_valid_range(__middle
, __last
);
2274 std::make_heap(__first
, __middle
);
2275 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
2276 if (*__i
< *__first
)
2277 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
));
2278 std::sort_heap(__first
, __middle
);
2282 * @brief Sort the smallest elements of a sequence using a predicate
2284 * @param first An iterator.
2285 * @param middle Another iterator.
2286 * @param last Another iterator.
2287 * @param comp A comparison functor.
2290 * Sorts the smallest @p (middle-first) elements in the range
2291 * @p [first,last) and moves them to the range @p [first,middle). The
2292 * order of the remaining elements in the range @p [middle,last) is
2294 * After the sort if @p i and @j are iterators in the range
2295 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2296 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
2299 template<typename _RandomAccessIterator
, typename _Compare
>
2301 partial_sort(_RandomAccessIterator __first
,
2302 _RandomAccessIterator __middle
,
2303 _RandomAccessIterator __last
,
2306 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2309 // concept requirements
2310 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2311 _RandomAccessIterator
>)
2312 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2313 _ValueType
, _ValueType
>)
2314 __glibcxx_requires_valid_range(__first
, __middle
);
2315 __glibcxx_requires_valid_range(__middle
, __last
);
2317 std::make_heap(__first
, __middle
, __comp
);
2318 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
2319 if (__comp(*__i
, *__first
))
2320 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
), __comp
);
2321 std::sort_heap(__first
, __middle
, __comp
);
2325 * @brief Copy the smallest elements of a sequence.
2326 * @param first An iterator.
2327 * @param last Another iterator.
2328 * @param result_first A random-access iterator.
2329 * @param result_last Another random-access iterator.
2330 * @return An iterator indicating the end of the resulting sequence.
2332 * Copies and sorts the smallest N values from the range @p [first,last)
2333 * to the range beginning at @p result_first, where the number of
2334 * elements to be copied, @p N, is the smaller of @p (last-first) and
2335 * @p (result_last-result_first).
2336 * After the sort if @p i and @j are iterators in the range
2337 * @p [result_first,result_first+N) such that @i precedes @j then
2338 * @p *j<*i is false.
2339 * The value returned is @p result_first+N.
2341 template<typename _InputIterator
, typename _RandomAccessIterator
>
2342 _RandomAccessIterator
2343 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2344 _RandomAccessIterator __result_first
,
2345 _RandomAccessIterator __result_last
)
2347 typedef typename iterator_traits
<_InputIterator
>::value_type
2349 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2351 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2354 // concept requirements
2355 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2356 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2358 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
2359 __glibcxx_function_requires(_LessThanComparableConcept
<_InputValueType
>)
2360 __glibcxx_requires_valid_range(__first
, __last
);
2361 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2363 if (__result_first
== __result_last
)
2364 return __result_last
;
2365 _RandomAccessIterator __result_real_last
= __result_first
;
2366 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2368 *__result_real_last
= *__first
;
2369 ++__result_real_last
;
2372 std::make_heap(__result_first
, __result_real_last
);
2373 while (__first
!= __last
)
2375 if (*__first
< *__result_first
)
2376 std::__adjust_heap(__result_first
, _DistanceType(0),
2377 _DistanceType(__result_real_last
2379 _InputValueType(*__first
));
2382 std::sort_heap(__result_first
, __result_real_last
);
2383 return __result_real_last
;
2387 * @brief Copy the smallest elements of a sequence using a predicate for
2389 * @param first An input iterator.
2390 * @param last Another input iterator.
2391 * @param result_first A random-access iterator.
2392 * @param result_last Another random-access iterator.
2393 * @param comp A comparison functor.
2394 * @return An iterator indicating the end of the resulting sequence.
2396 * Copies and sorts the smallest N values from the range @p [first,last)
2397 * to the range beginning at @p result_first, where the number of
2398 * elements to be copied, @p N, is the smaller of @p (last-first) and
2399 * @p (result_last-result_first).
2400 * After the sort if @p i and @j are iterators in the range
2401 * @p [result_first,result_first+N) such that @i precedes @j then
2402 * @p comp(*j,*i) is false.
2403 * The value returned is @p result_first+N.
2405 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
2406 _RandomAccessIterator
2407 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2408 _RandomAccessIterator __result_first
,
2409 _RandomAccessIterator __result_last
,
2412 typedef typename iterator_traits
<_InputIterator
>::value_type
2414 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2416 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2419 // concept requirements
2420 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2421 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2422 _RandomAccessIterator
>)
2423 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2425 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2426 _OutputValueType
, _OutputValueType
>)
2427 __glibcxx_requires_valid_range(__first
, __last
);
2428 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2430 if (__result_first
== __result_last
)
2431 return __result_last
;
2432 _RandomAccessIterator __result_real_last
= __result_first
;
2433 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2435 *__result_real_last
= *__first
;
2436 ++__result_real_last
;
2439 std::make_heap(__result_first
, __result_real_last
, __comp
);
2440 while (__first
!= __last
)
2442 if (__comp(*__first
, *__result_first
))
2443 std::__adjust_heap(__result_first
, _DistanceType(0),
2444 _DistanceType(__result_real_last
2446 _InputValueType(*__first
),
2450 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2451 return __result_real_last
;
2456 * This is a helper function for the sort routine.
2459 template<typename _RandomAccessIterator
, typename _Size
>
2461 __introsort_loop(_RandomAccessIterator __first
,
2462 _RandomAccessIterator __last
,
2463 _Size __depth_limit
)
2465 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2468 while (__last
- __first
> _S_threshold
)
2470 if (__depth_limit
== 0)
2472 std::partial_sort(__first
, __last
, __last
);
2476 _RandomAccessIterator __cut
=
2477 std::__unguarded_partition(__first
, __last
,
2478 _ValueType(std::__median(*__first
,
2485 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2492 * This is a helper function for the sort routine.
2495 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2497 __introsort_loop(_RandomAccessIterator __first
,
2498 _RandomAccessIterator __last
,
2499 _Size __depth_limit
, _Compare __comp
)
2501 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2504 while (__last
- __first
> _S_threshold
)
2506 if (__depth_limit
== 0)
2508 std::partial_sort(__first
, __last
, __last
, __comp
);
2512 _RandomAccessIterator __cut
=
2513 std::__unguarded_partition(__first
, __last
,
2514 _ValueType(std::__median(*__first
,
2522 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2528 * @brief Sort the elements of a sequence.
2529 * @param first An iterator.
2530 * @param last Another iterator.
2533 * Sorts the elements in the range @p [first,last) in ascending order,
2534 * such that @p *(i+1)<*i is false for each iterator @p i in the range
2535 * @p [first,last-1).
2537 * The relative ordering of equivalent elements is not preserved, use
2538 * @p stable_sort() if this is needed.
2540 template<typename _RandomAccessIterator
>
2542 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
2544 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2547 // concept requirements
2548 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2549 _RandomAccessIterator
>)
2550 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2551 __glibcxx_requires_valid_range(__first
, __last
);
2553 if (__first
!= __last
)
2555 std::__introsort_loop(__first
, __last
, __lg(__last
- __first
) * 2);
2556 std::__final_insertion_sort(__first
, __last
);
2561 * @brief Sort the elements of a sequence using a predicate for comparison.
2562 * @param first An iterator.
2563 * @param last Another iterator.
2564 * @param comp A comparison functor.
2567 * Sorts the elements in the range @p [first,last) in ascending order,
2568 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
2569 * range @p [first,last-1).
2571 * The relative ordering of equivalent elements is not preserved, use
2572 * @p stable_sort() if this is needed.
2574 template<typename _RandomAccessIterator
, typename _Compare
>
2576 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2579 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2582 // concept requirements
2583 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2584 _RandomAccessIterator
>)
2585 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
2587 __glibcxx_requires_valid_range(__first
, __last
);
2589 if (__first
!= __last
)
2591 std::__introsort_loop(__first
, __last
, __lg(__last
- __first
) * 2,
2593 std::__final_insertion_sort(__first
, __last
, __comp
);
2598 * @brief Finds the first position in which @a val could be inserted
2599 * without changing the ordering.
2600 * @param first An iterator.
2601 * @param last Another iterator.
2602 * @param val The search term.
2603 * @return An iterator pointing to the first element "not less than" @a val,
2604 * or end() if every element is less than @a val.
2605 * @ingroup binarysearch
2607 template<typename _ForwardIterator
, typename _Tp
>
2609 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2612 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2614 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2617 // concept requirements
2618 // Note that these are slightly stricter than those of the 4-argument
2619 // version, defined next. The difference is in the strictness of the
2620 // comparison operations... so for looser checking, define your own
2621 // comparison function, as was intended.
2622 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2623 __glibcxx_function_requires(_SameTypeConcept
<_Tp
, _ValueType
>)
2624 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
2625 __glibcxx_requires_partitioned(__first
, __last
, __val
);
2627 _DistanceType __len
= std::distance(__first
, __last
);
2628 _DistanceType __half
;
2629 _ForwardIterator __middle
;
2633 __half
= __len
>> 1;
2635 std::advance(__middle
, __half
);
2636 if (*__middle
< __val
)
2640 __len
= __len
- __half
- 1;
2649 * @brief Finds the first position in which @a val could be inserted
2650 * without changing the ordering.
2651 * @param first An iterator.
2652 * @param last Another iterator.
2653 * @param val The search term.
2654 * @param comp A functor to use for comparisons.
2655 * @return An iterator pointing to the first element "not less than" @a val,
2656 * or end() if every element is less than @a val.
2657 * @ingroup binarysearch
2659 * The comparison function should have the same effects on ordering as
2660 * the function used for the initial sort.
2662 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2664 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2665 const _Tp
& __val
, _Compare __comp
)
2667 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2669 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2672 // concept requirements
2673 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2674 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2676 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
2678 _DistanceType __len
= std::distance(__first
, __last
);
2679 _DistanceType __half
;
2680 _ForwardIterator __middle
;
2684 __half
= __len
>> 1;
2686 std::advance(__middle
, __half
);
2687 if (__comp(*__middle
, __val
))
2691 __len
= __len
- __half
- 1;
2700 * @brief Finds the last position in which @a val could be inserted
2701 * without changing the ordering.
2702 * @param first An iterator.
2703 * @param last Another iterator.
2704 * @param val The search term.
2705 * @return An iterator pointing to the first element greater than @a val,
2706 * or end() if no elements are greater than @a val.
2707 * @ingroup binarysearch
2709 template<typename _ForwardIterator
, typename _Tp
>
2711 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2714 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2716 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2719 // concept requirements
2720 // See comments on lower_bound.
2721 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2722 __glibcxx_function_requires(_SameTypeConcept
<_Tp
, _ValueType
>)
2723 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
2724 __glibcxx_requires_partitioned(__first
, __last
, __val
);
2726 _DistanceType __len
= std::distance(__first
, __last
);
2727 _DistanceType __half
;
2728 _ForwardIterator __middle
;
2732 __half
= __len
>> 1;
2734 std::advance(__middle
, __half
);
2735 if (__val
< *__middle
)
2741 __len
= __len
- __half
- 1;
2748 * @brief Finds the last position in which @a val could be inserted
2749 * without changing the ordering.
2750 * @param first An iterator.
2751 * @param last Another iterator.
2752 * @param val The search term.
2753 * @param comp A functor to use for comparisons.
2754 * @return An iterator pointing to the first element greater than @a val,
2755 * or end() if no elements are greater than @a val.
2756 * @ingroup binarysearch
2758 * The comparison function should have the same effects on ordering as
2759 * the function used for the initial sort.
2761 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2763 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2764 const _Tp
& __val
, _Compare __comp
)
2766 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2768 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2771 // concept requirements
2772 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2773 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2775 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
2777 _DistanceType __len
= std::distance(__first
, __last
);
2778 _DistanceType __half
;
2779 _ForwardIterator __middle
;
2783 __half
= __len
>> 1;
2785 std::advance(__middle
, __half
);
2786 if (__comp(__val
, *__middle
))
2792 __len
= __len
- __half
- 1;
2800 * This is a helper function for the merge routines.
2803 template<typename _BidirectionalIterator
, typename _Distance
>
2805 __merge_without_buffer(_BidirectionalIterator __first
,
2806 _BidirectionalIterator __middle
,
2807 _BidirectionalIterator __last
,
2808 _Distance __len1
, _Distance __len2
)
2810 if (__len1
== 0 || __len2
== 0)
2812 if (__len1
+ __len2
== 2)
2814 if (*__middle
< *__first
)
2815 std::iter_swap(__first
, __middle
);
2818 _BidirectionalIterator __first_cut
= __first
;
2819 _BidirectionalIterator __second_cut
= __middle
;
2820 _Distance __len11
= 0;
2821 _Distance __len22
= 0;
2822 if (__len1
> __len2
)
2824 __len11
= __len1
/ 2;
2825 std::advance(__first_cut
, __len11
);
2826 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
2827 __len22
= std::distance(__middle
, __second_cut
);
2831 __len22
= __len2
/ 2;
2832 std::advance(__second_cut
, __len22
);
2833 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
2834 __len11
= std::distance(__first
, __first_cut
);
2836 std::rotate(__first_cut
, __middle
, __second_cut
);
2837 _BidirectionalIterator __new_middle
= __first_cut
;
2838 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2839 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2841 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2842 __len1
- __len11
, __len2
- __len22
);
2847 * This is a helper function for the merge routines.
2850 template<typename _BidirectionalIterator
, typename _Distance
,
2853 __merge_without_buffer(_BidirectionalIterator __first
,
2854 _BidirectionalIterator __middle
,
2855 _BidirectionalIterator __last
,
2856 _Distance __len1
, _Distance __len2
,
2859 if (__len1
== 0 || __len2
== 0)
2861 if (__len1
+ __len2
== 2)
2863 if (__comp(*__middle
, *__first
))
2864 std::iter_swap(__first
, __middle
);
2867 _BidirectionalIterator __first_cut
= __first
;
2868 _BidirectionalIterator __second_cut
= __middle
;
2869 _Distance __len11
= 0;
2870 _Distance __len22
= 0;
2871 if (__len1
> __len2
)
2873 __len11
= __len1
/ 2;
2874 std::advance(__first_cut
, __len11
);
2875 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2877 __len22
= std::distance(__middle
, __second_cut
);
2881 __len22
= __len2
/ 2;
2882 std::advance(__second_cut
, __len22
);
2883 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2885 __len11
= std::distance(__first
, __first_cut
);
2887 std::rotate(__first_cut
, __middle
, __second_cut
);
2888 _BidirectionalIterator __new_middle
= __first_cut
;
2889 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2890 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2891 __len11
, __len22
, __comp
);
2892 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2893 __len1
- __len11
, __len2
- __len22
, __comp
);
2898 * This is a helper function for the stable sorting routines.
2901 template<typename _RandomAccessIterator
>
2903 __inplace_stable_sort(_RandomAccessIterator __first
,
2904 _RandomAccessIterator __last
)
2906 if (__last
- __first
< 15)
2908 std::__insertion_sort(__first
, __last
);
2911 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2912 std::__inplace_stable_sort(__first
, __middle
);
2913 std::__inplace_stable_sort(__middle
, __last
);
2914 std::__merge_without_buffer(__first
, __middle
, __last
,
2921 * This is a helper function for the stable sorting routines.
2924 template<typename _RandomAccessIterator
, typename _Compare
>
2926 __inplace_stable_sort(_RandomAccessIterator __first
,
2927 _RandomAccessIterator __last
, _Compare __comp
)
2929 if (__last
- __first
< 15)
2931 std::__insertion_sort(__first
, __last
, __comp
);
2934 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2935 std::__inplace_stable_sort(__first
, __middle
, __comp
);
2936 std::__inplace_stable_sort(__middle
, __last
, __comp
);
2937 std::__merge_without_buffer(__first
, __middle
, __last
,
2944 * @brief Merges two sorted ranges.
2945 * @param first1 An iterator.
2946 * @param first2 Another iterator.
2947 * @param last1 Another iterator.
2948 * @param last2 Another iterator.
2949 * @param result An iterator pointing to the end of the merged range.
2950 * @return An iterator pointing to the first element "not less than" @a val.
2952 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
2953 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
2954 * must be sorted, and the output range must not overlap with either of
2955 * the input ranges. The sort is @e stable, that is, for equivalent
2956 * elements in the two ranges, elements from the first range will always
2957 * come before elements from the second.
2959 template<typename _InputIterator1
, typename _InputIterator2
,
2960 typename _OutputIterator
>
2962 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
2963 _InputIterator2 __first2
, _InputIterator2 __last2
,
2964 _OutputIterator __result
)
2966 // concept requirements
2967 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2968 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2969 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
2970 typename iterator_traits
<_InputIterator1
>::value_type
>)
2971 __glibcxx_function_requires(_SameTypeConcept
<
2972 typename iterator_traits
<_InputIterator1
>::value_type
,
2973 typename iterator_traits
<_InputIterator2
>::value_type
>)
2974 __glibcxx_function_requires(_LessThanComparableConcept
<
2975 typename iterator_traits
<_InputIterator1
>::value_type
>)
2976 __glibcxx_requires_sorted(__first1
, __last1
);
2977 __glibcxx_requires_sorted(__first2
, __last2
);
2979 while (__first1
!= __last1
&& __first2
!= __last2
)
2981 if (*__first2
< *__first1
)
2983 *__result
= *__first2
;
2988 *__result
= *__first1
;
2993 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
2998 * @brief Merges two sorted ranges.
2999 * @param first1 An iterator.
3000 * @param first2 Another iterator.
3001 * @param last1 Another iterator.
3002 * @param last2 Another iterator.
3003 * @param result An iterator pointing to the end of the merged range.
3004 * @param comp A functor to use for comparisons.
3005 * @return An iterator pointing to the first element "not less than" @a val.
3007 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
3008 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
3009 * must be sorted, and the output range must not overlap with either of
3010 * the input ranges. The sort is @e stable, that is, for equivalent
3011 * elements in the two ranges, elements from the first range will always
3012 * come before elements from the second.
3014 * The comparison function should have the same effects on ordering as
3015 * the function used for the initial sort.
3017 template<typename _InputIterator1
, typename _InputIterator2
,
3018 typename _OutputIterator
, typename _Compare
>
3020 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3021 _InputIterator2 __first2
, _InputIterator2 __last2
,
3022 _OutputIterator __result
, _Compare __comp
)
3024 // concept requirements
3025 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3026 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3027 __glibcxx_function_requires(_SameTypeConcept
<
3028 typename iterator_traits
<_InputIterator1
>::value_type
,
3029 typename iterator_traits
<_InputIterator2
>::value_type
>)
3030 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3031 typename iterator_traits
<_InputIterator1
>::value_type
>)
3032 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3033 typename iterator_traits
<_InputIterator1
>::value_type
,
3034 typename iterator_traits
<_InputIterator2
>::value_type
>)
3035 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
3036 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
3038 while (__first1
!= __last1
&& __first2
!= __last2
)
3040 if (__comp(*__first2
, *__first1
))
3042 *__result
= *__first2
;
3047 *__result
= *__first1
;
3052 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
3056 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3059 __merge_sort_loop(_RandomAccessIterator1 __first
,
3060 _RandomAccessIterator1 __last
,
3061 _RandomAccessIterator2 __result
,
3062 _Distance __step_size
)
3064 const _Distance __two_step
= 2 * __step_size
;
3066 while (__last
- __first
>= __two_step
)
3068 __result
= std::merge(__first
, __first
+ __step_size
,
3069 __first
+ __step_size
, __first
+ __two_step
,
3071 __first
+= __two_step
;
3074 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3075 std::merge(__first
, __first
+ __step_size
, __first
+ __step_size
, __last
,
3079 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3080 typename _Distance
, typename _Compare
>
3082 __merge_sort_loop(_RandomAccessIterator1 __first
,
3083 _RandomAccessIterator1 __last
,
3084 _RandomAccessIterator2 __result
, _Distance __step_size
,
3087 const _Distance __two_step
= 2 * __step_size
;
3089 while (__last
- __first
>= __two_step
)
3091 __result
= std::merge(__first
, __first
+ __step_size
,
3092 __first
+ __step_size
, __first
+ __two_step
,
3095 __first
+= __two_step
;
3097 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3099 std::merge(__first
, __first
+ __step_size
,
3100 __first
+ __step_size
, __last
,
3105 enum { _S_chunk_size
= 7 };
3107 template<typename _RandomAccessIterator
, typename _Distance
>
3109 __chunk_insertion_sort(_RandomAccessIterator __first
,
3110 _RandomAccessIterator __last
,
3111 _Distance __chunk_size
)
3113 while (__last
- __first
>= __chunk_size
)
3115 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3116 __first
+= __chunk_size
;
3118 std::__insertion_sort(__first
, __last
);
3121 template<typename _RandomAccessIterator
, typename _Distance
, typename _Compare
>
3123 __chunk_insertion_sort(_RandomAccessIterator __first
,
3124 _RandomAccessIterator __last
,
3125 _Distance __chunk_size
, _Compare __comp
)
3127 while (__last
- __first
>= __chunk_size
)
3129 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3130 __first
+= __chunk_size
;
3132 std::__insertion_sort(__first
, __last
, __comp
);
3135 template<typename _RandomAccessIterator
, typename _Pointer
>
3137 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3138 _RandomAccessIterator __last
,
3141 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3144 const _Distance __len
= __last
- __first
;
3145 const _Pointer __buffer_last
= __buffer
+ __len
;
3147 _Distance __step_size
= _S_chunk_size
;
3148 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3150 while (__step_size
< __len
)
3152 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3154 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3159 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3161 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3162 _RandomAccessIterator __last
,
3163 _Pointer __buffer
, _Compare __comp
)
3165 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3168 const _Distance __len
= __last
- __first
;
3169 const _Pointer __buffer_last
= __buffer
+ __len
;
3171 _Distance __step_size
= _S_chunk_size
;
3172 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3174 while (__step_size
< __len
)
3176 std::__merge_sort_loop(__first
, __last
, __buffer
,
3177 __step_size
, __comp
);
3179 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3180 __step_size
, __comp
);
3187 * This is a helper function for the merge routines.
3190 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3191 typename _BidirectionalIterator3
>
3192 _BidirectionalIterator3
3193 __merge_backward(_BidirectionalIterator1 __first1
,
3194 _BidirectionalIterator1 __last1
,
3195 _BidirectionalIterator2 __first2
,
3196 _BidirectionalIterator2 __last2
,
3197 _BidirectionalIterator3 __result
)
3199 if (__first1
== __last1
)
3200 return std::copy_backward(__first2
, __last2
, __result
);
3201 if (__first2
== __last2
)
3202 return std::copy_backward(__first1
, __last1
, __result
);
3207 if (*__last2
< *__last1
)
3209 *--__result
= *__last1
;
3210 if (__first1
== __last1
)
3211 return std::copy_backward(__first2
, ++__last2
, __result
);
3216 *--__result
= *__last2
;
3217 if (__first2
== __last2
)
3218 return std::copy_backward(__first1
, ++__last1
, __result
);
3226 * This is a helper function for the merge routines.
3229 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3230 typename _BidirectionalIterator3
, typename _Compare
>
3231 _BidirectionalIterator3
3232 __merge_backward(_BidirectionalIterator1 __first1
,
3233 _BidirectionalIterator1 __last1
,
3234 _BidirectionalIterator2 __first2
,
3235 _BidirectionalIterator2 __last2
,
3236 _BidirectionalIterator3 __result
,
3239 if (__first1
== __last1
)
3240 return std::copy_backward(__first2
, __last2
, __result
);
3241 if (__first2
== __last2
)
3242 return std::copy_backward(__first1
, __last1
, __result
);
3247 if (__comp(*__last2
, *__last1
))
3249 *--__result
= *__last1
;
3250 if (__first1
== __last1
)
3251 return std::copy_backward(__first2
, ++__last2
, __result
);
3256 *--__result
= *__last2
;
3257 if (__first2
== __last2
)
3258 return std::copy_backward(__first1
, ++__last1
, __result
);
3266 * This is a helper function for the merge routines.
3269 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3271 _BidirectionalIterator1
3272 __rotate_adaptive(_BidirectionalIterator1 __first
,
3273 _BidirectionalIterator1 __middle
,
3274 _BidirectionalIterator1 __last
,
3275 _Distance __len1
, _Distance __len2
,
3276 _BidirectionalIterator2 __buffer
,
3277 _Distance __buffer_size
)
3279 _BidirectionalIterator2 __buffer_end
;
3280 if (__len1
> __len2
&& __len2
<= __buffer_size
)
3282 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3283 std::copy_backward(__first
, __middle
, __last
);
3284 return std::copy(__buffer
, __buffer_end
, __first
);
3286 else if (__len1
<= __buffer_size
)
3288 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3289 std::copy(__middle
, __last
, __first
);
3290 return std::copy_backward(__buffer
, __buffer_end
, __last
);
3294 std::rotate(__first
, __middle
, __last
);
3295 std::advance(__first
, std::distance(__middle
, __last
));
3302 * This is a helper function for the merge routines.
3305 template<typename _BidirectionalIterator
, typename _Distance
,
3308 __merge_adaptive(_BidirectionalIterator __first
,
3309 _BidirectionalIterator __middle
,
3310 _BidirectionalIterator __last
,
3311 _Distance __len1
, _Distance __len2
,
3312 _Pointer __buffer
, _Distance __buffer_size
)
3314 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
3316 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3317 std::merge(__buffer
, __buffer_end
, __middle
, __last
, __first
);
3319 else if (__len2
<= __buffer_size
)
3321 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3322 std::__merge_backward(__first
, __middle
, __buffer
,
3323 __buffer_end
, __last
);
3327 _BidirectionalIterator __first_cut
= __first
;
3328 _BidirectionalIterator __second_cut
= __middle
;
3329 _Distance __len11
= 0;
3330 _Distance __len22
= 0;
3331 if (__len1
> __len2
)
3333 __len11
= __len1
/ 2;
3334 std::advance(__first_cut
, __len11
);
3335 __second_cut
= std::lower_bound(__middle
, __last
,
3337 __len22
= std::distance(__middle
, __second_cut
);
3341 __len22
= __len2
/ 2;
3342 std::advance(__second_cut
, __len22
);
3343 __first_cut
= std::upper_bound(__first
, __middle
,
3345 __len11
= std::distance(__first
, __first_cut
);
3347 _BidirectionalIterator __new_middle
=
3348 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3349 __len1
- __len11
, __len22
, __buffer
,
3351 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3352 __len22
, __buffer
, __buffer_size
);
3353 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3355 __len2
- __len22
, __buffer
, __buffer_size
);
3361 * This is a helper function for the merge routines.
3364 template<typename _BidirectionalIterator
, typename _Distance
, typename _Pointer
,
3367 __merge_adaptive(_BidirectionalIterator __first
,
3368 _BidirectionalIterator __middle
,
3369 _BidirectionalIterator __last
,
3370 _Distance __len1
, _Distance __len2
,
3371 _Pointer __buffer
, _Distance __buffer_size
,
3374 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
3376 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3377 std::merge(__buffer
, __buffer_end
, __middle
, __last
, __first
, __comp
);
3379 else if (__len2
<= __buffer_size
)
3381 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3382 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
3387 _BidirectionalIterator __first_cut
= __first
;
3388 _BidirectionalIterator __second_cut
= __middle
;
3389 _Distance __len11
= 0;
3390 _Distance __len22
= 0;
3391 if (__len1
> __len2
)
3393 __len11
= __len1
/ 2;
3394 std::advance(__first_cut
, __len11
);
3395 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3397 __len22
= std::distance(__middle
, __second_cut
);
3401 __len22
= __len2
/ 2;
3402 std::advance(__second_cut
, __len22
);
3403 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3405 __len11
= std::distance(__first
, __first_cut
);
3407 _BidirectionalIterator __new_middle
=
3408 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3409 __len1
- __len11
, __len22
, __buffer
,
3411 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3412 __len22
, __buffer
, __buffer_size
, __comp
);
3413 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3415 __len2
- __len22
, __buffer
,
3416 __buffer_size
, __comp
);
3421 * @brief Merges two sorted ranges in place.
3422 * @param first An iterator.
3423 * @param middle Another iterator.
3424 * @param last Another iterator.
3427 * Merges two sorted and consecutive ranges, [first,middle) and
3428 * [middle,last), and puts the result in [first,last). The output will
3429 * be sorted. The sort is @e stable, that is, for equivalent
3430 * elements in the two ranges, elements from the first range will always
3431 * come before elements from the second.
3433 * If enough additional memory is available, this takes (last-first)-1
3434 * comparisons. Otherwise an NlogN algorithm is used, where N is
3435 * distance(first,last).
3437 template<typename _BidirectionalIterator
>
3439 inplace_merge(_BidirectionalIterator __first
,
3440 _BidirectionalIterator __middle
,
3441 _BidirectionalIterator __last
)
3443 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3445 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3448 // concept requirements
3449 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3450 _BidirectionalIterator
>)
3451 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3452 __glibcxx_requires_sorted(__first
, __middle
);
3453 __glibcxx_requires_sorted(__middle
, __last
);
3455 if (__first
== __middle
|| __middle
== __last
)
3458 _DistanceType __len1
= std::distance(__first
, __middle
);
3459 _DistanceType __len2
= std::distance(__middle
, __last
);
3461 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3463 if (__buf
.begin() == 0)
3464 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3466 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3467 __buf
.begin(), _DistanceType(__buf
.size()));
3471 * @brief Merges two sorted ranges in place.
3472 * @param first An iterator.
3473 * @param middle Another iterator.
3474 * @param last Another iterator.
3475 * @param comp A functor to use for comparisons.
3478 * Merges two sorted and consecutive ranges, [first,middle) and
3479 * [middle,last), and puts the result in [first,last). The output will
3480 * be sorted. The sort is @e stable, that is, for equivalent
3481 * elements in the two ranges, elements from the first range will always
3482 * come before elements from the second.
3484 * If enough additional memory is available, this takes (last-first)-1
3485 * comparisons. Otherwise an NlogN algorithm is used, where N is
3486 * distance(first,last).
3488 * The comparison function should have the same effects on ordering as
3489 * the function used for the initial sort.
3491 template<typename _BidirectionalIterator
, typename _Compare
>
3493 inplace_merge(_BidirectionalIterator __first
,
3494 _BidirectionalIterator __middle
,
3495 _BidirectionalIterator __last
,
3498 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3500 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3503 // concept requirements
3504 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3505 _BidirectionalIterator
>)
3506 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3507 _ValueType
, _ValueType
>)
3508 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3509 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3511 if (__first
== __middle
|| __middle
== __last
)
3514 const _DistanceType __len1
= std::distance(__first
, __middle
);
3515 const _DistanceType __len2
= std::distance(__middle
, __last
);
3517 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3519 if (__buf
.begin() == 0)
3520 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3523 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3524 __buf
.begin(), _DistanceType(__buf
.size()),
3528 template<typename _RandomAccessIterator
, typename _Pointer
,
3531 __stable_sort_adaptive(_RandomAccessIterator __first
,
3532 _RandomAccessIterator __last
,
3533 _Pointer __buffer
, _Distance __buffer_size
)
3535 const _Distance __len
= (__last
- __first
+ 1) / 2;
3536 const _RandomAccessIterator __middle
= __first
+ __len
;
3537 if (__len
> __buffer_size
)
3539 std::__stable_sort_adaptive(__first
, __middle
,
3540 __buffer
, __buffer_size
);
3541 std::__stable_sort_adaptive(__middle
, __last
,
3542 __buffer
, __buffer_size
);
3546 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3547 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3549 std::__merge_adaptive(__first
, __middle
, __last
,
3550 _Distance(__middle
- __first
),
3551 _Distance(__last
- __middle
),
3552 __buffer
, __buffer_size
);
3555 template<typename _RandomAccessIterator
, typename _Pointer
,
3556 typename _Distance
, typename _Compare
>
3558 __stable_sort_adaptive(_RandomAccessIterator __first
,
3559 _RandomAccessIterator __last
,
3560 _Pointer __buffer
, _Distance __buffer_size
,
3563 const _Distance __len
= (__last
- __first
+ 1) / 2;
3564 const _RandomAccessIterator __middle
= __first
+ __len
;
3565 if (__len
> __buffer_size
)
3567 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3568 __buffer_size
, __comp
);
3569 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3570 __buffer_size
, __comp
);
3574 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3575 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3577 std::__merge_adaptive(__first
, __middle
, __last
,
3578 _Distance(__middle
- __first
),
3579 _Distance(__last
- __middle
),
3580 __buffer
, __buffer_size
,
3585 * @brief Sort the elements of a sequence, preserving the relative order
3586 * of equivalent elements.
3587 * @param first An iterator.
3588 * @param last Another iterator.
3591 * Sorts the elements in the range @p [first,last) in ascending order,
3592 * such that @p *(i+1)<*i is false for each iterator @p i in the range
3593 * @p [first,last-1).
3595 * The relative ordering of equivalent elements is preserved, so any two
3596 * elements @p x and @p y in the range @p [first,last) such that
3597 * @p x<y is false and @p y<x is false will have the same relative
3598 * ordering after calling @p stable_sort().
3600 template<typename _RandomAccessIterator
>
3602 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
3604 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3606 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3609 // concept requirements
3610 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3611 _RandomAccessIterator
>)
3612 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3613 __glibcxx_requires_valid_range(__first
, __last
);
3615 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
>
3616 buf(__first
, __last
);
3617 if (buf
.begin() == 0)
3618 std::__inplace_stable_sort(__first
, __last
);
3620 std::__stable_sort_adaptive(__first
, __last
, buf
.begin(),
3621 _DistanceType(buf
.size()));
3625 * @brief Sort the elements of a sequence using a predicate for comparison,
3626 * preserving the relative order of equivalent elements.
3627 * @param first An iterator.
3628 * @param last Another iterator.
3629 * @param comp A comparison functor.
3632 * Sorts the elements in the range @p [first,last) in ascending order,
3633 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
3634 * range @p [first,last-1).
3636 * The relative ordering of equivalent elements is preserved, so any two
3637 * elements @p x and @p y in the range @p [first,last) such that
3638 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
3639 * relative ordering after calling @p stable_sort().
3641 template<typename _RandomAccessIterator
, typename _Compare
>
3643 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3646 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3648 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3651 // concept requirements
3652 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3653 _RandomAccessIterator
>)
3654 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3657 __glibcxx_requires_valid_range(__first
, __last
);
3659 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> buf(__first
, __last
);
3660 if (buf
.begin() == 0)
3661 std::__inplace_stable_sort(__first
, __last
, __comp
);
3663 std::__stable_sort_adaptive(__first
, __last
, buf
.begin(),
3664 _DistanceType(buf
.size()), __comp
);
3668 * @brief Sort a sequence just enough to find a particular position.
3669 * @param first An iterator.
3670 * @param nth Another iterator.
3671 * @param last Another iterator.
3674 * Rearranges the elements in the range @p [first,last) so that @p *nth
3675 * is the same element that would have been in that position had the
3676 * whole sequence been sorted.
3677 * whole sequence been sorted. The elements either side of @p *nth are
3678 * not completely sorted, but for any iterator @i in the range
3679 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3680 * holds that @p *j<*i is false.
3682 template<typename _RandomAccessIterator
>
3684 nth_element(_RandomAccessIterator __first
,
3685 _RandomAccessIterator __nth
,
3686 _RandomAccessIterator __last
)
3688 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3691 // concept requirements
3692 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3693 _RandomAccessIterator
>)
3694 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3695 __glibcxx_requires_valid_range(__first
, __nth
);
3696 __glibcxx_requires_valid_range(__nth
, __last
);
3698 while (__last
- __first
> 3)
3700 _RandomAccessIterator __cut
=
3701 std::__unguarded_partition(__first
, __last
,
3702 _ValueType(std::__median(*__first
,
3714 std::__insertion_sort(__first
, __last
);
3718 * @brief Sort a sequence just enough to find a particular position
3719 * using a predicate for comparison.
3720 * @param first An iterator.
3721 * @param nth Another iterator.
3722 * @param last Another iterator.
3723 * @param comp A comparison functor.
3726 * Rearranges the elements in the range @p [first,last) so that @p *nth
3727 * is the same element that would have been in that position had the
3728 * whole sequence been sorted. The elements either side of @p *nth are
3729 * not completely sorted, but for any iterator @i in the range
3730 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3731 * holds that @p comp(*j,*i) is false.
3733 template<typename _RandomAccessIterator
, typename _Compare
>
3735 nth_element(_RandomAccessIterator __first
,
3736 _RandomAccessIterator __nth
,
3737 _RandomAccessIterator __last
,
3740 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3743 // concept requirements
3744 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3745 _RandomAccessIterator
>)
3746 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3747 _ValueType
, _ValueType
>)
3748 __glibcxx_requires_valid_range(__first
, __nth
);
3749 __glibcxx_requires_valid_range(__nth
, __last
);
3751 while (__last
- __first
> 3)
3753 _RandomAccessIterator __cut
=
3754 std::__unguarded_partition(__first
, __last
,
3755 _ValueType(std::__median(*__first
,
3767 std::__insertion_sort(__first
, __last
, __comp
);
3771 * @brief Finds the largest subrange in which @a val could be inserted
3772 * at any place in it without changing the ordering.
3773 * @param first An iterator.
3774 * @param last Another iterator.
3775 * @param val The search term.
3776 * @return An pair of iterators defining the subrange.
3777 * @ingroup binarysearch
3779 * This is equivalent to
3781 * std::make_pair(lower_bound(first, last, val),
3782 * upper_bound(first, last, val))
3784 * but does not actually call those functions.
3786 template<typename _ForwardIterator
, typename _Tp
>
3787 pair
<_ForwardIterator
, _ForwardIterator
>
3788 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
3791 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3793 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3796 // concept requirements
3797 // See comments on lower_bound.
3798 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3799 __glibcxx_function_requires(_SameTypeConcept
<_Tp
, _ValueType
>)
3800 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3801 __glibcxx_requires_partitioned(__first
, __last
, __val
);
3803 _DistanceType __len
= std::distance(__first
, __last
);
3804 _DistanceType __half
;
3805 _ForwardIterator __middle
, __left
, __right
;
3809 __half
= __len
>> 1;
3811 std::advance(__middle
, __half
);
3812 if (*__middle
< __val
)
3816 __len
= __len
- __half
- 1;
3818 else if (__val
< *__middle
)
3822 __left
= std::lower_bound(__first
, __middle
, __val
);
3823 std::advance(__first
, __len
);
3824 __right
= std::upper_bound(++__middle
, __first
, __val
);
3825 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
3828 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
3832 * @brief Finds the largest subrange in which @a val could be inserted
3833 * at any place in it without changing the ordering.
3834 * @param first An iterator.
3835 * @param last Another iterator.
3836 * @param val The search term.
3837 * @param comp A functor to use for comparisons.
3838 * @return An pair of iterators defining the subrange.
3839 * @ingroup binarysearch
3841 * This is equivalent to
3843 * std::make_pair(lower_bound(first, last, val, comp),
3844 * upper_bound(first, last, val, comp))
3846 * but does not actually call those functions.
3848 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3849 pair
<_ForwardIterator
, _ForwardIterator
>
3850 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
3854 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3856 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3859 // concept requirements
3860 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3861 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3863 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3865 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
3867 _DistanceType __len
= std::distance(__first
, __last
);
3868 _DistanceType __half
;
3869 _ForwardIterator __middle
, __left
, __right
;
3873 __half
= __len
>> 1;
3875 std::advance(__middle
, __half
);
3876 if (__comp(*__middle
, __val
))
3880 __len
= __len
- __half
- 1;
3882 else if (__comp(__val
, *__middle
))
3886 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
3887 std::advance(__first
, __len
);
3888 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
3889 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
3892 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
3896 * @brief Determines whether an element exists in a range.
3897 * @param first An iterator.
3898 * @param last Another iterator.
3899 * @param val The search term.
3900 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
3901 * @ingroup binarysearch
3903 * Note that this does not actually return an iterator to @a val. For
3904 * that, use std::find or a container's specialized find member functions.
3906 template<typename _ForwardIterator
, typename _Tp
>
3908 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
3911 // concept requirements
3912 // See comments on lower_bound.
3913 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3914 __glibcxx_function_requires(_SameTypeConcept
<_Tp
,
3915 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3916 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3917 __glibcxx_requires_partitioned(__first
, __last
, __val
);
3919 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
3920 return __i
!= __last
&& !(__val
< *__i
);
3924 * @brief Determines whether an element exists in a range.
3925 * @param first An iterator.
3926 * @param last Another iterator.
3927 * @param val The search term.
3928 * @param comp A functor to use for comparisons.
3929 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
3930 * @ingroup binarysearch
3932 * Note that this does not actually return an iterator to @a val. For
3933 * that, use std::find or a container's specialized find member functions.
3935 * The comparison function should have the same effects on ordering as
3936 * the function used for the initial sort.
3938 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3940 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
3941 const _Tp
& __val
, _Compare __comp
)
3943 // concept requirements
3944 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3945 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3946 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
3947 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _Tp
,
3948 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3949 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
3951 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
3952 return __i
!= __last
&& !__comp(__val
, *__i
);
3955 // Set algorithms: includes, set_union, set_intersection, set_difference,
3956 // set_symmetric_difference. All of these algorithms have the precondition
3957 // that their input ranges are sorted and the postcondition that their output
3958 // ranges are sorted.
3961 * @brief Determines whether all elements of a sequence exists in a range.
3962 * @param first1 Start of search range.
3963 * @param last1 End of search range.
3964 * @param first2 Start of sequence
3965 * @param last2 End of sequence.
3966 * @return True if each element in [first2,last2) is contained in order
3967 * within [first1,last1). False otherwise.
3968 * @ingroup setoperations
3970 * This operation expects both [first1,last1) and [first2,last2) to be
3971 * sorted. Searches for the presence of each element in [first2,last2)
3972 * within [first1,last1). The iterators over each range only move forward,
3973 * so this is a linear algorithm. If an element in [first2,last2) is not
3974 * found before the search iterator reaches @a last2, false is returned.
3976 template<typename _InputIterator1
, typename _InputIterator2
>
3978 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3979 _InputIterator2 __first2
, _InputIterator2 __last2
)
3981 // concept requirements
3982 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3983 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3984 __glibcxx_function_requires(_SameTypeConcept
<
3985 typename iterator_traits
<_InputIterator1
>::value_type
,
3986 typename iterator_traits
<_InputIterator2
>::value_type
>)
3987 __glibcxx_function_requires(_LessThanComparableConcept
<
3988 typename iterator_traits
<_InputIterator1
>::value_type
>)
3989 __glibcxx_requires_sorted(__first1
, __last1
);
3990 __glibcxx_requires_sorted(__first2
, __last2
);
3992 while (__first1
!= __last1
&& __first2
!= __last2
)
3993 if (*__first2
< *__first1
)
3995 else if(*__first1
< *__first2
)
3998 ++__first1
, ++__first2
;
4000 return __first2
== __last2
;
4004 * @brief Determines whether all elements of a sequence exists in a range
4006 * @param first1 Start of search range.
4007 * @param last1 End of search range.
4008 * @param first2 Start of sequence
4009 * @param last2 End of sequence.
4010 * @param comp Comparison function to use.
4011 * @return True if each element in [first2,last2) is contained in order
4012 * within [first1,last1) according to comp. False otherwise.
4013 * @ingroup setoperations
4015 * This operation expects both [first1,last1) and [first2,last2) to be
4016 * sorted. Searches for the presence of each element in [first2,last2)
4017 * within [first1,last1), using comp to decide. The iterators over each
4018 * range only move forward, so this is a linear algorithm. If an element
4019 * in [first2,last2) is not found before the search iterator reaches @a
4020 * last2, false is returned.
4022 template<typename _InputIterator1
, typename _InputIterator2
,
4025 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
4026 _InputIterator2 __first2
, _InputIterator2 __last2
, _Compare __comp
)
4028 // concept requirements
4029 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4030 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4031 __glibcxx_function_requires(_SameTypeConcept
<
4032 typename iterator_traits
<_InputIterator1
>::value_type
,
4033 typename iterator_traits
<_InputIterator2
>::value_type
>)
4034 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4035 typename iterator_traits
<_InputIterator1
>::value_type
,
4036 typename iterator_traits
<_InputIterator2
>::value_type
>)
4037 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4038 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4040 while (__first1
!= __last1
&& __first2
!= __last2
)
4041 if (__comp(*__first2
, *__first1
))
4043 else if(__comp(*__first1
, *__first2
))
4046 ++__first1
, ++__first2
;
4048 return __first2
== __last2
;
4052 * @brief Return the union of two sorted ranges.
4053 * @param first1 Start of first range.
4054 * @param last1 End of first range.
4055 * @param first2 Start of second range.
4056 * @param last2 End of second range.
4057 * @return End of the output range.
4058 * @ingroup setoperations
4060 * This operation iterates over both ranges, copying elements present in
4061 * each range in order to the output range. Iterators increment for each
4062 * range. When the current element of one range is less than the other,
4063 * that element is copied and the iterator advanced. If an element is
4064 * contained in both ranges, the element from the first range is copied and
4065 * both ranges advance. The output range may not overlap either input
4068 template<typename _InputIterator1
, typename _InputIterator2
,
4069 typename _OutputIterator
>
4071 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4072 _InputIterator2 __first2
, _InputIterator2 __last2
,
4073 _OutputIterator __result
)
4075 // concept requirements
4076 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4077 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4078 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4079 typename iterator_traits
<_InputIterator1
>::value_type
>)
4080 __glibcxx_function_requires(_SameTypeConcept
<
4081 typename iterator_traits
<_InputIterator1
>::value_type
,
4082 typename iterator_traits
<_InputIterator2
>::value_type
>)
4083 __glibcxx_function_requires(_LessThanComparableConcept
<
4084 typename iterator_traits
<_InputIterator1
>::value_type
>)
4085 __glibcxx_requires_sorted(__first1
, __last1
);
4086 __glibcxx_requires_sorted(__first2
, __last2
);
4088 while (__first1
!= __last1
&& __first2
!= __last2
)
4090 if (*__first1
< *__first2
)
4092 *__result
= *__first1
;
4095 else if (*__first2
< *__first1
)
4097 *__result
= *__first2
;
4102 *__result
= *__first1
;
4108 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4113 * @brief Return the union of two sorted ranges using a comparison functor.
4114 * @param first1 Start of first range.
4115 * @param last1 End of first range.
4116 * @param first2 Start of second range.
4117 * @param last2 End of second range.
4118 * @param comp The comparison functor.
4119 * @return End of the output range.
4120 * @ingroup setoperations
4122 * This operation iterates over both ranges, copying elements present in
4123 * each range in order to the output range. Iterators increment for each
4124 * range. When the current element of one range is less than the other
4125 * according to @a comp, that element is copied and the iterator advanced.
4126 * If an equivalent element according to @a comp is contained in both
4127 * ranges, the element from the first range is copied and both ranges
4128 * advance. The output range may not overlap either input range.
4130 template<typename _InputIterator1
, typename _InputIterator2
,
4131 typename _OutputIterator
, typename _Compare
>
4133 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4134 _InputIterator2 __first2
, _InputIterator2 __last2
,
4135 _OutputIterator __result
, _Compare __comp
)
4137 // concept requirements
4138 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4139 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4140 __glibcxx_function_requires(_SameTypeConcept
<
4141 typename iterator_traits
<_InputIterator1
>::value_type
,
4142 typename iterator_traits
<_InputIterator2
>::value_type
>)
4143 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4144 typename iterator_traits
<_InputIterator1
>::value_type
>)
4145 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4146 typename iterator_traits
<_InputIterator1
>::value_type
,
4147 typename iterator_traits
<_InputIterator2
>::value_type
>)
4148 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4149 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4151 while (__first1
!= __last1
&& __first2
!= __last2
)
4153 if (__comp(*__first1
, *__first2
))
4155 *__result
= *__first1
;
4158 else if (__comp(*__first2
, *__first1
))
4160 *__result
= *__first2
;
4165 *__result
= *__first1
;
4171 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4176 * @brief Return the intersection of two sorted ranges.
4177 * @param first1 Start of first range.
4178 * @param last1 End of first range.
4179 * @param first2 Start of second range.
4180 * @param last2 End of second range.
4181 * @return End of the output range.
4182 * @ingroup setoperations
4184 * This operation iterates over both ranges, copying elements present in
4185 * both ranges in order to the output range. Iterators increment for each
4186 * range. When the current element of one range is less than the other,
4187 * that iterator advances. If an element is contained in both ranges, the
4188 * element from the first range is copied and both ranges advance. The
4189 * output range may not overlap either input range.
4191 template<typename _InputIterator1
, typename _InputIterator2
,
4192 typename _OutputIterator
>
4194 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
4195 _InputIterator2 __first2
, _InputIterator2 __last2
,
4196 _OutputIterator __result
)
4198 // concept requirements
4199 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4200 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4201 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4202 typename iterator_traits
<_InputIterator1
>::value_type
>)
4203 __glibcxx_function_requires(_SameTypeConcept
<
4204 typename iterator_traits
<_InputIterator1
>::value_type
,
4205 typename iterator_traits
<_InputIterator2
>::value_type
>)
4206 __glibcxx_function_requires(_LessThanComparableConcept
<
4207 typename iterator_traits
<_InputIterator1
>::value_type
>)
4208 __glibcxx_requires_sorted(__first1
, __last1
);
4209 __glibcxx_requires_sorted(__first2
, __last2
);
4211 while (__first1
!= __last1
&& __first2
!= __last2
)
4212 if (*__first1
< *__first2
)
4214 else if (*__first2
< *__first1
)
4218 *__result
= *__first1
;
4227 * @brief Return the intersection of two sorted ranges using comparison
4229 * @param first1 Start of first range.
4230 * @param last1 End of first range.
4231 * @param first2 Start of second range.
4232 * @param last2 End of second range.
4233 * @param comp The comparison functor.
4234 * @return End of the output range.
4235 * @ingroup setoperations
4237 * This operation iterates over both ranges, copying elements present in
4238 * both ranges in order to the output range. Iterators increment for each
4239 * range. When the current element of one range is less than the other
4240 * according to @a comp, that iterator advances. If an element is
4241 * contained in both ranges according to @a comp, the element from the
4242 * first range is copied and both ranges advance. The output range may not
4243 * overlap either input range.
4245 template<typename _InputIterator1
, typename _InputIterator2
,
4246 typename _OutputIterator
, typename _Compare
>
4248 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
4249 _InputIterator2 __first2
, _InputIterator2 __last2
,
4250 _OutputIterator __result
, _Compare __comp
)
4252 // concept requirements
4253 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4254 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4255 __glibcxx_function_requires(_SameTypeConcept
<
4256 typename iterator_traits
<_InputIterator1
>::value_type
,
4257 typename iterator_traits
<_InputIterator2
>::value_type
>)
4258 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4259 typename iterator_traits
<_InputIterator1
>::value_type
>)
4260 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4261 typename iterator_traits
<_InputIterator1
>::value_type
,
4262 typename iterator_traits
<_InputIterator2
>::value_type
>)
4263 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4264 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4266 while (__first1
!= __last1
&& __first2
!= __last2
)
4267 if (__comp(*__first1
, *__first2
))
4269 else if (__comp(*__first2
, *__first1
))
4273 *__result
= *__first1
;
4282 * @brief Return the difference of two sorted ranges.
4283 * @param first1 Start of first range.
4284 * @param last1 End of first range.
4285 * @param first2 Start of second range.
4286 * @param last2 End of second range.
4287 * @return End of the output range.
4288 * @ingroup setoperations
4290 * This operation iterates over both ranges, copying elements present in
4291 * the first range but not the second in order to the output range.
4292 * Iterators increment for each range. When the current element of the
4293 * first range is less than the second, that element is copied and the
4294 * iterator advances. If the current element of the second range is less,
4295 * the iterator advances, but no element is copied. If an element is
4296 * contained in both ranges, no elements are copied and both ranges
4297 * advance. The output range may not overlap either input range.
4299 template<typename _InputIterator1
, typename _InputIterator2
,
4300 typename _OutputIterator
>
4302 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4303 _InputIterator2 __first2
, _InputIterator2 __last2
,
4304 _OutputIterator __result
)
4306 // concept requirements
4307 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4308 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4309 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4310 typename iterator_traits
<_InputIterator1
>::value_type
>)
4311 __glibcxx_function_requires(_SameTypeConcept
<
4312 typename iterator_traits
<_InputIterator1
>::value_type
,
4313 typename iterator_traits
<_InputIterator2
>::value_type
>)
4314 __glibcxx_function_requires(_LessThanComparableConcept
<
4315 typename iterator_traits
<_InputIterator1
>::value_type
>)
4316 __glibcxx_requires_sorted(__first1
, __last1
);
4317 __glibcxx_requires_sorted(__first2
, __last2
);
4319 while (__first1
!= __last1
&& __first2
!= __last2
)
4320 if (*__first1
< *__first2
)
4322 *__result
= *__first1
;
4326 else if (*__first2
< *__first1
)
4333 return std::copy(__first1
, __last1
, __result
);
4337 * @brief Return the difference of two sorted ranges using comparison
4339 * @param first1 Start of first range.
4340 * @param last1 End of first range.
4341 * @param first2 Start of second range.
4342 * @param last2 End of second range.
4343 * @param comp The comparison functor.
4344 * @return End of the output range.
4345 * @ingroup setoperations
4347 * This operation iterates over both ranges, copying elements present in
4348 * the first range but not the second in order to the output range.
4349 * Iterators increment for each range. When the current element of the
4350 * first range is less than the second according to @a comp, that element
4351 * is copied and the iterator advances. If the current element of the
4352 * second range is less, no element is copied and the iterator advances.
4353 * If an element is contained in both ranges according to @a comp, no
4354 * elements are copied and both ranges advance. The output range may not
4355 * overlap either input range.
4357 template<typename _InputIterator1
, typename _InputIterator2
,
4358 typename _OutputIterator
, typename _Compare
>
4360 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4361 _InputIterator2 __first2
, _InputIterator2 __last2
,
4362 _OutputIterator __result
, _Compare __comp
)
4364 // concept requirements
4365 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4366 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4367 __glibcxx_function_requires(_SameTypeConcept
<
4368 typename iterator_traits
<_InputIterator1
>::value_type
,
4369 typename iterator_traits
<_InputIterator2
>::value_type
>)
4370 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4371 typename iterator_traits
<_InputIterator1
>::value_type
>)
4372 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4373 typename iterator_traits
<_InputIterator1
>::value_type
,
4374 typename iterator_traits
<_InputIterator2
>::value_type
>)
4375 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4376 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4378 while (__first1
!= __last1
&& __first2
!= __last2
)
4379 if (__comp(*__first1
, *__first2
))
4381 *__result
= *__first1
;
4385 else if (__comp(*__first2
, *__first1
))
4392 return std::copy(__first1
, __last1
, __result
);
4396 * @brief Return the symmetric difference of two sorted ranges.
4397 * @param first1 Start of first range.
4398 * @param last1 End of first range.
4399 * @param first2 Start of second range.
4400 * @param last2 End of second range.
4401 * @return End of the output range.
4402 * @ingroup setoperations
4404 * This operation iterates over both ranges, copying elements present in
4405 * one range but not the other in order to the output range. Iterators
4406 * increment for each range. When the current element of one range is less
4407 * than the other, that element is copied and the iterator advances. If an
4408 * element is contained in both ranges, no elements are copied and both
4409 * ranges advance. The output range may not overlap either input range.
4411 template<typename _InputIterator1
, typename _InputIterator2
,
4412 typename _OutputIterator
>
4414 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4415 _InputIterator2 __first2
, _InputIterator2 __last2
,
4416 _OutputIterator __result
)
4418 // concept requirements
4419 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4420 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4421 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4422 typename iterator_traits
<_InputIterator1
>::value_type
>)
4423 __glibcxx_function_requires(_SameTypeConcept
<
4424 typename iterator_traits
<_InputIterator1
>::value_type
,
4425 typename iterator_traits
<_InputIterator2
>::value_type
>)
4426 __glibcxx_function_requires(_LessThanComparableConcept
<
4427 typename iterator_traits
<_InputIterator1
>::value_type
>)
4428 __glibcxx_requires_sorted(__first1
, __last1
);
4429 __glibcxx_requires_sorted(__first2
, __last2
);
4431 while (__first1
!= __last1
&& __first2
!= __last2
)
4432 if (*__first1
< *__first2
)
4434 *__result
= *__first1
;
4438 else if (*__first2
< *__first1
)
4440 *__result
= *__first2
;
4449 return std::copy(__first2
, __last2
, std::copy(__first1
,
4450 __last1
, __result
));
4454 * @brief Return the symmetric difference of two sorted ranges using
4455 * comparison functor.
4456 * @param first1 Start of first range.
4457 * @param last1 End of first range.
4458 * @param first2 Start of second range.
4459 * @param last2 End of second range.
4460 * @param comp The comparison functor.
4461 * @return End of the output range.
4462 * @ingroup setoperations
4464 * This operation iterates over both ranges, copying elements present in
4465 * one range but not the other in order to the output range. Iterators
4466 * increment for each range. When the current element of one range is less
4467 * than the other according to @a comp, that element is copied and the
4468 * iterator advances. If an element is contained in both ranges according
4469 * to @a comp, no elements are copied and both ranges advance. The output
4470 * range may not overlap either input range.
4472 template<typename _InputIterator1
, typename _InputIterator2
,
4473 typename _OutputIterator
, typename _Compare
>
4475 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4476 _InputIterator2 __first2
, _InputIterator2 __last2
,
4477 _OutputIterator __result
,
4480 // concept requirements
4481 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4482 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4483 __glibcxx_function_requires(_SameTypeConcept
<
4484 typename iterator_traits
<_InputIterator1
>::value_type
,
4485 typename iterator_traits
<_InputIterator2
>::value_type
>)
4486 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4487 typename iterator_traits
<_InputIterator1
>::value_type
>)
4488 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4489 typename iterator_traits
<_InputIterator1
>::value_type
,
4490 typename iterator_traits
<_InputIterator2
>::value_type
>)
4491 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4492 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4494 while (__first1
!= __last1
&& __first2
!= __last2
)
4495 if (__comp(*__first1
, *__first2
))
4497 *__result
= *__first1
;
4501 else if (__comp(*__first2
, *__first1
))
4503 *__result
= *__first2
;
4512 return std::copy(__first2
, __last2
, std::copy(__first1
,
4513 __last1
, __result
));
4516 // min_element and max_element, with and without an explicitly supplied
4517 // comparison function.
4520 * @brief Return the maximum element in a range.
4521 * @param first Start of range.
4522 * @param last End of range.
4523 * @return Iterator referencing the first instance of the largest value.
4525 template<typename _ForwardIterator
>
4527 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
4529 // concept requirements
4530 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4531 __glibcxx_function_requires(_LessThanComparableConcept
<
4532 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4533 __glibcxx_requires_valid_range(__first
, __last
);
4535 if (__first
== __last
)
4537 _ForwardIterator __result
= __first
;
4538 while (++__first
!= __last
)
4539 if (*__result
< *__first
)
4545 * @brief Return the maximum element in a range using comparison functor.
4546 * @param first Start of range.
4547 * @param last End of range.
4548 * @param comp Comparison functor.
4549 * @return Iterator referencing the first instance of the largest value
4550 * according to comp.
4552 template<typename _ForwardIterator
, typename _Compare
>
4554 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
4557 // concept requirements
4558 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4559 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4560 typename iterator_traits
<_ForwardIterator
>::value_type
,
4561 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4562 __glibcxx_requires_valid_range(__first
, __last
);
4564 if (__first
== __last
) return __first
;
4565 _ForwardIterator __result
= __first
;
4566 while (++__first
!= __last
)
4567 if (__comp(*__result
, *__first
)) __result
= __first
;
4572 * @brief Return the minimum element in a range.
4573 * @param first Start of range.
4574 * @param last End of range.
4575 * @return Iterator referencing the first instance of the smallest value.
4577 template<typename _ForwardIterator
>
4579 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
4581 // concept requirements
4582 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4583 __glibcxx_function_requires(_LessThanComparableConcept
<
4584 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4585 __glibcxx_requires_valid_range(__first
, __last
);
4587 if (__first
== __last
)
4589 _ForwardIterator __result
= __first
;
4590 while (++__first
!= __last
)
4591 if (*__first
< *__result
)
4597 * @brief Return the minimum element in a range using comparison functor.
4598 * @param first Start of range.
4599 * @param last End of range.
4600 * @param comp Comparison functor.
4601 * @return Iterator referencing the first instance of the smallest value
4602 * according to comp.
4604 template<typename _ForwardIterator
, typename _Compare
>
4606 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
4609 // concept requirements
4610 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4611 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4612 typename iterator_traits
<_ForwardIterator
>::value_type
,
4613 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4614 __glibcxx_requires_valid_range(__first
, __last
);
4616 if (__first
== __last
)
4618 _ForwardIterator __result
= __first
;
4619 while (++__first
!= __last
)
4620 if (__comp(*__first
, *__result
))
4625 // next_permutation and prev_permutation, with and without an explicitly
4626 // supplied comparison function.
4629 * @brief Permute range into the next "dictionary" ordering.
4630 * @param first Start of range.
4631 * @param last End of range.
4632 * @return False if wrapped to first permutation, true otherwise.
4634 * Treats all permutations of the range as a set of "dictionary" sorted
4635 * sequences. Permutes the current sequence into the next one of this set.
4636 * Returns true if there are more sequences to generate. If the sequence
4637 * is the largest of the set, the smallest is generated and false returned.
4639 template<typename _BidirectionalIterator
>
4641 next_permutation(_BidirectionalIterator __first
,
4642 _BidirectionalIterator __last
)
4644 // concept requirements
4645 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4646 _BidirectionalIterator
>)
4647 __glibcxx_function_requires(_LessThanComparableConcept
<
4648 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4649 __glibcxx_requires_valid_range(__first
, __last
);
4651 if (__first
== __last
)
4653 _BidirectionalIterator __i
= __first
;
4662 _BidirectionalIterator __ii
= __i
;
4666 _BidirectionalIterator __j
= __last
;
4667 while (!(*__i
< *--__j
))
4669 std::iter_swap(__i
, __j
);
4670 std::reverse(__ii
, __last
);
4675 std::reverse(__first
, __last
);
4682 * @brief Permute range into the next "dictionary" ordering using
4683 * comparison functor.
4684 * @param first Start of range.
4685 * @param last End of range.
4687 * @return False if wrapped to first permutation, true otherwise.
4689 * Treats all permutations of the range [first,last) as a set of
4690 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
4691 * sequence into the next one of this set. Returns true if there are more
4692 * sequences to generate. If the sequence is the largest of the set, the
4693 * smallest is generated and false returned.
4695 template<typename _BidirectionalIterator
, typename _Compare
>
4697 next_permutation(_BidirectionalIterator __first
,
4698 _BidirectionalIterator __last
, _Compare __comp
)
4700 // concept requirements
4701 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4702 _BidirectionalIterator
>)
4703 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4704 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
4705 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4706 __glibcxx_requires_valid_range(__first
, __last
);
4708 if (__first
== __last
)
4710 _BidirectionalIterator __i
= __first
;
4719 _BidirectionalIterator __ii
= __i
;
4721 if (__comp(*__i
, *__ii
))
4723 _BidirectionalIterator __j
= __last
;
4724 while (!__comp(*__i
, *--__j
))
4726 std::iter_swap(__i
, __j
);
4727 std::reverse(__ii
, __last
);
4732 std::reverse(__first
, __last
);
4739 * @brief Permute range into the previous "dictionary" ordering.
4740 * @param first Start of range.
4741 * @param last End of range.
4742 * @return False if wrapped to last permutation, true otherwise.
4744 * Treats all permutations of the range as a set of "dictionary" sorted
4745 * sequences. Permutes the current sequence into the previous one of this
4746 * set. Returns true if there are more sequences to generate. If the
4747 * sequence is the smallest of the set, the largest is generated and false
4750 template<typename _BidirectionalIterator
>
4752 prev_permutation(_BidirectionalIterator __first
,
4753 _BidirectionalIterator __last
)
4755 // concept requirements
4756 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4757 _BidirectionalIterator
>)
4758 __glibcxx_function_requires(_LessThanComparableConcept
<
4759 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4760 __glibcxx_requires_valid_range(__first
, __last
);
4762 if (__first
== __last
)
4764 _BidirectionalIterator __i
= __first
;
4773 _BidirectionalIterator __ii
= __i
;
4777 _BidirectionalIterator __j
= __last
;
4778 while (!(*--__j
< *__i
))
4780 std::iter_swap(__i
, __j
);
4781 std::reverse(__ii
, __last
);
4786 std::reverse(__first
, __last
);
4793 * @brief Permute range into the previous "dictionary" ordering using
4794 * comparison functor.
4795 * @param first Start of range.
4796 * @param last End of range.
4798 * @return False if wrapped to last permutation, true otherwise.
4800 * Treats all permutations of the range [first,last) as a set of
4801 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
4802 * sequence into the previous one of this set. Returns true if there are
4803 * more sequences to generate. If the sequence is the smallest of the set,
4804 * the largest is generated and false returned.
4806 template<typename _BidirectionalIterator
, typename _Compare
>
4808 prev_permutation(_BidirectionalIterator __first
,
4809 _BidirectionalIterator __last
, _Compare __comp
)
4811 // concept requirements
4812 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4813 _BidirectionalIterator
>)
4814 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4815 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
4816 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4817 __glibcxx_requires_valid_range(__first
, __last
);
4819 if (__first
== __last
)
4821 _BidirectionalIterator __i
= __first
;
4830 _BidirectionalIterator __ii
= __i
;
4832 if (__comp(*__ii
, *__i
))
4834 _BidirectionalIterator __j
= __last
;
4835 while (!__comp(*--__j
, *__i
))
4837 std::iter_swap(__i
, __j
);
4838 std::reverse(__ii
, __last
);
4843 std::reverse(__first
, __last
);
4849 // find_first_of, with and without an explicitly supplied comparison function.
4852 * @brief Find element from a set in a sequence.
4853 * @param first1 Start of range to search.
4854 * @param last1 End of range to search.
4855 * @param first2 Start of match candidates.
4856 * @param last2 End of match candidates.
4857 * @return The first iterator @c i in the range
4858 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4859 * interator in [first2,last2), or @p last1 if no such iterator exists.
4861 * Searches the range @p [first1,last1) for an element that is equal to
4862 * some element in the range [first2,last2). If found, returns an iterator
4863 * in the range [first1,last1), otherwise returns @p last1.
4865 template<typename _InputIterator
, typename _ForwardIterator
>
4867 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4868 _ForwardIterator __first2
, _ForwardIterator __last2
)
4870 // concept requirements
4871 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4872 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4873 __glibcxx_function_requires(_EqualOpConcept
<
4874 typename iterator_traits
<_InputIterator
>::value_type
,
4875 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4876 __glibcxx_requires_valid_range(__first1
, __last1
);
4877 __glibcxx_requires_valid_range(__first2
, __last2
);
4879 for ( ; __first1
!= __last1
; ++__first1
)
4880 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4881 if (*__first1
== *__iter
)
4887 * @brief Find element from a set in a sequence using a predicate.
4888 * @param first1 Start of range to search.
4889 * @param last1 End of range to search.
4890 * @param first2 Start of match candidates.
4891 * @param last2 End of match candidates.
4892 * @param comp Predicate to use.
4893 * @return The first iterator @c i in the range
4894 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4895 * interator in [first2,last2), or @p last1 if no such iterator exists.
4897 * Searches the range @p [first1,last1) for an element that is equal to
4898 * some element in the range [first2,last2). If found, returns an iterator in
4899 * the range [first1,last1), otherwise returns @p last1.
4901 template<typename _InputIterator
, typename _ForwardIterator
,
4902 typename _BinaryPredicate
>
4904 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4905 _ForwardIterator __first2
, _ForwardIterator __last2
,
4906 _BinaryPredicate __comp
)
4908 // concept requirements
4909 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4910 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4911 __glibcxx_function_requires(_EqualOpConcept
<
4912 typename iterator_traits
<_InputIterator
>::value_type
,
4913 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4914 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4915 typename iterator_traits
<_InputIterator
>::value_type
,
4916 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4917 __glibcxx_requires_valid_range(__first1
, __last1
);
4918 __glibcxx_requires_valid_range(__first2
, __last2
);
4920 for ( ; __first1
!= __last1
; ++__first1
)
4921 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4922 if (__comp(*__first1
, *__iter
))
4928 // find_end, with and without an explicitly supplied comparison function.
4929 // Search [first2, last2) as a subsequence in [first1, last1), and return
4930 // the *last* possible match. Note that find_end for bidirectional iterators
4931 // is much faster than for forward iterators.
4933 // find_end for forward iterators.
4934 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4936 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4937 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4938 forward_iterator_tag
, forward_iterator_tag
)
4940 if (__first2
== __last2
)
4944 _ForwardIterator1 __result
= __last1
;
4947 _ForwardIterator1 __new_result
4948 = std::search(__first1
, __last1
, __first2
, __last2
);
4949 if (__new_result
== __last1
)
4953 __result
= __new_result
;
4954 __first1
= __new_result
;
4961 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4962 typename _BinaryPredicate
>
4964 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4965 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4966 forward_iterator_tag
, forward_iterator_tag
,
4967 _BinaryPredicate __comp
)
4969 if (__first2
== __last2
)
4973 _ForwardIterator1 __result
= __last1
;
4976 _ForwardIterator1 __new_result
4977 = std::search(__first1
, __last1
, __first2
, __last2
, __comp
);
4978 if (__new_result
== __last1
)
4982 __result
= __new_result
;
4983 __first1
= __new_result
;
4990 // find_end for bidirectional iterators. Requires partial specialization.
4991 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
4992 _BidirectionalIterator1
4993 __find_end(_BidirectionalIterator1 __first1
,
4994 _BidirectionalIterator1 __last1
,
4995 _BidirectionalIterator2 __first2
,
4996 _BidirectionalIterator2 __last2
,
4997 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
4999 // concept requirements
5000 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5001 _BidirectionalIterator1
>)
5002 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5003 _BidirectionalIterator2
>)
5005 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
5006 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
5008 _RevIterator1
__rlast1(__first1
);
5009 _RevIterator2
__rlast2(__first2
);
5010 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
5011 _RevIterator2(__last2
), __rlast2
);
5013 if (__rresult
== __rlast1
)
5017 _BidirectionalIterator1 __result
= __rresult
.base();
5018 std::advance(__result
, -std::distance(__first2
, __last2
));
5023 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
5024 typename _BinaryPredicate
>
5025 _BidirectionalIterator1
5026 __find_end(_BidirectionalIterator1 __first1
,
5027 _BidirectionalIterator1 __last1
,
5028 _BidirectionalIterator2 __first2
,
5029 _BidirectionalIterator2 __last2
,
5030 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
5031 _BinaryPredicate __comp
)
5033 // concept requirements
5034 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5035 _BidirectionalIterator1
>)
5036 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5037 _BidirectionalIterator2
>)
5039 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
5040 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
5042 _RevIterator1
__rlast1(__first1
);
5043 _RevIterator2
__rlast2(__first2
);
5044 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
5045 _RevIterator2(__last2
), __rlast2
,
5048 if (__rresult
== __rlast1
)
5052 _BidirectionalIterator1 __result
= __rresult
.base();
5053 std::advance(__result
, -std::distance(__first2
, __last2
));
5058 // Dispatching functions for find_end.
5061 * @brief Find last matching subsequence in a sequence.
5062 * @param first1 Start of range to search.
5063 * @param last1 End of range to search.
5064 * @param first2 Start of sequence to match.
5065 * @param last2 End of sequence to match.
5066 * @return The last iterator @c i in the range
5067 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
5068 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
5069 * such iterator exists.
5071 * Searches the range @p [first1,last1) for a sub-sequence that compares
5072 * equal value-by-value with the sequence given by @p [first2,last2) and
5073 * returns an iterator to the first element of the sub-sequence, or
5074 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
5075 * last such subsequence contained in [first,last1).
5077 * Because the sub-sequence must lie completely within the range
5078 * @p [first1,last1) it must start at a position less than
5079 * @p last1-(last2-first2) where @p last2-first2 is the length of the
5081 * This means that the returned iterator @c i will be in the range
5082 * @p [first1,last1-(last2-first2))
5084 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
5085 inline _ForwardIterator1
5086 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
5087 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
5089 // concept requirements
5090 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
5091 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
5092 __glibcxx_function_requires(_EqualOpConcept
<
5093 typename iterator_traits
<_ForwardIterator1
>::value_type
,
5094 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
5095 __glibcxx_requires_valid_range(__first1
, __last1
);
5096 __glibcxx_requires_valid_range(__first2
, __last2
);
5098 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
5099 std::__iterator_category(__first1
),
5100 std::__iterator_category(__first2
));
5104 * @brief Find last matching subsequence in a sequence using a predicate.
5105 * @param first1 Start of range to search.
5106 * @param last1 End of range to search.
5107 * @param first2 Start of sequence to match.
5108 * @param last2 End of sequence to match.
5109 * @param comp The predicate to use.
5110 * @return The last iterator @c i in the range
5111 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
5112 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
5113 * @p last1 if no such iterator exists.
5115 * Searches the range @p [first1,last1) for a sub-sequence that compares
5116 * equal value-by-value with the sequence given by @p [first2,last2) using
5117 * comp as a predicate and returns an iterator to the first element of the
5118 * sub-sequence, or @p last1 if the sub-sequence is not found. The
5119 * sub-sequence will be the last such subsequence contained in
5122 * Because the sub-sequence must lie completely within the range
5123 * @p [first1,last1) it must start at a position less than
5124 * @p last1-(last2-first2) where @p last2-first2 is the length of the
5126 * This means that the returned iterator @c i will be in the range
5127 * @p [first1,last1-(last2-first2))
5129 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
5130 typename _BinaryPredicate
>
5131 inline _ForwardIterator1
5132 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
5133 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
5134 _BinaryPredicate __comp
)
5136 // concept requirements
5137 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
5138 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
5139 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
5140 typename iterator_traits
<_ForwardIterator1
>::value_type
,
5141 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
5142 __glibcxx_requires_valid_range(__first1
, __last1
);
5143 __glibcxx_requires_valid_range(__first2
, __last2
);
5145 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
5146 std::__iterator_category(__first1
),
5147 std::__iterator_category(__first2
),
5153 #endif /* _ALGO_H */