1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
64 #include <bits/stl_heap.h>
65 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
66 #include <debug/debug.h>
68 // See concept_check.h for the __glibcxx_*_requires macros.
73 * @brief Find the median of three values.
77 * @return One of @p a, @p b or @p c.
79 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
80 * then the value returned will be @c m.
81 * This is an SGI extension.
82 * @ingroup SGIextensions
84 template<typename _Tp
>
86 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
)
88 // concept requirements
89 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
106 * @brief Find the median of three values using a predicate for comparison.
110 * @param comp A binary predicate.
111 * @return One of @p a, @p b or @p c.
113 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
114 * and @p comp(m,n) are both true then the value returned will be @c m.
115 * This is an SGI extension.
116 * @ingroup SGIextensions
118 template<typename _Tp
, typename _Compare
>
120 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
, _Compare __comp
)
122 // concept requirements
123 __glibcxx_function_requires(_BinaryFunctionConcept
<_Compare
,bool,_Tp
,_Tp
>)
124 if (__comp(__a
, __b
))
125 if (__comp(__b
, __c
))
127 else if (__comp(__a
, __c
))
131 else if (__comp(__a
, __c
))
133 else if (__comp(__b
, __c
))
140 * @brief Apply a function to every element of a sequence.
141 * @param first An input iterator.
142 * @param last An input iterator.
143 * @param f A unary function object.
146 * Applies the function object @p f to each element in the range
147 * @p [first,last). @p f must not modify the order of the sequence.
148 * If @p f has a return value it is ignored.
150 template<typename _InputIterator
, typename _Function
>
152 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
154 // concept requirements
155 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
156 __glibcxx_requires_valid_range(__first
, __last
);
157 for ( ; __first
!= __last
; ++__first
)
164 * This is an overload used by find() for the Input Iterator case.
167 template<typename _InputIterator
, typename _Tp
>
168 inline _InputIterator
169 find(_InputIterator __first
, _InputIterator __last
,
170 const _Tp
& __val
, input_iterator_tag
)
172 while (__first
!= __last
&& !(*__first
== __val
))
179 * This is an overload used by find_if() for the Input Iterator case.
182 template<typename _InputIterator
, typename _Predicate
>
183 inline _InputIterator
184 find_if(_InputIterator __first
, _InputIterator __last
,
185 _Predicate __pred
, input_iterator_tag
)
187 while (__first
!= __last
&& !__pred(*__first
))
194 * This is an overload used by find() for the RAI case.
197 template<typename _RandomAccessIterator
, typename _Tp
>
198 _RandomAccessIterator
199 find(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
200 const _Tp
& __val
, random_access_iterator_tag
)
202 typename iterator_traits
<_RandomAccessIterator
>::difference_type
203 __trip_count
= (__last
- __first
) >> 2;
205 for ( ; __trip_count
> 0 ; --__trip_count
)
207 if (*__first
== __val
)
211 if (*__first
== __val
)
215 if (*__first
== __val
)
219 if (*__first
== __val
)
224 switch (__last
- __first
)
227 if (*__first
== __val
)
231 if (*__first
== __val
)
235 if (*__first
== __val
)
246 * This is an overload used by find_if() for the RAI case.
249 template<typename _RandomAccessIterator
, typename _Predicate
>
250 _RandomAccessIterator
251 find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
252 _Predicate __pred
, random_access_iterator_tag
)
254 typename iterator_traits
<_RandomAccessIterator
>::difference_type
255 __trip_count
= (__last
- __first
) >> 2;
257 for ( ; __trip_count
> 0 ; --__trip_count
)
259 if (__pred(*__first
))
263 if (__pred(*__first
))
267 if (__pred(*__first
))
271 if (__pred(*__first
))
276 switch (__last
- __first
)
279 if (__pred(*__first
))
283 if (__pred(*__first
))
287 if (__pred(*__first
))
297 * @brief Find the first occurrence of a value in a sequence.
298 * @param first An input iterator.
299 * @param last An input iterator.
300 * @param val The value to find.
301 * @return The first iterator @c i in the range @p [first,last)
302 * such that @c *i == @p val, or @p last if no such iterator exists.
304 template<typename _InputIterator
, typename _Tp
>
305 inline _InputIterator
306 find(_InputIterator __first
, _InputIterator __last
,
309 // concept requirements
310 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
311 __glibcxx_function_requires(_EqualOpConcept
<
312 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
313 __glibcxx_requires_valid_range(__first
, __last
);
314 return std::find(__first
, __last
, __val
,
315 std::__iterator_category(__first
));
319 * @brief Find the first element in a sequence for which a predicate is true.
320 * @param first An input iterator.
321 * @param last An input iterator.
322 * @param pred A predicate.
323 * @return The first iterator @c i in the range @p [first,last)
324 * such that @p pred(*i) is true, or @p last if no such iterator exists.
326 template<typename _InputIterator
, typename _Predicate
>
327 inline _InputIterator
328 find_if(_InputIterator __first
, _InputIterator __last
,
331 // concept requirements
332 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
333 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
334 typename iterator_traits
<_InputIterator
>::value_type
>)
335 __glibcxx_requires_valid_range(__first
, __last
);
336 return std::find_if(__first
, __last
, __pred
,
337 std::__iterator_category(__first
));
341 * @brief Find two adjacent values in a sequence that are equal.
342 * @param first A forward iterator.
343 * @param last A forward iterator.
344 * @return The first iterator @c i such that @c i and @c i+1 are both
345 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
346 * or @p last if no such iterator exists.
348 template<typename _ForwardIterator
>
350 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
352 // concept requirements
353 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
354 __glibcxx_function_requires(_EqualityComparableConcept
<
355 typename iterator_traits
<_ForwardIterator
>::value_type
>)
356 __glibcxx_requires_valid_range(__first
, __last
);
357 if (__first
== __last
)
359 _ForwardIterator __next
= __first
;
360 while(++__next
!= __last
)
362 if (*__first
== *__next
)
370 * @brief Find two adjacent values in a sequence using a predicate.
371 * @param first A forward iterator.
372 * @param last A forward iterator.
373 * @param binary_pred A binary predicate.
374 * @return The first iterator @c i such that @c i and @c i+1 are both
375 * valid iterators in @p [first,last) and such that
376 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
379 template<typename _ForwardIterator
, typename _BinaryPredicate
>
381 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
382 _BinaryPredicate __binary_pred
)
384 // concept requirements
385 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
386 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
387 typename iterator_traits
<_ForwardIterator
>::value_type
,
388 typename iterator_traits
<_ForwardIterator
>::value_type
>)
389 __glibcxx_requires_valid_range(__first
, __last
);
390 if (__first
== __last
)
392 _ForwardIterator __next
= __first
;
393 while(++__next
!= __last
)
395 if (__binary_pred(*__first
, *__next
))
403 * @brief Count the number of copies of a value in a sequence.
404 * @param first An input iterator.
405 * @param last An input iterator.
406 * @param value The value to be counted.
407 * @return The number of iterators @c i in the range @p [first,last)
408 * for which @c *i == @p value
410 template<typename _InputIterator
, typename _Tp
>
411 typename iterator_traits
<_InputIterator
>::difference_type
412 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
414 // concept requirements
415 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
416 __glibcxx_function_requires(_EqualityComparableConcept
<
417 typename iterator_traits
<_InputIterator
>::value_type
>)
418 __glibcxx_function_requires(_EqualityComparableConcept
<_Tp
>)
419 __glibcxx_requires_valid_range(__first
, __last
);
420 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
421 for ( ; __first
!= __last
; ++__first
)
422 if (*__first
== __value
)
428 * @brief Count the elements of a sequence for which a predicate is true.
429 * @param first An input iterator.
430 * @param last An input iterator.
431 * @param pred A predicate.
432 * @return The number of iterators @c i in the range @p [first,last)
433 * for which @p pred(*i) is true.
435 template<typename _InputIterator
, typename _Predicate
>
436 typename iterator_traits
<_InputIterator
>::difference_type
437 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
439 // concept requirements
440 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
441 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
442 typename iterator_traits
<_InputIterator
>::value_type
>)
443 __glibcxx_requires_valid_range(__first
, __last
);
444 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
445 for ( ; __first
!= __last
; ++__first
)
446 if (__pred(*__first
))
452 * @brief Search a sequence for a matching sub-sequence.
453 * @param first1 A forward iterator.
454 * @param last1 A forward iterator.
455 * @param first2 A forward iterator.
456 * @param last2 A forward iterator.
457 * @return The first iterator @c i in the range
458 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
459 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
460 * such iterator exists.
462 * Searches the range @p [first1,last1) for a sub-sequence that compares
463 * equal value-by-value with the sequence given by @p [first2,last2) and
464 * returns an iterator to the first element of the sub-sequence, or
465 * @p last1 if the sub-sequence is not found.
467 * Because the sub-sequence must lie completely within the range
468 * @p [first1,last1) it must start at a position less than
469 * @p last1-(last2-first2) where @p last2-first2 is the length of the
471 * This means that the returned iterator @c i will be in the range
472 * @p [first1,last1-(last2-first2))
474 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
476 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
477 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
479 // concept requirements
480 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
481 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
482 __glibcxx_function_requires(_EqualOpConcept
<
483 typename iterator_traits
<_ForwardIterator1
>::value_type
,
484 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
485 __glibcxx_requires_valid_range(__first1
, __last1
);
486 __glibcxx_requires_valid_range(__first2
, __last2
);
487 // Test for empty ranges
488 if (__first1
== __last1
|| __first2
== __last2
)
491 // Test for a pattern of length 1.
492 _ForwardIterator2
__tmp(__first2
);
494 if (__tmp
== __last2
)
495 return std::find(__first1
, __last1
, *__first2
);
498 _ForwardIterator2 __p1
, __p
;
499 __p1
= __first2
; ++__p1
;
500 _ForwardIterator1 __current
= __first1
;
502 while (__first1
!= __last1
)
504 __first1
= std::find(__first1
, __last1
, *__first2
);
505 if (__first1
== __last1
)
509 __current
= __first1
;
510 if (++__current
== __last1
)
513 while (*__current
== *__p
)
515 if (++__p
== __last2
)
517 if (++__current
== __last1
)
526 * @brief Search a sequence for a matching sub-sequence using a predicate.
527 * @param first1 A forward iterator.
528 * @param last1 A forward iterator.
529 * @param first2 A forward iterator.
530 * @param last2 A forward iterator.
531 * @param predicate A binary predicate.
532 * @return The first iterator @c i in the range
533 * @p [first1,last1-(last2-first2)) such that
534 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
535 * @p [0,last2-first2), or @p last1 if no such iterator exists.
537 * Searches the range @p [first1,last1) for a sub-sequence that compares
538 * equal value-by-value with the sequence given by @p [first2,last2),
539 * using @p predicate to determine equality, and returns an iterator
540 * to the first element of the sub-sequence, or @p last1 if no such
543 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
545 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
546 typename _BinaryPredicate
>
548 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
549 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
550 _BinaryPredicate __predicate
)
552 // concept requirements
553 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
554 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
555 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
556 typename iterator_traits
<_ForwardIterator1
>::value_type
,
557 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
558 __glibcxx_requires_valid_range(__first1
, __last1
);
559 __glibcxx_requires_valid_range(__first2
, __last2
);
561 // Test for empty ranges
562 if (__first1
== __last1
|| __first2
== __last2
)
565 // Test for a pattern of length 1.
566 _ForwardIterator2
__tmp(__first2
);
568 if (__tmp
== __last2
)
570 while (__first1
!= __last1
&& !__predicate(*__first1
, *__first2
))
576 _ForwardIterator2 __p1
, __p
;
577 __p1
= __first2
; ++__p1
;
578 _ForwardIterator1 __current
= __first1
;
580 while (__first1
!= __last1
)
582 while (__first1
!= __last1
)
584 if (__predicate(*__first1
, *__first2
))
588 while (__first1
!= __last1
&& !__predicate(*__first1
, *__first2
))
590 if (__first1
== __last1
)
594 __current
= __first1
;
595 if (++__current
== __last1
)
598 while (__predicate(*__current
, *__p
))
600 if (++__p
== __last2
)
602 if (++__current
== __last1
)
611 * @brief Search a sequence for a number of consecutive values.
612 * @param first A forward iterator.
613 * @param last A forward iterator.
614 * @param count The number of consecutive values.
615 * @param val The value to find.
616 * @return The first iterator @c i in the range @p [first,last-count)
617 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
618 * or @p last if no such iterator exists.
620 * Searches the range @p [first,last) for @p count consecutive elements
623 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
625 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
626 _Integer __count
, const _Tp
& __val
)
628 // concept requirements
629 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
630 __glibcxx_function_requires(_EqualityComparableConcept
<
631 typename iterator_traits
<_ForwardIterator
>::value_type
>)
632 __glibcxx_function_requires(_EqualityComparableConcept
<_Tp
>)
633 __glibcxx_requires_valid_range(__first
, __last
);
639 __first
= std::find(__first
, __last
, __val
);
640 while (__first
!= __last
)
642 typename iterator_traits
<_ForwardIterator
>::difference_type
644 _ForwardIterator __i
= __first
;
646 while (__i
!= __last
&& __n
!= 1 && *__i
== __val
)
654 __first
= std::find(__i
, __last
, __val
);
661 * @brief Search a sequence for a number of consecutive values using a
663 * @param first A forward iterator.
664 * @param last A forward iterator.
665 * @param count The number of consecutive values.
666 * @param val The value to find.
667 * @param binary_pred A binary predicate.
668 * @return The first iterator @c i in the range @p [first,last-count)
669 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
670 * range @p [0,count), or @p last if no such iterator exists.
672 * Searches the range @p [first,last) for @p count consecutive elements
673 * for which the predicate returns true.
675 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
676 typename _BinaryPredicate
>
678 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
679 _Integer __count
, const _Tp
& __val
,
680 _BinaryPredicate __binary_pred
)
682 // concept requirements
683 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
684 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
685 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
686 __glibcxx_requires_valid_range(__first
, __last
);
692 while (__first
!= __last
)
694 if (__binary_pred(*__first
, __val
))
698 while (__first
!= __last
)
700 typename iterator_traits
<_ForwardIterator
>::difference_type
702 _ForwardIterator __i
= __first
;
704 while (__i
!= __last
&& __n
!= 1 && __binary_pred(*__i
, __val
))
713 while (__i
!= __last
)
715 if (__binary_pred(*__i
, __val
))
727 * @brief Swap the elements of two sequences.
728 * @param first1 A forward iterator.
729 * @param last1 A forward iterator.
730 * @param first2 A forward iterator.
731 * @return An iterator equal to @p first2+(last1-first1).
733 * Swaps each element in the range @p [first1,last1) with the
734 * corresponding element in the range @p [first2,(last1-first1)).
735 * The ranges must not overlap.
737 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
739 swap_ranges(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
740 _ForwardIterator2 __first2
)
742 // concept requirements
743 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
745 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
747 __glibcxx_function_requires(_ConvertibleConcept
<
748 typename iterator_traits
<_ForwardIterator1
>::value_type
,
749 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
750 __glibcxx_function_requires(_ConvertibleConcept
<
751 typename iterator_traits
<_ForwardIterator2
>::value_type
,
752 typename iterator_traits
<_ForwardIterator1
>::value_type
>)
753 __glibcxx_requires_valid_range(__first1
, __last1
);
755 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
)
756 std::iter_swap(__first1
, __first2
);
761 * @brief Perform an operation on a sequence.
762 * @param first An input iterator.
763 * @param last An input iterator.
764 * @param result An output iterator.
765 * @param unary_op A unary operator.
766 * @return An output iterator equal to @p result+(last-first).
768 * Applies the operator to each element in the input range and assigns
769 * the results to successive elements of the output sequence.
770 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
771 * range @p [0,last-first).
773 * @p unary_op must not alter its argument.
775 template<typename _InputIterator
, typename _OutputIterator
,
776 typename _UnaryOperation
>
778 transform(_InputIterator __first
, _InputIterator __last
,
779 _OutputIterator __result
, _UnaryOperation __unary_op
)
781 // concept requirements
782 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
783 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
784 // "the type returned by a _UnaryOperation"
785 __typeof__(__unary_op(*__first
))>)
786 __glibcxx_requires_valid_range(__first
, __last
);
788 for ( ; __first
!= __last
; ++__first
, ++__result
)
789 *__result
= __unary_op(*__first
);
794 * @brief Perform an operation on corresponding elements of two sequences.
795 * @param first1 An input iterator.
796 * @param last1 An input iterator.
797 * @param first2 An input iterator.
798 * @param result An output iterator.
799 * @param binary_op A binary operator.
800 * @return An output iterator equal to @p result+(last-first).
802 * Applies the operator to the corresponding elements in the two
803 * input ranges and assigns the results to successive elements of the
805 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
806 * @c N in the range @p [0,last1-first1).
808 * @p binary_op must not alter either of its arguments.
810 template<typename _InputIterator1
, typename _InputIterator2
,
811 typename _OutputIterator
, typename _BinaryOperation
>
813 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
814 _InputIterator2 __first2
, _OutputIterator __result
,
815 _BinaryOperation __binary_op
)
817 // concept requirements
818 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
819 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
820 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
821 // "the type returned by a _BinaryOperation"
822 __typeof__(__binary_op(*__first1
,*__first2
))>)
823 __glibcxx_requires_valid_range(__first1
, __last1
);
825 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
826 *__result
= __binary_op(*__first1
, *__first2
);
831 * @brief Replace each occurrence of one value in a sequence with another
833 * @param first A forward iterator.
834 * @param last A forward iterator.
835 * @param old_value The value to be replaced.
836 * @param new_value The replacement value.
837 * @return replace() returns no value.
839 * For each iterator @c i in the range @p [first,last) if @c *i ==
840 * @p old_value then the assignment @c *i = @p new_value is performed.
842 template<typename _ForwardIterator
, typename _Tp
>
844 replace(_ForwardIterator __first
, _ForwardIterator __last
,
845 const _Tp
& __old_value
, const _Tp
& __new_value
)
847 // concept requirements
848 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
850 __glibcxx_function_requires(_EqualOpConcept
<
851 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
852 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
853 typename iterator_traits
<_ForwardIterator
>::value_type
>)
854 __glibcxx_requires_valid_range(__first
, __last
);
856 for ( ; __first
!= __last
; ++__first
)
857 if (*__first
== __old_value
)
858 *__first
= __new_value
;
862 * @brief Replace each value in a sequence for which a predicate returns
863 * true with another value.
864 * @param first A forward iterator.
865 * @param last A forward iterator.
866 * @param pred A predicate.
867 * @param new_value The replacement value.
868 * @return replace_if() returns no value.
870 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
871 * is true then the assignment @c *i = @p new_value is performed.
873 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
875 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
876 _Predicate __pred
, const _Tp
& __new_value
)
878 // concept requirements
879 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
881 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
882 typename iterator_traits
<_ForwardIterator
>::value_type
>)
883 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
884 typename iterator_traits
<_ForwardIterator
>::value_type
>)
885 __glibcxx_requires_valid_range(__first
, __last
);
887 for ( ; __first
!= __last
; ++__first
)
888 if (__pred(*__first
))
889 *__first
= __new_value
;
893 * @brief Copy a sequence, replacing each element of one value with another
895 * @param first An input iterator.
896 * @param last An input iterator.
897 * @param result An output iterator.
898 * @param old_value The value to be replaced.
899 * @param new_value The replacement value.
900 * @return The end of the output sequence, @p result+(last-first).
902 * Copies each element in the input range @p [first,last) to the
903 * output range @p [result,result+(last-first)) replacing elements
904 * equal to @p old_value with @p new_value.
906 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
908 replace_copy(_InputIterator __first
, _InputIterator __last
,
909 _OutputIterator __result
,
910 const _Tp
& __old_value
, const _Tp
& __new_value
)
912 // concept requirements
913 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
914 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
915 typename iterator_traits
<_InputIterator
>::value_type
>)
916 __glibcxx_function_requires(_EqualOpConcept
<
917 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
918 __glibcxx_requires_valid_range(__first
, __last
);
920 for ( ; __first
!= __last
; ++__first
, ++__result
)
921 *__result
= *__first
== __old_value
? __new_value
: *__first
;
926 * @brief Copy a sequence, replacing each value for which a predicate
927 * returns true with another value.
928 * @param first An input iterator.
929 * @param last An input iterator.
930 * @param result An output iterator.
931 * @param pred A predicate.
932 * @param new_value The replacement value.
933 * @return The end of the output sequence, @p result+(last-first).
935 * Copies each element in the range @p [first,last) to the range
936 * @p [result,result+(last-first)) replacing elements for which
937 * @p pred returns true with @p new_value.
939 template<typename _InputIterator
, typename _OutputIterator
,
940 typename _Predicate
, typename _Tp
>
942 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
943 _OutputIterator __result
,
944 _Predicate __pred
, const _Tp
& __new_value
)
946 // concept requirements
947 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
948 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
949 typename iterator_traits
<_InputIterator
>::value_type
>)
950 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
951 typename iterator_traits
<_InputIterator
>::value_type
>)
952 __glibcxx_requires_valid_range(__first
, __last
);
954 for ( ; __first
!= __last
; ++__first
, ++__result
)
955 *__result
= __pred(*__first
) ? __new_value
: *__first
;
960 * @brief Assign the result of a function object to each value in a
962 * @param first A forward iterator.
963 * @param last A forward iterator.
964 * @param gen A function object taking no arguments.
965 * @return generate() returns no value.
967 * Performs the assignment @c *i = @p gen() for each @c i in the range
970 template<typename _ForwardIterator
, typename _Generator
>
972 generate(_ForwardIterator __first
, _ForwardIterator __last
,
975 // concept requirements
976 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
977 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
978 typename iterator_traits
<_ForwardIterator
>::value_type
>)
979 __glibcxx_requires_valid_range(__first
, __last
);
981 for ( ; __first
!= __last
; ++__first
)
986 * @brief Assign the result of a function object to each value in a
988 * @param first A forward iterator.
989 * @param n The length of the sequence.
990 * @param gen A function object taking no arguments.
991 * @return The end of the sequence, @p first+n
993 * Performs the assignment @c *i = @p gen() for each @c i in the range
994 * @p [first,first+n).
996 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
998 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
1000 // concept requirements
1001 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1002 // "the type returned by a _Generator"
1003 __typeof__(__gen())>)
1005 for ( ; __n
> 0; --__n
, ++__first
)
1011 * @brief Copy a sequence, removing elements of a given value.
1012 * @param first An input iterator.
1013 * @param last An input iterator.
1014 * @param result An output iterator.
1015 * @param value The value to be removed.
1016 * @return An iterator designating the end of the resulting sequence.
1018 * Copies each element in the range @p [first,last) not equal to @p value
1019 * to the range beginning at @p result.
1020 * remove_copy() is stable, so the relative order of elements that are
1021 * copied is unchanged.
1023 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
1025 remove_copy(_InputIterator __first
, _InputIterator __last
,
1026 _OutputIterator __result
, const _Tp
& __value
)
1028 // concept requirements
1029 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1030 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1031 typename iterator_traits
<_InputIterator
>::value_type
>)
1032 __glibcxx_function_requires(_EqualOpConcept
<
1033 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
1034 __glibcxx_requires_valid_range(__first
, __last
);
1036 for ( ; __first
!= __last
; ++__first
)
1037 if (!(*__first
== __value
))
1039 *__result
= *__first
;
1046 * @brief Copy a sequence, removing elements for which a predicate is true.
1047 * @param first An input iterator.
1048 * @param last An input iterator.
1049 * @param result An output iterator.
1050 * @param pred A predicate.
1051 * @return An iterator designating the end of the resulting sequence.
1053 * Copies each element in the range @p [first,last) for which
1054 * @p pred returns true to the range beginning at @p result.
1056 * remove_copy_if() is stable, so the relative order of elements that are
1057 * copied is unchanged.
1059 template<typename _InputIterator
, typename _OutputIterator
,
1060 typename _Predicate
>
1062 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
1063 _OutputIterator __result
, _Predicate __pred
)
1065 // concept requirements
1066 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1067 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1068 typename iterator_traits
<_InputIterator
>::value_type
>)
1069 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1070 typename iterator_traits
<_InputIterator
>::value_type
>)
1071 __glibcxx_requires_valid_range(__first
, __last
);
1073 for ( ; __first
!= __last
; ++__first
)
1074 if (!__pred(*__first
))
1076 *__result
= *__first
;
1083 * @brief Remove elements from a sequence.
1084 * @param first An input iterator.
1085 * @param last An input iterator.
1086 * @param value The value to be removed.
1087 * @return An iterator designating the end of the resulting sequence.
1089 * All elements equal to @p value are removed from the range
1092 * remove() is stable, so the relative order of elements that are
1093 * not removed is unchanged.
1095 * Elements between the end of the resulting sequence and @p last
1096 * are still present, but their value is unspecified.
1098 template<typename _ForwardIterator
, typename _Tp
>
1100 remove(_ForwardIterator __first
, _ForwardIterator __last
,
1103 // concept requirements
1104 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1106 __glibcxx_function_requires(_EqualOpConcept
<
1107 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1108 __glibcxx_requires_valid_range(__first
, __last
);
1110 __first
= std::find(__first
, __last
, __value
);
1111 _ForwardIterator __i
= __first
;
1112 return __first
== __last
? __first
1113 : std::remove_copy(++__i
, __last
,
1118 * @brief Remove elements from a sequence using a predicate.
1119 * @param first A forward iterator.
1120 * @param last A forward iterator.
1121 * @param pred A predicate.
1122 * @return An iterator designating the end of the resulting sequence.
1124 * All elements for which @p pred returns true are removed from the range
1127 * remove_if() is stable, so the relative order of elements that are
1128 * not removed is unchanged.
1130 * Elements between the end of the resulting sequence and @p last
1131 * are still present, but their value is unspecified.
1133 template<typename _ForwardIterator
, typename _Predicate
>
1135 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1138 // concept requirements
1139 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1141 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1142 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1143 __glibcxx_requires_valid_range(__first
, __last
);
1145 __first
= std::find_if(__first
, __last
, __pred
);
1146 _ForwardIterator __i
= __first
;
1147 return __first
== __last
? __first
1148 : std::remove_copy_if(++__i
, __last
,
1154 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1156 * overloaded for output iterators.
1159 template<typename _InputIterator
, typename _OutputIterator
>
1161 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1162 _OutputIterator __result
,
1163 output_iterator_tag
)
1165 // concept requirements -- taken care of in dispatching function
1166 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1167 *__result
= __value
;
1168 while (++__first
!= __last
)
1169 if (!(__value
== *__first
))
1172 *++__result
= __value
;
1179 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1181 * overloaded for forward iterators.
1184 template<typename _InputIterator
, typename _ForwardIterator
>
1186 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1187 _ForwardIterator __result
,
1188 forward_iterator_tag
)
1190 // concept requirements -- taken care of in dispatching function
1191 *__result
= *__first
;
1192 while (++__first
!= __last
)
1193 if (!(*__result
== *__first
))
1194 *++__result
= *__first
;
1200 * This is an uglified
1201 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1203 * overloaded for output iterators.
1206 template<typename _InputIterator
, typename _OutputIterator
,
1207 typename _BinaryPredicate
>
1209 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1210 _OutputIterator __result
,
1211 _BinaryPredicate __binary_pred
,
1212 output_iterator_tag
)
1214 // concept requirements -- iterators already checked
1215 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1216 typename iterator_traits
<_InputIterator
>::value_type
,
1217 typename iterator_traits
<_InputIterator
>::value_type
>)
1219 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1220 *__result
= __value
;
1221 while (++__first
!= __last
)
1222 if (!__binary_pred(__value
, *__first
))
1225 *++__result
= __value
;
1232 * This is an uglified
1233 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1235 * overloaded for forward iterators.
1238 template<typename _InputIterator
, typename _ForwardIterator
,
1239 typename _BinaryPredicate
>
1241 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1242 _ForwardIterator __result
,
1243 _BinaryPredicate __binary_pred
,
1244 forward_iterator_tag
)
1246 // concept requirements -- iterators already checked
1247 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1248 typename iterator_traits
<_ForwardIterator
>::value_type
,
1249 typename iterator_traits
<_InputIterator
>::value_type
>)
1251 *__result
= *__first
;
1252 while (++__first
!= __last
)
1253 if (!__binary_pred(*__result
, *__first
)) *++__result
= *__first
;
1258 * @brief Copy a sequence, removing consecutive duplicate values.
1259 * @param first An input iterator.
1260 * @param last An input iterator.
1261 * @param result An output iterator.
1262 * @return An iterator designating the end of the resulting sequence.
1264 * Copies each element in the range @p [first,last) to the range
1265 * beginning at @p result, except that only the first element is copied
1266 * from groups of consecutive elements that compare equal.
1267 * unique_copy() is stable, so the relative order of elements that are
1268 * copied is unchanged.
1270 template<typename _InputIterator
, typename _OutputIterator
>
1271 inline _OutputIterator
1272 unique_copy(_InputIterator __first
, _InputIterator __last
,
1273 _OutputIterator __result
)
1275 // concept requirements
1276 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1277 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1278 typename iterator_traits
<_InputIterator
>::value_type
>)
1279 __glibcxx_function_requires(_EqualityComparableConcept
<
1280 typename iterator_traits
<_InputIterator
>::value_type
>)
1281 __glibcxx_requires_valid_range(__first
, __last
);
1283 typedef typename iterator_traits
<_OutputIterator
>::iterator_category
1286 if (__first
== __last
) return __result
;
1287 return std::__unique_copy(__first
, __last
, __result
, _IterType());
1291 * @brief Copy a sequence, removing consecutive values using a predicate.
1292 * @param first An input iterator.
1293 * @param last An input iterator.
1294 * @param result An output iterator.
1295 * @param binary_pred A binary predicate.
1296 * @return An iterator designating the end of the resulting sequence.
1298 * Copies each element in the range @p [first,last) to the range
1299 * beginning at @p result, except that only the first element is copied
1300 * from groups of consecutive elements for which @p binary_pred returns
1302 * unique_copy() is stable, so the relative order of elements that are
1303 * copied is unchanged.
1305 template<typename _InputIterator
, typename _OutputIterator
,
1306 typename _BinaryPredicate
>
1307 inline _OutputIterator
1308 unique_copy(_InputIterator __first
, _InputIterator __last
,
1309 _OutputIterator __result
,
1310 _BinaryPredicate __binary_pred
)
1312 // concept requirements -- predicates checked later
1313 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1314 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1315 typename iterator_traits
<_InputIterator
>::value_type
>)
1316 __glibcxx_requires_valid_range(__first
, __last
);
1318 typedef typename iterator_traits
<_OutputIterator
>::iterator_category
1321 if (__first
== __last
) return __result
;
1322 return std::__unique_copy(__first
, __last
, __result
,
1323 __binary_pred
, _IterType());
1327 * @brief Remove consecutive duplicate values from a sequence.
1328 * @param first A forward iterator.
1329 * @param last A forward iterator.
1330 * @return An iterator designating the end of the resulting sequence.
1332 * Removes all but the first element from each group of consecutive
1333 * values that compare equal.
1334 * unique() is stable, so the relative order of elements that are
1335 * not removed is unchanged.
1336 * Elements between the end of the resulting sequence and @p last
1337 * are still present, but their value is unspecified.
1339 template<typename _ForwardIterator
>
1341 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1343 // concept requirements
1344 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1346 __glibcxx_function_requires(_EqualityComparableConcept
<
1347 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1348 __glibcxx_requires_valid_range(__first
, __last
);
1350 // Skip the beginning, if already unique.
1351 __first
= std::adjacent_find(__first
, __last
);
1352 if (__first
== __last
)
1355 // Do the real copy work.
1356 _ForwardIterator __dest
= __first
;
1358 while (++__first
!= __last
)
1359 if (!(*__dest
== *__first
))
1360 *++__dest
= *__first
;
1365 * @brief Remove consecutive values from a sequence using a predicate.
1366 * @param first A forward iterator.
1367 * @param last A forward iterator.
1368 * @param binary_pred A binary predicate.
1369 * @return An iterator designating the end of the resulting sequence.
1371 * Removes all but the first element from each group of consecutive
1372 * values for which @p binary_pred returns true.
1373 * unique() is stable, so the relative order of elements that are
1374 * not removed is unchanged.
1375 * Elements between the end of the resulting sequence and @p last
1376 * are still present, but their value is unspecified.
1378 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1380 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1381 _BinaryPredicate __binary_pred
)
1383 // concept requirements
1384 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1386 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1387 typename iterator_traits
<_ForwardIterator
>::value_type
,
1388 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1389 __glibcxx_requires_valid_range(__first
, __last
);
1391 // Skip the beginning, if already unique.
1392 __first
= std::adjacent_find(__first
, __last
, __binary_pred
);
1393 if (__first
== __last
)
1396 // Do the real copy work.
1397 _ForwardIterator __dest
= __first
;
1399 while (++__first
!= __last
)
1400 if (!__binary_pred(*__dest
, *__first
))
1401 *++__dest
= *__first
;
1407 * This is an uglified reverse(_BidirectionalIterator,
1408 * _BidirectionalIterator)
1409 * overloaded for bidirectional iterators.
1412 template<typename _BidirectionalIterator
>
1414 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1415 bidirectional_iterator_tag
)
1418 if (__first
== __last
|| __first
== --__last
)
1422 std::iter_swap(__first
, __last
);
1429 * This is an uglified reverse(_BidirectionalIterator,
1430 * _BidirectionalIterator)
1431 * overloaded for random access iterators.
1434 template<typename _RandomAccessIterator
>
1436 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1437 random_access_iterator_tag
)
1439 if (__first
== __last
)
1442 while (__first
< __last
)
1444 std::iter_swap(__first
, __last
);
1451 * @brief Reverse a sequence.
1452 * @param first A bidirectional iterator.
1453 * @param last A bidirectional iterator.
1454 * @return reverse() returns no value.
1456 * Reverses the order of the elements in the range @p [first,last),
1457 * so that the first element becomes the last etc.
1458 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1459 * swaps @p *(first+i) and @p *(last-(i+1))
1461 template<typename _BidirectionalIterator
>
1463 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1465 // concept requirements
1466 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1467 _BidirectionalIterator
>)
1468 __glibcxx_requires_valid_range(__first
, __last
);
1469 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1473 * @brief Copy a sequence, reversing its elements.
1474 * @param first A bidirectional iterator.
1475 * @param last A bidirectional iterator.
1476 * @param result An output iterator.
1477 * @return An iterator designating the end of the resulting sequence.
1479 * Copies the elements in the range @p [first,last) to the range
1480 * @p [result,result+(last-first)) such that the order of the
1481 * elements is reversed.
1482 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1483 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1484 * The ranges @p [first,last) and @p [result,result+(last-first))
1487 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1489 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1490 _OutputIterator __result
)
1492 // concept requirements
1493 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1494 _BidirectionalIterator
>)
1495 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1496 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1497 __glibcxx_requires_valid_range(__first
, __last
);
1499 while (__first
!= __last
)
1502 *__result
= *__last
;
1511 * This is a helper function for the rotate algorithm specialized on RAIs.
1512 * It returns the greatest common divisor of two integer values.
1515 template<typename _EuclideanRingElement
>
1516 _EuclideanRingElement
1517 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1521 _EuclideanRingElement __t
= __m
% __n
;
1530 * This is a helper function for the rotate algorithm.
1533 template<typename _ForwardIterator
>
1535 __rotate(_ForwardIterator __first
,
1536 _ForwardIterator __middle
,
1537 _ForwardIterator __last
,
1538 forward_iterator_tag
)
1540 if (__first
== __middle
|| __last
== __middle
)
1543 _ForwardIterator __first2
= __middle
;
1546 swap(*__first
, *__first2
);
1549 if (__first
== __middle
)
1550 __middle
= __first2
;
1552 while (__first2
!= __last
);
1554 __first2
= __middle
;
1556 while (__first2
!= __last
)
1558 swap(*__first
, *__first2
);
1561 if (__first
== __middle
)
1562 __middle
= __first2
;
1563 else if (__first2
== __last
)
1564 __first2
= __middle
;
1570 * This is a helper function for the rotate algorithm.
1573 template<typename _BidirectionalIterator
>
1575 __rotate(_BidirectionalIterator __first
,
1576 _BidirectionalIterator __middle
,
1577 _BidirectionalIterator __last
,
1578 bidirectional_iterator_tag
)
1580 // concept requirements
1581 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1582 _BidirectionalIterator
>)
1584 if (__first
== __middle
|| __last
== __middle
)
1587 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1588 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1590 while (__first
!= __middle
&& __middle
!= __last
)
1592 swap(*__first
, *--__last
);
1596 if (__first
== __middle
)
1597 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1599 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1604 * This is a helper function for the rotate algorithm.
1607 template<typename _RandomAccessIterator
>
1609 __rotate(_RandomAccessIterator __first
,
1610 _RandomAccessIterator __middle
,
1611 _RandomAccessIterator __last
,
1612 random_access_iterator_tag
)
1614 // concept requirements
1615 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1616 _RandomAccessIterator
>)
1618 if (__first
== __middle
|| __last
== __middle
)
1621 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1623 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1626 const _Distance __n
= __last
- __first
;
1627 const _Distance __k
= __middle
- __first
;
1628 const _Distance __l
= __n
- __k
;
1632 std::swap_ranges(__first
, __middle
, __middle
);
1636 const _Distance __d
= __gcd(__n
, __k
);
1638 for (_Distance __i
= 0; __i
< __d
; __i
++)
1640 _ValueType __tmp
= *__first
;
1641 _RandomAccessIterator __p
= __first
;
1645 for (_Distance __j
= 0; __j
< __l
/ __d
; __j
++)
1647 if (__p
> __first
+ __l
)
1649 *__p
= *(__p
- __l
);
1653 *__p
= *(__p
+ __k
);
1659 for (_Distance __j
= 0; __j
< __k
/ __d
- 1; __j
++)
1661 if (__p
< __last
- __k
)
1663 *__p
= *(__p
+ __k
);
1666 *__p
= * (__p
- __l
);
1677 * @brief Rotate the elements of a sequence.
1678 * @param first A forward iterator.
1679 * @param middle A forward iterator.
1680 * @param last A forward iterator.
1683 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1684 * positions so that the element at @p middle is moved to @p first, the
1685 * element at @p middle+1 is moved to @first+1 and so on for each element
1686 * in the range @p [first,last).
1688 * This effectively swaps the ranges @p [first,middle) and
1691 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1692 * each @p n in the range @p [0,last-first).
1694 template<typename _ForwardIterator
>
1696 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1697 _ForwardIterator __last
)
1699 // concept requirements
1700 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1702 __glibcxx_requires_valid_range(__first
, __middle
);
1703 __glibcxx_requires_valid_range(__middle
, __last
);
1705 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1707 std::__rotate(__first
, __middle
, __last
, _IterType());
1711 * @brief Copy a sequence, rotating its elements.
1712 * @param first A forward iterator.
1713 * @param middle A forward iterator.
1714 * @param last A forward iterator.
1715 * @param result An output iterator.
1716 * @return An iterator designating the end of the resulting sequence.
1718 * Copies the elements of the range @p [first,last) to the range
1719 * beginning at @result, rotating the copied elements by @p (middle-first)
1720 * positions so that the element at @p middle is moved to @p result, the
1721 * element at @p middle+1 is moved to @result+1 and so on for each element
1722 * in the range @p [first,last).
1724 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1725 * each @p n in the range @p [0,last-first).
1727 template<typename _ForwardIterator
, typename _OutputIterator
>
1729 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1730 _ForwardIterator __last
, _OutputIterator __result
)
1732 // concept requirements
1733 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1734 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1735 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1736 __glibcxx_requires_valid_range(__first
, __middle
);
1737 __glibcxx_requires_valid_range(__middle
, __last
);
1739 return std::copy(__first
, __middle
,
1740 std::copy(__middle
, __last
, __result
));
1744 * @brief Randomly shuffle the elements of a sequence.
1745 * @param first A forward iterator.
1746 * @param last A forward iterator.
1749 * Reorder the elements in the range @p [first,last) using a random
1750 * distribution, so that every possible ordering of the sequence is
1753 template<typename _RandomAccessIterator
>
1755 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
1757 // concept requirements
1758 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1759 _RandomAccessIterator
>)
1760 __glibcxx_requires_valid_range(__first
, __last
);
1762 if (__first
!= __last
)
1763 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1764 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
1768 * @brief Shuffle the elements of a sequence using a random number
1770 * @param first A forward iterator.
1771 * @param last A forward iterator.
1772 * @param rand The RNG functor or function.
1775 * Reorders the elements in the range @p [first,last) using @p rand to
1776 * provide a random distribution. Calling @p rand(N) for a positive
1777 * integer @p N should return a randomly chosen integer from the
1780 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
1782 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1783 _RandomNumberGenerator
& __rand
)
1785 // concept requirements
1786 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1787 _RandomAccessIterator
>)
1788 __glibcxx_requires_valid_range(__first
, __last
);
1790 if (__first
== __last
)
1792 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1793 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
1799 * This is a helper function...
1802 template<typename _ForwardIterator
, typename _Predicate
>
1804 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1806 forward_iterator_tag
)
1808 if (__first
== __last
)
1811 while (__pred(*__first
))
1812 if (++__first
== __last
)
1815 _ForwardIterator __next
= __first
;
1817 while (++__next
!= __last
)
1818 if (__pred(*__next
))
1820 swap(*__first
, *__next
);
1829 * This is a helper function...
1832 template<typename _BidirectionalIterator
, typename _Predicate
>
1833 _BidirectionalIterator
1834 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1836 bidirectional_iterator_tag
)
1841 if (__first
== __last
)
1843 else if (__pred(*__first
))
1849 if (__first
== __last
)
1851 else if (!__pred(*__last
))
1855 std::iter_swap(__first
, __last
);
1861 * @brief Move elements for which a predicate is true to the beginning
1863 * @param first A forward iterator.
1864 * @param last A forward iterator.
1865 * @param pred A predicate functor.
1866 * @return An iterator @p middle such that @p pred(i) is true for each
1867 * iterator @p i in the range @p [first,middle) and false for each @p i
1868 * in the range @p [middle,last).
1870 * @p pred must not modify its operand. @p partition() does not preserve
1871 * the relative ordering of elements in each group, use
1872 * @p stable_partition() if this is needed.
1874 template<typename _ForwardIterator
, typename _Predicate
>
1875 inline _ForwardIterator
1876 partition(_ForwardIterator __first
, _ForwardIterator __last
,
1879 // concept requirements
1880 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1882 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1883 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1884 __glibcxx_requires_valid_range(__first
, __last
);
1886 return std::__partition(__first
, __last
, __pred
,
1887 std::__iterator_category(__first
));
1893 * This is a helper function...
1896 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1898 __inplace_stable_partition(_ForwardIterator __first
,
1899 _ForwardIterator __last
,
1900 _Predicate __pred
, _Distance __len
)
1903 return __pred(*__first
) ? __last
: __first
;
1904 _ForwardIterator __middle
= __first
;
1905 std::advance(__middle
, __len
/ 2);
1906 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1910 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1914 std::rotate(__begin
, __middle
, __end
);
1915 std::advance(__begin
, std::distance(__middle
, __end
));
1921 * This is a helper function...
1924 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1927 __stable_partition_adaptive(_ForwardIterator __first
,
1928 _ForwardIterator __last
,
1929 _Predicate __pred
, _Distance __len
,
1931 _Distance __buffer_size
)
1933 if (__len
<= __buffer_size
)
1935 _ForwardIterator __result1
= __first
;
1936 _Pointer __result2
= __buffer
;
1937 for ( ; __first
!= __last
; ++__first
)
1938 if (__pred(*__first
))
1940 *__result1
= *__first
;
1945 *__result2
= *__first
;
1948 std::copy(__buffer
, __result2
, __result1
);
1953 _ForwardIterator __middle
= __first
;
1954 std::advance(__middle
, __len
/ 2);
1955 _ForwardIterator __begin
=
1956 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1957 __len
/ 2, __buffer
,
1959 _ForwardIterator __end
=
1960 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1962 __buffer
, __buffer_size
);
1963 std::rotate(__begin
, __middle
, __end
);
1964 std::advance(__begin
, std::distance(__middle
, __end
));
1970 * @brief Move elements for which a predicate is true to the beginning
1971 * of a sequence, preserving relative ordering.
1972 * @param first A forward iterator.
1973 * @param last A forward iterator.
1974 * @param pred A predicate functor.
1975 * @return An iterator @p middle such that @p pred(i) is true for each
1976 * iterator @p i in the range @p [first,middle) and false for each @p i
1977 * in the range @p [middle,last).
1979 * Performs the same function as @p partition() with the additional
1980 * guarantee that the relative ordering of elements in each group is
1981 * preserved, so any two elements @p x and @p y in the range
1982 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1983 * relative ordering after calling @p stable_partition().
1985 template<typename _ForwardIterator
, typename _Predicate
>
1987 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1990 // concept requirements
1991 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1993 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1994 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1995 __glibcxx_requires_valid_range(__first
, __last
);
1997 if (__first
== __last
)
2001 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2003 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2006 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
2008 if (__buf
.size() > 0)
2010 std::__stable_partition_adaptive(__first
, __last
, __pred
,
2011 _DistanceType(__buf
.requested_size()),
2012 __buf
.begin(), __buf
.size());
2015 std::__inplace_stable_partition(__first
, __last
, __pred
,
2016 _DistanceType(__buf
.requested_size()));
2022 * This is a helper function...
2025 template<typename _RandomAccessIterator
, typename _Tp
>
2026 _RandomAccessIterator
2027 __unguarded_partition(_RandomAccessIterator __first
,
2028 _RandomAccessIterator __last
, _Tp __pivot
)
2032 while (*__first
< __pivot
)
2035 while (__pivot
< *__last
)
2037 if (!(__first
< __last
))
2039 std::iter_swap(__first
, __last
);
2046 * This is a helper function...
2049 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2050 _RandomAccessIterator
2051 __unguarded_partition(_RandomAccessIterator __first
,
2052 _RandomAccessIterator __last
,
2053 _Tp __pivot
, _Compare __comp
)
2057 while (__comp(*__first
, __pivot
))
2060 while (__comp(__pivot
, *__last
))
2062 if (!(__first
< __last
))
2064 std::iter_swap(__first
, __last
);
2072 * This controls some aspect of the sort routines.
2075 enum { _S_threshold
= 16 };
2079 * This is a helper function for the sort routine.
2082 template<typename _RandomAccessIterator
, typename _Tp
>
2084 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
2086 _RandomAccessIterator __next
= __last
;
2088 while (__val
< *__next
)
2099 * This is a helper function for the sort routine.
2102 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2104 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
2107 _RandomAccessIterator __next
= __last
;
2109 while (__comp(__val
, *__next
))
2120 * This is a helper function for the sort routine.
2123 template<typename _RandomAccessIterator
>
2125 __insertion_sort(_RandomAccessIterator __first
,
2126 _RandomAccessIterator __last
)
2128 if (__first
== __last
)
2131 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2133 typename iterator_traits
<_RandomAccessIterator
>::value_type
2135 if (__val
< *__first
)
2137 std::copy_backward(__first
, __i
, __i
+ 1);
2141 std::__unguarded_linear_insert(__i
, __val
);
2147 * This is a helper function for the sort routine.
2150 template<typename _RandomAccessIterator
, typename _Compare
>
2152 __insertion_sort(_RandomAccessIterator __first
,
2153 _RandomAccessIterator __last
, _Compare __comp
)
2155 if (__first
== __last
) return;
2157 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2159 typename iterator_traits
<_RandomAccessIterator
>::value_type
2161 if (__comp(__val
, *__first
))
2163 std::copy_backward(__first
, __i
, __i
+ 1);
2167 std::__unguarded_linear_insert(__i
, __val
, __comp
);
2173 * This is a helper function for the sort routine.
2176 template<typename _RandomAccessIterator
>
2178 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2179 _RandomAccessIterator __last
)
2181 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2184 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2185 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
2190 * This is a helper function for the sort routine.
2193 template<typename _RandomAccessIterator
, typename _Compare
>
2195 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2196 _RandomAccessIterator __last
, _Compare __comp
)
2198 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2201 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2202 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
2207 * This is a helper function for the sort routine.
2210 template<typename _RandomAccessIterator
>
2212 __final_insertion_sort(_RandomAccessIterator __first
,
2213 _RandomAccessIterator __last
)
2215 if (__last
- __first
> int(_S_threshold
))
2217 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
2218 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
2221 std::__insertion_sort(__first
, __last
);
2226 * This is a helper function for the sort routine.
2229 template<typename _RandomAccessIterator
, typename _Compare
>
2231 __final_insertion_sort(_RandomAccessIterator __first
,
2232 _RandomAccessIterator __last
, _Compare __comp
)
2234 if (__last
- __first
> int(_S_threshold
))
2236 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
2237 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
2241 std::__insertion_sort(__first
, __last
, __comp
);
2246 * This is a helper function for the sort routine.
2249 template<typename _Size
>
2254 for (__k
= 0; __n
!= 1; __n
>>= 1)
2260 * @brief Sort the smallest elements of a sequence.
2261 * @param first An iterator.
2262 * @param middle Another iterator.
2263 * @param last Another iterator.
2266 * Sorts the smallest @p (middle-first) elements in the range
2267 * @p [first,last) and moves them to the range @p [first,middle). The
2268 * order of the remaining elements in the range @p [middle,last) is
2270 * After the sort if @p i and @j are iterators in the range
2271 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2272 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
2274 template<typename _RandomAccessIterator
>
2276 partial_sort(_RandomAccessIterator __first
,
2277 _RandomAccessIterator __middle
,
2278 _RandomAccessIterator __last
)
2280 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2283 // concept requirements
2284 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2285 _RandomAccessIterator
>)
2286 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2287 __glibcxx_requires_valid_range(__first
, __middle
);
2288 __glibcxx_requires_valid_range(__middle
, __last
);
2290 std::make_heap(__first
, __middle
);
2291 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
2292 if (*__i
< *__first
)
2293 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
));
2294 std::sort_heap(__first
, __middle
);
2298 * @brief Sort the smallest elements of a sequence using a predicate
2300 * @param first An iterator.
2301 * @param middle Another iterator.
2302 * @param last Another iterator.
2303 * @param comp A comparison functor.
2306 * Sorts the smallest @p (middle-first) elements in the range
2307 * @p [first,last) and moves them to the range @p [first,middle). The
2308 * order of the remaining elements in the range @p [middle,last) is
2310 * After the sort if @p i and @j are iterators in the range
2311 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2312 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
2315 template<typename _RandomAccessIterator
, typename _Compare
>
2317 partial_sort(_RandomAccessIterator __first
,
2318 _RandomAccessIterator __middle
,
2319 _RandomAccessIterator __last
,
2322 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2325 // concept requirements
2326 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2327 _RandomAccessIterator
>)
2328 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2329 _ValueType
, _ValueType
>)
2330 __glibcxx_requires_valid_range(__first
, __middle
);
2331 __glibcxx_requires_valid_range(__middle
, __last
);
2333 std::make_heap(__first
, __middle
, __comp
);
2334 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
2335 if (__comp(*__i
, *__first
))
2336 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
), __comp
);
2337 std::sort_heap(__first
, __middle
, __comp
);
2341 * @brief Copy the smallest elements of a sequence.
2342 * @param first An iterator.
2343 * @param last Another iterator.
2344 * @param result_first A random-access iterator.
2345 * @param result_last Another random-access iterator.
2346 * @return An iterator indicating the end of the resulting sequence.
2348 * Copies and sorts the smallest N values from the range @p [first,last)
2349 * to the range beginning at @p result_first, where the number of
2350 * elements to be copied, @p N, is the smaller of @p (last-first) and
2351 * @p (result_last-result_first).
2352 * After the sort if @p i and @j are iterators in the range
2353 * @p [result_first,result_first+N) such that @i precedes @j then
2354 * @p *j<*i is false.
2355 * The value returned is @p result_first+N.
2357 template<typename _InputIterator
, typename _RandomAccessIterator
>
2358 _RandomAccessIterator
2359 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2360 _RandomAccessIterator __result_first
,
2361 _RandomAccessIterator __result_last
)
2363 typedef typename iterator_traits
<_InputIterator
>::value_type
2365 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2367 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2370 // concept requirements
2371 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2372 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2374 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
2375 __glibcxx_function_requires(_LessThanComparableConcept
<_InputValueType
>)
2376 __glibcxx_requires_valid_range(__first
, __last
);
2377 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2379 if (__result_first
== __result_last
)
2380 return __result_last
;
2381 _RandomAccessIterator __result_real_last
= __result_first
;
2382 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2384 *__result_real_last
= *__first
;
2385 ++__result_real_last
;
2388 std::make_heap(__result_first
, __result_real_last
);
2389 while (__first
!= __last
)
2391 if (*__first
< *__result_first
)
2392 std::__adjust_heap(__result_first
, _DistanceType(0),
2393 _DistanceType(__result_real_last
2395 _InputValueType(*__first
));
2398 std::sort_heap(__result_first
, __result_real_last
);
2399 return __result_real_last
;
2403 * @brief Copy the smallest elements of a sequence using a predicate for
2405 * @param first An input iterator.
2406 * @param last Another input iterator.
2407 * @param result_first A random-access iterator.
2408 * @param result_last Another random-access iterator.
2409 * @param comp A comparison functor.
2410 * @return An iterator indicating the end of the resulting sequence.
2412 * Copies and sorts the smallest N values from the range @p [first,last)
2413 * to the range beginning at @p result_first, where the number of
2414 * elements to be copied, @p N, is the smaller of @p (last-first) and
2415 * @p (result_last-result_first).
2416 * After the sort if @p i and @j are iterators in the range
2417 * @p [result_first,result_first+N) such that @i precedes @j then
2418 * @p comp(*j,*i) is false.
2419 * The value returned is @p result_first+N.
2421 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
2422 _RandomAccessIterator
2423 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2424 _RandomAccessIterator __result_first
,
2425 _RandomAccessIterator __result_last
,
2428 typedef typename iterator_traits
<_InputIterator
>::value_type
2430 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2432 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2435 // concept requirements
2436 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2437 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2438 _RandomAccessIterator
>)
2439 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2441 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2442 _OutputValueType
, _OutputValueType
>)
2443 __glibcxx_requires_valid_range(__first
, __last
);
2444 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2446 if (__result_first
== __result_last
)
2447 return __result_last
;
2448 _RandomAccessIterator __result_real_last
= __result_first
;
2449 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2451 *__result_real_last
= *__first
;
2452 ++__result_real_last
;
2455 std::make_heap(__result_first
, __result_real_last
, __comp
);
2456 while (__first
!= __last
)
2458 if (__comp(*__first
, *__result_first
))
2459 std::__adjust_heap(__result_first
, _DistanceType(0),
2460 _DistanceType(__result_real_last
2462 _InputValueType(*__first
),
2466 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2467 return __result_real_last
;
2472 * This is a helper function for the sort routine.
2475 template<typename _RandomAccessIterator
, typename _Size
>
2477 __introsort_loop(_RandomAccessIterator __first
,
2478 _RandomAccessIterator __last
,
2479 _Size __depth_limit
)
2481 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2484 while (__last
- __first
> int(_S_threshold
))
2486 if (__depth_limit
== 0)
2488 std::partial_sort(__first
, __last
, __last
);
2492 _RandomAccessIterator __cut
=
2493 std::__unguarded_partition(__first
, __last
,
2494 _ValueType(std::__median(*__first
,
2501 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2508 * This is a helper function for the sort routine.
2511 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2513 __introsort_loop(_RandomAccessIterator __first
,
2514 _RandomAccessIterator __last
,
2515 _Size __depth_limit
, _Compare __comp
)
2517 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2520 while (__last
- __first
> int(_S_threshold
))
2522 if (__depth_limit
== 0)
2524 std::partial_sort(__first
, __last
, __last
, __comp
);
2528 _RandomAccessIterator __cut
=
2529 std::__unguarded_partition(__first
, __last
,
2530 _ValueType(std::__median(*__first
,
2538 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2544 * @brief Sort the elements of a sequence.
2545 * @param first An iterator.
2546 * @param last Another iterator.
2549 * Sorts the elements in the range @p [first,last) in ascending order,
2550 * such that @p *(i+1)<*i is false for each iterator @p i in the range
2551 * @p [first,last-1).
2553 * The relative ordering of equivalent elements is not preserved, use
2554 * @p stable_sort() if this is needed.
2556 template<typename _RandomAccessIterator
>
2558 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
2560 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2563 // concept requirements
2564 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2565 _RandomAccessIterator
>)
2566 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2567 __glibcxx_requires_valid_range(__first
, __last
);
2569 if (__first
!= __last
)
2571 std::__introsort_loop(__first
, __last
, __lg(__last
- __first
) * 2);
2572 std::__final_insertion_sort(__first
, __last
);
2577 * @brief Sort the elements of a sequence using a predicate for comparison.
2578 * @param first An iterator.
2579 * @param last Another iterator.
2580 * @param comp A comparison functor.
2583 * Sorts the elements in the range @p [first,last) in ascending order,
2584 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
2585 * range @p [first,last-1).
2587 * The relative ordering of equivalent elements is not preserved, use
2588 * @p stable_sort() if this is needed.
2590 template<typename _RandomAccessIterator
, typename _Compare
>
2592 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2595 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2598 // concept requirements
2599 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2600 _RandomAccessIterator
>)
2601 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
2603 __glibcxx_requires_valid_range(__first
, __last
);
2605 if (__first
!= __last
)
2607 std::__introsort_loop(__first
, __last
, __lg(__last
- __first
) * 2,
2609 std::__final_insertion_sort(__first
, __last
, __comp
);
2614 * @brief Finds the first position in which @a val could be inserted
2615 * without changing the ordering.
2616 * @param first An iterator.
2617 * @param last Another iterator.
2618 * @param val The search term.
2619 * @return An iterator pointing to the first element "not less than" @a val,
2620 * or end() if every element is less than @a val.
2621 * @ingroup binarysearch
2623 template<typename _ForwardIterator
, typename _Tp
>
2625 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2628 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2630 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2633 // concept requirements
2634 // Note that these are slightly stricter than those of the 4-argument
2635 // version, defined next. The difference is in the strictness of the
2636 // comparison operations... so for looser checking, define your own
2637 // comparison function, as was intended.
2638 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2639 __glibcxx_function_requires(_SameTypeConcept
<_Tp
, _ValueType
>)
2640 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
2641 __glibcxx_requires_partitioned(__first
, __last
, __val
);
2643 _DistanceType __len
= std::distance(__first
, __last
);
2644 _DistanceType __half
;
2645 _ForwardIterator __middle
;
2649 __half
= __len
>> 1;
2651 std::advance(__middle
, __half
);
2652 if (*__middle
< __val
)
2656 __len
= __len
- __half
- 1;
2665 * @brief Finds the first position in which @a val could be inserted
2666 * without changing the ordering.
2667 * @param first An iterator.
2668 * @param last Another iterator.
2669 * @param val The search term.
2670 * @param comp A functor to use for comparisons.
2671 * @return An iterator pointing to the first element "not less than" @a val,
2672 * or end() if every element is less than @a val.
2673 * @ingroup binarysearch
2675 * The comparison function should have the same effects on ordering as
2676 * the function used for the initial sort.
2678 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2680 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2681 const _Tp
& __val
, _Compare __comp
)
2683 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2685 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2688 // concept requirements
2689 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2690 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2692 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
2694 _DistanceType __len
= std::distance(__first
, __last
);
2695 _DistanceType __half
;
2696 _ForwardIterator __middle
;
2700 __half
= __len
>> 1;
2702 std::advance(__middle
, __half
);
2703 if (__comp(*__middle
, __val
))
2707 __len
= __len
- __half
- 1;
2716 * @brief Finds the last position in which @a val could be inserted
2717 * without changing the ordering.
2718 * @param first An iterator.
2719 * @param last Another iterator.
2720 * @param val The search term.
2721 * @return An iterator pointing to the first element greater than @a val,
2722 * or end() if no elements are greater than @a val.
2723 * @ingroup binarysearch
2725 template<typename _ForwardIterator
, typename _Tp
>
2727 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2730 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2732 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2735 // concept requirements
2736 // See comments on lower_bound.
2737 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2738 __glibcxx_function_requires(_SameTypeConcept
<_Tp
, _ValueType
>)
2739 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
2740 __glibcxx_requires_partitioned(__first
, __last
, __val
);
2742 _DistanceType __len
= std::distance(__first
, __last
);
2743 _DistanceType __half
;
2744 _ForwardIterator __middle
;
2748 __half
= __len
>> 1;
2750 std::advance(__middle
, __half
);
2751 if (__val
< *__middle
)
2757 __len
= __len
- __half
- 1;
2764 * @brief Finds the last position in which @a val could be inserted
2765 * without changing the ordering.
2766 * @param first An iterator.
2767 * @param last Another iterator.
2768 * @param val The search term.
2769 * @param comp A functor to use for comparisons.
2770 * @return An iterator pointing to the first element greater than @a val,
2771 * or end() if no elements are greater than @a val.
2772 * @ingroup binarysearch
2774 * The comparison function should have the same effects on ordering as
2775 * the function used for the initial sort.
2777 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2779 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2780 const _Tp
& __val
, _Compare __comp
)
2782 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2784 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2787 // concept requirements
2788 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2789 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2791 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
2793 _DistanceType __len
= std::distance(__first
, __last
);
2794 _DistanceType __half
;
2795 _ForwardIterator __middle
;
2799 __half
= __len
>> 1;
2801 std::advance(__middle
, __half
);
2802 if (__comp(__val
, *__middle
))
2808 __len
= __len
- __half
- 1;
2816 * This is a helper function for the merge routines.
2819 template<typename _BidirectionalIterator
, typename _Distance
>
2821 __merge_without_buffer(_BidirectionalIterator __first
,
2822 _BidirectionalIterator __middle
,
2823 _BidirectionalIterator __last
,
2824 _Distance __len1
, _Distance __len2
)
2826 if (__len1
== 0 || __len2
== 0)
2828 if (__len1
+ __len2
== 2)
2830 if (*__middle
< *__first
)
2831 std::iter_swap(__first
, __middle
);
2834 _BidirectionalIterator __first_cut
= __first
;
2835 _BidirectionalIterator __second_cut
= __middle
;
2836 _Distance __len11
= 0;
2837 _Distance __len22
= 0;
2838 if (__len1
> __len2
)
2840 __len11
= __len1
/ 2;
2841 std::advance(__first_cut
, __len11
);
2842 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
2843 __len22
= std::distance(__middle
, __second_cut
);
2847 __len22
= __len2
/ 2;
2848 std::advance(__second_cut
, __len22
);
2849 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
2850 __len11
= std::distance(__first
, __first_cut
);
2852 std::rotate(__first_cut
, __middle
, __second_cut
);
2853 _BidirectionalIterator __new_middle
= __first_cut
;
2854 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2855 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2857 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2858 __len1
- __len11
, __len2
- __len22
);
2863 * This is a helper function for the merge routines.
2866 template<typename _BidirectionalIterator
, typename _Distance
,
2869 __merge_without_buffer(_BidirectionalIterator __first
,
2870 _BidirectionalIterator __middle
,
2871 _BidirectionalIterator __last
,
2872 _Distance __len1
, _Distance __len2
,
2875 if (__len1
== 0 || __len2
== 0)
2877 if (__len1
+ __len2
== 2)
2879 if (__comp(*__middle
, *__first
))
2880 std::iter_swap(__first
, __middle
);
2883 _BidirectionalIterator __first_cut
= __first
;
2884 _BidirectionalIterator __second_cut
= __middle
;
2885 _Distance __len11
= 0;
2886 _Distance __len22
= 0;
2887 if (__len1
> __len2
)
2889 __len11
= __len1
/ 2;
2890 std::advance(__first_cut
, __len11
);
2891 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2893 __len22
= std::distance(__middle
, __second_cut
);
2897 __len22
= __len2
/ 2;
2898 std::advance(__second_cut
, __len22
);
2899 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2901 __len11
= std::distance(__first
, __first_cut
);
2903 std::rotate(__first_cut
, __middle
, __second_cut
);
2904 _BidirectionalIterator __new_middle
= __first_cut
;
2905 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2906 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2907 __len11
, __len22
, __comp
);
2908 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2909 __len1
- __len11
, __len2
- __len22
, __comp
);
2914 * This is a helper function for the stable sorting routines.
2917 template<typename _RandomAccessIterator
>
2919 __inplace_stable_sort(_RandomAccessIterator __first
,
2920 _RandomAccessIterator __last
)
2922 if (__last
- __first
< 15)
2924 std::__insertion_sort(__first
, __last
);
2927 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2928 std::__inplace_stable_sort(__first
, __middle
);
2929 std::__inplace_stable_sort(__middle
, __last
);
2930 std::__merge_without_buffer(__first
, __middle
, __last
,
2937 * This is a helper function for the stable sorting routines.
2940 template<typename _RandomAccessIterator
, typename _Compare
>
2942 __inplace_stable_sort(_RandomAccessIterator __first
,
2943 _RandomAccessIterator __last
, _Compare __comp
)
2945 if (__last
- __first
< 15)
2947 std::__insertion_sort(__first
, __last
, __comp
);
2950 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2951 std::__inplace_stable_sort(__first
, __middle
, __comp
);
2952 std::__inplace_stable_sort(__middle
, __last
, __comp
);
2953 std::__merge_without_buffer(__first
, __middle
, __last
,
2960 * @brief Merges two sorted ranges.
2961 * @param first1 An iterator.
2962 * @param first2 Another iterator.
2963 * @param last1 Another iterator.
2964 * @param last2 Another iterator.
2965 * @param result An iterator pointing to the end of the merged range.
2966 * @return An iterator pointing to the first element "not less than" @a val.
2968 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
2969 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
2970 * must be sorted, and the output range must not overlap with either of
2971 * the input ranges. The sort is @e stable, that is, for equivalent
2972 * elements in the two ranges, elements from the first range will always
2973 * come before elements from the second.
2975 template<typename _InputIterator1
, typename _InputIterator2
,
2976 typename _OutputIterator
>
2978 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
2979 _InputIterator2 __first2
, _InputIterator2 __last2
,
2980 _OutputIterator __result
)
2982 // concept requirements
2983 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2984 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2985 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
2986 typename iterator_traits
<_InputIterator1
>::value_type
>)
2987 __glibcxx_function_requires(_SameTypeConcept
<
2988 typename iterator_traits
<_InputIterator1
>::value_type
,
2989 typename iterator_traits
<_InputIterator2
>::value_type
>)
2990 __glibcxx_function_requires(_LessThanComparableConcept
<
2991 typename iterator_traits
<_InputIterator1
>::value_type
>)
2992 __glibcxx_requires_sorted(__first1
, __last1
);
2993 __glibcxx_requires_sorted(__first2
, __last2
);
2995 while (__first1
!= __last1
&& __first2
!= __last2
)
2997 if (*__first2
< *__first1
)
2999 *__result
= *__first2
;
3004 *__result
= *__first1
;
3009 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
3014 * @brief Merges two sorted ranges.
3015 * @param first1 An iterator.
3016 * @param first2 Another iterator.
3017 * @param last1 Another iterator.
3018 * @param last2 Another iterator.
3019 * @param result An iterator pointing to the end of the merged range.
3020 * @param comp A functor to use for comparisons.
3021 * @return An iterator pointing to the first element "not less than" @a val.
3023 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
3024 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
3025 * must be sorted, and the output range must not overlap with either of
3026 * the input ranges. The sort is @e stable, that is, for equivalent
3027 * elements in the two ranges, elements from the first range will always
3028 * come before elements from the second.
3030 * The comparison function should have the same effects on ordering as
3031 * the function used for the initial sort.
3033 template<typename _InputIterator1
, typename _InputIterator2
,
3034 typename _OutputIterator
, typename _Compare
>
3036 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3037 _InputIterator2 __first2
, _InputIterator2 __last2
,
3038 _OutputIterator __result
, _Compare __comp
)
3040 // concept requirements
3041 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3042 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3043 __glibcxx_function_requires(_SameTypeConcept
<
3044 typename iterator_traits
<_InputIterator1
>::value_type
,
3045 typename iterator_traits
<_InputIterator2
>::value_type
>)
3046 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3047 typename iterator_traits
<_InputIterator1
>::value_type
>)
3048 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3049 typename iterator_traits
<_InputIterator1
>::value_type
,
3050 typename iterator_traits
<_InputIterator2
>::value_type
>)
3051 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
3052 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
3054 while (__first1
!= __last1
&& __first2
!= __last2
)
3056 if (__comp(*__first2
, *__first1
))
3058 *__result
= *__first2
;
3063 *__result
= *__first1
;
3068 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
3072 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3075 __merge_sort_loop(_RandomAccessIterator1 __first
,
3076 _RandomAccessIterator1 __last
,
3077 _RandomAccessIterator2 __result
,
3078 _Distance __step_size
)
3080 const _Distance __two_step
= 2 * __step_size
;
3082 while (__last
- __first
>= __two_step
)
3084 __result
= std::merge(__first
, __first
+ __step_size
,
3085 __first
+ __step_size
, __first
+ __two_step
,
3087 __first
+= __two_step
;
3090 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3091 std::merge(__first
, __first
+ __step_size
, __first
+ __step_size
, __last
,
3095 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3096 typename _Distance
, typename _Compare
>
3098 __merge_sort_loop(_RandomAccessIterator1 __first
,
3099 _RandomAccessIterator1 __last
,
3100 _RandomAccessIterator2 __result
, _Distance __step_size
,
3103 const _Distance __two_step
= 2 * __step_size
;
3105 while (__last
- __first
>= __two_step
)
3107 __result
= std::merge(__first
, __first
+ __step_size
,
3108 __first
+ __step_size
, __first
+ __two_step
,
3111 __first
+= __two_step
;
3113 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3115 std::merge(__first
, __first
+ __step_size
,
3116 __first
+ __step_size
, __last
,
3121 enum { _S_chunk_size
= 7 };
3123 template<typename _RandomAccessIterator
, typename _Distance
>
3125 __chunk_insertion_sort(_RandomAccessIterator __first
,
3126 _RandomAccessIterator __last
,
3127 _Distance __chunk_size
)
3129 while (__last
- __first
>= __chunk_size
)
3131 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3132 __first
+= __chunk_size
;
3134 std::__insertion_sort(__first
, __last
);
3137 template<typename _RandomAccessIterator
, typename _Distance
, typename _Compare
>
3139 __chunk_insertion_sort(_RandomAccessIterator __first
,
3140 _RandomAccessIterator __last
,
3141 _Distance __chunk_size
, _Compare __comp
)
3143 while (__last
- __first
>= __chunk_size
)
3145 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3146 __first
+= __chunk_size
;
3148 std::__insertion_sort(__first
, __last
, __comp
);
3151 template<typename _RandomAccessIterator
, typename _Pointer
>
3153 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3154 _RandomAccessIterator __last
,
3157 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3160 const _Distance __len
= __last
- __first
;
3161 const _Pointer __buffer_last
= __buffer
+ __len
;
3163 _Distance __step_size
= _S_chunk_size
;
3164 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3166 while (__step_size
< __len
)
3168 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3170 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3175 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3177 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3178 _RandomAccessIterator __last
,
3179 _Pointer __buffer
, _Compare __comp
)
3181 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3184 const _Distance __len
= __last
- __first
;
3185 const _Pointer __buffer_last
= __buffer
+ __len
;
3187 _Distance __step_size
= _S_chunk_size
;
3188 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3190 while (__step_size
< __len
)
3192 std::__merge_sort_loop(__first
, __last
, __buffer
,
3193 __step_size
, __comp
);
3195 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3196 __step_size
, __comp
);
3203 * This is a helper function for the merge routines.
3206 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3207 typename _BidirectionalIterator3
>
3208 _BidirectionalIterator3
3209 __merge_backward(_BidirectionalIterator1 __first1
,
3210 _BidirectionalIterator1 __last1
,
3211 _BidirectionalIterator2 __first2
,
3212 _BidirectionalIterator2 __last2
,
3213 _BidirectionalIterator3 __result
)
3215 if (__first1
== __last1
)
3216 return std::copy_backward(__first2
, __last2
, __result
);
3217 if (__first2
== __last2
)
3218 return std::copy_backward(__first1
, __last1
, __result
);
3223 if (*__last2
< *__last1
)
3225 *--__result
= *__last1
;
3226 if (__first1
== __last1
)
3227 return std::copy_backward(__first2
, ++__last2
, __result
);
3232 *--__result
= *__last2
;
3233 if (__first2
== __last2
)
3234 return std::copy_backward(__first1
, ++__last1
, __result
);
3242 * This is a helper function for the merge routines.
3245 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3246 typename _BidirectionalIterator3
, typename _Compare
>
3247 _BidirectionalIterator3
3248 __merge_backward(_BidirectionalIterator1 __first1
,
3249 _BidirectionalIterator1 __last1
,
3250 _BidirectionalIterator2 __first2
,
3251 _BidirectionalIterator2 __last2
,
3252 _BidirectionalIterator3 __result
,
3255 if (__first1
== __last1
)
3256 return std::copy_backward(__first2
, __last2
, __result
);
3257 if (__first2
== __last2
)
3258 return std::copy_backward(__first1
, __last1
, __result
);
3263 if (__comp(*__last2
, *__last1
))
3265 *--__result
= *__last1
;
3266 if (__first1
== __last1
)
3267 return std::copy_backward(__first2
, ++__last2
, __result
);
3272 *--__result
= *__last2
;
3273 if (__first2
== __last2
)
3274 return std::copy_backward(__first1
, ++__last1
, __result
);
3282 * This is a helper function for the merge routines.
3285 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3287 _BidirectionalIterator1
3288 __rotate_adaptive(_BidirectionalIterator1 __first
,
3289 _BidirectionalIterator1 __middle
,
3290 _BidirectionalIterator1 __last
,
3291 _Distance __len1
, _Distance __len2
,
3292 _BidirectionalIterator2 __buffer
,
3293 _Distance __buffer_size
)
3295 _BidirectionalIterator2 __buffer_end
;
3296 if (__len1
> __len2
&& __len2
<= __buffer_size
)
3298 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3299 std::copy_backward(__first
, __middle
, __last
);
3300 return std::copy(__buffer
, __buffer_end
, __first
);
3302 else if (__len1
<= __buffer_size
)
3304 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3305 std::copy(__middle
, __last
, __first
);
3306 return std::copy_backward(__buffer
, __buffer_end
, __last
);
3310 std::rotate(__first
, __middle
, __last
);
3311 std::advance(__first
, std::distance(__middle
, __last
));
3318 * This is a helper function for the merge routines.
3321 template<typename _BidirectionalIterator
, typename _Distance
,
3324 __merge_adaptive(_BidirectionalIterator __first
,
3325 _BidirectionalIterator __middle
,
3326 _BidirectionalIterator __last
,
3327 _Distance __len1
, _Distance __len2
,
3328 _Pointer __buffer
, _Distance __buffer_size
)
3330 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
3332 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3333 std::merge(__buffer
, __buffer_end
, __middle
, __last
, __first
);
3335 else if (__len2
<= __buffer_size
)
3337 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3338 std::__merge_backward(__first
, __middle
, __buffer
,
3339 __buffer_end
, __last
);
3343 _BidirectionalIterator __first_cut
= __first
;
3344 _BidirectionalIterator __second_cut
= __middle
;
3345 _Distance __len11
= 0;
3346 _Distance __len22
= 0;
3347 if (__len1
> __len2
)
3349 __len11
= __len1
/ 2;
3350 std::advance(__first_cut
, __len11
);
3351 __second_cut
= std::lower_bound(__middle
, __last
,
3353 __len22
= std::distance(__middle
, __second_cut
);
3357 __len22
= __len2
/ 2;
3358 std::advance(__second_cut
, __len22
);
3359 __first_cut
= std::upper_bound(__first
, __middle
,
3361 __len11
= std::distance(__first
, __first_cut
);
3363 _BidirectionalIterator __new_middle
=
3364 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3365 __len1
- __len11
, __len22
, __buffer
,
3367 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3368 __len22
, __buffer
, __buffer_size
);
3369 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3371 __len2
- __len22
, __buffer
, __buffer_size
);
3377 * This is a helper function for the merge routines.
3380 template<typename _BidirectionalIterator
, typename _Distance
, typename _Pointer
,
3383 __merge_adaptive(_BidirectionalIterator __first
,
3384 _BidirectionalIterator __middle
,
3385 _BidirectionalIterator __last
,
3386 _Distance __len1
, _Distance __len2
,
3387 _Pointer __buffer
, _Distance __buffer_size
,
3390 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
3392 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3393 std::merge(__buffer
, __buffer_end
, __middle
, __last
, __first
, __comp
);
3395 else if (__len2
<= __buffer_size
)
3397 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3398 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
3403 _BidirectionalIterator __first_cut
= __first
;
3404 _BidirectionalIterator __second_cut
= __middle
;
3405 _Distance __len11
= 0;
3406 _Distance __len22
= 0;
3407 if (__len1
> __len2
)
3409 __len11
= __len1
/ 2;
3410 std::advance(__first_cut
, __len11
);
3411 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3413 __len22
= std::distance(__middle
, __second_cut
);
3417 __len22
= __len2
/ 2;
3418 std::advance(__second_cut
, __len22
);
3419 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3421 __len11
= std::distance(__first
, __first_cut
);
3423 _BidirectionalIterator __new_middle
=
3424 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3425 __len1
- __len11
, __len22
, __buffer
,
3427 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3428 __len22
, __buffer
, __buffer_size
, __comp
);
3429 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3431 __len2
- __len22
, __buffer
,
3432 __buffer_size
, __comp
);
3437 * @brief Merges two sorted ranges in place.
3438 * @param first An iterator.
3439 * @param middle Another iterator.
3440 * @param last Another iterator.
3443 * Merges two sorted and consecutive ranges, [first,middle) and
3444 * [middle,last), and puts the result in [first,last). The output will
3445 * be sorted. The sort is @e stable, that is, for equivalent
3446 * elements in the two ranges, elements from the first range will always
3447 * come before elements from the second.
3449 * If enough additional memory is available, this takes (last-first)-1
3450 * comparisons. Otherwise an NlogN algorithm is used, where N is
3451 * distance(first,last).
3453 template<typename _BidirectionalIterator
>
3455 inplace_merge(_BidirectionalIterator __first
,
3456 _BidirectionalIterator __middle
,
3457 _BidirectionalIterator __last
)
3459 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3461 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3464 // concept requirements
3465 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3466 _BidirectionalIterator
>)
3467 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3468 __glibcxx_requires_sorted(__first
, __middle
);
3469 __glibcxx_requires_sorted(__middle
, __last
);
3471 if (__first
== __middle
|| __middle
== __last
)
3474 _DistanceType __len1
= std::distance(__first
, __middle
);
3475 _DistanceType __len2
= std::distance(__middle
, __last
);
3477 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3479 if (__buf
.begin() == 0)
3480 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3482 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3483 __buf
.begin(), _DistanceType(__buf
.size()));
3487 * @brief Merges two sorted ranges in place.
3488 * @param first An iterator.
3489 * @param middle Another iterator.
3490 * @param last Another iterator.
3491 * @param comp A functor to use for comparisons.
3494 * Merges two sorted and consecutive ranges, [first,middle) and
3495 * [middle,last), and puts the result in [first,last). The output will
3496 * be sorted. The sort is @e stable, that is, for equivalent
3497 * elements in the two ranges, elements from the first range will always
3498 * come before elements from the second.
3500 * If enough additional memory is available, this takes (last-first)-1
3501 * comparisons. Otherwise an NlogN algorithm is used, where N is
3502 * distance(first,last).
3504 * The comparison function should have the same effects on ordering as
3505 * the function used for the initial sort.
3507 template<typename _BidirectionalIterator
, typename _Compare
>
3509 inplace_merge(_BidirectionalIterator __first
,
3510 _BidirectionalIterator __middle
,
3511 _BidirectionalIterator __last
,
3514 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3516 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3519 // concept requirements
3520 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3521 _BidirectionalIterator
>)
3522 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3523 _ValueType
, _ValueType
>)
3524 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3525 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3527 if (__first
== __middle
|| __middle
== __last
)
3530 const _DistanceType __len1
= std::distance(__first
, __middle
);
3531 const _DistanceType __len2
= std::distance(__middle
, __last
);
3533 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3535 if (__buf
.begin() == 0)
3536 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3539 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3540 __buf
.begin(), _DistanceType(__buf
.size()),
3544 template<typename _RandomAccessIterator
, typename _Pointer
,
3547 __stable_sort_adaptive(_RandomAccessIterator __first
,
3548 _RandomAccessIterator __last
,
3549 _Pointer __buffer
, _Distance __buffer_size
)
3551 const _Distance __len
= (__last
- __first
+ 1) / 2;
3552 const _RandomAccessIterator __middle
= __first
+ __len
;
3553 if (__len
> __buffer_size
)
3555 std::__stable_sort_adaptive(__first
, __middle
,
3556 __buffer
, __buffer_size
);
3557 std::__stable_sort_adaptive(__middle
, __last
,
3558 __buffer
, __buffer_size
);
3562 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3563 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3565 std::__merge_adaptive(__first
, __middle
, __last
,
3566 _Distance(__middle
- __first
),
3567 _Distance(__last
- __middle
),
3568 __buffer
, __buffer_size
);
3571 template<typename _RandomAccessIterator
, typename _Pointer
,
3572 typename _Distance
, typename _Compare
>
3574 __stable_sort_adaptive(_RandomAccessIterator __first
,
3575 _RandomAccessIterator __last
,
3576 _Pointer __buffer
, _Distance __buffer_size
,
3579 const _Distance __len
= (__last
- __first
+ 1) / 2;
3580 const _RandomAccessIterator __middle
= __first
+ __len
;
3581 if (__len
> __buffer_size
)
3583 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3584 __buffer_size
, __comp
);
3585 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3586 __buffer_size
, __comp
);
3590 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3591 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3593 std::__merge_adaptive(__first
, __middle
, __last
,
3594 _Distance(__middle
- __first
),
3595 _Distance(__last
- __middle
),
3596 __buffer
, __buffer_size
,
3601 * @brief Sort the elements of a sequence, preserving the relative order
3602 * of equivalent elements.
3603 * @param first An iterator.
3604 * @param last Another iterator.
3607 * Sorts the elements in the range @p [first,last) in ascending order,
3608 * such that @p *(i+1)<*i is false for each iterator @p i in the range
3609 * @p [first,last-1).
3611 * The relative ordering of equivalent elements is preserved, so any two
3612 * elements @p x and @p y in the range @p [first,last) such that
3613 * @p x<y is false and @p y<x is false will have the same relative
3614 * ordering after calling @p stable_sort().
3616 template<typename _RandomAccessIterator
>
3618 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
3620 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3622 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3625 // concept requirements
3626 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3627 _RandomAccessIterator
>)
3628 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3629 __glibcxx_requires_valid_range(__first
, __last
);
3631 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
>
3632 buf(__first
, __last
);
3633 if (buf
.begin() == 0)
3634 std::__inplace_stable_sort(__first
, __last
);
3636 std::__stable_sort_adaptive(__first
, __last
, buf
.begin(),
3637 _DistanceType(buf
.size()));
3641 * @brief Sort the elements of a sequence using a predicate for comparison,
3642 * preserving the relative order of equivalent elements.
3643 * @param first An iterator.
3644 * @param last Another iterator.
3645 * @param comp A comparison functor.
3648 * Sorts the elements in the range @p [first,last) in ascending order,
3649 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
3650 * range @p [first,last-1).
3652 * The relative ordering of equivalent elements is preserved, so any two
3653 * elements @p x and @p y in the range @p [first,last) such that
3654 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
3655 * relative ordering after calling @p stable_sort().
3657 template<typename _RandomAccessIterator
, typename _Compare
>
3659 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3662 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3664 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3667 // concept requirements
3668 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3669 _RandomAccessIterator
>)
3670 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3673 __glibcxx_requires_valid_range(__first
, __last
);
3675 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> buf(__first
, __last
);
3676 if (buf
.begin() == 0)
3677 std::__inplace_stable_sort(__first
, __last
, __comp
);
3679 std::__stable_sort_adaptive(__first
, __last
, buf
.begin(),
3680 _DistanceType(buf
.size()), __comp
);
3684 * @brief Sort a sequence just enough to find a particular position.
3685 * @param first An iterator.
3686 * @param nth Another iterator.
3687 * @param last Another iterator.
3690 * Rearranges the elements in the range @p [first,last) so that @p *nth
3691 * is the same element that would have been in that position had the
3692 * whole sequence been sorted.
3693 * whole sequence been sorted. The elements either side of @p *nth are
3694 * not completely sorted, but for any iterator @i in the range
3695 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3696 * holds that @p *j<*i is false.
3698 template<typename _RandomAccessIterator
>
3700 nth_element(_RandomAccessIterator __first
,
3701 _RandomAccessIterator __nth
,
3702 _RandomAccessIterator __last
)
3704 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3707 // concept requirements
3708 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3709 _RandomAccessIterator
>)
3710 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3711 __glibcxx_requires_valid_range(__first
, __nth
);
3712 __glibcxx_requires_valid_range(__nth
, __last
);
3714 while (__last
- __first
> 3)
3716 _RandomAccessIterator __cut
=
3717 std::__unguarded_partition(__first
, __last
,
3718 _ValueType(std::__median(*__first
,
3730 std::__insertion_sort(__first
, __last
);
3734 * @brief Sort a sequence just enough to find a particular position
3735 * using a predicate for comparison.
3736 * @param first An iterator.
3737 * @param nth Another iterator.
3738 * @param last Another iterator.
3739 * @param comp A comparison functor.
3742 * Rearranges the elements in the range @p [first,last) so that @p *nth
3743 * is the same element that would have been in that position had the
3744 * whole sequence been sorted. The elements either side of @p *nth are
3745 * not completely sorted, but for any iterator @i in the range
3746 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3747 * holds that @p comp(*j,*i) is false.
3749 template<typename _RandomAccessIterator
, typename _Compare
>
3751 nth_element(_RandomAccessIterator __first
,
3752 _RandomAccessIterator __nth
,
3753 _RandomAccessIterator __last
,
3756 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3759 // concept requirements
3760 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3761 _RandomAccessIterator
>)
3762 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3763 _ValueType
, _ValueType
>)
3764 __glibcxx_requires_valid_range(__first
, __nth
);
3765 __glibcxx_requires_valid_range(__nth
, __last
);
3767 while (__last
- __first
> 3)
3769 _RandomAccessIterator __cut
=
3770 std::__unguarded_partition(__first
, __last
,
3771 _ValueType(std::__median(*__first
,
3783 std::__insertion_sort(__first
, __last
, __comp
);
3787 * @brief Finds the largest subrange in which @a val could be inserted
3788 * at any place in it without changing the ordering.
3789 * @param first An iterator.
3790 * @param last Another iterator.
3791 * @param val The search term.
3792 * @return An pair of iterators defining the subrange.
3793 * @ingroup binarysearch
3795 * This is equivalent to
3797 * std::make_pair(lower_bound(first, last, val),
3798 * upper_bound(first, last, val))
3800 * but does not actually call those functions.
3802 template<typename _ForwardIterator
, typename _Tp
>
3803 pair
<_ForwardIterator
, _ForwardIterator
>
3804 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
3807 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3809 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3812 // concept requirements
3813 // See comments on lower_bound.
3814 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3815 __glibcxx_function_requires(_SameTypeConcept
<_Tp
, _ValueType
>)
3816 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3817 __glibcxx_requires_partitioned(__first
, __last
, __val
);
3819 _DistanceType __len
= std::distance(__first
, __last
);
3820 _DistanceType __half
;
3821 _ForwardIterator __middle
, __left
, __right
;
3825 __half
= __len
>> 1;
3827 std::advance(__middle
, __half
);
3828 if (*__middle
< __val
)
3832 __len
= __len
- __half
- 1;
3834 else if (__val
< *__middle
)
3838 __left
= std::lower_bound(__first
, __middle
, __val
);
3839 std::advance(__first
, __len
);
3840 __right
= std::upper_bound(++__middle
, __first
, __val
);
3841 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
3844 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
3848 * @brief Finds the largest subrange in which @a val could be inserted
3849 * at any place in it without changing the ordering.
3850 * @param first An iterator.
3851 * @param last Another iterator.
3852 * @param val The search term.
3853 * @param comp A functor to use for comparisons.
3854 * @return An pair of iterators defining the subrange.
3855 * @ingroup binarysearch
3857 * This is equivalent to
3859 * std::make_pair(lower_bound(first, last, val, comp),
3860 * upper_bound(first, last, val, comp))
3862 * but does not actually call those functions.
3864 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3865 pair
<_ForwardIterator
, _ForwardIterator
>
3866 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
3870 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3872 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3875 // concept requirements
3876 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3877 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3879 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3881 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
3883 _DistanceType __len
= std::distance(__first
, __last
);
3884 _DistanceType __half
;
3885 _ForwardIterator __middle
, __left
, __right
;
3889 __half
= __len
>> 1;
3891 std::advance(__middle
, __half
);
3892 if (__comp(*__middle
, __val
))
3896 __len
= __len
- __half
- 1;
3898 else if (__comp(__val
, *__middle
))
3902 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
3903 std::advance(__first
, __len
);
3904 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
3905 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
3908 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
3912 * @brief Determines whether an element exists in a range.
3913 * @param first An iterator.
3914 * @param last Another iterator.
3915 * @param val The search term.
3916 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
3917 * @ingroup binarysearch
3919 * Note that this does not actually return an iterator to @a val. For
3920 * that, use std::find or a container's specialized find member functions.
3922 template<typename _ForwardIterator
, typename _Tp
>
3924 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
3927 // concept requirements
3928 // See comments on lower_bound.
3929 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3930 __glibcxx_function_requires(_SameTypeConcept
<_Tp
,
3931 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3932 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3933 __glibcxx_requires_partitioned(__first
, __last
, __val
);
3935 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
3936 return __i
!= __last
&& !(__val
< *__i
);
3940 * @brief Determines whether an element exists in a range.
3941 * @param first An iterator.
3942 * @param last Another iterator.
3943 * @param val The search term.
3944 * @param comp A functor to use for comparisons.
3945 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
3946 * @ingroup binarysearch
3948 * Note that this does not actually return an iterator to @a val. For
3949 * that, use std::find or a container's specialized find member functions.
3951 * The comparison function should have the same effects on ordering as
3952 * the function used for the initial sort.
3954 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3956 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
3957 const _Tp
& __val
, _Compare __comp
)
3959 // concept requirements
3960 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3961 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3962 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
3963 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _Tp
,
3964 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3965 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
3967 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
3968 return __i
!= __last
&& !__comp(__val
, *__i
);
3971 // Set algorithms: includes, set_union, set_intersection, set_difference,
3972 // set_symmetric_difference. All of these algorithms have the precondition
3973 // that their input ranges are sorted and the postcondition that their output
3974 // ranges are sorted.
3977 * @brief Determines whether all elements of a sequence exists in a range.
3978 * @param first1 Start of search range.
3979 * @param last1 End of search range.
3980 * @param first2 Start of sequence
3981 * @param last2 End of sequence.
3982 * @return True if each element in [first2,last2) is contained in order
3983 * within [first1,last1). False otherwise.
3984 * @ingroup setoperations
3986 * This operation expects both [first1,last1) and [first2,last2) to be
3987 * sorted. Searches for the presence of each element in [first2,last2)
3988 * within [first1,last1). The iterators over each range only move forward,
3989 * so this is a linear algorithm. If an element in [first2,last2) is not
3990 * found before the search iterator reaches @a last2, false is returned.
3992 template<typename _InputIterator1
, typename _InputIterator2
>
3994 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3995 _InputIterator2 __first2
, _InputIterator2 __last2
)
3997 // concept requirements
3998 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3999 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4000 __glibcxx_function_requires(_SameTypeConcept
<
4001 typename iterator_traits
<_InputIterator1
>::value_type
,
4002 typename iterator_traits
<_InputIterator2
>::value_type
>)
4003 __glibcxx_function_requires(_LessThanComparableConcept
<
4004 typename iterator_traits
<_InputIterator1
>::value_type
>)
4005 __glibcxx_requires_sorted(__first1
, __last1
);
4006 __glibcxx_requires_sorted(__first2
, __last2
);
4008 while (__first1
!= __last1
&& __first2
!= __last2
)
4009 if (*__first2
< *__first1
)
4011 else if(*__first1
< *__first2
)
4014 ++__first1
, ++__first2
;
4016 return __first2
== __last2
;
4020 * @brief Determines whether all elements of a sequence exists in a range
4022 * @param first1 Start of search range.
4023 * @param last1 End of search range.
4024 * @param first2 Start of sequence
4025 * @param last2 End of sequence.
4026 * @param comp Comparison function to use.
4027 * @return True if each element in [first2,last2) is contained in order
4028 * within [first1,last1) according to comp. False otherwise.
4029 * @ingroup setoperations
4031 * This operation expects both [first1,last1) and [first2,last2) to be
4032 * sorted. Searches for the presence of each element in [first2,last2)
4033 * within [first1,last1), using comp to decide. The iterators over each
4034 * range only move forward, so this is a linear algorithm. If an element
4035 * in [first2,last2) is not found before the search iterator reaches @a
4036 * last2, false is returned.
4038 template<typename _InputIterator1
, typename _InputIterator2
,
4041 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
4042 _InputIterator2 __first2
, _InputIterator2 __last2
, _Compare __comp
)
4044 // concept requirements
4045 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4046 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4047 __glibcxx_function_requires(_SameTypeConcept
<
4048 typename iterator_traits
<_InputIterator1
>::value_type
,
4049 typename iterator_traits
<_InputIterator2
>::value_type
>)
4050 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4051 typename iterator_traits
<_InputIterator1
>::value_type
,
4052 typename iterator_traits
<_InputIterator2
>::value_type
>)
4053 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4054 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4056 while (__first1
!= __last1
&& __first2
!= __last2
)
4057 if (__comp(*__first2
, *__first1
))
4059 else if(__comp(*__first1
, *__first2
))
4062 ++__first1
, ++__first2
;
4064 return __first2
== __last2
;
4068 * @brief Return the union of two sorted ranges.
4069 * @param first1 Start of first range.
4070 * @param last1 End of first range.
4071 * @param first2 Start of second range.
4072 * @param last2 End of second range.
4073 * @return End of the output range.
4074 * @ingroup setoperations
4076 * This operation iterates over both ranges, copying elements present in
4077 * each range in order to the output range. Iterators increment for each
4078 * range. When the current element of one range is less than the other,
4079 * that element is copied and the iterator advanced. If an element is
4080 * contained in both ranges, the element from the first range is copied and
4081 * both ranges advance. The output range may not overlap either input
4084 template<typename _InputIterator1
, typename _InputIterator2
,
4085 typename _OutputIterator
>
4087 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4088 _InputIterator2 __first2
, _InputIterator2 __last2
,
4089 _OutputIterator __result
)
4091 // concept requirements
4092 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4093 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4094 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4095 typename iterator_traits
<_InputIterator1
>::value_type
>)
4096 __glibcxx_function_requires(_SameTypeConcept
<
4097 typename iterator_traits
<_InputIterator1
>::value_type
,
4098 typename iterator_traits
<_InputIterator2
>::value_type
>)
4099 __glibcxx_function_requires(_LessThanComparableConcept
<
4100 typename iterator_traits
<_InputIterator1
>::value_type
>)
4101 __glibcxx_requires_sorted(__first1
, __last1
);
4102 __glibcxx_requires_sorted(__first2
, __last2
);
4104 while (__first1
!= __last1
&& __first2
!= __last2
)
4106 if (*__first1
< *__first2
)
4108 *__result
= *__first1
;
4111 else if (*__first2
< *__first1
)
4113 *__result
= *__first2
;
4118 *__result
= *__first1
;
4124 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4129 * @brief Return the union of two sorted ranges using a comparison functor.
4130 * @param first1 Start of first range.
4131 * @param last1 End of first range.
4132 * @param first2 Start of second range.
4133 * @param last2 End of second range.
4134 * @param comp The comparison functor.
4135 * @return End of the output range.
4136 * @ingroup setoperations
4138 * This operation iterates over both ranges, copying elements present in
4139 * each range in order to the output range. Iterators increment for each
4140 * range. When the current element of one range is less than the other
4141 * according to @a comp, that element is copied and the iterator advanced.
4142 * If an equivalent element according to @a comp is contained in both
4143 * ranges, the element from the first range is copied and both ranges
4144 * advance. The output range may not overlap either input range.
4146 template<typename _InputIterator1
, typename _InputIterator2
,
4147 typename _OutputIterator
, typename _Compare
>
4149 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4150 _InputIterator2 __first2
, _InputIterator2 __last2
,
4151 _OutputIterator __result
, _Compare __comp
)
4153 // concept requirements
4154 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4155 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4156 __glibcxx_function_requires(_SameTypeConcept
<
4157 typename iterator_traits
<_InputIterator1
>::value_type
,
4158 typename iterator_traits
<_InputIterator2
>::value_type
>)
4159 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4160 typename iterator_traits
<_InputIterator1
>::value_type
>)
4161 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4162 typename iterator_traits
<_InputIterator1
>::value_type
,
4163 typename iterator_traits
<_InputIterator2
>::value_type
>)
4164 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4165 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4167 while (__first1
!= __last1
&& __first2
!= __last2
)
4169 if (__comp(*__first1
, *__first2
))
4171 *__result
= *__first1
;
4174 else if (__comp(*__first2
, *__first1
))
4176 *__result
= *__first2
;
4181 *__result
= *__first1
;
4187 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4192 * @brief Return the intersection of two sorted ranges.
4193 * @param first1 Start of first range.
4194 * @param last1 End of first range.
4195 * @param first2 Start of second range.
4196 * @param last2 End of second range.
4197 * @return End of the output range.
4198 * @ingroup setoperations
4200 * This operation iterates over both ranges, copying elements present in
4201 * both ranges in order to the output range. Iterators increment for each
4202 * range. When the current element of one range is less than the other,
4203 * that iterator advances. If an element is contained in both ranges, the
4204 * element from the first range is copied and both ranges advance. The
4205 * output range may not overlap either input range.
4207 template<typename _InputIterator1
, typename _InputIterator2
,
4208 typename _OutputIterator
>
4210 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
4211 _InputIterator2 __first2
, _InputIterator2 __last2
,
4212 _OutputIterator __result
)
4214 // concept requirements
4215 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4216 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4217 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4218 typename iterator_traits
<_InputIterator1
>::value_type
>)
4219 __glibcxx_function_requires(_SameTypeConcept
<
4220 typename iterator_traits
<_InputIterator1
>::value_type
,
4221 typename iterator_traits
<_InputIterator2
>::value_type
>)
4222 __glibcxx_function_requires(_LessThanComparableConcept
<
4223 typename iterator_traits
<_InputIterator1
>::value_type
>)
4224 __glibcxx_requires_sorted(__first1
, __last1
);
4225 __glibcxx_requires_sorted(__first2
, __last2
);
4227 while (__first1
!= __last1
&& __first2
!= __last2
)
4228 if (*__first1
< *__first2
)
4230 else if (*__first2
< *__first1
)
4234 *__result
= *__first1
;
4243 * @brief Return the intersection of two sorted ranges using comparison
4245 * @param first1 Start of first range.
4246 * @param last1 End of first range.
4247 * @param first2 Start of second range.
4248 * @param last2 End of second range.
4249 * @param comp The comparison functor.
4250 * @return End of the output range.
4251 * @ingroup setoperations
4253 * This operation iterates over both ranges, copying elements present in
4254 * both ranges in order to the output range. Iterators increment for each
4255 * range. When the current element of one range is less than the other
4256 * according to @a comp, that iterator advances. If an element is
4257 * contained in both ranges according to @a comp, the element from the
4258 * first range is copied and both ranges advance. The output range may not
4259 * overlap either input range.
4261 template<typename _InputIterator1
, typename _InputIterator2
,
4262 typename _OutputIterator
, typename _Compare
>
4264 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
4265 _InputIterator2 __first2
, _InputIterator2 __last2
,
4266 _OutputIterator __result
, _Compare __comp
)
4268 // concept requirements
4269 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4270 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4271 __glibcxx_function_requires(_SameTypeConcept
<
4272 typename iterator_traits
<_InputIterator1
>::value_type
,
4273 typename iterator_traits
<_InputIterator2
>::value_type
>)
4274 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4275 typename iterator_traits
<_InputIterator1
>::value_type
>)
4276 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4277 typename iterator_traits
<_InputIterator1
>::value_type
,
4278 typename iterator_traits
<_InputIterator2
>::value_type
>)
4279 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4280 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4282 while (__first1
!= __last1
&& __first2
!= __last2
)
4283 if (__comp(*__first1
, *__first2
))
4285 else if (__comp(*__first2
, *__first1
))
4289 *__result
= *__first1
;
4298 * @brief Return the difference of two sorted ranges.
4299 * @param first1 Start of first range.
4300 * @param last1 End of first range.
4301 * @param first2 Start of second range.
4302 * @param last2 End of second range.
4303 * @return End of the output range.
4304 * @ingroup setoperations
4306 * This operation iterates over both ranges, copying elements present in
4307 * the first range but not the second in order to the output range.
4308 * Iterators increment for each range. When the current element of the
4309 * first range is less than the second, that element is copied and the
4310 * iterator advances. If the current element of the second range is less,
4311 * the iterator advances, but no element is copied. If an element is
4312 * contained in both ranges, no elements are copied and both ranges
4313 * advance. The output range may not overlap either input range.
4315 template<typename _InputIterator1
, typename _InputIterator2
,
4316 typename _OutputIterator
>
4318 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4319 _InputIterator2 __first2
, _InputIterator2 __last2
,
4320 _OutputIterator __result
)
4322 // concept requirements
4323 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4324 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4325 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4326 typename iterator_traits
<_InputIterator1
>::value_type
>)
4327 __glibcxx_function_requires(_SameTypeConcept
<
4328 typename iterator_traits
<_InputIterator1
>::value_type
,
4329 typename iterator_traits
<_InputIterator2
>::value_type
>)
4330 __glibcxx_function_requires(_LessThanComparableConcept
<
4331 typename iterator_traits
<_InputIterator1
>::value_type
>)
4332 __glibcxx_requires_sorted(__first1
, __last1
);
4333 __glibcxx_requires_sorted(__first2
, __last2
);
4335 while (__first1
!= __last1
&& __first2
!= __last2
)
4336 if (*__first1
< *__first2
)
4338 *__result
= *__first1
;
4342 else if (*__first2
< *__first1
)
4349 return std::copy(__first1
, __last1
, __result
);
4353 * @brief Return the difference of two sorted ranges using comparison
4355 * @param first1 Start of first range.
4356 * @param last1 End of first range.
4357 * @param first2 Start of second range.
4358 * @param last2 End of second range.
4359 * @param comp The comparison functor.
4360 * @return End of the output range.
4361 * @ingroup setoperations
4363 * This operation iterates over both ranges, copying elements present in
4364 * the first range but not the second in order to the output range.
4365 * Iterators increment for each range. When the current element of the
4366 * first range is less than the second according to @a comp, that element
4367 * is copied and the iterator advances. If the current element of the
4368 * second range is less, no element is copied and the iterator advances.
4369 * If an element is contained in both ranges according to @a comp, no
4370 * elements are copied and both ranges advance. The output range may not
4371 * overlap either input range.
4373 template<typename _InputIterator1
, typename _InputIterator2
,
4374 typename _OutputIterator
, typename _Compare
>
4376 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4377 _InputIterator2 __first2
, _InputIterator2 __last2
,
4378 _OutputIterator __result
, _Compare __comp
)
4380 // concept requirements
4381 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4382 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4383 __glibcxx_function_requires(_SameTypeConcept
<
4384 typename iterator_traits
<_InputIterator1
>::value_type
,
4385 typename iterator_traits
<_InputIterator2
>::value_type
>)
4386 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4387 typename iterator_traits
<_InputIterator1
>::value_type
>)
4388 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4389 typename iterator_traits
<_InputIterator1
>::value_type
,
4390 typename iterator_traits
<_InputIterator2
>::value_type
>)
4391 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4392 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4394 while (__first1
!= __last1
&& __first2
!= __last2
)
4395 if (__comp(*__first1
, *__first2
))
4397 *__result
= *__first1
;
4401 else if (__comp(*__first2
, *__first1
))
4408 return std::copy(__first1
, __last1
, __result
);
4412 * @brief Return the symmetric difference of two sorted ranges.
4413 * @param first1 Start of first range.
4414 * @param last1 End of first range.
4415 * @param first2 Start of second range.
4416 * @param last2 End of second range.
4417 * @return End of the output range.
4418 * @ingroup setoperations
4420 * This operation iterates over both ranges, copying elements present in
4421 * one range but not the other in order to the output range. Iterators
4422 * increment for each range. When the current element of one range is less
4423 * than the other, that element is copied and the iterator advances. If an
4424 * element is contained in both ranges, no elements are copied and both
4425 * ranges advance. The output range may not overlap either input range.
4427 template<typename _InputIterator1
, typename _InputIterator2
,
4428 typename _OutputIterator
>
4430 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4431 _InputIterator2 __first2
, _InputIterator2 __last2
,
4432 _OutputIterator __result
)
4434 // concept requirements
4435 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4436 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4437 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4438 typename iterator_traits
<_InputIterator1
>::value_type
>)
4439 __glibcxx_function_requires(_SameTypeConcept
<
4440 typename iterator_traits
<_InputIterator1
>::value_type
,
4441 typename iterator_traits
<_InputIterator2
>::value_type
>)
4442 __glibcxx_function_requires(_LessThanComparableConcept
<
4443 typename iterator_traits
<_InputIterator1
>::value_type
>)
4444 __glibcxx_requires_sorted(__first1
, __last1
);
4445 __glibcxx_requires_sorted(__first2
, __last2
);
4447 while (__first1
!= __last1
&& __first2
!= __last2
)
4448 if (*__first1
< *__first2
)
4450 *__result
= *__first1
;
4454 else if (*__first2
< *__first1
)
4456 *__result
= *__first2
;
4465 return std::copy(__first2
, __last2
, std::copy(__first1
,
4466 __last1
, __result
));
4470 * @brief Return the symmetric difference of two sorted ranges using
4471 * comparison functor.
4472 * @param first1 Start of first range.
4473 * @param last1 End of first range.
4474 * @param first2 Start of second range.
4475 * @param last2 End of second range.
4476 * @param comp The comparison functor.
4477 * @return End of the output range.
4478 * @ingroup setoperations
4480 * This operation iterates over both ranges, copying elements present in
4481 * one range but not the other in order to the output range. Iterators
4482 * increment for each range. When the current element of one range is less
4483 * than the other according to @a comp, that element is copied and the
4484 * iterator advances. If an element is contained in both ranges according
4485 * to @a comp, no elements are copied and both ranges advance. The output
4486 * range may not overlap either input range.
4488 template<typename _InputIterator1
, typename _InputIterator2
,
4489 typename _OutputIterator
, typename _Compare
>
4491 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4492 _InputIterator2 __first2
, _InputIterator2 __last2
,
4493 _OutputIterator __result
,
4496 // concept requirements
4497 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4498 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4499 __glibcxx_function_requires(_SameTypeConcept
<
4500 typename iterator_traits
<_InputIterator1
>::value_type
,
4501 typename iterator_traits
<_InputIterator2
>::value_type
>)
4502 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4503 typename iterator_traits
<_InputIterator1
>::value_type
>)
4504 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4505 typename iterator_traits
<_InputIterator1
>::value_type
,
4506 typename iterator_traits
<_InputIterator2
>::value_type
>)
4507 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4508 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4510 while (__first1
!= __last1
&& __first2
!= __last2
)
4511 if (__comp(*__first1
, *__first2
))
4513 *__result
= *__first1
;
4517 else if (__comp(*__first2
, *__first1
))
4519 *__result
= *__first2
;
4528 return std::copy(__first2
, __last2
, std::copy(__first1
,
4529 __last1
, __result
));
4532 // min_element and max_element, with and without an explicitly supplied
4533 // comparison function.
4536 * @brief Return the maximum element in a range.
4537 * @param first Start of range.
4538 * @param last End of range.
4539 * @return Iterator referencing the first instance of the largest value.
4541 template<typename _ForwardIterator
>
4543 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
4545 // concept requirements
4546 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4547 __glibcxx_function_requires(_LessThanComparableConcept
<
4548 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4549 __glibcxx_requires_valid_range(__first
, __last
);
4551 if (__first
== __last
)
4553 _ForwardIterator __result
= __first
;
4554 while (++__first
!= __last
)
4555 if (*__result
< *__first
)
4561 * @brief Return the maximum element in a range using comparison functor.
4562 * @param first Start of range.
4563 * @param last End of range.
4564 * @param comp Comparison functor.
4565 * @return Iterator referencing the first instance of the largest value
4566 * according to comp.
4568 template<typename _ForwardIterator
, typename _Compare
>
4570 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
4573 // concept requirements
4574 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4575 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4576 typename iterator_traits
<_ForwardIterator
>::value_type
,
4577 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4578 __glibcxx_requires_valid_range(__first
, __last
);
4580 if (__first
== __last
) return __first
;
4581 _ForwardIterator __result
= __first
;
4582 while (++__first
!= __last
)
4583 if (__comp(*__result
, *__first
)) __result
= __first
;
4588 * @brief Return the minimum element in a range.
4589 * @param first Start of range.
4590 * @param last End of range.
4591 * @return Iterator referencing the first instance of the smallest value.
4593 template<typename _ForwardIterator
>
4595 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
4597 // concept requirements
4598 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4599 __glibcxx_function_requires(_LessThanComparableConcept
<
4600 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4601 __glibcxx_requires_valid_range(__first
, __last
);
4603 if (__first
== __last
)
4605 _ForwardIterator __result
= __first
;
4606 while (++__first
!= __last
)
4607 if (*__first
< *__result
)
4613 * @brief Return the minimum element in a range using comparison functor.
4614 * @param first Start of range.
4615 * @param last End of range.
4616 * @param comp Comparison functor.
4617 * @return Iterator referencing the first instance of the smallest value
4618 * according to comp.
4620 template<typename _ForwardIterator
, typename _Compare
>
4622 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
4625 // concept requirements
4626 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4627 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4628 typename iterator_traits
<_ForwardIterator
>::value_type
,
4629 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4630 __glibcxx_requires_valid_range(__first
, __last
);
4632 if (__first
== __last
)
4634 _ForwardIterator __result
= __first
;
4635 while (++__first
!= __last
)
4636 if (__comp(*__first
, *__result
))
4641 // next_permutation and prev_permutation, with and without an explicitly
4642 // supplied comparison function.
4645 * @brief Permute range into the next "dictionary" ordering.
4646 * @param first Start of range.
4647 * @param last End of range.
4648 * @return False if wrapped to first permutation, true otherwise.
4650 * Treats all permutations of the range as a set of "dictionary" sorted
4651 * sequences. Permutes the current sequence into the next one of this set.
4652 * Returns true if there are more sequences to generate. If the sequence
4653 * is the largest of the set, the smallest is generated and false returned.
4655 template<typename _BidirectionalIterator
>
4657 next_permutation(_BidirectionalIterator __first
,
4658 _BidirectionalIterator __last
)
4660 // concept requirements
4661 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4662 _BidirectionalIterator
>)
4663 __glibcxx_function_requires(_LessThanComparableConcept
<
4664 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4665 __glibcxx_requires_valid_range(__first
, __last
);
4667 if (__first
== __last
)
4669 _BidirectionalIterator __i
= __first
;
4678 _BidirectionalIterator __ii
= __i
;
4682 _BidirectionalIterator __j
= __last
;
4683 while (!(*__i
< *--__j
))
4685 std::iter_swap(__i
, __j
);
4686 std::reverse(__ii
, __last
);
4691 std::reverse(__first
, __last
);
4698 * @brief Permute range into the next "dictionary" ordering using
4699 * comparison functor.
4700 * @param first Start of range.
4701 * @param last End of range.
4703 * @return False if wrapped to first permutation, true otherwise.
4705 * Treats all permutations of the range [first,last) as a set of
4706 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
4707 * sequence into the next one of this set. Returns true if there are more
4708 * sequences to generate. If the sequence is the largest of the set, the
4709 * smallest is generated and false returned.
4711 template<typename _BidirectionalIterator
, typename _Compare
>
4713 next_permutation(_BidirectionalIterator __first
,
4714 _BidirectionalIterator __last
, _Compare __comp
)
4716 // concept requirements
4717 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4718 _BidirectionalIterator
>)
4719 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4720 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
4721 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4722 __glibcxx_requires_valid_range(__first
, __last
);
4724 if (__first
== __last
)
4726 _BidirectionalIterator __i
= __first
;
4735 _BidirectionalIterator __ii
= __i
;
4737 if (__comp(*__i
, *__ii
))
4739 _BidirectionalIterator __j
= __last
;
4740 while (!__comp(*__i
, *--__j
))
4742 std::iter_swap(__i
, __j
);
4743 std::reverse(__ii
, __last
);
4748 std::reverse(__first
, __last
);
4755 * @brief Permute range into the previous "dictionary" ordering.
4756 * @param first Start of range.
4757 * @param last End of range.
4758 * @return False if wrapped to last permutation, true otherwise.
4760 * Treats all permutations of the range as a set of "dictionary" sorted
4761 * sequences. Permutes the current sequence into the previous one of this
4762 * set. Returns true if there are more sequences to generate. If the
4763 * sequence is the smallest of the set, the largest is generated and false
4766 template<typename _BidirectionalIterator
>
4768 prev_permutation(_BidirectionalIterator __first
,
4769 _BidirectionalIterator __last
)
4771 // concept requirements
4772 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4773 _BidirectionalIterator
>)
4774 __glibcxx_function_requires(_LessThanComparableConcept
<
4775 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4776 __glibcxx_requires_valid_range(__first
, __last
);
4778 if (__first
== __last
)
4780 _BidirectionalIterator __i
= __first
;
4789 _BidirectionalIterator __ii
= __i
;
4793 _BidirectionalIterator __j
= __last
;
4794 while (!(*--__j
< *__i
))
4796 std::iter_swap(__i
, __j
);
4797 std::reverse(__ii
, __last
);
4802 std::reverse(__first
, __last
);
4809 * @brief Permute range into the previous "dictionary" ordering using
4810 * comparison functor.
4811 * @param first Start of range.
4812 * @param last End of range.
4814 * @return False if wrapped to last permutation, true otherwise.
4816 * Treats all permutations of the range [first,last) as a set of
4817 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
4818 * sequence into the previous one of this set. Returns true if there are
4819 * more sequences to generate. If the sequence is the smallest of the set,
4820 * the largest is generated and false returned.
4822 template<typename _BidirectionalIterator
, typename _Compare
>
4824 prev_permutation(_BidirectionalIterator __first
,
4825 _BidirectionalIterator __last
, _Compare __comp
)
4827 // concept requirements
4828 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4829 _BidirectionalIterator
>)
4830 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4831 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
4832 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4833 __glibcxx_requires_valid_range(__first
, __last
);
4835 if (__first
== __last
)
4837 _BidirectionalIterator __i
= __first
;
4846 _BidirectionalIterator __ii
= __i
;
4848 if (__comp(*__ii
, *__i
))
4850 _BidirectionalIterator __j
= __last
;
4851 while (!__comp(*--__j
, *__i
))
4853 std::iter_swap(__i
, __j
);
4854 std::reverse(__ii
, __last
);
4859 std::reverse(__first
, __last
);
4865 // find_first_of, with and without an explicitly supplied comparison function.
4868 * @brief Find element from a set in a sequence.
4869 * @param first1 Start of range to search.
4870 * @param last1 End of range to search.
4871 * @param first2 Start of match candidates.
4872 * @param last2 End of match candidates.
4873 * @return The first iterator @c i in the range
4874 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4875 * interator in [first2,last2), or @p last1 if no such iterator exists.
4877 * Searches the range @p [first1,last1) for an element that is equal to
4878 * some element in the range [first2,last2). If found, returns an iterator
4879 * in the range [first1,last1), otherwise returns @p last1.
4881 template<typename _InputIterator
, typename _ForwardIterator
>
4883 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4884 _ForwardIterator __first2
, _ForwardIterator __last2
)
4886 // concept requirements
4887 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4888 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4889 __glibcxx_function_requires(_EqualOpConcept
<
4890 typename iterator_traits
<_InputIterator
>::value_type
,
4891 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4892 __glibcxx_requires_valid_range(__first1
, __last1
);
4893 __glibcxx_requires_valid_range(__first2
, __last2
);
4895 for ( ; __first1
!= __last1
; ++__first1
)
4896 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4897 if (*__first1
== *__iter
)
4903 * @brief Find element from a set in a sequence using a predicate.
4904 * @param first1 Start of range to search.
4905 * @param last1 End of range to search.
4906 * @param first2 Start of match candidates.
4907 * @param last2 End of match candidates.
4908 * @param comp Predicate to use.
4909 * @return The first iterator @c i in the range
4910 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4911 * interator in [first2,last2), or @p last1 if no such iterator exists.
4913 * Searches the range @p [first1,last1) for an element that is equal to
4914 * some element in the range [first2,last2). If found, returns an iterator in
4915 * the range [first1,last1), otherwise returns @p last1.
4917 template<typename _InputIterator
, typename _ForwardIterator
,
4918 typename _BinaryPredicate
>
4920 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4921 _ForwardIterator __first2
, _ForwardIterator __last2
,
4922 _BinaryPredicate __comp
)
4924 // concept requirements
4925 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4926 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4927 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4928 typename iterator_traits
<_InputIterator
>::value_type
,
4929 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4930 __glibcxx_requires_valid_range(__first1
, __last1
);
4931 __glibcxx_requires_valid_range(__first2
, __last2
);
4933 for ( ; __first1
!= __last1
; ++__first1
)
4934 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4935 if (__comp(*__first1
, *__iter
))
4941 // find_end, with and without an explicitly supplied comparison function.
4942 // Search [first2, last2) as a subsequence in [first1, last1), and return
4943 // the *last* possible match. Note that find_end for bidirectional iterators
4944 // is much faster than for forward iterators.
4946 // find_end for forward iterators.
4947 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4949 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4950 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4951 forward_iterator_tag
, forward_iterator_tag
)
4953 if (__first2
== __last2
)
4957 _ForwardIterator1 __result
= __last1
;
4960 _ForwardIterator1 __new_result
4961 = std::search(__first1
, __last1
, __first2
, __last2
);
4962 if (__new_result
== __last1
)
4966 __result
= __new_result
;
4967 __first1
= __new_result
;
4974 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4975 typename _BinaryPredicate
>
4977 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4978 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4979 forward_iterator_tag
, forward_iterator_tag
,
4980 _BinaryPredicate __comp
)
4982 if (__first2
== __last2
)
4986 _ForwardIterator1 __result
= __last1
;
4989 _ForwardIterator1 __new_result
4990 = std::search(__first1
, __last1
, __first2
, __last2
, __comp
);
4991 if (__new_result
== __last1
)
4995 __result
= __new_result
;
4996 __first1
= __new_result
;
5003 // find_end for bidirectional iterators. Requires partial specialization.
5004 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
5005 _BidirectionalIterator1
5006 __find_end(_BidirectionalIterator1 __first1
,
5007 _BidirectionalIterator1 __last1
,
5008 _BidirectionalIterator2 __first2
,
5009 _BidirectionalIterator2 __last2
,
5010 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
5012 // concept requirements
5013 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5014 _BidirectionalIterator1
>)
5015 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5016 _BidirectionalIterator2
>)
5018 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
5019 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
5021 _RevIterator1
__rlast1(__first1
);
5022 _RevIterator2
__rlast2(__first2
);
5023 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
5024 _RevIterator2(__last2
), __rlast2
);
5026 if (__rresult
== __rlast1
)
5030 _BidirectionalIterator1 __result
= __rresult
.base();
5031 std::advance(__result
, -std::distance(__first2
, __last2
));
5036 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
5037 typename _BinaryPredicate
>
5038 _BidirectionalIterator1
5039 __find_end(_BidirectionalIterator1 __first1
,
5040 _BidirectionalIterator1 __last1
,
5041 _BidirectionalIterator2 __first2
,
5042 _BidirectionalIterator2 __last2
,
5043 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
5044 _BinaryPredicate __comp
)
5046 // concept requirements
5047 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5048 _BidirectionalIterator1
>)
5049 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5050 _BidirectionalIterator2
>)
5052 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
5053 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
5055 _RevIterator1
__rlast1(__first1
);
5056 _RevIterator2
__rlast2(__first2
);
5057 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
5058 _RevIterator2(__last2
), __rlast2
,
5061 if (__rresult
== __rlast1
)
5065 _BidirectionalIterator1 __result
= __rresult
.base();
5066 std::advance(__result
, -std::distance(__first2
, __last2
));
5071 // Dispatching functions for find_end.
5074 * @brief Find last matching subsequence in a sequence.
5075 * @param first1 Start of range to search.
5076 * @param last1 End of range to search.
5077 * @param first2 Start of sequence to match.
5078 * @param last2 End of sequence to match.
5079 * @return The last iterator @c i in the range
5080 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
5081 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
5082 * such iterator exists.
5084 * Searches the range @p [first1,last1) for a sub-sequence that compares
5085 * equal value-by-value with the sequence given by @p [first2,last2) and
5086 * returns an iterator to the first element of the sub-sequence, or
5087 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
5088 * last such subsequence contained in [first,last1).
5090 * Because the sub-sequence must lie completely within the range
5091 * @p [first1,last1) it must start at a position less than
5092 * @p last1-(last2-first2) where @p last2-first2 is the length of the
5094 * This means that the returned iterator @c i will be in the range
5095 * @p [first1,last1-(last2-first2))
5097 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
5098 inline _ForwardIterator1
5099 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
5100 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
5102 // concept requirements
5103 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
5104 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
5105 __glibcxx_function_requires(_EqualOpConcept
<
5106 typename iterator_traits
<_ForwardIterator1
>::value_type
,
5107 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
5108 __glibcxx_requires_valid_range(__first1
, __last1
);
5109 __glibcxx_requires_valid_range(__first2
, __last2
);
5111 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
5112 std::__iterator_category(__first1
),
5113 std::__iterator_category(__first2
));
5117 * @brief Find last matching subsequence in a sequence using a predicate.
5118 * @param first1 Start of range to search.
5119 * @param last1 End of range to search.
5120 * @param first2 Start of sequence to match.
5121 * @param last2 End of sequence to match.
5122 * @param comp The predicate to use.
5123 * @return The last iterator @c i in the range
5124 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
5125 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
5126 * @p last1 if no such iterator exists.
5128 * Searches the range @p [first1,last1) for a sub-sequence that compares
5129 * equal value-by-value with the sequence given by @p [first2,last2) using
5130 * comp as a predicate and returns an iterator to the first element of the
5131 * sub-sequence, or @p last1 if the sub-sequence is not found. The
5132 * sub-sequence will be the last such subsequence contained in
5135 * Because the sub-sequence must lie completely within the range
5136 * @p [first1,last1) it must start at a position less than
5137 * @p last1-(last2-first2) where @p last2-first2 is the length of the
5139 * This means that the returned iterator @c i will be in the range
5140 * @p [first1,last1-(last2-first2))
5142 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
5143 typename _BinaryPredicate
>
5144 inline _ForwardIterator1
5145 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
5146 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
5147 _BinaryPredicate __comp
)
5149 // concept requirements
5150 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
5151 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
5152 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
5153 typename iterator_traits
<_ForwardIterator1
>::value_type
,
5154 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
5155 __glibcxx_requires_valid_range(__first1
, __last1
);
5156 __glibcxx_requires_valid_range(__first2
, __last2
);
5158 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
5159 std::__iterator_category(__first1
),
5160 std::__iterator_category(__first2
),
5166 #endif /* _ALGO_H */