1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
64 #include <bits/stl_heap.h>
65 #include <bits/stl_tempbuf.h> // for _Temporary_buffer
66 #include <debug/debug.h>
68 // See concept_check.h for the __glibcxx_*_requires macros.
73 * @brief Find the median of three values.
77 * @return One of @p a, @p b or @p c.
79 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
80 * then the value returned will be @c m.
81 * This is an SGI extension.
82 * @ingroup SGIextensions
84 template<typename _Tp
>
86 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
)
88 // concept requirements
89 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
106 * @brief Find the median of three values using a predicate for comparison.
110 * @param comp A binary predicate.
111 * @return One of @p a, @p b or @p c.
113 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
114 * and @p comp(m,n) are both true then the value returned will be @c m.
115 * This is an SGI extension.
116 * @ingroup SGIextensions
118 template<typename _Tp
, typename _Compare
>
120 __median(const _Tp
& __a
, const _Tp
& __b
, const _Tp
& __c
, _Compare __comp
)
122 // concept requirements
123 __glibcxx_function_requires(_BinaryFunctionConcept
<_Compare
,bool,_Tp
,_Tp
>)
124 if (__comp(__a
, __b
))
125 if (__comp(__b
, __c
))
127 else if (__comp(__a
, __c
))
131 else if (__comp(__a
, __c
))
133 else if (__comp(__b
, __c
))
140 * @brief Apply a function to every element of a sequence.
141 * @param first An input iterator.
142 * @param last An input iterator.
143 * @param f A unary function object.
146 * Applies the function object @p f to each element in the range
147 * @p [first,last). @p f must not modify the order of the sequence.
148 * If @p f has a return value it is ignored.
150 template<typename _InputIterator
, typename _Function
>
152 for_each(_InputIterator __first
, _InputIterator __last
, _Function __f
)
154 // concept requirements
155 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
156 __glibcxx_requires_valid_range(__first
, __last
);
157 for ( ; __first
!= __last
; ++__first
)
164 * This is an overload used by find() for the Input Iterator case.
167 template<typename _InputIterator
, typename _Tp
>
168 inline _InputIterator
169 find(_InputIterator __first
, _InputIterator __last
,
170 const _Tp
& __val
, input_iterator_tag
)
172 while (__first
!= __last
&& !(*__first
== __val
))
179 * This is an overload used by find_if() for the Input Iterator case.
182 template<typename _InputIterator
, typename _Predicate
>
183 inline _InputIterator
184 find_if(_InputIterator __first
, _InputIterator __last
,
185 _Predicate __pred
, input_iterator_tag
)
187 while (__first
!= __last
&& !__pred(*__first
))
194 * This is an overload used by find() for the RAI case.
197 template<typename _RandomAccessIterator
, typename _Tp
>
198 _RandomAccessIterator
199 find(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
200 const _Tp
& __val
, random_access_iterator_tag
)
202 typename iterator_traits
<_RandomAccessIterator
>::difference_type
203 __trip_count
= (__last
- __first
) >> 2;
205 for ( ; __trip_count
> 0 ; --__trip_count
)
207 if (*__first
== __val
)
211 if (*__first
== __val
)
215 if (*__first
== __val
)
219 if (*__first
== __val
)
224 switch (__last
- __first
)
227 if (*__first
== __val
)
231 if (*__first
== __val
)
235 if (*__first
== __val
)
246 * This is an overload used by find_if() for the RAI case.
249 template<typename _RandomAccessIterator
, typename _Predicate
>
250 _RandomAccessIterator
251 find_if(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
252 _Predicate __pred
, random_access_iterator_tag
)
254 typename iterator_traits
<_RandomAccessIterator
>::difference_type
255 __trip_count
= (__last
- __first
) >> 2;
257 for ( ; __trip_count
> 0 ; --__trip_count
)
259 if (__pred(*__first
))
263 if (__pred(*__first
))
267 if (__pred(*__first
))
271 if (__pred(*__first
))
276 switch (__last
- __first
)
279 if (__pred(*__first
))
283 if (__pred(*__first
))
287 if (__pred(*__first
))
297 * @brief Find the first occurrence of a value in a sequence.
298 * @param first An input iterator.
299 * @param last An input iterator.
300 * @param val The value to find.
301 * @return The first iterator @c i in the range @p [first,last)
302 * such that @c *i == @p val, or @p last if no such iterator exists.
304 template<typename _InputIterator
, typename _Tp
>
305 inline _InputIterator
306 find(_InputIterator __first
, _InputIterator __last
,
309 // concept requirements
310 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
311 __glibcxx_function_requires(_EqualOpConcept
<
312 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
313 __glibcxx_requires_valid_range(__first
, __last
);
314 return std::find(__first
, __last
, __val
,
315 std::__iterator_category(__first
));
319 * @brief Find the first element in a sequence for which a predicate is true.
320 * @param first An input iterator.
321 * @param last An input iterator.
322 * @param pred A predicate.
323 * @return The first iterator @c i in the range @p [first,last)
324 * such that @p pred(*i) is true, or @p last if no such iterator exists.
326 template<typename _InputIterator
, typename _Predicate
>
327 inline _InputIterator
328 find_if(_InputIterator __first
, _InputIterator __last
,
331 // concept requirements
332 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
333 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
334 typename iterator_traits
<_InputIterator
>::value_type
>)
335 __glibcxx_requires_valid_range(__first
, __last
);
336 return std::find_if(__first
, __last
, __pred
,
337 std::__iterator_category(__first
));
341 * @brief Find two adjacent values in a sequence that are equal.
342 * @param first A forward iterator.
343 * @param last A forward iterator.
344 * @return The first iterator @c i such that @c i and @c i+1 are both
345 * valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
346 * or @p last if no such iterator exists.
348 template<typename _ForwardIterator
>
350 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
)
352 // concept requirements
353 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
354 __glibcxx_function_requires(_EqualityComparableConcept
<
355 typename iterator_traits
<_ForwardIterator
>::value_type
>)
356 __glibcxx_requires_valid_range(__first
, __last
);
357 if (__first
== __last
)
359 _ForwardIterator __next
= __first
;
360 while(++__next
!= __last
)
362 if (*__first
== *__next
)
370 * @brief Find two adjacent values in a sequence using a predicate.
371 * @param first A forward iterator.
372 * @param last A forward iterator.
373 * @param binary_pred A binary predicate.
374 * @return The first iterator @c i such that @c i and @c i+1 are both
375 * valid iterators in @p [first,last) and such that
376 * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
379 template<typename _ForwardIterator
, typename _BinaryPredicate
>
381 adjacent_find(_ForwardIterator __first
, _ForwardIterator __last
,
382 _BinaryPredicate __binary_pred
)
384 // concept requirements
385 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
386 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
387 typename iterator_traits
<_ForwardIterator
>::value_type
,
388 typename iterator_traits
<_ForwardIterator
>::value_type
>)
389 __glibcxx_requires_valid_range(__first
, __last
);
390 if (__first
== __last
)
392 _ForwardIterator __next
= __first
;
393 while(++__next
!= __last
)
395 if (__binary_pred(*__first
, *__next
))
403 * @brief Count the number of copies of a value in a sequence.
404 * @param first An input iterator.
405 * @param last An input iterator.
406 * @param value The value to be counted.
407 * @return The number of iterators @c i in the range @p [first,last)
408 * for which @c *i == @p value
410 template<typename _InputIterator
, typename _Tp
>
411 typename iterator_traits
<_InputIterator
>::difference_type
412 count(_InputIterator __first
, _InputIterator __last
, const _Tp
& __value
)
414 // concept requirements
415 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
416 __glibcxx_function_requires(_EqualityComparableConcept
<
417 typename iterator_traits
<_InputIterator
>::value_type
>)
418 __glibcxx_function_requires(_EqualityComparableConcept
<_Tp
>)
419 __glibcxx_requires_valid_range(__first
, __last
);
420 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
421 for ( ; __first
!= __last
; ++__first
)
422 if (*__first
== __value
)
428 * @brief Count the elements of a sequence for which a predicate is true.
429 * @param first An input iterator.
430 * @param last An input iterator.
431 * @param pred A predicate.
432 * @return The number of iterators @c i in the range @p [first,last)
433 * for which @p pred(*i) is true.
435 template<typename _InputIterator
, typename _Predicate
>
436 typename iterator_traits
<_InputIterator
>::difference_type
437 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
439 // concept requirements
440 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
441 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
442 typename iterator_traits
<_InputIterator
>::value_type
>)
443 __glibcxx_requires_valid_range(__first
, __last
);
444 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
445 for ( ; __first
!= __last
; ++__first
)
446 if (__pred(*__first
))
452 * @brief Search a sequence for a matching sub-sequence.
453 * @param first1 A forward iterator.
454 * @param last1 A forward iterator.
455 * @param first2 A forward iterator.
456 * @param last2 A forward iterator.
457 * @return The first iterator @c i in the range
458 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
459 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
460 * such iterator exists.
462 * Searches the range @p [first1,last1) for a sub-sequence that compares
463 * equal value-by-value with the sequence given by @p [first2,last2) and
464 * returns an iterator to the first element of the sub-sequence, or
465 * @p last1 if the sub-sequence is not found.
467 * Because the sub-sequence must lie completely within the range
468 * @p [first1,last1) it must start at a position less than
469 * @p last1-(last2-first2) where @p last2-first2 is the length of the
471 * This means that the returned iterator @c i will be in the range
472 * @p [first1,last1-(last2-first2))
474 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
476 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
477 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
479 // concept requirements
480 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
481 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
482 __glibcxx_function_requires(_EqualOpConcept
<
483 typename iterator_traits
<_ForwardIterator1
>::value_type
,
484 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
485 __glibcxx_requires_valid_range(__first1
, __last1
);
486 __glibcxx_requires_valid_range(__first2
, __last2
);
487 // Test for empty ranges
488 if (__first1
== __last1
|| __first2
== __last2
)
491 // Test for a pattern of length 1.
492 _ForwardIterator2
__tmp(__first2
);
494 if (__tmp
== __last2
)
495 return std::find(__first1
, __last1
, *__first2
);
498 _ForwardIterator2 __p1
, __p
;
499 __p1
= __first2
; ++__p1
;
500 _ForwardIterator1 __current
= __first1
;
502 while (__first1
!= __last1
)
504 __first1
= std::find(__first1
, __last1
, *__first2
);
505 if (__first1
== __last1
)
509 __current
= __first1
;
510 if (++__current
== __last1
)
513 while (*__current
== *__p
)
515 if (++__p
== __last2
)
517 if (++__current
== __last1
)
526 * @brief Search a sequence for a matching sub-sequence using a predicate.
527 * @param first1 A forward iterator.
528 * @param last1 A forward iterator.
529 * @param first2 A forward iterator.
530 * @param last2 A forward iterator.
531 * @param predicate A binary predicate.
532 * @return The first iterator @c i in the range
533 * @p [first1,last1-(last2-first2)) such that
534 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
535 * @p [0,last2-first2), or @p last1 if no such iterator exists.
537 * Searches the range @p [first1,last1) for a sub-sequence that compares
538 * equal value-by-value with the sequence given by @p [first2,last2),
539 * using @p predicate to determine equality, and returns an iterator
540 * to the first element of the sub-sequence, or @p last1 if no such
543 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
545 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
546 typename _BinaryPredicate
>
548 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
549 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
550 _BinaryPredicate __predicate
)
552 // concept requirements
553 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
554 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
555 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
556 typename iterator_traits
<_ForwardIterator1
>::value_type
,
557 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
558 __glibcxx_requires_valid_range(__first1
, __last1
);
559 __glibcxx_requires_valid_range(__first2
, __last2
);
561 // Test for empty ranges
562 if (__first1
== __last1
|| __first2
== __last2
)
565 // Test for a pattern of length 1.
566 _ForwardIterator2
__tmp(__first2
);
568 if (__tmp
== __last2
)
570 while (__first1
!= __last1
&& !__predicate(*__first1
, *__first2
))
576 _ForwardIterator2 __p1
, __p
;
577 __p1
= __first2
; ++__p1
;
578 _ForwardIterator1 __current
= __first1
;
580 while (__first1
!= __last1
)
582 while (__first1
!= __last1
)
584 if (__predicate(*__first1
, *__first2
))
588 while (__first1
!= __last1
&& !__predicate(*__first1
, *__first2
))
590 if (__first1
== __last1
)
594 __current
= __first1
;
595 if (++__current
== __last1
)
598 while (__predicate(*__current
, *__p
))
600 if (++__p
== __last2
)
602 if (++__current
== __last1
)
611 * @brief Search a sequence for a number of consecutive values.
612 * @param first A forward iterator.
613 * @param last A forward iterator.
614 * @param count The number of consecutive values.
615 * @param val The value to find.
616 * @return The first iterator @c i in the range @p [first,last-count)
617 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
618 * or @p last if no such iterator exists.
620 * Searches the range @p [first,last) for @p count consecutive elements
623 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
625 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
626 _Integer __count
, const _Tp
& __val
)
628 // concept requirements
629 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
630 __glibcxx_function_requires(_EqualityComparableConcept
<
631 typename iterator_traits
<_ForwardIterator
>::value_type
>)
632 __glibcxx_function_requires(_EqualityComparableConcept
<_Tp
>)
633 __glibcxx_requires_valid_range(__first
, __last
);
639 __first
= std::find(__first
, __last
, __val
);
640 while (__first
!= __last
)
642 typename iterator_traits
<_ForwardIterator
>::difference_type
644 _ForwardIterator __i
= __first
;
646 while (__i
!= __last
&& __n
!= 1 && *__i
== __val
)
654 __first
= std::find(__i
, __last
, __val
);
661 * @brief Search a sequence for a number of consecutive values using a
663 * @param first A forward iterator.
664 * @param last A forward iterator.
665 * @param count The number of consecutive values.
666 * @param val The value to find.
667 * @param binary_pred A binary predicate.
668 * @return The first iterator @c i in the range @p [first,last-count)
669 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
670 * range @p [0,count), or @p last if no such iterator exists.
672 * Searches the range @p [first,last) for @p count consecutive elements
673 * for which the predicate returns true.
675 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
676 typename _BinaryPredicate
>
678 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
679 _Integer __count
, const _Tp
& __val
,
680 _BinaryPredicate __binary_pred
)
682 // concept requirements
683 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
684 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
685 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
686 __glibcxx_requires_valid_range(__first
, __last
);
692 while (__first
!= __last
)
694 if (__binary_pred(*__first
, __val
))
698 while (__first
!= __last
)
700 typename iterator_traits
<_ForwardIterator
>::difference_type
702 _ForwardIterator __i
= __first
;
704 while (__i
!= __last
&& __n
!= 1 && __binary_pred(*__i
, __val
))
713 while (__i
!= __last
)
715 if (__binary_pred(*__i
, __val
))
727 * @brief Swap the elements of two sequences.
728 * @param first1 A forward iterator.
729 * @param last1 A forward iterator.
730 * @param first2 A forward iterator.
731 * @return An iterator equal to @p first2+(last1-first1).
733 * Swaps each element in the range @p [first1,last1) with the
734 * corresponding element in the range @p [first2,(last1-first1)).
735 * The ranges must not overlap.
737 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
739 swap_ranges(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
740 _ForwardIterator2 __first2
)
742 // concept requirements
743 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
745 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
747 __glibcxx_function_requires(_ConvertibleConcept
<
748 typename iterator_traits
<_ForwardIterator1
>::value_type
,
749 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
750 __glibcxx_function_requires(_ConvertibleConcept
<
751 typename iterator_traits
<_ForwardIterator2
>::value_type
,
752 typename iterator_traits
<_ForwardIterator1
>::value_type
>)
753 __glibcxx_requires_valid_range(__first1
, __last1
);
755 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
)
756 std::iter_swap(__first1
, __first2
);
761 * @brief Perform an operation on a sequence.
762 * @param first An input iterator.
763 * @param last An input iterator.
764 * @param result An output iterator.
765 * @param unary_op A unary operator.
766 * @return An output iterator equal to @p result+(last-first).
768 * Applies the operator to each element in the input range and assigns
769 * the results to successive elements of the output sequence.
770 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
771 * range @p [0,last-first).
773 * @p unary_op must not alter its argument.
775 template<typename _InputIterator
, typename _OutputIterator
,
776 typename _UnaryOperation
>
778 transform(_InputIterator __first
, _InputIterator __last
,
779 _OutputIterator __result
, _UnaryOperation __unary_op
)
781 // concept requirements
782 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
783 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
784 // "the type returned by a _UnaryOperation"
785 __typeof__(__unary_op(*__first
))>)
786 __glibcxx_requires_valid_range(__first
, __last
);
788 for ( ; __first
!= __last
; ++__first
, ++__result
)
789 *__result
= __unary_op(*__first
);
794 * @brief Perform an operation on corresponding elements of two sequences.
795 * @param first1 An input iterator.
796 * @param last1 An input iterator.
797 * @param first2 An input iterator.
798 * @param result An output iterator.
799 * @param binary_op A binary operator.
800 * @return An output iterator equal to @p result+(last-first).
802 * Applies the operator to the corresponding elements in the two
803 * input ranges and assigns the results to successive elements of the
805 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
806 * @c N in the range @p [0,last1-first1).
808 * @p binary_op must not alter either of its arguments.
810 template<typename _InputIterator1
, typename _InputIterator2
,
811 typename _OutputIterator
, typename _BinaryOperation
>
813 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
814 _InputIterator2 __first2
, _OutputIterator __result
,
815 _BinaryOperation __binary_op
)
817 // concept requirements
818 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
819 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
820 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
821 // "the type returned by a _BinaryOperation"
822 __typeof__(__binary_op(*__first1
,*__first2
))>)
823 __glibcxx_requires_valid_range(__first1
, __last1
);
825 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
826 *__result
= __binary_op(*__first1
, *__first2
);
831 * @brief Replace each occurrence of one value in a sequence with another
833 * @param first A forward iterator.
834 * @param last A forward iterator.
835 * @param old_value The value to be replaced.
836 * @param new_value The replacement value.
837 * @return replace() returns no value.
839 * For each iterator @c i in the range @p [first,last) if @c *i ==
840 * @p old_value then the assignment @c *i = @p new_value is performed.
842 template<typename _ForwardIterator
, typename _Tp
>
844 replace(_ForwardIterator __first
, _ForwardIterator __last
,
845 const _Tp
& __old_value
, const _Tp
& __new_value
)
847 // concept requirements
848 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
850 __glibcxx_function_requires(_EqualOpConcept
<
851 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
852 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
853 typename iterator_traits
<_ForwardIterator
>::value_type
>)
854 __glibcxx_requires_valid_range(__first
, __last
);
856 for ( ; __first
!= __last
; ++__first
)
857 if (*__first
== __old_value
)
858 *__first
= __new_value
;
862 * @brief Replace each value in a sequence for which a predicate returns
863 * true with another value.
864 * @param first A forward iterator.
865 * @param last A forward iterator.
866 * @param pred A predicate.
867 * @param new_value The replacement value.
868 * @return replace_if() returns no value.
870 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
871 * is true then the assignment @c *i = @p new_value is performed.
873 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
875 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
876 _Predicate __pred
, const _Tp
& __new_value
)
878 // concept requirements
879 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
881 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
882 typename iterator_traits
<_ForwardIterator
>::value_type
>)
883 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
884 typename iterator_traits
<_ForwardIterator
>::value_type
>)
885 __glibcxx_requires_valid_range(__first
, __last
);
887 for ( ; __first
!= __last
; ++__first
)
888 if (__pred(*__first
))
889 *__first
= __new_value
;
893 * @brief Copy a sequence, replacing each element of one value with another
895 * @param first An input iterator.
896 * @param last An input iterator.
897 * @param result An output iterator.
898 * @param old_value The value to be replaced.
899 * @param new_value The replacement value.
900 * @return The end of the output sequence, @p result+(last-first).
902 * Copies each element in the input range @p [first,last) to the
903 * output range @p [result,result+(last-first)) replacing elements
904 * equal to @p old_value with @p new_value.
906 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
908 replace_copy(_InputIterator __first
, _InputIterator __last
,
909 _OutputIterator __result
,
910 const _Tp
& __old_value
, const _Tp
& __new_value
)
912 // concept requirements
913 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
914 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
915 typename iterator_traits
<_InputIterator
>::value_type
>)
916 __glibcxx_function_requires(_EqualOpConcept
<
917 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
918 __glibcxx_requires_valid_range(__first
, __last
);
920 for ( ; __first
!= __last
; ++__first
, ++__result
)
921 *__result
= *__first
== __old_value
? __new_value
: *__first
;
926 * @brief Copy a sequence, replacing each value for which a predicate
927 * returns true with another value.
928 * @param first An input iterator.
929 * @param last An input iterator.
930 * @param result An output iterator.
931 * @param pred A predicate.
932 * @param new_value The replacement value.
933 * @return The end of the output sequence, @p result+(last-first).
935 * Copies each element in the range @p [first,last) to the range
936 * @p [result,result+(last-first)) replacing elements for which
937 * @p pred returns true with @p new_value.
939 template<typename _InputIterator
, typename _OutputIterator
,
940 typename _Predicate
, typename _Tp
>
942 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
943 _OutputIterator __result
,
944 _Predicate __pred
, const _Tp
& __new_value
)
946 // concept requirements
947 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
948 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
949 typename iterator_traits
<_InputIterator
>::value_type
>)
950 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
951 typename iterator_traits
<_InputIterator
>::value_type
>)
952 __glibcxx_requires_valid_range(__first
, __last
);
954 for ( ; __first
!= __last
; ++__first
, ++__result
)
955 *__result
= __pred(*__first
) ? __new_value
: *__first
;
960 * @brief Assign the result of a function object to each value in a
962 * @param first A forward iterator.
963 * @param last A forward iterator.
964 * @param gen A function object taking no arguments.
965 * @return generate() returns no value.
967 * Performs the assignment @c *i = @p gen() for each @c i in the range
970 template<typename _ForwardIterator
, typename _Generator
>
972 generate(_ForwardIterator __first
, _ForwardIterator __last
,
975 // concept requirements
976 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
977 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
978 typename iterator_traits
<_ForwardIterator
>::value_type
>)
979 __glibcxx_requires_valid_range(__first
, __last
);
981 for ( ; __first
!= __last
; ++__first
)
986 * @brief Assign the result of a function object to each value in a
988 * @param first A forward iterator.
989 * @param n The length of the sequence.
990 * @param gen A function object taking no arguments.
991 * @return The end of the sequence, @p first+n
993 * Performs the assignment @c *i = @p gen() for each @c i in the range
994 * @p [first,first+n).
996 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
998 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
1000 // concept requirements
1001 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1002 // "the type returned by a _Generator"
1003 __typeof__(__gen())>)
1005 for ( ; __n
> 0; --__n
, ++__first
)
1011 * @brief Copy a sequence, removing elements of a given value.
1012 * @param first An input iterator.
1013 * @param last An input iterator.
1014 * @param result An output iterator.
1015 * @param value The value to be removed.
1016 * @return An iterator designating the end of the resulting sequence.
1018 * Copies each element in the range @p [first,last) not equal to @p value
1019 * to the range beginning at @p result.
1020 * remove_copy() is stable, so the relative order of elements that are
1021 * copied is unchanged.
1023 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
1025 remove_copy(_InputIterator __first
, _InputIterator __last
,
1026 _OutputIterator __result
, const _Tp
& __value
)
1028 // concept requirements
1029 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1030 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1031 typename iterator_traits
<_InputIterator
>::value_type
>)
1032 __glibcxx_function_requires(_EqualOpConcept
<
1033 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
1034 __glibcxx_requires_valid_range(__first
, __last
);
1036 for ( ; __first
!= __last
; ++__first
)
1037 if (!(*__first
== __value
))
1039 *__result
= *__first
;
1046 * @brief Copy a sequence, removing elements for which a predicate is true.
1047 * @param first An input iterator.
1048 * @param last An input iterator.
1049 * @param result An output iterator.
1050 * @param pred A predicate.
1051 * @return An iterator designating the end of the resulting sequence.
1053 * Copies each element in the range @p [first,last) for which
1054 * @p pred returns true to the range beginning at @p result.
1056 * remove_copy_if() is stable, so the relative order of elements that are
1057 * copied is unchanged.
1059 template<typename _InputIterator
, typename _OutputIterator
,
1060 typename _Predicate
>
1062 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
1063 _OutputIterator __result
, _Predicate __pred
)
1065 // concept requirements
1066 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1067 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1068 typename iterator_traits
<_InputIterator
>::value_type
>)
1069 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1070 typename iterator_traits
<_InputIterator
>::value_type
>)
1071 __glibcxx_requires_valid_range(__first
, __last
);
1073 for ( ; __first
!= __last
; ++__first
)
1074 if (!__pred(*__first
))
1076 *__result
= *__first
;
1083 * @brief Remove elements from a sequence.
1084 * @param first An input iterator.
1085 * @param last An input iterator.
1086 * @param value The value to be removed.
1087 * @return An iterator designating the end of the resulting sequence.
1089 * All elements equal to @p value are removed from the range
1092 * remove() is stable, so the relative order of elements that are
1093 * not removed is unchanged.
1095 * Elements between the end of the resulting sequence and @p last
1096 * are still present, but their value is unspecified.
1098 template<typename _ForwardIterator
, typename _Tp
>
1100 remove(_ForwardIterator __first
, _ForwardIterator __last
,
1103 // concept requirements
1104 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1106 __glibcxx_function_requires(_EqualOpConcept
<
1107 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1108 __glibcxx_requires_valid_range(__first
, __last
);
1110 __first
= std::find(__first
, __last
, __value
);
1111 _ForwardIterator __i
= __first
;
1112 return __first
== __last
? __first
1113 : std::remove_copy(++__i
, __last
,
1118 * @brief Remove elements from a sequence using a predicate.
1119 * @param first A forward iterator.
1120 * @param last A forward iterator.
1121 * @param pred A predicate.
1122 * @return An iterator designating the end of the resulting sequence.
1124 * All elements for which @p pred returns true are removed from the range
1127 * remove_if() is stable, so the relative order of elements that are
1128 * not removed is unchanged.
1130 * Elements between the end of the resulting sequence and @p last
1131 * are still present, but their value is unspecified.
1133 template<typename _ForwardIterator
, typename _Predicate
>
1135 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1138 // concept requirements
1139 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1141 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1142 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1143 __glibcxx_requires_valid_range(__first
, __last
);
1145 __first
= std::find_if(__first
, __last
, __pred
);
1146 _ForwardIterator __i
= __first
;
1147 return __first
== __last
? __first
1148 : std::remove_copy_if(++__i
, __last
,
1154 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1156 * overloaded for output iterators.
1159 template<typename _InputIterator
, typename _OutputIterator
>
1161 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1162 _OutputIterator __result
,
1163 output_iterator_tag
)
1165 // concept requirements -- taken care of in dispatching function
1166 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1167 *__result
= __value
;
1168 while (++__first
!= __last
)
1169 if (!(__value
== *__first
))
1172 *++__result
= __value
;
1179 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1181 * overloaded for forward iterators.
1184 template<typename _InputIterator
, typename _ForwardIterator
>
1186 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1187 _ForwardIterator __result
,
1188 forward_iterator_tag
)
1190 // concept requirements -- taken care of in dispatching function
1191 *__result
= *__first
;
1192 while (++__first
!= __last
)
1193 if (!(*__result
== *__first
))
1194 *++__result
= *__first
;
1200 * This is an uglified
1201 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1203 * overloaded for output iterators.
1206 template<typename _InputIterator
, typename _OutputIterator
,
1207 typename _BinaryPredicate
>
1209 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1210 _OutputIterator __result
,
1211 _BinaryPredicate __binary_pred
,
1212 output_iterator_tag
)
1214 // concept requirements -- iterators already checked
1215 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1216 typename iterator_traits
<_InputIterator
>::value_type
,
1217 typename iterator_traits
<_InputIterator
>::value_type
>)
1219 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1220 *__result
= __value
;
1221 while (++__first
!= __last
)
1222 if (!__binary_pred(__value
, *__first
))
1225 *++__result
= __value
;
1232 * This is an uglified
1233 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1235 * overloaded for forward iterators.
1238 template<typename _InputIterator
, typename _ForwardIterator
,
1239 typename _BinaryPredicate
>
1241 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1242 _ForwardIterator __result
,
1243 _BinaryPredicate __binary_pred
,
1244 forward_iterator_tag
)
1246 // concept requirements -- iterators already checked
1247 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1248 typename iterator_traits
<_ForwardIterator
>::value_type
,
1249 typename iterator_traits
<_InputIterator
>::value_type
>)
1251 *__result
= *__first
;
1252 while (++__first
!= __last
)
1253 if (!__binary_pred(*__result
, *__first
)) *++__result
= *__first
;
1258 * @brief Copy a sequence, removing consecutive duplicate values.
1259 * @param first An input iterator.
1260 * @param last An input iterator.
1261 * @param result An output iterator.
1262 * @return An iterator designating the end of the resulting sequence.
1264 * Copies each element in the range @p [first,last) to the range
1265 * beginning at @p result, except that only the first element is copied
1266 * from groups of consecutive elements that compare equal.
1267 * unique_copy() is stable, so the relative order of elements that are
1268 * copied is unchanged.
1270 template<typename _InputIterator
, typename _OutputIterator
>
1271 inline _OutputIterator
1272 unique_copy(_InputIterator __first
, _InputIterator __last
,
1273 _OutputIterator __result
)
1275 // concept requirements
1276 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1277 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1278 typename iterator_traits
<_InputIterator
>::value_type
>)
1279 __glibcxx_function_requires(_EqualityComparableConcept
<
1280 typename iterator_traits
<_InputIterator
>::value_type
>)
1281 __glibcxx_requires_valid_range(__first
, __last
);
1283 typedef typename iterator_traits
<_OutputIterator
>::iterator_category
1286 if (__first
== __last
) return __result
;
1287 return std::__unique_copy(__first
, __last
, __result
, _IterType());
1291 * @brief Copy a sequence, removing consecutive values using a predicate.
1292 * @param first An input iterator.
1293 * @param last An input iterator.
1294 * @param result An output iterator.
1295 * @param binary_pred A binary predicate.
1296 * @return An iterator designating the end of the resulting sequence.
1298 * Copies each element in the range @p [first,last) to the range
1299 * beginning at @p result, except that only the first element is copied
1300 * from groups of consecutive elements for which @p binary_pred returns
1302 * unique_copy() is stable, so the relative order of elements that are
1303 * copied is unchanged.
1305 template<typename _InputIterator
, typename _OutputIterator
,
1306 typename _BinaryPredicate
>
1307 inline _OutputIterator
1308 unique_copy(_InputIterator __first
, _InputIterator __last
,
1309 _OutputIterator __result
,
1310 _BinaryPredicate __binary_pred
)
1312 // concept requirements -- predicates checked later
1313 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1314 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1315 typename iterator_traits
<_InputIterator
>::value_type
>)
1316 __glibcxx_requires_valid_range(__first
, __last
);
1318 typedef typename iterator_traits
<_OutputIterator
>::iterator_category
1321 if (__first
== __last
) return __result
;
1322 return std::__unique_copy(__first
, __last
, __result
,
1323 __binary_pred
, _IterType());
1327 * @brief Remove consecutive duplicate values from a sequence.
1328 * @param first A forward iterator.
1329 * @param last A forward iterator.
1330 * @return An iterator designating the end of the resulting sequence.
1332 * Removes all but the first element from each group of consecutive
1333 * values that compare equal.
1334 * unique() is stable, so the relative order of elements that are
1335 * not removed is unchanged.
1336 * Elements between the end of the resulting sequence and @p last
1337 * are still present, but their value is unspecified.
1339 template<typename _ForwardIterator
>
1341 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1343 // concept requirements
1344 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1346 __glibcxx_function_requires(_EqualityComparableConcept
<
1347 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1348 __glibcxx_requires_valid_range(__first
, __last
);
1350 // Skip the beginning, if already unique.
1351 __first
= std::adjacent_find(__first
, __last
);
1352 if (__first
== __last
)
1355 // Do the real copy work.
1356 _ForwardIterator __dest
= __first
;
1358 while (++__first
!= __last
)
1359 if (!(*__dest
== *__first
))
1360 *++__dest
= *__first
;
1365 * @brief Remove consecutive values from a sequence using a predicate.
1366 * @param first A forward iterator.
1367 * @param last A forward iterator.
1368 * @param binary_pred A binary predicate.
1369 * @return An iterator designating the end of the resulting sequence.
1371 * Removes all but the first element from each group of consecutive
1372 * values for which @p binary_pred returns true.
1373 * unique() is stable, so the relative order of elements that are
1374 * not removed is unchanged.
1375 * Elements between the end of the resulting sequence and @p last
1376 * are still present, but their value is unspecified.
1378 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1380 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1381 _BinaryPredicate __binary_pred
)
1383 // concept requirements
1384 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1386 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1387 typename iterator_traits
<_ForwardIterator
>::value_type
,
1388 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1389 __glibcxx_requires_valid_range(__first
, __last
);
1391 // Skip the beginning, if already unique.
1392 __first
= std::adjacent_find(__first
, __last
, __binary_pred
);
1393 if (__first
== __last
)
1396 // Do the real copy work.
1397 _ForwardIterator __dest
= __first
;
1399 while (++__first
!= __last
)
1400 if (!__binary_pred(*__dest
, *__first
))
1401 *++__dest
= *__first
;
1407 * This is an uglified reverse(_BidirectionalIterator,
1408 * _BidirectionalIterator)
1409 * overloaded for bidirectional iterators.
1412 template<typename _BidirectionalIterator
>
1414 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1415 bidirectional_iterator_tag
)
1418 if (__first
== __last
|| __first
== --__last
)
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 const _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
, copy(__middle
, __last
, __result
));
1743 * @brief Randomly shuffle the elements of a sequence.
1744 * @param first A forward iterator.
1745 * @param last A forward iterator.
1748 * Reorder the elements in the range @p [first,last) using a random
1749 * distribution, so that every possible ordering of the sequence is
1752 template<typename _RandomAccessIterator
>
1754 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
1756 // concept requirements
1757 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1758 _RandomAccessIterator
>)
1759 __glibcxx_requires_valid_range(__first
, __last
);
1761 if (__first
!= __last
)
1762 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1763 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
1767 * @brief Shuffle the elements of a sequence using a random number
1769 * @param first A forward iterator.
1770 * @param last A forward iterator.
1771 * @param rand The RNG functor or function.
1774 * Reorders the elements in the range @p [first,last) using @p rand to
1775 * provide a random distribution. Calling @p rand(N) for a positive
1776 * integer @p N should return a randomly chosen integer from the
1779 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
1781 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1782 _RandomNumberGenerator
& __rand
)
1784 // concept requirements
1785 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1786 _RandomAccessIterator
>)
1787 __glibcxx_requires_valid_range(__first
, __last
);
1789 if (__first
== __last
)
1791 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1792 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
1798 * This is a helper function...
1801 template<typename _ForwardIterator
, typename _Predicate
>
1803 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1805 forward_iterator_tag
)
1807 if (__first
== __last
)
1810 while (__pred(*__first
))
1811 if (++__first
== __last
)
1814 _ForwardIterator __next
= __first
;
1816 while (++__next
!= __last
)
1817 if (__pred(*__next
))
1819 swap(*__first
, *__next
);
1828 * This is a helper function...
1831 template<typename _BidirectionalIterator
, typename _Predicate
>
1832 _BidirectionalIterator
1833 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1835 bidirectional_iterator_tag
)
1840 if (__first
== __last
)
1842 else if (__pred(*__first
))
1848 if (__first
== __last
)
1850 else if (!__pred(*__last
))
1854 std::iter_swap(__first
, __last
);
1860 * @brief Move elements for which a predicate is true to the beginning
1862 * @param first A forward iterator.
1863 * @param last A forward iterator.
1864 * @param pred A predicate functor.
1865 * @return An iterator @p middle such that @p pred(i) is true for each
1866 * iterator @p i in the range @p [first,middle) and false for each @p i
1867 * in the range @p [middle,last).
1869 * @p pred must not modify its operand. @p partition() does not preserve
1870 * the relative ordering of elements in each group, use
1871 * @p stable_partition() if this is needed.
1873 template<typename _ForwardIterator
, typename _Predicate
>
1874 inline _ForwardIterator
1875 partition(_ForwardIterator __first
, _ForwardIterator __last
,
1878 // concept requirements
1879 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1881 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1882 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1883 __glibcxx_requires_valid_range(__first
, __last
);
1885 return std::__partition(__first
, __last
, __pred
,
1886 std::__iterator_category(__first
));
1892 * This is a helper function...
1895 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1897 __inplace_stable_partition(_ForwardIterator __first
,
1898 _ForwardIterator __last
,
1899 _Predicate __pred
, _Distance __len
)
1902 return __pred(*__first
) ? __last
: __first
;
1903 _ForwardIterator __middle
= __first
;
1904 std::advance(__middle
, __len
/ 2);
1905 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1909 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1913 std::rotate(__begin
, __middle
, __end
);
1914 std::advance(__begin
, std::distance(__middle
, __end
));
1920 * This is a helper function...
1923 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1926 __stable_partition_adaptive(_ForwardIterator __first
,
1927 _ForwardIterator __last
,
1928 _Predicate __pred
, _Distance __len
,
1930 _Distance __buffer_size
)
1932 if (__len
<= __buffer_size
)
1934 _ForwardIterator __result1
= __first
;
1935 _Pointer __result2
= __buffer
;
1936 for ( ; __first
!= __last
; ++__first
)
1937 if (__pred(*__first
))
1939 *__result1
= *__first
;
1944 *__result2
= *__first
;
1947 std::copy(__buffer
, __result2
, __result1
);
1952 _ForwardIterator __middle
= __first
;
1953 std::advance(__middle
, __len
/ 2);
1954 _ForwardIterator __begin
=
1955 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1956 __len
/ 2, __buffer
,
1958 _ForwardIterator __end
=
1959 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1961 __buffer
, __buffer_size
);
1962 std::rotate(__begin
, __middle
, __end
);
1963 std::advance(__begin
, std::distance(__middle
, __end
));
1969 * @brief Move elements for which a predicate is true to the beginning
1970 * of a sequence, preserving relative ordering.
1971 * @param first A forward iterator.
1972 * @param last A forward iterator.
1973 * @param pred A predicate functor.
1974 * @return An iterator @p middle such that @p pred(i) is true for each
1975 * iterator @p i in the range @p [first,middle) and false for each @p i
1976 * in the range @p [middle,last).
1978 * Performs the same function as @p partition() with the additional
1979 * guarantee that the relative ordering of elements in each group is
1980 * preserved, so any two elements @p x and @p y in the range
1981 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1982 * relative ordering after calling @p stable_partition().
1984 template<typename _ForwardIterator
, typename _Predicate
>
1986 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1989 // concept requirements
1990 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1992 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1993 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1994 __glibcxx_requires_valid_range(__first
, __last
);
1996 if (__first
== __last
)
2000 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2002 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2005 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
2007 if (__buf
.size() > 0)
2009 std::__stable_partition_adaptive(__first
, __last
, __pred
,
2010 _DistanceType(__buf
.requested_size()),
2011 __buf
.begin(), __buf
.size());
2014 std::__inplace_stable_partition(__first
, __last
, __pred
,
2015 _DistanceType(__buf
.requested_size()));
2021 * This is a helper function...
2024 template<typename _RandomAccessIterator
, typename _Tp
>
2025 _RandomAccessIterator
2026 __unguarded_partition(_RandomAccessIterator __first
,
2027 _RandomAccessIterator __last
, _Tp __pivot
)
2031 while (*__first
< __pivot
)
2034 while (__pivot
< *__last
)
2036 if (!(__first
< __last
))
2038 std::iter_swap(__first
, __last
);
2045 * This is a helper function...
2048 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2049 _RandomAccessIterator
2050 __unguarded_partition(_RandomAccessIterator __first
,
2051 _RandomAccessIterator __last
,
2052 _Tp __pivot
, _Compare __comp
)
2056 while (__comp(*__first
, __pivot
))
2059 while (__comp(__pivot
, *__last
))
2061 if (!(__first
< __last
))
2063 std::iter_swap(__first
, __last
);
2071 * This controls some aspect of the sort routines.
2074 enum { _S_threshold
= 16 };
2078 * This is a helper function for the sort routine.
2081 template<typename _RandomAccessIterator
, typename _Tp
>
2083 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
2085 _RandomAccessIterator __next
= __last
;
2087 while (__val
< *__next
)
2098 * This is a helper function for the sort routine.
2101 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2103 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
2106 _RandomAccessIterator __next
= __last
;
2108 while (__comp(__val
, *__next
))
2119 * This is a helper function for the sort routine.
2122 template<typename _RandomAccessIterator
>
2124 __insertion_sort(_RandomAccessIterator __first
,
2125 _RandomAccessIterator __last
)
2127 if (__first
== __last
)
2130 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2132 typename iterator_traits
<_RandomAccessIterator
>::value_type
2134 if (__val
< *__first
)
2136 std::copy_backward(__first
, __i
, __i
+ 1);
2140 std::__unguarded_linear_insert(__i
, __val
);
2146 * This is a helper function for the sort routine.
2149 template<typename _RandomAccessIterator
, typename _Compare
>
2151 __insertion_sort(_RandomAccessIterator __first
,
2152 _RandomAccessIterator __last
, _Compare __comp
)
2154 if (__first
== __last
) return;
2156 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2158 typename iterator_traits
<_RandomAccessIterator
>::value_type
2160 if (__comp(__val
, *__first
))
2162 std::copy_backward(__first
, __i
, __i
+ 1);
2166 std::__unguarded_linear_insert(__i
, __val
, __comp
);
2172 * This is a helper function for the sort routine.
2175 template<typename _RandomAccessIterator
>
2177 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2178 _RandomAccessIterator __last
)
2180 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2183 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2184 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
2189 * This is a helper function for the sort routine.
2192 template<typename _RandomAccessIterator
, typename _Compare
>
2194 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2195 _RandomAccessIterator __last
, _Compare __comp
)
2197 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2200 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2201 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
2206 * This is a helper function for the sort routine.
2209 template<typename _RandomAccessIterator
>
2211 __final_insertion_sort(_RandomAccessIterator __first
,
2212 _RandomAccessIterator __last
)
2214 if (__last
- __first
> _S_threshold
)
2216 std::__insertion_sort(__first
, __first
+ _S_threshold
);
2217 std::__unguarded_insertion_sort(__first
+ _S_threshold
, __last
);
2220 std::__insertion_sort(__first
, __last
);
2225 * This is a helper function for the sort routine.
2228 template<typename _RandomAccessIterator
, typename _Compare
>
2230 __final_insertion_sort(_RandomAccessIterator __first
,
2231 _RandomAccessIterator __last
, _Compare __comp
)
2233 if (__last
- __first
> _S_threshold
)
2235 std::__insertion_sort(__first
, __first
+ _S_threshold
, __comp
);
2236 std::__unguarded_insertion_sort(__first
+ _S_threshold
, __last
,
2240 std::__insertion_sort(__first
, __last
, __comp
);
2245 * This is a helper function for the sort routine.
2248 template<typename _Size
>
2253 for (__k
= 0; __n
!= 1; __n
>>= 1)
2259 * @brief Sort the smallest elements of a sequence.
2260 * @param first An iterator.
2261 * @param middle Another iterator.
2262 * @param last Another iterator.
2265 * Sorts the smallest @p (middle-first) elements in the range
2266 * @p [first,last) and moves them to the range @p [first,middle). The
2267 * order of the remaining elements in the range @p [middle,last) is
2269 * After the sort if @p i and @j are iterators in the range
2270 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2271 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
2273 template<typename _RandomAccessIterator
>
2275 partial_sort(_RandomAccessIterator __first
,
2276 _RandomAccessIterator __middle
,
2277 _RandomAccessIterator __last
)
2279 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2282 // concept requirements
2283 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2284 _RandomAccessIterator
>)
2285 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2286 __glibcxx_requires_valid_range(__first
, __middle
);
2287 __glibcxx_requires_valid_range(__middle
, __last
);
2289 std::make_heap(__first
, __middle
);
2290 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
2291 if (*__i
< *__first
)
2292 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
));
2293 std::sort_heap(__first
, __middle
);
2297 * @brief Sort the smallest elements of a sequence using a predicate
2299 * @param first An iterator.
2300 * @param middle Another iterator.
2301 * @param last Another iterator.
2302 * @param comp A comparison functor.
2305 * Sorts the smallest @p (middle-first) elements in the range
2306 * @p [first,last) and moves them to the range @p [first,middle). The
2307 * order of the remaining elements in the range @p [middle,last) is
2309 * After the sort if @p i and @j are iterators in the range
2310 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2311 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
2314 template<typename _RandomAccessIterator
, typename _Compare
>
2316 partial_sort(_RandomAccessIterator __first
,
2317 _RandomAccessIterator __middle
,
2318 _RandomAccessIterator __last
,
2321 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2324 // concept requirements
2325 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2326 _RandomAccessIterator
>)
2327 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2328 _ValueType
, _ValueType
>)
2329 __glibcxx_requires_valid_range(__first
, __middle
);
2330 __glibcxx_requires_valid_range(__middle
, __last
);
2332 std::make_heap(__first
, __middle
, __comp
);
2333 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
2334 if (__comp(*__i
, *__first
))
2335 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
), __comp
);
2336 std::sort_heap(__first
, __middle
, __comp
);
2340 * @brief Copy the smallest elements of a sequence.
2341 * @param first An iterator.
2342 * @param last Another iterator.
2343 * @param result_first A random-access iterator.
2344 * @param result_last Another random-access iterator.
2345 * @return An iterator indicating the end of the resulting sequence.
2347 * Copies and sorts the smallest N values from the range @p [first,last)
2348 * to the range beginning at @p result_first, where the number of
2349 * elements to be copied, @p N, is the smaller of @p (last-first) and
2350 * @p (result_last-result_first).
2351 * After the sort if @p i and @j are iterators in the range
2352 * @p [result_first,result_first+N) such that @i precedes @j then
2353 * @p *j<*i is false.
2354 * The value returned is @p result_first+N.
2356 template<typename _InputIterator
, typename _RandomAccessIterator
>
2357 _RandomAccessIterator
2358 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2359 _RandomAccessIterator __result_first
,
2360 _RandomAccessIterator __result_last
)
2362 typedef typename iterator_traits
<_InputIterator
>::value_type
2364 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2366 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2369 // concept requirements
2370 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2371 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2373 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
2374 __glibcxx_function_requires(_LessThanComparableConcept
<_InputValueType
>)
2375 __glibcxx_requires_valid_range(__first
, __last
);
2376 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2378 if (__result_first
== __result_last
)
2379 return __result_last
;
2380 _RandomAccessIterator __result_real_last
= __result_first
;
2381 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2383 *__result_real_last
= *__first
;
2384 ++__result_real_last
;
2387 std::make_heap(__result_first
, __result_real_last
);
2388 while (__first
!= __last
)
2390 if (*__first
< *__result_first
)
2391 std::__adjust_heap(__result_first
, _DistanceType(0),
2392 _DistanceType(__result_real_last
2394 _InputValueType(*__first
));
2397 std::sort_heap(__result_first
, __result_real_last
);
2398 return __result_real_last
;
2402 * @brief Copy the smallest elements of a sequence using a predicate for
2404 * @param first An input iterator.
2405 * @param last Another input iterator.
2406 * @param result_first A random-access iterator.
2407 * @param result_last Another random-access iterator.
2408 * @param comp A comparison functor.
2409 * @return An iterator indicating the end of the resulting sequence.
2411 * Copies and sorts the smallest N values from the range @p [first,last)
2412 * to the range beginning at @p result_first, where the number of
2413 * elements to be copied, @p N, is the smaller of @p (last-first) and
2414 * @p (result_last-result_first).
2415 * After the sort if @p i and @j are iterators in the range
2416 * @p [result_first,result_first+N) such that @i precedes @j then
2417 * @p comp(*j,*i) is false.
2418 * The value returned is @p result_first+N.
2420 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
2421 _RandomAccessIterator
2422 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2423 _RandomAccessIterator __result_first
,
2424 _RandomAccessIterator __result_last
,
2427 typedef typename iterator_traits
<_InputIterator
>::value_type
2429 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2431 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2434 // concept requirements
2435 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2436 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2437 _RandomAccessIterator
>)
2438 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2440 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2441 _OutputValueType
, _OutputValueType
>)
2442 __glibcxx_requires_valid_range(__first
, __last
);
2443 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2445 if (__result_first
== __result_last
)
2446 return __result_last
;
2447 _RandomAccessIterator __result_real_last
= __result_first
;
2448 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2450 *__result_real_last
= *__first
;
2451 ++__result_real_last
;
2454 std::make_heap(__result_first
, __result_real_last
, __comp
);
2455 while (__first
!= __last
)
2457 if (__comp(*__first
, *__result_first
))
2458 std::__adjust_heap(__result_first
, _DistanceType(0),
2459 _DistanceType(__result_real_last
2461 _InputValueType(*__first
),
2465 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2466 return __result_real_last
;
2471 * This is a helper function for the sort routine.
2474 template<typename _RandomAccessIterator
, typename _Size
>
2476 __introsort_loop(_RandomAccessIterator __first
,
2477 _RandomAccessIterator __last
,
2478 _Size __depth_limit
)
2480 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2483 while (__last
- __first
> _S_threshold
)
2485 if (__depth_limit
== 0)
2487 std::partial_sort(__first
, __last
, __last
);
2491 _RandomAccessIterator __cut
=
2492 std::__unguarded_partition(__first
, __last
,
2493 _ValueType(std::__median(*__first
,
2500 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2507 * This is a helper function for the sort routine.
2510 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2512 __introsort_loop(_RandomAccessIterator __first
,
2513 _RandomAccessIterator __last
,
2514 _Size __depth_limit
, _Compare __comp
)
2516 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2519 while (__last
- __first
> _S_threshold
)
2521 if (__depth_limit
== 0)
2523 std::partial_sort(__first
, __last
, __last
, __comp
);
2527 _RandomAccessIterator __cut
=
2528 std::__unguarded_partition(__first
, __last
,
2529 _ValueType(std::__median(*__first
,
2537 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2543 * @brief Sort the elements of a sequence.
2544 * @param first An iterator.
2545 * @param last Another iterator.
2548 * Sorts the elements in the range @p [first,last) in ascending order,
2549 * such that @p *(i+1)<*i is false for each iterator @p i in the range
2550 * @p [first,last-1).
2552 * The relative ordering of equivalent elements is not preserved, use
2553 * @p stable_sort() if this is needed.
2555 template<typename _RandomAccessIterator
>
2557 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
2559 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2562 // concept requirements
2563 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2564 _RandomAccessIterator
>)
2565 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2566 __glibcxx_requires_valid_range(__first
, __last
);
2568 if (__first
!= __last
)
2570 std::__introsort_loop(__first
, __last
, __lg(__last
- __first
) * 2);
2571 std::__final_insertion_sort(__first
, __last
);
2576 * @brief Sort the elements of a sequence using a predicate for comparison.
2577 * @param first An iterator.
2578 * @param last Another iterator.
2579 * @param comp A comparison functor.
2582 * Sorts the elements in the range @p [first,last) in ascending order,
2583 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
2584 * range @p [first,last-1).
2586 * The relative ordering of equivalent elements is not preserved, use
2587 * @p stable_sort() if this is needed.
2589 template<typename _RandomAccessIterator
, typename _Compare
>
2591 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2594 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2597 // concept requirements
2598 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2599 _RandomAccessIterator
>)
2600 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
2602 __glibcxx_requires_valid_range(__first
, __last
);
2604 if (__first
!= __last
)
2606 std::__introsort_loop(__first
, __last
, __lg(__last
- __first
) * 2,
2608 std::__final_insertion_sort(__first
, __last
, __comp
);
2613 * @brief Finds the first position in which @a val could be inserted
2614 * without changing the ordering.
2615 * @param first An iterator.
2616 * @param last Another iterator.
2617 * @param val The search term.
2618 * @return An iterator pointing to the first element "not less than" @a val,
2619 * or end() if every element is less than @a val.
2620 * @ingroup binarysearch
2622 template<typename _ForwardIterator
, typename _Tp
>
2624 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2627 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2629 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2632 // concept requirements
2633 // Note that these are slightly stricter than those of the 4-argument
2634 // version, defined next. The difference is in the strictness of the
2635 // comparison operations... so for looser checking, define your own
2636 // comparison function, as was intended.
2637 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2638 __glibcxx_function_requires(_SameTypeConcept
<_Tp
, _ValueType
>)
2639 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
2640 __glibcxx_requires_partitioned(__first
, __last
, __val
);
2642 _DistanceType __len
= std::distance(__first
, __last
);
2643 _DistanceType __half
;
2644 _ForwardIterator __middle
;
2648 __half
= __len
>> 1;
2650 std::advance(__middle
, __half
);
2651 if (*__middle
< __val
)
2655 __len
= __len
- __half
- 1;
2664 * @brief Finds the first position in which @a val could be inserted
2665 * without changing the ordering.
2666 * @param first An iterator.
2667 * @param last Another iterator.
2668 * @param val The search term.
2669 * @param comp A functor to use for comparisons.
2670 * @return An iterator pointing to the first element "not less than" @a val,
2671 * or end() if every element is less than @a val.
2672 * @ingroup binarysearch
2674 * The comparison function should have the same effects on ordering as
2675 * the function used for the initial sort.
2677 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2679 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2680 const _Tp
& __val
, _Compare __comp
)
2682 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2684 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2687 // concept requirements
2688 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2689 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2691 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
2693 _DistanceType __len
= std::distance(__first
, __last
);
2694 _DistanceType __half
;
2695 _ForwardIterator __middle
;
2699 __half
= __len
>> 1;
2701 std::advance(__middle
, __half
);
2702 if (__comp(*__middle
, __val
))
2706 __len
= __len
- __half
- 1;
2715 * @brief Finds the last position in which @a val could be inserted
2716 * without changing the ordering.
2717 * @param first An iterator.
2718 * @param last Another iterator.
2719 * @param val The search term.
2720 * @return An iterator pointing to the first element greater than @a val,
2721 * or end() if no elements are greater than @a val.
2722 * @ingroup binarysearch
2724 template<typename _ForwardIterator
, typename _Tp
>
2726 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2729 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2731 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2734 // concept requirements
2735 // See comments on lower_bound.
2736 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2737 __glibcxx_function_requires(_SameTypeConcept
<_Tp
, _ValueType
>)
2738 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
2739 __glibcxx_requires_partitioned(__first
, __last
, __val
);
2741 _DistanceType __len
= std::distance(__first
, __last
);
2742 _DistanceType __half
;
2743 _ForwardIterator __middle
;
2747 __half
= __len
>> 1;
2749 std::advance(__middle
, __half
);
2750 if (__val
< *__middle
)
2756 __len
= __len
- __half
- 1;
2763 * @brief Finds the last position in which @a val could be inserted
2764 * without changing the ordering.
2765 * @param first An iterator.
2766 * @param last Another iterator.
2767 * @param val The search term.
2768 * @param comp A functor to use for comparisons.
2769 * @return An iterator pointing to the first element greater than @a val,
2770 * or end() if no elements are greater than @a val.
2771 * @ingroup binarysearch
2773 * The comparison function should have the same effects on ordering as
2774 * the function used for the initial sort.
2776 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2778 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2779 const _Tp
& __val
, _Compare __comp
)
2781 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2783 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2786 // concept requirements
2787 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2788 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2790 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
2792 _DistanceType __len
= std::distance(__first
, __last
);
2793 _DistanceType __half
;
2794 _ForwardIterator __middle
;
2798 __half
= __len
>> 1;
2800 std::advance(__middle
, __half
);
2801 if (__comp(__val
, *__middle
))
2807 __len
= __len
- __half
- 1;
2815 * This is a helper function for the merge routines.
2818 template<typename _BidirectionalIterator
, typename _Distance
>
2820 __merge_without_buffer(_BidirectionalIterator __first
,
2821 _BidirectionalIterator __middle
,
2822 _BidirectionalIterator __last
,
2823 _Distance __len1
, _Distance __len2
)
2825 if (__len1
== 0 || __len2
== 0)
2827 if (__len1
+ __len2
== 2)
2829 if (*__middle
< *__first
)
2830 std::iter_swap(__first
, __middle
);
2833 _BidirectionalIterator __first_cut
= __first
;
2834 _BidirectionalIterator __second_cut
= __middle
;
2835 _Distance __len11
= 0;
2836 _Distance __len22
= 0;
2837 if (__len1
> __len2
)
2839 __len11
= __len1
/ 2;
2840 std::advance(__first_cut
, __len11
);
2841 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
2842 __len22
= std::distance(__middle
, __second_cut
);
2846 __len22
= __len2
/ 2;
2847 std::advance(__second_cut
, __len22
);
2848 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
2849 __len11
= std::distance(__first
, __first_cut
);
2851 std::rotate(__first_cut
, __middle
, __second_cut
);
2852 _BidirectionalIterator __new_middle
= __first_cut
;
2853 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2854 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2856 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2857 __len1
- __len11
, __len2
- __len22
);
2862 * This is a helper function for the merge routines.
2865 template<typename _BidirectionalIterator
, typename _Distance
,
2868 __merge_without_buffer(_BidirectionalIterator __first
,
2869 _BidirectionalIterator __middle
,
2870 _BidirectionalIterator __last
,
2871 _Distance __len1
, _Distance __len2
,
2874 if (__len1
== 0 || __len2
== 0)
2876 if (__len1
+ __len2
== 2)
2878 if (__comp(*__middle
, *__first
))
2879 std::iter_swap(__first
, __middle
);
2882 _BidirectionalIterator __first_cut
= __first
;
2883 _BidirectionalIterator __second_cut
= __middle
;
2884 _Distance __len11
= 0;
2885 _Distance __len22
= 0;
2886 if (__len1
> __len2
)
2888 __len11
= __len1
/ 2;
2889 std::advance(__first_cut
, __len11
);
2890 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2892 __len22
= std::distance(__middle
, __second_cut
);
2896 __len22
= __len2
/ 2;
2897 std::advance(__second_cut
, __len22
);
2898 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2900 __len11
= std::distance(__first
, __first_cut
);
2902 std::rotate(__first_cut
, __middle
, __second_cut
);
2903 _BidirectionalIterator __new_middle
= __first_cut
;
2904 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2905 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2906 __len11
, __len22
, __comp
);
2907 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2908 __len1
- __len11
, __len2
- __len22
, __comp
);
2913 * This is a helper function for the stable sorting routines.
2916 template<typename _RandomAccessIterator
>
2918 __inplace_stable_sort(_RandomAccessIterator __first
,
2919 _RandomAccessIterator __last
)
2921 if (__last
- __first
< 15)
2923 std::__insertion_sort(__first
, __last
);
2926 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2927 std::__inplace_stable_sort(__first
, __middle
);
2928 std::__inplace_stable_sort(__middle
, __last
);
2929 std::__merge_without_buffer(__first
, __middle
, __last
,
2936 * This is a helper function for the stable sorting routines.
2939 template<typename _RandomAccessIterator
, typename _Compare
>
2941 __inplace_stable_sort(_RandomAccessIterator __first
,
2942 _RandomAccessIterator __last
, _Compare __comp
)
2944 if (__last
- __first
< 15)
2946 std::__insertion_sort(__first
, __last
, __comp
);
2949 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2950 std::__inplace_stable_sort(__first
, __middle
, __comp
);
2951 std::__inplace_stable_sort(__middle
, __last
, __comp
);
2952 std::__merge_without_buffer(__first
, __middle
, __last
,
2959 * @brief Merges two sorted ranges.
2960 * @param first1 An iterator.
2961 * @param first2 Another iterator.
2962 * @param last1 Another iterator.
2963 * @param last2 Another iterator.
2964 * @param result An iterator pointing to the end of the merged range.
2965 * @return An iterator pointing to the first element "not less than" @a val.
2967 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
2968 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
2969 * must be sorted, and the output range must not overlap with either of
2970 * the input ranges. The sort is @e stable, that is, for equivalent
2971 * elements in the two ranges, elements from the first range will always
2972 * come before elements from the second.
2974 template<typename _InputIterator1
, typename _InputIterator2
,
2975 typename _OutputIterator
>
2977 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
2978 _InputIterator2 __first2
, _InputIterator2 __last2
,
2979 _OutputIterator __result
)
2981 // concept requirements
2982 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2983 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2984 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
2985 typename iterator_traits
<_InputIterator1
>::value_type
>)
2986 __glibcxx_function_requires(_SameTypeConcept
<
2987 typename iterator_traits
<_InputIterator1
>::value_type
,
2988 typename iterator_traits
<_InputIterator2
>::value_type
>)
2989 __glibcxx_function_requires(_LessThanComparableConcept
<
2990 typename iterator_traits
<_InputIterator1
>::value_type
>)
2991 __glibcxx_requires_sorted(__first1
, __last1
);
2992 __glibcxx_requires_sorted(__first2
, __last2
);
2994 while (__first1
!= __last1
&& __first2
!= __last2
)
2996 if (*__first2
< *__first1
)
2998 *__result
= *__first2
;
3003 *__result
= *__first1
;
3008 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
3013 * @brief Merges two sorted ranges.
3014 * @param first1 An iterator.
3015 * @param first2 Another iterator.
3016 * @param last1 Another iterator.
3017 * @param last2 Another iterator.
3018 * @param result An iterator pointing to the end of the merged range.
3019 * @param comp A functor to use for comparisons.
3020 * @return An iterator pointing to the first element "not less than" @a val.
3022 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
3023 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
3024 * must be sorted, and the output range must not overlap with either of
3025 * the input ranges. The sort is @e stable, that is, for equivalent
3026 * elements in the two ranges, elements from the first range will always
3027 * come before elements from the second.
3029 * The comparison function should have the same effects on ordering as
3030 * the function used for the initial sort.
3032 template<typename _InputIterator1
, typename _InputIterator2
,
3033 typename _OutputIterator
, typename _Compare
>
3035 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3036 _InputIterator2 __first2
, _InputIterator2 __last2
,
3037 _OutputIterator __result
, _Compare __comp
)
3039 // concept requirements
3040 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3041 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3042 __glibcxx_function_requires(_SameTypeConcept
<
3043 typename iterator_traits
<_InputIterator1
>::value_type
,
3044 typename iterator_traits
<_InputIterator2
>::value_type
>)
3045 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3046 typename iterator_traits
<_InputIterator1
>::value_type
>)
3047 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3048 typename iterator_traits
<_InputIterator1
>::value_type
,
3049 typename iterator_traits
<_InputIterator2
>::value_type
>)
3050 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
3051 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
3053 while (__first1
!= __last1
&& __first2
!= __last2
)
3055 if (__comp(*__first2
, *__first1
))
3057 *__result
= *__first2
;
3062 *__result
= *__first1
;
3067 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
3071 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3074 __merge_sort_loop(_RandomAccessIterator1 __first
,
3075 _RandomAccessIterator1 __last
,
3076 _RandomAccessIterator2 __result
,
3077 _Distance __step_size
)
3079 const _Distance __two_step
= 2 * __step_size
;
3081 while (__last
- __first
>= __two_step
)
3083 __result
= std::merge(__first
, __first
+ __step_size
,
3084 __first
+ __step_size
, __first
+ __two_step
,
3086 __first
+= __two_step
;
3089 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3090 std::merge(__first
, __first
+ __step_size
, __first
+ __step_size
, __last
,
3094 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3095 typename _Distance
, typename _Compare
>
3097 __merge_sort_loop(_RandomAccessIterator1 __first
,
3098 _RandomAccessIterator1 __last
,
3099 _RandomAccessIterator2 __result
, _Distance __step_size
,
3102 const _Distance __two_step
= 2 * __step_size
;
3104 while (__last
- __first
>= __two_step
)
3106 __result
= std::merge(__first
, __first
+ __step_size
,
3107 __first
+ __step_size
, __first
+ __two_step
,
3110 __first
+= __two_step
;
3112 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3114 std::merge(__first
, __first
+ __step_size
,
3115 __first
+ __step_size
, __last
,
3120 enum { _S_chunk_size
= 7 };
3122 template<typename _RandomAccessIterator
, typename _Distance
>
3124 __chunk_insertion_sort(_RandomAccessIterator __first
,
3125 _RandomAccessIterator __last
,
3126 _Distance __chunk_size
)
3128 while (__last
- __first
>= __chunk_size
)
3130 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3131 __first
+= __chunk_size
;
3133 std::__insertion_sort(__first
, __last
);
3136 template<typename _RandomAccessIterator
, typename _Distance
, typename _Compare
>
3138 __chunk_insertion_sort(_RandomAccessIterator __first
,
3139 _RandomAccessIterator __last
,
3140 _Distance __chunk_size
, _Compare __comp
)
3142 while (__last
- __first
>= __chunk_size
)
3144 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3145 __first
+= __chunk_size
;
3147 std::__insertion_sort(__first
, __last
, __comp
);
3150 template<typename _RandomAccessIterator
, typename _Pointer
>
3152 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3153 _RandomAccessIterator __last
,
3156 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3159 const _Distance __len
= __last
- __first
;
3160 const _Pointer __buffer_last
= __buffer
+ __len
;
3162 _Distance __step_size
= _S_chunk_size
;
3163 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3165 while (__step_size
< __len
)
3167 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3169 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3174 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3176 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3177 _RandomAccessIterator __last
,
3178 _Pointer __buffer
, _Compare __comp
)
3180 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3183 const _Distance __len
= __last
- __first
;
3184 const _Pointer __buffer_last
= __buffer
+ __len
;
3186 _Distance __step_size
= _S_chunk_size
;
3187 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3189 while (__step_size
< __len
)
3191 std::__merge_sort_loop(__first
, __last
, __buffer
,
3192 __step_size
, __comp
);
3194 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3195 __step_size
, __comp
);
3202 * This is a helper function for the merge routines.
3205 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3206 typename _BidirectionalIterator3
>
3207 _BidirectionalIterator3
3208 __merge_backward(_BidirectionalIterator1 __first1
,
3209 _BidirectionalIterator1 __last1
,
3210 _BidirectionalIterator2 __first2
,
3211 _BidirectionalIterator2 __last2
,
3212 _BidirectionalIterator3 __result
)
3214 if (__first1
== __last1
)
3215 return std::copy_backward(__first2
, __last2
, __result
);
3216 if (__first2
== __last2
)
3217 return std::copy_backward(__first1
, __last1
, __result
);
3222 if (*__last2
< *__last1
)
3224 *--__result
= *__last1
;
3225 if (__first1
== __last1
)
3226 return std::copy_backward(__first2
, ++__last2
, __result
);
3231 *--__result
= *__last2
;
3232 if (__first2
== __last2
)
3233 return std::copy_backward(__first1
, ++__last1
, __result
);
3241 * This is a helper function for the merge routines.
3244 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3245 typename _BidirectionalIterator3
, typename _Compare
>
3246 _BidirectionalIterator3
3247 __merge_backward(_BidirectionalIterator1 __first1
,
3248 _BidirectionalIterator1 __last1
,
3249 _BidirectionalIterator2 __first2
,
3250 _BidirectionalIterator2 __last2
,
3251 _BidirectionalIterator3 __result
,
3254 if (__first1
== __last1
)
3255 return std::copy_backward(__first2
, __last2
, __result
);
3256 if (__first2
== __last2
)
3257 return std::copy_backward(__first1
, __last1
, __result
);
3262 if (__comp(*__last2
, *__last1
))
3264 *--__result
= *__last1
;
3265 if (__first1
== __last1
)
3266 return std::copy_backward(__first2
, ++__last2
, __result
);
3271 *--__result
= *__last2
;
3272 if (__first2
== __last2
)
3273 return std::copy_backward(__first1
, ++__last1
, __result
);
3281 * This is a helper function for the merge routines.
3284 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3286 _BidirectionalIterator1
3287 __rotate_adaptive(_BidirectionalIterator1 __first
,
3288 _BidirectionalIterator1 __middle
,
3289 _BidirectionalIterator1 __last
,
3290 _Distance __len1
, _Distance __len2
,
3291 _BidirectionalIterator2 __buffer
,
3292 _Distance __buffer_size
)
3294 _BidirectionalIterator2 __buffer_end
;
3295 if (__len1
> __len2
&& __len2
<= __buffer_size
)
3297 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3298 std::copy_backward(__first
, __middle
, __last
);
3299 return std::copy(__buffer
, __buffer_end
, __first
);
3301 else if (__len1
<= __buffer_size
)
3303 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3304 std::copy(__middle
, __last
, __first
);
3305 return std::copy_backward(__buffer
, __buffer_end
, __last
);
3309 std::rotate(__first
, __middle
, __last
);
3310 std::advance(__first
, std::distance(__middle
, __last
));
3317 * This is a helper function for the merge routines.
3320 template<typename _BidirectionalIterator
, typename _Distance
,
3323 __merge_adaptive(_BidirectionalIterator __first
,
3324 _BidirectionalIterator __middle
,
3325 _BidirectionalIterator __last
,
3326 _Distance __len1
, _Distance __len2
,
3327 _Pointer __buffer
, _Distance __buffer_size
)
3329 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
3331 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3332 std::merge(__buffer
, __buffer_end
, __middle
, __last
, __first
);
3334 else if (__len2
<= __buffer_size
)
3336 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3337 std::__merge_backward(__first
, __middle
, __buffer
,
3338 __buffer_end
, __last
);
3342 _BidirectionalIterator __first_cut
= __first
;
3343 _BidirectionalIterator __second_cut
= __middle
;
3344 _Distance __len11
= 0;
3345 _Distance __len22
= 0;
3346 if (__len1
> __len2
)
3348 __len11
= __len1
/ 2;
3349 std::advance(__first_cut
, __len11
);
3350 __second_cut
= std::lower_bound(__middle
, __last
,
3352 __len22
= std::distance(__middle
, __second_cut
);
3356 __len22
= __len2
/ 2;
3357 std::advance(__second_cut
, __len22
);
3358 __first_cut
= std::upper_bound(__first
, __middle
,
3360 __len11
= std::distance(__first
, __first_cut
);
3362 _BidirectionalIterator __new_middle
=
3363 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3364 __len1
- __len11
, __len22
, __buffer
,
3366 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3367 __len22
, __buffer
, __buffer_size
);
3368 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3370 __len2
- __len22
, __buffer
, __buffer_size
);
3376 * This is a helper function for the merge routines.
3379 template<typename _BidirectionalIterator
, typename _Distance
, typename _Pointer
,
3382 __merge_adaptive(_BidirectionalIterator __first
,
3383 _BidirectionalIterator __middle
,
3384 _BidirectionalIterator __last
,
3385 _Distance __len1
, _Distance __len2
,
3386 _Pointer __buffer
, _Distance __buffer_size
,
3389 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
3391 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3392 std::merge(__buffer
, __buffer_end
, __middle
, __last
, __first
, __comp
);
3394 else if (__len2
<= __buffer_size
)
3396 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3397 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
3402 _BidirectionalIterator __first_cut
= __first
;
3403 _BidirectionalIterator __second_cut
= __middle
;
3404 _Distance __len11
= 0;
3405 _Distance __len22
= 0;
3406 if (__len1
> __len2
)
3408 __len11
= __len1
/ 2;
3409 std::advance(__first_cut
, __len11
);
3410 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3412 __len22
= std::distance(__middle
, __second_cut
);
3416 __len22
= __len2
/ 2;
3417 std::advance(__second_cut
, __len22
);
3418 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3420 __len11
= std::distance(__first
, __first_cut
);
3422 _BidirectionalIterator __new_middle
=
3423 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3424 __len1
- __len11
, __len22
, __buffer
,
3426 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3427 __len22
, __buffer
, __buffer_size
, __comp
);
3428 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3430 __len2
- __len22
, __buffer
,
3431 __buffer_size
, __comp
);
3436 * @brief Merges two sorted ranges in place.
3437 * @param first An iterator.
3438 * @param middle Another iterator.
3439 * @param last Another iterator.
3442 * Merges two sorted and consecutive ranges, [first,middle) and
3443 * [middle,last), and puts the result in [first,last). The output will
3444 * be sorted. The sort is @e stable, that is, for equivalent
3445 * elements in the two ranges, elements from the first range will always
3446 * come before elements from the second.
3448 * If enough additional memory is available, this takes (last-first)-1
3449 * comparisons. Otherwise an NlogN algorithm is used, where N is
3450 * distance(first,last).
3452 template<typename _BidirectionalIterator
>
3454 inplace_merge(_BidirectionalIterator __first
,
3455 _BidirectionalIterator __middle
,
3456 _BidirectionalIterator __last
)
3458 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3460 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3463 // concept requirements
3464 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3465 _BidirectionalIterator
>)
3466 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3467 __glibcxx_requires_sorted(__first
, __middle
);
3468 __glibcxx_requires_sorted(__middle
, __last
);
3470 if (__first
== __middle
|| __middle
== __last
)
3473 _DistanceType __len1
= std::distance(__first
, __middle
);
3474 _DistanceType __len2
= std::distance(__middle
, __last
);
3476 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3478 if (__buf
.begin() == 0)
3479 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3481 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3482 __buf
.begin(), _DistanceType(__buf
.size()));
3486 * @brief Merges two sorted ranges in place.
3487 * @param first An iterator.
3488 * @param middle Another iterator.
3489 * @param last Another iterator.
3490 * @param comp A functor to use for comparisons.
3493 * Merges two sorted and consecutive ranges, [first,middle) and
3494 * [middle,last), and puts the result in [first,last). The output will
3495 * be sorted. The sort is @e stable, that is, for equivalent
3496 * elements in the two ranges, elements from the first range will always
3497 * come before elements from the second.
3499 * If enough additional memory is available, this takes (last-first)-1
3500 * comparisons. Otherwise an NlogN algorithm is used, where N is
3501 * distance(first,last).
3503 * The comparison function should have the same effects on ordering as
3504 * the function used for the initial sort.
3506 template<typename _BidirectionalIterator
, typename _Compare
>
3508 inplace_merge(_BidirectionalIterator __first
,
3509 _BidirectionalIterator __middle
,
3510 _BidirectionalIterator __last
,
3513 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3515 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3518 // concept requirements
3519 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3520 _BidirectionalIterator
>)
3521 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3522 _ValueType
, _ValueType
>)
3523 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3524 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3526 if (__first
== __middle
|| __middle
== __last
)
3529 const _DistanceType __len1
= std::distance(__first
, __middle
);
3530 const _DistanceType __len2
= std::distance(__middle
, __last
);
3532 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3534 if (__buf
.begin() == 0)
3535 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3538 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3539 __buf
.begin(), _DistanceType(__buf
.size()),
3543 template<typename _RandomAccessIterator
, typename _Pointer
,
3546 __stable_sort_adaptive(_RandomAccessIterator __first
,
3547 _RandomAccessIterator __last
,
3548 _Pointer __buffer
, _Distance __buffer_size
)
3550 const _Distance __len
= (__last
- __first
+ 1) / 2;
3551 const _RandomAccessIterator __middle
= __first
+ __len
;
3552 if (__len
> __buffer_size
)
3554 std::__stable_sort_adaptive(__first
, __middle
,
3555 __buffer
, __buffer_size
);
3556 std::__stable_sort_adaptive(__middle
, __last
,
3557 __buffer
, __buffer_size
);
3561 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3562 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3564 std::__merge_adaptive(__first
, __middle
, __last
,
3565 _Distance(__middle
- __first
),
3566 _Distance(__last
- __middle
),
3567 __buffer
, __buffer_size
);
3570 template<typename _RandomAccessIterator
, typename _Pointer
,
3571 typename _Distance
, typename _Compare
>
3573 __stable_sort_adaptive(_RandomAccessIterator __first
,
3574 _RandomAccessIterator __last
,
3575 _Pointer __buffer
, _Distance __buffer_size
,
3578 const _Distance __len
= (__last
- __first
+ 1) / 2;
3579 const _RandomAccessIterator __middle
= __first
+ __len
;
3580 if (__len
> __buffer_size
)
3582 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3583 __buffer_size
, __comp
);
3584 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3585 __buffer_size
, __comp
);
3589 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3590 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3592 std::__merge_adaptive(__first
, __middle
, __last
,
3593 _Distance(__middle
- __first
),
3594 _Distance(__last
- __middle
),
3595 __buffer
, __buffer_size
,
3600 * @brief Sort the elements of a sequence, preserving the relative order
3601 * of equivalent elements.
3602 * @param first An iterator.
3603 * @param last Another iterator.
3606 * Sorts the elements in the range @p [first,last) in ascending order,
3607 * such that @p *(i+1)<*i is false for each iterator @p i in the range
3608 * @p [first,last-1).
3610 * The relative ordering of equivalent elements is preserved, so any two
3611 * elements @p x and @p y in the range @p [first,last) such that
3612 * @p x<y is false and @p y<x is false will have the same relative
3613 * ordering after calling @p stable_sort().
3615 template<typename _RandomAccessIterator
>
3617 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
3619 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3621 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3624 // concept requirements
3625 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3626 _RandomAccessIterator
>)
3627 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3628 __glibcxx_requires_valid_range(__first
, __last
);
3630 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
>
3631 buf(__first
, __last
);
3632 if (buf
.begin() == 0)
3633 std::__inplace_stable_sort(__first
, __last
);
3635 std::__stable_sort_adaptive(__first
, __last
, buf
.begin(),
3636 _DistanceType(buf
.size()));
3640 * @brief Sort the elements of a sequence using a predicate for comparison,
3641 * preserving the relative order of equivalent elements.
3642 * @param first An iterator.
3643 * @param last Another iterator.
3644 * @param comp A comparison functor.
3647 * Sorts the elements in the range @p [first,last) in ascending order,
3648 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
3649 * range @p [first,last-1).
3651 * The relative ordering of equivalent elements is preserved, so any two
3652 * elements @p x and @p y in the range @p [first,last) such that
3653 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
3654 * relative ordering after calling @p stable_sort().
3656 template<typename _RandomAccessIterator
, typename _Compare
>
3658 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3661 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3663 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3666 // concept requirements
3667 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3668 _RandomAccessIterator
>)
3669 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3672 __glibcxx_requires_valid_range(__first
, __last
);
3674 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> buf(__first
, __last
);
3675 if (buf
.begin() == 0)
3676 std::__inplace_stable_sort(__first
, __last
, __comp
);
3678 std::__stable_sort_adaptive(__first
, __last
, buf
.begin(),
3679 _DistanceType(buf
.size()), __comp
);
3683 * @brief Sort a sequence just enough to find a particular position.
3684 * @param first An iterator.
3685 * @param nth Another iterator.
3686 * @param last Another iterator.
3689 * Rearranges the elements in the range @p [first,last) so that @p *nth
3690 * is the same element that would have been in that position had the
3691 * whole sequence been sorted.
3692 * whole sequence been sorted. The elements either side of @p *nth are
3693 * not completely sorted, but for any iterator @i in the range
3694 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3695 * holds that @p *j<*i is false.
3697 template<typename _RandomAccessIterator
>
3699 nth_element(_RandomAccessIterator __first
,
3700 _RandomAccessIterator __nth
,
3701 _RandomAccessIterator __last
)
3703 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3706 // concept requirements
3707 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3708 _RandomAccessIterator
>)
3709 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3710 __glibcxx_requires_valid_range(__first
, __nth
);
3711 __glibcxx_requires_valid_range(__nth
, __last
);
3713 while (__last
- __first
> 3)
3715 _RandomAccessIterator __cut
=
3716 std::__unguarded_partition(__first
, __last
,
3717 _ValueType(std::__median(*__first
,
3729 std::__insertion_sort(__first
, __last
);
3733 * @brief Sort a sequence just enough to find a particular position
3734 * using a predicate for comparison.
3735 * @param first An iterator.
3736 * @param nth Another iterator.
3737 * @param last Another iterator.
3738 * @param comp A comparison functor.
3741 * Rearranges the elements in the range @p [first,last) so that @p *nth
3742 * is the same element that would have been in that position had the
3743 * whole sequence been sorted. The elements either side of @p *nth are
3744 * not completely sorted, but for any iterator @i in the range
3745 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3746 * holds that @p comp(*j,*i) is false.
3748 template<typename _RandomAccessIterator
, typename _Compare
>
3750 nth_element(_RandomAccessIterator __first
,
3751 _RandomAccessIterator __nth
,
3752 _RandomAccessIterator __last
,
3755 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3758 // concept requirements
3759 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3760 _RandomAccessIterator
>)
3761 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3762 _ValueType
, _ValueType
>)
3763 __glibcxx_requires_valid_range(__first
, __nth
);
3764 __glibcxx_requires_valid_range(__nth
, __last
);
3766 while (__last
- __first
> 3)
3768 _RandomAccessIterator __cut
=
3769 std::__unguarded_partition(__first
, __last
,
3770 _ValueType(std::__median(*__first
,
3782 std::__insertion_sort(__first
, __last
, __comp
);
3786 * @brief Finds the largest subrange in which @a val could be inserted
3787 * at any place in it without changing the ordering.
3788 * @param first An iterator.
3789 * @param last Another iterator.
3790 * @param val The search term.
3791 * @return An pair of iterators defining the subrange.
3792 * @ingroup binarysearch
3794 * This is equivalent to
3796 * std::make_pair(lower_bound(first, last, val),
3797 * upper_bound(first, last, val))
3799 * but does not actually call those functions.
3801 template<typename _ForwardIterator
, typename _Tp
>
3802 pair
<_ForwardIterator
, _ForwardIterator
>
3803 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
3806 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3808 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3811 // concept requirements
3812 // See comments on lower_bound.
3813 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3814 __glibcxx_function_requires(_SameTypeConcept
<_Tp
, _ValueType
>)
3815 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3816 __glibcxx_requires_partitioned(__first
, __last
, __val
);
3818 _DistanceType __len
= std::distance(__first
, __last
);
3819 _DistanceType __half
;
3820 _ForwardIterator __middle
, __left
, __right
;
3824 __half
= __len
>> 1;
3826 std::advance(__middle
, __half
);
3827 if (*__middle
< __val
)
3831 __len
= __len
- __half
- 1;
3833 else if (__val
< *__middle
)
3837 __left
= std::lower_bound(__first
, __middle
, __val
);
3838 std::advance(__first
, __len
);
3839 __right
= std::upper_bound(++__middle
, __first
, __val
);
3840 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
3843 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
3847 * @brief Finds the largest subrange in which @a val could be inserted
3848 * at any place in it without changing the ordering.
3849 * @param first An iterator.
3850 * @param last Another iterator.
3851 * @param val The search term.
3852 * @param comp A functor to use for comparisons.
3853 * @return An pair of iterators defining the subrange.
3854 * @ingroup binarysearch
3856 * This is equivalent to
3858 * std::make_pair(lower_bound(first, last, val, comp),
3859 * upper_bound(first, last, val, comp))
3861 * but does not actually call those functions.
3863 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3864 pair
<_ForwardIterator
, _ForwardIterator
>
3865 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
3869 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3871 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3874 // concept requirements
3875 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3876 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3878 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3880 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
3882 _DistanceType __len
= std::distance(__first
, __last
);
3883 _DistanceType __half
;
3884 _ForwardIterator __middle
, __left
, __right
;
3888 __half
= __len
>> 1;
3890 std::advance(__middle
, __half
);
3891 if (__comp(*__middle
, __val
))
3895 __len
= __len
- __half
- 1;
3897 else if (__comp(__val
, *__middle
))
3901 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
3902 std::advance(__first
, __len
);
3903 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
3904 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
3907 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
3911 * @brief Determines whether an element exists in a range.
3912 * @param first An iterator.
3913 * @param last Another iterator.
3914 * @param val The search term.
3915 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
3916 * @ingroup binarysearch
3918 * Note that this does not actually return an iterator to @a val. For
3919 * that, use std::find or a container's specialized find member functions.
3921 template<typename _ForwardIterator
, typename _Tp
>
3923 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
3926 // concept requirements
3927 // See comments on lower_bound.
3928 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3929 __glibcxx_function_requires(_SameTypeConcept
<_Tp
,
3930 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3931 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3932 __glibcxx_requires_partitioned(__first
, __last
, __val
);
3934 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
3935 return __i
!= __last
&& !(__val
< *__i
);
3939 * @brief Determines whether an element exists in a range.
3940 * @param first An iterator.
3941 * @param last Another iterator.
3942 * @param val The search term.
3943 * @param comp A functor to use for comparisons.
3944 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
3945 * @ingroup binarysearch
3947 * Note that this does not actually return an iterator to @a val. For
3948 * that, use std::find or a container's specialized find member functions.
3950 * The comparison function should have the same effects on ordering as
3951 * the function used for the initial sort.
3953 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3955 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
3956 const _Tp
& __val
, _Compare __comp
)
3958 // concept requirements
3959 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3960 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3961 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
3962 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _Tp
,
3963 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3964 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
3966 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
3967 return __i
!= __last
&& !__comp(__val
, *__i
);
3970 // Set algorithms: includes, set_union, set_intersection, set_difference,
3971 // set_symmetric_difference. All of these algorithms have the precondition
3972 // that their input ranges are sorted and the postcondition that their output
3973 // ranges are sorted.
3976 * @brief Determines whether all elements of a sequence exists in a range.
3977 * @param first1 Start of search range.
3978 * @param last1 End of search range.
3979 * @param first2 Start of sequence
3980 * @param last2 End of sequence.
3981 * @return True if each element in [first2,last2) is contained in order
3982 * within [first1,last1). False otherwise.
3983 * @ingroup setoperations
3985 * This operation expects both [first1,last1) and [first2,last2) to be
3986 * sorted. Searches for the presence of each element in [first2,last2)
3987 * within [first1,last1). The iterators over each range only move forward,
3988 * so this is a linear algorithm. If an element in [first2,last2) is not
3989 * found before the search iterator reaches @a last2, false is returned.
3991 template<typename _InputIterator1
, typename _InputIterator2
>
3993 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3994 _InputIterator2 __first2
, _InputIterator2 __last2
)
3996 // concept requirements
3997 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3998 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3999 __glibcxx_function_requires(_SameTypeConcept
<
4000 typename iterator_traits
<_InputIterator1
>::value_type
,
4001 typename iterator_traits
<_InputIterator2
>::value_type
>)
4002 __glibcxx_function_requires(_LessThanComparableConcept
<
4003 typename iterator_traits
<_InputIterator1
>::value_type
>)
4004 __glibcxx_requires_sorted(__first1
, __last1
);
4005 __glibcxx_requires_sorted(__first2
, __last2
);
4007 while (__first1
!= __last1
&& __first2
!= __last2
)
4008 if (*__first2
< *__first1
)
4010 else if(*__first1
< *__first2
)
4013 ++__first1
, ++__first2
;
4015 return __first2
== __last2
;
4019 * @brief Determines whether all elements of a sequence exists in a range
4021 * @param first1 Start of search range.
4022 * @param last1 End of search range.
4023 * @param first2 Start of sequence
4024 * @param last2 End of sequence.
4025 * @param comp Comparison function to use.
4026 * @return True if each element in [first2,last2) is contained in order
4027 * within [first1,last1) according to comp. False otherwise.
4028 * @ingroup setoperations
4030 * This operation expects both [first1,last1) and [first2,last2) to be
4031 * sorted. Searches for the presence of each element in [first2,last2)
4032 * within [first1,last1), using comp to decide. The iterators over each
4033 * range only move forward, so this is a linear algorithm. If an element
4034 * in [first2,last2) is not found before the search iterator reaches @a
4035 * last2, false is returned.
4037 template<typename _InputIterator1
, typename _InputIterator2
,
4040 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
4041 _InputIterator2 __first2
, _InputIterator2 __last2
, _Compare __comp
)
4043 // concept requirements
4044 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4045 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4046 __glibcxx_function_requires(_SameTypeConcept
<
4047 typename iterator_traits
<_InputIterator1
>::value_type
,
4048 typename iterator_traits
<_InputIterator2
>::value_type
>)
4049 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4050 typename iterator_traits
<_InputIterator1
>::value_type
,
4051 typename iterator_traits
<_InputIterator2
>::value_type
>)
4052 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4053 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4055 while (__first1
!= __last1
&& __first2
!= __last2
)
4056 if (__comp(*__first2
, *__first1
))
4058 else if(__comp(*__first1
, *__first2
))
4061 ++__first1
, ++__first2
;
4063 return __first2
== __last2
;
4067 * @brief Return the union of two sorted ranges.
4068 * @param first1 Start of first range.
4069 * @param last1 End of first range.
4070 * @param first2 Start of second range.
4071 * @param last2 End of second range.
4072 * @return End of the output range.
4073 * @ingroup setoperations
4075 * This operation iterates over both ranges, copying elements present in
4076 * each range in order to the output range. Iterators increment for each
4077 * range. When the current element of one range is less than the other,
4078 * that element is copied and the iterator advanced. If an element is
4079 * contained in both ranges, the element from the first range is copied and
4080 * both ranges advance. The output range may not overlap either input
4083 template<typename _InputIterator1
, typename _InputIterator2
,
4084 typename _OutputIterator
>
4086 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4087 _InputIterator2 __first2
, _InputIterator2 __last2
,
4088 _OutputIterator __result
)
4090 // concept requirements
4091 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4092 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4093 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4094 typename iterator_traits
<_InputIterator1
>::value_type
>)
4095 __glibcxx_function_requires(_SameTypeConcept
<
4096 typename iterator_traits
<_InputIterator1
>::value_type
,
4097 typename iterator_traits
<_InputIterator2
>::value_type
>)
4098 __glibcxx_function_requires(_LessThanComparableConcept
<
4099 typename iterator_traits
<_InputIterator1
>::value_type
>)
4100 __glibcxx_requires_sorted(__first1
, __last1
);
4101 __glibcxx_requires_sorted(__first2
, __last2
);
4103 while (__first1
!= __last1
&& __first2
!= __last2
)
4105 if (*__first1
< *__first2
)
4107 *__result
= *__first1
;
4110 else if (*__first2
< *__first1
)
4112 *__result
= *__first2
;
4117 *__result
= *__first1
;
4123 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4128 * @brief Return the union of two sorted ranges using a comparison functor.
4129 * @param first1 Start of first range.
4130 * @param last1 End of first range.
4131 * @param first2 Start of second range.
4132 * @param last2 End of second range.
4133 * @param comp The comparison functor.
4134 * @return End of the output range.
4135 * @ingroup setoperations
4137 * This operation iterates over both ranges, copying elements present in
4138 * each range in order to the output range. Iterators increment for each
4139 * range. When the current element of one range is less than the other
4140 * according to @a comp, that element is copied and the iterator advanced.
4141 * If an equivalent element according to @a comp is contained in both
4142 * ranges, the element from the first range is copied and both ranges
4143 * advance. The output range may not overlap either input range.
4145 template<typename _InputIterator1
, typename _InputIterator2
,
4146 typename _OutputIterator
, typename _Compare
>
4148 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4149 _InputIterator2 __first2
, _InputIterator2 __last2
,
4150 _OutputIterator __result
, _Compare __comp
)
4152 // concept requirements
4153 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4154 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4155 __glibcxx_function_requires(_SameTypeConcept
<
4156 typename iterator_traits
<_InputIterator1
>::value_type
,
4157 typename iterator_traits
<_InputIterator2
>::value_type
>)
4158 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4159 typename iterator_traits
<_InputIterator1
>::value_type
>)
4160 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4161 typename iterator_traits
<_InputIterator1
>::value_type
,
4162 typename iterator_traits
<_InputIterator2
>::value_type
>)
4163 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4164 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4166 while (__first1
!= __last1
&& __first2
!= __last2
)
4168 if (__comp(*__first1
, *__first2
))
4170 *__result
= *__first1
;
4173 else if (__comp(*__first2
, *__first1
))
4175 *__result
= *__first2
;
4180 *__result
= *__first1
;
4186 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4191 * @brief Return the intersection of two sorted ranges.
4192 * @param first1 Start of first range.
4193 * @param last1 End of first range.
4194 * @param first2 Start of second range.
4195 * @param last2 End of second range.
4196 * @return End of the output range.
4197 * @ingroup setoperations
4199 * This operation iterates over both ranges, copying elements present in
4200 * both ranges in order to the output range. Iterators increment for each
4201 * range. When the current element of one range is less than the other,
4202 * that iterator advances. If an element is contained in both ranges, the
4203 * element from the first range is copied and both ranges advance. The
4204 * output range may not overlap either input range.
4206 template<typename _InputIterator1
, typename _InputIterator2
,
4207 typename _OutputIterator
>
4209 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
4210 _InputIterator2 __first2
, _InputIterator2 __last2
,
4211 _OutputIterator __result
)
4213 // concept requirements
4214 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4215 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4216 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4217 typename iterator_traits
<_InputIterator1
>::value_type
>)
4218 __glibcxx_function_requires(_SameTypeConcept
<
4219 typename iterator_traits
<_InputIterator1
>::value_type
,
4220 typename iterator_traits
<_InputIterator2
>::value_type
>)
4221 __glibcxx_function_requires(_LessThanComparableConcept
<
4222 typename iterator_traits
<_InputIterator1
>::value_type
>)
4223 __glibcxx_requires_sorted(__first1
, __last1
);
4224 __glibcxx_requires_sorted(__first2
, __last2
);
4226 while (__first1
!= __last1
&& __first2
!= __last2
)
4227 if (*__first1
< *__first2
)
4229 else if (*__first2
< *__first1
)
4233 *__result
= *__first1
;
4242 * @brief Return the intersection of two sorted ranges using comparison
4244 * @param first1 Start of first range.
4245 * @param last1 End of first range.
4246 * @param first2 Start of second range.
4247 * @param last2 End of second range.
4248 * @param comp The comparison functor.
4249 * @return End of the output range.
4250 * @ingroup setoperations
4252 * This operation iterates over both ranges, copying elements present in
4253 * both ranges in order to the output range. Iterators increment for each
4254 * range. When the current element of one range is less than the other
4255 * according to @a comp, that iterator advances. If an element is
4256 * contained in both ranges according to @a comp, the element from the
4257 * first range is copied and both ranges advance. The output range may not
4258 * overlap either input range.
4260 template<typename _InputIterator1
, typename _InputIterator2
,
4261 typename _OutputIterator
, typename _Compare
>
4263 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
4264 _InputIterator2 __first2
, _InputIterator2 __last2
,
4265 _OutputIterator __result
, _Compare __comp
)
4267 // concept requirements
4268 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4269 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4270 __glibcxx_function_requires(_SameTypeConcept
<
4271 typename iterator_traits
<_InputIterator1
>::value_type
,
4272 typename iterator_traits
<_InputIterator2
>::value_type
>)
4273 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4274 typename iterator_traits
<_InputIterator1
>::value_type
>)
4275 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4276 typename iterator_traits
<_InputIterator1
>::value_type
,
4277 typename iterator_traits
<_InputIterator2
>::value_type
>)
4278 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4279 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4281 while (__first1
!= __last1
&& __first2
!= __last2
)
4282 if (__comp(*__first1
, *__first2
))
4284 else if (__comp(*__first2
, *__first1
))
4288 *__result
= *__first1
;
4297 * @brief Return the difference of two sorted ranges.
4298 * @param first1 Start of first range.
4299 * @param last1 End of first range.
4300 * @param first2 Start of second range.
4301 * @param last2 End of second range.
4302 * @return End of the output range.
4303 * @ingroup setoperations
4305 * This operation iterates over both ranges, copying elements present in
4306 * the first range but not the second in order to the output range.
4307 * Iterators increment for each range. When the current element of the
4308 * first range is less than the second, that element is copied and the
4309 * iterator advances. If the current element of the second range is less,
4310 * the iterator advances, but no element is copied. If an element is
4311 * contained in both ranges, no elements are copied and both ranges
4312 * advance. The output range may not overlap either input range.
4314 template<typename _InputIterator1
, typename _InputIterator2
,
4315 typename _OutputIterator
>
4317 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4318 _InputIterator2 __first2
, _InputIterator2 __last2
,
4319 _OutputIterator __result
)
4321 // concept requirements
4322 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4323 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4324 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4325 typename iterator_traits
<_InputIterator1
>::value_type
>)
4326 __glibcxx_function_requires(_SameTypeConcept
<
4327 typename iterator_traits
<_InputIterator1
>::value_type
,
4328 typename iterator_traits
<_InputIterator2
>::value_type
>)
4329 __glibcxx_function_requires(_LessThanComparableConcept
<
4330 typename iterator_traits
<_InputIterator1
>::value_type
>)
4331 __glibcxx_requires_sorted(__first1
, __last1
);
4332 __glibcxx_requires_sorted(__first2
, __last2
);
4334 while (__first1
!= __last1
&& __first2
!= __last2
)
4335 if (*__first1
< *__first2
)
4337 *__result
= *__first1
;
4341 else if (*__first2
< *__first1
)
4348 return std::copy(__first1
, __last1
, __result
);
4352 * @brief Return the difference of two sorted ranges using comparison
4354 * @param first1 Start of first range.
4355 * @param last1 End of first range.
4356 * @param first2 Start of second range.
4357 * @param last2 End of second range.
4358 * @param comp The comparison functor.
4359 * @return End of the output range.
4360 * @ingroup setoperations
4362 * This operation iterates over both ranges, copying elements present in
4363 * the first range but not the second in order to the output range.
4364 * Iterators increment for each range. When the current element of the
4365 * first range is less than the second according to @a comp, that element
4366 * is copied and the iterator advances. If the current element of the
4367 * second range is less, no element is copied and the iterator advances.
4368 * If an element is contained in both ranges according to @a comp, no
4369 * elements are copied and both ranges advance. The output range may not
4370 * overlap either input range.
4372 template<typename _InputIterator1
, typename _InputIterator2
,
4373 typename _OutputIterator
, typename _Compare
>
4375 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4376 _InputIterator2 __first2
, _InputIterator2 __last2
,
4377 _OutputIterator __result
, _Compare __comp
)
4379 // concept requirements
4380 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4381 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4382 __glibcxx_function_requires(_SameTypeConcept
<
4383 typename iterator_traits
<_InputIterator1
>::value_type
,
4384 typename iterator_traits
<_InputIterator2
>::value_type
>)
4385 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4386 typename iterator_traits
<_InputIterator1
>::value_type
>)
4387 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4388 typename iterator_traits
<_InputIterator1
>::value_type
,
4389 typename iterator_traits
<_InputIterator2
>::value_type
>)
4390 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4391 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4393 while (__first1
!= __last1
&& __first2
!= __last2
)
4394 if (__comp(*__first1
, *__first2
))
4396 *__result
= *__first1
;
4400 else if (__comp(*__first2
, *__first1
))
4407 return std::copy(__first1
, __last1
, __result
);
4411 * @brief Return the symmetric difference of two sorted ranges.
4412 * @param first1 Start of first range.
4413 * @param last1 End of first range.
4414 * @param first2 Start of second range.
4415 * @param last2 End of second range.
4416 * @return End of the output range.
4417 * @ingroup setoperations
4419 * This operation iterates over both ranges, copying elements present in
4420 * one range but not the other in order to the output range. Iterators
4421 * increment for each range. When the current element of one range is less
4422 * than the other, that element is copied and the iterator advances. If an
4423 * element is contained in both ranges, no elements are copied and both
4424 * ranges advance. The output range may not overlap either input range.
4426 template<typename _InputIterator1
, typename _InputIterator2
,
4427 typename _OutputIterator
>
4429 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4430 _InputIterator2 __first2
, _InputIterator2 __last2
,
4431 _OutputIterator __result
)
4433 // concept requirements
4434 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4435 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4436 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4437 typename iterator_traits
<_InputIterator1
>::value_type
>)
4438 __glibcxx_function_requires(_SameTypeConcept
<
4439 typename iterator_traits
<_InputIterator1
>::value_type
,
4440 typename iterator_traits
<_InputIterator2
>::value_type
>)
4441 __glibcxx_function_requires(_LessThanComparableConcept
<
4442 typename iterator_traits
<_InputIterator1
>::value_type
>)
4443 __glibcxx_requires_sorted(__first1
, __last1
);
4444 __glibcxx_requires_sorted(__first2
, __last2
);
4446 while (__first1
!= __last1
&& __first2
!= __last2
)
4447 if (*__first1
< *__first2
)
4449 *__result
= *__first1
;
4453 else if (*__first2
< *__first1
)
4455 *__result
= *__first2
;
4464 return std::copy(__first2
, __last2
, std::copy(__first1
,
4465 __last1
, __result
));
4469 * @brief Return the symmetric difference of two sorted ranges using
4470 * comparison functor.
4471 * @param first1 Start of first range.
4472 * @param last1 End of first range.
4473 * @param first2 Start of second range.
4474 * @param last2 End of second range.
4475 * @param comp The comparison functor.
4476 * @return End of the output range.
4477 * @ingroup setoperations
4479 * This operation iterates over both ranges, copying elements present in
4480 * one range but not the other in order to the output range. Iterators
4481 * increment for each range. When the current element of one range is less
4482 * than the other according to @a comp, that element is copied and the
4483 * iterator advances. If an element is contained in both ranges according
4484 * to @a comp, no elements are copied and both ranges advance. The output
4485 * range may not overlap either input range.
4487 template<typename _InputIterator1
, typename _InputIterator2
,
4488 typename _OutputIterator
, typename _Compare
>
4490 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4491 _InputIterator2 __first2
, _InputIterator2 __last2
,
4492 _OutputIterator __result
,
4495 // concept requirements
4496 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4497 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4498 __glibcxx_function_requires(_SameTypeConcept
<
4499 typename iterator_traits
<_InputIterator1
>::value_type
,
4500 typename iterator_traits
<_InputIterator2
>::value_type
>)
4501 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4502 typename iterator_traits
<_InputIterator1
>::value_type
>)
4503 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4504 typename iterator_traits
<_InputIterator1
>::value_type
,
4505 typename iterator_traits
<_InputIterator2
>::value_type
>)
4506 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4507 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4509 while (__first1
!= __last1
&& __first2
!= __last2
)
4510 if (__comp(*__first1
, *__first2
))
4512 *__result
= *__first1
;
4516 else if (__comp(*__first2
, *__first1
))
4518 *__result
= *__first2
;
4527 return std::copy(__first2
, __last2
, std::copy(__first1
,
4528 __last1
, __result
));
4531 // min_element and max_element, with and without an explicitly supplied
4532 // comparison function.
4535 * @brief Return the maximum element in a range.
4536 * @param first Start of range.
4537 * @param last End of range.
4538 * @return Iterator referencing the first instance of the largest value.
4540 template<typename _ForwardIterator
>
4542 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
4544 // concept requirements
4545 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4546 __glibcxx_function_requires(_LessThanComparableConcept
<
4547 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4548 __glibcxx_requires_valid_range(__first
, __last
);
4550 if (__first
== __last
)
4552 _ForwardIterator __result
= __first
;
4553 while (++__first
!= __last
)
4554 if (*__result
< *__first
)
4560 * @brief Return the maximum element in a range using comparison functor.
4561 * @param first Start of range.
4562 * @param last End of range.
4563 * @param comp Comparison functor.
4564 * @return Iterator referencing the first instance of the largest value
4565 * according to comp.
4567 template<typename _ForwardIterator
, typename _Compare
>
4569 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
4572 // concept requirements
4573 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4574 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4575 typename iterator_traits
<_ForwardIterator
>::value_type
,
4576 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4577 __glibcxx_requires_valid_range(__first
, __last
);
4579 if (__first
== __last
) return __first
;
4580 _ForwardIterator __result
= __first
;
4581 while (++__first
!= __last
)
4582 if (__comp(*__result
, *__first
)) __result
= __first
;
4587 * @brief Return the minimum element in a range.
4588 * @param first Start of range.
4589 * @param last End of range.
4590 * @return Iterator referencing the first instance of the smallest value.
4592 template<typename _ForwardIterator
>
4594 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
4596 // concept requirements
4597 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4598 __glibcxx_function_requires(_LessThanComparableConcept
<
4599 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4600 __glibcxx_requires_valid_range(__first
, __last
);
4602 if (__first
== __last
)
4604 _ForwardIterator __result
= __first
;
4605 while (++__first
!= __last
)
4606 if (*__first
< *__result
)
4612 * @brief Return the minimum element in a range using comparison functor.
4613 * @param first Start of range.
4614 * @param last End of range.
4615 * @param comp Comparison functor.
4616 * @return Iterator referencing the first instance of the smallest value
4617 * according to comp.
4619 template<typename _ForwardIterator
, typename _Compare
>
4621 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
4624 // concept requirements
4625 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4626 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4627 typename iterator_traits
<_ForwardIterator
>::value_type
,
4628 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4629 __glibcxx_requires_valid_range(__first
, __last
);
4631 if (__first
== __last
)
4633 _ForwardIterator __result
= __first
;
4634 while (++__first
!= __last
)
4635 if (__comp(*__first
, *__result
))
4640 // next_permutation and prev_permutation, with and without an explicitly
4641 // supplied comparison function.
4644 * @brief Permute range into the next "dictionary" ordering.
4645 * @param first Start of range.
4646 * @param last End of range.
4647 * @return False if wrapped to first permutation, true otherwise.
4649 * Treats all permutations of the range as a set of "dictionary" sorted
4650 * sequences. Permutes the current sequence into the next one of this set.
4651 * Returns true if there are more sequences to generate. If the sequence
4652 * is the largest of the set, the smallest is generated and false returned.
4654 template<typename _BidirectionalIterator
>
4656 next_permutation(_BidirectionalIterator __first
,
4657 _BidirectionalIterator __last
)
4659 // concept requirements
4660 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4661 _BidirectionalIterator
>)
4662 __glibcxx_function_requires(_LessThanComparableConcept
<
4663 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4664 __glibcxx_requires_valid_range(__first
, __last
);
4666 if (__first
== __last
)
4668 _BidirectionalIterator __i
= __first
;
4677 _BidirectionalIterator __ii
= __i
;
4681 _BidirectionalIterator __j
= __last
;
4682 while (!(*__i
< *--__j
))
4684 std::iter_swap(__i
, __j
);
4685 std::reverse(__ii
, __last
);
4690 std::reverse(__first
, __last
);
4697 * @brief Permute range into the next "dictionary" ordering using
4698 * comparison functor.
4699 * @param first Start of range.
4700 * @param last End of range.
4702 * @return False if wrapped to first permutation, true otherwise.
4704 * Treats all permutations of the range [first,last) as a set of
4705 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
4706 * sequence into the next one of this set. Returns true if there are more
4707 * sequences to generate. If the sequence is the largest of the set, the
4708 * smallest is generated and false returned.
4710 template<typename _BidirectionalIterator
, typename _Compare
>
4712 next_permutation(_BidirectionalIterator __first
,
4713 _BidirectionalIterator __last
, _Compare __comp
)
4715 // concept requirements
4716 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4717 _BidirectionalIterator
>)
4718 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4719 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
4720 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4721 __glibcxx_requires_valid_range(__first
, __last
);
4723 if (__first
== __last
)
4725 _BidirectionalIterator __i
= __first
;
4734 _BidirectionalIterator __ii
= __i
;
4736 if (__comp(*__i
, *__ii
))
4738 _BidirectionalIterator __j
= __last
;
4739 while (!__comp(*__i
, *--__j
))
4741 std::iter_swap(__i
, __j
);
4742 std::reverse(__ii
, __last
);
4747 std::reverse(__first
, __last
);
4754 * @brief Permute range into the previous "dictionary" ordering.
4755 * @param first Start of range.
4756 * @param last End of range.
4757 * @return False if wrapped to last permutation, true otherwise.
4759 * Treats all permutations of the range as a set of "dictionary" sorted
4760 * sequences. Permutes the current sequence into the previous one of this
4761 * set. Returns true if there are more sequences to generate. If the
4762 * sequence is the smallest of the set, the largest is generated and false
4765 template<typename _BidirectionalIterator
>
4767 prev_permutation(_BidirectionalIterator __first
,
4768 _BidirectionalIterator __last
)
4770 // concept requirements
4771 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4772 _BidirectionalIterator
>)
4773 __glibcxx_function_requires(_LessThanComparableConcept
<
4774 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4775 __glibcxx_requires_valid_range(__first
, __last
);
4777 if (__first
== __last
)
4779 _BidirectionalIterator __i
= __first
;
4788 _BidirectionalIterator __ii
= __i
;
4792 _BidirectionalIterator __j
= __last
;
4793 while (!(*--__j
< *__i
))
4795 std::iter_swap(__i
, __j
);
4796 std::reverse(__ii
, __last
);
4801 std::reverse(__first
, __last
);
4808 * @brief Permute range into the previous "dictionary" ordering using
4809 * comparison functor.
4810 * @param first Start of range.
4811 * @param last End of range.
4813 * @return False if wrapped to last permutation, true otherwise.
4815 * Treats all permutations of the range [first,last) as a set of
4816 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
4817 * sequence into the previous one of this set. Returns true if there are
4818 * more sequences to generate. If the sequence is the smallest of the set,
4819 * the largest is generated and false returned.
4821 template<typename _BidirectionalIterator
, typename _Compare
>
4823 prev_permutation(_BidirectionalIterator __first
,
4824 _BidirectionalIterator __last
, _Compare __comp
)
4826 // concept requirements
4827 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4828 _BidirectionalIterator
>)
4829 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4830 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
4831 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4832 __glibcxx_requires_valid_range(__first
, __last
);
4834 if (__first
== __last
)
4836 _BidirectionalIterator __i
= __first
;
4845 _BidirectionalIterator __ii
= __i
;
4847 if (__comp(*__ii
, *__i
))
4849 _BidirectionalIterator __j
= __last
;
4850 while (!__comp(*--__j
, *__i
))
4852 std::iter_swap(__i
, __j
);
4853 std::reverse(__ii
, __last
);
4858 std::reverse(__first
, __last
);
4864 // find_first_of, with and without an explicitly supplied comparison function.
4867 * @brief Find element from a set in a sequence.
4868 * @param first1 Start of range to search.
4869 * @param last1 End of range to search.
4870 * @param first2 Start of match candidates.
4871 * @param last2 End of match candidates.
4872 * @return The first iterator @c i in the range
4873 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4874 * interator in [first2,last2), or @p last1 if no such iterator exists.
4876 * Searches the range @p [first1,last1) for an element that is equal to
4877 * some element in the range [first2,last2). If found, returns an iterator
4878 * in the range [first1,last1), otherwise returns @p last1.
4880 template<typename _InputIterator
, typename _ForwardIterator
>
4882 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4883 _ForwardIterator __first2
, _ForwardIterator __last2
)
4885 // concept requirements
4886 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4887 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4888 __glibcxx_function_requires(_EqualOpConcept
<
4889 typename iterator_traits
<_InputIterator
>::value_type
,
4890 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4891 __glibcxx_requires_valid_range(__first1
, __last1
);
4892 __glibcxx_requires_valid_range(__first2
, __last2
);
4894 for ( ; __first1
!= __last1
; ++__first1
)
4895 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4896 if (*__first1
== *__iter
)
4902 * @brief Find element from a set in a sequence using a predicate.
4903 * @param first1 Start of range to search.
4904 * @param last1 End of range to search.
4905 * @param first2 Start of match candidates.
4906 * @param last2 End of match candidates.
4907 * @param comp Predicate to use.
4908 * @return The first iterator @c i in the range
4909 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4910 * interator in [first2,last2), or @p last1 if no such iterator exists.
4912 * Searches the range @p [first1,last1) for an element that is equal to
4913 * some element in the range [first2,last2). If found, returns an iterator in
4914 * the range [first1,last1), otherwise returns @p last1.
4916 template<typename _InputIterator
, typename _ForwardIterator
,
4917 typename _BinaryPredicate
>
4919 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4920 _ForwardIterator __first2
, _ForwardIterator __last2
,
4921 _BinaryPredicate __comp
)
4923 // concept requirements
4924 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4925 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4926 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4927 typename iterator_traits
<_InputIterator
>::value_type
,
4928 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4929 __glibcxx_requires_valid_range(__first1
, __last1
);
4930 __glibcxx_requires_valid_range(__first2
, __last2
);
4932 for ( ; __first1
!= __last1
; ++__first1
)
4933 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4934 if (__comp(*__first1
, *__iter
))
4940 // find_end, with and without an explicitly supplied comparison function.
4941 // Search [first2, last2) as a subsequence in [first1, last1), and return
4942 // the *last* possible match. Note that find_end for bidirectional iterators
4943 // is much faster than for forward iterators.
4945 // find_end for forward iterators.
4946 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4948 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4949 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4950 forward_iterator_tag
, forward_iterator_tag
)
4952 if (__first2
== __last2
)
4956 _ForwardIterator1 __result
= __last1
;
4959 _ForwardIterator1 __new_result
4960 = std::search(__first1
, __last1
, __first2
, __last2
);
4961 if (__new_result
== __last1
)
4965 __result
= __new_result
;
4966 __first1
= __new_result
;
4973 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4974 typename _BinaryPredicate
>
4976 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4977 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4978 forward_iterator_tag
, forward_iterator_tag
,
4979 _BinaryPredicate __comp
)
4981 if (__first2
== __last2
)
4985 _ForwardIterator1 __result
= __last1
;
4988 _ForwardIterator1 __new_result
4989 = std::search(__first1
, __last1
, __first2
, __last2
, __comp
);
4990 if (__new_result
== __last1
)
4994 __result
= __new_result
;
4995 __first1
= __new_result
;
5002 // find_end for bidirectional iterators. Requires partial specialization.
5003 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
5004 _BidirectionalIterator1
5005 __find_end(_BidirectionalIterator1 __first1
,
5006 _BidirectionalIterator1 __last1
,
5007 _BidirectionalIterator2 __first2
,
5008 _BidirectionalIterator2 __last2
,
5009 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
5011 // concept requirements
5012 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5013 _BidirectionalIterator1
>)
5014 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5015 _BidirectionalIterator2
>)
5017 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
5018 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
5020 _RevIterator1
__rlast1(__first1
);
5021 _RevIterator2
__rlast2(__first2
);
5022 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
5023 _RevIterator2(__last2
), __rlast2
);
5025 if (__rresult
== __rlast1
)
5029 _BidirectionalIterator1 __result
= __rresult
.base();
5030 std::advance(__result
, -std::distance(__first2
, __last2
));
5035 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
5036 typename _BinaryPredicate
>
5037 _BidirectionalIterator1
5038 __find_end(_BidirectionalIterator1 __first1
,
5039 _BidirectionalIterator1 __last1
,
5040 _BidirectionalIterator2 __first2
,
5041 _BidirectionalIterator2 __last2
,
5042 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
5043 _BinaryPredicate __comp
)
5045 // concept requirements
5046 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5047 _BidirectionalIterator1
>)
5048 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5049 _BidirectionalIterator2
>)
5051 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
5052 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
5054 _RevIterator1
__rlast1(__first1
);
5055 _RevIterator2
__rlast2(__first2
);
5056 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
5057 _RevIterator2(__last2
), __rlast2
,
5060 if (__rresult
== __rlast1
)
5064 _BidirectionalIterator1 __result
= __rresult
.base();
5065 std::advance(__result
, -std::distance(__first2
, __last2
));
5070 // Dispatching functions for find_end.
5073 * @brief Find last matching subsequence in a sequence.
5074 * @param first1 Start of range to search.
5075 * @param last1 End of range to search.
5076 * @param first2 Start of sequence to match.
5077 * @param last2 End of sequence to match.
5078 * @return The last iterator @c i in the range
5079 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
5080 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
5081 * such iterator exists.
5083 * Searches the range @p [first1,last1) for a sub-sequence that compares
5084 * equal value-by-value with the sequence given by @p [first2,last2) and
5085 * returns an iterator to the first element of the sub-sequence, or
5086 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
5087 * last such subsequence contained in [first,last1).
5089 * Because the sub-sequence must lie completely within the range
5090 * @p [first1,last1) it must start at a position less than
5091 * @p last1-(last2-first2) where @p last2-first2 is the length of the
5093 * This means that the returned iterator @c i will be in the range
5094 * @p [first1,last1-(last2-first2))
5096 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
5097 inline _ForwardIterator1
5098 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
5099 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
5101 // concept requirements
5102 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
5103 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
5104 __glibcxx_function_requires(_EqualOpConcept
<
5105 typename iterator_traits
<_ForwardIterator1
>::value_type
,
5106 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
5107 __glibcxx_requires_valid_range(__first1
, __last1
);
5108 __glibcxx_requires_valid_range(__first2
, __last2
);
5110 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
5111 std::__iterator_category(__first1
),
5112 std::__iterator_category(__first2
));
5116 * @brief Find last matching subsequence in a sequence using a predicate.
5117 * @param first1 Start of range to search.
5118 * @param last1 End of range to search.
5119 * @param first2 Start of sequence to match.
5120 * @param last2 End of sequence to match.
5121 * @param comp The predicate to use.
5122 * @return The last iterator @c i in the range
5123 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
5124 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
5125 * @p last1 if no such iterator exists.
5127 * Searches the range @p [first1,last1) for a sub-sequence that compares
5128 * equal value-by-value with the sequence given by @p [first2,last2) using
5129 * comp as a predicate and returns an iterator to the first element of the
5130 * sub-sequence, or @p last1 if the sub-sequence is not found. The
5131 * sub-sequence will be the last such subsequence contained in
5134 * Because the sub-sequence must lie completely within the range
5135 * @p [first1,last1) it must start at a position less than
5136 * @p last1-(last2-first2) where @p last2-first2 is the length of the
5138 * This means that the returned iterator @c i will be in the range
5139 * @p [first1,last1-(last2-first2))
5141 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
5142 typename _BinaryPredicate
>
5143 inline _ForwardIterator1
5144 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
5145 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
5146 _BinaryPredicate __comp
)
5148 // concept requirements
5149 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
5150 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
5151 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
5152 typename iterator_traits
<_ForwardIterator1
>::value_type
,
5153 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
5154 __glibcxx_requires_valid_range(__first1
, __last1
);
5155 __glibcxx_requires_valid_range(__first2
, __last2
);
5157 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
5158 std::__iterator_category(__first1
),
5159 std::__iterator_category(__first2
),
5165 #endif /* _ALGO_H */