1 // Algorithm implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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(_EqualOpConcept
<
417 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
418 __glibcxx_requires_valid_range(__first
, __last
);
419 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
420 for ( ; __first
!= __last
; ++__first
)
421 if (*__first
== __value
)
427 * @brief Count the elements of a sequence for which a predicate is true.
428 * @param first An input iterator.
429 * @param last An input iterator.
430 * @param pred A predicate.
431 * @return The number of iterators @c i in the range @p [first,last)
432 * for which @p pred(*i) is true.
434 template<typename _InputIterator
, typename _Predicate
>
435 typename iterator_traits
<_InputIterator
>::difference_type
436 count_if(_InputIterator __first
, _InputIterator __last
, _Predicate __pred
)
438 // concept requirements
439 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
440 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
441 typename iterator_traits
<_InputIterator
>::value_type
>)
442 __glibcxx_requires_valid_range(__first
, __last
);
443 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
444 for ( ; __first
!= __last
; ++__first
)
445 if (__pred(*__first
))
451 * @brief Search a sequence for a matching sub-sequence.
452 * @param first1 A forward iterator.
453 * @param last1 A forward iterator.
454 * @param first2 A forward iterator.
455 * @param last2 A forward iterator.
456 * @return The first iterator @c i in the range
457 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
458 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
459 * such iterator exists.
461 * Searches the range @p [first1,last1) for a sub-sequence that compares
462 * equal value-by-value with the sequence given by @p [first2,last2) and
463 * returns an iterator to the first element of the sub-sequence, or
464 * @p last1 if the sub-sequence is not found.
466 * Because the sub-sequence must lie completely within the range
467 * @p [first1,last1) it must start at a position less than
468 * @p last1-(last2-first2) where @p last2-first2 is the length of the
470 * This means that the returned iterator @c i will be in the range
471 * @p [first1,last1-(last2-first2))
473 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
475 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
476 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
478 // concept requirements
479 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
480 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
481 __glibcxx_function_requires(_EqualOpConcept
<
482 typename iterator_traits
<_ForwardIterator1
>::value_type
,
483 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
484 __glibcxx_requires_valid_range(__first1
, __last1
);
485 __glibcxx_requires_valid_range(__first2
, __last2
);
486 // Test for empty ranges
487 if (__first1
== __last1
|| __first2
== __last2
)
490 // Test for a pattern of length 1.
491 _ForwardIterator2
__tmp(__first2
);
493 if (__tmp
== __last2
)
494 return std::find(__first1
, __last1
, *__first2
);
497 _ForwardIterator2 __p1
, __p
;
498 __p1
= __first2
; ++__p1
;
499 _ForwardIterator1 __current
= __first1
;
501 while (__first1
!= __last1
)
503 __first1
= std::find(__first1
, __last1
, *__first2
);
504 if (__first1
== __last1
)
508 __current
= __first1
;
509 if (++__current
== __last1
)
512 while (*__current
== *__p
)
514 if (++__p
== __last2
)
516 if (++__current
== __last1
)
525 * @brief Search a sequence for a matching sub-sequence using a predicate.
526 * @param first1 A forward iterator.
527 * @param last1 A forward iterator.
528 * @param first2 A forward iterator.
529 * @param last2 A forward iterator.
530 * @param predicate A binary predicate.
531 * @return The first iterator @c i in the range
532 * @p [first1,last1-(last2-first2)) such that
533 * @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
534 * @p [0,last2-first2), or @p last1 if no such iterator exists.
536 * Searches the range @p [first1,last1) for a sub-sequence that compares
537 * equal value-by-value with the sequence given by @p [first2,last2),
538 * using @p predicate to determine equality, and returns an iterator
539 * to the first element of the sub-sequence, or @p last1 if no such
542 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
544 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
545 typename _BinaryPredicate
>
547 search(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
548 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
549 _BinaryPredicate __predicate
)
551 // concept requirements
552 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
553 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
554 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
555 typename iterator_traits
<_ForwardIterator1
>::value_type
,
556 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
557 __glibcxx_requires_valid_range(__first1
, __last1
);
558 __glibcxx_requires_valid_range(__first2
, __last2
);
560 // Test for empty ranges
561 if (__first1
== __last1
|| __first2
== __last2
)
564 // Test for a pattern of length 1.
565 _ForwardIterator2
__tmp(__first2
);
567 if (__tmp
== __last2
)
569 while (__first1
!= __last1
&& !__predicate(*__first1
, *__first2
))
575 _ForwardIterator2 __p1
, __p
;
576 __p1
= __first2
; ++__p1
;
577 _ForwardIterator1 __current
= __first1
;
579 while (__first1
!= __last1
)
581 while (__first1
!= __last1
)
583 if (__predicate(*__first1
, *__first2
))
587 while (__first1
!= __last1
&& !__predicate(*__first1
, *__first2
))
589 if (__first1
== __last1
)
593 __current
= __first1
;
594 if (++__current
== __last1
)
597 while (__predicate(*__current
, *__p
))
599 if (++__p
== __last2
)
601 if (++__current
== __last1
)
610 * @brief Search a sequence for a number of consecutive values.
611 * @param first A forward iterator.
612 * @param last A forward iterator.
613 * @param count The number of consecutive values.
614 * @param val The value to find.
615 * @return The first iterator @c i in the range @p [first,last-count)
616 * such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
617 * or @p last if no such iterator exists.
619 * Searches the range @p [first,last) for @p count consecutive elements
622 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
>
624 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
625 _Integer __count
, const _Tp
& __val
)
627 // concept requirements
628 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
629 __glibcxx_function_requires(_EqualOpConcept
<
630 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
631 __glibcxx_requires_valid_range(__first
, __last
);
637 __first
= std::find(__first
, __last
, __val
);
638 while (__first
!= __last
)
640 typename iterator_traits
<_ForwardIterator
>::difference_type
642 _ForwardIterator __i
= __first
;
644 while (__i
!= __last
&& __n
!= 1 && *__i
== __val
)
652 __first
= std::find(__i
, __last
, __val
);
659 * @brief Search a sequence for a number of consecutive values using a
661 * @param first A forward iterator.
662 * @param last A forward iterator.
663 * @param count The number of consecutive values.
664 * @param val The value to find.
665 * @param binary_pred A binary predicate.
666 * @return The first iterator @c i in the range @p [first,last-count)
667 * such that @p binary_pred(*(i+N),val) is true for each @c N in the
668 * range @p [0,count), or @p last if no such iterator exists.
670 * Searches the range @p [first,last) for @p count consecutive elements
671 * for which the predicate returns true.
673 template<typename _ForwardIterator
, typename _Integer
, typename _Tp
,
674 typename _BinaryPredicate
>
676 search_n(_ForwardIterator __first
, _ForwardIterator __last
,
677 _Integer __count
, const _Tp
& __val
,
678 _BinaryPredicate __binary_pred
)
680 // concept requirements
681 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
682 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
683 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
684 __glibcxx_requires_valid_range(__first
, __last
);
690 while (__first
!= __last
)
692 if (__binary_pred(*__first
, __val
))
696 while (__first
!= __last
)
698 typename iterator_traits
<_ForwardIterator
>::difference_type
700 _ForwardIterator __i
= __first
;
702 while (__i
!= __last
&& __n
!= 1 && __binary_pred(*__i
, __val
))
711 while (__i
!= __last
)
713 if (__binary_pred(*__i
, __val
))
725 * @brief Swap the elements of two sequences.
726 * @param first1 A forward iterator.
727 * @param last1 A forward iterator.
728 * @param first2 A forward iterator.
729 * @return An iterator equal to @p first2+(last1-first1).
731 * Swaps each element in the range @p [first1,last1) with the
732 * corresponding element in the range @p [first2,(last1-first1)).
733 * The ranges must not overlap.
735 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
737 swap_ranges(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
738 _ForwardIterator2 __first2
)
740 // concept requirements
741 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
743 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
745 __glibcxx_function_requires(_ConvertibleConcept
<
746 typename iterator_traits
<_ForwardIterator1
>::value_type
,
747 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
748 __glibcxx_function_requires(_ConvertibleConcept
<
749 typename iterator_traits
<_ForwardIterator2
>::value_type
,
750 typename iterator_traits
<_ForwardIterator1
>::value_type
>)
751 __glibcxx_requires_valid_range(__first1
, __last1
);
753 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
)
754 std::iter_swap(__first1
, __first2
);
759 * @brief Perform an operation on a sequence.
760 * @param first An input iterator.
761 * @param last An input iterator.
762 * @param result An output iterator.
763 * @param unary_op A unary operator.
764 * @return An output iterator equal to @p result+(last-first).
766 * Applies the operator to each element in the input range and assigns
767 * the results to successive elements of the output sequence.
768 * Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
769 * range @p [0,last-first).
771 * @p unary_op must not alter its argument.
773 template<typename _InputIterator
, typename _OutputIterator
,
774 typename _UnaryOperation
>
776 transform(_InputIterator __first
, _InputIterator __last
,
777 _OutputIterator __result
, _UnaryOperation __unary_op
)
779 // concept requirements
780 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
781 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
782 // "the type returned by a _UnaryOperation"
783 __typeof__(__unary_op(*__first
))>)
784 __glibcxx_requires_valid_range(__first
, __last
);
786 for ( ; __first
!= __last
; ++__first
, ++__result
)
787 *__result
= __unary_op(*__first
);
792 * @brief Perform an operation on corresponding elements of two sequences.
793 * @param first1 An input iterator.
794 * @param last1 An input iterator.
795 * @param first2 An input iterator.
796 * @param result An output iterator.
797 * @param binary_op A binary operator.
798 * @return An output iterator equal to @p result+(last-first).
800 * Applies the operator to the corresponding elements in the two
801 * input ranges and assigns the results to successive elements of the
803 * Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
804 * @c N in the range @p [0,last1-first1).
806 * @p binary_op must not alter either of its arguments.
808 template<typename _InputIterator1
, typename _InputIterator2
,
809 typename _OutputIterator
, typename _BinaryOperation
>
811 transform(_InputIterator1 __first1
, _InputIterator1 __last1
,
812 _InputIterator2 __first2
, _OutputIterator __result
,
813 _BinaryOperation __binary_op
)
815 // concept requirements
816 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
817 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
818 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
819 // "the type returned by a _BinaryOperation"
820 __typeof__(__binary_op(*__first1
,*__first2
))>)
821 __glibcxx_requires_valid_range(__first1
, __last1
);
823 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
, ++__result
)
824 *__result
= __binary_op(*__first1
, *__first2
);
829 * @brief Replace each occurrence of one value in a sequence with another
831 * @param first A forward iterator.
832 * @param last A forward iterator.
833 * @param old_value The value to be replaced.
834 * @param new_value The replacement value.
835 * @return replace() returns no value.
837 * For each iterator @c i in the range @p [first,last) if @c *i ==
838 * @p old_value then the assignment @c *i = @p new_value is performed.
840 template<typename _ForwardIterator
, typename _Tp
>
842 replace(_ForwardIterator __first
, _ForwardIterator __last
,
843 const _Tp
& __old_value
, const _Tp
& __new_value
)
845 // concept requirements
846 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
848 __glibcxx_function_requires(_EqualOpConcept
<
849 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
850 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
851 typename iterator_traits
<_ForwardIterator
>::value_type
>)
852 __glibcxx_requires_valid_range(__first
, __last
);
854 for ( ; __first
!= __last
; ++__first
)
855 if (*__first
== __old_value
)
856 *__first
= __new_value
;
860 * @brief Replace each value in a sequence for which a predicate returns
861 * true with another value.
862 * @param first A forward iterator.
863 * @param last A forward iterator.
864 * @param pred A predicate.
865 * @param new_value The replacement value.
866 * @return replace_if() returns no value.
868 * For each iterator @c i in the range @p [first,last) if @p pred(*i)
869 * is true then the assignment @c *i = @p new_value is performed.
871 template<typename _ForwardIterator
, typename _Predicate
, typename _Tp
>
873 replace_if(_ForwardIterator __first
, _ForwardIterator __last
,
874 _Predicate __pred
, const _Tp
& __new_value
)
876 // concept requirements
877 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
879 __glibcxx_function_requires(_ConvertibleConcept
<_Tp
,
880 typename iterator_traits
<_ForwardIterator
>::value_type
>)
881 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
882 typename iterator_traits
<_ForwardIterator
>::value_type
>)
883 __glibcxx_requires_valid_range(__first
, __last
);
885 for ( ; __first
!= __last
; ++__first
)
886 if (__pred(*__first
))
887 *__first
= __new_value
;
891 * @brief Copy a sequence, replacing each element of one value with another
893 * @param first An input iterator.
894 * @param last An input iterator.
895 * @param result An output iterator.
896 * @param old_value The value to be replaced.
897 * @param new_value The replacement value.
898 * @return The end of the output sequence, @p result+(last-first).
900 * Copies each element in the input range @p [first,last) to the
901 * output range @p [result,result+(last-first)) replacing elements
902 * equal to @p old_value with @p new_value.
904 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
906 replace_copy(_InputIterator __first
, _InputIterator __last
,
907 _OutputIterator __result
,
908 const _Tp
& __old_value
, const _Tp
& __new_value
)
910 // concept requirements
911 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
912 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
913 typename iterator_traits
<_InputIterator
>::value_type
>)
914 __glibcxx_function_requires(_EqualOpConcept
<
915 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
916 __glibcxx_requires_valid_range(__first
, __last
);
918 for ( ; __first
!= __last
; ++__first
, ++__result
)
919 if (*__first
== __old_value
)
920 *__result
= __new_value
;
922 *__result
= *__first
;
927 * @brief Copy a sequence, replacing each value for which a predicate
928 * returns true with another value.
929 * @param first An input iterator.
930 * @param last An input iterator.
931 * @param result An output iterator.
932 * @param pred A predicate.
933 * @param new_value The replacement value.
934 * @return The end of the output sequence, @p result+(last-first).
936 * Copies each element in the range @p [first,last) to the range
937 * @p [result,result+(last-first)) replacing elements for which
938 * @p pred returns true with @p new_value.
940 template<typename _InputIterator
, typename _OutputIterator
,
941 typename _Predicate
, typename _Tp
>
943 replace_copy_if(_InputIterator __first
, _InputIterator __last
,
944 _OutputIterator __result
,
945 _Predicate __pred
, const _Tp
& __new_value
)
947 // concept requirements
948 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
949 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
950 typename iterator_traits
<_InputIterator
>::value_type
>)
951 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
952 typename iterator_traits
<_InputIterator
>::value_type
>)
953 __glibcxx_requires_valid_range(__first
, __last
);
955 for ( ; __first
!= __last
; ++__first
, ++__result
)
956 if (__pred(*__first
))
957 *__result
= __new_value
;
959 *__result
= *__first
;
964 * @brief Assign the result of a function object to each value in a
966 * @param first A forward iterator.
967 * @param last A forward iterator.
968 * @param gen A function object taking no arguments.
969 * @return generate() returns no value.
971 * Performs the assignment @c *i = @p gen() for each @c i in the range
974 template<typename _ForwardIterator
, typename _Generator
>
976 generate(_ForwardIterator __first
, _ForwardIterator __last
,
979 // concept requirements
980 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
981 __glibcxx_function_requires(_GeneratorConcept
<_Generator
,
982 typename iterator_traits
<_ForwardIterator
>::value_type
>)
983 __glibcxx_requires_valid_range(__first
, __last
);
985 for ( ; __first
!= __last
; ++__first
)
990 * @brief Assign the result of a function object to each value in a
992 * @param first A forward iterator.
993 * @param n The length of the sequence.
994 * @param gen A function object taking no arguments.
995 * @return The end of the sequence, @p first+n
997 * Performs the assignment @c *i = @p gen() for each @c i in the range
998 * @p [first,first+n).
1000 template<typename _OutputIterator
, typename _Size
, typename _Generator
>
1002 generate_n(_OutputIterator __first
, _Size __n
, _Generator __gen
)
1004 // concept requirements
1005 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1006 // "the type returned by a _Generator"
1007 __typeof__(__gen())>)
1009 for ( ; __n
> 0; --__n
, ++__first
)
1015 * @brief Copy a sequence, removing elements of a given value.
1016 * @param first An input iterator.
1017 * @param last An input iterator.
1018 * @param result An output iterator.
1019 * @param value The value to be removed.
1020 * @return An iterator designating the end of the resulting sequence.
1022 * Copies each element in the range @p [first,last) not equal to @p value
1023 * to the range beginning at @p result.
1024 * remove_copy() is stable, so the relative order of elements that are
1025 * copied is unchanged.
1027 template<typename _InputIterator
, typename _OutputIterator
, typename _Tp
>
1029 remove_copy(_InputIterator __first
, _InputIterator __last
,
1030 _OutputIterator __result
, const _Tp
& __value
)
1032 // concept requirements
1033 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1034 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1035 typename iterator_traits
<_InputIterator
>::value_type
>)
1036 __glibcxx_function_requires(_EqualOpConcept
<
1037 typename iterator_traits
<_InputIterator
>::value_type
, _Tp
>)
1038 __glibcxx_requires_valid_range(__first
, __last
);
1040 for ( ; __first
!= __last
; ++__first
)
1041 if (!(*__first
== __value
))
1043 *__result
= *__first
;
1050 * @brief Copy a sequence, removing elements for which a predicate is true.
1051 * @param first An input iterator.
1052 * @param last An input iterator.
1053 * @param result An output iterator.
1054 * @param pred A predicate.
1055 * @return An iterator designating the end of the resulting sequence.
1057 * Copies each element in the range @p [first,last) for which
1058 * @p pred returns true to the range beginning at @p result.
1060 * remove_copy_if() is stable, so the relative order of elements that are
1061 * copied is unchanged.
1063 template<typename _InputIterator
, typename _OutputIterator
,
1064 typename _Predicate
>
1066 remove_copy_if(_InputIterator __first
, _InputIterator __last
,
1067 _OutputIterator __result
, _Predicate __pred
)
1069 // concept requirements
1070 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1071 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1072 typename iterator_traits
<_InputIterator
>::value_type
>)
1073 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1074 typename iterator_traits
<_InputIterator
>::value_type
>)
1075 __glibcxx_requires_valid_range(__first
, __last
);
1077 for ( ; __first
!= __last
; ++__first
)
1078 if (!__pred(*__first
))
1080 *__result
= *__first
;
1087 * @brief Remove elements from a sequence.
1088 * @param first An input iterator.
1089 * @param last An input iterator.
1090 * @param value The value to be removed.
1091 * @return An iterator designating the end of the resulting sequence.
1093 * All elements equal to @p value are removed from the range
1096 * remove() is stable, so the relative order of elements that are
1097 * not removed is unchanged.
1099 * Elements between the end of the resulting sequence and @p last
1100 * are still present, but their value is unspecified.
1102 template<typename _ForwardIterator
, typename _Tp
>
1104 remove(_ForwardIterator __first
, _ForwardIterator __last
,
1107 // concept requirements
1108 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1110 __glibcxx_function_requires(_EqualOpConcept
<
1111 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
1112 __glibcxx_requires_valid_range(__first
, __last
);
1114 __first
= std::find(__first
, __last
, __value
);
1115 _ForwardIterator __i
= __first
;
1116 return __first
== __last
? __first
1117 : std::remove_copy(++__i
, __last
,
1122 * @brief Remove elements from a sequence using a predicate.
1123 * @param first A forward iterator.
1124 * @param last A forward iterator.
1125 * @param pred A predicate.
1126 * @return An iterator designating the end of the resulting sequence.
1128 * All elements for which @p pred returns true are removed from the range
1131 * remove_if() is stable, so the relative order of elements that are
1132 * not removed is unchanged.
1134 * Elements between the end of the resulting sequence and @p last
1135 * are still present, but their value is unspecified.
1137 template<typename _ForwardIterator
, typename _Predicate
>
1139 remove_if(_ForwardIterator __first
, _ForwardIterator __last
,
1142 // concept requirements
1143 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1145 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1146 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1147 __glibcxx_requires_valid_range(__first
, __last
);
1149 __first
= std::find_if(__first
, __last
, __pred
);
1150 _ForwardIterator __i
= __first
;
1151 return __first
== __last
? __first
1152 : std::remove_copy_if(++__i
, __last
,
1158 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1160 * overloaded for output iterators.
1163 template<typename _InputIterator
, typename _OutputIterator
>
1165 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1166 _OutputIterator __result
,
1167 output_iterator_tag
)
1169 // concept requirements -- taken care of in dispatching function
1170 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1171 *__result
= __value
;
1172 while (++__first
!= __last
)
1173 if (!(__value
== *__first
))
1176 *++__result
= __value
;
1183 * This is an uglified unique_copy(_InputIterator, _InputIterator,
1185 * overloaded for forward iterators.
1188 template<typename _InputIterator
, typename _ForwardIterator
>
1190 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1191 _ForwardIterator __result
,
1192 forward_iterator_tag
)
1194 // concept requirements -- taken care of in dispatching function
1195 *__result
= *__first
;
1196 while (++__first
!= __last
)
1197 if (!(*__result
== *__first
))
1198 *++__result
= *__first
;
1204 * This is an uglified
1205 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1207 * overloaded for output iterators.
1210 template<typename _InputIterator
, typename _OutputIterator
,
1211 typename _BinaryPredicate
>
1213 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1214 _OutputIterator __result
,
1215 _BinaryPredicate __binary_pred
,
1216 output_iterator_tag
)
1218 // concept requirements -- iterators already checked
1219 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1220 typename iterator_traits
<_InputIterator
>::value_type
,
1221 typename iterator_traits
<_InputIterator
>::value_type
>)
1223 typename iterator_traits
<_InputIterator
>::value_type __value
= *__first
;
1224 *__result
= __value
;
1225 while (++__first
!= __last
)
1226 if (!__binary_pred(__value
, *__first
))
1229 *++__result
= __value
;
1236 * This is an uglified
1237 * unique_copy(_InputIterator, _InputIterator, _OutputIterator,
1239 * overloaded for forward iterators.
1242 template<typename _InputIterator
, typename _ForwardIterator
,
1243 typename _BinaryPredicate
>
1245 __unique_copy(_InputIterator __first
, _InputIterator __last
,
1246 _ForwardIterator __result
,
1247 _BinaryPredicate __binary_pred
,
1248 forward_iterator_tag
)
1250 // concept requirements -- iterators already checked
1251 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1252 typename iterator_traits
<_ForwardIterator
>::value_type
,
1253 typename iterator_traits
<_InputIterator
>::value_type
>)
1255 *__result
= *__first
;
1256 while (++__first
!= __last
)
1257 if (!__binary_pred(*__result
, *__first
)) *++__result
= *__first
;
1262 * @brief Copy a sequence, removing consecutive duplicate values.
1263 * @param first An input iterator.
1264 * @param last An input iterator.
1265 * @param result An output iterator.
1266 * @return An iterator designating the end of the resulting sequence.
1268 * Copies each element in the range @p [first,last) to the range
1269 * beginning at @p result, except that only the first element is copied
1270 * from groups of consecutive elements that compare equal.
1271 * unique_copy() is stable, so the relative order of elements that are
1272 * copied is unchanged.
1274 template<typename _InputIterator
, typename _OutputIterator
>
1275 inline _OutputIterator
1276 unique_copy(_InputIterator __first
, _InputIterator __last
,
1277 _OutputIterator __result
)
1279 // concept requirements
1280 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1281 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1282 typename iterator_traits
<_InputIterator
>::value_type
>)
1283 __glibcxx_function_requires(_EqualityComparableConcept
<
1284 typename iterator_traits
<_InputIterator
>::value_type
>)
1285 __glibcxx_requires_valid_range(__first
, __last
);
1287 typedef typename iterator_traits
<_OutputIterator
>::iterator_category
1290 if (__first
== __last
) return __result
;
1291 return std::__unique_copy(__first
, __last
, __result
, _IterType());
1295 * @brief Copy a sequence, removing consecutive values using a predicate.
1296 * @param first An input iterator.
1297 * @param last An input iterator.
1298 * @param result An output iterator.
1299 * @param binary_pred A binary predicate.
1300 * @return An iterator designating the end of the resulting sequence.
1302 * Copies each element in the range @p [first,last) to the range
1303 * beginning at @p result, except that only the first element is copied
1304 * from groups of consecutive elements for which @p binary_pred returns
1306 * unique_copy() is stable, so the relative order of elements that are
1307 * copied is unchanged.
1309 template<typename _InputIterator
, typename _OutputIterator
,
1310 typename _BinaryPredicate
>
1311 inline _OutputIterator
1312 unique_copy(_InputIterator __first
, _InputIterator __last
,
1313 _OutputIterator __result
,
1314 _BinaryPredicate __binary_pred
)
1316 // concept requirements -- predicates checked later
1317 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
1318 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1319 typename iterator_traits
<_InputIterator
>::value_type
>)
1320 __glibcxx_requires_valid_range(__first
, __last
);
1322 typedef typename iterator_traits
<_OutputIterator
>::iterator_category
1325 if (__first
== __last
) return __result
;
1326 return std::__unique_copy(__first
, __last
, __result
,
1327 __binary_pred
, _IterType());
1331 * @brief Remove consecutive duplicate values from a sequence.
1332 * @param first A forward iterator.
1333 * @param last A forward iterator.
1334 * @return An iterator designating the end of the resulting sequence.
1336 * Removes all but the first element from each group of consecutive
1337 * values that compare equal.
1338 * unique() is stable, so the relative order of elements that are
1339 * not removed is unchanged.
1340 * Elements between the end of the resulting sequence and @p last
1341 * are still present, but their value is unspecified.
1343 template<typename _ForwardIterator
>
1345 unique(_ForwardIterator __first
, _ForwardIterator __last
)
1347 // concept requirements
1348 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1350 __glibcxx_function_requires(_EqualityComparableConcept
<
1351 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1352 __glibcxx_requires_valid_range(__first
, __last
);
1354 // Skip the beginning, if already unique.
1355 __first
= std::adjacent_find(__first
, __last
);
1356 if (__first
== __last
)
1359 // Do the real copy work.
1360 _ForwardIterator __dest
= __first
;
1362 while (++__first
!= __last
)
1363 if (!(*__dest
== *__first
))
1364 *++__dest
= *__first
;
1369 * @brief Remove consecutive values from a sequence using a predicate.
1370 * @param first A forward iterator.
1371 * @param last A forward iterator.
1372 * @param binary_pred A binary predicate.
1373 * @return An iterator designating the end of the resulting sequence.
1375 * Removes all but the first element from each group of consecutive
1376 * values for which @p binary_pred returns true.
1377 * unique() is stable, so the relative order of elements that are
1378 * not removed is unchanged.
1379 * Elements between the end of the resulting sequence and @p last
1380 * are still present, but their value is unspecified.
1382 template<typename _ForwardIterator
, typename _BinaryPredicate
>
1384 unique(_ForwardIterator __first
, _ForwardIterator __last
,
1385 _BinaryPredicate __binary_pred
)
1387 // concept requirements
1388 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1390 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
1391 typename iterator_traits
<_ForwardIterator
>::value_type
,
1392 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1393 __glibcxx_requires_valid_range(__first
, __last
);
1395 // Skip the beginning, if already unique.
1396 __first
= std::adjacent_find(__first
, __last
, __binary_pred
);
1397 if (__first
== __last
)
1400 // Do the real copy work.
1401 _ForwardIterator __dest
= __first
;
1403 while (++__first
!= __last
)
1404 if (!__binary_pred(*__dest
, *__first
))
1405 *++__dest
= *__first
;
1411 * This is an uglified reverse(_BidirectionalIterator,
1412 * _BidirectionalIterator)
1413 * overloaded for bidirectional iterators.
1416 template<typename _BidirectionalIterator
>
1418 __reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1419 bidirectional_iterator_tag
)
1422 if (__first
== __last
|| __first
== --__last
)
1426 std::iter_swap(__first
, __last
);
1433 * This is an uglified reverse(_BidirectionalIterator,
1434 * _BidirectionalIterator)
1435 * overloaded for random access iterators.
1438 template<typename _RandomAccessIterator
>
1440 __reverse(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1441 random_access_iterator_tag
)
1443 if (__first
== __last
)
1446 while (__first
< __last
)
1448 std::iter_swap(__first
, __last
);
1455 * @brief Reverse a sequence.
1456 * @param first A bidirectional iterator.
1457 * @param last A bidirectional iterator.
1458 * @return reverse() returns no value.
1460 * Reverses the order of the elements in the range @p [first,last),
1461 * so that the first element becomes the last etc.
1462 * For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
1463 * swaps @p *(first+i) and @p *(last-(i+1))
1465 template<typename _BidirectionalIterator
>
1467 reverse(_BidirectionalIterator __first
, _BidirectionalIterator __last
)
1469 // concept requirements
1470 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1471 _BidirectionalIterator
>)
1472 __glibcxx_requires_valid_range(__first
, __last
);
1473 std::__reverse(__first
, __last
, std::__iterator_category(__first
));
1477 * @brief Copy a sequence, reversing its elements.
1478 * @param first A bidirectional iterator.
1479 * @param last A bidirectional iterator.
1480 * @param result An output iterator.
1481 * @return An iterator designating the end of the resulting sequence.
1483 * Copies the elements in the range @p [first,last) to the range
1484 * @p [result,result+(last-first)) such that the order of the
1485 * elements is reversed.
1486 * For every @c i such that @p 0<=i<=(last-first), @p reverse_copy()
1487 * performs the assignment @p *(result+(last-first)-i) = *(first+i).
1488 * The ranges @p [first,last) and @p [result,result+(last-first))
1491 template<typename _BidirectionalIterator
, typename _OutputIterator
>
1493 reverse_copy(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1494 _OutputIterator __result
)
1496 // concept requirements
1497 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
1498 _BidirectionalIterator
>)
1499 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1500 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
1501 __glibcxx_requires_valid_range(__first
, __last
);
1503 while (__first
!= __last
)
1506 *__result
= *__last
;
1515 * This is a helper function for the rotate algorithm specialized on RAIs.
1516 * It returns the greatest common divisor of two integer values.
1519 template<typename _EuclideanRingElement
>
1520 _EuclideanRingElement
1521 __gcd(_EuclideanRingElement __m
, _EuclideanRingElement __n
)
1525 _EuclideanRingElement __t
= __m
% __n
;
1534 * This is a helper function for the rotate algorithm.
1537 template<typename _ForwardIterator
>
1539 __rotate(_ForwardIterator __first
,
1540 _ForwardIterator __middle
,
1541 _ForwardIterator __last
,
1542 forward_iterator_tag
)
1544 if (__first
== __middle
|| __last
== __middle
)
1547 _ForwardIterator __first2
= __middle
;
1550 swap(*__first
, *__first2
);
1553 if (__first
== __middle
)
1554 __middle
= __first2
;
1556 while (__first2
!= __last
);
1558 __first2
= __middle
;
1560 while (__first2
!= __last
)
1562 swap(*__first
, *__first2
);
1565 if (__first
== __middle
)
1566 __middle
= __first2
;
1567 else if (__first2
== __last
)
1568 __first2
= __middle
;
1574 * This is a helper function for the rotate algorithm.
1577 template<typename _BidirectionalIterator
>
1579 __rotate(_BidirectionalIterator __first
,
1580 _BidirectionalIterator __middle
,
1581 _BidirectionalIterator __last
,
1582 bidirectional_iterator_tag
)
1584 // concept requirements
1585 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
1586 _BidirectionalIterator
>)
1588 if (__first
== __middle
|| __last
== __middle
)
1591 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1592 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1594 while (__first
!= __middle
&& __middle
!= __last
)
1596 swap(*__first
, *--__last
);
1600 if (__first
== __middle
)
1601 std::__reverse(__middle
, __last
, bidirectional_iterator_tag());
1603 std::__reverse(__first
, __middle
, bidirectional_iterator_tag());
1608 * This is a helper function for the rotate algorithm.
1611 template<typename _RandomAccessIterator
>
1613 __rotate(_RandomAccessIterator __first
,
1614 _RandomAccessIterator __middle
,
1615 _RandomAccessIterator __last
,
1616 random_access_iterator_tag
)
1618 // concept requirements
1619 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1620 _RandomAccessIterator
>)
1622 if (__first
== __middle
|| __last
== __middle
)
1625 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
1627 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
1630 const _Distance __n
= __last
- __first
;
1631 const _Distance __k
= __middle
- __first
;
1632 const _Distance __l
= __n
- __k
;
1636 std::swap_ranges(__first
, __middle
, __middle
);
1640 const _Distance __d
= __gcd(__n
, __k
);
1642 for (_Distance __i
= 0; __i
< __d
; __i
++)
1644 _ValueType __tmp
= *__first
;
1645 _RandomAccessIterator __p
= __first
;
1649 for (_Distance __j
= 0; __j
< __l
/ __d
; __j
++)
1651 if (__p
> __first
+ __l
)
1653 *__p
= *(__p
- __l
);
1657 *__p
= *(__p
+ __k
);
1663 for (_Distance __j
= 0; __j
< __k
/ __d
- 1; __j
++)
1665 if (__p
< __last
- __k
)
1667 *__p
= *(__p
+ __k
);
1670 *__p
= * (__p
- __l
);
1681 * @brief Rotate the elements of a sequence.
1682 * @param first A forward iterator.
1683 * @param middle A forward iterator.
1684 * @param last A forward iterator.
1687 * Rotates the elements of the range @p [first,last) by @p (middle-first)
1688 * positions so that the element at @p middle is moved to @p first, the
1689 * element at @p middle+1 is moved to @first+1 and so on for each element
1690 * in the range @p [first,last).
1692 * This effectively swaps the ranges @p [first,middle) and
1695 * Performs @p *(first+(n+(last-middle))%(last-first))=*(first+n) for
1696 * each @p n in the range @p [0,last-first).
1698 template<typename _ForwardIterator
>
1700 rotate(_ForwardIterator __first
, _ForwardIterator __middle
,
1701 _ForwardIterator __last
)
1703 // concept requirements
1704 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1706 __glibcxx_requires_valid_range(__first
, __middle
);
1707 __glibcxx_requires_valid_range(__middle
, __last
);
1709 typedef typename iterator_traits
<_ForwardIterator
>::iterator_category
1711 std::__rotate(__first
, __middle
, __last
, _IterType());
1715 * @brief Copy a sequence, rotating its elements.
1716 * @param first A forward iterator.
1717 * @param middle A forward iterator.
1718 * @param last A forward iterator.
1719 * @param result An output iterator.
1720 * @return An iterator designating the end of the resulting sequence.
1722 * Copies the elements of the range @p [first,last) to the range
1723 * beginning at @result, rotating the copied elements by @p (middle-first)
1724 * positions so that the element at @p middle is moved to @p result, the
1725 * element at @p middle+1 is moved to @result+1 and so on for each element
1726 * in the range @p [first,last).
1728 * Performs @p *(result+(n+(last-middle))%(last-first))=*(first+n) for
1729 * each @p n in the range @p [0,last-first).
1731 template<typename _ForwardIterator
, typename _OutputIterator
>
1733 rotate_copy(_ForwardIterator __first
, _ForwardIterator __middle
,
1734 _ForwardIterator __last
, _OutputIterator __result
)
1736 // concept requirements
1737 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1738 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
1739 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1740 __glibcxx_requires_valid_range(__first
, __middle
);
1741 __glibcxx_requires_valid_range(__middle
, __last
);
1743 return std::copy(__first
, __middle
,
1744 std::copy(__middle
, __last
, __result
));
1748 * @brief Randomly shuffle the elements of a sequence.
1749 * @param first A forward iterator.
1750 * @param last A forward iterator.
1753 * Reorder the elements in the range @p [first,last) using a random
1754 * distribution, so that every possible ordering of the sequence is
1757 template<typename _RandomAccessIterator
>
1759 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
1761 // concept requirements
1762 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1763 _RandomAccessIterator
>)
1764 __glibcxx_requires_valid_range(__first
, __last
);
1766 if (__first
!= __last
)
1767 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1768 std::iter_swap(__i
, __first
+ (std::rand() % ((__i
- __first
) + 1)));
1772 * @brief Shuffle the elements of a sequence using a random number
1774 * @param first A forward iterator.
1775 * @param last A forward iterator.
1776 * @param rand The RNG functor or function.
1779 * Reorders the elements in the range @p [first,last) using @p rand to
1780 * provide a random distribution. Calling @p rand(N) for a positive
1781 * integer @p N should return a randomly chosen integer from the
1784 template<typename _RandomAccessIterator
, typename _RandomNumberGenerator
>
1786 random_shuffle(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
1787 _RandomNumberGenerator
& __rand
)
1789 // concept requirements
1790 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
1791 _RandomAccessIterator
>)
1792 __glibcxx_requires_valid_range(__first
, __last
);
1794 if (__first
== __last
)
1796 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
1797 std::iter_swap(__i
, __first
+ __rand((__i
- __first
) + 1));
1803 * This is a helper function...
1806 template<typename _ForwardIterator
, typename _Predicate
>
1808 __partition(_ForwardIterator __first
, _ForwardIterator __last
,
1810 forward_iterator_tag
)
1812 if (__first
== __last
)
1815 while (__pred(*__first
))
1816 if (++__first
== __last
)
1819 _ForwardIterator __next
= __first
;
1821 while (++__next
!= __last
)
1822 if (__pred(*__next
))
1824 swap(*__first
, *__next
);
1833 * This is a helper function...
1836 template<typename _BidirectionalIterator
, typename _Predicate
>
1837 _BidirectionalIterator
1838 __partition(_BidirectionalIterator __first
, _BidirectionalIterator __last
,
1840 bidirectional_iterator_tag
)
1845 if (__first
== __last
)
1847 else if (__pred(*__first
))
1853 if (__first
== __last
)
1855 else if (!__pred(*__last
))
1859 std::iter_swap(__first
, __last
);
1865 * @brief Move elements for which a predicate is true to the beginning
1867 * @param first A forward iterator.
1868 * @param last A forward iterator.
1869 * @param pred A predicate functor.
1870 * @return An iterator @p middle such that @p pred(i) is true for each
1871 * iterator @p i in the range @p [first,middle) and false for each @p i
1872 * in the range @p [middle,last).
1874 * @p pred must not modify its operand. @p partition() does not preserve
1875 * the relative ordering of elements in each group, use
1876 * @p stable_partition() if this is needed.
1878 template<typename _ForwardIterator
, typename _Predicate
>
1879 inline _ForwardIterator
1880 partition(_ForwardIterator __first
, _ForwardIterator __last
,
1883 // concept requirements
1884 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1886 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1887 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1888 __glibcxx_requires_valid_range(__first
, __last
);
1890 return std::__partition(__first
, __last
, __pred
,
1891 std::__iterator_category(__first
));
1897 * This is a helper function...
1900 template<typename _ForwardIterator
, typename _Predicate
, typename _Distance
>
1902 __inplace_stable_partition(_ForwardIterator __first
,
1903 _ForwardIterator __last
,
1904 _Predicate __pred
, _Distance __len
)
1907 return __pred(*__first
) ? __last
: __first
;
1908 _ForwardIterator __middle
= __first
;
1909 std::advance(__middle
, __len
/ 2);
1910 _ForwardIterator __begin
= std::__inplace_stable_partition(__first
,
1914 _ForwardIterator __end
= std::__inplace_stable_partition(__middle
, __last
,
1918 std::rotate(__begin
, __middle
, __end
);
1919 std::advance(__begin
, std::distance(__middle
, __end
));
1925 * This is a helper function...
1928 template<typename _ForwardIterator
, typename _Pointer
, typename _Predicate
,
1931 __stable_partition_adaptive(_ForwardIterator __first
,
1932 _ForwardIterator __last
,
1933 _Predicate __pred
, _Distance __len
,
1935 _Distance __buffer_size
)
1937 if (__len
<= __buffer_size
)
1939 _ForwardIterator __result1
= __first
;
1940 _Pointer __result2
= __buffer
;
1941 for ( ; __first
!= __last
; ++__first
)
1942 if (__pred(*__first
))
1944 *__result1
= *__first
;
1949 *__result2
= *__first
;
1952 std::copy(__buffer
, __result2
, __result1
);
1957 _ForwardIterator __middle
= __first
;
1958 std::advance(__middle
, __len
/ 2);
1959 _ForwardIterator __begin
=
1960 std::__stable_partition_adaptive(__first
, __middle
, __pred
,
1961 __len
/ 2, __buffer
,
1963 _ForwardIterator __end
=
1964 std::__stable_partition_adaptive(__middle
, __last
, __pred
,
1966 __buffer
, __buffer_size
);
1967 std::rotate(__begin
, __middle
, __end
);
1968 std::advance(__begin
, std::distance(__middle
, __end
));
1974 * @brief Move elements for which a predicate is true to the beginning
1975 * of a sequence, preserving relative ordering.
1976 * @param first A forward iterator.
1977 * @param last A forward iterator.
1978 * @param pred A predicate functor.
1979 * @return An iterator @p middle such that @p pred(i) is true for each
1980 * iterator @p i in the range @p [first,middle) and false for each @p i
1981 * in the range @p [middle,last).
1983 * Performs the same function as @p partition() with the additional
1984 * guarantee that the relative ordering of elements in each group is
1985 * preserved, so any two elements @p x and @p y in the range
1986 * @p [first,last) such that @p pred(x)==pred(y) will have the same
1987 * relative ordering after calling @p stable_partition().
1989 template<typename _ForwardIterator
, typename _Predicate
>
1991 stable_partition(_ForwardIterator __first
, _ForwardIterator __last
,
1994 // concept requirements
1995 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
1997 __glibcxx_function_requires(_UnaryPredicateConcept
<_Predicate
,
1998 typename iterator_traits
<_ForwardIterator
>::value_type
>)
1999 __glibcxx_requires_valid_range(__first
, __last
);
2001 if (__first
== __last
)
2005 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2007 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2010 _Temporary_buffer
<_ForwardIterator
, _ValueType
> __buf(__first
,
2012 if (__buf
.size() > 0)
2014 std::__stable_partition_adaptive(__first
, __last
, __pred
,
2015 _DistanceType(__buf
.requested_size()),
2016 __buf
.begin(), __buf
.size());
2019 std::__inplace_stable_partition(__first
, __last
, __pred
,
2020 _DistanceType(__buf
.requested_size()));
2026 * This is a helper function...
2029 template<typename _RandomAccessIterator
, typename _Tp
>
2030 _RandomAccessIterator
2031 __unguarded_partition(_RandomAccessIterator __first
,
2032 _RandomAccessIterator __last
, _Tp __pivot
)
2036 while (*__first
< __pivot
)
2039 while (__pivot
< *__last
)
2041 if (!(__first
< __last
))
2043 std::iter_swap(__first
, __last
);
2050 * This is a helper function...
2053 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2054 _RandomAccessIterator
2055 __unguarded_partition(_RandomAccessIterator __first
,
2056 _RandomAccessIterator __last
,
2057 _Tp __pivot
, _Compare __comp
)
2061 while (__comp(*__first
, __pivot
))
2064 while (__comp(__pivot
, *__last
))
2066 if (!(__first
< __last
))
2068 std::iter_swap(__first
, __last
);
2076 * This controls some aspect of the sort routines.
2079 enum { _S_threshold
= 16 };
2083 * This is a helper function for the sort routine.
2086 template<typename _RandomAccessIterator
, typename _Tp
>
2088 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
)
2090 _RandomAccessIterator __next
= __last
;
2092 while (__val
< *__next
)
2103 * This is a helper function for the sort routine.
2106 template<typename _RandomAccessIterator
, typename _Tp
, typename _Compare
>
2108 __unguarded_linear_insert(_RandomAccessIterator __last
, _Tp __val
,
2111 _RandomAccessIterator __next
= __last
;
2113 while (__comp(__val
, *__next
))
2124 * This is a helper function for the sort routine.
2127 template<typename _RandomAccessIterator
>
2129 __insertion_sort(_RandomAccessIterator __first
,
2130 _RandomAccessIterator __last
)
2132 if (__first
== __last
)
2135 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2137 typename iterator_traits
<_RandomAccessIterator
>::value_type
2139 if (__val
< *__first
)
2141 std::copy_backward(__first
, __i
, __i
+ 1);
2145 std::__unguarded_linear_insert(__i
, __val
);
2151 * This is a helper function for the sort routine.
2154 template<typename _RandomAccessIterator
, typename _Compare
>
2156 __insertion_sort(_RandomAccessIterator __first
,
2157 _RandomAccessIterator __last
, _Compare __comp
)
2159 if (__first
== __last
) return;
2161 for (_RandomAccessIterator __i
= __first
+ 1; __i
!= __last
; ++__i
)
2163 typename iterator_traits
<_RandomAccessIterator
>::value_type
2165 if (__comp(__val
, *__first
))
2167 std::copy_backward(__first
, __i
, __i
+ 1);
2171 std::__unguarded_linear_insert(__i
, __val
, __comp
);
2177 * This is a helper function for the sort routine.
2180 template<typename _RandomAccessIterator
>
2182 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2183 _RandomAccessIterator __last
)
2185 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2188 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2189 std::__unguarded_linear_insert(__i
, _ValueType(*__i
));
2194 * This is a helper function for the sort routine.
2197 template<typename _RandomAccessIterator
, typename _Compare
>
2199 __unguarded_insertion_sort(_RandomAccessIterator __first
,
2200 _RandomAccessIterator __last
, _Compare __comp
)
2202 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2205 for (_RandomAccessIterator __i
= __first
; __i
!= __last
; ++__i
)
2206 std::__unguarded_linear_insert(__i
, _ValueType(*__i
), __comp
);
2211 * This is a helper function for the sort routine.
2214 template<typename _RandomAccessIterator
>
2216 __final_insertion_sort(_RandomAccessIterator __first
,
2217 _RandomAccessIterator __last
)
2219 if (__last
- __first
> int(_S_threshold
))
2221 std::__insertion_sort(__first
, __first
+ int(_S_threshold
));
2222 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
);
2225 std::__insertion_sort(__first
, __last
);
2230 * This is a helper function for the sort routine.
2233 template<typename _RandomAccessIterator
, typename _Compare
>
2235 __final_insertion_sort(_RandomAccessIterator __first
,
2236 _RandomAccessIterator __last
, _Compare __comp
)
2238 if (__last
- __first
> int(_S_threshold
))
2240 std::__insertion_sort(__first
, __first
+ int(_S_threshold
), __comp
);
2241 std::__unguarded_insertion_sort(__first
+ int(_S_threshold
), __last
,
2245 std::__insertion_sort(__first
, __last
, __comp
);
2250 * This is a helper function for the sort routine.
2253 template<typename _Size
>
2258 for (__k
= 0; __n
!= 1; __n
>>= 1)
2264 * @brief Sort the smallest elements of a sequence.
2265 * @param first An iterator.
2266 * @param middle Another iterator.
2267 * @param last Another iterator.
2270 * Sorts the smallest @p (middle-first) elements in the range
2271 * @p [first,last) and moves them to the range @p [first,middle). The
2272 * order of the remaining elements in the range @p [middle,last) is
2274 * After the sort if @p i and @j are iterators in the range
2275 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2276 * the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
2278 template<typename _RandomAccessIterator
>
2280 partial_sort(_RandomAccessIterator __first
,
2281 _RandomAccessIterator __middle
,
2282 _RandomAccessIterator __last
)
2284 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2287 // concept requirements
2288 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2289 _RandomAccessIterator
>)
2290 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2291 __glibcxx_requires_valid_range(__first
, __middle
);
2292 __glibcxx_requires_valid_range(__middle
, __last
);
2294 std::make_heap(__first
, __middle
);
2295 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
2296 if (*__i
< *__first
)
2297 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
));
2298 std::sort_heap(__first
, __middle
);
2302 * @brief Sort the smallest elements of a sequence using a predicate
2304 * @param first An iterator.
2305 * @param middle Another iterator.
2306 * @param last Another iterator.
2307 * @param comp A comparison functor.
2310 * Sorts the smallest @p (middle-first) elements in the range
2311 * @p [first,last) and moves them to the range @p [first,middle). The
2312 * order of the remaining elements in the range @p [middle,last) is
2314 * After the sort if @p i and @j are iterators in the range
2315 * @p [first,middle) such that @i precedes @j and @k is an iterator in
2316 * the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
2319 template<typename _RandomAccessIterator
, typename _Compare
>
2321 partial_sort(_RandomAccessIterator __first
,
2322 _RandomAccessIterator __middle
,
2323 _RandomAccessIterator __last
,
2326 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2329 // concept requirements
2330 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2331 _RandomAccessIterator
>)
2332 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2333 _ValueType
, _ValueType
>)
2334 __glibcxx_requires_valid_range(__first
, __middle
);
2335 __glibcxx_requires_valid_range(__middle
, __last
);
2337 std::make_heap(__first
, __middle
, __comp
);
2338 for (_RandomAccessIterator __i
= __middle
; __i
< __last
; ++__i
)
2339 if (__comp(*__i
, *__first
))
2340 std::__pop_heap(__first
, __middle
, __i
, _ValueType(*__i
), __comp
);
2341 std::sort_heap(__first
, __middle
, __comp
);
2345 * @brief Copy the smallest elements of a sequence.
2346 * @param first An iterator.
2347 * @param last Another iterator.
2348 * @param result_first A random-access iterator.
2349 * @param result_last Another random-access iterator.
2350 * @return An iterator indicating the end of the resulting sequence.
2352 * Copies and sorts the smallest N values from the range @p [first,last)
2353 * to the range beginning at @p result_first, where the number of
2354 * elements to be copied, @p N, is the smaller of @p (last-first) and
2355 * @p (result_last-result_first).
2356 * After the sort if @p i and @j are iterators in the range
2357 * @p [result_first,result_first+N) such that @i precedes @j then
2358 * @p *j<*i is false.
2359 * The value returned is @p result_first+N.
2361 template<typename _InputIterator
, typename _RandomAccessIterator
>
2362 _RandomAccessIterator
2363 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2364 _RandomAccessIterator __result_first
,
2365 _RandomAccessIterator __result_last
)
2367 typedef typename iterator_traits
<_InputIterator
>::value_type
2369 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2371 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2374 // concept requirements
2375 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2376 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2378 __glibcxx_function_requires(_LessThanComparableConcept
<_OutputValueType
>)
2379 __glibcxx_function_requires(_LessThanComparableConcept
<_InputValueType
>)
2380 __glibcxx_requires_valid_range(__first
, __last
);
2381 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2383 if (__result_first
== __result_last
)
2384 return __result_last
;
2385 _RandomAccessIterator __result_real_last
= __result_first
;
2386 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2388 *__result_real_last
= *__first
;
2389 ++__result_real_last
;
2392 std::make_heap(__result_first
, __result_real_last
);
2393 while (__first
!= __last
)
2395 if (*__first
< *__result_first
)
2396 std::__adjust_heap(__result_first
, _DistanceType(0),
2397 _DistanceType(__result_real_last
2399 _InputValueType(*__first
));
2402 std::sort_heap(__result_first
, __result_real_last
);
2403 return __result_real_last
;
2407 * @brief Copy the smallest elements of a sequence using a predicate for
2409 * @param first An input iterator.
2410 * @param last Another input iterator.
2411 * @param result_first A random-access iterator.
2412 * @param result_last Another random-access iterator.
2413 * @param comp A comparison functor.
2414 * @return An iterator indicating the end of the resulting sequence.
2416 * Copies and sorts the smallest N values from the range @p [first,last)
2417 * to the range beginning at @p result_first, where the number of
2418 * elements to be copied, @p N, is the smaller of @p (last-first) and
2419 * @p (result_last-result_first).
2420 * After the sort if @p i and @j are iterators in the range
2421 * @p [result_first,result_first+N) such that @i precedes @j then
2422 * @p comp(*j,*i) is false.
2423 * The value returned is @p result_first+N.
2425 template<typename _InputIterator
, typename _RandomAccessIterator
, typename _Compare
>
2426 _RandomAccessIterator
2427 partial_sort_copy(_InputIterator __first
, _InputIterator __last
,
2428 _RandomAccessIterator __result_first
,
2429 _RandomAccessIterator __result_last
,
2432 typedef typename iterator_traits
<_InputIterator
>::value_type
2434 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2436 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
2439 // concept requirements
2440 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
2441 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2442 _RandomAccessIterator
>)
2443 __glibcxx_function_requires(_ConvertibleConcept
<_InputValueType
,
2445 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2446 _OutputValueType
, _OutputValueType
>)
2447 __glibcxx_requires_valid_range(__first
, __last
);
2448 __glibcxx_requires_valid_range(__result_first
, __result_last
);
2450 if (__result_first
== __result_last
)
2451 return __result_last
;
2452 _RandomAccessIterator __result_real_last
= __result_first
;
2453 while(__first
!= __last
&& __result_real_last
!= __result_last
)
2455 *__result_real_last
= *__first
;
2456 ++__result_real_last
;
2459 std::make_heap(__result_first
, __result_real_last
, __comp
);
2460 while (__first
!= __last
)
2462 if (__comp(*__first
, *__result_first
))
2463 std::__adjust_heap(__result_first
, _DistanceType(0),
2464 _DistanceType(__result_real_last
2466 _InputValueType(*__first
),
2470 std::sort_heap(__result_first
, __result_real_last
, __comp
);
2471 return __result_real_last
;
2476 * This is a helper function for the sort routine.
2479 template<typename _RandomAccessIterator
, typename _Size
>
2481 __introsort_loop(_RandomAccessIterator __first
,
2482 _RandomAccessIterator __last
,
2483 _Size __depth_limit
)
2485 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2488 while (__last
- __first
> int(_S_threshold
))
2490 if (__depth_limit
== 0)
2492 std::partial_sort(__first
, __last
, __last
);
2496 _RandomAccessIterator __cut
=
2497 std::__unguarded_partition(__first
, __last
,
2498 _ValueType(std::__median(*__first
,
2505 std::__introsort_loop(__cut
, __last
, __depth_limit
);
2512 * This is a helper function for the sort routine.
2515 template<typename _RandomAccessIterator
, typename _Size
, typename _Compare
>
2517 __introsort_loop(_RandomAccessIterator __first
,
2518 _RandomAccessIterator __last
,
2519 _Size __depth_limit
, _Compare __comp
)
2521 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2524 while (__last
- __first
> int(_S_threshold
))
2526 if (__depth_limit
== 0)
2528 std::partial_sort(__first
, __last
, __last
, __comp
);
2532 _RandomAccessIterator __cut
=
2533 std::__unguarded_partition(__first
, __last
,
2534 _ValueType(std::__median(*__first
,
2542 std::__introsort_loop(__cut
, __last
, __depth_limit
, __comp
);
2548 * @brief Sort the elements of a sequence.
2549 * @param first An iterator.
2550 * @param last Another iterator.
2553 * Sorts the elements in the range @p [first,last) in ascending order,
2554 * such that @p *(i+1)<*i is false for each iterator @p i in the range
2555 * @p [first,last-1).
2557 * The relative ordering of equivalent elements is not preserved, use
2558 * @p stable_sort() if this is needed.
2560 template<typename _RandomAccessIterator
>
2562 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
2564 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2567 // concept requirements
2568 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2569 _RandomAccessIterator
>)
2570 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
2571 __glibcxx_requires_valid_range(__first
, __last
);
2573 if (__first
!= __last
)
2575 std::__introsort_loop(__first
, __last
, __lg(__last
- __first
) * 2);
2576 std::__final_insertion_sort(__first
, __last
);
2581 * @brief Sort the elements of a sequence using a predicate for comparison.
2582 * @param first An iterator.
2583 * @param last Another iterator.
2584 * @param comp A comparison functor.
2587 * Sorts the elements in the range @p [first,last) in ascending order,
2588 * such that @p comp(*(i+1),*i) is false for every iterator @p i in the
2589 * range @p [first,last-1).
2591 * The relative ordering of equivalent elements is not preserved, use
2592 * @p stable_sort() if this is needed.
2594 template<typename _RandomAccessIterator
, typename _Compare
>
2596 sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
2599 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
2602 // concept requirements
2603 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
2604 _RandomAccessIterator
>)
2605 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _ValueType
,
2607 __glibcxx_requires_valid_range(__first
, __last
);
2609 if (__first
!= __last
)
2611 std::__introsort_loop(__first
, __last
, __lg(__last
- __first
) * 2,
2613 std::__final_insertion_sort(__first
, __last
, __comp
);
2618 * @brief Finds the first position in which @a val could be inserted
2619 * without changing the ordering.
2620 * @param first An iterator.
2621 * @param last Another iterator.
2622 * @param val The search term.
2623 * @return An iterator pointing to the first element "not less than" @a val,
2624 * or end() if every element is less than @a val.
2625 * @ingroup binarysearch
2627 template<typename _ForwardIterator
, typename _Tp
>
2629 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2632 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2634 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2637 // concept requirements
2638 // Note that these are slightly stricter than those of the 4-argument
2639 // version, defined next. The difference is in the strictness of the
2640 // comparison operations... so for looser checking, define your own
2641 // comparison function, as was intended.
2642 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2643 __glibcxx_function_requires(_SameTypeConcept
<_Tp
, _ValueType
>)
2644 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
2645 __glibcxx_requires_partitioned(__first
, __last
, __val
);
2647 _DistanceType __len
= std::distance(__first
, __last
);
2648 _DistanceType __half
;
2649 _ForwardIterator __middle
;
2653 __half
= __len
>> 1;
2655 std::advance(__middle
, __half
);
2656 if (*__middle
< __val
)
2660 __len
= __len
- __half
- 1;
2669 * @brief Finds the first position in which @a val could be inserted
2670 * without changing the ordering.
2671 * @param first An iterator.
2672 * @param last Another iterator.
2673 * @param val The search term.
2674 * @param comp A functor to use for comparisons.
2675 * @return An iterator pointing to the first element "not less than" @a val,
2676 * or end() if every element is less than @a val.
2677 * @ingroup binarysearch
2679 * The comparison function should have the same effects on ordering as
2680 * the function used for the initial sort.
2682 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2684 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2685 const _Tp
& __val
, _Compare __comp
)
2687 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2689 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2692 // concept requirements
2693 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2694 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2696 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
2698 _DistanceType __len
= std::distance(__first
, __last
);
2699 _DistanceType __half
;
2700 _ForwardIterator __middle
;
2704 __half
= __len
>> 1;
2706 std::advance(__middle
, __half
);
2707 if (__comp(*__middle
, __val
))
2711 __len
= __len
- __half
- 1;
2720 * @brief Finds the last position in which @a val could be inserted
2721 * without changing the ordering.
2722 * @param first An iterator.
2723 * @param last Another iterator.
2724 * @param val The search term.
2725 * @return An iterator pointing to the first element greater than @a val,
2726 * or end() if no elements are greater than @a val.
2727 * @ingroup binarysearch
2729 template<typename _ForwardIterator
, typename _Tp
>
2731 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2734 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2736 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2739 // concept requirements
2740 // See comments on lower_bound.
2741 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2742 __glibcxx_function_requires(_SameTypeConcept
<_Tp
, _ValueType
>)
2743 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
2744 __glibcxx_requires_partitioned(__first
, __last
, __val
);
2746 _DistanceType __len
= std::distance(__first
, __last
);
2747 _DistanceType __half
;
2748 _ForwardIterator __middle
;
2752 __half
= __len
>> 1;
2754 std::advance(__middle
, __half
);
2755 if (__val
< *__middle
)
2761 __len
= __len
- __half
- 1;
2768 * @brief Finds the last position in which @a val could be inserted
2769 * without changing the ordering.
2770 * @param first An iterator.
2771 * @param last Another iterator.
2772 * @param val The search term.
2773 * @param comp A functor to use for comparisons.
2774 * @return An iterator pointing to the first element greater than @a val,
2775 * or end() if no elements are greater than @a val.
2776 * @ingroup binarysearch
2778 * The comparison function should have the same effects on ordering as
2779 * the function used for the initial sort.
2781 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
2783 upper_bound(_ForwardIterator __first
, _ForwardIterator __last
,
2784 const _Tp
& __val
, _Compare __comp
)
2786 typedef typename iterator_traits
<_ForwardIterator
>::value_type
2788 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
2791 // concept requirements
2792 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
2793 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
2795 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
2797 _DistanceType __len
= std::distance(__first
, __last
);
2798 _DistanceType __half
;
2799 _ForwardIterator __middle
;
2803 __half
= __len
>> 1;
2805 std::advance(__middle
, __half
);
2806 if (__comp(__val
, *__middle
))
2812 __len
= __len
- __half
- 1;
2820 * This is a helper function for the merge routines.
2823 template<typename _BidirectionalIterator
, typename _Distance
>
2825 __merge_without_buffer(_BidirectionalIterator __first
,
2826 _BidirectionalIterator __middle
,
2827 _BidirectionalIterator __last
,
2828 _Distance __len1
, _Distance __len2
)
2830 if (__len1
== 0 || __len2
== 0)
2832 if (__len1
+ __len2
== 2)
2834 if (*__middle
< *__first
)
2835 std::iter_swap(__first
, __middle
);
2838 _BidirectionalIterator __first_cut
= __first
;
2839 _BidirectionalIterator __second_cut
= __middle
;
2840 _Distance __len11
= 0;
2841 _Distance __len22
= 0;
2842 if (__len1
> __len2
)
2844 __len11
= __len1
/ 2;
2845 std::advance(__first_cut
, __len11
);
2846 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
);
2847 __len22
= std::distance(__middle
, __second_cut
);
2851 __len22
= __len2
/ 2;
2852 std::advance(__second_cut
, __len22
);
2853 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
);
2854 __len11
= std::distance(__first
, __first_cut
);
2856 std::rotate(__first_cut
, __middle
, __second_cut
);
2857 _BidirectionalIterator __new_middle
= __first_cut
;
2858 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2859 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2861 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2862 __len1
- __len11
, __len2
- __len22
);
2867 * This is a helper function for the merge routines.
2870 template<typename _BidirectionalIterator
, typename _Distance
,
2873 __merge_without_buffer(_BidirectionalIterator __first
,
2874 _BidirectionalIterator __middle
,
2875 _BidirectionalIterator __last
,
2876 _Distance __len1
, _Distance __len2
,
2879 if (__len1
== 0 || __len2
== 0)
2881 if (__len1
+ __len2
== 2)
2883 if (__comp(*__middle
, *__first
))
2884 std::iter_swap(__first
, __middle
);
2887 _BidirectionalIterator __first_cut
= __first
;
2888 _BidirectionalIterator __second_cut
= __middle
;
2889 _Distance __len11
= 0;
2890 _Distance __len22
= 0;
2891 if (__len1
> __len2
)
2893 __len11
= __len1
/ 2;
2894 std::advance(__first_cut
, __len11
);
2895 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
2897 __len22
= std::distance(__middle
, __second_cut
);
2901 __len22
= __len2
/ 2;
2902 std::advance(__second_cut
, __len22
);
2903 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
2905 __len11
= std::distance(__first
, __first_cut
);
2907 std::rotate(__first_cut
, __middle
, __second_cut
);
2908 _BidirectionalIterator __new_middle
= __first_cut
;
2909 std::advance(__new_middle
, std::distance(__middle
, __second_cut
));
2910 std::__merge_without_buffer(__first
, __first_cut
, __new_middle
,
2911 __len11
, __len22
, __comp
);
2912 std::__merge_without_buffer(__new_middle
, __second_cut
, __last
,
2913 __len1
- __len11
, __len2
- __len22
, __comp
);
2918 * This is a helper function for the stable sorting routines.
2921 template<typename _RandomAccessIterator
>
2923 __inplace_stable_sort(_RandomAccessIterator __first
,
2924 _RandomAccessIterator __last
)
2926 if (__last
- __first
< 15)
2928 std::__insertion_sort(__first
, __last
);
2931 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2932 std::__inplace_stable_sort(__first
, __middle
);
2933 std::__inplace_stable_sort(__middle
, __last
);
2934 std::__merge_without_buffer(__first
, __middle
, __last
,
2941 * This is a helper function for the stable sorting routines.
2944 template<typename _RandomAccessIterator
, typename _Compare
>
2946 __inplace_stable_sort(_RandomAccessIterator __first
,
2947 _RandomAccessIterator __last
, _Compare __comp
)
2949 if (__last
- __first
< 15)
2951 std::__insertion_sort(__first
, __last
, __comp
);
2954 _RandomAccessIterator __middle
= __first
+ (__last
- __first
) / 2;
2955 std::__inplace_stable_sort(__first
, __middle
, __comp
);
2956 std::__inplace_stable_sort(__middle
, __last
, __comp
);
2957 std::__merge_without_buffer(__first
, __middle
, __last
,
2964 * @brief Merges two sorted ranges.
2965 * @param first1 An iterator.
2966 * @param first2 Another iterator.
2967 * @param last1 Another iterator.
2968 * @param last2 Another iterator.
2969 * @param result An iterator pointing to the end of the merged range.
2970 * @return An iterator pointing to the first element "not less than" @a val.
2972 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
2973 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
2974 * must be sorted, and the output range must not overlap with either of
2975 * the input ranges. The sort is @e stable, that is, for equivalent
2976 * elements in the two ranges, elements from the first range will always
2977 * come before elements from the second.
2979 template<typename _InputIterator1
, typename _InputIterator2
,
2980 typename _OutputIterator
>
2982 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
2983 _InputIterator2 __first2
, _InputIterator2 __last2
,
2984 _OutputIterator __result
)
2986 // concept requirements
2987 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
2988 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
2989 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
2990 typename iterator_traits
<_InputIterator1
>::value_type
>)
2991 __glibcxx_function_requires(_SameTypeConcept
<
2992 typename iterator_traits
<_InputIterator1
>::value_type
,
2993 typename iterator_traits
<_InputIterator2
>::value_type
>)
2994 __glibcxx_function_requires(_LessThanComparableConcept
<
2995 typename iterator_traits
<_InputIterator1
>::value_type
>)
2996 __glibcxx_requires_sorted(__first1
, __last1
);
2997 __glibcxx_requires_sorted(__first2
, __last2
);
2999 while (__first1
!= __last1
&& __first2
!= __last2
)
3001 if (*__first2
< *__first1
)
3003 *__result
= *__first2
;
3008 *__result
= *__first1
;
3013 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
3018 * @brief Merges two sorted ranges.
3019 * @param first1 An iterator.
3020 * @param first2 Another iterator.
3021 * @param last1 Another iterator.
3022 * @param last2 Another iterator.
3023 * @param result An iterator pointing to the end of the merged range.
3024 * @param comp A functor to use for comparisons.
3025 * @return An iterator pointing to the first element "not less than" @a val.
3027 * Merges the ranges [first1,last1) and [first2,last2) into the sorted range
3028 * [result, result + (last1-first1) + (last2-first2)). Both input ranges
3029 * must be sorted, and the output range must not overlap with either of
3030 * the input ranges. The sort is @e stable, that is, for equivalent
3031 * elements in the two ranges, elements from the first range will always
3032 * come before elements from the second.
3034 * The comparison function should have the same effects on ordering as
3035 * the function used for the initial sort.
3037 template<typename _InputIterator1
, typename _InputIterator2
,
3038 typename _OutputIterator
, typename _Compare
>
3040 merge(_InputIterator1 __first1
, _InputIterator1 __last1
,
3041 _InputIterator2 __first2
, _InputIterator2 __last2
,
3042 _OutputIterator __result
, _Compare __comp
)
3044 // concept requirements
3045 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
3046 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
3047 __glibcxx_function_requires(_SameTypeConcept
<
3048 typename iterator_traits
<_InputIterator1
>::value_type
,
3049 typename iterator_traits
<_InputIterator2
>::value_type
>)
3050 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
3051 typename iterator_traits
<_InputIterator1
>::value_type
>)
3052 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3053 typename iterator_traits
<_InputIterator1
>::value_type
,
3054 typename iterator_traits
<_InputIterator2
>::value_type
>)
3055 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
3056 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
3058 while (__first1
!= __last1
&& __first2
!= __last2
)
3060 if (__comp(*__first2
, *__first1
))
3062 *__result
= *__first2
;
3067 *__result
= *__first1
;
3072 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
3076 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3079 __merge_sort_loop(_RandomAccessIterator1 __first
,
3080 _RandomAccessIterator1 __last
,
3081 _RandomAccessIterator2 __result
,
3082 _Distance __step_size
)
3084 const _Distance __two_step
= 2 * __step_size
;
3086 while (__last
- __first
>= __two_step
)
3088 __result
= std::merge(__first
, __first
+ __step_size
,
3089 __first
+ __step_size
, __first
+ __two_step
,
3091 __first
+= __two_step
;
3094 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3095 std::merge(__first
, __first
+ __step_size
, __first
+ __step_size
, __last
,
3099 template<typename _RandomAccessIterator1
, typename _RandomAccessIterator2
,
3100 typename _Distance
, typename _Compare
>
3102 __merge_sort_loop(_RandomAccessIterator1 __first
,
3103 _RandomAccessIterator1 __last
,
3104 _RandomAccessIterator2 __result
, _Distance __step_size
,
3107 const _Distance __two_step
= 2 * __step_size
;
3109 while (__last
- __first
>= __two_step
)
3111 __result
= std::merge(__first
, __first
+ __step_size
,
3112 __first
+ __step_size
, __first
+ __two_step
,
3115 __first
+= __two_step
;
3117 __step_size
= std::min(_Distance(__last
- __first
), __step_size
);
3119 std::merge(__first
, __first
+ __step_size
,
3120 __first
+ __step_size
, __last
,
3125 enum { _S_chunk_size
= 7 };
3127 template<typename _RandomAccessIterator
, typename _Distance
>
3129 __chunk_insertion_sort(_RandomAccessIterator __first
,
3130 _RandomAccessIterator __last
,
3131 _Distance __chunk_size
)
3133 while (__last
- __first
>= __chunk_size
)
3135 std::__insertion_sort(__first
, __first
+ __chunk_size
);
3136 __first
+= __chunk_size
;
3138 std::__insertion_sort(__first
, __last
);
3141 template<typename _RandomAccessIterator
, typename _Distance
, typename _Compare
>
3143 __chunk_insertion_sort(_RandomAccessIterator __first
,
3144 _RandomAccessIterator __last
,
3145 _Distance __chunk_size
, _Compare __comp
)
3147 while (__last
- __first
>= __chunk_size
)
3149 std::__insertion_sort(__first
, __first
+ __chunk_size
, __comp
);
3150 __first
+= __chunk_size
;
3152 std::__insertion_sort(__first
, __last
, __comp
);
3155 template<typename _RandomAccessIterator
, typename _Pointer
>
3157 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3158 _RandomAccessIterator __last
,
3161 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3164 const _Distance __len
= __last
- __first
;
3165 const _Pointer __buffer_last
= __buffer
+ __len
;
3167 _Distance __step_size
= _S_chunk_size
;
3168 std::__chunk_insertion_sort(__first
, __last
, __step_size
);
3170 while (__step_size
< __len
)
3172 std::__merge_sort_loop(__first
, __last
, __buffer
, __step_size
);
3174 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
, __step_size
);
3179 template<typename _RandomAccessIterator
, typename _Pointer
, typename _Compare
>
3181 __merge_sort_with_buffer(_RandomAccessIterator __first
,
3182 _RandomAccessIterator __last
,
3183 _Pointer __buffer
, _Compare __comp
)
3185 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3188 const _Distance __len
= __last
- __first
;
3189 const _Pointer __buffer_last
= __buffer
+ __len
;
3191 _Distance __step_size
= _S_chunk_size
;
3192 std::__chunk_insertion_sort(__first
, __last
, __step_size
, __comp
);
3194 while (__step_size
< __len
)
3196 std::__merge_sort_loop(__first
, __last
, __buffer
,
3197 __step_size
, __comp
);
3199 std::__merge_sort_loop(__buffer
, __buffer_last
, __first
,
3200 __step_size
, __comp
);
3207 * This is a helper function for the merge routines.
3210 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3211 typename _BidirectionalIterator3
>
3212 _BidirectionalIterator3
3213 __merge_backward(_BidirectionalIterator1 __first1
,
3214 _BidirectionalIterator1 __last1
,
3215 _BidirectionalIterator2 __first2
,
3216 _BidirectionalIterator2 __last2
,
3217 _BidirectionalIterator3 __result
)
3219 if (__first1
== __last1
)
3220 return std::copy_backward(__first2
, __last2
, __result
);
3221 if (__first2
== __last2
)
3222 return std::copy_backward(__first1
, __last1
, __result
);
3227 if (*__last2
< *__last1
)
3229 *--__result
= *__last1
;
3230 if (__first1
== __last1
)
3231 return std::copy_backward(__first2
, ++__last2
, __result
);
3236 *--__result
= *__last2
;
3237 if (__first2
== __last2
)
3238 return std::copy_backward(__first1
, ++__last1
, __result
);
3246 * This is a helper function for the merge routines.
3249 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3250 typename _BidirectionalIterator3
, typename _Compare
>
3251 _BidirectionalIterator3
3252 __merge_backward(_BidirectionalIterator1 __first1
,
3253 _BidirectionalIterator1 __last1
,
3254 _BidirectionalIterator2 __first2
,
3255 _BidirectionalIterator2 __last2
,
3256 _BidirectionalIterator3 __result
,
3259 if (__first1
== __last1
)
3260 return std::copy_backward(__first2
, __last2
, __result
);
3261 if (__first2
== __last2
)
3262 return std::copy_backward(__first1
, __last1
, __result
);
3267 if (__comp(*__last2
, *__last1
))
3269 *--__result
= *__last1
;
3270 if (__first1
== __last1
)
3271 return std::copy_backward(__first2
, ++__last2
, __result
);
3276 *--__result
= *__last2
;
3277 if (__first2
== __last2
)
3278 return std::copy_backward(__first1
, ++__last1
, __result
);
3286 * This is a helper function for the merge routines.
3289 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
3291 _BidirectionalIterator1
3292 __rotate_adaptive(_BidirectionalIterator1 __first
,
3293 _BidirectionalIterator1 __middle
,
3294 _BidirectionalIterator1 __last
,
3295 _Distance __len1
, _Distance __len2
,
3296 _BidirectionalIterator2 __buffer
,
3297 _Distance __buffer_size
)
3299 _BidirectionalIterator2 __buffer_end
;
3300 if (__len1
> __len2
&& __len2
<= __buffer_size
)
3302 __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3303 std::copy_backward(__first
, __middle
, __last
);
3304 return std::copy(__buffer
, __buffer_end
, __first
);
3306 else if (__len1
<= __buffer_size
)
3308 __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3309 std::copy(__middle
, __last
, __first
);
3310 return std::copy_backward(__buffer
, __buffer_end
, __last
);
3314 std::rotate(__first
, __middle
, __last
);
3315 std::advance(__first
, std::distance(__middle
, __last
));
3322 * This is a helper function for the merge routines.
3325 template<typename _BidirectionalIterator
, typename _Distance
,
3328 __merge_adaptive(_BidirectionalIterator __first
,
3329 _BidirectionalIterator __middle
,
3330 _BidirectionalIterator __last
,
3331 _Distance __len1
, _Distance __len2
,
3332 _Pointer __buffer
, _Distance __buffer_size
)
3334 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
3336 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3337 std::merge(__buffer
, __buffer_end
, __middle
, __last
, __first
);
3339 else if (__len2
<= __buffer_size
)
3341 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3342 std::__merge_backward(__first
, __middle
, __buffer
,
3343 __buffer_end
, __last
);
3347 _BidirectionalIterator __first_cut
= __first
;
3348 _BidirectionalIterator __second_cut
= __middle
;
3349 _Distance __len11
= 0;
3350 _Distance __len22
= 0;
3351 if (__len1
> __len2
)
3353 __len11
= __len1
/ 2;
3354 std::advance(__first_cut
, __len11
);
3355 __second_cut
= std::lower_bound(__middle
, __last
,
3357 __len22
= std::distance(__middle
, __second_cut
);
3361 __len22
= __len2
/ 2;
3362 std::advance(__second_cut
, __len22
);
3363 __first_cut
= std::upper_bound(__first
, __middle
,
3365 __len11
= std::distance(__first
, __first_cut
);
3367 _BidirectionalIterator __new_middle
=
3368 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3369 __len1
- __len11
, __len22
, __buffer
,
3371 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3372 __len22
, __buffer
, __buffer_size
);
3373 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3375 __len2
- __len22
, __buffer
, __buffer_size
);
3381 * This is a helper function for the merge routines.
3384 template<typename _BidirectionalIterator
, typename _Distance
, typename _Pointer
,
3387 __merge_adaptive(_BidirectionalIterator __first
,
3388 _BidirectionalIterator __middle
,
3389 _BidirectionalIterator __last
,
3390 _Distance __len1
, _Distance __len2
,
3391 _Pointer __buffer
, _Distance __buffer_size
,
3394 if (__len1
<= __len2
&& __len1
<= __buffer_size
)
3396 _Pointer __buffer_end
= std::copy(__first
, __middle
, __buffer
);
3397 std::merge(__buffer
, __buffer_end
, __middle
, __last
, __first
, __comp
);
3399 else if (__len2
<= __buffer_size
)
3401 _Pointer __buffer_end
= std::copy(__middle
, __last
, __buffer
);
3402 std::__merge_backward(__first
, __middle
, __buffer
, __buffer_end
,
3407 _BidirectionalIterator __first_cut
= __first
;
3408 _BidirectionalIterator __second_cut
= __middle
;
3409 _Distance __len11
= 0;
3410 _Distance __len22
= 0;
3411 if (__len1
> __len2
)
3413 __len11
= __len1
/ 2;
3414 std::advance(__first_cut
, __len11
);
3415 __second_cut
= std::lower_bound(__middle
, __last
, *__first_cut
,
3417 __len22
= std::distance(__middle
, __second_cut
);
3421 __len22
= __len2
/ 2;
3422 std::advance(__second_cut
, __len22
);
3423 __first_cut
= std::upper_bound(__first
, __middle
, *__second_cut
,
3425 __len11
= std::distance(__first
, __first_cut
);
3427 _BidirectionalIterator __new_middle
=
3428 std::__rotate_adaptive(__first_cut
, __middle
, __second_cut
,
3429 __len1
- __len11
, __len22
, __buffer
,
3431 std::__merge_adaptive(__first
, __first_cut
, __new_middle
, __len11
,
3432 __len22
, __buffer
, __buffer_size
, __comp
);
3433 std::__merge_adaptive(__new_middle
, __second_cut
, __last
,
3435 __len2
- __len22
, __buffer
,
3436 __buffer_size
, __comp
);
3441 * @brief Merges two sorted ranges in place.
3442 * @param first An iterator.
3443 * @param middle Another iterator.
3444 * @param last Another iterator.
3447 * Merges two sorted and consecutive ranges, [first,middle) and
3448 * [middle,last), and puts the result in [first,last). The output will
3449 * be sorted. The sort is @e stable, that is, for equivalent
3450 * elements in the two ranges, elements from the first range will always
3451 * come before elements from the second.
3453 * If enough additional memory is available, this takes (last-first)-1
3454 * comparisons. Otherwise an NlogN algorithm is used, where N is
3455 * distance(first,last).
3457 template<typename _BidirectionalIterator
>
3459 inplace_merge(_BidirectionalIterator __first
,
3460 _BidirectionalIterator __middle
,
3461 _BidirectionalIterator __last
)
3463 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3465 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3468 // concept requirements
3469 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3470 _BidirectionalIterator
>)
3471 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3472 __glibcxx_requires_sorted(__first
, __middle
);
3473 __glibcxx_requires_sorted(__middle
, __last
);
3475 if (__first
== __middle
|| __middle
== __last
)
3478 _DistanceType __len1
= std::distance(__first
, __middle
);
3479 _DistanceType __len2
= std::distance(__middle
, __last
);
3481 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3483 if (__buf
.begin() == 0)
3484 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
, __len2
);
3486 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3487 __buf
.begin(), _DistanceType(__buf
.size()));
3491 * @brief Merges two sorted ranges in place.
3492 * @param first An iterator.
3493 * @param middle Another iterator.
3494 * @param last Another iterator.
3495 * @param comp A functor to use for comparisons.
3498 * Merges two sorted and consecutive ranges, [first,middle) and
3499 * [middle,last), and puts the result in [first,last). The output will
3500 * be sorted. The sort is @e stable, that is, for equivalent
3501 * elements in the two ranges, elements from the first range will always
3502 * come before elements from the second.
3504 * If enough additional memory is available, this takes (last-first)-1
3505 * comparisons. Otherwise an NlogN algorithm is used, where N is
3506 * distance(first,last).
3508 * The comparison function should have the same effects on ordering as
3509 * the function used for the initial sort.
3511 template<typename _BidirectionalIterator
, typename _Compare
>
3513 inplace_merge(_BidirectionalIterator __first
,
3514 _BidirectionalIterator __middle
,
3515 _BidirectionalIterator __last
,
3518 typedef typename iterator_traits
<_BidirectionalIterator
>::value_type
3520 typedef typename iterator_traits
<_BidirectionalIterator
>::difference_type
3523 // concept requirements
3524 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<
3525 _BidirectionalIterator
>)
3526 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3527 _ValueType
, _ValueType
>)
3528 __glibcxx_requires_sorted_pred(__first
, __middle
, __comp
);
3529 __glibcxx_requires_sorted_pred(__middle
, __last
, __comp
);
3531 if (__first
== __middle
|| __middle
== __last
)
3534 const _DistanceType __len1
= std::distance(__first
, __middle
);
3535 const _DistanceType __len2
= std::distance(__middle
, __last
);
3537 _Temporary_buffer
<_BidirectionalIterator
, _ValueType
> __buf(__first
,
3539 if (__buf
.begin() == 0)
3540 std::__merge_without_buffer(__first
, __middle
, __last
, __len1
,
3543 std::__merge_adaptive(__first
, __middle
, __last
, __len1
, __len2
,
3544 __buf
.begin(), _DistanceType(__buf
.size()),
3548 template<typename _RandomAccessIterator
, typename _Pointer
,
3551 __stable_sort_adaptive(_RandomAccessIterator __first
,
3552 _RandomAccessIterator __last
,
3553 _Pointer __buffer
, _Distance __buffer_size
)
3555 const _Distance __len
= (__last
- __first
+ 1) / 2;
3556 const _RandomAccessIterator __middle
= __first
+ __len
;
3557 if (__len
> __buffer_size
)
3559 std::__stable_sort_adaptive(__first
, __middle
,
3560 __buffer
, __buffer_size
);
3561 std::__stable_sort_adaptive(__middle
, __last
,
3562 __buffer
, __buffer_size
);
3566 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
);
3567 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
);
3569 std::__merge_adaptive(__first
, __middle
, __last
,
3570 _Distance(__middle
- __first
),
3571 _Distance(__last
- __middle
),
3572 __buffer
, __buffer_size
);
3575 template<typename _RandomAccessIterator
, typename _Pointer
,
3576 typename _Distance
, typename _Compare
>
3578 __stable_sort_adaptive(_RandomAccessIterator __first
,
3579 _RandomAccessIterator __last
,
3580 _Pointer __buffer
, _Distance __buffer_size
,
3583 const _Distance __len
= (__last
- __first
+ 1) / 2;
3584 const _RandomAccessIterator __middle
= __first
+ __len
;
3585 if (__len
> __buffer_size
)
3587 std::__stable_sort_adaptive(__first
, __middle
, __buffer
,
3588 __buffer_size
, __comp
);
3589 std::__stable_sort_adaptive(__middle
, __last
, __buffer
,
3590 __buffer_size
, __comp
);
3594 std::__merge_sort_with_buffer(__first
, __middle
, __buffer
, __comp
);
3595 std::__merge_sort_with_buffer(__middle
, __last
, __buffer
, __comp
);
3597 std::__merge_adaptive(__first
, __middle
, __last
,
3598 _Distance(__middle
- __first
),
3599 _Distance(__last
- __middle
),
3600 __buffer
, __buffer_size
,
3605 * @brief Sort the elements of a sequence, preserving the relative order
3606 * of equivalent elements.
3607 * @param first An iterator.
3608 * @param last Another iterator.
3611 * Sorts the elements in the range @p [first,last) in ascending order,
3612 * such that @p *(i+1)<*i is false for each iterator @p i in the range
3613 * @p [first,last-1).
3615 * The relative ordering of equivalent elements is preserved, so any two
3616 * elements @p x and @p y in the range @p [first,last) such that
3617 * @p x<y is false and @p y<x is false will have the same relative
3618 * ordering after calling @p stable_sort().
3620 template<typename _RandomAccessIterator
>
3622 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
)
3624 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3626 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3629 // concept requirements
3630 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3631 _RandomAccessIterator
>)
3632 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3633 __glibcxx_requires_valid_range(__first
, __last
);
3635 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
>
3636 buf(__first
, __last
);
3637 if (buf
.begin() == 0)
3638 std::__inplace_stable_sort(__first
, __last
);
3640 std::__stable_sort_adaptive(__first
, __last
, buf
.begin(),
3641 _DistanceType(buf
.size()));
3645 * @brief Sort the elements of a sequence using a predicate for comparison,
3646 * preserving the relative order of equivalent elements.
3647 * @param first An iterator.
3648 * @param last Another iterator.
3649 * @param comp A comparison functor.
3652 * Sorts the elements in the range @p [first,last) in ascending order,
3653 * such that @p comp(*(i+1),*i) is false for each iterator @p i in the
3654 * range @p [first,last-1).
3656 * The relative ordering of equivalent elements is preserved, so any two
3657 * elements @p x and @p y in the range @p [first,last) such that
3658 * @p comp(x,y) is false and @p comp(y,x) is false will have the same
3659 * relative ordering after calling @p stable_sort().
3661 template<typename _RandomAccessIterator
, typename _Compare
>
3663 stable_sort(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
3666 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3668 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
3671 // concept requirements
3672 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3673 _RandomAccessIterator
>)
3674 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3677 __glibcxx_requires_valid_range(__first
, __last
);
3679 _Temporary_buffer
<_RandomAccessIterator
, _ValueType
> buf(__first
, __last
);
3680 if (buf
.begin() == 0)
3681 std::__inplace_stable_sort(__first
, __last
, __comp
);
3683 std::__stable_sort_adaptive(__first
, __last
, buf
.begin(),
3684 _DistanceType(buf
.size()), __comp
);
3688 * @brief Sort a sequence just enough to find a particular position.
3689 * @param first An iterator.
3690 * @param nth Another iterator.
3691 * @param last Another iterator.
3694 * Rearranges the elements in the range @p [first,last) so that @p *nth
3695 * is the same element that would have been in that position had the
3696 * whole sequence been sorted.
3697 * whole sequence been sorted. The elements either side of @p *nth are
3698 * not completely sorted, but for any iterator @i in the range
3699 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3700 * holds that @p *j<*i is false.
3702 template<typename _RandomAccessIterator
>
3704 nth_element(_RandomAccessIterator __first
,
3705 _RandomAccessIterator __nth
,
3706 _RandomAccessIterator __last
)
3708 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3711 // concept requirements
3712 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3713 _RandomAccessIterator
>)
3714 __glibcxx_function_requires(_LessThanComparableConcept
<_ValueType
>)
3715 __glibcxx_requires_valid_range(__first
, __nth
);
3716 __glibcxx_requires_valid_range(__nth
, __last
);
3718 while (__last
- __first
> 3)
3720 _RandomAccessIterator __cut
=
3721 std::__unguarded_partition(__first
, __last
,
3722 _ValueType(std::__median(*__first
,
3734 std::__insertion_sort(__first
, __last
);
3738 * @brief Sort a sequence just enough to find a particular position
3739 * using a predicate for comparison.
3740 * @param first An iterator.
3741 * @param nth Another iterator.
3742 * @param last Another iterator.
3743 * @param comp A comparison functor.
3746 * Rearranges the elements in the range @p [first,last) so that @p *nth
3747 * is the same element that would have been in that position had the
3748 * whole sequence been sorted. The elements either side of @p *nth are
3749 * not completely sorted, but for any iterator @i in the range
3750 * @p [first,nth) and any iterator @j in the range @p [nth,last) it
3751 * holds that @p comp(*j,*i) is false.
3753 template<typename _RandomAccessIterator
, typename _Compare
>
3755 nth_element(_RandomAccessIterator __first
,
3756 _RandomAccessIterator __nth
,
3757 _RandomAccessIterator __last
,
3760 typedef typename iterator_traits
<_RandomAccessIterator
>::value_type
3763 // concept requirements
3764 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept
<
3765 _RandomAccessIterator
>)
3766 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3767 _ValueType
, _ValueType
>)
3768 __glibcxx_requires_valid_range(__first
, __nth
);
3769 __glibcxx_requires_valid_range(__nth
, __last
);
3771 while (__last
- __first
> 3)
3773 _RandomAccessIterator __cut
=
3774 std::__unguarded_partition(__first
, __last
,
3775 _ValueType(std::__median(*__first
,
3787 std::__insertion_sort(__first
, __last
, __comp
);
3791 * @brief Finds the largest subrange in which @a val could be inserted
3792 * at any place in it without changing the ordering.
3793 * @param first An iterator.
3794 * @param last Another iterator.
3795 * @param val The search term.
3796 * @return An pair of iterators defining the subrange.
3797 * @ingroup binarysearch
3799 * This is equivalent to
3801 * std::make_pair(lower_bound(first, last, val),
3802 * upper_bound(first, last, val))
3804 * but does not actually call those functions.
3806 template<typename _ForwardIterator
, typename _Tp
>
3807 pair
<_ForwardIterator
, _ForwardIterator
>
3808 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
3811 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3813 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3816 // concept requirements
3817 // See comments on lower_bound.
3818 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3819 __glibcxx_function_requires(_SameTypeConcept
<_Tp
, _ValueType
>)
3820 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3821 __glibcxx_requires_partitioned(__first
, __last
, __val
);
3823 _DistanceType __len
= std::distance(__first
, __last
);
3824 _DistanceType __half
;
3825 _ForwardIterator __middle
, __left
, __right
;
3829 __half
= __len
>> 1;
3831 std::advance(__middle
, __half
);
3832 if (*__middle
< __val
)
3836 __len
= __len
- __half
- 1;
3838 else if (__val
< *__middle
)
3842 __left
= std::lower_bound(__first
, __middle
, __val
);
3843 std::advance(__first
, __len
);
3844 __right
= std::upper_bound(++__middle
, __first
, __val
);
3845 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
3848 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
3852 * @brief Finds the largest subrange in which @a val could be inserted
3853 * at any place in it without changing the ordering.
3854 * @param first An iterator.
3855 * @param last Another iterator.
3856 * @param val The search term.
3857 * @param comp A functor to use for comparisons.
3858 * @return An pair of iterators defining the subrange.
3859 * @ingroup binarysearch
3861 * This is equivalent to
3863 * std::make_pair(lower_bound(first, last, val, comp),
3864 * upper_bound(first, last, val, comp))
3866 * but does not actually call those functions.
3868 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3869 pair
<_ForwardIterator
, _ForwardIterator
>
3870 equal_range(_ForwardIterator __first
, _ForwardIterator __last
,
3874 typedef typename iterator_traits
<_ForwardIterator
>::value_type
3876 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
3879 // concept requirements
3880 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3881 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3883 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3885 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
3887 _DistanceType __len
= std::distance(__first
, __last
);
3888 _DistanceType __half
;
3889 _ForwardIterator __middle
, __left
, __right
;
3893 __half
= __len
>> 1;
3895 std::advance(__middle
, __half
);
3896 if (__comp(*__middle
, __val
))
3900 __len
= __len
- __half
- 1;
3902 else if (__comp(__val
, *__middle
))
3906 __left
= std::lower_bound(__first
, __middle
, __val
, __comp
);
3907 std::advance(__first
, __len
);
3908 __right
= std::upper_bound(++__middle
, __first
, __val
, __comp
);
3909 return pair
<_ForwardIterator
, _ForwardIterator
>(__left
, __right
);
3912 return pair
<_ForwardIterator
, _ForwardIterator
>(__first
, __first
);
3916 * @brief Determines whether an element exists in a range.
3917 * @param first An iterator.
3918 * @param last Another iterator.
3919 * @param val The search term.
3920 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
3921 * @ingroup binarysearch
3923 * Note that this does not actually return an iterator to @a val. For
3924 * that, use std::find or a container's specialized find member functions.
3926 template<typename _ForwardIterator
, typename _Tp
>
3928 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
3931 // concept requirements
3932 // See comments on lower_bound.
3933 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3934 __glibcxx_function_requires(_SameTypeConcept
<_Tp
,
3935 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3936 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
3937 __glibcxx_requires_partitioned(__first
, __last
, __val
);
3939 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
);
3940 return __i
!= __last
&& !(__val
< *__i
);
3944 * @brief Determines whether an element exists in a range.
3945 * @param first An iterator.
3946 * @param last Another iterator.
3947 * @param val The search term.
3948 * @param comp A functor to use for comparisons.
3949 * @return True if @a val (or its equivelent) is in [@a first,@a last ].
3950 * @ingroup binarysearch
3952 * Note that this does not actually return an iterator to @a val. For
3953 * that, use std::find or a container's specialized find member functions.
3955 * The comparison function should have the same effects on ordering as
3956 * the function used for the initial sort.
3958 template<typename _ForwardIterator
, typename _Tp
, typename _Compare
>
3960 binary_search(_ForwardIterator __first
, _ForwardIterator __last
,
3961 const _Tp
& __val
, _Compare __comp
)
3963 // concept requirements
3964 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
3965 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
3966 typename iterator_traits
<_ForwardIterator
>::value_type
, _Tp
>)
3967 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
, _Tp
,
3968 typename iterator_traits
<_ForwardIterator
>::value_type
>)
3969 __glibcxx_requires_partitioned_pred(__first
, __last
, __val
, __comp
);
3971 _ForwardIterator __i
= std::lower_bound(__first
, __last
, __val
, __comp
);
3972 return __i
!= __last
&& !__comp(__val
, *__i
);
3975 // Set algorithms: includes, set_union, set_intersection, set_difference,
3976 // set_symmetric_difference. All of these algorithms have the precondition
3977 // that their input ranges are sorted and the postcondition that their output
3978 // ranges are sorted.
3981 * @brief Determines whether all elements of a sequence exists in a range.
3982 * @param first1 Start of search range.
3983 * @param last1 End of search range.
3984 * @param first2 Start of sequence
3985 * @param last2 End of sequence.
3986 * @return True if each element in [first2,last2) is contained in order
3987 * within [first1,last1). False otherwise.
3988 * @ingroup setoperations
3990 * This operation expects both [first1,last1) and [first2,last2) to be
3991 * sorted. Searches for the presence of each element in [first2,last2)
3992 * within [first1,last1). The iterators over each range only move forward,
3993 * so this is a linear algorithm. If an element in [first2,last2) is not
3994 * found before the search iterator reaches @a last2, false is returned.
3996 template<typename _InputIterator1
, typename _InputIterator2
>
3998 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
3999 _InputIterator2 __first2
, _InputIterator2 __last2
)
4001 // concept requirements
4002 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4003 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4004 __glibcxx_function_requires(_SameTypeConcept
<
4005 typename iterator_traits
<_InputIterator1
>::value_type
,
4006 typename iterator_traits
<_InputIterator2
>::value_type
>)
4007 __glibcxx_function_requires(_LessThanComparableConcept
<
4008 typename iterator_traits
<_InputIterator1
>::value_type
>)
4009 __glibcxx_requires_sorted(__first1
, __last1
);
4010 __glibcxx_requires_sorted(__first2
, __last2
);
4012 while (__first1
!= __last1
&& __first2
!= __last2
)
4013 if (*__first2
< *__first1
)
4015 else if(*__first1
< *__first2
)
4018 ++__first1
, ++__first2
;
4020 return __first2
== __last2
;
4024 * @brief Determines whether all elements of a sequence exists in a range
4026 * @param first1 Start of search range.
4027 * @param last1 End of search range.
4028 * @param first2 Start of sequence
4029 * @param last2 End of sequence.
4030 * @param comp Comparison function to use.
4031 * @return True if each element in [first2,last2) is contained in order
4032 * within [first1,last1) according to comp. False otherwise.
4033 * @ingroup setoperations
4035 * This operation expects both [first1,last1) and [first2,last2) to be
4036 * sorted. Searches for the presence of each element in [first2,last2)
4037 * within [first1,last1), using comp to decide. The iterators over each
4038 * range only move forward, so this is a linear algorithm. If an element
4039 * in [first2,last2) is not found before the search iterator reaches @a
4040 * last2, false is returned.
4042 template<typename _InputIterator1
, typename _InputIterator2
,
4045 includes(_InputIterator1 __first1
, _InputIterator1 __last1
,
4046 _InputIterator2 __first2
, _InputIterator2 __last2
, _Compare __comp
)
4048 // concept requirements
4049 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4050 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4051 __glibcxx_function_requires(_SameTypeConcept
<
4052 typename iterator_traits
<_InputIterator1
>::value_type
,
4053 typename iterator_traits
<_InputIterator2
>::value_type
>)
4054 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4055 typename iterator_traits
<_InputIterator1
>::value_type
,
4056 typename iterator_traits
<_InputIterator2
>::value_type
>)
4057 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4058 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4060 while (__first1
!= __last1
&& __first2
!= __last2
)
4061 if (__comp(*__first2
, *__first1
))
4063 else if(__comp(*__first1
, *__first2
))
4066 ++__first1
, ++__first2
;
4068 return __first2
== __last2
;
4072 * @brief Return the union of two sorted ranges.
4073 * @param first1 Start of first range.
4074 * @param last1 End of first range.
4075 * @param first2 Start of second range.
4076 * @param last2 End of second range.
4077 * @return End of the output range.
4078 * @ingroup setoperations
4080 * This operation iterates over both ranges, copying elements present in
4081 * each range in order to the output range. Iterators increment for each
4082 * range. When the current element of one range is less than the other,
4083 * that element is copied and the iterator advanced. If an element is
4084 * contained in both ranges, the element from the first range is copied and
4085 * both ranges advance. The output range may not overlap either input
4088 template<typename _InputIterator1
, typename _InputIterator2
,
4089 typename _OutputIterator
>
4091 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4092 _InputIterator2 __first2
, _InputIterator2 __last2
,
4093 _OutputIterator __result
)
4095 // concept requirements
4096 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4097 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4098 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4099 typename iterator_traits
<_InputIterator1
>::value_type
>)
4100 __glibcxx_function_requires(_SameTypeConcept
<
4101 typename iterator_traits
<_InputIterator1
>::value_type
,
4102 typename iterator_traits
<_InputIterator2
>::value_type
>)
4103 __glibcxx_function_requires(_LessThanComparableConcept
<
4104 typename iterator_traits
<_InputIterator1
>::value_type
>)
4105 __glibcxx_requires_sorted(__first1
, __last1
);
4106 __glibcxx_requires_sorted(__first2
, __last2
);
4108 while (__first1
!= __last1
&& __first2
!= __last2
)
4110 if (*__first1
< *__first2
)
4112 *__result
= *__first1
;
4115 else if (*__first2
< *__first1
)
4117 *__result
= *__first2
;
4122 *__result
= *__first1
;
4128 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4133 * @brief Return the union of two sorted ranges using a comparison functor.
4134 * @param first1 Start of first range.
4135 * @param last1 End of first range.
4136 * @param first2 Start of second range.
4137 * @param last2 End of second range.
4138 * @param comp The comparison functor.
4139 * @return End of the output range.
4140 * @ingroup setoperations
4142 * This operation iterates over both ranges, copying elements present in
4143 * each range in order to the output range. Iterators increment for each
4144 * range. When the current element of one range is less than the other
4145 * according to @a comp, that element is copied and the iterator advanced.
4146 * If an equivalent element according to @a comp is contained in both
4147 * ranges, the element from the first range is copied and both ranges
4148 * advance. The output range may not overlap either input range.
4150 template<typename _InputIterator1
, typename _InputIterator2
,
4151 typename _OutputIterator
, typename _Compare
>
4153 set_union(_InputIterator1 __first1
, _InputIterator1 __last1
,
4154 _InputIterator2 __first2
, _InputIterator2 __last2
,
4155 _OutputIterator __result
, _Compare __comp
)
4157 // concept requirements
4158 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4159 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4160 __glibcxx_function_requires(_SameTypeConcept
<
4161 typename iterator_traits
<_InputIterator1
>::value_type
,
4162 typename iterator_traits
<_InputIterator2
>::value_type
>)
4163 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4164 typename iterator_traits
<_InputIterator1
>::value_type
>)
4165 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4166 typename iterator_traits
<_InputIterator1
>::value_type
,
4167 typename iterator_traits
<_InputIterator2
>::value_type
>)
4168 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4169 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4171 while (__first1
!= __last1
&& __first2
!= __last2
)
4173 if (__comp(*__first1
, *__first2
))
4175 *__result
= *__first1
;
4178 else if (__comp(*__first2
, *__first1
))
4180 *__result
= *__first2
;
4185 *__result
= *__first1
;
4191 return std::copy(__first2
, __last2
, std::copy(__first1
, __last1
,
4196 * @brief Return the intersection of two sorted ranges.
4197 * @param first1 Start of first range.
4198 * @param last1 End of first range.
4199 * @param first2 Start of second range.
4200 * @param last2 End of second range.
4201 * @return End of the output range.
4202 * @ingroup setoperations
4204 * This operation iterates over both ranges, copying elements present in
4205 * both ranges in order to the output range. Iterators increment for each
4206 * range. When the current element of one range is less than the other,
4207 * that iterator advances. If an element is contained in both ranges, the
4208 * element from the first range is copied and both ranges advance. The
4209 * output range may not overlap either input range.
4211 template<typename _InputIterator1
, typename _InputIterator2
,
4212 typename _OutputIterator
>
4214 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
4215 _InputIterator2 __first2
, _InputIterator2 __last2
,
4216 _OutputIterator __result
)
4218 // concept requirements
4219 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4220 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4221 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4222 typename iterator_traits
<_InputIterator1
>::value_type
>)
4223 __glibcxx_function_requires(_SameTypeConcept
<
4224 typename iterator_traits
<_InputIterator1
>::value_type
,
4225 typename iterator_traits
<_InputIterator2
>::value_type
>)
4226 __glibcxx_function_requires(_LessThanComparableConcept
<
4227 typename iterator_traits
<_InputIterator1
>::value_type
>)
4228 __glibcxx_requires_sorted(__first1
, __last1
);
4229 __glibcxx_requires_sorted(__first2
, __last2
);
4231 while (__first1
!= __last1
&& __first2
!= __last2
)
4232 if (*__first1
< *__first2
)
4234 else if (*__first2
< *__first1
)
4238 *__result
= *__first1
;
4247 * @brief Return the intersection of two sorted ranges using comparison
4249 * @param first1 Start of first range.
4250 * @param last1 End of first range.
4251 * @param first2 Start of second range.
4252 * @param last2 End of second range.
4253 * @param comp The comparison functor.
4254 * @return End of the output range.
4255 * @ingroup setoperations
4257 * This operation iterates over both ranges, copying elements present in
4258 * both ranges in order to the output range. Iterators increment for each
4259 * range. When the current element of one range is less than the other
4260 * according to @a comp, that iterator advances. If an element is
4261 * contained in both ranges according to @a comp, the element from the
4262 * first range is copied and both ranges advance. The output range may not
4263 * overlap either input range.
4265 template<typename _InputIterator1
, typename _InputIterator2
,
4266 typename _OutputIterator
, typename _Compare
>
4268 set_intersection(_InputIterator1 __first1
, _InputIterator1 __last1
,
4269 _InputIterator2 __first2
, _InputIterator2 __last2
,
4270 _OutputIterator __result
, _Compare __comp
)
4272 // concept requirements
4273 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4274 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4275 __glibcxx_function_requires(_SameTypeConcept
<
4276 typename iterator_traits
<_InputIterator1
>::value_type
,
4277 typename iterator_traits
<_InputIterator2
>::value_type
>)
4278 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4279 typename iterator_traits
<_InputIterator1
>::value_type
>)
4280 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4281 typename iterator_traits
<_InputIterator1
>::value_type
,
4282 typename iterator_traits
<_InputIterator2
>::value_type
>)
4283 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4284 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4286 while (__first1
!= __last1
&& __first2
!= __last2
)
4287 if (__comp(*__first1
, *__first2
))
4289 else if (__comp(*__first2
, *__first1
))
4293 *__result
= *__first1
;
4302 * @brief Return the difference of two sorted ranges.
4303 * @param first1 Start of first range.
4304 * @param last1 End of first range.
4305 * @param first2 Start of second range.
4306 * @param last2 End of second range.
4307 * @return End of the output range.
4308 * @ingroup setoperations
4310 * This operation iterates over both ranges, copying elements present in
4311 * the first range but not the second in order to the output range.
4312 * Iterators increment for each range. When the current element of the
4313 * first range is less than the second, that element is copied and the
4314 * iterator advances. If the current element of the second range is less,
4315 * the iterator advances, but no element is copied. If an element is
4316 * contained in both ranges, no elements are copied and both ranges
4317 * advance. The output range may not overlap either input range.
4319 template<typename _InputIterator1
, typename _InputIterator2
,
4320 typename _OutputIterator
>
4322 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4323 _InputIterator2 __first2
, _InputIterator2 __last2
,
4324 _OutputIterator __result
)
4326 // concept requirements
4327 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4328 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4329 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4330 typename iterator_traits
<_InputIterator1
>::value_type
>)
4331 __glibcxx_function_requires(_SameTypeConcept
<
4332 typename iterator_traits
<_InputIterator1
>::value_type
,
4333 typename iterator_traits
<_InputIterator2
>::value_type
>)
4334 __glibcxx_function_requires(_LessThanComparableConcept
<
4335 typename iterator_traits
<_InputIterator1
>::value_type
>)
4336 __glibcxx_requires_sorted(__first1
, __last1
);
4337 __glibcxx_requires_sorted(__first2
, __last2
);
4339 while (__first1
!= __last1
&& __first2
!= __last2
)
4340 if (*__first1
< *__first2
)
4342 *__result
= *__first1
;
4346 else if (*__first2
< *__first1
)
4353 return std::copy(__first1
, __last1
, __result
);
4357 * @brief Return the difference of two sorted ranges using comparison
4359 * @param first1 Start of first range.
4360 * @param last1 End of first range.
4361 * @param first2 Start of second range.
4362 * @param last2 End of second range.
4363 * @param comp The comparison functor.
4364 * @return End of the output range.
4365 * @ingroup setoperations
4367 * This operation iterates over both ranges, copying elements present in
4368 * the first range but not the second in order to the output range.
4369 * Iterators increment for each range. When the current element of the
4370 * first range is less than the second according to @a comp, that element
4371 * is copied and the iterator advances. If the current element of the
4372 * second range is less, no element is copied and the iterator advances.
4373 * If an element is contained in both ranges according to @a comp, no
4374 * elements are copied and both ranges advance. The output range may not
4375 * overlap either input range.
4377 template<typename _InputIterator1
, typename _InputIterator2
,
4378 typename _OutputIterator
, typename _Compare
>
4380 set_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4381 _InputIterator2 __first2
, _InputIterator2 __last2
,
4382 _OutputIterator __result
, _Compare __comp
)
4384 // concept requirements
4385 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4386 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4387 __glibcxx_function_requires(_SameTypeConcept
<
4388 typename iterator_traits
<_InputIterator1
>::value_type
,
4389 typename iterator_traits
<_InputIterator2
>::value_type
>)
4390 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4391 typename iterator_traits
<_InputIterator1
>::value_type
>)
4392 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4393 typename iterator_traits
<_InputIterator1
>::value_type
,
4394 typename iterator_traits
<_InputIterator2
>::value_type
>)
4395 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4396 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4398 while (__first1
!= __last1
&& __first2
!= __last2
)
4399 if (__comp(*__first1
, *__first2
))
4401 *__result
= *__first1
;
4405 else if (__comp(*__first2
, *__first1
))
4412 return std::copy(__first1
, __last1
, __result
);
4416 * @brief Return the symmetric difference of two sorted ranges.
4417 * @param first1 Start of first range.
4418 * @param last1 End of first range.
4419 * @param first2 Start of second range.
4420 * @param last2 End of second range.
4421 * @return End of the output range.
4422 * @ingroup setoperations
4424 * This operation iterates over both ranges, copying elements present in
4425 * one range but not the other in order to the output range. Iterators
4426 * increment for each range. When the current element of one range is less
4427 * than the other, that element is copied and the iterator advances. If an
4428 * element is contained in both ranges, no elements are copied and both
4429 * ranges advance. The output range may not overlap either input range.
4431 template<typename _InputIterator1
, typename _InputIterator2
,
4432 typename _OutputIterator
>
4434 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4435 _InputIterator2 __first2
, _InputIterator2 __last2
,
4436 _OutputIterator __result
)
4438 // concept requirements
4439 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4440 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4441 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4442 typename iterator_traits
<_InputIterator1
>::value_type
>)
4443 __glibcxx_function_requires(_SameTypeConcept
<
4444 typename iterator_traits
<_InputIterator1
>::value_type
,
4445 typename iterator_traits
<_InputIterator2
>::value_type
>)
4446 __glibcxx_function_requires(_LessThanComparableConcept
<
4447 typename iterator_traits
<_InputIterator1
>::value_type
>)
4448 __glibcxx_requires_sorted(__first1
, __last1
);
4449 __glibcxx_requires_sorted(__first2
, __last2
);
4451 while (__first1
!= __last1
&& __first2
!= __last2
)
4452 if (*__first1
< *__first2
)
4454 *__result
= *__first1
;
4458 else if (*__first2
< *__first1
)
4460 *__result
= *__first2
;
4469 return std::copy(__first2
, __last2
, std::copy(__first1
,
4470 __last1
, __result
));
4474 * @brief Return the symmetric difference of two sorted ranges using
4475 * comparison functor.
4476 * @param first1 Start of first range.
4477 * @param last1 End of first range.
4478 * @param first2 Start of second range.
4479 * @param last2 End of second range.
4480 * @param comp The comparison functor.
4481 * @return End of the output range.
4482 * @ingroup setoperations
4484 * This operation iterates over both ranges, copying elements present in
4485 * one range but not the other in order to the output range. Iterators
4486 * increment for each range. When the current element of one range is less
4487 * than the other according to @a comp, that element is copied and the
4488 * iterator advances. If an element is contained in both ranges according
4489 * to @a comp, no elements are copied and both ranges advance. The output
4490 * range may not overlap either input range.
4492 template<typename _InputIterator1
, typename _InputIterator2
,
4493 typename _OutputIterator
, typename _Compare
>
4495 set_symmetric_difference(_InputIterator1 __first1
, _InputIterator1 __last1
,
4496 _InputIterator2 __first2
, _InputIterator2 __last2
,
4497 _OutputIterator __result
,
4500 // concept requirements
4501 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
4502 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
4503 __glibcxx_function_requires(_SameTypeConcept
<
4504 typename iterator_traits
<_InputIterator1
>::value_type
,
4505 typename iterator_traits
<_InputIterator2
>::value_type
>)
4506 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
4507 typename iterator_traits
<_InputIterator1
>::value_type
>)
4508 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4509 typename iterator_traits
<_InputIterator1
>::value_type
,
4510 typename iterator_traits
<_InputIterator2
>::value_type
>)
4511 __glibcxx_requires_sorted_pred(__first1
, __last1
, __comp
);
4512 __glibcxx_requires_sorted_pred(__first2
, __last2
, __comp
);
4514 while (__first1
!= __last1
&& __first2
!= __last2
)
4515 if (__comp(*__first1
, *__first2
))
4517 *__result
= *__first1
;
4521 else if (__comp(*__first2
, *__first1
))
4523 *__result
= *__first2
;
4532 return std::copy(__first2
, __last2
, std::copy(__first1
,
4533 __last1
, __result
));
4536 // min_element and max_element, with and without an explicitly supplied
4537 // comparison function.
4540 * @brief Return the maximum element in a range.
4541 * @param first Start of range.
4542 * @param last End of range.
4543 * @return Iterator referencing the first instance of the largest value.
4545 template<typename _ForwardIterator
>
4547 max_element(_ForwardIterator __first
, _ForwardIterator __last
)
4549 // concept requirements
4550 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4551 __glibcxx_function_requires(_LessThanComparableConcept
<
4552 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4553 __glibcxx_requires_valid_range(__first
, __last
);
4555 if (__first
== __last
)
4557 _ForwardIterator __result
= __first
;
4558 while (++__first
!= __last
)
4559 if (*__result
< *__first
)
4565 * @brief Return the maximum element in a range using comparison functor.
4566 * @param first Start of range.
4567 * @param last End of range.
4568 * @param comp Comparison functor.
4569 * @return Iterator referencing the first instance of the largest value
4570 * according to comp.
4572 template<typename _ForwardIterator
, typename _Compare
>
4574 max_element(_ForwardIterator __first
, _ForwardIterator __last
,
4577 // concept requirements
4578 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4579 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4580 typename iterator_traits
<_ForwardIterator
>::value_type
,
4581 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4582 __glibcxx_requires_valid_range(__first
, __last
);
4584 if (__first
== __last
) return __first
;
4585 _ForwardIterator __result
= __first
;
4586 while (++__first
!= __last
)
4587 if (__comp(*__result
, *__first
)) __result
= __first
;
4592 * @brief Return the minimum element in a range.
4593 * @param first Start of range.
4594 * @param last End of range.
4595 * @return Iterator referencing the first instance of the smallest value.
4597 template<typename _ForwardIterator
>
4599 min_element(_ForwardIterator __first
, _ForwardIterator __last
)
4601 // concept requirements
4602 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4603 __glibcxx_function_requires(_LessThanComparableConcept
<
4604 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4605 __glibcxx_requires_valid_range(__first
, __last
);
4607 if (__first
== __last
)
4609 _ForwardIterator __result
= __first
;
4610 while (++__first
!= __last
)
4611 if (*__first
< *__result
)
4617 * @brief Return the minimum element in a range using comparison functor.
4618 * @param first Start of range.
4619 * @param last End of range.
4620 * @param comp Comparison functor.
4621 * @return Iterator referencing the first instance of the smallest value
4622 * according to comp.
4624 template<typename _ForwardIterator
, typename _Compare
>
4626 min_element(_ForwardIterator __first
, _ForwardIterator __last
,
4629 // concept requirements
4630 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4631 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4632 typename iterator_traits
<_ForwardIterator
>::value_type
,
4633 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4634 __glibcxx_requires_valid_range(__first
, __last
);
4636 if (__first
== __last
)
4638 _ForwardIterator __result
= __first
;
4639 while (++__first
!= __last
)
4640 if (__comp(*__first
, *__result
))
4645 // next_permutation and prev_permutation, with and without an explicitly
4646 // supplied comparison function.
4649 * @brief Permute range into the next "dictionary" ordering.
4650 * @param first Start of range.
4651 * @param last End of range.
4652 * @return False if wrapped to first permutation, true otherwise.
4654 * Treats all permutations of the range as a set of "dictionary" sorted
4655 * sequences. Permutes the current sequence into the next one of this set.
4656 * Returns true if there are more sequences to generate. If the sequence
4657 * is the largest of the set, the smallest is generated and false returned.
4659 template<typename _BidirectionalIterator
>
4661 next_permutation(_BidirectionalIterator __first
,
4662 _BidirectionalIterator __last
)
4664 // concept requirements
4665 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4666 _BidirectionalIterator
>)
4667 __glibcxx_function_requires(_LessThanComparableConcept
<
4668 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4669 __glibcxx_requires_valid_range(__first
, __last
);
4671 if (__first
== __last
)
4673 _BidirectionalIterator __i
= __first
;
4682 _BidirectionalIterator __ii
= __i
;
4686 _BidirectionalIterator __j
= __last
;
4687 while (!(*__i
< *--__j
))
4689 std::iter_swap(__i
, __j
);
4690 std::reverse(__ii
, __last
);
4695 std::reverse(__first
, __last
);
4702 * @brief Permute range into the next "dictionary" ordering using
4703 * comparison functor.
4704 * @param first Start of range.
4705 * @param last End of range.
4707 * @return False if wrapped to first permutation, true otherwise.
4709 * Treats all permutations of the range [first,last) as a set of
4710 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
4711 * sequence into the next one of this set. Returns true if there are more
4712 * sequences to generate. If the sequence is the largest of the set, the
4713 * smallest is generated and false returned.
4715 template<typename _BidirectionalIterator
, typename _Compare
>
4717 next_permutation(_BidirectionalIterator __first
,
4718 _BidirectionalIterator __last
, _Compare __comp
)
4720 // concept requirements
4721 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4722 _BidirectionalIterator
>)
4723 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4724 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
4725 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4726 __glibcxx_requires_valid_range(__first
, __last
);
4728 if (__first
== __last
)
4730 _BidirectionalIterator __i
= __first
;
4739 _BidirectionalIterator __ii
= __i
;
4741 if (__comp(*__i
, *__ii
))
4743 _BidirectionalIterator __j
= __last
;
4744 while (!__comp(*__i
, *--__j
))
4746 std::iter_swap(__i
, __j
);
4747 std::reverse(__ii
, __last
);
4752 std::reverse(__first
, __last
);
4759 * @brief Permute range into the previous "dictionary" ordering.
4760 * @param first Start of range.
4761 * @param last End of range.
4762 * @return False if wrapped to last permutation, true otherwise.
4764 * Treats all permutations of the range as a set of "dictionary" sorted
4765 * sequences. Permutes the current sequence into the previous one of this
4766 * set. Returns true if there are more sequences to generate. If the
4767 * sequence is the smallest of the set, the largest is generated and false
4770 template<typename _BidirectionalIterator
>
4772 prev_permutation(_BidirectionalIterator __first
,
4773 _BidirectionalIterator __last
)
4775 // concept requirements
4776 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4777 _BidirectionalIterator
>)
4778 __glibcxx_function_requires(_LessThanComparableConcept
<
4779 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4780 __glibcxx_requires_valid_range(__first
, __last
);
4782 if (__first
== __last
)
4784 _BidirectionalIterator __i
= __first
;
4793 _BidirectionalIterator __ii
= __i
;
4797 _BidirectionalIterator __j
= __last
;
4798 while (!(*--__j
< *__i
))
4800 std::iter_swap(__i
, __j
);
4801 std::reverse(__ii
, __last
);
4806 std::reverse(__first
, __last
);
4813 * @brief Permute range into the previous "dictionary" ordering using
4814 * comparison functor.
4815 * @param first Start of range.
4816 * @param last End of range.
4818 * @return False if wrapped to last permutation, true otherwise.
4820 * Treats all permutations of the range [first,last) as a set of
4821 * "dictionary" sorted sequences ordered by @a comp. Permutes the current
4822 * sequence into the previous one of this set. Returns true if there are
4823 * more sequences to generate. If the sequence is the smallest of the set,
4824 * the largest is generated and false returned.
4826 template<typename _BidirectionalIterator
, typename _Compare
>
4828 prev_permutation(_BidirectionalIterator __first
,
4829 _BidirectionalIterator __last
, _Compare __comp
)
4831 // concept requirements
4832 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
4833 _BidirectionalIterator
>)
4834 __glibcxx_function_requires(_BinaryPredicateConcept
<_Compare
,
4835 typename iterator_traits
<_BidirectionalIterator
>::value_type
,
4836 typename iterator_traits
<_BidirectionalIterator
>::value_type
>)
4837 __glibcxx_requires_valid_range(__first
, __last
);
4839 if (__first
== __last
)
4841 _BidirectionalIterator __i
= __first
;
4850 _BidirectionalIterator __ii
= __i
;
4852 if (__comp(*__ii
, *__i
))
4854 _BidirectionalIterator __j
= __last
;
4855 while (!__comp(*--__j
, *__i
))
4857 std::iter_swap(__i
, __j
);
4858 std::reverse(__ii
, __last
);
4863 std::reverse(__first
, __last
);
4869 // find_first_of, with and without an explicitly supplied comparison function.
4872 * @brief Find element from a set in a sequence.
4873 * @param first1 Start of range to search.
4874 * @param last1 End of range to search.
4875 * @param first2 Start of match candidates.
4876 * @param last2 End of match candidates.
4877 * @return The first iterator @c i in the range
4878 * @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
4879 * interator in [first2,last2), or @p last1 if no such iterator exists.
4881 * Searches the range @p [first1,last1) for an element that is equal to
4882 * some element in the range [first2,last2). If found, returns an iterator
4883 * in the range [first1,last1), otherwise returns @p last1.
4885 template<typename _InputIterator
, typename _ForwardIterator
>
4887 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4888 _ForwardIterator __first2
, _ForwardIterator __last2
)
4890 // concept requirements
4891 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4892 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4893 __glibcxx_function_requires(_EqualOpConcept
<
4894 typename iterator_traits
<_InputIterator
>::value_type
,
4895 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4896 __glibcxx_requires_valid_range(__first1
, __last1
);
4897 __glibcxx_requires_valid_range(__first2
, __last2
);
4899 for ( ; __first1
!= __last1
; ++__first1
)
4900 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4901 if (*__first1
== *__iter
)
4907 * @brief Find element from a set in a sequence using a predicate.
4908 * @param first1 Start of range to search.
4909 * @param last1 End of range to search.
4910 * @param first2 Start of match candidates.
4911 * @param last2 End of match candidates.
4912 * @param comp Predicate to use.
4913 * @return The first iterator @c i in the range
4914 * @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
4915 * interator in [first2,last2), or @p last1 if no such iterator exists.
4917 * Searches the range @p [first1,last1) for an element that is equal to
4918 * some element in the range [first2,last2). If found, returns an iterator in
4919 * the range [first1,last1), otherwise returns @p last1.
4921 template<typename _InputIterator
, typename _ForwardIterator
,
4922 typename _BinaryPredicate
>
4924 find_first_of(_InputIterator __first1
, _InputIterator __last1
,
4925 _ForwardIterator __first2
, _ForwardIterator __last2
,
4926 _BinaryPredicate __comp
)
4928 // concept requirements
4929 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
4930 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
4931 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
4932 typename iterator_traits
<_InputIterator
>::value_type
,
4933 typename iterator_traits
<_ForwardIterator
>::value_type
>)
4934 __glibcxx_requires_valid_range(__first1
, __last1
);
4935 __glibcxx_requires_valid_range(__first2
, __last2
);
4937 for ( ; __first1
!= __last1
; ++__first1
)
4938 for (_ForwardIterator __iter
= __first2
; __iter
!= __last2
; ++__iter
)
4939 if (__comp(*__first1
, *__iter
))
4945 // find_end, with and without an explicitly supplied comparison function.
4946 // Search [first2, last2) as a subsequence in [first1, last1), and return
4947 // the *last* possible match. Note that find_end for bidirectional iterators
4948 // is much faster than for forward iterators.
4950 // find_end for forward iterators.
4951 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
4953 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4954 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4955 forward_iterator_tag
, forward_iterator_tag
)
4957 if (__first2
== __last2
)
4961 _ForwardIterator1 __result
= __last1
;
4964 _ForwardIterator1 __new_result
4965 = std::search(__first1
, __last1
, __first2
, __last2
);
4966 if (__new_result
== __last1
)
4970 __result
= __new_result
;
4971 __first1
= __new_result
;
4978 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
4979 typename _BinaryPredicate
>
4981 __find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
4982 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
4983 forward_iterator_tag
, forward_iterator_tag
,
4984 _BinaryPredicate __comp
)
4986 if (__first2
== __last2
)
4990 _ForwardIterator1 __result
= __last1
;
4993 _ForwardIterator1 __new_result
4994 = std::search(__first1
, __last1
, __first2
, __last2
, __comp
);
4995 if (__new_result
== __last1
)
4999 __result
= __new_result
;
5000 __first1
= __new_result
;
5007 // find_end for bidirectional iterators. Requires partial specialization.
5008 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
5009 _BidirectionalIterator1
5010 __find_end(_BidirectionalIterator1 __first1
,
5011 _BidirectionalIterator1 __last1
,
5012 _BidirectionalIterator2 __first2
,
5013 _BidirectionalIterator2 __last2
,
5014 bidirectional_iterator_tag
, bidirectional_iterator_tag
)
5016 // concept requirements
5017 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5018 _BidirectionalIterator1
>)
5019 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5020 _BidirectionalIterator2
>)
5022 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
5023 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
5025 _RevIterator1
__rlast1(__first1
);
5026 _RevIterator2
__rlast2(__first2
);
5027 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
5028 _RevIterator2(__last2
), __rlast2
);
5030 if (__rresult
== __rlast1
)
5034 _BidirectionalIterator1 __result
= __rresult
.base();
5035 std::advance(__result
, -std::distance(__first2
, __last2
));
5040 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
5041 typename _BinaryPredicate
>
5042 _BidirectionalIterator1
5043 __find_end(_BidirectionalIterator1 __first1
,
5044 _BidirectionalIterator1 __last1
,
5045 _BidirectionalIterator2 __first2
,
5046 _BidirectionalIterator2 __last2
,
5047 bidirectional_iterator_tag
, bidirectional_iterator_tag
,
5048 _BinaryPredicate __comp
)
5050 // concept requirements
5051 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5052 _BidirectionalIterator1
>)
5053 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
5054 _BidirectionalIterator2
>)
5056 typedef reverse_iterator
<_BidirectionalIterator1
> _RevIterator1
;
5057 typedef reverse_iterator
<_BidirectionalIterator2
> _RevIterator2
;
5059 _RevIterator1
__rlast1(__first1
);
5060 _RevIterator2
__rlast2(__first2
);
5061 _RevIterator1 __rresult
= std::search(_RevIterator1(__last1
), __rlast1
,
5062 _RevIterator2(__last2
), __rlast2
,
5065 if (__rresult
== __rlast1
)
5069 _BidirectionalIterator1 __result
= __rresult
.base();
5070 std::advance(__result
, -std::distance(__first2
, __last2
));
5075 // Dispatching functions for find_end.
5078 * @brief Find last matching subsequence in a sequence.
5079 * @param first1 Start of range to search.
5080 * @param last1 End of range to search.
5081 * @param first2 Start of sequence to match.
5082 * @param last2 End of sequence to match.
5083 * @return The last iterator @c i in the range
5084 * @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
5085 * for each @c N in the range @p [0,last2-first2), or @p last1 if no
5086 * such iterator exists.
5088 * Searches the range @p [first1,last1) for a sub-sequence that compares
5089 * equal value-by-value with the sequence given by @p [first2,last2) and
5090 * returns an iterator to the first element of the sub-sequence, or
5091 * @p last1 if the sub-sequence is not found. The sub-sequence will be the
5092 * last such subsequence contained in [first,last1).
5094 * Because the sub-sequence must lie completely within the range
5095 * @p [first1,last1) it must start at a position less than
5096 * @p last1-(last2-first2) where @p last2-first2 is the length of the
5098 * This means that the returned iterator @c i will be in the range
5099 * @p [first1,last1-(last2-first2))
5101 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
5102 inline _ForwardIterator1
5103 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
5104 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
)
5106 // concept requirements
5107 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
5108 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
5109 __glibcxx_function_requires(_EqualOpConcept
<
5110 typename iterator_traits
<_ForwardIterator1
>::value_type
,
5111 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
5112 __glibcxx_requires_valid_range(__first1
, __last1
);
5113 __glibcxx_requires_valid_range(__first2
, __last2
);
5115 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
5116 std::__iterator_category(__first1
),
5117 std::__iterator_category(__first2
));
5121 * @brief Find last matching subsequence in a sequence using a predicate.
5122 * @param first1 Start of range to search.
5123 * @param last1 End of range to search.
5124 * @param first2 Start of sequence to match.
5125 * @param last2 End of sequence to match.
5126 * @param comp The predicate to use.
5127 * @return The last iterator @c i in the range
5128 * @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
5129 * (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
5130 * @p last1 if no such iterator exists.
5132 * Searches the range @p [first1,last1) for a sub-sequence that compares
5133 * equal value-by-value with the sequence given by @p [first2,last2) using
5134 * comp as a predicate and returns an iterator to the first element of the
5135 * sub-sequence, or @p last1 if the sub-sequence is not found. The
5136 * sub-sequence will be the last such subsequence contained in
5139 * Because the sub-sequence must lie completely within the range
5140 * @p [first1,last1) it must start at a position less than
5141 * @p last1-(last2-first2) where @p last2-first2 is the length of the
5143 * This means that the returned iterator @c i will be in the range
5144 * @p [first1,last1-(last2-first2))
5146 template<typename _ForwardIterator1
, typename _ForwardIterator2
,
5147 typename _BinaryPredicate
>
5148 inline _ForwardIterator1
5149 find_end(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
5150 _ForwardIterator2 __first2
, _ForwardIterator2 __last2
,
5151 _BinaryPredicate __comp
)
5153 // concept requirements
5154 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator1
>)
5155 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator2
>)
5156 __glibcxx_function_requires(_BinaryPredicateConcept
<_BinaryPredicate
,
5157 typename iterator_traits
<_ForwardIterator1
>::value_type
,
5158 typename iterator_traits
<_ForwardIterator2
>::value_type
>)
5159 __glibcxx_requires_valid_range(__first1
, __last1
);
5160 __glibcxx_requires_valid_range(__first2
, __last2
);
5162 return std::__find_end(__first1
, __last1
, __first2
, __last2
,
5163 std::__iterator_category(__first1
),
5164 std::__iterator_category(__first2
),
5170 #endif /* _ALGO_H */